PR debug/84456
[official-gcc.git] / gcc / dwarf2out.c
blobc39910f26cb3ed08c01ca760642d561d6c746341
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're forcing the view number to be reset, e.g. at a
2944 function entry.
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 FORCE_RESET_NEXT_VIEW(x) ((x) = (var_loc_view)-1)
2955 #define RESET_NEXT_VIEW(x) ((x) = (var_loc_view)0)
2956 #define FORCE_RESETTING_VIEW_P(x) ((x) == (var_loc_view)-1)
2957 #define RESETTING_VIEW_P(x) ((x) == (var_loc_view)0 || FORCE_RESETTING_VIEW_P (x))
2959 vec<dw_line_info_entry, va_gc> *entries;
2962 /* If we're keep track of location views and their reset points, and
2963 INSN is a reset point (i.e., it necessarily advances the PC), mark
2964 the next view in TABLE as reset. */
2966 static void
2967 maybe_reset_location_view (rtx_insn *insn, dw_line_info_table *table)
2969 if (!debug_internal_reset_location_views)
2970 return;
2972 /* Maybe turn (part of?) this test into a default target hook. */
2973 int reset = 0;
2975 if (targetm.reset_location_view)
2976 reset = targetm.reset_location_view (insn);
2978 if (reset)
2980 else if (JUMP_TABLE_DATA_P (insn))
2981 reset = 1;
2982 else if (GET_CODE (insn) == USE
2983 || GET_CODE (insn) == CLOBBER
2984 || GET_CODE (insn) == ASM_INPUT
2985 || asm_noperands (insn) >= 0)
2987 else if (get_attr_min_length (insn) > 0)
2988 reset = 1;
2990 if (reset > 0 && !RESETTING_VIEW_P (table->view))
2991 RESET_NEXT_VIEW (table->view);
2994 /* Each DIE attribute has a field specifying the attribute kind,
2995 a link to the next attribute in the chain, and an attribute value.
2996 Attributes are typically linked below the DIE they modify. */
2998 typedef struct GTY(()) dw_attr_struct {
2999 enum dwarf_attribute dw_attr;
3000 dw_val_node dw_attr_val;
3002 dw_attr_node;
3005 /* The Debugging Information Entry (DIE) structure. DIEs form a tree.
3006 The children of each node form a circular list linked by
3007 die_sib. die_child points to the node *before* the "first" child node. */
3009 typedef struct GTY((chain_circular ("%h.die_sib"), for_user)) die_struct {
3010 union die_symbol_or_type_node
3012 const char * GTY ((tag ("0"))) die_symbol;
3013 comdat_type_node *GTY ((tag ("1"))) die_type_node;
3015 GTY ((desc ("%0.comdat_type_p"))) die_id;
3016 vec<dw_attr_node, va_gc> *die_attr;
3017 dw_die_ref die_parent;
3018 dw_die_ref die_child;
3019 dw_die_ref die_sib;
3020 dw_die_ref die_definition; /* ref from a specification to its definition */
3021 dw_offset die_offset;
3022 unsigned long die_abbrev;
3023 int die_mark;
3024 unsigned int decl_id;
3025 enum dwarf_tag die_tag;
3026 /* Die is used and must not be pruned as unused. */
3027 BOOL_BITFIELD die_perennial_p : 1;
3028 BOOL_BITFIELD comdat_type_p : 1; /* DIE has a type signature */
3029 /* For an external ref to die_symbol if die_offset contains an extra
3030 offset to that symbol. */
3031 BOOL_BITFIELD with_offset : 1;
3032 /* Whether this DIE was removed from the DIE tree, for example via
3033 prune_unused_types. We don't consider those present from the
3034 DIE lookup routines. */
3035 BOOL_BITFIELD removed : 1;
3036 /* Lots of spare bits. */
3038 die_node;
3040 /* Set to TRUE while dwarf2out_early_global_decl is running. */
3041 static bool early_dwarf;
3042 static bool early_dwarf_finished;
3043 struct set_early_dwarf {
3044 bool saved;
3045 set_early_dwarf () : saved(early_dwarf)
3047 gcc_assert (! early_dwarf_finished);
3048 early_dwarf = true;
3050 ~set_early_dwarf () { early_dwarf = saved; }
3053 /* Evaluate 'expr' while 'c' is set to each child of DIE in order. */
3054 #define FOR_EACH_CHILD(die, c, expr) do { \
3055 c = die->die_child; \
3056 if (c) do { \
3057 c = c->die_sib; \
3058 expr; \
3059 } while (c != die->die_child); \
3060 } while (0)
3062 /* The pubname structure */
3064 typedef struct GTY(()) pubname_struct {
3065 dw_die_ref die;
3066 const char *name;
3068 pubname_entry;
3071 struct GTY(()) dw_ranges {
3072 const char *label;
3073 /* If this is positive, it's a block number, otherwise it's a
3074 bitwise-negated index into dw_ranges_by_label. */
3075 int num;
3076 /* Index for the range list for DW_FORM_rnglistx. */
3077 unsigned int idx : 31;
3078 /* True if this range might be possibly in a different section
3079 from previous entry. */
3080 unsigned int maybe_new_sec : 1;
3083 /* A structure to hold a macinfo entry. */
3085 typedef struct GTY(()) macinfo_struct {
3086 unsigned char code;
3087 unsigned HOST_WIDE_INT lineno;
3088 const char *info;
3090 macinfo_entry;
3093 struct GTY(()) dw_ranges_by_label {
3094 const char *begin;
3095 const char *end;
3098 /* The comdat type node structure. */
3099 struct GTY(()) comdat_type_node
3101 dw_die_ref root_die;
3102 dw_die_ref type_die;
3103 dw_die_ref skeleton_die;
3104 char signature[DWARF_TYPE_SIGNATURE_SIZE];
3105 comdat_type_node *next;
3108 /* A list of DIEs for which we can't determine ancestry (parent_die
3109 field) just yet. Later in dwarf2out_finish we will fill in the
3110 missing bits. */
3111 typedef struct GTY(()) limbo_die_struct {
3112 dw_die_ref die;
3113 /* The tree for which this DIE was created. We use this to
3114 determine ancestry later. */
3115 tree created_for;
3116 struct limbo_die_struct *next;
3118 limbo_die_node;
3120 typedef struct skeleton_chain_struct
3122 dw_die_ref old_die;
3123 dw_die_ref new_die;
3124 struct skeleton_chain_struct *parent;
3126 skeleton_chain_node;
3128 /* Define a macro which returns nonzero for a TYPE_DECL which was
3129 implicitly generated for a type.
3131 Note that, unlike the C front-end (which generates a NULL named
3132 TYPE_DECL node for each complete tagged type, each array type,
3133 and each function type node created) the C++ front-end generates
3134 a _named_ TYPE_DECL node for each tagged type node created.
3135 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
3136 generate a DW_TAG_typedef DIE for them. Likewise with the Ada
3137 front-end, but for each type, tagged or not. */
3139 #define TYPE_DECL_IS_STUB(decl) \
3140 (DECL_NAME (decl) == NULL_TREE \
3141 || (DECL_ARTIFICIAL (decl) \
3142 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
3143 /* This is necessary for stub decls that \
3144 appear in nested inline functions. */ \
3145 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
3146 && (decl_ultimate_origin (decl) \
3147 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
3149 /* Information concerning the compilation unit's programming
3150 language, and compiler version. */
3152 /* Fixed size portion of the DWARF compilation unit header. */
3153 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
3154 (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE \
3155 + (dwarf_version >= 5 ? 4 : 3))
3157 /* Fixed size portion of the DWARF comdat type unit header. */
3158 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
3159 (DWARF_COMPILE_UNIT_HEADER_SIZE \
3160 + DWARF_TYPE_SIGNATURE_SIZE + DWARF_OFFSET_SIZE)
3162 /* Fixed size portion of the DWARF skeleton compilation unit header. */
3163 #define DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE \
3164 (DWARF_COMPILE_UNIT_HEADER_SIZE + (dwarf_version >= 5 ? 8 : 0))
3166 /* Fixed size portion of public names info. */
3167 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
3169 /* Fixed size portion of the address range info. */
3170 #define DWARF_ARANGES_HEADER_SIZE \
3171 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
3172 DWARF2_ADDR_SIZE * 2) \
3173 - DWARF_INITIAL_LENGTH_SIZE)
3175 /* Size of padding portion in the address range info. It must be
3176 aligned to twice the pointer size. */
3177 #define DWARF_ARANGES_PAD_SIZE \
3178 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
3179 DWARF2_ADDR_SIZE * 2) \
3180 - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
3182 /* Use assembler line directives if available. */
3183 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
3184 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
3185 #define DWARF2_ASM_LINE_DEBUG_INFO 1
3186 #else
3187 #define DWARF2_ASM_LINE_DEBUG_INFO 0
3188 #endif
3189 #endif
3191 /* Use assembler views in line directives if available. */
3192 #ifndef DWARF2_ASM_VIEW_DEBUG_INFO
3193 #ifdef HAVE_AS_DWARF2_DEBUG_VIEW
3194 #define DWARF2_ASM_VIEW_DEBUG_INFO 1
3195 #else
3196 #define DWARF2_ASM_VIEW_DEBUG_INFO 0
3197 #endif
3198 #endif
3200 /* Return true if GCC configure detected assembler support for .loc. */
3202 bool
3203 dwarf2out_default_as_loc_support (void)
3205 return DWARF2_ASM_LINE_DEBUG_INFO;
3206 #if (GCC_VERSION >= 3000)
3207 # undef DWARF2_ASM_LINE_DEBUG_INFO
3208 # pragma GCC poison DWARF2_ASM_LINE_DEBUG_INFO
3209 #endif
3212 /* Return true if GCC configure detected assembler support for views
3213 in .loc directives. */
3215 bool
3216 dwarf2out_default_as_locview_support (void)
3218 return DWARF2_ASM_VIEW_DEBUG_INFO;
3219 #if (GCC_VERSION >= 3000)
3220 # undef DWARF2_ASM_VIEW_DEBUG_INFO
3221 # pragma GCC poison DWARF2_ASM_VIEW_DEBUG_INFO
3222 #endif
3225 /* A bit is set in ZERO_VIEW_P if we are using the assembler-supported
3226 view computation, and it refers to a view identifier for which we
3227 will not emit a label because it is known to map to a view number
3228 zero. We won't allocate the bitmap if we're not using assembler
3229 support for location views, but we have to make the variable
3230 visible for GGC and for code that will be optimized out for lack of
3231 support but that's still parsed and compiled. We could abstract it
3232 out with macros, but it's not worth it. */
3233 static GTY(()) bitmap zero_view_p;
3235 /* Evaluate to TRUE iff N is known to identify the first location view
3236 at its PC. When not using assembler location view computation,
3237 that must be view number zero. Otherwise, ZERO_VIEW_P is allocated
3238 and views label numbers recorded in it are the ones known to be
3239 zero. */
3240 #define ZERO_VIEW_P(N) ((N) == (var_loc_view)0 \
3241 || (N) == (var_loc_view)-1 \
3242 || (zero_view_p \
3243 && bitmap_bit_p (zero_view_p, (N))))
3245 /* Return true iff we're to emit .loc directives for the assembler to
3246 generate line number sections.
3248 When we're not emitting views, all we need from the assembler is
3249 support for .loc directives.
3251 If we are emitting views, we can only use the assembler's .loc
3252 support if it also supports views.
3254 When the compiler is emitting the line number programs and
3255 computing view numbers itself, it resets view numbers at known PC
3256 changes and counts from that, and then it emits view numbers as
3257 literal constants in locviewlists. There are cases in which the
3258 compiler is not sure about PC changes, e.g. when extra alignment is
3259 requested for a label. In these cases, the compiler may not reset
3260 the view counter, and the potential PC advance in the line number
3261 program will use an opcode that does not reset the view counter
3262 even if the PC actually changes, so that compiler and debug info
3263 consumer can keep view numbers in sync.
3265 When the compiler defers view computation to the assembler, it
3266 emits symbolic view numbers in locviewlists, with the exception of
3267 views known to be zero (forced resets, or reset after
3268 compiler-visible PC changes): instead of emitting symbols for
3269 these, we emit literal zero and assert the assembler agrees with
3270 the compiler's assessment. We could use symbolic views everywhere,
3271 instead of special-casing zero views, but then we'd be unable to
3272 optimize out locviewlists that contain only zeros. */
3274 static bool
3275 output_asm_line_debug_info (void)
3277 return (dwarf2out_as_loc_support
3278 && (dwarf2out_as_locview_support
3279 || !debug_variable_location_views));
3282 /* Minimum line offset in a special line info. opcode.
3283 This value was chosen to give a reasonable range of values. */
3284 #define DWARF_LINE_BASE -10
3286 /* First special line opcode - leave room for the standard opcodes. */
3287 #define DWARF_LINE_OPCODE_BASE ((int)DW_LNS_set_isa + 1)
3289 /* Range of line offsets in a special line info. opcode. */
3290 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
3292 /* Flag that indicates the initial value of the is_stmt_start flag.
3293 In the present implementation, we do not mark any lines as
3294 the beginning of a source statement, because that information
3295 is not made available by the GCC front-end. */
3296 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
3298 /* Maximum number of operations per instruction bundle. */
3299 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
3300 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
3301 #endif
3303 /* This location is used by calc_die_sizes() to keep track
3304 the offset of each DIE within the .debug_info section. */
3305 static unsigned long next_die_offset;
3307 /* Record the root of the DIE's built for the current compilation unit. */
3308 static GTY(()) dw_die_ref single_comp_unit_die;
3310 /* A list of type DIEs that have been separated into comdat sections. */
3311 static GTY(()) comdat_type_node *comdat_type_list;
3313 /* A list of CU DIEs that have been separated. */
3314 static GTY(()) limbo_die_node *cu_die_list;
3316 /* A list of DIEs with a NULL parent waiting to be relocated. */
3317 static GTY(()) limbo_die_node *limbo_die_list;
3319 /* A list of DIEs for which we may have to generate
3320 DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set. */
3321 static GTY(()) limbo_die_node *deferred_asm_name;
3323 struct dwarf_file_hasher : ggc_ptr_hash<dwarf_file_data>
3325 typedef const char *compare_type;
3327 static hashval_t hash (dwarf_file_data *);
3328 static bool equal (dwarf_file_data *, const char *);
3331 /* Filenames referenced by this compilation unit. */
3332 static GTY(()) hash_table<dwarf_file_hasher> *file_table;
3334 struct decl_die_hasher : ggc_ptr_hash<die_node>
3336 typedef tree compare_type;
3338 static hashval_t hash (die_node *);
3339 static bool equal (die_node *, tree);
3341 /* A hash table of references to DIE's that describe declarations.
3342 The key is a DECL_UID() which is a unique number identifying each decl. */
3343 static GTY (()) hash_table<decl_die_hasher> *decl_die_table;
3345 struct GTY ((for_user)) variable_value_struct {
3346 unsigned int decl_id;
3347 vec<dw_die_ref, va_gc> *dies;
3350 struct variable_value_hasher : ggc_ptr_hash<variable_value_struct>
3352 typedef tree compare_type;
3354 static hashval_t hash (variable_value_struct *);
3355 static bool equal (variable_value_struct *, tree);
3357 /* A hash table of DIEs that contain DW_OP_GNU_variable_value with
3358 dw_val_class_decl_ref class, indexed by FUNCTION_DECLs which is
3359 DECL_CONTEXT of the referenced VAR_DECLs. */
3360 static GTY (()) hash_table<variable_value_hasher> *variable_value_hash;
3362 struct block_die_hasher : ggc_ptr_hash<die_struct>
3364 static hashval_t hash (die_struct *);
3365 static bool equal (die_struct *, die_struct *);
3368 /* A hash table of references to DIE's that describe COMMON blocks.
3369 The key is DECL_UID() ^ die_parent. */
3370 static GTY (()) hash_table<block_die_hasher> *common_block_die_table;
3372 typedef struct GTY(()) die_arg_entry_struct {
3373 dw_die_ref die;
3374 tree arg;
3375 } die_arg_entry;
3378 /* Node of the variable location list. */
3379 struct GTY ((chain_next ("%h.next"))) var_loc_node {
3380 /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
3381 EXPR_LIST chain. For small bitsizes, bitsize is encoded
3382 in mode of the EXPR_LIST node and first EXPR_LIST operand
3383 is either NOTE_INSN_VAR_LOCATION for a piece with a known
3384 location or NULL for padding. For larger bitsizes,
3385 mode is 0 and first operand is a CONCAT with bitsize
3386 as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
3387 NULL as second operand. */
3388 rtx GTY (()) loc;
3389 const char * GTY (()) label;
3390 struct var_loc_node * GTY (()) next;
3391 var_loc_view view;
3394 /* Variable location list. */
3395 struct GTY ((for_user)) var_loc_list_def {
3396 struct var_loc_node * GTY (()) first;
3398 /* Pointer to the last but one or last element of the
3399 chained list. If the list is empty, both first and
3400 last are NULL, if the list contains just one node
3401 or the last node certainly is not redundant, it points
3402 to the last node, otherwise points to the last but one.
3403 Do not mark it for GC because it is marked through the chain. */
3404 struct var_loc_node * GTY ((skip ("%h"))) last;
3406 /* Pointer to the last element before section switch,
3407 if NULL, either sections weren't switched or first
3408 is after section switch. */
3409 struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
3411 /* DECL_UID of the variable decl. */
3412 unsigned int decl_id;
3414 typedef struct var_loc_list_def var_loc_list;
3416 /* Call argument location list. */
3417 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
3418 rtx GTY (()) call_arg_loc_note;
3419 const char * GTY (()) label;
3420 tree GTY (()) block;
3421 bool tail_call_p;
3422 rtx GTY (()) symbol_ref;
3423 struct call_arg_loc_node * GTY (()) next;
3427 struct decl_loc_hasher : ggc_ptr_hash<var_loc_list>
3429 typedef const_tree compare_type;
3431 static hashval_t hash (var_loc_list *);
3432 static bool equal (var_loc_list *, const_tree);
3435 /* Table of decl location linked lists. */
3436 static GTY (()) hash_table<decl_loc_hasher> *decl_loc_table;
3438 /* Head and tail of call_arg_loc chain. */
3439 static GTY (()) struct call_arg_loc_node *call_arg_locations;
3440 static struct call_arg_loc_node *call_arg_loc_last;
3442 /* Number of call sites in the current function. */
3443 static int call_site_count = -1;
3444 /* Number of tail call sites in the current function. */
3445 static int tail_call_site_count = -1;
3447 /* A cached location list. */
3448 struct GTY ((for_user)) cached_dw_loc_list_def {
3449 /* The DECL_UID of the decl that this entry describes. */
3450 unsigned int decl_id;
3452 /* The cached location list. */
3453 dw_loc_list_ref loc_list;
3455 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
3457 struct dw_loc_list_hasher : ggc_ptr_hash<cached_dw_loc_list>
3460 typedef const_tree compare_type;
3462 static hashval_t hash (cached_dw_loc_list *);
3463 static bool equal (cached_dw_loc_list *, const_tree);
3466 /* Table of cached location lists. */
3467 static GTY (()) hash_table<dw_loc_list_hasher> *cached_dw_loc_list_table;
3469 /* A vector of references to DIE's that are uniquely identified by their tag,
3470 presence/absence of children DIE's, and list of attribute/value pairs. */
3471 static GTY(()) vec<dw_die_ref, va_gc> *abbrev_die_table;
3473 /* A hash map to remember the stack usage for DWARF procedures. The value
3474 stored is the stack size difference between before the DWARF procedure
3475 invokation and after it returned. In other words, for a DWARF procedure
3476 that consumes N stack slots and that pushes M ones, this stores M - N. */
3477 static hash_map<dw_die_ref, int> *dwarf_proc_stack_usage_map;
3479 /* A global counter for generating labels for line number data. */
3480 static unsigned int line_info_label_num;
3482 /* The current table to which we should emit line number information
3483 for the current function. This will be set up at the beginning of
3484 assembly for the function. */
3485 static GTY(()) dw_line_info_table *cur_line_info_table;
3487 /* The two default tables of line number info. */
3488 static GTY(()) dw_line_info_table *text_section_line_info;
3489 static GTY(()) dw_line_info_table *cold_text_section_line_info;
3491 /* The set of all non-default tables of line number info. */
3492 static GTY(()) vec<dw_line_info_table *, va_gc> *separate_line_info;
3494 /* A flag to tell pubnames/types export if there is an info section to
3495 refer to. */
3496 static bool info_section_emitted;
3498 /* A pointer to the base of a table that contains a list of publicly
3499 accessible names. */
3500 static GTY (()) vec<pubname_entry, va_gc> *pubname_table;
3502 /* A pointer to the base of a table that contains a list of publicly
3503 accessible types. */
3504 static GTY (()) vec<pubname_entry, va_gc> *pubtype_table;
3506 /* A pointer to the base of a table that contains a list of macro
3507 defines/undefines (and file start/end markers). */
3508 static GTY (()) vec<macinfo_entry, va_gc> *macinfo_table;
3510 /* True if .debug_macinfo or .debug_macros section is going to be
3511 emitted. */
3512 #define have_macinfo \
3513 ((!XCOFF_DEBUGGING_INFO || HAVE_XCOFF_DWARF_EXTRAS) \
3514 && debug_info_level >= DINFO_LEVEL_VERBOSE \
3515 && !macinfo_table->is_empty ())
3517 /* Vector of dies for which we should generate .debug_ranges info. */
3518 static GTY (()) vec<dw_ranges, va_gc> *ranges_table;
3520 /* Vector of pairs of labels referenced in ranges_table. */
3521 static GTY (()) vec<dw_ranges_by_label, va_gc> *ranges_by_label;
3523 /* Whether we have location lists that need outputting */
3524 static GTY(()) bool have_location_lists;
3526 /* Unique label counter. */
3527 static GTY(()) unsigned int loclabel_num;
3529 /* Unique label counter for point-of-call tables. */
3530 static GTY(()) unsigned int poc_label_num;
3532 /* The last file entry emitted by maybe_emit_file(). */
3533 static GTY(()) struct dwarf_file_data * last_emitted_file;
3535 /* Number of internal labels generated by gen_internal_sym(). */
3536 static GTY(()) int label_num;
3538 static GTY(()) vec<die_arg_entry, va_gc> *tmpl_value_parm_die_table;
3540 /* Instances of generic types for which we need to generate debug
3541 info that describe their generic parameters and arguments. That
3542 generation needs to happen once all types are properly laid out so
3543 we do it at the end of compilation. */
3544 static GTY(()) vec<tree, va_gc> *generic_type_instances;
3546 /* Offset from the "steady-state frame pointer" to the frame base,
3547 within the current function. */
3548 static poly_int64 frame_pointer_fb_offset;
3549 static bool frame_pointer_fb_offset_valid;
3551 static vec<dw_die_ref> base_types;
3553 /* Flags to represent a set of attribute classes for attributes that represent
3554 a scalar value (bounds, pointers, ...). */
3555 enum dw_scalar_form
3557 dw_scalar_form_constant = 0x01,
3558 dw_scalar_form_exprloc = 0x02,
3559 dw_scalar_form_reference = 0x04
3562 /* Forward declarations for functions defined in this file. */
3564 static int is_pseudo_reg (const_rtx);
3565 static tree type_main_variant (tree);
3566 static int is_tagged_type (const_tree);
3567 static const char *dwarf_tag_name (unsigned);
3568 static const char *dwarf_attr_name (unsigned);
3569 static const char *dwarf_form_name (unsigned);
3570 static tree decl_ultimate_origin (const_tree);
3571 static tree decl_class_context (tree);
3572 static void add_dwarf_attr (dw_die_ref, dw_attr_node *);
3573 static inline enum dw_val_class AT_class (dw_attr_node *);
3574 static inline unsigned int AT_index (dw_attr_node *);
3575 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
3576 static inline unsigned AT_flag (dw_attr_node *);
3577 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
3578 static inline HOST_WIDE_INT AT_int (dw_attr_node *);
3579 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
3580 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_node *);
3581 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
3582 HOST_WIDE_INT, unsigned HOST_WIDE_INT);
3583 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
3584 unsigned int, unsigned char *);
3585 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
3586 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
3587 static inline const char *AT_string (dw_attr_node *);
3588 static enum dwarf_form AT_string_form (dw_attr_node *);
3589 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
3590 static void add_AT_specification (dw_die_ref, dw_die_ref);
3591 static inline dw_die_ref AT_ref (dw_attr_node *);
3592 static inline int AT_ref_external (dw_attr_node *);
3593 static inline void set_AT_ref_external (dw_attr_node *, int);
3594 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
3595 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
3596 static inline dw_loc_descr_ref AT_loc (dw_attr_node *);
3597 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
3598 dw_loc_list_ref);
3599 static inline dw_loc_list_ref AT_loc_list (dw_attr_node *);
3600 static void add_AT_view_list (dw_die_ref, enum dwarf_attribute);
3601 static inline dw_loc_list_ref AT_loc_list (dw_attr_node *);
3602 static addr_table_entry *add_addr_table_entry (void *, enum ate_kind);
3603 static void remove_addr_table_entry (addr_table_entry *);
3604 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx, bool);
3605 static inline rtx AT_addr (dw_attr_node *);
3606 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
3607 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
3608 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
3609 static void add_AT_loclistsptr (dw_die_ref, enum dwarf_attribute,
3610 const char *);
3611 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
3612 unsigned HOST_WIDE_INT);
3613 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
3614 unsigned long, bool);
3615 static inline const char *AT_lbl (dw_attr_node *);
3616 static dw_attr_node *get_AT (dw_die_ref, enum dwarf_attribute);
3617 static const char *get_AT_low_pc (dw_die_ref);
3618 static const char *get_AT_hi_pc (dw_die_ref);
3619 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
3620 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
3621 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
3622 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
3623 static bool is_cxx (void);
3624 static bool is_cxx (const_tree);
3625 static bool is_fortran (void);
3626 static bool is_ada (void);
3627 static bool remove_AT (dw_die_ref, enum dwarf_attribute);
3628 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
3629 static void add_child_die (dw_die_ref, dw_die_ref);
3630 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
3631 static dw_die_ref lookup_type_die (tree);
3632 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
3633 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
3634 static void equate_type_number_to_die (tree, dw_die_ref);
3635 static dw_die_ref lookup_decl_die (tree);
3636 static var_loc_list *lookup_decl_loc (const_tree);
3637 static void equate_decl_number_to_die (tree, dw_die_ref);
3638 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *, var_loc_view);
3639 static void print_spaces (FILE *);
3640 static void print_die (dw_die_ref, FILE *);
3641 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3642 static void attr_checksum (dw_attr_node *, struct md5_ctx *, int *);
3643 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3644 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
3645 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
3646 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
3647 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_node *,
3648 struct md5_ctx *, int *);
3649 struct checksum_attributes;
3650 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
3651 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
3652 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
3653 static void generate_type_signature (dw_die_ref, comdat_type_node *);
3654 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3655 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
3656 static int same_attr_p (dw_attr_node *, dw_attr_node *, int *);
3657 static int same_die_p (dw_die_ref, dw_die_ref, int *);
3658 static int is_type_die (dw_die_ref);
3659 static int is_comdat_die (dw_die_ref);
3660 static inline bool is_template_instantiation (dw_die_ref);
3661 static int is_declaration_die (dw_die_ref);
3662 static int should_move_die_to_comdat (dw_die_ref);
3663 static dw_die_ref clone_as_declaration (dw_die_ref);
3664 static dw_die_ref clone_die (dw_die_ref);
3665 static dw_die_ref clone_tree (dw_die_ref);
3666 static dw_die_ref copy_declaration_context (dw_die_ref, dw_die_ref);
3667 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
3668 static void generate_skeleton_bottom_up (skeleton_chain_node *);
3669 static dw_die_ref generate_skeleton (dw_die_ref);
3670 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
3671 dw_die_ref,
3672 dw_die_ref);
3673 static void break_out_comdat_types (dw_die_ref);
3674 static void copy_decls_for_unworthy_types (dw_die_ref);
3676 static void add_sibling_attributes (dw_die_ref);
3677 static void output_location_lists (dw_die_ref);
3678 static int constant_size (unsigned HOST_WIDE_INT);
3679 static unsigned long size_of_die (dw_die_ref);
3680 static void calc_die_sizes (dw_die_ref);
3681 static void calc_base_type_die_sizes (void);
3682 static void mark_dies (dw_die_ref);
3683 static void unmark_dies (dw_die_ref);
3684 static void unmark_all_dies (dw_die_ref);
3685 static unsigned long size_of_pubnames (vec<pubname_entry, va_gc> *);
3686 static unsigned long size_of_aranges (void);
3687 static enum dwarf_form value_format (dw_attr_node *);
3688 static void output_value_format (dw_attr_node *);
3689 static void output_abbrev_section (void);
3690 static void output_die_abbrevs (unsigned long, dw_die_ref);
3691 static void output_die (dw_die_ref);
3692 static void output_compilation_unit_header (enum dwarf_unit_type);
3693 static void output_comp_unit (dw_die_ref, int, const unsigned char *);
3694 static void output_comdat_type_unit (comdat_type_node *);
3695 static const char *dwarf2_name (tree, int);
3696 static void add_pubname (tree, dw_die_ref);
3697 static void add_enumerator_pubname (const char *, dw_die_ref);
3698 static void add_pubname_string (const char *, dw_die_ref);
3699 static void add_pubtype (tree, dw_die_ref);
3700 static void output_pubnames (vec<pubname_entry, va_gc> *);
3701 static void output_aranges (void);
3702 static unsigned int add_ranges (const_tree, bool = false);
3703 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3704 bool *, bool);
3705 static void output_ranges (void);
3706 static dw_line_info_table *new_line_info_table (void);
3707 static void output_line_info (bool);
3708 static void output_file_names (void);
3709 static dw_die_ref base_type_die (tree, bool);
3710 static int is_base_type (tree);
3711 static dw_die_ref subrange_type_die (tree, tree, tree, tree, dw_die_ref);
3712 static int decl_quals (const_tree);
3713 static dw_die_ref modified_type_die (tree, int, bool, dw_die_ref);
3714 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3715 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3716 static int type_is_enum (const_tree);
3717 static unsigned int dbx_reg_number (const_rtx);
3718 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3719 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3720 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3721 enum var_init_status);
3722 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3723 enum var_init_status);
3724 static dw_loc_descr_ref based_loc_descr (rtx, poly_int64,
3725 enum var_init_status);
3726 static int is_based_loc (const_rtx);
3727 static bool resolve_one_addr (rtx *);
3728 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3729 enum var_init_status);
3730 static dw_loc_descr_ref loc_descriptor (rtx, machine_mode mode,
3731 enum var_init_status);
3732 struct loc_descr_context;
3733 static void add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref);
3734 static void add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list);
3735 static dw_loc_list_ref loc_list_from_tree (tree, int,
3736 struct loc_descr_context *);
3737 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int,
3738 struct loc_descr_context *);
3739 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
3740 static tree field_type (const_tree);
3741 static unsigned int simple_type_align_in_bits (const_tree);
3742 static unsigned int simple_decl_align_in_bits (const_tree);
3743 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3744 struct vlr_context;
3745 static dw_loc_descr_ref field_byte_offset (const_tree, struct vlr_context *,
3746 HOST_WIDE_INT *);
3747 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3748 dw_loc_list_ref);
3749 static void add_data_member_location_attribute (dw_die_ref, tree,
3750 struct vlr_context *);
3751 static bool add_const_value_attribute (dw_die_ref, rtx);
3752 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3753 static void insert_wide_int (const wide_int &, unsigned char *, int);
3754 static void insert_float (const_rtx, unsigned char *);
3755 static rtx rtl_for_decl_location (tree);
3756 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool);
3757 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3758 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3759 static void add_name_attribute (dw_die_ref, const char *);
3760 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3761 static void add_comp_dir_attribute (dw_die_ref);
3762 static void add_scalar_info (dw_die_ref, enum dwarf_attribute, tree, int,
3763 struct loc_descr_context *);
3764 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree,
3765 struct loc_descr_context *);
3766 static void add_subscript_info (dw_die_ref, tree, bool);
3767 static void add_byte_size_attribute (dw_die_ref, tree);
3768 static void add_alignment_attribute (dw_die_ref, tree);
3769 static inline void add_bit_offset_attribute (dw_die_ref, tree,
3770 struct vlr_context *);
3771 static void add_bit_size_attribute (dw_die_ref, tree);
3772 static void add_prototyped_attribute (dw_die_ref, tree);
3773 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
3774 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3775 static void add_src_coords_attributes (dw_die_ref, tree);
3776 static void add_name_and_src_coords_attributes (dw_die_ref, tree, bool = false);
3777 static void add_discr_value (dw_die_ref, dw_discr_value *);
3778 static void add_discr_list (dw_die_ref, dw_discr_list_ref);
3779 static inline dw_discr_list_ref AT_discr_list (dw_attr_node *);
3780 static void push_decl_scope (tree);
3781 static void pop_decl_scope (void);
3782 static dw_die_ref scope_die_for (tree, dw_die_ref);
3783 static inline int local_scope_p (dw_die_ref);
3784 static inline int class_scope_p (dw_die_ref);
3785 static inline int class_or_namespace_scope_p (dw_die_ref);
3786 static void add_type_attribute (dw_die_ref, tree, int, bool, dw_die_ref);
3787 static void add_calling_convention_attribute (dw_die_ref, tree);
3788 static const char *type_tag (const_tree);
3789 static tree member_declared_type (const_tree);
3790 #if 0
3791 static const char *decl_start_label (tree);
3792 #endif
3793 static void gen_array_type_die (tree, dw_die_ref);
3794 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3795 #if 0
3796 static void gen_entry_point_die (tree, dw_die_ref);
3797 #endif
3798 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3799 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3800 static dw_die_ref gen_formal_parameter_pack_die (tree, tree, dw_die_ref, tree*);
3801 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3802 static void gen_formal_types_die (tree, dw_die_ref);
3803 static void gen_subprogram_die (tree, dw_die_ref);
3804 static void gen_variable_die (tree, tree, dw_die_ref);
3805 static void gen_const_die (tree, dw_die_ref);
3806 static void gen_label_die (tree, dw_die_ref);
3807 static void gen_lexical_block_die (tree, dw_die_ref);
3808 static void gen_inlined_subroutine_die (tree, dw_die_ref);
3809 static void gen_field_die (tree, struct vlr_context *, dw_die_ref);
3810 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3811 static dw_die_ref gen_compile_unit_die (const char *);
3812 static void gen_inheritance_die (tree, tree, tree, dw_die_ref);
3813 static void gen_member_die (tree, dw_die_ref);
3814 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3815 enum debug_info_usage);
3816 static void gen_subroutine_type_die (tree, dw_die_ref);
3817 static void gen_typedef_die (tree, dw_die_ref);
3818 static void gen_type_die (tree, dw_die_ref);
3819 static void gen_block_die (tree, dw_die_ref);
3820 static void decls_for_scope (tree, dw_die_ref);
3821 static bool is_naming_typedef_decl (const_tree);
3822 static inline dw_die_ref get_context_die (tree);
3823 static void gen_namespace_die (tree, dw_die_ref);
3824 static dw_die_ref gen_namelist_decl (tree, dw_die_ref, tree);
3825 static dw_die_ref gen_decl_die (tree, tree, struct vlr_context *, dw_die_ref);
3826 static dw_die_ref force_decl_die (tree);
3827 static dw_die_ref force_type_die (tree);
3828 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3829 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3830 static struct dwarf_file_data * lookup_filename (const char *);
3831 static void retry_incomplete_types (void);
3832 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3833 static void gen_generic_params_dies (tree);
3834 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
3835 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
3836 static void splice_child_die (dw_die_ref, dw_die_ref);
3837 static int file_info_cmp (const void *, const void *);
3838 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *, var_loc_view,
3839 const char *, var_loc_view, const char *);
3840 static void output_loc_list (dw_loc_list_ref);
3841 static char *gen_internal_sym (const char *);
3842 static bool want_pubnames (void);
3844 static void prune_unmark_dies (dw_die_ref);
3845 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3846 static void prune_unused_types_mark (dw_die_ref, int);
3847 static void prune_unused_types_walk (dw_die_ref);
3848 static void prune_unused_types_walk_attribs (dw_die_ref);
3849 static void prune_unused_types_prune (dw_die_ref);
3850 static void prune_unused_types (void);
3851 static int maybe_emit_file (struct dwarf_file_data *fd);
3852 static inline const char *AT_vms_delta1 (dw_attr_node *);
3853 static inline const char *AT_vms_delta2 (dw_attr_node *);
3854 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
3855 const char *, const char *);
3856 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
3857 static void gen_remaining_tmpl_value_param_die_attribute (void);
3858 static bool generic_type_p (tree);
3859 static void schedule_generic_params_dies_gen (tree t);
3860 static void gen_scheduled_generic_parms_dies (void);
3861 static void resolve_variable_values (void);
3863 static const char *comp_dir_string (void);
3865 static void hash_loc_operands (dw_loc_descr_ref, inchash::hash &);
3867 /* enum for tracking thread-local variables whose address is really an offset
3868 relative to the TLS pointer, which will need link-time relocation, but will
3869 not need relocation by the DWARF consumer. */
3871 enum dtprel_bool
3873 dtprel_false = 0,
3874 dtprel_true = 1
3877 /* Return the operator to use for an address of a variable. For dtprel_true, we
3878 use DW_OP_const*. For regular variables, which need both link-time
3879 relocation and consumer-level relocation (e.g., to account for shared objects
3880 loaded at a random address), we use DW_OP_addr*. */
3882 static inline enum dwarf_location_atom
3883 dw_addr_op (enum dtprel_bool dtprel)
3885 if (dtprel == dtprel_true)
3886 return (dwarf_split_debug_info ? DW_OP_GNU_const_index
3887 : (DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u));
3888 else
3889 return dwarf_split_debug_info ? DW_OP_GNU_addr_index : DW_OP_addr;
3892 /* Return a pointer to a newly allocated address location description. If
3893 dwarf_split_debug_info is true, then record the address with the appropriate
3894 relocation. */
3895 static inline dw_loc_descr_ref
3896 new_addr_loc_descr (rtx addr, enum dtprel_bool dtprel)
3898 dw_loc_descr_ref ref = new_loc_descr (dw_addr_op (dtprel), 0, 0);
3900 ref->dw_loc_oprnd1.val_class = dw_val_class_addr;
3901 ref->dw_loc_oprnd1.v.val_addr = addr;
3902 ref->dtprel = dtprel;
3903 if (dwarf_split_debug_info)
3904 ref->dw_loc_oprnd1.val_entry
3905 = add_addr_table_entry (addr,
3906 dtprel ? ate_kind_rtx_dtprel : ate_kind_rtx);
3907 else
3908 ref->dw_loc_oprnd1.val_entry = NULL;
3910 return ref;
3913 /* Section names used to hold DWARF debugging information. */
3915 #ifndef DEBUG_INFO_SECTION
3916 #define DEBUG_INFO_SECTION ".debug_info"
3917 #endif
3918 #ifndef DEBUG_DWO_INFO_SECTION
3919 #define DEBUG_DWO_INFO_SECTION ".debug_info.dwo"
3920 #endif
3921 #ifndef DEBUG_LTO_INFO_SECTION
3922 #define DEBUG_LTO_INFO_SECTION ".gnu.debuglto_.debug_info"
3923 #endif
3924 #ifndef DEBUG_LTO_DWO_INFO_SECTION
3925 #define DEBUG_LTO_DWO_INFO_SECTION ".gnu.debuglto_.debug_info.dwo"
3926 #endif
3927 #ifndef DEBUG_ABBREV_SECTION
3928 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
3929 #endif
3930 #ifndef DEBUG_LTO_ABBREV_SECTION
3931 #define DEBUG_LTO_ABBREV_SECTION ".gnu.debuglto_.debug_abbrev"
3932 #endif
3933 #ifndef DEBUG_DWO_ABBREV_SECTION
3934 #define DEBUG_DWO_ABBREV_SECTION ".debug_abbrev.dwo"
3935 #endif
3936 #ifndef DEBUG_LTO_DWO_ABBREV_SECTION
3937 #define DEBUG_LTO_DWO_ABBREV_SECTION ".gnu.debuglto_.debug_abbrev.dwo"
3938 #endif
3939 #ifndef DEBUG_ARANGES_SECTION
3940 #define DEBUG_ARANGES_SECTION ".debug_aranges"
3941 #endif
3942 #ifndef DEBUG_ADDR_SECTION
3943 #define DEBUG_ADDR_SECTION ".debug_addr"
3944 #endif
3945 #ifndef DEBUG_MACINFO_SECTION
3946 #define DEBUG_MACINFO_SECTION ".debug_macinfo"
3947 #endif
3948 #ifndef DEBUG_LTO_MACINFO_SECTION
3949 #define DEBUG_LTO_MACINFO_SECTION ".gnu.debuglto_.debug_macinfo"
3950 #endif
3951 #ifndef DEBUG_DWO_MACINFO_SECTION
3952 #define DEBUG_DWO_MACINFO_SECTION ".debug_macinfo.dwo"
3953 #endif
3954 #ifndef DEBUG_LTO_DWO_MACINFO_SECTION
3955 #define DEBUG_LTO_DWO_MACINFO_SECTION ".gnu.debuglto_.debug_macinfo.dwo"
3956 #endif
3957 #ifndef DEBUG_MACRO_SECTION
3958 #define DEBUG_MACRO_SECTION ".debug_macro"
3959 #endif
3960 #ifndef DEBUG_LTO_MACRO_SECTION
3961 #define DEBUG_LTO_MACRO_SECTION ".gnu.debuglto_.debug_macro"
3962 #endif
3963 #ifndef DEBUG_DWO_MACRO_SECTION
3964 #define DEBUG_DWO_MACRO_SECTION ".debug_macro.dwo"
3965 #endif
3966 #ifndef DEBUG_LTO_DWO_MACRO_SECTION
3967 #define DEBUG_LTO_DWO_MACRO_SECTION ".gnu.debuglto_.debug_macro.dwo"
3968 #endif
3969 #ifndef DEBUG_LINE_SECTION
3970 #define DEBUG_LINE_SECTION ".debug_line"
3971 #endif
3972 #ifndef DEBUG_LTO_LINE_SECTION
3973 #define DEBUG_LTO_LINE_SECTION ".gnu.debuglto_.debug_line"
3974 #endif
3975 #ifndef DEBUG_DWO_LINE_SECTION
3976 #define DEBUG_DWO_LINE_SECTION ".debug_line.dwo"
3977 #endif
3978 #ifndef DEBUG_LTO_DWO_LINE_SECTION
3979 #define DEBUG_LTO_DWO_LINE_SECTION ".gnu.debuglto_.debug_line.dwo"
3980 #endif
3981 #ifndef DEBUG_LOC_SECTION
3982 #define DEBUG_LOC_SECTION ".debug_loc"
3983 #endif
3984 #ifndef DEBUG_DWO_LOC_SECTION
3985 #define DEBUG_DWO_LOC_SECTION ".debug_loc.dwo"
3986 #endif
3987 #ifndef DEBUG_LOCLISTS_SECTION
3988 #define DEBUG_LOCLISTS_SECTION ".debug_loclists"
3989 #endif
3990 #ifndef DEBUG_DWO_LOCLISTS_SECTION
3991 #define DEBUG_DWO_LOCLISTS_SECTION ".debug_loclists.dwo"
3992 #endif
3993 #ifndef DEBUG_PUBNAMES_SECTION
3994 #define DEBUG_PUBNAMES_SECTION \
3995 ((debug_generate_pub_sections == 2) \
3996 ? ".debug_gnu_pubnames" : ".debug_pubnames")
3997 #endif
3998 #ifndef DEBUG_PUBTYPES_SECTION
3999 #define DEBUG_PUBTYPES_SECTION \
4000 ((debug_generate_pub_sections == 2) \
4001 ? ".debug_gnu_pubtypes" : ".debug_pubtypes")
4002 #endif
4003 #ifndef DEBUG_STR_OFFSETS_SECTION
4004 #define DEBUG_STR_OFFSETS_SECTION ".debug_str_offsets"
4005 #endif
4006 #ifndef DEBUG_DWO_STR_OFFSETS_SECTION
4007 #define DEBUG_DWO_STR_OFFSETS_SECTION ".debug_str_offsets.dwo"
4008 #endif
4009 #ifndef DEBUG_LTO_DWO_STR_OFFSETS_SECTION
4010 #define DEBUG_LTO_DWO_STR_OFFSETS_SECTION ".gnu.debuglto_.debug_str_offsets.dwo"
4011 #endif
4012 #ifndef DEBUG_STR_SECTION
4013 #define DEBUG_STR_SECTION ".debug_str"
4014 #endif
4015 #ifndef DEBUG_LTO_STR_SECTION
4016 #define DEBUG_LTO_STR_SECTION ".gnu.debuglto_.debug_str"
4017 #endif
4018 #ifndef DEBUG_STR_DWO_SECTION
4019 #define DEBUG_STR_DWO_SECTION ".debug_str.dwo"
4020 #endif
4021 #ifndef DEBUG_LTO_STR_DWO_SECTION
4022 #define DEBUG_LTO_STR_DWO_SECTION ".gnu.debuglto_.debug_str.dwo"
4023 #endif
4024 #ifndef DEBUG_RANGES_SECTION
4025 #define DEBUG_RANGES_SECTION ".debug_ranges"
4026 #endif
4027 #ifndef DEBUG_RNGLISTS_SECTION
4028 #define DEBUG_RNGLISTS_SECTION ".debug_rnglists"
4029 #endif
4030 #ifndef DEBUG_LINE_STR_SECTION
4031 #define DEBUG_LINE_STR_SECTION ".debug_line_str"
4032 #endif
4033 #ifndef DEBUG_LTO_LINE_STR_SECTION
4034 #define DEBUG_LTO_LINE_STR_SECTION ".gnu.debuglto_.debug_line_str"
4035 #endif
4037 /* Standard ELF section names for compiled code and data. */
4038 #ifndef TEXT_SECTION_NAME
4039 #define TEXT_SECTION_NAME ".text"
4040 #endif
4042 /* Section flags for .debug_str section. */
4043 #define DEBUG_STR_SECTION_FLAGS \
4044 (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings \
4045 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
4046 : SECTION_DEBUG)
4048 /* Section flags for .debug_str.dwo section. */
4049 #define DEBUG_STR_DWO_SECTION_FLAGS (SECTION_DEBUG | SECTION_EXCLUDE)
4051 /* Attribute used to refer to the macro section. */
4052 #define DEBUG_MACRO_ATTRIBUTE (dwarf_version >= 5 ? DW_AT_macros \
4053 : dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros)
4055 /* Labels we insert at beginning sections we can reference instead of
4056 the section names themselves. */
4058 #ifndef TEXT_SECTION_LABEL
4059 #define TEXT_SECTION_LABEL "Ltext"
4060 #endif
4061 #ifndef COLD_TEXT_SECTION_LABEL
4062 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
4063 #endif
4064 #ifndef DEBUG_LINE_SECTION_LABEL
4065 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
4066 #endif
4067 #ifndef DEBUG_SKELETON_LINE_SECTION_LABEL
4068 #define DEBUG_SKELETON_LINE_SECTION_LABEL "Lskeleton_debug_line"
4069 #endif
4070 #ifndef DEBUG_INFO_SECTION_LABEL
4071 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
4072 #endif
4073 #ifndef DEBUG_SKELETON_INFO_SECTION_LABEL
4074 #define DEBUG_SKELETON_INFO_SECTION_LABEL "Lskeleton_debug_info"
4075 #endif
4076 #ifndef DEBUG_ABBREV_SECTION_LABEL
4077 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
4078 #endif
4079 #ifndef DEBUG_SKELETON_ABBREV_SECTION_LABEL
4080 #define DEBUG_SKELETON_ABBREV_SECTION_LABEL "Lskeleton_debug_abbrev"
4081 #endif
4082 #ifndef DEBUG_ADDR_SECTION_LABEL
4083 #define DEBUG_ADDR_SECTION_LABEL "Ldebug_addr"
4084 #endif
4085 #ifndef DEBUG_LOC_SECTION_LABEL
4086 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
4087 #endif
4088 #ifndef DEBUG_RANGES_SECTION_LABEL
4089 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
4090 #endif
4091 #ifndef DEBUG_MACINFO_SECTION_LABEL
4092 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
4093 #endif
4094 #ifndef DEBUG_MACRO_SECTION_LABEL
4095 #define DEBUG_MACRO_SECTION_LABEL "Ldebug_macro"
4096 #endif
4097 #define SKELETON_COMP_DIE_ABBREV 1
4098 #define SKELETON_TYPE_DIE_ABBREV 2
4100 /* Definitions of defaults for formats and names of various special
4101 (artificial) labels which may be generated within this file (when the -g
4102 options is used and DWARF2_DEBUGGING_INFO is in effect.
4103 If necessary, these may be overridden from within the tm.h file, but
4104 typically, overriding these defaults is unnecessary. */
4106 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
4107 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4108 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4109 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
4110 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4111 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4112 static char debug_skeleton_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4113 static char debug_skeleton_abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4114 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4115 static char debug_addr_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4116 static char debug_skeleton_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4117 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4118 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4119 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
4120 static char ranges_base_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
4122 #ifndef TEXT_END_LABEL
4123 #define TEXT_END_LABEL "Letext"
4124 #endif
4125 #ifndef COLD_END_LABEL
4126 #define COLD_END_LABEL "Letext_cold"
4127 #endif
4128 #ifndef BLOCK_BEGIN_LABEL
4129 #define BLOCK_BEGIN_LABEL "LBB"
4130 #endif
4131 #ifndef BLOCK_INLINE_ENTRY_LABEL
4132 #define BLOCK_INLINE_ENTRY_LABEL "LBI"
4133 #endif
4134 #ifndef BLOCK_END_LABEL
4135 #define BLOCK_END_LABEL "LBE"
4136 #endif
4137 #ifndef LINE_CODE_LABEL
4138 #define LINE_CODE_LABEL "LM"
4139 #endif
4142 /* Return the root of the DIE's built for the current compilation unit. */
4143 static dw_die_ref
4144 comp_unit_die (void)
4146 if (!single_comp_unit_die)
4147 single_comp_unit_die = gen_compile_unit_die (NULL);
4148 return single_comp_unit_die;
4151 /* We allow a language front-end to designate a function that is to be
4152 called to "demangle" any name before it is put into a DIE. */
4154 static const char *(*demangle_name_func) (const char *);
4156 void
4157 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
4159 demangle_name_func = func;
4162 /* Test if rtl node points to a pseudo register. */
4164 static inline int
4165 is_pseudo_reg (const_rtx rtl)
4167 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
4168 || (GET_CODE (rtl) == SUBREG
4169 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
4172 /* Return a reference to a type, with its const and volatile qualifiers
4173 removed. */
4175 static inline tree
4176 type_main_variant (tree type)
4178 type = TYPE_MAIN_VARIANT (type);
4180 /* ??? There really should be only one main variant among any group of
4181 variants of a given type (and all of the MAIN_VARIANT values for all
4182 members of the group should point to that one type) but sometimes the C
4183 front-end messes this up for array types, so we work around that bug
4184 here. */
4185 if (TREE_CODE (type) == ARRAY_TYPE)
4186 while (type != TYPE_MAIN_VARIANT (type))
4187 type = TYPE_MAIN_VARIANT (type);
4189 return type;
4192 /* Return nonzero if the given type node represents a tagged type. */
4194 static inline int
4195 is_tagged_type (const_tree type)
4197 enum tree_code code = TREE_CODE (type);
4199 return (code == RECORD_TYPE || code == UNION_TYPE
4200 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
4203 /* Set label to debug_info_section_label + die_offset of a DIE reference. */
4205 static void
4206 get_ref_die_offset_label (char *label, dw_die_ref ref)
4208 sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
4211 /* Return die_offset of a DIE reference to a base type. */
4213 static unsigned long int
4214 get_base_type_offset (dw_die_ref ref)
4216 if (ref->die_offset)
4217 return ref->die_offset;
4218 if (comp_unit_die ()->die_abbrev)
4220 calc_base_type_die_sizes ();
4221 gcc_assert (ref->die_offset);
4223 return ref->die_offset;
4226 /* Return die_offset of a DIE reference other than base type. */
4228 static unsigned long int
4229 get_ref_die_offset (dw_die_ref ref)
4231 gcc_assert (ref->die_offset);
4232 return ref->die_offset;
4235 /* Convert a DIE tag into its string name. */
4237 static const char *
4238 dwarf_tag_name (unsigned int tag)
4240 const char *name = get_DW_TAG_name (tag);
4242 if (name != NULL)
4243 return name;
4245 return "DW_TAG_<unknown>";
4248 /* Convert a DWARF attribute code into its string name. */
4250 static const char *
4251 dwarf_attr_name (unsigned int attr)
4253 const char *name;
4255 switch (attr)
4257 #if VMS_DEBUGGING_INFO
4258 case DW_AT_HP_prologue:
4259 return "DW_AT_HP_prologue";
4260 #else
4261 case DW_AT_MIPS_loop_unroll_factor:
4262 return "DW_AT_MIPS_loop_unroll_factor";
4263 #endif
4265 #if VMS_DEBUGGING_INFO
4266 case DW_AT_HP_epilogue:
4267 return "DW_AT_HP_epilogue";
4268 #else
4269 case DW_AT_MIPS_stride:
4270 return "DW_AT_MIPS_stride";
4271 #endif
4274 name = get_DW_AT_name (attr);
4276 if (name != NULL)
4277 return name;
4279 return "DW_AT_<unknown>";
4282 /* Convert a DWARF value form code into its string name. */
4284 static const char *
4285 dwarf_form_name (unsigned int form)
4287 const char *name = get_DW_FORM_name (form);
4289 if (name != NULL)
4290 return name;
4292 return "DW_FORM_<unknown>";
4295 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
4296 instance of an inlined instance of a decl which is local to an inline
4297 function, so we have to trace all of the way back through the origin chain
4298 to find out what sort of node actually served as the original seed for the
4299 given block. */
4301 static tree
4302 decl_ultimate_origin (const_tree decl)
4304 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
4305 return NULL_TREE;
4307 /* DECL_ABSTRACT_ORIGIN can point to itself; ignore that if
4308 we're trying to output the abstract instance of this function. */
4309 if (DECL_ABSTRACT_P (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
4310 return NULL_TREE;
4312 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
4313 most distant ancestor, this should never happen. */
4314 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
4316 return DECL_ABSTRACT_ORIGIN (decl);
4319 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
4320 of a virtual function may refer to a base class, so we check the 'this'
4321 parameter. */
4323 static tree
4324 decl_class_context (tree decl)
4326 tree context = NULL_TREE;
4328 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
4329 context = DECL_CONTEXT (decl);
4330 else
4331 context = TYPE_MAIN_VARIANT
4332 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
4334 if (context && !TYPE_P (context))
4335 context = NULL_TREE;
4337 return context;
4340 /* Add an attribute/value pair to a DIE. */
4342 static inline void
4343 add_dwarf_attr (dw_die_ref die, dw_attr_node *attr)
4345 /* Maybe this should be an assert? */
4346 if (die == NULL)
4347 return;
4349 if (flag_checking)
4351 /* Check we do not add duplicate attrs. Can't use get_AT here
4352 because that recurses to the specification/abstract origin DIE. */
4353 dw_attr_node *a;
4354 unsigned ix;
4355 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4356 gcc_assert (a->dw_attr != attr->dw_attr);
4359 vec_safe_reserve (die->die_attr, 1);
4360 vec_safe_push (die->die_attr, *attr);
4363 static inline enum dw_val_class
4364 AT_class (dw_attr_node *a)
4366 return a->dw_attr_val.val_class;
4369 /* Return the index for any attribute that will be referenced with a
4370 DW_FORM_GNU_addr_index or DW_FORM_GNU_str_index. String indices
4371 are stored in dw_attr_val.v.val_str for reference counting
4372 pruning. */
4374 static inline unsigned int
4375 AT_index (dw_attr_node *a)
4377 if (AT_class (a) == dw_val_class_str)
4378 return a->dw_attr_val.v.val_str->index;
4379 else if (a->dw_attr_val.val_entry != NULL)
4380 return a->dw_attr_val.val_entry->index;
4381 return NOT_INDEXED;
4384 /* Add a flag value attribute to a DIE. */
4386 static inline void
4387 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
4389 dw_attr_node attr;
4391 attr.dw_attr = attr_kind;
4392 attr.dw_attr_val.val_class = dw_val_class_flag;
4393 attr.dw_attr_val.val_entry = NULL;
4394 attr.dw_attr_val.v.val_flag = flag;
4395 add_dwarf_attr (die, &attr);
4398 static inline unsigned
4399 AT_flag (dw_attr_node *a)
4401 gcc_assert (a && AT_class (a) == dw_val_class_flag);
4402 return a->dw_attr_val.v.val_flag;
4405 /* Add a signed integer attribute value to a DIE. */
4407 static inline void
4408 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
4410 dw_attr_node attr;
4412 attr.dw_attr = attr_kind;
4413 attr.dw_attr_val.val_class = dw_val_class_const;
4414 attr.dw_attr_val.val_entry = NULL;
4415 attr.dw_attr_val.v.val_int = int_val;
4416 add_dwarf_attr (die, &attr);
4419 static inline HOST_WIDE_INT
4420 AT_int (dw_attr_node *a)
4422 gcc_assert (a && (AT_class (a) == dw_val_class_const
4423 || AT_class (a) == dw_val_class_const_implicit));
4424 return a->dw_attr_val.v.val_int;
4427 /* Add an unsigned integer attribute value to a DIE. */
4429 static inline void
4430 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
4431 unsigned HOST_WIDE_INT unsigned_val)
4433 dw_attr_node attr;
4435 attr.dw_attr = attr_kind;
4436 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
4437 attr.dw_attr_val.val_entry = NULL;
4438 attr.dw_attr_val.v.val_unsigned = unsigned_val;
4439 add_dwarf_attr (die, &attr);
4442 static inline unsigned HOST_WIDE_INT
4443 AT_unsigned (dw_attr_node *a)
4445 gcc_assert (a && (AT_class (a) == dw_val_class_unsigned_const
4446 || AT_class (a) == dw_val_class_unsigned_const_implicit));
4447 return a->dw_attr_val.v.val_unsigned;
4450 /* Add an unsigned wide integer attribute value to a DIE. */
4452 static inline void
4453 add_AT_wide (dw_die_ref die, enum dwarf_attribute attr_kind,
4454 const wide_int& w)
4456 dw_attr_node attr;
4458 attr.dw_attr = attr_kind;
4459 attr.dw_attr_val.val_class = dw_val_class_wide_int;
4460 attr.dw_attr_val.val_entry = NULL;
4461 attr.dw_attr_val.v.val_wide = ggc_alloc<wide_int> ();
4462 *attr.dw_attr_val.v.val_wide = w;
4463 add_dwarf_attr (die, &attr);
4466 /* Add an unsigned double integer attribute value to a DIE. */
4468 static inline void
4469 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
4470 HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
4472 dw_attr_node attr;
4474 attr.dw_attr = attr_kind;
4475 attr.dw_attr_val.val_class = dw_val_class_const_double;
4476 attr.dw_attr_val.val_entry = NULL;
4477 attr.dw_attr_val.v.val_double.high = high;
4478 attr.dw_attr_val.v.val_double.low = low;
4479 add_dwarf_attr (die, &attr);
4482 /* Add a floating point attribute value to a DIE and return it. */
4484 static inline void
4485 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
4486 unsigned int length, unsigned int elt_size, unsigned char *array)
4488 dw_attr_node attr;
4490 attr.dw_attr = attr_kind;
4491 attr.dw_attr_val.val_class = dw_val_class_vec;
4492 attr.dw_attr_val.val_entry = NULL;
4493 attr.dw_attr_val.v.val_vec.length = length;
4494 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
4495 attr.dw_attr_val.v.val_vec.array = array;
4496 add_dwarf_attr (die, &attr);
4499 /* Add an 8-byte data attribute value to a DIE. */
4501 static inline void
4502 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
4503 unsigned char data8[8])
4505 dw_attr_node attr;
4507 attr.dw_attr = attr_kind;
4508 attr.dw_attr_val.val_class = dw_val_class_data8;
4509 attr.dw_attr_val.val_entry = NULL;
4510 memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
4511 add_dwarf_attr (die, &attr);
4514 /* Add DW_AT_low_pc and DW_AT_high_pc to a DIE. When using
4515 dwarf_split_debug_info, address attributes in dies destined for the
4516 final executable have force_direct set to avoid using indexed
4517 references. */
4519 static inline void
4520 add_AT_low_high_pc (dw_die_ref die, const char *lbl_low, const char *lbl_high,
4521 bool force_direct)
4523 dw_attr_node attr;
4524 char * lbl_id;
4526 lbl_id = xstrdup (lbl_low);
4527 attr.dw_attr = DW_AT_low_pc;
4528 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4529 attr.dw_attr_val.v.val_lbl_id = lbl_id;
4530 if (dwarf_split_debug_info && !force_direct)
4531 attr.dw_attr_val.val_entry
4532 = add_addr_table_entry (lbl_id, ate_kind_label);
4533 else
4534 attr.dw_attr_val.val_entry = NULL;
4535 add_dwarf_attr (die, &attr);
4537 attr.dw_attr = DW_AT_high_pc;
4538 if (dwarf_version < 4)
4539 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4540 else
4541 attr.dw_attr_val.val_class = dw_val_class_high_pc;
4542 lbl_id = xstrdup (lbl_high);
4543 attr.dw_attr_val.v.val_lbl_id = lbl_id;
4544 if (attr.dw_attr_val.val_class == dw_val_class_lbl_id
4545 && dwarf_split_debug_info && !force_direct)
4546 attr.dw_attr_val.val_entry
4547 = add_addr_table_entry (lbl_id, ate_kind_label);
4548 else
4549 attr.dw_attr_val.val_entry = NULL;
4550 add_dwarf_attr (die, &attr);
4553 /* Hash and equality functions for debug_str_hash. */
4555 hashval_t
4556 indirect_string_hasher::hash (indirect_string_node *x)
4558 return htab_hash_string (x->str);
4561 bool
4562 indirect_string_hasher::equal (indirect_string_node *x1, const char *x2)
4564 return strcmp (x1->str, x2) == 0;
4567 /* Add STR to the given string hash table. */
4569 static struct indirect_string_node *
4570 find_AT_string_in_table (const char *str,
4571 hash_table<indirect_string_hasher> *table)
4573 struct indirect_string_node *node;
4575 indirect_string_node **slot
4576 = table->find_slot_with_hash (str, htab_hash_string (str), INSERT);
4577 if (*slot == NULL)
4579 node = ggc_cleared_alloc<indirect_string_node> ();
4580 node->str = ggc_strdup (str);
4581 *slot = node;
4583 else
4584 node = *slot;
4586 node->refcount++;
4587 return node;
4590 /* Add STR to the indirect string hash table. */
4592 static struct indirect_string_node *
4593 find_AT_string (const char *str)
4595 if (! debug_str_hash)
4596 debug_str_hash = hash_table<indirect_string_hasher>::create_ggc (10);
4598 return find_AT_string_in_table (str, debug_str_hash);
4601 /* Add a string attribute value to a DIE. */
4603 static inline void
4604 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
4606 dw_attr_node attr;
4607 struct indirect_string_node *node;
4609 node = find_AT_string (str);
4611 attr.dw_attr = attr_kind;
4612 attr.dw_attr_val.val_class = dw_val_class_str;
4613 attr.dw_attr_val.val_entry = NULL;
4614 attr.dw_attr_val.v.val_str = node;
4615 add_dwarf_attr (die, &attr);
4618 static inline const char *
4619 AT_string (dw_attr_node *a)
4621 gcc_assert (a && AT_class (a) == dw_val_class_str);
4622 return a->dw_attr_val.v.val_str->str;
4625 /* Call this function directly to bypass AT_string_form's logic to put
4626 the string inline in the die. */
4628 static void
4629 set_indirect_string (struct indirect_string_node *node)
4631 char label[MAX_ARTIFICIAL_LABEL_BYTES];
4632 /* Already indirect is a no op. */
4633 if (node->form == DW_FORM_strp
4634 || node->form == DW_FORM_line_strp
4635 || node->form == DW_FORM_GNU_str_index)
4637 gcc_assert (node->label);
4638 return;
4640 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
4641 ++dw2_string_counter;
4642 node->label = xstrdup (label);
4644 if (!dwarf_split_debug_info)
4646 node->form = DW_FORM_strp;
4647 node->index = NOT_INDEXED;
4649 else
4651 node->form = DW_FORM_GNU_str_index;
4652 node->index = NO_INDEX_ASSIGNED;
4656 /* A helper function for dwarf2out_finish, called to reset indirect
4657 string decisions done for early LTO dwarf output before fat object
4658 dwarf output. */
4661 reset_indirect_string (indirect_string_node **h, void *)
4663 struct indirect_string_node *node = *h;
4664 if (node->form == DW_FORM_strp || node->form == DW_FORM_GNU_str_index)
4666 free (node->label);
4667 node->label = NULL;
4668 node->form = (dwarf_form) 0;
4669 node->index = 0;
4671 return 1;
4674 /* Find out whether a string should be output inline in DIE
4675 or out-of-line in .debug_str section. */
4677 static enum dwarf_form
4678 find_string_form (struct indirect_string_node *node)
4680 unsigned int len;
4682 if (node->form)
4683 return node->form;
4685 len = strlen (node->str) + 1;
4687 /* If the string is shorter or equal to the size of the reference, it is
4688 always better to put it inline. */
4689 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
4690 return node->form = DW_FORM_string;
4692 /* If we cannot expect the linker to merge strings in .debug_str
4693 section, only put it into .debug_str if it is worth even in this
4694 single module. */
4695 if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
4696 || ((debug_str_section->common.flags & SECTION_MERGE) == 0
4697 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
4698 return node->form = DW_FORM_string;
4700 set_indirect_string (node);
4702 return node->form;
4705 /* Find out whether the string referenced from the attribute should be
4706 output inline in DIE or out-of-line in .debug_str section. */
4708 static enum dwarf_form
4709 AT_string_form (dw_attr_node *a)
4711 gcc_assert (a && AT_class (a) == dw_val_class_str);
4712 return find_string_form (a->dw_attr_val.v.val_str);
4715 /* Add a DIE reference attribute value to a DIE. */
4717 static inline void
4718 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
4720 dw_attr_node attr;
4721 gcc_checking_assert (targ_die != NULL);
4723 /* With LTO we can end up trying to reference something we didn't create
4724 a DIE for. Avoid crashing later on a NULL referenced DIE. */
4725 if (targ_die == NULL)
4726 return;
4728 attr.dw_attr = attr_kind;
4729 attr.dw_attr_val.val_class = dw_val_class_die_ref;
4730 attr.dw_attr_val.val_entry = NULL;
4731 attr.dw_attr_val.v.val_die_ref.die = targ_die;
4732 attr.dw_attr_val.v.val_die_ref.external = 0;
4733 add_dwarf_attr (die, &attr);
4736 /* Change DIE reference REF to point to NEW_DIE instead. */
4738 static inline void
4739 change_AT_die_ref (dw_attr_node *ref, dw_die_ref new_die)
4741 gcc_assert (ref->dw_attr_val.val_class == dw_val_class_die_ref);
4742 ref->dw_attr_val.v.val_die_ref.die = new_die;
4743 ref->dw_attr_val.v.val_die_ref.external = 0;
4746 /* Add an AT_specification attribute to a DIE, and also make the back
4747 pointer from the specification to the definition. */
4749 static inline void
4750 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4752 add_AT_die_ref (die, DW_AT_specification, targ_die);
4753 gcc_assert (!targ_die->die_definition);
4754 targ_die->die_definition = die;
4757 static inline dw_die_ref
4758 AT_ref (dw_attr_node *a)
4760 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4761 return a->dw_attr_val.v.val_die_ref.die;
4764 static inline int
4765 AT_ref_external (dw_attr_node *a)
4767 if (a && AT_class (a) == dw_val_class_die_ref)
4768 return a->dw_attr_val.v.val_die_ref.external;
4770 return 0;
4773 static inline void
4774 set_AT_ref_external (dw_attr_node *a, int i)
4776 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4777 a->dw_attr_val.v.val_die_ref.external = i;
4780 /* Add an FDE reference attribute value to a DIE. */
4782 static inline void
4783 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
4785 dw_attr_node attr;
4787 attr.dw_attr = attr_kind;
4788 attr.dw_attr_val.val_class = dw_val_class_fde_ref;
4789 attr.dw_attr_val.val_entry = NULL;
4790 attr.dw_attr_val.v.val_fde_index = targ_fde;
4791 add_dwarf_attr (die, &attr);
4794 /* Add a location description attribute value to a DIE. */
4796 static inline void
4797 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4799 dw_attr_node attr;
4801 attr.dw_attr = attr_kind;
4802 attr.dw_attr_val.val_class = dw_val_class_loc;
4803 attr.dw_attr_val.val_entry = NULL;
4804 attr.dw_attr_val.v.val_loc = loc;
4805 add_dwarf_attr (die, &attr);
4808 static inline dw_loc_descr_ref
4809 AT_loc (dw_attr_node *a)
4811 gcc_assert (a && AT_class (a) == dw_val_class_loc);
4812 return a->dw_attr_val.v.val_loc;
4815 static inline void
4816 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4818 dw_attr_node attr;
4820 if (XCOFF_DEBUGGING_INFO && !HAVE_XCOFF_DWARF_EXTRAS)
4821 return;
4823 attr.dw_attr = attr_kind;
4824 attr.dw_attr_val.val_class = dw_val_class_loc_list;
4825 attr.dw_attr_val.val_entry = NULL;
4826 attr.dw_attr_val.v.val_loc_list = loc_list;
4827 add_dwarf_attr (die, &attr);
4828 have_location_lists = true;
4831 static inline dw_loc_list_ref
4832 AT_loc_list (dw_attr_node *a)
4834 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4835 return a->dw_attr_val.v.val_loc_list;
4838 /* Add a view list attribute to DIE. It must have a DW_AT_location
4839 attribute, because the view list complements the location list. */
4841 static inline void
4842 add_AT_view_list (dw_die_ref die, enum dwarf_attribute attr_kind)
4844 dw_attr_node attr;
4846 if (XCOFF_DEBUGGING_INFO && !HAVE_XCOFF_DWARF_EXTRAS)
4847 return;
4849 attr.dw_attr = attr_kind;
4850 attr.dw_attr_val.val_class = dw_val_class_view_list;
4851 attr.dw_attr_val.val_entry = NULL;
4852 attr.dw_attr_val.v.val_view_list = die;
4853 add_dwarf_attr (die, &attr);
4854 gcc_checking_assert (get_AT (die, DW_AT_location));
4855 gcc_assert (have_location_lists);
4858 /* Return a pointer to the location list referenced by the attribute.
4859 If the named attribute is a view list, look up the corresponding
4860 DW_AT_location attribute and return its location list. */
4862 static inline dw_loc_list_ref *
4863 AT_loc_list_ptr (dw_attr_node *a)
4865 gcc_assert (a);
4866 switch (AT_class (a))
4868 case dw_val_class_loc_list:
4869 return &a->dw_attr_val.v.val_loc_list;
4870 case dw_val_class_view_list:
4872 dw_attr_node *l;
4873 l = get_AT (a->dw_attr_val.v.val_view_list, DW_AT_location);
4874 if (!l)
4875 return NULL;
4876 gcc_checking_assert (l + 1 == a);
4877 return AT_loc_list_ptr (l);
4879 default:
4880 gcc_unreachable ();
4884 /* Return the location attribute value associated with a view list
4885 attribute value. */
4887 static inline dw_val_node *
4888 view_list_to_loc_list_val_node (dw_val_node *val)
4890 gcc_assert (val->val_class == dw_val_class_view_list);
4891 dw_attr_node *loc = get_AT (val->v.val_view_list, DW_AT_location);
4892 if (!loc)
4893 return NULL;
4894 gcc_checking_assert (&(loc + 1)->dw_attr_val == val);
4895 gcc_assert (AT_class (loc) == dw_val_class_loc_list);
4896 return &loc->dw_attr_val;
4899 struct addr_hasher : ggc_ptr_hash<addr_table_entry>
4901 static hashval_t hash (addr_table_entry *);
4902 static bool equal (addr_table_entry *, addr_table_entry *);
4905 /* Table of entries into the .debug_addr section. */
4907 static GTY (()) hash_table<addr_hasher> *addr_index_table;
4909 /* Hash an address_table_entry. */
4911 hashval_t
4912 addr_hasher::hash (addr_table_entry *a)
4914 inchash::hash hstate;
4915 switch (a->kind)
4917 case ate_kind_rtx:
4918 hstate.add_int (0);
4919 break;
4920 case ate_kind_rtx_dtprel:
4921 hstate.add_int (1);
4922 break;
4923 case ate_kind_label:
4924 return htab_hash_string (a->addr.label);
4925 default:
4926 gcc_unreachable ();
4928 inchash::add_rtx (a->addr.rtl, hstate);
4929 return hstate.end ();
4932 /* Determine equality for two address_table_entries. */
4934 bool
4935 addr_hasher::equal (addr_table_entry *a1, addr_table_entry *a2)
4937 if (a1->kind != a2->kind)
4938 return 0;
4939 switch (a1->kind)
4941 case ate_kind_rtx:
4942 case ate_kind_rtx_dtprel:
4943 return rtx_equal_p (a1->addr.rtl, a2->addr.rtl);
4944 case ate_kind_label:
4945 return strcmp (a1->addr.label, a2->addr.label) == 0;
4946 default:
4947 gcc_unreachable ();
4951 /* Initialize an addr_table_entry. */
4953 void
4954 init_addr_table_entry (addr_table_entry *e, enum ate_kind kind, void *addr)
4956 e->kind = kind;
4957 switch (kind)
4959 case ate_kind_rtx:
4960 case ate_kind_rtx_dtprel:
4961 e->addr.rtl = (rtx) addr;
4962 break;
4963 case ate_kind_label:
4964 e->addr.label = (char *) addr;
4965 break;
4967 e->refcount = 0;
4968 e->index = NO_INDEX_ASSIGNED;
4971 /* Add attr to the address table entry to the table. Defer setting an
4972 index until output time. */
4974 static addr_table_entry *
4975 add_addr_table_entry (void *addr, enum ate_kind kind)
4977 addr_table_entry *node;
4978 addr_table_entry finder;
4980 gcc_assert (dwarf_split_debug_info);
4981 if (! addr_index_table)
4982 addr_index_table = hash_table<addr_hasher>::create_ggc (10);
4983 init_addr_table_entry (&finder, kind, addr);
4984 addr_table_entry **slot = addr_index_table->find_slot (&finder, INSERT);
4986 if (*slot == HTAB_EMPTY_ENTRY)
4988 node = ggc_cleared_alloc<addr_table_entry> ();
4989 init_addr_table_entry (node, kind, addr);
4990 *slot = node;
4992 else
4993 node = *slot;
4995 node->refcount++;
4996 return node;
4999 /* Remove an entry from the addr table by decrementing its refcount.
5000 Strictly, decrementing the refcount would be enough, but the
5001 assertion that the entry is actually in the table has found
5002 bugs. */
5004 static void
5005 remove_addr_table_entry (addr_table_entry *entry)
5007 gcc_assert (dwarf_split_debug_info && addr_index_table);
5008 /* After an index is assigned, the table is frozen. */
5009 gcc_assert (entry->refcount > 0 && entry->index == NO_INDEX_ASSIGNED);
5010 entry->refcount--;
5013 /* Given a location list, remove all addresses it refers to from the
5014 address_table. */
5016 static void
5017 remove_loc_list_addr_table_entries (dw_loc_descr_ref descr)
5019 for (; descr; descr = descr->dw_loc_next)
5020 if (descr->dw_loc_oprnd1.val_entry != NULL)
5022 gcc_assert (descr->dw_loc_oprnd1.val_entry->index == NO_INDEX_ASSIGNED);
5023 remove_addr_table_entry (descr->dw_loc_oprnd1.val_entry);
5027 /* A helper function for dwarf2out_finish called through
5028 htab_traverse. Assign an addr_table_entry its index. All entries
5029 must be collected into the table when this function is called,
5030 because the indexing code relies on htab_traverse to traverse nodes
5031 in the same order for each run. */
5034 index_addr_table_entry (addr_table_entry **h, unsigned int *index)
5036 addr_table_entry *node = *h;
5038 /* Don't index unreferenced nodes. */
5039 if (node->refcount == 0)
5040 return 1;
5042 gcc_assert (node->index == NO_INDEX_ASSIGNED);
5043 node->index = *index;
5044 *index += 1;
5046 return 1;
5049 /* Add an address constant attribute value to a DIE. When using
5050 dwarf_split_debug_info, address attributes in dies destined for the
5051 final executable should be direct references--setting the parameter
5052 force_direct ensures this behavior. */
5054 static inline void
5055 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr,
5056 bool force_direct)
5058 dw_attr_node attr;
5060 attr.dw_attr = attr_kind;
5061 attr.dw_attr_val.val_class = dw_val_class_addr;
5062 attr.dw_attr_val.v.val_addr = addr;
5063 if (dwarf_split_debug_info && !force_direct)
5064 attr.dw_attr_val.val_entry = add_addr_table_entry (addr, ate_kind_rtx);
5065 else
5066 attr.dw_attr_val.val_entry = NULL;
5067 add_dwarf_attr (die, &attr);
5070 /* Get the RTX from to an address DIE attribute. */
5072 static inline rtx
5073 AT_addr (dw_attr_node *a)
5075 gcc_assert (a && AT_class (a) == dw_val_class_addr);
5076 return a->dw_attr_val.v.val_addr;
5079 /* Add a file attribute value to a DIE. */
5081 static inline void
5082 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
5083 struct dwarf_file_data *fd)
5085 dw_attr_node attr;
5087 attr.dw_attr = attr_kind;
5088 attr.dw_attr_val.val_class = dw_val_class_file;
5089 attr.dw_attr_val.val_entry = NULL;
5090 attr.dw_attr_val.v.val_file = fd;
5091 add_dwarf_attr (die, &attr);
5094 /* Get the dwarf_file_data from a file DIE attribute. */
5096 static inline struct dwarf_file_data *
5097 AT_file (dw_attr_node *a)
5099 gcc_assert (a && (AT_class (a) == dw_val_class_file
5100 || AT_class (a) == dw_val_class_file_implicit));
5101 return a->dw_attr_val.v.val_file;
5104 /* Add a vms delta attribute value to a DIE. */
5106 static inline void
5107 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
5108 const char *lbl1, const char *lbl2)
5110 dw_attr_node attr;
5112 attr.dw_attr = attr_kind;
5113 attr.dw_attr_val.val_class = dw_val_class_vms_delta;
5114 attr.dw_attr_val.val_entry = NULL;
5115 attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
5116 attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
5117 add_dwarf_attr (die, &attr);
5120 /* Add a label identifier attribute value to a DIE. */
5122 static inline void
5123 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind,
5124 const char *lbl_id)
5126 dw_attr_node attr;
5128 attr.dw_attr = attr_kind;
5129 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
5130 attr.dw_attr_val.val_entry = NULL;
5131 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
5132 if (dwarf_split_debug_info)
5133 attr.dw_attr_val.val_entry
5134 = add_addr_table_entry (attr.dw_attr_val.v.val_lbl_id,
5135 ate_kind_label);
5136 add_dwarf_attr (die, &attr);
5139 /* Add a section offset attribute value to a DIE, an offset into the
5140 debug_line section. */
5142 static inline void
5143 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
5144 const char *label)
5146 dw_attr_node attr;
5148 attr.dw_attr = attr_kind;
5149 attr.dw_attr_val.val_class = dw_val_class_lineptr;
5150 attr.dw_attr_val.val_entry = NULL;
5151 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
5152 add_dwarf_attr (die, &attr);
5155 /* Add a section offset attribute value to a DIE, an offset into the
5156 debug_loclists section. */
5158 static inline void
5159 add_AT_loclistsptr (dw_die_ref die, enum dwarf_attribute attr_kind,
5160 const char *label)
5162 dw_attr_node attr;
5164 attr.dw_attr = attr_kind;
5165 attr.dw_attr_val.val_class = dw_val_class_loclistsptr;
5166 attr.dw_attr_val.val_entry = NULL;
5167 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
5168 add_dwarf_attr (die, &attr);
5171 /* Add a section offset attribute value to a DIE, an offset into the
5172 debug_macinfo section. */
5174 static inline void
5175 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
5176 const char *label)
5178 dw_attr_node attr;
5180 attr.dw_attr = attr_kind;
5181 attr.dw_attr_val.val_class = dw_val_class_macptr;
5182 attr.dw_attr_val.val_entry = NULL;
5183 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
5184 add_dwarf_attr (die, &attr);
5187 /* Add an offset attribute value to a DIE. */
5189 static inline void
5190 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
5191 unsigned HOST_WIDE_INT offset)
5193 dw_attr_node attr;
5195 attr.dw_attr = attr_kind;
5196 attr.dw_attr_val.val_class = dw_val_class_offset;
5197 attr.dw_attr_val.val_entry = NULL;
5198 attr.dw_attr_val.v.val_offset = offset;
5199 add_dwarf_attr (die, &attr);
5202 /* Add a range_list attribute value to a DIE. When using
5203 dwarf_split_debug_info, address attributes in dies destined for the
5204 final executable should be direct references--setting the parameter
5205 force_direct ensures this behavior. */
5207 #define UNRELOCATED_OFFSET ((addr_table_entry *) 1)
5208 #define RELOCATED_OFFSET (NULL)
5210 static void
5211 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
5212 long unsigned int offset, bool force_direct)
5214 dw_attr_node attr;
5216 attr.dw_attr = attr_kind;
5217 attr.dw_attr_val.val_class = dw_val_class_range_list;
5218 /* For the range_list attribute, use val_entry to store whether the
5219 offset should follow split-debug-info or normal semantics. This
5220 value is read in output_range_list_offset. */
5221 if (dwarf_split_debug_info && !force_direct)
5222 attr.dw_attr_val.val_entry = UNRELOCATED_OFFSET;
5223 else
5224 attr.dw_attr_val.val_entry = RELOCATED_OFFSET;
5225 attr.dw_attr_val.v.val_offset = offset;
5226 add_dwarf_attr (die, &attr);
5229 /* Return the start label of a delta attribute. */
5231 static inline const char *
5232 AT_vms_delta1 (dw_attr_node *a)
5234 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
5235 return a->dw_attr_val.v.val_vms_delta.lbl1;
5238 /* Return the end label of a delta attribute. */
5240 static inline const char *
5241 AT_vms_delta2 (dw_attr_node *a)
5243 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
5244 return a->dw_attr_val.v.val_vms_delta.lbl2;
5247 static inline const char *
5248 AT_lbl (dw_attr_node *a)
5250 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
5251 || AT_class (a) == dw_val_class_lineptr
5252 || AT_class (a) == dw_val_class_macptr
5253 || AT_class (a) == dw_val_class_loclistsptr
5254 || AT_class (a) == dw_val_class_high_pc));
5255 return a->dw_attr_val.v.val_lbl_id;
5258 /* Get the attribute of type attr_kind. */
5260 static dw_attr_node *
5261 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5263 dw_attr_node *a;
5264 unsigned ix;
5265 dw_die_ref spec = NULL;
5267 if (! die)
5268 return NULL;
5270 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5271 if (a->dw_attr == attr_kind)
5272 return a;
5273 else if (a->dw_attr == DW_AT_specification
5274 || a->dw_attr == DW_AT_abstract_origin)
5275 spec = AT_ref (a);
5277 if (spec)
5278 return get_AT (spec, attr_kind);
5280 return NULL;
5283 /* Returns the parent of the declaration of DIE. */
5285 static dw_die_ref
5286 get_die_parent (dw_die_ref die)
5288 dw_die_ref t;
5290 if (!die)
5291 return NULL;
5293 if ((t = get_AT_ref (die, DW_AT_abstract_origin))
5294 || (t = get_AT_ref (die, DW_AT_specification)))
5295 die = t;
5297 return die->die_parent;
5300 /* Return the "low pc" attribute value, typically associated with a subprogram
5301 DIE. Return null if the "low pc" attribute is either not present, or if it
5302 cannot be represented as an assembler label identifier. */
5304 static inline const char *
5305 get_AT_low_pc (dw_die_ref die)
5307 dw_attr_node *a = get_AT (die, DW_AT_low_pc);
5309 return a ? AT_lbl (a) : NULL;
5312 /* Return the "high pc" attribute value, typically associated with a subprogram
5313 DIE. Return null if the "high pc" attribute is either not present, or if it
5314 cannot be represented as an assembler label identifier. */
5316 static inline const char *
5317 get_AT_hi_pc (dw_die_ref die)
5319 dw_attr_node *a = get_AT (die, DW_AT_high_pc);
5321 return a ? AT_lbl (a) : NULL;
5324 /* Return the value of the string attribute designated by ATTR_KIND, or
5325 NULL if it is not present. */
5327 static inline const char *
5328 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
5330 dw_attr_node *a = get_AT (die, attr_kind);
5332 return a ? AT_string (a) : NULL;
5335 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
5336 if it is not present. */
5338 static inline int
5339 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
5341 dw_attr_node *a = get_AT (die, attr_kind);
5343 return a ? AT_flag (a) : 0;
5346 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
5347 if it is not present. */
5349 static inline unsigned
5350 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
5352 dw_attr_node *a = get_AT (die, attr_kind);
5354 return a ? AT_unsigned (a) : 0;
5357 static inline dw_die_ref
5358 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
5360 dw_attr_node *a = get_AT (die, attr_kind);
5362 return a ? AT_ref (a) : NULL;
5365 static inline struct dwarf_file_data *
5366 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
5368 dw_attr_node *a = get_AT (die, attr_kind);
5370 return a ? AT_file (a) : NULL;
5373 /* Return TRUE if the language is C++. */
5375 static inline bool
5376 is_cxx (void)
5378 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5380 return (lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus
5381 || lang == DW_LANG_C_plus_plus_11 || lang == DW_LANG_C_plus_plus_14);
5384 /* Return TRUE if DECL was created by the C++ frontend. */
5386 static bool
5387 is_cxx (const_tree decl)
5389 if (in_lto_p)
5391 const_tree context = get_ultimate_context (decl);
5392 if (context && TRANSLATION_UNIT_LANGUAGE (context))
5393 return strncmp (TRANSLATION_UNIT_LANGUAGE (context), "GNU C++", 7) == 0;
5395 return is_cxx ();
5398 /* Return TRUE if the language is Fortran. */
5400 static inline bool
5401 is_fortran (void)
5403 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5405 return (lang == DW_LANG_Fortran77
5406 || lang == DW_LANG_Fortran90
5407 || lang == DW_LANG_Fortran95
5408 || lang == DW_LANG_Fortran03
5409 || lang == DW_LANG_Fortran08);
5412 static inline bool
5413 is_fortran (const_tree decl)
5415 if (in_lto_p)
5417 const_tree context = get_ultimate_context (decl);
5418 if (context && TRANSLATION_UNIT_LANGUAGE (context))
5419 return (strncmp (TRANSLATION_UNIT_LANGUAGE (context),
5420 "GNU Fortran", 11) == 0
5421 || strcmp (TRANSLATION_UNIT_LANGUAGE (context),
5422 "GNU F77") == 0);
5424 return is_fortran ();
5427 /* Return TRUE if the language is Ada. */
5429 static inline bool
5430 is_ada (void)
5432 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5434 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
5437 /* Remove the specified attribute if present. Return TRUE if removal
5438 was successful. */
5440 static bool
5441 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5443 dw_attr_node *a;
5444 unsigned ix;
5446 if (! die)
5447 return false;
5449 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5450 if (a->dw_attr == attr_kind)
5452 if (AT_class (a) == dw_val_class_str)
5453 if (a->dw_attr_val.v.val_str->refcount)
5454 a->dw_attr_val.v.val_str->refcount--;
5456 /* vec::ordered_remove should help reduce the number of abbrevs
5457 that are needed. */
5458 die->die_attr->ordered_remove (ix);
5459 return true;
5461 return false;
5464 /* Remove CHILD from its parent. PREV must have the property that
5465 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
5467 static void
5468 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
5470 gcc_assert (child->die_parent == prev->die_parent);
5471 gcc_assert (prev->die_sib == child);
5472 if (prev == child)
5474 gcc_assert (child->die_parent->die_child == child);
5475 prev = NULL;
5477 else
5478 prev->die_sib = child->die_sib;
5479 if (child->die_parent->die_child == child)
5480 child->die_parent->die_child = prev;
5481 child->die_sib = NULL;
5484 /* Replace OLD_CHILD with NEW_CHILD. PREV must have the property that
5485 PREV->DIE_SIB == OLD_CHILD. Does not alter OLD_CHILD. */
5487 static void
5488 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
5490 dw_die_ref parent = old_child->die_parent;
5492 gcc_assert (parent == prev->die_parent);
5493 gcc_assert (prev->die_sib == old_child);
5495 new_child->die_parent = parent;
5496 if (prev == old_child)
5498 gcc_assert (parent->die_child == old_child);
5499 new_child->die_sib = new_child;
5501 else
5503 prev->die_sib = new_child;
5504 new_child->die_sib = old_child->die_sib;
5506 if (old_child->die_parent->die_child == old_child)
5507 old_child->die_parent->die_child = new_child;
5508 old_child->die_sib = NULL;
5511 /* Move all children from OLD_PARENT to NEW_PARENT. */
5513 static void
5514 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
5516 dw_die_ref c;
5517 new_parent->die_child = old_parent->die_child;
5518 old_parent->die_child = NULL;
5519 FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
5522 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
5523 matches TAG. */
5525 static void
5526 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
5528 dw_die_ref c;
5530 c = die->die_child;
5531 if (c) do {
5532 dw_die_ref prev = c;
5533 c = c->die_sib;
5534 while (c->die_tag == tag)
5536 remove_child_with_prev (c, prev);
5537 c->die_parent = NULL;
5538 /* Might have removed every child. */
5539 if (die->die_child == NULL)
5540 return;
5541 c = prev->die_sib;
5543 } while (c != die->die_child);
5546 /* Add a CHILD_DIE as the last child of DIE. */
5548 static void
5549 add_child_die (dw_die_ref die, dw_die_ref child_die)
5551 /* FIXME this should probably be an assert. */
5552 if (! die || ! child_die)
5553 return;
5554 gcc_assert (die != child_die);
5556 child_die->die_parent = die;
5557 if (die->die_child)
5559 child_die->die_sib = die->die_child->die_sib;
5560 die->die_child->die_sib = child_die;
5562 else
5563 child_die->die_sib = child_die;
5564 die->die_child = child_die;
5567 /* Like add_child_die, but put CHILD_DIE after AFTER_DIE. */
5569 static void
5570 add_child_die_after (dw_die_ref die, dw_die_ref child_die,
5571 dw_die_ref after_die)
5573 gcc_assert (die
5574 && child_die
5575 && after_die
5576 && die->die_child
5577 && die != child_die);
5579 child_die->die_parent = die;
5580 child_die->die_sib = after_die->die_sib;
5581 after_die->die_sib = child_die;
5582 if (die->die_child == after_die)
5583 die->die_child = child_die;
5586 /* Unassociate CHILD from its parent, and make its parent be
5587 NEW_PARENT. */
5589 static void
5590 reparent_child (dw_die_ref child, dw_die_ref new_parent)
5592 for (dw_die_ref p = child->die_parent->die_child; ; p = p->die_sib)
5593 if (p->die_sib == child)
5595 remove_child_with_prev (child, p);
5596 break;
5598 add_child_die (new_parent, child);
5601 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
5602 is the specification, to the end of PARENT's list of children.
5603 This is done by removing and re-adding it. */
5605 static void
5606 splice_child_die (dw_die_ref parent, dw_die_ref child)
5608 /* We want the declaration DIE from inside the class, not the
5609 specification DIE at toplevel. */
5610 if (child->die_parent != parent)
5612 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
5614 if (tmp)
5615 child = tmp;
5618 gcc_assert (child->die_parent == parent
5619 || (child->die_parent
5620 == get_AT_ref (parent, DW_AT_specification)));
5622 reparent_child (child, parent);
5625 /* Create and return a new die with TAG_VALUE as tag. */
5627 static inline dw_die_ref
5628 new_die_raw (enum dwarf_tag tag_value)
5630 dw_die_ref die = ggc_cleared_alloc<die_node> ();
5631 die->die_tag = tag_value;
5632 return die;
5635 /* Create and return a new die with a parent of PARENT_DIE. If
5636 PARENT_DIE is NULL, the new DIE is placed in limbo and an
5637 associated tree T must be supplied to determine parenthood
5638 later. */
5640 static inline dw_die_ref
5641 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
5643 dw_die_ref die = new_die_raw (tag_value);
5645 if (parent_die != NULL)
5646 add_child_die (parent_die, die);
5647 else
5649 limbo_die_node *limbo_node;
5651 /* No DIEs created after early dwarf should end up in limbo,
5652 because the limbo list should not persist past LTO
5653 streaming. */
5654 if (tag_value != DW_TAG_compile_unit
5655 /* These are allowed because they're generated while
5656 breaking out COMDAT units late. */
5657 && tag_value != DW_TAG_type_unit
5658 && tag_value != DW_TAG_skeleton_unit
5659 && !early_dwarf
5660 /* Allow nested functions to live in limbo because they will
5661 only temporarily live there, as decls_for_scope will fix
5662 them up. */
5663 && (TREE_CODE (t) != FUNCTION_DECL
5664 || !decl_function_context (t))
5665 /* Same as nested functions above but for types. Types that
5666 are local to a function will be fixed in
5667 decls_for_scope. */
5668 && (!RECORD_OR_UNION_TYPE_P (t)
5669 || !TYPE_CONTEXT (t)
5670 || TREE_CODE (TYPE_CONTEXT (t)) != FUNCTION_DECL)
5671 /* FIXME debug-early: Allow late limbo DIE creation for LTO,
5672 especially in the ltrans stage, but once we implement LTO
5673 dwarf streaming, we should remove this exception. */
5674 && !in_lto_p)
5676 fprintf (stderr, "symbol ended up in limbo too late:");
5677 debug_generic_stmt (t);
5678 gcc_unreachable ();
5681 limbo_node = ggc_cleared_alloc<limbo_die_node> ();
5682 limbo_node->die = die;
5683 limbo_node->created_for = t;
5684 limbo_node->next = limbo_die_list;
5685 limbo_die_list = limbo_node;
5688 return die;
5691 /* Return the DIE associated with the given type specifier. */
5693 static inline dw_die_ref
5694 lookup_type_die (tree type)
5696 dw_die_ref die = TYPE_SYMTAB_DIE (type);
5697 if (die && die->removed)
5699 TYPE_SYMTAB_DIE (type) = NULL;
5700 return NULL;
5702 return die;
5705 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
5706 anonymous type named by the typedef TYPE_DIE, return the DIE of the
5707 anonymous type instead the one of the naming typedef. */
5709 static inline dw_die_ref
5710 strip_naming_typedef (tree type, dw_die_ref type_die)
5712 if (type
5713 && TREE_CODE (type) == RECORD_TYPE
5714 && type_die
5715 && type_die->die_tag == DW_TAG_typedef
5716 && is_naming_typedef_decl (TYPE_NAME (type)))
5717 type_die = get_AT_ref (type_die, DW_AT_type);
5718 return type_die;
5721 /* Like lookup_type_die, but if type is an anonymous type named by a
5722 typedef[1], return the DIE of the anonymous type instead the one of
5723 the naming typedef. This is because in gen_typedef_die, we did
5724 equate the anonymous struct named by the typedef with the DIE of
5725 the naming typedef. So by default, lookup_type_die on an anonymous
5726 struct yields the DIE of the naming typedef.
5728 [1]: Read the comment of is_naming_typedef_decl to learn about what
5729 a naming typedef is. */
5731 static inline dw_die_ref
5732 lookup_type_die_strip_naming_typedef (tree type)
5734 dw_die_ref die = lookup_type_die (type);
5735 return strip_naming_typedef (type, die);
5738 /* Equate a DIE to a given type specifier. */
5740 static inline void
5741 equate_type_number_to_die (tree type, dw_die_ref type_die)
5743 TYPE_SYMTAB_DIE (type) = type_die;
5746 /* Returns a hash value for X (which really is a die_struct). */
5748 inline hashval_t
5749 decl_die_hasher::hash (die_node *x)
5751 return (hashval_t) x->decl_id;
5754 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
5756 inline bool
5757 decl_die_hasher::equal (die_node *x, tree y)
5759 return (x->decl_id == DECL_UID (y));
5762 /* Return the DIE associated with a given declaration. */
5764 static inline dw_die_ref
5765 lookup_decl_die (tree decl)
5767 dw_die_ref *die = decl_die_table->find_slot_with_hash (decl, DECL_UID (decl),
5768 NO_INSERT);
5769 if (!die)
5770 return NULL;
5771 if ((*die)->removed)
5773 decl_die_table->clear_slot (die);
5774 return NULL;
5776 return *die;
5780 /* For DECL which might have early dwarf output query a SYMBOL + OFFSET
5781 style reference. Return true if we found one refering to a DIE for
5782 DECL, otherwise return false. */
5784 static bool
5785 dwarf2out_die_ref_for_decl (tree decl, const char **sym,
5786 unsigned HOST_WIDE_INT *off)
5788 dw_die_ref die;
5790 if (flag_wpa && !decl_die_table)
5791 return false;
5793 if (TREE_CODE (decl) == BLOCK)
5794 die = BLOCK_DIE (decl);
5795 else
5796 die = lookup_decl_die (decl);
5797 if (!die)
5798 return false;
5800 /* During WPA stage we currently use DIEs to store the
5801 decl <-> label + offset map. That's quite inefficient but it
5802 works for now. */
5803 if (flag_wpa)
5805 dw_die_ref ref = get_AT_ref (die, DW_AT_abstract_origin);
5806 if (!ref)
5808 gcc_assert (die == comp_unit_die ());
5809 return false;
5811 *off = ref->die_offset;
5812 *sym = ref->die_id.die_symbol;
5813 return true;
5816 /* Similar to get_ref_die_offset_label, but using the "correct"
5817 label. */
5818 *off = die->die_offset;
5819 while (die->die_parent)
5820 die = die->die_parent;
5821 /* For the containing CU DIE we compute a die_symbol in
5822 compute_comp_unit_symbol. */
5823 gcc_assert (die->die_tag == DW_TAG_compile_unit
5824 && die->die_id.die_symbol != NULL);
5825 *sym = die->die_id.die_symbol;
5826 return true;
5829 /* Add a reference of kind ATTR_KIND to a DIE at SYMBOL + OFFSET to DIE. */
5831 static void
5832 add_AT_external_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind,
5833 const char *symbol, HOST_WIDE_INT offset)
5835 /* Create a fake DIE that contains the reference. Don't use
5836 new_die because we don't want to end up in the limbo list. */
5837 dw_die_ref ref = new_die_raw (die->die_tag);
5838 ref->die_id.die_symbol = IDENTIFIER_POINTER (get_identifier (symbol));
5839 ref->die_offset = offset;
5840 ref->with_offset = 1;
5841 add_AT_die_ref (die, attr_kind, ref);
5844 /* Create a DIE for DECL if required and add a reference to a DIE
5845 at SYMBOL + OFFSET which contains attributes dumped early. */
5847 static void
5848 dwarf2out_register_external_die (tree decl, const char *sym,
5849 unsigned HOST_WIDE_INT off)
5851 if (debug_info_level == DINFO_LEVEL_NONE)
5852 return;
5854 if (flag_wpa && !decl_die_table)
5855 decl_die_table = hash_table<decl_die_hasher>::create_ggc (1000);
5857 dw_die_ref die
5858 = TREE_CODE (decl) == BLOCK ? BLOCK_DIE (decl) : lookup_decl_die (decl);
5859 gcc_assert (!die);
5861 tree ctx;
5862 dw_die_ref parent = NULL;
5863 /* Need to lookup a DIE for the decls context - the containing
5864 function or translation unit. */
5865 if (TREE_CODE (decl) == BLOCK)
5867 ctx = BLOCK_SUPERCONTEXT (decl);
5868 /* ??? We do not output DIEs for all scopes thus skip as
5869 many DIEs as needed. */
5870 while (TREE_CODE (ctx) == BLOCK
5871 && !BLOCK_DIE (ctx))
5872 ctx = BLOCK_SUPERCONTEXT (ctx);
5874 else
5875 ctx = DECL_CONTEXT (decl);
5876 while (ctx && TYPE_P (ctx))
5877 ctx = TYPE_CONTEXT (ctx);
5878 if (ctx)
5880 if (TREE_CODE (ctx) == BLOCK)
5881 parent = BLOCK_DIE (ctx);
5882 else if (TREE_CODE (ctx) == TRANSLATION_UNIT_DECL
5883 /* Keep the 1:1 association during WPA. */
5884 && !flag_wpa)
5885 /* Otherwise all late annotations go to the main CU which
5886 imports the original CUs. */
5887 parent = comp_unit_die ();
5888 else if (TREE_CODE (ctx) == FUNCTION_DECL
5889 && TREE_CODE (decl) != PARM_DECL
5890 && TREE_CODE (decl) != BLOCK)
5891 /* Leave function local entities parent determination to when
5892 we process scope vars. */
5894 else
5895 parent = lookup_decl_die (ctx);
5897 else
5898 /* In some cases the FEs fail to set DECL_CONTEXT properly.
5899 Handle this case gracefully by globalizing stuff. */
5900 parent = comp_unit_die ();
5901 /* Create a DIE "stub". */
5902 switch (TREE_CODE (decl))
5904 case TRANSLATION_UNIT_DECL:
5905 if (! flag_wpa)
5907 die = comp_unit_die ();
5908 dw_die_ref import = new_die (DW_TAG_imported_unit, die, NULL_TREE);
5909 add_AT_external_die_ref (import, DW_AT_import, sym, off);
5910 /* We re-target all CU decls to the LTRANS CU DIE, so no need
5911 to create a DIE for the original CUs. */
5912 return;
5914 /* Keep the 1:1 association during WPA. */
5915 die = new_die (DW_TAG_compile_unit, NULL, decl);
5916 break;
5917 case NAMESPACE_DECL:
5918 if (is_fortran (decl))
5919 die = new_die (DW_TAG_module, parent, decl);
5920 else
5921 die = new_die (DW_TAG_namespace, parent, decl);
5922 break;
5923 case FUNCTION_DECL:
5924 die = new_die (DW_TAG_subprogram, parent, decl);
5925 break;
5926 case VAR_DECL:
5927 die = new_die (DW_TAG_variable, parent, decl);
5928 break;
5929 case RESULT_DECL:
5930 die = new_die (DW_TAG_variable, parent, decl);
5931 break;
5932 case PARM_DECL:
5933 die = new_die (DW_TAG_formal_parameter, parent, decl);
5934 break;
5935 case CONST_DECL:
5936 die = new_die (DW_TAG_constant, parent, decl);
5937 break;
5938 case LABEL_DECL:
5939 die = new_die (DW_TAG_label, parent, decl);
5940 break;
5941 case BLOCK:
5942 die = new_die (DW_TAG_lexical_block, parent, decl);
5943 break;
5944 default:
5945 gcc_unreachable ();
5947 if (TREE_CODE (decl) == BLOCK)
5948 BLOCK_DIE (decl) = die;
5949 else
5950 equate_decl_number_to_die (decl, die);
5952 /* Add a reference to the DIE providing early debug at $sym + off. */
5953 add_AT_external_die_ref (die, DW_AT_abstract_origin, sym, off);
5956 /* Returns a hash value for X (which really is a var_loc_list). */
5958 inline hashval_t
5959 decl_loc_hasher::hash (var_loc_list *x)
5961 return (hashval_t) x->decl_id;
5964 /* Return nonzero if decl_id of var_loc_list X is the same as
5965 UID of decl *Y. */
5967 inline bool
5968 decl_loc_hasher::equal (var_loc_list *x, const_tree y)
5970 return (x->decl_id == DECL_UID (y));
5973 /* Return the var_loc list associated with a given declaration. */
5975 static inline var_loc_list *
5976 lookup_decl_loc (const_tree decl)
5978 if (!decl_loc_table)
5979 return NULL;
5980 return decl_loc_table->find_with_hash (decl, DECL_UID (decl));
5983 /* Returns a hash value for X (which really is a cached_dw_loc_list_list). */
5985 inline hashval_t
5986 dw_loc_list_hasher::hash (cached_dw_loc_list *x)
5988 return (hashval_t) x->decl_id;
5991 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
5992 UID of decl *Y. */
5994 inline bool
5995 dw_loc_list_hasher::equal (cached_dw_loc_list *x, const_tree y)
5997 return (x->decl_id == DECL_UID (y));
6000 /* Equate a DIE to a particular declaration. */
6002 static void
6003 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
6005 unsigned int decl_id = DECL_UID (decl);
6007 *decl_die_table->find_slot_with_hash (decl, decl_id, INSERT) = decl_die;
6008 decl_die->decl_id = decl_id;
6011 /* Return how many bits covers PIECE EXPR_LIST. */
6013 static HOST_WIDE_INT
6014 decl_piece_bitsize (rtx piece)
6016 int ret = (int) GET_MODE (piece);
6017 if (ret)
6018 return ret;
6019 gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
6020 && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
6021 return INTVAL (XEXP (XEXP (piece, 0), 0));
6024 /* Return pointer to the location of location note in PIECE EXPR_LIST. */
6026 static rtx *
6027 decl_piece_varloc_ptr (rtx piece)
6029 if ((int) GET_MODE (piece))
6030 return &XEXP (piece, 0);
6031 else
6032 return &XEXP (XEXP (piece, 0), 1);
6035 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
6036 Next is the chain of following piece nodes. */
6038 static rtx_expr_list *
6039 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
6041 if (bitsize > 0 && bitsize <= (int) MAX_MACHINE_MODE)
6042 return alloc_EXPR_LIST (bitsize, loc_note, next);
6043 else
6044 return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
6045 GEN_INT (bitsize),
6046 loc_note), next);
6049 /* Return rtx that should be stored into loc field for
6050 LOC_NOTE and BITPOS/BITSIZE. */
6052 static rtx
6053 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
6054 HOST_WIDE_INT bitsize)
6056 if (bitsize != -1)
6058 loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
6059 if (bitpos != 0)
6060 loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
6062 return loc_note;
6065 /* This function either modifies location piece list *DEST in
6066 place (if SRC and INNER is NULL), or copies location piece list
6067 *SRC to *DEST while modifying it. Location BITPOS is modified
6068 to contain LOC_NOTE, any pieces overlapping it are removed resp.
6069 not copied and if needed some padding around it is added.
6070 When modifying in place, DEST should point to EXPR_LIST where
6071 earlier pieces cover PIECE_BITPOS bits, when copying SRC points
6072 to the start of the whole list and INNER points to the EXPR_LIST
6073 where earlier pieces cover PIECE_BITPOS bits. */
6075 static void
6076 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
6077 HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
6078 HOST_WIDE_INT bitsize, rtx loc_note)
6080 HOST_WIDE_INT diff;
6081 bool copy = inner != NULL;
6083 if (copy)
6085 /* First copy all nodes preceding the current bitpos. */
6086 while (src != inner)
6088 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
6089 decl_piece_bitsize (*src), NULL_RTX);
6090 dest = &XEXP (*dest, 1);
6091 src = &XEXP (*src, 1);
6094 /* Add padding if needed. */
6095 if (bitpos != piece_bitpos)
6097 *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
6098 copy ? NULL_RTX : *dest);
6099 dest = &XEXP (*dest, 1);
6101 else if (*dest && decl_piece_bitsize (*dest) == bitsize)
6103 gcc_assert (!copy);
6104 /* A piece with correct bitpos and bitsize already exist,
6105 just update the location for it and return. */
6106 *decl_piece_varloc_ptr (*dest) = loc_note;
6107 return;
6109 /* Add the piece that changed. */
6110 *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
6111 dest = &XEXP (*dest, 1);
6112 /* Skip over pieces that overlap it. */
6113 diff = bitpos - piece_bitpos + bitsize;
6114 if (!copy)
6115 src = dest;
6116 while (diff > 0 && *src)
6118 rtx piece = *src;
6119 diff -= decl_piece_bitsize (piece);
6120 if (copy)
6121 src = &XEXP (piece, 1);
6122 else
6124 *src = XEXP (piece, 1);
6125 free_EXPR_LIST_node (piece);
6128 /* Add padding if needed. */
6129 if (diff < 0 && *src)
6131 if (!copy)
6132 dest = src;
6133 *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
6134 dest = &XEXP (*dest, 1);
6136 if (!copy)
6137 return;
6138 /* Finally copy all nodes following it. */
6139 while (*src)
6141 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
6142 decl_piece_bitsize (*src), NULL_RTX);
6143 dest = &XEXP (*dest, 1);
6144 src = &XEXP (*src, 1);
6148 /* Add a variable location node to the linked list for DECL. */
6150 static struct var_loc_node *
6151 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label, var_loc_view view)
6153 unsigned int decl_id;
6154 var_loc_list *temp;
6155 struct var_loc_node *loc = NULL;
6156 HOST_WIDE_INT bitsize = -1, bitpos = -1;
6158 if (VAR_P (decl) && DECL_HAS_DEBUG_EXPR_P (decl))
6160 tree realdecl = DECL_DEBUG_EXPR (decl);
6161 if (handled_component_p (realdecl)
6162 || (TREE_CODE (realdecl) == MEM_REF
6163 && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR))
6165 bool reverse;
6166 tree innerdecl = get_ref_base_and_extent_hwi (realdecl, &bitpos,
6167 &bitsize, &reverse);
6168 if (!innerdecl
6169 || !DECL_P (innerdecl)
6170 || DECL_IGNORED_P (innerdecl)
6171 || TREE_STATIC (innerdecl)
6172 || bitsize == 0
6173 || bitpos + bitsize > 256)
6174 return NULL;
6175 decl = innerdecl;
6179 decl_id = DECL_UID (decl);
6180 var_loc_list **slot
6181 = decl_loc_table->find_slot_with_hash (decl, decl_id, INSERT);
6182 if (*slot == NULL)
6184 temp = ggc_cleared_alloc<var_loc_list> ();
6185 temp->decl_id = decl_id;
6186 *slot = temp;
6188 else
6189 temp = *slot;
6191 /* For PARM_DECLs try to keep around the original incoming value,
6192 even if that means we'll emit a zero-range .debug_loc entry. */
6193 if (temp->last
6194 && temp->first == temp->last
6195 && TREE_CODE (decl) == PARM_DECL
6196 && NOTE_P (temp->first->loc)
6197 && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
6198 && DECL_INCOMING_RTL (decl)
6199 && NOTE_VAR_LOCATION_LOC (temp->first->loc)
6200 && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
6201 == GET_CODE (DECL_INCOMING_RTL (decl))
6202 && prev_real_insn (as_a<rtx_insn *> (temp->first->loc)) == NULL_RTX
6203 && (bitsize != -1
6204 || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
6205 NOTE_VAR_LOCATION_LOC (loc_note))
6206 || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
6207 != NOTE_VAR_LOCATION_STATUS (loc_note))))
6209 loc = ggc_cleared_alloc<var_loc_node> ();
6210 temp->first->next = loc;
6211 temp->last = loc;
6212 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
6214 else if (temp->last)
6216 struct var_loc_node *last = temp->last, *unused = NULL;
6217 rtx *piece_loc = NULL, last_loc_note;
6218 HOST_WIDE_INT piece_bitpos = 0;
6219 if (last->next)
6221 last = last->next;
6222 gcc_assert (last->next == NULL);
6224 if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
6226 piece_loc = &last->loc;
6229 HOST_WIDE_INT cur_bitsize = decl_piece_bitsize (*piece_loc);
6230 if (piece_bitpos + cur_bitsize > bitpos)
6231 break;
6232 piece_bitpos += cur_bitsize;
6233 piece_loc = &XEXP (*piece_loc, 1);
6235 while (*piece_loc);
6237 /* TEMP->LAST here is either pointer to the last but one or
6238 last element in the chained list, LAST is pointer to the
6239 last element. */
6240 if (label && strcmp (last->label, label) == 0 && last->view == view)
6242 /* For SRA optimized variables if there weren't any real
6243 insns since last note, just modify the last node. */
6244 if (piece_loc != NULL)
6246 adjust_piece_list (piece_loc, NULL, NULL,
6247 bitpos, piece_bitpos, bitsize, loc_note);
6248 return NULL;
6250 /* If the last note doesn't cover any instructions, remove it. */
6251 if (temp->last != last)
6253 temp->last->next = NULL;
6254 unused = last;
6255 last = temp->last;
6256 gcc_assert (strcmp (last->label, label) != 0 || last->view != view);
6258 else
6260 gcc_assert (temp->first == temp->last
6261 || (temp->first->next == temp->last
6262 && TREE_CODE (decl) == PARM_DECL));
6263 memset (temp->last, '\0', sizeof (*temp->last));
6264 temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
6265 return temp->last;
6268 if (bitsize == -1 && NOTE_P (last->loc))
6269 last_loc_note = last->loc;
6270 else if (piece_loc != NULL
6271 && *piece_loc != NULL_RTX
6272 && piece_bitpos == bitpos
6273 && decl_piece_bitsize (*piece_loc) == bitsize)
6274 last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
6275 else
6276 last_loc_note = NULL_RTX;
6277 /* If the current location is the same as the end of the list,
6278 and either both or neither of the locations is uninitialized,
6279 we have nothing to do. */
6280 if (last_loc_note == NULL_RTX
6281 || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
6282 NOTE_VAR_LOCATION_LOC (loc_note)))
6283 || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
6284 != NOTE_VAR_LOCATION_STATUS (loc_note))
6285 && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
6286 == VAR_INIT_STATUS_UNINITIALIZED)
6287 || (NOTE_VAR_LOCATION_STATUS (loc_note)
6288 == VAR_INIT_STATUS_UNINITIALIZED))))
6290 /* Add LOC to the end of list and update LAST. If the last
6291 element of the list has been removed above, reuse its
6292 memory for the new node, otherwise allocate a new one. */
6293 if (unused)
6295 loc = unused;
6296 memset (loc, '\0', sizeof (*loc));
6298 else
6299 loc = ggc_cleared_alloc<var_loc_node> ();
6300 if (bitsize == -1 || piece_loc == NULL)
6301 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
6302 else
6303 adjust_piece_list (&loc->loc, &last->loc, piece_loc,
6304 bitpos, piece_bitpos, bitsize, loc_note);
6305 last->next = loc;
6306 /* Ensure TEMP->LAST will point either to the new last but one
6307 element of the chain, or to the last element in it. */
6308 if (last != temp->last)
6309 temp->last = last;
6311 else if (unused)
6312 ggc_free (unused);
6314 else
6316 loc = ggc_cleared_alloc<var_loc_node> ();
6317 temp->first = loc;
6318 temp->last = loc;
6319 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
6321 return loc;
6324 /* Keep track of the number of spaces used to indent the
6325 output of the debugging routines that print the structure of
6326 the DIE internal representation. */
6327 static int print_indent;
6329 /* Indent the line the number of spaces given by print_indent. */
6331 static inline void
6332 print_spaces (FILE *outfile)
6334 fprintf (outfile, "%*s", print_indent, "");
6337 /* Print a type signature in hex. */
6339 static inline void
6340 print_signature (FILE *outfile, char *sig)
6342 int i;
6344 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
6345 fprintf (outfile, "%02x", sig[i] & 0xff);
6348 static inline void
6349 print_discr_value (FILE *outfile, dw_discr_value *discr_value)
6351 if (discr_value->pos)
6352 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, discr_value->v.sval);
6353 else
6354 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, discr_value->v.uval);
6357 static void print_loc_descr (dw_loc_descr_ref, FILE *);
6359 /* Print the value associated to the VAL DWARF value node to OUTFILE. If
6360 RECURSE, output location descriptor operations. */
6362 static void
6363 print_dw_val (dw_val_node *val, bool recurse, FILE *outfile)
6365 switch (val->val_class)
6367 case dw_val_class_addr:
6368 fprintf (outfile, "address");
6369 break;
6370 case dw_val_class_offset:
6371 fprintf (outfile, "offset");
6372 break;
6373 case dw_val_class_loc:
6374 fprintf (outfile, "location descriptor");
6375 if (val->v.val_loc == NULL)
6376 fprintf (outfile, " -> <null>\n");
6377 else if (recurse)
6379 fprintf (outfile, ":\n");
6380 print_indent += 4;
6381 print_loc_descr (val->v.val_loc, outfile);
6382 print_indent -= 4;
6384 else
6385 fprintf (outfile, " (%p)\n", (void *) val->v.val_loc);
6386 break;
6387 case dw_val_class_loc_list:
6388 fprintf (outfile, "location list -> label:%s",
6389 val->v.val_loc_list->ll_symbol);
6390 break;
6391 case dw_val_class_view_list:
6392 val = view_list_to_loc_list_val_node (val);
6393 fprintf (outfile, "location list with views -> labels:%s and %s",
6394 val->v.val_loc_list->ll_symbol,
6395 val->v.val_loc_list->vl_symbol);
6396 break;
6397 case dw_val_class_range_list:
6398 fprintf (outfile, "range list");
6399 break;
6400 case dw_val_class_const:
6401 case dw_val_class_const_implicit:
6402 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, val->v.val_int);
6403 break;
6404 case dw_val_class_unsigned_const:
6405 case dw_val_class_unsigned_const_implicit:
6406 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, val->v.val_unsigned);
6407 break;
6408 case dw_val_class_const_double:
6409 fprintf (outfile, "constant (" HOST_WIDE_INT_PRINT_DEC","\
6410 HOST_WIDE_INT_PRINT_UNSIGNED")",
6411 val->v.val_double.high,
6412 val->v.val_double.low);
6413 break;
6414 case dw_val_class_wide_int:
6416 int i = val->v.val_wide->get_len ();
6417 fprintf (outfile, "constant (");
6418 gcc_assert (i > 0);
6419 if (val->v.val_wide->elt (i - 1) == 0)
6420 fprintf (outfile, "0x");
6421 fprintf (outfile, HOST_WIDE_INT_PRINT_HEX,
6422 val->v.val_wide->elt (--i));
6423 while (--i >= 0)
6424 fprintf (outfile, HOST_WIDE_INT_PRINT_PADDED_HEX,
6425 val->v.val_wide->elt (i));
6426 fprintf (outfile, ")");
6427 break;
6429 case dw_val_class_vec:
6430 fprintf (outfile, "floating-point or vector constant");
6431 break;
6432 case dw_val_class_flag:
6433 fprintf (outfile, "%u", val->v.val_flag);
6434 break;
6435 case dw_val_class_die_ref:
6436 if (val->v.val_die_ref.die != NULL)
6438 dw_die_ref die = val->v.val_die_ref.die;
6440 if (die->comdat_type_p)
6442 fprintf (outfile, "die -> signature: ");
6443 print_signature (outfile,
6444 die->die_id.die_type_node->signature);
6446 else if (die->die_id.die_symbol)
6448 fprintf (outfile, "die -> label: %s", die->die_id.die_symbol);
6449 if (die->with_offset)
6450 fprintf (outfile, " + %ld", die->die_offset);
6452 else
6453 fprintf (outfile, "die -> %ld", die->die_offset);
6454 fprintf (outfile, " (%p)", (void *) die);
6456 else
6457 fprintf (outfile, "die -> <null>");
6458 break;
6459 case dw_val_class_vms_delta:
6460 fprintf (outfile, "delta: @slotcount(%s-%s)",
6461 val->v.val_vms_delta.lbl2, val->v.val_vms_delta.lbl1);
6462 break;
6463 case dw_val_class_lbl_id:
6464 case dw_val_class_lineptr:
6465 case dw_val_class_macptr:
6466 case dw_val_class_loclistsptr:
6467 case dw_val_class_high_pc:
6468 fprintf (outfile, "label: %s", val->v.val_lbl_id);
6469 break;
6470 case dw_val_class_str:
6471 if (val->v.val_str->str != NULL)
6472 fprintf (outfile, "\"%s\"", val->v.val_str->str);
6473 else
6474 fprintf (outfile, "<null>");
6475 break;
6476 case dw_val_class_file:
6477 case dw_val_class_file_implicit:
6478 fprintf (outfile, "\"%s\" (%d)", val->v.val_file->filename,
6479 val->v.val_file->emitted_number);
6480 break;
6481 case dw_val_class_data8:
6483 int i;
6485 for (i = 0; i < 8; i++)
6486 fprintf (outfile, "%02x", val->v.val_data8[i]);
6487 break;
6489 case dw_val_class_discr_value:
6490 print_discr_value (outfile, &val->v.val_discr_value);
6491 break;
6492 case dw_val_class_discr_list:
6493 for (dw_discr_list_ref node = val->v.val_discr_list;
6494 node != NULL;
6495 node = node->dw_discr_next)
6497 if (node->dw_discr_range)
6499 fprintf (outfile, " .. ");
6500 print_discr_value (outfile, &node->dw_discr_lower_bound);
6501 print_discr_value (outfile, &node->dw_discr_upper_bound);
6503 else
6504 print_discr_value (outfile, &node->dw_discr_lower_bound);
6506 if (node->dw_discr_next != NULL)
6507 fprintf (outfile, " | ");
6509 default:
6510 break;
6514 /* Likewise, for a DIE attribute. */
6516 static void
6517 print_attribute (dw_attr_node *a, bool recurse, FILE *outfile)
6519 print_dw_val (&a->dw_attr_val, recurse, outfile);
6523 /* Print the list of operands in the LOC location description to OUTFILE. This
6524 routine is a debugging aid only. */
6526 static void
6527 print_loc_descr (dw_loc_descr_ref loc, FILE *outfile)
6529 dw_loc_descr_ref l = loc;
6531 if (loc == NULL)
6533 print_spaces (outfile);
6534 fprintf (outfile, "<null>\n");
6535 return;
6538 for (l = loc; l != NULL; l = l->dw_loc_next)
6540 print_spaces (outfile);
6541 fprintf (outfile, "(%p) %s",
6542 (void *) l,
6543 dwarf_stack_op_name (l->dw_loc_opc));
6544 if (l->dw_loc_oprnd1.val_class != dw_val_class_none)
6546 fprintf (outfile, " ");
6547 print_dw_val (&l->dw_loc_oprnd1, false, outfile);
6549 if (l->dw_loc_oprnd2.val_class != dw_val_class_none)
6551 fprintf (outfile, ", ");
6552 print_dw_val (&l->dw_loc_oprnd2, false, outfile);
6554 fprintf (outfile, "\n");
6558 /* Print the information associated with a given DIE, and its children.
6559 This routine is a debugging aid only. */
6561 static void
6562 print_die (dw_die_ref die, FILE *outfile)
6564 dw_attr_node *a;
6565 dw_die_ref c;
6566 unsigned ix;
6568 print_spaces (outfile);
6569 fprintf (outfile, "DIE %4ld: %s (%p)\n",
6570 die->die_offset, dwarf_tag_name (die->die_tag),
6571 (void*) die);
6572 print_spaces (outfile);
6573 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
6574 fprintf (outfile, " offset: %ld", die->die_offset);
6575 fprintf (outfile, " mark: %d\n", die->die_mark);
6577 if (die->comdat_type_p)
6579 print_spaces (outfile);
6580 fprintf (outfile, " signature: ");
6581 print_signature (outfile, die->die_id.die_type_node->signature);
6582 fprintf (outfile, "\n");
6585 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6587 print_spaces (outfile);
6588 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
6590 print_attribute (a, true, outfile);
6591 fprintf (outfile, "\n");
6594 if (die->die_child != NULL)
6596 print_indent += 4;
6597 FOR_EACH_CHILD (die, c, print_die (c, outfile));
6598 print_indent -= 4;
6600 if (print_indent == 0)
6601 fprintf (outfile, "\n");
6604 /* Print the list of operations in the LOC location description. */
6606 DEBUG_FUNCTION void
6607 debug_dwarf_loc_descr (dw_loc_descr_ref loc)
6609 print_loc_descr (loc, stderr);
6612 /* Print the information collected for a given DIE. */
6614 DEBUG_FUNCTION void
6615 debug_dwarf_die (dw_die_ref die)
6617 print_die (die, stderr);
6620 DEBUG_FUNCTION void
6621 debug (die_struct &ref)
6623 print_die (&ref, stderr);
6626 DEBUG_FUNCTION void
6627 debug (die_struct *ptr)
6629 if (ptr)
6630 debug (*ptr);
6631 else
6632 fprintf (stderr, "<nil>\n");
6636 /* Print all DWARF information collected for the compilation unit.
6637 This routine is a debugging aid only. */
6639 DEBUG_FUNCTION void
6640 debug_dwarf (void)
6642 print_indent = 0;
6643 print_die (comp_unit_die (), stderr);
6646 /* Verify the DIE tree structure. */
6648 DEBUG_FUNCTION void
6649 verify_die (dw_die_ref die)
6651 gcc_assert (!die->die_mark);
6652 if (die->die_parent == NULL
6653 && die->die_sib == NULL)
6654 return;
6655 /* Verify the die_sib list is cyclic. */
6656 dw_die_ref x = die;
6659 x->die_mark = 1;
6660 x = x->die_sib;
6662 while (x && !x->die_mark);
6663 gcc_assert (x == die);
6664 x = die;
6667 /* Verify all dies have the same parent. */
6668 gcc_assert (x->die_parent == die->die_parent);
6669 if (x->die_child)
6671 /* Verify the child has the proper parent and recurse. */
6672 gcc_assert (x->die_child->die_parent == x);
6673 verify_die (x->die_child);
6675 x->die_mark = 0;
6676 x = x->die_sib;
6678 while (x && x->die_mark);
6681 /* Sanity checks on DIEs. */
6683 static void
6684 check_die (dw_die_ref die)
6686 unsigned ix;
6687 dw_attr_node *a;
6688 bool inline_found = false;
6689 int n_location = 0, n_low_pc = 0, n_high_pc = 0, n_artificial = 0;
6690 int n_decl_line = 0, n_decl_column = 0, n_decl_file = 0;
6691 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6693 switch (a->dw_attr)
6695 case DW_AT_inline:
6696 if (a->dw_attr_val.v.val_unsigned)
6697 inline_found = true;
6698 break;
6699 case DW_AT_location:
6700 ++n_location;
6701 break;
6702 case DW_AT_low_pc:
6703 ++n_low_pc;
6704 break;
6705 case DW_AT_high_pc:
6706 ++n_high_pc;
6707 break;
6708 case DW_AT_artificial:
6709 ++n_artificial;
6710 break;
6711 case DW_AT_decl_column:
6712 ++n_decl_column;
6713 break;
6714 case DW_AT_decl_line:
6715 ++n_decl_line;
6716 break;
6717 case DW_AT_decl_file:
6718 ++n_decl_file;
6719 break;
6720 default:
6721 break;
6724 if (n_location > 1 || n_low_pc > 1 || n_high_pc > 1 || n_artificial > 1
6725 || n_decl_column > 1 || n_decl_line > 1 || n_decl_file > 1)
6727 fprintf (stderr, "Duplicate attributes in DIE:\n");
6728 debug_dwarf_die (die);
6729 gcc_unreachable ();
6731 if (inline_found)
6733 /* A debugging information entry that is a member of an abstract
6734 instance tree [that has DW_AT_inline] should not contain any
6735 attributes which describe aspects of the subroutine which vary
6736 between distinct inlined expansions or distinct out-of-line
6737 expansions. */
6738 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6739 gcc_assert (a->dw_attr != DW_AT_low_pc
6740 && a->dw_attr != DW_AT_high_pc
6741 && a->dw_attr != DW_AT_location
6742 && a->dw_attr != DW_AT_frame_base
6743 && a->dw_attr != DW_AT_call_all_calls
6744 && a->dw_attr != DW_AT_GNU_all_call_sites);
6748 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
6749 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
6750 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
6752 /* Calculate the checksum of a location expression. */
6754 static inline void
6755 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
6757 int tem;
6758 inchash::hash hstate;
6759 hashval_t hash;
6761 tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
6762 CHECKSUM (tem);
6763 hash_loc_operands (loc, hstate);
6764 hash = hstate.end();
6765 CHECKSUM (hash);
6768 /* Calculate the checksum of an attribute. */
6770 static void
6771 attr_checksum (dw_attr_node *at, struct md5_ctx *ctx, int *mark)
6773 dw_loc_descr_ref loc;
6774 rtx r;
6776 CHECKSUM (at->dw_attr);
6778 /* We don't care that this was compiled with a different compiler
6779 snapshot; if the output is the same, that's what matters. */
6780 if (at->dw_attr == DW_AT_producer)
6781 return;
6783 switch (AT_class (at))
6785 case dw_val_class_const:
6786 case dw_val_class_const_implicit:
6787 CHECKSUM (at->dw_attr_val.v.val_int);
6788 break;
6789 case dw_val_class_unsigned_const:
6790 case dw_val_class_unsigned_const_implicit:
6791 CHECKSUM (at->dw_attr_val.v.val_unsigned);
6792 break;
6793 case dw_val_class_const_double:
6794 CHECKSUM (at->dw_attr_val.v.val_double);
6795 break;
6796 case dw_val_class_wide_int:
6797 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
6798 get_full_len (*at->dw_attr_val.v.val_wide)
6799 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
6800 break;
6801 case dw_val_class_vec:
6802 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
6803 (at->dw_attr_val.v.val_vec.length
6804 * at->dw_attr_val.v.val_vec.elt_size));
6805 break;
6806 case dw_val_class_flag:
6807 CHECKSUM (at->dw_attr_val.v.val_flag);
6808 break;
6809 case dw_val_class_str:
6810 CHECKSUM_STRING (AT_string (at));
6811 break;
6813 case dw_val_class_addr:
6814 r = AT_addr (at);
6815 gcc_assert (GET_CODE (r) == SYMBOL_REF);
6816 CHECKSUM_STRING (XSTR (r, 0));
6817 break;
6819 case dw_val_class_offset:
6820 CHECKSUM (at->dw_attr_val.v.val_offset);
6821 break;
6823 case dw_val_class_loc:
6824 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6825 loc_checksum (loc, ctx);
6826 break;
6828 case dw_val_class_die_ref:
6829 die_checksum (AT_ref (at), ctx, mark);
6830 break;
6832 case dw_val_class_fde_ref:
6833 case dw_val_class_vms_delta:
6834 case dw_val_class_lbl_id:
6835 case dw_val_class_lineptr:
6836 case dw_val_class_macptr:
6837 case dw_val_class_loclistsptr:
6838 case dw_val_class_high_pc:
6839 break;
6841 case dw_val_class_file:
6842 case dw_val_class_file_implicit:
6843 CHECKSUM_STRING (AT_file (at)->filename);
6844 break;
6846 case dw_val_class_data8:
6847 CHECKSUM (at->dw_attr_val.v.val_data8);
6848 break;
6850 default:
6851 break;
6855 /* Calculate the checksum of a DIE. */
6857 static void
6858 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6860 dw_die_ref c;
6861 dw_attr_node *a;
6862 unsigned ix;
6864 /* To avoid infinite recursion. */
6865 if (die->die_mark)
6867 CHECKSUM (die->die_mark);
6868 return;
6870 die->die_mark = ++(*mark);
6872 CHECKSUM (die->die_tag);
6874 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6875 attr_checksum (a, ctx, mark);
6877 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
6880 #undef CHECKSUM
6881 #undef CHECKSUM_BLOCK
6882 #undef CHECKSUM_STRING
6884 /* For DWARF-4 types, include the trailing NULL when checksumming strings. */
6885 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
6886 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
6887 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
6888 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
6889 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
6890 #define CHECKSUM_ATTR(FOO) \
6891 if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
6893 /* Calculate the checksum of a number in signed LEB128 format. */
6895 static void
6896 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
6898 unsigned char byte;
6899 bool more;
6901 while (1)
6903 byte = (value & 0x7f);
6904 value >>= 7;
6905 more = !((value == 0 && (byte & 0x40) == 0)
6906 || (value == -1 && (byte & 0x40) != 0));
6907 if (more)
6908 byte |= 0x80;
6909 CHECKSUM (byte);
6910 if (!more)
6911 break;
6915 /* Calculate the checksum of a number in unsigned LEB128 format. */
6917 static void
6918 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
6920 while (1)
6922 unsigned char byte = (value & 0x7f);
6923 value >>= 7;
6924 if (value != 0)
6925 /* More bytes to follow. */
6926 byte |= 0x80;
6927 CHECKSUM (byte);
6928 if (value == 0)
6929 break;
6933 /* Checksum the context of the DIE. This adds the names of any
6934 surrounding namespaces or structures to the checksum. */
6936 static void
6937 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
6939 const char *name;
6940 dw_die_ref spec;
6941 int tag = die->die_tag;
6943 if (tag != DW_TAG_namespace
6944 && tag != DW_TAG_structure_type
6945 && tag != DW_TAG_class_type)
6946 return;
6948 name = get_AT_string (die, DW_AT_name);
6950 spec = get_AT_ref (die, DW_AT_specification);
6951 if (spec != NULL)
6952 die = spec;
6954 if (die->die_parent != NULL)
6955 checksum_die_context (die->die_parent, ctx);
6957 CHECKSUM_ULEB128 ('C');
6958 CHECKSUM_ULEB128 (tag);
6959 if (name != NULL)
6960 CHECKSUM_STRING (name);
6963 /* Calculate the checksum of a location expression. */
6965 static inline void
6966 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
6968 /* Special case for lone DW_OP_plus_uconst: checksum as if the location
6969 were emitted as a DW_FORM_sdata instead of a location expression. */
6970 if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
6972 CHECKSUM_ULEB128 (DW_FORM_sdata);
6973 CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
6974 return;
6977 /* Otherwise, just checksum the raw location expression. */
6978 while (loc != NULL)
6980 inchash::hash hstate;
6981 hashval_t hash;
6983 CHECKSUM_ULEB128 (loc->dtprel);
6984 CHECKSUM_ULEB128 (loc->dw_loc_opc);
6985 hash_loc_operands (loc, hstate);
6986 hash = hstate.end ();
6987 CHECKSUM (hash);
6988 loc = loc->dw_loc_next;
6992 /* Calculate the checksum of an attribute. */
6994 static void
6995 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_node *at,
6996 struct md5_ctx *ctx, int *mark)
6998 dw_loc_descr_ref loc;
6999 rtx r;
7001 if (AT_class (at) == dw_val_class_die_ref)
7003 dw_die_ref target_die = AT_ref (at);
7005 /* For pointer and reference types, we checksum only the (qualified)
7006 name of the target type (if there is a name). For friend entries,
7007 we checksum only the (qualified) name of the target type or function.
7008 This allows the checksum to remain the same whether the target type
7009 is complete or not. */
7010 if ((at->dw_attr == DW_AT_type
7011 && (tag == DW_TAG_pointer_type
7012 || tag == DW_TAG_reference_type
7013 || tag == DW_TAG_rvalue_reference_type
7014 || tag == DW_TAG_ptr_to_member_type))
7015 || (at->dw_attr == DW_AT_friend
7016 && tag == DW_TAG_friend))
7018 dw_attr_node *name_attr = get_AT (target_die, DW_AT_name);
7020 if (name_attr != NULL)
7022 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
7024 if (decl == NULL)
7025 decl = target_die;
7026 CHECKSUM_ULEB128 ('N');
7027 CHECKSUM_ULEB128 (at->dw_attr);
7028 if (decl->die_parent != NULL)
7029 checksum_die_context (decl->die_parent, ctx);
7030 CHECKSUM_ULEB128 ('E');
7031 CHECKSUM_STRING (AT_string (name_attr));
7032 return;
7036 /* For all other references to another DIE, we check to see if the
7037 target DIE has already been visited. If it has, we emit a
7038 backward reference; if not, we descend recursively. */
7039 if (target_die->die_mark > 0)
7041 CHECKSUM_ULEB128 ('R');
7042 CHECKSUM_ULEB128 (at->dw_attr);
7043 CHECKSUM_ULEB128 (target_die->die_mark);
7045 else
7047 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
7049 if (decl == NULL)
7050 decl = target_die;
7051 target_die->die_mark = ++(*mark);
7052 CHECKSUM_ULEB128 ('T');
7053 CHECKSUM_ULEB128 (at->dw_attr);
7054 if (decl->die_parent != NULL)
7055 checksum_die_context (decl->die_parent, ctx);
7056 die_checksum_ordered (target_die, ctx, mark);
7058 return;
7061 CHECKSUM_ULEB128 ('A');
7062 CHECKSUM_ULEB128 (at->dw_attr);
7064 switch (AT_class (at))
7066 case dw_val_class_const:
7067 case dw_val_class_const_implicit:
7068 CHECKSUM_ULEB128 (DW_FORM_sdata);
7069 CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
7070 break;
7072 case dw_val_class_unsigned_const:
7073 case dw_val_class_unsigned_const_implicit:
7074 CHECKSUM_ULEB128 (DW_FORM_sdata);
7075 CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
7076 break;
7078 case dw_val_class_const_double:
7079 CHECKSUM_ULEB128 (DW_FORM_block);
7080 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
7081 CHECKSUM (at->dw_attr_val.v.val_double);
7082 break;
7084 case dw_val_class_wide_int:
7085 CHECKSUM_ULEB128 (DW_FORM_block);
7086 CHECKSUM_ULEB128 (get_full_len (*at->dw_attr_val.v.val_wide)
7087 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
7088 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
7089 get_full_len (*at->dw_attr_val.v.val_wide)
7090 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
7091 break;
7093 case dw_val_class_vec:
7094 CHECKSUM_ULEB128 (DW_FORM_block);
7095 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_vec.length
7096 * at->dw_attr_val.v.val_vec.elt_size);
7097 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
7098 (at->dw_attr_val.v.val_vec.length
7099 * at->dw_attr_val.v.val_vec.elt_size));
7100 break;
7102 case dw_val_class_flag:
7103 CHECKSUM_ULEB128 (DW_FORM_flag);
7104 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
7105 break;
7107 case dw_val_class_str:
7108 CHECKSUM_ULEB128 (DW_FORM_string);
7109 CHECKSUM_STRING (AT_string (at));
7110 break;
7112 case dw_val_class_addr:
7113 r = AT_addr (at);
7114 gcc_assert (GET_CODE (r) == SYMBOL_REF);
7115 CHECKSUM_ULEB128 (DW_FORM_string);
7116 CHECKSUM_STRING (XSTR (r, 0));
7117 break;
7119 case dw_val_class_offset:
7120 CHECKSUM_ULEB128 (DW_FORM_sdata);
7121 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
7122 break;
7124 case dw_val_class_loc:
7125 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
7126 loc_checksum_ordered (loc, ctx);
7127 break;
7129 case dw_val_class_fde_ref:
7130 case dw_val_class_lbl_id:
7131 case dw_val_class_lineptr:
7132 case dw_val_class_macptr:
7133 case dw_val_class_loclistsptr:
7134 case dw_val_class_high_pc:
7135 break;
7137 case dw_val_class_file:
7138 case dw_val_class_file_implicit:
7139 CHECKSUM_ULEB128 (DW_FORM_string);
7140 CHECKSUM_STRING (AT_file (at)->filename);
7141 break;
7143 case dw_val_class_data8:
7144 CHECKSUM (at->dw_attr_val.v.val_data8);
7145 break;
7147 default:
7148 break;
7152 struct checksum_attributes
7154 dw_attr_node *at_name;
7155 dw_attr_node *at_type;
7156 dw_attr_node *at_friend;
7157 dw_attr_node *at_accessibility;
7158 dw_attr_node *at_address_class;
7159 dw_attr_node *at_alignment;
7160 dw_attr_node *at_allocated;
7161 dw_attr_node *at_artificial;
7162 dw_attr_node *at_associated;
7163 dw_attr_node *at_binary_scale;
7164 dw_attr_node *at_bit_offset;
7165 dw_attr_node *at_bit_size;
7166 dw_attr_node *at_bit_stride;
7167 dw_attr_node *at_byte_size;
7168 dw_attr_node *at_byte_stride;
7169 dw_attr_node *at_const_value;
7170 dw_attr_node *at_containing_type;
7171 dw_attr_node *at_count;
7172 dw_attr_node *at_data_location;
7173 dw_attr_node *at_data_member_location;
7174 dw_attr_node *at_decimal_scale;
7175 dw_attr_node *at_decimal_sign;
7176 dw_attr_node *at_default_value;
7177 dw_attr_node *at_digit_count;
7178 dw_attr_node *at_discr;
7179 dw_attr_node *at_discr_list;
7180 dw_attr_node *at_discr_value;
7181 dw_attr_node *at_encoding;
7182 dw_attr_node *at_endianity;
7183 dw_attr_node *at_explicit;
7184 dw_attr_node *at_is_optional;
7185 dw_attr_node *at_location;
7186 dw_attr_node *at_lower_bound;
7187 dw_attr_node *at_mutable;
7188 dw_attr_node *at_ordering;
7189 dw_attr_node *at_picture_string;
7190 dw_attr_node *at_prototyped;
7191 dw_attr_node *at_small;
7192 dw_attr_node *at_segment;
7193 dw_attr_node *at_string_length;
7194 dw_attr_node *at_string_length_bit_size;
7195 dw_attr_node *at_string_length_byte_size;
7196 dw_attr_node *at_threads_scaled;
7197 dw_attr_node *at_upper_bound;
7198 dw_attr_node *at_use_location;
7199 dw_attr_node *at_use_UTF8;
7200 dw_attr_node *at_variable_parameter;
7201 dw_attr_node *at_virtuality;
7202 dw_attr_node *at_visibility;
7203 dw_attr_node *at_vtable_elem_location;
7206 /* Collect the attributes that we will want to use for the checksum. */
7208 static void
7209 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
7211 dw_attr_node *a;
7212 unsigned ix;
7214 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7216 switch (a->dw_attr)
7218 case DW_AT_name:
7219 attrs->at_name = a;
7220 break;
7221 case DW_AT_type:
7222 attrs->at_type = a;
7223 break;
7224 case DW_AT_friend:
7225 attrs->at_friend = a;
7226 break;
7227 case DW_AT_accessibility:
7228 attrs->at_accessibility = a;
7229 break;
7230 case DW_AT_address_class:
7231 attrs->at_address_class = a;
7232 break;
7233 case DW_AT_alignment:
7234 attrs->at_alignment = a;
7235 break;
7236 case DW_AT_allocated:
7237 attrs->at_allocated = a;
7238 break;
7239 case DW_AT_artificial:
7240 attrs->at_artificial = a;
7241 break;
7242 case DW_AT_associated:
7243 attrs->at_associated = a;
7244 break;
7245 case DW_AT_binary_scale:
7246 attrs->at_binary_scale = a;
7247 break;
7248 case DW_AT_bit_offset:
7249 attrs->at_bit_offset = a;
7250 break;
7251 case DW_AT_bit_size:
7252 attrs->at_bit_size = a;
7253 break;
7254 case DW_AT_bit_stride:
7255 attrs->at_bit_stride = a;
7256 break;
7257 case DW_AT_byte_size:
7258 attrs->at_byte_size = a;
7259 break;
7260 case DW_AT_byte_stride:
7261 attrs->at_byte_stride = a;
7262 break;
7263 case DW_AT_const_value:
7264 attrs->at_const_value = a;
7265 break;
7266 case DW_AT_containing_type:
7267 attrs->at_containing_type = a;
7268 break;
7269 case DW_AT_count:
7270 attrs->at_count = a;
7271 break;
7272 case DW_AT_data_location:
7273 attrs->at_data_location = a;
7274 break;
7275 case DW_AT_data_member_location:
7276 attrs->at_data_member_location = a;
7277 break;
7278 case DW_AT_decimal_scale:
7279 attrs->at_decimal_scale = a;
7280 break;
7281 case DW_AT_decimal_sign:
7282 attrs->at_decimal_sign = a;
7283 break;
7284 case DW_AT_default_value:
7285 attrs->at_default_value = a;
7286 break;
7287 case DW_AT_digit_count:
7288 attrs->at_digit_count = a;
7289 break;
7290 case DW_AT_discr:
7291 attrs->at_discr = a;
7292 break;
7293 case DW_AT_discr_list:
7294 attrs->at_discr_list = a;
7295 break;
7296 case DW_AT_discr_value:
7297 attrs->at_discr_value = a;
7298 break;
7299 case DW_AT_encoding:
7300 attrs->at_encoding = a;
7301 break;
7302 case DW_AT_endianity:
7303 attrs->at_endianity = a;
7304 break;
7305 case DW_AT_explicit:
7306 attrs->at_explicit = a;
7307 break;
7308 case DW_AT_is_optional:
7309 attrs->at_is_optional = a;
7310 break;
7311 case DW_AT_location:
7312 attrs->at_location = a;
7313 break;
7314 case DW_AT_lower_bound:
7315 attrs->at_lower_bound = a;
7316 break;
7317 case DW_AT_mutable:
7318 attrs->at_mutable = a;
7319 break;
7320 case DW_AT_ordering:
7321 attrs->at_ordering = a;
7322 break;
7323 case DW_AT_picture_string:
7324 attrs->at_picture_string = a;
7325 break;
7326 case DW_AT_prototyped:
7327 attrs->at_prototyped = a;
7328 break;
7329 case DW_AT_small:
7330 attrs->at_small = a;
7331 break;
7332 case DW_AT_segment:
7333 attrs->at_segment = a;
7334 break;
7335 case DW_AT_string_length:
7336 attrs->at_string_length = a;
7337 break;
7338 case DW_AT_string_length_bit_size:
7339 attrs->at_string_length_bit_size = a;
7340 break;
7341 case DW_AT_string_length_byte_size:
7342 attrs->at_string_length_byte_size = a;
7343 break;
7344 case DW_AT_threads_scaled:
7345 attrs->at_threads_scaled = a;
7346 break;
7347 case DW_AT_upper_bound:
7348 attrs->at_upper_bound = a;
7349 break;
7350 case DW_AT_use_location:
7351 attrs->at_use_location = a;
7352 break;
7353 case DW_AT_use_UTF8:
7354 attrs->at_use_UTF8 = a;
7355 break;
7356 case DW_AT_variable_parameter:
7357 attrs->at_variable_parameter = a;
7358 break;
7359 case DW_AT_virtuality:
7360 attrs->at_virtuality = a;
7361 break;
7362 case DW_AT_visibility:
7363 attrs->at_visibility = a;
7364 break;
7365 case DW_AT_vtable_elem_location:
7366 attrs->at_vtable_elem_location = a;
7367 break;
7368 default:
7369 break;
7374 /* Calculate the checksum of a DIE, using an ordered subset of attributes. */
7376 static void
7377 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
7379 dw_die_ref c;
7380 dw_die_ref decl;
7381 struct checksum_attributes attrs;
7383 CHECKSUM_ULEB128 ('D');
7384 CHECKSUM_ULEB128 (die->die_tag);
7386 memset (&attrs, 0, sizeof (attrs));
7388 decl = get_AT_ref (die, DW_AT_specification);
7389 if (decl != NULL)
7390 collect_checksum_attributes (&attrs, decl);
7391 collect_checksum_attributes (&attrs, die);
7393 CHECKSUM_ATTR (attrs.at_name);
7394 CHECKSUM_ATTR (attrs.at_accessibility);
7395 CHECKSUM_ATTR (attrs.at_address_class);
7396 CHECKSUM_ATTR (attrs.at_allocated);
7397 CHECKSUM_ATTR (attrs.at_artificial);
7398 CHECKSUM_ATTR (attrs.at_associated);
7399 CHECKSUM_ATTR (attrs.at_binary_scale);
7400 CHECKSUM_ATTR (attrs.at_bit_offset);
7401 CHECKSUM_ATTR (attrs.at_bit_size);
7402 CHECKSUM_ATTR (attrs.at_bit_stride);
7403 CHECKSUM_ATTR (attrs.at_byte_size);
7404 CHECKSUM_ATTR (attrs.at_byte_stride);
7405 CHECKSUM_ATTR (attrs.at_const_value);
7406 CHECKSUM_ATTR (attrs.at_containing_type);
7407 CHECKSUM_ATTR (attrs.at_count);
7408 CHECKSUM_ATTR (attrs.at_data_location);
7409 CHECKSUM_ATTR (attrs.at_data_member_location);
7410 CHECKSUM_ATTR (attrs.at_decimal_scale);
7411 CHECKSUM_ATTR (attrs.at_decimal_sign);
7412 CHECKSUM_ATTR (attrs.at_default_value);
7413 CHECKSUM_ATTR (attrs.at_digit_count);
7414 CHECKSUM_ATTR (attrs.at_discr);
7415 CHECKSUM_ATTR (attrs.at_discr_list);
7416 CHECKSUM_ATTR (attrs.at_discr_value);
7417 CHECKSUM_ATTR (attrs.at_encoding);
7418 CHECKSUM_ATTR (attrs.at_endianity);
7419 CHECKSUM_ATTR (attrs.at_explicit);
7420 CHECKSUM_ATTR (attrs.at_is_optional);
7421 CHECKSUM_ATTR (attrs.at_location);
7422 CHECKSUM_ATTR (attrs.at_lower_bound);
7423 CHECKSUM_ATTR (attrs.at_mutable);
7424 CHECKSUM_ATTR (attrs.at_ordering);
7425 CHECKSUM_ATTR (attrs.at_picture_string);
7426 CHECKSUM_ATTR (attrs.at_prototyped);
7427 CHECKSUM_ATTR (attrs.at_small);
7428 CHECKSUM_ATTR (attrs.at_segment);
7429 CHECKSUM_ATTR (attrs.at_string_length);
7430 CHECKSUM_ATTR (attrs.at_string_length_bit_size);
7431 CHECKSUM_ATTR (attrs.at_string_length_byte_size);
7432 CHECKSUM_ATTR (attrs.at_threads_scaled);
7433 CHECKSUM_ATTR (attrs.at_upper_bound);
7434 CHECKSUM_ATTR (attrs.at_use_location);
7435 CHECKSUM_ATTR (attrs.at_use_UTF8);
7436 CHECKSUM_ATTR (attrs.at_variable_parameter);
7437 CHECKSUM_ATTR (attrs.at_virtuality);
7438 CHECKSUM_ATTR (attrs.at_visibility);
7439 CHECKSUM_ATTR (attrs.at_vtable_elem_location);
7440 CHECKSUM_ATTR (attrs.at_type);
7441 CHECKSUM_ATTR (attrs.at_friend);
7442 CHECKSUM_ATTR (attrs.at_alignment);
7444 /* Checksum the child DIEs. */
7445 c = die->die_child;
7446 if (c) do {
7447 dw_attr_node *name_attr;
7449 c = c->die_sib;
7450 name_attr = get_AT (c, DW_AT_name);
7451 if (is_template_instantiation (c))
7453 /* Ignore instantiations of member type and function templates. */
7455 else if (name_attr != NULL
7456 && (is_type_die (c) || c->die_tag == DW_TAG_subprogram))
7458 /* Use a shallow checksum for named nested types and member
7459 functions. */
7460 CHECKSUM_ULEB128 ('S');
7461 CHECKSUM_ULEB128 (c->die_tag);
7462 CHECKSUM_STRING (AT_string (name_attr));
7464 else
7466 /* Use a deep checksum for other children. */
7467 /* Mark this DIE so it gets processed when unmarking. */
7468 if (c->die_mark == 0)
7469 c->die_mark = -1;
7470 die_checksum_ordered (c, ctx, mark);
7472 } while (c != die->die_child);
7474 CHECKSUM_ULEB128 (0);
7477 /* Add a type name and tag to a hash. */
7478 static void
7479 die_odr_checksum (int tag, const char *name, md5_ctx *ctx)
7481 CHECKSUM_ULEB128 (tag);
7482 CHECKSUM_STRING (name);
7485 #undef CHECKSUM
7486 #undef CHECKSUM_STRING
7487 #undef CHECKSUM_ATTR
7488 #undef CHECKSUM_LEB128
7489 #undef CHECKSUM_ULEB128
7491 /* Generate the type signature for DIE. This is computed by generating an
7492 MD5 checksum over the DIE's tag, its relevant attributes, and its
7493 children. Attributes that are references to other DIEs are processed
7494 by recursion, using the MARK field to prevent infinite recursion.
7495 If the DIE is nested inside a namespace or another type, we also
7496 need to include that context in the signature. The lower 64 bits
7497 of the resulting MD5 checksum comprise the signature. */
7499 static void
7500 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
7502 int mark;
7503 const char *name;
7504 unsigned char checksum[16];
7505 struct md5_ctx ctx;
7506 dw_die_ref decl;
7507 dw_die_ref parent;
7509 name = get_AT_string (die, DW_AT_name);
7510 decl = get_AT_ref (die, DW_AT_specification);
7511 parent = get_die_parent (die);
7513 /* First, compute a signature for just the type name (and its surrounding
7514 context, if any. This is stored in the type unit DIE for link-time
7515 ODR (one-definition rule) checking. */
7517 if (is_cxx () && name != NULL)
7519 md5_init_ctx (&ctx);
7521 /* Checksum the names of surrounding namespaces and structures. */
7522 if (parent != NULL)
7523 checksum_die_context (parent, &ctx);
7525 /* Checksum the current DIE. */
7526 die_odr_checksum (die->die_tag, name, &ctx);
7527 md5_finish_ctx (&ctx, checksum);
7529 add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
7532 /* Next, compute the complete type signature. */
7534 md5_init_ctx (&ctx);
7535 mark = 1;
7536 die->die_mark = mark;
7538 /* Checksum the names of surrounding namespaces and structures. */
7539 if (parent != NULL)
7540 checksum_die_context (parent, &ctx);
7542 /* Checksum the DIE and its children. */
7543 die_checksum_ordered (die, &ctx, &mark);
7544 unmark_all_dies (die);
7545 md5_finish_ctx (&ctx, checksum);
7547 /* Store the signature in the type node and link the type DIE and the
7548 type node together. */
7549 memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
7550 DWARF_TYPE_SIGNATURE_SIZE);
7551 die->comdat_type_p = true;
7552 die->die_id.die_type_node = type_node;
7553 type_node->type_die = die;
7555 /* If the DIE is a specification, link its declaration to the type node
7556 as well. */
7557 if (decl != NULL)
7559 decl->comdat_type_p = true;
7560 decl->die_id.die_type_node = type_node;
7564 /* Do the location expressions look same? */
7565 static inline int
7566 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
7568 return loc1->dw_loc_opc == loc2->dw_loc_opc
7569 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
7570 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
7573 /* Do the values look the same? */
7574 static int
7575 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
7577 dw_loc_descr_ref loc1, loc2;
7578 rtx r1, r2;
7580 if (v1->val_class != v2->val_class)
7581 return 0;
7583 switch (v1->val_class)
7585 case dw_val_class_const:
7586 case dw_val_class_const_implicit:
7587 return v1->v.val_int == v2->v.val_int;
7588 case dw_val_class_unsigned_const:
7589 case dw_val_class_unsigned_const_implicit:
7590 return v1->v.val_unsigned == v2->v.val_unsigned;
7591 case dw_val_class_const_double:
7592 return v1->v.val_double.high == v2->v.val_double.high
7593 && v1->v.val_double.low == v2->v.val_double.low;
7594 case dw_val_class_wide_int:
7595 return *v1->v.val_wide == *v2->v.val_wide;
7596 case dw_val_class_vec:
7597 if (v1->v.val_vec.length != v2->v.val_vec.length
7598 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
7599 return 0;
7600 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
7601 v1->v.val_vec.length * v1->v.val_vec.elt_size))
7602 return 0;
7603 return 1;
7604 case dw_val_class_flag:
7605 return v1->v.val_flag == v2->v.val_flag;
7606 case dw_val_class_str:
7607 return !strcmp (v1->v.val_str->str, v2->v.val_str->str);
7609 case dw_val_class_addr:
7610 r1 = v1->v.val_addr;
7611 r2 = v2->v.val_addr;
7612 if (GET_CODE (r1) != GET_CODE (r2))
7613 return 0;
7614 return !rtx_equal_p (r1, r2);
7616 case dw_val_class_offset:
7617 return v1->v.val_offset == v2->v.val_offset;
7619 case dw_val_class_loc:
7620 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
7621 loc1 && loc2;
7622 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
7623 if (!same_loc_p (loc1, loc2, mark))
7624 return 0;
7625 return !loc1 && !loc2;
7627 case dw_val_class_die_ref:
7628 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
7630 case dw_val_class_fde_ref:
7631 case dw_val_class_vms_delta:
7632 case dw_val_class_lbl_id:
7633 case dw_val_class_lineptr:
7634 case dw_val_class_macptr:
7635 case dw_val_class_loclistsptr:
7636 case dw_val_class_high_pc:
7637 return 1;
7639 case dw_val_class_file:
7640 case dw_val_class_file_implicit:
7641 return v1->v.val_file == v2->v.val_file;
7643 case dw_val_class_data8:
7644 return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
7646 default:
7647 return 1;
7651 /* Do the attributes look the same? */
7653 static int
7654 same_attr_p (dw_attr_node *at1, dw_attr_node *at2, int *mark)
7656 if (at1->dw_attr != at2->dw_attr)
7657 return 0;
7659 /* We don't care that this was compiled with a different compiler
7660 snapshot; if the output is the same, that's what matters. */
7661 if (at1->dw_attr == DW_AT_producer)
7662 return 1;
7664 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
7667 /* Do the dies look the same? */
7669 static int
7670 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
7672 dw_die_ref c1, c2;
7673 dw_attr_node *a1;
7674 unsigned ix;
7676 /* To avoid infinite recursion. */
7677 if (die1->die_mark)
7678 return die1->die_mark == die2->die_mark;
7679 die1->die_mark = die2->die_mark = ++(*mark);
7681 if (die1->die_tag != die2->die_tag)
7682 return 0;
7684 if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
7685 return 0;
7687 FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
7688 if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
7689 return 0;
7691 c1 = die1->die_child;
7692 c2 = die2->die_child;
7693 if (! c1)
7695 if (c2)
7696 return 0;
7698 else
7699 for (;;)
7701 if (!same_die_p (c1, c2, mark))
7702 return 0;
7703 c1 = c1->die_sib;
7704 c2 = c2->die_sib;
7705 if (c1 == die1->die_child)
7707 if (c2 == die2->die_child)
7708 break;
7709 else
7710 return 0;
7714 return 1;
7717 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
7718 children, and set die_symbol. */
7720 static void
7721 compute_comp_unit_symbol (dw_die_ref unit_die)
7723 const char *die_name = get_AT_string (unit_die, DW_AT_name);
7724 const char *base = die_name ? lbasename (die_name) : "anonymous";
7725 char *name = XALLOCAVEC (char, strlen (base) + 64);
7726 char *p;
7727 int i, mark;
7728 unsigned char checksum[16];
7729 struct md5_ctx ctx;
7731 /* Compute the checksum of the DIE, then append part of it as hex digits to
7732 the name filename of the unit. */
7734 md5_init_ctx (&ctx);
7735 mark = 0;
7736 die_checksum (unit_die, &ctx, &mark);
7737 unmark_all_dies (unit_die);
7738 md5_finish_ctx (&ctx, checksum);
7740 /* When we this for comp_unit_die () we have a DW_AT_name that might
7741 not start with a letter but with anything valid for filenames and
7742 clean_symbol_name doesn't fix that up. Prepend 'g' if the first
7743 character is not a letter. */
7744 sprintf (name, "%s%s.", ISALPHA (*base) ? "" : "g", base);
7745 clean_symbol_name (name);
7747 p = name + strlen (name);
7748 for (i = 0; i < 4; i++)
7750 sprintf (p, "%.2x", checksum[i]);
7751 p += 2;
7754 unit_die->die_id.die_symbol = xstrdup (name);
7757 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
7759 static int
7760 is_type_die (dw_die_ref die)
7762 switch (die->die_tag)
7764 case DW_TAG_array_type:
7765 case DW_TAG_class_type:
7766 case DW_TAG_interface_type:
7767 case DW_TAG_enumeration_type:
7768 case DW_TAG_pointer_type:
7769 case DW_TAG_reference_type:
7770 case DW_TAG_rvalue_reference_type:
7771 case DW_TAG_string_type:
7772 case DW_TAG_structure_type:
7773 case DW_TAG_subroutine_type:
7774 case DW_TAG_union_type:
7775 case DW_TAG_ptr_to_member_type:
7776 case DW_TAG_set_type:
7777 case DW_TAG_subrange_type:
7778 case DW_TAG_base_type:
7779 case DW_TAG_const_type:
7780 case DW_TAG_file_type:
7781 case DW_TAG_packed_type:
7782 case DW_TAG_volatile_type:
7783 case DW_TAG_typedef:
7784 return 1;
7785 default:
7786 return 0;
7790 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
7791 Basically, we want to choose the bits that are likely to be shared between
7792 compilations (types) and leave out the bits that are specific to individual
7793 compilations (functions). */
7795 static int
7796 is_comdat_die (dw_die_ref c)
7798 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
7799 we do for stabs. The advantage is a greater likelihood of sharing between
7800 objects that don't include headers in the same order (and therefore would
7801 put the base types in a different comdat). jason 8/28/00 */
7803 if (c->die_tag == DW_TAG_base_type)
7804 return 0;
7806 if (c->die_tag == DW_TAG_pointer_type
7807 || c->die_tag == DW_TAG_reference_type
7808 || c->die_tag == DW_TAG_rvalue_reference_type
7809 || c->die_tag == DW_TAG_const_type
7810 || c->die_tag == DW_TAG_volatile_type)
7812 dw_die_ref t = get_AT_ref (c, DW_AT_type);
7814 return t ? is_comdat_die (t) : 0;
7817 return is_type_die (c);
7820 /* Returns true iff C is a compile-unit DIE. */
7822 static inline bool
7823 is_cu_die (dw_die_ref c)
7825 return c && (c->die_tag == DW_TAG_compile_unit
7826 || c->die_tag == DW_TAG_skeleton_unit);
7829 /* Returns true iff C is a unit DIE of some sort. */
7831 static inline bool
7832 is_unit_die (dw_die_ref c)
7834 return c && (c->die_tag == DW_TAG_compile_unit
7835 || c->die_tag == DW_TAG_partial_unit
7836 || c->die_tag == DW_TAG_type_unit
7837 || c->die_tag == DW_TAG_skeleton_unit);
7840 /* Returns true iff C is a namespace DIE. */
7842 static inline bool
7843 is_namespace_die (dw_die_ref c)
7845 return c && c->die_tag == DW_TAG_namespace;
7848 /* Returns true iff C is a class or structure DIE. */
7850 static inline bool
7851 is_class_die (dw_die_ref c)
7853 return c && (c->die_tag == DW_TAG_class_type
7854 || c->die_tag == DW_TAG_structure_type);
7857 /* Return non-zero if this DIE is a template parameter. */
7859 static inline bool
7860 is_template_parameter (dw_die_ref die)
7862 switch (die->die_tag)
7864 case DW_TAG_template_type_param:
7865 case DW_TAG_template_value_param:
7866 case DW_TAG_GNU_template_template_param:
7867 case DW_TAG_GNU_template_parameter_pack:
7868 return true;
7869 default:
7870 return false;
7874 /* Return non-zero if this DIE represents a template instantiation. */
7876 static inline bool
7877 is_template_instantiation (dw_die_ref die)
7879 dw_die_ref c;
7881 if (!is_type_die (die) && die->die_tag != DW_TAG_subprogram)
7882 return false;
7883 FOR_EACH_CHILD (die, c, if (is_template_parameter (c)) return true);
7884 return false;
7887 static char *
7888 gen_internal_sym (const char *prefix)
7890 char buf[MAX_ARTIFICIAL_LABEL_BYTES];
7892 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
7893 return xstrdup (buf);
7896 /* Return non-zero if this DIE is a declaration. */
7898 static int
7899 is_declaration_die (dw_die_ref die)
7901 dw_attr_node *a;
7902 unsigned ix;
7904 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7905 if (a->dw_attr == DW_AT_declaration)
7906 return 1;
7908 return 0;
7911 /* Return non-zero if this DIE is nested inside a subprogram. */
7913 static int
7914 is_nested_in_subprogram (dw_die_ref die)
7916 dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
7918 if (decl == NULL)
7919 decl = die;
7920 return local_scope_p (decl);
7923 /* Return non-zero if this DIE contains a defining declaration of a
7924 subprogram. */
7926 static int
7927 contains_subprogram_definition (dw_die_ref die)
7929 dw_die_ref c;
7931 if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
7932 return 1;
7933 FOR_EACH_CHILD (die, c, if (contains_subprogram_definition (c)) return 1);
7934 return 0;
7937 /* Return non-zero if this is a type DIE that should be moved to a
7938 COMDAT .debug_types section or .debug_info section with DW_UT_*type
7939 unit type. */
7941 static int
7942 should_move_die_to_comdat (dw_die_ref die)
7944 switch (die->die_tag)
7946 case DW_TAG_class_type:
7947 case DW_TAG_structure_type:
7948 case DW_TAG_enumeration_type:
7949 case DW_TAG_union_type:
7950 /* Don't move declarations, inlined instances, types nested in a
7951 subprogram, or types that contain subprogram definitions. */
7952 if (is_declaration_die (die)
7953 || get_AT (die, DW_AT_abstract_origin)
7954 || is_nested_in_subprogram (die)
7955 || contains_subprogram_definition (die))
7956 return 0;
7957 return 1;
7958 case DW_TAG_array_type:
7959 case DW_TAG_interface_type:
7960 case DW_TAG_pointer_type:
7961 case DW_TAG_reference_type:
7962 case DW_TAG_rvalue_reference_type:
7963 case DW_TAG_string_type:
7964 case DW_TAG_subroutine_type:
7965 case DW_TAG_ptr_to_member_type:
7966 case DW_TAG_set_type:
7967 case DW_TAG_subrange_type:
7968 case DW_TAG_base_type:
7969 case DW_TAG_const_type:
7970 case DW_TAG_file_type:
7971 case DW_TAG_packed_type:
7972 case DW_TAG_volatile_type:
7973 case DW_TAG_typedef:
7974 default:
7975 return 0;
7979 /* Make a clone of DIE. */
7981 static dw_die_ref
7982 clone_die (dw_die_ref die)
7984 dw_die_ref clone = new_die_raw (die->die_tag);
7985 dw_attr_node *a;
7986 unsigned ix;
7988 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7989 add_dwarf_attr (clone, a);
7991 return clone;
7994 /* Make a clone of the tree rooted at DIE. */
7996 static dw_die_ref
7997 clone_tree (dw_die_ref die)
7999 dw_die_ref c;
8000 dw_die_ref clone = clone_die (die);
8002 FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree (c)));
8004 return clone;
8007 /* Make a clone of DIE as a declaration. */
8009 static dw_die_ref
8010 clone_as_declaration (dw_die_ref die)
8012 dw_die_ref clone;
8013 dw_die_ref decl;
8014 dw_attr_node *a;
8015 unsigned ix;
8017 /* If the DIE is already a declaration, just clone it. */
8018 if (is_declaration_die (die))
8019 return clone_die (die);
8021 /* If the DIE is a specification, just clone its declaration DIE. */
8022 decl = get_AT_ref (die, DW_AT_specification);
8023 if (decl != NULL)
8025 clone = clone_die (decl);
8026 if (die->comdat_type_p)
8027 add_AT_die_ref (clone, DW_AT_signature, die);
8028 return clone;
8031 clone = new_die_raw (die->die_tag);
8033 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8035 /* We don't want to copy over all attributes.
8036 For example we don't want DW_AT_byte_size because otherwise we will no
8037 longer have a declaration and GDB will treat it as a definition. */
8039 switch (a->dw_attr)
8041 case DW_AT_abstract_origin:
8042 case DW_AT_artificial:
8043 case DW_AT_containing_type:
8044 case DW_AT_external:
8045 case DW_AT_name:
8046 case DW_AT_type:
8047 case DW_AT_virtuality:
8048 case DW_AT_linkage_name:
8049 case DW_AT_MIPS_linkage_name:
8050 add_dwarf_attr (clone, a);
8051 break;
8052 case DW_AT_byte_size:
8053 case DW_AT_alignment:
8054 default:
8055 break;
8059 if (die->comdat_type_p)
8060 add_AT_die_ref (clone, DW_AT_signature, die);
8062 add_AT_flag (clone, DW_AT_declaration, 1);
8063 return clone;
8067 /* Structure to map a DIE in one CU to its copy in a comdat type unit. */
8069 struct decl_table_entry
8071 dw_die_ref orig;
8072 dw_die_ref copy;
8075 /* Helpers to manipulate hash table of copied declarations. */
8077 /* Hashtable helpers. */
8079 struct decl_table_entry_hasher : free_ptr_hash <decl_table_entry>
8081 typedef die_struct *compare_type;
8082 static inline hashval_t hash (const decl_table_entry *);
8083 static inline bool equal (const decl_table_entry *, const die_struct *);
8086 inline hashval_t
8087 decl_table_entry_hasher::hash (const decl_table_entry *entry)
8089 return htab_hash_pointer (entry->orig);
8092 inline bool
8093 decl_table_entry_hasher::equal (const decl_table_entry *entry1,
8094 const die_struct *entry2)
8096 return entry1->orig == entry2;
8099 typedef hash_table<decl_table_entry_hasher> decl_hash_type;
8101 /* Copy DIE and its ancestors, up to, but not including, the compile unit
8102 or type unit entry, to a new tree. Adds the new tree to UNIT and returns
8103 a pointer to the copy of DIE. If DECL_TABLE is provided, it is used
8104 to check if the ancestor has already been copied into UNIT. */
8106 static dw_die_ref
8107 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die,
8108 decl_hash_type *decl_table)
8110 dw_die_ref parent = die->die_parent;
8111 dw_die_ref new_parent = unit;
8112 dw_die_ref copy;
8113 decl_table_entry **slot = NULL;
8114 struct decl_table_entry *entry = NULL;
8116 if (decl_table)
8118 /* Check if the entry has already been copied to UNIT. */
8119 slot = decl_table->find_slot_with_hash (die, htab_hash_pointer (die),
8120 INSERT);
8121 if (*slot != HTAB_EMPTY_ENTRY)
8123 entry = *slot;
8124 return entry->copy;
8127 /* Record in DECL_TABLE that DIE has been copied to UNIT. */
8128 entry = XCNEW (struct decl_table_entry);
8129 entry->orig = die;
8130 entry->copy = NULL;
8131 *slot = entry;
8134 if (parent != NULL)
8136 dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
8137 if (spec != NULL)
8138 parent = spec;
8139 if (!is_unit_die (parent))
8140 new_parent = copy_ancestor_tree (unit, parent, decl_table);
8143 copy = clone_as_declaration (die);
8144 add_child_die (new_parent, copy);
8146 if (decl_table)
8148 /* Record the pointer to the copy. */
8149 entry->copy = copy;
8152 return copy;
8154 /* Copy the declaration context to the new type unit DIE. This includes
8155 any surrounding namespace or type declarations. If the DIE has an
8156 AT_specification attribute, it also includes attributes and children
8157 attached to the specification, and returns a pointer to the original
8158 parent of the declaration DIE. Returns NULL otherwise. */
8160 static dw_die_ref
8161 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
8163 dw_die_ref decl;
8164 dw_die_ref new_decl;
8165 dw_die_ref orig_parent = NULL;
8167 decl = get_AT_ref (die, DW_AT_specification);
8168 if (decl == NULL)
8169 decl = die;
8170 else
8172 unsigned ix;
8173 dw_die_ref c;
8174 dw_attr_node *a;
8176 /* The original DIE will be changed to a declaration, and must
8177 be moved to be a child of the original declaration DIE. */
8178 orig_parent = decl->die_parent;
8180 /* Copy the type node pointer from the new DIE to the original
8181 declaration DIE so we can forward references later. */
8182 decl->comdat_type_p = true;
8183 decl->die_id.die_type_node = die->die_id.die_type_node;
8185 remove_AT (die, DW_AT_specification);
8187 FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
8189 if (a->dw_attr != DW_AT_name
8190 && a->dw_attr != DW_AT_declaration
8191 && a->dw_attr != DW_AT_external)
8192 add_dwarf_attr (die, a);
8195 FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree (c)));
8198 if (decl->die_parent != NULL
8199 && !is_unit_die (decl->die_parent))
8201 new_decl = copy_ancestor_tree (unit, decl, NULL);
8202 if (new_decl != NULL)
8204 remove_AT (new_decl, DW_AT_signature);
8205 add_AT_specification (die, new_decl);
8209 return orig_parent;
8212 /* Generate the skeleton ancestor tree for the given NODE, then clone
8213 the DIE and add the clone into the tree. */
8215 static void
8216 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
8218 if (node->new_die != NULL)
8219 return;
8221 node->new_die = clone_as_declaration (node->old_die);
8223 if (node->parent != NULL)
8225 generate_skeleton_ancestor_tree (node->parent);
8226 add_child_die (node->parent->new_die, node->new_die);
8230 /* Generate a skeleton tree of DIEs containing any declarations that are
8231 found in the original tree. We traverse the tree looking for declaration
8232 DIEs, and construct the skeleton from the bottom up whenever we find one. */
8234 static void
8235 generate_skeleton_bottom_up (skeleton_chain_node *parent)
8237 skeleton_chain_node node;
8238 dw_die_ref c;
8239 dw_die_ref first;
8240 dw_die_ref prev = NULL;
8241 dw_die_ref next = NULL;
8243 node.parent = parent;
8245 first = c = parent->old_die->die_child;
8246 if (c)
8247 next = c->die_sib;
8248 if (c) do {
8249 if (prev == NULL || prev->die_sib == c)
8250 prev = c;
8251 c = next;
8252 next = (c == first ? NULL : c->die_sib);
8253 node.old_die = c;
8254 node.new_die = NULL;
8255 if (is_declaration_die (c))
8257 if (is_template_instantiation (c))
8259 /* Instantiated templates do not need to be cloned into the
8260 type unit. Just move the DIE and its children back to
8261 the skeleton tree (in the main CU). */
8262 remove_child_with_prev (c, prev);
8263 add_child_die (parent->new_die, c);
8264 c = prev;
8266 else if (c->comdat_type_p)
8268 /* This is the skeleton of earlier break_out_comdat_types
8269 type. Clone the existing DIE, but keep the children
8270 under the original (which is in the main CU). */
8271 dw_die_ref clone = clone_die (c);
8273 replace_child (c, clone, prev);
8274 generate_skeleton_ancestor_tree (parent);
8275 add_child_die (parent->new_die, c);
8276 c = clone;
8277 continue;
8279 else
8281 /* Clone the existing DIE, move the original to the skeleton
8282 tree (which is in the main CU), and put the clone, with
8283 all the original's children, where the original came from
8284 (which is about to be moved to the type unit). */
8285 dw_die_ref clone = clone_die (c);
8286 move_all_children (c, clone);
8288 /* If the original has a DW_AT_object_pointer attribute,
8289 it would now point to a child DIE just moved to the
8290 cloned tree, so we need to remove that attribute from
8291 the original. */
8292 remove_AT (c, DW_AT_object_pointer);
8294 replace_child (c, clone, prev);
8295 generate_skeleton_ancestor_tree (parent);
8296 add_child_die (parent->new_die, c);
8297 node.old_die = clone;
8298 node.new_die = c;
8299 c = clone;
8302 generate_skeleton_bottom_up (&node);
8303 } while (next != NULL);
8306 /* Wrapper function for generate_skeleton_bottom_up. */
8308 static dw_die_ref
8309 generate_skeleton (dw_die_ref die)
8311 skeleton_chain_node node;
8313 node.old_die = die;
8314 node.new_die = NULL;
8315 node.parent = NULL;
8317 /* If this type definition is nested inside another type,
8318 and is not an instantiation of a template, always leave
8319 at least a declaration in its place. */
8320 if (die->die_parent != NULL
8321 && is_type_die (die->die_parent)
8322 && !is_template_instantiation (die))
8323 node.new_die = clone_as_declaration (die);
8325 generate_skeleton_bottom_up (&node);
8326 return node.new_die;
8329 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
8330 declaration. The original DIE is moved to a new compile unit so that
8331 existing references to it follow it to the new location. If any of the
8332 original DIE's descendants is a declaration, we need to replace the
8333 original DIE with a skeleton tree and move the declarations back into the
8334 skeleton tree. */
8336 static dw_die_ref
8337 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
8338 dw_die_ref prev)
8340 dw_die_ref skeleton, orig_parent;
8342 /* Copy the declaration context to the type unit DIE. If the returned
8343 ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
8344 that DIE. */
8345 orig_parent = copy_declaration_context (unit, child);
8347 skeleton = generate_skeleton (child);
8348 if (skeleton == NULL)
8349 remove_child_with_prev (child, prev);
8350 else
8352 skeleton->comdat_type_p = true;
8353 skeleton->die_id.die_type_node = child->die_id.die_type_node;
8355 /* If the original DIE was a specification, we need to put
8356 the skeleton under the parent DIE of the declaration.
8357 This leaves the original declaration in the tree, but
8358 it will be pruned later since there are no longer any
8359 references to it. */
8360 if (orig_parent != NULL)
8362 remove_child_with_prev (child, prev);
8363 add_child_die (orig_parent, skeleton);
8365 else
8366 replace_child (child, skeleton, prev);
8369 return skeleton;
8372 static void
8373 copy_dwarf_procs_ref_in_attrs (dw_die_ref die,
8374 comdat_type_node *type_node,
8375 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs);
8377 /* Helper for copy_dwarf_procs_ref_in_dies. Make a copy of the DIE DWARF
8378 procedure, put it under TYPE_NODE and return the copy. Continue looking for
8379 DWARF procedure references in the DW_AT_location attribute. */
8381 static dw_die_ref
8382 copy_dwarf_procedure (dw_die_ref die,
8383 comdat_type_node *type_node,
8384 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8386 gcc_assert (die->die_tag == DW_TAG_dwarf_procedure);
8388 /* DWARF procedures are not supposed to have children... */
8389 gcc_assert (die->die_child == NULL);
8391 /* ... and they are supposed to have only one attribute: DW_AT_location. */
8392 gcc_assert (vec_safe_length (die->die_attr) == 1
8393 && ((*die->die_attr)[0].dw_attr == DW_AT_location));
8395 /* Do not copy more than once DWARF procedures. */
8396 bool existed;
8397 dw_die_ref &die_copy = copied_dwarf_procs.get_or_insert (die, &existed);
8398 if (existed)
8399 return die_copy;
8401 die_copy = clone_die (die);
8402 add_child_die (type_node->root_die, die_copy);
8403 copy_dwarf_procs_ref_in_attrs (die_copy, type_node, copied_dwarf_procs);
8404 return die_copy;
8407 /* Helper for copy_dwarf_procs_ref_in_dies. Look for references to DWARF
8408 procedures in DIE's attributes. */
8410 static void
8411 copy_dwarf_procs_ref_in_attrs (dw_die_ref die,
8412 comdat_type_node *type_node,
8413 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8415 dw_attr_node *a;
8416 unsigned i;
8418 FOR_EACH_VEC_SAFE_ELT (die->die_attr, i, a)
8420 dw_loc_descr_ref loc;
8422 if (a->dw_attr_val.val_class != dw_val_class_loc)
8423 continue;
8425 for (loc = a->dw_attr_val.v.val_loc; loc != NULL; loc = loc->dw_loc_next)
8427 switch (loc->dw_loc_opc)
8429 case DW_OP_call2:
8430 case DW_OP_call4:
8431 case DW_OP_call_ref:
8432 gcc_assert (loc->dw_loc_oprnd1.val_class
8433 == dw_val_class_die_ref);
8434 loc->dw_loc_oprnd1.v.val_die_ref.die
8435 = copy_dwarf_procedure (loc->dw_loc_oprnd1.v.val_die_ref.die,
8436 type_node,
8437 copied_dwarf_procs);
8439 default:
8440 break;
8446 /* Copy DWARF procedures that are referenced by the DIE tree to TREE_NODE and
8447 rewrite references to point to the copies.
8449 References are looked for in DIE's attributes and recursively in all its
8450 children attributes that are location descriptions. COPIED_DWARF_PROCS is a
8451 mapping from old DWARF procedures to their copy. It is used not to copy
8452 twice the same DWARF procedure under TYPE_NODE. */
8454 static void
8455 copy_dwarf_procs_ref_in_dies (dw_die_ref die,
8456 comdat_type_node *type_node,
8457 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8459 dw_die_ref c;
8461 copy_dwarf_procs_ref_in_attrs (die, type_node, copied_dwarf_procs);
8462 FOR_EACH_CHILD (die, c, copy_dwarf_procs_ref_in_dies (c,
8463 type_node,
8464 copied_dwarf_procs));
8467 /* Traverse the DIE and set up additional .debug_types or .debug_info
8468 DW_UT_*type sections for each type worthy of being placed in a COMDAT
8469 section. */
8471 static void
8472 break_out_comdat_types (dw_die_ref die)
8474 dw_die_ref c;
8475 dw_die_ref first;
8476 dw_die_ref prev = NULL;
8477 dw_die_ref next = NULL;
8478 dw_die_ref unit = NULL;
8480 first = c = die->die_child;
8481 if (c)
8482 next = c->die_sib;
8483 if (c) do {
8484 if (prev == NULL || prev->die_sib == c)
8485 prev = c;
8486 c = next;
8487 next = (c == first ? NULL : c->die_sib);
8488 if (should_move_die_to_comdat (c))
8490 dw_die_ref replacement;
8491 comdat_type_node *type_node;
8493 /* Break out nested types into their own type units. */
8494 break_out_comdat_types (c);
8496 /* Create a new type unit DIE as the root for the new tree, and
8497 add it to the list of comdat types. */
8498 unit = new_die (DW_TAG_type_unit, NULL, NULL);
8499 add_AT_unsigned (unit, DW_AT_language,
8500 get_AT_unsigned (comp_unit_die (), DW_AT_language));
8501 type_node = ggc_cleared_alloc<comdat_type_node> ();
8502 type_node->root_die = unit;
8503 type_node->next = comdat_type_list;
8504 comdat_type_list = type_node;
8506 /* Generate the type signature. */
8507 generate_type_signature (c, type_node);
8509 /* Copy the declaration context, attributes, and children of the
8510 declaration into the new type unit DIE, then remove this DIE
8511 from the main CU (or replace it with a skeleton if necessary). */
8512 replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
8513 type_node->skeleton_die = replacement;
8515 /* Add the DIE to the new compunit. */
8516 add_child_die (unit, c);
8518 /* Types can reference DWARF procedures for type size or data location
8519 expressions. Calls in DWARF expressions cannot target procedures
8520 that are not in the same section. So we must copy DWARF procedures
8521 along with this type and then rewrite references to them. */
8522 hash_map<dw_die_ref, dw_die_ref> copied_dwarf_procs;
8523 copy_dwarf_procs_ref_in_dies (c, type_node, copied_dwarf_procs);
8525 if (replacement != NULL)
8526 c = replacement;
8528 else if (c->die_tag == DW_TAG_namespace
8529 || c->die_tag == DW_TAG_class_type
8530 || c->die_tag == DW_TAG_structure_type
8531 || c->die_tag == DW_TAG_union_type)
8533 /* Look for nested types that can be broken out. */
8534 break_out_comdat_types (c);
8536 } while (next != NULL);
8539 /* Like clone_tree, but copy DW_TAG_subprogram DIEs as declarations.
8540 Enter all the cloned children into the hash table decl_table. */
8542 static dw_die_ref
8543 clone_tree_partial (dw_die_ref die, decl_hash_type *decl_table)
8545 dw_die_ref c;
8546 dw_die_ref clone;
8547 struct decl_table_entry *entry;
8548 decl_table_entry **slot;
8550 if (die->die_tag == DW_TAG_subprogram)
8551 clone = clone_as_declaration (die);
8552 else
8553 clone = clone_die (die);
8555 slot = decl_table->find_slot_with_hash (die,
8556 htab_hash_pointer (die), INSERT);
8558 /* Assert that DIE isn't in the hash table yet. If it would be there
8559 before, the ancestors would be necessarily there as well, therefore
8560 clone_tree_partial wouldn't be called. */
8561 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
8563 entry = XCNEW (struct decl_table_entry);
8564 entry->orig = die;
8565 entry->copy = clone;
8566 *slot = entry;
8568 if (die->die_tag != DW_TAG_subprogram)
8569 FOR_EACH_CHILD (die, c,
8570 add_child_die (clone, clone_tree_partial (c, decl_table)));
8572 return clone;
8575 /* Walk the DIE and its children, looking for references to incomplete
8576 or trivial types that are unmarked (i.e., that are not in the current
8577 type_unit). */
8579 static void
8580 copy_decls_walk (dw_die_ref unit, dw_die_ref die, decl_hash_type *decl_table)
8582 dw_die_ref c;
8583 dw_attr_node *a;
8584 unsigned ix;
8586 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8588 if (AT_class (a) == dw_val_class_die_ref)
8590 dw_die_ref targ = AT_ref (a);
8591 decl_table_entry **slot;
8592 struct decl_table_entry *entry;
8594 if (targ->die_mark != 0 || targ->comdat_type_p)
8595 continue;
8597 slot = decl_table->find_slot_with_hash (targ,
8598 htab_hash_pointer (targ),
8599 INSERT);
8601 if (*slot != HTAB_EMPTY_ENTRY)
8603 /* TARG has already been copied, so we just need to
8604 modify the reference to point to the copy. */
8605 entry = *slot;
8606 a->dw_attr_val.v.val_die_ref.die = entry->copy;
8608 else
8610 dw_die_ref parent = unit;
8611 dw_die_ref copy = clone_die (targ);
8613 /* Record in DECL_TABLE that TARG has been copied.
8614 Need to do this now, before the recursive call,
8615 because DECL_TABLE may be expanded and SLOT
8616 would no longer be a valid pointer. */
8617 entry = XCNEW (struct decl_table_entry);
8618 entry->orig = targ;
8619 entry->copy = copy;
8620 *slot = entry;
8622 /* If TARG is not a declaration DIE, we need to copy its
8623 children. */
8624 if (!is_declaration_die (targ))
8626 FOR_EACH_CHILD (
8627 targ, c,
8628 add_child_die (copy,
8629 clone_tree_partial (c, decl_table)));
8632 /* Make sure the cloned tree is marked as part of the
8633 type unit. */
8634 mark_dies (copy);
8636 /* If TARG has surrounding context, copy its ancestor tree
8637 into the new type unit. */
8638 if (targ->die_parent != NULL
8639 && !is_unit_die (targ->die_parent))
8640 parent = copy_ancestor_tree (unit, targ->die_parent,
8641 decl_table);
8643 add_child_die (parent, copy);
8644 a->dw_attr_val.v.val_die_ref.die = copy;
8646 /* Make sure the newly-copied DIE is walked. If it was
8647 installed in a previously-added context, it won't
8648 get visited otherwise. */
8649 if (parent != unit)
8651 /* Find the highest point of the newly-added tree,
8652 mark each node along the way, and walk from there. */
8653 parent->die_mark = 1;
8654 while (parent->die_parent
8655 && parent->die_parent->die_mark == 0)
8657 parent = parent->die_parent;
8658 parent->die_mark = 1;
8660 copy_decls_walk (unit, parent, decl_table);
8666 FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
8669 /* Copy declarations for "unworthy" types into the new comdat section.
8670 Incomplete types, modified types, and certain other types aren't broken
8671 out into comdat sections of their own, so they don't have a signature,
8672 and we need to copy the declaration into the same section so that we
8673 don't have an external reference. */
8675 static void
8676 copy_decls_for_unworthy_types (dw_die_ref unit)
8678 mark_dies (unit);
8679 decl_hash_type decl_table (10);
8680 copy_decls_walk (unit, unit, &decl_table);
8681 unmark_dies (unit);
8684 /* Traverse the DIE and add a sibling attribute if it may have the
8685 effect of speeding up access to siblings. To save some space,
8686 avoid generating sibling attributes for DIE's without children. */
8688 static void
8689 add_sibling_attributes (dw_die_ref die)
8691 dw_die_ref c;
8693 if (! die->die_child)
8694 return;
8696 if (die->die_parent && die != die->die_parent->die_child)
8697 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
8699 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
8702 /* Output all location lists for the DIE and its children. */
8704 static void
8705 output_location_lists (dw_die_ref die)
8707 dw_die_ref c;
8708 dw_attr_node *a;
8709 unsigned ix;
8711 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8712 if (AT_class (a) == dw_val_class_loc_list)
8713 output_loc_list (AT_loc_list (a));
8715 FOR_EACH_CHILD (die, c, output_location_lists (c));
8718 /* During assign_location_list_indexes and output_loclists_offset the
8719 current index, after it the number of assigned indexes (i.e. how
8720 large the .debug_loclists* offset table should be). */
8721 static unsigned int loc_list_idx;
8723 /* Output all location list offsets for the DIE and its children. */
8725 static void
8726 output_loclists_offsets (dw_die_ref die)
8728 dw_die_ref c;
8729 dw_attr_node *a;
8730 unsigned ix;
8732 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8733 if (AT_class (a) == dw_val_class_loc_list)
8735 dw_loc_list_ref l = AT_loc_list (a);
8736 if (l->offset_emitted)
8737 continue;
8738 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l->ll_symbol,
8739 loc_section_label, NULL);
8740 gcc_assert (l->hash == loc_list_idx);
8741 loc_list_idx++;
8742 l->offset_emitted = true;
8745 FOR_EACH_CHILD (die, c, output_loclists_offsets (c));
8748 /* Recursively set indexes of location lists. */
8750 static void
8751 assign_location_list_indexes (dw_die_ref die)
8753 dw_die_ref c;
8754 dw_attr_node *a;
8755 unsigned ix;
8757 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8758 if (AT_class (a) == dw_val_class_loc_list)
8760 dw_loc_list_ref list = AT_loc_list (a);
8761 if (!list->num_assigned)
8763 list->num_assigned = true;
8764 list->hash = loc_list_idx++;
8768 FOR_EACH_CHILD (die, c, assign_location_list_indexes (c));
8771 /* We want to limit the number of external references, because they are
8772 larger than local references: a relocation takes multiple words, and
8773 even a sig8 reference is always eight bytes, whereas a local reference
8774 can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
8775 So if we encounter multiple external references to the same type DIE, we
8776 make a local typedef stub for it and redirect all references there.
8778 This is the element of the hash table for keeping track of these
8779 references. */
8781 struct external_ref
8783 dw_die_ref type;
8784 dw_die_ref stub;
8785 unsigned n_refs;
8788 /* Hashtable helpers. */
8790 struct external_ref_hasher : free_ptr_hash <external_ref>
8792 static inline hashval_t hash (const external_ref *);
8793 static inline bool equal (const external_ref *, const external_ref *);
8796 inline hashval_t
8797 external_ref_hasher::hash (const external_ref *r)
8799 dw_die_ref die = r->type;
8800 hashval_t h = 0;
8802 /* We can't use the address of the DIE for hashing, because
8803 that will make the order of the stub DIEs non-deterministic. */
8804 if (! die->comdat_type_p)
8805 /* We have a symbol; use it to compute a hash. */
8806 h = htab_hash_string (die->die_id.die_symbol);
8807 else
8809 /* We have a type signature; use a subset of the bits as the hash.
8810 The 8-byte signature is at least as large as hashval_t. */
8811 comdat_type_node *type_node = die->die_id.die_type_node;
8812 memcpy (&h, type_node->signature, sizeof (h));
8814 return h;
8817 inline bool
8818 external_ref_hasher::equal (const external_ref *r1, const external_ref *r2)
8820 return r1->type == r2->type;
8823 typedef hash_table<external_ref_hasher> external_ref_hash_type;
8825 /* Return a pointer to the external_ref for references to DIE. */
8827 static struct external_ref *
8828 lookup_external_ref (external_ref_hash_type *map, dw_die_ref die)
8830 struct external_ref ref, *ref_p;
8831 external_ref **slot;
8833 ref.type = die;
8834 slot = map->find_slot (&ref, INSERT);
8835 if (*slot != HTAB_EMPTY_ENTRY)
8836 return *slot;
8838 ref_p = XCNEW (struct external_ref);
8839 ref_p->type = die;
8840 *slot = ref_p;
8841 return ref_p;
8844 /* Subroutine of optimize_external_refs, below.
8846 If we see a type skeleton, record it as our stub. If we see external
8847 references, remember how many we've seen. */
8849 static void
8850 optimize_external_refs_1 (dw_die_ref die, external_ref_hash_type *map)
8852 dw_die_ref c;
8853 dw_attr_node *a;
8854 unsigned ix;
8855 struct external_ref *ref_p;
8857 if (is_type_die (die)
8858 && (c = get_AT_ref (die, DW_AT_signature)))
8860 /* This is a local skeleton; use it for local references. */
8861 ref_p = lookup_external_ref (map, c);
8862 ref_p->stub = die;
8865 /* Scan the DIE references, and remember any that refer to DIEs from
8866 other CUs (i.e. those which are not marked). */
8867 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8868 if (AT_class (a) == dw_val_class_die_ref
8869 && (c = AT_ref (a))->die_mark == 0
8870 && is_type_die (c))
8872 ref_p = lookup_external_ref (map, c);
8873 ref_p->n_refs++;
8876 FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
8879 /* htab_traverse callback function for optimize_external_refs, below. SLOT
8880 points to an external_ref, DATA is the CU we're processing. If we don't
8881 already have a local stub, and we have multiple refs, build a stub. */
8884 dwarf2_build_local_stub (external_ref **slot, dw_die_ref data)
8886 struct external_ref *ref_p = *slot;
8888 if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
8890 /* We have multiple references to this type, so build a small stub.
8891 Both of these forms are a bit dodgy from the perspective of the
8892 DWARF standard, since technically they should have names. */
8893 dw_die_ref cu = data;
8894 dw_die_ref type = ref_p->type;
8895 dw_die_ref stub = NULL;
8897 if (type->comdat_type_p)
8899 /* If we refer to this type via sig8, use AT_signature. */
8900 stub = new_die (type->die_tag, cu, NULL_TREE);
8901 add_AT_die_ref (stub, DW_AT_signature, type);
8903 else
8905 /* Otherwise, use a typedef with no name. */
8906 stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
8907 add_AT_die_ref (stub, DW_AT_type, type);
8910 stub->die_mark++;
8911 ref_p->stub = stub;
8913 return 1;
8916 /* DIE is a unit; look through all the DIE references to see if there are
8917 any external references to types, and if so, create local stubs for
8918 them which will be applied in build_abbrev_table. This is useful because
8919 references to local DIEs are smaller. */
8921 static external_ref_hash_type *
8922 optimize_external_refs (dw_die_ref die)
8924 external_ref_hash_type *map = new external_ref_hash_type (10);
8925 optimize_external_refs_1 (die, map);
8926 map->traverse <dw_die_ref, dwarf2_build_local_stub> (die);
8927 return map;
8930 /* The following 3 variables are temporaries that are computed only during the
8931 build_abbrev_table call and used and released during the following
8932 optimize_abbrev_table call. */
8934 /* First abbrev_id that can be optimized based on usage. */
8935 static unsigned int abbrev_opt_start;
8937 /* Maximum abbrev_id of a base type plus one (we can't optimize DIEs with
8938 abbrev_id smaller than this, because they must be already sized
8939 during build_abbrev_table). */
8940 static unsigned int abbrev_opt_base_type_end;
8942 /* Vector of usage counts during build_abbrev_table. Indexed by
8943 abbrev_id - abbrev_opt_start. */
8944 static vec<unsigned int> abbrev_usage_count;
8946 /* Vector of all DIEs added with die_abbrev >= abbrev_opt_start. */
8947 static vec<dw_die_ref> sorted_abbrev_dies;
8949 /* The format of each DIE (and its attribute value pairs) is encoded in an
8950 abbreviation table. This routine builds the abbreviation table and assigns
8951 a unique abbreviation id for each abbreviation entry. The children of each
8952 die are visited recursively. */
8954 static void
8955 build_abbrev_table (dw_die_ref die, external_ref_hash_type *extern_map)
8957 unsigned int abbrev_id = 0;
8958 dw_die_ref c;
8959 dw_attr_node *a;
8960 unsigned ix;
8961 dw_die_ref abbrev;
8963 /* Scan the DIE references, and replace any that refer to
8964 DIEs from other CUs (i.e. those which are not marked) with
8965 the local stubs we built in optimize_external_refs. */
8966 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8967 if (AT_class (a) == dw_val_class_die_ref
8968 && (c = AT_ref (a))->die_mark == 0)
8970 struct external_ref *ref_p;
8971 gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
8973 ref_p = lookup_external_ref (extern_map, c);
8974 if (ref_p->stub && ref_p->stub != die)
8975 change_AT_die_ref (a, ref_p->stub);
8976 else
8977 /* We aren't changing this reference, so mark it external. */
8978 set_AT_ref_external (a, 1);
8981 FOR_EACH_VEC_SAFE_ELT (abbrev_die_table, abbrev_id, abbrev)
8983 dw_attr_node *die_a, *abbrev_a;
8984 unsigned ix;
8985 bool ok = true;
8987 if (abbrev_id == 0)
8988 continue;
8989 if (abbrev->die_tag != die->die_tag)
8990 continue;
8991 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
8992 continue;
8994 if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
8995 continue;
8997 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
8999 abbrev_a = &(*abbrev->die_attr)[ix];
9000 if ((abbrev_a->dw_attr != die_a->dw_attr)
9001 || (value_format (abbrev_a) != value_format (die_a)))
9003 ok = false;
9004 break;
9007 if (ok)
9008 break;
9011 if (abbrev_id >= vec_safe_length (abbrev_die_table))
9013 vec_safe_push (abbrev_die_table, die);
9014 if (abbrev_opt_start)
9015 abbrev_usage_count.safe_push (0);
9017 if (abbrev_opt_start && abbrev_id >= abbrev_opt_start)
9019 abbrev_usage_count[abbrev_id - abbrev_opt_start]++;
9020 sorted_abbrev_dies.safe_push (die);
9023 die->die_abbrev = abbrev_id;
9024 FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
9027 /* Callback function for sorted_abbrev_dies vector sorting. We sort
9028 by die_abbrev's usage count, from the most commonly used
9029 abbreviation to the least. */
9031 static int
9032 die_abbrev_cmp (const void *p1, const void *p2)
9034 dw_die_ref die1 = *(const dw_die_ref *) p1;
9035 dw_die_ref die2 = *(const dw_die_ref *) p2;
9037 gcc_checking_assert (die1->die_abbrev >= abbrev_opt_start);
9038 gcc_checking_assert (die2->die_abbrev >= abbrev_opt_start);
9040 if (die1->die_abbrev >= abbrev_opt_base_type_end
9041 && die2->die_abbrev >= abbrev_opt_base_type_end)
9043 if (abbrev_usage_count[die1->die_abbrev - abbrev_opt_start]
9044 > abbrev_usage_count[die2->die_abbrev - abbrev_opt_start])
9045 return -1;
9046 if (abbrev_usage_count[die1->die_abbrev - abbrev_opt_start]
9047 < abbrev_usage_count[die2->die_abbrev - abbrev_opt_start])
9048 return 1;
9051 /* Stabilize the sort. */
9052 if (die1->die_abbrev < die2->die_abbrev)
9053 return -1;
9054 if (die1->die_abbrev > die2->die_abbrev)
9055 return 1;
9057 return 0;
9060 /* Convert dw_val_class_const and dw_val_class_unsigned_const class attributes
9061 of DIEs in between sorted_abbrev_dies[first_id] and abbrev_dies[end_id - 1]
9062 into dw_val_class_const_implicit or
9063 dw_val_class_unsigned_const_implicit. */
9065 static void
9066 optimize_implicit_const (unsigned int first_id, unsigned int end,
9067 vec<bool> &implicit_consts)
9069 /* It never makes sense if there is just one DIE using the abbreviation. */
9070 if (end < first_id + 2)
9071 return;
9073 dw_attr_node *a;
9074 unsigned ix, i;
9075 dw_die_ref die = sorted_abbrev_dies[first_id];
9076 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9077 if (implicit_consts[ix])
9079 enum dw_val_class new_class = dw_val_class_none;
9080 switch (AT_class (a))
9082 case dw_val_class_unsigned_const:
9083 if ((HOST_WIDE_INT) AT_unsigned (a) < 0)
9084 continue;
9086 /* The .debug_abbrev section will grow by
9087 size_of_sleb128 (AT_unsigned (a)) and we avoid the constants
9088 in all the DIEs using that abbreviation. */
9089 if (constant_size (AT_unsigned (a)) * (end - first_id)
9090 <= (unsigned) size_of_sleb128 (AT_unsigned (a)))
9091 continue;
9093 new_class = dw_val_class_unsigned_const_implicit;
9094 break;
9096 case dw_val_class_const:
9097 new_class = dw_val_class_const_implicit;
9098 break;
9100 case dw_val_class_file:
9101 new_class = dw_val_class_file_implicit;
9102 break;
9104 default:
9105 continue;
9107 for (i = first_id; i < end; i++)
9108 (*sorted_abbrev_dies[i]->die_attr)[ix].dw_attr_val.val_class
9109 = new_class;
9113 /* Attempt to optimize abbreviation table from abbrev_opt_start
9114 abbreviation above. */
9116 static void
9117 optimize_abbrev_table (void)
9119 if (abbrev_opt_start
9120 && vec_safe_length (abbrev_die_table) > abbrev_opt_start
9121 && (dwarf_version >= 5 || vec_safe_length (abbrev_die_table) > 127))
9123 auto_vec<bool, 32> implicit_consts;
9124 sorted_abbrev_dies.qsort (die_abbrev_cmp);
9126 unsigned int abbrev_id = abbrev_opt_start - 1;
9127 unsigned int first_id = ~0U;
9128 unsigned int last_abbrev_id = 0;
9129 unsigned int i;
9130 dw_die_ref die;
9131 if (abbrev_opt_base_type_end > abbrev_opt_start)
9132 abbrev_id = abbrev_opt_base_type_end - 1;
9133 /* Reassign abbreviation ids from abbrev_opt_start above, so that
9134 most commonly used abbreviations come first. */
9135 FOR_EACH_VEC_ELT (sorted_abbrev_dies, i, die)
9137 dw_attr_node *a;
9138 unsigned ix;
9140 /* If calc_base_type_die_sizes has been called, the CU and
9141 base types after it can't be optimized, because we've already
9142 calculated their DIE offsets. We've sorted them first. */
9143 if (die->die_abbrev < abbrev_opt_base_type_end)
9144 continue;
9145 if (die->die_abbrev != last_abbrev_id)
9147 last_abbrev_id = die->die_abbrev;
9148 if (dwarf_version >= 5 && first_id != ~0U)
9149 optimize_implicit_const (first_id, i, implicit_consts);
9150 abbrev_id++;
9151 (*abbrev_die_table)[abbrev_id] = die;
9152 if (dwarf_version >= 5)
9154 first_id = i;
9155 implicit_consts.truncate (0);
9157 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9158 switch (AT_class (a))
9160 case dw_val_class_const:
9161 case dw_val_class_unsigned_const:
9162 case dw_val_class_file:
9163 implicit_consts.safe_push (true);
9164 break;
9165 default:
9166 implicit_consts.safe_push (false);
9167 break;
9171 else if (dwarf_version >= 5)
9173 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9174 if (!implicit_consts[ix])
9175 continue;
9176 else
9178 dw_attr_node *other_a
9179 = &(*(*abbrev_die_table)[abbrev_id]->die_attr)[ix];
9180 if (!dw_val_equal_p (&a->dw_attr_val,
9181 &other_a->dw_attr_val))
9182 implicit_consts[ix] = false;
9185 die->die_abbrev = abbrev_id;
9187 gcc_assert (abbrev_id == vec_safe_length (abbrev_die_table) - 1);
9188 if (dwarf_version >= 5 && first_id != ~0U)
9189 optimize_implicit_const (first_id, i, implicit_consts);
9192 abbrev_opt_start = 0;
9193 abbrev_opt_base_type_end = 0;
9194 abbrev_usage_count.release ();
9195 sorted_abbrev_dies.release ();
9198 /* Return the power-of-two number of bytes necessary to represent VALUE. */
9200 static int
9201 constant_size (unsigned HOST_WIDE_INT value)
9203 int log;
9205 if (value == 0)
9206 log = 0;
9207 else
9208 log = floor_log2 (value);
9210 log = log / 8;
9211 log = 1 << (floor_log2 (log) + 1);
9213 return log;
9216 /* Return the size of a DIE as it is represented in the
9217 .debug_info section. */
9219 static unsigned long
9220 size_of_die (dw_die_ref die)
9222 unsigned long size = 0;
9223 dw_attr_node *a;
9224 unsigned ix;
9225 enum dwarf_form form;
9227 size += size_of_uleb128 (die->die_abbrev);
9228 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9230 switch (AT_class (a))
9232 case dw_val_class_addr:
9233 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
9235 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
9236 size += size_of_uleb128 (AT_index (a));
9238 else
9239 size += DWARF2_ADDR_SIZE;
9240 break;
9241 case dw_val_class_offset:
9242 size += DWARF_OFFSET_SIZE;
9243 break;
9244 case dw_val_class_loc:
9246 unsigned long lsize = size_of_locs (AT_loc (a));
9248 /* Block length. */
9249 if (dwarf_version >= 4)
9250 size += size_of_uleb128 (lsize);
9251 else
9252 size += constant_size (lsize);
9253 size += lsize;
9255 break;
9256 case dw_val_class_loc_list:
9257 case dw_val_class_view_list:
9258 if (dwarf_split_debug_info && dwarf_version >= 5)
9260 gcc_assert (AT_loc_list (a)->num_assigned);
9261 size += size_of_uleb128 (AT_loc_list (a)->hash);
9263 else
9264 size += DWARF_OFFSET_SIZE;
9265 break;
9266 case dw_val_class_range_list:
9267 if (value_format (a) == DW_FORM_rnglistx)
9269 gcc_assert (rnglist_idx);
9270 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
9271 size += size_of_uleb128 (r->idx);
9273 else
9274 size += DWARF_OFFSET_SIZE;
9275 break;
9276 case dw_val_class_const:
9277 size += size_of_sleb128 (AT_int (a));
9278 break;
9279 case dw_val_class_unsigned_const:
9281 int csize = constant_size (AT_unsigned (a));
9282 if (dwarf_version == 3
9283 && a->dw_attr == DW_AT_data_member_location
9284 && csize >= 4)
9285 size += size_of_uleb128 (AT_unsigned (a));
9286 else
9287 size += csize;
9289 break;
9290 case dw_val_class_const_implicit:
9291 case dw_val_class_unsigned_const_implicit:
9292 case dw_val_class_file_implicit:
9293 /* These occupy no size in the DIE, just an extra sleb128 in
9294 .debug_abbrev. */
9295 break;
9296 case dw_val_class_const_double:
9297 size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
9298 if (HOST_BITS_PER_WIDE_INT >= DWARF_LARGEST_DATA_FORM_BITS)
9299 size++; /* block */
9300 break;
9301 case dw_val_class_wide_int:
9302 size += (get_full_len (*a->dw_attr_val.v.val_wide)
9303 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
9304 if (get_full_len (*a->dw_attr_val.v.val_wide)
9305 * HOST_BITS_PER_WIDE_INT > DWARF_LARGEST_DATA_FORM_BITS)
9306 size++; /* block */
9307 break;
9308 case dw_val_class_vec:
9309 size += constant_size (a->dw_attr_val.v.val_vec.length
9310 * a->dw_attr_val.v.val_vec.elt_size)
9311 + a->dw_attr_val.v.val_vec.length
9312 * a->dw_attr_val.v.val_vec.elt_size; /* block */
9313 break;
9314 case dw_val_class_flag:
9315 if (dwarf_version >= 4)
9316 /* Currently all add_AT_flag calls pass in 1 as last argument,
9317 so DW_FORM_flag_present can be used. If that ever changes,
9318 we'll need to use DW_FORM_flag and have some optimization
9319 in build_abbrev_table that will change those to
9320 DW_FORM_flag_present if it is set to 1 in all DIEs using
9321 the same abbrev entry. */
9322 gcc_assert (a->dw_attr_val.v.val_flag == 1);
9323 else
9324 size += 1;
9325 break;
9326 case dw_val_class_die_ref:
9327 if (AT_ref_external (a))
9329 /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
9330 we use DW_FORM_ref_addr. In DWARF2, DW_FORM_ref_addr
9331 is sized by target address length, whereas in DWARF3
9332 it's always sized as an offset. */
9333 if (use_debug_types)
9334 size += DWARF_TYPE_SIGNATURE_SIZE;
9335 else if (dwarf_version == 2)
9336 size += DWARF2_ADDR_SIZE;
9337 else
9338 size += DWARF_OFFSET_SIZE;
9340 else
9341 size += DWARF_OFFSET_SIZE;
9342 break;
9343 case dw_val_class_fde_ref:
9344 size += DWARF_OFFSET_SIZE;
9345 break;
9346 case dw_val_class_lbl_id:
9347 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
9349 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
9350 size += size_of_uleb128 (AT_index (a));
9352 else
9353 size += DWARF2_ADDR_SIZE;
9354 break;
9355 case dw_val_class_lineptr:
9356 case dw_val_class_macptr:
9357 case dw_val_class_loclistsptr:
9358 size += DWARF_OFFSET_SIZE;
9359 break;
9360 case dw_val_class_str:
9361 form = AT_string_form (a);
9362 if (form == DW_FORM_strp || form == DW_FORM_line_strp)
9363 size += DWARF_OFFSET_SIZE;
9364 else if (form == DW_FORM_GNU_str_index)
9365 size += size_of_uleb128 (AT_index (a));
9366 else
9367 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
9368 break;
9369 case dw_val_class_file:
9370 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
9371 break;
9372 case dw_val_class_data8:
9373 size += 8;
9374 break;
9375 case dw_val_class_vms_delta:
9376 size += DWARF_OFFSET_SIZE;
9377 break;
9378 case dw_val_class_high_pc:
9379 size += DWARF2_ADDR_SIZE;
9380 break;
9381 case dw_val_class_discr_value:
9382 size += size_of_discr_value (&a->dw_attr_val.v.val_discr_value);
9383 break;
9384 case dw_val_class_discr_list:
9386 unsigned block_size = size_of_discr_list (AT_discr_list (a));
9388 /* This is a block, so we have the block length and then its
9389 data. */
9390 size += constant_size (block_size) + block_size;
9392 break;
9393 default:
9394 gcc_unreachable ();
9398 return size;
9401 /* Size the debugging information associated with a given DIE. Visits the
9402 DIE's children recursively. Updates the global variable next_die_offset, on
9403 each time through. Uses the current value of next_die_offset to update the
9404 die_offset field in each DIE. */
9406 static void
9407 calc_die_sizes (dw_die_ref die)
9409 dw_die_ref c;
9411 gcc_assert (die->die_offset == 0
9412 || (unsigned long int) die->die_offset == next_die_offset);
9413 die->die_offset = next_die_offset;
9414 next_die_offset += size_of_die (die);
9416 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
9418 if (die->die_child != NULL)
9419 /* Count the null byte used to terminate sibling lists. */
9420 next_die_offset += 1;
9423 /* Size just the base type children at the start of the CU.
9424 This is needed because build_abbrev needs to size locs
9425 and sizing of type based stack ops needs to know die_offset
9426 values for the base types. */
9428 static void
9429 calc_base_type_die_sizes (void)
9431 unsigned long die_offset = (dwarf_split_debug_info
9432 ? DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
9433 : DWARF_COMPILE_UNIT_HEADER_SIZE);
9434 unsigned int i;
9435 dw_die_ref base_type;
9436 #if ENABLE_ASSERT_CHECKING
9437 dw_die_ref prev = comp_unit_die ()->die_child;
9438 #endif
9440 die_offset += size_of_die (comp_unit_die ());
9441 for (i = 0; base_types.iterate (i, &base_type); i++)
9443 #if ENABLE_ASSERT_CHECKING
9444 gcc_assert (base_type->die_offset == 0
9445 && prev->die_sib == base_type
9446 && base_type->die_child == NULL
9447 && base_type->die_abbrev);
9448 prev = base_type;
9449 #endif
9450 if (abbrev_opt_start
9451 && base_type->die_abbrev >= abbrev_opt_base_type_end)
9452 abbrev_opt_base_type_end = base_type->die_abbrev + 1;
9453 base_type->die_offset = die_offset;
9454 die_offset += size_of_die (base_type);
9458 /* Set the marks for a die and its children. We do this so
9459 that we know whether or not a reference needs to use FORM_ref_addr; only
9460 DIEs in the same CU will be marked. We used to clear out the offset
9461 and use that as the flag, but ran into ordering problems. */
9463 static void
9464 mark_dies (dw_die_ref die)
9466 dw_die_ref c;
9468 gcc_assert (!die->die_mark);
9470 die->die_mark = 1;
9471 FOR_EACH_CHILD (die, c, mark_dies (c));
9474 /* Clear the marks for a die and its children. */
9476 static void
9477 unmark_dies (dw_die_ref die)
9479 dw_die_ref c;
9481 if (! use_debug_types)
9482 gcc_assert (die->die_mark);
9484 die->die_mark = 0;
9485 FOR_EACH_CHILD (die, c, unmark_dies (c));
9488 /* Clear the marks for a die, its children and referred dies. */
9490 static void
9491 unmark_all_dies (dw_die_ref die)
9493 dw_die_ref c;
9494 dw_attr_node *a;
9495 unsigned ix;
9497 if (!die->die_mark)
9498 return;
9499 die->die_mark = 0;
9501 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
9503 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9504 if (AT_class (a) == dw_val_class_die_ref)
9505 unmark_all_dies (AT_ref (a));
9508 /* Calculate if the entry should appear in the final output file. It may be
9509 from a pruned a type. */
9511 static bool
9512 include_pubname_in_output (vec<pubname_entry, va_gc> *table, pubname_entry *p)
9514 /* By limiting gnu pubnames to definitions only, gold can generate a
9515 gdb index without entries for declarations, which don't include
9516 enough information to be useful. */
9517 if (debug_generate_pub_sections == 2 && is_declaration_die (p->die))
9518 return false;
9520 if (table == pubname_table)
9522 /* Enumerator names are part of the pubname table, but the
9523 parent DW_TAG_enumeration_type die may have been pruned.
9524 Don't output them if that is the case. */
9525 if (p->die->die_tag == DW_TAG_enumerator &&
9526 (p->die->die_parent == NULL
9527 || !p->die->die_parent->die_perennial_p))
9528 return false;
9530 /* Everything else in the pubname table is included. */
9531 return true;
9534 /* The pubtypes table shouldn't include types that have been
9535 pruned. */
9536 return (p->die->die_offset != 0
9537 || !flag_eliminate_unused_debug_types);
9540 /* Return the size of the .debug_pubnames or .debug_pubtypes table
9541 generated for the compilation unit. */
9543 static unsigned long
9544 size_of_pubnames (vec<pubname_entry, va_gc> *names)
9546 unsigned long size;
9547 unsigned i;
9548 pubname_entry *p;
9549 int space_for_flags = (debug_generate_pub_sections == 2) ? 1 : 0;
9551 size = DWARF_PUBNAMES_HEADER_SIZE;
9552 FOR_EACH_VEC_ELT (*names, i, p)
9553 if (include_pubname_in_output (names, p))
9554 size += strlen (p->name) + DWARF_OFFSET_SIZE + 1 + space_for_flags;
9556 size += DWARF_OFFSET_SIZE;
9557 return size;
9560 /* Return the size of the information in the .debug_aranges section. */
9562 static unsigned long
9563 size_of_aranges (void)
9565 unsigned long size;
9567 size = DWARF_ARANGES_HEADER_SIZE;
9569 /* Count the address/length pair for this compilation unit. */
9570 if (text_section_used)
9571 size += 2 * DWARF2_ADDR_SIZE;
9572 if (cold_text_section_used)
9573 size += 2 * DWARF2_ADDR_SIZE;
9574 if (have_multiple_function_sections)
9576 unsigned fde_idx;
9577 dw_fde_ref fde;
9579 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
9581 if (DECL_IGNORED_P (fde->decl))
9582 continue;
9583 if (!fde->in_std_section)
9584 size += 2 * DWARF2_ADDR_SIZE;
9585 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
9586 size += 2 * DWARF2_ADDR_SIZE;
9590 /* Count the two zero words used to terminated the address range table. */
9591 size += 2 * DWARF2_ADDR_SIZE;
9592 return size;
9595 /* Select the encoding of an attribute value. */
9597 static enum dwarf_form
9598 value_format (dw_attr_node *a)
9600 switch (AT_class (a))
9602 case dw_val_class_addr:
9603 /* Only very few attributes allow DW_FORM_addr. */
9604 switch (a->dw_attr)
9606 case DW_AT_low_pc:
9607 case DW_AT_high_pc:
9608 case DW_AT_entry_pc:
9609 case DW_AT_trampoline:
9610 return (AT_index (a) == NOT_INDEXED
9611 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
9612 default:
9613 break;
9615 switch (DWARF2_ADDR_SIZE)
9617 case 1:
9618 return DW_FORM_data1;
9619 case 2:
9620 return DW_FORM_data2;
9621 case 4:
9622 return DW_FORM_data4;
9623 case 8:
9624 return DW_FORM_data8;
9625 default:
9626 gcc_unreachable ();
9628 case dw_val_class_loc_list:
9629 case dw_val_class_view_list:
9630 if (dwarf_split_debug_info
9631 && dwarf_version >= 5
9632 && AT_loc_list (a)->num_assigned)
9633 return DW_FORM_loclistx;
9634 /* FALLTHRU */
9635 case dw_val_class_range_list:
9636 /* For range lists in DWARF 5, use DW_FORM_rnglistx from .debug_info.dwo
9637 but in .debug_info use DW_FORM_sec_offset, which is shorter if we
9638 care about sizes of .debug* sections in shared libraries and
9639 executables and don't take into account relocations that affect just
9640 relocatable objects - for DW_FORM_rnglistx we'd have to emit offset
9641 table in the .debug_rnglists section. */
9642 if (dwarf_split_debug_info
9643 && dwarf_version >= 5
9644 && AT_class (a) == dw_val_class_range_list
9645 && rnglist_idx
9646 && a->dw_attr_val.val_entry != RELOCATED_OFFSET)
9647 return DW_FORM_rnglistx;
9648 if (dwarf_version >= 4)
9649 return DW_FORM_sec_offset;
9650 /* FALLTHRU */
9651 case dw_val_class_vms_delta:
9652 case dw_val_class_offset:
9653 switch (DWARF_OFFSET_SIZE)
9655 case 4:
9656 return DW_FORM_data4;
9657 case 8:
9658 return DW_FORM_data8;
9659 default:
9660 gcc_unreachable ();
9662 case dw_val_class_loc:
9663 if (dwarf_version >= 4)
9664 return DW_FORM_exprloc;
9665 switch (constant_size (size_of_locs (AT_loc (a))))
9667 case 1:
9668 return DW_FORM_block1;
9669 case 2:
9670 return DW_FORM_block2;
9671 case 4:
9672 return DW_FORM_block4;
9673 default:
9674 gcc_unreachable ();
9676 case dw_val_class_const:
9677 return DW_FORM_sdata;
9678 case dw_val_class_unsigned_const:
9679 switch (constant_size (AT_unsigned (a)))
9681 case 1:
9682 return DW_FORM_data1;
9683 case 2:
9684 return DW_FORM_data2;
9685 case 4:
9686 /* In DWARF3 DW_AT_data_member_location with
9687 DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
9688 constant, so we need to use DW_FORM_udata if we need
9689 a large constant. */
9690 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
9691 return DW_FORM_udata;
9692 return DW_FORM_data4;
9693 case 8:
9694 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
9695 return DW_FORM_udata;
9696 return DW_FORM_data8;
9697 default:
9698 gcc_unreachable ();
9700 case dw_val_class_const_implicit:
9701 case dw_val_class_unsigned_const_implicit:
9702 case dw_val_class_file_implicit:
9703 return DW_FORM_implicit_const;
9704 case dw_val_class_const_double:
9705 switch (HOST_BITS_PER_WIDE_INT)
9707 case 8:
9708 return DW_FORM_data2;
9709 case 16:
9710 return DW_FORM_data4;
9711 case 32:
9712 return DW_FORM_data8;
9713 case 64:
9714 if (dwarf_version >= 5)
9715 return DW_FORM_data16;
9716 /* FALLTHRU */
9717 default:
9718 return DW_FORM_block1;
9720 case dw_val_class_wide_int:
9721 switch (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT)
9723 case 8:
9724 return DW_FORM_data1;
9725 case 16:
9726 return DW_FORM_data2;
9727 case 32:
9728 return DW_FORM_data4;
9729 case 64:
9730 return DW_FORM_data8;
9731 case 128:
9732 if (dwarf_version >= 5)
9733 return DW_FORM_data16;
9734 /* FALLTHRU */
9735 default:
9736 return DW_FORM_block1;
9738 case dw_val_class_vec:
9739 switch (constant_size (a->dw_attr_val.v.val_vec.length
9740 * a->dw_attr_val.v.val_vec.elt_size))
9742 case 1:
9743 return DW_FORM_block1;
9744 case 2:
9745 return DW_FORM_block2;
9746 case 4:
9747 return DW_FORM_block4;
9748 default:
9749 gcc_unreachable ();
9751 case dw_val_class_flag:
9752 if (dwarf_version >= 4)
9754 /* Currently all add_AT_flag calls pass in 1 as last argument,
9755 so DW_FORM_flag_present can be used. If that ever changes,
9756 we'll need to use DW_FORM_flag and have some optimization
9757 in build_abbrev_table that will change those to
9758 DW_FORM_flag_present if it is set to 1 in all DIEs using
9759 the same abbrev entry. */
9760 gcc_assert (a->dw_attr_val.v.val_flag == 1);
9761 return DW_FORM_flag_present;
9763 return DW_FORM_flag;
9764 case dw_val_class_die_ref:
9765 if (AT_ref_external (a))
9766 return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
9767 else
9768 return DW_FORM_ref;
9769 case dw_val_class_fde_ref:
9770 return DW_FORM_data;
9771 case dw_val_class_lbl_id:
9772 return (AT_index (a) == NOT_INDEXED
9773 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
9774 case dw_val_class_lineptr:
9775 case dw_val_class_macptr:
9776 case dw_val_class_loclistsptr:
9777 return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
9778 case dw_val_class_str:
9779 return AT_string_form (a);
9780 case dw_val_class_file:
9781 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
9783 case 1:
9784 return DW_FORM_data1;
9785 case 2:
9786 return DW_FORM_data2;
9787 case 4:
9788 return DW_FORM_data4;
9789 default:
9790 gcc_unreachable ();
9793 case dw_val_class_data8:
9794 return DW_FORM_data8;
9796 case dw_val_class_high_pc:
9797 switch (DWARF2_ADDR_SIZE)
9799 case 1:
9800 return DW_FORM_data1;
9801 case 2:
9802 return DW_FORM_data2;
9803 case 4:
9804 return DW_FORM_data4;
9805 case 8:
9806 return DW_FORM_data8;
9807 default:
9808 gcc_unreachable ();
9811 case dw_val_class_discr_value:
9812 return (a->dw_attr_val.v.val_discr_value.pos
9813 ? DW_FORM_udata
9814 : DW_FORM_sdata);
9815 case dw_val_class_discr_list:
9816 switch (constant_size (size_of_discr_list (AT_discr_list (a))))
9818 case 1:
9819 return DW_FORM_block1;
9820 case 2:
9821 return DW_FORM_block2;
9822 case 4:
9823 return DW_FORM_block4;
9824 default:
9825 gcc_unreachable ();
9828 default:
9829 gcc_unreachable ();
9833 /* Output the encoding of an attribute value. */
9835 static void
9836 output_value_format (dw_attr_node *a)
9838 enum dwarf_form form = value_format (a);
9840 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
9843 /* Given a die and id, produce the appropriate abbreviations. */
9845 static void
9846 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
9848 unsigned ix;
9849 dw_attr_node *a_attr;
9851 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
9852 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
9853 dwarf_tag_name (abbrev->die_tag));
9855 if (abbrev->die_child != NULL)
9856 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
9857 else
9858 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
9860 for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
9862 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
9863 dwarf_attr_name (a_attr->dw_attr));
9864 output_value_format (a_attr);
9865 if (value_format (a_attr) == DW_FORM_implicit_const)
9867 if (AT_class (a_attr) == dw_val_class_file_implicit)
9869 int f = maybe_emit_file (a_attr->dw_attr_val.v.val_file);
9870 const char *filename = a_attr->dw_attr_val.v.val_file->filename;
9871 dw2_asm_output_data_sleb128 (f, "(%s)", filename);
9873 else
9874 dw2_asm_output_data_sleb128 (a_attr->dw_attr_val.v.val_int, NULL);
9878 dw2_asm_output_data (1, 0, NULL);
9879 dw2_asm_output_data (1, 0, NULL);
9883 /* Output the .debug_abbrev section which defines the DIE abbreviation
9884 table. */
9886 static void
9887 output_abbrev_section (void)
9889 unsigned int abbrev_id;
9890 dw_die_ref abbrev;
9892 FOR_EACH_VEC_SAFE_ELT (abbrev_die_table, abbrev_id, abbrev)
9893 if (abbrev_id != 0)
9894 output_die_abbrevs (abbrev_id, abbrev);
9896 /* Terminate the table. */
9897 dw2_asm_output_data (1, 0, NULL);
9900 /* Return a new location list, given the begin and end range, and the
9901 expression. */
9903 static inline dw_loc_list_ref
9904 new_loc_list (dw_loc_descr_ref expr, const char *begin, var_loc_view vbegin,
9905 const char *end, var_loc_view vend,
9906 const char *section)
9908 dw_loc_list_ref retlist = ggc_cleared_alloc<dw_loc_list_node> ();
9910 retlist->begin = begin;
9911 retlist->begin_entry = NULL;
9912 retlist->end = end;
9913 retlist->expr = expr;
9914 retlist->section = section;
9915 retlist->vbegin = vbegin;
9916 retlist->vend = vend;
9918 return retlist;
9921 /* Return true iff there's any nonzero view number in the loc list. */
9923 static bool
9924 loc_list_has_views (dw_loc_list_ref list)
9926 if (!debug_variable_location_views)
9927 return false;
9929 for (dw_loc_list_ref loc = list;
9930 loc != NULL; loc = loc->dw_loc_next)
9931 if (!ZERO_VIEW_P (loc->vbegin) || !ZERO_VIEW_P (loc->vend))
9932 return true;
9934 return false;
9937 /* Generate a new internal symbol for this location list node, if it
9938 hasn't got one yet. */
9940 static inline void
9941 gen_llsym (dw_loc_list_ref list)
9943 gcc_assert (!list->ll_symbol);
9944 list->ll_symbol = gen_internal_sym ("LLST");
9946 if (!loc_list_has_views (list))
9947 return;
9949 if (dwarf2out_locviews_in_attribute ())
9951 /* Use the same label_num for the view list. */
9952 label_num--;
9953 list->vl_symbol = gen_internal_sym ("LVUS");
9955 else
9956 list->vl_symbol = list->ll_symbol;
9959 /* Generate a symbol for the list, but only if we really want to emit
9960 it as a list. */
9962 static inline void
9963 maybe_gen_llsym (dw_loc_list_ref list)
9965 if (!list || (!list->dw_loc_next && !loc_list_has_views (list)))
9966 return;
9968 gen_llsym (list);
9971 /* Determine whether or not to skip loc_list entry CURR. If we're not
9972 to skip it, and SIZEP is non-null, store the size of CURR->expr's
9973 representation in *SIZEP. */
9975 static bool
9976 skip_loc_list_entry (dw_loc_list_ref curr, unsigned long *sizep = 0)
9978 /* Don't output an entry that starts and ends at the same address. */
9979 if (strcmp (curr->begin, curr->end) == 0
9980 && curr->vbegin == curr->vend && !curr->force)
9981 return true;
9983 unsigned long size = size_of_locs (curr->expr);
9985 /* If the expression is too large, drop it on the floor. We could
9986 perhaps put it into DW_TAG_dwarf_procedure and refer to that
9987 in the expression, but >= 64KB expressions for a single value
9988 in a single range are unlikely very useful. */
9989 if (dwarf_version < 5 && size > 0xffff)
9990 return true;
9992 if (sizep)
9993 *sizep = size;
9995 return false;
9998 /* Output a view pair loclist entry for CURR, if it requires one. */
10000 static void
10001 dwarf2out_maybe_output_loclist_view_pair (dw_loc_list_ref curr)
10003 if (!dwarf2out_locviews_in_loclist ())
10004 return;
10006 if (ZERO_VIEW_P (curr->vbegin) && ZERO_VIEW_P (curr->vend))
10007 return;
10009 #ifdef DW_LLE_view_pair
10010 dw2_asm_output_data (1, DW_LLE_view_pair, "DW_LLE_view_pair");
10012 if (dwarf2out_as_locview_support)
10014 if (ZERO_VIEW_P (curr->vbegin))
10015 dw2_asm_output_data_uleb128 (0, "Location view begin");
10016 else
10018 char label[MAX_ARTIFICIAL_LABEL_BYTES];
10019 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", curr->vbegin);
10020 dw2_asm_output_symname_uleb128 (label, "Location view begin");
10023 if (ZERO_VIEW_P (curr->vend))
10024 dw2_asm_output_data_uleb128 (0, "Location view end");
10025 else
10027 char label[MAX_ARTIFICIAL_LABEL_BYTES];
10028 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", curr->vend);
10029 dw2_asm_output_symname_uleb128 (label, "Location view end");
10032 else
10034 dw2_asm_output_data_uleb128 (curr->vbegin, "Location view begin");
10035 dw2_asm_output_data_uleb128 (curr->vend, "Location view end");
10037 #endif /* DW_LLE_view_pair */
10039 return;
10042 /* Output the location list given to us. */
10044 static void
10045 output_loc_list (dw_loc_list_ref list_head)
10047 int vcount = 0, lcount = 0;
10049 if (list_head->emitted)
10050 return;
10051 list_head->emitted = true;
10053 if (list_head->vl_symbol && dwarf2out_locviews_in_attribute ())
10055 ASM_OUTPUT_LABEL (asm_out_file, list_head->vl_symbol);
10057 for (dw_loc_list_ref curr = list_head; curr != NULL;
10058 curr = curr->dw_loc_next)
10060 if (skip_loc_list_entry (curr))
10061 continue;
10063 vcount++;
10065 /* ?? dwarf_split_debug_info? */
10066 if (dwarf2out_as_locview_support)
10068 char label[MAX_ARTIFICIAL_LABEL_BYTES];
10070 if (!ZERO_VIEW_P (curr->vbegin))
10072 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", curr->vbegin);
10073 dw2_asm_output_symname_uleb128 (label,
10074 "View list begin (%s)",
10075 list_head->vl_symbol);
10077 else
10078 dw2_asm_output_data_uleb128 (0,
10079 "View list begin (%s)",
10080 list_head->vl_symbol);
10082 if (!ZERO_VIEW_P (curr->vend))
10084 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", curr->vend);
10085 dw2_asm_output_symname_uleb128 (label,
10086 "View list end (%s)",
10087 list_head->vl_symbol);
10089 else
10090 dw2_asm_output_data_uleb128 (0,
10091 "View list end (%s)",
10092 list_head->vl_symbol);
10094 else
10096 dw2_asm_output_data_uleb128 (curr->vbegin,
10097 "View list begin (%s)",
10098 list_head->vl_symbol);
10099 dw2_asm_output_data_uleb128 (curr->vend,
10100 "View list end (%s)",
10101 list_head->vl_symbol);
10106 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
10108 const char *last_section = NULL;
10109 const char *base_label = NULL;
10111 /* Walk the location list, and output each range + expression. */
10112 for (dw_loc_list_ref curr = list_head; curr != NULL;
10113 curr = curr->dw_loc_next)
10115 unsigned long size;
10117 /* Skip this entry? If we skip it here, we must skip it in the
10118 view list above as well. */
10119 if (skip_loc_list_entry (curr, &size))
10120 continue;
10122 lcount++;
10124 if (dwarf_version >= 5)
10126 if (dwarf_split_debug_info)
10128 dwarf2out_maybe_output_loclist_view_pair (curr);
10129 /* For -gsplit-dwarf, emit DW_LLE_starx_length, which has
10130 uleb128 index into .debug_addr and uleb128 length. */
10131 dw2_asm_output_data (1, DW_LLE_startx_length,
10132 "DW_LLE_startx_length (%s)",
10133 list_head->ll_symbol);
10134 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
10135 "Location list range start index "
10136 "(%s)", curr->begin);
10137 /* FIXME: This will ICE ifndef HAVE_AS_LEB128.
10138 For that case we probably need to emit DW_LLE_startx_endx,
10139 but we'd need 2 .debug_addr entries rather than just one. */
10140 dw2_asm_output_delta_uleb128 (curr->end, curr->begin,
10141 "Location list length (%s)",
10142 list_head->ll_symbol);
10144 else if (!have_multiple_function_sections && HAVE_AS_LEB128)
10146 dwarf2out_maybe_output_loclist_view_pair (curr);
10147 /* If all code is in .text section, the base address is
10148 already provided by the CU attributes. Use
10149 DW_LLE_offset_pair where both addresses are uleb128 encoded
10150 offsets against that base. */
10151 dw2_asm_output_data (1, DW_LLE_offset_pair,
10152 "DW_LLE_offset_pair (%s)",
10153 list_head->ll_symbol);
10154 dw2_asm_output_delta_uleb128 (curr->begin, curr->section,
10155 "Location list begin address (%s)",
10156 list_head->ll_symbol);
10157 dw2_asm_output_delta_uleb128 (curr->end, curr->section,
10158 "Location list end address (%s)",
10159 list_head->ll_symbol);
10161 else if (HAVE_AS_LEB128)
10163 /* Otherwise, find out how many consecutive entries could share
10164 the same base entry. If just one, emit DW_LLE_start_length,
10165 otherwise emit DW_LLE_base_address for the base address
10166 followed by a series of DW_LLE_offset_pair. */
10167 if (last_section == NULL || curr->section != last_section)
10169 dw_loc_list_ref curr2;
10170 for (curr2 = curr->dw_loc_next; curr2 != NULL;
10171 curr2 = curr2->dw_loc_next)
10173 if (strcmp (curr2->begin, curr2->end) == 0
10174 && !curr2->force)
10175 continue;
10176 break;
10178 if (curr2 == NULL || curr->section != curr2->section)
10179 last_section = NULL;
10180 else
10182 last_section = curr->section;
10183 base_label = curr->begin;
10184 dw2_asm_output_data (1, DW_LLE_base_address,
10185 "DW_LLE_base_address (%s)",
10186 list_head->ll_symbol);
10187 dw2_asm_output_addr (DWARF2_ADDR_SIZE, base_label,
10188 "Base address (%s)",
10189 list_head->ll_symbol);
10192 /* Only one entry with the same base address. Use
10193 DW_LLE_start_length with absolute address and uleb128
10194 length. */
10195 if (last_section == NULL)
10197 dwarf2out_maybe_output_loclist_view_pair (curr);
10198 dw2_asm_output_data (1, DW_LLE_start_length,
10199 "DW_LLE_start_length (%s)",
10200 list_head->ll_symbol);
10201 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
10202 "Location list begin address (%s)",
10203 list_head->ll_symbol);
10204 dw2_asm_output_delta_uleb128 (curr->end, curr->begin,
10205 "Location list length "
10206 "(%s)", list_head->ll_symbol);
10208 /* Otherwise emit DW_LLE_offset_pair, relative to above emitted
10209 DW_LLE_base_address. */
10210 else
10212 dwarf2out_maybe_output_loclist_view_pair (curr);
10213 dw2_asm_output_data (1, DW_LLE_offset_pair,
10214 "DW_LLE_offset_pair (%s)",
10215 list_head->ll_symbol);
10216 dw2_asm_output_delta_uleb128 (curr->begin, base_label,
10217 "Location list begin address "
10218 "(%s)", list_head->ll_symbol);
10219 dw2_asm_output_delta_uleb128 (curr->end, base_label,
10220 "Location list end address "
10221 "(%s)", list_head->ll_symbol);
10224 /* The assembler does not support .uleb128 directive. Emit
10225 DW_LLE_start_end with a pair of absolute addresses. */
10226 else
10228 dwarf2out_maybe_output_loclist_view_pair (curr);
10229 dw2_asm_output_data (1, DW_LLE_start_end,
10230 "DW_LLE_start_end (%s)",
10231 list_head->ll_symbol);
10232 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
10233 "Location list begin address (%s)",
10234 list_head->ll_symbol);
10235 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
10236 "Location list end address (%s)",
10237 list_head->ll_symbol);
10240 else if (dwarf_split_debug_info)
10242 /* For -gsplit-dwarf -gdwarf-{2,3,4} emit index into .debug_addr
10243 and 4 byte length. */
10244 dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
10245 "Location list start/length entry (%s)",
10246 list_head->ll_symbol);
10247 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
10248 "Location list range start index (%s)",
10249 curr->begin);
10250 /* The length field is 4 bytes. If we ever need to support
10251 an 8-byte length, we can add a new DW_LLE code or fall back
10252 to DW_LLE_GNU_start_end_entry. */
10253 dw2_asm_output_delta (4, curr->end, curr->begin,
10254 "Location list range length (%s)",
10255 list_head->ll_symbol);
10257 else if (!have_multiple_function_sections)
10259 /* Pair of relative addresses against start of text section. */
10260 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
10261 "Location list begin address (%s)",
10262 list_head->ll_symbol);
10263 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
10264 "Location list end address (%s)",
10265 list_head->ll_symbol);
10267 else
10269 /* Pair of absolute addresses. */
10270 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
10271 "Location list begin address (%s)",
10272 list_head->ll_symbol);
10273 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
10274 "Location list end address (%s)",
10275 list_head->ll_symbol);
10278 /* Output the block length for this list of location operations. */
10279 if (dwarf_version >= 5)
10280 dw2_asm_output_data_uleb128 (size, "Location expression size");
10281 else
10283 gcc_assert (size <= 0xffff);
10284 dw2_asm_output_data (2, size, "Location expression size");
10287 output_loc_sequence (curr->expr, -1);
10290 /* And finally list termination. */
10291 if (dwarf_version >= 5)
10292 dw2_asm_output_data (1, DW_LLE_end_of_list,
10293 "DW_LLE_end_of_list (%s)", list_head->ll_symbol);
10294 else if (dwarf_split_debug_info)
10295 dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
10296 "Location list terminator (%s)",
10297 list_head->ll_symbol);
10298 else
10300 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
10301 "Location list terminator begin (%s)",
10302 list_head->ll_symbol);
10303 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
10304 "Location list terminator end (%s)",
10305 list_head->ll_symbol);
10308 gcc_assert (!list_head->vl_symbol
10309 || vcount == lcount * (dwarf2out_locviews_in_attribute () ? 1 : 0));
10312 /* Output a range_list offset into the .debug_ranges or .debug_rnglists
10313 section. Emit a relocated reference if val_entry is NULL, otherwise,
10314 emit an indirect reference. */
10316 static void
10317 output_range_list_offset (dw_attr_node *a)
10319 const char *name = dwarf_attr_name (a->dw_attr);
10321 if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
10323 if (dwarf_version >= 5)
10325 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
10326 dw2_asm_output_offset (DWARF_OFFSET_SIZE, r->label,
10327 debug_ranges_section, "%s", name);
10329 else
10331 char *p = strchr (ranges_section_label, '\0');
10332 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
10333 a->dw_attr_val.v.val_offset * 2 * DWARF2_ADDR_SIZE);
10334 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
10335 debug_ranges_section, "%s", name);
10336 *p = '\0';
10339 else if (dwarf_version >= 5)
10341 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
10342 gcc_assert (rnglist_idx);
10343 dw2_asm_output_data_uleb128 (r->idx, "%s", name);
10345 else
10346 dw2_asm_output_data (DWARF_OFFSET_SIZE,
10347 a->dw_attr_val.v.val_offset * 2 * DWARF2_ADDR_SIZE,
10348 "%s (offset from %s)", name, ranges_section_label);
10351 /* Output the offset into the debug_loc section. */
10353 static void
10354 output_loc_list_offset (dw_attr_node *a)
10356 char *sym = AT_loc_list (a)->ll_symbol;
10358 gcc_assert (sym);
10359 if (!dwarf_split_debug_info)
10360 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
10361 "%s", dwarf_attr_name (a->dw_attr));
10362 else if (dwarf_version >= 5)
10364 gcc_assert (AT_loc_list (a)->num_assigned);
10365 dw2_asm_output_data_uleb128 (AT_loc_list (a)->hash, "%s (%s)",
10366 dwarf_attr_name (a->dw_attr),
10367 sym);
10369 else
10370 dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
10371 "%s", dwarf_attr_name (a->dw_attr));
10374 /* Output the offset into the debug_loc section. */
10376 static void
10377 output_view_list_offset (dw_attr_node *a)
10379 char *sym = (*AT_loc_list_ptr (a))->vl_symbol;
10381 gcc_assert (sym);
10382 if (dwarf_split_debug_info)
10383 dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
10384 "%s", dwarf_attr_name (a->dw_attr));
10385 else
10386 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
10387 "%s", dwarf_attr_name (a->dw_attr));
10390 /* Output an attribute's index or value appropriately. */
10392 static void
10393 output_attr_index_or_value (dw_attr_node *a)
10395 const char *name = dwarf_attr_name (a->dw_attr);
10397 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
10399 dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
10400 return;
10402 switch (AT_class (a))
10404 case dw_val_class_addr:
10405 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
10406 break;
10407 case dw_val_class_high_pc:
10408 case dw_val_class_lbl_id:
10409 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
10410 break;
10411 default:
10412 gcc_unreachable ();
10416 /* Output a type signature. */
10418 static inline void
10419 output_signature (const char *sig, const char *name)
10421 int i;
10423 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
10424 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
10427 /* Output a discriminant value. */
10429 static inline void
10430 output_discr_value (dw_discr_value *discr_value, const char *name)
10432 if (discr_value->pos)
10433 dw2_asm_output_data_uleb128 (discr_value->v.uval, "%s", name);
10434 else
10435 dw2_asm_output_data_sleb128 (discr_value->v.sval, "%s", name);
10438 /* Output the DIE and its attributes. Called recursively to generate
10439 the definitions of each child DIE. */
10441 static void
10442 output_die (dw_die_ref die)
10444 dw_attr_node *a;
10445 dw_die_ref c;
10446 unsigned long size;
10447 unsigned ix;
10449 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
10450 (unsigned long)die->die_offset,
10451 dwarf_tag_name (die->die_tag));
10453 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
10455 const char *name = dwarf_attr_name (a->dw_attr);
10457 switch (AT_class (a))
10459 case dw_val_class_addr:
10460 output_attr_index_or_value (a);
10461 break;
10463 case dw_val_class_offset:
10464 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
10465 "%s", name);
10466 break;
10468 case dw_val_class_range_list:
10469 output_range_list_offset (a);
10470 break;
10472 case dw_val_class_loc:
10473 size = size_of_locs (AT_loc (a));
10475 /* Output the block length for this list of location operations. */
10476 if (dwarf_version >= 4)
10477 dw2_asm_output_data_uleb128 (size, "%s", name);
10478 else
10479 dw2_asm_output_data (constant_size (size), size, "%s", name);
10481 output_loc_sequence (AT_loc (a), -1);
10482 break;
10484 case dw_val_class_const:
10485 /* ??? It would be slightly more efficient to use a scheme like is
10486 used for unsigned constants below, but gdb 4.x does not sign
10487 extend. Gdb 5.x does sign extend. */
10488 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
10489 break;
10491 case dw_val_class_unsigned_const:
10493 int csize = constant_size (AT_unsigned (a));
10494 if (dwarf_version == 3
10495 && a->dw_attr == DW_AT_data_member_location
10496 && csize >= 4)
10497 dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
10498 else
10499 dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
10501 break;
10503 case dw_val_class_const_implicit:
10504 if (flag_debug_asm)
10505 fprintf (asm_out_file, "\t\t\t%s %s ("
10506 HOST_WIDE_INT_PRINT_DEC ")\n",
10507 ASM_COMMENT_START, name, AT_int (a));
10508 break;
10510 case dw_val_class_unsigned_const_implicit:
10511 if (flag_debug_asm)
10512 fprintf (asm_out_file, "\t\t\t%s %s ("
10513 HOST_WIDE_INT_PRINT_HEX ")\n",
10514 ASM_COMMENT_START, name, AT_unsigned (a));
10515 break;
10517 case dw_val_class_const_double:
10519 unsigned HOST_WIDE_INT first, second;
10521 if (HOST_BITS_PER_WIDE_INT >= DWARF_LARGEST_DATA_FORM_BITS)
10522 dw2_asm_output_data (1,
10523 HOST_BITS_PER_DOUBLE_INT
10524 / HOST_BITS_PER_CHAR,
10525 NULL);
10527 if (WORDS_BIG_ENDIAN)
10529 first = a->dw_attr_val.v.val_double.high;
10530 second = a->dw_attr_val.v.val_double.low;
10532 else
10534 first = a->dw_attr_val.v.val_double.low;
10535 second = a->dw_attr_val.v.val_double.high;
10538 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
10539 first, "%s", name);
10540 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
10541 second, NULL);
10543 break;
10545 case dw_val_class_wide_int:
10547 int i;
10548 int len = get_full_len (*a->dw_attr_val.v.val_wide);
10549 int l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
10550 if (len * HOST_BITS_PER_WIDE_INT > DWARF_LARGEST_DATA_FORM_BITS)
10551 dw2_asm_output_data (1, get_full_len (*a->dw_attr_val.v.val_wide)
10552 * l, NULL);
10554 if (WORDS_BIG_ENDIAN)
10555 for (i = len - 1; i >= 0; --i)
10557 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
10558 "%s", name);
10559 name = "";
10561 else
10562 for (i = 0; i < len; ++i)
10564 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
10565 "%s", name);
10566 name = "";
10569 break;
10571 case dw_val_class_vec:
10573 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
10574 unsigned int len = a->dw_attr_val.v.val_vec.length;
10575 unsigned int i;
10576 unsigned char *p;
10578 dw2_asm_output_data (constant_size (len * elt_size),
10579 len * elt_size, "%s", name);
10580 if (elt_size > sizeof (HOST_WIDE_INT))
10582 elt_size /= 2;
10583 len *= 2;
10585 for (i = 0, p = (unsigned char *) a->dw_attr_val.v.val_vec.array;
10586 i < len;
10587 i++, p += elt_size)
10588 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
10589 "fp or vector constant word %u", i);
10590 break;
10593 case dw_val_class_flag:
10594 if (dwarf_version >= 4)
10596 /* Currently all add_AT_flag calls pass in 1 as last argument,
10597 so DW_FORM_flag_present can be used. If that ever changes,
10598 we'll need to use DW_FORM_flag and have some optimization
10599 in build_abbrev_table that will change those to
10600 DW_FORM_flag_present if it is set to 1 in all DIEs using
10601 the same abbrev entry. */
10602 gcc_assert (AT_flag (a) == 1);
10603 if (flag_debug_asm)
10604 fprintf (asm_out_file, "\t\t\t%s %s\n",
10605 ASM_COMMENT_START, name);
10606 break;
10608 dw2_asm_output_data (1, AT_flag (a), "%s", name);
10609 break;
10611 case dw_val_class_loc_list:
10612 output_loc_list_offset (a);
10613 break;
10615 case dw_val_class_view_list:
10616 output_view_list_offset (a);
10617 break;
10619 case dw_val_class_die_ref:
10620 if (AT_ref_external (a))
10622 if (AT_ref (a)->comdat_type_p)
10624 comdat_type_node *type_node
10625 = AT_ref (a)->die_id.die_type_node;
10627 gcc_assert (type_node);
10628 output_signature (type_node->signature, name);
10630 else
10632 const char *sym = AT_ref (a)->die_id.die_symbol;
10633 int size;
10635 gcc_assert (sym);
10636 /* In DWARF2, DW_FORM_ref_addr is sized by target address
10637 length, whereas in DWARF3 it's always sized as an
10638 offset. */
10639 if (dwarf_version == 2)
10640 size = DWARF2_ADDR_SIZE;
10641 else
10642 size = DWARF_OFFSET_SIZE;
10643 /* ??? We cannot unconditionally output die_offset if
10644 non-zero - others might create references to those
10645 DIEs via symbols.
10646 And we do not clear its DIE offset after outputting it
10647 (and the label refers to the actual DIEs, not the
10648 DWARF CU unit header which is when using label + offset
10649 would be the correct thing to do).
10650 ??? This is the reason for the with_offset flag. */
10651 if (AT_ref (a)->with_offset)
10652 dw2_asm_output_offset (size, sym, AT_ref (a)->die_offset,
10653 debug_info_section, "%s", name);
10654 else
10655 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
10656 name);
10659 else
10661 gcc_assert (AT_ref (a)->die_offset);
10662 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
10663 "%s", name);
10665 break;
10667 case dw_val_class_fde_ref:
10669 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
10671 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
10672 a->dw_attr_val.v.val_fde_index * 2);
10673 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
10674 "%s", name);
10676 break;
10678 case dw_val_class_vms_delta:
10679 #ifdef ASM_OUTPUT_DWARF_VMS_DELTA
10680 dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
10681 AT_vms_delta2 (a), AT_vms_delta1 (a),
10682 "%s", name);
10683 #else
10684 dw2_asm_output_delta (DWARF_OFFSET_SIZE,
10685 AT_vms_delta2 (a), AT_vms_delta1 (a),
10686 "%s", name);
10687 #endif
10688 break;
10690 case dw_val_class_lbl_id:
10691 output_attr_index_or_value (a);
10692 break;
10694 case dw_val_class_lineptr:
10695 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
10696 debug_line_section, "%s", name);
10697 break;
10699 case dw_val_class_macptr:
10700 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
10701 debug_macinfo_section, "%s", name);
10702 break;
10704 case dw_val_class_loclistsptr:
10705 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
10706 debug_loc_section, "%s", name);
10707 break;
10709 case dw_val_class_str:
10710 if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
10711 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
10712 a->dw_attr_val.v.val_str->label,
10713 debug_str_section,
10714 "%s: \"%s\"", name, AT_string (a));
10715 else if (a->dw_attr_val.v.val_str->form == DW_FORM_line_strp)
10716 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
10717 a->dw_attr_val.v.val_str->label,
10718 debug_line_str_section,
10719 "%s: \"%s\"", name, AT_string (a));
10720 else if (a->dw_attr_val.v.val_str->form == DW_FORM_GNU_str_index)
10721 dw2_asm_output_data_uleb128 (AT_index (a),
10722 "%s: \"%s\"", name, AT_string (a));
10723 else
10724 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
10725 break;
10727 case dw_val_class_file:
10729 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
10731 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
10732 a->dw_attr_val.v.val_file->filename);
10733 break;
10736 case dw_val_class_file_implicit:
10737 if (flag_debug_asm)
10738 fprintf (asm_out_file, "\t\t\t%s %s (%d, %s)\n",
10739 ASM_COMMENT_START, name,
10740 maybe_emit_file (a->dw_attr_val.v.val_file),
10741 a->dw_attr_val.v.val_file->filename);
10742 break;
10744 case dw_val_class_data8:
10746 int i;
10748 for (i = 0; i < 8; i++)
10749 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
10750 i == 0 ? "%s" : NULL, name);
10751 break;
10754 case dw_val_class_high_pc:
10755 dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
10756 get_AT_low_pc (die), "DW_AT_high_pc");
10757 break;
10759 case dw_val_class_discr_value:
10760 output_discr_value (&a->dw_attr_val.v.val_discr_value, name);
10761 break;
10763 case dw_val_class_discr_list:
10765 dw_discr_list_ref list = AT_discr_list (a);
10766 const int size = size_of_discr_list (list);
10768 /* This is a block, so output its length first. */
10769 dw2_asm_output_data (constant_size (size), size,
10770 "%s: block size", name);
10772 for (; list != NULL; list = list->dw_discr_next)
10774 /* One byte for the discriminant value descriptor, and then as
10775 many LEB128 numbers as required. */
10776 if (list->dw_discr_range)
10777 dw2_asm_output_data (1, DW_DSC_range,
10778 "%s: DW_DSC_range", name);
10779 else
10780 dw2_asm_output_data (1, DW_DSC_label,
10781 "%s: DW_DSC_label", name);
10783 output_discr_value (&list->dw_discr_lower_bound, name);
10784 if (list->dw_discr_range)
10785 output_discr_value (&list->dw_discr_upper_bound, name);
10787 break;
10790 default:
10791 gcc_unreachable ();
10795 FOR_EACH_CHILD (die, c, output_die (c));
10797 /* Add null byte to terminate sibling list. */
10798 if (die->die_child != NULL)
10799 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
10800 (unsigned long) die->die_offset);
10803 /* Output the dwarf version number. */
10805 static void
10806 output_dwarf_version ()
10808 /* ??? For now, if -gdwarf-6 is specified, we output version 5 with
10809 views in loclist. That will change eventually. */
10810 if (dwarf_version == 6)
10812 static bool once;
10813 if (!once)
10815 warning (0,
10816 "-gdwarf-6 is output as version 5 with incompatibilities");
10817 once = true;
10819 dw2_asm_output_data (2, 5, "DWARF version number");
10821 else
10822 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
10825 /* Output the compilation unit that appears at the beginning of the
10826 .debug_info section, and precedes the DIE descriptions. */
10828 static void
10829 output_compilation_unit_header (enum dwarf_unit_type ut)
10831 if (!XCOFF_DEBUGGING_INFO)
10833 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10834 dw2_asm_output_data (4, 0xffffffff,
10835 "Initial length escape value indicating 64-bit DWARF extension");
10836 dw2_asm_output_data (DWARF_OFFSET_SIZE,
10837 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
10838 "Length of Compilation Unit Info");
10841 output_dwarf_version ();
10842 if (dwarf_version >= 5)
10844 const char *name;
10845 switch (ut)
10847 case DW_UT_compile: name = "DW_UT_compile"; break;
10848 case DW_UT_type: name = "DW_UT_type"; break;
10849 case DW_UT_split_compile: name = "DW_UT_split_compile"; break;
10850 case DW_UT_split_type: name = "DW_UT_split_type"; break;
10851 default: gcc_unreachable ();
10853 dw2_asm_output_data (1, ut, "%s", name);
10854 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10856 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
10857 debug_abbrev_section,
10858 "Offset Into Abbrev. Section");
10859 if (dwarf_version < 5)
10860 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10863 /* Output the compilation unit DIE and its children. */
10865 static void
10866 output_comp_unit (dw_die_ref die, int output_if_empty,
10867 const unsigned char *dwo_id)
10869 const char *secname, *oldsym;
10870 char *tmp;
10872 /* Unless we are outputting main CU, we may throw away empty ones. */
10873 if (!output_if_empty && die->die_child == NULL)
10874 return;
10876 /* Even if there are no children of this DIE, we must output the information
10877 about the compilation unit. Otherwise, on an empty translation unit, we
10878 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
10879 will then complain when examining the file. First mark all the DIEs in
10880 this CU so we know which get local refs. */
10881 mark_dies (die);
10883 external_ref_hash_type *extern_map = optimize_external_refs (die);
10885 /* For now, optimize only the main CU, in order to optimize the rest
10886 we'd need to see all of them earlier. Leave the rest for post-linking
10887 tools like DWZ. */
10888 if (die == comp_unit_die ())
10889 abbrev_opt_start = vec_safe_length (abbrev_die_table);
10891 build_abbrev_table (die, extern_map);
10893 optimize_abbrev_table ();
10895 delete extern_map;
10897 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
10898 next_die_offset = (dwo_id
10899 ? DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
10900 : DWARF_COMPILE_UNIT_HEADER_SIZE);
10901 calc_die_sizes (die);
10903 oldsym = die->die_id.die_symbol;
10904 if (oldsym && die->comdat_type_p)
10906 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
10908 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
10909 secname = tmp;
10910 die->die_id.die_symbol = NULL;
10911 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
10913 else
10915 switch_to_section (debug_info_section);
10916 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
10917 info_section_emitted = true;
10920 /* For LTO cross unit DIE refs we want a symbol on the start of the
10921 debuginfo section, not on the CU DIE. */
10922 if ((flag_generate_lto || flag_generate_offload) && oldsym)
10924 /* ??? No way to get visibility assembled without a decl. */
10925 tree decl = build_decl (UNKNOWN_LOCATION, VAR_DECL,
10926 get_identifier (oldsym), char_type_node);
10927 TREE_PUBLIC (decl) = true;
10928 TREE_STATIC (decl) = true;
10929 DECL_ARTIFICIAL (decl) = true;
10930 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
10931 DECL_VISIBILITY_SPECIFIED (decl) = true;
10932 targetm.asm_out.assemble_visibility (decl, VISIBILITY_HIDDEN);
10933 #ifdef ASM_WEAKEN_LABEL
10934 /* We prefer a .weak because that handles duplicates from duplicate
10935 archive members in a graceful way. */
10936 ASM_WEAKEN_LABEL (asm_out_file, oldsym);
10937 #else
10938 targetm.asm_out.globalize_label (asm_out_file, oldsym);
10939 #endif
10940 ASM_OUTPUT_LABEL (asm_out_file, oldsym);
10943 /* Output debugging information. */
10944 output_compilation_unit_header (dwo_id
10945 ? DW_UT_split_compile : DW_UT_compile);
10946 if (dwarf_version >= 5)
10948 if (dwo_id != NULL)
10949 for (int i = 0; i < 8; i++)
10950 dw2_asm_output_data (1, dwo_id[i], i == 0 ? "DWO id" : NULL);
10952 output_die (die);
10954 /* Leave the marks on the main CU, so we can check them in
10955 output_pubnames. */
10956 if (oldsym)
10958 unmark_dies (die);
10959 die->die_id.die_symbol = oldsym;
10963 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
10964 and .debug_pubtypes. This is configured per-target, but can be
10965 overridden by the -gpubnames or -gno-pubnames options. */
10967 static inline bool
10968 want_pubnames (void)
10970 if (debug_info_level <= DINFO_LEVEL_TERSE)
10971 return false;
10972 if (debug_generate_pub_sections != -1)
10973 return debug_generate_pub_sections;
10974 return targetm.want_debug_pub_sections;
10977 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes. */
10979 static void
10980 add_AT_pubnames (dw_die_ref die)
10982 if (want_pubnames ())
10983 add_AT_flag (die, DW_AT_GNU_pubnames, 1);
10986 /* Add a string attribute value to a skeleton DIE. */
10988 static inline void
10989 add_skeleton_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
10990 const char *str)
10992 dw_attr_node attr;
10993 struct indirect_string_node *node;
10995 if (! skeleton_debug_str_hash)
10996 skeleton_debug_str_hash
10997 = hash_table<indirect_string_hasher>::create_ggc (10);
10999 node = find_AT_string_in_table (str, skeleton_debug_str_hash);
11000 find_string_form (node);
11001 if (node->form == DW_FORM_GNU_str_index)
11002 node->form = DW_FORM_strp;
11004 attr.dw_attr = attr_kind;
11005 attr.dw_attr_val.val_class = dw_val_class_str;
11006 attr.dw_attr_val.val_entry = NULL;
11007 attr.dw_attr_val.v.val_str = node;
11008 add_dwarf_attr (die, &attr);
11011 /* Helper function to generate top-level dies for skeleton debug_info and
11012 debug_types. */
11014 static void
11015 add_top_level_skeleton_die_attrs (dw_die_ref die)
11017 const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
11018 const char *comp_dir = comp_dir_string ();
11020 add_skeleton_AT_string (die, dwarf_AT (DW_AT_dwo_name), dwo_file_name);
11021 if (comp_dir != NULL)
11022 add_skeleton_AT_string (die, DW_AT_comp_dir, comp_dir);
11023 add_AT_pubnames (die);
11024 add_AT_lineptr (die, DW_AT_GNU_addr_base, debug_addr_section_label);
11027 /* Output skeleton debug sections that point to the dwo file. */
11029 static void
11030 output_skeleton_debug_sections (dw_die_ref comp_unit,
11031 const unsigned char *dwo_id)
11033 /* These attributes will be found in the full debug_info section. */
11034 remove_AT (comp_unit, DW_AT_producer);
11035 remove_AT (comp_unit, DW_AT_language);
11037 switch_to_section (debug_skeleton_info_section);
11038 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
11040 /* Produce the skeleton compilation-unit header. This one differs enough from
11041 a normal CU header that it's better not to call output_compilation_unit
11042 header. */
11043 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11044 dw2_asm_output_data (4, 0xffffffff,
11045 "Initial length escape value indicating 64-bit "
11046 "DWARF extension");
11048 dw2_asm_output_data (DWARF_OFFSET_SIZE,
11049 DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
11050 - DWARF_INITIAL_LENGTH_SIZE
11051 + size_of_die (comp_unit),
11052 "Length of Compilation Unit Info");
11053 output_dwarf_version ();
11054 if (dwarf_version >= 5)
11056 dw2_asm_output_data (1, DW_UT_skeleton, "DW_UT_skeleton");
11057 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
11059 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_abbrev_section_label,
11060 debug_skeleton_abbrev_section,
11061 "Offset Into Abbrev. Section");
11062 if (dwarf_version < 5)
11063 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
11064 else
11065 for (int i = 0; i < 8; i++)
11066 dw2_asm_output_data (1, dwo_id[i], i == 0 ? "DWO id" : NULL);
11068 comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
11069 output_die (comp_unit);
11071 /* Build the skeleton debug_abbrev section. */
11072 switch_to_section (debug_skeleton_abbrev_section);
11073 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
11075 output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
11077 dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
11080 /* Output a comdat type unit DIE and its children. */
11082 static void
11083 output_comdat_type_unit (comdat_type_node *node)
11085 const char *secname;
11086 char *tmp;
11087 int i;
11088 #if defined (OBJECT_FORMAT_ELF)
11089 tree comdat_key;
11090 #endif
11092 /* First mark all the DIEs in this CU so we know which get local refs. */
11093 mark_dies (node->root_die);
11095 external_ref_hash_type *extern_map = optimize_external_refs (node->root_die);
11097 build_abbrev_table (node->root_die, extern_map);
11099 delete extern_map;
11100 extern_map = NULL;
11102 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
11103 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
11104 calc_die_sizes (node->root_die);
11106 #if defined (OBJECT_FORMAT_ELF)
11107 if (dwarf_version >= 5)
11109 if (!dwarf_split_debug_info)
11110 secname = ".debug_info";
11111 else
11112 secname = ".debug_info.dwo";
11114 else if (!dwarf_split_debug_info)
11115 secname = ".debug_types";
11116 else
11117 secname = ".debug_types.dwo";
11119 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
11120 sprintf (tmp, dwarf_version >= 5 ? "wi." : "wt.");
11121 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11122 sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
11123 comdat_key = get_identifier (tmp);
11124 targetm.asm_out.named_section (secname,
11125 SECTION_DEBUG | SECTION_LINKONCE,
11126 comdat_key);
11127 #else
11128 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
11129 sprintf (tmp, (dwarf_version >= 5
11130 ? ".gnu.linkonce.wi." : ".gnu.linkonce.wt."));
11131 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11132 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
11133 secname = tmp;
11134 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
11135 #endif
11137 /* Output debugging information. */
11138 output_compilation_unit_header (dwarf_split_debug_info
11139 ? DW_UT_split_type : DW_UT_type);
11140 output_signature (node->signature, "Type Signature");
11141 dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
11142 "Offset to Type DIE");
11143 output_die (node->root_die);
11145 unmark_dies (node->root_die);
11148 /* Return the DWARF2/3 pubname associated with a decl. */
11150 static const char *
11151 dwarf2_name (tree decl, int scope)
11153 if (DECL_NAMELESS (decl))
11154 return NULL;
11155 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
11158 /* Add a new entry to .debug_pubnames if appropriate. */
11160 static void
11161 add_pubname_string (const char *str, dw_die_ref die)
11163 pubname_entry e;
11165 e.die = die;
11166 e.name = xstrdup (str);
11167 vec_safe_push (pubname_table, e);
11170 static void
11171 add_pubname (tree decl, dw_die_ref die)
11173 if (!want_pubnames ())
11174 return;
11176 /* Don't add items to the table when we expect that the consumer will have
11177 just read the enclosing die. For example, if the consumer is looking at a
11178 class_member, it will either be inside the class already, or will have just
11179 looked up the class to find the member. Either way, searching the class is
11180 faster than searching the index. */
11181 if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
11182 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
11184 const char *name = dwarf2_name (decl, 1);
11186 if (name)
11187 add_pubname_string (name, die);
11191 /* Add an enumerator to the pubnames section. */
11193 static void
11194 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
11196 pubname_entry e;
11198 gcc_assert (scope_name);
11199 e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
11200 e.die = die;
11201 vec_safe_push (pubname_table, e);
11204 /* Add a new entry to .debug_pubtypes if appropriate. */
11206 static void
11207 add_pubtype (tree decl, dw_die_ref die)
11209 pubname_entry e;
11211 if (!want_pubnames ())
11212 return;
11214 if ((TREE_PUBLIC (decl)
11215 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
11216 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
11218 tree scope = NULL;
11219 const char *scope_name = "";
11220 const char *sep = is_cxx () ? "::" : ".";
11221 const char *name;
11223 scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
11224 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
11226 scope_name = lang_hooks.dwarf_name (scope, 1);
11227 if (scope_name != NULL && scope_name[0] != '\0')
11228 scope_name = concat (scope_name, sep, NULL);
11229 else
11230 scope_name = "";
11233 if (TYPE_P (decl))
11234 name = type_tag (decl);
11235 else
11236 name = lang_hooks.dwarf_name (decl, 1);
11238 /* If we don't have a name for the type, there's no point in adding
11239 it to the table. */
11240 if (name != NULL && name[0] != '\0')
11242 e.die = die;
11243 e.name = concat (scope_name, name, NULL);
11244 vec_safe_push (pubtype_table, e);
11247 /* Although it might be more consistent to add the pubinfo for the
11248 enumerators as their dies are created, they should only be added if the
11249 enum type meets the criteria above. So rather than re-check the parent
11250 enum type whenever an enumerator die is created, just output them all
11251 here. This isn't protected by the name conditional because anonymous
11252 enums don't have names. */
11253 if (die->die_tag == DW_TAG_enumeration_type)
11255 dw_die_ref c;
11257 FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
11262 /* Output a single entry in the pubnames table. */
11264 static void
11265 output_pubname (dw_offset die_offset, pubname_entry *entry)
11267 dw_die_ref die = entry->die;
11268 int is_static = get_AT_flag (die, DW_AT_external) ? 0 : 1;
11270 dw2_asm_output_data (DWARF_OFFSET_SIZE, die_offset, "DIE offset");
11272 if (debug_generate_pub_sections == 2)
11274 /* This logic follows gdb's method for determining the value of the flag
11275 byte. */
11276 uint32_t flags = GDB_INDEX_SYMBOL_KIND_NONE;
11277 switch (die->die_tag)
11279 case DW_TAG_typedef:
11280 case DW_TAG_base_type:
11281 case DW_TAG_subrange_type:
11282 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
11283 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
11284 break;
11285 case DW_TAG_enumerator:
11286 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
11287 GDB_INDEX_SYMBOL_KIND_VARIABLE);
11288 if (!is_cxx ())
11289 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
11290 break;
11291 case DW_TAG_subprogram:
11292 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
11293 GDB_INDEX_SYMBOL_KIND_FUNCTION);
11294 if (!is_ada ())
11295 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
11296 break;
11297 case DW_TAG_constant:
11298 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
11299 GDB_INDEX_SYMBOL_KIND_VARIABLE);
11300 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
11301 break;
11302 case DW_TAG_variable:
11303 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
11304 GDB_INDEX_SYMBOL_KIND_VARIABLE);
11305 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
11306 break;
11307 case DW_TAG_namespace:
11308 case DW_TAG_imported_declaration:
11309 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
11310 break;
11311 case DW_TAG_class_type:
11312 case DW_TAG_interface_type:
11313 case DW_TAG_structure_type:
11314 case DW_TAG_union_type:
11315 case DW_TAG_enumeration_type:
11316 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
11317 if (!is_cxx ())
11318 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
11319 break;
11320 default:
11321 /* An unusual tag. Leave the flag-byte empty. */
11322 break;
11324 dw2_asm_output_data (1, flags >> GDB_INDEX_CU_BITSIZE,
11325 "GDB-index flags");
11328 dw2_asm_output_nstring (entry->name, -1, "external name");
11332 /* Output the public names table used to speed up access to externally
11333 visible names; or the public types table used to find type definitions. */
11335 static void
11336 output_pubnames (vec<pubname_entry, va_gc> *names)
11338 unsigned i;
11339 unsigned long pubnames_length = size_of_pubnames (names);
11340 pubname_entry *pub;
11342 if (!XCOFF_DEBUGGING_INFO)
11344 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11345 dw2_asm_output_data (4, 0xffffffff,
11346 "Initial length escape value indicating 64-bit DWARF extension");
11347 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
11348 "Pub Info Length");
11351 /* Version number for pubnames/pubtypes is independent of dwarf version. */
11352 dw2_asm_output_data (2, 2, "DWARF pubnames/pubtypes version");
11354 if (dwarf_split_debug_info)
11355 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
11356 debug_skeleton_info_section,
11357 "Offset of Compilation Unit Info");
11358 else
11359 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
11360 debug_info_section,
11361 "Offset of Compilation Unit Info");
11362 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
11363 "Compilation Unit Length");
11365 FOR_EACH_VEC_ELT (*names, i, pub)
11367 if (include_pubname_in_output (names, pub))
11369 dw_offset die_offset = pub->die->die_offset;
11371 /* We shouldn't see pubnames for DIEs outside of the main CU. */
11372 if (names == pubname_table && pub->die->die_tag != DW_TAG_enumerator)
11373 gcc_assert (pub->die->die_mark);
11375 /* If we're putting types in their own .debug_types sections,
11376 the .debug_pubtypes table will still point to the compile
11377 unit (not the type unit), so we want to use the offset of
11378 the skeleton DIE (if there is one). */
11379 if (pub->die->comdat_type_p && names == pubtype_table)
11381 comdat_type_node *type_node = pub->die->die_id.die_type_node;
11383 if (type_node != NULL)
11384 die_offset = (type_node->skeleton_die != NULL
11385 ? type_node->skeleton_die->die_offset
11386 : comp_unit_die ()->die_offset);
11389 output_pubname (die_offset, pub);
11393 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
11396 /* Output public names and types tables if necessary. */
11398 static void
11399 output_pubtables (void)
11401 if (!want_pubnames () || !info_section_emitted)
11402 return;
11404 switch_to_section (debug_pubnames_section);
11405 output_pubnames (pubname_table);
11406 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
11407 It shouldn't hurt to emit it always, since pure DWARF2 consumers
11408 simply won't look for the section. */
11409 switch_to_section (debug_pubtypes_section);
11410 output_pubnames (pubtype_table);
11414 /* Output the information that goes into the .debug_aranges table.
11415 Namely, define the beginning and ending address range of the
11416 text section generated for this compilation unit. */
11418 static void
11419 output_aranges (void)
11421 unsigned i;
11422 unsigned long aranges_length = size_of_aranges ();
11424 if (!XCOFF_DEBUGGING_INFO)
11426 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11427 dw2_asm_output_data (4, 0xffffffff,
11428 "Initial length escape value indicating 64-bit DWARF extension");
11429 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
11430 "Length of Address Ranges Info");
11433 /* Version number for aranges is still 2, even up to DWARF5. */
11434 dw2_asm_output_data (2, 2, "DWARF aranges version");
11435 if (dwarf_split_debug_info)
11436 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
11437 debug_skeleton_info_section,
11438 "Offset of Compilation Unit Info");
11439 else
11440 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
11441 debug_info_section,
11442 "Offset of Compilation Unit Info");
11443 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
11444 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
11446 /* We need to align to twice the pointer size here. */
11447 if (DWARF_ARANGES_PAD_SIZE)
11449 /* Pad using a 2 byte words so that padding is correct for any
11450 pointer size. */
11451 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
11452 2 * DWARF2_ADDR_SIZE);
11453 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
11454 dw2_asm_output_data (2, 0, NULL);
11457 /* It is necessary not to output these entries if the sections were
11458 not used; if the sections were not used, the length will be 0 and
11459 the address may end up as 0 if the section is discarded by ld
11460 --gc-sections, leaving an invalid (0, 0) entry that can be
11461 confused with the terminator. */
11462 if (text_section_used)
11464 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
11465 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
11466 text_section_label, "Length");
11468 if (cold_text_section_used)
11470 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
11471 "Address");
11472 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
11473 cold_text_section_label, "Length");
11476 if (have_multiple_function_sections)
11478 unsigned fde_idx;
11479 dw_fde_ref fde;
11481 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
11483 if (DECL_IGNORED_P (fde->decl))
11484 continue;
11485 if (!fde->in_std_section)
11487 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
11488 "Address");
11489 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
11490 fde->dw_fde_begin, "Length");
11492 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
11494 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
11495 "Address");
11496 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
11497 fde->dw_fde_second_begin, "Length");
11502 /* Output the terminator words. */
11503 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11504 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11507 /* Add a new entry to .debug_ranges. Return its index into
11508 ranges_table vector. */
11510 static unsigned int
11511 add_ranges_num (int num, bool maybe_new_sec)
11513 dw_ranges r = { NULL, num, 0, maybe_new_sec };
11514 vec_safe_push (ranges_table, r);
11515 return vec_safe_length (ranges_table) - 1;
11518 /* Add a new entry to .debug_ranges corresponding to a block, or a
11519 range terminator if BLOCK is NULL. MAYBE_NEW_SEC is true if
11520 this entry might be in a different section from previous range. */
11522 static unsigned int
11523 add_ranges (const_tree block, bool maybe_new_sec)
11525 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0, maybe_new_sec);
11528 /* Note that (*rnglist_table)[offset] is either a head of a rnglist
11529 chain, or middle entry of a chain that will be directly referred to. */
11531 static void
11532 note_rnglist_head (unsigned int offset)
11534 if (dwarf_version < 5 || (*ranges_table)[offset].label)
11535 return;
11536 (*ranges_table)[offset].label = gen_internal_sym ("LLRL");
11539 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
11540 When using dwarf_split_debug_info, address attributes in dies destined
11541 for the final executable should be direct references--setting the
11542 parameter force_direct ensures this behavior. */
11544 static void
11545 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
11546 bool *added, bool force_direct)
11548 unsigned int in_use = vec_safe_length (ranges_by_label);
11549 unsigned int offset;
11550 dw_ranges_by_label rbl = { begin, end };
11551 vec_safe_push (ranges_by_label, rbl);
11552 offset = add_ranges_num (-(int)in_use - 1, true);
11553 if (!*added)
11555 add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
11556 *added = true;
11557 note_rnglist_head (offset);
11561 /* Emit .debug_ranges section. */
11563 static void
11564 output_ranges (void)
11566 unsigned i;
11567 static const char *const start_fmt = "Offset %#x";
11568 const char *fmt = start_fmt;
11569 dw_ranges *r;
11571 switch_to_section (debug_ranges_section);
11572 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
11573 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11575 int block_num = r->num;
11577 if (block_num > 0)
11579 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
11580 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
11582 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
11583 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
11585 /* If all code is in the text section, then the compilation
11586 unit base address defaults to DW_AT_low_pc, which is the
11587 base of the text section. */
11588 if (!have_multiple_function_sections)
11590 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
11591 text_section_label,
11592 fmt, i * 2 * DWARF2_ADDR_SIZE);
11593 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
11594 text_section_label, NULL);
11597 /* Otherwise, the compilation unit base address is zero,
11598 which allows us to use absolute addresses, and not worry
11599 about whether the target supports cross-section
11600 arithmetic. */
11601 else
11603 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11604 fmt, i * 2 * DWARF2_ADDR_SIZE);
11605 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
11608 fmt = NULL;
11611 /* Negative block_num stands for an index into ranges_by_label. */
11612 else if (block_num < 0)
11614 int lab_idx = - block_num - 1;
11616 if (!have_multiple_function_sections)
11618 gcc_unreachable ();
11619 #if 0
11620 /* If we ever use add_ranges_by_labels () for a single
11621 function section, all we have to do is to take out
11622 the #if 0 above. */
11623 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11624 (*ranges_by_label)[lab_idx].begin,
11625 text_section_label,
11626 fmt, i * 2 * DWARF2_ADDR_SIZE);
11627 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11628 (*ranges_by_label)[lab_idx].end,
11629 text_section_label, NULL);
11630 #endif
11632 else
11634 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11635 (*ranges_by_label)[lab_idx].begin,
11636 fmt, i * 2 * DWARF2_ADDR_SIZE);
11637 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11638 (*ranges_by_label)[lab_idx].end,
11639 NULL);
11642 else
11644 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11645 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11646 fmt = start_fmt;
11651 /* Non-zero if .debug_line_str should be used for .debug_line section
11652 strings or strings that are likely shareable with those. */
11653 #define DWARF5_USE_DEBUG_LINE_STR \
11654 (!DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET \
11655 && (DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) != 0 \
11656 /* FIXME: there is no .debug_line_str.dwo section, \
11657 for -gsplit-dwarf we should use DW_FORM_strx instead. */ \
11658 && !dwarf_split_debug_info)
11660 /* Assign .debug_rnglists indexes. */
11662 static void
11663 index_rnglists (void)
11665 unsigned i;
11666 dw_ranges *r;
11668 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11669 if (r->label)
11670 r->idx = rnglist_idx++;
11673 /* Emit .debug_rnglists section. */
11675 static void
11676 output_rnglists (unsigned generation)
11678 unsigned i;
11679 dw_ranges *r;
11680 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
11681 char l2[MAX_ARTIFICIAL_LABEL_BYTES];
11682 char basebuf[MAX_ARTIFICIAL_LABEL_BYTES];
11684 switch_to_section (debug_ranges_section);
11685 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
11686 /* There are up to 4 unique ranges labels per generation.
11687 See also init_sections_and_labels. */
11688 ASM_GENERATE_INTERNAL_LABEL (l1, DEBUG_RANGES_SECTION_LABEL,
11689 2 + generation * 4);
11690 ASM_GENERATE_INTERNAL_LABEL (l2, DEBUG_RANGES_SECTION_LABEL,
11691 3 + generation * 4);
11692 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11693 dw2_asm_output_data (4, 0xffffffff,
11694 "Initial length escape value indicating "
11695 "64-bit DWARF extension");
11696 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
11697 "Length of Range Lists");
11698 ASM_OUTPUT_LABEL (asm_out_file, l1);
11699 output_dwarf_version ();
11700 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
11701 dw2_asm_output_data (1, 0, "Segment Size");
11702 /* Emit the offset table only for -gsplit-dwarf. If we don't care
11703 about relocation sizes and primarily care about the size of .debug*
11704 sections in linked shared libraries and executables, then
11705 the offset table plus corresponding DW_FORM_rnglistx uleb128 indexes
11706 into it are usually larger than just DW_FORM_sec_offset offsets
11707 into the .debug_rnglists section. */
11708 dw2_asm_output_data (4, dwarf_split_debug_info ? rnglist_idx : 0,
11709 "Offset Entry Count");
11710 if (dwarf_split_debug_info)
11712 ASM_OUTPUT_LABEL (asm_out_file, ranges_base_label);
11713 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11714 if (r->label)
11715 dw2_asm_output_delta (DWARF_OFFSET_SIZE, r->label,
11716 ranges_base_label, NULL);
11719 const char *lab = "";
11720 unsigned int len = vec_safe_length (ranges_table);
11721 const char *base = NULL;
11722 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11724 int block_num = r->num;
11726 if (r->label)
11728 ASM_OUTPUT_LABEL (asm_out_file, r->label);
11729 lab = r->label;
11731 if (HAVE_AS_LEB128 && (r->label || r->maybe_new_sec))
11732 base = NULL;
11733 if (block_num > 0)
11735 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
11736 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
11738 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
11739 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
11741 if (HAVE_AS_LEB128)
11743 /* If all code is in the text section, then the compilation
11744 unit base address defaults to DW_AT_low_pc, which is the
11745 base of the text section. */
11746 if (!have_multiple_function_sections)
11748 dw2_asm_output_data (1, DW_RLE_offset_pair,
11749 "DW_RLE_offset_pair (%s)", lab);
11750 dw2_asm_output_delta_uleb128 (blabel, text_section_label,
11751 "Range begin address (%s)", lab);
11752 dw2_asm_output_delta_uleb128 (elabel, text_section_label,
11753 "Range end address (%s)", lab);
11754 continue;
11756 if (base == NULL)
11758 dw_ranges *r2 = NULL;
11759 if (i < len - 1)
11760 r2 = &(*ranges_table)[i + 1];
11761 if (r2
11762 && r2->num != 0
11763 && r2->label == NULL
11764 && !r2->maybe_new_sec)
11766 dw2_asm_output_data (1, DW_RLE_base_address,
11767 "DW_RLE_base_address (%s)", lab);
11768 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11769 "Base address (%s)", lab);
11770 strcpy (basebuf, blabel);
11771 base = basebuf;
11774 if (base)
11776 dw2_asm_output_data (1, DW_RLE_offset_pair,
11777 "DW_RLE_offset_pair (%s)", lab);
11778 dw2_asm_output_delta_uleb128 (blabel, base,
11779 "Range begin address (%s)", lab);
11780 dw2_asm_output_delta_uleb128 (elabel, base,
11781 "Range end address (%s)", lab);
11782 continue;
11784 dw2_asm_output_data (1, DW_RLE_start_length,
11785 "DW_RLE_start_length (%s)", lab);
11786 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11787 "Range begin address (%s)", lab);
11788 dw2_asm_output_delta_uleb128 (elabel, blabel,
11789 "Range length (%s)", lab);
11791 else
11793 dw2_asm_output_data (1, DW_RLE_start_end,
11794 "DW_RLE_start_end (%s)", lab);
11795 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11796 "Range begin address (%s)", lab);
11797 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel,
11798 "Range end address (%s)", lab);
11802 /* Negative block_num stands for an index into ranges_by_label. */
11803 else if (block_num < 0)
11805 int lab_idx = - block_num - 1;
11806 const char *blabel = (*ranges_by_label)[lab_idx].begin;
11807 const char *elabel = (*ranges_by_label)[lab_idx].end;
11809 if (!have_multiple_function_sections)
11810 gcc_unreachable ();
11811 if (HAVE_AS_LEB128)
11813 dw2_asm_output_data (1, DW_RLE_start_length,
11814 "DW_RLE_start_length (%s)", lab);
11815 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11816 "Range begin address (%s)", lab);
11817 dw2_asm_output_delta_uleb128 (elabel, blabel,
11818 "Range length (%s)", lab);
11820 else
11822 dw2_asm_output_data (1, DW_RLE_start_end,
11823 "DW_RLE_start_end (%s)", lab);
11824 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11825 "Range begin address (%s)", lab);
11826 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel,
11827 "Range end address (%s)", lab);
11830 else
11831 dw2_asm_output_data (1, DW_RLE_end_of_list,
11832 "DW_RLE_end_of_list (%s)", lab);
11834 ASM_OUTPUT_LABEL (asm_out_file, l2);
11837 /* Data structure containing information about input files. */
11838 struct file_info
11840 const char *path; /* Complete file name. */
11841 const char *fname; /* File name part. */
11842 int length; /* Length of entire string. */
11843 struct dwarf_file_data * file_idx; /* Index in input file table. */
11844 int dir_idx; /* Index in directory table. */
11847 /* Data structure containing information about directories with source
11848 files. */
11849 struct dir_info
11851 const char *path; /* Path including directory name. */
11852 int length; /* Path length. */
11853 int prefix; /* Index of directory entry which is a prefix. */
11854 int count; /* Number of files in this directory. */
11855 int dir_idx; /* Index of directory used as base. */
11858 /* Callback function for file_info comparison. We sort by looking at
11859 the directories in the path. */
11861 static int
11862 file_info_cmp (const void *p1, const void *p2)
11864 const struct file_info *const s1 = (const struct file_info *) p1;
11865 const struct file_info *const s2 = (const struct file_info *) p2;
11866 const unsigned char *cp1;
11867 const unsigned char *cp2;
11869 /* Take care of file names without directories. We need to make sure that
11870 we return consistent values to qsort since some will get confused if
11871 we return the same value when identical operands are passed in opposite
11872 orders. So if neither has a directory, return 0 and otherwise return
11873 1 or -1 depending on which one has the directory. */
11874 if ((s1->path == s1->fname || s2->path == s2->fname))
11875 return (s2->path == s2->fname) - (s1->path == s1->fname);
11877 cp1 = (const unsigned char *) s1->path;
11878 cp2 = (const unsigned char *) s2->path;
11880 while (1)
11882 ++cp1;
11883 ++cp2;
11884 /* Reached the end of the first path? If so, handle like above. */
11885 if ((cp1 == (const unsigned char *) s1->fname)
11886 || (cp2 == (const unsigned char *) s2->fname))
11887 return ((cp2 == (const unsigned char *) s2->fname)
11888 - (cp1 == (const unsigned char *) s1->fname));
11890 /* Character of current path component the same? */
11891 else if (*cp1 != *cp2)
11892 return *cp1 - *cp2;
11896 struct file_name_acquire_data
11898 struct file_info *files;
11899 int used_files;
11900 int max_files;
11903 /* Traversal function for the hash table. */
11906 file_name_acquire (dwarf_file_data **slot, file_name_acquire_data *fnad)
11908 struct dwarf_file_data *d = *slot;
11909 struct file_info *fi;
11910 const char *f;
11912 gcc_assert (fnad->max_files >= d->emitted_number);
11914 if (! d->emitted_number)
11915 return 1;
11917 gcc_assert (fnad->max_files != fnad->used_files);
11919 fi = fnad->files + fnad->used_files++;
11921 /* Skip all leading "./". */
11922 f = d->filename;
11923 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
11924 f += 2;
11926 /* Create a new array entry. */
11927 fi->path = f;
11928 fi->length = strlen (f);
11929 fi->file_idx = d;
11931 /* Search for the file name part. */
11932 f = strrchr (f, DIR_SEPARATOR);
11933 #if defined (DIR_SEPARATOR_2)
11935 char *g = strrchr (fi->path, DIR_SEPARATOR_2);
11937 if (g != NULL)
11939 if (f == NULL || f < g)
11940 f = g;
11943 #endif
11945 fi->fname = f == NULL ? fi->path : f + 1;
11946 return 1;
11949 /* Helper function for output_file_names. Emit a FORM encoded
11950 string STR, with assembly comment start ENTRY_KIND and
11951 index IDX */
11953 static void
11954 output_line_string (enum dwarf_form form, const char *str,
11955 const char *entry_kind, unsigned int idx)
11957 switch (form)
11959 case DW_FORM_string:
11960 dw2_asm_output_nstring (str, -1, "%s: %#x", entry_kind, idx);
11961 break;
11962 case DW_FORM_line_strp:
11963 if (!debug_line_str_hash)
11964 debug_line_str_hash
11965 = hash_table<indirect_string_hasher>::create_ggc (10);
11967 struct indirect_string_node *node;
11968 node = find_AT_string_in_table (str, debug_line_str_hash);
11969 set_indirect_string (node);
11970 node->form = form;
11971 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
11972 debug_line_str_section, "%s: %#x: \"%s\"",
11973 entry_kind, 0, node->str);
11974 break;
11975 default:
11976 gcc_unreachable ();
11980 /* Output the directory table and the file name table. We try to minimize
11981 the total amount of memory needed. A heuristic is used to avoid large
11982 slowdowns with many input files. */
11984 static void
11985 output_file_names (void)
11987 struct file_name_acquire_data fnad;
11988 int numfiles;
11989 struct file_info *files;
11990 struct dir_info *dirs;
11991 int *saved;
11992 int *savehere;
11993 int *backmap;
11994 int ndirs;
11995 int idx_offset;
11996 int i;
11998 if (!last_emitted_file)
12000 if (dwarf_version >= 5)
12002 dw2_asm_output_data (1, 0, "Directory entry format count");
12003 dw2_asm_output_data_uleb128 (0, "Directories count");
12004 dw2_asm_output_data (1, 0, "File name entry format count");
12005 dw2_asm_output_data_uleb128 (0, "File names count");
12007 else
12009 dw2_asm_output_data (1, 0, "End directory table");
12010 dw2_asm_output_data (1, 0, "End file name table");
12012 return;
12015 numfiles = last_emitted_file->emitted_number;
12017 /* Allocate the various arrays we need. */
12018 files = XALLOCAVEC (struct file_info, numfiles);
12019 dirs = XALLOCAVEC (struct dir_info, numfiles);
12021 fnad.files = files;
12022 fnad.used_files = 0;
12023 fnad.max_files = numfiles;
12024 file_table->traverse<file_name_acquire_data *, file_name_acquire> (&fnad);
12025 gcc_assert (fnad.used_files == fnad.max_files);
12027 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
12029 /* Find all the different directories used. */
12030 dirs[0].path = files[0].path;
12031 dirs[0].length = files[0].fname - files[0].path;
12032 dirs[0].prefix = -1;
12033 dirs[0].count = 1;
12034 dirs[0].dir_idx = 0;
12035 files[0].dir_idx = 0;
12036 ndirs = 1;
12038 for (i = 1; i < numfiles; i++)
12039 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
12040 && memcmp (dirs[ndirs - 1].path, files[i].path,
12041 dirs[ndirs - 1].length) == 0)
12043 /* Same directory as last entry. */
12044 files[i].dir_idx = ndirs - 1;
12045 ++dirs[ndirs - 1].count;
12047 else
12049 int j;
12051 /* This is a new directory. */
12052 dirs[ndirs].path = files[i].path;
12053 dirs[ndirs].length = files[i].fname - files[i].path;
12054 dirs[ndirs].count = 1;
12055 dirs[ndirs].dir_idx = ndirs;
12056 files[i].dir_idx = ndirs;
12058 /* Search for a prefix. */
12059 dirs[ndirs].prefix = -1;
12060 for (j = 0; j < ndirs; j++)
12061 if (dirs[j].length < dirs[ndirs].length
12062 && dirs[j].length > 1
12063 && (dirs[ndirs].prefix == -1
12064 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
12065 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
12066 dirs[ndirs].prefix = j;
12068 ++ndirs;
12071 /* Now to the actual work. We have to find a subset of the directories which
12072 allow expressing the file name using references to the directory table
12073 with the least amount of characters. We do not do an exhaustive search
12074 where we would have to check out every combination of every single
12075 possible prefix. Instead we use a heuristic which provides nearly optimal
12076 results in most cases and never is much off. */
12077 saved = XALLOCAVEC (int, ndirs);
12078 savehere = XALLOCAVEC (int, ndirs);
12080 memset (saved, '\0', ndirs * sizeof (saved[0]));
12081 for (i = 0; i < ndirs; i++)
12083 int j;
12084 int total;
12086 /* We can always save some space for the current directory. But this
12087 does not mean it will be enough to justify adding the directory. */
12088 savehere[i] = dirs[i].length;
12089 total = (savehere[i] - saved[i]) * dirs[i].count;
12091 for (j = i + 1; j < ndirs; j++)
12093 savehere[j] = 0;
12094 if (saved[j] < dirs[i].length)
12096 /* Determine whether the dirs[i] path is a prefix of the
12097 dirs[j] path. */
12098 int k;
12100 k = dirs[j].prefix;
12101 while (k != -1 && k != (int) i)
12102 k = dirs[k].prefix;
12104 if (k == (int) i)
12106 /* Yes it is. We can possibly save some memory by
12107 writing the filenames in dirs[j] relative to
12108 dirs[i]. */
12109 savehere[j] = dirs[i].length;
12110 total += (savehere[j] - saved[j]) * dirs[j].count;
12115 /* Check whether we can save enough to justify adding the dirs[i]
12116 directory. */
12117 if (total > dirs[i].length + 1)
12119 /* It's worthwhile adding. */
12120 for (j = i; j < ndirs; j++)
12121 if (savehere[j] > 0)
12123 /* Remember how much we saved for this directory so far. */
12124 saved[j] = savehere[j];
12126 /* Remember the prefix directory. */
12127 dirs[j].dir_idx = i;
12132 /* Emit the directory name table. */
12133 idx_offset = dirs[0].length > 0 ? 1 : 0;
12134 enum dwarf_form str_form = DW_FORM_string;
12135 enum dwarf_form idx_form = DW_FORM_udata;
12136 if (dwarf_version >= 5)
12138 const char *comp_dir = comp_dir_string ();
12139 if (comp_dir == NULL)
12140 comp_dir = "";
12141 dw2_asm_output_data (1, 1, "Directory entry format count");
12142 if (DWARF5_USE_DEBUG_LINE_STR)
12143 str_form = DW_FORM_line_strp;
12144 dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
12145 dw2_asm_output_data_uleb128 (str_form, "%s",
12146 get_DW_FORM_name (str_form));
12147 dw2_asm_output_data_uleb128 (ndirs + idx_offset, "Directories count");
12148 if (str_form == DW_FORM_string)
12150 dw2_asm_output_nstring (comp_dir, -1, "Directory Entry: %#x", 0);
12151 for (i = 1 - idx_offset; i < ndirs; i++)
12152 dw2_asm_output_nstring (dirs[i].path,
12153 dirs[i].length
12154 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
12155 "Directory Entry: %#x", i + idx_offset);
12157 else
12159 output_line_string (str_form, comp_dir, "Directory Entry", 0);
12160 for (i = 1 - idx_offset; i < ndirs; i++)
12162 const char *str
12163 = ggc_alloc_string (dirs[i].path,
12164 dirs[i].length
12165 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR);
12166 output_line_string (str_form, str, "Directory Entry",
12167 (unsigned) i + idx_offset);
12171 else
12173 for (i = 1 - idx_offset; i < ndirs; i++)
12174 dw2_asm_output_nstring (dirs[i].path,
12175 dirs[i].length
12176 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
12177 "Directory Entry: %#x", i + idx_offset);
12179 dw2_asm_output_data (1, 0, "End directory table");
12182 /* We have to emit them in the order of emitted_number since that's
12183 used in the debug info generation. To do this efficiently we
12184 generate a back-mapping of the indices first. */
12185 backmap = XALLOCAVEC (int, numfiles);
12186 for (i = 0; i < numfiles; i++)
12187 backmap[files[i].file_idx->emitted_number - 1] = i;
12189 if (dwarf_version >= 5)
12191 const char *filename0 = get_AT_string (comp_unit_die (), DW_AT_name);
12192 if (filename0 == NULL)
12193 filename0 = "";
12194 /* DW_LNCT_directory_index can use DW_FORM_udata, DW_FORM_data1 and
12195 DW_FORM_data2. Choose one based on the number of directories
12196 and how much space would they occupy in each encoding.
12197 If we have at most 256 directories, all indexes fit into
12198 a single byte, so DW_FORM_data1 is most compact (if there
12199 are at most 128 directories, DW_FORM_udata would be as
12200 compact as that, but not shorter and slower to decode). */
12201 if (ndirs + idx_offset <= 256)
12202 idx_form = DW_FORM_data1;
12203 /* If there are more than 65536 directories, we have to use
12204 DW_FORM_udata, DW_FORM_data2 can't refer to them.
12205 Otherwise, compute what space would occupy if all the indexes
12206 used DW_FORM_udata - sum - and compare that to how large would
12207 be DW_FORM_data2 encoding, and pick the more efficient one. */
12208 else if (ndirs + idx_offset <= 65536)
12210 unsigned HOST_WIDE_INT sum = 1;
12211 for (i = 0; i < numfiles; i++)
12213 int file_idx = backmap[i];
12214 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
12215 sum += size_of_uleb128 (dir_idx);
12217 if (sum >= HOST_WIDE_INT_UC (2) * (numfiles + 1))
12218 idx_form = DW_FORM_data2;
12220 #ifdef VMS_DEBUGGING_INFO
12221 dw2_asm_output_data (1, 4, "File name entry format count");
12222 #else
12223 dw2_asm_output_data (1, 2, "File name entry format count");
12224 #endif
12225 dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
12226 dw2_asm_output_data_uleb128 (str_form, "%s",
12227 get_DW_FORM_name (str_form));
12228 dw2_asm_output_data_uleb128 (DW_LNCT_directory_index,
12229 "DW_LNCT_directory_index");
12230 dw2_asm_output_data_uleb128 (idx_form, "%s",
12231 get_DW_FORM_name (idx_form));
12232 #ifdef VMS_DEBUGGING_INFO
12233 dw2_asm_output_data_uleb128 (DW_LNCT_timestamp, "DW_LNCT_timestamp");
12234 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
12235 dw2_asm_output_data_uleb128 (DW_LNCT_size, "DW_LNCT_size");
12236 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
12237 #endif
12238 dw2_asm_output_data_uleb128 (numfiles + 1, "File names count");
12240 output_line_string (str_form, filename0, "File Entry", 0);
12242 /* Include directory index. */
12243 if (idx_form != DW_FORM_udata)
12244 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
12245 0, NULL);
12246 else
12247 dw2_asm_output_data_uleb128 (0, NULL);
12249 #ifdef VMS_DEBUGGING_INFO
12250 dw2_asm_output_data_uleb128 (0, NULL);
12251 dw2_asm_output_data_uleb128 (0, NULL);
12252 #endif
12255 /* Now write all the file names. */
12256 for (i = 0; i < numfiles; i++)
12258 int file_idx = backmap[i];
12259 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
12261 #ifdef VMS_DEBUGGING_INFO
12262 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
12264 /* Setting these fields can lead to debugger miscomparisons,
12265 but VMS Debug requires them to be set correctly. */
12267 int ver;
12268 long long cdt;
12269 long siz;
12270 int maxfilelen = (strlen (files[file_idx].path)
12271 + dirs[dir_idx].length
12272 + MAX_VMS_VERSION_LEN + 1);
12273 char *filebuf = XALLOCAVEC (char, maxfilelen);
12275 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
12276 snprintf (filebuf, maxfilelen, "%s;%d",
12277 files[file_idx].path + dirs[dir_idx].length, ver);
12279 output_line_string (str_form, filebuf, "File Entry", (unsigned) i + 1);
12281 /* Include directory index. */
12282 if (dwarf_version >= 5 && idx_form != DW_FORM_udata)
12283 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
12284 dir_idx + idx_offset, NULL);
12285 else
12286 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
12288 /* Modification time. */
12289 dw2_asm_output_data_uleb128 ((vms_file_stats_name (files[file_idx].path,
12290 &cdt, 0, 0, 0) == 0)
12291 ? cdt : 0, NULL);
12293 /* File length in bytes. */
12294 dw2_asm_output_data_uleb128 ((vms_file_stats_name (files[file_idx].path,
12295 0, &siz, 0, 0) == 0)
12296 ? siz : 0, NULL);
12297 #else
12298 output_line_string (str_form,
12299 files[file_idx].path + dirs[dir_idx].length,
12300 "File Entry", (unsigned) i + 1);
12302 /* Include directory index. */
12303 if (dwarf_version >= 5 && idx_form != DW_FORM_udata)
12304 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
12305 dir_idx + idx_offset, NULL);
12306 else
12307 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
12309 if (dwarf_version >= 5)
12310 continue;
12312 /* Modification time. */
12313 dw2_asm_output_data_uleb128 (0, NULL);
12315 /* File length in bytes. */
12316 dw2_asm_output_data_uleb128 (0, NULL);
12317 #endif /* VMS_DEBUGGING_INFO */
12320 if (dwarf_version < 5)
12321 dw2_asm_output_data (1, 0, "End file name table");
12325 /* Output one line number table into the .debug_line section. */
12327 static void
12328 output_one_line_info_table (dw_line_info_table *table)
12330 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
12331 unsigned int current_line = 1;
12332 bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
12333 dw_line_info_entry *ent, *prev_addr;
12334 size_t i;
12335 unsigned int view;
12337 view = 0;
12339 FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
12341 switch (ent->opcode)
12343 case LI_set_address:
12344 /* ??? Unfortunately, we have little choice here currently, and
12345 must always use the most general form. GCC does not know the
12346 address delta itself, so we can't use DW_LNS_advance_pc. Many
12347 ports do have length attributes which will give an upper bound
12348 on the address range. We could perhaps use length attributes
12349 to determine when it is safe to use DW_LNS_fixed_advance_pc. */
12350 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
12352 view = 0;
12354 /* This can handle any delta. This takes
12355 4+DWARF2_ADDR_SIZE bytes. */
12356 dw2_asm_output_data (1, 0, "set address %s%s", line_label,
12357 debug_variable_location_views
12358 ? ", reset view to 0" : "");
12359 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12360 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12361 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12363 prev_addr = ent;
12364 break;
12366 case LI_adv_address:
12368 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
12369 char prev_label[MAX_ARTIFICIAL_LABEL_BYTES];
12370 ASM_GENERATE_INTERNAL_LABEL (prev_label, LINE_CODE_LABEL, prev_addr->val);
12372 view++;
12374 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc, "fixed advance PC, increment view to %i", view);
12375 dw2_asm_output_delta (2, line_label, prev_label,
12376 "from %s to %s", prev_label, line_label);
12378 prev_addr = ent;
12379 break;
12382 case LI_set_line:
12383 if (ent->val == current_line)
12385 /* We still need to start a new row, so output a copy insn. */
12386 dw2_asm_output_data (1, DW_LNS_copy,
12387 "copy line %u", current_line);
12389 else
12391 int line_offset = ent->val - current_line;
12392 int line_delta = line_offset - DWARF_LINE_BASE;
12394 current_line = ent->val;
12395 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
12397 /* This can handle deltas from -10 to 234, using the current
12398 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
12399 This takes 1 byte. */
12400 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
12401 "line %u", current_line);
12403 else
12405 /* This can handle any delta. This takes at least 4 bytes,
12406 depending on the value being encoded. */
12407 dw2_asm_output_data (1, DW_LNS_advance_line,
12408 "advance to line %u", current_line);
12409 dw2_asm_output_data_sleb128 (line_offset, NULL);
12410 dw2_asm_output_data (1, DW_LNS_copy, NULL);
12413 break;
12415 case LI_set_file:
12416 dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
12417 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
12418 break;
12420 case LI_set_column:
12421 dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
12422 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
12423 break;
12425 case LI_negate_stmt:
12426 current_is_stmt = !current_is_stmt;
12427 dw2_asm_output_data (1, DW_LNS_negate_stmt,
12428 "is_stmt %d", current_is_stmt);
12429 break;
12431 case LI_set_prologue_end:
12432 dw2_asm_output_data (1, DW_LNS_set_prologue_end,
12433 "set prologue end");
12434 break;
12436 case LI_set_epilogue_begin:
12437 dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
12438 "set epilogue begin");
12439 break;
12441 case LI_set_discriminator:
12442 dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
12443 dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
12444 dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
12445 dw2_asm_output_data_uleb128 (ent->val, NULL);
12446 break;
12450 /* Emit debug info for the address of the end of the table. */
12451 dw2_asm_output_data (1, 0, "set address %s", table->end_label);
12452 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12453 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12454 dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
12456 dw2_asm_output_data (1, 0, "end sequence");
12457 dw2_asm_output_data_uleb128 (1, NULL);
12458 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
12461 /* Output the source line number correspondence information. This
12462 information goes into the .debug_line section. */
12464 static void
12465 output_line_info (bool prologue_only)
12467 static unsigned int generation;
12468 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
12469 char p1[MAX_ARTIFICIAL_LABEL_BYTES], p2[MAX_ARTIFICIAL_LABEL_BYTES];
12470 bool saw_one = false;
12471 int opc;
12473 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, generation);
12474 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, generation);
12475 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, generation);
12476 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, generation++);
12478 if (!XCOFF_DEBUGGING_INFO)
12480 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
12481 dw2_asm_output_data (4, 0xffffffff,
12482 "Initial length escape value indicating 64-bit DWARF extension");
12483 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
12484 "Length of Source Line Info");
12487 ASM_OUTPUT_LABEL (asm_out_file, l1);
12489 output_dwarf_version ();
12490 if (dwarf_version >= 5)
12492 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
12493 dw2_asm_output_data (1, 0, "Segment Size");
12495 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
12496 ASM_OUTPUT_LABEL (asm_out_file, p1);
12498 /* Define the architecture-dependent minimum instruction length (in bytes).
12499 In this implementation of DWARF, this field is used for information
12500 purposes only. Since GCC generates assembly language, we have no
12501 a priori knowledge of how many instruction bytes are generated for each
12502 source line, and therefore can use only the DW_LNE_set_address and
12503 DW_LNS_fixed_advance_pc line information commands. Accordingly, we fix
12504 this as '1', which is "correct enough" for all architectures,
12505 and don't let the target override. */
12506 dw2_asm_output_data (1, 1, "Minimum Instruction Length");
12508 if (dwarf_version >= 4)
12509 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
12510 "Maximum Operations Per Instruction");
12511 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
12512 "Default is_stmt_start flag");
12513 dw2_asm_output_data (1, DWARF_LINE_BASE,
12514 "Line Base Value (Special Opcodes)");
12515 dw2_asm_output_data (1, DWARF_LINE_RANGE,
12516 "Line Range Value (Special Opcodes)");
12517 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
12518 "Special Opcode Base");
12520 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
12522 int n_op_args;
12523 switch (opc)
12525 case DW_LNS_advance_pc:
12526 case DW_LNS_advance_line:
12527 case DW_LNS_set_file:
12528 case DW_LNS_set_column:
12529 case DW_LNS_fixed_advance_pc:
12530 case DW_LNS_set_isa:
12531 n_op_args = 1;
12532 break;
12533 default:
12534 n_op_args = 0;
12535 break;
12538 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
12539 opc, n_op_args);
12542 /* Write out the information about the files we use. */
12543 output_file_names ();
12544 ASM_OUTPUT_LABEL (asm_out_file, p2);
12545 if (prologue_only)
12547 /* Output the marker for the end of the line number info. */
12548 ASM_OUTPUT_LABEL (asm_out_file, l2);
12549 return;
12552 if (separate_line_info)
12554 dw_line_info_table *table;
12555 size_t i;
12557 FOR_EACH_VEC_ELT (*separate_line_info, i, table)
12558 if (table->in_use)
12560 output_one_line_info_table (table);
12561 saw_one = true;
12564 if (cold_text_section_line_info && cold_text_section_line_info->in_use)
12566 output_one_line_info_table (cold_text_section_line_info);
12567 saw_one = true;
12570 /* ??? Some Darwin linkers crash on a .debug_line section with no
12571 sequences. Further, merely a DW_LNE_end_sequence entry is not
12572 sufficient -- the address column must also be initialized.
12573 Make sure to output at least one set_address/end_sequence pair,
12574 choosing .text since that section is always present. */
12575 if (text_section_line_info->in_use || !saw_one)
12576 output_one_line_info_table (text_section_line_info);
12578 /* Output the marker for the end of the line number info. */
12579 ASM_OUTPUT_LABEL (asm_out_file, l2);
12582 /* Return true if DW_AT_endianity should be emitted according to REVERSE. */
12584 static inline bool
12585 need_endianity_attribute_p (bool reverse)
12587 return reverse && (dwarf_version >= 3 || !dwarf_strict);
12590 /* Given a pointer to a tree node for some base type, return a pointer to
12591 a DIE that describes the given type. REVERSE is true if the type is
12592 to be interpreted in the reverse storage order wrt the target order.
12594 This routine must only be called for GCC type nodes that correspond to
12595 Dwarf base (fundamental) types. */
12597 static dw_die_ref
12598 base_type_die (tree type, bool reverse)
12600 dw_die_ref base_type_result;
12601 enum dwarf_type encoding;
12602 bool fpt_used = false;
12603 struct fixed_point_type_info fpt_info;
12604 tree type_bias = NULL_TREE;
12606 /* If this is a subtype that should not be emitted as a subrange type,
12607 use the base type. See subrange_type_for_debug_p. */
12608 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
12609 type = TREE_TYPE (type);
12611 switch (TREE_CODE (type))
12613 case INTEGER_TYPE:
12614 if ((dwarf_version >= 4 || !dwarf_strict)
12615 && TYPE_NAME (type)
12616 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12617 && DECL_IS_BUILTIN (TYPE_NAME (type))
12618 && DECL_NAME (TYPE_NAME (type)))
12620 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
12621 if (strcmp (name, "char16_t") == 0
12622 || strcmp (name, "char32_t") == 0)
12624 encoding = DW_ATE_UTF;
12625 break;
12628 if ((dwarf_version >= 3 || !dwarf_strict)
12629 && lang_hooks.types.get_fixed_point_type_info)
12631 memset (&fpt_info, 0, sizeof (fpt_info));
12632 if (lang_hooks.types.get_fixed_point_type_info (type, &fpt_info))
12634 fpt_used = true;
12635 encoding = ((TYPE_UNSIGNED (type))
12636 ? DW_ATE_unsigned_fixed
12637 : DW_ATE_signed_fixed);
12638 break;
12641 if (TYPE_STRING_FLAG (type))
12643 if (TYPE_UNSIGNED (type))
12644 encoding = DW_ATE_unsigned_char;
12645 else
12646 encoding = DW_ATE_signed_char;
12648 else if (TYPE_UNSIGNED (type))
12649 encoding = DW_ATE_unsigned;
12650 else
12651 encoding = DW_ATE_signed;
12653 if (!dwarf_strict
12654 && lang_hooks.types.get_type_bias)
12655 type_bias = lang_hooks.types.get_type_bias (type);
12656 break;
12658 case REAL_TYPE:
12659 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
12661 if (dwarf_version >= 3 || !dwarf_strict)
12662 encoding = DW_ATE_decimal_float;
12663 else
12664 encoding = DW_ATE_lo_user;
12666 else
12667 encoding = DW_ATE_float;
12668 break;
12670 case FIXED_POINT_TYPE:
12671 if (!(dwarf_version >= 3 || !dwarf_strict))
12672 encoding = DW_ATE_lo_user;
12673 else if (TYPE_UNSIGNED (type))
12674 encoding = DW_ATE_unsigned_fixed;
12675 else
12676 encoding = DW_ATE_signed_fixed;
12677 break;
12679 /* Dwarf2 doesn't know anything about complex ints, so use
12680 a user defined type for it. */
12681 case COMPLEX_TYPE:
12682 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
12683 encoding = DW_ATE_complex_float;
12684 else
12685 encoding = DW_ATE_lo_user;
12686 break;
12688 case BOOLEAN_TYPE:
12689 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
12690 encoding = DW_ATE_boolean;
12691 break;
12693 default:
12694 /* No other TREE_CODEs are Dwarf fundamental types. */
12695 gcc_unreachable ();
12698 base_type_result = new_die_raw (DW_TAG_base_type);
12700 add_AT_unsigned (base_type_result, DW_AT_byte_size,
12701 int_size_in_bytes (type));
12702 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
12704 if (need_endianity_attribute_p (reverse))
12705 add_AT_unsigned (base_type_result, DW_AT_endianity,
12706 BYTES_BIG_ENDIAN ? DW_END_little : DW_END_big);
12708 add_alignment_attribute (base_type_result, type);
12710 if (fpt_used)
12712 switch (fpt_info.scale_factor_kind)
12714 case fixed_point_scale_factor_binary:
12715 add_AT_int (base_type_result, DW_AT_binary_scale,
12716 fpt_info.scale_factor.binary);
12717 break;
12719 case fixed_point_scale_factor_decimal:
12720 add_AT_int (base_type_result, DW_AT_decimal_scale,
12721 fpt_info.scale_factor.decimal);
12722 break;
12724 case fixed_point_scale_factor_arbitrary:
12725 /* Arbitrary scale factors cannot be described in standard DWARF,
12726 yet. */
12727 if (!dwarf_strict)
12729 /* Describe the scale factor as a rational constant. */
12730 const dw_die_ref scale_factor
12731 = new_die (DW_TAG_constant, comp_unit_die (), type);
12733 add_AT_unsigned (scale_factor, DW_AT_GNU_numerator,
12734 fpt_info.scale_factor.arbitrary.numerator);
12735 add_AT_int (scale_factor, DW_AT_GNU_denominator,
12736 fpt_info.scale_factor.arbitrary.denominator);
12738 add_AT_die_ref (base_type_result, DW_AT_small, scale_factor);
12740 break;
12742 default:
12743 gcc_unreachable ();
12747 if (type_bias)
12748 add_scalar_info (base_type_result, DW_AT_GNU_bias, type_bias,
12749 dw_scalar_form_constant
12750 | dw_scalar_form_exprloc
12751 | dw_scalar_form_reference,
12752 NULL);
12754 return base_type_result;
12757 /* A C++ function with deduced return type can have a TEMPLATE_TYPE_PARM
12758 named 'auto' in its type: return true for it, false otherwise. */
12760 static inline bool
12761 is_cxx_auto (tree type)
12763 if (is_cxx ())
12765 tree name = TYPE_IDENTIFIER (type);
12766 if (name == get_identifier ("auto")
12767 || name == get_identifier ("decltype(auto)"))
12768 return true;
12770 return false;
12773 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
12774 given input type is a Dwarf "fundamental" type. Otherwise return null. */
12776 static inline int
12777 is_base_type (tree type)
12779 switch (TREE_CODE (type))
12781 case INTEGER_TYPE:
12782 case REAL_TYPE:
12783 case FIXED_POINT_TYPE:
12784 case COMPLEX_TYPE:
12785 case BOOLEAN_TYPE:
12786 case POINTER_BOUNDS_TYPE:
12787 return 1;
12789 case VOID_TYPE:
12790 case ARRAY_TYPE:
12791 case RECORD_TYPE:
12792 case UNION_TYPE:
12793 case QUAL_UNION_TYPE:
12794 case ENUMERAL_TYPE:
12795 case FUNCTION_TYPE:
12796 case METHOD_TYPE:
12797 case POINTER_TYPE:
12798 case REFERENCE_TYPE:
12799 case NULLPTR_TYPE:
12800 case OFFSET_TYPE:
12801 case LANG_TYPE:
12802 case VECTOR_TYPE:
12803 return 0;
12805 default:
12806 if (is_cxx_auto (type))
12807 return 0;
12808 gcc_unreachable ();
12811 return 0;
12814 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
12815 node, return the size in bits for the type if it is a constant, or else
12816 return the alignment for the type if the type's size is not constant, or
12817 else return BITS_PER_WORD if the type actually turns out to be an
12818 ERROR_MARK node. */
12820 static inline unsigned HOST_WIDE_INT
12821 simple_type_size_in_bits (const_tree type)
12823 if (TREE_CODE (type) == ERROR_MARK)
12824 return BITS_PER_WORD;
12825 else if (TYPE_SIZE (type) == NULL_TREE)
12826 return 0;
12827 else if (tree_fits_uhwi_p (TYPE_SIZE (type)))
12828 return tree_to_uhwi (TYPE_SIZE (type));
12829 else
12830 return TYPE_ALIGN (type);
12833 /* Similarly, but return an offset_int instead of UHWI. */
12835 static inline offset_int
12836 offset_int_type_size_in_bits (const_tree type)
12838 if (TREE_CODE (type) == ERROR_MARK)
12839 return BITS_PER_WORD;
12840 else if (TYPE_SIZE (type) == NULL_TREE)
12841 return 0;
12842 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
12843 return wi::to_offset (TYPE_SIZE (type));
12844 else
12845 return TYPE_ALIGN (type);
12848 /* Given a pointer to a tree node for a subrange type, return a pointer
12849 to a DIE that describes the given type. */
12851 static dw_die_ref
12852 subrange_type_die (tree type, tree low, tree high, tree bias,
12853 dw_die_ref context_die)
12855 dw_die_ref subrange_die;
12856 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
12858 if (context_die == NULL)
12859 context_die = comp_unit_die ();
12861 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
12863 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
12865 /* The size of the subrange type and its base type do not match,
12866 so we need to generate a size attribute for the subrange type. */
12867 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
12870 add_alignment_attribute (subrange_die, type);
12872 if (low)
12873 add_bound_info (subrange_die, DW_AT_lower_bound, low, NULL);
12874 if (high)
12875 add_bound_info (subrange_die, DW_AT_upper_bound, high, NULL);
12876 if (bias && !dwarf_strict)
12877 add_scalar_info (subrange_die, DW_AT_GNU_bias, bias,
12878 dw_scalar_form_constant
12879 | dw_scalar_form_exprloc
12880 | dw_scalar_form_reference,
12881 NULL);
12883 return subrange_die;
12886 /* Returns the (const and/or volatile) cv_qualifiers associated with
12887 the decl node. This will normally be augmented with the
12888 cv_qualifiers of the underlying type in add_type_attribute. */
12890 static int
12891 decl_quals (const_tree decl)
12893 return ((TREE_READONLY (decl)
12894 /* The C++ front-end correctly marks reference-typed
12895 variables as readonly, but from a language (and debug
12896 info) standpoint they are not const-qualified. */
12897 && TREE_CODE (TREE_TYPE (decl)) != REFERENCE_TYPE
12898 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED)
12899 | (TREE_THIS_VOLATILE (decl)
12900 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED));
12903 /* Determine the TYPE whose qualifiers match the largest strict subset
12904 of the given TYPE_QUALS, and return its qualifiers. Ignore all
12905 qualifiers outside QUAL_MASK. */
12907 static int
12908 get_nearest_type_subqualifiers (tree type, int type_quals, int qual_mask)
12910 tree t;
12911 int best_rank = 0, best_qual = 0, max_rank;
12913 type_quals &= qual_mask;
12914 max_rank = popcount_hwi (type_quals) - 1;
12916 for (t = TYPE_MAIN_VARIANT (type); t && best_rank < max_rank;
12917 t = TYPE_NEXT_VARIANT (t))
12919 int q = TYPE_QUALS (t) & qual_mask;
12921 if ((q & type_quals) == q && q != type_quals
12922 && check_base_type (t, type))
12924 int rank = popcount_hwi (q);
12926 if (rank > best_rank)
12928 best_rank = rank;
12929 best_qual = q;
12934 return best_qual;
12937 struct dwarf_qual_info_t { int q; enum dwarf_tag t; };
12938 static const dwarf_qual_info_t dwarf_qual_info[] =
12940 { TYPE_QUAL_CONST, DW_TAG_const_type },
12941 { TYPE_QUAL_VOLATILE, DW_TAG_volatile_type },
12942 { TYPE_QUAL_RESTRICT, DW_TAG_restrict_type },
12943 { TYPE_QUAL_ATOMIC, DW_TAG_atomic_type }
12945 static const unsigned int dwarf_qual_info_size
12946 = sizeof (dwarf_qual_info) / sizeof (dwarf_qual_info[0]);
12948 /* If DIE is a qualified DIE of some base DIE with the same parent,
12949 return the base DIE, otherwise return NULL. Set MASK to the
12950 qualifiers added compared to the returned DIE. */
12952 static dw_die_ref
12953 qualified_die_p (dw_die_ref die, int *mask, unsigned int depth)
12955 unsigned int i;
12956 for (i = 0; i < dwarf_qual_info_size; i++)
12957 if (die->die_tag == dwarf_qual_info[i].t)
12958 break;
12959 if (i == dwarf_qual_info_size)
12960 return NULL;
12961 if (vec_safe_length (die->die_attr) != 1)
12962 return NULL;
12963 dw_die_ref type = get_AT_ref (die, DW_AT_type);
12964 if (type == NULL || type->die_parent != die->die_parent)
12965 return NULL;
12966 *mask |= dwarf_qual_info[i].q;
12967 if (depth)
12969 dw_die_ref ret = qualified_die_p (type, mask, depth - 1);
12970 if (ret)
12971 return ret;
12973 return type;
12976 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
12977 entry that chains the modifiers specified by CV_QUALS in front of the
12978 given type. REVERSE is true if the type is to be interpreted in the
12979 reverse storage order wrt the target order. */
12981 static dw_die_ref
12982 modified_type_die (tree type, int cv_quals, bool reverse,
12983 dw_die_ref context_die)
12985 enum tree_code code = TREE_CODE (type);
12986 dw_die_ref mod_type_die;
12987 dw_die_ref sub_die = NULL;
12988 tree item_type = NULL;
12989 tree qualified_type;
12990 tree name, low, high;
12991 dw_die_ref mod_scope;
12992 /* Only these cv-qualifiers are currently handled. */
12993 const int cv_qual_mask = (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE
12994 | TYPE_QUAL_RESTRICT | TYPE_QUAL_ATOMIC |
12995 ENCODE_QUAL_ADDR_SPACE(~0U));
12996 const bool reverse_base_type
12997 = need_endianity_attribute_p (reverse) && is_base_type (type);
12999 if (code == ERROR_MARK)
13000 return NULL;
13002 if (lang_hooks.types.get_debug_type)
13004 tree debug_type = lang_hooks.types.get_debug_type (type);
13006 if (debug_type != NULL_TREE && debug_type != type)
13007 return modified_type_die (debug_type, cv_quals, reverse, context_die);
13010 cv_quals &= cv_qual_mask;
13012 /* Don't emit DW_TAG_restrict_type for DWARFv2, since it is a type
13013 tag modifier (and not an attribute) old consumers won't be able
13014 to handle it. */
13015 if (dwarf_version < 3)
13016 cv_quals &= ~TYPE_QUAL_RESTRICT;
13018 /* Likewise for DW_TAG_atomic_type for DWARFv5. */
13019 if (dwarf_version < 5)
13020 cv_quals &= ~TYPE_QUAL_ATOMIC;
13022 /* See if we already have the appropriately qualified variant of
13023 this type. */
13024 qualified_type = get_qualified_type (type, cv_quals);
13026 if (qualified_type == sizetype)
13028 /* Try not to expose the internal sizetype type's name. */
13029 if (TYPE_NAME (qualified_type)
13030 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
13032 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
13034 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
13035 && (TYPE_PRECISION (t)
13036 == TYPE_PRECISION (qualified_type))
13037 && (TYPE_UNSIGNED (t)
13038 == TYPE_UNSIGNED (qualified_type)));
13039 qualified_type = t;
13041 else if (qualified_type == sizetype
13042 && TREE_CODE (sizetype) == TREE_CODE (size_type_node)
13043 && TYPE_PRECISION (sizetype) == TYPE_PRECISION (size_type_node)
13044 && TYPE_UNSIGNED (sizetype) == TYPE_UNSIGNED (size_type_node))
13045 qualified_type = size_type_node;
13048 /* If we do, then we can just use its DIE, if it exists. */
13049 if (qualified_type)
13051 mod_type_die = lookup_type_die (qualified_type);
13053 /* DW_AT_endianity doesn't come from a qualifier on the type, so it is
13054 dealt with specially: the DIE with the attribute, if it exists, is
13055 placed immediately after the regular DIE for the same base type. */
13056 if (mod_type_die
13057 && (!reverse_base_type
13058 || ((mod_type_die = mod_type_die->die_sib) != NULL
13059 && get_AT_unsigned (mod_type_die, DW_AT_endianity))))
13060 return mod_type_die;
13063 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
13065 /* Handle C typedef types. */
13066 if (name
13067 && TREE_CODE (name) == TYPE_DECL
13068 && DECL_ORIGINAL_TYPE (name)
13069 && !DECL_ARTIFICIAL (name))
13071 tree dtype = TREE_TYPE (name);
13073 /* Skip the typedef for base types with DW_AT_endianity, no big deal. */
13074 if (qualified_type == dtype && !reverse_base_type)
13076 tree origin = decl_ultimate_origin (name);
13078 /* Typedef variants that have an abstract origin don't get their own
13079 type DIE (see gen_typedef_die), so fall back on the ultimate
13080 abstract origin instead. */
13081 if (origin != NULL && origin != name)
13082 return modified_type_die (TREE_TYPE (origin), cv_quals, reverse,
13083 context_die);
13085 /* For a named type, use the typedef. */
13086 gen_type_die (qualified_type, context_die);
13087 return lookup_type_die (qualified_type);
13089 else
13091 int dquals = TYPE_QUALS_NO_ADDR_SPACE (dtype);
13092 dquals &= cv_qual_mask;
13093 if ((dquals & ~cv_quals) != TYPE_UNQUALIFIED
13094 || (cv_quals == dquals && DECL_ORIGINAL_TYPE (name) != type))
13095 /* cv-unqualified version of named type. Just use
13096 the unnamed type to which it refers. */
13097 return modified_type_die (DECL_ORIGINAL_TYPE (name), cv_quals,
13098 reverse, context_die);
13099 /* Else cv-qualified version of named type; fall through. */
13103 mod_scope = scope_die_for (type, context_die);
13105 if (cv_quals)
13107 int sub_quals = 0, first_quals = 0;
13108 unsigned i;
13109 dw_die_ref first = NULL, last = NULL;
13111 /* Determine a lesser qualified type that most closely matches
13112 this one. Then generate DW_TAG_* entries for the remaining
13113 qualifiers. */
13114 sub_quals = get_nearest_type_subqualifiers (type, cv_quals,
13115 cv_qual_mask);
13116 if (sub_quals && use_debug_types)
13118 bool needed = false;
13119 /* If emitting type units, make sure the order of qualifiers
13120 is canonical. Thus, start from unqualified type if
13121 an earlier qualifier is missing in sub_quals, but some later
13122 one is present there. */
13123 for (i = 0; i < dwarf_qual_info_size; i++)
13124 if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
13125 needed = true;
13126 else if (needed && (dwarf_qual_info[i].q & cv_quals))
13128 sub_quals = 0;
13129 break;
13132 mod_type_die = modified_type_die (type, sub_quals, reverse, context_die);
13133 if (mod_scope && mod_type_die && mod_type_die->die_parent == mod_scope)
13135 /* As not all intermediate qualified DIEs have corresponding
13136 tree types, ensure that qualified DIEs in the same scope
13137 as their DW_AT_type are emitted after their DW_AT_type,
13138 only with other qualified DIEs for the same type possibly
13139 in between them. Determine the range of such qualified
13140 DIEs now (first being the base type, last being corresponding
13141 last qualified DIE for it). */
13142 unsigned int count = 0;
13143 first = qualified_die_p (mod_type_die, &first_quals,
13144 dwarf_qual_info_size);
13145 if (first == NULL)
13146 first = mod_type_die;
13147 gcc_assert ((first_quals & ~sub_quals) == 0);
13148 for (count = 0, last = first;
13149 count < (1U << dwarf_qual_info_size);
13150 count++, last = last->die_sib)
13152 int quals = 0;
13153 if (last == mod_scope->die_child)
13154 break;
13155 if (qualified_die_p (last->die_sib, &quals, dwarf_qual_info_size)
13156 != first)
13157 break;
13161 for (i = 0; i < dwarf_qual_info_size; i++)
13162 if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
13164 dw_die_ref d;
13165 if (first && first != last)
13167 for (d = first->die_sib; ; d = d->die_sib)
13169 int quals = 0;
13170 qualified_die_p (d, &quals, dwarf_qual_info_size);
13171 if (quals == (first_quals | dwarf_qual_info[i].q))
13172 break;
13173 if (d == last)
13175 d = NULL;
13176 break;
13179 if (d)
13181 mod_type_die = d;
13182 continue;
13185 if (first)
13187 d = new_die_raw (dwarf_qual_info[i].t);
13188 add_child_die_after (mod_scope, d, last);
13189 last = d;
13191 else
13192 d = new_die (dwarf_qual_info[i].t, mod_scope, type);
13193 if (mod_type_die)
13194 add_AT_die_ref (d, DW_AT_type, mod_type_die);
13195 mod_type_die = d;
13196 first_quals |= dwarf_qual_info[i].q;
13199 else if (code == POINTER_TYPE || code == REFERENCE_TYPE)
13201 dwarf_tag tag = DW_TAG_pointer_type;
13202 if (code == REFERENCE_TYPE)
13204 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
13205 tag = DW_TAG_rvalue_reference_type;
13206 else
13207 tag = DW_TAG_reference_type;
13209 mod_type_die = new_die (tag, mod_scope, type);
13211 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
13212 simple_type_size_in_bits (type) / BITS_PER_UNIT);
13213 add_alignment_attribute (mod_type_die, type);
13214 item_type = TREE_TYPE (type);
13216 addr_space_t as = TYPE_ADDR_SPACE (item_type);
13217 if (!ADDR_SPACE_GENERIC_P (as))
13219 int action = targetm.addr_space.debug (as);
13220 if (action >= 0)
13222 /* Positive values indicate an address_class. */
13223 add_AT_unsigned (mod_type_die, DW_AT_address_class, action);
13225 else
13227 /* Negative values indicate an (inverted) segment base reg. */
13228 dw_loc_descr_ref d
13229 = one_reg_loc_descriptor (~action, VAR_INIT_STATUS_INITIALIZED);
13230 add_AT_loc (mod_type_die, DW_AT_segment, d);
13234 else if (code == INTEGER_TYPE
13235 && TREE_TYPE (type) != NULL_TREE
13236 && subrange_type_for_debug_p (type, &low, &high))
13238 tree bias = NULL_TREE;
13239 if (lang_hooks.types.get_type_bias)
13240 bias = lang_hooks.types.get_type_bias (type);
13241 mod_type_die = subrange_type_die (type, low, high, bias, context_die);
13242 item_type = TREE_TYPE (type);
13244 else if (is_base_type (type))
13246 mod_type_die = base_type_die (type, reverse);
13248 /* The DIE with DW_AT_endianity is placed right after the naked DIE. */
13249 if (reverse_base_type)
13251 dw_die_ref after_die
13252 = modified_type_die (type, cv_quals, false, context_die);
13253 add_child_die_after (comp_unit_die (), mod_type_die, after_die);
13255 else
13256 add_child_die (comp_unit_die (), mod_type_die);
13258 add_pubtype (type, mod_type_die);
13260 else
13262 gen_type_die (type, context_die);
13264 /* We have to get the type_main_variant here (and pass that to the
13265 `lookup_type_die' routine) because the ..._TYPE node we have
13266 might simply be a *copy* of some original type node (where the
13267 copy was created to help us keep track of typedef names) and
13268 that copy might have a different TYPE_UID from the original
13269 ..._TYPE node. */
13270 if (TREE_CODE (type) == FUNCTION_TYPE
13271 || TREE_CODE (type) == METHOD_TYPE)
13273 /* For function/method types, can't just use type_main_variant here,
13274 because that can have different ref-qualifiers for C++,
13275 but try to canonicalize. */
13276 tree main = TYPE_MAIN_VARIANT (type);
13277 for (tree t = main; t; t = TYPE_NEXT_VARIANT (t))
13278 if (TYPE_QUALS_NO_ADDR_SPACE (t) == 0
13279 && check_base_type (t, main)
13280 && check_lang_type (t, type))
13281 return lookup_type_die (t);
13282 return lookup_type_die (type);
13284 else if (TREE_CODE (type) != VECTOR_TYPE
13285 && TREE_CODE (type) != ARRAY_TYPE)
13286 return lookup_type_die (type_main_variant (type));
13287 else
13288 /* Vectors have the debugging information in the type,
13289 not the main variant. */
13290 return lookup_type_die (type);
13293 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
13294 don't output a DW_TAG_typedef, since there isn't one in the
13295 user's program; just attach a DW_AT_name to the type.
13296 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
13297 if the base type already has the same name. */
13298 if (name
13299 && ((TREE_CODE (name) != TYPE_DECL
13300 && (qualified_type == TYPE_MAIN_VARIANT (type)
13301 || (cv_quals == TYPE_UNQUALIFIED)))
13302 || (TREE_CODE (name) == TYPE_DECL
13303 && TREE_TYPE (name) == qualified_type
13304 && DECL_NAME (name))))
13306 if (TREE_CODE (name) == TYPE_DECL)
13307 /* Could just call add_name_and_src_coords_attributes here,
13308 but since this is a builtin type it doesn't have any
13309 useful source coordinates anyway. */
13310 name = DECL_NAME (name);
13311 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
13313 /* This probably indicates a bug. */
13314 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
13316 name = TYPE_IDENTIFIER (type);
13317 add_name_attribute (mod_type_die,
13318 name ? IDENTIFIER_POINTER (name) : "__unknown__");
13321 if (qualified_type && !reverse_base_type)
13322 equate_type_number_to_die (qualified_type, mod_type_die);
13324 if (item_type)
13325 /* We must do this after the equate_type_number_to_die call, in case
13326 this is a recursive type. This ensures that the modified_type_die
13327 recursion will terminate even if the type is recursive. Recursive
13328 types are possible in Ada. */
13329 sub_die = modified_type_die (item_type,
13330 TYPE_QUALS_NO_ADDR_SPACE (item_type),
13331 reverse,
13332 context_die);
13334 if (sub_die != NULL)
13335 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
13337 add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
13338 if (TYPE_ARTIFICIAL (type))
13339 add_AT_flag (mod_type_die, DW_AT_artificial, 1);
13341 return mod_type_die;
13344 /* Generate DIEs for the generic parameters of T.
13345 T must be either a generic type or a generic function.
13346 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
13348 static void
13349 gen_generic_params_dies (tree t)
13351 tree parms, args;
13352 int parms_num, i;
13353 dw_die_ref die = NULL;
13354 int non_default;
13356 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
13357 return;
13359 if (TYPE_P (t))
13360 die = lookup_type_die (t);
13361 else if (DECL_P (t))
13362 die = lookup_decl_die (t);
13364 gcc_assert (die);
13366 parms = lang_hooks.get_innermost_generic_parms (t);
13367 if (!parms)
13368 /* T has no generic parameter. It means T is neither a generic type
13369 or function. End of story. */
13370 return;
13372 parms_num = TREE_VEC_LENGTH (parms);
13373 args = lang_hooks.get_innermost_generic_args (t);
13374 if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
13375 non_default = int_cst_value (TREE_CHAIN (args));
13376 else
13377 non_default = TREE_VEC_LENGTH (args);
13378 for (i = 0; i < parms_num; i++)
13380 tree parm, arg, arg_pack_elems;
13381 dw_die_ref parm_die;
13383 parm = TREE_VEC_ELT (parms, i);
13384 arg = TREE_VEC_ELT (args, i);
13385 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
13386 gcc_assert (parm && TREE_VALUE (parm) && arg);
13388 if (parm && TREE_VALUE (parm) && arg)
13390 /* If PARM represents a template parameter pack,
13391 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
13392 by DW_TAG_template_*_parameter DIEs for the argument
13393 pack elements of ARG. Note that ARG would then be
13394 an argument pack. */
13395 if (arg_pack_elems)
13396 parm_die = template_parameter_pack_die (TREE_VALUE (parm),
13397 arg_pack_elems,
13398 die);
13399 else
13400 parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
13401 true /* emit name */, die);
13402 if (i >= non_default)
13403 add_AT_flag (parm_die, DW_AT_default_value, 1);
13408 /* Create and return a DIE for PARM which should be
13409 the representation of a generic type parameter.
13410 For instance, in the C++ front end, PARM would be a template parameter.
13411 ARG is the argument to PARM.
13412 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
13413 name of the PARM.
13414 PARENT_DIE is the parent DIE which the new created DIE should be added to,
13415 as a child node. */
13417 static dw_die_ref
13418 generic_parameter_die (tree parm, tree arg,
13419 bool emit_name_p,
13420 dw_die_ref parent_die)
13422 dw_die_ref tmpl_die = NULL;
13423 const char *name = NULL;
13425 if (!parm || !DECL_NAME (parm) || !arg)
13426 return NULL;
13428 /* We support non-type generic parameters and arguments,
13429 type generic parameters and arguments, as well as
13430 generic generic parameters (a.k.a. template template parameters in C++)
13431 and arguments. */
13432 if (TREE_CODE (parm) == PARM_DECL)
13433 /* PARM is a nontype generic parameter */
13434 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
13435 else if (TREE_CODE (parm) == TYPE_DECL)
13436 /* PARM is a type generic parameter. */
13437 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
13438 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
13439 /* PARM is a generic generic parameter.
13440 Its DIE is a GNU extension. It shall have a
13441 DW_AT_name attribute to represent the name of the template template
13442 parameter, and a DW_AT_GNU_template_name attribute to represent the
13443 name of the template template argument. */
13444 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
13445 parent_die, parm);
13446 else
13447 gcc_unreachable ();
13449 if (tmpl_die)
13451 tree tmpl_type;
13453 /* If PARM is a generic parameter pack, it means we are
13454 emitting debug info for a template argument pack element.
13455 In other terms, ARG is a template argument pack element.
13456 In that case, we don't emit any DW_AT_name attribute for
13457 the die. */
13458 if (emit_name_p)
13460 name = IDENTIFIER_POINTER (DECL_NAME (parm));
13461 gcc_assert (name);
13462 add_AT_string (tmpl_die, DW_AT_name, name);
13465 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
13467 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
13468 TMPL_DIE should have a child DW_AT_type attribute that is set
13469 to the type of the argument to PARM, which is ARG.
13470 If PARM is a type generic parameter, TMPL_DIE should have a
13471 child DW_AT_type that is set to ARG. */
13472 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
13473 add_type_attribute (tmpl_die, tmpl_type,
13474 (TREE_THIS_VOLATILE (tmpl_type)
13475 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED),
13476 false, parent_die);
13478 else
13480 /* So TMPL_DIE is a DIE representing a
13481 a generic generic template parameter, a.k.a template template
13482 parameter in C++ and arg is a template. */
13484 /* The DW_AT_GNU_template_name attribute of the DIE must be set
13485 to the name of the argument. */
13486 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
13487 if (name)
13488 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
13491 if (TREE_CODE (parm) == PARM_DECL)
13492 /* So PARM is a non-type generic parameter.
13493 DWARF3 5.6.8 says we must set a DW_AT_const_value child
13494 attribute of TMPL_DIE which value represents the value
13495 of ARG.
13496 We must be careful here:
13497 The value of ARG might reference some function decls.
13498 We might currently be emitting debug info for a generic
13499 type and types are emitted before function decls, we don't
13500 know if the function decls referenced by ARG will actually be
13501 emitted after cgraph computations.
13502 So must defer the generation of the DW_AT_const_value to
13503 after cgraph is ready. */
13504 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
13507 return tmpl_die;
13510 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
13511 PARM_PACK must be a template parameter pack. The returned DIE
13512 will be child DIE of PARENT_DIE. */
13514 static dw_die_ref
13515 template_parameter_pack_die (tree parm_pack,
13516 tree parm_pack_args,
13517 dw_die_ref parent_die)
13519 dw_die_ref die;
13520 int j;
13522 gcc_assert (parent_die && parm_pack);
13524 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
13525 add_name_and_src_coords_attributes (die, parm_pack);
13526 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
13527 generic_parameter_die (parm_pack,
13528 TREE_VEC_ELT (parm_pack_args, j),
13529 false /* Don't emit DW_AT_name */,
13530 die);
13531 return die;
13534 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
13535 an enumerated type. */
13537 static inline int
13538 type_is_enum (const_tree type)
13540 return TREE_CODE (type) == ENUMERAL_TYPE;
13543 /* Return the DBX register number described by a given RTL node. */
13545 static unsigned int
13546 dbx_reg_number (const_rtx rtl)
13548 unsigned regno = REGNO (rtl);
13550 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
13552 #ifdef LEAF_REG_REMAP
13553 if (crtl->uses_only_leaf_regs)
13555 int leaf_reg = LEAF_REG_REMAP (regno);
13556 if (leaf_reg != -1)
13557 regno = (unsigned) leaf_reg;
13559 #endif
13561 regno = DBX_REGISTER_NUMBER (regno);
13562 gcc_assert (regno != INVALID_REGNUM);
13563 return regno;
13566 /* Optionally add a DW_OP_piece term to a location description expression.
13567 DW_OP_piece is only added if the location description expression already
13568 doesn't end with DW_OP_piece. */
13570 static void
13571 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
13573 dw_loc_descr_ref loc;
13575 if (*list_head != NULL)
13577 /* Find the end of the chain. */
13578 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
13581 if (loc->dw_loc_opc != DW_OP_piece)
13582 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
13586 /* Return a location descriptor that designates a machine register or
13587 zero if there is none. */
13589 static dw_loc_descr_ref
13590 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
13592 rtx regs;
13594 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
13595 return 0;
13597 /* We only use "frame base" when we're sure we're talking about the
13598 post-prologue local stack frame. We do this by *not* running
13599 register elimination until this point, and recognizing the special
13600 argument pointer and soft frame pointer rtx's.
13601 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
13602 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
13603 && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
13605 dw_loc_descr_ref result = NULL;
13607 if (dwarf_version >= 4 || !dwarf_strict)
13609 result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
13610 initialized);
13611 if (result)
13612 add_loc_descr (&result,
13613 new_loc_descr (DW_OP_stack_value, 0, 0));
13615 return result;
13618 regs = targetm.dwarf_register_span (rtl);
13620 if (REG_NREGS (rtl) > 1 || regs)
13621 return multiple_reg_loc_descriptor (rtl, regs, initialized);
13622 else
13624 unsigned int dbx_regnum = dbx_reg_number (rtl);
13625 if (dbx_regnum == IGNORED_DWARF_REGNUM)
13626 return 0;
13627 return one_reg_loc_descriptor (dbx_regnum, initialized);
13631 /* Return a location descriptor that designates a machine register for
13632 a given hard register number. */
13634 static dw_loc_descr_ref
13635 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
13637 dw_loc_descr_ref reg_loc_descr;
13639 if (regno <= 31)
13640 reg_loc_descr
13641 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
13642 else
13643 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
13645 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13646 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13648 return reg_loc_descr;
13651 /* Given an RTL of a register, return a location descriptor that
13652 designates a value that spans more than one register. */
13654 static dw_loc_descr_ref
13655 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
13656 enum var_init_status initialized)
13658 int size, i;
13659 dw_loc_descr_ref loc_result = NULL;
13661 /* Simple, contiguous registers. */
13662 if (regs == NULL_RTX)
13664 unsigned reg = REGNO (rtl);
13665 int nregs;
13667 #ifdef LEAF_REG_REMAP
13668 if (crtl->uses_only_leaf_regs)
13670 int leaf_reg = LEAF_REG_REMAP (reg);
13671 if (leaf_reg != -1)
13672 reg = (unsigned) leaf_reg;
13674 #endif
13676 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
13677 nregs = REG_NREGS (rtl);
13679 /* At present we only track constant-sized pieces. */
13680 if (!GET_MODE_SIZE (GET_MODE (rtl)).is_constant (&size))
13681 return NULL;
13682 size /= nregs;
13684 loc_result = NULL;
13685 while (nregs--)
13687 dw_loc_descr_ref t;
13689 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
13690 VAR_INIT_STATUS_INITIALIZED);
13691 add_loc_descr (&loc_result, t);
13692 add_loc_descr_op_piece (&loc_result, size);
13693 ++reg;
13695 return loc_result;
13698 /* Now onto stupid register sets in non contiguous locations. */
13700 gcc_assert (GET_CODE (regs) == PARALLEL);
13702 /* At present we only track constant-sized pieces. */
13703 if (!GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0))).is_constant (&size))
13704 return NULL;
13705 loc_result = NULL;
13707 for (i = 0; i < XVECLEN (regs, 0); ++i)
13709 dw_loc_descr_ref t;
13711 t = one_reg_loc_descriptor (dbx_reg_number (XVECEXP (regs, 0, i)),
13712 VAR_INIT_STATUS_INITIALIZED);
13713 add_loc_descr (&loc_result, t);
13714 add_loc_descr_op_piece (&loc_result, size);
13717 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13718 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13719 return loc_result;
13722 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
13724 /* Return a location descriptor that designates a constant i,
13725 as a compound operation from constant (i >> shift), constant shift
13726 and DW_OP_shl. */
13728 static dw_loc_descr_ref
13729 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
13731 dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
13732 add_loc_descr (&ret, int_loc_descriptor (shift));
13733 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
13734 return ret;
13737 /* Return a location descriptor that designates constant POLY_I. */
13739 static dw_loc_descr_ref
13740 int_loc_descriptor (poly_int64 poly_i)
13742 enum dwarf_location_atom op;
13744 HOST_WIDE_INT i;
13745 if (!poly_i.is_constant (&i))
13747 /* Create location descriptions for the non-constant part and
13748 add any constant offset at the end. */
13749 dw_loc_descr_ref ret = NULL;
13750 HOST_WIDE_INT constant = poly_i.coeffs[0];
13751 for (unsigned int j = 1; j < NUM_POLY_INT_COEFFS; ++j)
13753 HOST_WIDE_INT coeff = poly_i.coeffs[j];
13754 if (coeff != 0)
13756 dw_loc_descr_ref start = ret;
13757 unsigned int factor;
13758 int bias;
13759 unsigned int regno = targetm.dwarf_poly_indeterminate_value
13760 (j, &factor, &bias);
13762 /* Add COEFF * ((REGNO / FACTOR) - BIAS) to the value:
13763 add COEFF * (REGNO / FACTOR) now and subtract
13764 COEFF * BIAS from the final constant part. */
13765 constant -= coeff * bias;
13766 add_loc_descr (&ret, new_reg_loc_descr (regno, 0));
13767 if (coeff % factor == 0)
13768 coeff /= factor;
13769 else
13771 int amount = exact_log2 (factor);
13772 gcc_assert (amount >= 0);
13773 add_loc_descr (&ret, int_loc_descriptor (amount));
13774 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
13776 if (coeff != 1)
13778 add_loc_descr (&ret, int_loc_descriptor (coeff));
13779 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
13781 if (start)
13782 add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
13785 loc_descr_plus_const (&ret, constant);
13786 return ret;
13789 /* Pick the smallest representation of a constant, rather than just
13790 defaulting to the LEB encoding. */
13791 if (i >= 0)
13793 int clz = clz_hwi (i);
13794 int ctz = ctz_hwi (i);
13795 if (i <= 31)
13796 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
13797 else if (i <= 0xff)
13798 op = DW_OP_const1u;
13799 else if (i <= 0xffff)
13800 op = DW_OP_const2u;
13801 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
13802 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
13803 /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
13804 DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
13805 while DW_OP_const4u is 5 bytes. */
13806 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
13807 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13808 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
13809 /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
13810 while DW_OP_const4u is 5 bytes. */
13811 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
13813 else if (DWARF2_ADDR_SIZE == 4 && i > 0x7fffffff
13814 && size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i)
13815 <= 4)
13817 /* As i >= 2**31, the double cast above will yield a negative number.
13818 Since wrapping is defined in DWARF expressions we can output big
13819 positive integers as small negative ones, regardless of the size
13820 of host wide ints.
13822 Here, since the evaluator will handle 32-bit values and since i >=
13823 2**31, we know it's going to be interpreted as a negative literal:
13824 store it this way if we can do better than 5 bytes this way. */
13825 return int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i);
13827 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
13828 op = DW_OP_const4u;
13830 /* Past this point, i >= 0x100000000 and thus DW_OP_constu will take at
13831 least 6 bytes: see if we can do better before falling back to it. */
13832 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13833 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
13834 /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes. */
13835 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
13836 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
13837 && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
13838 >= HOST_BITS_PER_WIDE_INT)
13839 /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
13840 DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes. */
13841 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
13842 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
13843 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
13844 && size_of_uleb128 (i) > 6)
13845 /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes. */
13846 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
13847 else
13848 op = DW_OP_constu;
13850 else
13852 if (i >= -0x80)
13853 op = DW_OP_const1s;
13854 else if (i >= -0x8000)
13855 op = DW_OP_const2s;
13856 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
13858 if (size_of_int_loc_descriptor (i) < 5)
13860 dw_loc_descr_ref ret = int_loc_descriptor (-i);
13861 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
13862 return ret;
13864 op = DW_OP_const4s;
13866 else
13868 if (size_of_int_loc_descriptor (i)
13869 < (unsigned long) 1 + size_of_sleb128 (i))
13871 dw_loc_descr_ref ret = int_loc_descriptor (-i);
13872 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
13873 return ret;
13875 op = DW_OP_consts;
13879 return new_loc_descr (op, i, 0);
13882 /* Likewise, for unsigned constants. */
13884 static dw_loc_descr_ref
13885 uint_loc_descriptor (unsigned HOST_WIDE_INT i)
13887 const unsigned HOST_WIDE_INT max_int = INTTYPE_MAXIMUM (HOST_WIDE_INT);
13888 const unsigned HOST_WIDE_INT max_uint
13889 = INTTYPE_MAXIMUM (unsigned HOST_WIDE_INT);
13891 /* If possible, use the clever signed constants handling. */
13892 if (i <= max_int)
13893 return int_loc_descriptor ((HOST_WIDE_INT) i);
13895 /* Here, we are left with positive numbers that cannot be represented as
13896 HOST_WIDE_INT, i.e.:
13897 max (HOST_WIDE_INT) < i <= max (unsigned HOST_WIDE_INT)
13899 Using DW_OP_const4/8/./u operation to encode them consumes a lot of bytes
13900 whereas may be better to output a negative integer: thanks to integer
13901 wrapping, we know that:
13902 x = x - 2 ** DWARF2_ADDR_SIZE
13903 = x - 2 * (max (HOST_WIDE_INT) + 1)
13904 So numbers close to max (unsigned HOST_WIDE_INT) could be represented as
13905 small negative integers. Let's try that in cases it will clearly improve
13906 the encoding: there is no gain turning DW_OP_const4u into
13907 DW_OP_const4s. */
13908 if (DWARF2_ADDR_SIZE * 8 == HOST_BITS_PER_WIDE_INT
13909 && ((DWARF2_ADDR_SIZE == 4 && i > max_uint - 0x8000)
13910 || (DWARF2_ADDR_SIZE == 8 && i > max_uint - 0x80000000)))
13912 const unsigned HOST_WIDE_INT first_shift = i - max_int - 1;
13914 /* Now, -1 < first_shift <= max (HOST_WIDE_INT)
13915 i.e. 0 <= first_shift <= max (HOST_WIDE_INT). */
13916 const HOST_WIDE_INT second_shift
13917 = (HOST_WIDE_INT) first_shift - (HOST_WIDE_INT) max_int - 1;
13919 /* So we finally have:
13920 -max (HOST_WIDE_INT) - 1 <= second_shift <= -1.
13921 i.e. min (HOST_WIDE_INT) <= second_shift < 0. */
13922 return int_loc_descriptor (second_shift);
13925 /* Last chance: fallback to a simple constant operation. */
13926 return new_loc_descr
13927 ((HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
13928 ? DW_OP_const4u
13929 : DW_OP_const8u,
13930 i, 0);
13933 /* Generate and return a location description that computes the unsigned
13934 comparison of the two stack top entries (a OP b where b is the top-most
13935 entry and a is the second one). The KIND of comparison can be LT_EXPR,
13936 LE_EXPR, GT_EXPR or GE_EXPR. */
13938 static dw_loc_descr_ref
13939 uint_comparison_loc_list (enum tree_code kind)
13941 enum dwarf_location_atom op, flip_op;
13942 dw_loc_descr_ref ret, bra_node, jmp_node, tmp;
13944 switch (kind)
13946 case LT_EXPR:
13947 op = DW_OP_lt;
13948 break;
13949 case LE_EXPR:
13950 op = DW_OP_le;
13951 break;
13952 case GT_EXPR:
13953 op = DW_OP_gt;
13954 break;
13955 case GE_EXPR:
13956 op = DW_OP_ge;
13957 break;
13958 default:
13959 gcc_unreachable ();
13962 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
13963 jmp_node = new_loc_descr (DW_OP_skip, 0, 0);
13965 /* Until DWARFv4, operations all work on signed integers. It is nevertheless
13966 possible to perform unsigned comparisons: we just have to distinguish
13967 three cases:
13969 1. when a and b have the same sign (as signed integers); then we should
13970 return: a OP(signed) b;
13972 2. when a is a negative signed integer while b is a positive one, then a
13973 is a greater unsigned integer than b; likewise when a and b's roles
13974 are flipped.
13976 So first, compare the sign of the two operands. */
13977 ret = new_loc_descr (DW_OP_over, 0, 0);
13978 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
13979 add_loc_descr (&ret, new_loc_descr (DW_OP_xor, 0, 0));
13980 /* If they have different signs (i.e. they have different sign bits), then
13981 the stack top value has now the sign bit set and thus it's smaller than
13982 zero. */
13983 add_loc_descr (&ret, new_loc_descr (DW_OP_lit0, 0, 0));
13984 add_loc_descr (&ret, new_loc_descr (DW_OP_lt, 0, 0));
13985 add_loc_descr (&ret, bra_node);
13987 /* We are in case 1. At this point, we know both operands have the same
13988 sign, to it's safe to use the built-in signed comparison. */
13989 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
13990 add_loc_descr (&ret, jmp_node);
13992 /* We are in case 2. Here, we know both operands do not have the same sign,
13993 so we have to flip the signed comparison. */
13994 flip_op = (kind == LT_EXPR || kind == LE_EXPR) ? DW_OP_gt : DW_OP_lt;
13995 tmp = new_loc_descr (flip_op, 0, 0);
13996 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13997 bra_node->dw_loc_oprnd1.v.val_loc = tmp;
13998 add_loc_descr (&ret, tmp);
14000 /* This dummy operation is necessary to make the two branches join. */
14001 tmp = new_loc_descr (DW_OP_nop, 0, 0);
14002 jmp_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14003 jmp_node->dw_loc_oprnd1.v.val_loc = tmp;
14004 add_loc_descr (&ret, tmp);
14006 return ret;
14009 /* Likewise, but takes the location description lists (might be destructive on
14010 them). Return NULL if either is NULL or if concatenation fails. */
14012 static dw_loc_list_ref
14013 loc_list_from_uint_comparison (dw_loc_list_ref left, dw_loc_list_ref right,
14014 enum tree_code kind)
14016 if (left == NULL || right == NULL)
14017 return NULL;
14019 add_loc_list (&left, right);
14020 if (left == NULL)
14021 return NULL;
14023 add_loc_descr_to_each (left, uint_comparison_loc_list (kind));
14024 return left;
14027 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
14028 without actually allocating it. */
14030 static unsigned long
14031 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
14033 return size_of_int_loc_descriptor (i >> shift)
14034 + size_of_int_loc_descriptor (shift)
14035 + 1;
14038 /* Return size_of_locs (int_loc_descriptor (i)) without
14039 actually allocating it. */
14041 static unsigned long
14042 size_of_int_loc_descriptor (HOST_WIDE_INT i)
14044 unsigned long s;
14046 if (i >= 0)
14048 int clz, ctz;
14049 if (i <= 31)
14050 return 1;
14051 else if (i <= 0xff)
14052 return 2;
14053 else if (i <= 0xffff)
14054 return 3;
14055 clz = clz_hwi (i);
14056 ctz = ctz_hwi (i);
14057 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
14058 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
14059 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
14060 - clz - 5);
14061 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
14062 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
14063 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
14064 - clz - 8);
14065 else if (DWARF2_ADDR_SIZE == 4 && i > 0x7fffffff
14066 && size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i)
14067 <= 4)
14068 return size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i);
14069 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
14070 return 5;
14071 s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
14072 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
14073 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
14074 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
14075 - clz - 8);
14076 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
14077 && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
14078 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
14079 - clz - 16);
14080 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
14081 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
14082 && s > 6)
14083 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
14084 - clz - 32);
14085 else
14086 return 1 + s;
14088 else
14090 if (i >= -0x80)
14091 return 2;
14092 else if (i >= -0x8000)
14093 return 3;
14094 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
14096 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
14098 s = size_of_int_loc_descriptor (-i) + 1;
14099 if (s < 5)
14100 return s;
14102 return 5;
14104 else
14106 unsigned long r = 1 + size_of_sleb128 (i);
14107 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
14109 s = size_of_int_loc_descriptor (-i) + 1;
14110 if (s < r)
14111 return s;
14113 return r;
14118 /* Return loc description representing "address" of integer value.
14119 This can appear only as toplevel expression. */
14121 static dw_loc_descr_ref
14122 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
14124 int litsize;
14125 dw_loc_descr_ref loc_result = NULL;
14127 if (!(dwarf_version >= 4 || !dwarf_strict))
14128 return NULL;
14130 litsize = size_of_int_loc_descriptor (i);
14131 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
14132 is more compact. For DW_OP_stack_value we need:
14133 litsize + 1 (DW_OP_stack_value)
14134 and for DW_OP_implicit_value:
14135 1 (DW_OP_implicit_value) + 1 (length) + size. */
14136 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
14138 loc_result = int_loc_descriptor (i);
14139 add_loc_descr (&loc_result,
14140 new_loc_descr (DW_OP_stack_value, 0, 0));
14141 return loc_result;
14144 loc_result = new_loc_descr (DW_OP_implicit_value,
14145 size, 0);
14146 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
14147 loc_result->dw_loc_oprnd2.v.val_int = i;
14148 return loc_result;
14151 /* Return a location descriptor that designates a base+offset location. */
14153 static dw_loc_descr_ref
14154 based_loc_descr (rtx reg, poly_int64 offset,
14155 enum var_init_status initialized)
14157 unsigned int regno;
14158 dw_loc_descr_ref result;
14159 dw_fde_ref fde = cfun->fde;
14161 /* We only use "frame base" when we're sure we're talking about the
14162 post-prologue local stack frame. We do this by *not* running
14163 register elimination until this point, and recognizing the special
14164 argument pointer and soft frame pointer rtx's. */
14165 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
14167 rtx elim = (ira_use_lra_p
14168 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
14169 : eliminate_regs (reg, VOIDmode, NULL_RTX));
14171 if (elim != reg)
14173 elim = strip_offset_and_add (elim, &offset);
14174 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
14175 && (elim == hard_frame_pointer_rtx
14176 || elim == stack_pointer_rtx))
14177 || elim == (frame_pointer_needed
14178 ? hard_frame_pointer_rtx
14179 : stack_pointer_rtx));
14181 /* If drap register is used to align stack, use frame
14182 pointer + offset to access stack variables. If stack
14183 is aligned without drap, use stack pointer + offset to
14184 access stack variables. */
14185 if (crtl->stack_realign_tried
14186 && reg == frame_pointer_rtx)
14188 int base_reg
14189 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
14190 ? HARD_FRAME_POINTER_REGNUM
14191 : REGNO (elim));
14192 return new_reg_loc_descr (base_reg, offset);
14195 gcc_assert (frame_pointer_fb_offset_valid);
14196 offset += frame_pointer_fb_offset;
14197 HOST_WIDE_INT const_offset;
14198 if (offset.is_constant (&const_offset))
14199 return new_loc_descr (DW_OP_fbreg, const_offset, 0);
14200 else
14202 dw_loc_descr_ref ret = new_loc_descr (DW_OP_fbreg, 0, 0);
14203 loc_descr_plus_const (&ret, offset);
14204 return ret;
14209 regno = REGNO (reg);
14210 #ifdef LEAF_REG_REMAP
14211 if (crtl->uses_only_leaf_regs)
14213 int leaf_reg = LEAF_REG_REMAP (regno);
14214 if (leaf_reg != -1)
14215 regno = (unsigned) leaf_reg;
14217 #endif
14218 regno = DWARF_FRAME_REGNUM (regno);
14220 HOST_WIDE_INT const_offset;
14221 if (!optimize && fde
14222 && (fde->drap_reg == regno || fde->vdrap_reg == regno)
14223 && offset.is_constant (&const_offset))
14225 /* Use cfa+offset to represent the location of arguments passed
14226 on the stack when drap is used to align stack.
14227 Only do this when not optimizing, for optimized code var-tracking
14228 is supposed to track where the arguments live and the register
14229 used as vdrap or drap in some spot might be used for something
14230 else in other part of the routine. */
14231 return new_loc_descr (DW_OP_fbreg, const_offset, 0);
14234 result = new_reg_loc_descr (regno, offset);
14236 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
14237 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14239 return result;
14242 /* Return true if this RTL expression describes a base+offset calculation. */
14244 static inline int
14245 is_based_loc (const_rtx rtl)
14247 return (GET_CODE (rtl) == PLUS
14248 && ((REG_P (XEXP (rtl, 0))
14249 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
14250 && CONST_INT_P (XEXP (rtl, 1)))));
14253 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
14254 failed. */
14256 static dw_loc_descr_ref
14257 tls_mem_loc_descriptor (rtx mem)
14259 tree base;
14260 dw_loc_descr_ref loc_result;
14262 if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
14263 return NULL;
14265 base = get_base_address (MEM_EXPR (mem));
14266 if (base == NULL
14267 || !VAR_P (base)
14268 || !DECL_THREAD_LOCAL_P (base))
14269 return NULL;
14271 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1, NULL);
14272 if (loc_result == NULL)
14273 return NULL;
14275 if (maybe_ne (MEM_OFFSET (mem), 0))
14276 loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
14278 return loc_result;
14281 /* Output debug info about reason why we failed to expand expression as dwarf
14282 expression. */
14284 static void
14285 expansion_failed (tree expr, rtx rtl, char const *reason)
14287 if (dump_file && (dump_flags & TDF_DETAILS))
14289 fprintf (dump_file, "Failed to expand as dwarf: ");
14290 if (expr)
14291 print_generic_expr (dump_file, expr, dump_flags);
14292 if (rtl)
14294 fprintf (dump_file, "\n");
14295 print_rtl (dump_file, rtl);
14297 fprintf (dump_file, "\nReason: %s\n", reason);
14301 /* Helper function for const_ok_for_output. */
14303 static bool
14304 const_ok_for_output_1 (rtx rtl)
14306 if (targetm.const_not_ok_for_debug_p (rtl))
14308 if (GET_CODE (rtl) != UNSPEC)
14310 expansion_failed (NULL_TREE, rtl,
14311 "Expression rejected for debug by the backend.\n");
14312 return false;
14315 /* If delegitimize_address couldn't do anything with the UNSPEC, and
14316 the target hook doesn't explicitly allow it in debug info, assume
14317 we can't express it in the debug info. */
14318 /* Don't complain about TLS UNSPECs, those are just too hard to
14319 delegitimize. Note this could be a non-decl SYMBOL_REF such as
14320 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
14321 rather than DECL_THREAD_LOCAL_P is not just an optimization. */
14322 if (flag_checking
14323 && (XVECLEN (rtl, 0) == 0
14324 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
14325 || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE))
14326 inform (current_function_decl
14327 ? DECL_SOURCE_LOCATION (current_function_decl)
14328 : UNKNOWN_LOCATION,
14329 #if NUM_UNSPEC_VALUES > 0
14330 "non-delegitimized UNSPEC %s (%d) found in variable location",
14331 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
14332 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
14333 XINT (rtl, 1));
14334 #else
14335 "non-delegitimized UNSPEC %d found in variable location",
14336 XINT (rtl, 1));
14337 #endif
14338 expansion_failed (NULL_TREE, rtl,
14339 "UNSPEC hasn't been delegitimized.\n");
14340 return false;
14343 if (CONST_POLY_INT_P (rtl))
14344 return false;
14346 if (targetm.const_not_ok_for_debug_p (rtl))
14348 expansion_failed (NULL_TREE, rtl,
14349 "Expression rejected for debug by the backend.\n");
14350 return false;
14353 /* FIXME: Refer to PR60655. It is possible for simplification
14354 of rtl expressions in var tracking to produce such expressions.
14355 We should really identify / validate expressions
14356 enclosed in CONST that can be handled by assemblers on various
14357 targets and only handle legitimate cases here. */
14358 switch (GET_CODE (rtl))
14360 case SYMBOL_REF:
14361 break;
14362 case NOT:
14363 case NEG:
14364 return false;
14365 default:
14366 return true;
14369 if (CONSTANT_POOL_ADDRESS_P (rtl))
14371 bool marked;
14372 get_pool_constant_mark (rtl, &marked);
14373 /* If all references to this pool constant were optimized away,
14374 it was not output and thus we can't represent it. */
14375 if (!marked)
14377 expansion_failed (NULL_TREE, rtl,
14378 "Constant was removed from constant pool.\n");
14379 return false;
14383 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
14384 return false;
14386 /* Avoid references to external symbols in debug info, on several targets
14387 the linker might even refuse to link when linking a shared library,
14388 and in many other cases the relocations for .debug_info/.debug_loc are
14389 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
14390 to be defined within the same shared library or executable are fine. */
14391 if (SYMBOL_REF_EXTERNAL_P (rtl))
14393 tree decl = SYMBOL_REF_DECL (rtl);
14395 if (decl == NULL || !targetm.binds_local_p (decl))
14397 expansion_failed (NULL_TREE, rtl,
14398 "Symbol not defined in current TU.\n");
14399 return false;
14403 return true;
14406 /* Return true if constant RTL can be emitted in DW_OP_addr or
14407 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
14408 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
14410 static bool
14411 const_ok_for_output (rtx rtl)
14413 if (GET_CODE (rtl) == SYMBOL_REF)
14414 return const_ok_for_output_1 (rtl);
14416 if (GET_CODE (rtl) == CONST)
14418 subrtx_var_iterator::array_type array;
14419 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 0), ALL)
14420 if (!const_ok_for_output_1 (*iter))
14421 return false;
14422 return true;
14425 return true;
14428 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
14429 if possible, NULL otherwise. */
14431 static dw_die_ref
14432 base_type_for_mode (machine_mode mode, bool unsignedp)
14434 dw_die_ref type_die;
14435 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
14437 if (type == NULL)
14438 return NULL;
14439 switch (TREE_CODE (type))
14441 case INTEGER_TYPE:
14442 case REAL_TYPE:
14443 break;
14444 default:
14445 return NULL;
14447 type_die = lookup_type_die (type);
14448 if (!type_die)
14449 type_die = modified_type_die (type, TYPE_UNQUALIFIED, false,
14450 comp_unit_die ());
14451 if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
14452 return NULL;
14453 return type_die;
14456 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
14457 type matching MODE, or, if MODE is narrower than or as wide as
14458 DWARF2_ADDR_SIZE, untyped. Return NULL if the conversion is not
14459 possible. */
14461 static dw_loc_descr_ref
14462 convert_descriptor_to_mode (scalar_int_mode mode, dw_loc_descr_ref op)
14464 machine_mode outer_mode = mode;
14465 dw_die_ref type_die;
14466 dw_loc_descr_ref cvt;
14468 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
14470 add_loc_descr (&op, new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0));
14471 return op;
14473 type_die = base_type_for_mode (outer_mode, 1);
14474 if (type_die == NULL)
14475 return NULL;
14476 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14477 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14478 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14479 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14480 add_loc_descr (&op, cvt);
14481 return op;
14484 /* Return location descriptor for comparison OP with operands OP0 and OP1. */
14486 static dw_loc_descr_ref
14487 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
14488 dw_loc_descr_ref op1)
14490 dw_loc_descr_ref ret = op0;
14491 add_loc_descr (&ret, op1);
14492 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
14493 if (STORE_FLAG_VALUE != 1)
14495 add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
14496 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
14498 return ret;
14501 /* Subroutine of scompare_loc_descriptor for the case in which we're
14502 comparing two scalar integer operands OP0 and OP1 that have mode OP_MODE,
14503 and in which OP_MODE is bigger than DWARF2_ADDR_SIZE. */
14505 static dw_loc_descr_ref
14506 scompare_loc_descriptor_wide (enum dwarf_location_atom op,
14507 scalar_int_mode op_mode,
14508 dw_loc_descr_ref op0, dw_loc_descr_ref op1)
14510 dw_die_ref type_die = base_type_for_mode (op_mode, 0);
14511 dw_loc_descr_ref cvt;
14513 if (type_die == NULL)
14514 return NULL;
14515 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14516 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14517 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14518 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14519 add_loc_descr (&op0, cvt);
14520 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14521 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14522 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14523 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14524 add_loc_descr (&op1, cvt);
14525 return compare_loc_descriptor (op, op0, op1);
14528 /* Subroutine of scompare_loc_descriptor for the case in which we're
14529 comparing two scalar integer operands OP0 and OP1 that have mode OP_MODE,
14530 and in which OP_MODE is smaller than DWARF2_ADDR_SIZE. */
14532 static dw_loc_descr_ref
14533 scompare_loc_descriptor_narrow (enum dwarf_location_atom op, rtx rtl,
14534 scalar_int_mode op_mode,
14535 dw_loc_descr_ref op0, dw_loc_descr_ref op1)
14537 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
14538 /* For eq/ne, if the operands are known to be zero-extended,
14539 there is no need to do the fancy shifting up. */
14540 if (op == DW_OP_eq || op == DW_OP_ne)
14542 dw_loc_descr_ref last0, last1;
14543 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
14545 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
14547 /* deref_size zero extends, and for constants we can check
14548 whether they are zero extended or not. */
14549 if (((last0->dw_loc_opc == DW_OP_deref_size
14550 && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
14551 || (CONST_INT_P (XEXP (rtl, 0))
14552 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
14553 == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
14554 && ((last1->dw_loc_opc == DW_OP_deref_size
14555 && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
14556 || (CONST_INT_P (XEXP (rtl, 1))
14557 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
14558 == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
14559 return compare_loc_descriptor (op, op0, op1);
14561 /* EQ/NE comparison against constant in narrower type than
14562 DWARF2_ADDR_SIZE can be performed either as
14563 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
14564 DW_OP_{eq,ne}
14566 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
14567 DW_OP_{eq,ne}. Pick whatever is shorter. */
14568 if (CONST_INT_P (XEXP (rtl, 1))
14569 && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
14570 && (size_of_int_loc_descriptor (shift) + 1
14571 + size_of_int_loc_descriptor (UINTVAL (XEXP (rtl, 1)) << shift)
14572 >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
14573 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
14574 & GET_MODE_MASK (op_mode))))
14576 add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
14577 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14578 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
14579 & GET_MODE_MASK (op_mode));
14580 return compare_loc_descriptor (op, op0, op1);
14583 add_loc_descr (&op0, int_loc_descriptor (shift));
14584 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
14585 if (CONST_INT_P (XEXP (rtl, 1)))
14586 op1 = int_loc_descriptor (UINTVAL (XEXP (rtl, 1)) << shift);
14587 else
14589 add_loc_descr (&op1, int_loc_descriptor (shift));
14590 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
14592 return compare_loc_descriptor (op, op0, op1);
14595 /* Return location descriptor for unsigned comparison OP RTL. */
14597 static dw_loc_descr_ref
14598 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
14599 machine_mode mem_mode)
14601 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
14602 dw_loc_descr_ref op0, op1;
14604 if (op_mode == VOIDmode)
14605 op_mode = GET_MODE (XEXP (rtl, 1));
14606 if (op_mode == VOIDmode)
14607 return NULL;
14609 scalar_int_mode int_op_mode;
14610 if (dwarf_strict
14611 && dwarf_version < 5
14612 && (!is_a <scalar_int_mode> (op_mode, &int_op_mode)
14613 || GET_MODE_SIZE (int_op_mode) > DWARF2_ADDR_SIZE))
14614 return NULL;
14616 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
14617 VAR_INIT_STATUS_INITIALIZED);
14618 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
14619 VAR_INIT_STATUS_INITIALIZED);
14621 if (op0 == NULL || op1 == NULL)
14622 return NULL;
14624 if (is_a <scalar_int_mode> (op_mode, &int_op_mode))
14626 if (GET_MODE_SIZE (int_op_mode) < DWARF2_ADDR_SIZE)
14627 return scompare_loc_descriptor_narrow (op, rtl, int_op_mode, op0, op1);
14629 if (GET_MODE_SIZE (int_op_mode) > DWARF2_ADDR_SIZE)
14630 return scompare_loc_descriptor_wide (op, int_op_mode, op0, op1);
14632 return compare_loc_descriptor (op, op0, op1);
14635 /* Return location descriptor for unsigned comparison OP RTL. */
14637 static dw_loc_descr_ref
14638 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
14639 machine_mode mem_mode)
14641 dw_loc_descr_ref op0, op1;
14643 machine_mode test_op_mode = GET_MODE (XEXP (rtl, 0));
14644 if (test_op_mode == VOIDmode)
14645 test_op_mode = GET_MODE (XEXP (rtl, 1));
14647 scalar_int_mode op_mode;
14648 if (!is_a <scalar_int_mode> (test_op_mode, &op_mode))
14649 return NULL;
14651 if (dwarf_strict
14652 && dwarf_version < 5
14653 && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
14654 return NULL;
14656 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
14657 VAR_INIT_STATUS_INITIALIZED);
14658 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
14659 VAR_INIT_STATUS_INITIALIZED);
14661 if (op0 == NULL || op1 == NULL)
14662 return NULL;
14664 if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
14666 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
14667 dw_loc_descr_ref last0, last1;
14668 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
14670 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
14672 if (CONST_INT_P (XEXP (rtl, 0)))
14673 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
14674 /* deref_size zero extends, so no need to mask it again. */
14675 else if (last0->dw_loc_opc != DW_OP_deref_size
14676 || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
14678 add_loc_descr (&op0, int_loc_descriptor (mask));
14679 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14681 if (CONST_INT_P (XEXP (rtl, 1)))
14682 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
14683 /* deref_size zero extends, so no need to mask it again. */
14684 else if (last1->dw_loc_opc != DW_OP_deref_size
14685 || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
14687 add_loc_descr (&op1, int_loc_descriptor (mask));
14688 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
14691 else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
14693 HOST_WIDE_INT bias = 1;
14694 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
14695 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14696 if (CONST_INT_P (XEXP (rtl, 1)))
14697 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
14698 + INTVAL (XEXP (rtl, 1)));
14699 else
14700 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
14701 bias, 0));
14703 return compare_loc_descriptor (op, op0, op1);
14706 /* Return location descriptor for {U,S}{MIN,MAX}. */
14708 static dw_loc_descr_ref
14709 minmax_loc_descriptor (rtx rtl, machine_mode mode,
14710 machine_mode mem_mode)
14712 enum dwarf_location_atom op;
14713 dw_loc_descr_ref op0, op1, ret;
14714 dw_loc_descr_ref bra_node, drop_node;
14716 scalar_int_mode int_mode;
14717 if (dwarf_strict
14718 && dwarf_version < 5
14719 && (!is_a <scalar_int_mode> (mode, &int_mode)
14720 || GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE))
14721 return NULL;
14723 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14724 VAR_INIT_STATUS_INITIALIZED);
14725 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14726 VAR_INIT_STATUS_INITIALIZED);
14728 if (op0 == NULL || op1 == NULL)
14729 return NULL;
14731 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
14732 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
14733 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
14734 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
14736 /* Checked by the caller. */
14737 int_mode = as_a <scalar_int_mode> (mode);
14738 if (GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE)
14740 HOST_WIDE_INT mask = GET_MODE_MASK (int_mode);
14741 add_loc_descr (&op0, int_loc_descriptor (mask));
14742 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14743 add_loc_descr (&op1, int_loc_descriptor (mask));
14744 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
14746 else if (GET_MODE_SIZE (int_mode) == DWARF2_ADDR_SIZE)
14748 HOST_WIDE_INT bias = 1;
14749 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
14750 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14751 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14754 else if (is_a <scalar_int_mode> (mode, &int_mode)
14755 && GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE)
14757 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (int_mode)) * BITS_PER_UNIT;
14758 add_loc_descr (&op0, int_loc_descriptor (shift));
14759 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
14760 add_loc_descr (&op1, int_loc_descriptor (shift));
14761 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
14763 else if (is_a <scalar_int_mode> (mode, &int_mode)
14764 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
14766 dw_die_ref type_die = base_type_for_mode (int_mode, 0);
14767 dw_loc_descr_ref cvt;
14768 if (type_die == NULL)
14769 return NULL;
14770 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14771 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14772 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14773 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14774 add_loc_descr (&op0, cvt);
14775 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14776 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14777 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14778 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14779 add_loc_descr (&op1, cvt);
14782 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
14783 op = DW_OP_lt;
14784 else
14785 op = DW_OP_gt;
14786 ret = op0;
14787 add_loc_descr (&ret, op1);
14788 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
14789 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14790 add_loc_descr (&ret, bra_node);
14791 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14792 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
14793 add_loc_descr (&ret, drop_node);
14794 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14795 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
14796 if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
14797 && is_a <scalar_int_mode> (mode, &int_mode)
14798 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
14799 ret = convert_descriptor_to_mode (int_mode, ret);
14800 return ret;
14803 /* Helper function for mem_loc_descriptor. Perform OP binary op,
14804 but after converting arguments to type_die, afterwards
14805 convert back to unsigned. */
14807 static dw_loc_descr_ref
14808 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
14809 scalar_int_mode mode, machine_mode mem_mode)
14811 dw_loc_descr_ref cvt, op0, op1;
14813 if (type_die == NULL)
14814 return NULL;
14815 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14816 VAR_INIT_STATUS_INITIALIZED);
14817 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14818 VAR_INIT_STATUS_INITIALIZED);
14819 if (op0 == NULL || op1 == NULL)
14820 return NULL;
14821 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14822 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14823 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14824 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14825 add_loc_descr (&op0, cvt);
14826 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14827 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14828 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14829 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14830 add_loc_descr (&op1, cvt);
14831 add_loc_descr (&op0, op1);
14832 add_loc_descr (&op0, new_loc_descr (op, 0, 0));
14833 return convert_descriptor_to_mode (mode, op0);
14836 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
14837 const0 is DW_OP_lit0 or corresponding typed constant,
14838 const1 is DW_OP_lit1 or corresponding typed constant
14839 and constMSB is constant with just the MSB bit set
14840 for the mode):
14841 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
14842 L1: const0 DW_OP_swap
14843 L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
14844 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
14845 L3: DW_OP_drop
14846 L4: DW_OP_nop
14848 CTZ is similar:
14849 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
14850 L1: const0 DW_OP_swap
14851 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
14852 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
14853 L3: DW_OP_drop
14854 L4: DW_OP_nop
14856 FFS is similar:
14857 DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
14858 L1: const1 DW_OP_swap
14859 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
14860 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
14861 L3: DW_OP_drop
14862 L4: DW_OP_nop */
14864 static dw_loc_descr_ref
14865 clz_loc_descriptor (rtx rtl, scalar_int_mode mode,
14866 machine_mode mem_mode)
14868 dw_loc_descr_ref op0, ret, tmp;
14869 HOST_WIDE_INT valv;
14870 dw_loc_descr_ref l1jump, l1label;
14871 dw_loc_descr_ref l2jump, l2label;
14872 dw_loc_descr_ref l3jump, l3label;
14873 dw_loc_descr_ref l4jump, l4label;
14874 rtx msb;
14876 if (GET_MODE (XEXP (rtl, 0)) != mode)
14877 return NULL;
14879 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14880 VAR_INIT_STATUS_INITIALIZED);
14881 if (op0 == NULL)
14882 return NULL;
14883 ret = op0;
14884 if (GET_CODE (rtl) == CLZ)
14886 if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
14887 valv = GET_MODE_BITSIZE (mode);
14889 else if (GET_CODE (rtl) == FFS)
14890 valv = 0;
14891 else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
14892 valv = GET_MODE_BITSIZE (mode);
14893 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
14894 l1jump = new_loc_descr (DW_OP_bra, 0, 0);
14895 add_loc_descr (&ret, l1jump);
14896 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
14897 tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
14898 VAR_INIT_STATUS_INITIALIZED);
14899 if (tmp == NULL)
14900 return NULL;
14901 add_loc_descr (&ret, tmp);
14902 l4jump = new_loc_descr (DW_OP_skip, 0, 0);
14903 add_loc_descr (&ret, l4jump);
14904 l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
14905 ? const1_rtx : const0_rtx,
14906 mode, mem_mode,
14907 VAR_INIT_STATUS_INITIALIZED);
14908 if (l1label == NULL)
14909 return NULL;
14910 add_loc_descr (&ret, l1label);
14911 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14912 l2label = new_loc_descr (DW_OP_dup, 0, 0);
14913 add_loc_descr (&ret, l2label);
14914 if (GET_CODE (rtl) != CLZ)
14915 msb = const1_rtx;
14916 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
14917 msb = GEN_INT (HOST_WIDE_INT_1U
14918 << (GET_MODE_BITSIZE (mode) - 1));
14919 else
14920 msb = immed_wide_int_const
14921 (wi::set_bit_in_zero (GET_MODE_PRECISION (mode) - 1,
14922 GET_MODE_PRECISION (mode)), mode);
14923 if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
14924 tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
14925 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
14926 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
14927 else
14928 tmp = mem_loc_descriptor (msb, mode, mem_mode,
14929 VAR_INIT_STATUS_INITIALIZED);
14930 if (tmp == NULL)
14931 return NULL;
14932 add_loc_descr (&ret, tmp);
14933 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
14934 l3jump = new_loc_descr (DW_OP_bra, 0, 0);
14935 add_loc_descr (&ret, l3jump);
14936 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
14937 VAR_INIT_STATUS_INITIALIZED);
14938 if (tmp == NULL)
14939 return NULL;
14940 add_loc_descr (&ret, tmp);
14941 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
14942 ? DW_OP_shl : DW_OP_shr, 0, 0));
14943 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14944 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
14945 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14946 l2jump = new_loc_descr (DW_OP_skip, 0, 0);
14947 add_loc_descr (&ret, l2jump);
14948 l3label = new_loc_descr (DW_OP_drop, 0, 0);
14949 add_loc_descr (&ret, l3label);
14950 l4label = new_loc_descr (DW_OP_nop, 0, 0);
14951 add_loc_descr (&ret, l4label);
14952 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14953 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
14954 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14955 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
14956 l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14957 l3jump->dw_loc_oprnd1.v.val_loc = l3label;
14958 l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14959 l4jump->dw_loc_oprnd1.v.val_loc = l4label;
14960 return ret;
14963 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
14964 const1 is DW_OP_lit1 or corresponding typed constant):
14965 const0 DW_OP_swap
14966 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
14967 DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
14968 L2: DW_OP_drop
14970 PARITY is similar:
14971 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
14972 DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
14973 L2: DW_OP_drop */
14975 static dw_loc_descr_ref
14976 popcount_loc_descriptor (rtx rtl, scalar_int_mode mode,
14977 machine_mode mem_mode)
14979 dw_loc_descr_ref op0, ret, tmp;
14980 dw_loc_descr_ref l1jump, l1label;
14981 dw_loc_descr_ref l2jump, l2label;
14983 if (GET_MODE (XEXP (rtl, 0)) != mode)
14984 return NULL;
14986 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14987 VAR_INIT_STATUS_INITIALIZED);
14988 if (op0 == NULL)
14989 return NULL;
14990 ret = op0;
14991 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
14992 VAR_INIT_STATUS_INITIALIZED);
14993 if (tmp == NULL)
14994 return NULL;
14995 add_loc_descr (&ret, tmp);
14996 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14997 l1label = new_loc_descr (DW_OP_dup, 0, 0);
14998 add_loc_descr (&ret, l1label);
14999 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
15000 add_loc_descr (&ret, l2jump);
15001 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
15002 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
15003 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
15004 VAR_INIT_STATUS_INITIALIZED);
15005 if (tmp == NULL)
15006 return NULL;
15007 add_loc_descr (&ret, tmp);
15008 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
15009 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
15010 ? DW_OP_plus : DW_OP_xor, 0, 0));
15011 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15012 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
15013 VAR_INIT_STATUS_INITIALIZED);
15014 add_loc_descr (&ret, tmp);
15015 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
15016 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
15017 add_loc_descr (&ret, l1jump);
15018 l2label = new_loc_descr (DW_OP_drop, 0, 0);
15019 add_loc_descr (&ret, l2label);
15020 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15021 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
15022 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15023 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
15024 return ret;
15027 /* BSWAP (constS is initial shift count, either 56 or 24):
15028 constS const0
15029 L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
15030 const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
15031 DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
15032 DW_OP_minus DW_OP_swap DW_OP_skip <L1>
15033 L2: DW_OP_drop DW_OP_swap DW_OP_drop */
15035 static dw_loc_descr_ref
15036 bswap_loc_descriptor (rtx rtl, scalar_int_mode mode,
15037 machine_mode mem_mode)
15039 dw_loc_descr_ref op0, ret, tmp;
15040 dw_loc_descr_ref l1jump, l1label;
15041 dw_loc_descr_ref l2jump, l2label;
15043 if (BITS_PER_UNIT != 8
15044 || (GET_MODE_BITSIZE (mode) != 32
15045 && GET_MODE_BITSIZE (mode) != 64))
15046 return NULL;
15048 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15049 VAR_INIT_STATUS_INITIALIZED);
15050 if (op0 == NULL)
15051 return NULL;
15053 ret = op0;
15054 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
15055 mode, mem_mode,
15056 VAR_INIT_STATUS_INITIALIZED);
15057 if (tmp == NULL)
15058 return NULL;
15059 add_loc_descr (&ret, tmp);
15060 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
15061 VAR_INIT_STATUS_INITIALIZED);
15062 if (tmp == NULL)
15063 return NULL;
15064 add_loc_descr (&ret, tmp);
15065 l1label = new_loc_descr (DW_OP_pick, 2, 0);
15066 add_loc_descr (&ret, l1label);
15067 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
15068 mode, mem_mode,
15069 VAR_INIT_STATUS_INITIALIZED);
15070 add_loc_descr (&ret, tmp);
15071 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
15072 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
15073 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
15074 tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
15075 VAR_INIT_STATUS_INITIALIZED);
15076 if (tmp == NULL)
15077 return NULL;
15078 add_loc_descr (&ret, tmp);
15079 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
15080 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
15081 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
15082 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
15083 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15084 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
15085 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
15086 VAR_INIT_STATUS_INITIALIZED);
15087 add_loc_descr (&ret, tmp);
15088 add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
15089 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
15090 add_loc_descr (&ret, l2jump);
15091 tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
15092 VAR_INIT_STATUS_INITIALIZED);
15093 add_loc_descr (&ret, tmp);
15094 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
15095 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15096 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
15097 add_loc_descr (&ret, l1jump);
15098 l2label = new_loc_descr (DW_OP_drop, 0, 0);
15099 add_loc_descr (&ret, l2label);
15100 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15101 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
15102 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15103 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
15104 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15105 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
15106 return ret;
15109 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
15110 DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
15111 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
15112 DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
15114 ROTATERT is similar:
15115 DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
15116 DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
15117 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or */
15119 static dw_loc_descr_ref
15120 rotate_loc_descriptor (rtx rtl, scalar_int_mode mode,
15121 machine_mode mem_mode)
15123 rtx rtlop1 = XEXP (rtl, 1);
15124 dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
15125 int i;
15127 if (is_narrower_int_mode (GET_MODE (rtlop1), mode))
15128 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
15129 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15130 VAR_INIT_STATUS_INITIALIZED);
15131 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
15132 VAR_INIT_STATUS_INITIALIZED);
15133 if (op0 == NULL || op1 == NULL)
15134 return NULL;
15135 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
15136 for (i = 0; i < 2; i++)
15138 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
15139 mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
15140 mode, mem_mode,
15141 VAR_INIT_STATUS_INITIALIZED);
15142 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
15143 mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
15144 ? DW_OP_const4u
15145 : HOST_BITS_PER_WIDE_INT == 64
15146 ? DW_OP_const8u : DW_OP_constu,
15147 GET_MODE_MASK (mode), 0);
15148 else
15149 mask[i] = NULL;
15150 if (mask[i] == NULL)
15151 return NULL;
15152 add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
15154 ret = op0;
15155 add_loc_descr (&ret, op1);
15156 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
15157 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
15158 if (GET_CODE (rtl) == ROTATERT)
15160 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
15161 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
15162 GET_MODE_BITSIZE (mode), 0));
15164 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
15165 if (mask[0] != NULL)
15166 add_loc_descr (&ret, mask[0]);
15167 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
15168 if (mask[1] != NULL)
15170 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15171 add_loc_descr (&ret, mask[1]);
15172 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15174 if (GET_CODE (rtl) == ROTATE)
15176 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
15177 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
15178 GET_MODE_BITSIZE (mode), 0));
15180 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
15181 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
15182 return ret;
15185 /* Helper function for mem_loc_descriptor. Return DW_OP_GNU_parameter_ref
15186 for DEBUG_PARAMETER_REF RTL. */
15188 static dw_loc_descr_ref
15189 parameter_ref_descriptor (rtx rtl)
15191 dw_loc_descr_ref ret;
15192 dw_die_ref ref;
15194 if (dwarf_strict)
15195 return NULL;
15196 gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
15197 /* With LTO during LTRANS we get the late DIE that refers to the early
15198 DIE, thus we add another indirection here. This seems to confuse
15199 gdb enough to make gcc.dg/guality/pr68860-1.c FAIL with LTO. */
15200 ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
15201 ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
15202 if (ref)
15204 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15205 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
15206 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
15208 else
15210 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
15211 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
15213 return ret;
15216 /* The following routine converts the RTL for a variable or parameter
15217 (resident in memory) into an equivalent Dwarf representation of a
15218 mechanism for getting the address of that same variable onto the top of a
15219 hypothetical "address evaluation" stack.
15221 When creating memory location descriptors, we are effectively transforming
15222 the RTL for a memory-resident object into its Dwarf postfix expression
15223 equivalent. This routine recursively descends an RTL tree, turning
15224 it into Dwarf postfix code as it goes.
15226 MODE is the mode that should be assumed for the rtl if it is VOIDmode.
15228 MEM_MODE is the mode of the memory reference, needed to handle some
15229 autoincrement addressing modes.
15231 Return 0 if we can't represent the location. */
15233 dw_loc_descr_ref
15234 mem_loc_descriptor (rtx rtl, machine_mode mode,
15235 machine_mode mem_mode,
15236 enum var_init_status initialized)
15238 dw_loc_descr_ref mem_loc_result = NULL;
15239 enum dwarf_location_atom op;
15240 dw_loc_descr_ref op0, op1;
15241 rtx inner = NULL_RTX;
15242 poly_int64 offset;
15244 if (mode == VOIDmode)
15245 mode = GET_MODE (rtl);
15247 /* Note that for a dynamically sized array, the location we will generate a
15248 description of here will be the lowest numbered location which is
15249 actually within the array. That's *not* necessarily the same as the
15250 zeroth element of the array. */
15252 rtl = targetm.delegitimize_address (rtl);
15254 if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
15255 return NULL;
15257 scalar_int_mode int_mode, inner_mode, op1_mode;
15258 switch (GET_CODE (rtl))
15260 case POST_INC:
15261 case POST_DEC:
15262 case POST_MODIFY:
15263 return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
15265 case SUBREG:
15266 /* The case of a subreg may arise when we have a local (register)
15267 variable or a formal (register) parameter which doesn't quite fill
15268 up an entire register. For now, just assume that it is
15269 legitimate to make the Dwarf info refer to the whole register which
15270 contains the given subreg. */
15271 if (!subreg_lowpart_p (rtl))
15272 break;
15273 inner = SUBREG_REG (rtl);
15274 /* FALLTHRU */
15275 case TRUNCATE:
15276 if (inner == NULL_RTX)
15277 inner = XEXP (rtl, 0);
15278 if (is_a <scalar_int_mode> (mode, &int_mode)
15279 && is_a <scalar_int_mode> (GET_MODE (inner), &inner_mode)
15280 && (GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
15281 #ifdef POINTERS_EXTEND_UNSIGNED
15282 || (int_mode == Pmode && mem_mode != VOIDmode)
15283 #endif
15285 && GET_MODE_SIZE (inner_mode) <= DWARF2_ADDR_SIZE)
15287 mem_loc_result = mem_loc_descriptor (inner,
15288 inner_mode,
15289 mem_mode, initialized);
15290 break;
15292 if (dwarf_strict && dwarf_version < 5)
15293 break;
15294 if (is_a <scalar_int_mode> (mode, &int_mode)
15295 && is_a <scalar_int_mode> (GET_MODE (inner), &inner_mode)
15296 ? GET_MODE_SIZE (int_mode) <= GET_MODE_SIZE (inner_mode)
15297 : known_eq (GET_MODE_SIZE (mode), GET_MODE_SIZE (GET_MODE (inner))))
15299 dw_die_ref type_die;
15300 dw_loc_descr_ref cvt;
15302 mem_loc_result = mem_loc_descriptor (inner,
15303 GET_MODE (inner),
15304 mem_mode, initialized);
15305 if (mem_loc_result == NULL)
15306 break;
15307 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
15308 if (type_die == NULL)
15310 mem_loc_result = NULL;
15311 break;
15313 if (maybe_ne (GET_MODE_SIZE (mode), GET_MODE_SIZE (GET_MODE (inner))))
15314 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15315 else
15316 cvt = new_loc_descr (dwarf_OP (DW_OP_reinterpret), 0, 0);
15317 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15318 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15319 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15320 add_loc_descr (&mem_loc_result, cvt);
15321 if (is_a <scalar_int_mode> (mode, &int_mode)
15322 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE)
15324 /* Convert it to untyped afterwards. */
15325 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15326 add_loc_descr (&mem_loc_result, cvt);
15329 break;
15331 case REG:
15332 if (!is_a <scalar_int_mode> (mode, &int_mode)
15333 || (GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE
15334 && rtl != arg_pointer_rtx
15335 && rtl != frame_pointer_rtx
15336 #ifdef POINTERS_EXTEND_UNSIGNED
15337 && (int_mode != Pmode || mem_mode == VOIDmode)
15338 #endif
15341 dw_die_ref type_die;
15342 unsigned int dbx_regnum;
15344 if (dwarf_strict && dwarf_version < 5)
15345 break;
15346 if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
15347 break;
15348 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
15349 if (type_die == NULL)
15350 break;
15352 dbx_regnum = dbx_reg_number (rtl);
15353 if (dbx_regnum == IGNORED_DWARF_REGNUM)
15354 break;
15355 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_regval_type),
15356 dbx_regnum, 0);
15357 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
15358 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
15359 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
15360 break;
15362 /* Whenever a register number forms a part of the description of the
15363 method for calculating the (dynamic) address of a memory resident
15364 object, DWARF rules require the register number be referred to as
15365 a "base register". This distinction is not based in any way upon
15366 what category of register the hardware believes the given register
15367 belongs to. This is strictly DWARF terminology we're dealing with
15368 here. Note that in cases where the location of a memory-resident
15369 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
15370 OP_CONST (0)) the actual DWARF location descriptor that we generate
15371 may just be OP_BASEREG (basereg). This may look deceptively like
15372 the object in question was allocated to a register (rather than in
15373 memory) so DWARF consumers need to be aware of the subtle
15374 distinction between OP_REG and OP_BASEREG. */
15375 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
15376 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
15377 else if (stack_realign_drap
15378 && crtl->drap_reg
15379 && crtl->args.internal_arg_pointer == rtl
15380 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
15382 /* If RTL is internal_arg_pointer, which has been optimized
15383 out, use DRAP instead. */
15384 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
15385 VAR_INIT_STATUS_INITIALIZED);
15387 break;
15389 case SIGN_EXTEND:
15390 case ZERO_EXTEND:
15391 if (!is_a <scalar_int_mode> (mode, &int_mode)
15392 || !is_a <scalar_int_mode> (GET_MODE (XEXP (rtl, 0)), &inner_mode))
15393 break;
15394 op0 = mem_loc_descriptor (XEXP (rtl, 0), inner_mode,
15395 mem_mode, VAR_INIT_STATUS_INITIALIZED);
15396 if (op0 == 0)
15397 break;
15398 else if (GET_CODE (rtl) == ZERO_EXTEND
15399 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
15400 && GET_MODE_BITSIZE (inner_mode) < HOST_BITS_PER_WIDE_INT
15401 /* If DW_OP_const{1,2,4}u won't be used, it is shorter
15402 to expand zero extend as two shifts instead of
15403 masking. */
15404 && GET_MODE_SIZE (inner_mode) <= 4)
15406 mem_loc_result = op0;
15407 add_loc_descr (&mem_loc_result,
15408 int_loc_descriptor (GET_MODE_MASK (inner_mode)));
15409 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
15411 else if (GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE)
15413 int shift = DWARF2_ADDR_SIZE - GET_MODE_SIZE (inner_mode);
15414 shift *= BITS_PER_UNIT;
15415 if (GET_CODE (rtl) == SIGN_EXTEND)
15416 op = DW_OP_shra;
15417 else
15418 op = DW_OP_shr;
15419 mem_loc_result = op0;
15420 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
15421 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
15422 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
15423 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15425 else if (!dwarf_strict || dwarf_version >= 5)
15427 dw_die_ref type_die1, type_die2;
15428 dw_loc_descr_ref cvt;
15430 type_die1 = base_type_for_mode (inner_mode,
15431 GET_CODE (rtl) == ZERO_EXTEND);
15432 if (type_die1 == NULL)
15433 break;
15434 type_die2 = base_type_for_mode (int_mode, 1);
15435 if (type_die2 == NULL)
15436 break;
15437 mem_loc_result = op0;
15438 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15439 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15440 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
15441 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15442 add_loc_descr (&mem_loc_result, cvt);
15443 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15444 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15445 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
15446 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15447 add_loc_descr (&mem_loc_result, cvt);
15449 break;
15451 case MEM:
15453 rtx new_rtl = avoid_constant_pool_reference (rtl);
15454 if (new_rtl != rtl)
15456 mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
15457 initialized);
15458 if (mem_loc_result != NULL)
15459 return mem_loc_result;
15462 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
15463 get_address_mode (rtl), mode,
15464 VAR_INIT_STATUS_INITIALIZED);
15465 if (mem_loc_result == NULL)
15466 mem_loc_result = tls_mem_loc_descriptor (rtl);
15467 if (mem_loc_result != NULL)
15469 if (!is_a <scalar_int_mode> (mode, &int_mode)
15470 || GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
15472 dw_die_ref type_die;
15473 dw_loc_descr_ref deref;
15474 HOST_WIDE_INT size;
15476 if (dwarf_strict && dwarf_version < 5)
15477 return NULL;
15478 if (!GET_MODE_SIZE (mode).is_constant (&size))
15479 return NULL;
15480 type_die
15481 = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
15482 if (type_die == NULL)
15483 return NULL;
15484 deref = new_loc_descr (dwarf_OP (DW_OP_deref_type), size, 0);
15485 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
15486 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
15487 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
15488 add_loc_descr (&mem_loc_result, deref);
15490 else if (GET_MODE_SIZE (int_mode) == DWARF2_ADDR_SIZE)
15491 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
15492 else
15493 add_loc_descr (&mem_loc_result,
15494 new_loc_descr (DW_OP_deref_size,
15495 GET_MODE_SIZE (int_mode), 0));
15497 break;
15499 case LO_SUM:
15500 return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
15502 case LABEL_REF:
15503 /* Some ports can transform a symbol ref into a label ref, because
15504 the symbol ref is too far away and has to be dumped into a constant
15505 pool. */
15506 case CONST:
15507 case SYMBOL_REF:
15508 if (!is_a <scalar_int_mode> (mode, &int_mode)
15509 || (GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE
15510 #ifdef POINTERS_EXTEND_UNSIGNED
15511 && (int_mode != Pmode || mem_mode == VOIDmode)
15512 #endif
15514 break;
15515 if (GET_CODE (rtl) == SYMBOL_REF
15516 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
15518 dw_loc_descr_ref temp;
15520 /* If this is not defined, we have no way to emit the data. */
15521 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
15522 break;
15524 temp = new_addr_loc_descr (rtl, dtprel_true);
15526 /* We check for DWARF 5 here because gdb did not implement
15527 DW_OP_form_tls_address until after 7.12. */
15528 mem_loc_result = new_loc_descr ((dwarf_version >= 5
15529 ? DW_OP_form_tls_address
15530 : DW_OP_GNU_push_tls_address),
15531 0, 0);
15532 add_loc_descr (&mem_loc_result, temp);
15534 break;
15537 if (!const_ok_for_output (rtl))
15539 if (GET_CODE (rtl) == CONST)
15540 switch (GET_CODE (XEXP (rtl, 0)))
15542 case NOT:
15543 op = DW_OP_not;
15544 goto try_const_unop;
15545 case NEG:
15546 op = DW_OP_neg;
15547 goto try_const_unop;
15548 try_const_unop:
15549 rtx arg;
15550 arg = XEXP (XEXP (rtl, 0), 0);
15551 if (!CONSTANT_P (arg))
15552 arg = gen_rtx_CONST (int_mode, arg);
15553 op0 = mem_loc_descriptor (arg, int_mode, mem_mode,
15554 initialized);
15555 if (op0)
15557 mem_loc_result = op0;
15558 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15560 break;
15561 default:
15562 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), int_mode,
15563 mem_mode, initialized);
15564 break;
15566 break;
15569 symref:
15570 mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
15571 vec_safe_push (used_rtx_array, rtl);
15572 break;
15574 case CONCAT:
15575 case CONCATN:
15576 case VAR_LOCATION:
15577 case DEBUG_IMPLICIT_PTR:
15578 expansion_failed (NULL_TREE, rtl,
15579 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
15580 return 0;
15582 case ENTRY_VALUE:
15583 if (dwarf_strict && dwarf_version < 5)
15584 return NULL;
15585 if (REG_P (ENTRY_VALUE_EXP (rtl)))
15587 if (!is_a <scalar_int_mode> (mode, &int_mode)
15588 || GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
15589 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
15590 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15591 else
15593 unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
15594 if (dbx_regnum == IGNORED_DWARF_REGNUM)
15595 return NULL;
15596 op0 = one_reg_loc_descriptor (dbx_regnum,
15597 VAR_INIT_STATUS_INITIALIZED);
15600 else if (MEM_P (ENTRY_VALUE_EXP (rtl))
15601 && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
15603 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
15604 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15605 if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
15606 return NULL;
15608 else
15609 gcc_unreachable ();
15610 if (op0 == NULL)
15611 return NULL;
15612 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_entry_value), 0, 0);
15613 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
15614 mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
15615 break;
15617 case DEBUG_PARAMETER_REF:
15618 mem_loc_result = parameter_ref_descriptor (rtl);
15619 break;
15621 case PRE_MODIFY:
15622 /* Extract the PLUS expression nested inside and fall into
15623 PLUS code below. */
15624 rtl = XEXP (rtl, 1);
15625 goto plus;
15627 case PRE_INC:
15628 case PRE_DEC:
15629 /* Turn these into a PLUS expression and fall into the PLUS code
15630 below. */
15631 rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
15632 gen_int_mode (GET_CODE (rtl) == PRE_INC
15633 ? GET_MODE_UNIT_SIZE (mem_mode)
15634 : -GET_MODE_UNIT_SIZE (mem_mode),
15635 mode));
15637 /* fall through */
15639 case PLUS:
15640 plus:
15641 if (is_based_loc (rtl)
15642 && is_a <scalar_int_mode> (mode, &int_mode)
15643 && (GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
15644 || XEXP (rtl, 0) == arg_pointer_rtx
15645 || XEXP (rtl, 0) == frame_pointer_rtx))
15646 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
15647 INTVAL (XEXP (rtl, 1)),
15648 VAR_INIT_STATUS_INITIALIZED);
15649 else
15651 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15652 VAR_INIT_STATUS_INITIALIZED);
15653 if (mem_loc_result == 0)
15654 break;
15656 if (CONST_INT_P (XEXP (rtl, 1))
15657 && (GET_MODE_SIZE (as_a <scalar_int_mode> (mode))
15658 <= DWARF2_ADDR_SIZE))
15659 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
15660 else
15662 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15663 VAR_INIT_STATUS_INITIALIZED);
15664 if (op1 == 0)
15665 return NULL;
15666 add_loc_descr (&mem_loc_result, op1);
15667 add_loc_descr (&mem_loc_result,
15668 new_loc_descr (DW_OP_plus, 0, 0));
15671 break;
15673 /* If a pseudo-reg is optimized away, it is possible for it to
15674 be replaced with a MEM containing a multiply or shift. */
15675 case MINUS:
15676 op = DW_OP_minus;
15677 goto do_binop;
15679 case MULT:
15680 op = DW_OP_mul;
15681 goto do_binop;
15683 case DIV:
15684 if ((!dwarf_strict || dwarf_version >= 5)
15685 && is_a <scalar_int_mode> (mode, &int_mode)
15686 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
15688 mem_loc_result = typed_binop (DW_OP_div, rtl,
15689 base_type_for_mode (mode, 0),
15690 int_mode, mem_mode);
15691 break;
15693 op = DW_OP_div;
15694 goto do_binop;
15696 case UMOD:
15697 op = DW_OP_mod;
15698 goto do_binop;
15700 case ASHIFT:
15701 op = DW_OP_shl;
15702 goto do_shift;
15704 case ASHIFTRT:
15705 op = DW_OP_shra;
15706 goto do_shift;
15708 case LSHIFTRT:
15709 op = DW_OP_shr;
15710 goto do_shift;
15712 do_shift:
15713 if (!is_a <scalar_int_mode> (mode, &int_mode))
15714 break;
15715 op0 = mem_loc_descriptor (XEXP (rtl, 0), int_mode, mem_mode,
15716 VAR_INIT_STATUS_INITIALIZED);
15718 rtx rtlop1 = XEXP (rtl, 1);
15719 if (is_a <scalar_int_mode> (GET_MODE (rtlop1), &op1_mode)
15720 && GET_MODE_BITSIZE (op1_mode) < GET_MODE_BITSIZE (int_mode))
15721 rtlop1 = gen_rtx_ZERO_EXTEND (int_mode, rtlop1);
15722 op1 = mem_loc_descriptor (rtlop1, int_mode, mem_mode,
15723 VAR_INIT_STATUS_INITIALIZED);
15726 if (op0 == 0 || op1 == 0)
15727 break;
15729 mem_loc_result = op0;
15730 add_loc_descr (&mem_loc_result, op1);
15731 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15732 break;
15734 case AND:
15735 op = DW_OP_and;
15736 goto do_binop;
15738 case IOR:
15739 op = DW_OP_or;
15740 goto do_binop;
15742 case XOR:
15743 op = DW_OP_xor;
15744 goto do_binop;
15746 do_binop:
15747 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15748 VAR_INIT_STATUS_INITIALIZED);
15749 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15750 VAR_INIT_STATUS_INITIALIZED);
15752 if (op0 == 0 || op1 == 0)
15753 break;
15755 mem_loc_result = op0;
15756 add_loc_descr (&mem_loc_result, op1);
15757 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15758 break;
15760 case MOD:
15761 if ((!dwarf_strict || dwarf_version >= 5)
15762 && is_a <scalar_int_mode> (mode, &int_mode)
15763 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
15765 mem_loc_result = typed_binop (DW_OP_mod, rtl,
15766 base_type_for_mode (mode, 0),
15767 int_mode, mem_mode);
15768 break;
15771 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15772 VAR_INIT_STATUS_INITIALIZED);
15773 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15774 VAR_INIT_STATUS_INITIALIZED);
15776 if (op0 == 0 || op1 == 0)
15777 break;
15779 mem_loc_result = op0;
15780 add_loc_descr (&mem_loc_result, op1);
15781 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
15782 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
15783 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
15784 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
15785 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
15786 break;
15788 case UDIV:
15789 if ((!dwarf_strict || dwarf_version >= 5)
15790 && is_a <scalar_int_mode> (mode, &int_mode))
15792 if (GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
15794 op = DW_OP_div;
15795 goto do_binop;
15797 mem_loc_result = typed_binop (DW_OP_div, rtl,
15798 base_type_for_mode (int_mode, 1),
15799 int_mode, mem_mode);
15801 break;
15803 case NOT:
15804 op = DW_OP_not;
15805 goto do_unop;
15807 case ABS:
15808 op = DW_OP_abs;
15809 goto do_unop;
15811 case NEG:
15812 op = DW_OP_neg;
15813 goto do_unop;
15815 do_unop:
15816 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15817 VAR_INIT_STATUS_INITIALIZED);
15819 if (op0 == 0)
15820 break;
15822 mem_loc_result = op0;
15823 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15824 break;
15826 case CONST_INT:
15827 if (!is_a <scalar_int_mode> (mode, &int_mode)
15828 || GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
15829 #ifdef POINTERS_EXTEND_UNSIGNED
15830 || (int_mode == Pmode
15831 && mem_mode != VOIDmode
15832 && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
15833 #endif
15836 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
15837 break;
15839 if ((!dwarf_strict || dwarf_version >= 5)
15840 && (GET_MODE_BITSIZE (int_mode) == HOST_BITS_PER_WIDE_INT
15841 || GET_MODE_BITSIZE (int_mode) == HOST_BITS_PER_DOUBLE_INT))
15843 dw_die_ref type_die = base_type_for_mode (int_mode, 1);
15844 scalar_int_mode amode;
15845 if (type_die == NULL)
15846 return NULL;
15847 if (INTVAL (rtl) >= 0
15848 && (int_mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT, 0)
15849 .exists (&amode))
15850 && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
15851 /* const DW_OP_convert <XXX> vs.
15852 DW_OP_const_type <XXX, 1, const>. */
15853 && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
15854 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (int_mode))
15856 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
15857 op0 = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15858 op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15859 op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15860 op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
15861 add_loc_descr (&mem_loc_result, op0);
15862 return mem_loc_result;
15864 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0,
15865 INTVAL (rtl));
15866 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15867 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15868 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
15869 if (GET_MODE_BITSIZE (int_mode) == HOST_BITS_PER_WIDE_INT)
15870 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
15871 else
15873 mem_loc_result->dw_loc_oprnd2.val_class
15874 = dw_val_class_const_double;
15875 mem_loc_result->dw_loc_oprnd2.v.val_double
15876 = double_int::from_shwi (INTVAL (rtl));
15879 break;
15881 case CONST_DOUBLE:
15882 if (!dwarf_strict || dwarf_version >= 5)
15884 dw_die_ref type_die;
15886 /* Note that if TARGET_SUPPORTS_WIDE_INT == 0, a
15887 CONST_DOUBLE rtx could represent either a large integer
15888 or a floating-point constant. If TARGET_SUPPORTS_WIDE_INT != 0,
15889 the value is always a floating point constant.
15891 When it is an integer, a CONST_DOUBLE is used whenever
15892 the constant requires 2 HWIs to be adequately represented.
15893 We output CONST_DOUBLEs as blocks. */
15894 if (mode == VOIDmode
15895 || (GET_MODE (rtl) == VOIDmode
15896 && maybe_ne (GET_MODE_BITSIZE (mode),
15897 HOST_BITS_PER_DOUBLE_INT)))
15898 break;
15899 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
15900 if (type_die == NULL)
15901 return NULL;
15902 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0, 0);
15903 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15904 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15905 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
15906 #if TARGET_SUPPORTS_WIDE_INT == 0
15907 if (!SCALAR_FLOAT_MODE_P (mode))
15909 mem_loc_result->dw_loc_oprnd2.val_class
15910 = dw_val_class_const_double;
15911 mem_loc_result->dw_loc_oprnd2.v.val_double
15912 = rtx_to_double_int (rtl);
15914 else
15915 #endif
15917 scalar_float_mode float_mode = as_a <scalar_float_mode> (mode);
15918 unsigned int length = GET_MODE_SIZE (float_mode);
15919 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
15921 insert_float (rtl, array);
15922 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
15923 mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
15924 mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
15925 mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
15928 break;
15930 case CONST_WIDE_INT:
15931 if (!dwarf_strict || dwarf_version >= 5)
15933 dw_die_ref type_die;
15935 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
15936 if (type_die == NULL)
15937 return NULL;
15938 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0, 0);
15939 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15940 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15941 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
15942 mem_loc_result->dw_loc_oprnd2.val_class
15943 = dw_val_class_wide_int;
15944 mem_loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
15945 *mem_loc_result->dw_loc_oprnd2.v.val_wide = rtx_mode_t (rtl, mode);
15947 break;
15949 case CONST_POLY_INT:
15950 mem_loc_result = int_loc_descriptor (rtx_to_poly_int64 (rtl));
15951 break;
15953 case EQ:
15954 mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
15955 break;
15957 case GE:
15958 mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
15959 break;
15961 case GT:
15962 mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
15963 break;
15965 case LE:
15966 mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
15967 break;
15969 case LT:
15970 mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
15971 break;
15973 case NE:
15974 mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
15975 break;
15977 case GEU:
15978 mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
15979 break;
15981 case GTU:
15982 mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
15983 break;
15985 case LEU:
15986 mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
15987 break;
15989 case LTU:
15990 mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
15991 break;
15993 case UMIN:
15994 case UMAX:
15995 if (!SCALAR_INT_MODE_P (mode))
15996 break;
15997 /* FALLTHRU */
15998 case SMIN:
15999 case SMAX:
16000 mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
16001 break;
16003 case ZERO_EXTRACT:
16004 case SIGN_EXTRACT:
16005 if (CONST_INT_P (XEXP (rtl, 1))
16006 && CONST_INT_P (XEXP (rtl, 2))
16007 && is_a <scalar_int_mode> (mode, &int_mode)
16008 && is_a <scalar_int_mode> (GET_MODE (XEXP (rtl, 0)), &inner_mode)
16009 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
16010 && GET_MODE_SIZE (inner_mode) <= DWARF2_ADDR_SIZE
16011 && ((unsigned) INTVAL (XEXP (rtl, 1))
16012 + (unsigned) INTVAL (XEXP (rtl, 2))
16013 <= GET_MODE_BITSIZE (int_mode)))
16015 int shift, size;
16016 op0 = mem_loc_descriptor (XEXP (rtl, 0), inner_mode,
16017 mem_mode, VAR_INIT_STATUS_INITIALIZED);
16018 if (op0 == 0)
16019 break;
16020 if (GET_CODE (rtl) == SIGN_EXTRACT)
16021 op = DW_OP_shra;
16022 else
16023 op = DW_OP_shr;
16024 mem_loc_result = op0;
16025 size = INTVAL (XEXP (rtl, 1));
16026 shift = INTVAL (XEXP (rtl, 2));
16027 if (BITS_BIG_ENDIAN)
16028 shift = GET_MODE_BITSIZE (inner_mode) - shift - size;
16029 if (shift + size != (int) DWARF2_ADDR_SIZE)
16031 add_loc_descr (&mem_loc_result,
16032 int_loc_descriptor (DWARF2_ADDR_SIZE
16033 - shift - size));
16034 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
16036 if (size != (int) DWARF2_ADDR_SIZE)
16038 add_loc_descr (&mem_loc_result,
16039 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
16040 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
16043 break;
16045 case IF_THEN_ELSE:
16047 dw_loc_descr_ref op2, bra_node, drop_node;
16048 op0 = mem_loc_descriptor (XEXP (rtl, 0),
16049 GET_MODE (XEXP (rtl, 0)) == VOIDmode
16050 ? word_mode : GET_MODE (XEXP (rtl, 0)),
16051 mem_mode, VAR_INIT_STATUS_INITIALIZED);
16052 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
16053 VAR_INIT_STATUS_INITIALIZED);
16054 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
16055 VAR_INIT_STATUS_INITIALIZED);
16056 if (op0 == NULL || op1 == NULL || op2 == NULL)
16057 break;
16059 mem_loc_result = op1;
16060 add_loc_descr (&mem_loc_result, op2);
16061 add_loc_descr (&mem_loc_result, op0);
16062 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
16063 add_loc_descr (&mem_loc_result, bra_node);
16064 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
16065 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
16066 add_loc_descr (&mem_loc_result, drop_node);
16067 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
16068 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
16070 break;
16072 case FLOAT_EXTEND:
16073 case FLOAT_TRUNCATE:
16074 case FLOAT:
16075 case UNSIGNED_FLOAT:
16076 case FIX:
16077 case UNSIGNED_FIX:
16078 if (!dwarf_strict || dwarf_version >= 5)
16080 dw_die_ref type_die;
16081 dw_loc_descr_ref cvt;
16083 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
16084 mem_mode, VAR_INIT_STATUS_INITIALIZED);
16085 if (op0 == NULL)
16086 break;
16087 if (is_a <scalar_int_mode> (GET_MODE (XEXP (rtl, 0)), &int_mode)
16088 && (GET_CODE (rtl) == FLOAT
16089 || GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE))
16091 type_die = base_type_for_mode (int_mode,
16092 GET_CODE (rtl) == UNSIGNED_FLOAT);
16093 if (type_die == NULL)
16094 break;
16095 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
16096 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16097 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16098 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
16099 add_loc_descr (&op0, cvt);
16101 type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
16102 if (type_die == NULL)
16103 break;
16104 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
16105 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16106 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16107 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
16108 add_loc_descr (&op0, cvt);
16109 if (is_a <scalar_int_mode> (mode, &int_mode)
16110 && (GET_CODE (rtl) == FIX
16111 || GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE))
16113 op0 = convert_descriptor_to_mode (int_mode, op0);
16114 if (op0 == NULL)
16115 break;
16117 mem_loc_result = op0;
16119 break;
16121 case CLZ:
16122 case CTZ:
16123 case FFS:
16124 if (is_a <scalar_int_mode> (mode, &int_mode))
16125 mem_loc_result = clz_loc_descriptor (rtl, int_mode, mem_mode);
16126 break;
16128 case POPCOUNT:
16129 case PARITY:
16130 if (is_a <scalar_int_mode> (mode, &int_mode))
16131 mem_loc_result = popcount_loc_descriptor (rtl, int_mode, mem_mode);
16132 break;
16134 case BSWAP:
16135 if (is_a <scalar_int_mode> (mode, &int_mode))
16136 mem_loc_result = bswap_loc_descriptor (rtl, int_mode, mem_mode);
16137 break;
16139 case ROTATE:
16140 case ROTATERT:
16141 if (is_a <scalar_int_mode> (mode, &int_mode))
16142 mem_loc_result = rotate_loc_descriptor (rtl, int_mode, mem_mode);
16143 break;
16145 case COMPARE:
16146 /* In theory, we could implement the above. */
16147 /* DWARF cannot represent the unsigned compare operations
16148 natively. */
16149 case SS_MULT:
16150 case US_MULT:
16151 case SS_DIV:
16152 case US_DIV:
16153 case SS_PLUS:
16154 case US_PLUS:
16155 case SS_MINUS:
16156 case US_MINUS:
16157 case SS_NEG:
16158 case US_NEG:
16159 case SS_ABS:
16160 case SS_ASHIFT:
16161 case US_ASHIFT:
16162 case SS_TRUNCATE:
16163 case US_TRUNCATE:
16164 case UNORDERED:
16165 case ORDERED:
16166 case UNEQ:
16167 case UNGE:
16168 case UNGT:
16169 case UNLE:
16170 case UNLT:
16171 case LTGT:
16172 case FRACT_CONVERT:
16173 case UNSIGNED_FRACT_CONVERT:
16174 case SAT_FRACT:
16175 case UNSIGNED_SAT_FRACT:
16176 case SQRT:
16177 case ASM_OPERANDS:
16178 case VEC_MERGE:
16179 case VEC_SELECT:
16180 case VEC_CONCAT:
16181 case VEC_DUPLICATE:
16182 case VEC_SERIES:
16183 case UNSPEC:
16184 case HIGH:
16185 case FMA:
16186 case STRICT_LOW_PART:
16187 case CONST_VECTOR:
16188 case CONST_FIXED:
16189 case CLRSB:
16190 case CLOBBER:
16191 /* If delegitimize_address couldn't do anything with the UNSPEC, we
16192 can't express it in the debug info. This can happen e.g. with some
16193 TLS UNSPECs. */
16194 break;
16196 case CONST_STRING:
16197 resolve_one_addr (&rtl);
16198 goto symref;
16200 /* RTL sequences inside PARALLEL record a series of DWARF operations for
16201 the expression. An UNSPEC rtx represents a raw DWARF operation,
16202 new_loc_descr is called for it to build the operation directly.
16203 Otherwise mem_loc_descriptor is called recursively. */
16204 case PARALLEL:
16206 int index = 0;
16207 dw_loc_descr_ref exp_result = NULL;
16209 for (; index < XVECLEN (rtl, 0); index++)
16211 rtx elem = XVECEXP (rtl, 0, index);
16212 if (GET_CODE (elem) == UNSPEC)
16214 /* Each DWARF operation UNSPEC contain two operands, if
16215 one operand is not used for the operation, const0_rtx is
16216 passed. */
16217 gcc_assert (XVECLEN (elem, 0) == 2);
16219 HOST_WIDE_INT dw_op = XINT (elem, 1);
16220 HOST_WIDE_INT oprnd1 = INTVAL (XVECEXP (elem, 0, 0));
16221 HOST_WIDE_INT oprnd2 = INTVAL (XVECEXP (elem, 0, 1));
16222 exp_result
16223 = new_loc_descr ((enum dwarf_location_atom) dw_op, oprnd1,
16224 oprnd2);
16226 else
16227 exp_result
16228 = mem_loc_descriptor (elem, mode, mem_mode,
16229 VAR_INIT_STATUS_INITIALIZED);
16231 if (!mem_loc_result)
16232 mem_loc_result = exp_result;
16233 else
16234 add_loc_descr (&mem_loc_result, exp_result);
16237 break;
16240 default:
16241 if (flag_checking)
16243 print_rtl (stderr, rtl);
16244 gcc_unreachable ();
16246 break;
16249 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
16250 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
16252 return mem_loc_result;
16255 /* Return a descriptor that describes the concatenation of two locations.
16256 This is typically a complex variable. */
16258 static dw_loc_descr_ref
16259 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
16261 /* At present we only track constant-sized pieces. */
16262 unsigned int size0, size1;
16263 if (!GET_MODE_SIZE (GET_MODE (x0)).is_constant (&size0)
16264 || !GET_MODE_SIZE (GET_MODE (x1)).is_constant (&size1))
16265 return 0;
16267 dw_loc_descr_ref cc_loc_result = NULL;
16268 dw_loc_descr_ref x0_ref
16269 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
16270 dw_loc_descr_ref x1_ref
16271 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
16273 if (x0_ref == 0 || x1_ref == 0)
16274 return 0;
16276 cc_loc_result = x0_ref;
16277 add_loc_descr_op_piece (&cc_loc_result, size0);
16279 add_loc_descr (&cc_loc_result, x1_ref);
16280 add_loc_descr_op_piece (&cc_loc_result, size1);
16282 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
16283 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
16285 return cc_loc_result;
16288 /* Return a descriptor that describes the concatenation of N
16289 locations. */
16291 static dw_loc_descr_ref
16292 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
16294 unsigned int i;
16295 dw_loc_descr_ref cc_loc_result = NULL;
16296 unsigned int n = XVECLEN (concatn, 0);
16297 unsigned int size;
16299 for (i = 0; i < n; ++i)
16301 dw_loc_descr_ref ref;
16302 rtx x = XVECEXP (concatn, 0, i);
16304 /* At present we only track constant-sized pieces. */
16305 if (!GET_MODE_SIZE (GET_MODE (x)).is_constant (&size))
16306 return NULL;
16308 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
16309 if (ref == NULL)
16310 return NULL;
16312 add_loc_descr (&cc_loc_result, ref);
16313 add_loc_descr_op_piece (&cc_loc_result, size);
16316 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
16317 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
16319 return cc_loc_result;
16322 /* Helper function for loc_descriptor. Return DW_OP_implicit_pointer
16323 for DEBUG_IMPLICIT_PTR RTL. */
16325 static dw_loc_descr_ref
16326 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
16328 dw_loc_descr_ref ret;
16329 dw_die_ref ref;
16331 if (dwarf_strict && dwarf_version < 5)
16332 return NULL;
16333 gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
16334 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
16335 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
16336 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
16337 ret = new_loc_descr (dwarf_OP (DW_OP_implicit_pointer), 0, offset);
16338 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
16339 if (ref)
16341 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16342 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
16343 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
16345 else
16347 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
16348 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
16350 return ret;
16353 /* Output a proper Dwarf location descriptor for a variable or parameter
16354 which is either allocated in a register or in a memory location. For a
16355 register, we just generate an OP_REG and the register number. For a
16356 memory location we provide a Dwarf postfix expression describing how to
16357 generate the (dynamic) address of the object onto the address stack.
16359 MODE is mode of the decl if this loc_descriptor is going to be used in
16360 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
16361 allowed, VOIDmode otherwise.
16363 If we don't know how to describe it, return 0. */
16365 static dw_loc_descr_ref
16366 loc_descriptor (rtx rtl, machine_mode mode,
16367 enum var_init_status initialized)
16369 dw_loc_descr_ref loc_result = NULL;
16370 scalar_int_mode int_mode;
16372 switch (GET_CODE (rtl))
16374 case SUBREG:
16375 /* The case of a subreg may arise when we have a local (register)
16376 variable or a formal (register) parameter which doesn't quite fill
16377 up an entire register. For now, just assume that it is
16378 legitimate to make the Dwarf info refer to the whole register which
16379 contains the given subreg. */
16380 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
16381 loc_result = loc_descriptor (SUBREG_REG (rtl),
16382 GET_MODE (SUBREG_REG (rtl)), initialized);
16383 else
16384 goto do_default;
16385 break;
16387 case REG:
16388 loc_result = reg_loc_descriptor (rtl, initialized);
16389 break;
16391 case MEM:
16392 loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
16393 GET_MODE (rtl), initialized);
16394 if (loc_result == NULL)
16395 loc_result = tls_mem_loc_descriptor (rtl);
16396 if (loc_result == NULL)
16398 rtx new_rtl = avoid_constant_pool_reference (rtl);
16399 if (new_rtl != rtl)
16400 loc_result = loc_descriptor (new_rtl, mode, initialized);
16402 break;
16404 case CONCAT:
16405 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
16406 initialized);
16407 break;
16409 case CONCATN:
16410 loc_result = concatn_loc_descriptor (rtl, initialized);
16411 break;
16413 case VAR_LOCATION:
16414 /* Single part. */
16415 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
16417 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
16418 if (GET_CODE (loc) == EXPR_LIST)
16419 loc = XEXP (loc, 0);
16420 loc_result = loc_descriptor (loc, mode, initialized);
16421 break;
16424 rtl = XEXP (rtl, 1);
16425 /* FALLTHRU */
16427 case PARALLEL:
16429 rtvec par_elems = XVEC (rtl, 0);
16430 int num_elem = GET_NUM_ELEM (par_elems);
16431 machine_mode mode;
16432 int i, size;
16434 /* Create the first one, so we have something to add to. */
16435 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
16436 VOIDmode, initialized);
16437 if (loc_result == NULL)
16438 return NULL;
16439 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
16440 /* At present we only track constant-sized pieces. */
16441 if (!GET_MODE_SIZE (mode).is_constant (&size))
16442 return NULL;
16443 add_loc_descr_op_piece (&loc_result, size);
16444 for (i = 1; i < num_elem; i++)
16446 dw_loc_descr_ref temp;
16448 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
16449 VOIDmode, initialized);
16450 if (temp == NULL)
16451 return NULL;
16452 add_loc_descr (&loc_result, temp);
16453 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
16454 /* At present we only track constant-sized pieces. */
16455 if (!GET_MODE_SIZE (mode).is_constant (&size))
16456 return NULL;
16457 add_loc_descr_op_piece (&loc_result, size);
16460 break;
16462 case CONST_INT:
16463 if (mode != VOIDmode && mode != BLKmode)
16465 int_mode = as_a <scalar_int_mode> (mode);
16466 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (int_mode),
16467 INTVAL (rtl));
16469 break;
16471 case CONST_DOUBLE:
16472 if (mode == VOIDmode)
16473 mode = GET_MODE (rtl);
16475 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
16477 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
16479 /* Note that a CONST_DOUBLE rtx could represent either an integer
16480 or a floating-point constant. A CONST_DOUBLE is used whenever
16481 the constant requires more than one word in order to be
16482 adequately represented. We output CONST_DOUBLEs as blocks. */
16483 scalar_mode smode = as_a <scalar_mode> (mode);
16484 loc_result = new_loc_descr (DW_OP_implicit_value,
16485 GET_MODE_SIZE (smode), 0);
16486 #if TARGET_SUPPORTS_WIDE_INT == 0
16487 if (!SCALAR_FLOAT_MODE_P (smode))
16489 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
16490 loc_result->dw_loc_oprnd2.v.val_double
16491 = rtx_to_double_int (rtl);
16493 else
16494 #endif
16496 unsigned int length = GET_MODE_SIZE (smode);
16497 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
16499 insert_float (rtl, array);
16500 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
16501 loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
16502 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
16503 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
16506 break;
16508 case CONST_WIDE_INT:
16509 if (mode == VOIDmode)
16510 mode = GET_MODE (rtl);
16512 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
16514 int_mode = as_a <scalar_int_mode> (mode);
16515 loc_result = new_loc_descr (DW_OP_implicit_value,
16516 GET_MODE_SIZE (int_mode), 0);
16517 loc_result->dw_loc_oprnd2.val_class = dw_val_class_wide_int;
16518 loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
16519 *loc_result->dw_loc_oprnd2.v.val_wide = rtx_mode_t (rtl, int_mode);
16521 break;
16523 case CONST_VECTOR:
16524 if (mode == VOIDmode)
16525 mode = GET_MODE (rtl);
16527 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
16529 unsigned int length;
16530 if (!CONST_VECTOR_NUNITS (rtl).is_constant (&length))
16531 return NULL;
16533 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
16534 unsigned char *array
16535 = ggc_vec_alloc<unsigned char> (length * elt_size);
16536 unsigned int i;
16537 unsigned char *p;
16538 machine_mode imode = GET_MODE_INNER (mode);
16540 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
16541 switch (GET_MODE_CLASS (mode))
16543 case MODE_VECTOR_INT:
16544 for (i = 0, p = array; i < length; i++, p += elt_size)
16546 rtx elt = CONST_VECTOR_ELT (rtl, i);
16547 insert_wide_int (rtx_mode_t (elt, imode), p, elt_size);
16549 break;
16551 case MODE_VECTOR_FLOAT:
16552 for (i = 0, p = array; i < length; i++, p += elt_size)
16554 rtx elt = CONST_VECTOR_ELT (rtl, i);
16555 insert_float (elt, p);
16557 break;
16559 default:
16560 gcc_unreachable ();
16563 loc_result = new_loc_descr (DW_OP_implicit_value,
16564 length * elt_size, 0);
16565 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
16566 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
16567 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
16568 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
16570 break;
16572 case CONST:
16573 if (mode == VOIDmode
16574 || CONST_SCALAR_INT_P (XEXP (rtl, 0))
16575 || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
16576 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
16578 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
16579 break;
16581 /* FALLTHROUGH */
16582 case SYMBOL_REF:
16583 if (!const_ok_for_output (rtl))
16584 break;
16585 /* FALLTHROUGH */
16586 case LABEL_REF:
16587 if (is_a <scalar_int_mode> (mode, &int_mode)
16588 && GET_MODE_SIZE (int_mode) == DWARF2_ADDR_SIZE
16589 && (dwarf_version >= 4 || !dwarf_strict))
16591 loc_result = new_addr_loc_descr (rtl, dtprel_false);
16592 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
16593 vec_safe_push (used_rtx_array, rtl);
16595 break;
16597 case DEBUG_IMPLICIT_PTR:
16598 loc_result = implicit_ptr_descriptor (rtl, 0);
16599 break;
16601 case PLUS:
16602 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
16603 && CONST_INT_P (XEXP (rtl, 1)))
16605 loc_result
16606 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
16607 break;
16609 /* FALLTHRU */
16610 do_default:
16611 default:
16612 if ((is_a <scalar_int_mode> (mode, &int_mode)
16613 && GET_MODE (rtl) == int_mode
16614 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
16615 && dwarf_version >= 4)
16616 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
16618 /* Value expression. */
16619 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
16620 if (loc_result)
16621 add_loc_descr (&loc_result,
16622 new_loc_descr (DW_OP_stack_value, 0, 0));
16624 break;
16627 return loc_result;
16630 /* We need to figure out what section we should use as the base for the
16631 address ranges where a given location is valid.
16632 1. If this particular DECL has a section associated with it, use that.
16633 2. If this function has a section associated with it, use that.
16634 3. Otherwise, use the text section.
16635 XXX: If you split a variable across multiple sections, we won't notice. */
16637 static const char *
16638 secname_for_decl (const_tree decl)
16640 const char *secname;
16642 if (VAR_OR_FUNCTION_DECL_P (decl)
16643 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl) || TREE_STATIC (decl))
16644 && DECL_SECTION_NAME (decl))
16645 secname = DECL_SECTION_NAME (decl);
16646 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
16647 secname = DECL_SECTION_NAME (current_function_decl);
16648 else if (cfun && in_cold_section_p)
16649 secname = crtl->subsections.cold_section_label;
16650 else
16651 secname = text_section_label;
16653 return secname;
16656 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
16658 static bool
16659 decl_by_reference_p (tree decl)
16661 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
16662 || VAR_P (decl))
16663 && DECL_BY_REFERENCE (decl));
16666 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
16667 for VARLOC. */
16669 static dw_loc_descr_ref
16670 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
16671 enum var_init_status initialized)
16673 int have_address = 0;
16674 dw_loc_descr_ref descr;
16675 machine_mode mode;
16677 if (want_address != 2)
16679 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
16680 /* Single part. */
16681 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
16683 varloc = PAT_VAR_LOCATION_LOC (varloc);
16684 if (GET_CODE (varloc) == EXPR_LIST)
16685 varloc = XEXP (varloc, 0);
16686 mode = GET_MODE (varloc);
16687 if (MEM_P (varloc))
16689 rtx addr = XEXP (varloc, 0);
16690 descr = mem_loc_descriptor (addr, get_address_mode (varloc),
16691 mode, initialized);
16692 if (descr)
16693 have_address = 1;
16694 else
16696 rtx x = avoid_constant_pool_reference (varloc);
16697 if (x != varloc)
16698 descr = mem_loc_descriptor (x, mode, VOIDmode,
16699 initialized);
16702 else
16703 descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
16705 else
16706 return 0;
16708 else
16710 if (GET_CODE (varloc) == VAR_LOCATION)
16711 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
16712 else
16713 mode = DECL_MODE (loc);
16714 descr = loc_descriptor (varloc, mode, initialized);
16715 have_address = 1;
16718 if (!descr)
16719 return 0;
16721 if (want_address == 2 && !have_address
16722 && (dwarf_version >= 4 || !dwarf_strict))
16724 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
16726 expansion_failed (loc, NULL_RTX,
16727 "DWARF address size mismatch");
16728 return 0;
16730 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
16731 have_address = 1;
16733 /* Show if we can't fill the request for an address. */
16734 if (want_address && !have_address)
16736 expansion_failed (loc, NULL_RTX,
16737 "Want address and only have value");
16738 return 0;
16741 /* If we've got an address and don't want one, dereference. */
16742 if (!want_address && have_address)
16744 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
16745 enum dwarf_location_atom op;
16747 if (size > DWARF2_ADDR_SIZE || size == -1)
16749 expansion_failed (loc, NULL_RTX,
16750 "DWARF address size mismatch");
16751 return 0;
16753 else if (size == DWARF2_ADDR_SIZE)
16754 op = DW_OP_deref;
16755 else
16756 op = DW_OP_deref_size;
16758 add_loc_descr (&descr, new_loc_descr (op, size, 0));
16761 return descr;
16764 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
16765 if it is not possible. */
16767 static dw_loc_descr_ref
16768 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
16770 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
16771 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
16772 else if (dwarf_version >= 3 || !dwarf_strict)
16773 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
16774 else
16775 return NULL;
16778 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
16779 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
16781 static dw_loc_descr_ref
16782 dw_sra_loc_expr (tree decl, rtx loc)
16784 rtx p;
16785 unsigned HOST_WIDE_INT padsize = 0;
16786 dw_loc_descr_ref descr, *descr_tail;
16787 unsigned HOST_WIDE_INT decl_size;
16788 rtx varloc;
16789 enum var_init_status initialized;
16791 if (DECL_SIZE (decl) == NULL
16792 || !tree_fits_uhwi_p (DECL_SIZE (decl)))
16793 return NULL;
16795 decl_size = tree_to_uhwi (DECL_SIZE (decl));
16796 descr = NULL;
16797 descr_tail = &descr;
16799 for (p = loc; p; p = XEXP (p, 1))
16801 unsigned HOST_WIDE_INT bitsize = decl_piece_bitsize (p);
16802 rtx loc_note = *decl_piece_varloc_ptr (p);
16803 dw_loc_descr_ref cur_descr;
16804 dw_loc_descr_ref *tail, last = NULL;
16805 unsigned HOST_WIDE_INT opsize = 0;
16807 if (loc_note == NULL_RTX
16808 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
16810 padsize += bitsize;
16811 continue;
16813 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
16814 varloc = NOTE_VAR_LOCATION (loc_note);
16815 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
16816 if (cur_descr == NULL)
16818 padsize += bitsize;
16819 continue;
16822 /* Check that cur_descr either doesn't use
16823 DW_OP_*piece operations, or their sum is equal
16824 to bitsize. Otherwise we can't embed it. */
16825 for (tail = &cur_descr; *tail != NULL;
16826 tail = &(*tail)->dw_loc_next)
16827 if ((*tail)->dw_loc_opc == DW_OP_piece)
16829 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
16830 * BITS_PER_UNIT;
16831 last = *tail;
16833 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
16835 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
16836 last = *tail;
16839 if (last != NULL && opsize != bitsize)
16841 padsize += bitsize;
16842 /* Discard the current piece of the descriptor and release any
16843 addr_table entries it uses. */
16844 remove_loc_list_addr_table_entries (cur_descr);
16845 continue;
16848 /* If there is a hole, add DW_OP_*piece after empty DWARF
16849 expression, which means that those bits are optimized out. */
16850 if (padsize)
16852 if (padsize > decl_size)
16854 remove_loc_list_addr_table_entries (cur_descr);
16855 goto discard_descr;
16857 decl_size -= padsize;
16858 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
16859 if (*descr_tail == NULL)
16861 remove_loc_list_addr_table_entries (cur_descr);
16862 goto discard_descr;
16864 descr_tail = &(*descr_tail)->dw_loc_next;
16865 padsize = 0;
16867 *descr_tail = cur_descr;
16868 descr_tail = tail;
16869 if (bitsize > decl_size)
16870 goto discard_descr;
16871 decl_size -= bitsize;
16872 if (last == NULL)
16874 HOST_WIDE_INT offset = 0;
16875 if (GET_CODE (varloc) == VAR_LOCATION
16876 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
16878 varloc = PAT_VAR_LOCATION_LOC (varloc);
16879 if (GET_CODE (varloc) == EXPR_LIST)
16880 varloc = XEXP (varloc, 0);
16884 if (GET_CODE (varloc) == CONST
16885 || GET_CODE (varloc) == SIGN_EXTEND
16886 || GET_CODE (varloc) == ZERO_EXTEND)
16887 varloc = XEXP (varloc, 0);
16888 else if (GET_CODE (varloc) == SUBREG)
16889 varloc = SUBREG_REG (varloc);
16890 else
16891 break;
16893 while (1);
16894 /* DW_OP_bit_size offset should be zero for register
16895 or implicit location descriptions and empty location
16896 descriptions, but for memory addresses needs big endian
16897 adjustment. */
16898 if (MEM_P (varloc))
16900 unsigned HOST_WIDE_INT memsize;
16901 if (!poly_uint64 (MEM_SIZE (varloc)).is_constant (&memsize))
16902 goto discard_descr;
16903 memsize *= BITS_PER_UNIT;
16904 if (memsize != bitsize)
16906 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
16907 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
16908 goto discard_descr;
16909 if (memsize < bitsize)
16910 goto discard_descr;
16911 if (BITS_BIG_ENDIAN)
16912 offset = memsize - bitsize;
16916 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
16917 if (*descr_tail == NULL)
16918 goto discard_descr;
16919 descr_tail = &(*descr_tail)->dw_loc_next;
16923 /* If there were any non-empty expressions, add padding till the end of
16924 the decl. */
16925 if (descr != NULL && decl_size != 0)
16927 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
16928 if (*descr_tail == NULL)
16929 goto discard_descr;
16931 return descr;
16933 discard_descr:
16934 /* Discard the descriptor and release any addr_table entries it uses. */
16935 remove_loc_list_addr_table_entries (descr);
16936 return NULL;
16939 /* Return the dwarf representation of the location list LOC_LIST of
16940 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
16941 function. */
16943 static dw_loc_list_ref
16944 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
16946 const char *endname, *secname;
16947 var_loc_view endview;
16948 rtx varloc;
16949 enum var_init_status initialized;
16950 struct var_loc_node *node;
16951 dw_loc_descr_ref descr;
16952 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
16953 dw_loc_list_ref list = NULL;
16954 dw_loc_list_ref *listp = &list;
16956 /* Now that we know what section we are using for a base,
16957 actually construct the list of locations.
16958 The first location information is what is passed to the
16959 function that creates the location list, and the remaining
16960 locations just get added on to that list.
16961 Note that we only know the start address for a location
16962 (IE location changes), so to build the range, we use
16963 the range [current location start, next location start].
16964 This means we have to special case the last node, and generate
16965 a range of [last location start, end of function label]. */
16967 if (cfun && crtl->has_bb_partition)
16969 bool save_in_cold_section_p = in_cold_section_p;
16970 in_cold_section_p = first_function_block_is_cold;
16971 if (loc_list->last_before_switch == NULL)
16972 in_cold_section_p = !in_cold_section_p;
16973 secname = secname_for_decl (decl);
16974 in_cold_section_p = save_in_cold_section_p;
16976 else
16977 secname = secname_for_decl (decl);
16979 for (node = loc_list->first; node; node = node->next)
16981 bool range_across_switch = false;
16982 if (GET_CODE (node->loc) == EXPR_LIST
16983 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
16985 if (GET_CODE (node->loc) == EXPR_LIST)
16987 descr = NULL;
16988 /* This requires DW_OP_{,bit_}piece, which is not usable
16989 inside DWARF expressions. */
16990 if (want_address == 2)
16991 descr = dw_sra_loc_expr (decl, node->loc);
16993 else
16995 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
16996 varloc = NOTE_VAR_LOCATION (node->loc);
16997 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
16999 if (descr)
17001 /* If section switch happens in between node->label
17002 and node->next->label (or end of function) and
17003 we can't emit it as a single entry list,
17004 emit two ranges, first one ending at the end
17005 of first partition and second one starting at the
17006 beginning of second partition. */
17007 if (node == loc_list->last_before_switch
17008 && (node != loc_list->first || loc_list->first->next)
17009 && current_function_decl)
17011 endname = cfun->fde->dw_fde_end;
17012 endview = 0;
17013 range_across_switch = true;
17015 /* The variable has a location between NODE->LABEL and
17016 NODE->NEXT->LABEL. */
17017 else if (node->next)
17018 endname = node->next->label, endview = node->next->view;
17019 /* If the variable has a location at the last label
17020 it keeps its location until the end of function. */
17021 else if (!current_function_decl)
17022 endname = text_end_label, endview = 0;
17023 else
17025 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
17026 current_function_funcdef_no);
17027 endname = ggc_strdup (label_id);
17028 endview = 0;
17031 *listp = new_loc_list (descr, node->label, node->view,
17032 endname, endview, secname);
17033 if (TREE_CODE (decl) == PARM_DECL
17034 && node == loc_list->first
17035 && NOTE_P (node->loc)
17036 && strcmp (node->label, endname) == 0)
17037 (*listp)->force = true;
17038 listp = &(*listp)->dw_loc_next;
17042 if (cfun
17043 && crtl->has_bb_partition
17044 && node == loc_list->last_before_switch)
17046 bool save_in_cold_section_p = in_cold_section_p;
17047 in_cold_section_p = !first_function_block_is_cold;
17048 secname = secname_for_decl (decl);
17049 in_cold_section_p = save_in_cold_section_p;
17052 if (range_across_switch)
17054 if (GET_CODE (node->loc) == EXPR_LIST)
17055 descr = dw_sra_loc_expr (decl, node->loc);
17056 else
17058 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
17059 varloc = NOTE_VAR_LOCATION (node->loc);
17060 descr = dw_loc_list_1 (decl, varloc, want_address,
17061 initialized);
17063 gcc_assert (descr);
17064 /* The variable has a location between NODE->LABEL and
17065 NODE->NEXT->LABEL. */
17066 if (node->next)
17067 endname = node->next->label, endview = node->next->view;
17068 else
17069 endname = cfun->fde->dw_fde_second_end, endview = 0;
17070 *listp = new_loc_list (descr, cfun->fde->dw_fde_second_begin, 0,
17071 endname, endview, secname);
17072 listp = &(*listp)->dw_loc_next;
17076 /* Try to avoid the overhead of a location list emitting a location
17077 expression instead, but only if we didn't have more than one
17078 location entry in the first place. If some entries were not
17079 representable, we don't want to pretend a single entry that was
17080 applies to the entire scope in which the variable is
17081 available. */
17082 if (list && loc_list->first->next)
17083 gen_llsym (list);
17084 else
17085 maybe_gen_llsym (list);
17087 return list;
17090 /* Return if the loc_list has only single element and thus can be represented
17091 as location description. */
17093 static bool
17094 single_element_loc_list_p (dw_loc_list_ref list)
17096 gcc_assert (!list->dw_loc_next || list->ll_symbol);
17097 return !list->ll_symbol;
17100 /* Duplicate a single element of location list. */
17102 static inline dw_loc_descr_ref
17103 copy_loc_descr (dw_loc_descr_ref ref)
17105 dw_loc_descr_ref copy = ggc_alloc<dw_loc_descr_node> ();
17106 memcpy (copy, ref, sizeof (dw_loc_descr_node));
17107 return copy;
17110 /* To each location in list LIST append loc descr REF. */
17112 static void
17113 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
17115 dw_loc_descr_ref copy;
17116 add_loc_descr (&list->expr, ref);
17117 list = list->dw_loc_next;
17118 while (list)
17120 copy = copy_loc_descr (ref);
17121 add_loc_descr (&list->expr, copy);
17122 while (copy->dw_loc_next)
17123 copy = copy->dw_loc_next = copy_loc_descr (copy->dw_loc_next);
17124 list = list->dw_loc_next;
17128 /* To each location in list LIST prepend loc descr REF. */
17130 static void
17131 prepend_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
17133 dw_loc_descr_ref copy;
17134 dw_loc_descr_ref ref_end = list->expr;
17135 add_loc_descr (&ref, list->expr);
17136 list->expr = ref;
17137 list = list->dw_loc_next;
17138 while (list)
17140 dw_loc_descr_ref end = list->expr;
17141 list->expr = copy = copy_loc_descr (ref);
17142 while (copy->dw_loc_next != ref_end)
17143 copy = copy->dw_loc_next = copy_loc_descr (copy->dw_loc_next);
17144 copy->dw_loc_next = end;
17145 list = list->dw_loc_next;
17149 /* Given two lists RET and LIST
17150 produce location list that is result of adding expression in LIST
17151 to expression in RET on each position in program.
17152 Might be destructive on both RET and LIST.
17154 TODO: We handle only simple cases of RET or LIST having at most one
17155 element. General case would involve sorting the lists in program order
17156 and merging them that will need some additional work.
17157 Adding that will improve quality of debug info especially for SRA-ed
17158 structures. */
17160 static void
17161 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
17163 if (!list)
17164 return;
17165 if (!*ret)
17167 *ret = list;
17168 return;
17170 if (!list->dw_loc_next)
17172 add_loc_descr_to_each (*ret, list->expr);
17173 return;
17175 if (!(*ret)->dw_loc_next)
17177 prepend_loc_descr_to_each (list, (*ret)->expr);
17178 *ret = list;
17179 return;
17181 expansion_failed (NULL_TREE, NULL_RTX,
17182 "Don't know how to merge two non-trivial"
17183 " location lists.\n");
17184 *ret = NULL;
17185 return;
17188 /* LOC is constant expression. Try a luck, look it up in constant
17189 pool and return its loc_descr of its address. */
17191 static dw_loc_descr_ref
17192 cst_pool_loc_descr (tree loc)
17194 /* Get an RTL for this, if something has been emitted. */
17195 rtx rtl = lookup_constant_def (loc);
17197 if (!rtl || !MEM_P (rtl))
17199 gcc_assert (!rtl);
17200 return 0;
17202 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
17204 /* TODO: We might get more coverage if we was actually delaying expansion
17205 of all expressions till end of compilation when constant pools are fully
17206 populated. */
17207 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
17209 expansion_failed (loc, NULL_RTX,
17210 "CST value in contant pool but not marked.");
17211 return 0;
17213 return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
17214 GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
17217 /* Return dw_loc_list representing address of addr_expr LOC
17218 by looking for inner INDIRECT_REF expression and turning
17219 it into simple arithmetics.
17221 See loc_list_from_tree for the meaning of CONTEXT. */
17223 static dw_loc_list_ref
17224 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev,
17225 loc_descr_context *context)
17227 tree obj, offset;
17228 poly_int64 bitsize, bitpos, bytepos;
17229 machine_mode mode;
17230 int unsignedp, reversep, volatilep = 0;
17231 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
17233 obj = get_inner_reference (TREE_OPERAND (loc, 0),
17234 &bitsize, &bitpos, &offset, &mode,
17235 &unsignedp, &reversep, &volatilep);
17236 STRIP_NOPS (obj);
17237 if (!multiple_p (bitpos, BITS_PER_UNIT, &bytepos))
17239 expansion_failed (loc, NULL_RTX, "bitfield access");
17240 return 0;
17242 if (!INDIRECT_REF_P (obj))
17244 expansion_failed (obj,
17245 NULL_RTX, "no indirect ref in inner refrence");
17246 return 0;
17248 if (!offset && known_eq (bitpos, 0))
17249 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1,
17250 context);
17251 else if (toplev
17252 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
17253 && (dwarf_version >= 4 || !dwarf_strict))
17255 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0, context);
17256 if (!list_ret)
17257 return 0;
17258 if (offset)
17260 /* Variable offset. */
17261 list_ret1 = loc_list_from_tree (offset, 0, context);
17262 if (list_ret1 == 0)
17263 return 0;
17264 add_loc_list (&list_ret, list_ret1);
17265 if (!list_ret)
17266 return 0;
17267 add_loc_descr_to_each (list_ret,
17268 new_loc_descr (DW_OP_plus, 0, 0));
17270 HOST_WIDE_INT value;
17271 if (bytepos.is_constant (&value) && value > 0)
17272 add_loc_descr_to_each (list_ret,
17273 new_loc_descr (DW_OP_plus_uconst, value, 0));
17274 else if (maybe_ne (bytepos, 0))
17275 loc_list_plus_const (list_ret, bytepos);
17276 add_loc_descr_to_each (list_ret,
17277 new_loc_descr (DW_OP_stack_value, 0, 0));
17279 return list_ret;
17282 /* Set LOC to the next operation that is not a DW_OP_nop operation. In the case
17283 all operations from LOC are nops, move to the last one. Insert in NOPS all
17284 operations that are skipped. */
17286 static void
17287 loc_descr_to_next_no_nop (dw_loc_descr_ref &loc,
17288 hash_set<dw_loc_descr_ref> &nops)
17290 while (loc->dw_loc_next != NULL && loc->dw_loc_opc == DW_OP_nop)
17292 nops.add (loc);
17293 loc = loc->dw_loc_next;
17297 /* Helper for loc_descr_without_nops: free the location description operation
17298 P. */
17300 bool
17301 free_loc_descr (const dw_loc_descr_ref &loc, void *data ATTRIBUTE_UNUSED)
17303 ggc_free (loc);
17304 return true;
17307 /* Remove all DW_OP_nop operations from LOC except, if it exists, the one that
17308 finishes LOC. */
17310 static void
17311 loc_descr_without_nops (dw_loc_descr_ref &loc)
17313 if (loc->dw_loc_opc == DW_OP_nop && loc->dw_loc_next == NULL)
17314 return;
17316 /* Set of all DW_OP_nop operations we remove. */
17317 hash_set<dw_loc_descr_ref> nops;
17319 /* First, strip all prefix NOP operations in order to keep the head of the
17320 operations list. */
17321 loc_descr_to_next_no_nop (loc, nops);
17323 for (dw_loc_descr_ref cur = loc; cur != NULL;)
17325 /* For control flow operations: strip "prefix" nops in destination
17326 labels. */
17327 if (cur->dw_loc_oprnd1.val_class == dw_val_class_loc)
17328 loc_descr_to_next_no_nop (cur->dw_loc_oprnd1.v.val_loc, nops);
17329 if (cur->dw_loc_oprnd2.val_class == dw_val_class_loc)
17330 loc_descr_to_next_no_nop (cur->dw_loc_oprnd2.v.val_loc, nops);
17332 /* Do the same for the operations that follow, then move to the next
17333 iteration. */
17334 if (cur->dw_loc_next != NULL)
17335 loc_descr_to_next_no_nop (cur->dw_loc_next, nops);
17336 cur = cur->dw_loc_next;
17339 nops.traverse<void *, free_loc_descr> (NULL);
17343 struct dwarf_procedure_info;
17345 /* Helper structure for location descriptions generation. */
17346 struct loc_descr_context
17348 /* The type that is implicitly referenced by DW_OP_push_object_address, or
17349 NULL_TREE if DW_OP_push_object_address in invalid for this location
17350 description. This is used when processing PLACEHOLDER_EXPR nodes. */
17351 tree context_type;
17352 /* The ..._DECL node that should be translated as a
17353 DW_OP_push_object_address operation. */
17354 tree base_decl;
17355 /* Information about the DWARF procedure we are currently generating. NULL if
17356 we are not generating a DWARF procedure. */
17357 struct dwarf_procedure_info *dpi;
17358 /* True if integral PLACEHOLDER_EXPR stands for the first argument passed
17359 by consumer. Used for DW_TAG_generic_subrange attributes. */
17360 bool placeholder_arg;
17361 /* True if PLACEHOLDER_EXPR has been seen. */
17362 bool placeholder_seen;
17365 /* DWARF procedures generation
17367 DWARF expressions (aka. location descriptions) are used to encode variable
17368 things such as sizes or offsets. Such computations can have redundant parts
17369 that can be factorized in order to reduce the size of the output debug
17370 information. This is the whole point of DWARF procedures.
17372 Thanks to stor-layout.c, size and offset expressions in GENERIC trees are
17373 already factorized into functions ("size functions") in order to handle very
17374 big and complex types. Such functions are quite simple: they have integral
17375 arguments, they return an integral result and their body contains only a
17376 return statement with arithmetic expressions. This is the only kind of
17377 function we are interested in translating into DWARF procedures, here.
17379 DWARF expressions and DWARF procedure are executed using a stack, so we have
17380 to define some calling convention for them to interact. Let's say that:
17382 - Before calling a DWARF procedure, DWARF expressions must push on the stack
17383 all arguments in reverse order (right-to-left) so that when the DWARF
17384 procedure execution starts, the first argument is the top of the stack.
17386 - Then, when returning, the DWARF procedure must have consumed all arguments
17387 on the stack, must have pushed the result and touched nothing else.
17389 - Each integral argument and the result are integral types can be hold in a
17390 single stack slot.
17392 - We call "frame offset" the number of stack slots that are "under DWARF
17393 procedure control": it includes the arguments slots, the temporaries and
17394 the result slot. Thus, it is equal to the number of arguments when the
17395 procedure execution starts and must be equal to one (the result) when it
17396 returns. */
17398 /* Helper structure used when generating operations for a DWARF procedure. */
17399 struct dwarf_procedure_info
17401 /* The FUNCTION_DECL node corresponding to the DWARF procedure that is
17402 currently translated. */
17403 tree fndecl;
17404 /* The number of arguments FNDECL takes. */
17405 unsigned args_count;
17408 /* Return a pointer to a newly created DIE node for a DWARF procedure. Add
17409 LOCATION as its DW_AT_location attribute. If FNDECL is not NULL_TREE,
17410 equate it to this DIE. */
17412 static dw_die_ref
17413 new_dwarf_proc_die (dw_loc_descr_ref location, tree fndecl,
17414 dw_die_ref parent_die)
17416 dw_die_ref dwarf_proc_die;
17418 if ((dwarf_version < 3 && dwarf_strict)
17419 || location == NULL)
17420 return NULL;
17422 dwarf_proc_die = new_die (DW_TAG_dwarf_procedure, parent_die, fndecl);
17423 if (fndecl)
17424 equate_decl_number_to_die (fndecl, dwarf_proc_die);
17425 add_AT_loc (dwarf_proc_die, DW_AT_location, location);
17426 return dwarf_proc_die;
17429 /* Return whether TYPE is a supported type as a DWARF procedure argument
17430 type or return type (we handle only scalar types and pointer types that
17431 aren't wider than the DWARF expression evaluation stack. */
17433 static bool
17434 is_handled_procedure_type (tree type)
17436 return ((INTEGRAL_TYPE_P (type)
17437 || TREE_CODE (type) == OFFSET_TYPE
17438 || TREE_CODE (type) == POINTER_TYPE)
17439 && int_size_in_bytes (type) <= DWARF2_ADDR_SIZE);
17442 /* Helper for resolve_args_picking: do the same but stop when coming across
17443 visited nodes. For each node we visit, register in FRAME_OFFSETS the frame
17444 offset *before* evaluating the corresponding operation. */
17446 static bool
17447 resolve_args_picking_1 (dw_loc_descr_ref loc, unsigned initial_frame_offset,
17448 struct dwarf_procedure_info *dpi,
17449 hash_map<dw_loc_descr_ref, unsigned> &frame_offsets)
17451 /* The "frame_offset" identifier is already used to name a macro... */
17452 unsigned frame_offset_ = initial_frame_offset;
17453 dw_loc_descr_ref l;
17455 for (l = loc; l != NULL;)
17457 bool existed;
17458 unsigned &l_frame_offset = frame_offsets.get_or_insert (l, &existed);
17460 /* If we already met this node, there is nothing to compute anymore. */
17461 if (existed)
17463 /* Make sure that the stack size is consistent wherever the execution
17464 flow comes from. */
17465 gcc_assert ((unsigned) l_frame_offset == frame_offset_);
17466 break;
17468 l_frame_offset = frame_offset_;
17470 /* If needed, relocate the picking offset with respect to the frame
17471 offset. */
17472 if (l->frame_offset_rel)
17474 unsigned HOST_WIDE_INT off;
17475 switch (l->dw_loc_opc)
17477 case DW_OP_pick:
17478 off = l->dw_loc_oprnd1.v.val_unsigned;
17479 break;
17480 case DW_OP_dup:
17481 off = 0;
17482 break;
17483 case DW_OP_over:
17484 off = 1;
17485 break;
17486 default:
17487 gcc_unreachable ();
17489 /* frame_offset_ is the size of the current stack frame, including
17490 incoming arguments. Besides, the arguments are pushed
17491 right-to-left. Thus, in order to access the Nth argument from
17492 this operation node, the picking has to skip temporaries *plus*
17493 one stack slot per argument (0 for the first one, 1 for the second
17494 one, etc.).
17496 The targetted argument number (N) is already set as the operand,
17497 and the number of temporaries can be computed with:
17498 frame_offsets_ - dpi->args_count */
17499 off += frame_offset_ - dpi->args_count;
17501 /* DW_OP_pick handles only offsets from 0 to 255 (inclusive)... */
17502 if (off > 255)
17503 return false;
17505 if (off == 0)
17507 l->dw_loc_opc = DW_OP_dup;
17508 l->dw_loc_oprnd1.v.val_unsigned = 0;
17510 else if (off == 1)
17512 l->dw_loc_opc = DW_OP_over;
17513 l->dw_loc_oprnd1.v.val_unsigned = 0;
17515 else
17517 l->dw_loc_opc = DW_OP_pick;
17518 l->dw_loc_oprnd1.v.val_unsigned = off;
17522 /* Update frame_offset according to the effect the current operation has
17523 on the stack. */
17524 switch (l->dw_loc_opc)
17526 case DW_OP_deref:
17527 case DW_OP_swap:
17528 case DW_OP_rot:
17529 case DW_OP_abs:
17530 case DW_OP_neg:
17531 case DW_OP_not:
17532 case DW_OP_plus_uconst:
17533 case DW_OP_skip:
17534 case DW_OP_reg0:
17535 case DW_OP_reg1:
17536 case DW_OP_reg2:
17537 case DW_OP_reg3:
17538 case DW_OP_reg4:
17539 case DW_OP_reg5:
17540 case DW_OP_reg6:
17541 case DW_OP_reg7:
17542 case DW_OP_reg8:
17543 case DW_OP_reg9:
17544 case DW_OP_reg10:
17545 case DW_OP_reg11:
17546 case DW_OP_reg12:
17547 case DW_OP_reg13:
17548 case DW_OP_reg14:
17549 case DW_OP_reg15:
17550 case DW_OP_reg16:
17551 case DW_OP_reg17:
17552 case DW_OP_reg18:
17553 case DW_OP_reg19:
17554 case DW_OP_reg20:
17555 case DW_OP_reg21:
17556 case DW_OP_reg22:
17557 case DW_OP_reg23:
17558 case DW_OP_reg24:
17559 case DW_OP_reg25:
17560 case DW_OP_reg26:
17561 case DW_OP_reg27:
17562 case DW_OP_reg28:
17563 case DW_OP_reg29:
17564 case DW_OP_reg30:
17565 case DW_OP_reg31:
17566 case DW_OP_bregx:
17567 case DW_OP_piece:
17568 case DW_OP_deref_size:
17569 case DW_OP_nop:
17570 case DW_OP_bit_piece:
17571 case DW_OP_implicit_value:
17572 case DW_OP_stack_value:
17573 break;
17575 case DW_OP_addr:
17576 case DW_OP_const1u:
17577 case DW_OP_const1s:
17578 case DW_OP_const2u:
17579 case DW_OP_const2s:
17580 case DW_OP_const4u:
17581 case DW_OP_const4s:
17582 case DW_OP_const8u:
17583 case DW_OP_const8s:
17584 case DW_OP_constu:
17585 case DW_OP_consts:
17586 case DW_OP_dup:
17587 case DW_OP_over:
17588 case DW_OP_pick:
17589 case DW_OP_lit0:
17590 case DW_OP_lit1:
17591 case DW_OP_lit2:
17592 case DW_OP_lit3:
17593 case DW_OP_lit4:
17594 case DW_OP_lit5:
17595 case DW_OP_lit6:
17596 case DW_OP_lit7:
17597 case DW_OP_lit8:
17598 case DW_OP_lit9:
17599 case DW_OP_lit10:
17600 case DW_OP_lit11:
17601 case DW_OP_lit12:
17602 case DW_OP_lit13:
17603 case DW_OP_lit14:
17604 case DW_OP_lit15:
17605 case DW_OP_lit16:
17606 case DW_OP_lit17:
17607 case DW_OP_lit18:
17608 case DW_OP_lit19:
17609 case DW_OP_lit20:
17610 case DW_OP_lit21:
17611 case DW_OP_lit22:
17612 case DW_OP_lit23:
17613 case DW_OP_lit24:
17614 case DW_OP_lit25:
17615 case DW_OP_lit26:
17616 case DW_OP_lit27:
17617 case DW_OP_lit28:
17618 case DW_OP_lit29:
17619 case DW_OP_lit30:
17620 case DW_OP_lit31:
17621 case DW_OP_breg0:
17622 case DW_OP_breg1:
17623 case DW_OP_breg2:
17624 case DW_OP_breg3:
17625 case DW_OP_breg4:
17626 case DW_OP_breg5:
17627 case DW_OP_breg6:
17628 case DW_OP_breg7:
17629 case DW_OP_breg8:
17630 case DW_OP_breg9:
17631 case DW_OP_breg10:
17632 case DW_OP_breg11:
17633 case DW_OP_breg12:
17634 case DW_OP_breg13:
17635 case DW_OP_breg14:
17636 case DW_OP_breg15:
17637 case DW_OP_breg16:
17638 case DW_OP_breg17:
17639 case DW_OP_breg18:
17640 case DW_OP_breg19:
17641 case DW_OP_breg20:
17642 case DW_OP_breg21:
17643 case DW_OP_breg22:
17644 case DW_OP_breg23:
17645 case DW_OP_breg24:
17646 case DW_OP_breg25:
17647 case DW_OP_breg26:
17648 case DW_OP_breg27:
17649 case DW_OP_breg28:
17650 case DW_OP_breg29:
17651 case DW_OP_breg30:
17652 case DW_OP_breg31:
17653 case DW_OP_fbreg:
17654 case DW_OP_push_object_address:
17655 case DW_OP_call_frame_cfa:
17656 case DW_OP_GNU_variable_value:
17657 ++frame_offset_;
17658 break;
17660 case DW_OP_drop:
17661 case DW_OP_xderef:
17662 case DW_OP_and:
17663 case DW_OP_div:
17664 case DW_OP_minus:
17665 case DW_OP_mod:
17666 case DW_OP_mul:
17667 case DW_OP_or:
17668 case DW_OP_plus:
17669 case DW_OP_shl:
17670 case DW_OP_shr:
17671 case DW_OP_shra:
17672 case DW_OP_xor:
17673 case DW_OP_bra:
17674 case DW_OP_eq:
17675 case DW_OP_ge:
17676 case DW_OP_gt:
17677 case DW_OP_le:
17678 case DW_OP_lt:
17679 case DW_OP_ne:
17680 case DW_OP_regx:
17681 case DW_OP_xderef_size:
17682 --frame_offset_;
17683 break;
17685 case DW_OP_call2:
17686 case DW_OP_call4:
17687 case DW_OP_call_ref:
17689 dw_die_ref dwarf_proc = l->dw_loc_oprnd1.v.val_die_ref.die;
17690 int *stack_usage = dwarf_proc_stack_usage_map->get (dwarf_proc);
17692 if (stack_usage == NULL)
17693 return false;
17694 frame_offset_ += *stack_usage;
17695 break;
17698 case DW_OP_implicit_pointer:
17699 case DW_OP_entry_value:
17700 case DW_OP_const_type:
17701 case DW_OP_regval_type:
17702 case DW_OP_deref_type:
17703 case DW_OP_convert:
17704 case DW_OP_reinterpret:
17705 case DW_OP_form_tls_address:
17706 case DW_OP_GNU_push_tls_address:
17707 case DW_OP_GNU_uninit:
17708 case DW_OP_GNU_encoded_addr:
17709 case DW_OP_GNU_implicit_pointer:
17710 case DW_OP_GNU_entry_value:
17711 case DW_OP_GNU_const_type:
17712 case DW_OP_GNU_regval_type:
17713 case DW_OP_GNU_deref_type:
17714 case DW_OP_GNU_convert:
17715 case DW_OP_GNU_reinterpret:
17716 case DW_OP_GNU_parameter_ref:
17717 /* loc_list_from_tree will probably not output these operations for
17718 size functions, so assume they will not appear here. */
17719 /* Fall through... */
17721 default:
17722 gcc_unreachable ();
17725 /* Now, follow the control flow (except subroutine calls). */
17726 switch (l->dw_loc_opc)
17728 case DW_OP_bra:
17729 if (!resolve_args_picking_1 (l->dw_loc_next, frame_offset_, dpi,
17730 frame_offsets))
17731 return false;
17732 /* Fall through. */
17734 case DW_OP_skip:
17735 l = l->dw_loc_oprnd1.v.val_loc;
17736 break;
17738 case DW_OP_stack_value:
17739 return true;
17741 default:
17742 l = l->dw_loc_next;
17743 break;
17747 return true;
17750 /* Make a DFS over operations reachable through LOC (i.e. follow branch
17751 operations) in order to resolve the operand of DW_OP_pick operations that
17752 target DWARF procedure arguments (DPI). INITIAL_FRAME_OFFSET is the frame
17753 offset *before* LOC is executed. Return if all relocations were
17754 successful. */
17756 static bool
17757 resolve_args_picking (dw_loc_descr_ref loc, unsigned initial_frame_offset,
17758 struct dwarf_procedure_info *dpi)
17760 /* Associate to all visited operations the frame offset *before* evaluating
17761 this operation. */
17762 hash_map<dw_loc_descr_ref, unsigned> frame_offsets;
17764 return resolve_args_picking_1 (loc, initial_frame_offset, dpi,
17765 frame_offsets);
17768 /* Try to generate a DWARF procedure that computes the same result as FNDECL.
17769 Return NULL if it is not possible. */
17771 static dw_die_ref
17772 function_to_dwarf_procedure (tree fndecl)
17774 struct loc_descr_context ctx;
17775 struct dwarf_procedure_info dpi;
17776 dw_die_ref dwarf_proc_die;
17777 tree tree_body = DECL_SAVED_TREE (fndecl);
17778 dw_loc_descr_ref loc_body, epilogue;
17780 tree cursor;
17781 unsigned i;
17783 /* Do not generate multiple DWARF procedures for the same function
17784 declaration. */
17785 dwarf_proc_die = lookup_decl_die (fndecl);
17786 if (dwarf_proc_die != NULL)
17787 return dwarf_proc_die;
17789 /* DWARF procedures are available starting with the DWARFv3 standard. */
17790 if (dwarf_version < 3 && dwarf_strict)
17791 return NULL;
17793 /* We handle only functions for which we still have a body, that return a
17794 supported type and that takes arguments with supported types. Note that
17795 there is no point translating functions that return nothing. */
17796 if (tree_body == NULL_TREE
17797 || DECL_RESULT (fndecl) == NULL_TREE
17798 || !is_handled_procedure_type (TREE_TYPE (DECL_RESULT (fndecl))))
17799 return NULL;
17801 for (cursor = DECL_ARGUMENTS (fndecl);
17802 cursor != NULL_TREE;
17803 cursor = TREE_CHAIN (cursor))
17804 if (!is_handled_procedure_type (TREE_TYPE (cursor)))
17805 return NULL;
17807 /* Match only "expr" in: RETURN_EXPR (MODIFY_EXPR (RESULT_DECL, expr)). */
17808 if (TREE_CODE (tree_body) != RETURN_EXPR)
17809 return NULL;
17810 tree_body = TREE_OPERAND (tree_body, 0);
17811 if (TREE_CODE (tree_body) != MODIFY_EXPR
17812 || TREE_OPERAND (tree_body, 0) != DECL_RESULT (fndecl))
17813 return NULL;
17814 tree_body = TREE_OPERAND (tree_body, 1);
17816 /* Try to translate the body expression itself. Note that this will probably
17817 cause an infinite recursion if its call graph has a cycle. This is very
17818 unlikely for size functions, however, so don't bother with such things at
17819 the moment. */
17820 ctx.context_type = NULL_TREE;
17821 ctx.base_decl = NULL_TREE;
17822 ctx.dpi = &dpi;
17823 ctx.placeholder_arg = false;
17824 ctx.placeholder_seen = false;
17825 dpi.fndecl = fndecl;
17826 dpi.args_count = list_length (DECL_ARGUMENTS (fndecl));
17827 loc_body = loc_descriptor_from_tree (tree_body, 0, &ctx);
17828 if (!loc_body)
17829 return NULL;
17831 /* After evaluating all operands in "loc_body", we should still have on the
17832 stack all arguments plus the desired function result (top of the stack).
17833 Generate code in order to keep only the result in our stack frame. */
17834 epilogue = NULL;
17835 for (i = 0; i < dpi.args_count; ++i)
17837 dw_loc_descr_ref op_couple = new_loc_descr (DW_OP_swap, 0, 0);
17838 op_couple->dw_loc_next = new_loc_descr (DW_OP_drop, 0, 0);
17839 op_couple->dw_loc_next->dw_loc_next = epilogue;
17840 epilogue = op_couple;
17842 add_loc_descr (&loc_body, epilogue);
17843 if (!resolve_args_picking (loc_body, dpi.args_count, &dpi))
17844 return NULL;
17846 /* Trailing nops from loc_descriptor_from_tree (if any) cannot be removed
17847 because they are considered useful. Now there is an epilogue, they are
17848 not anymore, so give it another try. */
17849 loc_descr_without_nops (loc_body);
17851 /* fndecl may be used both as a regular DW_TAG_subprogram DIE and as
17852 a DW_TAG_dwarf_procedure, so we may have a conflict, here. It's unlikely,
17853 though, given that size functions do not come from source, so they should
17854 not have a dedicated DW_TAG_subprogram DIE. */
17855 dwarf_proc_die
17856 = new_dwarf_proc_die (loc_body, fndecl,
17857 get_context_die (DECL_CONTEXT (fndecl)));
17859 /* The called DWARF procedure consumes one stack slot per argument and
17860 returns one stack slot. */
17861 dwarf_proc_stack_usage_map->put (dwarf_proc_die, 1 - dpi.args_count);
17863 return dwarf_proc_die;
17867 /* Generate Dwarf location list representing LOC.
17868 If WANT_ADDRESS is false, expression computing LOC will be computed
17869 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
17870 if WANT_ADDRESS is 2, expression computing address useable in location
17871 will be returned (i.e. DW_OP_reg can be used
17872 to refer to register values).
17874 CONTEXT provides information to customize the location descriptions
17875 generation. Its context_type field specifies what type is implicitly
17876 referenced by DW_OP_push_object_address. If it is NULL_TREE, this operation
17877 will not be generated.
17879 Its DPI field determines whether we are generating a DWARF expression for a
17880 DWARF procedure, so PARM_DECL references are processed specifically.
17882 If CONTEXT is NULL, the behavior is the same as if context_type, base_decl
17883 and dpi fields were null. */
17885 static dw_loc_list_ref
17886 loc_list_from_tree_1 (tree loc, int want_address,
17887 struct loc_descr_context *context)
17889 dw_loc_descr_ref ret = NULL, ret1 = NULL;
17890 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
17891 int have_address = 0;
17892 enum dwarf_location_atom op;
17894 /* ??? Most of the time we do not take proper care for sign/zero
17895 extending the values properly. Hopefully this won't be a real
17896 problem... */
17898 if (context != NULL
17899 && context->base_decl == loc
17900 && want_address == 0)
17902 if (dwarf_version >= 3 || !dwarf_strict)
17903 return new_loc_list (new_loc_descr (DW_OP_push_object_address, 0, 0),
17904 NULL, 0, NULL, 0, NULL);
17905 else
17906 return NULL;
17909 switch (TREE_CODE (loc))
17911 case ERROR_MARK:
17912 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
17913 return 0;
17915 case PLACEHOLDER_EXPR:
17916 /* This case involves extracting fields from an object to determine the
17917 position of other fields. It is supposed to appear only as the first
17918 operand of COMPONENT_REF nodes and to reference precisely the type
17919 that the context allows. */
17920 if (context != NULL
17921 && TREE_TYPE (loc) == context->context_type
17922 && want_address >= 1)
17924 if (dwarf_version >= 3 || !dwarf_strict)
17926 ret = new_loc_descr (DW_OP_push_object_address, 0, 0);
17927 have_address = 1;
17928 break;
17930 else
17931 return NULL;
17933 /* For DW_TAG_generic_subrange attributes, PLACEHOLDER_EXPR stands for
17934 the single argument passed by consumer. */
17935 else if (context != NULL
17936 && context->placeholder_arg
17937 && INTEGRAL_TYPE_P (TREE_TYPE (loc))
17938 && want_address == 0)
17940 ret = new_loc_descr (DW_OP_pick, 0, 0);
17941 ret->frame_offset_rel = 1;
17942 context->placeholder_seen = true;
17943 break;
17945 else
17946 expansion_failed (loc, NULL_RTX,
17947 "PLACEHOLDER_EXPR for an unexpected type");
17948 break;
17950 case CALL_EXPR:
17952 const int nargs = call_expr_nargs (loc);
17953 tree callee = get_callee_fndecl (loc);
17954 int i;
17955 dw_die_ref dwarf_proc;
17957 if (callee == NULL_TREE)
17958 goto call_expansion_failed;
17960 /* We handle only functions that return an integer. */
17961 if (!is_handled_procedure_type (TREE_TYPE (TREE_TYPE (callee))))
17962 goto call_expansion_failed;
17964 dwarf_proc = function_to_dwarf_procedure (callee);
17965 if (dwarf_proc == NULL)
17966 goto call_expansion_failed;
17968 /* Evaluate arguments right-to-left so that the first argument will
17969 be the top-most one on the stack. */
17970 for (i = nargs - 1; i >= 0; --i)
17972 dw_loc_descr_ref loc_descr
17973 = loc_descriptor_from_tree (CALL_EXPR_ARG (loc, i), 0,
17974 context);
17976 if (loc_descr == NULL)
17977 goto call_expansion_failed;
17979 add_loc_descr (&ret, loc_descr);
17982 ret1 = new_loc_descr (DW_OP_call4, 0, 0);
17983 ret1->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
17984 ret1->dw_loc_oprnd1.v.val_die_ref.die = dwarf_proc;
17985 ret1->dw_loc_oprnd1.v.val_die_ref.external = 0;
17986 add_loc_descr (&ret, ret1);
17987 break;
17989 call_expansion_failed:
17990 expansion_failed (loc, NULL_RTX, "CALL_EXPR");
17991 /* There are no opcodes for these operations. */
17992 return 0;
17995 case PREINCREMENT_EXPR:
17996 case PREDECREMENT_EXPR:
17997 case POSTINCREMENT_EXPR:
17998 case POSTDECREMENT_EXPR:
17999 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
18000 /* There are no opcodes for these operations. */
18001 return 0;
18003 case ADDR_EXPR:
18004 /* If we already want an address, see if there is INDIRECT_REF inside
18005 e.g. for &this->field. */
18006 if (want_address)
18008 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
18009 (loc, want_address == 2, context);
18010 if (list_ret)
18011 have_address = 1;
18012 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
18013 && (ret = cst_pool_loc_descr (loc)))
18014 have_address = 1;
18016 /* Otherwise, process the argument and look for the address. */
18017 if (!list_ret && !ret)
18018 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 1, context);
18019 else
18021 if (want_address)
18022 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
18023 return NULL;
18025 break;
18027 case VAR_DECL:
18028 if (DECL_THREAD_LOCAL_P (loc))
18030 rtx rtl;
18031 enum dwarf_location_atom tls_op;
18032 enum dtprel_bool dtprel = dtprel_false;
18034 if (targetm.have_tls)
18036 /* If this is not defined, we have no way to emit the
18037 data. */
18038 if (!targetm.asm_out.output_dwarf_dtprel)
18039 return 0;
18041 /* The way DW_OP_GNU_push_tls_address is specified, we
18042 can only look up addresses of objects in the current
18043 module. We used DW_OP_addr as first op, but that's
18044 wrong, because DW_OP_addr is relocated by the debug
18045 info consumer, while DW_OP_GNU_push_tls_address
18046 operand shouldn't be. */
18047 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
18048 return 0;
18049 dtprel = dtprel_true;
18050 /* We check for DWARF 5 here because gdb did not implement
18051 DW_OP_form_tls_address until after 7.12. */
18052 tls_op = (dwarf_version >= 5 ? DW_OP_form_tls_address
18053 : DW_OP_GNU_push_tls_address);
18055 else
18057 if (!targetm.emutls.debug_form_tls_address
18058 || !(dwarf_version >= 3 || !dwarf_strict))
18059 return 0;
18060 /* We stuffed the control variable into the DECL_VALUE_EXPR
18061 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
18062 no longer appear in gimple code. We used the control
18063 variable in specific so that we could pick it up here. */
18064 loc = DECL_VALUE_EXPR (loc);
18065 tls_op = DW_OP_form_tls_address;
18068 rtl = rtl_for_decl_location (loc);
18069 if (rtl == NULL_RTX)
18070 return 0;
18072 if (!MEM_P (rtl))
18073 return 0;
18074 rtl = XEXP (rtl, 0);
18075 if (! CONSTANT_P (rtl))
18076 return 0;
18078 ret = new_addr_loc_descr (rtl, dtprel);
18079 ret1 = new_loc_descr (tls_op, 0, 0);
18080 add_loc_descr (&ret, ret1);
18082 have_address = 1;
18083 break;
18085 /* FALLTHRU */
18087 case PARM_DECL:
18088 if (context != NULL && context->dpi != NULL
18089 && DECL_CONTEXT (loc) == context->dpi->fndecl)
18091 /* We are generating code for a DWARF procedure and we want to access
18092 one of its arguments: find the appropriate argument offset and let
18093 the resolve_args_picking pass compute the offset that complies
18094 with the stack frame size. */
18095 unsigned i = 0;
18096 tree cursor;
18098 for (cursor = DECL_ARGUMENTS (context->dpi->fndecl);
18099 cursor != NULL_TREE && cursor != loc;
18100 cursor = TREE_CHAIN (cursor), ++i)
18102 /* If we are translating a DWARF procedure, all referenced parameters
18103 must belong to the current function. */
18104 gcc_assert (cursor != NULL_TREE);
18106 ret = new_loc_descr (DW_OP_pick, i, 0);
18107 ret->frame_offset_rel = 1;
18108 break;
18110 /* FALLTHRU */
18112 case RESULT_DECL:
18113 if (DECL_HAS_VALUE_EXPR_P (loc))
18114 return loc_list_from_tree_1 (DECL_VALUE_EXPR (loc),
18115 want_address, context);
18116 /* FALLTHRU */
18118 case FUNCTION_DECL:
18120 rtx rtl;
18121 var_loc_list *loc_list = lookup_decl_loc (loc);
18123 if (loc_list && loc_list->first)
18125 list_ret = dw_loc_list (loc_list, loc, want_address);
18126 have_address = want_address != 0;
18127 break;
18129 rtl = rtl_for_decl_location (loc);
18130 if (rtl == NULL_RTX)
18132 if (TREE_CODE (loc) != FUNCTION_DECL
18133 && early_dwarf
18134 && current_function_decl
18135 && want_address != 1
18136 && ! DECL_IGNORED_P (loc)
18137 && (INTEGRAL_TYPE_P (TREE_TYPE (loc))
18138 || POINTER_TYPE_P (TREE_TYPE (loc)))
18139 && DECL_CONTEXT (loc) == current_function_decl
18140 && (GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (TREE_TYPE (loc)))
18141 <= DWARF2_ADDR_SIZE))
18143 dw_die_ref ref = lookup_decl_die (loc);
18144 ret = new_loc_descr (DW_OP_GNU_variable_value, 0, 0);
18145 if (ref)
18147 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
18148 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
18149 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
18151 else
18153 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
18154 ret->dw_loc_oprnd1.v.val_decl_ref = loc;
18156 break;
18158 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
18159 return 0;
18161 else if (CONST_INT_P (rtl))
18163 HOST_WIDE_INT val = INTVAL (rtl);
18164 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
18165 val &= GET_MODE_MASK (DECL_MODE (loc));
18166 ret = int_loc_descriptor (val);
18168 else if (GET_CODE (rtl) == CONST_STRING)
18170 expansion_failed (loc, NULL_RTX, "CONST_STRING");
18171 return 0;
18173 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
18174 ret = new_addr_loc_descr (rtl, dtprel_false);
18175 else
18177 machine_mode mode, mem_mode;
18179 /* Certain constructs can only be represented at top-level. */
18180 if (want_address == 2)
18182 ret = loc_descriptor (rtl, VOIDmode,
18183 VAR_INIT_STATUS_INITIALIZED);
18184 have_address = 1;
18186 else
18188 mode = GET_MODE (rtl);
18189 mem_mode = VOIDmode;
18190 if (MEM_P (rtl))
18192 mem_mode = mode;
18193 mode = get_address_mode (rtl);
18194 rtl = XEXP (rtl, 0);
18195 have_address = 1;
18197 ret = mem_loc_descriptor (rtl, mode, mem_mode,
18198 VAR_INIT_STATUS_INITIALIZED);
18200 if (!ret)
18201 expansion_failed (loc, rtl,
18202 "failed to produce loc descriptor for rtl");
18205 break;
18207 case MEM_REF:
18208 if (!integer_zerop (TREE_OPERAND (loc, 1)))
18210 have_address = 1;
18211 goto do_plus;
18213 /* Fallthru. */
18214 case INDIRECT_REF:
18215 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
18216 have_address = 1;
18217 break;
18219 case TARGET_MEM_REF:
18220 case SSA_NAME:
18221 case DEBUG_EXPR_DECL:
18222 return NULL;
18224 case COMPOUND_EXPR:
18225 return loc_list_from_tree_1 (TREE_OPERAND (loc, 1), want_address,
18226 context);
18228 CASE_CONVERT:
18229 case VIEW_CONVERT_EXPR:
18230 case SAVE_EXPR:
18231 case MODIFY_EXPR:
18232 case NON_LVALUE_EXPR:
18233 return loc_list_from_tree_1 (TREE_OPERAND (loc, 0), want_address,
18234 context);
18236 case COMPONENT_REF:
18237 case BIT_FIELD_REF:
18238 case ARRAY_REF:
18239 case ARRAY_RANGE_REF:
18240 case REALPART_EXPR:
18241 case IMAGPART_EXPR:
18243 tree obj, offset;
18244 poly_int64 bitsize, bitpos, bytepos;
18245 machine_mode mode;
18246 int unsignedp, reversep, volatilep = 0;
18248 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
18249 &unsignedp, &reversep, &volatilep);
18251 gcc_assert (obj != loc);
18253 list_ret = loc_list_from_tree_1 (obj,
18254 want_address == 2
18255 && known_eq (bitpos, 0)
18256 && !offset ? 2 : 1,
18257 context);
18258 /* TODO: We can extract value of the small expression via shifting even
18259 for nonzero bitpos. */
18260 if (list_ret == 0)
18261 return 0;
18262 if (!multiple_p (bitpos, BITS_PER_UNIT, &bytepos)
18263 || !multiple_p (bitsize, BITS_PER_UNIT))
18265 expansion_failed (loc, NULL_RTX,
18266 "bitfield access");
18267 return 0;
18270 if (offset != NULL_TREE)
18272 /* Variable offset. */
18273 list_ret1 = loc_list_from_tree_1 (offset, 0, context);
18274 if (list_ret1 == 0)
18275 return 0;
18276 add_loc_list (&list_ret, list_ret1);
18277 if (!list_ret)
18278 return 0;
18279 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
18282 HOST_WIDE_INT value;
18283 if (bytepos.is_constant (&value) && value > 0)
18284 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst,
18285 value, 0));
18286 else if (maybe_ne (bytepos, 0))
18287 loc_list_plus_const (list_ret, bytepos);
18289 have_address = 1;
18290 break;
18293 case INTEGER_CST:
18294 if ((want_address || !tree_fits_shwi_p (loc))
18295 && (ret = cst_pool_loc_descr (loc)))
18296 have_address = 1;
18297 else if (want_address == 2
18298 && tree_fits_shwi_p (loc)
18299 && (ret = address_of_int_loc_descriptor
18300 (int_size_in_bytes (TREE_TYPE (loc)),
18301 tree_to_shwi (loc))))
18302 have_address = 1;
18303 else if (tree_fits_shwi_p (loc))
18304 ret = int_loc_descriptor (tree_to_shwi (loc));
18305 else if (tree_fits_uhwi_p (loc))
18306 ret = uint_loc_descriptor (tree_to_uhwi (loc));
18307 else
18309 expansion_failed (loc, NULL_RTX,
18310 "Integer operand is not host integer");
18311 return 0;
18313 break;
18315 case CONSTRUCTOR:
18316 case REAL_CST:
18317 case STRING_CST:
18318 case COMPLEX_CST:
18319 if ((ret = cst_pool_loc_descr (loc)))
18320 have_address = 1;
18321 else if (TREE_CODE (loc) == CONSTRUCTOR)
18323 tree type = TREE_TYPE (loc);
18324 unsigned HOST_WIDE_INT size = int_size_in_bytes (type);
18325 unsigned HOST_WIDE_INT offset = 0;
18326 unsigned HOST_WIDE_INT cnt;
18327 constructor_elt *ce;
18329 if (TREE_CODE (type) == RECORD_TYPE)
18331 /* This is very limited, but it's enough to output
18332 pointers to member functions, as long as the
18333 referenced function is defined in the current
18334 translation unit. */
18335 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (loc), cnt, ce)
18337 tree val = ce->value;
18339 tree field = ce->index;
18341 if (val)
18342 STRIP_NOPS (val);
18344 if (!field || DECL_BIT_FIELD (field))
18346 expansion_failed (loc, NULL_RTX,
18347 "bitfield in record type constructor");
18348 size = offset = (unsigned HOST_WIDE_INT)-1;
18349 ret = NULL;
18350 break;
18353 HOST_WIDE_INT fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
18354 unsigned HOST_WIDE_INT pos = int_byte_position (field);
18355 gcc_assert (pos + fieldsize <= size);
18356 if (pos < offset)
18358 expansion_failed (loc, NULL_RTX,
18359 "out-of-order fields in record constructor");
18360 size = offset = (unsigned HOST_WIDE_INT)-1;
18361 ret = NULL;
18362 break;
18364 if (pos > offset)
18366 ret1 = new_loc_descr (DW_OP_piece, pos - offset, 0);
18367 add_loc_descr (&ret, ret1);
18368 offset = pos;
18370 if (val && fieldsize != 0)
18372 ret1 = loc_descriptor_from_tree (val, want_address, context);
18373 if (!ret1)
18375 expansion_failed (loc, NULL_RTX,
18376 "unsupported expression in field");
18377 size = offset = (unsigned HOST_WIDE_INT)-1;
18378 ret = NULL;
18379 break;
18381 add_loc_descr (&ret, ret1);
18383 if (fieldsize)
18385 ret1 = new_loc_descr (DW_OP_piece, fieldsize, 0);
18386 add_loc_descr (&ret, ret1);
18387 offset = pos + fieldsize;
18391 if (offset != size)
18393 ret1 = new_loc_descr (DW_OP_piece, size - offset, 0);
18394 add_loc_descr (&ret, ret1);
18395 offset = size;
18398 have_address = !!want_address;
18400 else
18401 expansion_failed (loc, NULL_RTX,
18402 "constructor of non-record type");
18404 else
18405 /* We can construct small constants here using int_loc_descriptor. */
18406 expansion_failed (loc, NULL_RTX,
18407 "constructor or constant not in constant pool");
18408 break;
18410 case TRUTH_AND_EXPR:
18411 case TRUTH_ANDIF_EXPR:
18412 case BIT_AND_EXPR:
18413 op = DW_OP_and;
18414 goto do_binop;
18416 case TRUTH_XOR_EXPR:
18417 case BIT_XOR_EXPR:
18418 op = DW_OP_xor;
18419 goto do_binop;
18421 case TRUTH_OR_EXPR:
18422 case TRUTH_ORIF_EXPR:
18423 case BIT_IOR_EXPR:
18424 op = DW_OP_or;
18425 goto do_binop;
18427 case FLOOR_DIV_EXPR:
18428 case CEIL_DIV_EXPR:
18429 case ROUND_DIV_EXPR:
18430 case TRUNC_DIV_EXPR:
18431 case EXACT_DIV_EXPR:
18432 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
18433 return 0;
18434 op = DW_OP_div;
18435 goto do_binop;
18437 case MINUS_EXPR:
18438 op = DW_OP_minus;
18439 goto do_binop;
18441 case FLOOR_MOD_EXPR:
18442 case CEIL_MOD_EXPR:
18443 case ROUND_MOD_EXPR:
18444 case TRUNC_MOD_EXPR:
18445 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
18447 op = DW_OP_mod;
18448 goto do_binop;
18450 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
18451 list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
18452 if (list_ret == 0 || list_ret1 == 0)
18453 return 0;
18455 add_loc_list (&list_ret, list_ret1);
18456 if (list_ret == 0)
18457 return 0;
18458 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
18459 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
18460 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
18461 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
18462 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
18463 break;
18465 case MULT_EXPR:
18466 op = DW_OP_mul;
18467 goto do_binop;
18469 case LSHIFT_EXPR:
18470 op = DW_OP_shl;
18471 goto do_binop;
18473 case RSHIFT_EXPR:
18474 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
18475 goto do_binop;
18477 case POINTER_PLUS_EXPR:
18478 case PLUS_EXPR:
18479 do_plus:
18480 if (tree_fits_shwi_p (TREE_OPERAND (loc, 1)))
18482 /* Big unsigned numbers can fit in HOST_WIDE_INT but it may be
18483 smarter to encode their opposite. The DW_OP_plus_uconst operation
18484 takes 1 + X bytes, X being the size of the ULEB128 addend. On the
18485 other hand, a "<push literal>; DW_OP_minus" pattern takes 1 + Y
18486 bytes, Y being the size of the operation that pushes the opposite
18487 of the addend. So let's choose the smallest representation. */
18488 const tree tree_addend = TREE_OPERAND (loc, 1);
18489 offset_int wi_addend;
18490 HOST_WIDE_INT shwi_addend;
18491 dw_loc_descr_ref loc_naddend;
18493 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
18494 if (list_ret == 0)
18495 return 0;
18497 /* Try to get the literal to push. It is the opposite of the addend,
18498 so as we rely on wrapping during DWARF evaluation, first decode
18499 the literal as a "DWARF-sized" signed number. */
18500 wi_addend = wi::to_offset (tree_addend);
18501 wi_addend = wi::sext (wi_addend, DWARF2_ADDR_SIZE * 8);
18502 shwi_addend = wi_addend.to_shwi ();
18503 loc_naddend = (shwi_addend != INTTYPE_MINIMUM (HOST_WIDE_INT))
18504 ? int_loc_descriptor (-shwi_addend)
18505 : NULL;
18507 if (loc_naddend != NULL
18508 && ((unsigned) size_of_uleb128 (shwi_addend)
18509 > size_of_loc_descr (loc_naddend)))
18511 add_loc_descr_to_each (list_ret, loc_naddend);
18512 add_loc_descr_to_each (list_ret,
18513 new_loc_descr (DW_OP_minus, 0, 0));
18515 else
18517 for (dw_loc_descr_ref loc_cur = loc_naddend; loc_cur != NULL; )
18519 loc_naddend = loc_cur;
18520 loc_cur = loc_cur->dw_loc_next;
18521 ggc_free (loc_naddend);
18523 loc_list_plus_const (list_ret, wi_addend.to_shwi ());
18525 break;
18528 op = DW_OP_plus;
18529 goto do_binop;
18531 case LE_EXPR:
18532 op = DW_OP_le;
18533 goto do_comp_binop;
18535 case GE_EXPR:
18536 op = DW_OP_ge;
18537 goto do_comp_binop;
18539 case LT_EXPR:
18540 op = DW_OP_lt;
18541 goto do_comp_binop;
18543 case GT_EXPR:
18544 op = DW_OP_gt;
18545 goto do_comp_binop;
18547 do_comp_binop:
18548 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
18550 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
18551 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
18552 list_ret = loc_list_from_uint_comparison (list_ret, list_ret1,
18553 TREE_CODE (loc));
18554 break;
18556 else
18557 goto do_binop;
18559 case EQ_EXPR:
18560 op = DW_OP_eq;
18561 goto do_binop;
18563 case NE_EXPR:
18564 op = DW_OP_ne;
18565 goto do_binop;
18567 do_binop:
18568 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
18569 list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
18570 if (list_ret == 0 || list_ret1 == 0)
18571 return 0;
18573 add_loc_list (&list_ret, list_ret1);
18574 if (list_ret == 0)
18575 return 0;
18576 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
18577 break;
18579 case TRUTH_NOT_EXPR:
18580 case BIT_NOT_EXPR:
18581 op = DW_OP_not;
18582 goto do_unop;
18584 case ABS_EXPR:
18585 op = DW_OP_abs;
18586 goto do_unop;
18588 case NEGATE_EXPR:
18589 op = DW_OP_neg;
18590 goto do_unop;
18592 do_unop:
18593 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
18594 if (list_ret == 0)
18595 return 0;
18597 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
18598 break;
18600 case MIN_EXPR:
18601 case MAX_EXPR:
18603 const enum tree_code code =
18604 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
18606 loc = build3 (COND_EXPR, TREE_TYPE (loc),
18607 build2 (code, integer_type_node,
18608 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
18609 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
18612 /* fall through */
18614 case COND_EXPR:
18616 dw_loc_descr_ref lhs
18617 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0, context);
18618 dw_loc_list_ref rhs
18619 = loc_list_from_tree_1 (TREE_OPERAND (loc, 2), 0, context);
18620 dw_loc_descr_ref bra_node, jump_node, tmp;
18622 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
18623 if (list_ret == 0 || lhs == 0 || rhs == 0)
18624 return 0;
18626 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
18627 add_loc_descr_to_each (list_ret, bra_node);
18629 add_loc_list (&list_ret, rhs);
18630 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
18631 add_loc_descr_to_each (list_ret, jump_node);
18633 add_loc_descr_to_each (list_ret, lhs);
18634 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
18635 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
18637 /* ??? Need a node to point the skip at. Use a nop. */
18638 tmp = new_loc_descr (DW_OP_nop, 0, 0);
18639 add_loc_descr_to_each (list_ret, tmp);
18640 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
18641 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
18643 break;
18645 case FIX_TRUNC_EXPR:
18646 return 0;
18648 default:
18649 /* Leave front-end specific codes as simply unknown. This comes
18650 up, for instance, with the C STMT_EXPR. */
18651 if ((unsigned int) TREE_CODE (loc)
18652 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
18654 expansion_failed (loc, NULL_RTX,
18655 "language specific tree node");
18656 return 0;
18659 /* Otherwise this is a generic code; we should just lists all of
18660 these explicitly. We forgot one. */
18661 if (flag_checking)
18662 gcc_unreachable ();
18664 /* In a release build, we want to degrade gracefully: better to
18665 generate incomplete debugging information than to crash. */
18666 return NULL;
18669 if (!ret && !list_ret)
18670 return 0;
18672 if (want_address == 2 && !have_address
18673 && (dwarf_version >= 4 || !dwarf_strict))
18675 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
18677 expansion_failed (loc, NULL_RTX,
18678 "DWARF address size mismatch");
18679 return 0;
18681 if (ret)
18682 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
18683 else
18684 add_loc_descr_to_each (list_ret,
18685 new_loc_descr (DW_OP_stack_value, 0, 0));
18686 have_address = 1;
18688 /* Show if we can't fill the request for an address. */
18689 if (want_address && !have_address)
18691 expansion_failed (loc, NULL_RTX,
18692 "Want address and only have value");
18693 return 0;
18696 gcc_assert (!ret || !list_ret);
18698 /* If we've got an address and don't want one, dereference. */
18699 if (!want_address && have_address)
18701 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
18703 if (size > DWARF2_ADDR_SIZE || size == -1)
18705 expansion_failed (loc, NULL_RTX,
18706 "DWARF address size mismatch");
18707 return 0;
18709 else if (size == DWARF2_ADDR_SIZE)
18710 op = DW_OP_deref;
18711 else
18712 op = DW_OP_deref_size;
18714 if (ret)
18715 add_loc_descr (&ret, new_loc_descr (op, size, 0));
18716 else
18717 add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
18719 if (ret)
18720 list_ret = new_loc_list (ret, NULL, 0, NULL, 0, NULL);
18722 return list_ret;
18725 /* Likewise, but strip useless DW_OP_nop operations in the resulting
18726 expressions. */
18728 static dw_loc_list_ref
18729 loc_list_from_tree (tree loc, int want_address,
18730 struct loc_descr_context *context)
18732 dw_loc_list_ref result = loc_list_from_tree_1 (loc, want_address, context);
18734 for (dw_loc_list_ref loc_cur = result;
18735 loc_cur != NULL; loc_cur = loc_cur->dw_loc_next)
18736 loc_descr_without_nops (loc_cur->expr);
18737 return result;
18740 /* Same as above but return only single location expression. */
18741 static dw_loc_descr_ref
18742 loc_descriptor_from_tree (tree loc, int want_address,
18743 struct loc_descr_context *context)
18745 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address, context);
18746 if (!ret)
18747 return NULL;
18748 if (ret->dw_loc_next)
18750 expansion_failed (loc, NULL_RTX,
18751 "Location list where only loc descriptor needed");
18752 return NULL;
18754 return ret->expr;
18757 /* Given a value, round it up to the lowest multiple of `boundary'
18758 which is not less than the value itself. */
18760 static inline HOST_WIDE_INT
18761 ceiling (HOST_WIDE_INT value, unsigned int boundary)
18763 return (((value + boundary - 1) / boundary) * boundary);
18766 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
18767 pointer to the declared type for the relevant field variable, or return
18768 `integer_type_node' if the given node turns out to be an
18769 ERROR_MARK node. */
18771 static inline tree
18772 field_type (const_tree decl)
18774 tree type;
18776 if (TREE_CODE (decl) == ERROR_MARK)
18777 return integer_type_node;
18779 type = DECL_BIT_FIELD_TYPE (decl);
18780 if (type == NULL_TREE)
18781 type = TREE_TYPE (decl);
18783 return type;
18786 /* Given a pointer to a tree node, return the alignment in bits for
18787 it, or else return BITS_PER_WORD if the node actually turns out to
18788 be an ERROR_MARK node. */
18790 static inline unsigned
18791 simple_type_align_in_bits (const_tree type)
18793 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
18796 static inline unsigned
18797 simple_decl_align_in_bits (const_tree decl)
18799 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
18802 /* Return the result of rounding T up to ALIGN. */
18804 static inline offset_int
18805 round_up_to_align (const offset_int &t, unsigned int align)
18807 return wi::udiv_trunc (t + align - 1, align) * align;
18810 /* Compute the size of TYPE in bytes. If possible, return NULL and store the
18811 size as an integer constant in CST_SIZE. Otherwise, if possible, return a
18812 DWARF expression that computes the size. Return NULL and set CST_SIZE to -1
18813 if we fail to return the size in one of these two forms. */
18815 static dw_loc_descr_ref
18816 type_byte_size (const_tree type, HOST_WIDE_INT *cst_size)
18818 tree tree_size;
18819 struct loc_descr_context ctx;
18821 /* Return a constant integer in priority, if possible. */
18822 *cst_size = int_size_in_bytes (type);
18823 if (*cst_size != -1)
18824 return NULL;
18826 ctx.context_type = const_cast<tree> (type);
18827 ctx.base_decl = NULL_TREE;
18828 ctx.dpi = NULL;
18829 ctx.placeholder_arg = false;
18830 ctx.placeholder_seen = false;
18832 type = TYPE_MAIN_VARIANT (type);
18833 tree_size = TYPE_SIZE_UNIT (type);
18834 return ((tree_size != NULL_TREE)
18835 ? loc_descriptor_from_tree (tree_size, 0, &ctx)
18836 : NULL);
18839 /* Helper structure for RECORD_TYPE processing. */
18840 struct vlr_context
18842 /* Root RECORD_TYPE. It is needed to generate data member location
18843 descriptions in variable-length records (VLR), but also to cope with
18844 variants, which are composed of nested structures multiplexed with
18845 QUAL_UNION_TYPE nodes. Each time such a structure is passed to a
18846 function processing a FIELD_DECL, it is required to be non null. */
18847 tree struct_type;
18848 /* When generating a variant part in a RECORD_TYPE (i.e. a nested
18849 QUAL_UNION_TYPE), this holds an expression that computes the offset for
18850 this variant part as part of the root record (in storage units). For
18851 regular records, it must be NULL_TREE. */
18852 tree variant_part_offset;
18855 /* Given a pointer to a FIELD_DECL, compute the byte offset of the lowest
18856 addressed byte of the "containing object" for the given FIELD_DECL. If
18857 possible, return a native constant through CST_OFFSET (in which case NULL is
18858 returned); otherwise return a DWARF expression that computes the offset.
18860 Set *CST_OFFSET to 0 and return NULL if we are unable to determine what
18861 that offset is, either because the argument turns out to be a pointer to an
18862 ERROR_MARK node, or because the offset expression is too complex for us.
18864 CTX is required: see the comment for VLR_CONTEXT. */
18866 static dw_loc_descr_ref
18867 field_byte_offset (const_tree decl, struct vlr_context *ctx,
18868 HOST_WIDE_INT *cst_offset)
18870 tree tree_result;
18871 dw_loc_list_ref loc_result;
18873 *cst_offset = 0;
18875 if (TREE_CODE (decl) == ERROR_MARK)
18876 return NULL;
18877 else
18878 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
18880 /* We cannot handle variable bit offsets at the moment, so abort if it's the
18881 case. */
18882 if (TREE_CODE (DECL_FIELD_BIT_OFFSET (decl)) != INTEGER_CST)
18883 return NULL;
18885 #ifdef PCC_BITFIELD_TYPE_MATTERS
18886 /* We used to handle only constant offsets in all cases. Now, we handle
18887 properly dynamic byte offsets only when PCC bitfield type doesn't
18888 matter. */
18889 if (PCC_BITFIELD_TYPE_MATTERS
18890 && TREE_CODE (DECL_FIELD_OFFSET (decl)) == INTEGER_CST)
18892 offset_int object_offset_in_bits;
18893 offset_int object_offset_in_bytes;
18894 offset_int bitpos_int;
18895 tree type;
18896 tree field_size_tree;
18897 offset_int deepest_bitpos;
18898 offset_int field_size_in_bits;
18899 unsigned int type_align_in_bits;
18900 unsigned int decl_align_in_bits;
18901 offset_int type_size_in_bits;
18903 bitpos_int = wi::to_offset (bit_position (decl));
18904 type = field_type (decl);
18905 type_size_in_bits = offset_int_type_size_in_bits (type);
18906 type_align_in_bits = simple_type_align_in_bits (type);
18908 field_size_tree = DECL_SIZE (decl);
18910 /* The size could be unspecified if there was an error, or for
18911 a flexible array member. */
18912 if (!field_size_tree)
18913 field_size_tree = bitsize_zero_node;
18915 /* If the size of the field is not constant, use the type size. */
18916 if (TREE_CODE (field_size_tree) == INTEGER_CST)
18917 field_size_in_bits = wi::to_offset (field_size_tree);
18918 else
18919 field_size_in_bits = type_size_in_bits;
18921 decl_align_in_bits = simple_decl_align_in_bits (decl);
18923 /* The GCC front-end doesn't make any attempt to keep track of the
18924 starting bit offset (relative to the start of the containing
18925 structure type) of the hypothetical "containing object" for a
18926 bit-field. Thus, when computing the byte offset value for the
18927 start of the "containing object" of a bit-field, we must deduce
18928 this information on our own. This can be rather tricky to do in
18929 some cases. For example, handling the following structure type
18930 definition when compiling for an i386/i486 target (which only
18931 aligns long long's to 32-bit boundaries) can be very tricky:
18933 struct S { int field1; long long field2:31; };
18935 Fortunately, there is a simple rule-of-thumb which can be used
18936 in such cases. When compiling for an i386/i486, GCC will
18937 allocate 8 bytes for the structure shown above. It decides to
18938 do this based upon one simple rule for bit-field allocation.
18939 GCC allocates each "containing object" for each bit-field at
18940 the first (i.e. lowest addressed) legitimate alignment boundary
18941 (based upon the required minimum alignment for the declared
18942 type of the field) which it can possibly use, subject to the
18943 condition that there is still enough available space remaining
18944 in the containing object (when allocated at the selected point)
18945 to fully accommodate all of the bits of the bit-field itself.
18947 This simple rule makes it obvious why GCC allocates 8 bytes for
18948 each object of the structure type shown above. When looking
18949 for a place to allocate the "containing object" for `field2',
18950 the compiler simply tries to allocate a 64-bit "containing
18951 object" at each successive 32-bit boundary (starting at zero)
18952 until it finds a place to allocate that 64- bit field such that
18953 at least 31 contiguous (and previously unallocated) bits remain
18954 within that selected 64 bit field. (As it turns out, for the
18955 example above, the compiler finds it is OK to allocate the
18956 "containing object" 64-bit field at bit-offset zero within the
18957 structure type.)
18959 Here we attempt to work backwards from the limited set of facts
18960 we're given, and we try to deduce from those facts, where GCC
18961 must have believed that the containing object started (within
18962 the structure type). The value we deduce is then used (by the
18963 callers of this routine) to generate DW_AT_location and
18964 DW_AT_bit_offset attributes for fields (both bit-fields and, in
18965 the case of DW_AT_location, regular fields as well). */
18967 /* Figure out the bit-distance from the start of the structure to
18968 the "deepest" bit of the bit-field. */
18969 deepest_bitpos = bitpos_int + field_size_in_bits;
18971 /* This is the tricky part. Use some fancy footwork to deduce
18972 where the lowest addressed bit of the containing object must
18973 be. */
18974 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
18976 /* Round up to type_align by default. This works best for
18977 bitfields. */
18978 object_offset_in_bits
18979 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
18981 if (wi::gtu_p (object_offset_in_bits, bitpos_int))
18983 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
18985 /* Round up to decl_align instead. */
18986 object_offset_in_bits
18987 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
18990 object_offset_in_bytes
18991 = wi::lrshift (object_offset_in_bits, LOG2_BITS_PER_UNIT);
18992 if (ctx->variant_part_offset == NULL_TREE)
18994 *cst_offset = object_offset_in_bytes.to_shwi ();
18995 return NULL;
18997 tree_result = wide_int_to_tree (sizetype, object_offset_in_bytes);
18999 else
19000 #endif /* PCC_BITFIELD_TYPE_MATTERS */
19001 tree_result = byte_position (decl);
19003 if (ctx->variant_part_offset != NULL_TREE)
19004 tree_result = fold_build2 (PLUS_EXPR, TREE_TYPE (tree_result),
19005 ctx->variant_part_offset, tree_result);
19007 /* If the byte offset is a constant, it's simplier to handle a native
19008 constant rather than a DWARF expression. */
19009 if (TREE_CODE (tree_result) == INTEGER_CST)
19011 *cst_offset = wi::to_offset (tree_result).to_shwi ();
19012 return NULL;
19014 struct loc_descr_context loc_ctx = {
19015 ctx->struct_type, /* context_type */
19016 NULL_TREE, /* base_decl */
19017 NULL, /* dpi */
19018 false, /* placeholder_arg */
19019 false /* placeholder_seen */
19021 loc_result = loc_list_from_tree (tree_result, 0, &loc_ctx);
19023 /* We want a DWARF expression: abort if we only have a location list with
19024 multiple elements. */
19025 if (!loc_result || !single_element_loc_list_p (loc_result))
19026 return NULL;
19027 else
19028 return loc_result->expr;
19031 /* The following routines define various Dwarf attributes and any data
19032 associated with them. */
19034 /* Add a location description attribute value to a DIE.
19036 This emits location attributes suitable for whole variables and
19037 whole parameters. Note that the location attributes for struct fields are
19038 generated by the routine `data_member_location_attribute' below. */
19040 static inline void
19041 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
19042 dw_loc_list_ref descr)
19044 bool check_no_locviews = true;
19045 if (descr == 0)
19046 return;
19047 if (single_element_loc_list_p (descr))
19048 add_AT_loc (die, attr_kind, descr->expr);
19049 else
19051 add_AT_loc_list (die, attr_kind, descr);
19052 gcc_assert (descr->ll_symbol);
19053 if (attr_kind == DW_AT_location && descr->vl_symbol
19054 && dwarf2out_locviews_in_attribute ())
19056 add_AT_view_list (die, DW_AT_GNU_locviews);
19057 check_no_locviews = false;
19061 if (check_no_locviews)
19062 gcc_assert (!get_AT (die, DW_AT_GNU_locviews));
19065 /* Add DW_AT_accessibility attribute to DIE if needed. */
19067 static void
19068 add_accessibility_attribute (dw_die_ref die, tree decl)
19070 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
19071 children, otherwise the default is DW_ACCESS_public. In DWARF2
19072 the default has always been DW_ACCESS_public. */
19073 if (TREE_PROTECTED (decl))
19074 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
19075 else if (TREE_PRIVATE (decl))
19077 if (dwarf_version == 2
19078 || die->die_parent == NULL
19079 || die->die_parent->die_tag != DW_TAG_class_type)
19080 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
19082 else if (dwarf_version > 2
19083 && die->die_parent
19084 && die->die_parent->die_tag == DW_TAG_class_type)
19085 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
19088 /* Attach the specialized form of location attribute used for data members of
19089 struct and union types. In the special case of a FIELD_DECL node which
19090 represents a bit-field, the "offset" part of this special location
19091 descriptor must indicate the distance in bytes from the lowest-addressed
19092 byte of the containing struct or union type to the lowest-addressed byte of
19093 the "containing object" for the bit-field. (See the `field_byte_offset'
19094 function above).
19096 For any given bit-field, the "containing object" is a hypothetical object
19097 (of some integral or enum type) within which the given bit-field lives. The
19098 type of this hypothetical "containing object" is always the same as the
19099 declared type of the individual bit-field itself (for GCC anyway... the
19100 DWARF spec doesn't actually mandate this). Note that it is the size (in
19101 bytes) of the hypothetical "containing object" which will be given in the
19102 DW_AT_byte_size attribute for this bit-field. (See the
19103 `byte_size_attribute' function below.) It is also used when calculating the
19104 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
19105 function below.)
19107 CTX is required: see the comment for VLR_CONTEXT. */
19109 static void
19110 add_data_member_location_attribute (dw_die_ref die,
19111 tree decl,
19112 struct vlr_context *ctx)
19114 HOST_WIDE_INT offset;
19115 dw_loc_descr_ref loc_descr = 0;
19117 if (TREE_CODE (decl) == TREE_BINFO)
19119 /* We're working on the TAG_inheritance for a base class. */
19120 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
19122 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
19123 aren't at a fixed offset from all (sub)objects of the same
19124 type. We need to extract the appropriate offset from our
19125 vtable. The following dwarf expression means
19127 BaseAddr = ObAddr + *((*ObAddr) - Offset)
19129 This is specific to the V3 ABI, of course. */
19131 dw_loc_descr_ref tmp;
19133 /* Make a copy of the object address. */
19134 tmp = new_loc_descr (DW_OP_dup, 0, 0);
19135 add_loc_descr (&loc_descr, tmp);
19137 /* Extract the vtable address. */
19138 tmp = new_loc_descr (DW_OP_deref, 0, 0);
19139 add_loc_descr (&loc_descr, tmp);
19141 /* Calculate the address of the offset. */
19142 offset = tree_to_shwi (BINFO_VPTR_FIELD (decl));
19143 gcc_assert (offset < 0);
19145 tmp = int_loc_descriptor (-offset);
19146 add_loc_descr (&loc_descr, tmp);
19147 tmp = new_loc_descr (DW_OP_minus, 0, 0);
19148 add_loc_descr (&loc_descr, tmp);
19150 /* Extract the offset. */
19151 tmp = new_loc_descr (DW_OP_deref, 0, 0);
19152 add_loc_descr (&loc_descr, tmp);
19154 /* Add it to the object address. */
19155 tmp = new_loc_descr (DW_OP_plus, 0, 0);
19156 add_loc_descr (&loc_descr, tmp);
19158 else
19159 offset = tree_to_shwi (BINFO_OFFSET (decl));
19161 else
19163 loc_descr = field_byte_offset (decl, ctx, &offset);
19165 /* If loc_descr is available then we know the field offset is dynamic.
19166 However, GDB does not handle dynamic field offsets very well at the
19167 moment. */
19168 if (loc_descr != NULL && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL)
19170 loc_descr = NULL;
19171 offset = 0;
19174 /* Data member location evalutation starts with the base address on the
19175 stack. Compute the field offset and add it to this base address. */
19176 else if (loc_descr != NULL)
19177 add_loc_descr (&loc_descr, new_loc_descr (DW_OP_plus, 0, 0));
19180 if (! loc_descr)
19182 /* While DW_AT_data_bit_offset has been added already in DWARF4,
19183 e.g. GDB only added support to it in November 2016. For DWARF5
19184 we need newer debug info consumers anyway. We might change this
19185 to dwarf_version >= 4 once most consumers catched up. */
19186 if (dwarf_version >= 5
19187 && TREE_CODE (decl) == FIELD_DECL
19188 && DECL_BIT_FIELD_TYPE (decl))
19190 tree off = bit_position (decl);
19191 if (tree_fits_uhwi_p (off) && get_AT (die, DW_AT_bit_size))
19193 remove_AT (die, DW_AT_byte_size);
19194 remove_AT (die, DW_AT_bit_offset);
19195 add_AT_unsigned (die, DW_AT_data_bit_offset, tree_to_uhwi (off));
19196 return;
19199 if (dwarf_version > 2)
19201 /* Don't need to output a location expression, just the constant. */
19202 if (offset < 0)
19203 add_AT_int (die, DW_AT_data_member_location, offset);
19204 else
19205 add_AT_unsigned (die, DW_AT_data_member_location, offset);
19206 return;
19208 else
19210 enum dwarf_location_atom op;
19212 /* The DWARF2 standard says that we should assume that the structure
19213 address is already on the stack, so we can specify a structure
19214 field address by using DW_OP_plus_uconst. */
19215 op = DW_OP_plus_uconst;
19216 loc_descr = new_loc_descr (op, offset, 0);
19220 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
19223 /* Writes integer values to dw_vec_const array. */
19225 static void
19226 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
19228 while (size != 0)
19230 *dest++ = val & 0xff;
19231 val >>= 8;
19232 --size;
19236 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
19238 static HOST_WIDE_INT
19239 extract_int (const unsigned char *src, unsigned int size)
19241 HOST_WIDE_INT val = 0;
19243 src += size;
19244 while (size != 0)
19246 val <<= 8;
19247 val |= *--src & 0xff;
19248 --size;
19250 return val;
19253 /* Writes wide_int values to dw_vec_const array. */
19255 static void
19256 insert_wide_int (const wide_int &val, unsigned char *dest, int elt_size)
19258 int i;
19260 if (elt_size <= HOST_BITS_PER_WIDE_INT/BITS_PER_UNIT)
19262 insert_int ((HOST_WIDE_INT) val.elt (0), elt_size, dest);
19263 return;
19266 /* We'd have to extend this code to support odd sizes. */
19267 gcc_assert (elt_size % (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT) == 0);
19269 int n = elt_size / (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
19271 if (WORDS_BIG_ENDIAN)
19272 for (i = n - 1; i >= 0; i--)
19274 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
19275 dest += sizeof (HOST_WIDE_INT);
19277 else
19278 for (i = 0; i < n; i++)
19280 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
19281 dest += sizeof (HOST_WIDE_INT);
19285 /* Writes floating point values to dw_vec_const array. */
19287 static void
19288 insert_float (const_rtx rtl, unsigned char *array)
19290 long val[4];
19291 int i;
19292 scalar_float_mode mode = as_a <scalar_float_mode> (GET_MODE (rtl));
19294 real_to_target (val, CONST_DOUBLE_REAL_VALUE (rtl), mode);
19296 /* real_to_target puts 32-bit pieces in each long. Pack them. */
19297 for (i = 0; i < GET_MODE_SIZE (mode) / 4; i++)
19299 insert_int (val[i], 4, array);
19300 array += 4;
19304 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
19305 does not have a "location" either in memory or in a register. These
19306 things can arise in GNU C when a constant is passed as an actual parameter
19307 to an inlined function. They can also arise in C++ where declared
19308 constants do not necessarily get memory "homes". */
19310 static bool
19311 add_const_value_attribute (dw_die_ref die, rtx rtl)
19313 switch (GET_CODE (rtl))
19315 case CONST_INT:
19317 HOST_WIDE_INT val = INTVAL (rtl);
19319 if (val < 0)
19320 add_AT_int (die, DW_AT_const_value, val);
19321 else
19322 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
19324 return true;
19326 case CONST_WIDE_INT:
19328 wide_int w1 = rtx_mode_t (rtl, MAX_MODE_INT);
19329 unsigned int prec = MIN (wi::min_precision (w1, UNSIGNED),
19330 (unsigned int)CONST_WIDE_INT_NUNITS (rtl) * HOST_BITS_PER_WIDE_INT);
19331 wide_int w = wi::zext (w1, prec);
19332 add_AT_wide (die, DW_AT_const_value, w);
19334 return true;
19336 case CONST_DOUBLE:
19337 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
19338 floating-point constant. A CONST_DOUBLE is used whenever the
19339 constant requires more than one word in order to be adequately
19340 represented. */
19341 if (TARGET_SUPPORTS_WIDE_INT == 0
19342 && !SCALAR_FLOAT_MODE_P (GET_MODE (rtl)))
19343 add_AT_double (die, DW_AT_const_value,
19344 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
19345 else
19347 scalar_float_mode mode = as_a <scalar_float_mode> (GET_MODE (rtl));
19348 unsigned int length = GET_MODE_SIZE (mode);
19349 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
19351 insert_float (rtl, array);
19352 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
19354 return true;
19356 case CONST_VECTOR:
19358 unsigned int length;
19359 if (!CONST_VECTOR_NUNITS (rtl).is_constant (&length))
19360 return false;
19362 machine_mode mode = GET_MODE (rtl);
19363 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
19364 unsigned char *array
19365 = ggc_vec_alloc<unsigned char> (length * elt_size);
19366 unsigned int i;
19367 unsigned char *p;
19368 machine_mode imode = GET_MODE_INNER (mode);
19370 switch (GET_MODE_CLASS (mode))
19372 case MODE_VECTOR_INT:
19373 for (i = 0, p = array; i < length; i++, p += elt_size)
19375 rtx elt = CONST_VECTOR_ELT (rtl, i);
19376 insert_wide_int (rtx_mode_t (elt, imode), p, elt_size);
19378 break;
19380 case MODE_VECTOR_FLOAT:
19381 for (i = 0, p = array; i < length; i++, p += elt_size)
19383 rtx elt = CONST_VECTOR_ELT (rtl, i);
19384 insert_float (elt, p);
19386 break;
19388 default:
19389 gcc_unreachable ();
19392 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
19394 return true;
19396 case CONST_STRING:
19397 if (dwarf_version >= 4 || !dwarf_strict)
19399 dw_loc_descr_ref loc_result;
19400 resolve_one_addr (&rtl);
19401 rtl_addr:
19402 loc_result = new_addr_loc_descr (rtl, dtprel_false);
19403 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
19404 add_AT_loc (die, DW_AT_location, loc_result);
19405 vec_safe_push (used_rtx_array, rtl);
19406 return true;
19408 return false;
19410 case CONST:
19411 if (CONSTANT_P (XEXP (rtl, 0)))
19412 return add_const_value_attribute (die, XEXP (rtl, 0));
19413 /* FALLTHROUGH */
19414 case SYMBOL_REF:
19415 if (!const_ok_for_output (rtl))
19416 return false;
19417 /* FALLTHROUGH */
19418 case LABEL_REF:
19419 if (dwarf_version >= 4 || !dwarf_strict)
19420 goto rtl_addr;
19421 return false;
19423 case PLUS:
19424 /* In cases where an inlined instance of an inline function is passed
19425 the address of an `auto' variable (which is local to the caller) we
19426 can get a situation where the DECL_RTL of the artificial local
19427 variable (for the inlining) which acts as a stand-in for the
19428 corresponding formal parameter (of the inline function) will look
19429 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
19430 exactly a compile-time constant expression, but it isn't the address
19431 of the (artificial) local variable either. Rather, it represents the
19432 *value* which the artificial local variable always has during its
19433 lifetime. We currently have no way to represent such quasi-constant
19434 values in Dwarf, so for now we just punt and generate nothing. */
19435 return false;
19437 case HIGH:
19438 case CONST_FIXED:
19439 return false;
19441 case MEM:
19442 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
19443 && MEM_READONLY_P (rtl)
19444 && GET_MODE (rtl) == BLKmode)
19446 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
19447 return true;
19449 return false;
19451 default:
19452 /* No other kinds of rtx should be possible here. */
19453 gcc_unreachable ();
19455 return false;
19458 /* Determine whether the evaluation of EXPR references any variables
19459 or functions which aren't otherwise used (and therefore may not be
19460 output). */
19461 static tree
19462 reference_to_unused (tree * tp, int * walk_subtrees,
19463 void * data ATTRIBUTE_UNUSED)
19465 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
19466 *walk_subtrees = 0;
19468 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
19469 && ! TREE_ASM_WRITTEN (*tp))
19470 return *tp;
19471 /* ??? The C++ FE emits debug information for using decls, so
19472 putting gcc_unreachable here falls over. See PR31899. For now
19473 be conservative. */
19474 else if (!symtab->global_info_ready && VAR_OR_FUNCTION_DECL_P (*tp))
19475 return *tp;
19476 else if (VAR_P (*tp))
19478 varpool_node *node = varpool_node::get (*tp);
19479 if (!node || !node->definition)
19480 return *tp;
19482 else if (TREE_CODE (*tp) == FUNCTION_DECL
19483 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
19485 /* The call graph machinery must have finished analyzing,
19486 optimizing and gimplifying the CU by now.
19487 So if *TP has no call graph node associated
19488 to it, it means *TP will not be emitted. */
19489 if (!cgraph_node::get (*tp))
19490 return *tp;
19492 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
19493 return *tp;
19495 return NULL_TREE;
19498 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
19499 for use in a later add_const_value_attribute call. */
19501 static rtx
19502 rtl_for_decl_init (tree init, tree type)
19504 rtx rtl = NULL_RTX;
19506 STRIP_NOPS (init);
19508 /* If a variable is initialized with a string constant without embedded
19509 zeros, build CONST_STRING. */
19510 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
19512 tree enttype = TREE_TYPE (type);
19513 tree domain = TYPE_DOMAIN (type);
19514 scalar_int_mode mode;
19516 if (is_int_mode (TYPE_MODE (enttype), &mode)
19517 && GET_MODE_SIZE (mode) == 1
19518 && domain
19519 && integer_zerop (TYPE_MIN_VALUE (domain))
19520 && compare_tree_int (TYPE_MAX_VALUE (domain),
19521 TREE_STRING_LENGTH (init) - 1) == 0
19522 && ((size_t) TREE_STRING_LENGTH (init)
19523 == strlen (TREE_STRING_POINTER (init)) + 1))
19525 rtl = gen_rtx_CONST_STRING (VOIDmode,
19526 ggc_strdup (TREE_STRING_POINTER (init)));
19527 rtl = gen_rtx_MEM (BLKmode, rtl);
19528 MEM_READONLY_P (rtl) = 1;
19531 /* Other aggregates, and complex values, could be represented using
19532 CONCAT: FIXME! */
19533 else if (AGGREGATE_TYPE_P (type)
19534 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
19535 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
19536 || TREE_CODE (type) == COMPLEX_TYPE)
19538 /* Vectors only work if their mode is supported by the target.
19539 FIXME: generic vectors ought to work too. */
19540 else if (TREE_CODE (type) == VECTOR_TYPE
19541 && !VECTOR_MODE_P (TYPE_MODE (type)))
19543 /* If the initializer is something that we know will expand into an
19544 immediate RTL constant, expand it now. We must be careful not to
19545 reference variables which won't be output. */
19546 else if (initializer_constant_valid_p (init, type)
19547 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
19549 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
19550 possible. */
19551 if (TREE_CODE (type) == VECTOR_TYPE)
19552 switch (TREE_CODE (init))
19554 case VECTOR_CST:
19555 break;
19556 case CONSTRUCTOR:
19557 if (TREE_CONSTANT (init))
19559 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
19560 bool constant_p = true;
19561 tree value;
19562 unsigned HOST_WIDE_INT ix;
19564 /* Even when ctor is constant, it might contain non-*_CST
19565 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
19566 belong into VECTOR_CST nodes. */
19567 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
19568 if (!CONSTANT_CLASS_P (value))
19570 constant_p = false;
19571 break;
19574 if (constant_p)
19576 init = build_vector_from_ctor (type, elts);
19577 break;
19580 /* FALLTHRU */
19582 default:
19583 return NULL;
19586 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
19588 /* If expand_expr returns a MEM, it wasn't immediate. */
19589 gcc_assert (!rtl || !MEM_P (rtl));
19592 return rtl;
19595 /* Generate RTL for the variable DECL to represent its location. */
19597 static rtx
19598 rtl_for_decl_location (tree decl)
19600 rtx rtl;
19602 /* Here we have to decide where we are going to say the parameter "lives"
19603 (as far as the debugger is concerned). We only have a couple of
19604 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
19606 DECL_RTL normally indicates where the parameter lives during most of the
19607 activation of the function. If optimization is enabled however, this
19608 could be either NULL or else a pseudo-reg. Both of those cases indicate
19609 that the parameter doesn't really live anywhere (as far as the code
19610 generation parts of GCC are concerned) during most of the function's
19611 activation. That will happen (for example) if the parameter is never
19612 referenced within the function.
19614 We could just generate a location descriptor here for all non-NULL
19615 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
19616 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
19617 where DECL_RTL is NULL or is a pseudo-reg.
19619 Note however that we can only get away with using DECL_INCOMING_RTL as
19620 a backup substitute for DECL_RTL in certain limited cases. In cases
19621 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
19622 we can be sure that the parameter was passed using the same type as it is
19623 declared to have within the function, and that its DECL_INCOMING_RTL
19624 points us to a place where a value of that type is passed.
19626 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
19627 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
19628 because in these cases DECL_INCOMING_RTL points us to a value of some
19629 type which is *different* from the type of the parameter itself. Thus,
19630 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
19631 such cases, the debugger would end up (for example) trying to fetch a
19632 `float' from a place which actually contains the first part of a
19633 `double'. That would lead to really incorrect and confusing
19634 output at debug-time.
19636 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
19637 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
19638 are a couple of exceptions however. On little-endian machines we can
19639 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
19640 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
19641 an integral type that is smaller than TREE_TYPE (decl). These cases arise
19642 when (on a little-endian machine) a non-prototyped function has a
19643 parameter declared to be of type `short' or `char'. In such cases,
19644 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
19645 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
19646 passed `int' value. If the debugger then uses that address to fetch
19647 a `short' or a `char' (on a little-endian machine) the result will be
19648 the correct data, so we allow for such exceptional cases below.
19650 Note that our goal here is to describe the place where the given formal
19651 parameter lives during most of the function's activation (i.e. between the
19652 end of the prologue and the start of the epilogue). We'll do that as best
19653 as we can. Note however that if the given formal parameter is modified
19654 sometime during the execution of the function, then a stack backtrace (at
19655 debug-time) will show the function as having been called with the *new*
19656 value rather than the value which was originally passed in. This happens
19657 rarely enough that it is not a major problem, but it *is* a problem, and
19658 I'd like to fix it.
19660 A future version of dwarf2out.c may generate two additional attributes for
19661 any given DW_TAG_formal_parameter DIE which will describe the "passed
19662 type" and the "passed location" for the given formal parameter in addition
19663 to the attributes we now generate to indicate the "declared type" and the
19664 "active location" for each parameter. This additional set of attributes
19665 could be used by debuggers for stack backtraces. Separately, note that
19666 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
19667 This happens (for example) for inlined-instances of inline function formal
19668 parameters which are never referenced. This really shouldn't be
19669 happening. All PARM_DECL nodes should get valid non-NULL
19670 DECL_INCOMING_RTL values. FIXME. */
19672 /* Use DECL_RTL as the "location" unless we find something better. */
19673 rtl = DECL_RTL_IF_SET (decl);
19675 /* When generating abstract instances, ignore everything except
19676 constants, symbols living in memory, and symbols living in
19677 fixed registers. */
19678 if (! reload_completed)
19680 if (rtl
19681 && (CONSTANT_P (rtl)
19682 || (MEM_P (rtl)
19683 && CONSTANT_P (XEXP (rtl, 0)))
19684 || (REG_P (rtl)
19685 && VAR_P (decl)
19686 && TREE_STATIC (decl))))
19688 rtl = targetm.delegitimize_address (rtl);
19689 return rtl;
19691 rtl = NULL_RTX;
19693 else if (TREE_CODE (decl) == PARM_DECL)
19695 if (rtl == NULL_RTX
19696 || is_pseudo_reg (rtl)
19697 || (MEM_P (rtl)
19698 && is_pseudo_reg (XEXP (rtl, 0))
19699 && DECL_INCOMING_RTL (decl)
19700 && MEM_P (DECL_INCOMING_RTL (decl))
19701 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
19703 tree declared_type = TREE_TYPE (decl);
19704 tree passed_type = DECL_ARG_TYPE (decl);
19705 machine_mode dmode = TYPE_MODE (declared_type);
19706 machine_mode pmode = TYPE_MODE (passed_type);
19708 /* This decl represents a formal parameter which was optimized out.
19709 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
19710 all cases where (rtl == NULL_RTX) just below. */
19711 if (dmode == pmode)
19712 rtl = DECL_INCOMING_RTL (decl);
19713 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
19714 && SCALAR_INT_MODE_P (dmode)
19715 && known_le (GET_MODE_SIZE (dmode), GET_MODE_SIZE (pmode))
19716 && DECL_INCOMING_RTL (decl))
19718 rtx inc = DECL_INCOMING_RTL (decl);
19719 if (REG_P (inc))
19720 rtl = inc;
19721 else if (MEM_P (inc))
19723 if (BYTES_BIG_ENDIAN)
19724 rtl = adjust_address_nv (inc, dmode,
19725 GET_MODE_SIZE (pmode)
19726 - GET_MODE_SIZE (dmode));
19727 else
19728 rtl = inc;
19733 /* If the parm was passed in registers, but lives on the stack, then
19734 make a big endian correction if the mode of the type of the
19735 parameter is not the same as the mode of the rtl. */
19736 /* ??? This is the same series of checks that are made in dbxout.c before
19737 we reach the big endian correction code there. It isn't clear if all
19738 of these checks are necessary here, but keeping them all is the safe
19739 thing to do. */
19740 else if (MEM_P (rtl)
19741 && XEXP (rtl, 0) != const0_rtx
19742 && ! CONSTANT_P (XEXP (rtl, 0))
19743 /* Not passed in memory. */
19744 && !MEM_P (DECL_INCOMING_RTL (decl))
19745 /* Not passed by invisible reference. */
19746 && (!REG_P (XEXP (rtl, 0))
19747 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
19748 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
19749 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
19750 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
19751 #endif
19753 /* Big endian correction check. */
19754 && BYTES_BIG_ENDIAN
19755 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
19756 && known_lt (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))),
19757 UNITS_PER_WORD))
19759 machine_mode addr_mode = get_address_mode (rtl);
19760 poly_int64 offset = (UNITS_PER_WORD
19761 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
19763 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
19764 plus_constant (addr_mode, XEXP (rtl, 0), offset));
19767 else if (VAR_P (decl)
19768 && rtl
19769 && MEM_P (rtl)
19770 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl)))
19772 machine_mode addr_mode = get_address_mode (rtl);
19773 poly_int64 offset = byte_lowpart_offset (TYPE_MODE (TREE_TYPE (decl)),
19774 GET_MODE (rtl));
19776 /* If a variable is declared "register" yet is smaller than
19777 a register, then if we store the variable to memory, it
19778 looks like we're storing a register-sized value, when in
19779 fact we are not. We need to adjust the offset of the
19780 storage location to reflect the actual value's bytes,
19781 else gdb will not be able to display it. */
19782 if (maybe_ne (offset, 0))
19783 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
19784 plus_constant (addr_mode, XEXP (rtl, 0), offset));
19787 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
19788 and will have been substituted directly into all expressions that use it.
19789 C does not have such a concept, but C++ and other languages do. */
19790 if (!rtl && VAR_P (decl) && DECL_INITIAL (decl))
19791 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
19793 if (rtl)
19794 rtl = targetm.delegitimize_address (rtl);
19796 /* If we don't look past the constant pool, we risk emitting a
19797 reference to a constant pool entry that isn't referenced from
19798 code, and thus is not emitted. */
19799 if (rtl)
19800 rtl = avoid_constant_pool_reference (rtl);
19802 /* Try harder to get a rtl. If this symbol ends up not being emitted
19803 in the current CU, resolve_addr will remove the expression referencing
19804 it. */
19805 if (rtl == NULL_RTX
19806 && VAR_P (decl)
19807 && !DECL_EXTERNAL (decl)
19808 && TREE_STATIC (decl)
19809 && DECL_NAME (decl)
19810 && !DECL_HARD_REGISTER (decl)
19811 && DECL_MODE (decl) != VOIDmode)
19813 rtl = make_decl_rtl_for_debug (decl);
19814 if (!MEM_P (rtl)
19815 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
19816 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
19817 rtl = NULL_RTX;
19820 return rtl;
19823 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
19824 returned. If so, the decl for the COMMON block is returned, and the
19825 value is the offset into the common block for the symbol. */
19827 static tree
19828 fortran_common (tree decl, HOST_WIDE_INT *value)
19830 tree val_expr, cvar;
19831 machine_mode mode;
19832 poly_int64 bitsize, bitpos;
19833 tree offset;
19834 HOST_WIDE_INT cbitpos;
19835 int unsignedp, reversep, volatilep = 0;
19837 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
19838 it does not have a value (the offset into the common area), or if it
19839 is thread local (as opposed to global) then it isn't common, and shouldn't
19840 be handled as such. */
19841 if (!VAR_P (decl)
19842 || !TREE_STATIC (decl)
19843 || !DECL_HAS_VALUE_EXPR_P (decl)
19844 || !is_fortran ())
19845 return NULL_TREE;
19847 val_expr = DECL_VALUE_EXPR (decl);
19848 if (TREE_CODE (val_expr) != COMPONENT_REF)
19849 return NULL_TREE;
19851 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset, &mode,
19852 &unsignedp, &reversep, &volatilep);
19854 if (cvar == NULL_TREE
19855 || !VAR_P (cvar)
19856 || DECL_ARTIFICIAL (cvar)
19857 || !TREE_PUBLIC (cvar)
19858 /* We don't expect to have to cope with variable offsets,
19859 since at present all static data must have a constant size. */
19860 || !bitpos.is_constant (&cbitpos))
19861 return NULL_TREE;
19863 *value = 0;
19864 if (offset != NULL)
19866 if (!tree_fits_shwi_p (offset))
19867 return NULL_TREE;
19868 *value = tree_to_shwi (offset);
19870 if (cbitpos != 0)
19871 *value += cbitpos / BITS_PER_UNIT;
19873 return cvar;
19876 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
19877 data attribute for a variable or a parameter. We generate the
19878 DW_AT_const_value attribute only in those cases where the given variable
19879 or parameter does not have a true "location" either in memory or in a
19880 register. This can happen (for example) when a constant is passed as an
19881 actual argument in a call to an inline function. (It's possible that
19882 these things can crop up in other ways also.) Note that one type of
19883 constant value which can be passed into an inlined function is a constant
19884 pointer. This can happen for example if an actual argument in an inlined
19885 function call evaluates to a compile-time constant address.
19887 CACHE_P is true if it is worth caching the location list for DECL,
19888 so that future calls can reuse it rather than regenerate it from scratch.
19889 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
19890 since we will need to refer to them each time the function is inlined. */
19892 static bool
19893 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p)
19895 rtx rtl;
19896 dw_loc_list_ref list;
19897 var_loc_list *loc_list;
19898 cached_dw_loc_list *cache;
19900 if (early_dwarf)
19901 return false;
19903 if (TREE_CODE (decl) == ERROR_MARK)
19904 return false;
19906 if (get_AT (die, DW_AT_location)
19907 || get_AT (die, DW_AT_const_value))
19908 return true;
19910 gcc_assert (VAR_P (decl) || TREE_CODE (decl) == PARM_DECL
19911 || TREE_CODE (decl) == RESULT_DECL);
19913 /* Try to get some constant RTL for this decl, and use that as the value of
19914 the location. */
19916 rtl = rtl_for_decl_location (decl);
19917 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
19918 && add_const_value_attribute (die, rtl))
19919 return true;
19921 /* See if we have single element location list that is equivalent to
19922 a constant value. That way we are better to use add_const_value_attribute
19923 rather than expanding constant value equivalent. */
19924 loc_list = lookup_decl_loc (decl);
19925 if (loc_list
19926 && loc_list->first
19927 && loc_list->first->next == NULL
19928 && NOTE_P (loc_list->first->loc)
19929 && NOTE_VAR_LOCATION (loc_list->first->loc)
19930 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
19932 struct var_loc_node *node;
19934 node = loc_list->first;
19935 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
19936 if (GET_CODE (rtl) == EXPR_LIST)
19937 rtl = XEXP (rtl, 0);
19938 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
19939 && add_const_value_attribute (die, rtl))
19940 return true;
19942 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
19943 list several times. See if we've already cached the contents. */
19944 list = NULL;
19945 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
19946 cache_p = false;
19947 if (cache_p)
19949 cache = cached_dw_loc_list_table->find_with_hash (decl, DECL_UID (decl));
19950 if (cache)
19951 list = cache->loc_list;
19953 if (list == NULL)
19955 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2,
19956 NULL);
19957 /* It is usually worth caching this result if the decl is from
19958 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
19959 if (cache_p && list && list->dw_loc_next)
19961 cached_dw_loc_list **slot
19962 = cached_dw_loc_list_table->find_slot_with_hash (decl,
19963 DECL_UID (decl),
19964 INSERT);
19965 cache = ggc_cleared_alloc<cached_dw_loc_list> ();
19966 cache->decl_id = DECL_UID (decl);
19967 cache->loc_list = list;
19968 *slot = cache;
19971 if (list)
19973 add_AT_location_description (die, DW_AT_location, list);
19974 return true;
19976 /* None of that worked, so it must not really have a location;
19977 try adding a constant value attribute from the DECL_INITIAL. */
19978 return tree_add_const_value_attribute_for_decl (die, decl);
19981 /* Helper function for tree_add_const_value_attribute. Natively encode
19982 initializer INIT into an array. Return true if successful. */
19984 static bool
19985 native_encode_initializer (tree init, unsigned char *array, int size)
19987 tree type;
19989 if (init == NULL_TREE)
19990 return false;
19992 STRIP_NOPS (init);
19993 switch (TREE_CODE (init))
19995 case STRING_CST:
19996 type = TREE_TYPE (init);
19997 if (TREE_CODE (type) == ARRAY_TYPE)
19999 tree enttype = TREE_TYPE (type);
20000 scalar_int_mode mode;
20002 if (!is_int_mode (TYPE_MODE (enttype), &mode)
20003 || GET_MODE_SIZE (mode) != 1)
20004 return false;
20005 if (int_size_in_bytes (type) != size)
20006 return false;
20007 if (size > TREE_STRING_LENGTH (init))
20009 memcpy (array, TREE_STRING_POINTER (init),
20010 TREE_STRING_LENGTH (init));
20011 memset (array + TREE_STRING_LENGTH (init),
20012 '\0', size - TREE_STRING_LENGTH (init));
20014 else
20015 memcpy (array, TREE_STRING_POINTER (init), size);
20016 return true;
20018 return false;
20019 case CONSTRUCTOR:
20020 type = TREE_TYPE (init);
20021 if (int_size_in_bytes (type) != size)
20022 return false;
20023 if (TREE_CODE (type) == ARRAY_TYPE)
20025 HOST_WIDE_INT min_index;
20026 unsigned HOST_WIDE_INT cnt;
20027 int curpos = 0, fieldsize;
20028 constructor_elt *ce;
20030 if (TYPE_DOMAIN (type) == NULL_TREE
20031 || !tree_fits_shwi_p (TYPE_MIN_VALUE (TYPE_DOMAIN (type))))
20032 return false;
20034 fieldsize = int_size_in_bytes (TREE_TYPE (type));
20035 if (fieldsize <= 0)
20036 return false;
20038 min_index = tree_to_shwi (TYPE_MIN_VALUE (TYPE_DOMAIN (type)));
20039 memset (array, '\0', size);
20040 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
20042 tree val = ce->value;
20043 tree index = ce->index;
20044 int pos = curpos;
20045 if (index && TREE_CODE (index) == RANGE_EXPR)
20046 pos = (tree_to_shwi (TREE_OPERAND (index, 0)) - min_index)
20047 * fieldsize;
20048 else if (index)
20049 pos = (tree_to_shwi (index) - min_index) * fieldsize;
20051 if (val)
20053 STRIP_NOPS (val);
20054 if (!native_encode_initializer (val, array + pos, fieldsize))
20055 return false;
20057 curpos = pos + fieldsize;
20058 if (index && TREE_CODE (index) == RANGE_EXPR)
20060 int count = tree_to_shwi (TREE_OPERAND (index, 1))
20061 - tree_to_shwi (TREE_OPERAND (index, 0));
20062 while (count-- > 0)
20064 if (val)
20065 memcpy (array + curpos, array + pos, fieldsize);
20066 curpos += fieldsize;
20069 gcc_assert (curpos <= size);
20071 return true;
20073 else if (TREE_CODE (type) == RECORD_TYPE
20074 || TREE_CODE (type) == UNION_TYPE)
20076 tree field = NULL_TREE;
20077 unsigned HOST_WIDE_INT cnt;
20078 constructor_elt *ce;
20080 if (int_size_in_bytes (type) != size)
20081 return false;
20083 if (TREE_CODE (type) == RECORD_TYPE)
20084 field = TYPE_FIELDS (type);
20086 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
20088 tree val = ce->value;
20089 int pos, fieldsize;
20091 if (ce->index != 0)
20092 field = ce->index;
20094 if (val)
20095 STRIP_NOPS (val);
20097 if (field == NULL_TREE || DECL_BIT_FIELD (field))
20098 return false;
20100 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
20101 && TYPE_DOMAIN (TREE_TYPE (field))
20102 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
20103 return false;
20104 else if (DECL_SIZE_UNIT (field) == NULL_TREE
20105 || !tree_fits_shwi_p (DECL_SIZE_UNIT (field)))
20106 return false;
20107 fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
20108 pos = int_byte_position (field);
20109 gcc_assert (pos + fieldsize <= size);
20110 if (val && fieldsize != 0
20111 && !native_encode_initializer (val, array + pos, fieldsize))
20112 return false;
20114 return true;
20116 return false;
20117 case VIEW_CONVERT_EXPR:
20118 case NON_LVALUE_EXPR:
20119 return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
20120 default:
20121 return native_encode_expr (init, array, size) == size;
20125 /* Attach a DW_AT_const_value attribute to DIE. The value of the
20126 attribute is the const value T. */
20128 static bool
20129 tree_add_const_value_attribute (dw_die_ref die, tree t)
20131 tree init;
20132 tree type = TREE_TYPE (t);
20133 rtx rtl;
20135 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
20136 return false;
20138 init = t;
20139 gcc_assert (!DECL_P (init));
20141 if (TREE_CODE (init) == INTEGER_CST)
20143 if (tree_fits_uhwi_p (init))
20145 add_AT_unsigned (die, DW_AT_const_value, tree_to_uhwi (init));
20146 return true;
20148 if (tree_fits_shwi_p (init))
20150 add_AT_int (die, DW_AT_const_value, tree_to_shwi (init));
20151 return true;
20154 if (! early_dwarf)
20156 rtl = rtl_for_decl_init (init, type);
20157 if (rtl)
20158 return add_const_value_attribute (die, rtl);
20160 /* If the host and target are sane, try harder. */
20161 if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
20162 && initializer_constant_valid_p (init, type))
20164 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
20165 if (size > 0 && (int) size == size)
20167 unsigned char *array = ggc_cleared_vec_alloc<unsigned char> (size);
20169 if (native_encode_initializer (init, array, size))
20171 add_AT_vec (die, DW_AT_const_value, size, 1, array);
20172 return true;
20174 ggc_free (array);
20177 return false;
20180 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
20181 attribute is the const value of T, where T is an integral constant
20182 variable with static storage duration
20183 (so it can't be a PARM_DECL or a RESULT_DECL). */
20185 static bool
20186 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
20189 if (!decl
20190 || (!VAR_P (decl) && TREE_CODE (decl) != CONST_DECL)
20191 || (VAR_P (decl) && !TREE_STATIC (decl)))
20192 return false;
20194 if (TREE_READONLY (decl)
20195 && ! TREE_THIS_VOLATILE (decl)
20196 && DECL_INITIAL (decl))
20197 /* OK */;
20198 else
20199 return false;
20201 /* Don't add DW_AT_const_value if abstract origin already has one. */
20202 if (get_AT (var_die, DW_AT_const_value))
20203 return false;
20205 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
20208 /* Convert the CFI instructions for the current function into a
20209 location list. This is used for DW_AT_frame_base when we targeting
20210 a dwarf2 consumer that does not support the dwarf3
20211 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
20212 expressions. */
20214 static dw_loc_list_ref
20215 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
20217 int ix;
20218 dw_fde_ref fde;
20219 dw_loc_list_ref list, *list_tail;
20220 dw_cfi_ref cfi;
20221 dw_cfa_location last_cfa, next_cfa;
20222 const char *start_label, *last_label, *section;
20223 dw_cfa_location remember;
20225 fde = cfun->fde;
20226 gcc_assert (fde != NULL);
20228 section = secname_for_decl (current_function_decl);
20229 list_tail = &list;
20230 list = NULL;
20232 memset (&next_cfa, 0, sizeof (next_cfa));
20233 next_cfa.reg = INVALID_REGNUM;
20234 remember = next_cfa;
20236 start_label = fde->dw_fde_begin;
20238 /* ??? Bald assumption that the CIE opcode list does not contain
20239 advance opcodes. */
20240 FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
20241 lookup_cfa_1 (cfi, &next_cfa, &remember);
20243 last_cfa = next_cfa;
20244 last_label = start_label;
20246 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
20248 /* If the first partition contained no CFI adjustments, the
20249 CIE opcodes apply to the whole first partition. */
20250 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
20251 fde->dw_fde_begin, 0, fde->dw_fde_end, 0, section);
20252 list_tail =&(*list_tail)->dw_loc_next;
20253 start_label = last_label = fde->dw_fde_second_begin;
20256 FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
20258 switch (cfi->dw_cfi_opc)
20260 case DW_CFA_set_loc:
20261 case DW_CFA_advance_loc1:
20262 case DW_CFA_advance_loc2:
20263 case DW_CFA_advance_loc4:
20264 if (!cfa_equal_p (&last_cfa, &next_cfa))
20266 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
20267 start_label, 0, last_label, 0, section);
20269 list_tail = &(*list_tail)->dw_loc_next;
20270 last_cfa = next_cfa;
20271 start_label = last_label;
20273 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
20274 break;
20276 case DW_CFA_advance_loc:
20277 /* The encoding is complex enough that we should never emit this. */
20278 gcc_unreachable ();
20280 default:
20281 lookup_cfa_1 (cfi, &next_cfa, &remember);
20282 break;
20284 if (ix + 1 == fde->dw_fde_switch_cfi_index)
20286 if (!cfa_equal_p (&last_cfa, &next_cfa))
20288 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
20289 start_label, 0, last_label, 0, section);
20291 list_tail = &(*list_tail)->dw_loc_next;
20292 last_cfa = next_cfa;
20293 start_label = last_label;
20295 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
20296 start_label, 0, fde->dw_fde_end, 0, section);
20297 list_tail = &(*list_tail)->dw_loc_next;
20298 start_label = last_label = fde->dw_fde_second_begin;
20302 if (!cfa_equal_p (&last_cfa, &next_cfa))
20304 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
20305 start_label, 0, last_label, 0, section);
20306 list_tail = &(*list_tail)->dw_loc_next;
20307 start_label = last_label;
20310 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
20311 start_label, 0,
20312 fde->dw_fde_second_begin
20313 ? fde->dw_fde_second_end : fde->dw_fde_end, 0,
20314 section);
20316 maybe_gen_llsym (list);
20318 return list;
20321 /* Compute a displacement from the "steady-state frame pointer" to the
20322 frame base (often the same as the CFA), and store it in
20323 frame_pointer_fb_offset. OFFSET is added to the displacement
20324 before the latter is negated. */
20326 static void
20327 compute_frame_pointer_to_fb_displacement (poly_int64 offset)
20329 rtx reg, elim;
20331 #ifdef FRAME_POINTER_CFA_OFFSET
20332 reg = frame_pointer_rtx;
20333 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
20334 #else
20335 reg = arg_pointer_rtx;
20336 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
20337 #endif
20339 elim = (ira_use_lra_p
20340 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
20341 : eliminate_regs (reg, VOIDmode, NULL_RTX));
20342 elim = strip_offset_and_add (elim, &offset);
20344 frame_pointer_fb_offset = -offset;
20346 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
20347 in which to eliminate. This is because it's stack pointer isn't
20348 directly accessible as a register within the ISA. To work around
20349 this, assume that while we cannot provide a proper value for
20350 frame_pointer_fb_offset, we won't need one either. */
20351 frame_pointer_fb_offset_valid
20352 = ((SUPPORTS_STACK_ALIGNMENT
20353 && (elim == hard_frame_pointer_rtx
20354 || elim == stack_pointer_rtx))
20355 || elim == (frame_pointer_needed
20356 ? hard_frame_pointer_rtx
20357 : stack_pointer_rtx));
20360 /* Generate a DW_AT_name attribute given some string value to be included as
20361 the value of the attribute. */
20363 static void
20364 add_name_attribute (dw_die_ref die, const char *name_string)
20366 if (name_string != NULL && *name_string != 0)
20368 if (demangle_name_func)
20369 name_string = (*demangle_name_func) (name_string);
20371 add_AT_string (die, DW_AT_name, name_string);
20375 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
20376 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
20377 of TYPE accordingly.
20379 ??? This is a temporary measure until after we're able to generate
20380 regular DWARF for the complex Ada type system. */
20382 static void
20383 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
20384 dw_die_ref context_die)
20386 tree dtype;
20387 dw_die_ref dtype_die;
20389 if (!lang_hooks.types.descriptive_type)
20390 return;
20392 dtype = lang_hooks.types.descriptive_type (type);
20393 if (!dtype)
20394 return;
20396 dtype_die = lookup_type_die (dtype);
20397 if (!dtype_die)
20399 gen_type_die (dtype, context_die);
20400 dtype_die = lookup_type_die (dtype);
20401 gcc_assert (dtype_die);
20404 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
20407 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir. */
20409 static const char *
20410 comp_dir_string (void)
20412 const char *wd;
20413 char *wd1;
20414 static const char *cached_wd = NULL;
20416 if (cached_wd != NULL)
20417 return cached_wd;
20419 wd = get_src_pwd ();
20420 if (wd == NULL)
20421 return NULL;
20423 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
20425 int wdlen;
20427 wdlen = strlen (wd);
20428 wd1 = ggc_vec_alloc<char> (wdlen + 2);
20429 strcpy (wd1, wd);
20430 wd1 [wdlen] = DIR_SEPARATOR;
20431 wd1 [wdlen + 1] = 0;
20432 wd = wd1;
20435 cached_wd = remap_debug_filename (wd);
20436 return cached_wd;
20439 /* Generate a DW_AT_comp_dir attribute for DIE. */
20441 static void
20442 add_comp_dir_attribute (dw_die_ref die)
20444 const char * wd = comp_dir_string ();
20445 if (wd != NULL)
20446 add_AT_string (die, DW_AT_comp_dir, wd);
20449 /* Given a tree node VALUE describing a scalar attribute ATTR (i.e. a bound, a
20450 pointer computation, ...), output a representation for that bound according
20451 to the accepted FORMS (see enum dw_scalar_form) and add it to DIE. See
20452 loc_list_from_tree for the meaning of CONTEXT. */
20454 static void
20455 add_scalar_info (dw_die_ref die, enum dwarf_attribute attr, tree value,
20456 int forms, struct loc_descr_context *context)
20458 dw_die_ref context_die, decl_die;
20459 dw_loc_list_ref list;
20460 bool strip_conversions = true;
20461 bool placeholder_seen = false;
20463 while (strip_conversions)
20464 switch (TREE_CODE (value))
20466 case ERROR_MARK:
20467 case SAVE_EXPR:
20468 return;
20470 CASE_CONVERT:
20471 case VIEW_CONVERT_EXPR:
20472 value = TREE_OPERAND (value, 0);
20473 break;
20475 default:
20476 strip_conversions = false;
20477 break;
20480 /* If possible and permitted, output the attribute as a constant. */
20481 if ((forms & dw_scalar_form_constant) != 0
20482 && TREE_CODE (value) == INTEGER_CST)
20484 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (value));
20486 /* If HOST_WIDE_INT is big enough then represent the bound as
20487 a constant value. We need to choose a form based on
20488 whether the type is signed or unsigned. We cannot just
20489 call add_AT_unsigned if the value itself is positive
20490 (add_AT_unsigned might add the unsigned value encoded as
20491 DW_FORM_data[1248]). Some DWARF consumers will lookup the
20492 bounds type and then sign extend any unsigned values found
20493 for signed types. This is needed only for
20494 DW_AT_{lower,upper}_bound, since for most other attributes,
20495 consumers will treat DW_FORM_data[1248] as unsigned values,
20496 regardless of the underlying type. */
20497 if (prec <= HOST_BITS_PER_WIDE_INT
20498 || tree_fits_uhwi_p (value))
20500 if (TYPE_UNSIGNED (TREE_TYPE (value)))
20501 add_AT_unsigned (die, attr, TREE_INT_CST_LOW (value));
20502 else
20503 add_AT_int (die, attr, TREE_INT_CST_LOW (value));
20505 else
20506 /* Otherwise represent the bound as an unsigned value with
20507 the precision of its type. The precision and signedness
20508 of the type will be necessary to re-interpret it
20509 unambiguously. */
20510 add_AT_wide (die, attr, wi::to_wide (value));
20511 return;
20514 /* Otherwise, if it's possible and permitted too, output a reference to
20515 another DIE. */
20516 if ((forms & dw_scalar_form_reference) != 0)
20518 tree decl = NULL_TREE;
20520 /* Some type attributes reference an outer type. For instance, the upper
20521 bound of an array may reference an embedding record (this happens in
20522 Ada). */
20523 if (TREE_CODE (value) == COMPONENT_REF
20524 && TREE_CODE (TREE_OPERAND (value, 0)) == PLACEHOLDER_EXPR
20525 && TREE_CODE (TREE_OPERAND (value, 1)) == FIELD_DECL)
20526 decl = TREE_OPERAND (value, 1);
20528 else if (VAR_P (value)
20529 || TREE_CODE (value) == PARM_DECL
20530 || TREE_CODE (value) == RESULT_DECL)
20531 decl = value;
20533 if (decl != NULL_TREE)
20535 dw_die_ref decl_die = lookup_decl_die (decl);
20537 /* ??? Can this happen, or should the variable have been bound
20538 first? Probably it can, since I imagine that we try to create
20539 the types of parameters in the order in which they exist in
20540 the list, and won't have created a forward reference to a
20541 later parameter. */
20542 if (decl_die != NULL)
20544 add_AT_die_ref (die, attr, decl_die);
20545 return;
20550 /* Last chance: try to create a stack operation procedure to evaluate the
20551 value. Do nothing if even that is not possible or permitted. */
20552 if ((forms & dw_scalar_form_exprloc) == 0)
20553 return;
20555 list = loc_list_from_tree (value, 2, context);
20556 if (context && context->placeholder_arg)
20558 placeholder_seen = context->placeholder_seen;
20559 context->placeholder_seen = false;
20561 if (list == NULL || single_element_loc_list_p (list))
20563 /* If this attribute is not a reference nor constant, it is
20564 a DWARF expression rather than location description. For that
20565 loc_list_from_tree (value, 0, &context) is needed. */
20566 dw_loc_list_ref list2 = loc_list_from_tree (value, 0, context);
20567 if (list2 && single_element_loc_list_p (list2))
20569 if (placeholder_seen)
20571 struct dwarf_procedure_info dpi;
20572 dpi.fndecl = NULL_TREE;
20573 dpi.args_count = 1;
20574 if (!resolve_args_picking (list2->expr, 1, &dpi))
20575 return;
20577 add_AT_loc (die, attr, list2->expr);
20578 return;
20582 /* If that failed to give a single element location list, fall back to
20583 outputting this as a reference... still if permitted. */
20584 if (list == NULL
20585 || (forms & dw_scalar_form_reference) == 0
20586 || placeholder_seen)
20587 return;
20589 if (current_function_decl == 0)
20590 context_die = comp_unit_die ();
20591 else
20592 context_die = lookup_decl_die (current_function_decl);
20594 decl_die = new_die (DW_TAG_variable, context_die, value);
20595 add_AT_flag (decl_die, DW_AT_artificial, 1);
20596 add_type_attribute (decl_die, TREE_TYPE (value), TYPE_QUAL_CONST, false,
20597 context_die);
20598 add_AT_location_description (decl_die, DW_AT_location, list);
20599 add_AT_die_ref (die, attr, decl_die);
20602 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
20603 default. */
20605 static int
20606 lower_bound_default (void)
20608 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
20610 case DW_LANG_C:
20611 case DW_LANG_C89:
20612 case DW_LANG_C99:
20613 case DW_LANG_C11:
20614 case DW_LANG_C_plus_plus:
20615 case DW_LANG_C_plus_plus_11:
20616 case DW_LANG_C_plus_plus_14:
20617 case DW_LANG_ObjC:
20618 case DW_LANG_ObjC_plus_plus:
20619 return 0;
20620 case DW_LANG_Fortran77:
20621 case DW_LANG_Fortran90:
20622 case DW_LANG_Fortran95:
20623 case DW_LANG_Fortran03:
20624 case DW_LANG_Fortran08:
20625 return 1;
20626 case DW_LANG_UPC:
20627 case DW_LANG_D:
20628 case DW_LANG_Python:
20629 return dwarf_version >= 4 ? 0 : -1;
20630 case DW_LANG_Ada95:
20631 case DW_LANG_Ada83:
20632 case DW_LANG_Cobol74:
20633 case DW_LANG_Cobol85:
20634 case DW_LANG_Modula2:
20635 case DW_LANG_PLI:
20636 return dwarf_version >= 4 ? 1 : -1;
20637 default:
20638 return -1;
20642 /* Given a tree node describing an array bound (either lower or upper) output
20643 a representation for that bound. */
20645 static void
20646 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr,
20647 tree bound, struct loc_descr_context *context)
20649 int dflt;
20651 while (1)
20652 switch (TREE_CODE (bound))
20654 /* Strip all conversions. */
20655 CASE_CONVERT:
20656 case VIEW_CONVERT_EXPR:
20657 bound = TREE_OPERAND (bound, 0);
20658 break;
20660 /* All fixed-bounds are represented by INTEGER_CST nodes. Lower bounds
20661 are even omitted when they are the default. */
20662 case INTEGER_CST:
20663 /* If the value for this bound is the default one, we can even omit the
20664 attribute. */
20665 if (bound_attr == DW_AT_lower_bound
20666 && tree_fits_shwi_p (bound)
20667 && (dflt = lower_bound_default ()) != -1
20668 && tree_to_shwi (bound) == dflt)
20669 return;
20671 /* FALLTHRU */
20673 default:
20674 /* Because of the complex interaction there can be with other GNAT
20675 encodings, GDB isn't ready yet to handle proper DWARF description
20676 for self-referencial subrange bounds: let GNAT encodings do the
20677 magic in such a case. */
20678 if (is_ada ()
20679 && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL
20680 && contains_placeholder_p (bound))
20681 return;
20683 add_scalar_info (subrange_die, bound_attr, bound,
20684 dw_scalar_form_constant
20685 | dw_scalar_form_exprloc
20686 | dw_scalar_form_reference,
20687 context);
20688 return;
20692 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
20693 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
20694 Note that the block of subscript information for an array type also
20695 includes information about the element type of the given array type.
20697 This function reuses previously set type and bound information if
20698 available. */
20700 static void
20701 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
20703 unsigned dimension_number;
20704 tree lower, upper;
20705 dw_die_ref child = type_die->die_child;
20707 for (dimension_number = 0;
20708 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
20709 type = TREE_TYPE (type), dimension_number++)
20711 tree domain = TYPE_DOMAIN (type);
20713 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
20714 break;
20716 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
20717 and (in GNU C only) variable bounds. Handle all three forms
20718 here. */
20720 /* Find and reuse a previously generated DW_TAG_subrange_type if
20721 available.
20723 For multi-dimensional arrays, as we iterate through the
20724 various dimensions in the enclosing for loop above, we also
20725 iterate through the DIE children and pick at each
20726 DW_TAG_subrange_type previously generated (if available).
20727 Each child DW_TAG_subrange_type DIE describes the range of
20728 the current dimension. At this point we should have as many
20729 DW_TAG_subrange_type's as we have dimensions in the
20730 array. */
20731 dw_die_ref subrange_die = NULL;
20732 if (child)
20733 while (1)
20735 child = child->die_sib;
20736 if (child->die_tag == DW_TAG_subrange_type)
20737 subrange_die = child;
20738 if (child == type_die->die_child)
20740 /* If we wrapped around, stop looking next time. */
20741 child = NULL;
20742 break;
20744 if (child->die_tag == DW_TAG_subrange_type)
20745 break;
20747 if (!subrange_die)
20748 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
20750 if (domain)
20752 /* We have an array type with specified bounds. */
20753 lower = TYPE_MIN_VALUE (domain);
20754 upper = TYPE_MAX_VALUE (domain);
20756 /* Define the index type. */
20757 if (TREE_TYPE (domain)
20758 && !get_AT (subrange_die, DW_AT_type))
20760 /* ??? This is probably an Ada unnamed subrange type. Ignore the
20761 TREE_TYPE field. We can't emit debug info for this
20762 because it is an unnamed integral type. */
20763 if (TREE_CODE (domain) == INTEGER_TYPE
20764 && TYPE_NAME (domain) == NULL_TREE
20765 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
20766 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
20768 else
20769 add_type_attribute (subrange_die, TREE_TYPE (domain),
20770 TYPE_UNQUALIFIED, false, type_die);
20773 /* ??? If upper is NULL, the array has unspecified length,
20774 but it does have a lower bound. This happens with Fortran
20775 dimension arr(N:*)
20776 Since the debugger is definitely going to need to know N
20777 to produce useful results, go ahead and output the lower
20778 bound solo, and hope the debugger can cope. */
20780 if (!get_AT (subrange_die, DW_AT_lower_bound))
20781 add_bound_info (subrange_die, DW_AT_lower_bound, lower, NULL);
20782 if (upper && !get_AT (subrange_die, DW_AT_upper_bound))
20783 add_bound_info (subrange_die, DW_AT_upper_bound, upper, NULL);
20786 /* Otherwise we have an array type with an unspecified length. The
20787 DWARF-2 spec does not say how to handle this; let's just leave out the
20788 bounds. */
20792 /* Add a DW_AT_byte_size attribute to DIE with TREE_NODE's size. */
20794 static void
20795 add_byte_size_attribute (dw_die_ref die, tree tree_node)
20797 dw_die_ref decl_die;
20798 HOST_WIDE_INT size;
20799 dw_loc_descr_ref size_expr = NULL;
20801 switch (TREE_CODE (tree_node))
20803 case ERROR_MARK:
20804 size = 0;
20805 break;
20806 case ENUMERAL_TYPE:
20807 case RECORD_TYPE:
20808 case UNION_TYPE:
20809 case QUAL_UNION_TYPE:
20810 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
20811 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
20813 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
20814 return;
20816 size_expr = type_byte_size (tree_node, &size);
20817 break;
20818 case FIELD_DECL:
20819 /* For a data member of a struct or union, the DW_AT_byte_size is
20820 generally given as the number of bytes normally allocated for an
20821 object of the *declared* type of the member itself. This is true
20822 even for bit-fields. */
20823 size = int_size_in_bytes (field_type (tree_node));
20824 break;
20825 default:
20826 gcc_unreachable ();
20829 /* Support for dynamically-sized objects was introduced by DWARFv3.
20830 At the moment, GDB does not handle variable byte sizes very well,
20831 though. */
20832 if ((dwarf_version >= 3 || !dwarf_strict)
20833 && gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL
20834 && size_expr != NULL)
20835 add_AT_loc (die, DW_AT_byte_size, size_expr);
20837 /* Note that `size' might be -1 when we get to this point. If it is, that
20838 indicates that the byte size of the entity in question is variable and
20839 that we could not generate a DWARF expression that computes it. */
20840 if (size >= 0)
20841 add_AT_unsigned (die, DW_AT_byte_size, size);
20844 /* Add a DW_AT_alignment attribute to DIE with TREE_NODE's non-default
20845 alignment. */
20847 static void
20848 add_alignment_attribute (dw_die_ref die, tree tree_node)
20850 if (dwarf_version < 5 && dwarf_strict)
20851 return;
20853 unsigned align;
20855 if (DECL_P (tree_node))
20857 if (!DECL_USER_ALIGN (tree_node))
20858 return;
20860 align = DECL_ALIGN_UNIT (tree_node);
20862 else if (TYPE_P (tree_node))
20864 if (!TYPE_USER_ALIGN (tree_node))
20865 return;
20867 align = TYPE_ALIGN_UNIT (tree_node);
20869 else
20870 gcc_unreachable ();
20872 add_AT_unsigned (die, DW_AT_alignment, align);
20875 /* For a FIELD_DECL node which represents a bit-field, output an attribute
20876 which specifies the distance in bits from the highest order bit of the
20877 "containing object" for the bit-field to the highest order bit of the
20878 bit-field itself.
20880 For any given bit-field, the "containing object" is a hypothetical object
20881 (of some integral or enum type) within which the given bit-field lives. The
20882 type of this hypothetical "containing object" is always the same as the
20883 declared type of the individual bit-field itself. The determination of the
20884 exact location of the "containing object" for a bit-field is rather
20885 complicated. It's handled by the `field_byte_offset' function (above).
20887 CTX is required: see the comment for VLR_CONTEXT.
20889 Note that it is the size (in bytes) of the hypothetical "containing object"
20890 which will be given in the DW_AT_byte_size attribute for this bit-field.
20891 (See `byte_size_attribute' above). */
20893 static inline void
20894 add_bit_offset_attribute (dw_die_ref die, tree decl, struct vlr_context *ctx)
20896 HOST_WIDE_INT object_offset_in_bytes;
20897 tree original_type = DECL_BIT_FIELD_TYPE (decl);
20898 HOST_WIDE_INT bitpos_int;
20899 HOST_WIDE_INT highest_order_object_bit_offset;
20900 HOST_WIDE_INT highest_order_field_bit_offset;
20901 HOST_WIDE_INT bit_offset;
20903 field_byte_offset (decl, ctx, &object_offset_in_bytes);
20905 /* Must be a field and a bit field. */
20906 gcc_assert (original_type && TREE_CODE (decl) == FIELD_DECL);
20908 /* We can't yet handle bit-fields whose offsets are variable, so if we
20909 encounter such things, just return without generating any attribute
20910 whatsoever. Likewise for variable or too large size. */
20911 if (! tree_fits_shwi_p (bit_position (decl))
20912 || ! tree_fits_uhwi_p (DECL_SIZE (decl)))
20913 return;
20915 bitpos_int = int_bit_position (decl);
20917 /* Note that the bit offset is always the distance (in bits) from the
20918 highest-order bit of the "containing object" to the highest-order bit of
20919 the bit-field itself. Since the "high-order end" of any object or field
20920 is different on big-endian and little-endian machines, the computation
20921 below must take account of these differences. */
20922 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
20923 highest_order_field_bit_offset = bitpos_int;
20925 if (! BYTES_BIG_ENDIAN)
20927 highest_order_field_bit_offset += tree_to_shwi (DECL_SIZE (decl));
20928 highest_order_object_bit_offset +=
20929 simple_type_size_in_bits (original_type);
20932 bit_offset
20933 = (! BYTES_BIG_ENDIAN
20934 ? highest_order_object_bit_offset - highest_order_field_bit_offset
20935 : highest_order_field_bit_offset - highest_order_object_bit_offset);
20937 if (bit_offset < 0)
20938 add_AT_int (die, DW_AT_bit_offset, bit_offset);
20939 else
20940 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
20943 /* For a FIELD_DECL node which represents a bit field, output an attribute
20944 which specifies the length in bits of the given field. */
20946 static inline void
20947 add_bit_size_attribute (dw_die_ref die, tree decl)
20949 /* Must be a field and a bit field. */
20950 gcc_assert (TREE_CODE (decl) == FIELD_DECL
20951 && DECL_BIT_FIELD_TYPE (decl));
20953 if (tree_fits_uhwi_p (DECL_SIZE (decl)))
20954 add_AT_unsigned (die, DW_AT_bit_size, tree_to_uhwi (DECL_SIZE (decl)));
20957 /* If the compiled language is ANSI C, then add a 'prototyped'
20958 attribute, if arg types are given for the parameters of a function. */
20960 static inline void
20961 add_prototyped_attribute (dw_die_ref die, tree func_type)
20963 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
20965 case DW_LANG_C:
20966 case DW_LANG_C89:
20967 case DW_LANG_C99:
20968 case DW_LANG_C11:
20969 case DW_LANG_ObjC:
20970 if (prototype_p (func_type))
20971 add_AT_flag (die, DW_AT_prototyped, 1);
20972 break;
20973 default:
20974 break;
20978 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
20979 by looking in the type declaration, the object declaration equate table or
20980 the block mapping. */
20982 static inline dw_die_ref
20983 add_abstract_origin_attribute (dw_die_ref die, tree origin)
20985 dw_die_ref origin_die = NULL;
20987 if (DECL_P (origin))
20989 dw_die_ref c;
20990 origin_die = lookup_decl_die (origin);
20991 /* "Unwrap" the decls DIE which we put in the imported unit context.
20992 We are looking for the abstract copy here. */
20993 if (in_lto_p
20994 && origin_die
20995 && (c = get_AT_ref (origin_die, DW_AT_abstract_origin))
20996 /* ??? Identify this better. */
20997 && c->with_offset)
20998 origin_die = c;
21000 else if (TYPE_P (origin))
21001 origin_die = lookup_type_die (origin);
21002 else if (TREE_CODE (origin) == BLOCK)
21003 origin_die = BLOCK_DIE (origin);
21005 /* XXX: Functions that are never lowered don't always have correct block
21006 trees (in the case of java, they simply have no block tree, in some other
21007 languages). For these functions, there is nothing we can really do to
21008 output correct debug info for inlined functions in all cases. Rather
21009 than die, we'll just produce deficient debug info now, in that we will
21010 have variables without a proper abstract origin. In the future, when all
21011 functions are lowered, we should re-add a gcc_assert (origin_die)
21012 here. */
21014 if (origin_die)
21015 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
21016 return origin_die;
21019 /* We do not currently support the pure_virtual attribute. */
21021 static inline void
21022 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
21024 if (DECL_VINDEX (func_decl))
21026 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
21028 if (tree_fits_shwi_p (DECL_VINDEX (func_decl)))
21029 add_AT_loc (die, DW_AT_vtable_elem_location,
21030 new_loc_descr (DW_OP_constu,
21031 tree_to_shwi (DECL_VINDEX (func_decl)),
21032 0));
21034 /* GNU extension: Record what type this method came from originally. */
21035 if (debug_info_level > DINFO_LEVEL_TERSE
21036 && DECL_CONTEXT (func_decl))
21037 add_AT_die_ref (die, DW_AT_containing_type,
21038 lookup_type_die (DECL_CONTEXT (func_decl)));
21042 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
21043 given decl. This used to be a vendor extension until after DWARF 4
21044 standardized it. */
21046 static void
21047 add_linkage_attr (dw_die_ref die, tree decl)
21049 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
21051 /* Mimic what assemble_name_raw does with a leading '*'. */
21052 if (name[0] == '*')
21053 name = &name[1];
21055 if (dwarf_version >= 4)
21056 add_AT_string (die, DW_AT_linkage_name, name);
21057 else
21058 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
21061 /* Add source coordinate attributes for the given decl. */
21063 static void
21064 add_src_coords_attributes (dw_die_ref die, tree decl)
21066 expanded_location s;
21068 if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
21069 return;
21070 s = expand_location (DECL_SOURCE_LOCATION (decl));
21071 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
21072 add_AT_unsigned (die, DW_AT_decl_line, s.line);
21073 if (debug_column_info && s.column)
21074 add_AT_unsigned (die, DW_AT_decl_column, s.column);
21077 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
21079 static void
21080 add_linkage_name_raw (dw_die_ref die, tree decl)
21082 /* Defer until we have an assembler name set. */
21083 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
21085 limbo_die_node *asm_name;
21087 asm_name = ggc_cleared_alloc<limbo_die_node> ();
21088 asm_name->die = die;
21089 asm_name->created_for = decl;
21090 asm_name->next = deferred_asm_name;
21091 deferred_asm_name = asm_name;
21093 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
21094 add_linkage_attr (die, decl);
21097 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl if desired. */
21099 static void
21100 add_linkage_name (dw_die_ref die, tree decl)
21102 if (debug_info_level > DINFO_LEVEL_NONE
21103 && VAR_OR_FUNCTION_DECL_P (decl)
21104 && TREE_PUBLIC (decl)
21105 && !(VAR_P (decl) && DECL_REGISTER (decl))
21106 && die->die_tag != DW_TAG_member)
21107 add_linkage_name_raw (die, decl);
21110 /* Add a DW_AT_name attribute and source coordinate attribute for the
21111 given decl, but only if it actually has a name. */
21113 static void
21114 add_name_and_src_coords_attributes (dw_die_ref die, tree decl,
21115 bool no_linkage_name)
21117 tree decl_name;
21119 decl_name = DECL_NAME (decl);
21120 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
21122 const char *name = dwarf2_name (decl, 0);
21123 if (name)
21124 add_name_attribute (die, name);
21125 if (! DECL_ARTIFICIAL (decl))
21126 add_src_coords_attributes (die, decl);
21128 if (!no_linkage_name)
21129 add_linkage_name (die, decl);
21132 #ifdef VMS_DEBUGGING_INFO
21133 /* Get the function's name, as described by its RTL. This may be different
21134 from the DECL_NAME name used in the source file. */
21135 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
21137 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
21138 XEXP (DECL_RTL (decl), 0), false);
21139 vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
21141 #endif /* VMS_DEBUGGING_INFO */
21144 /* Add VALUE as a DW_AT_discr_value attribute to DIE. */
21146 static void
21147 add_discr_value (dw_die_ref die, dw_discr_value *value)
21149 dw_attr_node attr;
21151 attr.dw_attr = DW_AT_discr_value;
21152 attr.dw_attr_val.val_class = dw_val_class_discr_value;
21153 attr.dw_attr_val.val_entry = NULL;
21154 attr.dw_attr_val.v.val_discr_value.pos = value->pos;
21155 if (value->pos)
21156 attr.dw_attr_val.v.val_discr_value.v.uval = value->v.uval;
21157 else
21158 attr.dw_attr_val.v.val_discr_value.v.sval = value->v.sval;
21159 add_dwarf_attr (die, &attr);
21162 /* Add DISCR_LIST as a DW_AT_discr_list to DIE. */
21164 static void
21165 add_discr_list (dw_die_ref die, dw_discr_list_ref discr_list)
21167 dw_attr_node attr;
21169 attr.dw_attr = DW_AT_discr_list;
21170 attr.dw_attr_val.val_class = dw_val_class_discr_list;
21171 attr.dw_attr_val.val_entry = NULL;
21172 attr.dw_attr_val.v.val_discr_list = discr_list;
21173 add_dwarf_attr (die, &attr);
21176 static inline dw_discr_list_ref
21177 AT_discr_list (dw_attr_node *attr)
21179 return attr->dw_attr_val.v.val_discr_list;
21182 #ifdef VMS_DEBUGGING_INFO
21183 /* Output the debug main pointer die for VMS */
21185 void
21186 dwarf2out_vms_debug_main_pointer (void)
21188 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21189 dw_die_ref die;
21191 /* Allocate the VMS debug main subprogram die. */
21192 die = new_die_raw (DW_TAG_subprogram);
21193 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
21194 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
21195 current_function_funcdef_no);
21196 add_AT_lbl_id (die, DW_AT_entry_pc, label);
21198 /* Make it the first child of comp_unit_die (). */
21199 die->die_parent = comp_unit_die ();
21200 if (comp_unit_die ()->die_child)
21202 die->die_sib = comp_unit_die ()->die_child->die_sib;
21203 comp_unit_die ()->die_child->die_sib = die;
21205 else
21207 die->die_sib = die;
21208 comp_unit_die ()->die_child = die;
21211 #endif /* VMS_DEBUGGING_INFO */
21213 /* Push a new declaration scope. */
21215 static void
21216 push_decl_scope (tree scope)
21218 vec_safe_push (decl_scope_table, scope);
21221 /* Pop a declaration scope. */
21223 static inline void
21224 pop_decl_scope (void)
21226 decl_scope_table->pop ();
21229 /* walk_tree helper function for uses_local_type, below. */
21231 static tree
21232 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
21234 if (!TYPE_P (*tp))
21235 *walk_subtrees = 0;
21236 else
21238 tree name = TYPE_NAME (*tp);
21239 if (name && DECL_P (name) && decl_function_context (name))
21240 return *tp;
21242 return NULL_TREE;
21245 /* If TYPE involves a function-local type (including a local typedef to a
21246 non-local type), returns that type; otherwise returns NULL_TREE. */
21248 static tree
21249 uses_local_type (tree type)
21251 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
21252 return used;
21255 /* Return the DIE for the scope that immediately contains this type.
21256 Non-named types that do not involve a function-local type get global
21257 scope. Named types nested in namespaces or other types get their
21258 containing scope. All other types (i.e. function-local named types) get
21259 the current active scope. */
21261 static dw_die_ref
21262 scope_die_for (tree t, dw_die_ref context_die)
21264 dw_die_ref scope_die = NULL;
21265 tree containing_scope;
21267 /* Non-types always go in the current scope. */
21268 gcc_assert (TYPE_P (t));
21270 /* Use the scope of the typedef, rather than the scope of the type
21271 it refers to. */
21272 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
21273 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
21274 else
21275 containing_scope = TYPE_CONTEXT (t);
21277 /* Use the containing namespace if there is one. */
21278 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
21280 if (context_die == lookup_decl_die (containing_scope))
21281 /* OK */;
21282 else if (debug_info_level > DINFO_LEVEL_TERSE)
21283 context_die = get_context_die (containing_scope);
21284 else
21285 containing_scope = NULL_TREE;
21288 /* Ignore function type "scopes" from the C frontend. They mean that
21289 a tagged type is local to a parmlist of a function declarator, but
21290 that isn't useful to DWARF. */
21291 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
21292 containing_scope = NULL_TREE;
21294 if (SCOPE_FILE_SCOPE_P (containing_scope))
21296 /* If T uses a local type keep it local as well, to avoid references
21297 to function-local DIEs from outside the function. */
21298 if (current_function_decl && uses_local_type (t))
21299 scope_die = context_die;
21300 else
21301 scope_die = comp_unit_die ();
21303 else if (TYPE_P (containing_scope))
21305 /* For types, we can just look up the appropriate DIE. */
21306 if (debug_info_level > DINFO_LEVEL_TERSE)
21307 scope_die = get_context_die (containing_scope);
21308 else
21310 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
21311 if (scope_die == NULL)
21312 scope_die = comp_unit_die ();
21315 else
21316 scope_die = context_die;
21318 return scope_die;
21321 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
21323 static inline int
21324 local_scope_p (dw_die_ref context_die)
21326 for (; context_die; context_die = context_die->die_parent)
21327 if (context_die->die_tag == DW_TAG_inlined_subroutine
21328 || context_die->die_tag == DW_TAG_subprogram)
21329 return 1;
21331 return 0;
21334 /* Returns nonzero if CONTEXT_DIE is a class. */
21336 static inline int
21337 class_scope_p (dw_die_ref context_die)
21339 return (context_die
21340 && (context_die->die_tag == DW_TAG_structure_type
21341 || context_die->die_tag == DW_TAG_class_type
21342 || context_die->die_tag == DW_TAG_interface_type
21343 || context_die->die_tag == DW_TAG_union_type));
21346 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
21347 whether or not to treat a DIE in this context as a declaration. */
21349 static inline int
21350 class_or_namespace_scope_p (dw_die_ref context_die)
21352 return (class_scope_p (context_die)
21353 || (context_die && context_die->die_tag == DW_TAG_namespace));
21356 /* Many forms of DIEs require a "type description" attribute. This
21357 routine locates the proper "type descriptor" die for the type given
21358 by 'type' plus any additional qualifiers given by 'cv_quals', and
21359 adds a DW_AT_type attribute below the given die. */
21361 static void
21362 add_type_attribute (dw_die_ref object_die, tree type, int cv_quals,
21363 bool reverse, dw_die_ref context_die)
21365 enum tree_code code = TREE_CODE (type);
21366 dw_die_ref type_die = NULL;
21368 /* ??? If this type is an unnamed subrange type of an integral, floating-point
21369 or fixed-point type, use the inner type. This is because we have no
21370 support for unnamed types in base_type_die. This can happen if this is
21371 an Ada subrange type. Correct solution is emit a subrange type die. */
21372 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
21373 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
21374 type = TREE_TYPE (type), code = TREE_CODE (type);
21376 if (code == ERROR_MARK
21377 /* Handle a special case. For functions whose return type is void, we
21378 generate *no* type attribute. (Note that no object may have type
21379 `void', so this only applies to function return types). */
21380 || code == VOID_TYPE)
21381 return;
21383 type_die = modified_type_die (type,
21384 cv_quals | TYPE_QUALS (type),
21385 reverse,
21386 context_die);
21388 if (type_die != NULL)
21389 add_AT_die_ref (object_die, DW_AT_type, type_die);
21392 /* Given an object die, add the calling convention attribute for the
21393 function call type. */
21394 static void
21395 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
21397 enum dwarf_calling_convention value = DW_CC_normal;
21399 value = ((enum dwarf_calling_convention)
21400 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
21402 if (is_fortran ()
21403 && id_equal (DECL_ASSEMBLER_NAME (decl), "MAIN__"))
21405 /* DWARF 2 doesn't provide a way to identify a program's source-level
21406 entry point. DW_AT_calling_convention attributes are only meant
21407 to describe functions' calling conventions. However, lacking a
21408 better way to signal the Fortran main program, we used this for
21409 a long time, following existing custom. Now, DWARF 4 has
21410 DW_AT_main_subprogram, which we add below, but some tools still
21411 rely on the old way, which we thus keep. */
21412 value = DW_CC_program;
21414 if (dwarf_version >= 4 || !dwarf_strict)
21415 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
21418 /* Only add the attribute if the backend requests it, and
21419 is not DW_CC_normal. */
21420 if (value && (value != DW_CC_normal))
21421 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
21424 /* Given a tree pointer to a struct, class, union, or enum type node, return
21425 a pointer to the (string) tag name for the given type, or zero if the type
21426 was declared without a tag. */
21428 static const char *
21429 type_tag (const_tree type)
21431 const char *name = 0;
21433 if (TYPE_NAME (type) != 0)
21435 tree t = 0;
21437 /* Find the IDENTIFIER_NODE for the type name. */
21438 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
21439 && !TYPE_NAMELESS (type))
21440 t = TYPE_NAME (type);
21442 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
21443 a TYPE_DECL node, regardless of whether or not a `typedef' was
21444 involved. */
21445 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
21446 && ! DECL_IGNORED_P (TYPE_NAME (type)))
21448 /* We want to be extra verbose. Don't call dwarf_name if
21449 DECL_NAME isn't set. The default hook for decl_printable_name
21450 doesn't like that, and in this context it's correct to return
21451 0, instead of "<anonymous>" or the like. */
21452 if (DECL_NAME (TYPE_NAME (type))
21453 && !DECL_NAMELESS (TYPE_NAME (type)))
21454 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
21457 /* Now get the name as a string, or invent one. */
21458 if (!name && t != 0)
21459 name = IDENTIFIER_POINTER (t);
21462 return (name == 0 || *name == '\0') ? 0 : name;
21465 /* Return the type associated with a data member, make a special check
21466 for bit field types. */
21468 static inline tree
21469 member_declared_type (const_tree member)
21471 return (DECL_BIT_FIELD_TYPE (member)
21472 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
21475 /* Get the decl's label, as described by its RTL. This may be different
21476 from the DECL_NAME name used in the source file. */
21478 #if 0
21479 static const char *
21480 decl_start_label (tree decl)
21482 rtx x;
21483 const char *fnname;
21485 x = DECL_RTL (decl);
21486 gcc_assert (MEM_P (x));
21488 x = XEXP (x, 0);
21489 gcc_assert (GET_CODE (x) == SYMBOL_REF);
21491 fnname = XSTR (x, 0);
21492 return fnname;
21494 #endif
21496 /* For variable-length arrays that have been previously generated, but
21497 may be incomplete due to missing subscript info, fill the subscript
21498 info. Return TRUE if this is one of those cases. */
21499 static bool
21500 fill_variable_array_bounds (tree type)
21502 if (TREE_ASM_WRITTEN (type)
21503 && TREE_CODE (type) == ARRAY_TYPE
21504 && variably_modified_type_p (type, NULL))
21506 dw_die_ref array_die = lookup_type_die (type);
21507 if (!array_die)
21508 return false;
21509 add_subscript_info (array_die, type, !is_ada ());
21510 return true;
21512 return false;
21515 /* These routines generate the internal representation of the DIE's for
21516 the compilation unit. Debugging information is collected by walking
21517 the declaration trees passed in from dwarf2out_decl(). */
21519 static void
21520 gen_array_type_die (tree type, dw_die_ref context_die)
21522 dw_die_ref array_die;
21524 /* GNU compilers represent multidimensional array types as sequences of one
21525 dimensional array types whose element types are themselves array types.
21526 We sometimes squish that down to a single array_type DIE with multiple
21527 subscripts in the Dwarf debugging info. The draft Dwarf specification
21528 say that we are allowed to do this kind of compression in C, because
21529 there is no difference between an array of arrays and a multidimensional
21530 array. We don't do this for Ada to remain as close as possible to the
21531 actual representation, which is especially important against the language
21532 flexibilty wrt arrays of variable size. */
21534 bool collapse_nested_arrays = !is_ada ();
21536 if (fill_variable_array_bounds (type))
21537 return;
21539 dw_die_ref scope_die = scope_die_for (type, context_die);
21540 tree element_type;
21542 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
21543 DW_TAG_string_type doesn't have DW_AT_type attribute). */
21544 if (TYPE_STRING_FLAG (type)
21545 && TREE_CODE (type) == ARRAY_TYPE
21546 && is_fortran ()
21547 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
21549 HOST_WIDE_INT size;
21551 array_die = new_die (DW_TAG_string_type, scope_die, type);
21552 add_name_attribute (array_die, type_tag (type));
21553 equate_type_number_to_die (type, array_die);
21554 size = int_size_in_bytes (type);
21555 if (size >= 0)
21556 add_AT_unsigned (array_die, DW_AT_byte_size, size);
21557 /* ??? We can't annotate types late, but for LTO we may not
21558 generate a location early either (gfortran.dg/save_6.f90). */
21559 else if (! (early_dwarf && (flag_generate_lto || flag_generate_offload))
21560 && TYPE_DOMAIN (type) != NULL_TREE
21561 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE)
21563 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
21564 tree rszdecl = szdecl;
21566 size = int_size_in_bytes (TREE_TYPE (szdecl));
21567 if (!DECL_P (szdecl))
21569 if (TREE_CODE (szdecl) == INDIRECT_REF
21570 && DECL_P (TREE_OPERAND (szdecl, 0)))
21572 rszdecl = TREE_OPERAND (szdecl, 0);
21573 if (int_size_in_bytes (TREE_TYPE (rszdecl))
21574 != DWARF2_ADDR_SIZE)
21575 size = 0;
21577 else
21578 size = 0;
21580 if (size > 0)
21582 dw_loc_list_ref loc
21583 = loc_list_from_tree (rszdecl, szdecl == rszdecl ? 2 : 0,
21584 NULL);
21585 if (loc)
21587 add_AT_location_description (array_die, DW_AT_string_length,
21588 loc);
21589 if (size != DWARF2_ADDR_SIZE)
21590 add_AT_unsigned (array_die, dwarf_version >= 5
21591 ? DW_AT_string_length_byte_size
21592 : DW_AT_byte_size, size);
21596 return;
21599 array_die = new_die (DW_TAG_array_type, scope_die, type);
21600 add_name_attribute (array_die, type_tag (type));
21601 equate_type_number_to_die (type, array_die);
21603 if (TREE_CODE (type) == VECTOR_TYPE)
21604 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
21606 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
21607 if (is_fortran ()
21608 && TREE_CODE (type) == ARRAY_TYPE
21609 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
21610 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
21611 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
21613 #if 0
21614 /* We default the array ordering. Debuggers will probably do the right
21615 things even if DW_AT_ordering is not present. It's not even an issue
21616 until we start to get into multidimensional arrays anyway. If a debugger
21617 is ever caught doing the Wrong Thing for multi-dimensional arrays,
21618 then we'll have to put the DW_AT_ordering attribute back in. (But if
21619 and when we find out that we need to put these in, we will only do so
21620 for multidimensional arrays. */
21621 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
21622 #endif
21624 if (TREE_CODE (type) == VECTOR_TYPE)
21626 /* For VECTOR_TYPEs we use an array die with appropriate bounds. */
21627 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
21628 add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node, NULL);
21629 add_bound_info (subrange_die, DW_AT_upper_bound,
21630 size_int (TYPE_VECTOR_SUBPARTS (type) - 1), NULL);
21632 else
21633 add_subscript_info (array_die, type, collapse_nested_arrays);
21635 /* Add representation of the type of the elements of this array type and
21636 emit the corresponding DIE if we haven't done it already. */
21637 element_type = TREE_TYPE (type);
21638 if (collapse_nested_arrays)
21639 while (TREE_CODE (element_type) == ARRAY_TYPE)
21641 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
21642 break;
21643 element_type = TREE_TYPE (element_type);
21646 add_type_attribute (array_die, element_type, TYPE_UNQUALIFIED,
21647 TREE_CODE (type) == ARRAY_TYPE
21648 && TYPE_REVERSE_STORAGE_ORDER (type),
21649 context_die);
21651 add_gnat_descriptive_type_attribute (array_die, type, context_die);
21652 if (TYPE_ARTIFICIAL (type))
21653 add_AT_flag (array_die, DW_AT_artificial, 1);
21655 if (get_AT (array_die, DW_AT_name))
21656 add_pubtype (type, array_die);
21658 add_alignment_attribute (array_die, type);
21661 /* This routine generates DIE for array with hidden descriptor, details
21662 are filled into *info by a langhook. */
21664 static void
21665 gen_descr_array_type_die (tree type, struct array_descr_info *info,
21666 dw_die_ref context_die)
21668 const dw_die_ref scope_die = scope_die_for (type, context_die);
21669 const dw_die_ref array_die = new_die (DW_TAG_array_type, scope_die, type);
21670 struct loc_descr_context context = { type, info->base_decl, NULL,
21671 false, false };
21672 enum dwarf_tag subrange_tag = DW_TAG_subrange_type;
21673 int dim;
21675 add_name_attribute (array_die, type_tag (type));
21676 equate_type_number_to_die (type, array_die);
21678 if (info->ndimensions > 1)
21679 switch (info->ordering)
21681 case array_descr_ordering_row_major:
21682 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
21683 break;
21684 case array_descr_ordering_column_major:
21685 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
21686 break;
21687 default:
21688 break;
21691 if (dwarf_version >= 3 || !dwarf_strict)
21693 if (info->data_location)
21694 add_scalar_info (array_die, DW_AT_data_location, info->data_location,
21695 dw_scalar_form_exprloc, &context);
21696 if (info->associated)
21697 add_scalar_info (array_die, DW_AT_associated, info->associated,
21698 dw_scalar_form_constant
21699 | dw_scalar_form_exprloc
21700 | dw_scalar_form_reference, &context);
21701 if (info->allocated)
21702 add_scalar_info (array_die, DW_AT_allocated, info->allocated,
21703 dw_scalar_form_constant
21704 | dw_scalar_form_exprloc
21705 | dw_scalar_form_reference, &context);
21706 if (info->stride)
21708 const enum dwarf_attribute attr
21709 = (info->stride_in_bits) ? DW_AT_bit_stride : DW_AT_byte_stride;
21710 const int forms
21711 = (info->stride_in_bits)
21712 ? dw_scalar_form_constant
21713 : (dw_scalar_form_constant
21714 | dw_scalar_form_exprloc
21715 | dw_scalar_form_reference);
21717 add_scalar_info (array_die, attr, info->stride, forms, &context);
21720 if (dwarf_version >= 5)
21722 if (info->rank)
21724 add_scalar_info (array_die, DW_AT_rank, info->rank,
21725 dw_scalar_form_constant
21726 | dw_scalar_form_exprloc, &context);
21727 subrange_tag = DW_TAG_generic_subrange;
21728 context.placeholder_arg = true;
21732 add_gnat_descriptive_type_attribute (array_die, type, context_die);
21734 for (dim = 0; dim < info->ndimensions; dim++)
21736 dw_die_ref subrange_die = new_die (subrange_tag, array_die, NULL);
21738 if (info->dimen[dim].bounds_type)
21739 add_type_attribute (subrange_die,
21740 info->dimen[dim].bounds_type, TYPE_UNQUALIFIED,
21741 false, context_die);
21742 if (info->dimen[dim].lower_bound)
21743 add_bound_info (subrange_die, DW_AT_lower_bound,
21744 info->dimen[dim].lower_bound, &context);
21745 if (info->dimen[dim].upper_bound)
21746 add_bound_info (subrange_die, DW_AT_upper_bound,
21747 info->dimen[dim].upper_bound, &context);
21748 if ((dwarf_version >= 3 || !dwarf_strict) && info->dimen[dim].stride)
21749 add_scalar_info (subrange_die, DW_AT_byte_stride,
21750 info->dimen[dim].stride,
21751 dw_scalar_form_constant
21752 | dw_scalar_form_exprloc
21753 | dw_scalar_form_reference,
21754 &context);
21757 gen_type_die (info->element_type, context_die);
21758 add_type_attribute (array_die, info->element_type, TYPE_UNQUALIFIED,
21759 TREE_CODE (type) == ARRAY_TYPE
21760 && TYPE_REVERSE_STORAGE_ORDER (type),
21761 context_die);
21763 if (get_AT (array_die, DW_AT_name))
21764 add_pubtype (type, array_die);
21766 add_alignment_attribute (array_die, type);
21769 #if 0
21770 static void
21771 gen_entry_point_die (tree decl, dw_die_ref context_die)
21773 tree origin = decl_ultimate_origin (decl);
21774 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
21776 if (origin != NULL)
21777 add_abstract_origin_attribute (decl_die, origin);
21778 else
21780 add_name_and_src_coords_attributes (decl_die, decl);
21781 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
21782 TYPE_UNQUALIFIED, false, context_die);
21785 if (DECL_ABSTRACT_P (decl))
21786 equate_decl_number_to_die (decl, decl_die);
21787 else
21788 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
21790 #endif
21792 /* Walk through the list of incomplete types again, trying once more to
21793 emit full debugging info for them. */
21795 static void
21796 retry_incomplete_types (void)
21798 set_early_dwarf s;
21799 int i;
21801 for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
21802 if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
21803 gen_type_die ((*incomplete_types)[i], comp_unit_die ());
21804 vec_safe_truncate (incomplete_types, 0);
21807 /* Determine what tag to use for a record type. */
21809 static enum dwarf_tag
21810 record_type_tag (tree type)
21812 if (! lang_hooks.types.classify_record)
21813 return DW_TAG_structure_type;
21815 switch (lang_hooks.types.classify_record (type))
21817 case RECORD_IS_STRUCT:
21818 return DW_TAG_structure_type;
21820 case RECORD_IS_CLASS:
21821 return DW_TAG_class_type;
21823 case RECORD_IS_INTERFACE:
21824 if (dwarf_version >= 3 || !dwarf_strict)
21825 return DW_TAG_interface_type;
21826 return DW_TAG_structure_type;
21828 default:
21829 gcc_unreachable ();
21833 /* Generate a DIE to represent an enumeration type. Note that these DIEs
21834 include all of the information about the enumeration values also. Each
21835 enumerated type name/value is listed as a child of the enumerated type
21836 DIE. */
21838 static dw_die_ref
21839 gen_enumeration_type_die (tree type, dw_die_ref context_die)
21841 dw_die_ref type_die = lookup_type_die (type);
21843 if (type_die == NULL)
21845 type_die = new_die (DW_TAG_enumeration_type,
21846 scope_die_for (type, context_die), type);
21847 equate_type_number_to_die (type, type_die);
21848 add_name_attribute (type_die, type_tag (type));
21849 if (dwarf_version >= 4 || !dwarf_strict)
21851 if (ENUM_IS_SCOPED (type))
21852 add_AT_flag (type_die, DW_AT_enum_class, 1);
21853 if (ENUM_IS_OPAQUE (type))
21854 add_AT_flag (type_die, DW_AT_declaration, 1);
21856 if (!dwarf_strict)
21857 add_AT_unsigned (type_die, DW_AT_encoding,
21858 TYPE_UNSIGNED (type)
21859 ? DW_ATE_unsigned
21860 : DW_ATE_signed);
21862 else if (! TYPE_SIZE (type))
21863 return type_die;
21864 else
21865 remove_AT (type_die, DW_AT_declaration);
21867 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
21868 given enum type is incomplete, do not generate the DW_AT_byte_size
21869 attribute or the DW_AT_element_list attribute. */
21870 if (TYPE_SIZE (type))
21872 tree link;
21874 TREE_ASM_WRITTEN (type) = 1;
21875 add_byte_size_attribute (type_die, type);
21876 add_alignment_attribute (type_die, type);
21877 if (dwarf_version >= 3 || !dwarf_strict)
21879 tree underlying = lang_hooks.types.enum_underlying_base_type (type);
21880 add_type_attribute (type_die, underlying, TYPE_UNQUALIFIED, false,
21881 context_die);
21883 if (TYPE_STUB_DECL (type) != NULL_TREE)
21885 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
21886 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
21889 /* If the first reference to this type was as the return type of an
21890 inline function, then it may not have a parent. Fix this now. */
21891 if (type_die->die_parent == NULL)
21892 add_child_die (scope_die_for (type, context_die), type_die);
21894 for (link = TYPE_VALUES (type);
21895 link != NULL; link = TREE_CHAIN (link))
21897 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
21898 tree value = TREE_VALUE (link);
21900 add_name_attribute (enum_die,
21901 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
21903 if (TREE_CODE (value) == CONST_DECL)
21904 value = DECL_INITIAL (value);
21906 if (simple_type_size_in_bits (TREE_TYPE (value))
21907 <= HOST_BITS_PER_WIDE_INT || tree_fits_shwi_p (value))
21909 /* For constant forms created by add_AT_unsigned DWARF
21910 consumers (GDB, elfutils, etc.) always zero extend
21911 the value. Only when the actual value is negative
21912 do we need to use add_AT_int to generate a constant
21913 form that can represent negative values. */
21914 HOST_WIDE_INT val = TREE_INT_CST_LOW (value);
21915 if (TYPE_UNSIGNED (TREE_TYPE (value)) || val >= 0)
21916 add_AT_unsigned (enum_die, DW_AT_const_value,
21917 (unsigned HOST_WIDE_INT) val);
21918 else
21919 add_AT_int (enum_die, DW_AT_const_value, val);
21921 else
21922 /* Enumeration constants may be wider than HOST_WIDE_INT. Handle
21923 that here. TODO: This should be re-worked to use correct
21924 signed/unsigned double tags for all cases. */
21925 add_AT_wide (enum_die, DW_AT_const_value, wi::to_wide (value));
21928 add_gnat_descriptive_type_attribute (type_die, type, context_die);
21929 if (TYPE_ARTIFICIAL (type))
21930 add_AT_flag (type_die, DW_AT_artificial, 1);
21932 else
21933 add_AT_flag (type_die, DW_AT_declaration, 1);
21935 add_pubtype (type, type_die);
21937 return type_die;
21940 /* Generate a DIE to represent either a real live formal parameter decl or to
21941 represent just the type of some formal parameter position in some function
21942 type.
21944 Note that this routine is a bit unusual because its argument may be a
21945 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
21946 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
21947 node. If it's the former then this function is being called to output a
21948 DIE to represent a formal parameter object (or some inlining thereof). If
21949 it's the latter, then this function is only being called to output a
21950 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
21951 argument type of some subprogram type.
21952 If EMIT_NAME_P is true, name and source coordinate attributes
21953 are emitted. */
21955 static dw_die_ref
21956 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
21957 dw_die_ref context_die)
21959 tree node_or_origin = node ? node : origin;
21960 tree ultimate_origin;
21961 dw_die_ref parm_die = NULL;
21963 if (DECL_P (node_or_origin))
21965 parm_die = lookup_decl_die (node);
21967 /* If the contexts differ, we may not be talking about the same
21968 thing.
21969 ??? When in LTO the DIE parent is the "abstract" copy and the
21970 context_die is the specification "copy". But this whole block
21971 should eventually be no longer needed. */
21972 if (parm_die && parm_die->die_parent != context_die && !in_lto_p)
21974 if (!DECL_ABSTRACT_P (node))
21976 /* This can happen when creating an inlined instance, in
21977 which case we need to create a new DIE that will get
21978 annotated with DW_AT_abstract_origin. */
21979 parm_die = NULL;
21981 else
21982 gcc_unreachable ();
21985 if (parm_die && parm_die->die_parent == NULL)
21987 /* Check that parm_die already has the right attributes that
21988 we would have added below. If any attributes are
21989 missing, fall through to add them. */
21990 if (! DECL_ABSTRACT_P (node_or_origin)
21991 && !get_AT (parm_die, DW_AT_location)
21992 && !get_AT (parm_die, DW_AT_const_value))
21993 /* We are missing location info, and are about to add it. */
21995 else
21997 add_child_die (context_die, parm_die);
21998 return parm_die;
22003 /* If we have a previously generated DIE, use it, unless this is an
22004 concrete instance (origin != NULL), in which case we need a new
22005 DIE with a corresponding DW_AT_abstract_origin. */
22006 bool reusing_die;
22007 if (parm_die && origin == NULL)
22008 reusing_die = true;
22009 else
22011 parm_die = new_die (DW_TAG_formal_parameter, context_die, node);
22012 reusing_die = false;
22015 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
22017 case tcc_declaration:
22018 ultimate_origin = decl_ultimate_origin (node_or_origin);
22019 if (node || ultimate_origin)
22020 origin = ultimate_origin;
22022 if (reusing_die)
22023 goto add_location;
22025 if (origin != NULL)
22026 add_abstract_origin_attribute (parm_die, origin);
22027 else if (emit_name_p)
22028 add_name_and_src_coords_attributes (parm_die, node);
22029 if (origin == NULL
22030 || (! DECL_ABSTRACT_P (node_or_origin)
22031 && variably_modified_type_p (TREE_TYPE (node_or_origin),
22032 decl_function_context
22033 (node_or_origin))))
22035 tree type = TREE_TYPE (node_or_origin);
22036 if (decl_by_reference_p (node_or_origin))
22037 add_type_attribute (parm_die, TREE_TYPE (type),
22038 TYPE_UNQUALIFIED,
22039 false, context_die);
22040 else
22041 add_type_attribute (parm_die, type,
22042 decl_quals (node_or_origin),
22043 false, context_die);
22045 if (origin == NULL && DECL_ARTIFICIAL (node))
22046 add_AT_flag (parm_die, DW_AT_artificial, 1);
22047 add_location:
22048 if (node && node != origin)
22049 equate_decl_number_to_die (node, parm_die);
22050 if (! DECL_ABSTRACT_P (node_or_origin))
22051 add_location_or_const_value_attribute (parm_die, node_or_origin,
22052 node == NULL);
22054 break;
22056 case tcc_type:
22057 /* We were called with some kind of a ..._TYPE node. */
22058 add_type_attribute (parm_die, node_or_origin, TYPE_UNQUALIFIED, false,
22059 context_die);
22060 break;
22062 default:
22063 gcc_unreachable ();
22066 return parm_die;
22069 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
22070 children DW_TAG_formal_parameter DIEs representing the arguments of the
22071 parameter pack.
22073 PARM_PACK must be a function parameter pack.
22074 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
22075 must point to the subsequent arguments of the function PACK_ARG belongs to.
22076 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
22077 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
22078 following the last one for which a DIE was generated. */
22080 static dw_die_ref
22081 gen_formal_parameter_pack_die (tree parm_pack,
22082 tree pack_arg,
22083 dw_die_ref subr_die,
22084 tree *next_arg)
22086 tree arg;
22087 dw_die_ref parm_pack_die;
22089 gcc_assert (parm_pack
22090 && lang_hooks.function_parameter_pack_p (parm_pack)
22091 && subr_die);
22093 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
22094 add_src_coords_attributes (parm_pack_die, parm_pack);
22096 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
22098 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
22099 parm_pack))
22100 break;
22101 gen_formal_parameter_die (arg, NULL,
22102 false /* Don't emit name attribute. */,
22103 parm_pack_die);
22105 if (next_arg)
22106 *next_arg = arg;
22107 return parm_pack_die;
22110 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
22111 at the end of an (ANSI prototyped) formal parameters list. */
22113 static void
22114 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
22116 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
22119 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
22120 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
22121 parameters as specified in some function type specification (except for
22122 those which appear as part of a function *definition*). */
22124 static void
22125 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
22127 tree link;
22128 tree formal_type = NULL;
22129 tree first_parm_type;
22130 tree arg;
22132 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
22134 arg = DECL_ARGUMENTS (function_or_method_type);
22135 function_or_method_type = TREE_TYPE (function_or_method_type);
22137 else
22138 arg = NULL_TREE;
22140 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
22142 /* Make our first pass over the list of formal parameter types and output a
22143 DW_TAG_formal_parameter DIE for each one. */
22144 for (link = first_parm_type; link; )
22146 dw_die_ref parm_die;
22148 formal_type = TREE_VALUE (link);
22149 if (formal_type == void_type_node)
22150 break;
22152 /* Output a (nameless) DIE to represent the formal parameter itself. */
22153 if (!POINTER_BOUNDS_TYPE_P (formal_type))
22155 parm_die = gen_formal_parameter_die (formal_type, NULL,
22156 true /* Emit name attribute. */,
22157 context_die);
22158 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
22159 && link == first_parm_type)
22161 add_AT_flag (parm_die, DW_AT_artificial, 1);
22162 if (dwarf_version >= 3 || !dwarf_strict)
22163 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
22165 else if (arg && DECL_ARTIFICIAL (arg))
22166 add_AT_flag (parm_die, DW_AT_artificial, 1);
22169 link = TREE_CHAIN (link);
22170 if (arg)
22171 arg = DECL_CHAIN (arg);
22174 /* If this function type has an ellipsis, add a
22175 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
22176 if (formal_type != void_type_node)
22177 gen_unspecified_parameters_die (function_or_method_type, context_die);
22179 /* Make our second (and final) pass over the list of formal parameter types
22180 and output DIEs to represent those types (as necessary). */
22181 for (link = TYPE_ARG_TYPES (function_or_method_type);
22182 link && TREE_VALUE (link);
22183 link = TREE_CHAIN (link))
22184 gen_type_die (TREE_VALUE (link), context_die);
22187 /* We want to generate the DIE for TYPE so that we can generate the
22188 die for MEMBER, which has been defined; we will need to refer back
22189 to the member declaration nested within TYPE. If we're trying to
22190 generate minimal debug info for TYPE, processing TYPE won't do the
22191 trick; we need to attach the member declaration by hand. */
22193 static void
22194 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
22196 gen_type_die (type, context_die);
22198 /* If we're trying to avoid duplicate debug info, we may not have
22199 emitted the member decl for this function. Emit it now. */
22200 if (TYPE_STUB_DECL (type)
22201 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
22202 && ! lookup_decl_die (member))
22204 dw_die_ref type_die;
22205 gcc_assert (!decl_ultimate_origin (member));
22207 push_decl_scope (type);
22208 type_die = lookup_type_die_strip_naming_typedef (type);
22209 if (TREE_CODE (member) == FUNCTION_DECL)
22210 gen_subprogram_die (member, type_die);
22211 else if (TREE_CODE (member) == FIELD_DECL)
22213 /* Ignore the nameless fields that are used to skip bits but handle
22214 C++ anonymous unions and structs. */
22215 if (DECL_NAME (member) != NULL_TREE
22216 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
22217 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
22219 struct vlr_context vlr_ctx = {
22220 DECL_CONTEXT (member), /* struct_type */
22221 NULL_TREE /* variant_part_offset */
22223 gen_type_die (member_declared_type (member), type_die);
22224 gen_field_die (member, &vlr_ctx, type_die);
22227 else
22228 gen_variable_die (member, NULL_TREE, type_die);
22230 pop_decl_scope ();
22234 /* Forward declare these functions, because they are mutually recursive
22235 with their set_block_* pairing functions. */
22236 static void set_decl_origin_self (tree);
22238 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
22239 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
22240 that it points to the node itself, thus indicating that the node is its
22241 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
22242 the given node is NULL, recursively descend the decl/block tree which
22243 it is the root of, and for each other ..._DECL or BLOCK node contained
22244 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
22245 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
22246 values to point to themselves. */
22248 static void
22249 set_block_origin_self (tree stmt)
22251 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
22253 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
22256 tree local_decl;
22258 for (local_decl = BLOCK_VARS (stmt);
22259 local_decl != NULL_TREE;
22260 local_decl = DECL_CHAIN (local_decl))
22261 /* Do not recurse on nested functions since the inlining status
22262 of parent and child can be different as per the DWARF spec. */
22263 if (TREE_CODE (local_decl) != FUNCTION_DECL
22264 && !DECL_EXTERNAL (local_decl))
22265 set_decl_origin_self (local_decl);
22269 tree subblock;
22271 for (subblock = BLOCK_SUBBLOCKS (stmt);
22272 subblock != NULL_TREE;
22273 subblock = BLOCK_CHAIN (subblock))
22274 set_block_origin_self (subblock); /* Recurse. */
22279 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
22280 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
22281 node to so that it points to the node itself, thus indicating that the
22282 node represents its own (abstract) origin. Additionally, if the
22283 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
22284 the decl/block tree of which the given node is the root of, and for
22285 each other ..._DECL or BLOCK node contained therein whose
22286 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
22287 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
22288 point to themselves. */
22290 static void
22291 set_decl_origin_self (tree decl)
22293 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
22295 DECL_ABSTRACT_ORIGIN (decl) = decl;
22296 if (TREE_CODE (decl) == FUNCTION_DECL)
22298 tree arg;
22300 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
22301 DECL_ABSTRACT_ORIGIN (arg) = arg;
22302 if (DECL_INITIAL (decl) != NULL_TREE
22303 && DECL_INITIAL (decl) != error_mark_node)
22304 set_block_origin_self (DECL_INITIAL (decl));
22309 /* Mark the early DIE for DECL as the abstract instance. */
22311 static void
22312 dwarf2out_abstract_function (tree decl)
22314 dw_die_ref old_die;
22316 /* Make sure we have the actual abstract inline, not a clone. */
22317 decl = DECL_ORIGIN (decl);
22319 if (DECL_IGNORED_P (decl))
22320 return;
22322 old_die = lookup_decl_die (decl);
22323 /* With early debug we always have an old DIE unless we are in LTO
22324 and the user did not compile but only link with debug. */
22325 if (in_lto_p && ! old_die)
22326 return;
22327 gcc_assert (old_die != NULL);
22328 if (get_AT (old_die, DW_AT_inline)
22329 || get_AT (old_die, DW_AT_abstract_origin))
22330 /* We've already generated the abstract instance. */
22331 return;
22333 /* Go ahead and put DW_AT_inline on the DIE. */
22334 if (DECL_DECLARED_INLINE_P (decl))
22336 if (cgraph_function_possibly_inlined_p (decl))
22337 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_declared_inlined);
22338 else
22339 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_declared_not_inlined);
22341 else
22343 if (cgraph_function_possibly_inlined_p (decl))
22344 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_inlined);
22345 else
22346 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_not_inlined);
22349 if (DECL_DECLARED_INLINE_P (decl)
22350 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
22351 add_AT_flag (old_die, DW_AT_artificial, 1);
22353 set_decl_origin_self (decl);
22356 /* Helper function of premark_used_types() which gets called through
22357 htab_traverse.
22359 Marks the DIE of a given type in *SLOT as perennial, so it never gets
22360 marked as unused by prune_unused_types. */
22362 bool
22363 premark_used_types_helper (tree const &type, void *)
22365 dw_die_ref die;
22367 die = lookup_type_die (type);
22368 if (die != NULL)
22369 die->die_perennial_p = 1;
22370 return true;
22373 /* Helper function of premark_types_used_by_global_vars which gets called
22374 through htab_traverse.
22376 Marks the DIE of a given type in *SLOT as perennial, so it never gets
22377 marked as unused by prune_unused_types. The DIE of the type is marked
22378 only if the global variable using the type will actually be emitted. */
22381 premark_types_used_by_global_vars_helper (types_used_by_vars_entry **slot,
22382 void *)
22384 struct types_used_by_vars_entry *entry;
22385 dw_die_ref die;
22387 entry = (struct types_used_by_vars_entry *) *slot;
22388 gcc_assert (entry->type != NULL
22389 && entry->var_decl != NULL);
22390 die = lookup_type_die (entry->type);
22391 if (die)
22393 /* Ask cgraph if the global variable really is to be emitted.
22394 If yes, then we'll keep the DIE of ENTRY->TYPE. */
22395 varpool_node *node = varpool_node::get (entry->var_decl);
22396 if (node && node->definition)
22398 die->die_perennial_p = 1;
22399 /* Keep the parent DIEs as well. */
22400 while ((die = die->die_parent) && die->die_perennial_p == 0)
22401 die->die_perennial_p = 1;
22404 return 1;
22407 /* Mark all members of used_types_hash as perennial. */
22409 static void
22410 premark_used_types (struct function *fun)
22412 if (fun && fun->used_types_hash)
22413 fun->used_types_hash->traverse<void *, premark_used_types_helper> (NULL);
22416 /* Mark all members of types_used_by_vars_entry as perennial. */
22418 static void
22419 premark_types_used_by_global_vars (void)
22421 if (types_used_by_vars_hash)
22422 types_used_by_vars_hash
22423 ->traverse<void *, premark_types_used_by_global_vars_helper> (NULL);
22426 /* Generate a DW_TAG_call_site DIE in function DECL under SUBR_DIE
22427 for CA_LOC call arg loc node. */
22429 static dw_die_ref
22430 gen_call_site_die (tree decl, dw_die_ref subr_die,
22431 struct call_arg_loc_node *ca_loc)
22433 dw_die_ref stmt_die = NULL, die;
22434 tree block = ca_loc->block;
22436 while (block
22437 && block != DECL_INITIAL (decl)
22438 && TREE_CODE (block) == BLOCK)
22440 stmt_die = BLOCK_DIE (block);
22441 if (stmt_die)
22442 break;
22443 block = BLOCK_SUPERCONTEXT (block);
22445 if (stmt_die == NULL)
22446 stmt_die = subr_die;
22447 die = new_die (dwarf_TAG (DW_TAG_call_site), stmt_die, NULL_TREE);
22448 add_AT_lbl_id (die, dwarf_AT (DW_AT_call_return_pc), ca_loc->label);
22449 if (ca_loc->tail_call_p)
22450 add_AT_flag (die, dwarf_AT (DW_AT_call_tail_call), 1);
22451 if (ca_loc->symbol_ref)
22453 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
22454 if (tdie)
22455 add_AT_die_ref (die, dwarf_AT (DW_AT_call_origin), tdie);
22456 else
22457 add_AT_addr (die, dwarf_AT (DW_AT_call_origin), ca_loc->symbol_ref,
22458 false);
22460 return die;
22463 /* Generate a DIE to represent a declared function (either file-scope or
22464 block-local). */
22466 static void
22467 gen_subprogram_die (tree decl, dw_die_ref context_die)
22469 tree origin = decl_ultimate_origin (decl);
22470 dw_die_ref subr_die;
22471 dw_die_ref old_die = lookup_decl_die (decl);
22473 /* This function gets called multiple times for different stages of
22474 the debug process. For example, for func() in this code:
22476 namespace S
22478 void func() { ... }
22481 ...we get called 4 times. Twice in early debug and twice in
22482 late debug:
22484 Early debug
22485 -----------
22487 1. Once while generating func() within the namespace. This is
22488 the declaration. The declaration bit below is set, as the
22489 context is the namespace.
22491 A new DIE will be generated with DW_AT_declaration set.
22493 2. Once for func() itself. This is the specification. The
22494 declaration bit below is clear as the context is the CU.
22496 We will use the cached DIE from (1) to create a new DIE with
22497 DW_AT_specification pointing to the declaration in (1).
22499 Late debug via rest_of_handle_final()
22500 -------------------------------------
22502 3. Once generating func() within the namespace. This is also the
22503 declaration, as in (1), but this time we will early exit below
22504 as we have a cached DIE and a declaration needs no additional
22505 annotations (no locations), as the source declaration line
22506 info is enough.
22508 4. Once for func() itself. As in (2), this is the specification,
22509 but this time we will re-use the cached DIE, and just annotate
22510 it with the location information that should now be available.
22512 For something without namespaces, but with abstract instances, we
22513 are also called a multiple times:
22515 class Base
22517 public:
22518 Base (); // constructor declaration (1)
22521 Base::Base () { } // constructor specification (2)
22523 Early debug
22524 -----------
22526 1. Once for the Base() constructor by virtue of it being a
22527 member of the Base class. This is done via
22528 rest_of_type_compilation.
22530 This is a declaration, so a new DIE will be created with
22531 DW_AT_declaration.
22533 2. Once for the Base() constructor definition, but this time
22534 while generating the abstract instance of the base
22535 constructor (__base_ctor) which is being generated via early
22536 debug of reachable functions.
22538 Even though we have a cached version of the declaration (1),
22539 we will create a DW_AT_specification of the declaration DIE
22540 in (1).
22542 3. Once for the __base_ctor itself, but this time, we generate
22543 an DW_AT_abstract_origin version of the DW_AT_specification in
22544 (2).
22546 Late debug via rest_of_handle_final
22547 -----------------------------------
22549 4. One final time for the __base_ctor (which will have a cached
22550 DIE with DW_AT_abstract_origin created in (3). This time,
22551 we will just annotate the location information now
22552 available.
22554 int declaration = (current_function_decl != decl
22555 || class_or_namespace_scope_p (context_die));
22557 /* A declaration that has been previously dumped needs no
22558 additional information. */
22559 if (old_die && declaration)
22560 return;
22562 /* Now that the C++ front end lazily declares artificial member fns, we
22563 might need to retrofit the declaration into its class. */
22564 if (!declaration && !origin && !old_die
22565 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
22566 && !class_or_namespace_scope_p (context_die)
22567 && debug_info_level > DINFO_LEVEL_TERSE)
22568 old_die = force_decl_die (decl);
22570 /* A concrete instance, tag a new DIE with DW_AT_abstract_origin. */
22571 if (origin != NULL)
22573 gcc_assert (!declaration || local_scope_p (context_die));
22575 /* Fixup die_parent for the abstract instance of a nested
22576 inline function. */
22577 if (old_die && old_die->die_parent == NULL)
22578 add_child_die (context_die, old_die);
22580 if (old_die && get_AT_ref (old_die, DW_AT_abstract_origin))
22582 /* If we have a DW_AT_abstract_origin we have a working
22583 cached version. */
22584 subr_die = old_die;
22586 else
22588 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
22589 add_abstract_origin_attribute (subr_die, origin);
22590 /* This is where the actual code for a cloned function is.
22591 Let's emit linkage name attribute for it. This helps
22592 debuggers to e.g, set breakpoints into
22593 constructors/destructors when the user asks "break
22594 K::K". */
22595 add_linkage_name (subr_die, decl);
22598 /* A cached copy, possibly from early dwarf generation. Reuse as
22599 much as possible. */
22600 else if (old_die)
22602 if (!get_AT_flag (old_die, DW_AT_declaration)
22603 /* We can have a normal definition following an inline one in the
22604 case of redefinition of GNU C extern inlines.
22605 It seems reasonable to use AT_specification in this case. */
22606 && !get_AT (old_die, DW_AT_inline))
22608 /* Detect and ignore this case, where we are trying to output
22609 something we have already output. */
22610 if (get_AT (old_die, DW_AT_low_pc)
22611 || get_AT (old_die, DW_AT_ranges))
22612 return;
22614 /* If we have no location information, this must be a
22615 partially generated DIE from early dwarf generation.
22616 Fall through and generate it. */
22619 /* If the definition comes from the same place as the declaration,
22620 maybe use the old DIE. We always want the DIE for this function
22621 that has the *_pc attributes to be under comp_unit_die so the
22622 debugger can find it. We also need to do this for abstract
22623 instances of inlines, since the spec requires the out-of-line copy
22624 to have the same parent. For local class methods, this doesn't
22625 apply; we just use the old DIE. */
22626 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
22627 struct dwarf_file_data * file_index = lookup_filename (s.file);
22628 if ((is_cu_die (old_die->die_parent)
22629 /* This condition fixes the inconsistency/ICE with the
22630 following Fortran test (or some derivative thereof) while
22631 building libgfortran:
22633 module some_m
22634 contains
22635 logical function funky (FLAG)
22636 funky = .true.
22637 end function
22638 end module
22640 || (old_die->die_parent
22641 && old_die->die_parent->die_tag == DW_TAG_module)
22642 || context_die == NULL)
22643 && (DECL_ARTIFICIAL (decl)
22644 /* The location attributes may be in the abstract origin
22645 which in the case of LTO might be not available to
22646 look at. */
22647 || get_AT (old_die, DW_AT_abstract_origin)
22648 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
22649 && (get_AT_unsigned (old_die, DW_AT_decl_line)
22650 == (unsigned) s.line)
22651 && (!debug_column_info
22652 || s.column == 0
22653 || (get_AT_unsigned (old_die, DW_AT_decl_column)
22654 == (unsigned) s.column)))))
22656 subr_die = old_die;
22658 /* Clear out the declaration attribute, but leave the
22659 parameters so they can be augmented with location
22660 information later. Unless this was a declaration, in
22661 which case, wipe out the nameless parameters and recreate
22662 them further down. */
22663 if (remove_AT (subr_die, DW_AT_declaration))
22666 remove_AT (subr_die, DW_AT_object_pointer);
22667 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
22670 /* Make a specification pointing to the previously built
22671 declaration. */
22672 else
22674 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
22675 add_AT_specification (subr_die, old_die);
22676 add_pubname (decl, subr_die);
22677 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
22678 add_AT_file (subr_die, DW_AT_decl_file, file_index);
22679 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
22680 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
22681 if (debug_column_info
22682 && s.column
22683 && (get_AT_unsigned (old_die, DW_AT_decl_column)
22684 != (unsigned) s.column))
22685 add_AT_unsigned (subr_die, DW_AT_decl_column, s.column);
22687 /* If the prototype had an 'auto' or 'decltype(auto)' return type,
22688 emit the real type on the definition die. */
22689 if (is_cxx () && debug_info_level > DINFO_LEVEL_TERSE)
22691 dw_die_ref die = get_AT_ref (old_die, DW_AT_type);
22692 if (die == auto_die || die == decltype_auto_die)
22693 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
22694 TYPE_UNQUALIFIED, false, context_die);
22697 /* When we process the method declaration, we haven't seen
22698 the out-of-class defaulted definition yet, so we have to
22699 recheck now. */
22700 if ((dwarf_version >= 5 || ! dwarf_strict)
22701 && !get_AT (subr_die, DW_AT_defaulted))
22703 int defaulted
22704 = lang_hooks.decls.decl_dwarf_attribute (decl,
22705 DW_AT_defaulted);
22706 if (defaulted != -1)
22708 /* Other values must have been handled before. */
22709 gcc_assert (defaulted == DW_DEFAULTED_out_of_class);
22710 add_AT_unsigned (subr_die, DW_AT_defaulted, defaulted);
22715 /* Create a fresh DIE for anything else. */
22716 else
22718 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
22720 if (TREE_PUBLIC (decl))
22721 add_AT_flag (subr_die, DW_AT_external, 1);
22723 add_name_and_src_coords_attributes (subr_die, decl);
22724 add_pubname (decl, subr_die);
22725 if (debug_info_level > DINFO_LEVEL_TERSE)
22727 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
22728 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
22729 TYPE_UNQUALIFIED, false, context_die);
22732 add_pure_or_virtual_attribute (subr_die, decl);
22733 if (DECL_ARTIFICIAL (decl))
22734 add_AT_flag (subr_die, DW_AT_artificial, 1);
22736 if (TREE_THIS_VOLATILE (decl) && (dwarf_version >= 5 || !dwarf_strict))
22737 add_AT_flag (subr_die, DW_AT_noreturn, 1);
22739 add_alignment_attribute (subr_die, decl);
22741 add_accessibility_attribute (subr_die, decl);
22744 /* Unless we have an existing non-declaration DIE, equate the new
22745 DIE. */
22746 if (!old_die || is_declaration_die (old_die))
22747 equate_decl_number_to_die (decl, subr_die);
22749 if (declaration)
22751 if (!old_die || !get_AT (old_die, DW_AT_inline))
22753 add_AT_flag (subr_die, DW_AT_declaration, 1);
22755 /* If this is an explicit function declaration then generate
22756 a DW_AT_explicit attribute. */
22757 if ((dwarf_version >= 3 || !dwarf_strict)
22758 && lang_hooks.decls.decl_dwarf_attribute (decl,
22759 DW_AT_explicit) == 1)
22760 add_AT_flag (subr_die, DW_AT_explicit, 1);
22762 /* If this is a C++11 deleted special function member then generate
22763 a DW_AT_deleted attribute. */
22764 if ((dwarf_version >= 5 || !dwarf_strict)
22765 && lang_hooks.decls.decl_dwarf_attribute (decl,
22766 DW_AT_deleted) == 1)
22767 add_AT_flag (subr_die, DW_AT_deleted, 1);
22769 /* If this is a C++11 defaulted special function member then
22770 generate a DW_AT_defaulted attribute. */
22771 if (dwarf_version >= 5 || !dwarf_strict)
22773 int defaulted
22774 = lang_hooks.decls.decl_dwarf_attribute (decl,
22775 DW_AT_defaulted);
22776 if (defaulted != -1)
22777 add_AT_unsigned (subr_die, DW_AT_defaulted, defaulted);
22780 /* If this is a C++11 non-static member function with & ref-qualifier
22781 then generate a DW_AT_reference attribute. */
22782 if ((dwarf_version >= 5 || !dwarf_strict)
22783 && lang_hooks.decls.decl_dwarf_attribute (decl,
22784 DW_AT_reference) == 1)
22785 add_AT_flag (subr_die, DW_AT_reference, 1);
22787 /* If this is a C++11 non-static member function with &&
22788 ref-qualifier then generate a DW_AT_reference attribute. */
22789 if ((dwarf_version >= 5 || !dwarf_strict)
22790 && lang_hooks.decls.decl_dwarf_attribute (decl,
22791 DW_AT_rvalue_reference)
22792 == 1)
22793 add_AT_flag (subr_die, DW_AT_rvalue_reference, 1);
22796 /* For non DECL_EXTERNALs, if range information is available, fill
22797 the DIE with it. */
22798 else if (!DECL_EXTERNAL (decl) && !early_dwarf)
22800 HOST_WIDE_INT cfa_fb_offset;
22802 struct function *fun = DECL_STRUCT_FUNCTION (decl);
22804 if (!crtl->has_bb_partition)
22806 dw_fde_ref fde = fun->fde;
22807 if (fde->dw_fde_begin)
22809 /* We have already generated the labels. */
22810 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
22811 fde->dw_fde_end, false);
22813 else
22815 /* Create start/end labels and add the range. */
22816 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
22817 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
22818 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
22819 current_function_funcdef_no);
22820 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
22821 current_function_funcdef_no);
22822 add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
22823 false);
22826 #if VMS_DEBUGGING_INFO
22827 /* HP OpenVMS Industry Standard 64: DWARF Extensions
22828 Section 2.3 Prologue and Epilogue Attributes:
22829 When a breakpoint is set on entry to a function, it is generally
22830 desirable for execution to be suspended, not on the very first
22831 instruction of the function, but rather at a point after the
22832 function's frame has been set up, after any language defined local
22833 declaration processing has been completed, and before execution of
22834 the first statement of the function begins. Debuggers generally
22835 cannot properly determine where this point is. Similarly for a
22836 breakpoint set on exit from a function. The prologue and epilogue
22837 attributes allow a compiler to communicate the location(s) to use. */
22840 if (fde->dw_fde_vms_end_prologue)
22841 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
22842 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
22844 if (fde->dw_fde_vms_begin_epilogue)
22845 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
22846 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
22848 #endif
22851 else
22853 /* Generate pubnames entries for the split function code ranges. */
22854 dw_fde_ref fde = fun->fde;
22856 if (fde->dw_fde_second_begin)
22858 if (dwarf_version >= 3 || !dwarf_strict)
22860 /* We should use ranges for non-contiguous code section
22861 addresses. Use the actual code range for the initial
22862 section, since the HOT/COLD labels might precede an
22863 alignment offset. */
22864 bool range_list_added = false;
22865 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
22866 fde->dw_fde_end, &range_list_added,
22867 false);
22868 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
22869 fde->dw_fde_second_end,
22870 &range_list_added, false);
22871 if (range_list_added)
22872 add_ranges (NULL);
22874 else
22876 /* There is no real support in DW2 for this .. so we make
22877 a work-around. First, emit the pub name for the segment
22878 containing the function label. Then make and emit a
22879 simplified subprogram DIE for the second segment with the
22880 name pre-fixed by __hot/cold_sect_of_. We use the same
22881 linkage name for the second die so that gdb will find both
22882 sections when given "b foo". */
22883 const char *name = NULL;
22884 tree decl_name = DECL_NAME (decl);
22885 dw_die_ref seg_die;
22887 /* Do the 'primary' section. */
22888 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
22889 fde->dw_fde_end, false);
22891 /* Build a minimal DIE for the secondary section. */
22892 seg_die = new_die (DW_TAG_subprogram,
22893 subr_die->die_parent, decl);
22895 if (TREE_PUBLIC (decl))
22896 add_AT_flag (seg_die, DW_AT_external, 1);
22898 if (decl_name != NULL
22899 && IDENTIFIER_POINTER (decl_name) != NULL)
22901 name = dwarf2_name (decl, 1);
22902 if (! DECL_ARTIFICIAL (decl))
22903 add_src_coords_attributes (seg_die, decl);
22905 add_linkage_name (seg_die, decl);
22907 gcc_assert (name != NULL);
22908 add_pure_or_virtual_attribute (seg_die, decl);
22909 if (DECL_ARTIFICIAL (decl))
22910 add_AT_flag (seg_die, DW_AT_artificial, 1);
22912 name = concat ("__second_sect_of_", name, NULL);
22913 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
22914 fde->dw_fde_second_end, false);
22915 add_name_attribute (seg_die, name);
22916 if (want_pubnames ())
22917 add_pubname_string (name, seg_die);
22920 else
22921 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
22922 false);
22925 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
22927 /* We define the "frame base" as the function's CFA. This is more
22928 convenient for several reasons: (1) It's stable across the prologue
22929 and epilogue, which makes it better than just a frame pointer,
22930 (2) With dwarf3, there exists a one-byte encoding that allows us
22931 to reference the .debug_frame data by proxy, but failing that,
22932 (3) We can at least reuse the code inspection and interpretation
22933 code that determines the CFA position at various points in the
22934 function. */
22935 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
22937 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
22938 add_AT_loc (subr_die, DW_AT_frame_base, op);
22940 else
22942 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
22943 if (list->dw_loc_next)
22944 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
22945 else
22946 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
22949 /* Compute a displacement from the "steady-state frame pointer" to
22950 the CFA. The former is what all stack slots and argument slots
22951 will reference in the rtl; the latter is what we've told the
22952 debugger about. We'll need to adjust all frame_base references
22953 by this displacement. */
22954 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
22956 if (fun->static_chain_decl)
22958 /* DWARF requires here a location expression that computes the
22959 address of the enclosing subprogram's frame base. The machinery
22960 in tree-nested.c is supposed to store this specific address in the
22961 last field of the FRAME record. */
22962 const tree frame_type
22963 = TREE_TYPE (TREE_TYPE (fun->static_chain_decl));
22964 const tree fb_decl = tree_last (TYPE_FIELDS (frame_type));
22966 tree fb_expr
22967 = build1 (INDIRECT_REF, frame_type, fun->static_chain_decl);
22968 fb_expr = build3 (COMPONENT_REF, TREE_TYPE (fb_decl),
22969 fb_expr, fb_decl, NULL_TREE);
22971 add_AT_location_description (subr_die, DW_AT_static_link,
22972 loc_list_from_tree (fb_expr, 0, NULL));
22975 resolve_variable_values ();
22978 /* Generate child dies for template paramaters. */
22979 if (early_dwarf && debug_info_level > DINFO_LEVEL_TERSE)
22980 gen_generic_params_dies (decl);
22982 /* Now output descriptions of the arguments for this function. This gets
22983 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
22984 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
22985 `...' at the end of the formal parameter list. In order to find out if
22986 there was a trailing ellipsis or not, we must instead look at the type
22987 associated with the FUNCTION_DECL. This will be a node of type
22988 FUNCTION_TYPE. If the chain of type nodes hanging off of this
22989 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
22990 an ellipsis at the end. */
22992 /* In the case where we are describing a mere function declaration, all we
22993 need to do here (and all we *can* do here) is to describe the *types* of
22994 its formal parameters. */
22995 if (debug_info_level <= DINFO_LEVEL_TERSE)
22997 else if (declaration)
22998 gen_formal_types_die (decl, subr_die);
22999 else
23001 /* Generate DIEs to represent all known formal parameters. */
23002 tree parm = DECL_ARGUMENTS (decl);
23003 tree generic_decl = early_dwarf
23004 ? lang_hooks.decls.get_generic_function_decl (decl) : NULL;
23005 tree generic_decl_parm = generic_decl
23006 ? DECL_ARGUMENTS (generic_decl)
23007 : NULL;
23009 /* Now we want to walk the list of parameters of the function and
23010 emit their relevant DIEs.
23012 We consider the case of DECL being an instance of a generic function
23013 as well as it being a normal function.
23015 If DECL is an instance of a generic function we walk the
23016 parameters of the generic function declaration _and_ the parameters of
23017 DECL itself. This is useful because we want to emit specific DIEs for
23018 function parameter packs and those are declared as part of the
23019 generic function declaration. In that particular case,
23020 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
23021 That DIE has children DIEs representing the set of arguments
23022 of the pack. Note that the set of pack arguments can be empty.
23023 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
23024 children DIE.
23026 Otherwise, we just consider the parameters of DECL. */
23027 while (generic_decl_parm || parm)
23029 if (generic_decl_parm
23030 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
23031 gen_formal_parameter_pack_die (generic_decl_parm,
23032 parm, subr_die,
23033 &parm);
23034 else if (parm && !POINTER_BOUNDS_P (parm))
23036 dw_die_ref parm_die = gen_decl_die (parm, NULL, NULL, subr_die);
23038 if (early_dwarf
23039 && parm == DECL_ARGUMENTS (decl)
23040 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
23041 && parm_die
23042 && (dwarf_version >= 3 || !dwarf_strict))
23043 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
23045 parm = DECL_CHAIN (parm);
23047 else if (parm)
23048 parm = DECL_CHAIN (parm);
23050 if (generic_decl_parm)
23051 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
23054 /* Decide whether we need an unspecified_parameters DIE at the end.
23055 There are 2 more cases to do this for: 1) the ansi ... declaration -
23056 this is detectable when the end of the arg list is not a
23057 void_type_node 2) an unprototyped function declaration (not a
23058 definition). This just means that we have no info about the
23059 parameters at all. */
23060 if (early_dwarf)
23062 if (prototype_p (TREE_TYPE (decl)))
23064 /* This is the prototyped case, check for.... */
23065 if (stdarg_p (TREE_TYPE (decl)))
23066 gen_unspecified_parameters_die (decl, subr_die);
23068 else if (DECL_INITIAL (decl) == NULL_TREE)
23069 gen_unspecified_parameters_die (decl, subr_die);
23073 if (subr_die != old_die)
23074 /* Add the calling convention attribute if requested. */
23075 add_calling_convention_attribute (subr_die, decl);
23077 /* Output Dwarf info for all of the stuff within the body of the function
23078 (if it has one - it may be just a declaration).
23080 OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
23081 a function. This BLOCK actually represents the outermost binding contour
23082 for the function, i.e. the contour in which the function's formal
23083 parameters and labels get declared. Curiously, it appears that the front
23084 end doesn't actually put the PARM_DECL nodes for the current function onto
23085 the BLOCK_VARS list for this outer scope, but are strung off of the
23086 DECL_ARGUMENTS list for the function instead.
23088 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
23089 the LABEL_DECL nodes for the function however, and we output DWARF info
23090 for those in decls_for_scope. Just within the `outer_scope' there will be
23091 a BLOCK node representing the function's outermost pair of curly braces,
23092 and any blocks used for the base and member initializers of a C++
23093 constructor function. */
23094 tree outer_scope = DECL_INITIAL (decl);
23095 if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
23097 int call_site_note_count = 0;
23098 int tail_call_site_note_count = 0;
23100 /* Emit a DW_TAG_variable DIE for a named return value. */
23101 if (DECL_NAME (DECL_RESULT (decl)))
23102 gen_decl_die (DECL_RESULT (decl), NULL, NULL, subr_die);
23104 /* The first time through decls_for_scope we will generate the
23105 DIEs for the locals. The second time, we fill in the
23106 location info. */
23107 decls_for_scope (outer_scope, subr_die);
23109 if (call_arg_locations && (!dwarf_strict || dwarf_version >= 5))
23111 struct call_arg_loc_node *ca_loc;
23112 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
23114 dw_die_ref die = NULL;
23115 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
23116 rtx arg, next_arg;
23118 for (arg = (ca_loc->call_arg_loc_note != NULL_RTX
23119 ? XEXP (ca_loc->call_arg_loc_note, 0)
23120 : NULL_RTX);
23121 arg; arg = next_arg)
23123 dw_loc_descr_ref reg, val;
23124 machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
23125 dw_die_ref cdie, tdie = NULL;
23127 next_arg = XEXP (arg, 1);
23128 if (REG_P (XEXP (XEXP (arg, 0), 0))
23129 && next_arg
23130 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
23131 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
23132 && REGNO (XEXP (XEXP (arg, 0), 0))
23133 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
23134 next_arg = XEXP (next_arg, 1);
23135 if (mode == VOIDmode)
23137 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
23138 if (mode == VOIDmode)
23139 mode = GET_MODE (XEXP (arg, 0));
23141 if (mode == VOIDmode || mode == BLKmode)
23142 continue;
23143 /* Get dynamic information about call target only if we
23144 have no static information: we cannot generate both
23145 DW_AT_call_origin and DW_AT_call_target
23146 attributes. */
23147 if (ca_loc->symbol_ref == NULL_RTX)
23149 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
23151 tloc = XEXP (XEXP (arg, 0), 1);
23152 continue;
23154 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
23155 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
23157 tlocc = XEXP (XEXP (arg, 0), 1);
23158 continue;
23161 reg = NULL;
23162 if (REG_P (XEXP (XEXP (arg, 0), 0)))
23163 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
23164 VAR_INIT_STATUS_INITIALIZED);
23165 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
23167 rtx mem = XEXP (XEXP (arg, 0), 0);
23168 reg = mem_loc_descriptor (XEXP (mem, 0),
23169 get_address_mode (mem),
23170 GET_MODE (mem),
23171 VAR_INIT_STATUS_INITIALIZED);
23173 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
23174 == DEBUG_PARAMETER_REF)
23176 tree tdecl
23177 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
23178 tdie = lookup_decl_die (tdecl);
23179 if (tdie == NULL)
23180 continue;
23182 else
23183 continue;
23184 if (reg == NULL
23185 && GET_CODE (XEXP (XEXP (arg, 0), 0))
23186 != DEBUG_PARAMETER_REF)
23187 continue;
23188 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
23189 VOIDmode,
23190 VAR_INIT_STATUS_INITIALIZED);
23191 if (val == NULL)
23192 continue;
23193 if (die == NULL)
23194 die = gen_call_site_die (decl, subr_die, ca_loc);
23195 cdie = new_die (dwarf_TAG (DW_TAG_call_site_parameter), die,
23196 NULL_TREE);
23197 if (reg != NULL)
23198 add_AT_loc (cdie, DW_AT_location, reg);
23199 else if (tdie != NULL)
23200 add_AT_die_ref (cdie, dwarf_AT (DW_AT_call_parameter),
23201 tdie);
23202 add_AT_loc (cdie, dwarf_AT (DW_AT_call_value), val);
23203 if (next_arg != XEXP (arg, 1))
23205 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
23206 if (mode == VOIDmode)
23207 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
23208 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
23209 0), 1),
23210 mode, VOIDmode,
23211 VAR_INIT_STATUS_INITIALIZED);
23212 if (val != NULL)
23213 add_AT_loc (cdie, dwarf_AT (DW_AT_call_data_value),
23214 val);
23217 if (die == NULL
23218 && (ca_loc->symbol_ref || tloc))
23219 die = gen_call_site_die (decl, subr_die, ca_loc);
23220 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
23222 dw_loc_descr_ref tval = NULL;
23224 if (tloc != NULL_RTX)
23225 tval = mem_loc_descriptor (tloc,
23226 GET_MODE (tloc) == VOIDmode
23227 ? Pmode : GET_MODE (tloc),
23228 VOIDmode,
23229 VAR_INIT_STATUS_INITIALIZED);
23230 if (tval)
23231 add_AT_loc (die, dwarf_AT (DW_AT_call_target), tval);
23232 else if (tlocc != NULL_RTX)
23234 tval = mem_loc_descriptor (tlocc,
23235 GET_MODE (tlocc) == VOIDmode
23236 ? Pmode : GET_MODE (tlocc),
23237 VOIDmode,
23238 VAR_INIT_STATUS_INITIALIZED);
23239 if (tval)
23240 add_AT_loc (die,
23241 dwarf_AT (DW_AT_call_target_clobbered),
23242 tval);
23245 if (die != NULL)
23247 call_site_note_count++;
23248 if (ca_loc->tail_call_p)
23249 tail_call_site_note_count++;
23253 call_arg_locations = NULL;
23254 call_arg_loc_last = NULL;
23255 if (tail_call_site_count >= 0
23256 && tail_call_site_count == tail_call_site_note_count
23257 && (!dwarf_strict || dwarf_version >= 5))
23259 if (call_site_count >= 0
23260 && call_site_count == call_site_note_count)
23261 add_AT_flag (subr_die, dwarf_AT (DW_AT_call_all_calls), 1);
23262 else
23263 add_AT_flag (subr_die, dwarf_AT (DW_AT_call_all_tail_calls), 1);
23265 call_site_count = -1;
23266 tail_call_site_count = -1;
23269 /* Mark used types after we have created DIEs for the functions scopes. */
23270 premark_used_types (DECL_STRUCT_FUNCTION (decl));
23273 /* Returns a hash value for X (which really is a die_struct). */
23275 hashval_t
23276 block_die_hasher::hash (die_struct *d)
23278 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
23281 /* Return nonzero if decl_id and die_parent of die_struct X is the same
23282 as decl_id and die_parent of die_struct Y. */
23284 bool
23285 block_die_hasher::equal (die_struct *x, die_struct *y)
23287 return x->decl_id == y->decl_id && x->die_parent == y->die_parent;
23290 /* Hold information about markers for inlined entry points. */
23291 struct GTY ((for_user)) inline_entry_data
23293 /* The block that's the inlined_function_outer_scope for an inlined
23294 function. */
23295 tree block;
23297 /* The label at the inlined entry point. */
23298 const char *label_pfx;
23299 unsigned int label_num;
23301 /* The view number to be used as the inlined entry point. */
23302 var_loc_view view;
23305 struct inline_entry_data_hasher : ggc_ptr_hash <inline_entry_data>
23307 typedef tree compare_type;
23308 static inline hashval_t hash (const inline_entry_data *);
23309 static inline bool equal (const inline_entry_data *, const_tree);
23312 /* Hash table routines for inline_entry_data. */
23314 inline hashval_t
23315 inline_entry_data_hasher::hash (const inline_entry_data *data)
23317 return htab_hash_pointer (data->block);
23320 inline bool
23321 inline_entry_data_hasher::equal (const inline_entry_data *data,
23322 const_tree block)
23324 return data->block == block;
23327 /* Inlined entry points pending DIE creation in this compilation unit. */
23329 static GTY(()) hash_table<inline_entry_data_hasher> *inline_entry_data_table;
23332 /* Return TRUE if DECL, which may have been previously generated as
23333 OLD_DIE, is a candidate for a DW_AT_specification. DECLARATION is
23334 true if decl (or its origin) is either an extern declaration or a
23335 class/namespace scoped declaration.
23337 The declare_in_namespace support causes us to get two DIEs for one
23338 variable, both of which are declarations. We want to avoid
23339 considering one to be a specification, so we must test for
23340 DECLARATION and DW_AT_declaration. */
23341 static inline bool
23342 decl_will_get_specification_p (dw_die_ref old_die, tree decl, bool declaration)
23344 return (old_die && TREE_STATIC (decl) && !declaration
23345 && get_AT_flag (old_die, DW_AT_declaration) == 1);
23348 /* Return true if DECL is a local static. */
23350 static inline bool
23351 local_function_static (tree decl)
23353 gcc_assert (VAR_P (decl));
23354 return TREE_STATIC (decl)
23355 && DECL_CONTEXT (decl)
23356 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL;
23359 /* Generate a DIE to represent a declared data object.
23360 Either DECL or ORIGIN must be non-null. */
23362 static void
23363 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
23365 HOST_WIDE_INT off = 0;
23366 tree com_decl;
23367 tree decl_or_origin = decl ? decl : origin;
23368 tree ultimate_origin;
23369 dw_die_ref var_die;
23370 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
23371 bool declaration = (DECL_EXTERNAL (decl_or_origin)
23372 || class_or_namespace_scope_p (context_die));
23373 bool specialization_p = false;
23374 bool no_linkage_name = false;
23376 /* While C++ inline static data members have definitions inside of the
23377 class, force the first DIE to be a declaration, then let gen_member_die
23378 reparent it to the class context and call gen_variable_die again
23379 to create the outside of the class DIE for the definition. */
23380 if (!declaration
23381 && old_die == NULL
23382 && decl
23383 && DECL_CONTEXT (decl)
23384 && TYPE_P (DECL_CONTEXT (decl))
23385 && lang_hooks.decls.decl_dwarf_attribute (decl, DW_AT_inline) != -1)
23387 declaration = true;
23388 if (dwarf_version < 5)
23389 no_linkage_name = true;
23392 ultimate_origin = decl_ultimate_origin (decl_or_origin);
23393 if (decl || ultimate_origin)
23394 origin = ultimate_origin;
23395 com_decl = fortran_common (decl_or_origin, &off);
23397 /* Symbol in common gets emitted as a child of the common block, in the form
23398 of a data member. */
23399 if (com_decl)
23401 dw_die_ref com_die;
23402 dw_loc_list_ref loc = NULL;
23403 die_node com_die_arg;
23405 var_die = lookup_decl_die (decl_or_origin);
23406 if (var_die)
23408 if (! early_dwarf && get_AT (var_die, DW_AT_location) == NULL)
23410 loc = loc_list_from_tree (com_decl, off ? 1 : 2, NULL);
23411 if (loc)
23413 if (off)
23415 /* Optimize the common case. */
23416 if (single_element_loc_list_p (loc)
23417 && loc->expr->dw_loc_opc == DW_OP_addr
23418 && loc->expr->dw_loc_next == NULL
23419 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
23420 == SYMBOL_REF)
23422 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
23423 loc->expr->dw_loc_oprnd1.v.val_addr
23424 = plus_constant (GET_MODE (x), x , off);
23426 else
23427 loc_list_plus_const (loc, off);
23429 add_AT_location_description (var_die, DW_AT_location, loc);
23430 remove_AT (var_die, DW_AT_declaration);
23433 return;
23436 if (common_block_die_table == NULL)
23437 common_block_die_table = hash_table<block_die_hasher>::create_ggc (10);
23439 com_die_arg.decl_id = DECL_UID (com_decl);
23440 com_die_arg.die_parent = context_die;
23441 com_die = common_block_die_table->find (&com_die_arg);
23442 if (! early_dwarf)
23443 loc = loc_list_from_tree (com_decl, 2, NULL);
23444 if (com_die == NULL)
23446 const char *cnam
23447 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
23448 die_node **slot;
23450 com_die = new_die (DW_TAG_common_block, context_die, decl);
23451 add_name_and_src_coords_attributes (com_die, com_decl);
23452 if (loc)
23454 add_AT_location_description (com_die, DW_AT_location, loc);
23455 /* Avoid sharing the same loc descriptor between
23456 DW_TAG_common_block and DW_TAG_variable. */
23457 loc = loc_list_from_tree (com_decl, 2, NULL);
23459 else if (DECL_EXTERNAL (decl_or_origin))
23460 add_AT_flag (com_die, DW_AT_declaration, 1);
23461 if (want_pubnames ())
23462 add_pubname_string (cnam, com_die); /* ??? needed? */
23463 com_die->decl_id = DECL_UID (com_decl);
23464 slot = common_block_die_table->find_slot (com_die, INSERT);
23465 *slot = com_die;
23467 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
23469 add_AT_location_description (com_die, DW_AT_location, loc);
23470 loc = loc_list_from_tree (com_decl, 2, NULL);
23471 remove_AT (com_die, DW_AT_declaration);
23473 var_die = new_die (DW_TAG_variable, com_die, decl);
23474 add_name_and_src_coords_attributes (var_die, decl_or_origin);
23475 add_type_attribute (var_die, TREE_TYPE (decl_or_origin),
23476 decl_quals (decl_or_origin), false,
23477 context_die);
23478 add_alignment_attribute (var_die, decl);
23479 add_AT_flag (var_die, DW_AT_external, 1);
23480 if (loc)
23482 if (off)
23484 /* Optimize the common case. */
23485 if (single_element_loc_list_p (loc)
23486 && loc->expr->dw_loc_opc == DW_OP_addr
23487 && loc->expr->dw_loc_next == NULL
23488 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
23490 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
23491 loc->expr->dw_loc_oprnd1.v.val_addr
23492 = plus_constant (GET_MODE (x), x, off);
23494 else
23495 loc_list_plus_const (loc, off);
23497 add_AT_location_description (var_die, DW_AT_location, loc);
23499 else if (DECL_EXTERNAL (decl_or_origin))
23500 add_AT_flag (var_die, DW_AT_declaration, 1);
23501 if (decl)
23502 equate_decl_number_to_die (decl, var_die);
23503 return;
23506 if (old_die)
23508 if (declaration)
23510 /* A declaration that has been previously dumped, needs no
23511 further annotations, since it doesn't need location on
23512 the second pass. */
23513 return;
23515 else if (decl_will_get_specification_p (old_die, decl, declaration)
23516 && !get_AT (old_die, DW_AT_specification))
23518 /* Fall-thru so we can make a new variable die along with a
23519 DW_AT_specification. */
23521 else if (origin && old_die->die_parent != context_die)
23523 /* If we will be creating an inlined instance, we need a
23524 new DIE that will get annotated with
23525 DW_AT_abstract_origin. */
23526 gcc_assert (!DECL_ABSTRACT_P (decl));
23528 else
23530 /* If a DIE was dumped early, it still needs location info.
23531 Skip to where we fill the location bits. */
23532 var_die = old_die;
23534 /* ??? In LTRANS we cannot annotate early created variably
23535 modified type DIEs without copying them and adjusting all
23536 references to them. Thus we dumped them again. Also add a
23537 reference to them but beware of -g0 compile and -g link
23538 in which case the reference will be already present. */
23539 tree type = TREE_TYPE (decl_or_origin);
23540 if (in_lto_p
23541 && ! get_AT (var_die, DW_AT_type)
23542 && variably_modified_type_p
23543 (type, decl_function_context (decl_or_origin)))
23545 if (decl_by_reference_p (decl_or_origin))
23546 add_type_attribute (var_die, TREE_TYPE (type),
23547 TYPE_UNQUALIFIED, false, context_die);
23548 else
23549 add_type_attribute (var_die, type, decl_quals (decl_or_origin),
23550 false, context_die);
23553 goto gen_variable_die_location;
23557 /* For static data members, the declaration in the class is supposed
23558 to have DW_TAG_member tag in DWARF{3,4} and we emit it for compatibility
23559 also in DWARF2; the specification should still be DW_TAG_variable
23560 referencing the DW_TAG_member DIE. */
23561 if (declaration && class_scope_p (context_die) && dwarf_version < 5)
23562 var_die = new_die (DW_TAG_member, context_die, decl);
23563 else
23564 var_die = new_die (DW_TAG_variable, context_die, decl);
23566 if (origin != NULL)
23567 add_abstract_origin_attribute (var_die, origin);
23569 /* Loop unrolling can create multiple blocks that refer to the same
23570 static variable, so we must test for the DW_AT_declaration flag.
23572 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
23573 copy decls and set the DECL_ABSTRACT_P flag on them instead of
23574 sharing them.
23576 ??? Duplicated blocks have been rewritten to use .debug_ranges. */
23577 else if (decl_will_get_specification_p (old_die, decl, declaration))
23579 /* This is a definition of a C++ class level static. */
23580 add_AT_specification (var_die, old_die);
23581 specialization_p = true;
23582 if (DECL_NAME (decl))
23584 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
23585 struct dwarf_file_data * file_index = lookup_filename (s.file);
23587 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
23588 add_AT_file (var_die, DW_AT_decl_file, file_index);
23590 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
23591 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
23593 if (debug_column_info
23594 && s.column
23595 && (get_AT_unsigned (old_die, DW_AT_decl_column)
23596 != (unsigned) s.column))
23597 add_AT_unsigned (var_die, DW_AT_decl_column, s.column);
23599 if (old_die->die_tag == DW_TAG_member)
23600 add_linkage_name (var_die, decl);
23603 else
23604 add_name_and_src_coords_attributes (var_die, decl, no_linkage_name);
23606 if ((origin == NULL && !specialization_p)
23607 || (origin != NULL
23608 && !DECL_ABSTRACT_P (decl_or_origin)
23609 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
23610 decl_function_context
23611 (decl_or_origin))))
23613 tree type = TREE_TYPE (decl_or_origin);
23615 if (decl_by_reference_p (decl_or_origin))
23616 add_type_attribute (var_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
23617 context_die);
23618 else
23619 add_type_attribute (var_die, type, decl_quals (decl_or_origin), false,
23620 context_die);
23623 if (origin == NULL && !specialization_p)
23625 if (TREE_PUBLIC (decl))
23626 add_AT_flag (var_die, DW_AT_external, 1);
23628 if (DECL_ARTIFICIAL (decl))
23629 add_AT_flag (var_die, DW_AT_artificial, 1);
23631 add_alignment_attribute (var_die, decl);
23633 add_accessibility_attribute (var_die, decl);
23636 if (declaration)
23637 add_AT_flag (var_die, DW_AT_declaration, 1);
23639 if (decl && (DECL_ABSTRACT_P (decl)
23640 || !old_die || is_declaration_die (old_die)))
23641 equate_decl_number_to_die (decl, var_die);
23643 gen_variable_die_location:
23644 if (! declaration
23645 && (! DECL_ABSTRACT_P (decl_or_origin)
23646 /* Local static vars are shared between all clones/inlines,
23647 so emit DW_AT_location on the abstract DIE if DECL_RTL is
23648 already set. */
23649 || (VAR_P (decl_or_origin)
23650 && TREE_STATIC (decl_or_origin)
23651 && DECL_RTL_SET_P (decl_or_origin))))
23653 if (early_dwarf)
23654 add_pubname (decl_or_origin, var_die);
23655 else
23656 add_location_or_const_value_attribute (var_die, decl_or_origin,
23657 decl == NULL);
23659 else
23660 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
23662 if ((dwarf_version >= 4 || !dwarf_strict)
23663 && lang_hooks.decls.decl_dwarf_attribute (decl_or_origin,
23664 DW_AT_const_expr) == 1
23665 && !get_AT (var_die, DW_AT_const_expr)
23666 && !specialization_p)
23667 add_AT_flag (var_die, DW_AT_const_expr, 1);
23669 if (!dwarf_strict)
23671 int inl = lang_hooks.decls.decl_dwarf_attribute (decl_or_origin,
23672 DW_AT_inline);
23673 if (inl != -1
23674 && !get_AT (var_die, DW_AT_inline)
23675 && !specialization_p)
23676 add_AT_unsigned (var_die, DW_AT_inline, inl);
23680 /* Generate a DIE to represent a named constant. */
23682 static void
23683 gen_const_die (tree decl, dw_die_ref context_die)
23685 dw_die_ref const_die;
23686 tree type = TREE_TYPE (decl);
23688 const_die = lookup_decl_die (decl);
23689 if (const_die)
23690 return;
23692 const_die = new_die (DW_TAG_constant, context_die, decl);
23693 equate_decl_number_to_die (decl, const_die);
23694 add_name_and_src_coords_attributes (const_die, decl);
23695 add_type_attribute (const_die, type, TYPE_QUAL_CONST, false, context_die);
23696 if (TREE_PUBLIC (decl))
23697 add_AT_flag (const_die, DW_AT_external, 1);
23698 if (DECL_ARTIFICIAL (decl))
23699 add_AT_flag (const_die, DW_AT_artificial, 1);
23700 tree_add_const_value_attribute_for_decl (const_die, decl);
23703 /* Generate a DIE to represent a label identifier. */
23705 static void
23706 gen_label_die (tree decl, dw_die_ref context_die)
23708 tree origin = decl_ultimate_origin (decl);
23709 dw_die_ref lbl_die = lookup_decl_die (decl);
23710 rtx insn;
23711 char label[MAX_ARTIFICIAL_LABEL_BYTES];
23713 if (!lbl_die)
23715 lbl_die = new_die (DW_TAG_label, context_die, decl);
23716 equate_decl_number_to_die (decl, lbl_die);
23718 if (origin != NULL)
23719 add_abstract_origin_attribute (lbl_die, origin);
23720 else
23721 add_name_and_src_coords_attributes (lbl_die, decl);
23724 if (DECL_ABSTRACT_P (decl))
23725 equate_decl_number_to_die (decl, lbl_die);
23726 else if (! early_dwarf)
23728 insn = DECL_RTL_IF_SET (decl);
23730 /* Deleted labels are programmer specified labels which have been
23731 eliminated because of various optimizations. We still emit them
23732 here so that it is possible to put breakpoints on them. */
23733 if (insn
23734 && (LABEL_P (insn)
23735 || ((NOTE_P (insn)
23736 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
23738 /* When optimization is enabled (via -O) some parts of the compiler
23739 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
23740 represent source-level labels which were explicitly declared by
23741 the user. This really shouldn't be happening though, so catch
23742 it if it ever does happen. */
23743 gcc_assert (!as_a<rtx_insn *> (insn)->deleted ());
23745 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
23746 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
23748 else if (insn
23749 && NOTE_P (insn)
23750 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
23751 && CODE_LABEL_NUMBER (insn) != -1)
23753 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
23754 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
23759 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
23760 attributes to the DIE for a block STMT, to describe where the inlined
23761 function was called from. This is similar to add_src_coords_attributes. */
23763 static inline void
23764 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
23766 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
23768 if (dwarf_version >= 3 || !dwarf_strict)
23770 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
23771 add_AT_unsigned (die, DW_AT_call_line, s.line);
23772 if (debug_column_info && s.column)
23773 add_AT_unsigned (die, DW_AT_call_column, s.column);
23778 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
23779 Add low_pc and high_pc attributes to the DIE for a block STMT. */
23781 static inline void
23782 add_high_low_attributes (tree stmt, dw_die_ref die)
23784 char label[MAX_ARTIFICIAL_LABEL_BYTES];
23786 if (inline_entry_data **iedp
23787 = !inline_entry_data_table ? NULL
23788 : inline_entry_data_table->find_slot_with_hash (stmt,
23789 htab_hash_pointer (stmt),
23790 NO_INSERT))
23792 inline_entry_data *ied = *iedp;
23793 gcc_assert (MAY_HAVE_DEBUG_MARKER_INSNS);
23794 gcc_assert (debug_inline_points);
23795 gcc_assert (inlined_function_outer_scope_p (stmt));
23797 ASM_GENERATE_INTERNAL_LABEL (label, ied->label_pfx, ied->label_num);
23798 add_AT_lbl_id (die, DW_AT_entry_pc, label);
23800 if (debug_variable_location_views && !ZERO_VIEW_P (ied->view)
23801 && !dwarf_strict)
23803 if (!output_asm_line_debug_info ())
23804 add_AT_unsigned (die, DW_AT_GNU_entry_view, ied->view);
23805 else
23807 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", ied->view);
23808 /* FIXME: this will resolve to a small number. Could we
23809 possibly emit smaller data? Ideally we'd emit a
23810 uleb128, but that would make the size of DIEs
23811 impossible for the compiler to compute, since it's
23812 the assembler that computes the value of the view
23813 label in this case. Ideally, we'd have a single form
23814 encompassing both the address and the view, and
23815 indirecting them through a table might make things
23816 easier, but even that would be more wasteful,
23817 space-wise, than what we have now. */
23818 add_AT_lbl_id (die, DW_AT_GNU_entry_view, label);
23822 inline_entry_data_table->clear_slot (iedp);
23825 if (BLOCK_FRAGMENT_CHAIN (stmt)
23826 && (dwarf_version >= 3 || !dwarf_strict))
23828 tree chain, superblock = NULL_TREE;
23829 dw_die_ref pdie;
23830 dw_attr_node *attr = NULL;
23832 if (!debug_inline_points && inlined_function_outer_scope_p (stmt))
23834 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
23835 BLOCK_NUMBER (stmt));
23836 add_AT_lbl_id (die, DW_AT_entry_pc, label);
23839 /* Optimize duplicate .debug_ranges lists or even tails of
23840 lists. If this BLOCK has same ranges as its supercontext,
23841 lookup DW_AT_ranges attribute in the supercontext (and
23842 recursively so), verify that the ranges_table contains the
23843 right values and use it instead of adding a new .debug_range. */
23844 for (chain = stmt, pdie = die;
23845 BLOCK_SAME_RANGE (chain);
23846 chain = BLOCK_SUPERCONTEXT (chain))
23848 dw_attr_node *new_attr;
23850 pdie = pdie->die_parent;
23851 if (pdie == NULL)
23852 break;
23853 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
23854 break;
23855 new_attr = get_AT (pdie, DW_AT_ranges);
23856 if (new_attr == NULL
23857 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
23858 break;
23859 attr = new_attr;
23860 superblock = BLOCK_SUPERCONTEXT (chain);
23862 if (attr != NULL
23863 && ((*ranges_table)[attr->dw_attr_val.v.val_offset].num
23864 == BLOCK_NUMBER (superblock))
23865 && BLOCK_FRAGMENT_CHAIN (superblock))
23867 unsigned long off = attr->dw_attr_val.v.val_offset;
23868 unsigned long supercnt = 0, thiscnt = 0;
23869 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
23870 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
23872 ++supercnt;
23873 gcc_checking_assert ((*ranges_table)[off + supercnt].num
23874 == BLOCK_NUMBER (chain));
23876 gcc_checking_assert ((*ranges_table)[off + supercnt + 1].num == 0);
23877 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
23878 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
23879 ++thiscnt;
23880 gcc_assert (supercnt >= thiscnt);
23881 add_AT_range_list (die, DW_AT_ranges, off + supercnt - thiscnt,
23882 false);
23883 note_rnglist_head (off + supercnt - thiscnt);
23884 return;
23887 unsigned int offset = add_ranges (stmt, true);
23888 add_AT_range_list (die, DW_AT_ranges, offset, false);
23889 note_rnglist_head (offset);
23891 bool prev_in_cold = BLOCK_IN_COLD_SECTION_P (stmt);
23892 chain = BLOCK_FRAGMENT_CHAIN (stmt);
23895 add_ranges (chain, prev_in_cold != BLOCK_IN_COLD_SECTION_P (chain));
23896 prev_in_cold = BLOCK_IN_COLD_SECTION_P (chain);
23897 chain = BLOCK_FRAGMENT_CHAIN (chain);
23899 while (chain);
23900 add_ranges (NULL);
23902 else
23904 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
23905 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
23906 BLOCK_NUMBER (stmt));
23907 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
23908 BLOCK_NUMBER (stmt));
23909 add_AT_low_high_pc (die, label, label_high, false);
23913 /* Generate a DIE for a lexical block. */
23915 static void
23916 gen_lexical_block_die (tree stmt, dw_die_ref context_die)
23918 dw_die_ref old_die = BLOCK_DIE (stmt);
23919 dw_die_ref stmt_die = NULL;
23920 if (!old_die)
23922 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
23923 BLOCK_DIE (stmt) = stmt_die;
23926 if (BLOCK_ABSTRACT (stmt))
23928 if (old_die)
23930 /* This must have been generated early and it won't even
23931 need location information since it's a DW_AT_inline
23932 function. */
23933 if (flag_checking)
23934 for (dw_die_ref c = context_die; c; c = c->die_parent)
23935 if (c->die_tag == DW_TAG_inlined_subroutine
23936 || c->die_tag == DW_TAG_subprogram)
23938 gcc_assert (get_AT (c, DW_AT_inline));
23939 break;
23941 return;
23944 else if (BLOCK_ABSTRACT_ORIGIN (stmt))
23946 /* If this is an inlined instance, create a new lexical die for
23947 anything below to attach DW_AT_abstract_origin to. */
23948 if (old_die)
23950 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
23951 BLOCK_DIE (stmt) = stmt_die;
23952 old_die = NULL;
23955 tree origin = block_ultimate_origin (stmt);
23956 if (origin != NULL_TREE && origin != stmt)
23957 add_abstract_origin_attribute (stmt_die, origin);
23960 if (old_die)
23961 stmt_die = old_die;
23963 /* A non abstract block whose blocks have already been reordered
23964 should have the instruction range for this block. If so, set the
23965 high/low attributes. */
23966 if (!early_dwarf && !BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
23968 gcc_assert (stmt_die);
23969 add_high_low_attributes (stmt, stmt_die);
23972 decls_for_scope (stmt, stmt_die);
23975 /* Generate a DIE for an inlined subprogram. */
23977 static void
23978 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die)
23980 tree decl;
23982 /* The instance of function that is effectively being inlined shall not
23983 be abstract. */
23984 gcc_assert (! BLOCK_ABSTRACT (stmt));
23986 decl = block_ultimate_origin (stmt);
23988 /* Make sure any inlined functions are known to be inlineable. */
23989 gcc_checking_assert (DECL_ABSTRACT_P (decl)
23990 || cgraph_function_possibly_inlined_p (decl));
23992 if (! BLOCK_ABSTRACT (stmt))
23994 dw_die_ref subr_die
23995 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
23997 if (call_arg_locations || debug_inline_points)
23998 BLOCK_DIE (stmt) = subr_die;
23999 add_abstract_origin_attribute (subr_die, decl);
24000 if (TREE_ASM_WRITTEN (stmt))
24001 add_high_low_attributes (stmt, subr_die);
24002 add_call_src_coords_attributes (stmt, subr_die);
24004 decls_for_scope (stmt, subr_die);
24008 /* Generate a DIE for a field in a record, or structure. CTX is required: see
24009 the comment for VLR_CONTEXT. */
24011 static void
24012 gen_field_die (tree decl, struct vlr_context *ctx, dw_die_ref context_die)
24014 dw_die_ref decl_die;
24016 if (TREE_TYPE (decl) == error_mark_node)
24017 return;
24019 decl_die = new_die (DW_TAG_member, context_die, decl);
24020 add_name_and_src_coords_attributes (decl_die, decl);
24021 add_type_attribute (decl_die, member_declared_type (decl), decl_quals (decl),
24022 TYPE_REVERSE_STORAGE_ORDER (DECL_FIELD_CONTEXT (decl)),
24023 context_die);
24025 if (DECL_BIT_FIELD_TYPE (decl))
24027 add_byte_size_attribute (decl_die, decl);
24028 add_bit_size_attribute (decl_die, decl);
24029 add_bit_offset_attribute (decl_die, decl, ctx);
24032 add_alignment_attribute (decl_die, decl);
24034 /* If we have a variant part offset, then we are supposed to process a member
24035 of a QUAL_UNION_TYPE, which is how we represent variant parts in
24036 trees. */
24037 gcc_assert (ctx->variant_part_offset == NULL_TREE
24038 || TREE_CODE (DECL_FIELD_CONTEXT (decl)) != QUAL_UNION_TYPE);
24039 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
24040 add_data_member_location_attribute (decl_die, decl, ctx);
24042 if (DECL_ARTIFICIAL (decl))
24043 add_AT_flag (decl_die, DW_AT_artificial, 1);
24045 add_accessibility_attribute (decl_die, decl);
24047 /* Equate decl number to die, so that we can look up this decl later on. */
24048 equate_decl_number_to_die (decl, decl_die);
24051 /* Generate a DIE for a pointer to a member type. TYPE can be an
24052 OFFSET_TYPE, for a pointer to data member, or a RECORD_TYPE, for a
24053 pointer to member function. */
24055 static void
24056 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
24058 if (lookup_type_die (type))
24059 return;
24061 dw_die_ref ptr_die = new_die (DW_TAG_ptr_to_member_type,
24062 scope_die_for (type, context_die), type);
24064 equate_type_number_to_die (type, ptr_die);
24065 add_AT_die_ref (ptr_die, DW_AT_containing_type,
24066 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
24067 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
24068 context_die);
24069 add_alignment_attribute (ptr_die, type);
24071 if (TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE
24072 && TREE_CODE (TREE_TYPE (type)) != METHOD_TYPE)
24074 dw_loc_descr_ref op = new_loc_descr (DW_OP_plus, 0, 0);
24075 add_AT_loc (ptr_die, DW_AT_use_location, op);
24079 static char *producer_string;
24081 /* Return a heap allocated producer string including command line options
24082 if -grecord-gcc-switches. */
24084 static char *
24085 gen_producer_string (void)
24087 size_t j;
24088 auto_vec<const char *> switches;
24089 const char *language_string = lang_hooks.name;
24090 char *producer, *tail;
24091 const char *p;
24092 size_t len = dwarf_record_gcc_switches ? 0 : 3;
24093 size_t plen = strlen (language_string) + 1 + strlen (version_string);
24095 for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
24096 switch (save_decoded_options[j].opt_index)
24098 case OPT_o:
24099 case OPT_d:
24100 case OPT_dumpbase:
24101 case OPT_dumpdir:
24102 case OPT_auxbase:
24103 case OPT_auxbase_strip:
24104 case OPT_quiet:
24105 case OPT_version:
24106 case OPT_v:
24107 case OPT_w:
24108 case OPT_L:
24109 case OPT_D:
24110 case OPT_I:
24111 case OPT_U:
24112 case OPT_SPECIAL_unknown:
24113 case OPT_SPECIAL_ignore:
24114 case OPT_SPECIAL_program_name:
24115 case OPT_SPECIAL_input_file:
24116 case OPT_grecord_gcc_switches:
24117 case OPT__output_pch_:
24118 case OPT_fdiagnostics_show_location_:
24119 case OPT_fdiagnostics_show_option:
24120 case OPT_fdiagnostics_show_caret:
24121 case OPT_fdiagnostics_color_:
24122 case OPT_fverbose_asm:
24123 case OPT____:
24124 case OPT__sysroot_:
24125 case OPT_nostdinc:
24126 case OPT_nostdinc__:
24127 case OPT_fpreprocessed:
24128 case OPT_fltrans_output_list_:
24129 case OPT_fresolution_:
24130 case OPT_fdebug_prefix_map_:
24131 case OPT_fmacro_prefix_map_:
24132 case OPT_ffile_prefix_map_:
24133 case OPT_fcompare_debug:
24134 /* Ignore these. */
24135 continue;
24136 default:
24137 if (cl_options[save_decoded_options[j].opt_index].flags
24138 & CL_NO_DWARF_RECORD)
24139 continue;
24140 gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
24141 == '-');
24142 switch (save_decoded_options[j].canonical_option[0][1])
24144 case 'M':
24145 case 'i':
24146 case 'W':
24147 continue;
24148 case 'f':
24149 if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
24150 "dump", 4) == 0)
24151 continue;
24152 break;
24153 default:
24154 break;
24156 switches.safe_push (save_decoded_options[j].orig_option_with_args_text);
24157 len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
24158 break;
24161 producer = XNEWVEC (char, plen + 1 + len + 1);
24162 tail = producer;
24163 sprintf (tail, "%s %s", language_string, version_string);
24164 tail += plen;
24166 FOR_EACH_VEC_ELT (switches, j, p)
24168 len = strlen (p);
24169 *tail = ' ';
24170 memcpy (tail + 1, p, len);
24171 tail += len + 1;
24174 *tail = '\0';
24175 return producer;
24178 /* Given a C and/or C++ language/version string return the "highest".
24179 C++ is assumed to be "higher" than C in this case. Used for merging
24180 LTO translation unit languages. */
24181 static const char *
24182 highest_c_language (const char *lang1, const char *lang2)
24184 if (strcmp ("GNU C++17", lang1) == 0 || strcmp ("GNU C++17", lang2) == 0)
24185 return "GNU C++17";
24186 if (strcmp ("GNU C++14", lang1) == 0 || strcmp ("GNU C++14", lang2) == 0)
24187 return "GNU C++14";
24188 if (strcmp ("GNU C++11", lang1) == 0 || strcmp ("GNU C++11", lang2) == 0)
24189 return "GNU C++11";
24190 if (strcmp ("GNU C++98", lang1) == 0 || strcmp ("GNU C++98", lang2) == 0)
24191 return "GNU C++98";
24193 if (strcmp ("GNU C17", lang1) == 0 || strcmp ("GNU C17", lang2) == 0)
24194 return "GNU C17";
24195 if (strcmp ("GNU C11", lang1) == 0 || strcmp ("GNU C11", lang2) == 0)
24196 return "GNU C11";
24197 if (strcmp ("GNU C99", lang1) == 0 || strcmp ("GNU C99", lang2) == 0)
24198 return "GNU C99";
24199 if (strcmp ("GNU C89", lang1) == 0 || strcmp ("GNU C89", lang2) == 0)
24200 return "GNU C89";
24202 gcc_unreachable ();
24206 /* Generate the DIE for the compilation unit. */
24208 static dw_die_ref
24209 gen_compile_unit_die (const char *filename)
24211 dw_die_ref die;
24212 const char *language_string = lang_hooks.name;
24213 int language;
24215 die = new_die (DW_TAG_compile_unit, NULL, NULL);
24217 if (filename)
24219 add_name_attribute (die, filename);
24220 /* Don't add cwd for <built-in>. */
24221 if (filename[0] != '<')
24222 add_comp_dir_attribute (die);
24225 add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
24227 /* If our producer is LTO try to figure out a common language to use
24228 from the global list of translation units. */
24229 if (strcmp (language_string, "GNU GIMPLE") == 0)
24231 unsigned i;
24232 tree t;
24233 const char *common_lang = NULL;
24235 FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
24237 if (!TRANSLATION_UNIT_LANGUAGE (t))
24238 continue;
24239 if (!common_lang)
24240 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
24241 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
24243 else if (strncmp (common_lang, "GNU C", 5) == 0
24244 && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
24245 /* Mixing C and C++ is ok, use C++ in that case. */
24246 common_lang = highest_c_language (common_lang,
24247 TRANSLATION_UNIT_LANGUAGE (t));
24248 else
24250 /* Fall back to C. */
24251 common_lang = NULL;
24252 break;
24256 if (common_lang)
24257 language_string = common_lang;
24260 language = DW_LANG_C;
24261 if (strncmp (language_string, "GNU C", 5) == 0
24262 && ISDIGIT (language_string[5]))
24264 language = DW_LANG_C89;
24265 if (dwarf_version >= 3 || !dwarf_strict)
24267 if (strcmp (language_string, "GNU C89") != 0)
24268 language = DW_LANG_C99;
24270 if (dwarf_version >= 5 /* || !dwarf_strict */)
24271 if (strcmp (language_string, "GNU C11") == 0
24272 || strcmp (language_string, "GNU C17") == 0)
24273 language = DW_LANG_C11;
24276 else if (strncmp (language_string, "GNU C++", 7) == 0)
24278 language = DW_LANG_C_plus_plus;
24279 if (dwarf_version >= 5 /* || !dwarf_strict */)
24281 if (strcmp (language_string, "GNU C++11") == 0)
24282 language = DW_LANG_C_plus_plus_11;
24283 else if (strcmp (language_string, "GNU C++14") == 0)
24284 language = DW_LANG_C_plus_plus_14;
24285 else if (strcmp (language_string, "GNU C++17") == 0)
24286 /* For now. */
24287 language = DW_LANG_C_plus_plus_14;
24290 else if (strcmp (language_string, "GNU F77") == 0)
24291 language = DW_LANG_Fortran77;
24292 else if (dwarf_version >= 3 || !dwarf_strict)
24294 if (strcmp (language_string, "GNU Ada") == 0)
24295 language = DW_LANG_Ada95;
24296 else if (strncmp (language_string, "GNU Fortran", 11) == 0)
24298 language = DW_LANG_Fortran95;
24299 if (dwarf_version >= 5 /* || !dwarf_strict */)
24301 if (strcmp (language_string, "GNU Fortran2003") == 0)
24302 language = DW_LANG_Fortran03;
24303 else if (strcmp (language_string, "GNU Fortran2008") == 0)
24304 language = DW_LANG_Fortran08;
24307 else if (strcmp (language_string, "GNU Objective-C") == 0)
24308 language = DW_LANG_ObjC;
24309 else if (strcmp (language_string, "GNU Objective-C++") == 0)
24310 language = DW_LANG_ObjC_plus_plus;
24311 else if (dwarf_version >= 5 || !dwarf_strict)
24313 if (strcmp (language_string, "GNU Go") == 0)
24314 language = DW_LANG_Go;
24317 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
24318 else if (strncmp (language_string, "GNU Fortran", 11) == 0)
24319 language = DW_LANG_Fortran90;
24321 add_AT_unsigned (die, DW_AT_language, language);
24323 switch (language)
24325 case DW_LANG_Fortran77:
24326 case DW_LANG_Fortran90:
24327 case DW_LANG_Fortran95:
24328 case DW_LANG_Fortran03:
24329 case DW_LANG_Fortran08:
24330 /* Fortran has case insensitive identifiers and the front-end
24331 lowercases everything. */
24332 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
24333 break;
24334 default:
24335 /* The default DW_ID_case_sensitive doesn't need to be specified. */
24336 break;
24338 return die;
24341 /* Generate the DIE for a base class. */
24343 static void
24344 gen_inheritance_die (tree binfo, tree access, tree type,
24345 dw_die_ref context_die)
24347 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
24348 struct vlr_context ctx = { type, NULL };
24350 add_type_attribute (die, BINFO_TYPE (binfo), TYPE_UNQUALIFIED, false,
24351 context_die);
24352 add_data_member_location_attribute (die, binfo, &ctx);
24354 if (BINFO_VIRTUAL_P (binfo))
24355 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
24357 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
24358 children, otherwise the default is DW_ACCESS_public. In DWARF2
24359 the default has always been DW_ACCESS_private. */
24360 if (access == access_public_node)
24362 if (dwarf_version == 2
24363 || context_die->die_tag == DW_TAG_class_type)
24364 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
24366 else if (access == access_protected_node)
24367 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
24368 else if (dwarf_version > 2
24369 && context_die->die_tag != DW_TAG_class_type)
24370 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
24373 /* Return whether DECL is a FIELD_DECL that represents the variant part of a
24374 structure. */
24375 static bool
24376 is_variant_part (tree decl)
24378 return (TREE_CODE (decl) == FIELD_DECL
24379 && TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE);
24382 /* Check that OPERAND is a reference to a field in STRUCT_TYPE. If it is,
24383 return the FIELD_DECL. Return NULL_TREE otherwise. */
24385 static tree
24386 analyze_discr_in_predicate (tree operand, tree struct_type)
24388 bool continue_stripping = true;
24389 while (continue_stripping)
24390 switch (TREE_CODE (operand))
24392 CASE_CONVERT:
24393 operand = TREE_OPERAND (operand, 0);
24394 break;
24395 default:
24396 continue_stripping = false;
24397 break;
24400 /* Match field access to members of struct_type only. */
24401 if (TREE_CODE (operand) == COMPONENT_REF
24402 && TREE_CODE (TREE_OPERAND (operand, 0)) == PLACEHOLDER_EXPR
24403 && TREE_TYPE (TREE_OPERAND (operand, 0)) == struct_type
24404 && TREE_CODE (TREE_OPERAND (operand, 1)) == FIELD_DECL)
24405 return TREE_OPERAND (operand, 1);
24406 else
24407 return NULL_TREE;
24410 /* Check that SRC is a constant integer that can be represented as a native
24411 integer constant (either signed or unsigned). If so, store it into DEST and
24412 return true. Return false otherwise. */
24414 static bool
24415 get_discr_value (tree src, dw_discr_value *dest)
24417 tree discr_type = TREE_TYPE (src);
24419 if (lang_hooks.types.get_debug_type)
24421 tree debug_type = lang_hooks.types.get_debug_type (discr_type);
24422 if (debug_type != NULL)
24423 discr_type = debug_type;
24426 if (TREE_CODE (src) != INTEGER_CST || !INTEGRAL_TYPE_P (discr_type))
24427 return false;
24429 /* Signedness can vary between the original type and the debug type. This
24430 can happen for character types in Ada for instance: the character type
24431 used for code generation can be signed, to be compatible with the C one,
24432 but from a debugger point of view, it must be unsigned. */
24433 bool is_orig_unsigned = TYPE_UNSIGNED (TREE_TYPE (src));
24434 bool is_debug_unsigned = TYPE_UNSIGNED (discr_type);
24436 if (is_orig_unsigned != is_debug_unsigned)
24437 src = fold_convert (discr_type, src);
24439 if (!(is_debug_unsigned ? tree_fits_uhwi_p (src) : tree_fits_shwi_p (src)))
24440 return false;
24442 dest->pos = is_debug_unsigned;
24443 if (is_debug_unsigned)
24444 dest->v.uval = tree_to_uhwi (src);
24445 else
24446 dest->v.sval = tree_to_shwi (src);
24448 return true;
24451 /* Try to extract synthetic properties out of VARIANT_PART_DECL, which is a
24452 FIELD_DECL in STRUCT_TYPE that represents a variant part. If unsuccessful,
24453 store NULL_TREE in DISCR_DECL. Otherwise:
24455 - store the discriminant field in STRUCT_TYPE that controls the variant
24456 part to *DISCR_DECL
24458 - put in *DISCR_LISTS_P an array where for each variant, the item
24459 represents the corresponding matching list of discriminant values.
24461 - put in *DISCR_LISTS_LENGTH the number of variants, which is the size of
24462 the above array.
24464 Note that when the array is allocated (i.e. when the analysis is
24465 successful), it is up to the caller to free the array. */
24467 static void
24468 analyze_variants_discr (tree variant_part_decl,
24469 tree struct_type,
24470 tree *discr_decl,
24471 dw_discr_list_ref **discr_lists_p,
24472 unsigned *discr_lists_length)
24474 tree variant_part_type = TREE_TYPE (variant_part_decl);
24475 tree variant;
24476 dw_discr_list_ref *discr_lists;
24477 unsigned i;
24479 /* Compute how many variants there are in this variant part. */
24480 *discr_lists_length = 0;
24481 for (variant = TYPE_FIELDS (variant_part_type);
24482 variant != NULL_TREE;
24483 variant = DECL_CHAIN (variant))
24484 ++*discr_lists_length;
24486 *discr_decl = NULL_TREE;
24487 *discr_lists_p
24488 = (dw_discr_list_ref *) xcalloc (*discr_lists_length,
24489 sizeof (**discr_lists_p));
24490 discr_lists = *discr_lists_p;
24492 /* And then analyze all variants to extract discriminant information for all
24493 of them. This analysis is conservative: as soon as we detect something we
24494 do not support, abort everything and pretend we found nothing. */
24495 for (variant = TYPE_FIELDS (variant_part_type), i = 0;
24496 variant != NULL_TREE;
24497 variant = DECL_CHAIN (variant), ++i)
24499 tree match_expr = DECL_QUALIFIER (variant);
24501 /* Now, try to analyze the predicate and deduce a discriminant for
24502 it. */
24503 if (match_expr == boolean_true_node)
24504 /* Typically happens for the default variant: it matches all cases that
24505 previous variants rejected. Don't output any matching value for
24506 this one. */
24507 continue;
24509 /* The following loop tries to iterate over each discriminant
24510 possibility: single values or ranges. */
24511 while (match_expr != NULL_TREE)
24513 tree next_round_match_expr;
24514 tree candidate_discr = NULL_TREE;
24515 dw_discr_list_ref new_node = NULL;
24517 /* Possibilities are matched one after the other by nested
24518 TRUTH_ORIF_EXPR expressions. Process the current possibility and
24519 continue with the rest at next iteration. */
24520 if (TREE_CODE (match_expr) == TRUTH_ORIF_EXPR)
24522 next_round_match_expr = TREE_OPERAND (match_expr, 0);
24523 match_expr = TREE_OPERAND (match_expr, 1);
24525 else
24526 next_round_match_expr = NULL_TREE;
24528 if (match_expr == boolean_false_node)
24529 /* This sub-expression matches nothing: just wait for the next
24530 one. */
24533 else if (TREE_CODE (match_expr) == EQ_EXPR)
24535 /* We are matching: <discr_field> == <integer_cst>
24536 This sub-expression matches a single value. */
24537 tree integer_cst = TREE_OPERAND (match_expr, 1);
24539 candidate_discr
24540 = analyze_discr_in_predicate (TREE_OPERAND (match_expr, 0),
24541 struct_type);
24543 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
24544 if (!get_discr_value (integer_cst,
24545 &new_node->dw_discr_lower_bound))
24546 goto abort;
24547 new_node->dw_discr_range = false;
24550 else if (TREE_CODE (match_expr) == TRUTH_ANDIF_EXPR)
24552 /* We are matching:
24553 <discr_field> > <integer_cst>
24554 && <discr_field> < <integer_cst>.
24555 This sub-expression matches the range of values between the
24556 two matched integer constants. Note that comparisons can be
24557 inclusive or exclusive. */
24558 tree candidate_discr_1, candidate_discr_2;
24559 tree lower_cst, upper_cst;
24560 bool lower_cst_included, upper_cst_included;
24561 tree lower_op = TREE_OPERAND (match_expr, 0);
24562 tree upper_op = TREE_OPERAND (match_expr, 1);
24564 /* When the comparison is exclusive, the integer constant is not
24565 the discriminant range bound we are looking for: we will have
24566 to increment or decrement it. */
24567 if (TREE_CODE (lower_op) == GE_EXPR)
24568 lower_cst_included = true;
24569 else if (TREE_CODE (lower_op) == GT_EXPR)
24570 lower_cst_included = false;
24571 else
24572 goto abort;
24574 if (TREE_CODE (upper_op) == LE_EXPR)
24575 upper_cst_included = true;
24576 else if (TREE_CODE (upper_op) == LT_EXPR)
24577 upper_cst_included = false;
24578 else
24579 goto abort;
24581 /* Extract the discriminant from the first operand and check it
24582 is consistant with the same analysis in the second
24583 operand. */
24584 candidate_discr_1
24585 = analyze_discr_in_predicate (TREE_OPERAND (lower_op, 0),
24586 struct_type);
24587 candidate_discr_2
24588 = analyze_discr_in_predicate (TREE_OPERAND (upper_op, 0),
24589 struct_type);
24590 if (candidate_discr_1 == candidate_discr_2)
24591 candidate_discr = candidate_discr_1;
24592 else
24593 goto abort;
24595 /* Extract bounds from both. */
24596 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
24597 lower_cst = TREE_OPERAND (lower_op, 1);
24598 upper_cst = TREE_OPERAND (upper_op, 1);
24600 if (!lower_cst_included)
24601 lower_cst
24602 = fold_build2 (PLUS_EXPR, TREE_TYPE (lower_cst), lower_cst,
24603 build_int_cst (TREE_TYPE (lower_cst), 1));
24604 if (!upper_cst_included)
24605 upper_cst
24606 = fold_build2 (MINUS_EXPR, TREE_TYPE (upper_cst), upper_cst,
24607 build_int_cst (TREE_TYPE (upper_cst), 1));
24609 if (!get_discr_value (lower_cst,
24610 &new_node->dw_discr_lower_bound)
24611 || !get_discr_value (upper_cst,
24612 &new_node->dw_discr_upper_bound))
24613 goto abort;
24615 new_node->dw_discr_range = true;
24618 else
24619 /* Unsupported sub-expression: we cannot determine the set of
24620 matching discriminant values. Abort everything. */
24621 goto abort;
24623 /* If the discriminant info is not consistant with what we saw so
24624 far, consider the analysis failed and abort everything. */
24625 if (candidate_discr == NULL_TREE
24626 || (*discr_decl != NULL_TREE && candidate_discr != *discr_decl))
24627 goto abort;
24628 else
24629 *discr_decl = candidate_discr;
24631 if (new_node != NULL)
24633 new_node->dw_discr_next = discr_lists[i];
24634 discr_lists[i] = new_node;
24636 match_expr = next_round_match_expr;
24640 /* If we reach this point, we could match everything we were interested
24641 in. */
24642 return;
24644 abort:
24645 /* Clean all data structure and return no result. */
24646 free (*discr_lists_p);
24647 *discr_lists_p = NULL;
24648 *discr_decl = NULL_TREE;
24651 /* Generate a DIE to represent VARIANT_PART_DECL, a variant part that is part
24652 of STRUCT_TYPE, a record type. This new DIE is emitted as the next child
24653 under CONTEXT_DIE.
24655 Variant parts are supposed to be implemented as a FIELD_DECL whose type is a
24656 QUAL_UNION_TYPE: this is the VARIANT_PART_DECL parameter. The members for
24657 this type, which are record types, represent the available variants and each
24658 has a DECL_QUALIFIER attribute. The discriminant and the discriminant
24659 values are inferred from these attributes.
24661 In trees, the offsets for the fields inside these sub-records are relative
24662 to the variant part itself, whereas the corresponding DIEs should have
24663 offset attributes that are relative to the embedding record base address.
24664 This is why the caller must provide a VARIANT_PART_OFFSET expression: it
24665 must be an expression that computes the offset of the variant part to
24666 describe in DWARF. */
24668 static void
24669 gen_variant_part (tree variant_part_decl, struct vlr_context *vlr_ctx,
24670 dw_die_ref context_die)
24672 const tree variant_part_type = TREE_TYPE (variant_part_decl);
24673 tree variant_part_offset = vlr_ctx->variant_part_offset;
24674 struct loc_descr_context ctx = {
24675 vlr_ctx->struct_type, /* context_type */
24676 NULL_TREE, /* base_decl */
24677 NULL, /* dpi */
24678 false, /* placeholder_arg */
24679 false /* placeholder_seen */
24682 /* The FIELD_DECL node in STRUCT_TYPE that acts as the discriminant, or
24683 NULL_TREE if there is no such field. */
24684 tree discr_decl = NULL_TREE;
24685 dw_discr_list_ref *discr_lists;
24686 unsigned discr_lists_length = 0;
24687 unsigned i;
24689 dw_die_ref dwarf_proc_die = NULL;
24690 dw_die_ref variant_part_die
24691 = new_die (DW_TAG_variant_part, context_die, variant_part_type);
24693 equate_decl_number_to_die (variant_part_decl, variant_part_die);
24695 analyze_variants_discr (variant_part_decl, vlr_ctx->struct_type,
24696 &discr_decl, &discr_lists, &discr_lists_length);
24698 if (discr_decl != NULL_TREE)
24700 dw_die_ref discr_die = lookup_decl_die (discr_decl);
24702 if (discr_die)
24703 add_AT_die_ref (variant_part_die, DW_AT_discr, discr_die);
24704 else
24705 /* We have no DIE for the discriminant, so just discard all
24706 discrimimant information in the output. */
24707 discr_decl = NULL_TREE;
24710 /* If the offset for this variant part is more complex than a constant,
24711 create a DWARF procedure for it so that we will not have to generate DWARF
24712 expressions for it for each member. */
24713 if (TREE_CODE (variant_part_offset) != INTEGER_CST
24714 && (dwarf_version >= 3 || !dwarf_strict))
24716 const tree dwarf_proc_fndecl
24717 = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL, NULL_TREE,
24718 build_function_type (TREE_TYPE (variant_part_offset),
24719 NULL_TREE));
24720 const tree dwarf_proc_call = build_call_expr (dwarf_proc_fndecl, 0);
24721 const dw_loc_descr_ref dwarf_proc_body
24722 = loc_descriptor_from_tree (variant_part_offset, 0, &ctx);
24724 dwarf_proc_die = new_dwarf_proc_die (dwarf_proc_body,
24725 dwarf_proc_fndecl, context_die);
24726 if (dwarf_proc_die != NULL)
24727 variant_part_offset = dwarf_proc_call;
24730 /* Output DIEs for all variants. */
24731 i = 0;
24732 for (tree variant = TYPE_FIELDS (variant_part_type);
24733 variant != NULL_TREE;
24734 variant = DECL_CHAIN (variant), ++i)
24736 tree variant_type = TREE_TYPE (variant);
24737 dw_die_ref variant_die;
24739 /* All variants (i.e. members of a variant part) are supposed to be
24740 encoded as structures. Sub-variant parts are QUAL_UNION_TYPE fields
24741 under these records. */
24742 gcc_assert (TREE_CODE (variant_type) == RECORD_TYPE);
24744 variant_die = new_die (DW_TAG_variant, variant_part_die, variant_type);
24745 equate_decl_number_to_die (variant, variant_die);
24747 /* Output discriminant values this variant matches, if any. */
24748 if (discr_decl == NULL || discr_lists[i] == NULL)
24749 /* In the case we have discriminant information at all, this is
24750 probably the default variant: as the standard says, don't
24751 output any discriminant value/list attribute. */
24753 else if (discr_lists[i]->dw_discr_next == NULL
24754 && !discr_lists[i]->dw_discr_range)
24755 /* If there is only one accepted value, don't bother outputting a
24756 list. */
24757 add_discr_value (variant_die, &discr_lists[i]->dw_discr_lower_bound);
24758 else
24759 add_discr_list (variant_die, discr_lists[i]);
24761 for (tree member = TYPE_FIELDS (variant_type);
24762 member != NULL_TREE;
24763 member = DECL_CHAIN (member))
24765 struct vlr_context vlr_sub_ctx = {
24766 vlr_ctx->struct_type, /* struct_type */
24767 NULL /* variant_part_offset */
24769 if (is_variant_part (member))
24771 /* All offsets for fields inside variant parts are relative to
24772 the top-level embedding RECORD_TYPE's base address. On the
24773 other hand, offsets in GCC's types are relative to the
24774 nested-most variant part. So we have to sum offsets each time
24775 we recurse. */
24777 vlr_sub_ctx.variant_part_offset
24778 = fold_build2 (PLUS_EXPR, TREE_TYPE (variant_part_offset),
24779 variant_part_offset, byte_position (member));
24780 gen_variant_part (member, &vlr_sub_ctx, variant_die);
24782 else
24784 vlr_sub_ctx.variant_part_offset = variant_part_offset;
24785 gen_decl_die (member, NULL, &vlr_sub_ctx, variant_die);
24790 free (discr_lists);
24793 /* Generate a DIE for a class member. */
24795 static void
24796 gen_member_die (tree type, dw_die_ref context_die)
24798 tree member;
24799 tree binfo = TYPE_BINFO (type);
24801 gcc_assert (TYPE_MAIN_VARIANT (type) == type);
24803 /* If this is not an incomplete type, output descriptions of each of its
24804 members. Note that as we output the DIEs necessary to represent the
24805 members of this record or union type, we will also be trying to output
24806 DIEs to represent the *types* of those members. However the `type'
24807 function (above) will specifically avoid generating type DIEs for member
24808 types *within* the list of member DIEs for this (containing) type except
24809 for those types (of members) which are explicitly marked as also being
24810 members of this (containing) type themselves. The g++ front- end can
24811 force any given type to be treated as a member of some other (containing)
24812 type by setting the TYPE_CONTEXT of the given (member) type to point to
24813 the TREE node representing the appropriate (containing) type. */
24815 /* First output info about the base classes. */
24816 if (binfo)
24818 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
24819 int i;
24820 tree base;
24822 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
24823 gen_inheritance_die (base,
24824 (accesses ? (*accesses)[i] : access_public_node),
24825 type,
24826 context_die);
24829 /* Now output info about the data members and type members. */
24830 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
24832 struct vlr_context vlr_ctx = { type, NULL_TREE };
24833 bool static_inline_p
24834 = (TREE_STATIC (member)
24835 && (lang_hooks.decls.decl_dwarf_attribute (member, DW_AT_inline)
24836 != -1));
24838 /* Ignore clones. */
24839 if (DECL_ABSTRACT_ORIGIN (member))
24840 continue;
24842 /* If we thought we were generating minimal debug info for TYPE
24843 and then changed our minds, some of the member declarations
24844 may have already been defined. Don't define them again, but
24845 do put them in the right order. */
24847 if (dw_die_ref child = lookup_decl_die (member))
24849 /* Handle inline static data members, which only have in-class
24850 declarations. */
24851 dw_die_ref ref = NULL;
24852 if (child->die_tag == DW_TAG_variable
24853 && child->die_parent == comp_unit_die ())
24855 ref = get_AT_ref (child, DW_AT_specification);
24856 /* For C++17 inline static data members followed by redundant
24857 out of class redeclaration, we might get here with
24858 child being the DIE created for the out of class
24859 redeclaration and with its DW_AT_specification being
24860 the DIE created for in-class definition. We want to
24861 reparent the latter, and don't want to create another
24862 DIE with DW_AT_specification in that case, because
24863 we already have one. */
24864 if (ref
24865 && static_inline_p
24866 && ref->die_tag == DW_TAG_variable
24867 && ref->die_parent == comp_unit_die ()
24868 && get_AT (ref, DW_AT_specification) == NULL)
24870 child = ref;
24871 ref = NULL;
24872 static_inline_p = false;
24876 if (child->die_tag == DW_TAG_variable
24877 && child->die_parent == comp_unit_die ()
24878 && ref == NULL)
24880 reparent_child (child, context_die);
24881 if (dwarf_version < 5)
24882 child->die_tag = DW_TAG_member;
24884 else
24885 splice_child_die (context_die, child);
24888 /* Do not generate standard DWARF for variant parts if we are generating
24889 the corresponding GNAT encodings: DIEs generated for both would
24890 conflict in our mappings. */
24891 else if (is_variant_part (member)
24892 && gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL)
24894 vlr_ctx.variant_part_offset = byte_position (member);
24895 gen_variant_part (member, &vlr_ctx, context_die);
24897 else
24899 vlr_ctx.variant_part_offset = NULL_TREE;
24900 gen_decl_die (member, NULL, &vlr_ctx, context_die);
24903 /* For C++ inline static data members emit immediately a DW_TAG_variable
24904 DIE that will refer to that DW_TAG_member/DW_TAG_variable through
24905 DW_AT_specification. */
24906 if (static_inline_p)
24908 int old_extern = DECL_EXTERNAL (member);
24909 DECL_EXTERNAL (member) = 0;
24910 gen_decl_die (member, NULL, NULL, comp_unit_die ());
24911 DECL_EXTERNAL (member) = old_extern;
24916 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
24917 is set, we pretend that the type was never defined, so we only get the
24918 member DIEs needed by later specification DIEs. */
24920 static void
24921 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
24922 enum debug_info_usage usage)
24924 if (TREE_ASM_WRITTEN (type))
24926 /* Fill in the bound of variable-length fields in late dwarf if
24927 still incomplete. */
24928 if (!early_dwarf && variably_modified_type_p (type, NULL))
24929 for (tree member = TYPE_FIELDS (type);
24930 member;
24931 member = DECL_CHAIN (member))
24932 fill_variable_array_bounds (TREE_TYPE (member));
24933 return;
24936 dw_die_ref type_die = lookup_type_die (type);
24937 dw_die_ref scope_die = 0;
24938 int nested = 0;
24939 int complete = (TYPE_SIZE (type)
24940 && (! TYPE_STUB_DECL (type)
24941 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
24942 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
24943 complete = complete && should_emit_struct_debug (type, usage);
24945 if (type_die && ! complete)
24946 return;
24948 if (TYPE_CONTEXT (type) != NULL_TREE
24949 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
24950 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
24951 nested = 1;
24953 scope_die = scope_die_for (type, context_die);
24955 /* Generate child dies for template paramaters. */
24956 if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
24957 schedule_generic_params_dies_gen (type);
24959 if (! type_die || (nested && is_cu_die (scope_die)))
24960 /* First occurrence of type or toplevel definition of nested class. */
24962 dw_die_ref old_die = type_die;
24964 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
24965 ? record_type_tag (type) : DW_TAG_union_type,
24966 scope_die, type);
24967 equate_type_number_to_die (type, type_die);
24968 if (old_die)
24969 add_AT_specification (type_die, old_die);
24970 else
24971 add_name_attribute (type_die, type_tag (type));
24973 else
24974 remove_AT (type_die, DW_AT_declaration);
24976 /* If this type has been completed, then give it a byte_size attribute and
24977 then give a list of members. */
24978 if (complete && !ns_decl)
24980 /* Prevent infinite recursion in cases where the type of some member of
24981 this type is expressed in terms of this type itself. */
24982 TREE_ASM_WRITTEN (type) = 1;
24983 add_byte_size_attribute (type_die, type);
24984 add_alignment_attribute (type_die, type);
24985 if (TYPE_STUB_DECL (type) != NULL_TREE)
24987 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
24988 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
24991 /* If the first reference to this type was as the return type of an
24992 inline function, then it may not have a parent. Fix this now. */
24993 if (type_die->die_parent == NULL)
24994 add_child_die (scope_die, type_die);
24996 push_decl_scope (type);
24997 gen_member_die (type, type_die);
24998 pop_decl_scope ();
25000 add_gnat_descriptive_type_attribute (type_die, type, context_die);
25001 if (TYPE_ARTIFICIAL (type))
25002 add_AT_flag (type_die, DW_AT_artificial, 1);
25004 /* GNU extension: Record what type our vtable lives in. */
25005 if (TYPE_VFIELD (type))
25007 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
25009 gen_type_die (vtype, context_die);
25010 add_AT_die_ref (type_die, DW_AT_containing_type,
25011 lookup_type_die (vtype));
25014 else
25016 add_AT_flag (type_die, DW_AT_declaration, 1);
25018 /* We don't need to do this for function-local types. */
25019 if (TYPE_STUB_DECL (type)
25020 && ! decl_function_context (TYPE_STUB_DECL (type)))
25021 vec_safe_push (incomplete_types, type);
25024 if (get_AT (type_die, DW_AT_name))
25025 add_pubtype (type, type_die);
25028 /* Generate a DIE for a subroutine _type_. */
25030 static void
25031 gen_subroutine_type_die (tree type, dw_die_ref context_die)
25033 tree return_type = TREE_TYPE (type);
25034 dw_die_ref subr_die
25035 = new_die (DW_TAG_subroutine_type,
25036 scope_die_for (type, context_die), type);
25038 equate_type_number_to_die (type, subr_die);
25039 add_prototyped_attribute (subr_die, type);
25040 add_type_attribute (subr_die, return_type, TYPE_UNQUALIFIED, false,
25041 context_die);
25042 add_alignment_attribute (subr_die, type);
25043 gen_formal_types_die (type, subr_die);
25045 if (get_AT (subr_die, DW_AT_name))
25046 add_pubtype (type, subr_die);
25047 if ((dwarf_version >= 5 || !dwarf_strict)
25048 && lang_hooks.types.type_dwarf_attribute (type, DW_AT_reference) != -1)
25049 add_AT_flag (subr_die, DW_AT_reference, 1);
25050 if ((dwarf_version >= 5 || !dwarf_strict)
25051 && lang_hooks.types.type_dwarf_attribute (type,
25052 DW_AT_rvalue_reference) != -1)
25053 add_AT_flag (subr_die, DW_AT_rvalue_reference, 1);
25056 /* Generate a DIE for a type definition. */
25058 static void
25059 gen_typedef_die (tree decl, dw_die_ref context_die)
25061 dw_die_ref type_die;
25062 tree type;
25064 if (TREE_ASM_WRITTEN (decl))
25066 if (DECL_ORIGINAL_TYPE (decl))
25067 fill_variable_array_bounds (DECL_ORIGINAL_TYPE (decl));
25068 return;
25071 /* As we avoid creating DIEs for local typedefs (see decl_ultimate_origin
25072 checks in process_scope_var and modified_type_die), this should be called
25073 only for original types. */
25074 gcc_assert (decl_ultimate_origin (decl) == NULL
25075 || decl_ultimate_origin (decl) == decl);
25077 TREE_ASM_WRITTEN (decl) = 1;
25078 type_die = new_die (DW_TAG_typedef, context_die, decl);
25080 add_name_and_src_coords_attributes (type_die, decl);
25081 if (DECL_ORIGINAL_TYPE (decl))
25083 type = DECL_ORIGINAL_TYPE (decl);
25084 if (type == error_mark_node)
25085 return;
25087 gcc_assert (type != TREE_TYPE (decl));
25088 equate_type_number_to_die (TREE_TYPE (decl), type_die);
25090 else
25092 type = TREE_TYPE (decl);
25093 if (type == error_mark_node)
25094 return;
25096 if (is_naming_typedef_decl (TYPE_NAME (type)))
25098 /* Here, we are in the case of decl being a typedef naming
25099 an anonymous type, e.g:
25100 typedef struct {...} foo;
25101 In that case TREE_TYPE (decl) is not a typedef variant
25102 type and TYPE_NAME of the anonymous type is set to the
25103 TYPE_DECL of the typedef. This construct is emitted by
25104 the C++ FE.
25106 TYPE is the anonymous struct named by the typedef
25107 DECL. As we need the DW_AT_type attribute of the
25108 DW_TAG_typedef to point to the DIE of TYPE, let's
25109 generate that DIE right away. add_type_attribute
25110 called below will then pick (via lookup_type_die) that
25111 anonymous struct DIE. */
25112 if (!TREE_ASM_WRITTEN (type))
25113 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
25115 /* This is a GNU Extension. We are adding a
25116 DW_AT_linkage_name attribute to the DIE of the
25117 anonymous struct TYPE. The value of that attribute
25118 is the name of the typedef decl naming the anonymous
25119 struct. This greatly eases the work of consumers of
25120 this debug info. */
25121 add_linkage_name_raw (lookup_type_die (type), decl);
25125 add_type_attribute (type_die, type, decl_quals (decl), false,
25126 context_die);
25128 if (is_naming_typedef_decl (decl))
25129 /* We want that all subsequent calls to lookup_type_die with
25130 TYPE in argument yield the DW_TAG_typedef we have just
25131 created. */
25132 equate_type_number_to_die (type, type_die);
25134 add_alignment_attribute (type_die, TREE_TYPE (decl));
25136 add_accessibility_attribute (type_die, decl);
25138 if (DECL_ABSTRACT_P (decl))
25139 equate_decl_number_to_die (decl, type_die);
25141 if (get_AT (type_die, DW_AT_name))
25142 add_pubtype (decl, type_die);
25145 /* Generate a DIE for a struct, class, enum or union type. */
25147 static void
25148 gen_tagged_type_die (tree type,
25149 dw_die_ref context_die,
25150 enum debug_info_usage usage)
25152 int need_pop;
25154 if (type == NULL_TREE
25155 || !is_tagged_type (type))
25156 return;
25158 if (TREE_ASM_WRITTEN (type))
25159 need_pop = 0;
25160 /* If this is a nested type whose containing class hasn't been written
25161 out yet, writing it out will cover this one, too. This does not apply
25162 to instantiations of member class templates; they need to be added to
25163 the containing class as they are generated. FIXME: This hurts the
25164 idea of combining type decls from multiple TUs, since we can't predict
25165 what set of template instantiations we'll get. */
25166 else if (TYPE_CONTEXT (type)
25167 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
25168 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
25170 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
25172 if (TREE_ASM_WRITTEN (type))
25173 return;
25175 /* If that failed, attach ourselves to the stub. */
25176 push_decl_scope (TYPE_CONTEXT (type));
25177 context_die = lookup_type_die (TYPE_CONTEXT (type));
25178 need_pop = 1;
25180 else if (TYPE_CONTEXT (type) != NULL_TREE
25181 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
25183 /* If this type is local to a function that hasn't been written
25184 out yet, use a NULL context for now; it will be fixed up in
25185 decls_for_scope. */
25186 context_die = lookup_decl_die (TYPE_CONTEXT (type));
25187 /* A declaration DIE doesn't count; nested types need to go in the
25188 specification. */
25189 if (context_die && is_declaration_die (context_die))
25190 context_die = NULL;
25191 need_pop = 0;
25193 else
25195 context_die = declare_in_namespace (type, context_die);
25196 need_pop = 0;
25199 if (TREE_CODE (type) == ENUMERAL_TYPE)
25201 /* This might have been written out by the call to
25202 declare_in_namespace. */
25203 if (!TREE_ASM_WRITTEN (type))
25204 gen_enumeration_type_die (type, context_die);
25206 else
25207 gen_struct_or_union_type_die (type, context_die, usage);
25209 if (need_pop)
25210 pop_decl_scope ();
25212 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
25213 it up if it is ever completed. gen_*_type_die will set it for us
25214 when appropriate. */
25217 /* Generate a type description DIE. */
25219 static void
25220 gen_type_die_with_usage (tree type, dw_die_ref context_die,
25221 enum debug_info_usage usage)
25223 struct array_descr_info info;
25225 if (type == NULL_TREE || type == error_mark_node)
25226 return;
25228 if (flag_checking && type)
25229 verify_type (type);
25231 if (TYPE_NAME (type) != NULL_TREE
25232 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
25233 && is_redundant_typedef (TYPE_NAME (type))
25234 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
25235 /* The DECL of this type is a typedef we don't want to emit debug
25236 info for but we want debug info for its underlying typedef.
25237 This can happen for e.g, the injected-class-name of a C++
25238 type. */
25239 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
25241 /* If TYPE is a typedef type variant, let's generate debug info
25242 for the parent typedef which TYPE is a type of. */
25243 if (typedef_variant_p (type))
25245 if (TREE_ASM_WRITTEN (type))
25246 return;
25248 tree name = TYPE_NAME (type);
25249 tree origin = decl_ultimate_origin (name);
25250 if (origin != NULL && origin != name)
25252 gen_decl_die (origin, NULL, NULL, context_die);
25253 return;
25256 /* Prevent broken recursion; we can't hand off to the same type. */
25257 gcc_assert (DECL_ORIGINAL_TYPE (name) != type);
25259 /* Give typedefs the right scope. */
25260 context_die = scope_die_for (type, context_die);
25262 TREE_ASM_WRITTEN (type) = 1;
25264 gen_decl_die (name, NULL, NULL, context_die);
25265 return;
25268 /* If type is an anonymous tagged type named by a typedef, let's
25269 generate debug info for the typedef. */
25270 if (is_naming_typedef_decl (TYPE_NAME (type)))
25272 /* Use the DIE of the containing namespace as the parent DIE of
25273 the type description DIE we want to generate. */
25274 if (DECL_CONTEXT (TYPE_NAME (type))
25275 && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
25276 context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
25278 gen_decl_die (TYPE_NAME (type), NULL, NULL, context_die);
25279 return;
25282 if (lang_hooks.types.get_debug_type)
25284 tree debug_type = lang_hooks.types.get_debug_type (type);
25286 if (debug_type != NULL_TREE && debug_type != type)
25288 gen_type_die_with_usage (debug_type, context_die, usage);
25289 return;
25293 /* We are going to output a DIE to represent the unqualified version
25294 of this type (i.e. without any const or volatile qualifiers) so
25295 get the main variant (i.e. the unqualified version) of this type
25296 now. (Vectors and arrays are special because the debugging info is in the
25297 cloned type itself. Similarly function/method types can contain extra
25298 ref-qualification). */
25299 if (TREE_CODE (type) == FUNCTION_TYPE
25300 || TREE_CODE (type) == METHOD_TYPE)
25302 /* For function/method types, can't use type_main_variant here,
25303 because that can have different ref-qualifiers for C++,
25304 but try to canonicalize. */
25305 tree main = TYPE_MAIN_VARIANT (type);
25306 for (tree t = main; t; t = TYPE_NEXT_VARIANT (t))
25307 if (TYPE_QUALS_NO_ADDR_SPACE (t) == 0
25308 && check_base_type (t, main)
25309 && check_lang_type (t, type))
25311 type = t;
25312 break;
25315 else if (TREE_CODE (type) != VECTOR_TYPE
25316 && TREE_CODE (type) != ARRAY_TYPE)
25317 type = type_main_variant (type);
25319 /* If this is an array type with hidden descriptor, handle it first. */
25320 if (!TREE_ASM_WRITTEN (type)
25321 && lang_hooks.types.get_array_descr_info)
25323 memset (&info, 0, sizeof (info));
25324 if (lang_hooks.types.get_array_descr_info (type, &info))
25326 /* Fortran sometimes emits array types with no dimension. */
25327 gcc_assert (info.ndimensions >= 0
25328 && (info.ndimensions
25329 <= DWARF2OUT_ARRAY_DESCR_INFO_MAX_DIMEN));
25330 gen_descr_array_type_die (type, &info, context_die);
25331 TREE_ASM_WRITTEN (type) = 1;
25332 return;
25336 if (TREE_ASM_WRITTEN (type))
25338 /* Variable-length types may be incomplete even if
25339 TREE_ASM_WRITTEN. For such types, fall through to
25340 gen_array_type_die() and possibly fill in
25341 DW_AT_{upper,lower}_bound attributes. */
25342 if ((TREE_CODE (type) != ARRAY_TYPE
25343 && TREE_CODE (type) != RECORD_TYPE
25344 && TREE_CODE (type) != UNION_TYPE
25345 && TREE_CODE (type) != QUAL_UNION_TYPE)
25346 || !variably_modified_type_p (type, NULL))
25347 return;
25350 switch (TREE_CODE (type))
25352 case ERROR_MARK:
25353 break;
25355 case POINTER_TYPE:
25356 case REFERENCE_TYPE:
25357 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
25358 ensures that the gen_type_die recursion will terminate even if the
25359 type is recursive. Recursive types are possible in Ada. */
25360 /* ??? We could perhaps do this for all types before the switch
25361 statement. */
25362 TREE_ASM_WRITTEN (type) = 1;
25364 /* For these types, all that is required is that we output a DIE (or a
25365 set of DIEs) to represent the "basis" type. */
25366 gen_type_die_with_usage (TREE_TYPE (type), context_die,
25367 DINFO_USAGE_IND_USE);
25368 break;
25370 case OFFSET_TYPE:
25371 /* This code is used for C++ pointer-to-data-member types.
25372 Output a description of the relevant class type. */
25373 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
25374 DINFO_USAGE_IND_USE);
25376 /* Output a description of the type of the object pointed to. */
25377 gen_type_die_with_usage (TREE_TYPE (type), context_die,
25378 DINFO_USAGE_IND_USE);
25380 /* Now output a DIE to represent this pointer-to-data-member type
25381 itself. */
25382 gen_ptr_to_mbr_type_die (type, context_die);
25383 break;
25385 case FUNCTION_TYPE:
25386 /* Force out return type (in case it wasn't forced out already). */
25387 gen_type_die_with_usage (TREE_TYPE (type), context_die,
25388 DINFO_USAGE_DIR_USE);
25389 gen_subroutine_type_die (type, context_die);
25390 break;
25392 case METHOD_TYPE:
25393 /* Force out return type (in case it wasn't forced out already). */
25394 gen_type_die_with_usage (TREE_TYPE (type), context_die,
25395 DINFO_USAGE_DIR_USE);
25396 gen_subroutine_type_die (type, context_die);
25397 break;
25399 case ARRAY_TYPE:
25400 case VECTOR_TYPE:
25401 gen_array_type_die (type, context_die);
25402 break;
25404 case ENUMERAL_TYPE:
25405 case RECORD_TYPE:
25406 case UNION_TYPE:
25407 case QUAL_UNION_TYPE:
25408 gen_tagged_type_die (type, context_die, usage);
25409 return;
25411 case VOID_TYPE:
25412 case INTEGER_TYPE:
25413 case REAL_TYPE:
25414 case FIXED_POINT_TYPE:
25415 case COMPLEX_TYPE:
25416 case BOOLEAN_TYPE:
25417 case POINTER_BOUNDS_TYPE:
25418 /* No DIEs needed for fundamental types. */
25419 break;
25421 case NULLPTR_TYPE:
25422 case LANG_TYPE:
25423 /* Just use DW_TAG_unspecified_type. */
25425 dw_die_ref type_die = lookup_type_die (type);
25426 if (type_die == NULL)
25428 tree name = TYPE_IDENTIFIER (type);
25429 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (),
25430 type);
25431 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
25432 equate_type_number_to_die (type, type_die);
25435 break;
25437 default:
25438 if (is_cxx_auto (type))
25440 tree name = TYPE_IDENTIFIER (type);
25441 dw_die_ref *die = (name == get_identifier ("auto")
25442 ? &auto_die : &decltype_auto_die);
25443 if (!*die)
25445 *die = new_die (DW_TAG_unspecified_type,
25446 comp_unit_die (), NULL_TREE);
25447 add_name_attribute (*die, IDENTIFIER_POINTER (name));
25449 equate_type_number_to_die (type, *die);
25450 break;
25452 gcc_unreachable ();
25455 TREE_ASM_WRITTEN (type) = 1;
25458 static void
25459 gen_type_die (tree type, dw_die_ref context_die)
25461 if (type != error_mark_node)
25463 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
25464 if (flag_checking)
25466 dw_die_ref die = lookup_type_die (type);
25467 if (die)
25468 check_die (die);
25473 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
25474 things which are local to the given block. */
25476 static void
25477 gen_block_die (tree stmt, dw_die_ref context_die)
25479 int must_output_die = 0;
25480 bool inlined_func;
25482 /* Ignore blocks that are NULL. */
25483 if (stmt == NULL_TREE)
25484 return;
25486 inlined_func = inlined_function_outer_scope_p (stmt);
25488 /* If the block is one fragment of a non-contiguous block, do not
25489 process the variables, since they will have been done by the
25490 origin block. Do process subblocks. */
25491 if (BLOCK_FRAGMENT_ORIGIN (stmt))
25493 tree sub;
25495 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
25496 gen_block_die (sub, context_die);
25498 return;
25501 /* Determine if we need to output any Dwarf DIEs at all to represent this
25502 block. */
25503 if (inlined_func)
25504 /* The outer scopes for inlinings *must* always be represented. We
25505 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
25506 must_output_die = 1;
25507 else
25509 /* Determine if this block directly contains any "significant"
25510 local declarations which we will need to output DIEs for. */
25511 if (debug_info_level > DINFO_LEVEL_TERSE)
25512 /* We are not in terse mode so *any* local declaration counts
25513 as being a "significant" one. */
25514 must_output_die = ((BLOCK_VARS (stmt) != NULL
25515 || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
25516 && (TREE_USED (stmt)
25517 || TREE_ASM_WRITTEN (stmt)
25518 || BLOCK_ABSTRACT (stmt)));
25519 else if ((TREE_USED (stmt)
25520 || TREE_ASM_WRITTEN (stmt)
25521 || BLOCK_ABSTRACT (stmt))
25522 && !dwarf2out_ignore_block (stmt))
25523 must_output_die = 1;
25526 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
25527 DIE for any block which contains no significant local declarations at
25528 all. Rather, in such cases we just call `decls_for_scope' so that any
25529 needed Dwarf info for any sub-blocks will get properly generated. Note
25530 that in terse mode, our definition of what constitutes a "significant"
25531 local declaration gets restricted to include only inlined function
25532 instances and local (nested) function definitions. */
25533 if (must_output_die)
25535 if (inlined_func)
25537 /* If STMT block is abstract, that means we have been called
25538 indirectly from dwarf2out_abstract_function.
25539 That function rightfully marks the descendent blocks (of
25540 the abstract function it is dealing with) as being abstract,
25541 precisely to prevent us from emitting any
25542 DW_TAG_inlined_subroutine DIE as a descendent
25543 of an abstract function instance. So in that case, we should
25544 not call gen_inlined_subroutine_die.
25546 Later though, when cgraph asks dwarf2out to emit info
25547 for the concrete instance of the function decl into which
25548 the concrete instance of STMT got inlined, the later will lead
25549 to the generation of a DW_TAG_inlined_subroutine DIE. */
25550 if (! BLOCK_ABSTRACT (stmt))
25551 gen_inlined_subroutine_die (stmt, context_die);
25553 else
25554 gen_lexical_block_die (stmt, context_die);
25556 else
25557 decls_for_scope (stmt, context_die);
25560 /* Process variable DECL (or variable with origin ORIGIN) within
25561 block STMT and add it to CONTEXT_DIE. */
25562 static void
25563 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
25565 dw_die_ref die;
25566 tree decl_or_origin = decl ? decl : origin;
25568 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
25569 die = lookup_decl_die (decl_or_origin);
25570 else if (TREE_CODE (decl_or_origin) == TYPE_DECL)
25572 if (TYPE_DECL_IS_STUB (decl_or_origin))
25573 die = lookup_type_die (TREE_TYPE (decl_or_origin));
25574 else
25575 die = lookup_decl_die (decl_or_origin);
25576 /* Avoid re-creating the DIE late if it was optimized as unused early. */
25577 if (! die && ! early_dwarf)
25578 return;
25580 else
25581 die = NULL;
25583 /* Avoid creating DIEs for local typedefs and concrete static variables that
25584 will only be pruned later. */
25585 if ((origin || decl_ultimate_origin (decl))
25586 && (TREE_CODE (decl_or_origin) == TYPE_DECL
25587 || (VAR_P (decl_or_origin) && TREE_STATIC (decl_or_origin))))
25589 origin = decl_ultimate_origin (decl_or_origin);
25590 if (decl && VAR_P (decl) && die != NULL)
25592 die = lookup_decl_die (origin);
25593 if (die != NULL)
25594 equate_decl_number_to_die (decl, die);
25596 return;
25599 if (die != NULL && die->die_parent == NULL)
25600 add_child_die (context_die, die);
25601 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
25603 if (early_dwarf)
25604 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
25605 stmt, context_die);
25607 else
25609 if (decl && DECL_P (decl))
25611 die = lookup_decl_die (decl);
25613 /* Early created DIEs do not have a parent as the decls refer
25614 to the function as DECL_CONTEXT rather than the BLOCK. */
25615 if (die && die->die_parent == NULL)
25617 gcc_assert (in_lto_p);
25618 add_child_die (context_die, die);
25622 gen_decl_die (decl, origin, NULL, context_die);
25626 /* Generate all of the decls declared within a given scope and (recursively)
25627 all of its sub-blocks. */
25629 static void
25630 decls_for_scope (tree stmt, dw_die_ref context_die)
25632 tree decl;
25633 unsigned int i;
25634 tree subblocks;
25636 /* Ignore NULL blocks. */
25637 if (stmt == NULL_TREE)
25638 return;
25640 /* Output the DIEs to represent all of the data objects and typedefs
25641 declared directly within this block but not within any nested
25642 sub-blocks. Also, nested function and tag DIEs have been
25643 generated with a parent of NULL; fix that up now. We don't
25644 have to do this if we're at -g1. */
25645 if (debug_info_level > DINFO_LEVEL_TERSE)
25647 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
25648 process_scope_var (stmt, decl, NULL_TREE, context_die);
25649 /* BLOCK_NONLOCALIZED_VARs simply generate DIE stubs with abstract
25650 origin - avoid doing this twice as we have no good way to see
25651 if we've done it once already. */
25652 if (! early_dwarf)
25653 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
25655 decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
25656 if (decl == current_function_decl)
25657 /* Ignore declarations of the current function, while they
25658 are declarations, gen_subprogram_die would treat them
25659 as definitions again, because they are equal to
25660 current_function_decl and endlessly recurse. */;
25661 else if (TREE_CODE (decl) == FUNCTION_DECL)
25662 process_scope_var (stmt, decl, NULL_TREE, context_die);
25663 else
25664 process_scope_var (stmt, NULL_TREE, decl, context_die);
25668 /* Even if we're at -g1, we need to process the subblocks in order to get
25669 inlined call information. */
25671 /* Output the DIEs to represent all sub-blocks (and the items declared
25672 therein) of this block. */
25673 for (subblocks = BLOCK_SUBBLOCKS (stmt);
25674 subblocks != NULL;
25675 subblocks = BLOCK_CHAIN (subblocks))
25676 gen_block_die (subblocks, context_die);
25679 /* Is this a typedef we can avoid emitting? */
25681 bool
25682 is_redundant_typedef (const_tree decl)
25684 if (TYPE_DECL_IS_STUB (decl))
25685 return true;
25687 if (DECL_ARTIFICIAL (decl)
25688 && DECL_CONTEXT (decl)
25689 && is_tagged_type (DECL_CONTEXT (decl))
25690 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
25691 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
25692 /* Also ignore the artificial member typedef for the class name. */
25693 return true;
25695 return false;
25698 /* Return TRUE if TYPE is a typedef that names a type for linkage
25699 purposes. This kind of typedefs is produced by the C++ FE for
25700 constructs like:
25702 typedef struct {...} foo;
25704 In that case, there is no typedef variant type produced for foo.
25705 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
25706 struct type. */
25708 static bool
25709 is_naming_typedef_decl (const_tree decl)
25711 if (decl == NULL_TREE
25712 || TREE_CODE (decl) != TYPE_DECL
25713 || DECL_NAMELESS (decl)
25714 || !is_tagged_type (TREE_TYPE (decl))
25715 || DECL_IS_BUILTIN (decl)
25716 || is_redundant_typedef (decl)
25717 /* It looks like Ada produces TYPE_DECLs that are very similar
25718 to C++ naming typedefs but that have different
25719 semantics. Let's be specific to c++ for now. */
25720 || !is_cxx (decl))
25721 return FALSE;
25723 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
25724 && TYPE_NAME (TREE_TYPE (decl)) == decl
25725 && (TYPE_STUB_DECL (TREE_TYPE (decl))
25726 != TYPE_NAME (TREE_TYPE (decl))));
25729 /* Looks up the DIE for a context. */
25731 static inline dw_die_ref
25732 lookup_context_die (tree context)
25734 if (context)
25736 /* Find die that represents this context. */
25737 if (TYPE_P (context))
25739 context = TYPE_MAIN_VARIANT (context);
25740 dw_die_ref ctx = lookup_type_die (context);
25741 if (!ctx)
25742 return NULL;
25743 return strip_naming_typedef (context, ctx);
25745 else
25746 return lookup_decl_die (context);
25748 return comp_unit_die ();
25751 /* Returns the DIE for a context. */
25753 static inline dw_die_ref
25754 get_context_die (tree context)
25756 if (context)
25758 /* Find die that represents this context. */
25759 if (TYPE_P (context))
25761 context = TYPE_MAIN_VARIANT (context);
25762 return strip_naming_typedef (context, force_type_die (context));
25764 else
25765 return force_decl_die (context);
25767 return comp_unit_die ();
25770 /* Returns the DIE for decl. A DIE will always be returned. */
25772 static dw_die_ref
25773 force_decl_die (tree decl)
25775 dw_die_ref decl_die;
25776 unsigned saved_external_flag;
25777 tree save_fn = NULL_TREE;
25778 decl_die = lookup_decl_die (decl);
25779 if (!decl_die)
25781 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
25783 decl_die = lookup_decl_die (decl);
25784 if (decl_die)
25785 return decl_die;
25787 switch (TREE_CODE (decl))
25789 case FUNCTION_DECL:
25790 /* Clear current_function_decl, so that gen_subprogram_die thinks
25791 that this is a declaration. At this point, we just want to force
25792 declaration die. */
25793 save_fn = current_function_decl;
25794 current_function_decl = NULL_TREE;
25795 gen_subprogram_die (decl, context_die);
25796 current_function_decl = save_fn;
25797 break;
25799 case VAR_DECL:
25800 /* Set external flag to force declaration die. Restore it after
25801 gen_decl_die() call. */
25802 saved_external_flag = DECL_EXTERNAL (decl);
25803 DECL_EXTERNAL (decl) = 1;
25804 gen_decl_die (decl, NULL, NULL, context_die);
25805 DECL_EXTERNAL (decl) = saved_external_flag;
25806 break;
25808 case NAMESPACE_DECL:
25809 if (dwarf_version >= 3 || !dwarf_strict)
25810 dwarf2out_decl (decl);
25811 else
25812 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
25813 decl_die = comp_unit_die ();
25814 break;
25816 case TRANSLATION_UNIT_DECL:
25817 decl_die = comp_unit_die ();
25818 break;
25820 default:
25821 gcc_unreachable ();
25824 /* We should be able to find the DIE now. */
25825 if (!decl_die)
25826 decl_die = lookup_decl_die (decl);
25827 gcc_assert (decl_die);
25830 return decl_die;
25833 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
25834 always returned. */
25836 static dw_die_ref
25837 force_type_die (tree type)
25839 dw_die_ref type_die;
25841 type_die = lookup_type_die (type);
25842 if (!type_die)
25844 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
25846 type_die = modified_type_die (type, TYPE_QUALS_NO_ADDR_SPACE (type),
25847 false, context_die);
25848 gcc_assert (type_die);
25850 return type_die;
25853 /* Force out any required namespaces to be able to output DECL,
25854 and return the new context_die for it, if it's changed. */
25856 static dw_die_ref
25857 setup_namespace_context (tree thing, dw_die_ref context_die)
25859 tree context = (DECL_P (thing)
25860 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
25861 if (context && TREE_CODE (context) == NAMESPACE_DECL)
25862 /* Force out the namespace. */
25863 context_die = force_decl_die (context);
25865 return context_die;
25868 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
25869 type) within its namespace, if appropriate.
25871 For compatibility with older debuggers, namespace DIEs only contain
25872 declarations; all definitions are emitted at CU scope, with
25873 DW_AT_specification pointing to the declaration (like with class
25874 members). */
25876 static dw_die_ref
25877 declare_in_namespace (tree thing, dw_die_ref context_die)
25879 dw_die_ref ns_context;
25881 if (debug_info_level <= DINFO_LEVEL_TERSE)
25882 return context_die;
25884 /* External declarations in the local scope only need to be emitted
25885 once, not once in the namespace and once in the scope.
25887 This avoids declaring the `extern' below in the
25888 namespace DIE as well as in the innermost scope:
25890 namespace S
25892 int i=5;
25893 int foo()
25895 int i=8;
25896 extern int i;
25897 return i;
25901 if (DECL_P (thing) && DECL_EXTERNAL (thing) && local_scope_p (context_die))
25902 return context_die;
25904 /* If this decl is from an inlined function, then don't try to emit it in its
25905 namespace, as we will get confused. It would have already been emitted
25906 when the abstract instance of the inline function was emitted anyways. */
25907 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
25908 return context_die;
25910 ns_context = setup_namespace_context (thing, context_die);
25912 if (ns_context != context_die)
25914 if (is_fortran ())
25915 return ns_context;
25916 if (DECL_P (thing))
25917 gen_decl_die (thing, NULL, NULL, ns_context);
25918 else
25919 gen_type_die (thing, ns_context);
25921 return context_die;
25924 /* Generate a DIE for a namespace or namespace alias. */
25926 static void
25927 gen_namespace_die (tree decl, dw_die_ref context_die)
25929 dw_die_ref namespace_die;
25931 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
25932 they are an alias of. */
25933 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
25935 /* Output a real namespace or module. */
25936 context_die = setup_namespace_context (decl, comp_unit_die ());
25937 namespace_die = new_die (is_fortran ()
25938 ? DW_TAG_module : DW_TAG_namespace,
25939 context_die, decl);
25940 /* For Fortran modules defined in different CU don't add src coords. */
25941 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
25943 const char *name = dwarf2_name (decl, 0);
25944 if (name)
25945 add_name_attribute (namespace_die, name);
25947 else
25948 add_name_and_src_coords_attributes (namespace_die, decl);
25949 if (DECL_EXTERNAL (decl))
25950 add_AT_flag (namespace_die, DW_AT_declaration, 1);
25951 equate_decl_number_to_die (decl, namespace_die);
25953 else
25955 /* Output a namespace alias. */
25957 /* Force out the namespace we are an alias of, if necessary. */
25958 dw_die_ref origin_die
25959 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
25961 if (DECL_FILE_SCOPE_P (decl)
25962 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
25963 context_die = setup_namespace_context (decl, comp_unit_die ());
25964 /* Now create the namespace alias DIE. */
25965 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
25966 add_name_and_src_coords_attributes (namespace_die, decl);
25967 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
25968 equate_decl_number_to_die (decl, namespace_die);
25970 if ((dwarf_version >= 5 || !dwarf_strict)
25971 && lang_hooks.decls.decl_dwarf_attribute (decl,
25972 DW_AT_export_symbols) == 1)
25973 add_AT_flag (namespace_die, DW_AT_export_symbols, 1);
25975 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
25976 if (want_pubnames ())
25977 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
25980 /* Generate Dwarf debug information for a decl described by DECL.
25981 The return value is currently only meaningful for PARM_DECLs,
25982 for all other decls it returns NULL.
25984 If DECL is a FIELD_DECL, CTX is required: see the comment for VLR_CONTEXT.
25985 It can be NULL otherwise. */
25987 static dw_die_ref
25988 gen_decl_die (tree decl, tree origin, struct vlr_context *ctx,
25989 dw_die_ref context_die)
25991 tree decl_or_origin = decl ? decl : origin;
25992 tree class_origin = NULL, ultimate_origin;
25994 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
25995 return NULL;
25997 /* Ignore pointer bounds decls. */
25998 if (DECL_P (decl_or_origin)
25999 && TREE_TYPE (decl_or_origin)
26000 && POINTER_BOUNDS_P (decl_or_origin))
26001 return NULL;
26003 switch (TREE_CODE (decl_or_origin))
26005 case ERROR_MARK:
26006 break;
26008 case CONST_DECL:
26009 if (!is_fortran () && !is_ada ())
26011 /* The individual enumerators of an enum type get output when we output
26012 the Dwarf representation of the relevant enum type itself. */
26013 break;
26016 /* Emit its type. */
26017 gen_type_die (TREE_TYPE (decl), context_die);
26019 /* And its containing namespace. */
26020 context_die = declare_in_namespace (decl, context_die);
26022 gen_const_die (decl, context_die);
26023 break;
26025 case FUNCTION_DECL:
26026 #if 0
26027 /* FIXME */
26028 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
26029 on local redeclarations of global functions. That seems broken. */
26030 if (current_function_decl != decl)
26031 /* This is only a declaration. */;
26032 #endif
26034 /* We should have abstract copies already and should not generate
26035 stray type DIEs in late LTO dumping. */
26036 if (! early_dwarf)
26039 /* If we're emitting a clone, emit info for the abstract instance. */
26040 else if (origin || DECL_ORIGIN (decl) != decl)
26041 dwarf2out_abstract_function (origin
26042 ? DECL_ORIGIN (origin)
26043 : DECL_ABSTRACT_ORIGIN (decl));
26045 /* If we're emitting a possibly inlined function emit it as
26046 abstract instance. */
26047 else if (cgraph_function_possibly_inlined_p (decl)
26048 && ! DECL_ABSTRACT_P (decl)
26049 && ! class_or_namespace_scope_p (context_die)
26050 /* dwarf2out_abstract_function won't emit a die if this is just
26051 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
26052 that case, because that works only if we have a die. */
26053 && DECL_INITIAL (decl) != NULL_TREE)
26054 dwarf2out_abstract_function (decl);
26056 /* Otherwise we're emitting the primary DIE for this decl. */
26057 else if (debug_info_level > DINFO_LEVEL_TERSE)
26059 /* Before we describe the FUNCTION_DECL itself, make sure that we
26060 have its containing type. */
26061 if (!origin)
26062 origin = decl_class_context (decl);
26063 if (origin != NULL_TREE)
26064 gen_type_die (origin, context_die);
26066 /* And its return type. */
26067 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
26069 /* And its virtual context. */
26070 if (DECL_VINDEX (decl) != NULL_TREE)
26071 gen_type_die (DECL_CONTEXT (decl), context_die);
26073 /* Make sure we have a member DIE for decl. */
26074 if (origin != NULL_TREE)
26075 gen_type_die_for_member (origin, decl, context_die);
26077 /* And its containing namespace. */
26078 context_die = declare_in_namespace (decl, context_die);
26081 /* Now output a DIE to represent the function itself. */
26082 if (decl)
26083 gen_subprogram_die (decl, context_die);
26084 break;
26086 case TYPE_DECL:
26087 /* If we are in terse mode, don't generate any DIEs to represent any
26088 actual typedefs. */
26089 if (debug_info_level <= DINFO_LEVEL_TERSE)
26090 break;
26092 /* In the special case of a TYPE_DECL node representing the declaration
26093 of some type tag, if the given TYPE_DECL is marked as having been
26094 instantiated from some other (original) TYPE_DECL node (e.g. one which
26095 was generated within the original definition of an inline function) we
26096 used to generate a special (abbreviated) DW_TAG_structure_type,
26097 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
26098 should be actually referencing those DIEs, as variable DIEs with that
26099 type would be emitted already in the abstract origin, so it was always
26100 removed during unused type prunning. Don't add anything in this
26101 case. */
26102 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
26103 break;
26105 if (is_redundant_typedef (decl))
26106 gen_type_die (TREE_TYPE (decl), context_die);
26107 else
26108 /* Output a DIE to represent the typedef itself. */
26109 gen_typedef_die (decl, context_die);
26110 break;
26112 case LABEL_DECL:
26113 if (debug_info_level >= DINFO_LEVEL_NORMAL)
26114 gen_label_die (decl, context_die);
26115 break;
26117 case VAR_DECL:
26118 case RESULT_DECL:
26119 /* If we are in terse mode, don't generate any DIEs to represent any
26120 variable declarations or definitions. */
26121 if (debug_info_level <= DINFO_LEVEL_TERSE)
26122 break;
26124 /* Avoid generating stray type DIEs during late dwarf dumping.
26125 All types have been dumped early. */
26126 if (early_dwarf
26127 /* ??? But in LTRANS we cannot annotate early created variably
26128 modified type DIEs without copying them and adjusting all
26129 references to them. Dump them again as happens for inlining
26130 which copies both the decl and the types. */
26131 /* ??? And even non-LTO needs to re-visit type DIEs to fill
26132 in VLA bound information for example. */
26133 || (decl && variably_modified_type_p (TREE_TYPE (decl),
26134 current_function_decl)))
26136 /* Output any DIEs that are needed to specify the type of this data
26137 object. */
26138 if (decl_by_reference_p (decl_or_origin))
26139 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
26140 else
26141 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
26144 if (early_dwarf)
26146 /* And its containing type. */
26147 class_origin = decl_class_context (decl_or_origin);
26148 if (class_origin != NULL_TREE)
26149 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
26151 /* And its containing namespace. */
26152 context_die = declare_in_namespace (decl_or_origin, context_die);
26155 /* Now output the DIE to represent the data object itself. This gets
26156 complicated because of the possibility that the VAR_DECL really
26157 represents an inlined instance of a formal parameter for an inline
26158 function. */
26159 ultimate_origin = decl_ultimate_origin (decl_or_origin);
26160 if (ultimate_origin != NULL_TREE
26161 && TREE_CODE (ultimate_origin) == PARM_DECL)
26162 gen_formal_parameter_die (decl, origin,
26163 true /* Emit name attribute. */,
26164 context_die);
26165 else
26166 gen_variable_die (decl, origin, context_die);
26167 break;
26169 case FIELD_DECL:
26170 gcc_assert (ctx != NULL && ctx->struct_type != NULL);
26171 /* Ignore the nameless fields that are used to skip bits but handle C++
26172 anonymous unions and structs. */
26173 if (DECL_NAME (decl) != NULL_TREE
26174 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
26175 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
26177 gen_type_die (member_declared_type (decl), context_die);
26178 gen_field_die (decl, ctx, context_die);
26180 break;
26182 case PARM_DECL:
26183 /* Avoid generating stray type DIEs during late dwarf dumping.
26184 All types have been dumped early. */
26185 if (early_dwarf
26186 /* ??? But in LTRANS we cannot annotate early created variably
26187 modified type DIEs without copying them and adjusting all
26188 references to them. Dump them again as happens for inlining
26189 which copies both the decl and the types. */
26190 /* ??? And even non-LTO needs to re-visit type DIEs to fill
26191 in VLA bound information for example. */
26192 || (decl && variably_modified_type_p (TREE_TYPE (decl),
26193 current_function_decl)))
26195 if (DECL_BY_REFERENCE (decl_or_origin))
26196 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
26197 else
26198 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
26200 return gen_formal_parameter_die (decl, origin,
26201 true /* Emit name attribute. */,
26202 context_die);
26204 case NAMESPACE_DECL:
26205 if (dwarf_version >= 3 || !dwarf_strict)
26206 gen_namespace_die (decl, context_die);
26207 break;
26209 case IMPORTED_DECL:
26210 dwarf2out_imported_module_or_decl_1 (decl, DECL_NAME (decl),
26211 DECL_CONTEXT (decl), context_die);
26212 break;
26214 case NAMELIST_DECL:
26215 gen_namelist_decl (DECL_NAME (decl), context_die,
26216 NAMELIST_DECL_ASSOCIATED_DECL (decl));
26217 break;
26219 default:
26220 /* Probably some frontend-internal decl. Assume we don't care. */
26221 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
26222 break;
26225 return NULL;
26228 /* Output initial debug information for global DECL. Called at the
26229 end of the parsing process.
26231 This is the initial debug generation process. As such, the DIEs
26232 generated may be incomplete. A later debug generation pass
26233 (dwarf2out_late_global_decl) will augment the information generated
26234 in this pass (e.g., with complete location info). */
26236 static void
26237 dwarf2out_early_global_decl (tree decl)
26239 set_early_dwarf s;
26241 /* gen_decl_die() will set DECL_ABSTRACT because
26242 cgraph_function_possibly_inlined_p() returns true. This is in
26243 turn will cause DW_AT_inline attributes to be set.
26245 This happens because at early dwarf generation, there is no
26246 cgraph information, causing cgraph_function_possibly_inlined_p()
26247 to return true. Trick cgraph_function_possibly_inlined_p()
26248 while we generate dwarf early. */
26249 bool save = symtab->global_info_ready;
26250 symtab->global_info_ready = true;
26252 /* We don't handle TYPE_DECLs. If required, they'll be reached via
26253 other DECLs and they can point to template types or other things
26254 that dwarf2out can't handle when done via dwarf2out_decl. */
26255 if (TREE_CODE (decl) != TYPE_DECL
26256 && TREE_CODE (decl) != PARM_DECL)
26258 if (TREE_CODE (decl) == FUNCTION_DECL)
26260 tree save_fndecl = current_function_decl;
26262 /* For nested functions, make sure we have DIEs for the parents first
26263 so that all nested DIEs are generated at the proper scope in the
26264 first shot. */
26265 tree context = decl_function_context (decl);
26266 if (context != NULL)
26268 dw_die_ref context_die = lookup_decl_die (context);
26269 current_function_decl = context;
26271 /* Avoid emitting DIEs multiple times, but still process CONTEXT
26272 enough so that it lands in its own context. This avoids type
26273 pruning issues later on. */
26274 if (context_die == NULL || is_declaration_die (context_die))
26275 dwarf2out_decl (context);
26278 /* Emit an abstract origin of a function first. This happens
26279 with C++ constructor clones for example and makes
26280 dwarf2out_abstract_function happy which requires the early
26281 DIE of the abstract instance to be present. */
26282 tree origin = DECL_ABSTRACT_ORIGIN (decl);
26283 dw_die_ref origin_die;
26284 if (origin != NULL
26285 /* Do not emit the DIE multiple times but make sure to
26286 process it fully here in case we just saw a declaration. */
26287 && ((origin_die = lookup_decl_die (origin)) == NULL
26288 || is_declaration_die (origin_die)))
26290 current_function_decl = origin;
26291 dwarf2out_decl (origin);
26294 /* Emit the DIE for decl but avoid doing that multiple times. */
26295 dw_die_ref old_die;
26296 if ((old_die = lookup_decl_die (decl)) == NULL
26297 || is_declaration_die (old_die))
26299 current_function_decl = decl;
26300 dwarf2out_decl (decl);
26303 current_function_decl = save_fndecl;
26305 else
26306 dwarf2out_decl (decl);
26308 symtab->global_info_ready = save;
26311 /* Return whether EXPR is an expression with the following pattern:
26312 INDIRECT_REF (NOP_EXPR (INTEGER_CST)). */
26314 static bool
26315 is_trivial_indirect_ref (tree expr)
26317 if (expr == NULL_TREE || TREE_CODE (expr) != INDIRECT_REF)
26318 return false;
26320 tree nop = TREE_OPERAND (expr, 0);
26321 if (nop == NULL_TREE || TREE_CODE (nop) != NOP_EXPR)
26322 return false;
26324 tree int_cst = TREE_OPERAND (nop, 0);
26325 return int_cst != NULL_TREE && TREE_CODE (int_cst) == INTEGER_CST;
26328 /* Output debug information for global decl DECL. Called from
26329 toplev.c after compilation proper has finished. */
26331 static void
26332 dwarf2out_late_global_decl (tree decl)
26334 /* Fill-in any location information we were unable to determine
26335 on the first pass. */
26336 if (VAR_P (decl) && !POINTER_BOUNDS_P (decl))
26338 dw_die_ref die = lookup_decl_die (decl);
26340 /* We may have to generate early debug late for LTO in case debug
26341 was not enabled at compile-time or the target doesn't support
26342 the LTO early debug scheme. */
26343 if (! die && in_lto_p)
26345 dwarf2out_decl (decl);
26346 die = lookup_decl_die (decl);
26349 if (die)
26351 /* We get called via the symtab code invoking late_global_decl
26352 for symbols that are optimized out.
26354 Do not add locations for those, except if they have a
26355 DECL_VALUE_EXPR, in which case they are relevant for debuggers.
26356 Still don't add a location if the DECL_VALUE_EXPR is not a trivial
26357 INDIRECT_REF expression, as this could generate relocations to
26358 text symbols in LTO object files, which is invalid. */
26359 varpool_node *node = varpool_node::get (decl);
26360 if ((! node || ! node->definition)
26361 && ! (DECL_HAS_VALUE_EXPR_P (decl)
26362 && is_trivial_indirect_ref (DECL_VALUE_EXPR (decl))))
26363 tree_add_const_value_attribute_for_decl (die, decl);
26364 else
26365 add_location_or_const_value_attribute (die, decl, false);
26370 /* Output debug information for type decl DECL. Called from toplev.c
26371 and from language front ends (to record built-in types). */
26372 static void
26373 dwarf2out_type_decl (tree decl, int local)
26375 if (!local)
26377 set_early_dwarf s;
26378 dwarf2out_decl (decl);
26382 /* Output debug information for imported module or decl DECL.
26383 NAME is non-NULL name in the lexical block if the decl has been renamed.
26384 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
26385 that DECL belongs to.
26386 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
26387 static void
26388 dwarf2out_imported_module_or_decl_1 (tree decl,
26389 tree name,
26390 tree lexical_block,
26391 dw_die_ref lexical_block_die)
26393 expanded_location xloc;
26394 dw_die_ref imported_die = NULL;
26395 dw_die_ref at_import_die;
26397 if (TREE_CODE (decl) == IMPORTED_DECL)
26399 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
26400 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
26401 gcc_assert (decl);
26403 else
26404 xloc = expand_location (input_location);
26406 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
26408 at_import_die = force_type_die (TREE_TYPE (decl));
26409 /* For namespace N { typedef void T; } using N::T; base_type_die
26410 returns NULL, but DW_TAG_imported_declaration requires
26411 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
26412 if (!at_import_die)
26414 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
26415 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
26416 at_import_die = lookup_type_die (TREE_TYPE (decl));
26417 gcc_assert (at_import_die);
26420 else
26422 at_import_die = lookup_decl_die (decl);
26423 if (!at_import_die)
26425 /* If we're trying to avoid duplicate debug info, we may not have
26426 emitted the member decl for this field. Emit it now. */
26427 if (TREE_CODE (decl) == FIELD_DECL)
26429 tree type = DECL_CONTEXT (decl);
26431 if (TYPE_CONTEXT (type)
26432 && TYPE_P (TYPE_CONTEXT (type))
26433 && !should_emit_struct_debug (TYPE_CONTEXT (type),
26434 DINFO_USAGE_DIR_USE))
26435 return;
26436 gen_type_die_for_member (type, decl,
26437 get_context_die (TYPE_CONTEXT (type)));
26439 if (TREE_CODE (decl) == NAMELIST_DECL)
26440 at_import_die = gen_namelist_decl (DECL_NAME (decl),
26441 get_context_die (DECL_CONTEXT (decl)),
26442 NULL_TREE);
26443 else
26444 at_import_die = force_decl_die (decl);
26448 if (TREE_CODE (decl) == NAMESPACE_DECL)
26450 if (dwarf_version >= 3 || !dwarf_strict)
26451 imported_die = new_die (DW_TAG_imported_module,
26452 lexical_block_die,
26453 lexical_block);
26454 else
26455 return;
26457 else
26458 imported_die = new_die (DW_TAG_imported_declaration,
26459 lexical_block_die,
26460 lexical_block);
26462 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
26463 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
26464 if (debug_column_info && xloc.column)
26465 add_AT_unsigned (imported_die, DW_AT_decl_column, xloc.column);
26466 if (name)
26467 add_AT_string (imported_die, DW_AT_name,
26468 IDENTIFIER_POINTER (name));
26469 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
26472 /* Output debug information for imported module or decl DECL.
26473 NAME is non-NULL name in context if the decl has been renamed.
26474 CHILD is true if decl is one of the renamed decls as part of
26475 importing whole module.
26476 IMPLICIT is set if this hook is called for an implicit import
26477 such as inline namespace. */
26479 static void
26480 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
26481 bool child, bool implicit)
26483 /* dw_die_ref at_import_die; */
26484 dw_die_ref scope_die;
26486 if (debug_info_level <= DINFO_LEVEL_TERSE)
26487 return;
26489 gcc_assert (decl);
26491 /* For DWARF5, just DW_AT_export_symbols on the DW_TAG_namespace
26492 should be enough, for DWARF4 and older even if we emit as extension
26493 DW_AT_export_symbols add the implicit DW_TAG_imported_module anyway
26494 for the benefit of consumers unaware of DW_AT_export_symbols. */
26495 if (implicit
26496 && dwarf_version >= 5
26497 && lang_hooks.decls.decl_dwarf_attribute (decl,
26498 DW_AT_export_symbols) == 1)
26499 return;
26501 set_early_dwarf s;
26503 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
26504 We need decl DIE for reference and scope die. First, get DIE for the decl
26505 itself. */
26507 /* Get the scope die for decl context. Use comp_unit_die for global module
26508 or decl. If die is not found for non globals, force new die. */
26509 if (context
26510 && TYPE_P (context)
26511 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
26512 return;
26514 scope_die = get_context_die (context);
26516 if (child)
26518 /* DW_TAG_imported_module was introduced in the DWARFv3 specification, so
26519 there is nothing we can do, here. */
26520 if (dwarf_version < 3 && dwarf_strict)
26521 return;
26523 gcc_assert (scope_die->die_child);
26524 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
26525 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
26526 scope_die = scope_die->die_child;
26529 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
26530 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
26533 /* Output debug information for namelists. */
26535 static dw_die_ref
26536 gen_namelist_decl (tree name, dw_die_ref scope_die, tree item_decls)
26538 dw_die_ref nml_die, nml_item_die, nml_item_ref_die;
26539 tree value;
26540 unsigned i;
26542 if (debug_info_level <= DINFO_LEVEL_TERSE)
26543 return NULL;
26545 gcc_assert (scope_die != NULL);
26546 nml_die = new_die (DW_TAG_namelist, scope_die, NULL);
26547 add_AT_string (nml_die, DW_AT_name, IDENTIFIER_POINTER (name));
26549 /* If there are no item_decls, we have a nondefining namelist, e.g.
26550 with USE association; hence, set DW_AT_declaration. */
26551 if (item_decls == NULL_TREE)
26553 add_AT_flag (nml_die, DW_AT_declaration, 1);
26554 return nml_die;
26557 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (item_decls), i, value)
26559 nml_item_ref_die = lookup_decl_die (value);
26560 if (!nml_item_ref_die)
26561 nml_item_ref_die = force_decl_die (value);
26563 nml_item_die = new_die (DW_TAG_namelist_item, nml_die, NULL);
26564 add_AT_die_ref (nml_item_die, DW_AT_namelist_items, nml_item_ref_die);
26566 return nml_die;
26570 /* Write the debugging output for DECL and return the DIE. */
26572 static void
26573 dwarf2out_decl (tree decl)
26575 dw_die_ref context_die = comp_unit_die ();
26577 switch (TREE_CODE (decl))
26579 case ERROR_MARK:
26580 return;
26582 case FUNCTION_DECL:
26583 /* If we're a nested function, initially use a parent of NULL; if we're
26584 a plain function, this will be fixed up in decls_for_scope. If
26585 we're a method, it will be ignored, since we already have a DIE. */
26586 if (decl_function_context (decl)
26587 /* But if we're in terse mode, we don't care about scope. */
26588 && debug_info_level > DINFO_LEVEL_TERSE)
26589 context_die = NULL;
26590 break;
26592 case VAR_DECL:
26593 /* For local statics lookup proper context die. */
26594 if (local_function_static (decl))
26595 context_die = lookup_decl_die (DECL_CONTEXT (decl));
26597 /* If we are in terse mode, don't generate any DIEs to represent any
26598 variable declarations or definitions. */
26599 if (debug_info_level <= DINFO_LEVEL_TERSE)
26600 return;
26601 break;
26603 case CONST_DECL:
26604 if (debug_info_level <= DINFO_LEVEL_TERSE)
26605 return;
26606 if (!is_fortran () && !is_ada ())
26607 return;
26608 if (TREE_STATIC (decl) && decl_function_context (decl))
26609 context_die = lookup_decl_die (DECL_CONTEXT (decl));
26610 break;
26612 case NAMESPACE_DECL:
26613 case IMPORTED_DECL:
26614 if (debug_info_level <= DINFO_LEVEL_TERSE)
26615 return;
26616 if (lookup_decl_die (decl) != NULL)
26617 return;
26618 break;
26620 case TYPE_DECL:
26621 /* Don't emit stubs for types unless they are needed by other DIEs. */
26622 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
26623 return;
26625 /* Don't bother trying to generate any DIEs to represent any of the
26626 normal built-in types for the language we are compiling. */
26627 if (DECL_IS_BUILTIN (decl))
26628 return;
26630 /* If we are in terse mode, don't generate any DIEs for types. */
26631 if (debug_info_level <= DINFO_LEVEL_TERSE)
26632 return;
26634 /* If we're a function-scope tag, initially use a parent of NULL;
26635 this will be fixed up in decls_for_scope. */
26636 if (decl_function_context (decl))
26637 context_die = NULL;
26639 break;
26641 case NAMELIST_DECL:
26642 break;
26644 default:
26645 return;
26648 gen_decl_die (decl, NULL, NULL, context_die);
26650 if (flag_checking)
26652 dw_die_ref die = lookup_decl_die (decl);
26653 if (die)
26654 check_die (die);
26658 /* Write the debugging output for DECL. */
26660 static void
26661 dwarf2out_function_decl (tree decl)
26663 dwarf2out_decl (decl);
26664 call_arg_locations = NULL;
26665 call_arg_loc_last = NULL;
26666 call_site_count = -1;
26667 tail_call_site_count = -1;
26668 decl_loc_table->empty ();
26669 cached_dw_loc_list_table->empty ();
26672 /* Output a marker (i.e. a label) for the beginning of the generated code for
26673 a lexical block. */
26675 static void
26676 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
26677 unsigned int blocknum)
26679 switch_to_section (current_function_section ());
26680 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
26683 /* Output a marker (i.e. a label) for the end of the generated code for a
26684 lexical block. */
26686 static void
26687 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
26689 switch_to_section (current_function_section ());
26690 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
26693 /* Returns nonzero if it is appropriate not to emit any debugging
26694 information for BLOCK, because it doesn't contain any instructions.
26696 Don't allow this for blocks with nested functions or local classes
26697 as we would end up with orphans, and in the presence of scheduling
26698 we may end up calling them anyway. */
26700 static bool
26701 dwarf2out_ignore_block (const_tree block)
26703 tree decl;
26704 unsigned int i;
26706 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
26707 if (TREE_CODE (decl) == FUNCTION_DECL
26708 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
26709 return 0;
26710 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
26712 decl = BLOCK_NONLOCALIZED_VAR (block, i);
26713 if (TREE_CODE (decl) == FUNCTION_DECL
26714 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
26715 return 0;
26718 return 1;
26721 /* Hash table routines for file_hash. */
26723 bool
26724 dwarf_file_hasher::equal (dwarf_file_data *p1, const char *p2)
26726 return filename_cmp (p1->filename, p2) == 0;
26729 hashval_t
26730 dwarf_file_hasher::hash (dwarf_file_data *p)
26732 return htab_hash_string (p->filename);
26735 /* Lookup FILE_NAME (in the list of filenames that we know about here in
26736 dwarf2out.c) and return its "index". The index of each (known) filename is
26737 just a unique number which is associated with only that one filename. We
26738 need such numbers for the sake of generating labels (in the .debug_sfnames
26739 section) and references to those files numbers (in the .debug_srcinfo
26740 and .debug_macinfo sections). If the filename given as an argument is not
26741 found in our current list, add it to the list and assign it the next
26742 available unique index number. */
26744 static struct dwarf_file_data *
26745 lookup_filename (const char *file_name)
26747 struct dwarf_file_data * created;
26749 if (!file_name)
26750 return NULL;
26752 dwarf_file_data **slot
26753 = file_table->find_slot_with_hash (file_name, htab_hash_string (file_name),
26754 INSERT);
26755 if (*slot)
26756 return *slot;
26758 created = ggc_alloc<dwarf_file_data> ();
26759 created->filename = file_name;
26760 created->emitted_number = 0;
26761 *slot = created;
26762 return created;
26765 /* If the assembler will construct the file table, then translate the compiler
26766 internal file table number into the assembler file table number, and emit
26767 a .file directive if we haven't already emitted one yet. The file table
26768 numbers are different because we prune debug info for unused variables and
26769 types, which may include filenames. */
26771 static int
26772 maybe_emit_file (struct dwarf_file_data * fd)
26774 if (! fd->emitted_number)
26776 if (last_emitted_file)
26777 fd->emitted_number = last_emitted_file->emitted_number + 1;
26778 else
26779 fd->emitted_number = 1;
26780 last_emitted_file = fd;
26782 if (output_asm_line_debug_info ())
26784 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
26785 output_quoted_string (asm_out_file,
26786 remap_debug_filename (fd->filename));
26787 fputc ('\n', asm_out_file);
26791 return fd->emitted_number;
26794 /* Schedule generation of a DW_AT_const_value attribute to DIE.
26795 That generation should happen after function debug info has been
26796 generated. The value of the attribute is the constant value of ARG. */
26798 static void
26799 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
26801 die_arg_entry entry;
26803 if (!die || !arg)
26804 return;
26806 gcc_assert (early_dwarf);
26808 if (!tmpl_value_parm_die_table)
26809 vec_alloc (tmpl_value_parm_die_table, 32);
26811 entry.die = die;
26812 entry.arg = arg;
26813 vec_safe_push (tmpl_value_parm_die_table, entry);
26816 /* Return TRUE if T is an instance of generic type, FALSE
26817 otherwise. */
26819 static bool
26820 generic_type_p (tree t)
26822 if (t == NULL_TREE || !TYPE_P (t))
26823 return false;
26824 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
26827 /* Schedule the generation of the generic parameter dies for the
26828 instance of generic type T. The proper generation itself is later
26829 done by gen_scheduled_generic_parms_dies. */
26831 static void
26832 schedule_generic_params_dies_gen (tree t)
26834 if (!generic_type_p (t))
26835 return;
26837 gcc_assert (early_dwarf);
26839 if (!generic_type_instances)
26840 vec_alloc (generic_type_instances, 256);
26842 vec_safe_push (generic_type_instances, t);
26845 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
26846 by append_entry_to_tmpl_value_parm_die_table. This function must
26847 be called after function DIEs have been generated. */
26849 static void
26850 gen_remaining_tmpl_value_param_die_attribute (void)
26852 if (tmpl_value_parm_die_table)
26854 unsigned i, j;
26855 die_arg_entry *e;
26857 /* We do this in two phases - first get the cases we can
26858 handle during early-finish, preserving those we cannot
26859 (containing symbolic constants where we don't yet know
26860 whether we are going to output the referenced symbols).
26861 For those we try again at late-finish. */
26862 j = 0;
26863 FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
26865 if (!e->die->removed
26866 && !tree_add_const_value_attribute (e->die, e->arg))
26868 dw_loc_descr_ref loc = NULL;
26869 if (! early_dwarf
26870 && (dwarf_version >= 5 || !dwarf_strict))
26871 loc = loc_descriptor_from_tree (e->arg, 2, NULL);
26872 if (loc)
26873 add_AT_loc (e->die, DW_AT_location, loc);
26874 else
26875 (*tmpl_value_parm_die_table)[j++] = *e;
26878 tmpl_value_parm_die_table->truncate (j);
26882 /* Generate generic parameters DIEs for instances of generic types
26883 that have been previously scheduled by
26884 schedule_generic_params_dies_gen. This function must be called
26885 after all the types of the CU have been laid out. */
26887 static void
26888 gen_scheduled_generic_parms_dies (void)
26890 unsigned i;
26891 tree t;
26893 if (!generic_type_instances)
26894 return;
26896 FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
26897 if (COMPLETE_TYPE_P (t))
26898 gen_generic_params_dies (t);
26900 generic_type_instances = NULL;
26904 /* Replace DW_AT_name for the decl with name. */
26906 static void
26907 dwarf2out_set_name (tree decl, tree name)
26909 dw_die_ref die;
26910 dw_attr_node *attr;
26911 const char *dname;
26913 die = TYPE_SYMTAB_DIE (decl);
26914 if (!die)
26915 return;
26917 dname = dwarf2_name (name, 0);
26918 if (!dname)
26919 return;
26921 attr = get_AT (die, DW_AT_name);
26922 if (attr)
26924 struct indirect_string_node *node;
26926 node = find_AT_string (dname);
26927 /* replace the string. */
26928 attr->dw_attr_val.v.val_str = node;
26931 else
26932 add_name_attribute (die, dname);
26935 /* True if before or during processing of the first function being emitted. */
26936 static bool in_first_function_p = true;
26937 /* True if loc_note during dwarf2out_var_location call might still be
26938 before first real instruction at address equal to .Ltext0. */
26939 static bool maybe_at_text_label_p = true;
26940 /* One above highest N where .LVLN label might be equal to .Ltext0 label. */
26941 static unsigned int first_loclabel_num_not_at_text_label;
26943 /* Look ahead for a real insn, or for a begin stmt marker. */
26945 static rtx_insn *
26946 dwarf2out_next_real_insn (rtx_insn *loc_note)
26948 rtx_insn *next_real = NEXT_INSN (loc_note);
26950 while (next_real)
26951 if (INSN_P (next_real))
26952 break;
26953 else
26954 next_real = NEXT_INSN (next_real);
26956 return next_real;
26959 /* Called by the final INSN scan whenever we see a var location. We
26960 use it to drop labels in the right places, and throw the location in
26961 our lookup table. */
26963 static void
26964 dwarf2out_var_location (rtx_insn *loc_note)
26966 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
26967 struct var_loc_node *newloc;
26968 rtx_insn *next_real, *next_note;
26969 rtx_insn *call_insn = NULL;
26970 static const char *last_label;
26971 static const char *last_postcall_label;
26972 static bool last_in_cold_section_p;
26973 static rtx_insn *expected_next_loc_note;
26974 tree decl;
26975 bool var_loc_p;
26976 var_loc_view view = 0;
26978 if (!NOTE_P (loc_note))
26980 if (CALL_P (loc_note))
26982 maybe_reset_location_view (loc_note, cur_line_info_table);
26983 call_site_count++;
26984 if (SIBLING_CALL_P (loc_note))
26985 tail_call_site_count++;
26986 if (find_reg_note (loc_note, REG_CALL_ARG_LOCATION, NULL_RTX))
26988 call_insn = loc_note;
26989 loc_note = NULL;
26990 var_loc_p = false;
26992 next_real = dwarf2out_next_real_insn (call_insn);
26993 next_note = NULL;
26994 cached_next_real_insn = NULL;
26995 goto create_label;
26997 if (optimize == 0 && !flag_var_tracking)
26999 /* When the var-tracking pass is not running, there is no note
27000 for indirect calls whose target is compile-time known. In this
27001 case, process such calls specifically so that we generate call
27002 sites for them anyway. */
27003 rtx x = PATTERN (loc_note);
27004 if (GET_CODE (x) == PARALLEL)
27005 x = XVECEXP (x, 0, 0);
27006 if (GET_CODE (x) == SET)
27007 x = SET_SRC (x);
27008 if (GET_CODE (x) == CALL)
27009 x = XEXP (x, 0);
27010 if (!MEM_P (x)
27011 || GET_CODE (XEXP (x, 0)) != SYMBOL_REF
27012 || !SYMBOL_REF_DECL (XEXP (x, 0))
27013 || (TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0)))
27014 != FUNCTION_DECL))
27016 call_insn = loc_note;
27017 loc_note = NULL;
27018 var_loc_p = false;
27020 next_real = dwarf2out_next_real_insn (call_insn);
27021 next_note = NULL;
27022 cached_next_real_insn = NULL;
27023 goto create_label;
27027 else if (!debug_variable_location_views)
27028 gcc_unreachable ();
27029 else
27030 maybe_reset_location_view (loc_note, cur_line_info_table);
27032 return;
27035 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
27036 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
27037 return;
27039 /* Optimize processing a large consecutive sequence of location
27040 notes so we don't spend too much time in next_real_insn. If the
27041 next insn is another location note, remember the next_real_insn
27042 calculation for next time. */
27043 next_real = cached_next_real_insn;
27044 if (next_real)
27046 if (expected_next_loc_note != loc_note)
27047 next_real = NULL;
27050 next_note = NEXT_INSN (loc_note);
27051 if (! next_note
27052 || next_note->deleted ()
27053 || ! NOTE_P (next_note)
27054 || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
27055 && NOTE_KIND (next_note) != NOTE_INSN_BEGIN_STMT
27056 && NOTE_KIND (next_note) != NOTE_INSN_INLINE_ENTRY))
27057 next_note = NULL;
27059 if (! next_real)
27060 next_real = dwarf2out_next_real_insn (loc_note);
27062 if (next_note)
27064 expected_next_loc_note = next_note;
27065 cached_next_real_insn = next_real;
27067 else
27068 cached_next_real_insn = NULL;
27070 /* If there are no instructions which would be affected by this note,
27071 don't do anything. */
27072 if (var_loc_p
27073 && next_real == NULL_RTX
27074 && !NOTE_DURING_CALL_P (loc_note))
27075 return;
27077 create_label:
27079 if (next_real == NULL_RTX)
27080 next_real = get_last_insn ();
27082 /* If there were any real insns between note we processed last time
27083 and this note (or if it is the first note), clear
27084 last_{,postcall_}label so that they are not reused this time. */
27085 if (last_var_location_insn == NULL_RTX
27086 || last_var_location_insn != next_real
27087 || last_in_cold_section_p != in_cold_section_p)
27089 last_label = NULL;
27090 last_postcall_label = NULL;
27093 if (var_loc_p)
27095 const char *label
27096 = NOTE_DURING_CALL_P (loc_note) ? last_postcall_label : last_label;
27097 view = cur_line_info_table->view;
27098 decl = NOTE_VAR_LOCATION_DECL (loc_note);
27099 newloc = add_var_loc_to_decl (decl, loc_note, label, view);
27100 if (newloc == NULL)
27101 return;
27103 else
27105 decl = NULL_TREE;
27106 newloc = NULL;
27109 /* If there were no real insns between note we processed last time
27110 and this note, use the label we emitted last time. Otherwise
27111 create a new label and emit it. */
27112 if (last_label == NULL)
27114 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
27115 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
27116 loclabel_num++;
27117 last_label = ggc_strdup (loclabel);
27118 /* See if loclabel might be equal to .Ltext0. If yes,
27119 bump first_loclabel_num_not_at_text_label. */
27120 if (!have_multiple_function_sections
27121 && in_first_function_p
27122 && maybe_at_text_label_p)
27124 static rtx_insn *last_start;
27125 rtx_insn *insn;
27126 for (insn = loc_note; insn; insn = previous_insn (insn))
27127 if (insn == last_start)
27128 break;
27129 else if (!NONDEBUG_INSN_P (insn))
27130 continue;
27131 else
27133 rtx body = PATTERN (insn);
27134 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
27135 continue;
27136 /* Inline asm could occupy zero bytes. */
27137 else if (GET_CODE (body) == ASM_INPUT
27138 || asm_noperands (body) >= 0)
27139 continue;
27140 #ifdef HAVE_ATTR_length /* ??? We don't include insn-attr.h. */
27141 else if (HAVE_ATTR_length && get_attr_min_length (insn) == 0)
27142 continue;
27143 #endif
27144 else
27146 /* Assume insn has non-zero length. */
27147 maybe_at_text_label_p = false;
27148 break;
27151 if (maybe_at_text_label_p)
27153 last_start = loc_note;
27154 first_loclabel_num_not_at_text_label = loclabel_num;
27159 gcc_assert ((loc_note == NULL_RTX && call_insn != NULL_RTX)
27160 || (loc_note != NULL_RTX && call_insn == NULL_RTX));
27162 if (!var_loc_p)
27164 struct call_arg_loc_node *ca_loc
27165 = ggc_cleared_alloc<call_arg_loc_node> ();
27166 rtx_insn *prev = call_insn;
27168 ca_loc->call_arg_loc_note
27169 = find_reg_note (call_insn, REG_CALL_ARG_LOCATION, NULL_RTX);
27170 ca_loc->next = NULL;
27171 ca_loc->label = last_label;
27172 gcc_assert (prev
27173 && (CALL_P (prev)
27174 || (NONJUMP_INSN_P (prev)
27175 && GET_CODE (PATTERN (prev)) == SEQUENCE
27176 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
27177 if (!CALL_P (prev))
27178 prev = as_a <rtx_sequence *> (PATTERN (prev))->insn (0);
27179 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
27181 /* Look for a SYMBOL_REF in the "prev" instruction. */
27182 rtx x = get_call_rtx_from (PATTERN (prev));
27183 if (x)
27185 /* Try to get the call symbol, if any. */
27186 if (MEM_P (XEXP (x, 0)))
27187 x = XEXP (x, 0);
27188 /* First, look for a memory access to a symbol_ref. */
27189 if (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
27190 && SYMBOL_REF_DECL (XEXP (x, 0))
27191 && TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0))) == FUNCTION_DECL)
27192 ca_loc->symbol_ref = XEXP (x, 0);
27193 /* Otherwise, look at a compile-time known user-level function
27194 declaration. */
27195 else if (MEM_P (x)
27196 && MEM_EXPR (x)
27197 && TREE_CODE (MEM_EXPR (x)) == FUNCTION_DECL)
27198 ca_loc->symbol_ref = XEXP (DECL_RTL (MEM_EXPR (x)), 0);
27201 ca_loc->block = insn_scope (prev);
27202 if (call_arg_locations)
27203 call_arg_loc_last->next = ca_loc;
27204 else
27205 call_arg_locations = ca_loc;
27206 call_arg_loc_last = ca_loc;
27208 else if (loc_note != NULL_RTX && !NOTE_DURING_CALL_P (loc_note))
27210 newloc->label = last_label;
27211 newloc->view = view;
27213 else
27215 if (!last_postcall_label)
27217 sprintf (loclabel, "%s-1", last_label);
27218 last_postcall_label = ggc_strdup (loclabel);
27220 newloc->label = last_postcall_label;
27221 /* ??? This view is at last_label, not last_label-1, but we
27222 could only assume view at last_label-1 is zero if we could
27223 assume calls always have length greater than one. This is
27224 probably true in general, though there might be a rare
27225 exception to this rule, e.g. if a call insn is optimized out
27226 by target magic. Then, even the -1 in the label will be
27227 wrong, which might invalidate the range. Anyway, using view,
27228 though technically possibly incorrect, will work as far as
27229 ranges go: since L-1 is in the middle of the call insn,
27230 (L-1).0 and (L-1).V shouldn't make any difference, and having
27231 the loclist entry refer to the .loc entry might be useful, so
27232 leave it like this. */
27233 newloc->view = view;
27236 if (var_loc_p && flag_debug_asm)
27238 const char *name, *sep, *patstr;
27239 if (decl && DECL_NAME (decl))
27240 name = IDENTIFIER_POINTER (DECL_NAME (decl));
27241 else
27242 name = "";
27243 if (NOTE_VAR_LOCATION_LOC (loc_note))
27245 sep = " => ";
27246 patstr = str_pattern_slim (NOTE_VAR_LOCATION_LOC (loc_note));
27248 else
27250 sep = " ";
27251 patstr = "RESET";
27253 fprintf (asm_out_file, "\t%s DEBUG %s%s%s\n", ASM_COMMENT_START,
27254 name, sep, patstr);
27257 last_var_location_insn = next_real;
27258 last_in_cold_section_p = in_cold_section_p;
27261 /* Check whether BLOCK, a lexical block, is nested within OUTER, or is
27262 OUTER itself. If BOTHWAYS, check not only that BLOCK can reach
27263 OUTER through BLOCK_SUPERCONTEXT links, but also that there is a
27264 path from OUTER to BLOCK through BLOCK_SUBBLOCKs and
27265 BLOCK_FRAGMENT_ORIGIN links. */
27266 static bool
27267 block_within_block_p (tree block, tree outer, bool bothways)
27269 if (block == outer)
27270 return true;
27272 /* Quickly check that OUTER is up BLOCK's supercontext chain. */
27273 for (tree context = BLOCK_SUPERCONTEXT (block);
27274 context != outer;
27275 context = BLOCK_SUPERCONTEXT (context))
27276 if (!context || TREE_CODE (context) != BLOCK)
27277 return false;
27279 if (!bothways)
27280 return true;
27282 /* Now check that each block is actually referenced by its
27283 parent. */
27284 for (tree context = BLOCK_SUPERCONTEXT (block); ;
27285 context = BLOCK_SUPERCONTEXT (context))
27287 if (BLOCK_FRAGMENT_ORIGIN (context))
27289 gcc_assert (!BLOCK_SUBBLOCKS (context));
27290 context = BLOCK_FRAGMENT_ORIGIN (context);
27292 for (tree sub = BLOCK_SUBBLOCKS (context);
27293 sub != block;
27294 sub = BLOCK_CHAIN (sub))
27295 if (!sub)
27296 return false;
27297 if (context == outer)
27298 return true;
27299 else
27300 block = context;
27304 /* Called during final while assembling the marker of the entry point
27305 for an inlined function. */
27307 static void
27308 dwarf2out_inline_entry (tree block)
27310 gcc_assert (debug_inline_points);
27312 /* If we can't represent it, don't bother. */
27313 if (!(dwarf_version >= 3 || !dwarf_strict))
27314 return;
27316 gcc_assert (DECL_P (block_ultimate_origin (block)));
27318 /* Sanity check the block tree. This would catch a case in which
27319 BLOCK got removed from the tree reachable from the outermost
27320 lexical block, but got retained in markers. It would still link
27321 back to its parents, but some ancestor would be missing a link
27322 down the path to the sub BLOCK. If the block got removed, its
27323 BLOCK_NUMBER will not be a usable value. */
27324 if (flag_checking)
27325 gcc_assert (block_within_block_p (block,
27326 DECL_INITIAL (current_function_decl),
27327 true));
27329 gcc_assert (inlined_function_outer_scope_p (block));
27330 gcc_assert (!BLOCK_DIE (block));
27332 if (BLOCK_FRAGMENT_ORIGIN (block))
27333 block = BLOCK_FRAGMENT_ORIGIN (block);
27334 /* Can the entry point ever not be at the beginning of an
27335 unfragmented lexical block? */
27336 else if (!(BLOCK_FRAGMENT_CHAIN (block)
27337 || (cur_line_info_table
27338 && !ZERO_VIEW_P (cur_line_info_table->view))))
27339 return;
27341 if (!inline_entry_data_table)
27342 inline_entry_data_table
27343 = hash_table<inline_entry_data_hasher>::create_ggc (10);
27346 inline_entry_data **iedp
27347 = inline_entry_data_table->find_slot_with_hash (block,
27348 htab_hash_pointer (block),
27349 INSERT);
27350 if (*iedp)
27351 /* ??? Ideally, we'd record all entry points for the same inlined
27352 function (some may have been duplicated by e.g. unrolling), but
27353 we have no way to represent that ATM. */
27354 return;
27356 inline_entry_data *ied = *iedp = ggc_cleared_alloc<inline_entry_data> ();
27357 ied->block = block;
27358 ied->label_pfx = BLOCK_INLINE_ENTRY_LABEL;
27359 ied->label_num = BLOCK_NUMBER (block);
27360 if (cur_line_info_table)
27361 ied->view = cur_line_info_table->view;
27363 char label[MAX_ARTIFICIAL_LABEL_BYTES];
27365 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_INLINE_ENTRY_LABEL,
27366 BLOCK_NUMBER (block));
27367 ASM_OUTPUT_LABEL (asm_out_file, label);
27370 /* Called from finalize_size_functions for size functions so that their body
27371 can be encoded in the debug info to describe the layout of variable-length
27372 structures. */
27374 static void
27375 dwarf2out_size_function (tree decl)
27377 function_to_dwarf_procedure (decl);
27380 /* Note in one location list that text section has changed. */
27383 var_location_switch_text_section_1 (var_loc_list **slot, void *)
27385 var_loc_list *list = *slot;
27386 if (list->first)
27387 list->last_before_switch
27388 = list->last->next ? list->last->next : list->last;
27389 return 1;
27392 /* Note in all location lists that text section has changed. */
27394 static void
27395 var_location_switch_text_section (void)
27397 if (decl_loc_table == NULL)
27398 return;
27400 decl_loc_table->traverse<void *, var_location_switch_text_section_1> (NULL);
27403 /* Create a new line number table. */
27405 static dw_line_info_table *
27406 new_line_info_table (void)
27408 dw_line_info_table *table;
27410 table = ggc_cleared_alloc<dw_line_info_table> ();
27411 table->file_num = 1;
27412 table->line_num = 1;
27413 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
27414 FORCE_RESET_NEXT_VIEW (table->view);
27416 return table;
27419 /* Lookup the "current" table into which we emit line info, so
27420 that we don't have to do it for every source line. */
27422 static void
27423 set_cur_line_info_table (section *sec)
27425 dw_line_info_table *table;
27427 if (sec == text_section)
27428 table = text_section_line_info;
27429 else if (sec == cold_text_section)
27431 table = cold_text_section_line_info;
27432 if (!table)
27434 cold_text_section_line_info = table = new_line_info_table ();
27435 table->end_label = cold_end_label;
27438 else
27440 const char *end_label;
27442 if (crtl->has_bb_partition)
27444 if (in_cold_section_p)
27445 end_label = crtl->subsections.cold_section_end_label;
27446 else
27447 end_label = crtl->subsections.hot_section_end_label;
27449 else
27451 char label[MAX_ARTIFICIAL_LABEL_BYTES];
27452 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
27453 current_function_funcdef_no);
27454 end_label = ggc_strdup (label);
27457 table = new_line_info_table ();
27458 table->end_label = end_label;
27460 vec_safe_push (separate_line_info, table);
27463 if (output_asm_line_debug_info ())
27464 table->is_stmt = (cur_line_info_table
27465 ? cur_line_info_table->is_stmt
27466 : DWARF_LINE_DEFAULT_IS_STMT_START);
27467 cur_line_info_table = table;
27471 /* We need to reset the locations at the beginning of each
27472 function. We can't do this in the end_function hook, because the
27473 declarations that use the locations won't have been output when
27474 that hook is called. Also compute have_multiple_function_sections here. */
27476 static void
27477 dwarf2out_begin_function (tree fun)
27479 section *sec = function_section (fun);
27481 if (sec != text_section)
27482 have_multiple_function_sections = true;
27484 if (crtl->has_bb_partition && !cold_text_section)
27486 gcc_assert (current_function_decl == fun);
27487 cold_text_section = unlikely_text_section ();
27488 switch_to_section (cold_text_section);
27489 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
27490 switch_to_section (sec);
27493 dwarf2out_note_section_used ();
27494 call_site_count = 0;
27495 tail_call_site_count = 0;
27497 set_cur_line_info_table (sec);
27498 FORCE_RESET_NEXT_VIEW (cur_line_info_table->view);
27501 /* Helper function of dwarf2out_end_function, called only after emitting
27502 the very first function into assembly. Check if some .debug_loc range
27503 might end with a .LVL* label that could be equal to .Ltext0.
27504 In that case we must force using absolute addresses in .debug_loc ranges,
27505 because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
27506 .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
27507 list terminator.
27508 Set have_multiple_function_sections to true in that case and
27509 terminate htab traversal. */
27512 find_empty_loc_ranges_at_text_label (var_loc_list **slot, int)
27514 var_loc_list *entry = *slot;
27515 struct var_loc_node *node;
27517 node = entry->first;
27518 if (node && node->next && node->next->label)
27520 unsigned int i;
27521 const char *label = node->next->label;
27522 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
27524 for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
27526 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
27527 if (strcmp (label, loclabel) == 0)
27529 have_multiple_function_sections = true;
27530 return 0;
27534 return 1;
27537 /* Hook called after emitting a function into assembly.
27538 This does something only for the very first function emitted. */
27540 static void
27541 dwarf2out_end_function (unsigned int)
27543 if (in_first_function_p
27544 && !have_multiple_function_sections
27545 && first_loclabel_num_not_at_text_label
27546 && decl_loc_table)
27547 decl_loc_table->traverse<int, find_empty_loc_ranges_at_text_label> (0);
27548 in_first_function_p = false;
27549 maybe_at_text_label_p = false;
27552 /* Temporary holder for dwarf2out_register_main_translation_unit. Used to let
27553 front-ends register a translation unit even before dwarf2out_init is
27554 called. */
27555 static tree main_translation_unit = NULL_TREE;
27557 /* Hook called by front-ends after they built their main translation unit.
27558 Associate comp_unit_die to UNIT. */
27560 static void
27561 dwarf2out_register_main_translation_unit (tree unit)
27563 gcc_assert (TREE_CODE (unit) == TRANSLATION_UNIT_DECL
27564 && main_translation_unit == NULL_TREE);
27565 main_translation_unit = unit;
27566 /* If dwarf2out_init has not been called yet, it will perform the association
27567 itself looking at main_translation_unit. */
27568 if (decl_die_table != NULL)
27569 equate_decl_number_to_die (unit, comp_unit_die ());
27572 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
27574 static void
27575 push_dw_line_info_entry (dw_line_info_table *table,
27576 enum dw_line_info_opcode opcode, unsigned int val)
27578 dw_line_info_entry e;
27579 e.opcode = opcode;
27580 e.val = val;
27581 vec_safe_push (table->entries, e);
27584 /* Output a label to mark the beginning of a source code line entry
27585 and record information relating to this source line, in
27586 'line_info_table' for later output of the .debug_line section. */
27587 /* ??? The discriminator parameter ought to be unsigned. */
27589 static void
27590 dwarf2out_source_line (unsigned int line, unsigned int column,
27591 const char *filename,
27592 int discriminator, bool is_stmt)
27594 unsigned int file_num;
27595 dw_line_info_table *table;
27596 static var_loc_view lvugid;
27598 if (debug_info_level < DINFO_LEVEL_TERSE)
27599 return;
27601 table = cur_line_info_table;
27603 if (line == 0)
27605 if (debug_variable_location_views
27606 && output_asm_line_debug_info ()
27607 && table && !RESETTING_VIEW_P (table->view))
27609 /* If we're using the assembler to compute view numbers, we
27610 can't issue a .loc directive for line zero, so we can't
27611 get a view number at this point. We might attempt to
27612 compute it from the previous view, but since we're
27613 omitting the line number entry, we might as well omit the
27614 view number as well. That means pretending it's a view
27615 number zero, which might very well turn out to be
27616 correct. */
27617 if (!zero_view_p)
27618 zero_view_p = BITMAP_GGC_ALLOC ();
27619 bitmap_set_bit (zero_view_p, table->view);
27620 if (flag_debug_asm)
27622 char label[MAX_ARTIFICIAL_LABEL_BYTES];
27623 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", table->view);
27624 fprintf (asm_out_file, "\t%s line 0, omitted view ",
27625 ASM_COMMENT_START);
27626 assemble_name (asm_out_file, label);
27627 putc ('\n', asm_out_file);
27629 table->view = ++lvugid;
27631 return;
27634 /* The discriminator column was added in dwarf4. Simplify the below
27635 by simply removing it if we're not supposed to output it. */
27636 if (dwarf_version < 4 && dwarf_strict)
27637 discriminator = 0;
27639 if (!debug_column_info)
27640 column = 0;
27642 file_num = maybe_emit_file (lookup_filename (filename));
27644 /* ??? TODO: Elide duplicate line number entries. Traditionally,
27645 the debugger has used the second (possibly duplicate) line number
27646 at the beginning of the function to mark the end of the prologue.
27647 We could eliminate any other duplicates within the function. For
27648 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
27649 that second line number entry. */
27650 /* Recall that this end-of-prologue indication is *not* the same thing
27651 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
27652 to which the hook corresponds, follows the last insn that was
27653 emitted by gen_prologue. What we need is to precede the first insn
27654 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
27655 insn that corresponds to something the user wrote. These may be
27656 very different locations once scheduling is enabled. */
27658 if (0 && file_num == table->file_num
27659 && line == table->line_num
27660 && column == table->column_num
27661 && discriminator == table->discrim_num
27662 && is_stmt == table->is_stmt)
27663 return;
27665 switch_to_section (current_function_section ());
27667 /* If requested, emit something human-readable. */
27668 if (flag_debug_asm)
27670 if (debug_column_info)
27671 fprintf (asm_out_file, "\t%s %s:%d:%d\n", ASM_COMMENT_START,
27672 filename, line, column);
27673 else
27674 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
27675 filename, line);
27678 if (output_asm_line_debug_info ())
27680 /* Emit the .loc directive understood by GNU as. */
27681 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
27682 file_num, line, is_stmt, discriminator */
27683 fputs ("\t.loc ", asm_out_file);
27684 fprint_ul (asm_out_file, file_num);
27685 putc (' ', asm_out_file);
27686 fprint_ul (asm_out_file, line);
27687 putc (' ', asm_out_file);
27688 fprint_ul (asm_out_file, column);
27690 if (is_stmt != table->is_stmt)
27692 fputs (" is_stmt ", asm_out_file);
27693 putc (is_stmt ? '1' : '0', asm_out_file);
27695 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
27697 gcc_assert (discriminator > 0);
27698 fputs (" discriminator ", asm_out_file);
27699 fprint_ul (asm_out_file, (unsigned long) discriminator);
27701 if (debug_variable_location_views)
27703 if (!RESETTING_VIEW_P (table->view))
27705 /* When we're using the assembler to compute view
27706 numbers, we output symbolic labels after "view" in
27707 .loc directives, and the assembler will set them for
27708 us, so that we can refer to the view numbers in
27709 location lists. The only exceptions are when we know
27710 a view will be zero: "-0" is a forced reset, used
27711 e.g. in the beginning of functions, whereas "0" tells
27712 the assembler to check that there was a PC change
27713 since the previous view, in a way that implicitly
27714 resets the next view. */
27715 fputs (" view ", asm_out_file);
27716 char label[MAX_ARTIFICIAL_LABEL_BYTES];
27717 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", table->view);
27718 assemble_name (asm_out_file, label);
27719 table->view = ++lvugid;
27721 else
27723 if (FORCE_RESETTING_VIEW_P (table->view))
27724 fputs (" view -0", asm_out_file);
27725 else
27726 fputs (" view 0", asm_out_file);
27727 /* Mark the present view as a zero view. Earlier debug
27728 binds may have already added its id to loclists to be
27729 emitted later, so we can't reuse the id for something
27730 else. However, it's good to know whether a view is
27731 known to be zero, because then we may be able to
27732 optimize out locviews that are all zeros, so take
27733 note of it in zero_view_p. */
27734 if (!zero_view_p)
27735 zero_view_p = BITMAP_GGC_ALLOC ();
27736 bitmap_set_bit (zero_view_p, lvugid);
27737 table->view = ++lvugid;
27740 putc ('\n', asm_out_file);
27742 else
27744 unsigned int label_num = ++line_info_label_num;
27746 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
27748 if (debug_variable_location_views && !RESETTING_VIEW_P (table->view))
27749 push_dw_line_info_entry (table, LI_adv_address, label_num);
27750 else
27751 push_dw_line_info_entry (table, LI_set_address, label_num);
27752 if (debug_variable_location_views)
27754 bool resetting = FORCE_RESETTING_VIEW_P (table->view);
27755 if (resetting)
27756 table->view = 0;
27758 if (flag_debug_asm)
27759 fprintf (asm_out_file, "\t%s view %s%d\n",
27760 ASM_COMMENT_START,
27761 resetting ? "-" : "",
27762 table->view);
27764 table->view++;
27766 if (file_num != table->file_num)
27767 push_dw_line_info_entry (table, LI_set_file, file_num);
27768 if (discriminator != table->discrim_num)
27769 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
27770 if (is_stmt != table->is_stmt)
27771 push_dw_line_info_entry (table, LI_negate_stmt, 0);
27772 push_dw_line_info_entry (table, LI_set_line, line);
27773 if (debug_column_info)
27774 push_dw_line_info_entry (table, LI_set_column, column);
27777 table->file_num = file_num;
27778 table->line_num = line;
27779 table->column_num = column;
27780 table->discrim_num = discriminator;
27781 table->is_stmt = is_stmt;
27782 table->in_use = true;
27785 /* Record the beginning of a new source file. */
27787 static void
27788 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
27790 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
27792 macinfo_entry e;
27793 e.code = DW_MACINFO_start_file;
27794 e.lineno = lineno;
27795 e.info = ggc_strdup (filename);
27796 vec_safe_push (macinfo_table, e);
27800 /* Record the end of a source file. */
27802 static void
27803 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
27805 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
27807 macinfo_entry e;
27808 e.code = DW_MACINFO_end_file;
27809 e.lineno = lineno;
27810 e.info = NULL;
27811 vec_safe_push (macinfo_table, e);
27815 /* Called from debug_define in toplev.c. The `buffer' parameter contains
27816 the tail part of the directive line, i.e. the part which is past the
27817 initial whitespace, #, whitespace, directive-name, whitespace part. */
27819 static void
27820 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
27821 const char *buffer ATTRIBUTE_UNUSED)
27823 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
27825 macinfo_entry e;
27826 /* Insert a dummy first entry to be able to optimize the whole
27827 predefined macro block using DW_MACRO_import. */
27828 if (macinfo_table->is_empty () && lineno <= 1)
27830 e.code = 0;
27831 e.lineno = 0;
27832 e.info = NULL;
27833 vec_safe_push (macinfo_table, e);
27835 e.code = DW_MACINFO_define;
27836 e.lineno = lineno;
27837 e.info = ggc_strdup (buffer);
27838 vec_safe_push (macinfo_table, e);
27842 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
27843 the tail part of the directive line, i.e. the part which is past the
27844 initial whitespace, #, whitespace, directive-name, whitespace part. */
27846 static void
27847 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
27848 const char *buffer ATTRIBUTE_UNUSED)
27850 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
27852 macinfo_entry e;
27853 /* Insert a dummy first entry to be able to optimize the whole
27854 predefined macro block using DW_MACRO_import. */
27855 if (macinfo_table->is_empty () && lineno <= 1)
27857 e.code = 0;
27858 e.lineno = 0;
27859 e.info = NULL;
27860 vec_safe_push (macinfo_table, e);
27862 e.code = DW_MACINFO_undef;
27863 e.lineno = lineno;
27864 e.info = ggc_strdup (buffer);
27865 vec_safe_push (macinfo_table, e);
27869 /* Helpers to manipulate hash table of CUs. */
27871 struct macinfo_entry_hasher : nofree_ptr_hash <macinfo_entry>
27873 static inline hashval_t hash (const macinfo_entry *);
27874 static inline bool equal (const macinfo_entry *, const macinfo_entry *);
27877 inline hashval_t
27878 macinfo_entry_hasher::hash (const macinfo_entry *entry)
27880 return htab_hash_string (entry->info);
27883 inline bool
27884 macinfo_entry_hasher::equal (const macinfo_entry *entry1,
27885 const macinfo_entry *entry2)
27887 return !strcmp (entry1->info, entry2->info);
27890 typedef hash_table<macinfo_entry_hasher> macinfo_hash_type;
27892 /* Output a single .debug_macinfo entry. */
27894 static void
27895 output_macinfo_op (macinfo_entry *ref)
27897 int file_num;
27898 size_t len;
27899 struct indirect_string_node *node;
27900 char label[MAX_ARTIFICIAL_LABEL_BYTES];
27901 struct dwarf_file_data *fd;
27903 switch (ref->code)
27905 case DW_MACINFO_start_file:
27906 fd = lookup_filename (ref->info);
27907 file_num = maybe_emit_file (fd);
27908 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
27909 dw2_asm_output_data_uleb128 (ref->lineno,
27910 "Included from line number %lu",
27911 (unsigned long) ref->lineno);
27912 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
27913 break;
27914 case DW_MACINFO_end_file:
27915 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
27916 break;
27917 case DW_MACINFO_define:
27918 case DW_MACINFO_undef:
27919 len = strlen (ref->info) + 1;
27920 if (!dwarf_strict
27921 && len > DWARF_OFFSET_SIZE
27922 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
27923 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
27925 ref->code = ref->code == DW_MACINFO_define
27926 ? DW_MACRO_define_strp : DW_MACRO_undef_strp;
27927 output_macinfo_op (ref);
27928 return;
27930 dw2_asm_output_data (1, ref->code,
27931 ref->code == DW_MACINFO_define
27932 ? "Define macro" : "Undefine macro");
27933 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
27934 (unsigned long) ref->lineno);
27935 dw2_asm_output_nstring (ref->info, -1, "The macro");
27936 break;
27937 case DW_MACRO_define_strp:
27938 case DW_MACRO_undef_strp:
27939 node = find_AT_string (ref->info);
27940 gcc_assert (node
27941 && (node->form == DW_FORM_strp
27942 || node->form == DW_FORM_GNU_str_index));
27943 dw2_asm_output_data (1, ref->code,
27944 ref->code == DW_MACRO_define_strp
27945 ? "Define macro strp"
27946 : "Undefine macro strp");
27947 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
27948 (unsigned long) ref->lineno);
27949 if (node->form == DW_FORM_strp)
27950 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
27951 debug_str_section, "The macro: \"%s\"",
27952 ref->info);
27953 else
27954 dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
27955 ref->info);
27956 break;
27957 case DW_MACRO_import:
27958 dw2_asm_output_data (1, ref->code, "Import");
27959 ASM_GENERATE_INTERNAL_LABEL (label,
27960 DEBUG_MACRO_SECTION_LABEL,
27961 ref->lineno + macinfo_label_base);
27962 dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
27963 break;
27964 default:
27965 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
27966 ASM_COMMENT_START, (unsigned long) ref->code);
27967 break;
27971 /* Attempt to make a sequence of define/undef macinfo ops shareable with
27972 other compilation unit .debug_macinfo sections. IDX is the first
27973 index of a define/undef, return the number of ops that should be
27974 emitted in a comdat .debug_macinfo section and emit
27975 a DW_MACRO_import entry referencing it.
27976 If the define/undef entry should be emitted normally, return 0. */
27978 static unsigned
27979 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
27980 macinfo_hash_type **macinfo_htab)
27982 macinfo_entry *first, *second, *cur, *inc;
27983 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
27984 unsigned char checksum[16];
27985 struct md5_ctx ctx;
27986 char *grp_name, *tail;
27987 const char *base;
27988 unsigned int i, count, encoded_filename_len, linebuf_len;
27989 macinfo_entry **slot;
27991 first = &(*macinfo_table)[idx];
27992 second = &(*macinfo_table)[idx + 1];
27994 /* Optimize only if there are at least two consecutive define/undef ops,
27995 and either all of them are before first DW_MACINFO_start_file
27996 with lineno {0,1} (i.e. predefined macro block), or all of them are
27997 in some included header file. */
27998 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
27999 return 0;
28000 if (vec_safe_is_empty (files))
28002 if (first->lineno > 1 || second->lineno > 1)
28003 return 0;
28005 else if (first->lineno == 0)
28006 return 0;
28008 /* Find the last define/undef entry that can be grouped together
28009 with first and at the same time compute md5 checksum of their
28010 codes, linenumbers and strings. */
28011 md5_init_ctx (&ctx);
28012 for (i = idx; macinfo_table->iterate (i, &cur); i++)
28013 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
28014 break;
28015 else if (vec_safe_is_empty (files) && cur->lineno > 1)
28016 break;
28017 else
28019 unsigned char code = cur->code;
28020 md5_process_bytes (&code, 1, &ctx);
28021 checksum_uleb128 (cur->lineno, &ctx);
28022 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
28024 md5_finish_ctx (&ctx, checksum);
28025 count = i - idx;
28027 /* From the containing include filename (if any) pick up just
28028 usable characters from its basename. */
28029 if (vec_safe_is_empty (files))
28030 base = "";
28031 else
28032 base = lbasename (files->last ().info);
28033 for (encoded_filename_len = 0, i = 0; base[i]; i++)
28034 if (ISIDNUM (base[i]) || base[i] == '.')
28035 encoded_filename_len++;
28036 /* Count . at the end. */
28037 if (encoded_filename_len)
28038 encoded_filename_len++;
28040 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
28041 linebuf_len = strlen (linebuf);
28043 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
28044 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
28045 + 16 * 2 + 1);
28046 memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
28047 tail = grp_name + 4;
28048 if (encoded_filename_len)
28050 for (i = 0; base[i]; i++)
28051 if (ISIDNUM (base[i]) || base[i] == '.')
28052 *tail++ = base[i];
28053 *tail++ = '.';
28055 memcpy (tail, linebuf, linebuf_len);
28056 tail += linebuf_len;
28057 *tail++ = '.';
28058 for (i = 0; i < 16; i++)
28059 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
28061 /* Construct a macinfo_entry for DW_MACRO_import
28062 in the empty vector entry before the first define/undef. */
28063 inc = &(*macinfo_table)[idx - 1];
28064 inc->code = DW_MACRO_import;
28065 inc->lineno = 0;
28066 inc->info = ggc_strdup (grp_name);
28067 if (!*macinfo_htab)
28068 *macinfo_htab = new macinfo_hash_type (10);
28069 /* Avoid emitting duplicates. */
28070 slot = (*macinfo_htab)->find_slot (inc, INSERT);
28071 if (*slot != NULL)
28073 inc->code = 0;
28074 inc->info = NULL;
28075 /* If such an entry has been used before, just emit
28076 a DW_MACRO_import op. */
28077 inc = *slot;
28078 output_macinfo_op (inc);
28079 /* And clear all macinfo_entry in the range to avoid emitting them
28080 in the second pass. */
28081 for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
28083 cur->code = 0;
28084 cur->info = NULL;
28087 else
28089 *slot = inc;
28090 inc->lineno = (*macinfo_htab)->elements ();
28091 output_macinfo_op (inc);
28093 return count;
28096 /* Save any strings needed by the macinfo table in the debug str
28097 table. All strings must be collected into the table by the time
28098 index_string is called. */
28100 static void
28101 save_macinfo_strings (void)
28103 unsigned len;
28104 unsigned i;
28105 macinfo_entry *ref;
28107 for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
28109 switch (ref->code)
28111 /* Match the logic in output_macinfo_op to decide on
28112 indirect strings. */
28113 case DW_MACINFO_define:
28114 case DW_MACINFO_undef:
28115 len = strlen (ref->info) + 1;
28116 if (!dwarf_strict
28117 && len > DWARF_OFFSET_SIZE
28118 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
28119 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
28120 set_indirect_string (find_AT_string (ref->info));
28121 break;
28122 case DW_MACRO_define_strp:
28123 case DW_MACRO_undef_strp:
28124 set_indirect_string (find_AT_string (ref->info));
28125 break;
28126 default:
28127 break;
28132 /* Output macinfo section(s). */
28134 static void
28135 output_macinfo (const char *debug_line_label, bool early_lto_debug)
28137 unsigned i;
28138 unsigned long length = vec_safe_length (macinfo_table);
28139 macinfo_entry *ref;
28140 vec<macinfo_entry, va_gc> *files = NULL;
28141 macinfo_hash_type *macinfo_htab = NULL;
28142 char dl_section_ref[MAX_ARTIFICIAL_LABEL_BYTES];
28144 if (! length)
28145 return;
28147 /* output_macinfo* uses these interchangeably. */
28148 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_define
28149 && (int) DW_MACINFO_undef == (int) DW_MACRO_undef
28150 && (int) DW_MACINFO_start_file == (int) DW_MACRO_start_file
28151 && (int) DW_MACINFO_end_file == (int) DW_MACRO_end_file);
28153 /* AIX Assembler inserts the length, so adjust the reference to match the
28154 offset expected by debuggers. */
28155 strcpy (dl_section_ref, debug_line_label);
28156 if (XCOFF_DEBUGGING_INFO)
28157 strcat (dl_section_ref, DWARF_INITIAL_LENGTH_SIZE_STR);
28159 /* For .debug_macro emit the section header. */
28160 if (!dwarf_strict || dwarf_version >= 5)
28162 dw2_asm_output_data (2, dwarf_version >= 5 ? 5 : 4,
28163 "DWARF macro version number");
28164 if (DWARF_OFFSET_SIZE == 8)
28165 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
28166 else
28167 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
28168 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_line_label,
28169 debug_line_section, NULL);
28172 /* In the first loop, it emits the primary .debug_macinfo section
28173 and after each emitted op the macinfo_entry is cleared.
28174 If a longer range of define/undef ops can be optimized using
28175 DW_MACRO_import, the DW_MACRO_import op is emitted and kept in
28176 the vector before the first define/undef in the range and the
28177 whole range of define/undef ops is not emitted and kept. */
28178 for (i = 0; macinfo_table->iterate (i, &ref); i++)
28180 switch (ref->code)
28182 case DW_MACINFO_start_file:
28183 vec_safe_push (files, *ref);
28184 break;
28185 case DW_MACINFO_end_file:
28186 if (!vec_safe_is_empty (files))
28187 files->pop ();
28188 break;
28189 case DW_MACINFO_define:
28190 case DW_MACINFO_undef:
28191 if ((!dwarf_strict || dwarf_version >= 5)
28192 && HAVE_COMDAT_GROUP
28193 && vec_safe_length (files) != 1
28194 && i > 0
28195 && i + 1 < length
28196 && (*macinfo_table)[i - 1].code == 0)
28198 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
28199 if (count)
28201 i += count - 1;
28202 continue;
28205 break;
28206 case 0:
28207 /* A dummy entry may be inserted at the beginning to be able
28208 to optimize the whole block of predefined macros. */
28209 if (i == 0)
28210 continue;
28211 default:
28212 break;
28214 output_macinfo_op (ref);
28215 ref->info = NULL;
28216 ref->code = 0;
28219 if (!macinfo_htab)
28220 return;
28222 /* Save the number of transparent includes so we can adjust the
28223 label number for the fat LTO object DWARF. */
28224 unsigned macinfo_label_base_adj = macinfo_htab->elements ();
28226 delete macinfo_htab;
28227 macinfo_htab = NULL;
28229 /* If any DW_MACRO_import were used, on those DW_MACRO_import entries
28230 terminate the current chain and switch to a new comdat .debug_macinfo
28231 section and emit the define/undef entries within it. */
28232 for (i = 0; macinfo_table->iterate (i, &ref); i++)
28233 switch (ref->code)
28235 case 0:
28236 continue;
28237 case DW_MACRO_import:
28239 char label[MAX_ARTIFICIAL_LABEL_BYTES];
28240 tree comdat_key = get_identifier (ref->info);
28241 /* Terminate the previous .debug_macinfo section. */
28242 dw2_asm_output_data (1, 0, "End compilation unit");
28243 targetm.asm_out.named_section (debug_macinfo_section_name,
28244 SECTION_DEBUG
28245 | SECTION_LINKONCE
28246 | (early_lto_debug
28247 ? SECTION_EXCLUDE : 0),
28248 comdat_key);
28249 ASM_GENERATE_INTERNAL_LABEL (label,
28250 DEBUG_MACRO_SECTION_LABEL,
28251 ref->lineno + macinfo_label_base);
28252 ASM_OUTPUT_LABEL (asm_out_file, label);
28253 ref->code = 0;
28254 ref->info = NULL;
28255 dw2_asm_output_data (2, dwarf_version >= 5 ? 5 : 4,
28256 "DWARF macro version number");
28257 if (DWARF_OFFSET_SIZE == 8)
28258 dw2_asm_output_data (1, 1, "Flags: 64-bit");
28259 else
28260 dw2_asm_output_data (1, 0, "Flags: 32-bit");
28262 break;
28263 case DW_MACINFO_define:
28264 case DW_MACINFO_undef:
28265 output_macinfo_op (ref);
28266 ref->code = 0;
28267 ref->info = NULL;
28268 break;
28269 default:
28270 gcc_unreachable ();
28273 macinfo_label_base += macinfo_label_base_adj;
28276 /* Initialize the various sections and labels for dwarf output and prefix
28277 them with PREFIX if non-NULL. Returns the generation (zero based
28278 number of times function was called). */
28280 static unsigned
28281 init_sections_and_labels (bool early_lto_debug)
28283 /* As we may get called multiple times have a generation count for
28284 labels. */
28285 static unsigned generation = 0;
28287 if (early_lto_debug)
28289 if (!dwarf_split_debug_info)
28291 debug_info_section = get_section (DEBUG_LTO_INFO_SECTION,
28292 SECTION_DEBUG | SECTION_EXCLUDE,
28293 NULL);
28294 debug_abbrev_section = get_section (DEBUG_LTO_ABBREV_SECTION,
28295 SECTION_DEBUG | SECTION_EXCLUDE,
28296 NULL);
28297 debug_macinfo_section_name
28298 = ((dwarf_strict && dwarf_version < 5)
28299 ? DEBUG_LTO_MACINFO_SECTION : DEBUG_LTO_MACRO_SECTION);
28300 debug_macinfo_section = get_section (debug_macinfo_section_name,
28301 SECTION_DEBUG
28302 | SECTION_EXCLUDE, NULL);
28303 /* For macro info we have to refer to a debug_line section, so
28304 similar to split-dwarf emit a skeleton one for early debug. */
28305 debug_skeleton_line_section
28306 = get_section (DEBUG_LTO_LINE_SECTION,
28307 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
28308 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
28309 DEBUG_SKELETON_LINE_SECTION_LABEL,
28310 generation);
28312 else
28314 /* ??? Which of the following do we need early? */
28315 debug_info_section = get_section (DEBUG_LTO_DWO_INFO_SECTION,
28316 SECTION_DEBUG | SECTION_EXCLUDE,
28317 NULL);
28318 debug_abbrev_section = get_section (DEBUG_LTO_DWO_ABBREV_SECTION,
28319 SECTION_DEBUG | SECTION_EXCLUDE,
28320 NULL);
28321 debug_skeleton_info_section = get_section (DEBUG_LTO_INFO_SECTION,
28322 SECTION_DEBUG
28323 | SECTION_EXCLUDE, NULL);
28324 debug_skeleton_abbrev_section
28325 = get_section (DEBUG_LTO_ABBREV_SECTION,
28326 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
28327 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
28328 DEBUG_SKELETON_ABBREV_SECTION_LABEL,
28329 generation);
28331 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections
28332 stay in the main .o, but the skeleton_line goes into the split
28333 off dwo. */
28334 debug_skeleton_line_section
28335 = get_section (DEBUG_LTO_LINE_SECTION,
28336 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
28337 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
28338 DEBUG_SKELETON_LINE_SECTION_LABEL,
28339 generation);
28340 debug_str_offsets_section
28341 = get_section (DEBUG_LTO_DWO_STR_OFFSETS_SECTION,
28342 SECTION_DEBUG | SECTION_EXCLUDE,
28343 NULL);
28344 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
28345 DEBUG_SKELETON_INFO_SECTION_LABEL,
28346 generation);
28347 debug_str_dwo_section = get_section (DEBUG_LTO_STR_DWO_SECTION,
28348 DEBUG_STR_DWO_SECTION_FLAGS,
28349 NULL);
28350 debug_macinfo_section_name
28351 = ((dwarf_strict && dwarf_version < 5)
28352 ? DEBUG_LTO_DWO_MACINFO_SECTION : DEBUG_LTO_DWO_MACRO_SECTION);
28353 debug_macinfo_section = get_section (debug_macinfo_section_name,
28354 SECTION_DEBUG | SECTION_EXCLUDE,
28355 NULL);
28357 debug_str_section = get_section (DEBUG_LTO_STR_SECTION,
28358 DEBUG_STR_SECTION_FLAGS
28359 | SECTION_EXCLUDE, NULL);
28360 if (!dwarf_split_debug_info && !dwarf2out_as_loc_support)
28361 debug_line_str_section
28362 = get_section (DEBUG_LTO_LINE_STR_SECTION,
28363 DEBUG_STR_SECTION_FLAGS | SECTION_EXCLUDE, NULL);
28365 else
28367 if (!dwarf_split_debug_info)
28369 debug_info_section = get_section (DEBUG_INFO_SECTION,
28370 SECTION_DEBUG, NULL);
28371 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
28372 SECTION_DEBUG, NULL);
28373 debug_loc_section = get_section (dwarf_version >= 5
28374 ? DEBUG_LOCLISTS_SECTION
28375 : DEBUG_LOC_SECTION,
28376 SECTION_DEBUG, NULL);
28377 debug_macinfo_section_name
28378 = ((dwarf_strict && dwarf_version < 5)
28379 ? DEBUG_MACINFO_SECTION : DEBUG_MACRO_SECTION);
28380 debug_macinfo_section = get_section (debug_macinfo_section_name,
28381 SECTION_DEBUG, NULL);
28383 else
28385 debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
28386 SECTION_DEBUG | SECTION_EXCLUDE,
28387 NULL);
28388 debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
28389 SECTION_DEBUG | SECTION_EXCLUDE,
28390 NULL);
28391 debug_addr_section = get_section (DEBUG_ADDR_SECTION,
28392 SECTION_DEBUG, NULL);
28393 debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
28394 SECTION_DEBUG, NULL);
28395 debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
28396 SECTION_DEBUG, NULL);
28397 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
28398 DEBUG_SKELETON_ABBREV_SECTION_LABEL,
28399 generation);
28401 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections
28402 stay in the main .o, but the skeleton_line goes into the
28403 split off dwo. */
28404 debug_skeleton_line_section
28405 = get_section (DEBUG_DWO_LINE_SECTION,
28406 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
28407 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
28408 DEBUG_SKELETON_LINE_SECTION_LABEL,
28409 generation);
28410 debug_str_offsets_section
28411 = get_section (DEBUG_DWO_STR_OFFSETS_SECTION,
28412 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
28413 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
28414 DEBUG_SKELETON_INFO_SECTION_LABEL,
28415 generation);
28416 debug_loc_section = get_section (dwarf_version >= 5
28417 ? DEBUG_DWO_LOCLISTS_SECTION
28418 : DEBUG_DWO_LOC_SECTION,
28419 SECTION_DEBUG | SECTION_EXCLUDE,
28420 NULL);
28421 debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
28422 DEBUG_STR_DWO_SECTION_FLAGS,
28423 NULL);
28424 debug_macinfo_section_name
28425 = ((dwarf_strict && dwarf_version < 5)
28426 ? DEBUG_DWO_MACINFO_SECTION : DEBUG_DWO_MACRO_SECTION);
28427 debug_macinfo_section = get_section (debug_macinfo_section_name,
28428 SECTION_DEBUG | SECTION_EXCLUDE,
28429 NULL);
28431 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
28432 SECTION_DEBUG, NULL);
28433 debug_line_section = get_section (DEBUG_LINE_SECTION,
28434 SECTION_DEBUG, NULL);
28435 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
28436 SECTION_DEBUG, NULL);
28437 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
28438 SECTION_DEBUG, NULL);
28439 debug_str_section = get_section (DEBUG_STR_SECTION,
28440 DEBUG_STR_SECTION_FLAGS, NULL);
28441 if (!dwarf_split_debug_info && !output_asm_line_debug_info ())
28442 debug_line_str_section = get_section (DEBUG_LINE_STR_SECTION,
28443 DEBUG_STR_SECTION_FLAGS, NULL);
28445 debug_ranges_section = get_section (dwarf_version >= 5
28446 ? DEBUG_RNGLISTS_SECTION
28447 : DEBUG_RANGES_SECTION,
28448 SECTION_DEBUG, NULL);
28449 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
28450 SECTION_DEBUG, NULL);
28453 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
28454 DEBUG_ABBREV_SECTION_LABEL, generation);
28455 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
28456 DEBUG_INFO_SECTION_LABEL, generation);
28457 info_section_emitted = false;
28458 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
28459 DEBUG_LINE_SECTION_LABEL, generation);
28460 /* There are up to 4 unique ranges labels per generation.
28461 See also output_rnglists. */
28462 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
28463 DEBUG_RANGES_SECTION_LABEL, generation * 4);
28464 if (dwarf_version >= 5 && dwarf_split_debug_info)
28465 ASM_GENERATE_INTERNAL_LABEL (ranges_base_label,
28466 DEBUG_RANGES_SECTION_LABEL,
28467 1 + generation * 4);
28468 ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
28469 DEBUG_ADDR_SECTION_LABEL, generation);
28470 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
28471 (dwarf_strict && dwarf_version < 5)
28472 ? DEBUG_MACINFO_SECTION_LABEL
28473 : DEBUG_MACRO_SECTION_LABEL, generation);
28474 ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL,
28475 generation);
28477 ++generation;
28478 return generation - 1;
28481 /* Set up for Dwarf output at the start of compilation. */
28483 static void
28484 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
28486 /* Allocate the file_table. */
28487 file_table = hash_table<dwarf_file_hasher>::create_ggc (50);
28489 #ifndef DWARF2_LINENO_DEBUGGING_INFO
28490 /* Allocate the decl_die_table. */
28491 decl_die_table = hash_table<decl_die_hasher>::create_ggc (10);
28493 /* Allocate the decl_loc_table. */
28494 decl_loc_table = hash_table<decl_loc_hasher>::create_ggc (10);
28496 /* Allocate the cached_dw_loc_list_table. */
28497 cached_dw_loc_list_table = hash_table<dw_loc_list_hasher>::create_ggc (10);
28499 /* Allocate the initial hunk of the decl_scope_table. */
28500 vec_alloc (decl_scope_table, 256);
28502 /* Allocate the initial hunk of the abbrev_die_table. */
28503 vec_alloc (abbrev_die_table, 256);
28504 /* Zero-th entry is allocated, but unused. */
28505 abbrev_die_table->quick_push (NULL);
28507 /* Allocate the dwarf_proc_stack_usage_map. */
28508 dwarf_proc_stack_usage_map = new hash_map<dw_die_ref, int>;
28510 /* Allocate the pubtypes and pubnames vectors. */
28511 vec_alloc (pubname_table, 32);
28512 vec_alloc (pubtype_table, 32);
28514 vec_alloc (incomplete_types, 64);
28516 vec_alloc (used_rtx_array, 32);
28518 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
28519 vec_alloc (macinfo_table, 64);
28520 #endif
28522 /* If front-ends already registered a main translation unit but we were not
28523 ready to perform the association, do this now. */
28524 if (main_translation_unit != NULL_TREE)
28525 equate_decl_number_to_die (main_translation_unit, comp_unit_die ());
28528 /* Called before compile () starts outputtting functions, variables
28529 and toplevel asms into assembly. */
28531 static void
28532 dwarf2out_assembly_start (void)
28534 if (text_section_line_info)
28535 return;
28537 #ifndef DWARF2_LINENO_DEBUGGING_INFO
28538 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
28539 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
28540 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
28541 COLD_TEXT_SECTION_LABEL, 0);
28542 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
28544 switch_to_section (text_section);
28545 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
28546 #endif
28548 /* Make sure the line number table for .text always exists. */
28549 text_section_line_info = new_line_info_table ();
28550 text_section_line_info->end_label = text_end_label;
28552 #ifdef DWARF2_LINENO_DEBUGGING_INFO
28553 cur_line_info_table = text_section_line_info;
28554 #endif
28556 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
28557 && dwarf2out_do_cfi_asm ()
28558 && !dwarf2out_do_eh_frame ())
28559 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
28562 /* A helper function for dwarf2out_finish called through
28563 htab_traverse. Assign a string its index. All strings must be
28564 collected into the table by the time index_string is called,
28565 because the indexing code relies on htab_traverse to traverse nodes
28566 in the same order for each run. */
28569 index_string (indirect_string_node **h, unsigned int *index)
28571 indirect_string_node *node = *h;
28573 find_string_form (node);
28574 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
28576 gcc_assert (node->index == NO_INDEX_ASSIGNED);
28577 node->index = *index;
28578 *index += 1;
28580 return 1;
28583 /* A helper function for output_indirect_strings called through
28584 htab_traverse. Output the offset to a string and update the
28585 current offset. */
28588 output_index_string_offset (indirect_string_node **h, unsigned int *offset)
28590 indirect_string_node *node = *h;
28592 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
28594 /* Assert that this node has been assigned an index. */
28595 gcc_assert (node->index != NO_INDEX_ASSIGNED
28596 && node->index != NOT_INDEXED);
28597 dw2_asm_output_data (DWARF_OFFSET_SIZE, *offset,
28598 "indexed string 0x%x: %s", node->index, node->str);
28599 *offset += strlen (node->str) + 1;
28601 return 1;
28604 /* A helper function for dwarf2out_finish called through
28605 htab_traverse. Output the indexed string. */
28608 output_index_string (indirect_string_node **h, unsigned int *cur_idx)
28610 struct indirect_string_node *node = *h;
28612 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
28614 /* Assert that the strings are output in the same order as their
28615 indexes were assigned. */
28616 gcc_assert (*cur_idx == node->index);
28617 assemble_string (node->str, strlen (node->str) + 1);
28618 *cur_idx += 1;
28620 return 1;
28623 /* A helper function for dwarf2out_finish called through
28624 htab_traverse. Emit one queued .debug_str string. */
28627 output_indirect_string (indirect_string_node **h, enum dwarf_form form)
28629 struct indirect_string_node *node = *h;
28631 node->form = find_string_form (node);
28632 if (node->form == form && node->refcount > 0)
28634 ASM_OUTPUT_LABEL (asm_out_file, node->label);
28635 assemble_string (node->str, strlen (node->str) + 1);
28638 return 1;
28641 /* Output the indexed string table. */
28643 static void
28644 output_indirect_strings (void)
28646 switch_to_section (debug_str_section);
28647 if (!dwarf_split_debug_info)
28648 debug_str_hash->traverse<enum dwarf_form,
28649 output_indirect_string> (DW_FORM_strp);
28650 else
28652 unsigned int offset = 0;
28653 unsigned int cur_idx = 0;
28655 if (skeleton_debug_str_hash)
28656 skeleton_debug_str_hash->traverse<enum dwarf_form,
28657 output_indirect_string> (DW_FORM_strp);
28659 switch_to_section (debug_str_offsets_section);
28660 debug_str_hash->traverse_noresize
28661 <unsigned int *, output_index_string_offset> (&offset);
28662 switch_to_section (debug_str_dwo_section);
28663 debug_str_hash->traverse_noresize<unsigned int *, output_index_string>
28664 (&cur_idx);
28668 /* Callback for htab_traverse to assign an index to an entry in the
28669 table, and to write that entry to the .debug_addr section. */
28672 output_addr_table_entry (addr_table_entry **slot, unsigned int *cur_index)
28674 addr_table_entry *entry = *slot;
28676 if (entry->refcount == 0)
28678 gcc_assert (entry->index == NO_INDEX_ASSIGNED
28679 || entry->index == NOT_INDEXED);
28680 return 1;
28683 gcc_assert (entry->index == *cur_index);
28684 (*cur_index)++;
28686 switch (entry->kind)
28688 case ate_kind_rtx:
28689 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
28690 "0x%x", entry->index);
28691 break;
28692 case ate_kind_rtx_dtprel:
28693 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
28694 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
28695 DWARF2_ADDR_SIZE,
28696 entry->addr.rtl);
28697 fputc ('\n', asm_out_file);
28698 break;
28699 case ate_kind_label:
28700 dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
28701 "0x%x", entry->index);
28702 break;
28703 default:
28704 gcc_unreachable ();
28706 return 1;
28709 /* Produce the .debug_addr section. */
28711 static void
28712 output_addr_table (void)
28714 unsigned int index = 0;
28715 if (addr_index_table == NULL || addr_index_table->size () == 0)
28716 return;
28718 switch_to_section (debug_addr_section);
28719 addr_index_table
28720 ->traverse_noresize<unsigned int *, output_addr_table_entry> (&index);
28723 #if ENABLE_ASSERT_CHECKING
28724 /* Verify that all marks are clear. */
28726 static void
28727 verify_marks_clear (dw_die_ref die)
28729 dw_die_ref c;
28731 gcc_assert (! die->die_mark);
28732 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
28734 #endif /* ENABLE_ASSERT_CHECKING */
28736 /* Clear the marks for a die and its children.
28737 Be cool if the mark isn't set. */
28739 static void
28740 prune_unmark_dies (dw_die_ref die)
28742 dw_die_ref c;
28744 if (die->die_mark)
28745 die->die_mark = 0;
28746 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
28749 /* Given LOC that is referenced by a DIE we're marking as used, find all
28750 referenced DWARF procedures it references and mark them as used. */
28752 static void
28753 prune_unused_types_walk_loc_descr (dw_loc_descr_ref loc)
28755 for (; loc != NULL; loc = loc->dw_loc_next)
28756 switch (loc->dw_loc_opc)
28758 case DW_OP_implicit_pointer:
28759 case DW_OP_convert:
28760 case DW_OP_reinterpret:
28761 case DW_OP_GNU_implicit_pointer:
28762 case DW_OP_GNU_convert:
28763 case DW_OP_GNU_reinterpret:
28764 if (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref)
28765 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
28766 break;
28767 case DW_OP_GNU_variable_value:
28768 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
28770 dw_die_ref ref
28771 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
28772 if (ref == NULL)
28773 break;
28774 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
28775 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
28776 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
28778 /* FALLTHRU */
28779 case DW_OP_call2:
28780 case DW_OP_call4:
28781 case DW_OP_call_ref:
28782 case DW_OP_const_type:
28783 case DW_OP_GNU_const_type:
28784 case DW_OP_GNU_parameter_ref:
28785 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref);
28786 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
28787 break;
28788 case DW_OP_regval_type:
28789 case DW_OP_deref_type:
28790 case DW_OP_GNU_regval_type:
28791 case DW_OP_GNU_deref_type:
28792 gcc_assert (loc->dw_loc_oprnd2.val_class == dw_val_class_die_ref);
28793 prune_unused_types_mark (loc->dw_loc_oprnd2.v.val_die_ref.die, 1);
28794 break;
28795 case DW_OP_entry_value:
28796 case DW_OP_GNU_entry_value:
28797 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_loc);
28798 prune_unused_types_walk_loc_descr (loc->dw_loc_oprnd1.v.val_loc);
28799 break;
28800 default:
28801 break;
28805 /* Given DIE that we're marking as used, find any other dies
28806 it references as attributes and mark them as used. */
28808 static void
28809 prune_unused_types_walk_attribs (dw_die_ref die)
28811 dw_attr_node *a;
28812 unsigned ix;
28814 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
28816 switch (AT_class (a))
28818 /* Make sure DWARF procedures referenced by location descriptions will
28819 get emitted. */
28820 case dw_val_class_loc:
28821 prune_unused_types_walk_loc_descr (AT_loc (a));
28822 break;
28823 case dw_val_class_loc_list:
28824 for (dw_loc_list_ref list = AT_loc_list (a);
28825 list != NULL;
28826 list = list->dw_loc_next)
28827 prune_unused_types_walk_loc_descr (list->expr);
28828 break;
28830 case dw_val_class_view_list:
28831 /* This points to a loc_list in another attribute, so it's
28832 already covered. */
28833 break;
28835 case dw_val_class_die_ref:
28836 /* A reference to another DIE.
28837 Make sure that it will get emitted.
28838 If it was broken out into a comdat group, don't follow it. */
28839 if (! AT_ref (a)->comdat_type_p
28840 || a->dw_attr == DW_AT_specification)
28841 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
28842 break;
28844 case dw_val_class_str:
28845 /* Set the string's refcount to 0 so that prune_unused_types_mark
28846 accounts properly for it. */
28847 a->dw_attr_val.v.val_str->refcount = 0;
28848 break;
28850 default:
28851 break;
28856 /* Mark the generic parameters and arguments children DIEs of DIE. */
28858 static void
28859 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
28861 dw_die_ref c;
28863 if (die == NULL || die->die_child == NULL)
28864 return;
28865 c = die->die_child;
28868 if (is_template_parameter (c))
28869 prune_unused_types_mark (c, 1);
28870 c = c->die_sib;
28871 } while (c && c != die->die_child);
28874 /* Mark DIE as being used. If DOKIDS is true, then walk down
28875 to DIE's children. */
28877 static void
28878 prune_unused_types_mark (dw_die_ref die, int dokids)
28880 dw_die_ref c;
28882 if (die->die_mark == 0)
28884 /* We haven't done this node yet. Mark it as used. */
28885 die->die_mark = 1;
28886 /* If this is the DIE of a generic type instantiation,
28887 mark the children DIEs that describe its generic parms and
28888 args. */
28889 prune_unused_types_mark_generic_parms_dies (die);
28891 /* We also have to mark its parents as used.
28892 (But we don't want to mark our parent's kids due to this,
28893 unless it is a class.) */
28894 if (die->die_parent)
28895 prune_unused_types_mark (die->die_parent,
28896 class_scope_p (die->die_parent));
28898 /* Mark any referenced nodes. */
28899 prune_unused_types_walk_attribs (die);
28901 /* If this node is a specification,
28902 also mark the definition, if it exists. */
28903 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
28904 prune_unused_types_mark (die->die_definition, 1);
28907 if (dokids && die->die_mark != 2)
28909 /* We need to walk the children, but haven't done so yet.
28910 Remember that we've walked the kids. */
28911 die->die_mark = 2;
28913 /* If this is an array type, we need to make sure our
28914 kids get marked, even if they're types. If we're
28915 breaking out types into comdat sections, do this
28916 for all type definitions. */
28917 if (die->die_tag == DW_TAG_array_type
28918 || (use_debug_types
28919 && is_type_die (die) && ! is_declaration_die (die)))
28920 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
28921 else
28922 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
28926 /* For local classes, look if any static member functions were emitted
28927 and if so, mark them. */
28929 static void
28930 prune_unused_types_walk_local_classes (dw_die_ref die)
28932 dw_die_ref c;
28934 if (die->die_mark == 2)
28935 return;
28937 switch (die->die_tag)
28939 case DW_TAG_structure_type:
28940 case DW_TAG_union_type:
28941 case DW_TAG_class_type:
28942 break;
28944 case DW_TAG_subprogram:
28945 if (!get_AT_flag (die, DW_AT_declaration)
28946 || die->die_definition != NULL)
28947 prune_unused_types_mark (die, 1);
28948 return;
28950 default:
28951 return;
28954 /* Mark children. */
28955 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
28958 /* Walk the tree DIE and mark types that we actually use. */
28960 static void
28961 prune_unused_types_walk (dw_die_ref die)
28963 dw_die_ref c;
28965 /* Don't do anything if this node is already marked and
28966 children have been marked as well. */
28967 if (die->die_mark == 2)
28968 return;
28970 switch (die->die_tag)
28972 case DW_TAG_structure_type:
28973 case DW_TAG_union_type:
28974 case DW_TAG_class_type:
28975 if (die->die_perennial_p)
28976 break;
28978 for (c = die->die_parent; c; c = c->die_parent)
28979 if (c->die_tag == DW_TAG_subprogram)
28980 break;
28982 /* Finding used static member functions inside of classes
28983 is needed just for local classes, because for other classes
28984 static member function DIEs with DW_AT_specification
28985 are emitted outside of the DW_TAG_*_type. If we ever change
28986 it, we'd need to call this even for non-local classes. */
28987 if (c)
28988 prune_unused_types_walk_local_classes (die);
28990 /* It's a type node --- don't mark it. */
28991 return;
28993 case DW_TAG_const_type:
28994 case DW_TAG_packed_type:
28995 case DW_TAG_pointer_type:
28996 case DW_TAG_reference_type:
28997 case DW_TAG_rvalue_reference_type:
28998 case DW_TAG_volatile_type:
28999 case DW_TAG_typedef:
29000 case DW_TAG_array_type:
29001 case DW_TAG_interface_type:
29002 case DW_TAG_friend:
29003 case DW_TAG_enumeration_type:
29004 case DW_TAG_subroutine_type:
29005 case DW_TAG_string_type:
29006 case DW_TAG_set_type:
29007 case DW_TAG_subrange_type:
29008 case DW_TAG_ptr_to_member_type:
29009 case DW_TAG_file_type:
29010 /* Type nodes are useful only when other DIEs reference them --- don't
29011 mark them. */
29012 /* FALLTHROUGH */
29014 case DW_TAG_dwarf_procedure:
29015 /* Likewise for DWARF procedures. */
29017 if (die->die_perennial_p)
29018 break;
29020 return;
29022 default:
29023 /* Mark everything else. */
29024 break;
29027 if (die->die_mark == 0)
29029 die->die_mark = 1;
29031 /* Now, mark any dies referenced from here. */
29032 prune_unused_types_walk_attribs (die);
29035 die->die_mark = 2;
29037 /* Mark children. */
29038 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
29041 /* Increment the string counts on strings referred to from DIE's
29042 attributes. */
29044 static void
29045 prune_unused_types_update_strings (dw_die_ref die)
29047 dw_attr_node *a;
29048 unsigned ix;
29050 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
29051 if (AT_class (a) == dw_val_class_str)
29053 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
29054 s->refcount++;
29055 /* Avoid unnecessarily putting strings that are used less than
29056 twice in the hash table. */
29057 if (s->refcount
29058 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
29060 indirect_string_node **slot
29061 = debug_str_hash->find_slot_with_hash (s->str,
29062 htab_hash_string (s->str),
29063 INSERT);
29064 gcc_assert (*slot == NULL);
29065 *slot = s;
29070 /* Mark DIE and its children as removed. */
29072 static void
29073 mark_removed (dw_die_ref die)
29075 dw_die_ref c;
29076 die->removed = true;
29077 FOR_EACH_CHILD (die, c, mark_removed (c));
29080 /* Remove from the tree DIE any dies that aren't marked. */
29082 static void
29083 prune_unused_types_prune (dw_die_ref die)
29085 dw_die_ref c;
29087 gcc_assert (die->die_mark);
29088 prune_unused_types_update_strings (die);
29090 if (! die->die_child)
29091 return;
29093 c = die->die_child;
29094 do {
29095 dw_die_ref prev = c, next;
29096 for (c = c->die_sib; ! c->die_mark; c = next)
29097 if (c == die->die_child)
29099 /* No marked children between 'prev' and the end of the list. */
29100 if (prev == c)
29101 /* No marked children at all. */
29102 die->die_child = NULL;
29103 else
29105 prev->die_sib = c->die_sib;
29106 die->die_child = prev;
29108 c->die_sib = NULL;
29109 mark_removed (c);
29110 return;
29112 else
29114 next = c->die_sib;
29115 c->die_sib = NULL;
29116 mark_removed (c);
29119 if (c != prev->die_sib)
29120 prev->die_sib = c;
29121 prune_unused_types_prune (c);
29122 } while (c != die->die_child);
29125 /* Remove dies representing declarations that we never use. */
29127 static void
29128 prune_unused_types (void)
29130 unsigned int i;
29131 limbo_die_node *node;
29132 comdat_type_node *ctnode;
29133 pubname_entry *pub;
29134 dw_die_ref base_type;
29136 #if ENABLE_ASSERT_CHECKING
29137 /* All the marks should already be clear. */
29138 verify_marks_clear (comp_unit_die ());
29139 for (node = limbo_die_list; node; node = node->next)
29140 verify_marks_clear (node->die);
29141 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
29142 verify_marks_clear (ctnode->root_die);
29143 #endif /* ENABLE_ASSERT_CHECKING */
29145 /* Mark types that are used in global variables. */
29146 premark_types_used_by_global_vars ();
29148 /* Set the mark on nodes that are actually used. */
29149 prune_unused_types_walk (comp_unit_die ());
29150 for (node = limbo_die_list; node; node = node->next)
29151 prune_unused_types_walk (node->die);
29152 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
29154 prune_unused_types_walk (ctnode->root_die);
29155 prune_unused_types_mark (ctnode->type_die, 1);
29158 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
29159 are unusual in that they are pubnames that are the children of pubtypes.
29160 They should only be marked via their parent DW_TAG_enumeration_type die,
29161 not as roots in themselves. */
29162 FOR_EACH_VEC_ELT (*pubname_table, i, pub)
29163 if (pub->die->die_tag != DW_TAG_enumerator)
29164 prune_unused_types_mark (pub->die, 1);
29165 for (i = 0; base_types.iterate (i, &base_type); i++)
29166 prune_unused_types_mark (base_type, 1);
29168 /* For -fvar-tracking-assignments, also set the mark on nodes that could be
29169 referenced by DW_TAG_call_site DW_AT_call_origin (i.e. direct call
29170 callees). */
29171 cgraph_node *cnode;
29172 FOR_EACH_FUNCTION (cnode)
29173 if (cnode->referred_to_p (false))
29175 dw_die_ref die = lookup_decl_die (cnode->decl);
29176 if (die == NULL || die->die_mark)
29177 continue;
29178 for (cgraph_edge *e = cnode->callers; e; e = e->next_caller)
29179 if (e->caller != cnode
29180 && opt_for_fn (e->caller->decl, flag_var_tracking_assignments))
29182 prune_unused_types_mark (die, 1);
29183 break;
29187 if (debug_str_hash)
29188 debug_str_hash->empty ();
29189 if (skeleton_debug_str_hash)
29190 skeleton_debug_str_hash->empty ();
29191 prune_unused_types_prune (comp_unit_die ());
29192 for (limbo_die_node **pnode = &limbo_die_list; *pnode; )
29194 node = *pnode;
29195 if (!node->die->die_mark)
29196 *pnode = node->next;
29197 else
29199 prune_unused_types_prune (node->die);
29200 pnode = &node->next;
29203 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
29204 prune_unused_types_prune (ctnode->root_die);
29206 /* Leave the marks clear. */
29207 prune_unmark_dies (comp_unit_die ());
29208 for (node = limbo_die_list; node; node = node->next)
29209 prune_unmark_dies (node->die);
29210 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
29211 prune_unmark_dies (ctnode->root_die);
29214 /* Helpers to manipulate hash table of comdat type units. */
29216 struct comdat_type_hasher : nofree_ptr_hash <comdat_type_node>
29218 static inline hashval_t hash (const comdat_type_node *);
29219 static inline bool equal (const comdat_type_node *, const comdat_type_node *);
29222 inline hashval_t
29223 comdat_type_hasher::hash (const comdat_type_node *type_node)
29225 hashval_t h;
29226 memcpy (&h, type_node->signature, sizeof (h));
29227 return h;
29230 inline bool
29231 comdat_type_hasher::equal (const comdat_type_node *type_node_1,
29232 const comdat_type_node *type_node_2)
29234 return (! memcmp (type_node_1->signature, type_node_2->signature,
29235 DWARF_TYPE_SIGNATURE_SIZE));
29238 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
29239 to the location it would have been added, should we know its
29240 DECL_ASSEMBLER_NAME when we added other attributes. This will
29241 probably improve compactness of debug info, removing equivalent
29242 abbrevs, and hide any differences caused by deferring the
29243 computation of the assembler name, triggered by e.g. PCH. */
29245 static inline void
29246 move_linkage_attr (dw_die_ref die)
29248 unsigned ix = vec_safe_length (die->die_attr);
29249 dw_attr_node linkage = (*die->die_attr)[ix - 1];
29251 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
29252 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
29254 while (--ix > 0)
29256 dw_attr_node *prev = &(*die->die_attr)[ix - 1];
29258 if (prev->dw_attr == DW_AT_decl_line
29259 || prev->dw_attr == DW_AT_decl_column
29260 || prev->dw_attr == DW_AT_name)
29261 break;
29264 if (ix != vec_safe_length (die->die_attr) - 1)
29266 die->die_attr->pop ();
29267 die->die_attr->quick_insert (ix, linkage);
29271 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
29272 referenced from typed stack ops and count how often they are used. */
29274 static void
29275 mark_base_types (dw_loc_descr_ref loc)
29277 dw_die_ref base_type = NULL;
29279 for (; loc; loc = loc->dw_loc_next)
29281 switch (loc->dw_loc_opc)
29283 case DW_OP_regval_type:
29284 case DW_OP_deref_type:
29285 case DW_OP_GNU_regval_type:
29286 case DW_OP_GNU_deref_type:
29287 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
29288 break;
29289 case DW_OP_convert:
29290 case DW_OP_reinterpret:
29291 case DW_OP_GNU_convert:
29292 case DW_OP_GNU_reinterpret:
29293 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
29294 continue;
29295 /* FALLTHRU */
29296 case DW_OP_const_type:
29297 case DW_OP_GNU_const_type:
29298 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
29299 break;
29300 case DW_OP_entry_value:
29301 case DW_OP_GNU_entry_value:
29302 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
29303 continue;
29304 default:
29305 continue;
29307 gcc_assert (base_type->die_parent == comp_unit_die ());
29308 if (base_type->die_mark)
29309 base_type->die_mark++;
29310 else
29312 base_types.safe_push (base_type);
29313 base_type->die_mark = 1;
29318 /* Comparison function for sorting marked base types. */
29320 static int
29321 base_type_cmp (const void *x, const void *y)
29323 dw_die_ref dx = *(const dw_die_ref *) x;
29324 dw_die_ref dy = *(const dw_die_ref *) y;
29325 unsigned int byte_size1, byte_size2;
29326 unsigned int encoding1, encoding2;
29327 unsigned int align1, align2;
29328 if (dx->die_mark > dy->die_mark)
29329 return -1;
29330 if (dx->die_mark < dy->die_mark)
29331 return 1;
29332 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
29333 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
29334 if (byte_size1 < byte_size2)
29335 return 1;
29336 if (byte_size1 > byte_size2)
29337 return -1;
29338 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
29339 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
29340 if (encoding1 < encoding2)
29341 return 1;
29342 if (encoding1 > encoding2)
29343 return -1;
29344 align1 = get_AT_unsigned (dx, DW_AT_alignment);
29345 align2 = get_AT_unsigned (dy, DW_AT_alignment);
29346 if (align1 < align2)
29347 return 1;
29348 if (align1 > align2)
29349 return -1;
29350 return 0;
29353 /* Move base types marked by mark_base_types as early as possible
29354 in the CU, sorted by decreasing usage count both to make the
29355 uleb128 references as small as possible and to make sure they
29356 will have die_offset already computed by calc_die_sizes when
29357 sizes of typed stack loc ops is computed. */
29359 static void
29360 move_marked_base_types (void)
29362 unsigned int i;
29363 dw_die_ref base_type, die, c;
29365 if (base_types.is_empty ())
29366 return;
29368 /* Sort by decreasing usage count, they will be added again in that
29369 order later on. */
29370 base_types.qsort (base_type_cmp);
29371 die = comp_unit_die ();
29372 c = die->die_child;
29375 dw_die_ref prev = c;
29376 c = c->die_sib;
29377 while (c->die_mark)
29379 remove_child_with_prev (c, prev);
29380 /* As base types got marked, there must be at least
29381 one node other than DW_TAG_base_type. */
29382 gcc_assert (die->die_child != NULL);
29383 c = prev->die_sib;
29386 while (c != die->die_child);
29387 gcc_assert (die->die_child);
29388 c = die->die_child;
29389 for (i = 0; base_types.iterate (i, &base_type); i++)
29391 base_type->die_mark = 0;
29392 base_type->die_sib = c->die_sib;
29393 c->die_sib = base_type;
29394 c = base_type;
29398 /* Helper function for resolve_addr, attempt to resolve
29399 one CONST_STRING, return true if successful. Similarly verify that
29400 SYMBOL_REFs refer to variables emitted in the current CU. */
29402 static bool
29403 resolve_one_addr (rtx *addr)
29405 rtx rtl = *addr;
29407 if (GET_CODE (rtl) == CONST_STRING)
29409 size_t len = strlen (XSTR (rtl, 0)) + 1;
29410 tree t = build_string (len, XSTR (rtl, 0));
29411 tree tlen = size_int (len - 1);
29412 TREE_TYPE (t)
29413 = build_array_type (char_type_node, build_index_type (tlen));
29414 rtl = lookup_constant_def (t);
29415 if (!rtl || !MEM_P (rtl))
29416 return false;
29417 rtl = XEXP (rtl, 0);
29418 if (GET_CODE (rtl) == SYMBOL_REF
29419 && SYMBOL_REF_DECL (rtl)
29420 && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
29421 return false;
29422 vec_safe_push (used_rtx_array, rtl);
29423 *addr = rtl;
29424 return true;
29427 if (GET_CODE (rtl) == SYMBOL_REF
29428 && SYMBOL_REF_DECL (rtl))
29430 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
29432 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
29433 return false;
29435 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
29436 return false;
29439 if (GET_CODE (rtl) == CONST)
29441 subrtx_ptr_iterator::array_type array;
29442 FOR_EACH_SUBRTX_PTR (iter, array, &XEXP (rtl, 0), ALL)
29443 if (!resolve_one_addr (*iter))
29444 return false;
29447 return true;
29450 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
29451 if possible, and create DW_TAG_dwarf_procedure that can be referenced
29452 from DW_OP_implicit_pointer if the string hasn't been seen yet. */
29454 static rtx
29455 string_cst_pool_decl (tree t)
29457 rtx rtl = output_constant_def (t, 1);
29458 unsigned char *array;
29459 dw_loc_descr_ref l;
29460 tree decl;
29461 size_t len;
29462 dw_die_ref ref;
29464 if (!rtl || !MEM_P (rtl))
29465 return NULL_RTX;
29466 rtl = XEXP (rtl, 0);
29467 if (GET_CODE (rtl) != SYMBOL_REF
29468 || SYMBOL_REF_DECL (rtl) == NULL_TREE)
29469 return NULL_RTX;
29471 decl = SYMBOL_REF_DECL (rtl);
29472 if (!lookup_decl_die (decl))
29474 len = TREE_STRING_LENGTH (t);
29475 vec_safe_push (used_rtx_array, rtl);
29476 ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
29477 array = ggc_vec_alloc<unsigned char> (len);
29478 memcpy (array, TREE_STRING_POINTER (t), len);
29479 l = new_loc_descr (DW_OP_implicit_value, len, 0);
29480 l->dw_loc_oprnd2.val_class = dw_val_class_vec;
29481 l->dw_loc_oprnd2.v.val_vec.length = len;
29482 l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
29483 l->dw_loc_oprnd2.v.val_vec.array = array;
29484 add_AT_loc (ref, DW_AT_location, l);
29485 equate_decl_number_to_die (decl, ref);
29487 return rtl;
29490 /* Helper function of resolve_addr_in_expr. LOC is
29491 a DW_OP_addr followed by DW_OP_stack_value, either at the start
29492 of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
29493 resolved. Replace it (both DW_OP_addr and DW_OP_stack_value)
29494 with DW_OP_implicit_pointer if possible
29495 and return true, if unsuccessful, return false. */
29497 static bool
29498 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
29500 rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
29501 HOST_WIDE_INT offset = 0;
29502 dw_die_ref ref = NULL;
29503 tree decl;
29505 if (GET_CODE (rtl) == CONST
29506 && GET_CODE (XEXP (rtl, 0)) == PLUS
29507 && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
29509 offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
29510 rtl = XEXP (XEXP (rtl, 0), 0);
29512 if (GET_CODE (rtl) == CONST_STRING)
29514 size_t len = strlen (XSTR (rtl, 0)) + 1;
29515 tree t = build_string (len, XSTR (rtl, 0));
29516 tree tlen = size_int (len - 1);
29518 TREE_TYPE (t)
29519 = build_array_type (char_type_node, build_index_type (tlen));
29520 rtl = string_cst_pool_decl (t);
29521 if (!rtl)
29522 return false;
29524 if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
29526 decl = SYMBOL_REF_DECL (rtl);
29527 if (VAR_P (decl) && !DECL_EXTERNAL (decl))
29529 ref = lookup_decl_die (decl);
29530 if (ref && (get_AT (ref, DW_AT_location)
29531 || get_AT (ref, DW_AT_const_value)))
29533 loc->dw_loc_opc = dwarf_OP (DW_OP_implicit_pointer);
29534 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
29535 loc->dw_loc_oprnd1.val_entry = NULL;
29536 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
29537 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
29538 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
29539 loc->dw_loc_oprnd2.v.val_int = offset;
29540 return true;
29544 return false;
29547 /* Helper function for resolve_addr, handle one location
29548 expression, return false if at least one CONST_STRING or SYMBOL_REF in
29549 the location list couldn't be resolved. */
29551 static bool
29552 resolve_addr_in_expr (dw_attr_node *a, dw_loc_descr_ref loc)
29554 dw_loc_descr_ref keep = NULL;
29555 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
29556 switch (loc->dw_loc_opc)
29558 case DW_OP_addr:
29559 if (!resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
29561 if ((prev == NULL
29562 || prev->dw_loc_opc == DW_OP_piece
29563 || prev->dw_loc_opc == DW_OP_bit_piece)
29564 && loc->dw_loc_next
29565 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
29566 && (!dwarf_strict || dwarf_version >= 5)
29567 && optimize_one_addr_into_implicit_ptr (loc))
29568 break;
29569 return false;
29571 break;
29572 case DW_OP_GNU_addr_index:
29573 case DW_OP_GNU_const_index:
29574 if (loc->dw_loc_opc == DW_OP_GNU_addr_index
29575 || (loc->dw_loc_opc == DW_OP_GNU_const_index && loc->dtprel))
29577 rtx rtl = loc->dw_loc_oprnd1.val_entry->addr.rtl;
29578 if (!resolve_one_addr (&rtl))
29579 return false;
29580 remove_addr_table_entry (loc->dw_loc_oprnd1.val_entry);
29581 loc->dw_loc_oprnd1.val_entry
29582 = add_addr_table_entry (rtl, ate_kind_rtx);
29584 break;
29585 case DW_OP_const4u:
29586 case DW_OP_const8u:
29587 if (loc->dtprel
29588 && !resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
29589 return false;
29590 break;
29591 case DW_OP_plus_uconst:
29592 if (size_of_loc_descr (loc)
29593 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
29595 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
29597 dw_loc_descr_ref repl
29598 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
29599 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
29600 add_loc_descr (&repl, loc->dw_loc_next);
29601 *loc = *repl;
29603 break;
29604 case DW_OP_implicit_value:
29605 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
29606 && !resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr))
29607 return false;
29608 break;
29609 case DW_OP_implicit_pointer:
29610 case DW_OP_GNU_implicit_pointer:
29611 case DW_OP_GNU_parameter_ref:
29612 case DW_OP_GNU_variable_value:
29613 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
29615 dw_die_ref ref
29616 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
29617 if (ref == NULL)
29618 return false;
29619 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
29620 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
29621 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
29623 if (loc->dw_loc_opc == DW_OP_GNU_variable_value)
29625 if (prev == NULL
29626 && loc->dw_loc_next == NULL
29627 && AT_class (a) == dw_val_class_loc)
29628 switch (a->dw_attr)
29630 /* Following attributes allow both exprloc and reference,
29631 so if the whole expression is DW_OP_GNU_variable_value
29632 alone we could transform it into reference. */
29633 case DW_AT_byte_size:
29634 case DW_AT_bit_size:
29635 case DW_AT_lower_bound:
29636 case DW_AT_upper_bound:
29637 case DW_AT_bit_stride:
29638 case DW_AT_count:
29639 case DW_AT_allocated:
29640 case DW_AT_associated:
29641 case DW_AT_byte_stride:
29642 a->dw_attr_val.val_class = dw_val_class_die_ref;
29643 a->dw_attr_val.val_entry = NULL;
29644 a->dw_attr_val.v.val_die_ref.die
29645 = loc->dw_loc_oprnd1.v.val_die_ref.die;
29646 a->dw_attr_val.v.val_die_ref.external = 0;
29647 return true;
29648 default:
29649 break;
29651 if (dwarf_strict)
29652 return false;
29654 break;
29655 case DW_OP_const_type:
29656 case DW_OP_regval_type:
29657 case DW_OP_deref_type:
29658 case DW_OP_convert:
29659 case DW_OP_reinterpret:
29660 case DW_OP_GNU_const_type:
29661 case DW_OP_GNU_regval_type:
29662 case DW_OP_GNU_deref_type:
29663 case DW_OP_GNU_convert:
29664 case DW_OP_GNU_reinterpret:
29665 while (loc->dw_loc_next
29666 && (loc->dw_loc_next->dw_loc_opc == DW_OP_convert
29667 || loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert))
29669 dw_die_ref base1, base2;
29670 unsigned enc1, enc2, size1, size2;
29671 if (loc->dw_loc_opc == DW_OP_regval_type
29672 || loc->dw_loc_opc == DW_OP_deref_type
29673 || loc->dw_loc_opc == DW_OP_GNU_regval_type
29674 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
29675 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
29676 else if (loc->dw_loc_oprnd1.val_class
29677 == dw_val_class_unsigned_const)
29678 break;
29679 else
29680 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
29681 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
29682 == dw_val_class_unsigned_const)
29683 break;
29684 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
29685 gcc_assert (base1->die_tag == DW_TAG_base_type
29686 && base2->die_tag == DW_TAG_base_type);
29687 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
29688 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
29689 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
29690 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
29691 if (size1 == size2
29692 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
29693 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
29694 && loc != keep)
29695 || enc1 == enc2))
29697 /* Optimize away next DW_OP_convert after
29698 adjusting LOC's base type die reference. */
29699 if (loc->dw_loc_opc == DW_OP_regval_type
29700 || loc->dw_loc_opc == DW_OP_deref_type
29701 || loc->dw_loc_opc == DW_OP_GNU_regval_type
29702 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
29703 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
29704 else
29705 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
29706 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
29707 continue;
29709 /* Don't change integer DW_OP_convert after e.g. floating
29710 point typed stack entry. */
29711 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
29712 keep = loc->dw_loc_next;
29713 break;
29715 break;
29716 default:
29717 break;
29719 return true;
29722 /* Helper function of resolve_addr. DIE had DW_AT_location of
29723 DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
29724 and DW_OP_addr couldn't be resolved. resolve_addr has already
29725 removed the DW_AT_location attribute. This function attempts to
29726 add a new DW_AT_location attribute with DW_OP_implicit_pointer
29727 to it or DW_AT_const_value attribute, if possible. */
29729 static void
29730 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
29732 if (!VAR_P (decl)
29733 || lookup_decl_die (decl) != die
29734 || DECL_EXTERNAL (decl)
29735 || !TREE_STATIC (decl)
29736 || DECL_INITIAL (decl) == NULL_TREE
29737 || DECL_P (DECL_INITIAL (decl))
29738 || get_AT (die, DW_AT_const_value))
29739 return;
29741 tree init = DECL_INITIAL (decl);
29742 HOST_WIDE_INT offset = 0;
29743 /* For variables that have been optimized away and thus
29744 don't have a memory location, see if we can emit
29745 DW_AT_const_value instead. */
29746 if (tree_add_const_value_attribute (die, init))
29747 return;
29748 if (dwarf_strict && dwarf_version < 5)
29749 return;
29750 /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
29751 and ADDR_EXPR refers to a decl that has DW_AT_location or
29752 DW_AT_const_value (but isn't addressable, otherwise
29753 resolving the original DW_OP_addr wouldn't fail), see if
29754 we can add DW_OP_implicit_pointer. */
29755 STRIP_NOPS (init);
29756 if (TREE_CODE (init) == POINTER_PLUS_EXPR
29757 && tree_fits_shwi_p (TREE_OPERAND (init, 1)))
29759 offset = tree_to_shwi (TREE_OPERAND (init, 1));
29760 init = TREE_OPERAND (init, 0);
29761 STRIP_NOPS (init);
29763 if (TREE_CODE (init) != ADDR_EXPR)
29764 return;
29765 if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
29766 && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
29767 || (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL
29768 && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
29769 && TREE_OPERAND (init, 0) != decl))
29771 dw_die_ref ref;
29772 dw_loc_descr_ref l;
29774 if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
29776 rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
29777 if (!rtl)
29778 return;
29779 decl = SYMBOL_REF_DECL (rtl);
29781 else
29782 decl = TREE_OPERAND (init, 0);
29783 ref = lookup_decl_die (decl);
29784 if (ref == NULL
29785 || (!get_AT (ref, DW_AT_location)
29786 && !get_AT (ref, DW_AT_const_value)))
29787 return;
29788 l = new_loc_descr (dwarf_OP (DW_OP_implicit_pointer), 0, offset);
29789 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
29790 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
29791 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
29792 add_AT_loc (die, DW_AT_location, l);
29796 /* Return NULL if l is a DWARF expression, or first op that is not
29797 valid DWARF expression. */
29799 static dw_loc_descr_ref
29800 non_dwarf_expression (dw_loc_descr_ref l)
29802 while (l)
29804 if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
29805 return l;
29806 switch (l->dw_loc_opc)
29808 case DW_OP_regx:
29809 case DW_OP_implicit_value:
29810 case DW_OP_stack_value:
29811 case DW_OP_implicit_pointer:
29812 case DW_OP_GNU_implicit_pointer:
29813 case DW_OP_GNU_parameter_ref:
29814 case DW_OP_piece:
29815 case DW_OP_bit_piece:
29816 return l;
29817 default:
29818 break;
29820 l = l->dw_loc_next;
29822 return NULL;
29825 /* Return adjusted copy of EXPR:
29826 If it is empty DWARF expression, return it.
29827 If it is valid non-empty DWARF expression,
29828 return copy of EXPR with DW_OP_deref appended to it.
29829 If it is DWARF expression followed by DW_OP_reg{N,x}, return
29830 copy of the DWARF expression with DW_OP_breg{N,x} <0> appended.
29831 If it is DWARF expression followed by DW_OP_stack_value, return
29832 copy of the DWARF expression without anything appended.
29833 Otherwise, return NULL. */
29835 static dw_loc_descr_ref
29836 copy_deref_exprloc (dw_loc_descr_ref expr)
29838 dw_loc_descr_ref tail = NULL;
29840 if (expr == NULL)
29841 return NULL;
29843 dw_loc_descr_ref l = non_dwarf_expression (expr);
29844 if (l && l->dw_loc_next)
29845 return NULL;
29847 if (l)
29849 if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
29850 tail = new_loc_descr ((enum dwarf_location_atom)
29851 (DW_OP_breg0 + (l->dw_loc_opc - DW_OP_reg0)),
29852 0, 0);
29853 else
29854 switch (l->dw_loc_opc)
29856 case DW_OP_regx:
29857 tail = new_loc_descr (DW_OP_bregx,
29858 l->dw_loc_oprnd1.v.val_unsigned, 0);
29859 break;
29860 case DW_OP_stack_value:
29861 break;
29862 default:
29863 return NULL;
29866 else
29867 tail = new_loc_descr (DW_OP_deref, 0, 0);
29869 dw_loc_descr_ref ret = NULL, *p = &ret;
29870 while (expr != l)
29872 *p = new_loc_descr (expr->dw_loc_opc, 0, 0);
29873 (*p)->dw_loc_oprnd1 = expr->dw_loc_oprnd1;
29874 (*p)->dw_loc_oprnd2 = expr->dw_loc_oprnd2;
29875 p = &(*p)->dw_loc_next;
29876 expr = expr->dw_loc_next;
29878 *p = tail;
29879 return ret;
29882 /* For DW_AT_string_length attribute with DW_OP_GNU_variable_value
29883 reference to a variable or argument, adjust it if needed and return:
29884 -1 if the DW_AT_string_length attribute and DW_AT_{string_length_,}byte_size
29885 attribute if present should be removed
29886 0 keep the attribute perhaps with minor modifications, no need to rescan
29887 1 if the attribute has been successfully adjusted. */
29889 static int
29890 optimize_string_length (dw_attr_node *a)
29892 dw_loc_descr_ref l = AT_loc (a), lv;
29893 dw_die_ref die;
29894 if (l->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
29896 tree decl = l->dw_loc_oprnd1.v.val_decl_ref;
29897 die = lookup_decl_die (decl);
29898 if (die)
29900 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
29901 l->dw_loc_oprnd1.v.val_die_ref.die = die;
29902 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
29904 else
29905 return -1;
29907 else
29908 die = l->dw_loc_oprnd1.v.val_die_ref.die;
29910 /* DWARF5 allows reference class, so we can then reference the DIE.
29911 Only do this for DW_OP_GNU_variable_value DW_OP_stack_value. */
29912 if (l->dw_loc_next != NULL && dwarf_version >= 5)
29914 a->dw_attr_val.val_class = dw_val_class_die_ref;
29915 a->dw_attr_val.val_entry = NULL;
29916 a->dw_attr_val.v.val_die_ref.die = die;
29917 a->dw_attr_val.v.val_die_ref.external = 0;
29918 return 0;
29921 dw_attr_node *av = get_AT (die, DW_AT_location);
29922 dw_loc_list_ref d;
29923 bool non_dwarf_expr = false;
29925 if (av == NULL)
29926 return dwarf_strict ? -1 : 0;
29927 switch (AT_class (av))
29929 case dw_val_class_loc_list:
29930 for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
29931 if (d->expr && non_dwarf_expression (d->expr))
29932 non_dwarf_expr = true;
29933 break;
29934 case dw_val_class_view_list:
29935 gcc_unreachable ();
29936 case dw_val_class_loc:
29937 lv = AT_loc (av);
29938 if (lv == NULL)
29939 return dwarf_strict ? -1 : 0;
29940 if (non_dwarf_expression (lv))
29941 non_dwarf_expr = true;
29942 break;
29943 default:
29944 return dwarf_strict ? -1 : 0;
29947 /* If it is safe to transform DW_OP_GNU_variable_value DW_OP_stack_value
29948 into DW_OP_call4 or DW_OP_GNU_variable_value into
29949 DW_OP_call4 DW_OP_deref, do so. */
29950 if (!non_dwarf_expr
29951 && (l->dw_loc_next != NULL || AT_class (av) == dw_val_class_loc))
29953 l->dw_loc_opc = DW_OP_call4;
29954 if (l->dw_loc_next)
29955 l->dw_loc_next = NULL;
29956 else
29957 l->dw_loc_next = new_loc_descr (DW_OP_deref, 0, 0);
29958 return 0;
29961 /* For DW_OP_GNU_variable_value DW_OP_stack_value, we can just
29962 copy over the DW_AT_location attribute from die to a. */
29963 if (l->dw_loc_next != NULL)
29965 a->dw_attr_val = av->dw_attr_val;
29966 return 1;
29969 dw_loc_list_ref list, *p;
29970 switch (AT_class (av))
29972 case dw_val_class_loc_list:
29973 p = &list;
29974 list = NULL;
29975 for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
29977 lv = copy_deref_exprloc (d->expr);
29978 if (lv)
29980 *p = new_loc_list (lv, d->begin, d->vbegin, d->end, d->vend, d->section);
29981 p = &(*p)->dw_loc_next;
29983 else if (!dwarf_strict && d->expr)
29984 return 0;
29986 if (list == NULL)
29987 return dwarf_strict ? -1 : 0;
29988 a->dw_attr_val.val_class = dw_val_class_loc_list;
29989 gen_llsym (list);
29990 *AT_loc_list_ptr (a) = list;
29991 return 1;
29992 case dw_val_class_loc:
29993 lv = copy_deref_exprloc (AT_loc (av));
29994 if (lv == NULL)
29995 return dwarf_strict ? -1 : 0;
29996 a->dw_attr_val.v.val_loc = lv;
29997 return 1;
29998 default:
29999 gcc_unreachable ();
30003 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
30004 an address in .rodata section if the string literal is emitted there,
30005 or remove the containing location list or replace DW_AT_const_value
30006 with DW_AT_location and empty location expression, if it isn't found
30007 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
30008 to something that has been emitted in the current CU. */
30010 static void
30011 resolve_addr (dw_die_ref die)
30013 dw_die_ref c;
30014 dw_attr_node *a;
30015 dw_loc_list_ref *curr, *start, loc;
30016 unsigned ix;
30017 bool remove_AT_byte_size = false;
30019 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
30020 switch (AT_class (a))
30022 case dw_val_class_loc_list:
30023 start = curr = AT_loc_list_ptr (a);
30024 loc = *curr;
30025 gcc_assert (loc);
30026 /* The same list can be referenced more than once. See if we have
30027 already recorded the result from a previous pass. */
30028 if (loc->replaced)
30029 *curr = loc->dw_loc_next;
30030 else if (!loc->resolved_addr)
30032 /* As things stand, we do not expect or allow one die to
30033 reference a suffix of another die's location list chain.
30034 References must be identical or completely separate.
30035 There is therefore no need to cache the result of this
30036 pass on any list other than the first; doing so
30037 would lead to unnecessary writes. */
30038 while (*curr)
30040 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
30041 if (!resolve_addr_in_expr (a, (*curr)->expr))
30043 dw_loc_list_ref next = (*curr)->dw_loc_next;
30044 dw_loc_descr_ref l = (*curr)->expr;
30046 if (next && (*curr)->ll_symbol)
30048 gcc_assert (!next->ll_symbol);
30049 next->ll_symbol = (*curr)->ll_symbol;
30050 next->vl_symbol = (*curr)->vl_symbol;
30052 if (dwarf_split_debug_info)
30053 remove_loc_list_addr_table_entries (l);
30054 *curr = next;
30056 else
30058 mark_base_types ((*curr)->expr);
30059 curr = &(*curr)->dw_loc_next;
30062 if (loc == *start)
30063 loc->resolved_addr = 1;
30064 else
30066 loc->replaced = 1;
30067 loc->dw_loc_next = *start;
30070 if (!*start)
30072 remove_AT (die, a->dw_attr);
30073 ix--;
30075 break;
30076 case dw_val_class_view_list:
30078 gcc_checking_assert (a->dw_attr == DW_AT_GNU_locviews);
30079 gcc_checking_assert (dwarf2out_locviews_in_attribute ());
30080 dw_val_node *llnode
30081 = view_list_to_loc_list_val_node (&a->dw_attr_val);
30082 /* If we no longer have a loclist, or it no longer needs
30083 views, drop this attribute. */
30084 if (!llnode || !llnode->v.val_loc_list->vl_symbol)
30086 remove_AT (die, a->dw_attr);
30087 ix--;
30089 break;
30091 case dw_val_class_loc:
30093 dw_loc_descr_ref l = AT_loc (a);
30094 /* DW_OP_GNU_variable_value DW_OP_stack_value or
30095 DW_OP_GNU_variable_value in DW_AT_string_length can be converted
30096 into DW_OP_call4 or DW_OP_call4 DW_OP_deref, which is standard
30097 DWARF4 unlike DW_OP_GNU_variable_value. Or for DWARF5
30098 DW_OP_GNU_variable_value DW_OP_stack_value can be replaced
30099 with DW_FORM_ref referencing the same DIE as
30100 DW_OP_GNU_variable_value used to reference. */
30101 if (a->dw_attr == DW_AT_string_length
30102 && l
30103 && l->dw_loc_opc == DW_OP_GNU_variable_value
30104 && (l->dw_loc_next == NULL
30105 || (l->dw_loc_next->dw_loc_next == NULL
30106 && l->dw_loc_next->dw_loc_opc == DW_OP_stack_value)))
30108 switch (optimize_string_length (a))
30110 case -1:
30111 remove_AT (die, a->dw_attr);
30112 ix--;
30113 /* If we drop DW_AT_string_length, we need to drop also
30114 DW_AT_{string_length_,}byte_size. */
30115 remove_AT_byte_size = true;
30116 continue;
30117 default:
30118 break;
30119 case 1:
30120 /* Even if we keep the optimized DW_AT_string_length,
30121 it might have changed AT_class, so process it again. */
30122 ix--;
30123 continue;
30126 /* For -gdwarf-2 don't attempt to optimize
30127 DW_AT_data_member_location containing
30128 DW_OP_plus_uconst - older consumers might
30129 rely on it being that op instead of a more complex,
30130 but shorter, location description. */
30131 if ((dwarf_version > 2
30132 || a->dw_attr != DW_AT_data_member_location
30133 || l == NULL
30134 || l->dw_loc_opc != DW_OP_plus_uconst
30135 || l->dw_loc_next != NULL)
30136 && !resolve_addr_in_expr (a, l))
30138 if (dwarf_split_debug_info)
30139 remove_loc_list_addr_table_entries (l);
30140 if (l != NULL
30141 && l->dw_loc_next == NULL
30142 && l->dw_loc_opc == DW_OP_addr
30143 && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
30144 && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
30145 && a->dw_attr == DW_AT_location)
30147 tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
30148 remove_AT (die, a->dw_attr);
30149 ix--;
30150 optimize_location_into_implicit_ptr (die, decl);
30151 break;
30153 if (a->dw_attr == DW_AT_string_length)
30154 /* If we drop DW_AT_string_length, we need to drop also
30155 DW_AT_{string_length_,}byte_size. */
30156 remove_AT_byte_size = true;
30157 remove_AT (die, a->dw_attr);
30158 ix--;
30160 else
30161 mark_base_types (l);
30163 break;
30164 case dw_val_class_addr:
30165 if (a->dw_attr == DW_AT_const_value
30166 && !resolve_one_addr (&a->dw_attr_val.v.val_addr))
30168 if (AT_index (a) != NOT_INDEXED)
30169 remove_addr_table_entry (a->dw_attr_val.val_entry);
30170 remove_AT (die, a->dw_attr);
30171 ix--;
30173 if ((die->die_tag == DW_TAG_call_site
30174 && a->dw_attr == DW_AT_call_origin)
30175 || (die->die_tag == DW_TAG_GNU_call_site
30176 && a->dw_attr == DW_AT_abstract_origin))
30178 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
30179 dw_die_ref tdie = lookup_decl_die (tdecl);
30180 dw_die_ref cdie;
30181 if (tdie == NULL
30182 && DECL_EXTERNAL (tdecl)
30183 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE
30184 && (cdie = lookup_context_die (DECL_CONTEXT (tdecl))))
30186 dw_die_ref pdie = cdie;
30187 /* Make sure we don't add these DIEs into type units.
30188 We could emit skeleton DIEs for context (namespaces,
30189 outer structs/classes) and a skeleton DIE for the
30190 innermost context with DW_AT_signature pointing to the
30191 type unit. See PR78835. */
30192 while (pdie && pdie->die_tag != DW_TAG_type_unit)
30193 pdie = pdie->die_parent;
30194 if (pdie == NULL)
30196 /* Creating a full DIE for tdecl is overly expensive and
30197 at this point even wrong when in the LTO phase
30198 as it can end up generating new type DIEs we didn't
30199 output and thus optimize_external_refs will crash. */
30200 tdie = new_die (DW_TAG_subprogram, cdie, NULL_TREE);
30201 add_AT_flag (tdie, DW_AT_external, 1);
30202 add_AT_flag (tdie, DW_AT_declaration, 1);
30203 add_linkage_attr (tdie, tdecl);
30204 add_name_and_src_coords_attributes (tdie, tdecl, true);
30205 equate_decl_number_to_die (tdecl, tdie);
30208 if (tdie)
30210 a->dw_attr_val.val_class = dw_val_class_die_ref;
30211 a->dw_attr_val.v.val_die_ref.die = tdie;
30212 a->dw_attr_val.v.val_die_ref.external = 0;
30214 else
30216 if (AT_index (a) != NOT_INDEXED)
30217 remove_addr_table_entry (a->dw_attr_val.val_entry);
30218 remove_AT (die, a->dw_attr);
30219 ix--;
30222 break;
30223 default:
30224 break;
30227 if (remove_AT_byte_size)
30228 remove_AT (die, dwarf_version >= 5
30229 ? DW_AT_string_length_byte_size
30230 : DW_AT_byte_size);
30232 FOR_EACH_CHILD (die, c, resolve_addr (c));
30235 /* Helper routines for optimize_location_lists.
30236 This pass tries to share identical local lists in .debug_loc
30237 section. */
30239 /* Iteratively hash operands of LOC opcode into HSTATE. */
30241 static void
30242 hash_loc_operands (dw_loc_descr_ref loc, inchash::hash &hstate)
30244 dw_val_ref val1 = &loc->dw_loc_oprnd1;
30245 dw_val_ref val2 = &loc->dw_loc_oprnd2;
30247 switch (loc->dw_loc_opc)
30249 case DW_OP_const4u:
30250 case DW_OP_const8u:
30251 if (loc->dtprel)
30252 goto hash_addr;
30253 /* FALLTHRU */
30254 case DW_OP_const1u:
30255 case DW_OP_const1s:
30256 case DW_OP_const2u:
30257 case DW_OP_const2s:
30258 case DW_OP_const4s:
30259 case DW_OP_const8s:
30260 case DW_OP_constu:
30261 case DW_OP_consts:
30262 case DW_OP_pick:
30263 case DW_OP_plus_uconst:
30264 case DW_OP_breg0:
30265 case DW_OP_breg1:
30266 case DW_OP_breg2:
30267 case DW_OP_breg3:
30268 case DW_OP_breg4:
30269 case DW_OP_breg5:
30270 case DW_OP_breg6:
30271 case DW_OP_breg7:
30272 case DW_OP_breg8:
30273 case DW_OP_breg9:
30274 case DW_OP_breg10:
30275 case DW_OP_breg11:
30276 case DW_OP_breg12:
30277 case DW_OP_breg13:
30278 case DW_OP_breg14:
30279 case DW_OP_breg15:
30280 case DW_OP_breg16:
30281 case DW_OP_breg17:
30282 case DW_OP_breg18:
30283 case DW_OP_breg19:
30284 case DW_OP_breg20:
30285 case DW_OP_breg21:
30286 case DW_OP_breg22:
30287 case DW_OP_breg23:
30288 case DW_OP_breg24:
30289 case DW_OP_breg25:
30290 case DW_OP_breg26:
30291 case DW_OP_breg27:
30292 case DW_OP_breg28:
30293 case DW_OP_breg29:
30294 case DW_OP_breg30:
30295 case DW_OP_breg31:
30296 case DW_OP_regx:
30297 case DW_OP_fbreg:
30298 case DW_OP_piece:
30299 case DW_OP_deref_size:
30300 case DW_OP_xderef_size:
30301 hstate.add_object (val1->v.val_int);
30302 break;
30303 case DW_OP_skip:
30304 case DW_OP_bra:
30306 int offset;
30308 gcc_assert (val1->val_class == dw_val_class_loc);
30309 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
30310 hstate.add_object (offset);
30312 break;
30313 case DW_OP_implicit_value:
30314 hstate.add_object (val1->v.val_unsigned);
30315 switch (val2->val_class)
30317 case dw_val_class_const:
30318 hstate.add_object (val2->v.val_int);
30319 break;
30320 case dw_val_class_vec:
30322 unsigned int elt_size = val2->v.val_vec.elt_size;
30323 unsigned int len = val2->v.val_vec.length;
30325 hstate.add_int (elt_size);
30326 hstate.add_int (len);
30327 hstate.add (val2->v.val_vec.array, len * elt_size);
30329 break;
30330 case dw_val_class_const_double:
30331 hstate.add_object (val2->v.val_double.low);
30332 hstate.add_object (val2->v.val_double.high);
30333 break;
30334 case dw_val_class_wide_int:
30335 hstate.add (val2->v.val_wide->get_val (),
30336 get_full_len (*val2->v.val_wide)
30337 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
30338 break;
30339 case dw_val_class_addr:
30340 inchash::add_rtx (val2->v.val_addr, hstate);
30341 break;
30342 default:
30343 gcc_unreachable ();
30345 break;
30346 case DW_OP_bregx:
30347 case DW_OP_bit_piece:
30348 hstate.add_object (val1->v.val_int);
30349 hstate.add_object (val2->v.val_int);
30350 break;
30351 case DW_OP_addr:
30352 hash_addr:
30353 if (loc->dtprel)
30355 unsigned char dtprel = 0xd1;
30356 hstate.add_object (dtprel);
30358 inchash::add_rtx (val1->v.val_addr, hstate);
30359 break;
30360 case DW_OP_GNU_addr_index:
30361 case DW_OP_GNU_const_index:
30363 if (loc->dtprel)
30365 unsigned char dtprel = 0xd1;
30366 hstate.add_object (dtprel);
30368 inchash::add_rtx (val1->val_entry->addr.rtl, hstate);
30370 break;
30371 case DW_OP_implicit_pointer:
30372 case DW_OP_GNU_implicit_pointer:
30373 hstate.add_int (val2->v.val_int);
30374 break;
30375 case DW_OP_entry_value:
30376 case DW_OP_GNU_entry_value:
30377 hstate.add_object (val1->v.val_loc);
30378 break;
30379 case DW_OP_regval_type:
30380 case DW_OP_deref_type:
30381 case DW_OP_GNU_regval_type:
30382 case DW_OP_GNU_deref_type:
30384 unsigned int byte_size
30385 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
30386 unsigned int encoding
30387 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
30388 hstate.add_object (val1->v.val_int);
30389 hstate.add_object (byte_size);
30390 hstate.add_object (encoding);
30392 break;
30393 case DW_OP_convert:
30394 case DW_OP_reinterpret:
30395 case DW_OP_GNU_convert:
30396 case DW_OP_GNU_reinterpret:
30397 if (val1->val_class == dw_val_class_unsigned_const)
30399 hstate.add_object (val1->v.val_unsigned);
30400 break;
30402 /* FALLTHRU */
30403 case DW_OP_const_type:
30404 case DW_OP_GNU_const_type:
30406 unsigned int byte_size
30407 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
30408 unsigned int encoding
30409 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
30410 hstate.add_object (byte_size);
30411 hstate.add_object (encoding);
30412 if (loc->dw_loc_opc != DW_OP_const_type
30413 && loc->dw_loc_opc != DW_OP_GNU_const_type)
30414 break;
30415 hstate.add_object (val2->val_class);
30416 switch (val2->val_class)
30418 case dw_val_class_const:
30419 hstate.add_object (val2->v.val_int);
30420 break;
30421 case dw_val_class_vec:
30423 unsigned int elt_size = val2->v.val_vec.elt_size;
30424 unsigned int len = val2->v.val_vec.length;
30426 hstate.add_object (elt_size);
30427 hstate.add_object (len);
30428 hstate.add (val2->v.val_vec.array, len * elt_size);
30430 break;
30431 case dw_val_class_const_double:
30432 hstate.add_object (val2->v.val_double.low);
30433 hstate.add_object (val2->v.val_double.high);
30434 break;
30435 case dw_val_class_wide_int:
30436 hstate.add (val2->v.val_wide->get_val (),
30437 get_full_len (*val2->v.val_wide)
30438 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
30439 break;
30440 default:
30441 gcc_unreachable ();
30444 break;
30446 default:
30447 /* Other codes have no operands. */
30448 break;
30452 /* Iteratively hash the whole DWARF location expression LOC into HSTATE. */
30454 static inline void
30455 hash_locs (dw_loc_descr_ref loc, inchash::hash &hstate)
30457 dw_loc_descr_ref l;
30458 bool sizes_computed = false;
30459 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
30460 size_of_locs (loc);
30462 for (l = loc; l != NULL; l = l->dw_loc_next)
30464 enum dwarf_location_atom opc = l->dw_loc_opc;
30465 hstate.add_object (opc);
30466 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
30468 size_of_locs (loc);
30469 sizes_computed = true;
30471 hash_loc_operands (l, hstate);
30475 /* Compute hash of the whole location list LIST_HEAD. */
30477 static inline void
30478 hash_loc_list (dw_loc_list_ref list_head)
30480 dw_loc_list_ref curr = list_head;
30481 inchash::hash hstate;
30483 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
30485 hstate.add (curr->begin, strlen (curr->begin) + 1);
30486 hstate.add (curr->end, strlen (curr->end) + 1);
30487 hstate.add_object (curr->vbegin);
30488 hstate.add_object (curr->vend);
30489 if (curr->section)
30490 hstate.add (curr->section, strlen (curr->section) + 1);
30491 hash_locs (curr->expr, hstate);
30493 list_head->hash = hstate.end ();
30496 /* Return true if X and Y opcodes have the same operands. */
30498 static inline bool
30499 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
30501 dw_val_ref valx1 = &x->dw_loc_oprnd1;
30502 dw_val_ref valx2 = &x->dw_loc_oprnd2;
30503 dw_val_ref valy1 = &y->dw_loc_oprnd1;
30504 dw_val_ref valy2 = &y->dw_loc_oprnd2;
30506 switch (x->dw_loc_opc)
30508 case DW_OP_const4u:
30509 case DW_OP_const8u:
30510 if (x->dtprel)
30511 goto hash_addr;
30512 /* FALLTHRU */
30513 case DW_OP_const1u:
30514 case DW_OP_const1s:
30515 case DW_OP_const2u:
30516 case DW_OP_const2s:
30517 case DW_OP_const4s:
30518 case DW_OP_const8s:
30519 case DW_OP_constu:
30520 case DW_OP_consts:
30521 case DW_OP_pick:
30522 case DW_OP_plus_uconst:
30523 case DW_OP_breg0:
30524 case DW_OP_breg1:
30525 case DW_OP_breg2:
30526 case DW_OP_breg3:
30527 case DW_OP_breg4:
30528 case DW_OP_breg5:
30529 case DW_OP_breg6:
30530 case DW_OP_breg7:
30531 case DW_OP_breg8:
30532 case DW_OP_breg9:
30533 case DW_OP_breg10:
30534 case DW_OP_breg11:
30535 case DW_OP_breg12:
30536 case DW_OP_breg13:
30537 case DW_OP_breg14:
30538 case DW_OP_breg15:
30539 case DW_OP_breg16:
30540 case DW_OP_breg17:
30541 case DW_OP_breg18:
30542 case DW_OP_breg19:
30543 case DW_OP_breg20:
30544 case DW_OP_breg21:
30545 case DW_OP_breg22:
30546 case DW_OP_breg23:
30547 case DW_OP_breg24:
30548 case DW_OP_breg25:
30549 case DW_OP_breg26:
30550 case DW_OP_breg27:
30551 case DW_OP_breg28:
30552 case DW_OP_breg29:
30553 case DW_OP_breg30:
30554 case DW_OP_breg31:
30555 case DW_OP_regx:
30556 case DW_OP_fbreg:
30557 case DW_OP_piece:
30558 case DW_OP_deref_size:
30559 case DW_OP_xderef_size:
30560 return valx1->v.val_int == valy1->v.val_int;
30561 case DW_OP_skip:
30562 case DW_OP_bra:
30563 /* If splitting debug info, the use of DW_OP_GNU_addr_index
30564 can cause irrelevant differences in dw_loc_addr. */
30565 gcc_assert (valx1->val_class == dw_val_class_loc
30566 && valy1->val_class == dw_val_class_loc
30567 && (dwarf_split_debug_info
30568 || x->dw_loc_addr == y->dw_loc_addr));
30569 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
30570 case DW_OP_implicit_value:
30571 if (valx1->v.val_unsigned != valy1->v.val_unsigned
30572 || valx2->val_class != valy2->val_class)
30573 return false;
30574 switch (valx2->val_class)
30576 case dw_val_class_const:
30577 return valx2->v.val_int == valy2->v.val_int;
30578 case dw_val_class_vec:
30579 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
30580 && valx2->v.val_vec.length == valy2->v.val_vec.length
30581 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
30582 valx2->v.val_vec.elt_size
30583 * valx2->v.val_vec.length) == 0;
30584 case dw_val_class_const_double:
30585 return valx2->v.val_double.low == valy2->v.val_double.low
30586 && valx2->v.val_double.high == valy2->v.val_double.high;
30587 case dw_val_class_wide_int:
30588 return *valx2->v.val_wide == *valy2->v.val_wide;
30589 case dw_val_class_addr:
30590 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
30591 default:
30592 gcc_unreachable ();
30594 case DW_OP_bregx:
30595 case DW_OP_bit_piece:
30596 return valx1->v.val_int == valy1->v.val_int
30597 && valx2->v.val_int == valy2->v.val_int;
30598 case DW_OP_addr:
30599 hash_addr:
30600 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
30601 case DW_OP_GNU_addr_index:
30602 case DW_OP_GNU_const_index:
30604 rtx ax1 = valx1->val_entry->addr.rtl;
30605 rtx ay1 = valy1->val_entry->addr.rtl;
30606 return rtx_equal_p (ax1, ay1);
30608 case DW_OP_implicit_pointer:
30609 case DW_OP_GNU_implicit_pointer:
30610 return valx1->val_class == dw_val_class_die_ref
30611 && valx1->val_class == valy1->val_class
30612 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
30613 && valx2->v.val_int == valy2->v.val_int;
30614 case DW_OP_entry_value:
30615 case DW_OP_GNU_entry_value:
30616 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
30617 case DW_OP_const_type:
30618 case DW_OP_GNU_const_type:
30619 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
30620 || valx2->val_class != valy2->val_class)
30621 return false;
30622 switch (valx2->val_class)
30624 case dw_val_class_const:
30625 return valx2->v.val_int == valy2->v.val_int;
30626 case dw_val_class_vec:
30627 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
30628 && valx2->v.val_vec.length == valy2->v.val_vec.length
30629 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
30630 valx2->v.val_vec.elt_size
30631 * valx2->v.val_vec.length) == 0;
30632 case dw_val_class_const_double:
30633 return valx2->v.val_double.low == valy2->v.val_double.low
30634 && valx2->v.val_double.high == valy2->v.val_double.high;
30635 case dw_val_class_wide_int:
30636 return *valx2->v.val_wide == *valy2->v.val_wide;
30637 default:
30638 gcc_unreachable ();
30640 case DW_OP_regval_type:
30641 case DW_OP_deref_type:
30642 case DW_OP_GNU_regval_type:
30643 case DW_OP_GNU_deref_type:
30644 return valx1->v.val_int == valy1->v.val_int
30645 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
30646 case DW_OP_convert:
30647 case DW_OP_reinterpret:
30648 case DW_OP_GNU_convert:
30649 case DW_OP_GNU_reinterpret:
30650 if (valx1->val_class != valy1->val_class)
30651 return false;
30652 if (valx1->val_class == dw_val_class_unsigned_const)
30653 return valx1->v.val_unsigned == valy1->v.val_unsigned;
30654 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
30655 case DW_OP_GNU_parameter_ref:
30656 return valx1->val_class == dw_val_class_die_ref
30657 && valx1->val_class == valy1->val_class
30658 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
30659 default:
30660 /* Other codes have no operands. */
30661 return true;
30665 /* Return true if DWARF location expressions X and Y are the same. */
30667 static inline bool
30668 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
30670 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
30671 if (x->dw_loc_opc != y->dw_loc_opc
30672 || x->dtprel != y->dtprel
30673 || !compare_loc_operands (x, y))
30674 break;
30675 return x == NULL && y == NULL;
30678 /* Hashtable helpers. */
30680 struct loc_list_hasher : nofree_ptr_hash <dw_loc_list_struct>
30682 static inline hashval_t hash (const dw_loc_list_struct *);
30683 static inline bool equal (const dw_loc_list_struct *,
30684 const dw_loc_list_struct *);
30687 /* Return precomputed hash of location list X. */
30689 inline hashval_t
30690 loc_list_hasher::hash (const dw_loc_list_struct *x)
30692 return x->hash;
30695 /* Return true if location lists A and B are the same. */
30697 inline bool
30698 loc_list_hasher::equal (const dw_loc_list_struct *a,
30699 const dw_loc_list_struct *b)
30701 if (a == b)
30702 return 1;
30703 if (a->hash != b->hash)
30704 return 0;
30705 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
30706 if (strcmp (a->begin, b->begin) != 0
30707 || strcmp (a->end, b->end) != 0
30708 || (a->section == NULL) != (b->section == NULL)
30709 || (a->section && strcmp (a->section, b->section) != 0)
30710 || a->vbegin != b->vbegin || a->vend != b->vend
30711 || !compare_locs (a->expr, b->expr))
30712 break;
30713 return a == NULL && b == NULL;
30716 typedef hash_table<loc_list_hasher> loc_list_hash_type;
30719 /* Recursively optimize location lists referenced from DIE
30720 children and share them whenever possible. */
30722 static void
30723 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type *htab)
30725 dw_die_ref c;
30726 dw_attr_node *a;
30727 unsigned ix;
30728 dw_loc_list_struct **slot;
30729 bool drop_locviews = false;
30730 bool has_locviews = false;
30732 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
30733 if (AT_class (a) == dw_val_class_loc_list)
30735 dw_loc_list_ref list = AT_loc_list (a);
30736 /* TODO: perform some optimizations here, before hashing
30737 it and storing into the hash table. */
30738 hash_loc_list (list);
30739 slot = htab->find_slot_with_hash (list, list->hash, INSERT);
30740 if (*slot == NULL)
30742 *slot = list;
30743 if (loc_list_has_views (list))
30744 gcc_assert (list->vl_symbol);
30745 else if (list->vl_symbol)
30747 drop_locviews = true;
30748 list->vl_symbol = NULL;
30751 else
30753 if (list->vl_symbol && !(*slot)->vl_symbol)
30754 drop_locviews = true;
30755 a->dw_attr_val.v.val_loc_list = *slot;
30758 else if (AT_class (a) == dw_val_class_view_list)
30760 gcc_checking_assert (a->dw_attr == DW_AT_GNU_locviews);
30761 has_locviews = true;
30765 if (drop_locviews && has_locviews)
30766 remove_AT (die, DW_AT_GNU_locviews);
30768 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
30772 /* Recursively assign each location list a unique index into the debug_addr
30773 section. */
30775 static void
30776 index_location_lists (dw_die_ref die)
30778 dw_die_ref c;
30779 dw_attr_node *a;
30780 unsigned ix;
30782 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
30783 if (AT_class (a) == dw_val_class_loc_list)
30785 dw_loc_list_ref list = AT_loc_list (a);
30786 dw_loc_list_ref curr;
30787 for (curr = list; curr != NULL; curr = curr->dw_loc_next)
30789 /* Don't index an entry that has already been indexed
30790 or won't be output. */
30791 if (curr->begin_entry != NULL
30792 || skip_loc_list_entry (curr))
30793 continue;
30795 curr->begin_entry
30796 = add_addr_table_entry (xstrdup (curr->begin), ate_kind_label);
30800 FOR_EACH_CHILD (die, c, index_location_lists (c));
30803 /* Optimize location lists referenced from DIE
30804 children and share them whenever possible. */
30806 static void
30807 optimize_location_lists (dw_die_ref die)
30809 loc_list_hash_type htab (500);
30810 optimize_location_lists_1 (die, &htab);
30813 /* Traverse the limbo die list, and add parent/child links. The only
30814 dies without parents that should be here are concrete instances of
30815 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
30816 For concrete instances, we can get the parent die from the abstract
30817 instance. */
30819 static void
30820 flush_limbo_die_list (void)
30822 limbo_die_node *node;
30824 /* get_context_die calls force_decl_die, which can put new DIEs on the
30825 limbo list in LTO mode when nested functions are put in a different
30826 partition than that of their parent function. */
30827 while ((node = limbo_die_list))
30829 dw_die_ref die = node->die;
30830 limbo_die_list = node->next;
30832 if (die->die_parent == NULL)
30834 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
30836 if (origin && origin->die_parent)
30837 add_child_die (origin->die_parent, die);
30838 else if (is_cu_die (die))
30840 else if (seen_error ())
30841 /* It's OK to be confused by errors in the input. */
30842 add_child_die (comp_unit_die (), die);
30843 else
30845 /* In certain situations, the lexical block containing a
30846 nested function can be optimized away, which results
30847 in the nested function die being orphaned. Likewise
30848 with the return type of that nested function. Force
30849 this to be a child of the containing function.
30851 It may happen that even the containing function got fully
30852 inlined and optimized out. In that case we are lost and
30853 assign the empty child. This should not be big issue as
30854 the function is likely unreachable too. */
30855 gcc_assert (node->created_for);
30857 if (DECL_P (node->created_for))
30858 origin = get_context_die (DECL_CONTEXT (node->created_for));
30859 else if (TYPE_P (node->created_for))
30860 origin = scope_die_for (node->created_for, comp_unit_die ());
30861 else
30862 origin = comp_unit_die ();
30864 add_child_die (origin, die);
30870 /* Reset DIEs so we can output them again. */
30872 static void
30873 reset_dies (dw_die_ref die)
30875 dw_die_ref c;
30877 /* Remove stuff we re-generate. */
30878 die->die_mark = 0;
30879 die->die_offset = 0;
30880 die->die_abbrev = 0;
30881 remove_AT (die, DW_AT_sibling);
30883 FOR_EACH_CHILD (die, c, reset_dies (c));
30886 /* Output stuff that dwarf requires at the end of every file,
30887 and generate the DWARF-2 debugging info. */
30889 static void
30890 dwarf2out_finish (const char *)
30892 comdat_type_node *ctnode;
30893 dw_die_ref main_comp_unit_die;
30894 unsigned char checksum[16];
30895 char dl_section_ref[MAX_ARTIFICIAL_LABEL_BYTES];
30897 /* Flush out any latecomers to the limbo party. */
30898 flush_limbo_die_list ();
30900 if (inline_entry_data_table)
30901 gcc_assert (inline_entry_data_table->elements () == 0);
30903 if (flag_checking)
30905 verify_die (comp_unit_die ());
30906 for (limbo_die_node *node = cu_die_list; node; node = node->next)
30907 verify_die (node->die);
30910 /* We shouldn't have any symbols with delayed asm names for
30911 DIEs generated after early finish. */
30912 gcc_assert (deferred_asm_name == NULL);
30914 gen_remaining_tmpl_value_param_die_attribute ();
30916 if (flag_generate_lto || flag_generate_offload)
30918 gcc_assert (flag_fat_lto_objects || flag_generate_offload);
30920 /* Prune stuff so that dwarf2out_finish runs successfully
30921 for the fat part of the object. */
30922 reset_dies (comp_unit_die ());
30923 for (limbo_die_node *node = cu_die_list; node; node = node->next)
30924 reset_dies (node->die);
30926 hash_table<comdat_type_hasher> comdat_type_table (100);
30927 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
30929 comdat_type_node **slot
30930 = comdat_type_table.find_slot (ctnode, INSERT);
30932 /* Don't reset types twice. */
30933 if (*slot != HTAB_EMPTY_ENTRY)
30934 continue;
30936 /* Add a pointer to the line table for the main compilation unit
30937 so that the debugger can make sense of DW_AT_decl_file
30938 attributes. */
30939 if (debug_info_level >= DINFO_LEVEL_TERSE)
30940 reset_dies (ctnode->root_die);
30942 *slot = ctnode;
30945 /* Reset die CU symbol so we don't output it twice. */
30946 comp_unit_die ()->die_id.die_symbol = NULL;
30948 /* Remove DW_AT_macro from the early output. */
30949 if (have_macinfo)
30950 remove_AT (comp_unit_die (), DEBUG_MACRO_ATTRIBUTE);
30952 /* Remove indirect string decisions. */
30953 debug_str_hash->traverse<void *, reset_indirect_string> (NULL);
30956 #if ENABLE_ASSERT_CHECKING
30958 dw_die_ref die = comp_unit_die (), c;
30959 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
30961 #endif
30962 resolve_addr (comp_unit_die ());
30963 move_marked_base_types ();
30965 /* Initialize sections and labels used for actual assembler output. */
30966 unsigned generation = init_sections_and_labels (false);
30968 /* Traverse the DIE's and add sibling attributes to those DIE's that
30969 have children. */
30970 add_sibling_attributes (comp_unit_die ());
30971 limbo_die_node *node;
30972 for (node = cu_die_list; node; node = node->next)
30973 add_sibling_attributes (node->die);
30974 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
30975 add_sibling_attributes (ctnode->root_die);
30977 /* When splitting DWARF info, we put some attributes in the
30978 skeleton compile_unit DIE that remains in the .o, while
30979 most attributes go in the DWO compile_unit_die. */
30980 if (dwarf_split_debug_info)
30982 limbo_die_node *cu;
30983 main_comp_unit_die = gen_compile_unit_die (NULL);
30984 if (dwarf_version >= 5)
30985 main_comp_unit_die->die_tag = DW_TAG_skeleton_unit;
30986 cu = limbo_die_list;
30987 gcc_assert (cu->die == main_comp_unit_die);
30988 limbo_die_list = limbo_die_list->next;
30989 cu->next = cu_die_list;
30990 cu_die_list = cu;
30992 else
30993 main_comp_unit_die = comp_unit_die ();
30995 /* Output a terminator label for the .text section. */
30996 switch_to_section (text_section);
30997 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
30998 if (cold_text_section)
31000 switch_to_section (cold_text_section);
31001 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
31004 /* We can only use the low/high_pc attributes if all of the code was
31005 in .text. */
31006 if (!have_multiple_function_sections
31007 || (dwarf_version < 3 && dwarf_strict))
31009 /* Don't add if the CU has no associated code. */
31010 if (text_section_used)
31011 add_AT_low_high_pc (main_comp_unit_die, text_section_label,
31012 text_end_label, true);
31014 else
31016 unsigned fde_idx;
31017 dw_fde_ref fde;
31018 bool range_list_added = false;
31020 if (text_section_used)
31021 add_ranges_by_labels (main_comp_unit_die, text_section_label,
31022 text_end_label, &range_list_added, true);
31023 if (cold_text_section_used)
31024 add_ranges_by_labels (main_comp_unit_die, cold_text_section_label,
31025 cold_end_label, &range_list_added, true);
31027 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
31029 if (DECL_IGNORED_P (fde->decl))
31030 continue;
31031 if (!fde->in_std_section)
31032 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
31033 fde->dw_fde_end, &range_list_added,
31034 true);
31035 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
31036 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
31037 fde->dw_fde_second_end, &range_list_added,
31038 true);
31041 if (range_list_added)
31043 /* We need to give .debug_loc and .debug_ranges an appropriate
31044 "base address". Use zero so that these addresses become
31045 absolute. Historically, we've emitted the unexpected
31046 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
31047 Emit both to give time for other tools to adapt. */
31048 add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
31049 if (! dwarf_strict && dwarf_version < 4)
31050 add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
31052 add_ranges (NULL);
31056 /* AIX Assembler inserts the length, so adjust the reference to match the
31057 offset expected by debuggers. */
31058 strcpy (dl_section_ref, debug_line_section_label);
31059 if (XCOFF_DEBUGGING_INFO)
31060 strcat (dl_section_ref, DWARF_INITIAL_LENGTH_SIZE_STR);
31062 if (debug_info_level >= DINFO_LEVEL_TERSE)
31063 add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
31064 dl_section_ref);
31066 if (have_macinfo)
31067 add_AT_macptr (comp_unit_die (), DEBUG_MACRO_ATTRIBUTE,
31068 macinfo_section_label);
31070 if (dwarf_split_debug_info)
31072 if (have_location_lists)
31074 if (dwarf_version >= 5)
31075 add_AT_loclistsptr (comp_unit_die (), DW_AT_loclists_base,
31076 loc_section_label);
31077 /* optimize_location_lists calculates the size of the lists,
31078 so index them first, and assign indices to the entries.
31079 Although optimize_location_lists will remove entries from
31080 the table, it only does so for duplicates, and therefore
31081 only reduces ref_counts to 1. */
31082 index_location_lists (comp_unit_die ());
31085 if (addr_index_table != NULL)
31087 unsigned int index = 0;
31088 addr_index_table
31089 ->traverse_noresize<unsigned int *, index_addr_table_entry>
31090 (&index);
31094 loc_list_idx = 0;
31095 if (have_location_lists)
31097 optimize_location_lists (comp_unit_die ());
31098 /* And finally assign indexes to the entries for -gsplit-dwarf. */
31099 if (dwarf_version >= 5 && dwarf_split_debug_info)
31100 assign_location_list_indexes (comp_unit_die ());
31103 save_macinfo_strings ();
31105 if (dwarf_split_debug_info)
31107 unsigned int index = 0;
31109 /* Add attributes common to skeleton compile_units and
31110 type_units. Because these attributes include strings, it
31111 must be done before freezing the string table. Top-level
31112 skeleton die attrs are added when the skeleton type unit is
31113 created, so ensure it is created by this point. */
31114 add_top_level_skeleton_die_attrs (main_comp_unit_die);
31115 debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
31118 /* Output all of the compilation units. We put the main one last so that
31119 the offsets are available to output_pubnames. */
31120 for (node = cu_die_list; node; node = node->next)
31121 output_comp_unit (node->die, 0, NULL);
31123 hash_table<comdat_type_hasher> comdat_type_table (100);
31124 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
31126 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
31128 /* Don't output duplicate types. */
31129 if (*slot != HTAB_EMPTY_ENTRY)
31130 continue;
31132 /* Add a pointer to the line table for the main compilation unit
31133 so that the debugger can make sense of DW_AT_decl_file
31134 attributes. */
31135 if (debug_info_level >= DINFO_LEVEL_TERSE)
31136 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
31137 (!dwarf_split_debug_info
31138 ? dl_section_ref
31139 : debug_skeleton_line_section_label));
31141 output_comdat_type_unit (ctnode);
31142 *slot = ctnode;
31145 if (dwarf_split_debug_info)
31147 int mark;
31148 struct md5_ctx ctx;
31150 if (dwarf_version >= 5 && !vec_safe_is_empty (ranges_table))
31151 index_rnglists ();
31153 /* Compute a checksum of the comp_unit to use as the dwo_id. */
31154 md5_init_ctx (&ctx);
31155 mark = 0;
31156 die_checksum (comp_unit_die (), &ctx, &mark);
31157 unmark_all_dies (comp_unit_die ());
31158 md5_finish_ctx (&ctx, checksum);
31160 if (dwarf_version < 5)
31162 /* Use the first 8 bytes of the checksum as the dwo_id,
31163 and add it to both comp-unit DIEs. */
31164 add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
31165 add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
31168 /* Add the base offset of the ranges table to the skeleton
31169 comp-unit DIE. */
31170 if (!vec_safe_is_empty (ranges_table))
31172 if (dwarf_version >= 5)
31173 add_AT_lineptr (main_comp_unit_die, DW_AT_rnglists_base,
31174 ranges_base_label);
31175 else
31176 add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
31177 ranges_section_label);
31180 switch_to_section (debug_addr_section);
31181 ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
31182 output_addr_table ();
31185 /* Output the main compilation unit if non-empty or if .debug_macinfo
31186 or .debug_macro will be emitted. */
31187 output_comp_unit (comp_unit_die (), have_macinfo,
31188 dwarf_split_debug_info ? checksum : NULL);
31190 if (dwarf_split_debug_info && info_section_emitted)
31191 output_skeleton_debug_sections (main_comp_unit_die, checksum);
31193 /* Output the abbreviation table. */
31194 if (vec_safe_length (abbrev_die_table) != 1)
31196 switch_to_section (debug_abbrev_section);
31197 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
31198 output_abbrev_section ();
31201 /* Output location list section if necessary. */
31202 if (have_location_lists)
31204 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
31205 char l2[MAX_ARTIFICIAL_LABEL_BYTES];
31206 /* Output the location lists info. */
31207 switch_to_section (debug_loc_section);
31208 if (dwarf_version >= 5)
31210 ASM_GENERATE_INTERNAL_LABEL (l1, DEBUG_LOC_SECTION_LABEL, 1);
31211 ASM_GENERATE_INTERNAL_LABEL (l2, DEBUG_LOC_SECTION_LABEL, 2);
31212 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
31213 dw2_asm_output_data (4, 0xffffffff,
31214 "Initial length escape value indicating "
31215 "64-bit DWARF extension");
31216 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
31217 "Length of Location Lists");
31218 ASM_OUTPUT_LABEL (asm_out_file, l1);
31219 output_dwarf_version ();
31220 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
31221 dw2_asm_output_data (1, 0, "Segment Size");
31222 dw2_asm_output_data (4, dwarf_split_debug_info ? loc_list_idx : 0,
31223 "Offset Entry Count");
31225 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
31226 if (dwarf_version >= 5 && dwarf_split_debug_info)
31228 unsigned int save_loc_list_idx = loc_list_idx;
31229 loc_list_idx = 0;
31230 output_loclists_offsets (comp_unit_die ());
31231 gcc_assert (save_loc_list_idx == loc_list_idx);
31233 output_location_lists (comp_unit_die ());
31234 if (dwarf_version >= 5)
31235 ASM_OUTPUT_LABEL (asm_out_file, l2);
31238 output_pubtables ();
31240 /* Output the address range information if a CU (.debug_info section)
31241 was emitted. We output an empty table even if we had no functions
31242 to put in it. This because the consumer has no way to tell the
31243 difference between an empty table that we omitted and failure to
31244 generate a table that would have contained data. */
31245 if (info_section_emitted)
31247 switch_to_section (debug_aranges_section);
31248 output_aranges ();
31251 /* Output ranges section if necessary. */
31252 if (!vec_safe_is_empty (ranges_table))
31254 if (dwarf_version >= 5)
31255 output_rnglists (generation);
31256 else
31257 output_ranges ();
31260 /* Have to end the macro section. */
31261 if (have_macinfo)
31263 switch_to_section (debug_macinfo_section);
31264 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
31265 output_macinfo (!dwarf_split_debug_info ? debug_line_section_label
31266 : debug_skeleton_line_section_label, false);
31267 dw2_asm_output_data (1, 0, "End compilation unit");
31270 /* Output the source line correspondence table. We must do this
31271 even if there is no line information. Otherwise, on an empty
31272 translation unit, we will generate a present, but empty,
31273 .debug_info section. IRIX 6.5 `nm' will then complain when
31274 examining the file. This is done late so that any filenames
31275 used by the debug_info section are marked as 'used'. */
31276 switch_to_section (debug_line_section);
31277 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
31278 if (! output_asm_line_debug_info ())
31279 output_line_info (false);
31281 if (dwarf_split_debug_info && info_section_emitted)
31283 switch_to_section (debug_skeleton_line_section);
31284 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
31285 output_line_info (true);
31288 /* If we emitted any indirect strings, output the string table too. */
31289 if (debug_str_hash || skeleton_debug_str_hash)
31290 output_indirect_strings ();
31291 if (debug_line_str_hash)
31293 switch_to_section (debug_line_str_section);
31294 const enum dwarf_form form = DW_FORM_line_strp;
31295 debug_line_str_hash->traverse<enum dwarf_form,
31296 output_indirect_string> (form);
31300 /* Returns a hash value for X (which really is a variable_value_struct). */
31302 inline hashval_t
31303 variable_value_hasher::hash (variable_value_struct *x)
31305 return (hashval_t) x->decl_id;
31308 /* Return nonzero if decl_id of variable_value_struct X is the same as
31309 UID of decl Y. */
31311 inline bool
31312 variable_value_hasher::equal (variable_value_struct *x, tree y)
31314 return x->decl_id == DECL_UID (y);
31317 /* Helper function for resolve_variable_value, handle
31318 DW_OP_GNU_variable_value in one location expression.
31319 Return true if exprloc has been changed into loclist. */
31321 static bool
31322 resolve_variable_value_in_expr (dw_attr_node *a, dw_loc_descr_ref loc)
31324 dw_loc_descr_ref next;
31325 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = next)
31327 next = loc->dw_loc_next;
31328 if (loc->dw_loc_opc != DW_OP_GNU_variable_value
31329 || loc->dw_loc_oprnd1.val_class != dw_val_class_decl_ref)
31330 continue;
31332 tree decl = loc->dw_loc_oprnd1.v.val_decl_ref;
31333 if (DECL_CONTEXT (decl) != current_function_decl)
31334 continue;
31336 dw_die_ref ref = lookup_decl_die (decl);
31337 if (ref)
31339 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
31340 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
31341 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
31342 continue;
31344 dw_loc_list_ref l = loc_list_from_tree (decl, 0, NULL);
31345 if (l == NULL)
31346 continue;
31347 if (l->dw_loc_next)
31349 if (AT_class (a) != dw_val_class_loc)
31350 continue;
31351 switch (a->dw_attr)
31353 /* Following attributes allow both exprloc and loclist
31354 classes, so we can change them into a loclist. */
31355 case DW_AT_location:
31356 case DW_AT_string_length:
31357 case DW_AT_return_addr:
31358 case DW_AT_data_member_location:
31359 case DW_AT_frame_base:
31360 case DW_AT_segment:
31361 case DW_AT_static_link:
31362 case DW_AT_use_location:
31363 case DW_AT_vtable_elem_location:
31364 if (prev)
31366 prev->dw_loc_next = NULL;
31367 prepend_loc_descr_to_each (l, AT_loc (a));
31369 if (next)
31370 add_loc_descr_to_each (l, next);
31371 a->dw_attr_val.val_class = dw_val_class_loc_list;
31372 a->dw_attr_val.val_entry = NULL;
31373 a->dw_attr_val.v.val_loc_list = l;
31374 have_location_lists = true;
31375 return true;
31376 /* Following attributes allow both exprloc and reference,
31377 so if the whole expression is DW_OP_GNU_variable_value alone
31378 we could transform it into reference. */
31379 case DW_AT_byte_size:
31380 case DW_AT_bit_size:
31381 case DW_AT_lower_bound:
31382 case DW_AT_upper_bound:
31383 case DW_AT_bit_stride:
31384 case DW_AT_count:
31385 case DW_AT_allocated:
31386 case DW_AT_associated:
31387 case DW_AT_byte_stride:
31388 if (prev == NULL && next == NULL)
31389 break;
31390 /* FALLTHRU */
31391 default:
31392 if (dwarf_strict)
31393 continue;
31394 break;
31396 /* Create DW_TAG_variable that we can refer to. */
31397 gen_decl_die (decl, NULL_TREE, NULL,
31398 lookup_decl_die (current_function_decl));
31399 ref = lookup_decl_die (decl);
31400 if (ref)
31402 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
31403 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
31404 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
31406 continue;
31408 if (prev)
31410 prev->dw_loc_next = l->expr;
31411 add_loc_descr (&prev->dw_loc_next, next);
31412 free_loc_descr (loc, NULL);
31413 next = prev->dw_loc_next;
31415 else
31417 memcpy (loc, l->expr, sizeof (dw_loc_descr_node));
31418 add_loc_descr (&loc, next);
31419 next = loc;
31421 loc = prev;
31423 return false;
31426 /* Attempt to resolve DW_OP_GNU_variable_value using loc_list_from_tree. */
31428 static void
31429 resolve_variable_value (dw_die_ref die)
31431 dw_attr_node *a;
31432 dw_loc_list_ref loc;
31433 unsigned ix;
31435 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
31436 switch (AT_class (a))
31438 case dw_val_class_loc:
31439 if (!resolve_variable_value_in_expr (a, AT_loc (a)))
31440 break;
31441 /* FALLTHRU */
31442 case dw_val_class_loc_list:
31443 loc = AT_loc_list (a);
31444 gcc_assert (loc);
31445 for (; loc; loc = loc->dw_loc_next)
31446 resolve_variable_value_in_expr (a, loc->expr);
31447 break;
31448 default:
31449 break;
31453 /* Attempt to optimize DW_OP_GNU_variable_value refering to
31454 temporaries in the current function. */
31456 static void
31457 resolve_variable_values (void)
31459 if (!variable_value_hash || !current_function_decl)
31460 return;
31462 struct variable_value_struct *node
31463 = variable_value_hash->find_with_hash (current_function_decl,
31464 DECL_UID (current_function_decl));
31466 if (node == NULL)
31467 return;
31469 unsigned int i;
31470 dw_die_ref die;
31471 FOR_EACH_VEC_SAFE_ELT (node->dies, i, die)
31472 resolve_variable_value (die);
31475 /* Helper function for note_variable_value, handle one location
31476 expression. */
31478 static void
31479 note_variable_value_in_expr (dw_die_ref die, dw_loc_descr_ref loc)
31481 for (; loc; loc = loc->dw_loc_next)
31482 if (loc->dw_loc_opc == DW_OP_GNU_variable_value
31483 && loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
31485 tree decl = loc->dw_loc_oprnd1.v.val_decl_ref;
31486 dw_die_ref ref = lookup_decl_die (decl);
31487 if (! ref && (flag_generate_lto || flag_generate_offload))
31489 /* ??? This is somewhat a hack because we do not create DIEs
31490 for variables not in BLOCK trees early but when generating
31491 early LTO output we need the dw_val_class_decl_ref to be
31492 fully resolved. For fat LTO objects we'd also like to
31493 undo this after LTO dwarf output. */
31494 gcc_assert (DECL_CONTEXT (decl));
31495 dw_die_ref ctx = lookup_decl_die (DECL_CONTEXT (decl));
31496 gcc_assert (ctx != NULL);
31497 gen_decl_die (decl, NULL_TREE, NULL, ctx);
31498 ref = lookup_decl_die (decl);
31499 gcc_assert (ref != NULL);
31501 if (ref)
31503 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
31504 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
31505 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
31506 continue;
31508 if (VAR_P (decl)
31509 && DECL_CONTEXT (decl)
31510 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL
31511 && lookup_decl_die (DECL_CONTEXT (decl)))
31513 if (!variable_value_hash)
31514 variable_value_hash
31515 = hash_table<variable_value_hasher>::create_ggc (10);
31517 tree fndecl = DECL_CONTEXT (decl);
31518 struct variable_value_struct *node;
31519 struct variable_value_struct **slot
31520 = variable_value_hash->find_slot_with_hash (fndecl,
31521 DECL_UID (fndecl),
31522 INSERT);
31523 if (*slot == NULL)
31525 node = ggc_cleared_alloc<variable_value_struct> ();
31526 node->decl_id = DECL_UID (fndecl);
31527 *slot = node;
31529 else
31530 node = *slot;
31532 vec_safe_push (node->dies, die);
31537 /* Walk the tree DIE and note DIEs with DW_OP_GNU_variable_value still
31538 with dw_val_class_decl_ref operand. */
31540 static void
31541 note_variable_value (dw_die_ref die)
31543 dw_die_ref c;
31544 dw_attr_node *a;
31545 dw_loc_list_ref loc;
31546 unsigned ix;
31548 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
31549 switch (AT_class (a))
31551 case dw_val_class_loc_list:
31552 loc = AT_loc_list (a);
31553 gcc_assert (loc);
31554 if (!loc->noted_variable_value)
31556 loc->noted_variable_value = 1;
31557 for (; loc; loc = loc->dw_loc_next)
31558 note_variable_value_in_expr (die, loc->expr);
31560 break;
31561 case dw_val_class_loc:
31562 note_variable_value_in_expr (die, AT_loc (a));
31563 break;
31564 default:
31565 break;
31568 /* Mark children. */
31569 FOR_EACH_CHILD (die, c, note_variable_value (c));
31572 /* Perform any cleanups needed after the early debug generation pass
31573 has run. */
31575 static void
31576 dwarf2out_early_finish (const char *filename)
31578 set_early_dwarf s;
31580 /* PCH might result in DW_AT_producer string being restored from the
31581 header compilation, so always fill it with empty string initially
31582 and overwrite only here. */
31583 dw_attr_node *producer = get_AT (comp_unit_die (), DW_AT_producer);
31584 producer_string = gen_producer_string ();
31585 producer->dw_attr_val.v.val_str->refcount--;
31586 producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
31588 /* Add the name for the main input file now. We delayed this from
31589 dwarf2out_init to avoid complications with PCH. */
31590 add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
31591 add_comp_dir_attribute (comp_unit_die ());
31593 /* When emitting DWARF5 .debug_line_str, move DW_AT_name and
31594 DW_AT_comp_dir into .debug_line_str section. */
31595 if (!dwarf2out_as_loc_support
31596 && dwarf_version >= 5
31597 && DWARF5_USE_DEBUG_LINE_STR)
31599 for (int i = 0; i < 2; i++)
31601 dw_attr_node *a = get_AT (comp_unit_die (),
31602 i ? DW_AT_comp_dir : DW_AT_name);
31603 if (a == NULL
31604 || AT_class (a) != dw_val_class_str
31605 || strlen (AT_string (a)) + 1 <= DWARF_OFFSET_SIZE)
31606 continue;
31608 if (! debug_line_str_hash)
31609 debug_line_str_hash
31610 = hash_table<indirect_string_hasher>::create_ggc (10);
31612 struct indirect_string_node *node
31613 = find_AT_string_in_table (AT_string (a), debug_line_str_hash);
31614 set_indirect_string (node);
31615 node->form = DW_FORM_line_strp;
31616 a->dw_attr_val.v.val_str->refcount--;
31617 a->dw_attr_val.v.val_str = node;
31621 /* With LTO early dwarf was really finished at compile-time, so make
31622 sure to adjust the phase after annotating the LTRANS CU DIE. */
31623 if (in_lto_p)
31625 early_dwarf_finished = true;
31626 return;
31629 /* Walk through the list of incomplete types again, trying once more to
31630 emit full debugging info for them. */
31631 retry_incomplete_types ();
31633 /* The point here is to flush out the limbo list so that it is empty
31634 and we don't need to stream it for LTO. */
31635 flush_limbo_die_list ();
31637 gen_scheduled_generic_parms_dies ();
31638 gen_remaining_tmpl_value_param_die_attribute ();
31640 /* Add DW_AT_linkage_name for all deferred DIEs. */
31641 for (limbo_die_node *node = deferred_asm_name; node; node = node->next)
31643 tree decl = node->created_for;
31644 if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
31645 /* A missing DECL_ASSEMBLER_NAME can be a constant DIE that
31646 ended up in deferred_asm_name before we knew it was
31647 constant and never written to disk. */
31648 && DECL_ASSEMBLER_NAME (decl))
31650 add_linkage_attr (node->die, decl);
31651 move_linkage_attr (node->die);
31654 deferred_asm_name = NULL;
31656 if (flag_eliminate_unused_debug_types)
31657 prune_unused_types ();
31659 /* Generate separate COMDAT sections for type DIEs. */
31660 if (use_debug_types)
31662 break_out_comdat_types (comp_unit_die ());
31664 /* Each new type_unit DIE was added to the limbo die list when created.
31665 Since these have all been added to comdat_type_list, clear the
31666 limbo die list. */
31667 limbo_die_list = NULL;
31669 /* For each new comdat type unit, copy declarations for incomplete
31670 types to make the new unit self-contained (i.e., no direct
31671 references to the main compile unit). */
31672 for (comdat_type_node *ctnode = comdat_type_list;
31673 ctnode != NULL; ctnode = ctnode->next)
31674 copy_decls_for_unworthy_types (ctnode->root_die);
31675 copy_decls_for_unworthy_types (comp_unit_die ());
31677 /* In the process of copying declarations from one unit to another,
31678 we may have left some declarations behind that are no longer
31679 referenced. Prune them. */
31680 prune_unused_types ();
31683 /* Traverse the DIE's and note DIEs with DW_OP_GNU_variable_value still
31684 with dw_val_class_decl_ref operand. */
31685 note_variable_value (comp_unit_die ());
31686 for (limbo_die_node *node = cu_die_list; node; node = node->next)
31687 note_variable_value (node->die);
31688 for (comdat_type_node *ctnode = comdat_type_list; ctnode != NULL;
31689 ctnode = ctnode->next)
31690 note_variable_value (ctnode->root_die);
31691 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
31692 note_variable_value (node->die);
31694 /* The AT_pubnames attribute needs to go in all skeleton dies, including
31695 both the main_cu and all skeleton TUs. Making this call unconditional
31696 would end up either adding a second copy of the AT_pubnames attribute, or
31697 requiring a special case in add_top_level_skeleton_die_attrs. */
31698 if (!dwarf_split_debug_info)
31699 add_AT_pubnames (comp_unit_die ());
31701 /* The early debug phase is now finished. */
31702 early_dwarf_finished = true;
31704 /* Do not generate DWARF assembler now when not producing LTO bytecode. */
31705 if (!flag_generate_lto && !flag_generate_offload)
31706 return;
31708 /* Now as we are going to output for LTO initialize sections and labels
31709 to the LTO variants. We don't need a random-seed postfix as other
31710 LTO sections as linking the LTO debug sections into one in a partial
31711 link is fine. */
31712 init_sections_and_labels (true);
31714 /* The output below is modeled after dwarf2out_finish with all
31715 location related output removed and some LTO specific changes.
31716 Some refactoring might make both smaller and easier to match up. */
31718 /* Traverse the DIE's and add add sibling attributes to those DIE's
31719 that have children. */
31720 add_sibling_attributes (comp_unit_die ());
31721 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
31722 add_sibling_attributes (node->die);
31723 for (comdat_type_node *ctnode = comdat_type_list;
31724 ctnode != NULL; ctnode = ctnode->next)
31725 add_sibling_attributes (ctnode->root_die);
31727 if (have_macinfo)
31728 add_AT_macptr (comp_unit_die (), DEBUG_MACRO_ATTRIBUTE,
31729 macinfo_section_label);
31731 save_macinfo_strings ();
31733 if (dwarf_split_debug_info)
31735 unsigned int index = 0;
31736 debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
31739 /* Output all of the compilation units. We put the main one last so that
31740 the offsets are available to output_pubnames. */
31741 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
31742 output_comp_unit (node->die, 0, NULL);
31744 hash_table<comdat_type_hasher> comdat_type_table (100);
31745 for (comdat_type_node *ctnode = comdat_type_list;
31746 ctnode != NULL; ctnode = ctnode->next)
31748 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
31750 /* Don't output duplicate types. */
31751 if (*slot != HTAB_EMPTY_ENTRY)
31752 continue;
31754 /* Add a pointer to the line table for the main compilation unit
31755 so that the debugger can make sense of DW_AT_decl_file
31756 attributes. */
31757 if (debug_info_level >= DINFO_LEVEL_TERSE)
31758 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
31759 (!dwarf_split_debug_info
31760 ? debug_line_section_label
31761 : debug_skeleton_line_section_label));
31763 output_comdat_type_unit (ctnode);
31764 *slot = ctnode;
31767 /* Stick a unique symbol to the main debuginfo section. */
31768 compute_comp_unit_symbol (comp_unit_die ());
31770 /* Output the main compilation unit. We always need it if only for
31771 the CU symbol. */
31772 output_comp_unit (comp_unit_die (), true, NULL);
31774 /* Output the abbreviation table. */
31775 if (vec_safe_length (abbrev_die_table) != 1)
31777 switch_to_section (debug_abbrev_section);
31778 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
31779 output_abbrev_section ();
31782 /* Have to end the macro section. */
31783 if (have_macinfo)
31785 /* We have to save macinfo state if we need to output it again
31786 for the FAT part of the object. */
31787 vec<macinfo_entry, va_gc> *saved_macinfo_table = macinfo_table;
31788 if (flag_fat_lto_objects)
31789 macinfo_table = macinfo_table->copy ();
31791 switch_to_section (debug_macinfo_section);
31792 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
31793 output_macinfo (debug_skeleton_line_section_label, true);
31794 dw2_asm_output_data (1, 0, "End compilation unit");
31796 /* Emit a skeleton debug_line section. */
31797 switch_to_section (debug_skeleton_line_section);
31798 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
31799 output_line_info (true);
31801 if (flag_fat_lto_objects)
31803 vec_free (macinfo_table);
31804 macinfo_table = saved_macinfo_table;
31809 /* If we emitted any indirect strings, output the string table too. */
31810 if (debug_str_hash || skeleton_debug_str_hash)
31811 output_indirect_strings ();
31813 /* Switch back to the text section. */
31814 switch_to_section (text_section);
31817 /* Reset all state within dwarf2out.c so that we can rerun the compiler
31818 within the same process. For use by toplev::finalize. */
31820 void
31821 dwarf2out_c_finalize (void)
31823 last_var_location_insn = NULL;
31824 cached_next_real_insn = NULL;
31825 used_rtx_array = NULL;
31826 incomplete_types = NULL;
31827 decl_scope_table = NULL;
31828 debug_info_section = NULL;
31829 debug_skeleton_info_section = NULL;
31830 debug_abbrev_section = NULL;
31831 debug_skeleton_abbrev_section = NULL;
31832 debug_aranges_section = NULL;
31833 debug_addr_section = NULL;
31834 debug_macinfo_section = NULL;
31835 debug_line_section = NULL;
31836 debug_skeleton_line_section = NULL;
31837 debug_loc_section = NULL;
31838 debug_pubnames_section = NULL;
31839 debug_pubtypes_section = NULL;
31840 debug_str_section = NULL;
31841 debug_line_str_section = NULL;
31842 debug_str_dwo_section = NULL;
31843 debug_str_offsets_section = NULL;
31844 debug_ranges_section = NULL;
31845 debug_frame_section = NULL;
31846 fde_vec = NULL;
31847 debug_str_hash = NULL;
31848 debug_line_str_hash = NULL;
31849 skeleton_debug_str_hash = NULL;
31850 dw2_string_counter = 0;
31851 have_multiple_function_sections = false;
31852 text_section_used = false;
31853 cold_text_section_used = false;
31854 cold_text_section = NULL;
31855 current_unit_personality = NULL;
31857 early_dwarf = false;
31858 early_dwarf_finished = false;
31860 next_die_offset = 0;
31861 single_comp_unit_die = NULL;
31862 comdat_type_list = NULL;
31863 limbo_die_list = NULL;
31864 file_table = NULL;
31865 decl_die_table = NULL;
31866 common_block_die_table = NULL;
31867 decl_loc_table = NULL;
31868 call_arg_locations = NULL;
31869 call_arg_loc_last = NULL;
31870 call_site_count = -1;
31871 tail_call_site_count = -1;
31872 cached_dw_loc_list_table = NULL;
31873 abbrev_die_table = NULL;
31874 delete dwarf_proc_stack_usage_map;
31875 dwarf_proc_stack_usage_map = NULL;
31876 line_info_label_num = 0;
31877 cur_line_info_table = NULL;
31878 text_section_line_info = NULL;
31879 cold_text_section_line_info = NULL;
31880 separate_line_info = NULL;
31881 info_section_emitted = false;
31882 pubname_table = NULL;
31883 pubtype_table = NULL;
31884 macinfo_table = NULL;
31885 ranges_table = NULL;
31886 ranges_by_label = NULL;
31887 rnglist_idx = 0;
31888 have_location_lists = false;
31889 loclabel_num = 0;
31890 poc_label_num = 0;
31891 last_emitted_file = NULL;
31892 label_num = 0;
31893 tmpl_value_parm_die_table = NULL;
31894 generic_type_instances = NULL;
31895 frame_pointer_fb_offset = 0;
31896 frame_pointer_fb_offset_valid = false;
31897 base_types.release ();
31898 XDELETEVEC (producer_string);
31899 producer_string = NULL;
31902 #include "gt-dwarf2out.h"