PR middle-end/80422
[official-gcc.git] / gcc / dwarf2out.c
blob27fb9f0519361a90fdf0a3ac763123d93063556f
1 /* Output Dwarf2 format symbol table information from GCC.
2 Copyright (C) 1992-2017 Free Software Foundation, Inc.
3 Contributed by Gary Funck (gary@intrepid.com).
4 Derived from DWARF 1 implementation of Ron Guilmette (rfg@monkeys.com).
5 Extensively modified by Jason Merrill (jason@cygnus.com).
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
23 /* TODO: Emit .debug_line header even when there are no functions, since
24 the file numbers are used by .debug_info. Alternately, leave
25 out locations for types and decls.
26 Avoid talking about ctors and op= for PODs.
27 Factor out common prologue sequences into multiple CIEs. */
29 /* The first part of this file deals with the DWARF 2 frame unwind
30 information, which is also used by the GCC efficient exception handling
31 mechanism. The second part, controlled only by an #ifdef
32 DWARF2_DEBUGGING_INFO, deals with the other DWARF 2 debugging
33 information. */
35 /* DWARF2 Abbreviation Glossary:
37 CFA = Canonical Frame Address
38 a fixed address on the stack which identifies a call frame.
39 We define it to be the value of SP just before the call insn.
40 The CFA register and offset, which may change during the course
41 of the function, are used to calculate its value at runtime.
43 CFI = Call Frame Instruction
44 an instruction for the DWARF2 abstract machine
46 CIE = Common Information Entry
47 information describing information common to one or more FDEs
49 DIE = Debugging Information Entry
51 FDE = Frame Description Entry
52 information describing the stack call frame, in particular,
53 how to restore registers
55 DW_CFA_... = DWARF2 CFA call frame instruction
56 DW_TAG_... = DWARF2 DIE tag */
58 #include "config.h"
59 #include "system.h"
60 #include "coretypes.h"
61 #include "target.h"
62 #include "function.h"
63 #include "rtl.h"
64 #include "tree.h"
65 #include "memmodel.h"
66 #include "tm_p.h"
67 #include "stringpool.h"
68 #include "insn-config.h"
69 #include "ira.h"
70 #include "cgraph.h"
71 #include "diagnostic.h"
72 #include "fold-const.h"
73 #include "stor-layout.h"
74 #include "varasm.h"
75 #include "version.h"
76 #include "flags.h"
77 #include "rtlhash.h"
78 #include "reload.h"
79 #include "output.h"
80 #include "expr.h"
81 #include "dwarf2out.h"
82 #include "dwarf2asm.h"
83 #include "toplev.h"
84 #include "md5.h"
85 #include "tree-pretty-print.h"
86 #include "debug.h"
87 #include "common/common-target.h"
88 #include "langhooks.h"
89 #include "lra.h"
90 #include "dumpfile.h"
91 #include "opts.h"
92 #include "tree-dfa.h"
93 #include "gdb/gdb-index.h"
94 #include "rtl-iter.h"
96 static void dwarf2out_source_line (unsigned int, unsigned int, const char *,
97 int, bool);
98 static rtx_insn *last_var_location_insn;
99 static rtx_insn *cached_next_real_insn;
100 static void dwarf2out_decl (tree);
102 #ifndef XCOFF_DEBUGGING_INFO
103 #define XCOFF_DEBUGGING_INFO 0
104 #endif
106 #ifndef HAVE_XCOFF_DWARF_EXTRAS
107 #define HAVE_XCOFF_DWARF_EXTRAS 0
108 #endif
110 #ifdef VMS_DEBUGGING_INFO
111 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
113 /* Define this macro to be a nonzero value if the directory specifications
114 which are output in the debug info should end with a separator. */
115 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
116 /* Define this macro to evaluate to a nonzero value if GCC should refrain
117 from generating indirect strings in DWARF2 debug information, for instance
118 if your target is stuck with an old version of GDB that is unable to
119 process them properly or uses VMS Debug. */
120 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
121 #else
122 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
123 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
124 #endif
126 /* ??? Poison these here until it can be done generically. They've been
127 totally replaced in this file; make sure it stays that way. */
128 #undef DWARF2_UNWIND_INFO
129 #undef DWARF2_FRAME_INFO
130 #if (GCC_VERSION >= 3000)
131 #pragma GCC poison DWARF2_UNWIND_INFO DWARF2_FRAME_INFO
132 #endif
134 /* The size of the target's pointer type. */
135 #ifndef PTR_SIZE
136 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
137 #endif
139 /* Array of RTXes referenced by the debugging information, which therefore
140 must be kept around forever. */
141 static GTY(()) vec<rtx, va_gc> *used_rtx_array;
143 /* A pointer to the base of a list of incomplete types which might be
144 completed at some later time. incomplete_types_list needs to be a
145 vec<tree, va_gc> *because we want to tell the garbage collector about
146 it. */
147 static GTY(()) vec<tree, va_gc> *incomplete_types;
149 /* A pointer to the base of a table of references to declaration
150 scopes. This table is a display which tracks the nesting
151 of declaration scopes at the current scope and containing
152 scopes. This table is used to find the proper place to
153 define type declaration DIE's. */
154 static GTY(()) vec<tree, va_gc> *decl_scope_table;
156 /* Pointers to various DWARF2 sections. */
157 static GTY(()) section *debug_info_section;
158 static GTY(()) section *debug_skeleton_info_section;
159 static GTY(()) section *debug_abbrev_section;
160 static GTY(()) section *debug_skeleton_abbrev_section;
161 static GTY(()) section *debug_aranges_section;
162 static GTY(()) section *debug_addr_section;
163 static GTY(()) section *debug_macinfo_section;
164 static const char *debug_macinfo_section_name;
165 static GTY(()) section *debug_line_section;
166 static GTY(()) section *debug_skeleton_line_section;
167 static GTY(()) section *debug_loc_section;
168 static GTY(()) section *debug_pubnames_section;
169 static GTY(()) section *debug_pubtypes_section;
170 static GTY(()) section *debug_str_section;
171 static GTY(()) section *debug_line_str_section;
172 static GTY(()) section *debug_str_dwo_section;
173 static GTY(()) section *debug_str_offsets_section;
174 static GTY(()) section *debug_ranges_section;
175 static GTY(()) section *debug_frame_section;
177 /* Maximum size (in bytes) of an artificially generated label. */
178 #define MAX_ARTIFICIAL_LABEL_BYTES 30
180 /* According to the (draft) DWARF 3 specification, the initial length
181 should either be 4 or 12 bytes. When it's 12 bytes, the first 4
182 bytes are 0xffffffff, followed by the length stored in the next 8
183 bytes.
185 However, the SGI/MIPS ABI uses an initial length which is equal to
186 DWARF_OFFSET_SIZE. It is defined (elsewhere) accordingly. */
188 #ifndef DWARF_INITIAL_LENGTH_SIZE
189 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
190 #endif
192 /* Round SIZE up to the nearest BOUNDARY. */
193 #define DWARF_ROUND(SIZE,BOUNDARY) \
194 ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
196 /* CIE identifier. */
197 #if HOST_BITS_PER_WIDE_INT >= 64
198 #define DWARF_CIE_ID \
199 (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
200 #else
201 #define DWARF_CIE_ID DW_CIE_ID
202 #endif
205 /* A vector for a table that contains frame description
206 information for each routine. */
207 #define NOT_INDEXED (-1U)
208 #define NO_INDEX_ASSIGNED (-2U)
210 static GTY(()) vec<dw_fde_ref, va_gc> *fde_vec;
212 struct GTY((for_user)) indirect_string_node {
213 const char *str;
214 unsigned int refcount;
215 enum dwarf_form form;
216 char *label;
217 unsigned int index;
220 struct indirect_string_hasher : ggc_ptr_hash<indirect_string_node>
222 typedef const char *compare_type;
224 static hashval_t hash (indirect_string_node *);
225 static bool equal (indirect_string_node *, const char *);
228 static GTY (()) hash_table<indirect_string_hasher> *debug_str_hash;
230 static GTY (()) hash_table<indirect_string_hasher> *debug_line_str_hash;
232 /* With split_debug_info, both the comp_dir and dwo_name go in the
233 main object file, rather than the dwo, similar to the force_direct
234 parameter elsewhere but with additional complications:
236 1) The string is needed in both the main object file and the dwo.
237 That is, the comp_dir and dwo_name will appear in both places.
239 2) Strings can use four forms: DW_FORM_string, DW_FORM_strp,
240 DW_FORM_line_strp or DW_FORM_GNU_str_index.
242 3) GCC chooses the form to use late, depending on the size and
243 reference count.
245 Rather than forcing the all debug string handling functions and
246 callers to deal with these complications, simply use a separate,
247 special-cased string table for any attribute that should go in the
248 main object file. This limits the complexity to just the places
249 that need it. */
251 static GTY (()) hash_table<indirect_string_hasher> *skeleton_debug_str_hash;
253 static GTY(()) int dw2_string_counter;
255 /* True if the compilation unit places functions in more than one section. */
256 static GTY(()) bool have_multiple_function_sections = false;
258 /* Whether the default text and cold text sections have been used at all. */
260 static GTY(()) bool text_section_used = false;
261 static GTY(()) bool cold_text_section_used = false;
263 /* The default cold text section. */
264 static GTY(()) section *cold_text_section;
266 /* The DIE for C++14 'auto' in a function return type. */
267 static GTY(()) dw_die_ref auto_die;
269 /* The DIE for C++14 'decltype(auto)' in a function return type. */
270 static GTY(()) dw_die_ref decltype_auto_die;
272 /* Forward declarations for functions defined in this file. */
274 static void output_call_frame_info (int);
275 static void dwarf2out_note_section_used (void);
277 /* Personality decl of current unit. Used only when assembler does not support
278 personality CFI. */
279 static GTY(()) rtx current_unit_personality;
281 /* .debug_rnglists next index. */
282 static unsigned int rnglist_idx;
284 /* Data and reference forms for relocatable data. */
285 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
286 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
288 #ifndef DEBUG_FRAME_SECTION
289 #define DEBUG_FRAME_SECTION ".debug_frame"
290 #endif
292 #ifndef FUNC_BEGIN_LABEL
293 #define FUNC_BEGIN_LABEL "LFB"
294 #endif
296 #ifndef FUNC_END_LABEL
297 #define FUNC_END_LABEL "LFE"
298 #endif
300 #ifndef PROLOGUE_END_LABEL
301 #define PROLOGUE_END_LABEL "LPE"
302 #endif
304 #ifndef EPILOGUE_BEGIN_LABEL
305 #define EPILOGUE_BEGIN_LABEL "LEB"
306 #endif
308 #ifndef FRAME_BEGIN_LABEL
309 #define FRAME_BEGIN_LABEL "Lframe"
310 #endif
311 #define CIE_AFTER_SIZE_LABEL "LSCIE"
312 #define CIE_END_LABEL "LECIE"
313 #define FDE_LABEL "LSFDE"
314 #define FDE_AFTER_SIZE_LABEL "LASFDE"
315 #define FDE_END_LABEL "LEFDE"
316 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
317 #define LINE_NUMBER_END_LABEL "LELT"
318 #define LN_PROLOG_AS_LABEL "LASLTP"
319 #define LN_PROLOG_END_LABEL "LELTP"
320 #define DIE_LABEL_PREFIX "DW"
322 /* Match the base name of a file to the base name of a compilation unit. */
324 static int
325 matches_main_base (const char *path)
327 /* Cache the last query. */
328 static const char *last_path = NULL;
329 static int last_match = 0;
330 if (path != last_path)
332 const char *base;
333 int length = base_of_path (path, &base);
334 last_path = path;
335 last_match = (length == main_input_baselength
336 && memcmp (base, main_input_basename, length) == 0);
338 return last_match;
341 #ifdef DEBUG_DEBUG_STRUCT
343 static int
344 dump_struct_debug (tree type, enum debug_info_usage usage,
345 enum debug_struct_file criterion, int generic,
346 int matches, int result)
348 /* Find the type name. */
349 tree type_decl = TYPE_STUB_DECL (type);
350 tree t = type_decl;
351 const char *name = 0;
352 if (TREE_CODE (t) == TYPE_DECL)
353 t = DECL_NAME (t);
354 if (t)
355 name = IDENTIFIER_POINTER (t);
357 fprintf (stderr, " struct %d %s %s %s %s %d %p %s\n",
358 criterion,
359 DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
360 matches ? "bas" : "hdr",
361 generic ? "gen" : "ord",
362 usage == DINFO_USAGE_DFN ? ";" :
363 usage == DINFO_USAGE_DIR_USE ? "." : "*",
364 result,
365 (void*) type_decl, name);
366 return result;
368 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
369 dump_struct_debug (type, usage, criterion, generic, matches, result)
371 #else
373 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
374 (result)
376 #endif
378 /* Get the number of HOST_WIDE_INTs needed to represent the precision
379 of the number. Some constants have a large uniform precision, so
380 we get the precision needed for the actual value of the number. */
382 static unsigned int
383 get_full_len (const wide_int &op)
385 int prec = wi::min_precision (op, UNSIGNED);
386 return ((prec + HOST_BITS_PER_WIDE_INT - 1)
387 / HOST_BITS_PER_WIDE_INT);
390 static bool
391 should_emit_struct_debug (tree type, enum debug_info_usage usage)
393 enum debug_struct_file criterion;
394 tree type_decl;
395 bool generic = lang_hooks.types.generic_p (type);
397 if (generic)
398 criterion = debug_struct_generic[usage];
399 else
400 criterion = debug_struct_ordinary[usage];
402 if (criterion == DINFO_STRUCT_FILE_NONE)
403 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
404 if (criterion == DINFO_STRUCT_FILE_ANY)
405 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
407 type_decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type));
409 if (type_decl != NULL)
411 if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
412 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
414 if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
415 return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
418 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
421 /* Switch [BACK] to eh_frame_section. If we don't have an eh_frame_section,
422 switch to the data section instead, and write out a synthetic start label
423 for collect2 the first time around. */
425 static void
426 switch_to_eh_frame_section (bool back ATTRIBUTE_UNUSED)
428 if (eh_frame_section == 0)
430 int flags;
432 if (EH_TABLES_CAN_BE_READ_ONLY)
434 int fde_encoding;
435 int per_encoding;
436 int lsda_encoding;
438 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
439 /*global=*/0);
440 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
441 /*global=*/1);
442 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
443 /*global=*/0);
444 flags = ((! flag_pic
445 || ((fde_encoding & 0x70) != DW_EH_PE_absptr
446 && (fde_encoding & 0x70) != DW_EH_PE_aligned
447 && (per_encoding & 0x70) != DW_EH_PE_absptr
448 && (per_encoding & 0x70) != DW_EH_PE_aligned
449 && (lsda_encoding & 0x70) != DW_EH_PE_absptr
450 && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
451 ? 0 : SECTION_WRITE);
453 else
454 flags = SECTION_WRITE;
456 #ifdef EH_FRAME_SECTION_NAME
457 eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
458 #else
459 eh_frame_section = ((flags == SECTION_WRITE)
460 ? data_section : readonly_data_section);
461 #endif /* EH_FRAME_SECTION_NAME */
464 switch_to_section (eh_frame_section);
466 #ifdef EH_FRAME_THROUGH_COLLECT2
467 /* We have no special eh_frame section. Emit special labels to guide
468 collect2. */
469 if (!back)
471 tree label = get_file_function_name ("F");
472 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
473 targetm.asm_out.globalize_label (asm_out_file,
474 IDENTIFIER_POINTER (label));
475 ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
477 #endif
480 /* Switch [BACK] to the eh or debug frame table section, depending on
481 FOR_EH. */
483 static void
484 switch_to_frame_table_section (int for_eh, bool back)
486 if (for_eh)
487 switch_to_eh_frame_section (back);
488 else
490 if (!debug_frame_section)
491 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
492 SECTION_DEBUG, NULL);
493 switch_to_section (debug_frame_section);
497 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used. */
499 enum dw_cfi_oprnd_type
500 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
502 switch (cfi)
504 case DW_CFA_nop:
505 case DW_CFA_GNU_window_save:
506 case DW_CFA_remember_state:
507 case DW_CFA_restore_state:
508 return dw_cfi_oprnd_unused;
510 case DW_CFA_set_loc:
511 case DW_CFA_advance_loc1:
512 case DW_CFA_advance_loc2:
513 case DW_CFA_advance_loc4:
514 case DW_CFA_MIPS_advance_loc8:
515 return dw_cfi_oprnd_addr;
517 case DW_CFA_offset:
518 case DW_CFA_offset_extended:
519 case DW_CFA_def_cfa:
520 case DW_CFA_offset_extended_sf:
521 case DW_CFA_def_cfa_sf:
522 case DW_CFA_restore:
523 case DW_CFA_restore_extended:
524 case DW_CFA_undefined:
525 case DW_CFA_same_value:
526 case DW_CFA_def_cfa_register:
527 case DW_CFA_register:
528 case DW_CFA_expression:
529 case DW_CFA_val_expression:
530 return dw_cfi_oprnd_reg_num;
532 case DW_CFA_def_cfa_offset:
533 case DW_CFA_GNU_args_size:
534 case DW_CFA_def_cfa_offset_sf:
535 return dw_cfi_oprnd_offset;
537 case DW_CFA_def_cfa_expression:
538 return dw_cfi_oprnd_loc;
540 default:
541 gcc_unreachable ();
545 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used. */
547 enum dw_cfi_oprnd_type
548 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
550 switch (cfi)
552 case DW_CFA_def_cfa:
553 case DW_CFA_def_cfa_sf:
554 case DW_CFA_offset:
555 case DW_CFA_offset_extended_sf:
556 case DW_CFA_offset_extended:
557 return dw_cfi_oprnd_offset;
559 case DW_CFA_register:
560 return dw_cfi_oprnd_reg_num;
562 case DW_CFA_expression:
563 case DW_CFA_val_expression:
564 return dw_cfi_oprnd_loc;
566 default:
567 return dw_cfi_oprnd_unused;
571 /* Output one FDE. */
573 static void
574 output_fde (dw_fde_ref fde, bool for_eh, bool second,
575 char *section_start_label, int fde_encoding, char *augmentation,
576 bool any_lsda_needed, int lsda_encoding)
578 const char *begin, *end;
579 static unsigned int j;
580 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
582 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
583 /* empty */ 0);
584 targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
585 for_eh + j);
586 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
587 ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
588 if (!XCOFF_DEBUGGING_INFO || for_eh)
590 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
591 dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
592 " indicating 64-bit DWARF extension");
593 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
594 "FDE Length");
596 ASM_OUTPUT_LABEL (asm_out_file, l1);
598 if (for_eh)
599 dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
600 else
601 dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
602 debug_frame_section, "FDE CIE offset");
604 begin = second ? fde->dw_fde_second_begin : fde->dw_fde_begin;
605 end = second ? fde->dw_fde_second_end : fde->dw_fde_end;
607 if (for_eh)
609 rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
610 SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
611 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
612 "FDE initial location");
613 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
614 end, begin, "FDE address range");
616 else
618 dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
619 dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
622 if (augmentation[0])
624 if (any_lsda_needed)
626 int size = size_of_encoded_value (lsda_encoding);
628 if (lsda_encoding == DW_EH_PE_aligned)
630 int offset = ( 4 /* Length */
631 + 4 /* CIE offset */
632 + 2 * size_of_encoded_value (fde_encoding)
633 + 1 /* Augmentation size */ );
634 int pad = -offset & (PTR_SIZE - 1);
636 size += pad;
637 gcc_assert (size_of_uleb128 (size) == 1);
640 dw2_asm_output_data_uleb128 (size, "Augmentation size");
642 if (fde->uses_eh_lsda)
644 ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
645 fde->funcdef_number);
646 dw2_asm_output_encoded_addr_rtx (lsda_encoding,
647 gen_rtx_SYMBOL_REF (Pmode, l1),
648 false,
649 "Language Specific Data Area");
651 else
653 if (lsda_encoding == DW_EH_PE_aligned)
654 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
655 dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
656 "Language Specific Data Area (none)");
659 else
660 dw2_asm_output_data_uleb128 (0, "Augmentation size");
663 /* Loop through the Call Frame Instructions associated with this FDE. */
664 fde->dw_fde_current_label = begin;
666 size_t from, until, i;
668 from = 0;
669 until = vec_safe_length (fde->dw_fde_cfi);
671 if (fde->dw_fde_second_begin == NULL)
673 else if (!second)
674 until = fde->dw_fde_switch_cfi_index;
675 else
676 from = fde->dw_fde_switch_cfi_index;
678 for (i = from; i < until; i++)
679 output_cfi ((*fde->dw_fde_cfi)[i], fde, for_eh);
682 /* If we are to emit a ref/link from function bodies to their frame tables,
683 do it now. This is typically performed to make sure that tables
684 associated with functions are dragged with them and not discarded in
685 garbage collecting links. We need to do this on a per function basis to
686 cope with -ffunction-sections. */
688 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
689 /* Switch to the function section, emit the ref to the tables, and
690 switch *back* into the table section. */
691 switch_to_section (function_section (fde->decl));
692 ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
693 switch_to_frame_table_section (for_eh, true);
694 #endif
696 /* Pad the FDE out to an address sized boundary. */
697 ASM_OUTPUT_ALIGN (asm_out_file,
698 floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
699 ASM_OUTPUT_LABEL (asm_out_file, l2);
701 j += 2;
704 /* Return true if frame description entry FDE is needed for EH. */
706 static bool
707 fde_needed_for_eh_p (dw_fde_ref fde)
709 if (flag_asynchronous_unwind_tables)
710 return true;
712 if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
713 return true;
715 if (fde->uses_eh_lsda)
716 return true;
718 /* If exceptions are enabled, we have collected nothrow info. */
719 if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
720 return false;
722 return true;
725 /* Output the call frame information used to record information
726 that relates to calculating the frame pointer, and records the
727 location of saved registers. */
729 static void
730 output_call_frame_info (int for_eh)
732 unsigned int i;
733 dw_fde_ref fde;
734 dw_cfi_ref cfi;
735 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
736 char section_start_label[MAX_ARTIFICIAL_LABEL_BYTES];
737 bool any_lsda_needed = false;
738 char augmentation[6];
739 int augmentation_size;
740 int fde_encoding = DW_EH_PE_absptr;
741 int per_encoding = DW_EH_PE_absptr;
742 int lsda_encoding = DW_EH_PE_absptr;
743 int return_reg;
744 rtx personality = NULL;
745 int dw_cie_version;
747 /* Don't emit a CIE if there won't be any FDEs. */
748 if (!fde_vec)
749 return;
751 /* Nothing to do if the assembler's doing it all. */
752 if (dwarf2out_do_cfi_asm ())
753 return;
755 /* If we don't have any functions we'll want to unwind out of, don't emit
756 any EH unwind information. If we make FDEs linkonce, we may have to
757 emit an empty label for an FDE that wouldn't otherwise be emitted. We
758 want to avoid having an FDE kept around when the function it refers to
759 is discarded. Example where this matters: a primary function template
760 in C++ requires EH information, an explicit specialization doesn't. */
761 if (for_eh)
763 bool any_eh_needed = false;
765 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
767 if (fde->uses_eh_lsda)
768 any_eh_needed = any_lsda_needed = true;
769 else if (fde_needed_for_eh_p (fde))
770 any_eh_needed = true;
771 else if (TARGET_USES_WEAK_UNWIND_INFO)
772 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, 1, 1);
775 if (!any_eh_needed)
776 return;
779 /* We're going to be generating comments, so turn on app. */
780 if (flag_debug_asm)
781 app_enable ();
783 /* Switch to the proper frame section, first time. */
784 switch_to_frame_table_section (for_eh, false);
786 ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
787 ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
789 /* Output the CIE. */
790 ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
791 ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
792 if (!XCOFF_DEBUGGING_INFO || for_eh)
794 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
795 dw2_asm_output_data (4, 0xffffffff,
796 "Initial length escape value indicating 64-bit DWARF extension");
797 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
798 "Length of Common Information Entry");
800 ASM_OUTPUT_LABEL (asm_out_file, l1);
802 /* Now that the CIE pointer is PC-relative for EH,
803 use 0 to identify the CIE. */
804 dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
805 (for_eh ? 0 : DWARF_CIE_ID),
806 "CIE Identifier Tag");
808 /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
809 use CIE version 1, unless that would produce incorrect results
810 due to overflowing the return register column. */
811 return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
812 dw_cie_version = 1;
813 if (return_reg >= 256 || dwarf_version > 2)
814 dw_cie_version = 3;
815 dw2_asm_output_data (1, dw_cie_version, "CIE Version");
817 augmentation[0] = 0;
818 augmentation_size = 0;
820 personality = current_unit_personality;
821 if (for_eh)
823 char *p;
825 /* Augmentation:
826 z Indicates that a uleb128 is present to size the
827 augmentation section.
828 L Indicates the encoding (and thus presence) of
829 an LSDA pointer in the FDE augmentation.
830 R Indicates a non-default pointer encoding for
831 FDE code pointers.
832 P Indicates the presence of an encoding + language
833 personality routine in the CIE augmentation. */
835 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
836 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
837 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
839 p = augmentation + 1;
840 if (personality)
842 *p++ = 'P';
843 augmentation_size += 1 + size_of_encoded_value (per_encoding);
844 assemble_external_libcall (personality);
846 if (any_lsda_needed)
848 *p++ = 'L';
849 augmentation_size += 1;
851 if (fde_encoding != DW_EH_PE_absptr)
853 *p++ = 'R';
854 augmentation_size += 1;
856 if (p > augmentation + 1)
858 augmentation[0] = 'z';
859 *p = '\0';
862 /* Ug. Some platforms can't do unaligned dynamic relocations at all. */
863 if (personality && per_encoding == DW_EH_PE_aligned)
865 int offset = ( 4 /* Length */
866 + 4 /* CIE Id */
867 + 1 /* CIE version */
868 + strlen (augmentation) + 1 /* Augmentation */
869 + size_of_uleb128 (1) /* Code alignment */
870 + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
871 + 1 /* RA column */
872 + 1 /* Augmentation size */
873 + 1 /* Personality encoding */ );
874 int pad = -offset & (PTR_SIZE - 1);
876 augmentation_size += pad;
878 /* Augmentations should be small, so there's scarce need to
879 iterate for a solution. Die if we exceed one uleb128 byte. */
880 gcc_assert (size_of_uleb128 (augmentation_size) == 1);
884 dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
885 if (dw_cie_version >= 4)
887 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
888 dw2_asm_output_data (1, 0, "CIE Segment Size");
890 dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
891 dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
892 "CIE Data Alignment Factor");
894 if (dw_cie_version == 1)
895 dw2_asm_output_data (1, return_reg, "CIE RA Column");
896 else
897 dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
899 if (augmentation[0])
901 dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
902 if (personality)
904 dw2_asm_output_data (1, per_encoding, "Personality (%s)",
905 eh_data_format_name (per_encoding));
906 dw2_asm_output_encoded_addr_rtx (per_encoding,
907 personality,
908 true, NULL);
911 if (any_lsda_needed)
912 dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
913 eh_data_format_name (lsda_encoding));
915 if (fde_encoding != DW_EH_PE_absptr)
916 dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
917 eh_data_format_name (fde_encoding));
920 FOR_EACH_VEC_ELT (*cie_cfi_vec, i, cfi)
921 output_cfi (cfi, NULL, for_eh);
923 /* Pad the CIE out to an address sized boundary. */
924 ASM_OUTPUT_ALIGN (asm_out_file,
925 floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
926 ASM_OUTPUT_LABEL (asm_out_file, l2);
928 /* Loop through all of the FDE's. */
929 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
931 unsigned int k;
933 /* Don't emit EH unwind info for leaf functions that don't need it. */
934 if (for_eh && !fde_needed_for_eh_p (fde))
935 continue;
937 for (k = 0; k < (fde->dw_fde_second_begin ? 2 : 1); k++)
938 output_fde (fde, for_eh, k, section_start_label, fde_encoding,
939 augmentation, any_lsda_needed, lsda_encoding);
942 if (for_eh && targetm.terminate_dw2_eh_frame_info)
943 dw2_asm_output_data (4, 0, "End of Table");
945 /* Turn off app to make assembly quicker. */
946 if (flag_debug_asm)
947 app_disable ();
950 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed. */
952 static void
953 dwarf2out_do_cfi_startproc (bool second)
955 int enc;
956 rtx ref;
957 rtx personality = get_personality_function (current_function_decl);
959 fprintf (asm_out_file, "\t.cfi_startproc\n");
961 if (personality)
963 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
964 ref = personality;
966 /* ??? The GAS support isn't entirely consistent. We have to
967 handle indirect support ourselves, but PC-relative is done
968 in the assembler. Further, the assembler can't handle any
969 of the weirder relocation types. */
970 if (enc & DW_EH_PE_indirect)
971 ref = dw2_force_const_mem (ref, true);
973 fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
974 output_addr_const (asm_out_file, ref);
975 fputc ('\n', asm_out_file);
978 if (crtl->uses_eh_lsda)
980 char lab[MAX_ARTIFICIAL_LABEL_BYTES];
982 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
983 ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
984 current_function_funcdef_no);
985 ref = gen_rtx_SYMBOL_REF (Pmode, lab);
986 SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
988 if (enc & DW_EH_PE_indirect)
989 ref = dw2_force_const_mem (ref, true);
991 fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
992 output_addr_const (asm_out_file, ref);
993 fputc ('\n', asm_out_file);
997 /* Allocate CURRENT_FDE. Immediately initialize all we can, noting that
998 this allocation may be done before pass_final. */
1000 dw_fde_ref
1001 dwarf2out_alloc_current_fde (void)
1003 dw_fde_ref fde;
1005 fde = ggc_cleared_alloc<dw_fde_node> ();
1006 fde->decl = current_function_decl;
1007 fde->funcdef_number = current_function_funcdef_no;
1008 fde->fde_index = vec_safe_length (fde_vec);
1009 fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
1010 fde->uses_eh_lsda = crtl->uses_eh_lsda;
1011 fde->nothrow = crtl->nothrow;
1012 fde->drap_reg = INVALID_REGNUM;
1013 fde->vdrap_reg = INVALID_REGNUM;
1015 /* Record the FDE associated with this function. */
1016 cfun->fde = fde;
1017 vec_safe_push (fde_vec, fde);
1019 return fde;
1022 /* Output a marker (i.e. a label) for the beginning of a function, before
1023 the prologue. */
1025 void
1026 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
1027 unsigned int column ATTRIBUTE_UNUSED,
1028 const char *file ATTRIBUTE_UNUSED)
1030 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1031 char * dup_label;
1032 dw_fde_ref fde;
1033 section *fnsec;
1034 bool do_frame;
1036 current_function_func_begin_label = NULL;
1038 do_frame = dwarf2out_do_frame ();
1040 /* ??? current_function_func_begin_label is also used by except.c for
1041 call-site information. We must emit this label if it might be used. */
1042 if (!do_frame
1043 && (!flag_exceptions
1044 || targetm_common.except_unwind_info (&global_options) == UI_SJLJ))
1045 return;
1047 fnsec = function_section (current_function_decl);
1048 switch_to_section (fnsec);
1049 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
1050 current_function_funcdef_no);
1051 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
1052 current_function_funcdef_no);
1053 dup_label = xstrdup (label);
1054 current_function_func_begin_label = dup_label;
1056 /* We can elide the fde allocation if we're not emitting debug info. */
1057 if (!do_frame)
1058 return;
1060 /* Cater to the various TARGET_ASM_OUTPUT_MI_THUNK implementations that
1061 emit insns as rtx but bypass the bulk of rest_of_compilation, which
1062 would include pass_dwarf2_frame. If we've not created the FDE yet,
1063 do so now. */
1064 fde = cfun->fde;
1065 if (fde == NULL)
1066 fde = dwarf2out_alloc_current_fde ();
1068 /* Initialize the bits of CURRENT_FDE that were not available earlier. */
1069 fde->dw_fde_begin = dup_label;
1070 fde->dw_fde_current_label = dup_label;
1071 fde->in_std_section = (fnsec == text_section
1072 || (cold_text_section && fnsec == cold_text_section));
1074 /* We only want to output line number information for the genuine dwarf2
1075 prologue case, not the eh frame case. */
1076 #ifdef DWARF2_DEBUGGING_INFO
1077 if (file)
1078 dwarf2out_source_line (line, column, file, 0, true);
1079 #endif
1081 if (dwarf2out_do_cfi_asm ())
1082 dwarf2out_do_cfi_startproc (false);
1083 else
1085 rtx personality = get_personality_function (current_function_decl);
1086 if (!current_unit_personality)
1087 current_unit_personality = personality;
1089 /* We cannot keep a current personality per function as without CFI
1090 asm, at the point where we emit the CFI data, there is no current
1091 function anymore. */
1092 if (personality && current_unit_personality != personality)
1093 sorry ("multiple EH personalities are supported only with assemblers "
1094 "supporting .cfi_personality directive");
1098 /* Output a marker (i.e. a label) for the end of the generated code
1099 for a function prologue. This gets called *after* the prologue code has
1100 been generated. */
1102 void
1103 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
1104 const char *file ATTRIBUTE_UNUSED)
1106 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1108 /* Output a label to mark the endpoint of the code generated for this
1109 function. */
1110 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
1111 current_function_funcdef_no);
1112 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
1113 current_function_funcdef_no);
1114 cfun->fde->dw_fde_vms_end_prologue = xstrdup (label);
1117 /* Output a marker (i.e. a label) for the beginning of the generated code
1118 for a function epilogue. This gets called *before* the prologue code has
1119 been generated. */
1121 void
1122 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1123 const char *file ATTRIBUTE_UNUSED)
1125 dw_fde_ref fde = cfun->fde;
1126 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1128 if (fde->dw_fde_vms_begin_epilogue)
1129 return;
1131 /* Output a label to mark the endpoint of the code generated for this
1132 function. */
1133 ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
1134 current_function_funcdef_no);
1135 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
1136 current_function_funcdef_no);
1137 fde->dw_fde_vms_begin_epilogue = xstrdup (label);
1140 /* Output a marker (i.e. a label) for the absolute end of the generated code
1141 for a function definition. This gets called *after* the epilogue code has
1142 been generated. */
1144 void
1145 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1146 const char *file ATTRIBUTE_UNUSED)
1148 dw_fde_ref fde;
1149 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1151 last_var_location_insn = NULL;
1152 cached_next_real_insn = NULL;
1154 if (dwarf2out_do_cfi_asm ())
1155 fprintf (asm_out_file, "\t.cfi_endproc\n");
1157 /* Output a label to mark the endpoint of the code generated for this
1158 function. */
1159 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
1160 current_function_funcdef_no);
1161 ASM_OUTPUT_LABEL (asm_out_file, label);
1162 fde = cfun->fde;
1163 gcc_assert (fde != NULL);
1164 if (fde->dw_fde_second_begin == NULL)
1165 fde->dw_fde_end = xstrdup (label);
1168 void
1169 dwarf2out_frame_finish (void)
1171 /* Output call frame information. */
1172 if (targetm.debug_unwind_info () == UI_DWARF2)
1173 output_call_frame_info (0);
1175 /* Output another copy for the unwinder. */
1176 if ((flag_unwind_tables || flag_exceptions)
1177 && targetm_common.except_unwind_info (&global_options) == UI_DWARF2)
1178 output_call_frame_info (1);
1181 /* Note that the current function section is being used for code. */
1183 static void
1184 dwarf2out_note_section_used (void)
1186 section *sec = current_function_section ();
1187 if (sec == text_section)
1188 text_section_used = true;
1189 else if (sec == cold_text_section)
1190 cold_text_section_used = true;
1193 static void var_location_switch_text_section (void);
1194 static void set_cur_line_info_table (section *);
1196 void
1197 dwarf2out_switch_text_section (void)
1199 section *sect;
1200 dw_fde_ref fde = cfun->fde;
1202 gcc_assert (cfun && fde && fde->dw_fde_second_begin == NULL);
1204 if (!in_cold_section_p)
1206 fde->dw_fde_end = crtl->subsections.cold_section_end_label;
1207 fde->dw_fde_second_begin = crtl->subsections.hot_section_label;
1208 fde->dw_fde_second_end = crtl->subsections.hot_section_end_label;
1210 else
1212 fde->dw_fde_end = crtl->subsections.hot_section_end_label;
1213 fde->dw_fde_second_begin = crtl->subsections.cold_section_label;
1214 fde->dw_fde_second_end = crtl->subsections.cold_section_end_label;
1216 have_multiple_function_sections = true;
1218 /* There is no need to mark used sections when not debugging. */
1219 if (cold_text_section != NULL)
1220 dwarf2out_note_section_used ();
1222 if (dwarf2out_do_cfi_asm ())
1223 fprintf (asm_out_file, "\t.cfi_endproc\n");
1225 /* Now do the real section switch. */
1226 sect = current_function_section ();
1227 switch_to_section (sect);
1229 fde->second_in_std_section
1230 = (sect == text_section
1231 || (cold_text_section && sect == cold_text_section));
1233 if (dwarf2out_do_cfi_asm ())
1234 dwarf2out_do_cfi_startproc (true);
1236 var_location_switch_text_section ();
1238 if (cold_text_section != NULL)
1239 set_cur_line_info_table (sect);
1242 /* And now, the subset of the debugging information support code necessary
1243 for emitting location expressions. */
1245 /* Data about a single source file. */
1246 struct GTY((for_user)) dwarf_file_data {
1247 const char * filename;
1248 int emitted_number;
1251 /* Describe an entry into the .debug_addr section. */
1253 enum ate_kind {
1254 ate_kind_rtx,
1255 ate_kind_rtx_dtprel,
1256 ate_kind_label
1259 struct GTY((for_user)) addr_table_entry {
1260 enum ate_kind kind;
1261 unsigned int refcount;
1262 unsigned int index;
1263 union addr_table_entry_struct_union
1265 rtx GTY ((tag ("0"))) rtl;
1266 char * GTY ((tag ("1"))) label;
1268 GTY ((desc ("%1.kind"))) addr;
1271 /* Location lists are ranges + location descriptions for that range,
1272 so you can track variables that are in different places over
1273 their entire life. */
1274 typedef struct GTY(()) dw_loc_list_struct {
1275 dw_loc_list_ref dw_loc_next;
1276 const char *begin; /* Label and addr_entry for start of range */
1277 addr_table_entry *begin_entry;
1278 const char *end; /* Label for end of range */
1279 char *ll_symbol; /* Label for beginning of location list.
1280 Only on head of list */
1281 const char *section; /* Section this loclist is relative to */
1282 dw_loc_descr_ref expr;
1283 hashval_t hash;
1284 /* True if all addresses in this and subsequent lists are known to be
1285 resolved. */
1286 bool resolved_addr;
1287 /* True if this list has been replaced by dw_loc_next. */
1288 bool replaced;
1289 /* True if it has been emitted into .debug_loc* / .debug_loclists*
1290 section. */
1291 unsigned char emitted : 1;
1292 /* True if hash field is index rather than hash value. */
1293 unsigned char num_assigned : 1;
1294 /* True if .debug_loclists.dwo offset has been emitted for it already. */
1295 unsigned char offset_emitted : 1;
1296 /* True if note_variable_value_in_expr has been called on it. */
1297 unsigned char noted_variable_value : 1;
1298 /* True if the range should be emitted even if begin and end
1299 are the same. */
1300 bool force;
1301 } dw_loc_list_node;
1303 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
1304 static dw_loc_descr_ref uint_loc_descriptor (unsigned HOST_WIDE_INT);
1306 /* Convert a DWARF stack opcode into its string name. */
1308 static const char *
1309 dwarf_stack_op_name (unsigned int op)
1311 const char *name = get_DW_OP_name (op);
1313 if (name != NULL)
1314 return name;
1316 return "OP_<unknown>";
1319 /* Return a pointer to a newly allocated location description. Location
1320 descriptions are simple expression terms that can be strung
1321 together to form more complicated location (address) descriptions. */
1323 static inline dw_loc_descr_ref
1324 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
1325 unsigned HOST_WIDE_INT oprnd2)
1327 dw_loc_descr_ref descr = ggc_cleared_alloc<dw_loc_descr_node> ();
1329 descr->dw_loc_opc = op;
1330 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
1331 descr->dw_loc_oprnd1.val_entry = NULL;
1332 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
1333 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
1334 descr->dw_loc_oprnd2.val_entry = NULL;
1335 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
1337 return descr;
1340 /* Return a pointer to a newly allocated location description for
1341 REG and OFFSET. */
1343 static inline dw_loc_descr_ref
1344 new_reg_loc_descr (unsigned int reg, unsigned HOST_WIDE_INT offset)
1346 if (reg <= 31)
1347 return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
1348 offset, 0);
1349 else
1350 return new_loc_descr (DW_OP_bregx, reg, offset);
1353 /* Add a location description term to a location description expression. */
1355 static inline void
1356 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
1358 dw_loc_descr_ref *d;
1360 /* Find the end of the chain. */
1361 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
1364 *d = descr;
1367 /* Compare two location operands for exact equality. */
1369 static bool
1370 dw_val_equal_p (dw_val_node *a, dw_val_node *b)
1372 if (a->val_class != b->val_class)
1373 return false;
1374 switch (a->val_class)
1376 case dw_val_class_none:
1377 return true;
1378 case dw_val_class_addr:
1379 return rtx_equal_p (a->v.val_addr, b->v.val_addr);
1381 case dw_val_class_offset:
1382 case dw_val_class_unsigned_const:
1383 case dw_val_class_const:
1384 case dw_val_class_unsigned_const_implicit:
1385 case dw_val_class_const_implicit:
1386 case dw_val_class_range_list:
1387 /* These are all HOST_WIDE_INT, signed or unsigned. */
1388 return a->v.val_unsigned == b->v.val_unsigned;
1390 case dw_val_class_loc:
1391 return a->v.val_loc == b->v.val_loc;
1392 case dw_val_class_loc_list:
1393 return a->v.val_loc_list == b->v.val_loc_list;
1394 case dw_val_class_die_ref:
1395 return a->v.val_die_ref.die == b->v.val_die_ref.die;
1396 case dw_val_class_fde_ref:
1397 return a->v.val_fde_index == b->v.val_fde_index;
1398 case dw_val_class_lbl_id:
1399 case dw_val_class_lineptr:
1400 case dw_val_class_macptr:
1401 case dw_val_class_loclistsptr:
1402 case dw_val_class_high_pc:
1403 return strcmp (a->v.val_lbl_id, b->v.val_lbl_id) == 0;
1404 case dw_val_class_str:
1405 return a->v.val_str == b->v.val_str;
1406 case dw_val_class_flag:
1407 return a->v.val_flag == b->v.val_flag;
1408 case dw_val_class_file:
1409 case dw_val_class_file_implicit:
1410 return a->v.val_file == b->v.val_file;
1411 case dw_val_class_decl_ref:
1412 return a->v.val_decl_ref == b->v.val_decl_ref;
1414 case dw_val_class_const_double:
1415 return (a->v.val_double.high == b->v.val_double.high
1416 && a->v.val_double.low == b->v.val_double.low);
1418 case dw_val_class_wide_int:
1419 return *a->v.val_wide == *b->v.val_wide;
1421 case dw_val_class_vec:
1423 size_t a_len = a->v.val_vec.elt_size * a->v.val_vec.length;
1424 size_t b_len = b->v.val_vec.elt_size * b->v.val_vec.length;
1426 return (a_len == b_len
1427 && !memcmp (a->v.val_vec.array, b->v.val_vec.array, a_len));
1430 case dw_val_class_data8:
1431 return memcmp (a->v.val_data8, b->v.val_data8, 8) == 0;
1433 case dw_val_class_vms_delta:
1434 return (!strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1)
1435 && !strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1));
1437 case dw_val_class_discr_value:
1438 return (a->v.val_discr_value.pos == b->v.val_discr_value.pos
1439 && a->v.val_discr_value.v.uval == b->v.val_discr_value.v.uval);
1440 case dw_val_class_discr_list:
1441 /* It makes no sense comparing two discriminant value lists. */
1442 return false;
1444 gcc_unreachable ();
1447 /* Compare two location atoms for exact equality. */
1449 static bool
1450 loc_descr_equal_p_1 (dw_loc_descr_ref a, dw_loc_descr_ref b)
1452 if (a->dw_loc_opc != b->dw_loc_opc)
1453 return false;
1455 /* ??? This is only ever set for DW_OP_constNu, for N equal to the
1456 address size, but since we always allocate cleared storage it
1457 should be zero for other types of locations. */
1458 if (a->dtprel != b->dtprel)
1459 return false;
1461 return (dw_val_equal_p (&a->dw_loc_oprnd1, &b->dw_loc_oprnd1)
1462 && dw_val_equal_p (&a->dw_loc_oprnd2, &b->dw_loc_oprnd2));
1465 /* Compare two complete location expressions for exact equality. */
1467 bool
1468 loc_descr_equal_p (dw_loc_descr_ref a, dw_loc_descr_ref b)
1470 while (1)
1472 if (a == b)
1473 return true;
1474 if (a == NULL || b == NULL)
1475 return false;
1476 if (!loc_descr_equal_p_1 (a, b))
1477 return false;
1479 a = a->dw_loc_next;
1480 b = b->dw_loc_next;
1485 /* Add a constant OFFSET to a location expression. */
1487 static void
1488 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
1490 dw_loc_descr_ref loc;
1491 HOST_WIDE_INT *p;
1493 gcc_assert (*list_head != NULL);
1495 if (!offset)
1496 return;
1498 /* Find the end of the chain. */
1499 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
1502 p = NULL;
1503 if (loc->dw_loc_opc == DW_OP_fbreg
1504 || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
1505 p = &loc->dw_loc_oprnd1.v.val_int;
1506 else if (loc->dw_loc_opc == DW_OP_bregx)
1507 p = &loc->dw_loc_oprnd2.v.val_int;
1509 /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
1510 offset. Don't optimize if an signed integer overflow would happen. */
1511 if (p != NULL
1512 && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
1513 || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
1514 *p += offset;
1516 else if (offset > 0)
1517 loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
1519 else
1521 loc->dw_loc_next
1522 = uint_loc_descriptor (-(unsigned HOST_WIDE_INT) offset);
1523 add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
1527 /* Add a constant OFFSET to a location list. */
1529 static void
1530 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
1532 dw_loc_list_ref d;
1533 for (d = list_head; d != NULL; d = d->dw_loc_next)
1534 loc_descr_plus_const (&d->expr, offset);
1537 #define DWARF_REF_SIZE \
1538 (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
1540 /* The number of bits that can be encoded by largest DW_FORM_dataN.
1541 In DWARF4 and earlier it is DW_FORM_data8 with 64 bits, in DWARF5
1542 DW_FORM_data16 with 128 bits. */
1543 #define DWARF_LARGEST_DATA_FORM_BITS \
1544 (dwarf_version >= 5 ? 128 : 64)
1546 /* Utility inline function for construction of ops that were GNU extension
1547 before DWARF 5. */
1548 static inline enum dwarf_location_atom
1549 dwarf_OP (enum dwarf_location_atom op)
1551 switch (op)
1553 case DW_OP_implicit_pointer:
1554 if (dwarf_version < 5)
1555 return DW_OP_GNU_implicit_pointer;
1556 break;
1558 case DW_OP_entry_value:
1559 if (dwarf_version < 5)
1560 return DW_OP_GNU_entry_value;
1561 break;
1563 case DW_OP_const_type:
1564 if (dwarf_version < 5)
1565 return DW_OP_GNU_const_type;
1566 break;
1568 case DW_OP_regval_type:
1569 if (dwarf_version < 5)
1570 return DW_OP_GNU_regval_type;
1571 break;
1573 case DW_OP_deref_type:
1574 if (dwarf_version < 5)
1575 return DW_OP_GNU_deref_type;
1576 break;
1578 case DW_OP_convert:
1579 if (dwarf_version < 5)
1580 return DW_OP_GNU_convert;
1581 break;
1583 case DW_OP_reinterpret:
1584 if (dwarf_version < 5)
1585 return DW_OP_GNU_reinterpret;
1586 break;
1588 default:
1589 break;
1591 return op;
1594 /* Similarly for attributes. */
1595 static inline enum dwarf_attribute
1596 dwarf_AT (enum dwarf_attribute at)
1598 switch (at)
1600 case DW_AT_call_return_pc:
1601 if (dwarf_version < 5)
1602 return DW_AT_low_pc;
1603 break;
1605 case DW_AT_call_tail_call:
1606 if (dwarf_version < 5)
1607 return DW_AT_GNU_tail_call;
1608 break;
1610 case DW_AT_call_origin:
1611 if (dwarf_version < 5)
1612 return DW_AT_abstract_origin;
1613 break;
1615 case DW_AT_call_target:
1616 if (dwarf_version < 5)
1617 return DW_AT_GNU_call_site_target;
1618 break;
1620 case DW_AT_call_target_clobbered:
1621 if (dwarf_version < 5)
1622 return DW_AT_GNU_call_site_target_clobbered;
1623 break;
1625 case DW_AT_call_parameter:
1626 if (dwarf_version < 5)
1627 return DW_AT_abstract_origin;
1628 break;
1630 case DW_AT_call_value:
1631 if (dwarf_version < 5)
1632 return DW_AT_GNU_call_site_value;
1633 break;
1635 case DW_AT_call_data_value:
1636 if (dwarf_version < 5)
1637 return DW_AT_GNU_call_site_data_value;
1638 break;
1640 case DW_AT_call_all_calls:
1641 if (dwarf_version < 5)
1642 return DW_AT_GNU_all_call_sites;
1643 break;
1645 case DW_AT_call_all_tail_calls:
1646 if (dwarf_version < 5)
1647 return DW_AT_GNU_all_tail_call_sites;
1648 break;
1650 case DW_AT_dwo_name:
1651 if (dwarf_version < 5)
1652 return DW_AT_GNU_dwo_name;
1653 break;
1655 default:
1656 break;
1658 return at;
1661 /* And similarly for tags. */
1662 static inline enum dwarf_tag
1663 dwarf_TAG (enum dwarf_tag tag)
1665 switch (tag)
1667 case DW_TAG_call_site:
1668 if (dwarf_version < 5)
1669 return DW_TAG_GNU_call_site;
1670 break;
1672 case DW_TAG_call_site_parameter:
1673 if (dwarf_version < 5)
1674 return DW_TAG_GNU_call_site_parameter;
1675 break;
1677 default:
1678 break;
1680 return tag;
1683 static unsigned long int get_base_type_offset (dw_die_ref);
1685 /* Return the size of a location descriptor. */
1687 static unsigned long
1688 size_of_loc_descr (dw_loc_descr_ref loc)
1690 unsigned long size = 1;
1692 switch (loc->dw_loc_opc)
1694 case DW_OP_addr:
1695 size += DWARF2_ADDR_SIZE;
1696 break;
1697 case DW_OP_GNU_addr_index:
1698 case DW_OP_GNU_const_index:
1699 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1700 size += size_of_uleb128 (loc->dw_loc_oprnd1.val_entry->index);
1701 break;
1702 case DW_OP_const1u:
1703 case DW_OP_const1s:
1704 size += 1;
1705 break;
1706 case DW_OP_const2u:
1707 case DW_OP_const2s:
1708 size += 2;
1709 break;
1710 case DW_OP_const4u:
1711 case DW_OP_const4s:
1712 size += 4;
1713 break;
1714 case DW_OP_const8u:
1715 case DW_OP_const8s:
1716 size += 8;
1717 break;
1718 case DW_OP_constu:
1719 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1720 break;
1721 case DW_OP_consts:
1722 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1723 break;
1724 case DW_OP_pick:
1725 size += 1;
1726 break;
1727 case DW_OP_plus_uconst:
1728 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1729 break;
1730 case DW_OP_skip:
1731 case DW_OP_bra:
1732 size += 2;
1733 break;
1734 case DW_OP_breg0:
1735 case DW_OP_breg1:
1736 case DW_OP_breg2:
1737 case DW_OP_breg3:
1738 case DW_OP_breg4:
1739 case DW_OP_breg5:
1740 case DW_OP_breg6:
1741 case DW_OP_breg7:
1742 case DW_OP_breg8:
1743 case DW_OP_breg9:
1744 case DW_OP_breg10:
1745 case DW_OP_breg11:
1746 case DW_OP_breg12:
1747 case DW_OP_breg13:
1748 case DW_OP_breg14:
1749 case DW_OP_breg15:
1750 case DW_OP_breg16:
1751 case DW_OP_breg17:
1752 case DW_OP_breg18:
1753 case DW_OP_breg19:
1754 case DW_OP_breg20:
1755 case DW_OP_breg21:
1756 case DW_OP_breg22:
1757 case DW_OP_breg23:
1758 case DW_OP_breg24:
1759 case DW_OP_breg25:
1760 case DW_OP_breg26:
1761 case DW_OP_breg27:
1762 case DW_OP_breg28:
1763 case DW_OP_breg29:
1764 case DW_OP_breg30:
1765 case DW_OP_breg31:
1766 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1767 break;
1768 case DW_OP_regx:
1769 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1770 break;
1771 case DW_OP_fbreg:
1772 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1773 break;
1774 case DW_OP_bregx:
1775 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1776 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1777 break;
1778 case DW_OP_piece:
1779 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1780 break;
1781 case DW_OP_bit_piece:
1782 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1783 size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
1784 break;
1785 case DW_OP_deref_size:
1786 case DW_OP_xderef_size:
1787 size += 1;
1788 break;
1789 case DW_OP_call2:
1790 size += 2;
1791 break;
1792 case DW_OP_call4:
1793 size += 4;
1794 break;
1795 case DW_OP_call_ref:
1796 case DW_OP_GNU_variable_value:
1797 size += DWARF_REF_SIZE;
1798 break;
1799 case DW_OP_implicit_value:
1800 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1801 + loc->dw_loc_oprnd1.v.val_unsigned;
1802 break;
1803 case DW_OP_implicit_pointer:
1804 case DW_OP_GNU_implicit_pointer:
1805 size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1806 break;
1807 case DW_OP_entry_value:
1808 case DW_OP_GNU_entry_value:
1810 unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
1811 size += size_of_uleb128 (op_size) + op_size;
1812 break;
1814 case DW_OP_const_type:
1815 case DW_OP_GNU_const_type:
1817 unsigned long o
1818 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1819 size += size_of_uleb128 (o) + 1;
1820 switch (loc->dw_loc_oprnd2.val_class)
1822 case dw_val_class_vec:
1823 size += loc->dw_loc_oprnd2.v.val_vec.length
1824 * loc->dw_loc_oprnd2.v.val_vec.elt_size;
1825 break;
1826 case dw_val_class_const:
1827 size += HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
1828 break;
1829 case dw_val_class_const_double:
1830 size += HOST_BITS_PER_DOUBLE_INT / BITS_PER_UNIT;
1831 break;
1832 case dw_val_class_wide_int:
1833 size += (get_full_len (*loc->dw_loc_oprnd2.v.val_wide)
1834 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
1835 break;
1836 default:
1837 gcc_unreachable ();
1839 break;
1841 case DW_OP_regval_type:
1842 case DW_OP_GNU_regval_type:
1844 unsigned long o
1845 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1846 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1847 + size_of_uleb128 (o);
1849 break;
1850 case DW_OP_deref_type:
1851 case DW_OP_GNU_deref_type:
1853 unsigned long o
1854 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1855 size += 1 + size_of_uleb128 (o);
1857 break;
1858 case DW_OP_convert:
1859 case DW_OP_reinterpret:
1860 case DW_OP_GNU_convert:
1861 case DW_OP_GNU_reinterpret:
1862 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
1863 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1864 else
1866 unsigned long o
1867 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1868 size += size_of_uleb128 (o);
1870 break;
1871 case DW_OP_GNU_parameter_ref:
1872 size += 4;
1873 break;
1874 default:
1875 break;
1878 return size;
1881 /* Return the size of a series of location descriptors. */
1883 unsigned long
1884 size_of_locs (dw_loc_descr_ref loc)
1886 dw_loc_descr_ref l;
1887 unsigned long size;
1889 /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
1890 field, to avoid writing to a PCH file. */
1891 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1893 if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
1894 break;
1895 size += size_of_loc_descr (l);
1897 if (! l)
1898 return size;
1900 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1902 l->dw_loc_addr = size;
1903 size += size_of_loc_descr (l);
1906 return size;
1909 /* Return the size of the value in a DW_AT_discr_value attribute. */
1911 static int
1912 size_of_discr_value (dw_discr_value *discr_value)
1914 if (discr_value->pos)
1915 return size_of_uleb128 (discr_value->v.uval);
1916 else
1917 return size_of_sleb128 (discr_value->v.sval);
1920 /* Return the size of the value in a DW_AT_discr_list attribute. */
1922 static int
1923 size_of_discr_list (dw_discr_list_ref discr_list)
1925 int size = 0;
1927 for (dw_discr_list_ref list = discr_list;
1928 list != NULL;
1929 list = list->dw_discr_next)
1931 /* One byte for the discriminant value descriptor, and then one or two
1932 LEB128 numbers, depending on whether it's a single case label or a
1933 range label. */
1934 size += 1;
1935 size += size_of_discr_value (&list->dw_discr_lower_bound);
1936 if (list->dw_discr_range != 0)
1937 size += size_of_discr_value (&list->dw_discr_upper_bound);
1939 return size;
1942 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
1943 static void get_ref_die_offset_label (char *, dw_die_ref);
1944 static unsigned long int get_ref_die_offset (dw_die_ref);
1946 /* Output location description stack opcode's operands (if any).
1947 The for_eh_or_skip parameter controls whether register numbers are
1948 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
1949 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
1950 info). This should be suppressed for the cases that have not been converted
1951 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
1953 static void
1954 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
1956 dw_val_ref val1 = &loc->dw_loc_oprnd1;
1957 dw_val_ref val2 = &loc->dw_loc_oprnd2;
1959 switch (loc->dw_loc_opc)
1961 #ifdef DWARF2_DEBUGGING_INFO
1962 case DW_OP_const2u:
1963 case DW_OP_const2s:
1964 dw2_asm_output_data (2, val1->v.val_int, NULL);
1965 break;
1966 case DW_OP_const4u:
1967 if (loc->dtprel)
1969 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1970 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
1971 val1->v.val_addr);
1972 fputc ('\n', asm_out_file);
1973 break;
1975 /* FALLTHRU */
1976 case DW_OP_const4s:
1977 dw2_asm_output_data (4, val1->v.val_int, NULL);
1978 break;
1979 case DW_OP_const8u:
1980 if (loc->dtprel)
1982 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1983 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
1984 val1->v.val_addr);
1985 fputc ('\n', asm_out_file);
1986 break;
1988 /* FALLTHRU */
1989 case DW_OP_const8s:
1990 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
1991 dw2_asm_output_data (8, val1->v.val_int, NULL);
1992 break;
1993 case DW_OP_skip:
1994 case DW_OP_bra:
1996 int offset;
1998 gcc_assert (val1->val_class == dw_val_class_loc);
1999 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2001 dw2_asm_output_data (2, offset, NULL);
2003 break;
2004 case DW_OP_implicit_value:
2005 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2006 switch (val2->val_class)
2008 case dw_val_class_const:
2009 dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
2010 break;
2011 case dw_val_class_vec:
2013 unsigned int elt_size = val2->v.val_vec.elt_size;
2014 unsigned int len = val2->v.val_vec.length;
2015 unsigned int i;
2016 unsigned char *p;
2018 if (elt_size > sizeof (HOST_WIDE_INT))
2020 elt_size /= 2;
2021 len *= 2;
2023 for (i = 0, p = (unsigned char *) val2->v.val_vec.array;
2024 i < len;
2025 i++, p += elt_size)
2026 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2027 "fp or vector constant word %u", i);
2029 break;
2030 case dw_val_class_const_double:
2032 unsigned HOST_WIDE_INT first, second;
2034 if (WORDS_BIG_ENDIAN)
2036 first = val2->v.val_double.high;
2037 second = val2->v.val_double.low;
2039 else
2041 first = val2->v.val_double.low;
2042 second = val2->v.val_double.high;
2044 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2045 first, NULL);
2046 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2047 second, NULL);
2049 break;
2050 case dw_val_class_wide_int:
2052 int i;
2053 int len = get_full_len (*val2->v.val_wide);
2054 if (WORDS_BIG_ENDIAN)
2055 for (i = len - 1; i >= 0; --i)
2056 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2057 val2->v.val_wide->elt (i), NULL);
2058 else
2059 for (i = 0; i < len; ++i)
2060 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2061 val2->v.val_wide->elt (i), NULL);
2063 break;
2064 case dw_val_class_addr:
2065 gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
2066 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
2067 break;
2068 default:
2069 gcc_unreachable ();
2071 break;
2072 #else
2073 case DW_OP_const2u:
2074 case DW_OP_const2s:
2075 case DW_OP_const4u:
2076 case DW_OP_const4s:
2077 case DW_OP_const8u:
2078 case DW_OP_const8s:
2079 case DW_OP_skip:
2080 case DW_OP_bra:
2081 case DW_OP_implicit_value:
2082 /* We currently don't make any attempt to make sure these are
2083 aligned properly like we do for the main unwind info, so
2084 don't support emitting things larger than a byte if we're
2085 only doing unwinding. */
2086 gcc_unreachable ();
2087 #endif
2088 case DW_OP_const1u:
2089 case DW_OP_const1s:
2090 dw2_asm_output_data (1, val1->v.val_int, NULL);
2091 break;
2092 case DW_OP_constu:
2093 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2094 break;
2095 case DW_OP_consts:
2096 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2097 break;
2098 case DW_OP_pick:
2099 dw2_asm_output_data (1, val1->v.val_int, NULL);
2100 break;
2101 case DW_OP_plus_uconst:
2102 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2103 break;
2104 case DW_OP_breg0:
2105 case DW_OP_breg1:
2106 case DW_OP_breg2:
2107 case DW_OP_breg3:
2108 case DW_OP_breg4:
2109 case DW_OP_breg5:
2110 case DW_OP_breg6:
2111 case DW_OP_breg7:
2112 case DW_OP_breg8:
2113 case DW_OP_breg9:
2114 case DW_OP_breg10:
2115 case DW_OP_breg11:
2116 case DW_OP_breg12:
2117 case DW_OP_breg13:
2118 case DW_OP_breg14:
2119 case DW_OP_breg15:
2120 case DW_OP_breg16:
2121 case DW_OP_breg17:
2122 case DW_OP_breg18:
2123 case DW_OP_breg19:
2124 case DW_OP_breg20:
2125 case DW_OP_breg21:
2126 case DW_OP_breg22:
2127 case DW_OP_breg23:
2128 case DW_OP_breg24:
2129 case DW_OP_breg25:
2130 case DW_OP_breg26:
2131 case DW_OP_breg27:
2132 case DW_OP_breg28:
2133 case DW_OP_breg29:
2134 case DW_OP_breg30:
2135 case DW_OP_breg31:
2136 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2137 break;
2138 case DW_OP_regx:
2140 unsigned r = val1->v.val_unsigned;
2141 if (for_eh_or_skip >= 0)
2142 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2143 gcc_assert (size_of_uleb128 (r)
2144 == size_of_uleb128 (val1->v.val_unsigned));
2145 dw2_asm_output_data_uleb128 (r, NULL);
2147 break;
2148 case DW_OP_fbreg:
2149 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2150 break;
2151 case DW_OP_bregx:
2153 unsigned r = val1->v.val_unsigned;
2154 if (for_eh_or_skip >= 0)
2155 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2156 gcc_assert (size_of_uleb128 (r)
2157 == size_of_uleb128 (val1->v.val_unsigned));
2158 dw2_asm_output_data_uleb128 (r, NULL);
2159 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2161 break;
2162 case DW_OP_piece:
2163 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2164 break;
2165 case DW_OP_bit_piece:
2166 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2167 dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
2168 break;
2169 case DW_OP_deref_size:
2170 case DW_OP_xderef_size:
2171 dw2_asm_output_data (1, val1->v.val_int, NULL);
2172 break;
2174 case DW_OP_addr:
2175 if (loc->dtprel)
2177 if (targetm.asm_out.output_dwarf_dtprel)
2179 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
2180 DWARF2_ADDR_SIZE,
2181 val1->v.val_addr);
2182 fputc ('\n', asm_out_file);
2184 else
2185 gcc_unreachable ();
2187 else
2189 #ifdef DWARF2_DEBUGGING_INFO
2190 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
2191 #else
2192 gcc_unreachable ();
2193 #endif
2195 break;
2197 case DW_OP_GNU_addr_index:
2198 case DW_OP_GNU_const_index:
2199 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
2200 dw2_asm_output_data_uleb128 (loc->dw_loc_oprnd1.val_entry->index,
2201 "(index into .debug_addr)");
2202 break;
2204 case DW_OP_call2:
2205 case DW_OP_call4:
2207 unsigned long die_offset
2208 = get_ref_die_offset (val1->v.val_die_ref.die);
2209 /* Make sure the offset has been computed and that we can encode it as
2210 an operand. */
2211 gcc_assert (die_offset > 0
2212 && die_offset <= (loc->dw_loc_opc == DW_OP_call2
2213 ? 0xffff
2214 : 0xffffffff));
2215 dw2_asm_output_data ((loc->dw_loc_opc == DW_OP_call2) ? 2 : 4,
2216 die_offset, NULL);
2218 break;
2220 case DW_OP_call_ref:
2221 case DW_OP_GNU_variable_value:
2223 char label[MAX_ARTIFICIAL_LABEL_BYTES
2224 + HOST_BITS_PER_WIDE_INT / 2 + 2];
2225 gcc_assert (val1->val_class == dw_val_class_die_ref);
2226 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2227 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2229 break;
2231 case DW_OP_implicit_pointer:
2232 case DW_OP_GNU_implicit_pointer:
2234 char label[MAX_ARTIFICIAL_LABEL_BYTES
2235 + HOST_BITS_PER_WIDE_INT / 2 + 2];
2236 gcc_assert (val1->val_class == dw_val_class_die_ref);
2237 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2238 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2239 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2241 break;
2243 case DW_OP_entry_value:
2244 case DW_OP_GNU_entry_value:
2245 dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
2246 output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
2247 break;
2249 case DW_OP_const_type:
2250 case DW_OP_GNU_const_type:
2252 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
2253 gcc_assert (o);
2254 dw2_asm_output_data_uleb128 (o, NULL);
2255 switch (val2->val_class)
2257 case dw_val_class_const:
2258 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2259 dw2_asm_output_data (1, l, NULL);
2260 dw2_asm_output_data (l, val2->v.val_int, NULL);
2261 break;
2262 case dw_val_class_vec:
2264 unsigned int elt_size = val2->v.val_vec.elt_size;
2265 unsigned int len = val2->v.val_vec.length;
2266 unsigned int i;
2267 unsigned char *p;
2269 l = len * elt_size;
2270 dw2_asm_output_data (1, l, NULL);
2271 if (elt_size > sizeof (HOST_WIDE_INT))
2273 elt_size /= 2;
2274 len *= 2;
2276 for (i = 0, p = (unsigned char *) val2->v.val_vec.array;
2277 i < len;
2278 i++, p += elt_size)
2279 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2280 "fp or vector constant word %u", i);
2282 break;
2283 case dw_val_class_const_double:
2285 unsigned HOST_WIDE_INT first, second;
2286 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2288 dw2_asm_output_data (1, 2 * l, NULL);
2289 if (WORDS_BIG_ENDIAN)
2291 first = val2->v.val_double.high;
2292 second = val2->v.val_double.low;
2294 else
2296 first = val2->v.val_double.low;
2297 second = val2->v.val_double.high;
2299 dw2_asm_output_data (l, first, NULL);
2300 dw2_asm_output_data (l, second, NULL);
2302 break;
2303 case dw_val_class_wide_int:
2305 int i;
2306 int len = get_full_len (*val2->v.val_wide);
2307 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2309 dw2_asm_output_data (1, len * l, NULL);
2310 if (WORDS_BIG_ENDIAN)
2311 for (i = len - 1; i >= 0; --i)
2312 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2313 else
2314 for (i = 0; i < len; ++i)
2315 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2317 break;
2318 default:
2319 gcc_unreachable ();
2322 break;
2323 case DW_OP_regval_type:
2324 case DW_OP_GNU_regval_type:
2326 unsigned r = val1->v.val_unsigned;
2327 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2328 gcc_assert (o);
2329 if (for_eh_or_skip >= 0)
2331 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2332 gcc_assert (size_of_uleb128 (r)
2333 == size_of_uleb128 (val1->v.val_unsigned));
2335 dw2_asm_output_data_uleb128 (r, NULL);
2336 dw2_asm_output_data_uleb128 (o, NULL);
2338 break;
2339 case DW_OP_deref_type:
2340 case DW_OP_GNU_deref_type:
2342 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2343 gcc_assert (o);
2344 dw2_asm_output_data (1, val1->v.val_int, NULL);
2345 dw2_asm_output_data_uleb128 (o, NULL);
2347 break;
2348 case DW_OP_convert:
2349 case DW_OP_reinterpret:
2350 case DW_OP_GNU_convert:
2351 case DW_OP_GNU_reinterpret:
2352 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2353 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2354 else
2356 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
2357 gcc_assert (o);
2358 dw2_asm_output_data_uleb128 (o, NULL);
2360 break;
2362 case DW_OP_GNU_parameter_ref:
2364 unsigned long o;
2365 gcc_assert (val1->val_class == dw_val_class_die_ref);
2366 o = get_ref_die_offset (val1->v.val_die_ref.die);
2367 dw2_asm_output_data (4, o, NULL);
2369 break;
2371 default:
2372 /* Other codes have no operands. */
2373 break;
2377 /* Output a sequence of location operations.
2378 The for_eh_or_skip parameter controls whether register numbers are
2379 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2380 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2381 info). This should be suppressed for the cases that have not been converted
2382 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
2384 void
2385 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
2387 for (; loc != NULL; loc = loc->dw_loc_next)
2389 enum dwarf_location_atom opc = loc->dw_loc_opc;
2390 /* Output the opcode. */
2391 if (for_eh_or_skip >= 0
2392 && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2394 unsigned r = (opc - DW_OP_breg0);
2395 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2396 gcc_assert (r <= 31);
2397 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2399 else if (for_eh_or_skip >= 0
2400 && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2402 unsigned r = (opc - DW_OP_reg0);
2403 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2404 gcc_assert (r <= 31);
2405 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2408 dw2_asm_output_data (1, opc,
2409 "%s", dwarf_stack_op_name (opc));
2411 /* Output the operand(s) (if any). */
2412 output_loc_operands (loc, for_eh_or_skip);
2416 /* Output location description stack opcode's operands (if any).
2417 The output is single bytes on a line, suitable for .cfi_escape. */
2419 static void
2420 output_loc_operands_raw (dw_loc_descr_ref loc)
2422 dw_val_ref val1 = &loc->dw_loc_oprnd1;
2423 dw_val_ref val2 = &loc->dw_loc_oprnd2;
2425 switch (loc->dw_loc_opc)
2427 case DW_OP_addr:
2428 case DW_OP_GNU_addr_index:
2429 case DW_OP_GNU_const_index:
2430 case DW_OP_implicit_value:
2431 /* We cannot output addresses in .cfi_escape, only bytes. */
2432 gcc_unreachable ();
2434 case DW_OP_const1u:
2435 case DW_OP_const1s:
2436 case DW_OP_pick:
2437 case DW_OP_deref_size:
2438 case DW_OP_xderef_size:
2439 fputc (',', asm_out_file);
2440 dw2_asm_output_data_raw (1, val1->v.val_int);
2441 break;
2443 case DW_OP_const2u:
2444 case DW_OP_const2s:
2445 fputc (',', asm_out_file);
2446 dw2_asm_output_data_raw (2, val1->v.val_int);
2447 break;
2449 case DW_OP_const4u:
2450 case DW_OP_const4s:
2451 fputc (',', asm_out_file);
2452 dw2_asm_output_data_raw (4, val1->v.val_int);
2453 break;
2455 case DW_OP_const8u:
2456 case DW_OP_const8s:
2457 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2458 fputc (',', asm_out_file);
2459 dw2_asm_output_data_raw (8, val1->v.val_int);
2460 break;
2462 case DW_OP_skip:
2463 case DW_OP_bra:
2465 int offset;
2467 gcc_assert (val1->val_class == dw_val_class_loc);
2468 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2470 fputc (',', asm_out_file);
2471 dw2_asm_output_data_raw (2, offset);
2473 break;
2475 case DW_OP_regx:
2477 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2478 gcc_assert (size_of_uleb128 (r)
2479 == size_of_uleb128 (val1->v.val_unsigned));
2480 fputc (',', asm_out_file);
2481 dw2_asm_output_data_uleb128_raw (r);
2483 break;
2485 case DW_OP_constu:
2486 case DW_OP_plus_uconst:
2487 case DW_OP_piece:
2488 fputc (',', asm_out_file);
2489 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2490 break;
2492 case DW_OP_bit_piece:
2493 fputc (',', asm_out_file);
2494 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2495 dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
2496 break;
2498 case DW_OP_consts:
2499 case DW_OP_breg0:
2500 case DW_OP_breg1:
2501 case DW_OP_breg2:
2502 case DW_OP_breg3:
2503 case DW_OP_breg4:
2504 case DW_OP_breg5:
2505 case DW_OP_breg6:
2506 case DW_OP_breg7:
2507 case DW_OP_breg8:
2508 case DW_OP_breg9:
2509 case DW_OP_breg10:
2510 case DW_OP_breg11:
2511 case DW_OP_breg12:
2512 case DW_OP_breg13:
2513 case DW_OP_breg14:
2514 case DW_OP_breg15:
2515 case DW_OP_breg16:
2516 case DW_OP_breg17:
2517 case DW_OP_breg18:
2518 case DW_OP_breg19:
2519 case DW_OP_breg20:
2520 case DW_OP_breg21:
2521 case DW_OP_breg22:
2522 case DW_OP_breg23:
2523 case DW_OP_breg24:
2524 case DW_OP_breg25:
2525 case DW_OP_breg26:
2526 case DW_OP_breg27:
2527 case DW_OP_breg28:
2528 case DW_OP_breg29:
2529 case DW_OP_breg30:
2530 case DW_OP_breg31:
2531 case DW_OP_fbreg:
2532 fputc (',', asm_out_file);
2533 dw2_asm_output_data_sleb128_raw (val1->v.val_int);
2534 break;
2536 case DW_OP_bregx:
2538 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2539 gcc_assert (size_of_uleb128 (r)
2540 == size_of_uleb128 (val1->v.val_unsigned));
2541 fputc (',', asm_out_file);
2542 dw2_asm_output_data_uleb128_raw (r);
2543 fputc (',', asm_out_file);
2544 dw2_asm_output_data_sleb128_raw (val2->v.val_int);
2546 break;
2548 case DW_OP_implicit_pointer:
2549 case DW_OP_entry_value:
2550 case DW_OP_const_type:
2551 case DW_OP_regval_type:
2552 case DW_OP_deref_type:
2553 case DW_OP_convert:
2554 case DW_OP_reinterpret:
2555 case DW_OP_GNU_implicit_pointer:
2556 case DW_OP_GNU_entry_value:
2557 case DW_OP_GNU_const_type:
2558 case DW_OP_GNU_regval_type:
2559 case DW_OP_GNU_deref_type:
2560 case DW_OP_GNU_convert:
2561 case DW_OP_GNU_reinterpret:
2562 case DW_OP_GNU_parameter_ref:
2563 gcc_unreachable ();
2564 break;
2566 default:
2567 /* Other codes have no operands. */
2568 break;
2572 void
2573 output_loc_sequence_raw (dw_loc_descr_ref loc)
2575 while (1)
2577 enum dwarf_location_atom opc = loc->dw_loc_opc;
2578 /* Output the opcode. */
2579 if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2581 unsigned r = (opc - DW_OP_breg0);
2582 r = DWARF2_FRAME_REG_OUT (r, 1);
2583 gcc_assert (r <= 31);
2584 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2586 else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2588 unsigned r = (opc - DW_OP_reg0);
2589 r = DWARF2_FRAME_REG_OUT (r, 1);
2590 gcc_assert (r <= 31);
2591 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2593 /* Output the opcode. */
2594 fprintf (asm_out_file, "%#x", opc);
2595 output_loc_operands_raw (loc);
2597 if (!loc->dw_loc_next)
2598 break;
2599 loc = loc->dw_loc_next;
2601 fputc (',', asm_out_file);
2605 /* This function builds a dwarf location descriptor sequence from a
2606 dw_cfa_location, adding the given OFFSET to the result of the
2607 expression. */
2609 struct dw_loc_descr_node *
2610 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
2612 struct dw_loc_descr_node *head, *tmp;
2614 offset += cfa->offset;
2616 if (cfa->indirect)
2618 head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
2619 head->dw_loc_oprnd1.val_class = dw_val_class_const;
2620 head->dw_loc_oprnd1.val_entry = NULL;
2621 tmp = new_loc_descr (DW_OP_deref, 0, 0);
2622 add_loc_descr (&head, tmp);
2623 if (offset != 0)
2625 tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
2626 add_loc_descr (&head, tmp);
2629 else
2630 head = new_reg_loc_descr (cfa->reg, offset);
2632 return head;
2635 /* This function builds a dwarf location descriptor sequence for
2636 the address at OFFSET from the CFA when stack is aligned to
2637 ALIGNMENT byte. */
2639 struct dw_loc_descr_node *
2640 build_cfa_aligned_loc (dw_cfa_location *cfa,
2641 HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
2643 struct dw_loc_descr_node *head;
2644 unsigned int dwarf_fp
2645 = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
2647 /* When CFA is defined as FP+OFFSET, emulate stack alignment. */
2648 if (cfa->reg == HARD_FRAME_POINTER_REGNUM && cfa->indirect == 0)
2650 head = new_reg_loc_descr (dwarf_fp, 0);
2651 add_loc_descr (&head, int_loc_descriptor (alignment));
2652 add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
2653 loc_descr_plus_const (&head, offset);
2655 else
2656 head = new_reg_loc_descr (dwarf_fp, offset);
2657 return head;
2660 /* And now, the support for symbolic debugging information. */
2662 /* .debug_str support. */
2664 static void dwarf2out_init (const char *);
2665 static void dwarf2out_finish (const char *);
2666 static void dwarf2out_early_finish (const char *);
2667 static void dwarf2out_assembly_start (void);
2668 static void dwarf2out_define (unsigned int, const char *);
2669 static void dwarf2out_undef (unsigned int, const char *);
2670 static void dwarf2out_start_source_file (unsigned, const char *);
2671 static void dwarf2out_end_source_file (unsigned);
2672 static void dwarf2out_function_decl (tree);
2673 static void dwarf2out_begin_block (unsigned, unsigned);
2674 static void dwarf2out_end_block (unsigned, unsigned);
2675 static bool dwarf2out_ignore_block (const_tree);
2676 static void dwarf2out_early_global_decl (tree);
2677 static void dwarf2out_late_global_decl (tree);
2678 static void dwarf2out_type_decl (tree, int);
2679 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
2680 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
2681 dw_die_ref);
2682 static void dwarf2out_abstract_function (tree);
2683 static void dwarf2out_var_location (rtx_insn *);
2684 static void dwarf2out_size_function (tree);
2685 static void dwarf2out_begin_function (tree);
2686 static void dwarf2out_end_function (unsigned int);
2687 static void dwarf2out_register_main_translation_unit (tree unit);
2688 static void dwarf2out_set_name (tree, tree);
2690 /* The debug hooks structure. */
2692 const struct gcc_debug_hooks dwarf2_debug_hooks =
2694 dwarf2out_init,
2695 dwarf2out_finish,
2696 dwarf2out_early_finish,
2697 dwarf2out_assembly_start,
2698 dwarf2out_define,
2699 dwarf2out_undef,
2700 dwarf2out_start_source_file,
2701 dwarf2out_end_source_file,
2702 dwarf2out_begin_block,
2703 dwarf2out_end_block,
2704 dwarf2out_ignore_block,
2705 dwarf2out_source_line,
2706 dwarf2out_begin_prologue,
2707 #if VMS_DEBUGGING_INFO
2708 dwarf2out_vms_end_prologue,
2709 dwarf2out_vms_begin_epilogue,
2710 #else
2711 debug_nothing_int_charstar,
2712 debug_nothing_int_charstar,
2713 #endif
2714 dwarf2out_end_epilogue,
2715 dwarf2out_begin_function,
2716 dwarf2out_end_function, /* end_function */
2717 dwarf2out_register_main_translation_unit,
2718 dwarf2out_function_decl, /* function_decl */
2719 dwarf2out_early_global_decl,
2720 dwarf2out_late_global_decl,
2721 dwarf2out_type_decl, /* type_decl */
2722 dwarf2out_imported_module_or_decl,
2723 debug_nothing_tree, /* deferred_inline_function */
2724 /* The DWARF 2 backend tries to reduce debugging bloat by not
2725 emitting the abstract description of inline functions until
2726 something tries to reference them. */
2727 dwarf2out_abstract_function, /* outlining_inline_function */
2728 debug_nothing_rtx_code_label, /* label */
2729 debug_nothing_int, /* handle_pch */
2730 dwarf2out_var_location,
2731 dwarf2out_size_function, /* size_function */
2732 dwarf2out_switch_text_section,
2733 dwarf2out_set_name,
2734 1, /* start_end_main_source_file */
2735 TYPE_SYMTAB_IS_DIE /* tree_type_symtab_field */
2738 const struct gcc_debug_hooks dwarf2_lineno_debug_hooks =
2740 dwarf2out_init,
2741 debug_nothing_charstar,
2742 debug_nothing_charstar,
2743 dwarf2out_assembly_start,
2744 debug_nothing_int_charstar,
2745 debug_nothing_int_charstar,
2746 debug_nothing_int_charstar,
2747 debug_nothing_int,
2748 debug_nothing_int_int, /* begin_block */
2749 debug_nothing_int_int, /* end_block */
2750 debug_true_const_tree, /* ignore_block */
2751 dwarf2out_source_line, /* source_line */
2752 debug_nothing_int_int_charstar, /* begin_prologue */
2753 debug_nothing_int_charstar, /* end_prologue */
2754 debug_nothing_int_charstar, /* begin_epilogue */
2755 debug_nothing_int_charstar, /* end_epilogue */
2756 debug_nothing_tree, /* begin_function */
2757 debug_nothing_int, /* end_function */
2758 debug_nothing_tree, /* register_main_translation_unit */
2759 debug_nothing_tree, /* function_decl */
2760 debug_nothing_tree, /* early_global_decl */
2761 debug_nothing_tree, /* late_global_decl */
2762 debug_nothing_tree_int, /* type_decl */
2763 debug_nothing_tree_tree_tree_bool, /* imported_module_or_decl */
2764 debug_nothing_tree, /* deferred_inline_function */
2765 debug_nothing_tree, /* outlining_inline_function */
2766 debug_nothing_rtx_code_label, /* label */
2767 debug_nothing_int, /* handle_pch */
2768 debug_nothing_rtx_insn, /* var_location */
2769 debug_nothing_tree, /* size_function */
2770 debug_nothing_void, /* switch_text_section */
2771 debug_nothing_tree_tree, /* set_name */
2772 0, /* start_end_main_source_file */
2773 TYPE_SYMTAB_IS_ADDRESS /* tree_type_symtab_field */
2776 /* NOTE: In the comments in this file, many references are made to
2777 "Debugging Information Entries". This term is abbreviated as `DIE'
2778 throughout the remainder of this file. */
2780 /* An internal representation of the DWARF output is built, and then
2781 walked to generate the DWARF debugging info. The walk of the internal
2782 representation is done after the entire program has been compiled.
2783 The types below are used to describe the internal representation. */
2785 /* Whether to put type DIEs into their own section .debug_types instead
2786 of making them part of the .debug_info section. Only supported for
2787 Dwarf V4 or higher and the user didn't disable them through
2788 -fno-debug-types-section. It is more efficient to put them in a
2789 separate comdat sections since the linker will then be able to
2790 remove duplicates. But not all tools support .debug_types sections
2791 yet. For Dwarf V5 or higher .debug_types doesn't exist any more,
2792 it is DW_UT_type unit type in .debug_info section. */
2794 #define use_debug_types (dwarf_version >= 4 && flag_debug_types_section)
2796 /* Various DIE's use offsets relative to the beginning of the
2797 .debug_info section to refer to each other. */
2799 typedef long int dw_offset;
2801 struct comdat_type_node;
2803 /* The entries in the line_info table more-or-less mirror the opcodes
2804 that are used in the real dwarf line table. Arrays of these entries
2805 are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
2806 supported. */
2808 enum dw_line_info_opcode {
2809 /* Emit DW_LNE_set_address; the operand is the label index. */
2810 LI_set_address,
2812 /* Emit a row to the matrix with the given line. This may be done
2813 via any combination of DW_LNS_copy, DW_LNS_advance_line, and
2814 special opcodes. */
2815 LI_set_line,
2817 /* Emit a DW_LNS_set_file. */
2818 LI_set_file,
2820 /* Emit a DW_LNS_set_column. */
2821 LI_set_column,
2823 /* Emit a DW_LNS_negate_stmt; the operand is ignored. */
2824 LI_negate_stmt,
2826 /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored. */
2827 LI_set_prologue_end,
2828 LI_set_epilogue_begin,
2830 /* Emit a DW_LNE_set_discriminator. */
2831 LI_set_discriminator
2834 typedef struct GTY(()) dw_line_info_struct {
2835 enum dw_line_info_opcode opcode;
2836 unsigned int val;
2837 } dw_line_info_entry;
2840 struct GTY(()) dw_line_info_table {
2841 /* The label that marks the end of this section. */
2842 const char *end_label;
2844 /* The values for the last row of the matrix, as collected in the table.
2845 These are used to minimize the changes to the next row. */
2846 unsigned int file_num;
2847 unsigned int line_num;
2848 unsigned int column_num;
2849 int discrim_num;
2850 bool is_stmt;
2851 bool in_use;
2853 vec<dw_line_info_entry, va_gc> *entries;
2857 /* Each DIE attribute has a field specifying the attribute kind,
2858 a link to the next attribute in the chain, and an attribute value.
2859 Attributes are typically linked below the DIE they modify. */
2861 typedef struct GTY(()) dw_attr_struct {
2862 enum dwarf_attribute dw_attr;
2863 dw_val_node dw_attr_val;
2865 dw_attr_node;
2868 /* The Debugging Information Entry (DIE) structure. DIEs form a tree.
2869 The children of each node form a circular list linked by
2870 die_sib. die_child points to the node *before* the "first" child node. */
2872 typedef struct GTY((chain_circular ("%h.die_sib"), for_user)) die_struct {
2873 union die_symbol_or_type_node
2875 const char * GTY ((tag ("0"))) die_symbol;
2876 comdat_type_node *GTY ((tag ("1"))) die_type_node;
2878 GTY ((desc ("%0.comdat_type_p"))) die_id;
2879 vec<dw_attr_node, va_gc> *die_attr;
2880 dw_die_ref die_parent;
2881 dw_die_ref die_child;
2882 dw_die_ref die_sib;
2883 dw_die_ref die_definition; /* ref from a specification to its definition */
2884 dw_offset die_offset;
2885 unsigned long die_abbrev;
2886 int die_mark;
2887 unsigned int decl_id;
2888 enum dwarf_tag die_tag;
2889 /* Die is used and must not be pruned as unused. */
2890 BOOL_BITFIELD die_perennial_p : 1;
2891 BOOL_BITFIELD comdat_type_p : 1; /* DIE has a type signature */
2892 /* Whether this DIE was removed from the DIE tree, for example via
2893 prune_unused_types. We don't consider those present from the
2894 DIE lookup routines. */
2895 BOOL_BITFIELD removed : 1;
2896 /* Lots of spare bits. */
2898 die_node;
2900 /* Set to TRUE while dwarf2out_early_global_decl is running. */
2901 static bool early_dwarf;
2902 static bool early_dwarf_finished;
2903 struct set_early_dwarf {
2904 bool saved;
2905 set_early_dwarf () : saved(early_dwarf)
2907 gcc_assert (! early_dwarf_finished);
2908 early_dwarf = true;
2910 ~set_early_dwarf () { early_dwarf = saved; }
2913 /* Evaluate 'expr' while 'c' is set to each child of DIE in order. */
2914 #define FOR_EACH_CHILD(die, c, expr) do { \
2915 c = die->die_child; \
2916 if (c) do { \
2917 c = c->die_sib; \
2918 expr; \
2919 } while (c != die->die_child); \
2920 } while (0)
2922 /* The pubname structure */
2924 typedef struct GTY(()) pubname_struct {
2925 dw_die_ref die;
2926 const char *name;
2928 pubname_entry;
2931 struct GTY(()) dw_ranges {
2932 const char *label;
2933 /* If this is positive, it's a block number, otherwise it's a
2934 bitwise-negated index into dw_ranges_by_label. */
2935 int num;
2936 /* Index for the range list for DW_FORM_rnglistx. */
2937 unsigned int idx : 31;
2938 /* True if this range might be possibly in a different section
2939 from previous entry. */
2940 unsigned int maybe_new_sec : 1;
2943 /* A structure to hold a macinfo entry. */
2945 typedef struct GTY(()) macinfo_struct {
2946 unsigned char code;
2947 unsigned HOST_WIDE_INT lineno;
2948 const char *info;
2950 macinfo_entry;
2953 struct GTY(()) dw_ranges_by_label {
2954 const char *begin;
2955 const char *end;
2958 /* The comdat type node structure. */
2959 struct GTY(()) comdat_type_node
2961 dw_die_ref root_die;
2962 dw_die_ref type_die;
2963 dw_die_ref skeleton_die;
2964 char signature[DWARF_TYPE_SIGNATURE_SIZE];
2965 comdat_type_node *next;
2968 /* A list of DIEs for which we can't determine ancestry (parent_die
2969 field) just yet. Later in dwarf2out_finish we will fill in the
2970 missing bits. */
2971 typedef struct GTY(()) limbo_die_struct {
2972 dw_die_ref die;
2973 /* The tree for which this DIE was created. We use this to
2974 determine ancestry later. */
2975 tree created_for;
2976 struct limbo_die_struct *next;
2978 limbo_die_node;
2980 typedef struct skeleton_chain_struct
2982 dw_die_ref old_die;
2983 dw_die_ref new_die;
2984 struct skeleton_chain_struct *parent;
2986 skeleton_chain_node;
2988 /* Define a macro which returns nonzero for a TYPE_DECL which was
2989 implicitly generated for a type.
2991 Note that, unlike the C front-end (which generates a NULL named
2992 TYPE_DECL node for each complete tagged type, each array type,
2993 and each function type node created) the C++ front-end generates
2994 a _named_ TYPE_DECL node for each tagged type node created.
2995 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
2996 generate a DW_TAG_typedef DIE for them. Likewise with the Ada
2997 front-end, but for each type, tagged or not. */
2999 #define TYPE_DECL_IS_STUB(decl) \
3000 (DECL_NAME (decl) == NULL_TREE \
3001 || (DECL_ARTIFICIAL (decl) \
3002 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
3003 /* This is necessary for stub decls that \
3004 appear in nested inline functions. */ \
3005 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
3006 && (decl_ultimate_origin (decl) \
3007 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
3009 /* Information concerning the compilation unit's programming
3010 language, and compiler version. */
3012 /* Fixed size portion of the DWARF compilation unit header. */
3013 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
3014 (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE \
3015 + (dwarf_version >= 5 ? 4 : 3))
3017 /* Fixed size portion of the DWARF comdat type unit header. */
3018 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
3019 (DWARF_COMPILE_UNIT_HEADER_SIZE \
3020 + DWARF_TYPE_SIGNATURE_SIZE + DWARF_OFFSET_SIZE)
3022 /* Fixed size portion of the DWARF skeleton compilation unit header. */
3023 #define DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE \
3024 (DWARF_COMPILE_UNIT_HEADER_SIZE + (dwarf_version >= 5 ? 8 : 0))
3026 /* Fixed size portion of public names info. */
3027 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
3029 /* Fixed size portion of the address range info. */
3030 #define DWARF_ARANGES_HEADER_SIZE \
3031 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
3032 DWARF2_ADDR_SIZE * 2) \
3033 - DWARF_INITIAL_LENGTH_SIZE)
3035 /* Size of padding portion in the address range info. It must be
3036 aligned to twice the pointer size. */
3037 #define DWARF_ARANGES_PAD_SIZE \
3038 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
3039 DWARF2_ADDR_SIZE * 2) \
3040 - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
3042 /* Use assembler line directives if available. */
3043 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
3044 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
3045 #define DWARF2_ASM_LINE_DEBUG_INFO 1
3046 #else
3047 #define DWARF2_ASM_LINE_DEBUG_INFO 0
3048 #endif
3049 #endif
3051 /* Minimum line offset in a special line info. opcode.
3052 This value was chosen to give a reasonable range of values. */
3053 #define DWARF_LINE_BASE -10
3055 /* First special line opcode - leave room for the standard opcodes. */
3056 #define DWARF_LINE_OPCODE_BASE ((int)DW_LNS_set_isa + 1)
3058 /* Range of line offsets in a special line info. opcode. */
3059 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
3061 /* Flag that indicates the initial value of the is_stmt_start flag.
3062 In the present implementation, we do not mark any lines as
3063 the beginning of a source statement, because that information
3064 is not made available by the GCC front-end. */
3065 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
3067 /* Maximum number of operations per instruction bundle. */
3068 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
3069 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
3070 #endif
3072 /* This location is used by calc_die_sizes() to keep track
3073 the offset of each DIE within the .debug_info section. */
3074 static unsigned long next_die_offset;
3076 /* Record the root of the DIE's built for the current compilation unit. */
3077 static GTY(()) dw_die_ref single_comp_unit_die;
3079 /* A list of type DIEs that have been separated into comdat sections. */
3080 static GTY(()) comdat_type_node *comdat_type_list;
3082 /* A list of CU DIEs that have been separated. */
3083 static GTY(()) limbo_die_node *cu_die_list;
3085 /* A list of DIEs with a NULL parent waiting to be relocated. */
3086 static GTY(()) limbo_die_node *limbo_die_list;
3088 /* A list of DIEs for which we may have to generate
3089 DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set. */
3090 static GTY(()) limbo_die_node *deferred_asm_name;
3092 struct dwarf_file_hasher : ggc_ptr_hash<dwarf_file_data>
3094 typedef const char *compare_type;
3096 static hashval_t hash (dwarf_file_data *);
3097 static bool equal (dwarf_file_data *, const char *);
3100 /* Filenames referenced by this compilation unit. */
3101 static GTY(()) hash_table<dwarf_file_hasher> *file_table;
3103 struct decl_die_hasher : ggc_ptr_hash<die_node>
3105 typedef tree compare_type;
3107 static hashval_t hash (die_node *);
3108 static bool equal (die_node *, tree);
3110 /* A hash table of references to DIE's that describe declarations.
3111 The key is a DECL_UID() which is a unique number identifying each decl. */
3112 static GTY (()) hash_table<decl_die_hasher> *decl_die_table;
3114 struct GTY ((for_user)) variable_value_struct {
3115 unsigned int decl_id;
3116 vec<dw_die_ref, va_gc> *dies;
3119 struct variable_value_hasher : ggc_ptr_hash<variable_value_struct>
3121 typedef tree compare_type;
3123 static hashval_t hash (variable_value_struct *);
3124 static bool equal (variable_value_struct *, tree);
3126 /* A hash table of DIEs that contain DW_OP_GNU_variable_value with
3127 dw_val_class_decl_ref class, indexed by FUNCTION_DECLs which is
3128 DECL_CONTEXT of the referenced VAR_DECLs. */
3129 static GTY (()) hash_table<variable_value_hasher> *variable_value_hash;
3131 struct block_die_hasher : ggc_ptr_hash<die_struct>
3133 static hashval_t hash (die_struct *);
3134 static bool equal (die_struct *, die_struct *);
3137 /* A hash table of references to DIE's that describe COMMON blocks.
3138 The key is DECL_UID() ^ die_parent. */
3139 static GTY (()) hash_table<block_die_hasher> *common_block_die_table;
3141 typedef struct GTY(()) die_arg_entry_struct {
3142 dw_die_ref die;
3143 tree arg;
3144 } die_arg_entry;
3147 /* Node of the variable location list. */
3148 struct GTY ((chain_next ("%h.next"))) var_loc_node {
3149 /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
3150 EXPR_LIST chain. For small bitsizes, bitsize is encoded
3151 in mode of the EXPR_LIST node and first EXPR_LIST operand
3152 is either NOTE_INSN_VAR_LOCATION for a piece with a known
3153 location or NULL for padding. For larger bitsizes,
3154 mode is 0 and first operand is a CONCAT with bitsize
3155 as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
3156 NULL as second operand. */
3157 rtx GTY (()) loc;
3158 const char * GTY (()) label;
3159 struct var_loc_node * GTY (()) next;
3162 /* Variable location list. */
3163 struct GTY ((for_user)) var_loc_list_def {
3164 struct var_loc_node * GTY (()) first;
3166 /* Pointer to the last but one or last element of the
3167 chained list. If the list is empty, both first and
3168 last are NULL, if the list contains just one node
3169 or the last node certainly is not redundant, it points
3170 to the last node, otherwise points to the last but one.
3171 Do not mark it for GC because it is marked through the chain. */
3172 struct var_loc_node * GTY ((skip ("%h"))) last;
3174 /* Pointer to the last element before section switch,
3175 if NULL, either sections weren't switched or first
3176 is after section switch. */
3177 struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
3179 /* DECL_UID of the variable decl. */
3180 unsigned int decl_id;
3182 typedef struct var_loc_list_def var_loc_list;
3184 /* Call argument location list. */
3185 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
3186 rtx GTY (()) call_arg_loc_note;
3187 const char * GTY (()) label;
3188 tree GTY (()) block;
3189 bool tail_call_p;
3190 rtx GTY (()) symbol_ref;
3191 struct call_arg_loc_node * GTY (()) next;
3195 struct decl_loc_hasher : ggc_ptr_hash<var_loc_list>
3197 typedef const_tree compare_type;
3199 static hashval_t hash (var_loc_list *);
3200 static bool equal (var_loc_list *, const_tree);
3203 /* Table of decl location linked lists. */
3204 static GTY (()) hash_table<decl_loc_hasher> *decl_loc_table;
3206 /* Head and tail of call_arg_loc chain. */
3207 static GTY (()) struct call_arg_loc_node *call_arg_locations;
3208 static struct call_arg_loc_node *call_arg_loc_last;
3210 /* Number of call sites in the current function. */
3211 static int call_site_count = -1;
3212 /* Number of tail call sites in the current function. */
3213 static int tail_call_site_count = -1;
3215 /* A cached location list. */
3216 struct GTY ((for_user)) cached_dw_loc_list_def {
3217 /* The DECL_UID of the decl that this entry describes. */
3218 unsigned int decl_id;
3220 /* The cached location list. */
3221 dw_loc_list_ref loc_list;
3223 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
3225 struct dw_loc_list_hasher : ggc_ptr_hash<cached_dw_loc_list>
3228 typedef const_tree compare_type;
3230 static hashval_t hash (cached_dw_loc_list *);
3231 static bool equal (cached_dw_loc_list *, const_tree);
3234 /* Table of cached location lists. */
3235 static GTY (()) hash_table<dw_loc_list_hasher> *cached_dw_loc_list_table;
3237 /* A vector of references to DIE's that are uniquely identified by their tag,
3238 presence/absence of children DIE's, and list of attribute/value pairs. */
3239 static GTY(()) vec<dw_die_ref, va_gc> *abbrev_die_table;
3241 /* A hash map to remember the stack usage for DWARF procedures. The value
3242 stored is the stack size difference between before the DWARF procedure
3243 invokation and after it returned. In other words, for a DWARF procedure
3244 that consumes N stack slots and that pushes M ones, this stores M - N. */
3245 static hash_map<dw_die_ref, int> *dwarf_proc_stack_usage_map;
3247 /* A global counter for generating labels for line number data. */
3248 static unsigned int line_info_label_num;
3250 /* The current table to which we should emit line number information
3251 for the current function. This will be set up at the beginning of
3252 assembly for the function. */
3253 static GTY(()) dw_line_info_table *cur_line_info_table;
3255 /* The two default tables of line number info. */
3256 static GTY(()) dw_line_info_table *text_section_line_info;
3257 static GTY(()) dw_line_info_table *cold_text_section_line_info;
3259 /* The set of all non-default tables of line number info. */
3260 static GTY(()) vec<dw_line_info_table *, va_gc> *separate_line_info;
3262 /* A flag to tell pubnames/types export if there is an info section to
3263 refer to. */
3264 static bool info_section_emitted;
3266 /* A pointer to the base of a table that contains a list of publicly
3267 accessible names. */
3268 static GTY (()) vec<pubname_entry, va_gc> *pubname_table;
3270 /* A pointer to the base of a table that contains a list of publicly
3271 accessible types. */
3272 static GTY (()) vec<pubname_entry, va_gc> *pubtype_table;
3274 /* A pointer to the base of a table that contains a list of macro
3275 defines/undefines (and file start/end markers). */
3276 static GTY (()) vec<macinfo_entry, va_gc> *macinfo_table;
3278 /* True if .debug_macinfo or .debug_macros section is going to be
3279 emitted. */
3280 #define have_macinfo \
3281 ((!XCOFF_DEBUGGING_INFO || HAVE_XCOFF_DWARF_EXTRAS) \
3282 && debug_info_level >= DINFO_LEVEL_VERBOSE \
3283 && !macinfo_table->is_empty ())
3285 /* Vector of dies for which we should generate .debug_ranges info. */
3286 static GTY (()) vec<dw_ranges, va_gc> *ranges_table;
3288 /* Vector of pairs of labels referenced in ranges_table. */
3289 static GTY (()) vec<dw_ranges_by_label, va_gc> *ranges_by_label;
3291 /* Whether we have location lists that need outputting */
3292 static GTY(()) bool have_location_lists;
3294 /* Unique label counter. */
3295 static GTY(()) unsigned int loclabel_num;
3297 /* Unique label counter for point-of-call tables. */
3298 static GTY(()) unsigned int poc_label_num;
3300 /* The last file entry emitted by maybe_emit_file(). */
3301 static GTY(()) struct dwarf_file_data * last_emitted_file;
3303 /* Number of internal labels generated by gen_internal_sym(). */
3304 static GTY(()) int label_num;
3306 static GTY(()) vec<die_arg_entry, va_gc> *tmpl_value_parm_die_table;
3308 /* Instances of generic types for which we need to generate debug
3309 info that describe their generic parameters and arguments. That
3310 generation needs to happen once all types are properly laid out so
3311 we do it at the end of compilation. */
3312 static GTY(()) vec<tree, va_gc> *generic_type_instances;
3314 /* Offset from the "steady-state frame pointer" to the frame base,
3315 within the current function. */
3316 static HOST_WIDE_INT frame_pointer_fb_offset;
3317 static bool frame_pointer_fb_offset_valid;
3319 static vec<dw_die_ref> base_types;
3321 /* Flags to represent a set of attribute classes for attributes that represent
3322 a scalar value (bounds, pointers, ...). */
3323 enum dw_scalar_form
3325 dw_scalar_form_constant = 0x01,
3326 dw_scalar_form_exprloc = 0x02,
3327 dw_scalar_form_reference = 0x04
3330 /* Forward declarations for functions defined in this file. */
3332 static int is_pseudo_reg (const_rtx);
3333 static tree type_main_variant (tree);
3334 static int is_tagged_type (const_tree);
3335 static const char *dwarf_tag_name (unsigned);
3336 static const char *dwarf_attr_name (unsigned);
3337 static const char *dwarf_form_name (unsigned);
3338 static tree decl_ultimate_origin (const_tree);
3339 static tree decl_class_context (tree);
3340 static void add_dwarf_attr (dw_die_ref, dw_attr_node *);
3341 static inline enum dw_val_class AT_class (dw_attr_node *);
3342 static inline unsigned int AT_index (dw_attr_node *);
3343 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
3344 static inline unsigned AT_flag (dw_attr_node *);
3345 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
3346 static inline HOST_WIDE_INT AT_int (dw_attr_node *);
3347 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
3348 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_node *);
3349 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
3350 HOST_WIDE_INT, unsigned HOST_WIDE_INT);
3351 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
3352 unsigned int, unsigned char *);
3353 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
3354 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
3355 static inline const char *AT_string (dw_attr_node *);
3356 static enum dwarf_form AT_string_form (dw_attr_node *);
3357 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
3358 static void add_AT_specification (dw_die_ref, dw_die_ref);
3359 static inline dw_die_ref AT_ref (dw_attr_node *);
3360 static inline int AT_ref_external (dw_attr_node *);
3361 static inline void set_AT_ref_external (dw_attr_node *, int);
3362 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
3363 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
3364 static inline dw_loc_descr_ref AT_loc (dw_attr_node *);
3365 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
3366 dw_loc_list_ref);
3367 static inline dw_loc_list_ref AT_loc_list (dw_attr_node *);
3368 static addr_table_entry *add_addr_table_entry (void *, enum ate_kind);
3369 static void remove_addr_table_entry (addr_table_entry *);
3370 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx, bool);
3371 static inline rtx AT_addr (dw_attr_node *);
3372 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
3373 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
3374 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
3375 static void add_AT_loclistsptr (dw_die_ref, enum dwarf_attribute,
3376 const char *);
3377 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
3378 unsigned HOST_WIDE_INT);
3379 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
3380 unsigned long, bool);
3381 static inline const char *AT_lbl (dw_attr_node *);
3382 static dw_attr_node *get_AT (dw_die_ref, enum dwarf_attribute);
3383 static const char *get_AT_low_pc (dw_die_ref);
3384 static const char *get_AT_hi_pc (dw_die_ref);
3385 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
3386 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
3387 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
3388 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
3389 static bool is_cxx (void);
3390 static bool is_cxx (const_tree);
3391 static bool is_fortran (void);
3392 static bool is_ada (void);
3393 static bool remove_AT (dw_die_ref, enum dwarf_attribute);
3394 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
3395 static void add_child_die (dw_die_ref, dw_die_ref);
3396 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
3397 static dw_die_ref lookup_type_die (tree);
3398 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
3399 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
3400 static void equate_type_number_to_die (tree, dw_die_ref);
3401 static dw_die_ref lookup_decl_die (tree);
3402 static var_loc_list *lookup_decl_loc (const_tree);
3403 static void equate_decl_number_to_die (tree, dw_die_ref);
3404 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
3405 static void print_spaces (FILE *);
3406 static void print_die (dw_die_ref, FILE *);
3407 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
3408 static dw_die_ref pop_compile_unit (dw_die_ref);
3409 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3410 static void attr_checksum (dw_attr_node *, struct md5_ctx *, int *);
3411 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3412 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
3413 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
3414 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
3415 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_node *,
3416 struct md5_ctx *, int *);
3417 struct checksum_attributes;
3418 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
3419 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
3420 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
3421 static void generate_type_signature (dw_die_ref, comdat_type_node *);
3422 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3423 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
3424 static int same_attr_p (dw_attr_node *, dw_attr_node *, int *);
3425 static int same_die_p (dw_die_ref, dw_die_ref, int *);
3426 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
3427 static void compute_section_prefix (dw_die_ref);
3428 static int is_type_die (dw_die_ref);
3429 static int is_comdat_die (dw_die_ref);
3430 static int is_symbol_die (dw_die_ref);
3431 static inline bool is_template_instantiation (dw_die_ref);
3432 static void assign_symbol_names (dw_die_ref);
3433 static void break_out_includes (dw_die_ref);
3434 static int is_declaration_die (dw_die_ref);
3435 static int should_move_die_to_comdat (dw_die_ref);
3436 static dw_die_ref clone_as_declaration (dw_die_ref);
3437 static dw_die_ref clone_die (dw_die_ref);
3438 static dw_die_ref clone_tree (dw_die_ref);
3439 static dw_die_ref copy_declaration_context (dw_die_ref, dw_die_ref);
3440 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
3441 static void generate_skeleton_bottom_up (skeleton_chain_node *);
3442 static dw_die_ref generate_skeleton (dw_die_ref);
3443 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
3444 dw_die_ref,
3445 dw_die_ref);
3446 static void break_out_comdat_types (dw_die_ref);
3447 static void copy_decls_for_unworthy_types (dw_die_ref);
3449 static void add_sibling_attributes (dw_die_ref);
3450 static void output_location_lists (dw_die_ref);
3451 static int constant_size (unsigned HOST_WIDE_INT);
3452 static unsigned long size_of_die (dw_die_ref);
3453 static void calc_die_sizes (dw_die_ref);
3454 static void calc_base_type_die_sizes (void);
3455 static void mark_dies (dw_die_ref);
3456 static void unmark_dies (dw_die_ref);
3457 static void unmark_all_dies (dw_die_ref);
3458 static unsigned long size_of_pubnames (vec<pubname_entry, va_gc> *);
3459 static unsigned long size_of_aranges (void);
3460 static enum dwarf_form value_format (dw_attr_node *);
3461 static void output_value_format (dw_attr_node *);
3462 static void output_abbrev_section (void);
3463 static void output_die_abbrevs (unsigned long, dw_die_ref);
3464 static void output_die_symbol (dw_die_ref);
3465 static void output_die (dw_die_ref);
3466 static void output_compilation_unit_header (enum dwarf_unit_type);
3467 static void output_comp_unit (dw_die_ref, int, const unsigned char *);
3468 static void output_comdat_type_unit (comdat_type_node *);
3469 static const char *dwarf2_name (tree, int);
3470 static void add_pubname (tree, dw_die_ref);
3471 static void add_enumerator_pubname (const char *, dw_die_ref);
3472 static void add_pubname_string (const char *, dw_die_ref);
3473 static void add_pubtype (tree, dw_die_ref);
3474 static void output_pubnames (vec<pubname_entry, va_gc> *);
3475 static void output_aranges (void);
3476 static unsigned int add_ranges (const_tree, bool = false);
3477 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3478 bool *, bool);
3479 static void output_ranges (void);
3480 static dw_line_info_table *new_line_info_table (void);
3481 static void output_line_info (bool);
3482 static void output_file_names (void);
3483 static dw_die_ref base_type_die (tree, bool);
3484 static int is_base_type (tree);
3485 static dw_die_ref subrange_type_die (tree, tree, tree, tree, dw_die_ref);
3486 static int decl_quals (const_tree);
3487 static dw_die_ref modified_type_die (tree, int, bool, dw_die_ref);
3488 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3489 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3490 static int type_is_enum (const_tree);
3491 static unsigned int dbx_reg_number (const_rtx);
3492 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3493 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3494 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3495 enum var_init_status);
3496 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3497 enum var_init_status);
3498 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
3499 enum var_init_status);
3500 static int is_based_loc (const_rtx);
3501 static bool resolve_one_addr (rtx *);
3502 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3503 enum var_init_status);
3504 static dw_loc_descr_ref loc_descriptor (rtx, machine_mode mode,
3505 enum var_init_status);
3506 struct loc_descr_context;
3507 static void add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref);
3508 static void add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list);
3509 static dw_loc_list_ref loc_list_from_tree (tree, int,
3510 struct loc_descr_context *);
3511 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int,
3512 struct loc_descr_context *);
3513 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
3514 static tree field_type (const_tree);
3515 static unsigned int simple_type_align_in_bits (const_tree);
3516 static unsigned int simple_decl_align_in_bits (const_tree);
3517 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3518 struct vlr_context;
3519 static dw_loc_descr_ref field_byte_offset (const_tree, struct vlr_context *,
3520 HOST_WIDE_INT *);
3521 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3522 dw_loc_list_ref);
3523 static void add_data_member_location_attribute (dw_die_ref, tree,
3524 struct vlr_context *);
3525 static bool add_const_value_attribute (dw_die_ref, rtx);
3526 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3527 static void insert_wide_int (const wide_int &, unsigned char *, int);
3528 static void insert_float (const_rtx, unsigned char *);
3529 static rtx rtl_for_decl_location (tree);
3530 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool);
3531 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3532 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3533 static void add_name_attribute (dw_die_ref, const char *);
3534 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3535 static void add_comp_dir_attribute (dw_die_ref);
3536 static void add_scalar_info (dw_die_ref, enum dwarf_attribute, tree, int,
3537 struct loc_descr_context *);
3538 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree,
3539 struct loc_descr_context *);
3540 static void add_subscript_info (dw_die_ref, tree, bool);
3541 static void add_byte_size_attribute (dw_die_ref, tree);
3542 static void add_alignment_attribute (dw_die_ref, tree);
3543 static inline void add_bit_offset_attribute (dw_die_ref, tree,
3544 struct vlr_context *);
3545 static void add_bit_size_attribute (dw_die_ref, tree);
3546 static void add_prototyped_attribute (dw_die_ref, tree);
3547 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
3548 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3549 static void add_src_coords_attributes (dw_die_ref, tree);
3550 static void add_name_and_src_coords_attributes (dw_die_ref, tree, bool = false);
3551 static void add_discr_value (dw_die_ref, dw_discr_value *);
3552 static void add_discr_list (dw_die_ref, dw_discr_list_ref);
3553 static inline dw_discr_list_ref AT_discr_list (dw_attr_node *);
3554 static void push_decl_scope (tree);
3555 static void pop_decl_scope (void);
3556 static dw_die_ref scope_die_for (tree, dw_die_ref);
3557 static inline int local_scope_p (dw_die_ref);
3558 static inline int class_scope_p (dw_die_ref);
3559 static inline int class_or_namespace_scope_p (dw_die_ref);
3560 static void add_type_attribute (dw_die_ref, tree, int, bool, dw_die_ref);
3561 static void add_calling_convention_attribute (dw_die_ref, tree);
3562 static const char *type_tag (const_tree);
3563 static tree member_declared_type (const_tree);
3564 #if 0
3565 static const char *decl_start_label (tree);
3566 #endif
3567 static void gen_array_type_die (tree, dw_die_ref);
3568 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3569 #if 0
3570 static void gen_entry_point_die (tree, dw_die_ref);
3571 #endif
3572 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3573 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3574 static dw_die_ref gen_formal_parameter_pack_die (tree, tree, dw_die_ref, tree*);
3575 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3576 static void gen_formal_types_die (tree, dw_die_ref);
3577 static void gen_subprogram_die (tree, dw_die_ref);
3578 static void gen_variable_die (tree, tree, dw_die_ref);
3579 static void gen_const_die (tree, dw_die_ref);
3580 static void gen_label_die (tree, dw_die_ref);
3581 static void gen_lexical_block_die (tree, dw_die_ref);
3582 static void gen_inlined_subroutine_die (tree, dw_die_ref);
3583 static void gen_field_die (tree, struct vlr_context *, dw_die_ref);
3584 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3585 static dw_die_ref gen_compile_unit_die (const char *);
3586 static void gen_inheritance_die (tree, tree, tree, dw_die_ref);
3587 static void gen_member_die (tree, dw_die_ref);
3588 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3589 enum debug_info_usage);
3590 static void gen_subroutine_type_die (tree, dw_die_ref);
3591 static void gen_typedef_die (tree, dw_die_ref);
3592 static void gen_type_die (tree, dw_die_ref);
3593 static void gen_block_die (tree, dw_die_ref);
3594 static void decls_for_scope (tree, dw_die_ref);
3595 static bool is_naming_typedef_decl (const_tree);
3596 static inline dw_die_ref get_context_die (tree);
3597 static void gen_namespace_die (tree, dw_die_ref);
3598 static dw_die_ref gen_namelist_decl (tree, dw_die_ref, tree);
3599 static dw_die_ref gen_decl_die (tree, tree, struct vlr_context *, dw_die_ref);
3600 static dw_die_ref force_decl_die (tree);
3601 static dw_die_ref force_type_die (tree);
3602 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3603 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3604 static struct dwarf_file_data * lookup_filename (const char *);
3605 static void retry_incomplete_types (void);
3606 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3607 static void gen_generic_params_dies (tree);
3608 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
3609 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
3610 static void splice_child_die (dw_die_ref, dw_die_ref);
3611 static int file_info_cmp (const void *, const void *);
3612 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
3613 const char *, const char *);
3614 static void output_loc_list (dw_loc_list_ref);
3615 static char *gen_internal_sym (const char *);
3616 static bool want_pubnames (void);
3618 static void prune_unmark_dies (dw_die_ref);
3619 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3620 static void prune_unused_types_mark (dw_die_ref, int);
3621 static void prune_unused_types_walk (dw_die_ref);
3622 static void prune_unused_types_walk_attribs (dw_die_ref);
3623 static void prune_unused_types_prune (dw_die_ref);
3624 static void prune_unused_types (void);
3625 static int maybe_emit_file (struct dwarf_file_data *fd);
3626 static inline const char *AT_vms_delta1 (dw_attr_node *);
3627 static inline const char *AT_vms_delta2 (dw_attr_node *);
3628 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
3629 const char *, const char *);
3630 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
3631 static void gen_remaining_tmpl_value_param_die_attribute (void);
3632 static bool generic_type_p (tree);
3633 static void schedule_generic_params_dies_gen (tree t);
3634 static void gen_scheduled_generic_parms_dies (void);
3635 static void resolve_variable_values (void);
3637 static const char *comp_dir_string (void);
3639 static void hash_loc_operands (dw_loc_descr_ref, inchash::hash &);
3641 /* enum for tracking thread-local variables whose address is really an offset
3642 relative to the TLS pointer, which will need link-time relocation, but will
3643 not need relocation by the DWARF consumer. */
3645 enum dtprel_bool
3647 dtprel_false = 0,
3648 dtprel_true = 1
3651 /* Return the operator to use for an address of a variable. For dtprel_true, we
3652 use DW_OP_const*. For regular variables, which need both link-time
3653 relocation and consumer-level relocation (e.g., to account for shared objects
3654 loaded at a random address), we use DW_OP_addr*. */
3656 static inline enum dwarf_location_atom
3657 dw_addr_op (enum dtprel_bool dtprel)
3659 if (dtprel == dtprel_true)
3660 return (dwarf_split_debug_info ? DW_OP_GNU_const_index
3661 : (DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u));
3662 else
3663 return dwarf_split_debug_info ? DW_OP_GNU_addr_index : DW_OP_addr;
3666 /* Return a pointer to a newly allocated address location description. If
3667 dwarf_split_debug_info is true, then record the address with the appropriate
3668 relocation. */
3669 static inline dw_loc_descr_ref
3670 new_addr_loc_descr (rtx addr, enum dtprel_bool dtprel)
3672 dw_loc_descr_ref ref = new_loc_descr (dw_addr_op (dtprel), 0, 0);
3674 ref->dw_loc_oprnd1.val_class = dw_val_class_addr;
3675 ref->dw_loc_oprnd1.v.val_addr = addr;
3676 ref->dtprel = dtprel;
3677 if (dwarf_split_debug_info)
3678 ref->dw_loc_oprnd1.val_entry
3679 = add_addr_table_entry (addr,
3680 dtprel ? ate_kind_rtx_dtprel : ate_kind_rtx);
3681 else
3682 ref->dw_loc_oprnd1.val_entry = NULL;
3684 return ref;
3687 /* Section names used to hold DWARF debugging information. */
3689 #ifndef DEBUG_INFO_SECTION
3690 #define DEBUG_INFO_SECTION ".debug_info"
3691 #endif
3692 #ifndef DEBUG_DWO_INFO_SECTION
3693 #define DEBUG_DWO_INFO_SECTION ".debug_info.dwo"
3694 #endif
3695 #ifndef DEBUG_ABBREV_SECTION
3696 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
3697 #endif
3698 #ifndef DEBUG_DWO_ABBREV_SECTION
3699 #define DEBUG_DWO_ABBREV_SECTION ".debug_abbrev.dwo"
3700 #endif
3701 #ifndef DEBUG_ARANGES_SECTION
3702 #define DEBUG_ARANGES_SECTION ".debug_aranges"
3703 #endif
3704 #ifndef DEBUG_ADDR_SECTION
3705 #define DEBUG_ADDR_SECTION ".debug_addr"
3706 #endif
3707 #ifndef DEBUG_MACINFO_SECTION
3708 #define DEBUG_MACINFO_SECTION ".debug_macinfo"
3709 #endif
3710 #ifndef DEBUG_DWO_MACINFO_SECTION
3711 #define DEBUG_DWO_MACINFO_SECTION ".debug_macinfo.dwo"
3712 #endif
3713 #ifndef DEBUG_DWO_MACRO_SECTION
3714 #define DEBUG_DWO_MACRO_SECTION ".debug_macro.dwo"
3715 #endif
3716 #ifndef DEBUG_MACRO_SECTION
3717 #define DEBUG_MACRO_SECTION ".debug_macro"
3718 #endif
3719 #ifndef DEBUG_LINE_SECTION
3720 #define DEBUG_LINE_SECTION ".debug_line"
3721 #endif
3722 #ifndef DEBUG_DWO_LINE_SECTION
3723 #define DEBUG_DWO_LINE_SECTION ".debug_line.dwo"
3724 #endif
3725 #ifndef DEBUG_LOC_SECTION
3726 #define DEBUG_LOC_SECTION ".debug_loc"
3727 #endif
3728 #ifndef DEBUG_DWO_LOC_SECTION
3729 #define DEBUG_DWO_LOC_SECTION ".debug_loc.dwo"
3730 #endif
3731 #ifndef DEBUG_LOCLISTS_SECTION
3732 #define DEBUG_LOCLISTS_SECTION ".debug_loclists"
3733 #endif
3734 #ifndef DEBUG_DWO_LOCLISTS_SECTION
3735 #define DEBUG_DWO_LOCLISTS_SECTION ".debug_loclists.dwo"
3736 #endif
3737 #ifndef DEBUG_PUBNAMES_SECTION
3738 #define DEBUG_PUBNAMES_SECTION \
3739 ((debug_generate_pub_sections == 2) \
3740 ? ".debug_gnu_pubnames" : ".debug_pubnames")
3741 #endif
3742 #ifndef DEBUG_PUBTYPES_SECTION
3743 #define DEBUG_PUBTYPES_SECTION \
3744 ((debug_generate_pub_sections == 2) \
3745 ? ".debug_gnu_pubtypes" : ".debug_pubtypes")
3746 #endif
3747 #ifndef DEBUG_STR_OFFSETS_SECTION
3748 #define DEBUG_STR_OFFSETS_SECTION ".debug_str_offsets"
3749 #endif
3750 #ifndef DEBUG_DWO_STR_OFFSETS_SECTION
3751 #define DEBUG_DWO_STR_OFFSETS_SECTION ".debug_str_offsets.dwo"
3752 #endif
3753 #ifndef DEBUG_STR_DWO_SECTION
3754 #define DEBUG_STR_DWO_SECTION ".debug_str.dwo"
3755 #endif
3756 #ifndef DEBUG_STR_SECTION
3757 #define DEBUG_STR_SECTION ".debug_str"
3758 #endif
3759 #ifndef DEBUG_RANGES_SECTION
3760 #define DEBUG_RANGES_SECTION ".debug_ranges"
3761 #endif
3762 #ifndef DEBUG_RNGLISTS_SECTION
3763 #define DEBUG_RNGLISTS_SECTION ".debug_rnglists"
3764 #endif
3765 #ifndef DEBUG_LINE_STR_SECTION
3766 #define DEBUG_LINE_STR_SECTION ".debug_line_str"
3767 #endif
3769 /* Standard ELF section names for compiled code and data. */
3770 #ifndef TEXT_SECTION_NAME
3771 #define TEXT_SECTION_NAME ".text"
3772 #endif
3774 /* Section flags for .debug_str section. */
3775 #define DEBUG_STR_SECTION_FLAGS \
3776 (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings \
3777 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
3778 : SECTION_DEBUG)
3780 /* Section flags for .debug_str.dwo section. */
3781 #define DEBUG_STR_DWO_SECTION_FLAGS (SECTION_DEBUG | SECTION_EXCLUDE)
3783 /* Labels we insert at beginning sections we can reference instead of
3784 the section names themselves. */
3786 #ifndef TEXT_SECTION_LABEL
3787 #define TEXT_SECTION_LABEL "Ltext"
3788 #endif
3789 #ifndef COLD_TEXT_SECTION_LABEL
3790 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
3791 #endif
3792 #ifndef DEBUG_LINE_SECTION_LABEL
3793 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
3794 #endif
3795 #ifndef DEBUG_SKELETON_LINE_SECTION_LABEL
3796 #define DEBUG_SKELETON_LINE_SECTION_LABEL "Lskeleton_debug_line"
3797 #endif
3798 #ifndef DEBUG_INFO_SECTION_LABEL
3799 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
3800 #endif
3801 #ifndef DEBUG_SKELETON_INFO_SECTION_LABEL
3802 #define DEBUG_SKELETON_INFO_SECTION_LABEL "Lskeleton_debug_info"
3803 #endif
3804 #ifndef DEBUG_ABBREV_SECTION_LABEL
3805 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
3806 #endif
3807 #ifndef DEBUG_SKELETON_ABBREV_SECTION_LABEL
3808 #define DEBUG_SKELETON_ABBREV_SECTION_LABEL "Lskeleton_debug_abbrev"
3809 #endif
3810 #ifndef DEBUG_ADDR_SECTION_LABEL
3811 #define DEBUG_ADDR_SECTION_LABEL "Ldebug_addr"
3812 #endif
3813 #ifndef DEBUG_LOC_SECTION_LABEL
3814 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
3815 #endif
3816 #ifndef DEBUG_RANGES_SECTION_LABEL
3817 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
3818 #endif
3819 #ifndef DEBUG_MACINFO_SECTION_LABEL
3820 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
3821 #endif
3822 #ifndef DEBUG_MACRO_SECTION_LABEL
3823 #define DEBUG_MACRO_SECTION_LABEL "Ldebug_macro"
3824 #endif
3825 #define SKELETON_COMP_DIE_ABBREV 1
3826 #define SKELETON_TYPE_DIE_ABBREV 2
3828 /* Definitions of defaults for formats and names of various special
3829 (artificial) labels which may be generated within this file (when the -g
3830 options is used and DWARF2_DEBUGGING_INFO is in effect.
3831 If necessary, these may be overridden from within the tm.h file, but
3832 typically, overriding these defaults is unnecessary. */
3834 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3835 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3836 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3837 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3838 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3839 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3840 static char debug_skeleton_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3841 static char debug_skeleton_abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3842 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3843 static char debug_addr_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3844 static char debug_skeleton_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3845 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3846 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3847 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3848 static char ranges_base_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3850 #ifndef TEXT_END_LABEL
3851 #define TEXT_END_LABEL "Letext"
3852 #endif
3853 #ifndef COLD_END_LABEL
3854 #define COLD_END_LABEL "Letext_cold"
3855 #endif
3856 #ifndef BLOCK_BEGIN_LABEL
3857 #define BLOCK_BEGIN_LABEL "LBB"
3858 #endif
3859 #ifndef BLOCK_END_LABEL
3860 #define BLOCK_END_LABEL "LBE"
3861 #endif
3862 #ifndef LINE_CODE_LABEL
3863 #define LINE_CODE_LABEL "LM"
3864 #endif
3867 /* Return the root of the DIE's built for the current compilation unit. */
3868 static dw_die_ref
3869 comp_unit_die (void)
3871 if (!single_comp_unit_die)
3872 single_comp_unit_die = gen_compile_unit_die (NULL);
3873 return single_comp_unit_die;
3876 /* We allow a language front-end to designate a function that is to be
3877 called to "demangle" any name before it is put into a DIE. */
3879 static const char *(*demangle_name_func) (const char *);
3881 void
3882 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
3884 demangle_name_func = func;
3887 /* Test if rtl node points to a pseudo register. */
3889 static inline int
3890 is_pseudo_reg (const_rtx rtl)
3892 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
3893 || (GET_CODE (rtl) == SUBREG
3894 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
3897 /* Return a reference to a type, with its const and volatile qualifiers
3898 removed. */
3900 static inline tree
3901 type_main_variant (tree type)
3903 type = TYPE_MAIN_VARIANT (type);
3905 /* ??? There really should be only one main variant among any group of
3906 variants of a given type (and all of the MAIN_VARIANT values for all
3907 members of the group should point to that one type) but sometimes the C
3908 front-end messes this up for array types, so we work around that bug
3909 here. */
3910 if (TREE_CODE (type) == ARRAY_TYPE)
3911 while (type != TYPE_MAIN_VARIANT (type))
3912 type = TYPE_MAIN_VARIANT (type);
3914 return type;
3917 /* Return nonzero if the given type node represents a tagged type. */
3919 static inline int
3920 is_tagged_type (const_tree type)
3922 enum tree_code code = TREE_CODE (type);
3924 return (code == RECORD_TYPE || code == UNION_TYPE
3925 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
3928 /* Set label to debug_info_section_label + die_offset of a DIE reference. */
3930 static void
3931 get_ref_die_offset_label (char *label, dw_die_ref ref)
3933 sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
3936 /* Return die_offset of a DIE reference to a base type. */
3938 static unsigned long int
3939 get_base_type_offset (dw_die_ref ref)
3941 if (ref->die_offset)
3942 return ref->die_offset;
3943 if (comp_unit_die ()->die_abbrev)
3945 calc_base_type_die_sizes ();
3946 gcc_assert (ref->die_offset);
3948 return ref->die_offset;
3951 /* Return die_offset of a DIE reference other than base type. */
3953 static unsigned long int
3954 get_ref_die_offset (dw_die_ref ref)
3956 gcc_assert (ref->die_offset);
3957 return ref->die_offset;
3960 /* Convert a DIE tag into its string name. */
3962 static const char *
3963 dwarf_tag_name (unsigned int tag)
3965 const char *name = get_DW_TAG_name (tag);
3967 if (name != NULL)
3968 return name;
3970 return "DW_TAG_<unknown>";
3973 /* Convert a DWARF attribute code into its string name. */
3975 static const char *
3976 dwarf_attr_name (unsigned int attr)
3978 const char *name;
3980 switch (attr)
3982 #if VMS_DEBUGGING_INFO
3983 case DW_AT_HP_prologue:
3984 return "DW_AT_HP_prologue";
3985 #else
3986 case DW_AT_MIPS_loop_unroll_factor:
3987 return "DW_AT_MIPS_loop_unroll_factor";
3988 #endif
3990 #if VMS_DEBUGGING_INFO
3991 case DW_AT_HP_epilogue:
3992 return "DW_AT_HP_epilogue";
3993 #else
3994 case DW_AT_MIPS_stride:
3995 return "DW_AT_MIPS_stride";
3996 #endif
3999 name = get_DW_AT_name (attr);
4001 if (name != NULL)
4002 return name;
4004 return "DW_AT_<unknown>";
4007 /* Convert a DWARF value form code into its string name. */
4009 static const char *
4010 dwarf_form_name (unsigned int form)
4012 const char *name = get_DW_FORM_name (form);
4014 if (name != NULL)
4015 return name;
4017 return "DW_FORM_<unknown>";
4020 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
4021 instance of an inlined instance of a decl which is local to an inline
4022 function, so we have to trace all of the way back through the origin chain
4023 to find out what sort of node actually served as the original seed for the
4024 given block. */
4026 static tree
4027 decl_ultimate_origin (const_tree decl)
4029 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
4030 return NULL_TREE;
4032 /* DECL_ABSTRACT_ORIGIN can point to itself; ignore that if
4033 we're trying to output the abstract instance of this function. */
4034 if (DECL_ABSTRACT_P (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
4035 return NULL_TREE;
4037 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
4038 most distant ancestor, this should never happen. */
4039 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
4041 return DECL_ABSTRACT_ORIGIN (decl);
4044 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
4045 of a virtual function may refer to a base class, so we check the 'this'
4046 parameter. */
4048 static tree
4049 decl_class_context (tree decl)
4051 tree context = NULL_TREE;
4053 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
4054 context = DECL_CONTEXT (decl);
4055 else
4056 context = TYPE_MAIN_VARIANT
4057 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
4059 if (context && !TYPE_P (context))
4060 context = NULL_TREE;
4062 return context;
4065 /* Add an attribute/value pair to a DIE. */
4067 static inline void
4068 add_dwarf_attr (dw_die_ref die, dw_attr_node *attr)
4070 /* Maybe this should be an assert? */
4071 if (die == NULL)
4072 return;
4074 vec_safe_reserve (die->die_attr, 1);
4075 vec_safe_push (die->die_attr, *attr);
4078 static inline enum dw_val_class
4079 AT_class (dw_attr_node *a)
4081 return a->dw_attr_val.val_class;
4084 /* Return the index for any attribute that will be referenced with a
4085 DW_FORM_GNU_addr_index or DW_FORM_GNU_str_index. String indices
4086 are stored in dw_attr_val.v.val_str for reference counting
4087 pruning. */
4089 static inline unsigned int
4090 AT_index (dw_attr_node *a)
4092 if (AT_class (a) == dw_val_class_str)
4093 return a->dw_attr_val.v.val_str->index;
4094 else if (a->dw_attr_val.val_entry != NULL)
4095 return a->dw_attr_val.val_entry->index;
4096 return NOT_INDEXED;
4099 /* Add a flag value attribute to a DIE. */
4101 static inline void
4102 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
4104 dw_attr_node attr;
4106 attr.dw_attr = attr_kind;
4107 attr.dw_attr_val.val_class = dw_val_class_flag;
4108 attr.dw_attr_val.val_entry = NULL;
4109 attr.dw_attr_val.v.val_flag = flag;
4110 add_dwarf_attr (die, &attr);
4113 static inline unsigned
4114 AT_flag (dw_attr_node *a)
4116 gcc_assert (a && AT_class (a) == dw_val_class_flag);
4117 return a->dw_attr_val.v.val_flag;
4120 /* Add a signed integer attribute value to a DIE. */
4122 static inline void
4123 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
4125 dw_attr_node attr;
4127 attr.dw_attr = attr_kind;
4128 attr.dw_attr_val.val_class = dw_val_class_const;
4129 attr.dw_attr_val.val_entry = NULL;
4130 attr.dw_attr_val.v.val_int = int_val;
4131 add_dwarf_attr (die, &attr);
4134 static inline HOST_WIDE_INT
4135 AT_int (dw_attr_node *a)
4137 gcc_assert (a && (AT_class (a) == dw_val_class_const
4138 || AT_class (a) == dw_val_class_const_implicit));
4139 return a->dw_attr_val.v.val_int;
4142 /* Add an unsigned integer attribute value to a DIE. */
4144 static inline void
4145 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
4146 unsigned HOST_WIDE_INT unsigned_val)
4148 dw_attr_node attr;
4150 attr.dw_attr = attr_kind;
4151 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
4152 attr.dw_attr_val.val_entry = NULL;
4153 attr.dw_attr_val.v.val_unsigned = unsigned_val;
4154 add_dwarf_attr (die, &attr);
4157 static inline unsigned HOST_WIDE_INT
4158 AT_unsigned (dw_attr_node *a)
4160 gcc_assert (a && (AT_class (a) == dw_val_class_unsigned_const
4161 || AT_class (a) == dw_val_class_unsigned_const_implicit));
4162 return a->dw_attr_val.v.val_unsigned;
4165 /* Add an unsigned wide integer attribute value to a DIE. */
4167 static inline void
4168 add_AT_wide (dw_die_ref die, enum dwarf_attribute attr_kind,
4169 const wide_int& w)
4171 dw_attr_node attr;
4173 attr.dw_attr = attr_kind;
4174 attr.dw_attr_val.val_class = dw_val_class_wide_int;
4175 attr.dw_attr_val.val_entry = NULL;
4176 attr.dw_attr_val.v.val_wide = ggc_alloc<wide_int> ();
4177 *attr.dw_attr_val.v.val_wide = w;
4178 add_dwarf_attr (die, &attr);
4181 /* Add an unsigned double integer attribute value to a DIE. */
4183 static inline void
4184 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
4185 HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
4187 dw_attr_node attr;
4189 attr.dw_attr = attr_kind;
4190 attr.dw_attr_val.val_class = dw_val_class_const_double;
4191 attr.dw_attr_val.val_entry = NULL;
4192 attr.dw_attr_val.v.val_double.high = high;
4193 attr.dw_attr_val.v.val_double.low = low;
4194 add_dwarf_attr (die, &attr);
4197 /* Add a floating point attribute value to a DIE and return it. */
4199 static inline void
4200 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
4201 unsigned int length, unsigned int elt_size, unsigned char *array)
4203 dw_attr_node attr;
4205 attr.dw_attr = attr_kind;
4206 attr.dw_attr_val.val_class = dw_val_class_vec;
4207 attr.dw_attr_val.val_entry = NULL;
4208 attr.dw_attr_val.v.val_vec.length = length;
4209 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
4210 attr.dw_attr_val.v.val_vec.array = array;
4211 add_dwarf_attr (die, &attr);
4214 /* Add an 8-byte data attribute value to a DIE. */
4216 static inline void
4217 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
4218 unsigned char data8[8])
4220 dw_attr_node attr;
4222 attr.dw_attr = attr_kind;
4223 attr.dw_attr_val.val_class = dw_val_class_data8;
4224 attr.dw_attr_val.val_entry = NULL;
4225 memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
4226 add_dwarf_attr (die, &attr);
4229 /* Add DW_AT_low_pc and DW_AT_high_pc to a DIE. When using
4230 dwarf_split_debug_info, address attributes in dies destined for the
4231 final executable have force_direct set to avoid using indexed
4232 references. */
4234 static inline void
4235 add_AT_low_high_pc (dw_die_ref die, const char *lbl_low, const char *lbl_high,
4236 bool force_direct)
4238 dw_attr_node attr;
4239 char * lbl_id;
4241 lbl_id = xstrdup (lbl_low);
4242 attr.dw_attr = DW_AT_low_pc;
4243 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4244 attr.dw_attr_val.v.val_lbl_id = lbl_id;
4245 if (dwarf_split_debug_info && !force_direct)
4246 attr.dw_attr_val.val_entry
4247 = add_addr_table_entry (lbl_id, ate_kind_label);
4248 else
4249 attr.dw_attr_val.val_entry = NULL;
4250 add_dwarf_attr (die, &attr);
4252 attr.dw_attr = DW_AT_high_pc;
4253 if (dwarf_version < 4)
4254 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4255 else
4256 attr.dw_attr_val.val_class = dw_val_class_high_pc;
4257 lbl_id = xstrdup (lbl_high);
4258 attr.dw_attr_val.v.val_lbl_id = lbl_id;
4259 if (attr.dw_attr_val.val_class == dw_val_class_lbl_id
4260 && dwarf_split_debug_info && !force_direct)
4261 attr.dw_attr_val.val_entry
4262 = add_addr_table_entry (lbl_id, ate_kind_label);
4263 else
4264 attr.dw_attr_val.val_entry = NULL;
4265 add_dwarf_attr (die, &attr);
4268 /* Hash and equality functions for debug_str_hash. */
4270 hashval_t
4271 indirect_string_hasher::hash (indirect_string_node *x)
4273 return htab_hash_string (x->str);
4276 bool
4277 indirect_string_hasher::equal (indirect_string_node *x1, const char *x2)
4279 return strcmp (x1->str, x2) == 0;
4282 /* Add STR to the given string hash table. */
4284 static struct indirect_string_node *
4285 find_AT_string_in_table (const char *str,
4286 hash_table<indirect_string_hasher> *table)
4288 struct indirect_string_node *node;
4290 indirect_string_node **slot
4291 = table->find_slot_with_hash (str, htab_hash_string (str), INSERT);
4292 if (*slot == NULL)
4294 node = ggc_cleared_alloc<indirect_string_node> ();
4295 node->str = ggc_strdup (str);
4296 *slot = node;
4298 else
4299 node = *slot;
4301 node->refcount++;
4302 return node;
4305 /* Add STR to the indirect string hash table. */
4307 static struct indirect_string_node *
4308 find_AT_string (const char *str)
4310 if (! debug_str_hash)
4311 debug_str_hash = hash_table<indirect_string_hasher>::create_ggc (10);
4313 return find_AT_string_in_table (str, debug_str_hash);
4316 /* Add a string attribute value to a DIE. */
4318 static inline void
4319 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
4321 dw_attr_node attr;
4322 struct indirect_string_node *node;
4324 node = find_AT_string (str);
4326 attr.dw_attr = attr_kind;
4327 attr.dw_attr_val.val_class = dw_val_class_str;
4328 attr.dw_attr_val.val_entry = NULL;
4329 attr.dw_attr_val.v.val_str = node;
4330 add_dwarf_attr (die, &attr);
4333 static inline const char *
4334 AT_string (dw_attr_node *a)
4336 gcc_assert (a && AT_class (a) == dw_val_class_str);
4337 return a->dw_attr_val.v.val_str->str;
4340 /* Call this function directly to bypass AT_string_form's logic to put
4341 the string inline in the die. */
4343 static void
4344 set_indirect_string (struct indirect_string_node *node)
4346 char label[MAX_ARTIFICIAL_LABEL_BYTES];
4347 /* Already indirect is a no op. */
4348 if (node->form == DW_FORM_strp
4349 || node->form == DW_FORM_line_strp
4350 || node->form == DW_FORM_GNU_str_index)
4352 gcc_assert (node->label);
4353 return;
4355 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
4356 ++dw2_string_counter;
4357 node->label = xstrdup (label);
4359 if (!dwarf_split_debug_info)
4361 node->form = DW_FORM_strp;
4362 node->index = NOT_INDEXED;
4364 else
4366 node->form = DW_FORM_GNU_str_index;
4367 node->index = NO_INDEX_ASSIGNED;
4371 /* Find out whether a string should be output inline in DIE
4372 or out-of-line in .debug_str section. */
4374 static enum dwarf_form
4375 find_string_form (struct indirect_string_node *node)
4377 unsigned int len;
4379 if (node->form)
4380 return node->form;
4382 len = strlen (node->str) + 1;
4384 /* If the string is shorter or equal to the size of the reference, it is
4385 always better to put it inline. */
4386 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
4387 return node->form = DW_FORM_string;
4389 /* If we cannot expect the linker to merge strings in .debug_str
4390 section, only put it into .debug_str if it is worth even in this
4391 single module. */
4392 if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
4393 || ((debug_str_section->common.flags & SECTION_MERGE) == 0
4394 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
4395 return node->form = DW_FORM_string;
4397 set_indirect_string (node);
4399 return node->form;
4402 /* Find out whether the string referenced from the attribute should be
4403 output inline in DIE or out-of-line in .debug_str section. */
4405 static enum dwarf_form
4406 AT_string_form (dw_attr_node *a)
4408 gcc_assert (a && AT_class (a) == dw_val_class_str);
4409 return find_string_form (a->dw_attr_val.v.val_str);
4412 /* Add a DIE reference attribute value to a DIE. */
4414 static inline void
4415 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
4417 dw_attr_node attr;
4418 gcc_checking_assert (targ_die != NULL);
4420 /* With LTO we can end up trying to reference something we didn't create
4421 a DIE for. Avoid crashing later on a NULL referenced DIE. */
4422 if (targ_die == NULL)
4423 return;
4425 attr.dw_attr = attr_kind;
4426 attr.dw_attr_val.val_class = dw_val_class_die_ref;
4427 attr.dw_attr_val.val_entry = NULL;
4428 attr.dw_attr_val.v.val_die_ref.die = targ_die;
4429 attr.dw_attr_val.v.val_die_ref.external = 0;
4430 add_dwarf_attr (die, &attr);
4433 /* Change DIE reference REF to point to NEW_DIE instead. */
4435 static inline void
4436 change_AT_die_ref (dw_attr_node *ref, dw_die_ref new_die)
4438 gcc_assert (ref->dw_attr_val.val_class == dw_val_class_die_ref);
4439 ref->dw_attr_val.v.val_die_ref.die = new_die;
4440 ref->dw_attr_val.v.val_die_ref.external = 0;
4443 /* Add an AT_specification attribute to a DIE, and also make the back
4444 pointer from the specification to the definition. */
4446 static inline void
4447 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4449 add_AT_die_ref (die, DW_AT_specification, targ_die);
4450 gcc_assert (!targ_die->die_definition);
4451 targ_die->die_definition = die;
4454 static inline dw_die_ref
4455 AT_ref (dw_attr_node *a)
4457 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4458 return a->dw_attr_val.v.val_die_ref.die;
4461 static inline int
4462 AT_ref_external (dw_attr_node *a)
4464 if (a && AT_class (a) == dw_val_class_die_ref)
4465 return a->dw_attr_val.v.val_die_ref.external;
4467 return 0;
4470 static inline void
4471 set_AT_ref_external (dw_attr_node *a, int i)
4473 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4474 a->dw_attr_val.v.val_die_ref.external = i;
4477 /* Add an FDE reference attribute value to a DIE. */
4479 static inline void
4480 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
4482 dw_attr_node attr;
4484 attr.dw_attr = attr_kind;
4485 attr.dw_attr_val.val_class = dw_val_class_fde_ref;
4486 attr.dw_attr_val.val_entry = NULL;
4487 attr.dw_attr_val.v.val_fde_index = targ_fde;
4488 add_dwarf_attr (die, &attr);
4491 /* Add a location description attribute value to a DIE. */
4493 static inline void
4494 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4496 dw_attr_node attr;
4498 attr.dw_attr = attr_kind;
4499 attr.dw_attr_val.val_class = dw_val_class_loc;
4500 attr.dw_attr_val.val_entry = NULL;
4501 attr.dw_attr_val.v.val_loc = loc;
4502 add_dwarf_attr (die, &attr);
4505 static inline dw_loc_descr_ref
4506 AT_loc (dw_attr_node *a)
4508 gcc_assert (a && AT_class (a) == dw_val_class_loc);
4509 return a->dw_attr_val.v.val_loc;
4512 static inline void
4513 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4515 dw_attr_node attr;
4517 if (XCOFF_DEBUGGING_INFO && !HAVE_XCOFF_DWARF_EXTRAS)
4518 return;
4520 attr.dw_attr = attr_kind;
4521 attr.dw_attr_val.val_class = dw_val_class_loc_list;
4522 attr.dw_attr_val.val_entry = NULL;
4523 attr.dw_attr_val.v.val_loc_list = loc_list;
4524 add_dwarf_attr (die, &attr);
4525 have_location_lists = true;
4528 static inline dw_loc_list_ref
4529 AT_loc_list (dw_attr_node *a)
4531 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4532 return a->dw_attr_val.v.val_loc_list;
4535 static inline dw_loc_list_ref *
4536 AT_loc_list_ptr (dw_attr_node *a)
4538 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4539 return &a->dw_attr_val.v.val_loc_list;
4542 struct addr_hasher : ggc_ptr_hash<addr_table_entry>
4544 static hashval_t hash (addr_table_entry *);
4545 static bool equal (addr_table_entry *, addr_table_entry *);
4548 /* Table of entries into the .debug_addr section. */
4550 static GTY (()) hash_table<addr_hasher> *addr_index_table;
4552 /* Hash an address_table_entry. */
4554 hashval_t
4555 addr_hasher::hash (addr_table_entry *a)
4557 inchash::hash hstate;
4558 switch (a->kind)
4560 case ate_kind_rtx:
4561 hstate.add_int (0);
4562 break;
4563 case ate_kind_rtx_dtprel:
4564 hstate.add_int (1);
4565 break;
4566 case ate_kind_label:
4567 return htab_hash_string (a->addr.label);
4568 default:
4569 gcc_unreachable ();
4571 inchash::add_rtx (a->addr.rtl, hstate);
4572 return hstate.end ();
4575 /* Determine equality for two address_table_entries. */
4577 bool
4578 addr_hasher::equal (addr_table_entry *a1, addr_table_entry *a2)
4580 if (a1->kind != a2->kind)
4581 return 0;
4582 switch (a1->kind)
4584 case ate_kind_rtx:
4585 case ate_kind_rtx_dtprel:
4586 return rtx_equal_p (a1->addr.rtl, a2->addr.rtl);
4587 case ate_kind_label:
4588 return strcmp (a1->addr.label, a2->addr.label) == 0;
4589 default:
4590 gcc_unreachable ();
4594 /* Initialize an addr_table_entry. */
4596 void
4597 init_addr_table_entry (addr_table_entry *e, enum ate_kind kind, void *addr)
4599 e->kind = kind;
4600 switch (kind)
4602 case ate_kind_rtx:
4603 case ate_kind_rtx_dtprel:
4604 e->addr.rtl = (rtx) addr;
4605 break;
4606 case ate_kind_label:
4607 e->addr.label = (char *) addr;
4608 break;
4610 e->refcount = 0;
4611 e->index = NO_INDEX_ASSIGNED;
4614 /* Add attr to the address table entry to the table. Defer setting an
4615 index until output time. */
4617 static addr_table_entry *
4618 add_addr_table_entry (void *addr, enum ate_kind kind)
4620 addr_table_entry *node;
4621 addr_table_entry finder;
4623 gcc_assert (dwarf_split_debug_info);
4624 if (! addr_index_table)
4625 addr_index_table = hash_table<addr_hasher>::create_ggc (10);
4626 init_addr_table_entry (&finder, kind, addr);
4627 addr_table_entry **slot = addr_index_table->find_slot (&finder, INSERT);
4629 if (*slot == HTAB_EMPTY_ENTRY)
4631 node = ggc_cleared_alloc<addr_table_entry> ();
4632 init_addr_table_entry (node, kind, addr);
4633 *slot = node;
4635 else
4636 node = *slot;
4638 node->refcount++;
4639 return node;
4642 /* Remove an entry from the addr table by decrementing its refcount.
4643 Strictly, decrementing the refcount would be enough, but the
4644 assertion that the entry is actually in the table has found
4645 bugs. */
4647 static void
4648 remove_addr_table_entry (addr_table_entry *entry)
4650 gcc_assert (dwarf_split_debug_info && addr_index_table);
4651 /* After an index is assigned, the table is frozen. */
4652 gcc_assert (entry->refcount > 0 && entry->index == NO_INDEX_ASSIGNED);
4653 entry->refcount--;
4656 /* Given a location list, remove all addresses it refers to from the
4657 address_table. */
4659 static void
4660 remove_loc_list_addr_table_entries (dw_loc_descr_ref descr)
4662 for (; descr; descr = descr->dw_loc_next)
4663 if (descr->dw_loc_oprnd1.val_entry != NULL)
4665 gcc_assert (descr->dw_loc_oprnd1.val_entry->index == NO_INDEX_ASSIGNED);
4666 remove_addr_table_entry (descr->dw_loc_oprnd1.val_entry);
4670 /* A helper function for dwarf2out_finish called through
4671 htab_traverse. Assign an addr_table_entry its index. All entries
4672 must be collected into the table when this function is called,
4673 because the indexing code relies on htab_traverse to traverse nodes
4674 in the same order for each run. */
4677 index_addr_table_entry (addr_table_entry **h, unsigned int *index)
4679 addr_table_entry *node = *h;
4681 /* Don't index unreferenced nodes. */
4682 if (node->refcount == 0)
4683 return 1;
4685 gcc_assert (node->index == NO_INDEX_ASSIGNED);
4686 node->index = *index;
4687 *index += 1;
4689 return 1;
4692 /* Add an address constant attribute value to a DIE. When using
4693 dwarf_split_debug_info, address attributes in dies destined for the
4694 final executable should be direct references--setting the parameter
4695 force_direct ensures this behavior. */
4697 static inline void
4698 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr,
4699 bool force_direct)
4701 dw_attr_node attr;
4703 attr.dw_attr = attr_kind;
4704 attr.dw_attr_val.val_class = dw_val_class_addr;
4705 attr.dw_attr_val.v.val_addr = addr;
4706 if (dwarf_split_debug_info && !force_direct)
4707 attr.dw_attr_val.val_entry = add_addr_table_entry (addr, ate_kind_rtx);
4708 else
4709 attr.dw_attr_val.val_entry = NULL;
4710 add_dwarf_attr (die, &attr);
4713 /* Get the RTX from to an address DIE attribute. */
4715 static inline rtx
4716 AT_addr (dw_attr_node *a)
4718 gcc_assert (a && AT_class (a) == dw_val_class_addr);
4719 return a->dw_attr_val.v.val_addr;
4722 /* Add a file attribute value to a DIE. */
4724 static inline void
4725 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
4726 struct dwarf_file_data *fd)
4728 dw_attr_node attr;
4730 attr.dw_attr = attr_kind;
4731 attr.dw_attr_val.val_class = dw_val_class_file;
4732 attr.dw_attr_val.val_entry = NULL;
4733 attr.dw_attr_val.v.val_file = fd;
4734 add_dwarf_attr (die, &attr);
4737 /* Get the dwarf_file_data from a file DIE attribute. */
4739 static inline struct dwarf_file_data *
4740 AT_file (dw_attr_node *a)
4742 gcc_assert (a && (AT_class (a) == dw_val_class_file
4743 || AT_class (a) == dw_val_class_file_implicit));
4744 return a->dw_attr_val.v.val_file;
4747 /* Add a vms delta attribute value to a DIE. */
4749 static inline void
4750 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
4751 const char *lbl1, const char *lbl2)
4753 dw_attr_node attr;
4755 attr.dw_attr = attr_kind;
4756 attr.dw_attr_val.val_class = dw_val_class_vms_delta;
4757 attr.dw_attr_val.val_entry = NULL;
4758 attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
4759 attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
4760 add_dwarf_attr (die, &attr);
4763 /* Add a label identifier attribute value to a DIE. */
4765 static inline void
4766 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind,
4767 const char *lbl_id)
4769 dw_attr_node attr;
4771 attr.dw_attr = attr_kind;
4772 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4773 attr.dw_attr_val.val_entry = NULL;
4774 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
4775 if (dwarf_split_debug_info)
4776 attr.dw_attr_val.val_entry
4777 = add_addr_table_entry (attr.dw_attr_val.v.val_lbl_id,
4778 ate_kind_label);
4779 add_dwarf_attr (die, &attr);
4782 /* Add a section offset attribute value to a DIE, an offset into the
4783 debug_line section. */
4785 static inline void
4786 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4787 const char *label)
4789 dw_attr_node attr;
4791 attr.dw_attr = attr_kind;
4792 attr.dw_attr_val.val_class = dw_val_class_lineptr;
4793 attr.dw_attr_val.val_entry = NULL;
4794 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4795 add_dwarf_attr (die, &attr);
4798 /* Add a section offset attribute value to a DIE, an offset into the
4799 debug_loclists section. */
4801 static inline void
4802 add_AT_loclistsptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4803 const char *label)
4805 dw_attr_node attr;
4807 attr.dw_attr = attr_kind;
4808 attr.dw_attr_val.val_class = dw_val_class_loclistsptr;
4809 attr.dw_attr_val.val_entry = NULL;
4810 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4811 add_dwarf_attr (die, &attr);
4814 /* Add a section offset attribute value to a DIE, an offset into the
4815 debug_macinfo section. */
4817 static inline void
4818 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4819 const char *label)
4821 dw_attr_node attr;
4823 attr.dw_attr = attr_kind;
4824 attr.dw_attr_val.val_class = dw_val_class_macptr;
4825 attr.dw_attr_val.val_entry = NULL;
4826 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4827 add_dwarf_attr (die, &attr);
4830 /* Add an offset attribute value to a DIE. */
4832 static inline void
4833 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
4834 unsigned HOST_WIDE_INT offset)
4836 dw_attr_node attr;
4838 attr.dw_attr = attr_kind;
4839 attr.dw_attr_val.val_class = dw_val_class_offset;
4840 attr.dw_attr_val.val_entry = NULL;
4841 attr.dw_attr_val.v.val_offset = offset;
4842 add_dwarf_attr (die, &attr);
4845 /* Add a range_list attribute value to a DIE. When using
4846 dwarf_split_debug_info, address attributes in dies destined for the
4847 final executable should be direct references--setting the parameter
4848 force_direct ensures this behavior. */
4850 #define UNRELOCATED_OFFSET ((addr_table_entry *) 1)
4851 #define RELOCATED_OFFSET (NULL)
4853 static void
4854 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
4855 long unsigned int offset, bool force_direct)
4857 dw_attr_node attr;
4859 attr.dw_attr = attr_kind;
4860 attr.dw_attr_val.val_class = dw_val_class_range_list;
4861 /* For the range_list attribute, use val_entry to store whether the
4862 offset should follow split-debug-info or normal semantics. This
4863 value is read in output_range_list_offset. */
4864 if (dwarf_split_debug_info && !force_direct)
4865 attr.dw_attr_val.val_entry = UNRELOCATED_OFFSET;
4866 else
4867 attr.dw_attr_val.val_entry = RELOCATED_OFFSET;
4868 attr.dw_attr_val.v.val_offset = offset;
4869 add_dwarf_attr (die, &attr);
4872 /* Return the start label of a delta attribute. */
4874 static inline const char *
4875 AT_vms_delta1 (dw_attr_node *a)
4877 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4878 return a->dw_attr_val.v.val_vms_delta.lbl1;
4881 /* Return the end label of a delta attribute. */
4883 static inline const char *
4884 AT_vms_delta2 (dw_attr_node *a)
4886 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4887 return a->dw_attr_val.v.val_vms_delta.lbl2;
4890 static inline const char *
4891 AT_lbl (dw_attr_node *a)
4893 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
4894 || AT_class (a) == dw_val_class_lineptr
4895 || AT_class (a) == dw_val_class_macptr
4896 || AT_class (a) == dw_val_class_loclistsptr
4897 || AT_class (a) == dw_val_class_high_pc));
4898 return a->dw_attr_val.v.val_lbl_id;
4901 /* Get the attribute of type attr_kind. */
4903 static dw_attr_node *
4904 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4906 dw_attr_node *a;
4907 unsigned ix;
4908 dw_die_ref spec = NULL;
4910 if (! die)
4911 return NULL;
4913 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4914 if (a->dw_attr == attr_kind)
4915 return a;
4916 else if (a->dw_attr == DW_AT_specification
4917 || a->dw_attr == DW_AT_abstract_origin)
4918 spec = AT_ref (a);
4920 if (spec)
4921 return get_AT (spec, attr_kind);
4923 return NULL;
4926 /* Returns the parent of the declaration of DIE. */
4928 static dw_die_ref
4929 get_die_parent (dw_die_ref die)
4931 dw_die_ref t;
4933 if (!die)
4934 return NULL;
4936 if ((t = get_AT_ref (die, DW_AT_abstract_origin))
4937 || (t = get_AT_ref (die, DW_AT_specification)))
4938 die = t;
4940 return die->die_parent;
4943 /* Return the "low pc" attribute value, typically associated with a subprogram
4944 DIE. Return null if the "low pc" attribute is either not present, or if it
4945 cannot be represented as an assembler label identifier. */
4947 static inline const char *
4948 get_AT_low_pc (dw_die_ref die)
4950 dw_attr_node *a = get_AT (die, DW_AT_low_pc);
4952 return a ? AT_lbl (a) : NULL;
4955 /* Return the "high pc" attribute value, typically associated with a subprogram
4956 DIE. Return null if the "high pc" attribute is either not present, or if it
4957 cannot be represented as an assembler label identifier. */
4959 static inline const char *
4960 get_AT_hi_pc (dw_die_ref die)
4962 dw_attr_node *a = get_AT (die, DW_AT_high_pc);
4964 return a ? AT_lbl (a) : NULL;
4967 /* Return the value of the string attribute designated by ATTR_KIND, or
4968 NULL if it is not present. */
4970 static inline const char *
4971 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
4973 dw_attr_node *a = get_AT (die, attr_kind);
4975 return a ? AT_string (a) : NULL;
4978 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
4979 if it is not present. */
4981 static inline int
4982 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
4984 dw_attr_node *a = get_AT (die, attr_kind);
4986 return a ? AT_flag (a) : 0;
4989 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
4990 if it is not present. */
4992 static inline unsigned
4993 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
4995 dw_attr_node *a = get_AT (die, attr_kind);
4997 return a ? AT_unsigned (a) : 0;
5000 static inline dw_die_ref
5001 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
5003 dw_attr_node *a = get_AT (die, attr_kind);
5005 return a ? AT_ref (a) : NULL;
5008 static inline struct dwarf_file_data *
5009 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
5011 dw_attr_node *a = get_AT (die, attr_kind);
5013 return a ? AT_file (a) : NULL;
5016 /* Return TRUE if the language is C++. */
5018 static inline bool
5019 is_cxx (void)
5021 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5023 return (lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus
5024 || lang == DW_LANG_C_plus_plus_11 || lang == DW_LANG_C_plus_plus_14);
5027 /* Return TRUE if DECL was created by the C++ frontend. */
5029 static bool
5030 is_cxx (const_tree decl)
5032 if (in_lto_p)
5034 const_tree context = decl;
5035 while (context && TREE_CODE (context) != TRANSLATION_UNIT_DECL)
5037 if (TREE_CODE (context) == BLOCK)
5038 context = BLOCK_SUPERCONTEXT (context);
5039 else
5040 context = get_containing_scope (context);
5042 if (context && TRANSLATION_UNIT_LANGUAGE (context))
5043 return strncmp (TRANSLATION_UNIT_LANGUAGE (context), "GNU C++", 7) == 0;
5045 return is_cxx ();
5048 /* Return TRUE if the language is Java. */
5050 static inline bool
5051 is_java (void)
5053 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5055 return lang == DW_LANG_Java;
5058 /* Return TRUE if the language is Fortran. */
5060 static inline bool
5061 is_fortran (void)
5063 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5065 return (lang == DW_LANG_Fortran77
5066 || lang == DW_LANG_Fortran90
5067 || lang == DW_LANG_Fortran95
5068 || lang == DW_LANG_Fortran03
5069 || lang == DW_LANG_Fortran08);
5072 /* Return TRUE if the language is Ada. */
5074 static inline bool
5075 is_ada (void)
5077 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5079 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
5082 /* Remove the specified attribute if present. Return TRUE if removal
5083 was successful. */
5085 static bool
5086 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5088 dw_attr_node *a;
5089 unsigned ix;
5091 if (! die)
5092 return false;
5094 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5095 if (a->dw_attr == attr_kind)
5097 if (AT_class (a) == dw_val_class_str)
5098 if (a->dw_attr_val.v.val_str->refcount)
5099 a->dw_attr_val.v.val_str->refcount--;
5101 /* vec::ordered_remove should help reduce the number of abbrevs
5102 that are needed. */
5103 die->die_attr->ordered_remove (ix);
5104 return true;
5106 return false;
5109 /* Remove CHILD from its parent. PREV must have the property that
5110 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
5112 static void
5113 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
5115 gcc_assert (child->die_parent == prev->die_parent);
5116 gcc_assert (prev->die_sib == child);
5117 if (prev == child)
5119 gcc_assert (child->die_parent->die_child == child);
5120 prev = NULL;
5122 else
5123 prev->die_sib = child->die_sib;
5124 if (child->die_parent->die_child == child)
5125 child->die_parent->die_child = prev;
5126 child->die_sib = NULL;
5129 /* Replace OLD_CHILD with NEW_CHILD. PREV must have the property that
5130 PREV->DIE_SIB == OLD_CHILD. Does not alter OLD_CHILD. */
5132 static void
5133 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
5135 dw_die_ref parent = old_child->die_parent;
5137 gcc_assert (parent == prev->die_parent);
5138 gcc_assert (prev->die_sib == old_child);
5140 new_child->die_parent = parent;
5141 if (prev == old_child)
5143 gcc_assert (parent->die_child == old_child);
5144 new_child->die_sib = new_child;
5146 else
5148 prev->die_sib = new_child;
5149 new_child->die_sib = old_child->die_sib;
5151 if (old_child->die_parent->die_child == old_child)
5152 old_child->die_parent->die_child = new_child;
5153 old_child->die_sib = NULL;
5156 /* Move all children from OLD_PARENT to NEW_PARENT. */
5158 static void
5159 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
5161 dw_die_ref c;
5162 new_parent->die_child = old_parent->die_child;
5163 old_parent->die_child = NULL;
5164 FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
5167 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
5168 matches TAG. */
5170 static void
5171 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
5173 dw_die_ref c;
5175 c = die->die_child;
5176 if (c) do {
5177 dw_die_ref prev = c;
5178 c = c->die_sib;
5179 while (c->die_tag == tag)
5181 remove_child_with_prev (c, prev);
5182 c->die_parent = NULL;
5183 /* Might have removed every child. */
5184 if (die->die_child == NULL)
5185 return;
5186 c = prev->die_sib;
5188 } while (c != die->die_child);
5191 /* Add a CHILD_DIE as the last child of DIE. */
5193 static void
5194 add_child_die (dw_die_ref die, dw_die_ref child_die)
5196 /* FIXME this should probably be an assert. */
5197 if (! die || ! child_die)
5198 return;
5199 gcc_assert (die != child_die);
5201 child_die->die_parent = die;
5202 if (die->die_child)
5204 child_die->die_sib = die->die_child->die_sib;
5205 die->die_child->die_sib = child_die;
5207 else
5208 child_die->die_sib = child_die;
5209 die->die_child = child_die;
5212 /* Like add_child_die, but put CHILD_DIE after AFTER_DIE. */
5214 static void
5215 add_child_die_after (dw_die_ref die, dw_die_ref child_die,
5216 dw_die_ref after_die)
5218 gcc_assert (die
5219 && child_die
5220 && after_die
5221 && die->die_child
5222 && die != child_die);
5224 child_die->die_parent = die;
5225 child_die->die_sib = after_die->die_sib;
5226 after_die->die_sib = child_die;
5227 if (die->die_child == after_die)
5228 die->die_child = child_die;
5231 /* Unassociate CHILD from its parent, and make its parent be
5232 NEW_PARENT. */
5234 static void
5235 reparent_child (dw_die_ref child, dw_die_ref new_parent)
5237 for (dw_die_ref p = child->die_parent->die_child; ; p = p->die_sib)
5238 if (p->die_sib == child)
5240 remove_child_with_prev (child, p);
5241 break;
5243 add_child_die (new_parent, child);
5246 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
5247 is the specification, to the end of PARENT's list of children.
5248 This is done by removing and re-adding it. */
5250 static void
5251 splice_child_die (dw_die_ref parent, dw_die_ref child)
5253 /* We want the declaration DIE from inside the class, not the
5254 specification DIE at toplevel. */
5255 if (child->die_parent != parent)
5257 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
5259 if (tmp)
5260 child = tmp;
5263 gcc_assert (child->die_parent == parent
5264 || (child->die_parent
5265 == get_AT_ref (parent, DW_AT_specification)));
5267 reparent_child (child, parent);
5270 /* Create and return a new die with a parent of PARENT_DIE. If
5271 PARENT_DIE is NULL, the new DIE is placed in limbo and an
5272 associated tree T must be supplied to determine parenthood
5273 later. */
5275 static inline dw_die_ref
5276 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
5278 dw_die_ref die = ggc_cleared_alloc<die_node> ();
5280 die->die_tag = tag_value;
5282 if (parent_die != NULL)
5283 add_child_die (parent_die, die);
5284 else
5286 limbo_die_node *limbo_node;
5288 /* No DIEs created after early dwarf should end up in limbo,
5289 because the limbo list should not persist past LTO
5290 streaming. */
5291 if (tag_value != DW_TAG_compile_unit
5292 /* These are allowed because they're generated while
5293 breaking out COMDAT units late. */
5294 && tag_value != DW_TAG_type_unit
5295 && tag_value != DW_TAG_skeleton_unit
5296 && !early_dwarf
5297 /* Allow nested functions to live in limbo because they will
5298 only temporarily live there, as decls_for_scope will fix
5299 them up. */
5300 && (TREE_CODE (t) != FUNCTION_DECL
5301 || !decl_function_context (t))
5302 /* Same as nested functions above but for types. Types that
5303 are local to a function will be fixed in
5304 decls_for_scope. */
5305 && (!RECORD_OR_UNION_TYPE_P (t)
5306 || !TYPE_CONTEXT (t)
5307 || TREE_CODE (TYPE_CONTEXT (t)) != FUNCTION_DECL)
5308 /* FIXME debug-early: Allow late limbo DIE creation for LTO,
5309 especially in the ltrans stage, but once we implement LTO
5310 dwarf streaming, we should remove this exception. */
5311 && !in_lto_p)
5313 fprintf (stderr, "symbol ended up in limbo too late:");
5314 debug_generic_stmt (t);
5315 gcc_unreachable ();
5318 limbo_node = ggc_cleared_alloc<limbo_die_node> ();
5319 limbo_node->die = die;
5320 limbo_node->created_for = t;
5321 limbo_node->next = limbo_die_list;
5322 limbo_die_list = limbo_node;
5325 return die;
5328 /* Return the DIE associated with the given type specifier. */
5330 static inline dw_die_ref
5331 lookup_type_die (tree type)
5333 dw_die_ref die = TYPE_SYMTAB_DIE (type);
5334 if (die && die->removed)
5336 TYPE_SYMTAB_DIE (type) = NULL;
5337 return NULL;
5339 return die;
5342 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
5343 anonymous type named by the typedef TYPE_DIE, return the DIE of the
5344 anonymous type instead the one of the naming typedef. */
5346 static inline dw_die_ref
5347 strip_naming_typedef (tree type, dw_die_ref type_die)
5349 if (type
5350 && TREE_CODE (type) == RECORD_TYPE
5351 && type_die
5352 && type_die->die_tag == DW_TAG_typedef
5353 && is_naming_typedef_decl (TYPE_NAME (type)))
5354 type_die = get_AT_ref (type_die, DW_AT_type);
5355 return type_die;
5358 /* Like lookup_type_die, but if type is an anonymous type named by a
5359 typedef[1], return the DIE of the anonymous type instead the one of
5360 the naming typedef. This is because in gen_typedef_die, we did
5361 equate the anonymous struct named by the typedef with the DIE of
5362 the naming typedef. So by default, lookup_type_die on an anonymous
5363 struct yields the DIE of the naming typedef.
5365 [1]: Read the comment of is_naming_typedef_decl to learn about what
5366 a naming typedef is. */
5368 static inline dw_die_ref
5369 lookup_type_die_strip_naming_typedef (tree type)
5371 dw_die_ref die = lookup_type_die (type);
5372 return strip_naming_typedef (type, die);
5375 /* Equate a DIE to a given type specifier. */
5377 static inline void
5378 equate_type_number_to_die (tree type, dw_die_ref type_die)
5380 TYPE_SYMTAB_DIE (type) = type_die;
5383 /* Returns a hash value for X (which really is a die_struct). */
5385 inline hashval_t
5386 decl_die_hasher::hash (die_node *x)
5388 return (hashval_t) x->decl_id;
5391 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
5393 inline bool
5394 decl_die_hasher::equal (die_node *x, tree y)
5396 return (x->decl_id == DECL_UID (y));
5399 /* Return the DIE associated with a given declaration. */
5401 static inline dw_die_ref
5402 lookup_decl_die (tree decl)
5404 dw_die_ref *die = decl_die_table->find_slot_with_hash (decl, DECL_UID (decl),
5405 NO_INSERT);
5406 if (!die)
5407 return NULL;
5408 if ((*die)->removed)
5410 decl_die_table->clear_slot (die);
5411 return NULL;
5413 return *die;
5416 /* Returns a hash value for X (which really is a var_loc_list). */
5418 inline hashval_t
5419 decl_loc_hasher::hash (var_loc_list *x)
5421 return (hashval_t) x->decl_id;
5424 /* Return nonzero if decl_id of var_loc_list X is the same as
5425 UID of decl *Y. */
5427 inline bool
5428 decl_loc_hasher::equal (var_loc_list *x, const_tree y)
5430 return (x->decl_id == DECL_UID (y));
5433 /* Return the var_loc list associated with a given declaration. */
5435 static inline var_loc_list *
5436 lookup_decl_loc (const_tree decl)
5438 if (!decl_loc_table)
5439 return NULL;
5440 return decl_loc_table->find_with_hash (decl, DECL_UID (decl));
5443 /* Returns a hash value for X (which really is a cached_dw_loc_list_list). */
5445 inline hashval_t
5446 dw_loc_list_hasher::hash (cached_dw_loc_list *x)
5448 return (hashval_t) x->decl_id;
5451 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
5452 UID of decl *Y. */
5454 inline bool
5455 dw_loc_list_hasher::equal (cached_dw_loc_list *x, const_tree y)
5457 return (x->decl_id == DECL_UID (y));
5460 /* Equate a DIE to a particular declaration. */
5462 static void
5463 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
5465 unsigned int decl_id = DECL_UID (decl);
5467 *decl_die_table->find_slot_with_hash (decl, decl_id, INSERT) = decl_die;
5468 decl_die->decl_id = decl_id;
5471 /* Return how many bits covers PIECE EXPR_LIST. */
5473 static HOST_WIDE_INT
5474 decl_piece_bitsize (rtx piece)
5476 int ret = (int) GET_MODE (piece);
5477 if (ret)
5478 return ret;
5479 gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
5480 && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
5481 return INTVAL (XEXP (XEXP (piece, 0), 0));
5484 /* Return pointer to the location of location note in PIECE EXPR_LIST. */
5486 static rtx *
5487 decl_piece_varloc_ptr (rtx piece)
5489 if ((int) GET_MODE (piece))
5490 return &XEXP (piece, 0);
5491 else
5492 return &XEXP (XEXP (piece, 0), 1);
5495 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
5496 Next is the chain of following piece nodes. */
5498 static rtx_expr_list *
5499 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
5501 if (bitsize > 0 && bitsize <= (int) MAX_MACHINE_MODE)
5502 return alloc_EXPR_LIST (bitsize, loc_note, next);
5503 else
5504 return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
5505 GEN_INT (bitsize),
5506 loc_note), next);
5509 /* Return rtx that should be stored into loc field for
5510 LOC_NOTE and BITPOS/BITSIZE. */
5512 static rtx
5513 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
5514 HOST_WIDE_INT bitsize)
5516 if (bitsize != -1)
5518 loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
5519 if (bitpos != 0)
5520 loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
5522 return loc_note;
5525 /* This function either modifies location piece list *DEST in
5526 place (if SRC and INNER is NULL), or copies location piece list
5527 *SRC to *DEST while modifying it. Location BITPOS is modified
5528 to contain LOC_NOTE, any pieces overlapping it are removed resp.
5529 not copied and if needed some padding around it is added.
5530 When modifying in place, DEST should point to EXPR_LIST where
5531 earlier pieces cover PIECE_BITPOS bits, when copying SRC points
5532 to the start of the whole list and INNER points to the EXPR_LIST
5533 where earlier pieces cover PIECE_BITPOS bits. */
5535 static void
5536 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
5537 HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
5538 HOST_WIDE_INT bitsize, rtx loc_note)
5540 HOST_WIDE_INT diff;
5541 bool copy = inner != NULL;
5543 if (copy)
5545 /* First copy all nodes preceding the current bitpos. */
5546 while (src != inner)
5548 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5549 decl_piece_bitsize (*src), NULL_RTX);
5550 dest = &XEXP (*dest, 1);
5551 src = &XEXP (*src, 1);
5554 /* Add padding if needed. */
5555 if (bitpos != piece_bitpos)
5557 *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
5558 copy ? NULL_RTX : *dest);
5559 dest = &XEXP (*dest, 1);
5561 else if (*dest && decl_piece_bitsize (*dest) == bitsize)
5563 gcc_assert (!copy);
5564 /* A piece with correct bitpos and bitsize already exist,
5565 just update the location for it and return. */
5566 *decl_piece_varloc_ptr (*dest) = loc_note;
5567 return;
5569 /* Add the piece that changed. */
5570 *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
5571 dest = &XEXP (*dest, 1);
5572 /* Skip over pieces that overlap it. */
5573 diff = bitpos - piece_bitpos + bitsize;
5574 if (!copy)
5575 src = dest;
5576 while (diff > 0 && *src)
5578 rtx piece = *src;
5579 diff -= decl_piece_bitsize (piece);
5580 if (copy)
5581 src = &XEXP (piece, 1);
5582 else
5584 *src = XEXP (piece, 1);
5585 free_EXPR_LIST_node (piece);
5588 /* Add padding if needed. */
5589 if (diff < 0 && *src)
5591 if (!copy)
5592 dest = src;
5593 *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
5594 dest = &XEXP (*dest, 1);
5596 if (!copy)
5597 return;
5598 /* Finally copy all nodes following it. */
5599 while (*src)
5601 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5602 decl_piece_bitsize (*src), NULL_RTX);
5603 dest = &XEXP (*dest, 1);
5604 src = &XEXP (*src, 1);
5608 /* Add a variable location node to the linked list for DECL. */
5610 static struct var_loc_node *
5611 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
5613 unsigned int decl_id;
5614 var_loc_list *temp;
5615 struct var_loc_node *loc = NULL;
5616 HOST_WIDE_INT bitsize = -1, bitpos = -1;
5618 if (VAR_P (decl) && DECL_HAS_DEBUG_EXPR_P (decl))
5620 tree realdecl = DECL_DEBUG_EXPR (decl);
5621 if (handled_component_p (realdecl)
5622 || (TREE_CODE (realdecl) == MEM_REF
5623 && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR))
5625 HOST_WIDE_INT maxsize;
5626 bool reverse;
5627 tree innerdecl
5628 = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize,
5629 &reverse);
5630 if (!DECL_P (innerdecl)
5631 || DECL_IGNORED_P (innerdecl)
5632 || TREE_STATIC (innerdecl)
5633 || bitsize <= 0
5634 || bitpos + bitsize > 256
5635 || bitsize != maxsize)
5636 return NULL;
5637 decl = innerdecl;
5641 decl_id = DECL_UID (decl);
5642 var_loc_list **slot
5643 = decl_loc_table->find_slot_with_hash (decl, decl_id, INSERT);
5644 if (*slot == NULL)
5646 temp = ggc_cleared_alloc<var_loc_list> ();
5647 temp->decl_id = decl_id;
5648 *slot = temp;
5650 else
5651 temp = *slot;
5653 /* For PARM_DECLs try to keep around the original incoming value,
5654 even if that means we'll emit a zero-range .debug_loc entry. */
5655 if (temp->last
5656 && temp->first == temp->last
5657 && TREE_CODE (decl) == PARM_DECL
5658 && NOTE_P (temp->first->loc)
5659 && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
5660 && DECL_INCOMING_RTL (decl)
5661 && NOTE_VAR_LOCATION_LOC (temp->first->loc)
5662 && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
5663 == GET_CODE (DECL_INCOMING_RTL (decl))
5664 && prev_real_insn (as_a<rtx_insn *> (temp->first->loc)) == NULL_RTX
5665 && (bitsize != -1
5666 || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
5667 NOTE_VAR_LOCATION_LOC (loc_note))
5668 || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
5669 != NOTE_VAR_LOCATION_STATUS (loc_note))))
5671 loc = ggc_cleared_alloc<var_loc_node> ();
5672 temp->first->next = loc;
5673 temp->last = loc;
5674 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5676 else if (temp->last)
5678 struct var_loc_node *last = temp->last, *unused = NULL;
5679 rtx *piece_loc = NULL, last_loc_note;
5680 HOST_WIDE_INT piece_bitpos = 0;
5681 if (last->next)
5683 last = last->next;
5684 gcc_assert (last->next == NULL);
5686 if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
5688 piece_loc = &last->loc;
5691 HOST_WIDE_INT cur_bitsize = decl_piece_bitsize (*piece_loc);
5692 if (piece_bitpos + cur_bitsize > bitpos)
5693 break;
5694 piece_bitpos += cur_bitsize;
5695 piece_loc = &XEXP (*piece_loc, 1);
5697 while (*piece_loc);
5699 /* TEMP->LAST here is either pointer to the last but one or
5700 last element in the chained list, LAST is pointer to the
5701 last element. */
5702 if (label && strcmp (last->label, label) == 0)
5704 /* For SRA optimized variables if there weren't any real
5705 insns since last note, just modify the last node. */
5706 if (piece_loc != NULL)
5708 adjust_piece_list (piece_loc, NULL, NULL,
5709 bitpos, piece_bitpos, bitsize, loc_note);
5710 return NULL;
5712 /* If the last note doesn't cover any instructions, remove it. */
5713 if (temp->last != last)
5715 temp->last->next = NULL;
5716 unused = last;
5717 last = temp->last;
5718 gcc_assert (strcmp (last->label, label) != 0);
5720 else
5722 gcc_assert (temp->first == temp->last
5723 || (temp->first->next == temp->last
5724 && TREE_CODE (decl) == PARM_DECL));
5725 memset (temp->last, '\0', sizeof (*temp->last));
5726 temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
5727 return temp->last;
5730 if (bitsize == -1 && NOTE_P (last->loc))
5731 last_loc_note = last->loc;
5732 else if (piece_loc != NULL
5733 && *piece_loc != NULL_RTX
5734 && piece_bitpos == bitpos
5735 && decl_piece_bitsize (*piece_loc) == bitsize)
5736 last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
5737 else
5738 last_loc_note = NULL_RTX;
5739 /* If the current location is the same as the end of the list,
5740 and either both or neither of the locations is uninitialized,
5741 we have nothing to do. */
5742 if (last_loc_note == NULL_RTX
5743 || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
5744 NOTE_VAR_LOCATION_LOC (loc_note)))
5745 || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5746 != NOTE_VAR_LOCATION_STATUS (loc_note))
5747 && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5748 == VAR_INIT_STATUS_UNINITIALIZED)
5749 || (NOTE_VAR_LOCATION_STATUS (loc_note)
5750 == VAR_INIT_STATUS_UNINITIALIZED))))
5752 /* Add LOC to the end of list and update LAST. If the last
5753 element of the list has been removed above, reuse its
5754 memory for the new node, otherwise allocate a new one. */
5755 if (unused)
5757 loc = unused;
5758 memset (loc, '\0', sizeof (*loc));
5760 else
5761 loc = ggc_cleared_alloc<var_loc_node> ();
5762 if (bitsize == -1 || piece_loc == NULL)
5763 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5764 else
5765 adjust_piece_list (&loc->loc, &last->loc, piece_loc,
5766 bitpos, piece_bitpos, bitsize, loc_note);
5767 last->next = loc;
5768 /* Ensure TEMP->LAST will point either to the new last but one
5769 element of the chain, or to the last element in it. */
5770 if (last != temp->last)
5771 temp->last = last;
5773 else if (unused)
5774 ggc_free (unused);
5776 else
5778 loc = ggc_cleared_alloc<var_loc_node> ();
5779 temp->first = loc;
5780 temp->last = loc;
5781 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5783 return loc;
5786 /* Keep track of the number of spaces used to indent the
5787 output of the debugging routines that print the structure of
5788 the DIE internal representation. */
5789 static int print_indent;
5791 /* Indent the line the number of spaces given by print_indent. */
5793 static inline void
5794 print_spaces (FILE *outfile)
5796 fprintf (outfile, "%*s", print_indent, "");
5799 /* Print a type signature in hex. */
5801 static inline void
5802 print_signature (FILE *outfile, char *sig)
5804 int i;
5806 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
5807 fprintf (outfile, "%02x", sig[i] & 0xff);
5810 static inline void
5811 print_discr_value (FILE *outfile, dw_discr_value *discr_value)
5813 if (discr_value->pos)
5814 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, discr_value->v.sval);
5815 else
5816 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, discr_value->v.uval);
5819 static void print_loc_descr (dw_loc_descr_ref, FILE *);
5821 /* Print the value associated to the VAL DWARF value node to OUTFILE. If
5822 RECURSE, output location descriptor operations. */
5824 static void
5825 print_dw_val (dw_val_node *val, bool recurse, FILE *outfile)
5827 switch (val->val_class)
5829 case dw_val_class_addr:
5830 fprintf (outfile, "address");
5831 break;
5832 case dw_val_class_offset:
5833 fprintf (outfile, "offset");
5834 break;
5835 case dw_val_class_loc:
5836 fprintf (outfile, "location descriptor");
5837 if (val->v.val_loc == NULL)
5838 fprintf (outfile, " -> <null>\n");
5839 else if (recurse)
5841 fprintf (outfile, ":\n");
5842 print_indent += 4;
5843 print_loc_descr (val->v.val_loc, outfile);
5844 print_indent -= 4;
5846 else
5847 fprintf (outfile, " (%p)\n", (void *) val->v.val_loc);
5848 break;
5849 case dw_val_class_loc_list:
5850 fprintf (outfile, "location list -> label:%s",
5851 val->v.val_loc_list->ll_symbol);
5852 break;
5853 case dw_val_class_range_list:
5854 fprintf (outfile, "range list");
5855 break;
5856 case dw_val_class_const:
5857 case dw_val_class_const_implicit:
5858 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, val->v.val_int);
5859 break;
5860 case dw_val_class_unsigned_const:
5861 case dw_val_class_unsigned_const_implicit:
5862 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, val->v.val_unsigned);
5863 break;
5864 case dw_val_class_const_double:
5865 fprintf (outfile, "constant (" HOST_WIDE_INT_PRINT_DEC","\
5866 HOST_WIDE_INT_PRINT_UNSIGNED")",
5867 val->v.val_double.high,
5868 val->v.val_double.low);
5869 break;
5870 case dw_val_class_wide_int:
5872 int i = val->v.val_wide->get_len ();
5873 fprintf (outfile, "constant (");
5874 gcc_assert (i > 0);
5875 if (val->v.val_wide->elt (i - 1) == 0)
5876 fprintf (outfile, "0x");
5877 fprintf (outfile, HOST_WIDE_INT_PRINT_HEX,
5878 val->v.val_wide->elt (--i));
5879 while (--i >= 0)
5880 fprintf (outfile, HOST_WIDE_INT_PRINT_PADDED_HEX,
5881 val->v.val_wide->elt (i));
5882 fprintf (outfile, ")");
5883 break;
5885 case dw_val_class_vec:
5886 fprintf (outfile, "floating-point or vector constant");
5887 break;
5888 case dw_val_class_flag:
5889 fprintf (outfile, "%u", val->v.val_flag);
5890 break;
5891 case dw_val_class_die_ref:
5892 if (val->v.val_die_ref.die != NULL)
5894 dw_die_ref die = val->v.val_die_ref.die;
5896 if (die->comdat_type_p)
5898 fprintf (outfile, "die -> signature: ");
5899 print_signature (outfile,
5900 die->die_id.die_type_node->signature);
5902 else if (die->die_id.die_symbol)
5903 fprintf (outfile, "die -> label: %s", die->die_id.die_symbol);
5904 else
5905 fprintf (outfile, "die -> %ld", die->die_offset);
5906 fprintf (outfile, " (%p)", (void *) die);
5908 else
5909 fprintf (outfile, "die -> <null>");
5910 break;
5911 case dw_val_class_vms_delta:
5912 fprintf (outfile, "delta: @slotcount(%s-%s)",
5913 val->v.val_vms_delta.lbl2, val->v.val_vms_delta.lbl1);
5914 break;
5915 case dw_val_class_lbl_id:
5916 case dw_val_class_lineptr:
5917 case dw_val_class_macptr:
5918 case dw_val_class_loclistsptr:
5919 case dw_val_class_high_pc:
5920 fprintf (outfile, "label: %s", val->v.val_lbl_id);
5921 break;
5922 case dw_val_class_str:
5923 if (val->v.val_str->str != NULL)
5924 fprintf (outfile, "\"%s\"", val->v.val_str->str);
5925 else
5926 fprintf (outfile, "<null>");
5927 break;
5928 case dw_val_class_file:
5929 case dw_val_class_file_implicit:
5930 fprintf (outfile, "\"%s\" (%d)", val->v.val_file->filename,
5931 val->v.val_file->emitted_number);
5932 break;
5933 case dw_val_class_data8:
5935 int i;
5937 for (i = 0; i < 8; i++)
5938 fprintf (outfile, "%02x", val->v.val_data8[i]);
5939 break;
5941 case dw_val_class_discr_value:
5942 print_discr_value (outfile, &val->v.val_discr_value);
5943 break;
5944 case dw_val_class_discr_list:
5945 for (dw_discr_list_ref node = val->v.val_discr_list;
5946 node != NULL;
5947 node = node->dw_discr_next)
5949 if (node->dw_discr_range)
5951 fprintf (outfile, " .. ");
5952 print_discr_value (outfile, &node->dw_discr_lower_bound);
5953 print_discr_value (outfile, &node->dw_discr_upper_bound);
5955 else
5956 print_discr_value (outfile, &node->dw_discr_lower_bound);
5958 if (node->dw_discr_next != NULL)
5959 fprintf (outfile, " | ");
5961 default:
5962 break;
5966 /* Likewise, for a DIE attribute. */
5968 static void
5969 print_attribute (dw_attr_node *a, bool recurse, FILE *outfile)
5971 print_dw_val (&a->dw_attr_val, recurse, outfile);
5975 /* Print the list of operands in the LOC location description to OUTFILE. This
5976 routine is a debugging aid only. */
5978 static void
5979 print_loc_descr (dw_loc_descr_ref loc, FILE *outfile)
5981 dw_loc_descr_ref l = loc;
5983 if (loc == NULL)
5985 print_spaces (outfile);
5986 fprintf (outfile, "<null>\n");
5987 return;
5990 for (l = loc; l != NULL; l = l->dw_loc_next)
5992 print_spaces (outfile);
5993 fprintf (outfile, "(%p) %s",
5994 (void *) l,
5995 dwarf_stack_op_name (l->dw_loc_opc));
5996 if (l->dw_loc_oprnd1.val_class != dw_val_class_none)
5998 fprintf (outfile, " ");
5999 print_dw_val (&l->dw_loc_oprnd1, false, outfile);
6001 if (l->dw_loc_oprnd2.val_class != dw_val_class_none)
6003 fprintf (outfile, ", ");
6004 print_dw_val (&l->dw_loc_oprnd2, false, outfile);
6006 fprintf (outfile, "\n");
6010 /* Print the information associated with a given DIE, and its children.
6011 This routine is a debugging aid only. */
6013 static void
6014 print_die (dw_die_ref die, FILE *outfile)
6016 dw_attr_node *a;
6017 dw_die_ref c;
6018 unsigned ix;
6020 print_spaces (outfile);
6021 fprintf (outfile, "DIE %4ld: %s (%p)\n",
6022 die->die_offset, dwarf_tag_name (die->die_tag),
6023 (void*) die);
6024 print_spaces (outfile);
6025 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
6026 fprintf (outfile, " offset: %ld", die->die_offset);
6027 fprintf (outfile, " mark: %d\n", die->die_mark);
6029 if (die->comdat_type_p)
6031 print_spaces (outfile);
6032 fprintf (outfile, " signature: ");
6033 print_signature (outfile, die->die_id.die_type_node->signature);
6034 fprintf (outfile, "\n");
6037 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6039 print_spaces (outfile);
6040 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
6042 print_attribute (a, true, outfile);
6043 fprintf (outfile, "\n");
6046 if (die->die_child != NULL)
6048 print_indent += 4;
6049 FOR_EACH_CHILD (die, c, print_die (c, outfile));
6050 print_indent -= 4;
6052 if (print_indent == 0)
6053 fprintf (outfile, "\n");
6056 /* Print the list of operations in the LOC location description. */
6058 DEBUG_FUNCTION void
6059 debug_dwarf_loc_descr (dw_loc_descr_ref loc)
6061 print_loc_descr (loc, stderr);
6064 /* Print the information collected for a given DIE. */
6066 DEBUG_FUNCTION void
6067 debug_dwarf_die (dw_die_ref die)
6069 print_die (die, stderr);
6072 DEBUG_FUNCTION void
6073 debug (die_struct &ref)
6075 print_die (&ref, stderr);
6078 DEBUG_FUNCTION void
6079 debug (die_struct *ptr)
6081 if (ptr)
6082 debug (*ptr);
6083 else
6084 fprintf (stderr, "<nil>\n");
6088 /* Print all DWARF information collected for the compilation unit.
6089 This routine is a debugging aid only. */
6091 DEBUG_FUNCTION void
6092 debug_dwarf (void)
6094 print_indent = 0;
6095 print_die (comp_unit_die (), stderr);
6098 /* Verify the DIE tree structure. */
6100 DEBUG_FUNCTION void
6101 verify_die (dw_die_ref die)
6103 gcc_assert (!die->die_mark);
6104 if (die->die_parent == NULL
6105 && die->die_sib == NULL)
6106 return;
6107 /* Verify the die_sib list is cyclic. */
6108 dw_die_ref x = die;
6111 x->die_mark = 1;
6112 x = x->die_sib;
6114 while (x && !x->die_mark);
6115 gcc_assert (x == die);
6116 x = die;
6119 /* Verify all dies have the same parent. */
6120 gcc_assert (x->die_parent == die->die_parent);
6121 if (x->die_child)
6123 /* Verify the child has the proper parent and recurse. */
6124 gcc_assert (x->die_child->die_parent == x);
6125 verify_die (x->die_child);
6127 x->die_mark = 0;
6128 x = x->die_sib;
6130 while (x && x->die_mark);
6133 /* Sanity checks on DIEs. */
6135 static void
6136 check_die (dw_die_ref die)
6138 unsigned ix;
6139 dw_attr_node *a;
6140 bool inline_found = false;
6141 int n_location = 0, n_low_pc = 0, n_high_pc = 0, n_artificial = 0;
6142 int n_decl_line = 0, n_decl_column = 0, n_decl_file = 0;
6143 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6145 switch (a->dw_attr)
6147 case DW_AT_inline:
6148 if (a->dw_attr_val.v.val_unsigned)
6149 inline_found = true;
6150 break;
6151 case DW_AT_location:
6152 ++n_location;
6153 break;
6154 case DW_AT_low_pc:
6155 ++n_low_pc;
6156 break;
6157 case DW_AT_high_pc:
6158 ++n_high_pc;
6159 break;
6160 case DW_AT_artificial:
6161 ++n_artificial;
6162 break;
6163 case DW_AT_decl_column:
6164 ++n_decl_column;
6165 break;
6166 case DW_AT_decl_line:
6167 ++n_decl_line;
6168 break;
6169 case DW_AT_decl_file:
6170 ++n_decl_file;
6171 break;
6172 default:
6173 break;
6176 if (n_location > 1 || n_low_pc > 1 || n_high_pc > 1 || n_artificial > 1
6177 || n_decl_column > 1 || n_decl_line > 1 || n_decl_file > 1)
6179 fprintf (stderr, "Duplicate attributes in DIE:\n");
6180 debug_dwarf_die (die);
6181 gcc_unreachable ();
6183 if (inline_found)
6185 /* A debugging information entry that is a member of an abstract
6186 instance tree [that has DW_AT_inline] should not contain any
6187 attributes which describe aspects of the subroutine which vary
6188 between distinct inlined expansions or distinct out-of-line
6189 expansions. */
6190 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6191 gcc_assert (a->dw_attr != DW_AT_low_pc
6192 && a->dw_attr != DW_AT_high_pc
6193 && a->dw_attr != DW_AT_location
6194 && a->dw_attr != DW_AT_frame_base
6195 && a->dw_attr != DW_AT_call_all_calls
6196 && a->dw_attr != DW_AT_GNU_all_call_sites);
6200 /* Start a new compilation unit DIE for an include file. OLD_UNIT is the CU
6201 for the enclosing include file, if any. BINCL_DIE is the DW_TAG_GNU_BINCL
6202 DIE that marks the start of the DIEs for this include file. */
6204 static dw_die_ref
6205 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
6207 const char *filename = get_AT_string (bincl_die, DW_AT_name);
6208 dw_die_ref new_unit = gen_compile_unit_die (filename);
6210 new_unit->die_sib = old_unit;
6211 return new_unit;
6214 /* Close an include-file CU and reopen the enclosing one. */
6216 static dw_die_ref
6217 pop_compile_unit (dw_die_ref old_unit)
6219 dw_die_ref new_unit = old_unit->die_sib;
6221 old_unit->die_sib = NULL;
6222 return new_unit;
6225 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
6226 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
6227 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
6229 /* Calculate the checksum of a location expression. */
6231 static inline void
6232 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
6234 int tem;
6235 inchash::hash hstate;
6236 hashval_t hash;
6238 tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
6239 CHECKSUM (tem);
6240 hash_loc_operands (loc, hstate);
6241 hash = hstate.end();
6242 CHECKSUM (hash);
6245 /* Calculate the checksum of an attribute. */
6247 static void
6248 attr_checksum (dw_attr_node *at, struct md5_ctx *ctx, int *mark)
6250 dw_loc_descr_ref loc;
6251 rtx r;
6253 CHECKSUM (at->dw_attr);
6255 /* We don't care that this was compiled with a different compiler
6256 snapshot; if the output is the same, that's what matters. */
6257 if (at->dw_attr == DW_AT_producer)
6258 return;
6260 switch (AT_class (at))
6262 case dw_val_class_const:
6263 case dw_val_class_const_implicit:
6264 CHECKSUM (at->dw_attr_val.v.val_int);
6265 break;
6266 case dw_val_class_unsigned_const:
6267 case dw_val_class_unsigned_const_implicit:
6268 CHECKSUM (at->dw_attr_val.v.val_unsigned);
6269 break;
6270 case dw_val_class_const_double:
6271 CHECKSUM (at->dw_attr_val.v.val_double);
6272 break;
6273 case dw_val_class_wide_int:
6274 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
6275 get_full_len (*at->dw_attr_val.v.val_wide)
6276 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
6277 break;
6278 case dw_val_class_vec:
6279 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
6280 (at->dw_attr_val.v.val_vec.length
6281 * at->dw_attr_val.v.val_vec.elt_size));
6282 break;
6283 case dw_val_class_flag:
6284 CHECKSUM (at->dw_attr_val.v.val_flag);
6285 break;
6286 case dw_val_class_str:
6287 CHECKSUM_STRING (AT_string (at));
6288 break;
6290 case dw_val_class_addr:
6291 r = AT_addr (at);
6292 gcc_assert (GET_CODE (r) == SYMBOL_REF);
6293 CHECKSUM_STRING (XSTR (r, 0));
6294 break;
6296 case dw_val_class_offset:
6297 CHECKSUM (at->dw_attr_val.v.val_offset);
6298 break;
6300 case dw_val_class_loc:
6301 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6302 loc_checksum (loc, ctx);
6303 break;
6305 case dw_val_class_die_ref:
6306 die_checksum (AT_ref (at), ctx, mark);
6307 break;
6309 case dw_val_class_fde_ref:
6310 case dw_val_class_vms_delta:
6311 case dw_val_class_lbl_id:
6312 case dw_val_class_lineptr:
6313 case dw_val_class_macptr:
6314 case dw_val_class_loclistsptr:
6315 case dw_val_class_high_pc:
6316 break;
6318 case dw_val_class_file:
6319 case dw_val_class_file_implicit:
6320 CHECKSUM_STRING (AT_file (at)->filename);
6321 break;
6323 case dw_val_class_data8:
6324 CHECKSUM (at->dw_attr_val.v.val_data8);
6325 break;
6327 default:
6328 break;
6332 /* Calculate the checksum of a DIE. */
6334 static void
6335 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6337 dw_die_ref c;
6338 dw_attr_node *a;
6339 unsigned ix;
6341 /* To avoid infinite recursion. */
6342 if (die->die_mark)
6344 CHECKSUM (die->die_mark);
6345 return;
6347 die->die_mark = ++(*mark);
6349 CHECKSUM (die->die_tag);
6351 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6352 attr_checksum (a, ctx, mark);
6354 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
6357 #undef CHECKSUM
6358 #undef CHECKSUM_BLOCK
6359 #undef CHECKSUM_STRING
6361 /* For DWARF-4 types, include the trailing NULL when checksumming strings. */
6362 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
6363 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
6364 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
6365 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
6366 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
6367 #define CHECKSUM_ATTR(FOO) \
6368 if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
6370 /* Calculate the checksum of a number in signed LEB128 format. */
6372 static void
6373 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
6375 unsigned char byte;
6376 bool more;
6378 while (1)
6380 byte = (value & 0x7f);
6381 value >>= 7;
6382 more = !((value == 0 && (byte & 0x40) == 0)
6383 || (value == -1 && (byte & 0x40) != 0));
6384 if (more)
6385 byte |= 0x80;
6386 CHECKSUM (byte);
6387 if (!more)
6388 break;
6392 /* Calculate the checksum of a number in unsigned LEB128 format. */
6394 static void
6395 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
6397 while (1)
6399 unsigned char byte = (value & 0x7f);
6400 value >>= 7;
6401 if (value != 0)
6402 /* More bytes to follow. */
6403 byte |= 0x80;
6404 CHECKSUM (byte);
6405 if (value == 0)
6406 break;
6410 /* Checksum the context of the DIE. This adds the names of any
6411 surrounding namespaces or structures to the checksum. */
6413 static void
6414 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
6416 const char *name;
6417 dw_die_ref spec;
6418 int tag = die->die_tag;
6420 if (tag != DW_TAG_namespace
6421 && tag != DW_TAG_structure_type
6422 && tag != DW_TAG_class_type)
6423 return;
6425 name = get_AT_string (die, DW_AT_name);
6427 spec = get_AT_ref (die, DW_AT_specification);
6428 if (spec != NULL)
6429 die = spec;
6431 if (die->die_parent != NULL)
6432 checksum_die_context (die->die_parent, ctx);
6434 CHECKSUM_ULEB128 ('C');
6435 CHECKSUM_ULEB128 (tag);
6436 if (name != NULL)
6437 CHECKSUM_STRING (name);
6440 /* Calculate the checksum of a location expression. */
6442 static inline void
6443 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
6445 /* Special case for lone DW_OP_plus_uconst: checksum as if the location
6446 were emitted as a DW_FORM_sdata instead of a location expression. */
6447 if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
6449 CHECKSUM_ULEB128 (DW_FORM_sdata);
6450 CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
6451 return;
6454 /* Otherwise, just checksum the raw location expression. */
6455 while (loc != NULL)
6457 inchash::hash hstate;
6458 hashval_t hash;
6460 CHECKSUM_ULEB128 (loc->dtprel);
6461 CHECKSUM_ULEB128 (loc->dw_loc_opc);
6462 hash_loc_operands (loc, hstate);
6463 hash = hstate.end ();
6464 CHECKSUM (hash);
6465 loc = loc->dw_loc_next;
6469 /* Calculate the checksum of an attribute. */
6471 static void
6472 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_node *at,
6473 struct md5_ctx *ctx, int *mark)
6475 dw_loc_descr_ref loc;
6476 rtx r;
6478 if (AT_class (at) == dw_val_class_die_ref)
6480 dw_die_ref target_die = AT_ref (at);
6482 /* For pointer and reference types, we checksum only the (qualified)
6483 name of the target type (if there is a name). For friend entries,
6484 we checksum only the (qualified) name of the target type or function.
6485 This allows the checksum to remain the same whether the target type
6486 is complete or not. */
6487 if ((at->dw_attr == DW_AT_type
6488 && (tag == DW_TAG_pointer_type
6489 || tag == DW_TAG_reference_type
6490 || tag == DW_TAG_rvalue_reference_type
6491 || tag == DW_TAG_ptr_to_member_type))
6492 || (at->dw_attr == DW_AT_friend
6493 && tag == DW_TAG_friend))
6495 dw_attr_node *name_attr = get_AT (target_die, DW_AT_name);
6497 if (name_attr != NULL)
6499 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
6501 if (decl == NULL)
6502 decl = target_die;
6503 CHECKSUM_ULEB128 ('N');
6504 CHECKSUM_ULEB128 (at->dw_attr);
6505 if (decl->die_parent != NULL)
6506 checksum_die_context (decl->die_parent, ctx);
6507 CHECKSUM_ULEB128 ('E');
6508 CHECKSUM_STRING (AT_string (name_attr));
6509 return;
6513 /* For all other references to another DIE, we check to see if the
6514 target DIE has already been visited. If it has, we emit a
6515 backward reference; if not, we descend recursively. */
6516 if (target_die->die_mark > 0)
6518 CHECKSUM_ULEB128 ('R');
6519 CHECKSUM_ULEB128 (at->dw_attr);
6520 CHECKSUM_ULEB128 (target_die->die_mark);
6522 else
6524 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
6526 if (decl == NULL)
6527 decl = target_die;
6528 target_die->die_mark = ++(*mark);
6529 CHECKSUM_ULEB128 ('T');
6530 CHECKSUM_ULEB128 (at->dw_attr);
6531 if (decl->die_parent != NULL)
6532 checksum_die_context (decl->die_parent, ctx);
6533 die_checksum_ordered (target_die, ctx, mark);
6535 return;
6538 CHECKSUM_ULEB128 ('A');
6539 CHECKSUM_ULEB128 (at->dw_attr);
6541 switch (AT_class (at))
6543 case dw_val_class_const:
6544 case dw_val_class_const_implicit:
6545 CHECKSUM_ULEB128 (DW_FORM_sdata);
6546 CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
6547 break;
6549 case dw_val_class_unsigned_const:
6550 case dw_val_class_unsigned_const_implicit:
6551 CHECKSUM_ULEB128 (DW_FORM_sdata);
6552 CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
6553 break;
6555 case dw_val_class_const_double:
6556 CHECKSUM_ULEB128 (DW_FORM_block);
6557 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
6558 CHECKSUM (at->dw_attr_val.v.val_double);
6559 break;
6561 case dw_val_class_wide_int:
6562 CHECKSUM_ULEB128 (DW_FORM_block);
6563 CHECKSUM_ULEB128 (get_full_len (*at->dw_attr_val.v.val_wide)
6564 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
6565 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
6566 get_full_len (*at->dw_attr_val.v.val_wide)
6567 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
6568 break;
6570 case dw_val_class_vec:
6571 CHECKSUM_ULEB128 (DW_FORM_block);
6572 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_vec.length
6573 * at->dw_attr_val.v.val_vec.elt_size);
6574 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
6575 (at->dw_attr_val.v.val_vec.length
6576 * at->dw_attr_val.v.val_vec.elt_size));
6577 break;
6579 case dw_val_class_flag:
6580 CHECKSUM_ULEB128 (DW_FORM_flag);
6581 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
6582 break;
6584 case dw_val_class_str:
6585 CHECKSUM_ULEB128 (DW_FORM_string);
6586 CHECKSUM_STRING (AT_string (at));
6587 break;
6589 case dw_val_class_addr:
6590 r = AT_addr (at);
6591 gcc_assert (GET_CODE (r) == SYMBOL_REF);
6592 CHECKSUM_ULEB128 (DW_FORM_string);
6593 CHECKSUM_STRING (XSTR (r, 0));
6594 break;
6596 case dw_val_class_offset:
6597 CHECKSUM_ULEB128 (DW_FORM_sdata);
6598 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
6599 break;
6601 case dw_val_class_loc:
6602 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6603 loc_checksum_ordered (loc, ctx);
6604 break;
6606 case dw_val_class_fde_ref:
6607 case dw_val_class_lbl_id:
6608 case dw_val_class_lineptr:
6609 case dw_val_class_macptr:
6610 case dw_val_class_loclistsptr:
6611 case dw_val_class_high_pc:
6612 break;
6614 case dw_val_class_file:
6615 case dw_val_class_file_implicit:
6616 CHECKSUM_ULEB128 (DW_FORM_string);
6617 CHECKSUM_STRING (AT_file (at)->filename);
6618 break;
6620 case dw_val_class_data8:
6621 CHECKSUM (at->dw_attr_val.v.val_data8);
6622 break;
6624 default:
6625 break;
6629 struct checksum_attributes
6631 dw_attr_node *at_name;
6632 dw_attr_node *at_type;
6633 dw_attr_node *at_friend;
6634 dw_attr_node *at_accessibility;
6635 dw_attr_node *at_address_class;
6636 dw_attr_node *at_alignment;
6637 dw_attr_node *at_allocated;
6638 dw_attr_node *at_artificial;
6639 dw_attr_node *at_associated;
6640 dw_attr_node *at_binary_scale;
6641 dw_attr_node *at_bit_offset;
6642 dw_attr_node *at_bit_size;
6643 dw_attr_node *at_bit_stride;
6644 dw_attr_node *at_byte_size;
6645 dw_attr_node *at_byte_stride;
6646 dw_attr_node *at_const_value;
6647 dw_attr_node *at_containing_type;
6648 dw_attr_node *at_count;
6649 dw_attr_node *at_data_location;
6650 dw_attr_node *at_data_member_location;
6651 dw_attr_node *at_decimal_scale;
6652 dw_attr_node *at_decimal_sign;
6653 dw_attr_node *at_default_value;
6654 dw_attr_node *at_digit_count;
6655 dw_attr_node *at_discr;
6656 dw_attr_node *at_discr_list;
6657 dw_attr_node *at_discr_value;
6658 dw_attr_node *at_encoding;
6659 dw_attr_node *at_endianity;
6660 dw_attr_node *at_explicit;
6661 dw_attr_node *at_is_optional;
6662 dw_attr_node *at_location;
6663 dw_attr_node *at_lower_bound;
6664 dw_attr_node *at_mutable;
6665 dw_attr_node *at_ordering;
6666 dw_attr_node *at_picture_string;
6667 dw_attr_node *at_prototyped;
6668 dw_attr_node *at_small;
6669 dw_attr_node *at_segment;
6670 dw_attr_node *at_string_length;
6671 dw_attr_node *at_string_length_bit_size;
6672 dw_attr_node *at_string_length_byte_size;
6673 dw_attr_node *at_threads_scaled;
6674 dw_attr_node *at_upper_bound;
6675 dw_attr_node *at_use_location;
6676 dw_attr_node *at_use_UTF8;
6677 dw_attr_node *at_variable_parameter;
6678 dw_attr_node *at_virtuality;
6679 dw_attr_node *at_visibility;
6680 dw_attr_node *at_vtable_elem_location;
6683 /* Collect the attributes that we will want to use for the checksum. */
6685 static void
6686 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
6688 dw_attr_node *a;
6689 unsigned ix;
6691 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6693 switch (a->dw_attr)
6695 case DW_AT_name:
6696 attrs->at_name = a;
6697 break;
6698 case DW_AT_type:
6699 attrs->at_type = a;
6700 break;
6701 case DW_AT_friend:
6702 attrs->at_friend = a;
6703 break;
6704 case DW_AT_accessibility:
6705 attrs->at_accessibility = a;
6706 break;
6707 case DW_AT_address_class:
6708 attrs->at_address_class = a;
6709 break;
6710 case DW_AT_alignment:
6711 attrs->at_alignment = a;
6712 break;
6713 case DW_AT_allocated:
6714 attrs->at_allocated = a;
6715 break;
6716 case DW_AT_artificial:
6717 attrs->at_artificial = a;
6718 break;
6719 case DW_AT_associated:
6720 attrs->at_associated = a;
6721 break;
6722 case DW_AT_binary_scale:
6723 attrs->at_binary_scale = a;
6724 break;
6725 case DW_AT_bit_offset:
6726 attrs->at_bit_offset = a;
6727 break;
6728 case DW_AT_bit_size:
6729 attrs->at_bit_size = a;
6730 break;
6731 case DW_AT_bit_stride:
6732 attrs->at_bit_stride = a;
6733 break;
6734 case DW_AT_byte_size:
6735 attrs->at_byte_size = a;
6736 break;
6737 case DW_AT_byte_stride:
6738 attrs->at_byte_stride = a;
6739 break;
6740 case DW_AT_const_value:
6741 attrs->at_const_value = a;
6742 break;
6743 case DW_AT_containing_type:
6744 attrs->at_containing_type = a;
6745 break;
6746 case DW_AT_count:
6747 attrs->at_count = a;
6748 break;
6749 case DW_AT_data_location:
6750 attrs->at_data_location = a;
6751 break;
6752 case DW_AT_data_member_location:
6753 attrs->at_data_member_location = a;
6754 break;
6755 case DW_AT_decimal_scale:
6756 attrs->at_decimal_scale = a;
6757 break;
6758 case DW_AT_decimal_sign:
6759 attrs->at_decimal_sign = a;
6760 break;
6761 case DW_AT_default_value:
6762 attrs->at_default_value = a;
6763 break;
6764 case DW_AT_digit_count:
6765 attrs->at_digit_count = a;
6766 break;
6767 case DW_AT_discr:
6768 attrs->at_discr = a;
6769 break;
6770 case DW_AT_discr_list:
6771 attrs->at_discr_list = a;
6772 break;
6773 case DW_AT_discr_value:
6774 attrs->at_discr_value = a;
6775 break;
6776 case DW_AT_encoding:
6777 attrs->at_encoding = a;
6778 break;
6779 case DW_AT_endianity:
6780 attrs->at_endianity = a;
6781 break;
6782 case DW_AT_explicit:
6783 attrs->at_explicit = a;
6784 break;
6785 case DW_AT_is_optional:
6786 attrs->at_is_optional = a;
6787 break;
6788 case DW_AT_location:
6789 attrs->at_location = a;
6790 break;
6791 case DW_AT_lower_bound:
6792 attrs->at_lower_bound = a;
6793 break;
6794 case DW_AT_mutable:
6795 attrs->at_mutable = a;
6796 break;
6797 case DW_AT_ordering:
6798 attrs->at_ordering = a;
6799 break;
6800 case DW_AT_picture_string:
6801 attrs->at_picture_string = a;
6802 break;
6803 case DW_AT_prototyped:
6804 attrs->at_prototyped = a;
6805 break;
6806 case DW_AT_small:
6807 attrs->at_small = a;
6808 break;
6809 case DW_AT_segment:
6810 attrs->at_segment = a;
6811 break;
6812 case DW_AT_string_length:
6813 attrs->at_string_length = a;
6814 break;
6815 case DW_AT_string_length_bit_size:
6816 attrs->at_string_length_bit_size = a;
6817 break;
6818 case DW_AT_string_length_byte_size:
6819 attrs->at_string_length_byte_size = a;
6820 break;
6821 case DW_AT_threads_scaled:
6822 attrs->at_threads_scaled = a;
6823 break;
6824 case DW_AT_upper_bound:
6825 attrs->at_upper_bound = a;
6826 break;
6827 case DW_AT_use_location:
6828 attrs->at_use_location = a;
6829 break;
6830 case DW_AT_use_UTF8:
6831 attrs->at_use_UTF8 = a;
6832 break;
6833 case DW_AT_variable_parameter:
6834 attrs->at_variable_parameter = a;
6835 break;
6836 case DW_AT_virtuality:
6837 attrs->at_virtuality = a;
6838 break;
6839 case DW_AT_visibility:
6840 attrs->at_visibility = a;
6841 break;
6842 case DW_AT_vtable_elem_location:
6843 attrs->at_vtable_elem_location = a;
6844 break;
6845 default:
6846 break;
6851 /* Calculate the checksum of a DIE, using an ordered subset of attributes. */
6853 static void
6854 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6856 dw_die_ref c;
6857 dw_die_ref decl;
6858 struct checksum_attributes attrs;
6860 CHECKSUM_ULEB128 ('D');
6861 CHECKSUM_ULEB128 (die->die_tag);
6863 memset (&attrs, 0, sizeof (attrs));
6865 decl = get_AT_ref (die, DW_AT_specification);
6866 if (decl != NULL)
6867 collect_checksum_attributes (&attrs, decl);
6868 collect_checksum_attributes (&attrs, die);
6870 CHECKSUM_ATTR (attrs.at_name);
6871 CHECKSUM_ATTR (attrs.at_accessibility);
6872 CHECKSUM_ATTR (attrs.at_address_class);
6873 CHECKSUM_ATTR (attrs.at_allocated);
6874 CHECKSUM_ATTR (attrs.at_artificial);
6875 CHECKSUM_ATTR (attrs.at_associated);
6876 CHECKSUM_ATTR (attrs.at_binary_scale);
6877 CHECKSUM_ATTR (attrs.at_bit_offset);
6878 CHECKSUM_ATTR (attrs.at_bit_size);
6879 CHECKSUM_ATTR (attrs.at_bit_stride);
6880 CHECKSUM_ATTR (attrs.at_byte_size);
6881 CHECKSUM_ATTR (attrs.at_byte_stride);
6882 CHECKSUM_ATTR (attrs.at_const_value);
6883 CHECKSUM_ATTR (attrs.at_containing_type);
6884 CHECKSUM_ATTR (attrs.at_count);
6885 CHECKSUM_ATTR (attrs.at_data_location);
6886 CHECKSUM_ATTR (attrs.at_data_member_location);
6887 CHECKSUM_ATTR (attrs.at_decimal_scale);
6888 CHECKSUM_ATTR (attrs.at_decimal_sign);
6889 CHECKSUM_ATTR (attrs.at_default_value);
6890 CHECKSUM_ATTR (attrs.at_digit_count);
6891 CHECKSUM_ATTR (attrs.at_discr);
6892 CHECKSUM_ATTR (attrs.at_discr_list);
6893 CHECKSUM_ATTR (attrs.at_discr_value);
6894 CHECKSUM_ATTR (attrs.at_encoding);
6895 CHECKSUM_ATTR (attrs.at_endianity);
6896 CHECKSUM_ATTR (attrs.at_explicit);
6897 CHECKSUM_ATTR (attrs.at_is_optional);
6898 CHECKSUM_ATTR (attrs.at_location);
6899 CHECKSUM_ATTR (attrs.at_lower_bound);
6900 CHECKSUM_ATTR (attrs.at_mutable);
6901 CHECKSUM_ATTR (attrs.at_ordering);
6902 CHECKSUM_ATTR (attrs.at_picture_string);
6903 CHECKSUM_ATTR (attrs.at_prototyped);
6904 CHECKSUM_ATTR (attrs.at_small);
6905 CHECKSUM_ATTR (attrs.at_segment);
6906 CHECKSUM_ATTR (attrs.at_string_length);
6907 CHECKSUM_ATTR (attrs.at_string_length_bit_size);
6908 CHECKSUM_ATTR (attrs.at_string_length_byte_size);
6909 CHECKSUM_ATTR (attrs.at_threads_scaled);
6910 CHECKSUM_ATTR (attrs.at_upper_bound);
6911 CHECKSUM_ATTR (attrs.at_use_location);
6912 CHECKSUM_ATTR (attrs.at_use_UTF8);
6913 CHECKSUM_ATTR (attrs.at_variable_parameter);
6914 CHECKSUM_ATTR (attrs.at_virtuality);
6915 CHECKSUM_ATTR (attrs.at_visibility);
6916 CHECKSUM_ATTR (attrs.at_vtable_elem_location);
6917 CHECKSUM_ATTR (attrs.at_type);
6918 CHECKSUM_ATTR (attrs.at_friend);
6919 CHECKSUM_ATTR (attrs.at_alignment);
6921 /* Checksum the child DIEs. */
6922 c = die->die_child;
6923 if (c) do {
6924 dw_attr_node *name_attr;
6926 c = c->die_sib;
6927 name_attr = get_AT (c, DW_AT_name);
6928 if (is_template_instantiation (c))
6930 /* Ignore instantiations of member type and function templates. */
6932 else if (name_attr != NULL
6933 && (is_type_die (c) || c->die_tag == DW_TAG_subprogram))
6935 /* Use a shallow checksum for named nested types and member
6936 functions. */
6937 CHECKSUM_ULEB128 ('S');
6938 CHECKSUM_ULEB128 (c->die_tag);
6939 CHECKSUM_STRING (AT_string (name_attr));
6941 else
6943 /* Use a deep checksum for other children. */
6944 /* Mark this DIE so it gets processed when unmarking. */
6945 if (c->die_mark == 0)
6946 c->die_mark = -1;
6947 die_checksum_ordered (c, ctx, mark);
6949 } while (c != die->die_child);
6951 CHECKSUM_ULEB128 (0);
6954 /* Add a type name and tag to a hash. */
6955 static void
6956 die_odr_checksum (int tag, const char *name, md5_ctx *ctx)
6958 CHECKSUM_ULEB128 (tag);
6959 CHECKSUM_STRING (name);
6962 #undef CHECKSUM
6963 #undef CHECKSUM_STRING
6964 #undef CHECKSUM_ATTR
6965 #undef CHECKSUM_LEB128
6966 #undef CHECKSUM_ULEB128
6968 /* Generate the type signature for DIE. This is computed by generating an
6969 MD5 checksum over the DIE's tag, its relevant attributes, and its
6970 children. Attributes that are references to other DIEs are processed
6971 by recursion, using the MARK field to prevent infinite recursion.
6972 If the DIE is nested inside a namespace or another type, we also
6973 need to include that context in the signature. The lower 64 bits
6974 of the resulting MD5 checksum comprise the signature. */
6976 static void
6977 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
6979 int mark;
6980 const char *name;
6981 unsigned char checksum[16];
6982 struct md5_ctx ctx;
6983 dw_die_ref decl;
6984 dw_die_ref parent;
6986 name = get_AT_string (die, DW_AT_name);
6987 decl = get_AT_ref (die, DW_AT_specification);
6988 parent = get_die_parent (die);
6990 /* First, compute a signature for just the type name (and its surrounding
6991 context, if any. This is stored in the type unit DIE for link-time
6992 ODR (one-definition rule) checking. */
6994 if (is_cxx () && name != NULL)
6996 md5_init_ctx (&ctx);
6998 /* Checksum the names of surrounding namespaces and structures. */
6999 if (parent != NULL)
7000 checksum_die_context (parent, &ctx);
7002 /* Checksum the current DIE. */
7003 die_odr_checksum (die->die_tag, name, &ctx);
7004 md5_finish_ctx (&ctx, checksum);
7006 add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
7009 /* Next, compute the complete type signature. */
7011 md5_init_ctx (&ctx);
7012 mark = 1;
7013 die->die_mark = mark;
7015 /* Checksum the names of surrounding namespaces and structures. */
7016 if (parent != NULL)
7017 checksum_die_context (parent, &ctx);
7019 /* Checksum the DIE and its children. */
7020 die_checksum_ordered (die, &ctx, &mark);
7021 unmark_all_dies (die);
7022 md5_finish_ctx (&ctx, checksum);
7024 /* Store the signature in the type node and link the type DIE and the
7025 type node together. */
7026 memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
7027 DWARF_TYPE_SIGNATURE_SIZE);
7028 die->comdat_type_p = true;
7029 die->die_id.die_type_node = type_node;
7030 type_node->type_die = die;
7032 /* If the DIE is a specification, link its declaration to the type node
7033 as well. */
7034 if (decl != NULL)
7036 decl->comdat_type_p = true;
7037 decl->die_id.die_type_node = type_node;
7041 /* Do the location expressions look same? */
7042 static inline int
7043 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
7045 return loc1->dw_loc_opc == loc2->dw_loc_opc
7046 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
7047 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
7050 /* Do the values look the same? */
7051 static int
7052 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
7054 dw_loc_descr_ref loc1, loc2;
7055 rtx r1, r2;
7057 if (v1->val_class != v2->val_class)
7058 return 0;
7060 switch (v1->val_class)
7062 case dw_val_class_const:
7063 case dw_val_class_const_implicit:
7064 return v1->v.val_int == v2->v.val_int;
7065 case dw_val_class_unsigned_const:
7066 case dw_val_class_unsigned_const_implicit:
7067 return v1->v.val_unsigned == v2->v.val_unsigned;
7068 case dw_val_class_const_double:
7069 return v1->v.val_double.high == v2->v.val_double.high
7070 && v1->v.val_double.low == v2->v.val_double.low;
7071 case dw_val_class_wide_int:
7072 return *v1->v.val_wide == *v2->v.val_wide;
7073 case dw_val_class_vec:
7074 if (v1->v.val_vec.length != v2->v.val_vec.length
7075 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
7076 return 0;
7077 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
7078 v1->v.val_vec.length * v1->v.val_vec.elt_size))
7079 return 0;
7080 return 1;
7081 case dw_val_class_flag:
7082 return v1->v.val_flag == v2->v.val_flag;
7083 case dw_val_class_str:
7084 return !strcmp (v1->v.val_str->str, v2->v.val_str->str);
7086 case dw_val_class_addr:
7087 r1 = v1->v.val_addr;
7088 r2 = v2->v.val_addr;
7089 if (GET_CODE (r1) != GET_CODE (r2))
7090 return 0;
7091 return !rtx_equal_p (r1, r2);
7093 case dw_val_class_offset:
7094 return v1->v.val_offset == v2->v.val_offset;
7096 case dw_val_class_loc:
7097 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
7098 loc1 && loc2;
7099 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
7100 if (!same_loc_p (loc1, loc2, mark))
7101 return 0;
7102 return !loc1 && !loc2;
7104 case dw_val_class_die_ref:
7105 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
7107 case dw_val_class_fde_ref:
7108 case dw_val_class_vms_delta:
7109 case dw_val_class_lbl_id:
7110 case dw_val_class_lineptr:
7111 case dw_val_class_macptr:
7112 case dw_val_class_loclistsptr:
7113 case dw_val_class_high_pc:
7114 return 1;
7116 case dw_val_class_file:
7117 case dw_val_class_file_implicit:
7118 return v1->v.val_file == v2->v.val_file;
7120 case dw_val_class_data8:
7121 return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
7123 default:
7124 return 1;
7128 /* Do the attributes look the same? */
7130 static int
7131 same_attr_p (dw_attr_node *at1, dw_attr_node *at2, int *mark)
7133 if (at1->dw_attr != at2->dw_attr)
7134 return 0;
7136 /* We don't care that this was compiled with a different compiler
7137 snapshot; if the output is the same, that's what matters. */
7138 if (at1->dw_attr == DW_AT_producer)
7139 return 1;
7141 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
7144 /* Do the dies look the same? */
7146 static int
7147 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
7149 dw_die_ref c1, c2;
7150 dw_attr_node *a1;
7151 unsigned ix;
7153 /* To avoid infinite recursion. */
7154 if (die1->die_mark)
7155 return die1->die_mark == die2->die_mark;
7156 die1->die_mark = die2->die_mark = ++(*mark);
7158 if (die1->die_tag != die2->die_tag)
7159 return 0;
7161 if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
7162 return 0;
7164 FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
7165 if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
7166 return 0;
7168 c1 = die1->die_child;
7169 c2 = die2->die_child;
7170 if (! c1)
7172 if (c2)
7173 return 0;
7175 else
7176 for (;;)
7178 if (!same_die_p (c1, c2, mark))
7179 return 0;
7180 c1 = c1->die_sib;
7181 c2 = c2->die_sib;
7182 if (c1 == die1->die_child)
7184 if (c2 == die2->die_child)
7185 break;
7186 else
7187 return 0;
7191 return 1;
7194 /* Do the dies look the same? Wrapper around same_die_p. */
7196 static int
7197 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
7199 int mark = 0;
7200 int ret = same_die_p (die1, die2, &mark);
7202 unmark_all_dies (die1);
7203 unmark_all_dies (die2);
7205 return ret;
7208 /* The prefix to attach to symbols on DIEs in the current comdat debug
7209 info section. */
7210 static const char *comdat_symbol_id;
7212 /* The index of the current symbol within the current comdat CU. */
7213 static unsigned int comdat_symbol_number;
7215 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
7216 children, and set comdat_symbol_id accordingly. */
7218 static void
7219 compute_section_prefix (dw_die_ref unit_die)
7221 const char *die_name = get_AT_string (unit_die, DW_AT_name);
7222 const char *base = die_name ? lbasename (die_name) : "anonymous";
7223 char *name = XALLOCAVEC (char, strlen (base) + 64);
7224 char *p;
7225 int i, mark;
7226 unsigned char checksum[16];
7227 struct md5_ctx ctx;
7229 /* Compute the checksum of the DIE, then append part of it as hex digits to
7230 the name filename of the unit. */
7232 md5_init_ctx (&ctx);
7233 mark = 0;
7234 die_checksum (unit_die, &ctx, &mark);
7235 unmark_all_dies (unit_die);
7236 md5_finish_ctx (&ctx, checksum);
7238 sprintf (name, "%s.", base);
7239 clean_symbol_name (name);
7241 p = name + strlen (name);
7242 for (i = 0; i < 4; i++)
7244 sprintf (p, "%.2x", checksum[i]);
7245 p += 2;
7248 comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
7249 comdat_symbol_number = 0;
7252 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
7254 static int
7255 is_type_die (dw_die_ref die)
7257 switch (die->die_tag)
7259 case DW_TAG_array_type:
7260 case DW_TAG_class_type:
7261 case DW_TAG_interface_type:
7262 case DW_TAG_enumeration_type:
7263 case DW_TAG_pointer_type:
7264 case DW_TAG_reference_type:
7265 case DW_TAG_rvalue_reference_type:
7266 case DW_TAG_string_type:
7267 case DW_TAG_structure_type:
7268 case DW_TAG_subroutine_type:
7269 case DW_TAG_union_type:
7270 case DW_TAG_ptr_to_member_type:
7271 case DW_TAG_set_type:
7272 case DW_TAG_subrange_type:
7273 case DW_TAG_base_type:
7274 case DW_TAG_const_type:
7275 case DW_TAG_file_type:
7276 case DW_TAG_packed_type:
7277 case DW_TAG_volatile_type:
7278 case DW_TAG_typedef:
7279 return 1;
7280 default:
7281 return 0;
7285 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
7286 Basically, we want to choose the bits that are likely to be shared between
7287 compilations (types) and leave out the bits that are specific to individual
7288 compilations (functions). */
7290 static int
7291 is_comdat_die (dw_die_ref c)
7293 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
7294 we do for stabs. The advantage is a greater likelihood of sharing between
7295 objects that don't include headers in the same order (and therefore would
7296 put the base types in a different comdat). jason 8/28/00 */
7298 if (c->die_tag == DW_TAG_base_type)
7299 return 0;
7301 if (c->die_tag == DW_TAG_pointer_type
7302 || c->die_tag == DW_TAG_reference_type
7303 || c->die_tag == DW_TAG_rvalue_reference_type
7304 || c->die_tag == DW_TAG_const_type
7305 || c->die_tag == DW_TAG_volatile_type)
7307 dw_die_ref t = get_AT_ref (c, DW_AT_type);
7309 return t ? is_comdat_die (t) : 0;
7312 return is_type_die (c);
7315 /* Returns 1 iff C is the sort of DIE that might be referred to from another
7316 compilation unit. */
7318 static int
7319 is_symbol_die (dw_die_ref c)
7321 return (is_type_die (c)
7322 || is_declaration_die (c)
7323 || c->die_tag == DW_TAG_namespace
7324 || c->die_tag == DW_TAG_module);
7327 /* Returns true iff C is a compile-unit DIE. */
7329 static inline bool
7330 is_cu_die (dw_die_ref c)
7332 return c && (c->die_tag == DW_TAG_compile_unit
7333 || c->die_tag == DW_TAG_skeleton_unit);
7336 /* Returns true iff C is a unit DIE of some sort. */
7338 static inline bool
7339 is_unit_die (dw_die_ref c)
7341 return c && (c->die_tag == DW_TAG_compile_unit
7342 || c->die_tag == DW_TAG_partial_unit
7343 || c->die_tag == DW_TAG_type_unit
7344 || c->die_tag == DW_TAG_skeleton_unit);
7347 /* Returns true iff C is a namespace DIE. */
7349 static inline bool
7350 is_namespace_die (dw_die_ref c)
7352 return c && c->die_tag == DW_TAG_namespace;
7355 /* Returns true iff C is a class or structure DIE. */
7357 static inline bool
7358 is_class_die (dw_die_ref c)
7360 return c && (c->die_tag == DW_TAG_class_type
7361 || c->die_tag == DW_TAG_structure_type);
7364 /* Return non-zero if this DIE is a template parameter. */
7366 static inline bool
7367 is_template_parameter (dw_die_ref die)
7369 switch (die->die_tag)
7371 case DW_TAG_template_type_param:
7372 case DW_TAG_template_value_param:
7373 case DW_TAG_GNU_template_template_param:
7374 case DW_TAG_GNU_template_parameter_pack:
7375 return true;
7376 default:
7377 return false;
7381 /* Return non-zero if this DIE represents a template instantiation. */
7383 static inline bool
7384 is_template_instantiation (dw_die_ref die)
7386 dw_die_ref c;
7388 if (!is_type_die (die) && die->die_tag != DW_TAG_subprogram)
7389 return false;
7390 FOR_EACH_CHILD (die, c, if (is_template_parameter (c)) return true);
7391 return false;
7394 static char *
7395 gen_internal_sym (const char *prefix)
7397 char buf[MAX_ARTIFICIAL_LABEL_BYTES];
7399 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
7400 return xstrdup (buf);
7403 /* Assign symbols to all worthy DIEs under DIE. */
7405 static void
7406 assign_symbol_names (dw_die_ref die)
7408 dw_die_ref c;
7410 if (is_symbol_die (die) && !die->comdat_type_p)
7412 if (comdat_symbol_id)
7414 char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
7416 sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
7417 comdat_symbol_id, comdat_symbol_number++);
7418 die->die_id.die_symbol = xstrdup (p);
7420 else
7421 die->die_id.die_symbol = gen_internal_sym ("LDIE");
7424 FOR_EACH_CHILD (die, c, assign_symbol_names (c));
7427 struct cu_hash_table_entry
7429 dw_die_ref cu;
7430 unsigned min_comdat_num, max_comdat_num;
7431 struct cu_hash_table_entry *next;
7434 /* Helpers to manipulate hash table of CUs. */
7436 struct cu_hash_table_entry_hasher : pointer_hash <cu_hash_table_entry>
7438 typedef die_struct *compare_type;
7439 static inline hashval_t hash (const cu_hash_table_entry *);
7440 static inline bool equal (const cu_hash_table_entry *, const die_struct *);
7441 static inline void remove (cu_hash_table_entry *);
7444 inline hashval_t
7445 cu_hash_table_entry_hasher::hash (const cu_hash_table_entry *entry)
7447 return htab_hash_string (entry->cu->die_id.die_symbol);
7450 inline bool
7451 cu_hash_table_entry_hasher::equal (const cu_hash_table_entry *entry1,
7452 const die_struct *entry2)
7454 return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
7457 inline void
7458 cu_hash_table_entry_hasher::remove (cu_hash_table_entry *entry)
7460 struct cu_hash_table_entry *next;
7462 while (entry)
7464 next = entry->next;
7465 free (entry);
7466 entry = next;
7470 typedef hash_table<cu_hash_table_entry_hasher> cu_hash_type;
7472 /* Check whether we have already seen this CU and set up SYM_NUM
7473 accordingly. */
7474 static int
7475 check_duplicate_cu (dw_die_ref cu, cu_hash_type *htable, unsigned int *sym_num)
7477 struct cu_hash_table_entry dummy;
7478 struct cu_hash_table_entry **slot, *entry, *last = &dummy;
7480 dummy.max_comdat_num = 0;
7482 slot = htable->find_slot_with_hash (cu,
7483 htab_hash_string (cu->die_id.die_symbol),
7484 INSERT);
7485 entry = *slot;
7487 for (; entry; last = entry, entry = entry->next)
7489 if (same_die_p_wrap (cu, entry->cu))
7490 break;
7493 if (entry)
7495 *sym_num = entry->min_comdat_num;
7496 return 1;
7499 entry = XCNEW (struct cu_hash_table_entry);
7500 entry->cu = cu;
7501 entry->min_comdat_num = *sym_num = last->max_comdat_num;
7502 entry->next = *slot;
7503 *slot = entry;
7505 return 0;
7508 /* Record SYM_NUM to record of CU in HTABLE. */
7509 static void
7510 record_comdat_symbol_number (dw_die_ref cu, cu_hash_type *htable,
7511 unsigned int sym_num)
7513 struct cu_hash_table_entry **slot, *entry;
7515 slot = htable->find_slot_with_hash (cu,
7516 htab_hash_string (cu->die_id.die_symbol),
7517 NO_INSERT);
7518 entry = *slot;
7520 entry->max_comdat_num = sym_num;
7523 /* Traverse the DIE (which is always comp_unit_die), and set up
7524 additional compilation units for each of the include files we see
7525 bracketed by BINCL/EINCL. */
7527 static void
7528 break_out_includes (dw_die_ref die)
7530 dw_die_ref c;
7531 dw_die_ref unit = NULL;
7532 limbo_die_node *node, **pnode;
7534 c = die->die_child;
7535 if (c) do {
7536 dw_die_ref prev = c;
7537 c = c->die_sib;
7538 while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
7539 || (unit && is_comdat_die (c)))
7541 dw_die_ref next = c->die_sib;
7543 /* This DIE is for a secondary CU; remove it from the main one. */
7544 remove_child_with_prev (c, prev);
7546 if (c->die_tag == DW_TAG_GNU_BINCL)
7547 unit = push_new_compile_unit (unit, c);
7548 else if (c->die_tag == DW_TAG_GNU_EINCL)
7549 unit = pop_compile_unit (unit);
7550 else
7551 add_child_die (unit, c);
7552 c = next;
7553 if (c == die->die_child)
7554 break;
7556 } while (c != die->die_child);
7558 #if 0
7559 /* We can only use this in debugging, since the frontend doesn't check
7560 to make sure that we leave every include file we enter. */
7561 gcc_assert (!unit);
7562 #endif
7564 assign_symbol_names (die);
7565 cu_hash_type cu_hash_table (10);
7566 for (node = limbo_die_list, pnode = &limbo_die_list;
7567 node;
7568 node = node->next)
7570 int is_dupl;
7572 compute_section_prefix (node->die);
7573 is_dupl = check_duplicate_cu (node->die, &cu_hash_table,
7574 &comdat_symbol_number);
7575 assign_symbol_names (node->die);
7576 if (is_dupl)
7577 *pnode = node->next;
7578 else
7580 pnode = &node->next;
7581 record_comdat_symbol_number (node->die, &cu_hash_table,
7582 comdat_symbol_number);
7587 /* Return non-zero if this DIE is a declaration. */
7589 static int
7590 is_declaration_die (dw_die_ref die)
7592 dw_attr_node *a;
7593 unsigned ix;
7595 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7596 if (a->dw_attr == DW_AT_declaration)
7597 return 1;
7599 return 0;
7602 /* Return non-zero if this DIE is nested inside a subprogram. */
7604 static int
7605 is_nested_in_subprogram (dw_die_ref die)
7607 dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
7609 if (decl == NULL)
7610 decl = die;
7611 return local_scope_p (decl);
7614 /* Return non-zero if this DIE contains a defining declaration of a
7615 subprogram. */
7617 static int
7618 contains_subprogram_definition (dw_die_ref die)
7620 dw_die_ref c;
7622 if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
7623 return 1;
7624 FOR_EACH_CHILD (die, c, if (contains_subprogram_definition (c)) return 1);
7625 return 0;
7628 /* Return non-zero if this is a type DIE that should be moved to a
7629 COMDAT .debug_types section or .debug_info section with DW_UT_*type
7630 unit type. */
7632 static int
7633 should_move_die_to_comdat (dw_die_ref die)
7635 switch (die->die_tag)
7637 case DW_TAG_class_type:
7638 case DW_TAG_structure_type:
7639 case DW_TAG_enumeration_type:
7640 case DW_TAG_union_type:
7641 /* Don't move declarations, inlined instances, types nested in a
7642 subprogram, or types that contain subprogram definitions. */
7643 if (is_declaration_die (die)
7644 || get_AT (die, DW_AT_abstract_origin)
7645 || is_nested_in_subprogram (die)
7646 || contains_subprogram_definition (die))
7647 return 0;
7648 return 1;
7649 case DW_TAG_array_type:
7650 case DW_TAG_interface_type:
7651 case DW_TAG_pointer_type:
7652 case DW_TAG_reference_type:
7653 case DW_TAG_rvalue_reference_type:
7654 case DW_TAG_string_type:
7655 case DW_TAG_subroutine_type:
7656 case DW_TAG_ptr_to_member_type:
7657 case DW_TAG_set_type:
7658 case DW_TAG_subrange_type:
7659 case DW_TAG_base_type:
7660 case DW_TAG_const_type:
7661 case DW_TAG_file_type:
7662 case DW_TAG_packed_type:
7663 case DW_TAG_volatile_type:
7664 case DW_TAG_typedef:
7665 default:
7666 return 0;
7670 /* Make a clone of DIE. */
7672 static dw_die_ref
7673 clone_die (dw_die_ref die)
7675 dw_die_ref clone;
7676 dw_attr_node *a;
7677 unsigned ix;
7679 clone = ggc_cleared_alloc<die_node> ();
7680 clone->die_tag = die->die_tag;
7682 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7683 add_dwarf_attr (clone, a);
7685 return clone;
7688 /* Make a clone of the tree rooted at DIE. */
7690 static dw_die_ref
7691 clone_tree (dw_die_ref die)
7693 dw_die_ref c;
7694 dw_die_ref clone = clone_die (die);
7696 FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree (c)));
7698 return clone;
7701 /* Make a clone of DIE as a declaration. */
7703 static dw_die_ref
7704 clone_as_declaration (dw_die_ref die)
7706 dw_die_ref clone;
7707 dw_die_ref decl;
7708 dw_attr_node *a;
7709 unsigned ix;
7711 /* If the DIE is already a declaration, just clone it. */
7712 if (is_declaration_die (die))
7713 return clone_die (die);
7715 /* If the DIE is a specification, just clone its declaration DIE. */
7716 decl = get_AT_ref (die, DW_AT_specification);
7717 if (decl != NULL)
7719 clone = clone_die (decl);
7720 if (die->comdat_type_p)
7721 add_AT_die_ref (clone, DW_AT_signature, die);
7722 return clone;
7725 clone = ggc_cleared_alloc<die_node> ();
7726 clone->die_tag = die->die_tag;
7728 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7730 /* We don't want to copy over all attributes.
7731 For example we don't want DW_AT_byte_size because otherwise we will no
7732 longer have a declaration and GDB will treat it as a definition. */
7734 switch (a->dw_attr)
7736 case DW_AT_abstract_origin:
7737 case DW_AT_artificial:
7738 case DW_AT_containing_type:
7739 case DW_AT_external:
7740 case DW_AT_name:
7741 case DW_AT_type:
7742 case DW_AT_virtuality:
7743 case DW_AT_linkage_name:
7744 case DW_AT_MIPS_linkage_name:
7745 add_dwarf_attr (clone, a);
7746 break;
7747 case DW_AT_byte_size:
7748 case DW_AT_alignment:
7749 default:
7750 break;
7754 if (die->comdat_type_p)
7755 add_AT_die_ref (clone, DW_AT_signature, die);
7757 add_AT_flag (clone, DW_AT_declaration, 1);
7758 return clone;
7762 /* Structure to map a DIE in one CU to its copy in a comdat type unit. */
7764 struct decl_table_entry
7766 dw_die_ref orig;
7767 dw_die_ref copy;
7770 /* Helpers to manipulate hash table of copied declarations. */
7772 /* Hashtable helpers. */
7774 struct decl_table_entry_hasher : free_ptr_hash <decl_table_entry>
7776 typedef die_struct *compare_type;
7777 static inline hashval_t hash (const decl_table_entry *);
7778 static inline bool equal (const decl_table_entry *, const die_struct *);
7781 inline hashval_t
7782 decl_table_entry_hasher::hash (const decl_table_entry *entry)
7784 return htab_hash_pointer (entry->orig);
7787 inline bool
7788 decl_table_entry_hasher::equal (const decl_table_entry *entry1,
7789 const die_struct *entry2)
7791 return entry1->orig == entry2;
7794 typedef hash_table<decl_table_entry_hasher> decl_hash_type;
7796 /* Copy DIE and its ancestors, up to, but not including, the compile unit
7797 or type unit entry, to a new tree. Adds the new tree to UNIT and returns
7798 a pointer to the copy of DIE. If DECL_TABLE is provided, it is used
7799 to check if the ancestor has already been copied into UNIT. */
7801 static dw_die_ref
7802 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die,
7803 decl_hash_type *decl_table)
7805 dw_die_ref parent = die->die_parent;
7806 dw_die_ref new_parent = unit;
7807 dw_die_ref copy;
7808 decl_table_entry **slot = NULL;
7809 struct decl_table_entry *entry = NULL;
7811 if (decl_table)
7813 /* Check if the entry has already been copied to UNIT. */
7814 slot = decl_table->find_slot_with_hash (die, htab_hash_pointer (die),
7815 INSERT);
7816 if (*slot != HTAB_EMPTY_ENTRY)
7818 entry = *slot;
7819 return entry->copy;
7822 /* Record in DECL_TABLE that DIE has been copied to UNIT. */
7823 entry = XCNEW (struct decl_table_entry);
7824 entry->orig = die;
7825 entry->copy = NULL;
7826 *slot = entry;
7829 if (parent != NULL)
7831 dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
7832 if (spec != NULL)
7833 parent = spec;
7834 if (!is_unit_die (parent))
7835 new_parent = copy_ancestor_tree (unit, parent, decl_table);
7838 copy = clone_as_declaration (die);
7839 add_child_die (new_parent, copy);
7841 if (decl_table)
7843 /* Record the pointer to the copy. */
7844 entry->copy = copy;
7847 return copy;
7849 /* Copy the declaration context to the new type unit DIE. This includes
7850 any surrounding namespace or type declarations. If the DIE has an
7851 AT_specification attribute, it also includes attributes and children
7852 attached to the specification, and returns a pointer to the original
7853 parent of the declaration DIE. Returns NULL otherwise. */
7855 static dw_die_ref
7856 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
7858 dw_die_ref decl;
7859 dw_die_ref new_decl;
7860 dw_die_ref orig_parent = NULL;
7862 decl = get_AT_ref (die, DW_AT_specification);
7863 if (decl == NULL)
7864 decl = die;
7865 else
7867 unsigned ix;
7868 dw_die_ref c;
7869 dw_attr_node *a;
7871 /* The original DIE will be changed to a declaration, and must
7872 be moved to be a child of the original declaration DIE. */
7873 orig_parent = decl->die_parent;
7875 /* Copy the type node pointer from the new DIE to the original
7876 declaration DIE so we can forward references later. */
7877 decl->comdat_type_p = true;
7878 decl->die_id.die_type_node = die->die_id.die_type_node;
7880 remove_AT (die, DW_AT_specification);
7882 FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
7884 if (a->dw_attr != DW_AT_name
7885 && a->dw_attr != DW_AT_declaration
7886 && a->dw_attr != DW_AT_external)
7887 add_dwarf_attr (die, a);
7890 FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree (c)));
7893 if (decl->die_parent != NULL
7894 && !is_unit_die (decl->die_parent))
7896 new_decl = copy_ancestor_tree (unit, decl, NULL);
7897 if (new_decl != NULL)
7899 remove_AT (new_decl, DW_AT_signature);
7900 add_AT_specification (die, new_decl);
7904 return orig_parent;
7907 /* Generate the skeleton ancestor tree for the given NODE, then clone
7908 the DIE and add the clone into the tree. */
7910 static void
7911 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
7913 if (node->new_die != NULL)
7914 return;
7916 node->new_die = clone_as_declaration (node->old_die);
7918 if (node->parent != NULL)
7920 generate_skeleton_ancestor_tree (node->parent);
7921 add_child_die (node->parent->new_die, node->new_die);
7925 /* Generate a skeleton tree of DIEs containing any declarations that are
7926 found in the original tree. We traverse the tree looking for declaration
7927 DIEs, and construct the skeleton from the bottom up whenever we find one. */
7929 static void
7930 generate_skeleton_bottom_up (skeleton_chain_node *parent)
7932 skeleton_chain_node node;
7933 dw_die_ref c;
7934 dw_die_ref first;
7935 dw_die_ref prev = NULL;
7936 dw_die_ref next = NULL;
7938 node.parent = parent;
7940 first = c = parent->old_die->die_child;
7941 if (c)
7942 next = c->die_sib;
7943 if (c) do {
7944 if (prev == NULL || prev->die_sib == c)
7945 prev = c;
7946 c = next;
7947 next = (c == first ? NULL : c->die_sib);
7948 node.old_die = c;
7949 node.new_die = NULL;
7950 if (is_declaration_die (c))
7952 if (is_template_instantiation (c))
7954 /* Instantiated templates do not need to be cloned into the
7955 type unit. Just move the DIE and its children back to
7956 the skeleton tree (in the main CU). */
7957 remove_child_with_prev (c, prev);
7958 add_child_die (parent->new_die, c);
7959 c = prev;
7961 else if (c->comdat_type_p)
7963 /* This is the skeleton of earlier break_out_comdat_types
7964 type. Clone the existing DIE, but keep the children
7965 under the original (which is in the main CU). */
7966 dw_die_ref clone = clone_die (c);
7968 replace_child (c, clone, prev);
7969 generate_skeleton_ancestor_tree (parent);
7970 add_child_die (parent->new_die, c);
7971 c = clone;
7972 continue;
7974 else
7976 /* Clone the existing DIE, move the original to the skeleton
7977 tree (which is in the main CU), and put the clone, with
7978 all the original's children, where the original came from
7979 (which is about to be moved to the type unit). */
7980 dw_die_ref clone = clone_die (c);
7981 move_all_children (c, clone);
7983 /* If the original has a DW_AT_object_pointer attribute,
7984 it would now point to a child DIE just moved to the
7985 cloned tree, so we need to remove that attribute from
7986 the original. */
7987 remove_AT (c, DW_AT_object_pointer);
7989 replace_child (c, clone, prev);
7990 generate_skeleton_ancestor_tree (parent);
7991 add_child_die (parent->new_die, c);
7992 node.old_die = clone;
7993 node.new_die = c;
7994 c = clone;
7997 generate_skeleton_bottom_up (&node);
7998 } while (next != NULL);
8001 /* Wrapper function for generate_skeleton_bottom_up. */
8003 static dw_die_ref
8004 generate_skeleton (dw_die_ref die)
8006 skeleton_chain_node node;
8008 node.old_die = die;
8009 node.new_die = NULL;
8010 node.parent = NULL;
8012 /* If this type definition is nested inside another type,
8013 and is not an instantiation of a template, always leave
8014 at least a declaration in its place. */
8015 if (die->die_parent != NULL
8016 && is_type_die (die->die_parent)
8017 && !is_template_instantiation (die))
8018 node.new_die = clone_as_declaration (die);
8020 generate_skeleton_bottom_up (&node);
8021 return node.new_die;
8024 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
8025 declaration. The original DIE is moved to a new compile unit so that
8026 existing references to it follow it to the new location. If any of the
8027 original DIE's descendants is a declaration, we need to replace the
8028 original DIE with a skeleton tree and move the declarations back into the
8029 skeleton tree. */
8031 static dw_die_ref
8032 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
8033 dw_die_ref prev)
8035 dw_die_ref skeleton, orig_parent;
8037 /* Copy the declaration context to the type unit DIE. If the returned
8038 ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
8039 that DIE. */
8040 orig_parent = copy_declaration_context (unit, child);
8042 skeleton = generate_skeleton (child);
8043 if (skeleton == NULL)
8044 remove_child_with_prev (child, prev);
8045 else
8047 skeleton->comdat_type_p = true;
8048 skeleton->die_id.die_type_node = child->die_id.die_type_node;
8050 /* If the original DIE was a specification, we need to put
8051 the skeleton under the parent DIE of the declaration.
8052 This leaves the original declaration in the tree, but
8053 it will be pruned later since there are no longer any
8054 references to it. */
8055 if (orig_parent != NULL)
8057 remove_child_with_prev (child, prev);
8058 add_child_die (orig_parent, skeleton);
8060 else
8061 replace_child (child, skeleton, prev);
8064 return skeleton;
8067 static void
8068 copy_dwarf_procs_ref_in_attrs (dw_die_ref die,
8069 comdat_type_node *type_node,
8070 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs);
8072 /* Helper for copy_dwarf_procs_ref_in_dies. Make a copy of the DIE DWARF
8073 procedure, put it under TYPE_NODE and return the copy. Continue looking for
8074 DWARF procedure references in the DW_AT_location attribute. */
8076 static dw_die_ref
8077 copy_dwarf_procedure (dw_die_ref die,
8078 comdat_type_node *type_node,
8079 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8081 gcc_assert (die->die_tag == DW_TAG_dwarf_procedure);
8083 /* DWARF procedures are not supposed to have children... */
8084 gcc_assert (die->die_child == NULL);
8086 /* ... and they are supposed to have only one attribute: DW_AT_location. */
8087 gcc_assert (vec_safe_length (die->die_attr) == 1
8088 && ((*die->die_attr)[0].dw_attr == DW_AT_location));
8090 /* Do not copy more than once DWARF procedures. */
8091 bool existed;
8092 dw_die_ref &die_copy = copied_dwarf_procs.get_or_insert (die, &existed);
8093 if (existed)
8094 return die_copy;
8096 die_copy = clone_die (die);
8097 add_child_die (type_node->root_die, die_copy);
8098 copy_dwarf_procs_ref_in_attrs (die_copy, type_node, copied_dwarf_procs);
8099 return die_copy;
8102 /* Helper for copy_dwarf_procs_ref_in_dies. Look for references to DWARF
8103 procedures in DIE's attributes. */
8105 static void
8106 copy_dwarf_procs_ref_in_attrs (dw_die_ref die,
8107 comdat_type_node *type_node,
8108 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8110 dw_attr_node *a;
8111 unsigned i;
8113 FOR_EACH_VEC_SAFE_ELT (die->die_attr, i, a)
8115 dw_loc_descr_ref loc;
8117 if (a->dw_attr_val.val_class != dw_val_class_loc)
8118 continue;
8120 for (loc = a->dw_attr_val.v.val_loc; loc != NULL; loc = loc->dw_loc_next)
8122 switch (loc->dw_loc_opc)
8124 case DW_OP_call2:
8125 case DW_OP_call4:
8126 case DW_OP_call_ref:
8127 gcc_assert (loc->dw_loc_oprnd1.val_class
8128 == dw_val_class_die_ref);
8129 loc->dw_loc_oprnd1.v.val_die_ref.die
8130 = copy_dwarf_procedure (loc->dw_loc_oprnd1.v.val_die_ref.die,
8131 type_node,
8132 copied_dwarf_procs);
8134 default:
8135 break;
8141 /* Copy DWARF procedures that are referenced by the DIE tree to TREE_NODE and
8142 rewrite references to point to the copies.
8144 References are looked for in DIE's attributes and recursively in all its
8145 children attributes that are location descriptions. COPIED_DWARF_PROCS is a
8146 mapping from old DWARF procedures to their copy. It is used not to copy
8147 twice the same DWARF procedure under TYPE_NODE. */
8149 static void
8150 copy_dwarf_procs_ref_in_dies (dw_die_ref die,
8151 comdat_type_node *type_node,
8152 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8154 dw_die_ref c;
8156 copy_dwarf_procs_ref_in_attrs (die, type_node, copied_dwarf_procs);
8157 FOR_EACH_CHILD (die, c, copy_dwarf_procs_ref_in_dies (c,
8158 type_node,
8159 copied_dwarf_procs));
8162 /* Traverse the DIE and set up additional .debug_types or .debug_info
8163 DW_UT_*type sections for each type worthy of being placed in a COMDAT
8164 section. */
8166 static void
8167 break_out_comdat_types (dw_die_ref die)
8169 dw_die_ref c;
8170 dw_die_ref first;
8171 dw_die_ref prev = NULL;
8172 dw_die_ref next = NULL;
8173 dw_die_ref unit = NULL;
8175 first = c = die->die_child;
8176 if (c)
8177 next = c->die_sib;
8178 if (c) do {
8179 if (prev == NULL || prev->die_sib == c)
8180 prev = c;
8181 c = next;
8182 next = (c == first ? NULL : c->die_sib);
8183 if (should_move_die_to_comdat (c))
8185 dw_die_ref replacement;
8186 comdat_type_node *type_node;
8188 /* Break out nested types into their own type units. */
8189 break_out_comdat_types (c);
8191 /* Create a new type unit DIE as the root for the new tree, and
8192 add it to the list of comdat types. */
8193 unit = new_die (DW_TAG_type_unit, NULL, NULL);
8194 add_AT_unsigned (unit, DW_AT_language,
8195 get_AT_unsigned (comp_unit_die (), DW_AT_language));
8196 type_node = ggc_cleared_alloc<comdat_type_node> ();
8197 type_node->root_die = unit;
8198 type_node->next = comdat_type_list;
8199 comdat_type_list = type_node;
8201 /* Generate the type signature. */
8202 generate_type_signature (c, type_node);
8204 /* Copy the declaration context, attributes, and children of the
8205 declaration into the new type unit DIE, then remove this DIE
8206 from the main CU (or replace it with a skeleton if necessary). */
8207 replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
8208 type_node->skeleton_die = replacement;
8210 /* Add the DIE to the new compunit. */
8211 add_child_die (unit, c);
8213 /* Types can reference DWARF procedures for type size or data location
8214 expressions. Calls in DWARF expressions cannot target procedures
8215 that are not in the same section. So we must copy DWARF procedures
8216 along with this type and then rewrite references to them. */
8217 hash_map<dw_die_ref, dw_die_ref> copied_dwarf_procs;
8218 copy_dwarf_procs_ref_in_dies (c, type_node, copied_dwarf_procs);
8220 if (replacement != NULL)
8221 c = replacement;
8223 else if (c->die_tag == DW_TAG_namespace
8224 || c->die_tag == DW_TAG_class_type
8225 || c->die_tag == DW_TAG_structure_type
8226 || c->die_tag == DW_TAG_union_type)
8228 /* Look for nested types that can be broken out. */
8229 break_out_comdat_types (c);
8231 } while (next != NULL);
8234 /* Like clone_tree, but copy DW_TAG_subprogram DIEs as declarations.
8235 Enter all the cloned children into the hash table decl_table. */
8237 static dw_die_ref
8238 clone_tree_partial (dw_die_ref die, decl_hash_type *decl_table)
8240 dw_die_ref c;
8241 dw_die_ref clone;
8242 struct decl_table_entry *entry;
8243 decl_table_entry **slot;
8245 if (die->die_tag == DW_TAG_subprogram)
8246 clone = clone_as_declaration (die);
8247 else
8248 clone = clone_die (die);
8250 slot = decl_table->find_slot_with_hash (die,
8251 htab_hash_pointer (die), INSERT);
8253 /* Assert that DIE isn't in the hash table yet. If it would be there
8254 before, the ancestors would be necessarily there as well, therefore
8255 clone_tree_partial wouldn't be called. */
8256 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
8258 entry = XCNEW (struct decl_table_entry);
8259 entry->orig = die;
8260 entry->copy = clone;
8261 *slot = entry;
8263 if (die->die_tag != DW_TAG_subprogram)
8264 FOR_EACH_CHILD (die, c,
8265 add_child_die (clone, clone_tree_partial (c, decl_table)));
8267 return clone;
8270 /* Walk the DIE and its children, looking for references to incomplete
8271 or trivial types that are unmarked (i.e., that are not in the current
8272 type_unit). */
8274 static void
8275 copy_decls_walk (dw_die_ref unit, dw_die_ref die, decl_hash_type *decl_table)
8277 dw_die_ref c;
8278 dw_attr_node *a;
8279 unsigned ix;
8281 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8283 if (AT_class (a) == dw_val_class_die_ref)
8285 dw_die_ref targ = AT_ref (a);
8286 decl_table_entry **slot;
8287 struct decl_table_entry *entry;
8289 if (targ->die_mark != 0 || targ->comdat_type_p)
8290 continue;
8292 slot = decl_table->find_slot_with_hash (targ,
8293 htab_hash_pointer (targ),
8294 INSERT);
8296 if (*slot != HTAB_EMPTY_ENTRY)
8298 /* TARG has already been copied, so we just need to
8299 modify the reference to point to the copy. */
8300 entry = *slot;
8301 a->dw_attr_val.v.val_die_ref.die = entry->copy;
8303 else
8305 dw_die_ref parent = unit;
8306 dw_die_ref copy = clone_die (targ);
8308 /* Record in DECL_TABLE that TARG has been copied.
8309 Need to do this now, before the recursive call,
8310 because DECL_TABLE may be expanded and SLOT
8311 would no longer be a valid pointer. */
8312 entry = XCNEW (struct decl_table_entry);
8313 entry->orig = targ;
8314 entry->copy = copy;
8315 *slot = entry;
8317 /* If TARG is not a declaration DIE, we need to copy its
8318 children. */
8319 if (!is_declaration_die (targ))
8321 FOR_EACH_CHILD (
8322 targ, c,
8323 add_child_die (copy,
8324 clone_tree_partial (c, decl_table)));
8327 /* Make sure the cloned tree is marked as part of the
8328 type unit. */
8329 mark_dies (copy);
8331 /* If TARG has surrounding context, copy its ancestor tree
8332 into the new type unit. */
8333 if (targ->die_parent != NULL
8334 && !is_unit_die (targ->die_parent))
8335 parent = copy_ancestor_tree (unit, targ->die_parent,
8336 decl_table);
8338 add_child_die (parent, copy);
8339 a->dw_attr_val.v.val_die_ref.die = copy;
8341 /* Make sure the newly-copied DIE is walked. If it was
8342 installed in a previously-added context, it won't
8343 get visited otherwise. */
8344 if (parent != unit)
8346 /* Find the highest point of the newly-added tree,
8347 mark each node along the way, and walk from there. */
8348 parent->die_mark = 1;
8349 while (parent->die_parent
8350 && parent->die_parent->die_mark == 0)
8352 parent = parent->die_parent;
8353 parent->die_mark = 1;
8355 copy_decls_walk (unit, parent, decl_table);
8361 FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
8364 /* Copy declarations for "unworthy" types into the new comdat section.
8365 Incomplete types, modified types, and certain other types aren't broken
8366 out into comdat sections of their own, so they don't have a signature,
8367 and we need to copy the declaration into the same section so that we
8368 don't have an external reference. */
8370 static void
8371 copy_decls_for_unworthy_types (dw_die_ref unit)
8373 mark_dies (unit);
8374 decl_hash_type decl_table (10);
8375 copy_decls_walk (unit, unit, &decl_table);
8376 unmark_dies (unit);
8379 /* Traverse the DIE and add a sibling attribute if it may have the
8380 effect of speeding up access to siblings. To save some space,
8381 avoid generating sibling attributes for DIE's without children. */
8383 static void
8384 add_sibling_attributes (dw_die_ref die)
8386 dw_die_ref c;
8388 if (! die->die_child)
8389 return;
8391 if (die->die_parent && die != die->die_parent->die_child)
8392 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
8394 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
8397 /* Output all location lists for the DIE and its children. */
8399 static void
8400 output_location_lists (dw_die_ref die)
8402 dw_die_ref c;
8403 dw_attr_node *a;
8404 unsigned ix;
8406 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8407 if (AT_class (a) == dw_val_class_loc_list)
8408 output_loc_list (AT_loc_list (a));
8410 FOR_EACH_CHILD (die, c, output_location_lists (c));
8413 /* During assign_location_list_indexes and output_loclists_offset the
8414 current index, after it the number of assigned indexes (i.e. how
8415 large the .debug_loclists* offset table should be). */
8416 static unsigned int loc_list_idx;
8418 /* Output all location list offsets for the DIE and its children. */
8420 static void
8421 output_loclists_offsets (dw_die_ref die)
8423 dw_die_ref c;
8424 dw_attr_node *a;
8425 unsigned ix;
8427 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8428 if (AT_class (a) == dw_val_class_loc_list)
8430 dw_loc_list_ref l = AT_loc_list (a);
8431 if (l->offset_emitted)
8432 continue;
8433 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l->ll_symbol,
8434 loc_section_label, NULL);
8435 gcc_assert (l->hash == loc_list_idx);
8436 loc_list_idx++;
8437 l->offset_emitted = true;
8440 FOR_EACH_CHILD (die, c, output_loclists_offsets (c));
8443 /* Recursively set indexes of location lists. */
8445 static void
8446 assign_location_list_indexes (dw_die_ref die)
8448 dw_die_ref c;
8449 dw_attr_node *a;
8450 unsigned ix;
8452 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8453 if (AT_class (a) == dw_val_class_loc_list)
8455 dw_loc_list_ref list = AT_loc_list (a);
8456 if (!list->num_assigned)
8458 list->num_assigned = true;
8459 list->hash = loc_list_idx++;
8463 FOR_EACH_CHILD (die, c, assign_location_list_indexes (c));
8466 /* We want to limit the number of external references, because they are
8467 larger than local references: a relocation takes multiple words, and
8468 even a sig8 reference is always eight bytes, whereas a local reference
8469 can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
8470 So if we encounter multiple external references to the same type DIE, we
8471 make a local typedef stub for it and redirect all references there.
8473 This is the element of the hash table for keeping track of these
8474 references. */
8476 struct external_ref
8478 dw_die_ref type;
8479 dw_die_ref stub;
8480 unsigned n_refs;
8483 /* Hashtable helpers. */
8485 struct external_ref_hasher : free_ptr_hash <external_ref>
8487 static inline hashval_t hash (const external_ref *);
8488 static inline bool equal (const external_ref *, const external_ref *);
8491 inline hashval_t
8492 external_ref_hasher::hash (const external_ref *r)
8494 dw_die_ref die = r->type;
8495 hashval_t h = 0;
8497 /* We can't use the address of the DIE for hashing, because
8498 that will make the order of the stub DIEs non-deterministic. */
8499 if (! die->comdat_type_p)
8500 /* We have a symbol; use it to compute a hash. */
8501 h = htab_hash_string (die->die_id.die_symbol);
8502 else
8504 /* We have a type signature; use a subset of the bits as the hash.
8505 The 8-byte signature is at least as large as hashval_t. */
8506 comdat_type_node *type_node = die->die_id.die_type_node;
8507 memcpy (&h, type_node->signature, sizeof (h));
8509 return h;
8512 inline bool
8513 external_ref_hasher::equal (const external_ref *r1, const external_ref *r2)
8515 return r1->type == r2->type;
8518 typedef hash_table<external_ref_hasher> external_ref_hash_type;
8520 /* Return a pointer to the external_ref for references to DIE. */
8522 static struct external_ref *
8523 lookup_external_ref (external_ref_hash_type *map, dw_die_ref die)
8525 struct external_ref ref, *ref_p;
8526 external_ref **slot;
8528 ref.type = die;
8529 slot = map->find_slot (&ref, INSERT);
8530 if (*slot != HTAB_EMPTY_ENTRY)
8531 return *slot;
8533 ref_p = XCNEW (struct external_ref);
8534 ref_p->type = die;
8535 *slot = ref_p;
8536 return ref_p;
8539 /* Subroutine of optimize_external_refs, below.
8541 If we see a type skeleton, record it as our stub. If we see external
8542 references, remember how many we've seen. */
8544 static void
8545 optimize_external_refs_1 (dw_die_ref die, external_ref_hash_type *map)
8547 dw_die_ref c;
8548 dw_attr_node *a;
8549 unsigned ix;
8550 struct external_ref *ref_p;
8552 if (is_type_die (die)
8553 && (c = get_AT_ref (die, DW_AT_signature)))
8555 /* This is a local skeleton; use it for local references. */
8556 ref_p = lookup_external_ref (map, c);
8557 ref_p->stub = die;
8560 /* Scan the DIE references, and remember any that refer to DIEs from
8561 other CUs (i.e. those which are not marked). */
8562 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8563 if (AT_class (a) == dw_val_class_die_ref
8564 && (c = AT_ref (a))->die_mark == 0
8565 && is_type_die (c))
8567 ref_p = lookup_external_ref (map, c);
8568 ref_p->n_refs++;
8571 FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
8574 /* htab_traverse callback function for optimize_external_refs, below. SLOT
8575 points to an external_ref, DATA is the CU we're processing. If we don't
8576 already have a local stub, and we have multiple refs, build a stub. */
8579 dwarf2_build_local_stub (external_ref **slot, dw_die_ref data)
8581 struct external_ref *ref_p = *slot;
8583 if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
8585 /* We have multiple references to this type, so build a small stub.
8586 Both of these forms are a bit dodgy from the perspective of the
8587 DWARF standard, since technically they should have names. */
8588 dw_die_ref cu = data;
8589 dw_die_ref type = ref_p->type;
8590 dw_die_ref stub = NULL;
8592 if (type->comdat_type_p)
8594 /* If we refer to this type via sig8, use AT_signature. */
8595 stub = new_die (type->die_tag, cu, NULL_TREE);
8596 add_AT_die_ref (stub, DW_AT_signature, type);
8598 else
8600 /* Otherwise, use a typedef with no name. */
8601 stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
8602 add_AT_die_ref (stub, DW_AT_type, type);
8605 stub->die_mark++;
8606 ref_p->stub = stub;
8608 return 1;
8611 /* DIE is a unit; look through all the DIE references to see if there are
8612 any external references to types, and if so, create local stubs for
8613 them which will be applied in build_abbrev_table. This is useful because
8614 references to local DIEs are smaller. */
8616 static external_ref_hash_type *
8617 optimize_external_refs (dw_die_ref die)
8619 external_ref_hash_type *map = new external_ref_hash_type (10);
8620 optimize_external_refs_1 (die, map);
8621 map->traverse <dw_die_ref, dwarf2_build_local_stub> (die);
8622 return map;
8625 /* The following 3 variables are temporaries that are computed only during the
8626 build_abbrev_table call and used and released during the following
8627 optimize_abbrev_table call. */
8629 /* First abbrev_id that can be optimized based on usage. */
8630 static unsigned int abbrev_opt_start;
8632 /* Maximum abbrev_id of a base type plus one (we can't optimize DIEs with
8633 abbrev_id smaller than this, because they must be already sized
8634 during build_abbrev_table). */
8635 static unsigned int abbrev_opt_base_type_end;
8637 /* Vector of usage counts during build_abbrev_table. Indexed by
8638 abbrev_id - abbrev_opt_start. */
8639 static vec<unsigned int> abbrev_usage_count;
8641 /* Vector of all DIEs added with die_abbrev >= abbrev_opt_start. */
8642 static vec<dw_die_ref> sorted_abbrev_dies;
8644 /* The format of each DIE (and its attribute value pairs) is encoded in an
8645 abbreviation table. This routine builds the abbreviation table and assigns
8646 a unique abbreviation id for each abbreviation entry. The children of each
8647 die are visited recursively. */
8649 static void
8650 build_abbrev_table (dw_die_ref die, external_ref_hash_type *extern_map)
8652 unsigned int abbrev_id = 0;
8653 dw_die_ref c;
8654 dw_attr_node *a;
8655 unsigned ix;
8656 dw_die_ref abbrev;
8658 /* Scan the DIE references, and replace any that refer to
8659 DIEs from other CUs (i.e. those which are not marked) with
8660 the local stubs we built in optimize_external_refs. */
8661 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8662 if (AT_class (a) == dw_val_class_die_ref
8663 && (c = AT_ref (a))->die_mark == 0)
8665 struct external_ref *ref_p;
8666 gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
8668 ref_p = lookup_external_ref (extern_map, c);
8669 if (ref_p->stub && ref_p->stub != die)
8670 change_AT_die_ref (a, ref_p->stub);
8671 else
8672 /* We aren't changing this reference, so mark it external. */
8673 set_AT_ref_external (a, 1);
8676 FOR_EACH_VEC_SAFE_ELT (abbrev_die_table, abbrev_id, abbrev)
8678 dw_attr_node *die_a, *abbrev_a;
8679 unsigned ix;
8680 bool ok = true;
8682 if (abbrev_id == 0)
8683 continue;
8684 if (abbrev->die_tag != die->die_tag)
8685 continue;
8686 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
8687 continue;
8689 if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
8690 continue;
8692 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
8694 abbrev_a = &(*abbrev->die_attr)[ix];
8695 if ((abbrev_a->dw_attr != die_a->dw_attr)
8696 || (value_format (abbrev_a) != value_format (die_a)))
8698 ok = false;
8699 break;
8702 if (ok)
8703 break;
8706 if (abbrev_id >= vec_safe_length (abbrev_die_table))
8708 vec_safe_push (abbrev_die_table, die);
8709 if (abbrev_opt_start)
8710 abbrev_usage_count.safe_push (0);
8712 if (abbrev_opt_start && abbrev_id >= abbrev_opt_start)
8714 abbrev_usage_count[abbrev_id - abbrev_opt_start]++;
8715 sorted_abbrev_dies.safe_push (die);
8718 die->die_abbrev = abbrev_id;
8719 FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
8722 /* Callback function for sorted_abbrev_dies vector sorting. We sort
8723 by die_abbrev's usage count, from the most commonly used
8724 abbreviation to the least. */
8726 static int
8727 die_abbrev_cmp (const void *p1, const void *p2)
8729 dw_die_ref die1 = *(const dw_die_ref *) p1;
8730 dw_die_ref die2 = *(const dw_die_ref *) p2;
8732 gcc_checking_assert (die1->die_abbrev >= abbrev_opt_start);
8733 gcc_checking_assert (die2->die_abbrev >= abbrev_opt_start);
8735 if (die1->die_abbrev >= abbrev_opt_base_type_end
8736 && die2->die_abbrev >= abbrev_opt_base_type_end)
8738 if (abbrev_usage_count[die1->die_abbrev - abbrev_opt_start]
8739 > abbrev_usage_count[die2->die_abbrev - abbrev_opt_start])
8740 return -1;
8741 if (abbrev_usage_count[die1->die_abbrev - abbrev_opt_start]
8742 < abbrev_usage_count[die2->die_abbrev - abbrev_opt_start])
8743 return 1;
8746 /* Stabilize the sort. */
8747 if (die1->die_abbrev < die2->die_abbrev)
8748 return -1;
8749 if (die1->die_abbrev > die2->die_abbrev)
8750 return 1;
8752 return 0;
8755 /* Convert dw_val_class_const and dw_val_class_unsigned_const class attributes
8756 of DIEs in between sorted_abbrev_dies[first_id] and abbrev_dies[end_id - 1]
8757 into dw_val_class_const_implicit or
8758 dw_val_class_unsigned_const_implicit. */
8760 static void
8761 optimize_implicit_const (unsigned int first_id, unsigned int end,
8762 vec<bool> &implicit_consts)
8764 /* It never makes sense if there is just one DIE using the abbreviation. */
8765 if (end < first_id + 2)
8766 return;
8768 dw_attr_node *a;
8769 unsigned ix, i;
8770 dw_die_ref die = sorted_abbrev_dies[first_id];
8771 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8772 if (implicit_consts[ix])
8774 enum dw_val_class new_class = dw_val_class_none;
8775 switch (AT_class (a))
8777 case dw_val_class_unsigned_const:
8778 if ((HOST_WIDE_INT) AT_unsigned (a) < 0)
8779 continue;
8781 /* The .debug_abbrev section will grow by
8782 size_of_sleb128 (AT_unsigned (a)) and we avoid the constants
8783 in all the DIEs using that abbreviation. */
8784 if (constant_size (AT_unsigned (a)) * (end - first_id)
8785 <= (unsigned) size_of_sleb128 (AT_unsigned (a)))
8786 continue;
8788 new_class = dw_val_class_unsigned_const_implicit;
8789 break;
8791 case dw_val_class_const:
8792 new_class = dw_val_class_const_implicit;
8793 break;
8795 case dw_val_class_file:
8796 new_class = dw_val_class_file_implicit;
8797 break;
8799 default:
8800 continue;
8802 for (i = first_id; i < end; i++)
8803 (*sorted_abbrev_dies[i]->die_attr)[ix].dw_attr_val.val_class
8804 = new_class;
8808 /* Attempt to optimize abbreviation table from abbrev_opt_start
8809 abbreviation above. */
8811 static void
8812 optimize_abbrev_table (void)
8814 if (abbrev_opt_start
8815 && vec_safe_length (abbrev_die_table) > abbrev_opt_start
8816 && (dwarf_version >= 5 || vec_safe_length (abbrev_die_table) > 127))
8818 auto_vec<bool, 32> implicit_consts;
8819 sorted_abbrev_dies.qsort (die_abbrev_cmp);
8821 unsigned int abbrev_id = abbrev_opt_start - 1;
8822 unsigned int first_id = ~0U;
8823 unsigned int last_abbrev_id = 0;
8824 unsigned int i;
8825 dw_die_ref die;
8826 if (abbrev_opt_base_type_end > abbrev_opt_start)
8827 abbrev_id = abbrev_opt_base_type_end - 1;
8828 /* Reassign abbreviation ids from abbrev_opt_start above, so that
8829 most commonly used abbreviations come first. */
8830 FOR_EACH_VEC_ELT (sorted_abbrev_dies, i, die)
8832 dw_attr_node *a;
8833 unsigned ix;
8835 /* If calc_base_type_die_sizes has been called, the CU and
8836 base types after it can't be optimized, because we've already
8837 calculated their DIE offsets. We've sorted them first. */
8838 if (die->die_abbrev < abbrev_opt_base_type_end)
8839 continue;
8840 if (die->die_abbrev != last_abbrev_id)
8842 last_abbrev_id = die->die_abbrev;
8843 if (dwarf_version >= 5 && first_id != ~0U)
8844 optimize_implicit_const (first_id, i, implicit_consts);
8845 abbrev_id++;
8846 (*abbrev_die_table)[abbrev_id] = die;
8847 if (dwarf_version >= 5)
8849 first_id = i;
8850 implicit_consts.truncate (0);
8852 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8853 switch (AT_class (a))
8855 case dw_val_class_const:
8856 case dw_val_class_unsigned_const:
8857 case dw_val_class_file:
8858 implicit_consts.safe_push (true);
8859 break;
8860 default:
8861 implicit_consts.safe_push (false);
8862 break;
8866 else if (dwarf_version >= 5)
8868 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8869 if (!implicit_consts[ix])
8870 continue;
8871 else
8873 dw_attr_node *other_a
8874 = &(*(*abbrev_die_table)[abbrev_id]->die_attr)[ix];
8875 if (!dw_val_equal_p (&a->dw_attr_val,
8876 &other_a->dw_attr_val))
8877 implicit_consts[ix] = false;
8880 die->die_abbrev = abbrev_id;
8882 gcc_assert (abbrev_id == vec_safe_length (abbrev_die_table) - 1);
8883 if (dwarf_version >= 5 && first_id != ~0U)
8884 optimize_implicit_const (first_id, i, implicit_consts);
8887 abbrev_opt_start = 0;
8888 abbrev_opt_base_type_end = 0;
8889 abbrev_usage_count.release ();
8890 sorted_abbrev_dies.release ();
8893 /* Return the power-of-two number of bytes necessary to represent VALUE. */
8895 static int
8896 constant_size (unsigned HOST_WIDE_INT value)
8898 int log;
8900 if (value == 0)
8901 log = 0;
8902 else
8903 log = floor_log2 (value);
8905 log = log / 8;
8906 log = 1 << (floor_log2 (log) + 1);
8908 return log;
8911 /* Return the size of a DIE as it is represented in the
8912 .debug_info section. */
8914 static unsigned long
8915 size_of_die (dw_die_ref die)
8917 unsigned long size = 0;
8918 dw_attr_node *a;
8919 unsigned ix;
8920 enum dwarf_form form;
8922 size += size_of_uleb128 (die->die_abbrev);
8923 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8925 switch (AT_class (a))
8927 case dw_val_class_addr:
8928 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8930 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8931 size += size_of_uleb128 (AT_index (a));
8933 else
8934 size += DWARF2_ADDR_SIZE;
8935 break;
8936 case dw_val_class_offset:
8937 size += DWARF_OFFSET_SIZE;
8938 break;
8939 case dw_val_class_loc:
8941 unsigned long lsize = size_of_locs (AT_loc (a));
8943 /* Block length. */
8944 if (dwarf_version >= 4)
8945 size += size_of_uleb128 (lsize);
8946 else
8947 size += constant_size (lsize);
8948 size += lsize;
8950 break;
8951 case dw_val_class_loc_list:
8952 if (dwarf_split_debug_info && dwarf_version >= 5)
8954 gcc_assert (AT_loc_list (a)->num_assigned);
8955 size += size_of_uleb128 (AT_loc_list (a)->hash);
8957 else
8958 size += DWARF_OFFSET_SIZE;
8959 break;
8960 case dw_val_class_range_list:
8961 if (value_format (a) == DW_FORM_rnglistx)
8963 gcc_assert (rnglist_idx);
8964 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
8965 size += size_of_uleb128 (r->idx);
8967 else
8968 size += DWARF_OFFSET_SIZE;
8969 break;
8970 case dw_val_class_const:
8971 size += size_of_sleb128 (AT_int (a));
8972 break;
8973 case dw_val_class_unsigned_const:
8975 int csize = constant_size (AT_unsigned (a));
8976 if (dwarf_version == 3
8977 && a->dw_attr == DW_AT_data_member_location
8978 && csize >= 4)
8979 size += size_of_uleb128 (AT_unsigned (a));
8980 else
8981 size += csize;
8983 break;
8984 case dw_val_class_const_implicit:
8985 case dw_val_class_unsigned_const_implicit:
8986 case dw_val_class_file_implicit:
8987 /* These occupy no size in the DIE, just an extra sleb128 in
8988 .debug_abbrev. */
8989 break;
8990 case dw_val_class_const_double:
8991 size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
8992 if (HOST_BITS_PER_WIDE_INT >= DWARF_LARGEST_DATA_FORM_BITS)
8993 size++; /* block */
8994 break;
8995 case dw_val_class_wide_int:
8996 size += (get_full_len (*a->dw_attr_val.v.val_wide)
8997 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
8998 if (get_full_len (*a->dw_attr_val.v.val_wide)
8999 * HOST_BITS_PER_WIDE_INT > DWARF_LARGEST_DATA_FORM_BITS)
9000 size++; /* block */
9001 break;
9002 case dw_val_class_vec:
9003 size += constant_size (a->dw_attr_val.v.val_vec.length
9004 * a->dw_attr_val.v.val_vec.elt_size)
9005 + a->dw_attr_val.v.val_vec.length
9006 * a->dw_attr_val.v.val_vec.elt_size; /* block */
9007 break;
9008 case dw_val_class_flag:
9009 if (dwarf_version >= 4)
9010 /* Currently all add_AT_flag calls pass in 1 as last argument,
9011 so DW_FORM_flag_present can be used. If that ever changes,
9012 we'll need to use DW_FORM_flag and have some optimization
9013 in build_abbrev_table that will change those to
9014 DW_FORM_flag_present if it is set to 1 in all DIEs using
9015 the same abbrev entry. */
9016 gcc_assert (a->dw_attr_val.v.val_flag == 1);
9017 else
9018 size += 1;
9019 break;
9020 case dw_val_class_die_ref:
9021 if (AT_ref_external (a))
9023 /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
9024 we use DW_FORM_ref_addr. In DWARF2, DW_FORM_ref_addr
9025 is sized by target address length, whereas in DWARF3
9026 it's always sized as an offset. */
9027 if (use_debug_types)
9028 size += DWARF_TYPE_SIGNATURE_SIZE;
9029 else if (dwarf_version == 2)
9030 size += DWARF2_ADDR_SIZE;
9031 else
9032 size += DWARF_OFFSET_SIZE;
9034 else
9035 size += DWARF_OFFSET_SIZE;
9036 break;
9037 case dw_val_class_fde_ref:
9038 size += DWARF_OFFSET_SIZE;
9039 break;
9040 case dw_val_class_lbl_id:
9041 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
9043 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
9044 size += size_of_uleb128 (AT_index (a));
9046 else
9047 size += DWARF2_ADDR_SIZE;
9048 break;
9049 case dw_val_class_lineptr:
9050 case dw_val_class_macptr:
9051 case dw_val_class_loclistsptr:
9052 size += DWARF_OFFSET_SIZE;
9053 break;
9054 case dw_val_class_str:
9055 form = AT_string_form (a);
9056 if (form == DW_FORM_strp || form == DW_FORM_line_strp)
9057 size += DWARF_OFFSET_SIZE;
9058 else if (form == DW_FORM_GNU_str_index)
9059 size += size_of_uleb128 (AT_index (a));
9060 else
9061 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
9062 break;
9063 case dw_val_class_file:
9064 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
9065 break;
9066 case dw_val_class_data8:
9067 size += 8;
9068 break;
9069 case dw_val_class_vms_delta:
9070 size += DWARF_OFFSET_SIZE;
9071 break;
9072 case dw_val_class_high_pc:
9073 size += DWARF2_ADDR_SIZE;
9074 break;
9075 case dw_val_class_discr_value:
9076 size += size_of_discr_value (&a->dw_attr_val.v.val_discr_value);
9077 break;
9078 case dw_val_class_discr_list:
9080 unsigned block_size = size_of_discr_list (AT_discr_list (a));
9082 /* This is a block, so we have the block length and then its
9083 data. */
9084 size += constant_size (block_size) + block_size;
9086 break;
9087 default:
9088 gcc_unreachable ();
9092 return size;
9095 /* Size the debugging information associated with a given DIE. Visits the
9096 DIE's children recursively. Updates the global variable next_die_offset, on
9097 each time through. Uses the current value of next_die_offset to update the
9098 die_offset field in each DIE. */
9100 static void
9101 calc_die_sizes (dw_die_ref die)
9103 dw_die_ref c;
9105 gcc_assert (die->die_offset == 0
9106 || (unsigned long int) die->die_offset == next_die_offset);
9107 die->die_offset = next_die_offset;
9108 next_die_offset += size_of_die (die);
9110 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
9112 if (die->die_child != NULL)
9113 /* Count the null byte used to terminate sibling lists. */
9114 next_die_offset += 1;
9117 /* Size just the base type children at the start of the CU.
9118 This is needed because build_abbrev needs to size locs
9119 and sizing of type based stack ops needs to know die_offset
9120 values for the base types. */
9122 static void
9123 calc_base_type_die_sizes (void)
9125 unsigned long die_offset = (dwarf_split_debug_info
9126 ? DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
9127 : DWARF_COMPILE_UNIT_HEADER_SIZE);
9128 unsigned int i;
9129 dw_die_ref base_type;
9130 #if ENABLE_ASSERT_CHECKING
9131 dw_die_ref prev = comp_unit_die ()->die_child;
9132 #endif
9134 die_offset += size_of_die (comp_unit_die ());
9135 for (i = 0; base_types.iterate (i, &base_type); i++)
9137 #if ENABLE_ASSERT_CHECKING
9138 gcc_assert (base_type->die_offset == 0
9139 && prev->die_sib == base_type
9140 && base_type->die_child == NULL
9141 && base_type->die_abbrev);
9142 prev = base_type;
9143 #endif
9144 if (abbrev_opt_start
9145 && base_type->die_abbrev >= abbrev_opt_base_type_end)
9146 abbrev_opt_base_type_end = base_type->die_abbrev + 1;
9147 base_type->die_offset = die_offset;
9148 die_offset += size_of_die (base_type);
9152 /* Set the marks for a die and its children. We do this so
9153 that we know whether or not a reference needs to use FORM_ref_addr; only
9154 DIEs in the same CU will be marked. We used to clear out the offset
9155 and use that as the flag, but ran into ordering problems. */
9157 static void
9158 mark_dies (dw_die_ref die)
9160 dw_die_ref c;
9162 gcc_assert (!die->die_mark);
9164 die->die_mark = 1;
9165 FOR_EACH_CHILD (die, c, mark_dies (c));
9168 /* Clear the marks for a die and its children. */
9170 static void
9171 unmark_dies (dw_die_ref die)
9173 dw_die_ref c;
9175 if (! use_debug_types)
9176 gcc_assert (die->die_mark);
9178 die->die_mark = 0;
9179 FOR_EACH_CHILD (die, c, unmark_dies (c));
9182 /* Clear the marks for a die, its children and referred dies. */
9184 static void
9185 unmark_all_dies (dw_die_ref die)
9187 dw_die_ref c;
9188 dw_attr_node *a;
9189 unsigned ix;
9191 if (!die->die_mark)
9192 return;
9193 die->die_mark = 0;
9195 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
9197 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9198 if (AT_class (a) == dw_val_class_die_ref)
9199 unmark_all_dies (AT_ref (a));
9202 /* Calculate if the entry should appear in the final output file. It may be
9203 from a pruned a type. */
9205 static bool
9206 include_pubname_in_output (vec<pubname_entry, va_gc> *table, pubname_entry *p)
9208 /* By limiting gnu pubnames to definitions only, gold can generate a
9209 gdb index without entries for declarations, which don't include
9210 enough information to be useful. */
9211 if (debug_generate_pub_sections == 2 && is_declaration_die (p->die))
9212 return false;
9214 if (table == pubname_table)
9216 /* Enumerator names are part of the pubname table, but the
9217 parent DW_TAG_enumeration_type die may have been pruned.
9218 Don't output them if that is the case. */
9219 if (p->die->die_tag == DW_TAG_enumerator &&
9220 (p->die->die_parent == NULL
9221 || !p->die->die_parent->die_perennial_p))
9222 return false;
9224 /* Everything else in the pubname table is included. */
9225 return true;
9228 /* The pubtypes table shouldn't include types that have been
9229 pruned. */
9230 return (p->die->die_offset != 0
9231 || !flag_eliminate_unused_debug_types);
9234 /* Return the size of the .debug_pubnames or .debug_pubtypes table
9235 generated for the compilation unit. */
9237 static unsigned long
9238 size_of_pubnames (vec<pubname_entry, va_gc> *names)
9240 unsigned long size;
9241 unsigned i;
9242 pubname_entry *p;
9243 int space_for_flags = (debug_generate_pub_sections == 2) ? 1 : 0;
9245 size = DWARF_PUBNAMES_HEADER_SIZE;
9246 FOR_EACH_VEC_ELT (*names, i, p)
9247 if (include_pubname_in_output (names, p))
9248 size += strlen (p->name) + DWARF_OFFSET_SIZE + 1 + space_for_flags;
9250 size += DWARF_OFFSET_SIZE;
9251 return size;
9254 /* Return the size of the information in the .debug_aranges section. */
9256 static unsigned long
9257 size_of_aranges (void)
9259 unsigned long size;
9261 size = DWARF_ARANGES_HEADER_SIZE;
9263 /* Count the address/length pair for this compilation unit. */
9264 if (text_section_used)
9265 size += 2 * DWARF2_ADDR_SIZE;
9266 if (cold_text_section_used)
9267 size += 2 * DWARF2_ADDR_SIZE;
9268 if (have_multiple_function_sections)
9270 unsigned fde_idx;
9271 dw_fde_ref fde;
9273 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
9275 if (DECL_IGNORED_P (fde->decl))
9276 continue;
9277 if (!fde->in_std_section)
9278 size += 2 * DWARF2_ADDR_SIZE;
9279 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
9280 size += 2 * DWARF2_ADDR_SIZE;
9284 /* Count the two zero words used to terminated the address range table. */
9285 size += 2 * DWARF2_ADDR_SIZE;
9286 return size;
9289 /* Select the encoding of an attribute value. */
9291 static enum dwarf_form
9292 value_format (dw_attr_node *a)
9294 switch (AT_class (a))
9296 case dw_val_class_addr:
9297 /* Only very few attributes allow DW_FORM_addr. */
9298 switch (a->dw_attr)
9300 case DW_AT_low_pc:
9301 case DW_AT_high_pc:
9302 case DW_AT_entry_pc:
9303 case DW_AT_trampoline:
9304 return (AT_index (a) == NOT_INDEXED
9305 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
9306 default:
9307 break;
9309 switch (DWARF2_ADDR_SIZE)
9311 case 1:
9312 return DW_FORM_data1;
9313 case 2:
9314 return DW_FORM_data2;
9315 case 4:
9316 return DW_FORM_data4;
9317 case 8:
9318 return DW_FORM_data8;
9319 default:
9320 gcc_unreachable ();
9322 case dw_val_class_loc_list:
9323 if (dwarf_split_debug_info
9324 && dwarf_version >= 5
9325 && AT_loc_list (a)->num_assigned)
9326 return DW_FORM_loclistx;
9327 /* FALLTHRU */
9328 case dw_val_class_range_list:
9329 /* For range lists in DWARF 5, use DW_FORM_rnglistx from .debug_info.dwo
9330 but in .debug_info use DW_FORM_sec_offset, which is shorter if we
9331 care about sizes of .debug* sections in shared libraries and
9332 executables and don't take into account relocations that affect just
9333 relocatable objects - for DW_FORM_rnglistx we'd have to emit offset
9334 table in the .debug_rnglists section. */
9335 if (dwarf_split_debug_info
9336 && dwarf_version >= 5
9337 && AT_class (a) == dw_val_class_range_list
9338 && rnglist_idx
9339 && a->dw_attr_val.val_entry != RELOCATED_OFFSET)
9340 return DW_FORM_rnglistx;
9341 if (dwarf_version >= 4)
9342 return DW_FORM_sec_offset;
9343 /* FALLTHRU */
9344 case dw_val_class_vms_delta:
9345 case dw_val_class_offset:
9346 switch (DWARF_OFFSET_SIZE)
9348 case 4:
9349 return DW_FORM_data4;
9350 case 8:
9351 return DW_FORM_data8;
9352 default:
9353 gcc_unreachable ();
9355 case dw_val_class_loc:
9356 if (dwarf_version >= 4)
9357 return DW_FORM_exprloc;
9358 switch (constant_size (size_of_locs (AT_loc (a))))
9360 case 1:
9361 return DW_FORM_block1;
9362 case 2:
9363 return DW_FORM_block2;
9364 case 4:
9365 return DW_FORM_block4;
9366 default:
9367 gcc_unreachable ();
9369 case dw_val_class_const:
9370 return DW_FORM_sdata;
9371 case dw_val_class_unsigned_const:
9372 switch (constant_size (AT_unsigned (a)))
9374 case 1:
9375 return DW_FORM_data1;
9376 case 2:
9377 return DW_FORM_data2;
9378 case 4:
9379 /* In DWARF3 DW_AT_data_member_location with
9380 DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
9381 constant, so we need to use DW_FORM_udata if we need
9382 a large constant. */
9383 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
9384 return DW_FORM_udata;
9385 return DW_FORM_data4;
9386 case 8:
9387 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
9388 return DW_FORM_udata;
9389 return DW_FORM_data8;
9390 default:
9391 gcc_unreachable ();
9393 case dw_val_class_const_implicit:
9394 case dw_val_class_unsigned_const_implicit:
9395 case dw_val_class_file_implicit:
9396 return DW_FORM_implicit_const;
9397 case dw_val_class_const_double:
9398 switch (HOST_BITS_PER_WIDE_INT)
9400 case 8:
9401 return DW_FORM_data2;
9402 case 16:
9403 return DW_FORM_data4;
9404 case 32:
9405 return DW_FORM_data8;
9406 case 64:
9407 if (dwarf_version >= 5)
9408 return DW_FORM_data16;
9409 /* FALLTHRU */
9410 default:
9411 return DW_FORM_block1;
9413 case dw_val_class_wide_int:
9414 switch (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT)
9416 case 8:
9417 return DW_FORM_data1;
9418 case 16:
9419 return DW_FORM_data2;
9420 case 32:
9421 return DW_FORM_data4;
9422 case 64:
9423 return DW_FORM_data8;
9424 case 128:
9425 if (dwarf_version >= 5)
9426 return DW_FORM_data16;
9427 /* FALLTHRU */
9428 default:
9429 return DW_FORM_block1;
9431 case dw_val_class_vec:
9432 switch (constant_size (a->dw_attr_val.v.val_vec.length
9433 * a->dw_attr_val.v.val_vec.elt_size))
9435 case 1:
9436 return DW_FORM_block1;
9437 case 2:
9438 return DW_FORM_block2;
9439 case 4:
9440 return DW_FORM_block4;
9441 default:
9442 gcc_unreachable ();
9444 case dw_val_class_flag:
9445 if (dwarf_version >= 4)
9447 /* Currently all add_AT_flag calls pass in 1 as last argument,
9448 so DW_FORM_flag_present can be used. If that ever changes,
9449 we'll need to use DW_FORM_flag and have some optimization
9450 in build_abbrev_table that will change those to
9451 DW_FORM_flag_present if it is set to 1 in all DIEs using
9452 the same abbrev entry. */
9453 gcc_assert (a->dw_attr_val.v.val_flag == 1);
9454 return DW_FORM_flag_present;
9456 return DW_FORM_flag;
9457 case dw_val_class_die_ref:
9458 if (AT_ref_external (a))
9459 return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
9460 else
9461 return DW_FORM_ref;
9462 case dw_val_class_fde_ref:
9463 return DW_FORM_data;
9464 case dw_val_class_lbl_id:
9465 return (AT_index (a) == NOT_INDEXED
9466 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
9467 case dw_val_class_lineptr:
9468 case dw_val_class_macptr:
9469 case dw_val_class_loclistsptr:
9470 return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
9471 case dw_val_class_str:
9472 return AT_string_form (a);
9473 case dw_val_class_file:
9474 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
9476 case 1:
9477 return DW_FORM_data1;
9478 case 2:
9479 return DW_FORM_data2;
9480 case 4:
9481 return DW_FORM_data4;
9482 default:
9483 gcc_unreachable ();
9486 case dw_val_class_data8:
9487 return DW_FORM_data8;
9489 case dw_val_class_high_pc:
9490 switch (DWARF2_ADDR_SIZE)
9492 case 1:
9493 return DW_FORM_data1;
9494 case 2:
9495 return DW_FORM_data2;
9496 case 4:
9497 return DW_FORM_data4;
9498 case 8:
9499 return DW_FORM_data8;
9500 default:
9501 gcc_unreachable ();
9504 case dw_val_class_discr_value:
9505 return (a->dw_attr_val.v.val_discr_value.pos
9506 ? DW_FORM_udata
9507 : DW_FORM_sdata);
9508 case dw_val_class_discr_list:
9509 switch (constant_size (size_of_discr_list (AT_discr_list (a))))
9511 case 1:
9512 return DW_FORM_block1;
9513 case 2:
9514 return DW_FORM_block2;
9515 case 4:
9516 return DW_FORM_block4;
9517 default:
9518 gcc_unreachable ();
9521 default:
9522 gcc_unreachable ();
9526 /* Output the encoding of an attribute value. */
9528 static void
9529 output_value_format (dw_attr_node *a)
9531 enum dwarf_form form = value_format (a);
9533 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
9536 /* Given a die and id, produce the appropriate abbreviations. */
9538 static void
9539 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
9541 unsigned ix;
9542 dw_attr_node *a_attr;
9544 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
9545 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
9546 dwarf_tag_name (abbrev->die_tag));
9548 if (abbrev->die_child != NULL)
9549 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
9550 else
9551 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
9553 for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
9555 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
9556 dwarf_attr_name (a_attr->dw_attr));
9557 output_value_format (a_attr);
9558 if (value_format (a_attr) == DW_FORM_implicit_const)
9560 if (AT_class (a_attr) == dw_val_class_file_implicit)
9562 int f = maybe_emit_file (a_attr->dw_attr_val.v.val_file);
9563 const char *filename = a_attr->dw_attr_val.v.val_file->filename;
9564 dw2_asm_output_data_sleb128 (f, "(%s)", filename);
9566 else
9567 dw2_asm_output_data_sleb128 (a_attr->dw_attr_val.v.val_int, NULL);
9571 dw2_asm_output_data (1, 0, NULL);
9572 dw2_asm_output_data (1, 0, NULL);
9576 /* Output the .debug_abbrev section which defines the DIE abbreviation
9577 table. */
9579 static void
9580 output_abbrev_section (void)
9582 unsigned int abbrev_id;
9583 dw_die_ref abbrev;
9585 FOR_EACH_VEC_SAFE_ELT (abbrev_die_table, abbrev_id, abbrev)
9586 if (abbrev_id != 0)
9587 output_die_abbrevs (abbrev_id, abbrev);
9589 /* Terminate the table. */
9590 dw2_asm_output_data (1, 0, NULL);
9593 /* Output a symbol we can use to refer to this DIE from another CU. */
9595 static inline void
9596 output_die_symbol (dw_die_ref die)
9598 const char *sym = die->die_id.die_symbol;
9600 gcc_assert (!die->comdat_type_p);
9602 if (sym == 0)
9603 return;
9605 if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
9606 /* We make these global, not weak; if the target doesn't support
9607 .linkonce, it doesn't support combining the sections, so debugging
9608 will break. */
9609 targetm.asm_out.globalize_label (asm_out_file, sym);
9611 ASM_OUTPUT_LABEL (asm_out_file, sym);
9614 /* Return a new location list, given the begin and end range, and the
9615 expression. */
9617 static inline dw_loc_list_ref
9618 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
9619 const char *section)
9621 dw_loc_list_ref retlist = ggc_cleared_alloc<dw_loc_list_node> ();
9623 retlist->begin = begin;
9624 retlist->begin_entry = NULL;
9625 retlist->end = end;
9626 retlist->expr = expr;
9627 retlist->section = section;
9629 return retlist;
9632 /* Generate a new internal symbol for this location list node, if it
9633 hasn't got one yet. */
9635 static inline void
9636 gen_llsym (dw_loc_list_ref list)
9638 gcc_assert (!list->ll_symbol);
9639 list->ll_symbol = gen_internal_sym ("LLST");
9642 /* Output the location list given to us. */
9644 static void
9645 output_loc_list (dw_loc_list_ref list_head)
9647 if (list_head->emitted)
9648 return;
9649 list_head->emitted = true;
9651 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
9653 dw_loc_list_ref curr = list_head;
9654 const char *last_section = NULL;
9655 const char *base_label = NULL;
9657 /* Walk the location list, and output each range + expression. */
9658 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
9660 unsigned long size;
9661 /* Don't output an entry that starts and ends at the same address. */
9662 if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
9663 continue;
9664 size = size_of_locs (curr->expr);
9665 /* If the expression is too large, drop it on the floor. We could
9666 perhaps put it into DW_TAG_dwarf_procedure and refer to that
9667 in the expression, but >= 64KB expressions for a single value
9668 in a single range are unlikely very useful. */
9669 if (dwarf_version < 5 && size > 0xffff)
9670 continue;
9671 if (dwarf_version >= 5)
9673 if (dwarf_split_debug_info)
9675 /* For -gsplit-dwarf, emit DW_LLE_starx_length, which has
9676 uleb128 index into .debug_addr and uleb128 length. */
9677 dw2_asm_output_data (1, DW_LLE_startx_length,
9678 "DW_LLE_startx_length (%s)",
9679 list_head->ll_symbol);
9680 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
9681 "Location list range start index "
9682 "(%s)", curr->begin);
9683 /* FIXME: This will ICE ifndef HAVE_AS_LEB128.
9684 For that case we probably need to emit DW_LLE_startx_endx,
9685 but we'd need 2 .debug_addr entries rather than just one. */
9686 dw2_asm_output_delta_uleb128 (curr->end, curr->begin,
9687 "Location list length (%s)",
9688 list_head->ll_symbol);
9690 else if (!have_multiple_function_sections && HAVE_AS_LEB128)
9692 /* If all code is in .text section, the base address is
9693 already provided by the CU attributes. Use
9694 DW_LLE_offset_pair where both addresses are uleb128 encoded
9695 offsets against that base. */
9696 dw2_asm_output_data (1, DW_LLE_offset_pair,
9697 "DW_LLE_offset_pair (%s)",
9698 list_head->ll_symbol);
9699 dw2_asm_output_delta_uleb128 (curr->begin, curr->section,
9700 "Location list begin address (%s)",
9701 list_head->ll_symbol);
9702 dw2_asm_output_delta_uleb128 (curr->end, curr->section,
9703 "Location list end address (%s)",
9704 list_head->ll_symbol);
9706 else if (HAVE_AS_LEB128)
9708 /* Otherwise, find out how many consecutive entries could share
9709 the same base entry. If just one, emit DW_LLE_start_length,
9710 otherwise emit DW_LLE_base_address for the base address
9711 followed by a series of DW_LLE_offset_pair. */
9712 if (last_section == NULL || curr->section != last_section)
9714 dw_loc_list_ref curr2;
9715 for (curr2 = curr->dw_loc_next; curr2 != NULL;
9716 curr2 = curr2->dw_loc_next)
9718 if (strcmp (curr2->begin, curr2->end) == 0
9719 && !curr2->force)
9720 continue;
9721 break;
9723 if (curr2 == NULL || curr->section != curr2->section)
9724 last_section = NULL;
9725 else
9727 last_section = curr->section;
9728 base_label = curr->begin;
9729 dw2_asm_output_data (1, DW_LLE_base_address,
9730 "DW_LLE_base_address (%s)",
9731 list_head->ll_symbol);
9732 dw2_asm_output_addr (DWARF2_ADDR_SIZE, base_label,
9733 "Base address (%s)",
9734 list_head->ll_symbol);
9737 /* Only one entry with the same base address. Use
9738 DW_LLE_start_length with absolute address and uleb128
9739 length. */
9740 if (last_section == NULL)
9742 dw2_asm_output_data (1, DW_LLE_start_length,
9743 "DW_LLE_start_length (%s)",
9744 list_head->ll_symbol);
9745 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
9746 "Location list begin address (%s)",
9747 list_head->ll_symbol);
9748 dw2_asm_output_delta_uleb128 (curr->end, curr->begin,
9749 "Location list length "
9750 "(%s)", list_head->ll_symbol);
9752 /* Otherwise emit DW_LLE_offset_pair, relative to above emitted
9753 DW_LLE_base_address. */
9754 else
9756 dw2_asm_output_data (1, DW_LLE_offset_pair,
9757 "DW_LLE_offset_pair (%s)",
9758 list_head->ll_symbol);
9759 dw2_asm_output_delta_uleb128 (curr->begin, base_label,
9760 "Location list begin address "
9761 "(%s)", list_head->ll_symbol);
9762 dw2_asm_output_delta_uleb128 (curr->end, base_label,
9763 "Location list end address "
9764 "(%s)", list_head->ll_symbol);
9767 /* The assembler does not support .uleb128 directive. Emit
9768 DW_LLE_start_end with a pair of absolute addresses. */
9769 else
9771 dw2_asm_output_data (1, DW_LLE_start_end,
9772 "DW_LLE_start_end (%s)",
9773 list_head->ll_symbol);
9774 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
9775 "Location list begin address (%s)",
9776 list_head->ll_symbol);
9777 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
9778 "Location list end address (%s)",
9779 list_head->ll_symbol);
9782 else if (dwarf_split_debug_info)
9784 /* For -gsplit-dwarf -gdwarf-{2,3,4} emit index into .debug_addr
9785 and 4 byte length. */
9786 dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
9787 "Location list start/length entry (%s)",
9788 list_head->ll_symbol);
9789 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
9790 "Location list range start index (%s)",
9791 curr->begin);
9792 /* The length field is 4 bytes. If we ever need to support
9793 an 8-byte length, we can add a new DW_LLE code or fall back
9794 to DW_LLE_GNU_start_end_entry. */
9795 dw2_asm_output_delta (4, curr->end, curr->begin,
9796 "Location list range length (%s)",
9797 list_head->ll_symbol);
9799 else if (!have_multiple_function_sections)
9801 /* Pair of relative addresses against start of text section. */
9802 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
9803 "Location list begin address (%s)",
9804 list_head->ll_symbol);
9805 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
9806 "Location list end address (%s)",
9807 list_head->ll_symbol);
9809 else
9811 /* Pair of absolute addresses. */
9812 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
9813 "Location list begin address (%s)",
9814 list_head->ll_symbol);
9815 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
9816 "Location list end address (%s)",
9817 list_head->ll_symbol);
9820 /* Output the block length for this list of location operations. */
9821 if (dwarf_version >= 5)
9822 dw2_asm_output_data_uleb128 (size, "Location expression size");
9823 else
9825 gcc_assert (size <= 0xffff);
9826 dw2_asm_output_data (2, size, "Location expression size");
9829 output_loc_sequence (curr->expr, -1);
9832 /* And finally list termination. */
9833 if (dwarf_version >= 5)
9834 dw2_asm_output_data (1, DW_LLE_end_of_list,
9835 "DW_LLE_end_of_list (%s)", list_head->ll_symbol);
9836 else if (dwarf_split_debug_info)
9837 dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
9838 "Location list terminator (%s)",
9839 list_head->ll_symbol);
9840 else
9842 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
9843 "Location list terminator begin (%s)",
9844 list_head->ll_symbol);
9845 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
9846 "Location list terminator end (%s)",
9847 list_head->ll_symbol);
9851 /* Output a range_list offset into the .debug_ranges or .debug_rnglists
9852 section. Emit a relocated reference if val_entry is NULL, otherwise,
9853 emit an indirect reference. */
9855 static void
9856 output_range_list_offset (dw_attr_node *a)
9858 const char *name = dwarf_attr_name (a->dw_attr);
9860 if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
9862 if (dwarf_version >= 5)
9864 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
9865 dw2_asm_output_offset (DWARF_OFFSET_SIZE, r->label,
9866 debug_ranges_section, "%s", name);
9868 else
9870 char *p = strchr (ranges_section_label, '\0');
9871 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
9872 a->dw_attr_val.v.val_offset * 2 * DWARF2_ADDR_SIZE);
9873 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
9874 debug_ranges_section, "%s", name);
9875 *p = '\0';
9878 else if (dwarf_version >= 5)
9880 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
9881 gcc_assert (rnglist_idx);
9882 dw2_asm_output_data_uleb128 (r->idx, "%s", name);
9884 else
9885 dw2_asm_output_data (DWARF_OFFSET_SIZE,
9886 a->dw_attr_val.v.val_offset * 2 * DWARF2_ADDR_SIZE,
9887 "%s (offset from %s)", name, ranges_section_label);
9890 /* Output the offset into the debug_loc section. */
9892 static void
9893 output_loc_list_offset (dw_attr_node *a)
9895 char *sym = AT_loc_list (a)->ll_symbol;
9897 gcc_assert (sym);
9898 if (!dwarf_split_debug_info)
9899 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
9900 "%s", dwarf_attr_name (a->dw_attr));
9901 else if (dwarf_version >= 5)
9903 gcc_assert (AT_loc_list (a)->num_assigned);
9904 dw2_asm_output_data_uleb128 (AT_loc_list (a)->hash, "%s (%s)",
9905 dwarf_attr_name (a->dw_attr),
9906 sym);
9908 else
9909 dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
9910 "%s", dwarf_attr_name (a->dw_attr));
9913 /* Output an attribute's index or value appropriately. */
9915 static void
9916 output_attr_index_or_value (dw_attr_node *a)
9918 const char *name = dwarf_attr_name (a->dw_attr);
9920 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
9922 dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
9923 return;
9925 switch (AT_class (a))
9927 case dw_val_class_addr:
9928 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
9929 break;
9930 case dw_val_class_high_pc:
9931 case dw_val_class_lbl_id:
9932 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
9933 break;
9934 default:
9935 gcc_unreachable ();
9939 /* Output a type signature. */
9941 static inline void
9942 output_signature (const char *sig, const char *name)
9944 int i;
9946 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9947 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
9950 /* Output a discriminant value. */
9952 static inline void
9953 output_discr_value (dw_discr_value *discr_value, const char *name)
9955 if (discr_value->pos)
9956 dw2_asm_output_data_uleb128 (discr_value->v.uval, "%s", name);
9957 else
9958 dw2_asm_output_data_sleb128 (discr_value->v.sval, "%s", name);
9961 /* Output the DIE and its attributes. Called recursively to generate
9962 the definitions of each child DIE. */
9964 static void
9965 output_die (dw_die_ref die)
9967 dw_attr_node *a;
9968 dw_die_ref c;
9969 unsigned long size;
9970 unsigned ix;
9972 /* If someone in another CU might refer to us, set up a symbol for
9973 them to point to. */
9974 if (! die->comdat_type_p && die->die_id.die_symbol)
9975 output_die_symbol (die);
9977 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
9978 (unsigned long)die->die_offset,
9979 dwarf_tag_name (die->die_tag));
9981 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9983 const char *name = dwarf_attr_name (a->dw_attr);
9985 switch (AT_class (a))
9987 case dw_val_class_addr:
9988 output_attr_index_or_value (a);
9989 break;
9991 case dw_val_class_offset:
9992 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
9993 "%s", name);
9994 break;
9996 case dw_val_class_range_list:
9997 output_range_list_offset (a);
9998 break;
10000 case dw_val_class_loc:
10001 size = size_of_locs (AT_loc (a));
10003 /* Output the block length for this list of location operations. */
10004 if (dwarf_version >= 4)
10005 dw2_asm_output_data_uleb128 (size, "%s", name);
10006 else
10007 dw2_asm_output_data (constant_size (size), size, "%s", name);
10009 output_loc_sequence (AT_loc (a), -1);
10010 break;
10012 case dw_val_class_const:
10013 /* ??? It would be slightly more efficient to use a scheme like is
10014 used for unsigned constants below, but gdb 4.x does not sign
10015 extend. Gdb 5.x does sign extend. */
10016 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
10017 break;
10019 case dw_val_class_unsigned_const:
10021 int csize = constant_size (AT_unsigned (a));
10022 if (dwarf_version == 3
10023 && a->dw_attr == DW_AT_data_member_location
10024 && csize >= 4)
10025 dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
10026 else
10027 dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
10029 break;
10031 case dw_val_class_const_implicit:
10032 if (flag_debug_asm)
10033 fprintf (asm_out_file, "\t\t\t%s %s ("
10034 HOST_WIDE_INT_PRINT_DEC ")\n",
10035 ASM_COMMENT_START, name, AT_int (a));
10036 break;
10038 case dw_val_class_unsigned_const_implicit:
10039 if (flag_debug_asm)
10040 fprintf (asm_out_file, "\t\t\t%s %s ("
10041 HOST_WIDE_INT_PRINT_HEX ")\n",
10042 ASM_COMMENT_START, name, AT_unsigned (a));
10043 break;
10045 case dw_val_class_const_double:
10047 unsigned HOST_WIDE_INT first, second;
10049 if (HOST_BITS_PER_WIDE_INT >= DWARF_LARGEST_DATA_FORM_BITS)
10050 dw2_asm_output_data (1,
10051 HOST_BITS_PER_DOUBLE_INT
10052 / HOST_BITS_PER_CHAR,
10053 NULL);
10055 if (WORDS_BIG_ENDIAN)
10057 first = a->dw_attr_val.v.val_double.high;
10058 second = a->dw_attr_val.v.val_double.low;
10060 else
10062 first = a->dw_attr_val.v.val_double.low;
10063 second = a->dw_attr_val.v.val_double.high;
10066 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
10067 first, "%s", name);
10068 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
10069 second, NULL);
10071 break;
10073 case dw_val_class_wide_int:
10075 int i;
10076 int len = get_full_len (*a->dw_attr_val.v.val_wide);
10077 int l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
10078 if (len * HOST_BITS_PER_WIDE_INT > DWARF_LARGEST_DATA_FORM_BITS)
10079 dw2_asm_output_data (1, get_full_len (*a->dw_attr_val.v.val_wide)
10080 * l, NULL);
10082 if (WORDS_BIG_ENDIAN)
10083 for (i = len - 1; i >= 0; --i)
10085 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
10086 "%s", name);
10087 name = "";
10089 else
10090 for (i = 0; i < len; ++i)
10092 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
10093 "%s", name);
10094 name = "";
10097 break;
10099 case dw_val_class_vec:
10101 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
10102 unsigned int len = a->dw_attr_val.v.val_vec.length;
10103 unsigned int i;
10104 unsigned char *p;
10106 dw2_asm_output_data (constant_size (len * elt_size),
10107 len * elt_size, "%s", name);
10108 if (elt_size > sizeof (HOST_WIDE_INT))
10110 elt_size /= 2;
10111 len *= 2;
10113 for (i = 0, p = (unsigned char *) a->dw_attr_val.v.val_vec.array;
10114 i < len;
10115 i++, p += elt_size)
10116 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
10117 "fp or vector constant word %u", i);
10118 break;
10121 case dw_val_class_flag:
10122 if (dwarf_version >= 4)
10124 /* Currently all add_AT_flag calls pass in 1 as last argument,
10125 so DW_FORM_flag_present can be used. If that ever changes,
10126 we'll need to use DW_FORM_flag and have some optimization
10127 in build_abbrev_table that will change those to
10128 DW_FORM_flag_present if it is set to 1 in all DIEs using
10129 the same abbrev entry. */
10130 gcc_assert (AT_flag (a) == 1);
10131 if (flag_debug_asm)
10132 fprintf (asm_out_file, "\t\t\t%s %s\n",
10133 ASM_COMMENT_START, name);
10134 break;
10136 dw2_asm_output_data (1, AT_flag (a), "%s", name);
10137 break;
10139 case dw_val_class_loc_list:
10140 output_loc_list_offset (a);
10141 break;
10143 case dw_val_class_die_ref:
10144 if (AT_ref_external (a))
10146 if (AT_ref (a)->comdat_type_p)
10148 comdat_type_node *type_node
10149 = AT_ref (a)->die_id.die_type_node;
10151 gcc_assert (type_node);
10152 output_signature (type_node->signature, name);
10154 else
10156 const char *sym = AT_ref (a)->die_id.die_symbol;
10157 int size;
10159 gcc_assert (sym);
10160 /* In DWARF2, DW_FORM_ref_addr is sized by target address
10161 length, whereas in DWARF3 it's always sized as an
10162 offset. */
10163 if (dwarf_version == 2)
10164 size = DWARF2_ADDR_SIZE;
10165 else
10166 size = DWARF_OFFSET_SIZE;
10167 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
10168 name);
10171 else
10173 gcc_assert (AT_ref (a)->die_offset);
10174 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
10175 "%s", name);
10177 break;
10179 case dw_val_class_fde_ref:
10181 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
10183 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
10184 a->dw_attr_val.v.val_fde_index * 2);
10185 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
10186 "%s", name);
10188 break;
10190 case dw_val_class_vms_delta:
10191 #ifdef ASM_OUTPUT_DWARF_VMS_DELTA
10192 dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
10193 AT_vms_delta2 (a), AT_vms_delta1 (a),
10194 "%s", name);
10195 #else
10196 dw2_asm_output_delta (DWARF_OFFSET_SIZE,
10197 AT_vms_delta2 (a), AT_vms_delta1 (a),
10198 "%s", name);
10199 #endif
10200 break;
10202 case dw_val_class_lbl_id:
10203 output_attr_index_or_value (a);
10204 break;
10206 case dw_val_class_lineptr:
10207 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
10208 debug_line_section, "%s", name);
10209 break;
10211 case dw_val_class_macptr:
10212 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
10213 debug_macinfo_section, "%s", name);
10214 break;
10216 case dw_val_class_loclistsptr:
10217 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
10218 debug_loc_section, "%s", name);
10219 break;
10221 case dw_val_class_str:
10222 if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
10223 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
10224 a->dw_attr_val.v.val_str->label,
10225 debug_str_section,
10226 "%s: \"%s\"", name, AT_string (a));
10227 else if (a->dw_attr_val.v.val_str->form == DW_FORM_line_strp)
10228 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
10229 a->dw_attr_val.v.val_str->label,
10230 debug_line_str_section,
10231 "%s: \"%s\"", name, AT_string (a));
10232 else if (a->dw_attr_val.v.val_str->form == DW_FORM_GNU_str_index)
10233 dw2_asm_output_data_uleb128 (AT_index (a),
10234 "%s: \"%s\"", name, AT_string (a));
10235 else
10236 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
10237 break;
10239 case dw_val_class_file:
10241 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
10243 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
10244 a->dw_attr_val.v.val_file->filename);
10245 break;
10248 case dw_val_class_file_implicit:
10249 if (flag_debug_asm)
10250 fprintf (asm_out_file, "\t\t\t%s %s (%d, %s)\n",
10251 ASM_COMMENT_START, name,
10252 maybe_emit_file (a->dw_attr_val.v.val_file),
10253 a->dw_attr_val.v.val_file->filename);
10254 break;
10256 case dw_val_class_data8:
10258 int i;
10260 for (i = 0; i < 8; i++)
10261 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
10262 i == 0 ? "%s" : NULL, name);
10263 break;
10266 case dw_val_class_high_pc:
10267 dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
10268 get_AT_low_pc (die), "DW_AT_high_pc");
10269 break;
10271 case dw_val_class_discr_value:
10272 output_discr_value (&a->dw_attr_val.v.val_discr_value, name);
10273 break;
10275 case dw_val_class_discr_list:
10277 dw_discr_list_ref list = AT_discr_list (a);
10278 const int size = size_of_discr_list (list);
10280 /* This is a block, so output its length first. */
10281 dw2_asm_output_data (constant_size (size), size,
10282 "%s: block size", name);
10284 for (; list != NULL; list = list->dw_discr_next)
10286 /* One byte for the discriminant value descriptor, and then as
10287 many LEB128 numbers as required. */
10288 if (list->dw_discr_range)
10289 dw2_asm_output_data (1, DW_DSC_range,
10290 "%s: DW_DSC_range", name);
10291 else
10292 dw2_asm_output_data (1, DW_DSC_label,
10293 "%s: DW_DSC_label", name);
10295 output_discr_value (&list->dw_discr_lower_bound, name);
10296 if (list->dw_discr_range)
10297 output_discr_value (&list->dw_discr_upper_bound, name);
10299 break;
10302 default:
10303 gcc_unreachable ();
10307 FOR_EACH_CHILD (die, c, output_die (c));
10309 /* Add null byte to terminate sibling list. */
10310 if (die->die_child != NULL)
10311 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
10312 (unsigned long) die->die_offset);
10315 /* Output the compilation unit that appears at the beginning of the
10316 .debug_info section, and precedes the DIE descriptions. */
10318 static void
10319 output_compilation_unit_header (enum dwarf_unit_type ut)
10321 if (!XCOFF_DEBUGGING_INFO)
10323 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10324 dw2_asm_output_data (4, 0xffffffff,
10325 "Initial length escape value indicating 64-bit DWARF extension");
10326 dw2_asm_output_data (DWARF_OFFSET_SIZE,
10327 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
10328 "Length of Compilation Unit Info");
10331 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
10332 if (dwarf_version >= 5)
10334 const char *name;
10335 switch (ut)
10337 case DW_UT_compile: name = "DW_UT_compile"; break;
10338 case DW_UT_type: name = "DW_UT_type"; break;
10339 case DW_UT_split_compile: name = "DW_UT_split_compile"; break;
10340 case DW_UT_split_type: name = "DW_UT_split_type"; break;
10341 default: gcc_unreachable ();
10343 dw2_asm_output_data (1, ut, "%s", name);
10344 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10346 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
10347 debug_abbrev_section,
10348 "Offset Into Abbrev. Section");
10349 if (dwarf_version < 5)
10350 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10353 /* Output the compilation unit DIE and its children. */
10355 static void
10356 output_comp_unit (dw_die_ref die, int output_if_empty,
10357 const unsigned char *dwo_id)
10359 const char *secname, *oldsym;
10360 char *tmp;
10362 /* Unless we are outputting main CU, we may throw away empty ones. */
10363 if (!output_if_empty && die->die_child == NULL)
10364 return;
10366 /* Even if there are no children of this DIE, we must output the information
10367 about the compilation unit. Otherwise, on an empty translation unit, we
10368 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
10369 will then complain when examining the file. First mark all the DIEs in
10370 this CU so we know which get local refs. */
10371 mark_dies (die);
10373 external_ref_hash_type *extern_map = optimize_external_refs (die);
10375 /* For now, optimize only the main CU, in order to optimize the rest
10376 we'd need to see all of them earlier. Leave the rest for post-linking
10377 tools like DWZ. */
10378 if (die == comp_unit_die ())
10379 abbrev_opt_start = vec_safe_length (abbrev_die_table);
10381 build_abbrev_table (die, extern_map);
10383 optimize_abbrev_table ();
10385 delete extern_map;
10387 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
10388 next_die_offset = (dwo_id
10389 ? DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
10390 : DWARF_COMPILE_UNIT_HEADER_SIZE);
10391 calc_die_sizes (die);
10393 oldsym = die->die_id.die_symbol;
10394 if (oldsym)
10396 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
10398 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
10399 secname = tmp;
10400 die->die_id.die_symbol = NULL;
10401 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
10403 else
10405 switch_to_section (debug_info_section);
10406 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
10407 info_section_emitted = true;
10410 /* Output debugging information. */
10411 output_compilation_unit_header (dwo_id
10412 ? DW_UT_split_compile : DW_UT_compile);
10413 if (dwarf_version >= 5)
10415 if (dwo_id != NULL)
10416 for (int i = 0; i < 8; i++)
10417 dw2_asm_output_data (1, dwo_id[i], i == 0 ? "DWO id" : NULL);
10419 output_die (die);
10421 /* Leave the marks on the main CU, so we can check them in
10422 output_pubnames. */
10423 if (oldsym)
10425 unmark_dies (die);
10426 die->die_id.die_symbol = oldsym;
10430 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
10431 and .debug_pubtypes. This is configured per-target, but can be
10432 overridden by the -gpubnames or -gno-pubnames options. */
10434 static inline bool
10435 want_pubnames (void)
10437 if (debug_info_level <= DINFO_LEVEL_TERSE)
10438 return false;
10439 if (debug_generate_pub_sections != -1)
10440 return debug_generate_pub_sections;
10441 return targetm.want_debug_pub_sections;
10444 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes. */
10446 static void
10447 add_AT_pubnames (dw_die_ref die)
10449 if (want_pubnames ())
10450 add_AT_flag (die, DW_AT_GNU_pubnames, 1);
10453 /* Add a string attribute value to a skeleton DIE. */
10455 static inline void
10456 add_skeleton_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
10457 const char *str)
10459 dw_attr_node attr;
10460 struct indirect_string_node *node;
10462 if (! skeleton_debug_str_hash)
10463 skeleton_debug_str_hash
10464 = hash_table<indirect_string_hasher>::create_ggc (10);
10466 node = find_AT_string_in_table (str, skeleton_debug_str_hash);
10467 find_string_form (node);
10468 if (node->form == DW_FORM_GNU_str_index)
10469 node->form = DW_FORM_strp;
10471 attr.dw_attr = attr_kind;
10472 attr.dw_attr_val.val_class = dw_val_class_str;
10473 attr.dw_attr_val.val_entry = NULL;
10474 attr.dw_attr_val.v.val_str = node;
10475 add_dwarf_attr (die, &attr);
10478 /* Helper function to generate top-level dies for skeleton debug_info and
10479 debug_types. */
10481 static void
10482 add_top_level_skeleton_die_attrs (dw_die_ref die)
10484 const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
10485 const char *comp_dir = comp_dir_string ();
10487 add_skeleton_AT_string (die, dwarf_AT (DW_AT_dwo_name), dwo_file_name);
10488 if (comp_dir != NULL)
10489 add_skeleton_AT_string (die, DW_AT_comp_dir, comp_dir);
10490 add_AT_pubnames (die);
10491 add_AT_lineptr (die, DW_AT_GNU_addr_base, debug_addr_section_label);
10494 /* Output skeleton debug sections that point to the dwo file. */
10496 static void
10497 output_skeleton_debug_sections (dw_die_ref comp_unit,
10498 const unsigned char *dwo_id)
10500 /* These attributes will be found in the full debug_info section. */
10501 remove_AT (comp_unit, DW_AT_producer);
10502 remove_AT (comp_unit, DW_AT_language);
10504 switch_to_section (debug_skeleton_info_section);
10505 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
10507 /* Produce the skeleton compilation-unit header. This one differs enough from
10508 a normal CU header that it's better not to call output_compilation_unit
10509 header. */
10510 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10511 dw2_asm_output_data (4, 0xffffffff,
10512 "Initial length escape value indicating 64-bit "
10513 "DWARF extension");
10515 dw2_asm_output_data (DWARF_OFFSET_SIZE,
10516 DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
10517 - DWARF_INITIAL_LENGTH_SIZE
10518 + size_of_die (comp_unit),
10519 "Length of Compilation Unit Info");
10520 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
10521 if (dwarf_version >= 5)
10523 dw2_asm_output_data (1, DW_UT_skeleton, "DW_UT_skeleton");
10524 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10526 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_abbrev_section_label,
10527 debug_skeleton_abbrev_section,
10528 "Offset Into Abbrev. Section");
10529 if (dwarf_version < 5)
10530 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10531 else
10532 for (int i = 0; i < 8; i++)
10533 dw2_asm_output_data (1, dwo_id[i], i == 0 ? "DWO id" : NULL);
10535 comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
10536 output_die (comp_unit);
10538 /* Build the skeleton debug_abbrev section. */
10539 switch_to_section (debug_skeleton_abbrev_section);
10540 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
10542 output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
10544 dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
10547 /* Output a comdat type unit DIE and its children. */
10549 static void
10550 output_comdat_type_unit (comdat_type_node *node)
10552 const char *secname;
10553 char *tmp;
10554 int i;
10555 #if defined (OBJECT_FORMAT_ELF)
10556 tree comdat_key;
10557 #endif
10559 /* First mark all the DIEs in this CU so we know which get local refs. */
10560 mark_dies (node->root_die);
10562 external_ref_hash_type *extern_map = optimize_external_refs (node->root_die);
10564 build_abbrev_table (node->root_die, extern_map);
10566 delete extern_map;
10567 extern_map = NULL;
10569 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
10570 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
10571 calc_die_sizes (node->root_die);
10573 #if defined (OBJECT_FORMAT_ELF)
10574 if (dwarf_version >= 5)
10576 if (!dwarf_split_debug_info)
10577 secname = ".debug_info";
10578 else
10579 secname = ".debug_info.dwo";
10581 else if (!dwarf_split_debug_info)
10582 secname = ".debug_types";
10583 else
10584 secname = ".debug_types.dwo";
10586 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
10587 sprintf (tmp, dwarf_version >= 5 ? "wi." : "wt.");
10588 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
10589 sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
10590 comdat_key = get_identifier (tmp);
10591 targetm.asm_out.named_section (secname,
10592 SECTION_DEBUG | SECTION_LINKONCE,
10593 comdat_key);
10594 #else
10595 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
10596 sprintf (tmp, (dwarf_version >= 5
10597 ? ".gnu.linkonce.wi." : ".gnu.linkonce.wt."));
10598 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
10599 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
10600 secname = tmp;
10601 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
10602 #endif
10604 /* Output debugging information. */
10605 output_compilation_unit_header (dwarf_split_debug_info
10606 ? DW_UT_split_type : DW_UT_type);
10607 output_signature (node->signature, "Type Signature");
10608 dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
10609 "Offset to Type DIE");
10610 output_die (node->root_die);
10612 unmark_dies (node->root_die);
10615 /* Return the DWARF2/3 pubname associated with a decl. */
10617 static const char *
10618 dwarf2_name (tree decl, int scope)
10620 if (DECL_NAMELESS (decl))
10621 return NULL;
10622 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
10625 /* Add a new entry to .debug_pubnames if appropriate. */
10627 static void
10628 add_pubname_string (const char *str, dw_die_ref die)
10630 pubname_entry e;
10632 e.die = die;
10633 e.name = xstrdup (str);
10634 vec_safe_push (pubname_table, e);
10637 static void
10638 add_pubname (tree decl, dw_die_ref die)
10640 if (!want_pubnames ())
10641 return;
10643 /* Don't add items to the table when we expect that the consumer will have
10644 just read the enclosing die. For example, if the consumer is looking at a
10645 class_member, it will either be inside the class already, or will have just
10646 looked up the class to find the member. Either way, searching the class is
10647 faster than searching the index. */
10648 if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
10649 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
10651 const char *name = dwarf2_name (decl, 1);
10653 if (name)
10654 add_pubname_string (name, die);
10658 /* Add an enumerator to the pubnames section. */
10660 static void
10661 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
10663 pubname_entry e;
10665 gcc_assert (scope_name);
10666 e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
10667 e.die = die;
10668 vec_safe_push (pubname_table, e);
10671 /* Add a new entry to .debug_pubtypes if appropriate. */
10673 static void
10674 add_pubtype (tree decl, dw_die_ref die)
10676 pubname_entry e;
10678 if (!want_pubnames ())
10679 return;
10681 if ((TREE_PUBLIC (decl)
10682 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
10683 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
10685 tree scope = NULL;
10686 const char *scope_name = "";
10687 const char *sep = is_cxx () ? "::" : ".";
10688 const char *name;
10690 scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
10691 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
10693 scope_name = lang_hooks.dwarf_name (scope, 1);
10694 if (scope_name != NULL && scope_name[0] != '\0')
10695 scope_name = concat (scope_name, sep, NULL);
10696 else
10697 scope_name = "";
10700 if (TYPE_P (decl))
10701 name = type_tag (decl);
10702 else
10703 name = lang_hooks.dwarf_name (decl, 1);
10705 /* If we don't have a name for the type, there's no point in adding
10706 it to the table. */
10707 if (name != NULL && name[0] != '\0')
10709 e.die = die;
10710 e.name = concat (scope_name, name, NULL);
10711 vec_safe_push (pubtype_table, e);
10714 /* Although it might be more consistent to add the pubinfo for the
10715 enumerators as their dies are created, they should only be added if the
10716 enum type meets the criteria above. So rather than re-check the parent
10717 enum type whenever an enumerator die is created, just output them all
10718 here. This isn't protected by the name conditional because anonymous
10719 enums don't have names. */
10720 if (die->die_tag == DW_TAG_enumeration_type)
10722 dw_die_ref c;
10724 FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
10729 /* Output a single entry in the pubnames table. */
10731 static void
10732 output_pubname (dw_offset die_offset, pubname_entry *entry)
10734 dw_die_ref die = entry->die;
10735 int is_static = get_AT_flag (die, DW_AT_external) ? 0 : 1;
10737 dw2_asm_output_data (DWARF_OFFSET_SIZE, die_offset, "DIE offset");
10739 if (debug_generate_pub_sections == 2)
10741 /* This logic follows gdb's method for determining the value of the flag
10742 byte. */
10743 uint32_t flags = GDB_INDEX_SYMBOL_KIND_NONE;
10744 switch (die->die_tag)
10746 case DW_TAG_typedef:
10747 case DW_TAG_base_type:
10748 case DW_TAG_subrange_type:
10749 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
10750 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
10751 break;
10752 case DW_TAG_enumerator:
10753 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
10754 GDB_INDEX_SYMBOL_KIND_VARIABLE);
10755 if (!is_cxx () && !is_java ())
10756 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
10757 break;
10758 case DW_TAG_subprogram:
10759 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
10760 GDB_INDEX_SYMBOL_KIND_FUNCTION);
10761 if (!is_ada ())
10762 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
10763 break;
10764 case DW_TAG_constant:
10765 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
10766 GDB_INDEX_SYMBOL_KIND_VARIABLE);
10767 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
10768 break;
10769 case DW_TAG_variable:
10770 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
10771 GDB_INDEX_SYMBOL_KIND_VARIABLE);
10772 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
10773 break;
10774 case DW_TAG_namespace:
10775 case DW_TAG_imported_declaration:
10776 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
10777 break;
10778 case DW_TAG_class_type:
10779 case DW_TAG_interface_type:
10780 case DW_TAG_structure_type:
10781 case DW_TAG_union_type:
10782 case DW_TAG_enumeration_type:
10783 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
10784 if (!is_cxx () && !is_java ())
10785 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
10786 break;
10787 default:
10788 /* An unusual tag. Leave the flag-byte empty. */
10789 break;
10791 dw2_asm_output_data (1, flags >> GDB_INDEX_CU_BITSIZE,
10792 "GDB-index flags");
10795 dw2_asm_output_nstring (entry->name, -1, "external name");
10799 /* Output the public names table used to speed up access to externally
10800 visible names; or the public types table used to find type definitions. */
10802 static void
10803 output_pubnames (vec<pubname_entry, va_gc> *names)
10805 unsigned i;
10806 unsigned long pubnames_length = size_of_pubnames (names);
10807 pubname_entry *pub;
10809 if (!XCOFF_DEBUGGING_INFO)
10811 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10812 dw2_asm_output_data (4, 0xffffffff,
10813 "Initial length escape value indicating 64-bit DWARF extension");
10814 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
10815 "Pub Info Length");
10818 /* Version number for pubnames/pubtypes is independent of dwarf version. */
10819 dw2_asm_output_data (2, 2, "DWARF Version");
10821 if (dwarf_split_debug_info)
10822 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
10823 debug_skeleton_info_section,
10824 "Offset of Compilation Unit Info");
10825 else
10826 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
10827 debug_info_section,
10828 "Offset of Compilation Unit Info");
10829 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
10830 "Compilation Unit Length");
10832 FOR_EACH_VEC_ELT (*names, i, pub)
10834 if (include_pubname_in_output (names, pub))
10836 dw_offset die_offset = pub->die->die_offset;
10838 /* We shouldn't see pubnames for DIEs outside of the main CU. */
10839 if (names == pubname_table && pub->die->die_tag != DW_TAG_enumerator)
10840 gcc_assert (pub->die->die_mark);
10842 /* If we're putting types in their own .debug_types sections,
10843 the .debug_pubtypes table will still point to the compile
10844 unit (not the type unit), so we want to use the offset of
10845 the skeleton DIE (if there is one). */
10846 if (pub->die->comdat_type_p && names == pubtype_table)
10848 comdat_type_node *type_node = pub->die->die_id.die_type_node;
10850 if (type_node != NULL)
10851 die_offset = (type_node->skeleton_die != NULL
10852 ? type_node->skeleton_die->die_offset
10853 : comp_unit_die ()->die_offset);
10856 output_pubname (die_offset, pub);
10860 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
10863 /* Output public names and types tables if necessary. */
10865 static void
10866 output_pubtables (void)
10868 if (!want_pubnames () || !info_section_emitted)
10869 return;
10871 switch_to_section (debug_pubnames_section);
10872 output_pubnames (pubname_table);
10873 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
10874 It shouldn't hurt to emit it always, since pure DWARF2 consumers
10875 simply won't look for the section. */
10876 switch_to_section (debug_pubtypes_section);
10877 output_pubnames (pubtype_table);
10881 /* Output the information that goes into the .debug_aranges table.
10882 Namely, define the beginning and ending address range of the
10883 text section generated for this compilation unit. */
10885 static void
10886 output_aranges (void)
10888 unsigned i;
10889 unsigned long aranges_length = size_of_aranges ();
10891 if (!XCOFF_DEBUGGING_INFO)
10893 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10894 dw2_asm_output_data (4, 0xffffffff,
10895 "Initial length escape value indicating 64-bit DWARF extension");
10896 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
10897 "Length of Address Ranges Info");
10900 /* Version number for aranges is still 2, even up to DWARF5. */
10901 dw2_asm_output_data (2, 2, "DWARF Version");
10902 if (dwarf_split_debug_info)
10903 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
10904 debug_skeleton_info_section,
10905 "Offset of Compilation Unit Info");
10906 else
10907 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
10908 debug_info_section,
10909 "Offset of Compilation Unit Info");
10910 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
10911 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
10913 /* We need to align to twice the pointer size here. */
10914 if (DWARF_ARANGES_PAD_SIZE)
10916 /* Pad using a 2 byte words so that padding is correct for any
10917 pointer size. */
10918 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
10919 2 * DWARF2_ADDR_SIZE);
10920 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
10921 dw2_asm_output_data (2, 0, NULL);
10924 /* It is necessary not to output these entries if the sections were
10925 not used; if the sections were not used, the length will be 0 and
10926 the address may end up as 0 if the section is discarded by ld
10927 --gc-sections, leaving an invalid (0, 0) entry that can be
10928 confused with the terminator. */
10929 if (text_section_used)
10931 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
10932 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
10933 text_section_label, "Length");
10935 if (cold_text_section_used)
10937 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
10938 "Address");
10939 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
10940 cold_text_section_label, "Length");
10943 if (have_multiple_function_sections)
10945 unsigned fde_idx;
10946 dw_fde_ref fde;
10948 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
10950 if (DECL_IGNORED_P (fde->decl))
10951 continue;
10952 if (!fde->in_std_section)
10954 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
10955 "Address");
10956 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
10957 fde->dw_fde_begin, "Length");
10959 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
10961 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
10962 "Address");
10963 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
10964 fde->dw_fde_second_begin, "Length");
10969 /* Output the terminator words. */
10970 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
10971 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
10974 /* Add a new entry to .debug_ranges. Return its index into
10975 ranges_table vector. */
10977 static unsigned int
10978 add_ranges_num (int num, bool maybe_new_sec)
10980 dw_ranges r = { NULL, num, 0, maybe_new_sec };
10981 vec_safe_push (ranges_table, r);
10982 return vec_safe_length (ranges_table) - 1;
10985 /* Add a new entry to .debug_ranges corresponding to a block, or a
10986 range terminator if BLOCK is NULL. MAYBE_NEW_SEC is true if
10987 this entry might be in a different section from previous range. */
10989 static unsigned int
10990 add_ranges (const_tree block, bool maybe_new_sec)
10992 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0, maybe_new_sec);
10995 /* Note that (*rnglist_table)[offset] is either a head of a rnglist
10996 chain, or middle entry of a chain that will be directly referred to. */
10998 static void
10999 note_rnglist_head (unsigned int offset)
11001 if (dwarf_version < 5 || (*ranges_table)[offset].label)
11002 return;
11003 (*ranges_table)[offset].label = gen_internal_sym ("LLRL");
11006 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
11007 When using dwarf_split_debug_info, address attributes in dies destined
11008 for the final executable should be direct references--setting the
11009 parameter force_direct ensures this behavior. */
11011 static void
11012 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
11013 bool *added, bool force_direct)
11015 unsigned int in_use = vec_safe_length (ranges_by_label);
11016 unsigned int offset;
11017 dw_ranges_by_label rbl = { begin, end };
11018 vec_safe_push (ranges_by_label, rbl);
11019 offset = add_ranges_num (-(int)in_use - 1, true);
11020 if (!*added)
11022 add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
11023 *added = true;
11024 note_rnglist_head (offset);
11028 /* Emit .debug_ranges section. */
11030 static void
11031 output_ranges (void)
11033 unsigned i;
11034 static const char *const start_fmt = "Offset %#x";
11035 const char *fmt = start_fmt;
11036 dw_ranges *r;
11038 switch_to_section (debug_ranges_section);
11039 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
11040 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11042 int block_num = r->num;
11044 if (block_num > 0)
11046 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
11047 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
11049 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
11050 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
11052 /* If all code is in the text section, then the compilation
11053 unit base address defaults to DW_AT_low_pc, which is the
11054 base of the text section. */
11055 if (!have_multiple_function_sections)
11057 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
11058 text_section_label,
11059 fmt, i * 2 * DWARF2_ADDR_SIZE);
11060 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
11061 text_section_label, NULL);
11064 /* Otherwise, the compilation unit base address is zero,
11065 which allows us to use absolute addresses, and not worry
11066 about whether the target supports cross-section
11067 arithmetic. */
11068 else
11070 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11071 fmt, i * 2 * DWARF2_ADDR_SIZE);
11072 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
11075 fmt = NULL;
11078 /* Negative block_num stands for an index into ranges_by_label. */
11079 else if (block_num < 0)
11081 int lab_idx = - block_num - 1;
11083 if (!have_multiple_function_sections)
11085 gcc_unreachable ();
11086 #if 0
11087 /* If we ever use add_ranges_by_labels () for a single
11088 function section, all we have to do is to take out
11089 the #if 0 above. */
11090 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11091 (*ranges_by_label)[lab_idx].begin,
11092 text_section_label,
11093 fmt, i * 2 * DWARF2_ADDR_SIZE);
11094 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11095 (*ranges_by_label)[lab_idx].end,
11096 text_section_label, NULL);
11097 #endif
11099 else
11101 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11102 (*ranges_by_label)[lab_idx].begin,
11103 fmt, i * 2 * DWARF2_ADDR_SIZE);
11104 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11105 (*ranges_by_label)[lab_idx].end,
11106 NULL);
11109 else
11111 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11112 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11113 fmt = start_fmt;
11118 /* Non-zero if .debug_line_str should be used for .debug_line section
11119 strings or strings that are likely shareable with those. */
11120 #define DWARF5_USE_DEBUG_LINE_STR \
11121 (!DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET \
11122 && (DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) != 0 \
11123 /* FIXME: there is no .debug_line_str.dwo section, \
11124 for -gsplit-dwarf we should use DW_FORM_strx instead. */ \
11125 && !dwarf_split_debug_info)
11127 /* Assign .debug_rnglists indexes. */
11129 static void
11130 index_rnglists (void)
11132 unsigned i;
11133 dw_ranges *r;
11135 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11136 if (r->label)
11137 r->idx = rnglist_idx++;
11140 /* Emit .debug_rnglists section. */
11142 static void
11143 output_rnglists (void)
11145 unsigned i;
11146 dw_ranges *r;
11147 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
11148 char l2[MAX_ARTIFICIAL_LABEL_BYTES];
11149 char basebuf[MAX_ARTIFICIAL_LABEL_BYTES];
11151 switch_to_section (debug_ranges_section);
11152 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
11153 ASM_GENERATE_INTERNAL_LABEL (l1, DEBUG_RANGES_SECTION_LABEL, 2);
11154 ASM_GENERATE_INTERNAL_LABEL (l2, DEBUG_RANGES_SECTION_LABEL, 3);
11155 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11156 dw2_asm_output_data (4, 0xffffffff,
11157 "Initial length escape value indicating "
11158 "64-bit DWARF extension");
11159 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
11160 "Length of Range Lists");
11161 ASM_OUTPUT_LABEL (asm_out_file, l1);
11162 dw2_asm_output_data (2, dwarf_version, "DWARF Version");
11163 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
11164 dw2_asm_output_data (1, 0, "Segment Size");
11165 /* Emit the offset table only for -gsplit-dwarf. If we don't care
11166 about relocation sizes and primarily care about the size of .debug*
11167 sections in linked shared libraries and executables, then
11168 the offset table plus corresponding DW_FORM_rnglistx uleb128 indexes
11169 into it are usually larger than just DW_FORM_sec_offset offsets
11170 into the .debug_rnglists section. */
11171 dw2_asm_output_data (4, dwarf_split_debug_info ? rnglist_idx : 0,
11172 "Offset Entry Count");
11173 if (dwarf_split_debug_info)
11175 ASM_OUTPUT_LABEL (asm_out_file, ranges_base_label);
11176 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11177 if (r->label)
11178 dw2_asm_output_delta (DWARF_OFFSET_SIZE, r->label,
11179 ranges_base_label, NULL);
11182 const char *lab = "";
11183 unsigned int len = vec_safe_length (ranges_table);
11184 const char *base = NULL;
11185 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11187 int block_num = r->num;
11189 if (r->label)
11191 ASM_OUTPUT_LABEL (asm_out_file, r->label);
11192 lab = r->label;
11194 if (HAVE_AS_LEB128 && (r->label || r->maybe_new_sec))
11195 base = NULL;
11196 if (block_num > 0)
11198 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
11199 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
11201 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
11202 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
11204 if (HAVE_AS_LEB128)
11206 /* If all code is in the text section, then the compilation
11207 unit base address defaults to DW_AT_low_pc, which is the
11208 base of the text section. */
11209 if (!have_multiple_function_sections)
11211 dw2_asm_output_data (1, DW_RLE_offset_pair,
11212 "DW_RLE_offset_pair (%s)", lab);
11213 dw2_asm_output_delta_uleb128 (blabel, text_section_label,
11214 "Range begin address (%s)", lab);
11215 dw2_asm_output_delta_uleb128 (elabel, text_section_label,
11216 "Range end address (%s)", lab);
11217 continue;
11219 if (base == NULL)
11221 dw_ranges *r2 = NULL;
11222 if (i < len - 1)
11223 r2 = &(*ranges_table)[i + 1];
11224 if (r2
11225 && r2->num != 0
11226 && r2->label == NULL
11227 && !r2->maybe_new_sec)
11229 dw2_asm_output_data (1, DW_RLE_base_address,
11230 "DW_RLE_base_address (%s)", lab);
11231 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11232 "Base address (%s)", lab);
11233 strcpy (basebuf, blabel);
11234 base = basebuf;
11237 if (base)
11239 dw2_asm_output_data (1, DW_RLE_offset_pair,
11240 "DW_RLE_offset_pair (%s)", lab);
11241 dw2_asm_output_delta_uleb128 (blabel, base,
11242 "Range begin address (%s)", lab);
11243 dw2_asm_output_delta_uleb128 (elabel, base,
11244 "Range end address (%s)", lab);
11245 continue;
11247 dw2_asm_output_data (1, DW_RLE_start_length,
11248 "DW_RLE_start_length (%s)", lab);
11249 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11250 "Range begin address (%s)", lab);
11251 dw2_asm_output_delta_uleb128 (elabel, blabel,
11252 "Range length (%s)", lab);
11254 else
11256 dw2_asm_output_data (1, DW_RLE_start_end,
11257 "DW_RLE_start_end (%s)", lab);
11258 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11259 "Range begin address (%s)", lab);
11260 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel,
11261 "Range end address (%s)", lab);
11265 /* Negative block_num stands for an index into ranges_by_label. */
11266 else if (block_num < 0)
11268 int lab_idx = - block_num - 1;
11269 const char *blabel = (*ranges_by_label)[lab_idx].begin;
11270 const char *elabel = (*ranges_by_label)[lab_idx].end;
11272 if (!have_multiple_function_sections)
11273 gcc_unreachable ();
11274 if (HAVE_AS_LEB128)
11276 dw2_asm_output_data (1, DW_RLE_start_length,
11277 "DW_RLE_start_length (%s)", lab);
11278 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11279 "Range begin address (%s)", lab);
11280 dw2_asm_output_delta_uleb128 (elabel, blabel,
11281 "Range length (%s)", lab);
11283 else
11285 dw2_asm_output_data (1, DW_RLE_start_end,
11286 "DW_RLE_start_end (%s)", lab);
11287 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11288 "Range begin address (%s)", lab);
11289 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel,
11290 "Range end address (%s)", lab);
11293 else
11294 dw2_asm_output_data (1, DW_RLE_end_of_list,
11295 "DW_RLE_end_of_list (%s)", lab);
11297 ASM_OUTPUT_LABEL (asm_out_file, l2);
11300 /* Data structure containing information about input files. */
11301 struct file_info
11303 const char *path; /* Complete file name. */
11304 const char *fname; /* File name part. */
11305 int length; /* Length of entire string. */
11306 struct dwarf_file_data * file_idx; /* Index in input file table. */
11307 int dir_idx; /* Index in directory table. */
11310 /* Data structure containing information about directories with source
11311 files. */
11312 struct dir_info
11314 const char *path; /* Path including directory name. */
11315 int length; /* Path length. */
11316 int prefix; /* Index of directory entry which is a prefix. */
11317 int count; /* Number of files in this directory. */
11318 int dir_idx; /* Index of directory used as base. */
11321 /* Callback function for file_info comparison. We sort by looking at
11322 the directories in the path. */
11324 static int
11325 file_info_cmp (const void *p1, const void *p2)
11327 const struct file_info *const s1 = (const struct file_info *) p1;
11328 const struct file_info *const s2 = (const struct file_info *) p2;
11329 const unsigned char *cp1;
11330 const unsigned char *cp2;
11332 /* Take care of file names without directories. We need to make sure that
11333 we return consistent values to qsort since some will get confused if
11334 we return the same value when identical operands are passed in opposite
11335 orders. So if neither has a directory, return 0 and otherwise return
11336 1 or -1 depending on which one has the directory. */
11337 if ((s1->path == s1->fname || s2->path == s2->fname))
11338 return (s2->path == s2->fname) - (s1->path == s1->fname);
11340 cp1 = (const unsigned char *) s1->path;
11341 cp2 = (const unsigned char *) s2->path;
11343 while (1)
11345 ++cp1;
11346 ++cp2;
11347 /* Reached the end of the first path? If so, handle like above. */
11348 if ((cp1 == (const unsigned char *) s1->fname)
11349 || (cp2 == (const unsigned char *) s2->fname))
11350 return ((cp2 == (const unsigned char *) s2->fname)
11351 - (cp1 == (const unsigned char *) s1->fname));
11353 /* Character of current path component the same? */
11354 else if (*cp1 != *cp2)
11355 return *cp1 - *cp2;
11359 struct file_name_acquire_data
11361 struct file_info *files;
11362 int used_files;
11363 int max_files;
11366 /* Traversal function for the hash table. */
11369 file_name_acquire (dwarf_file_data **slot, file_name_acquire_data *fnad)
11371 struct dwarf_file_data *d = *slot;
11372 struct file_info *fi;
11373 const char *f;
11375 gcc_assert (fnad->max_files >= d->emitted_number);
11377 if (! d->emitted_number)
11378 return 1;
11380 gcc_assert (fnad->max_files != fnad->used_files);
11382 fi = fnad->files + fnad->used_files++;
11384 /* Skip all leading "./". */
11385 f = d->filename;
11386 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
11387 f += 2;
11389 /* Create a new array entry. */
11390 fi->path = f;
11391 fi->length = strlen (f);
11392 fi->file_idx = d;
11394 /* Search for the file name part. */
11395 f = strrchr (f, DIR_SEPARATOR);
11396 #if defined (DIR_SEPARATOR_2)
11398 char *g = strrchr (fi->path, DIR_SEPARATOR_2);
11400 if (g != NULL)
11402 if (f == NULL || f < g)
11403 f = g;
11406 #endif
11408 fi->fname = f == NULL ? fi->path : f + 1;
11409 return 1;
11412 /* Helper function for output_file_names. Emit a FORM encoded
11413 string STR, with assembly comment start ENTRY_KIND and
11414 index IDX */
11416 static void
11417 output_line_string (enum dwarf_form form, const char *str,
11418 const char *entry_kind, unsigned int idx)
11420 switch (form)
11422 case DW_FORM_string:
11423 dw2_asm_output_nstring (str, -1, "%s: %#x", entry_kind, idx);
11424 break;
11425 case DW_FORM_line_strp:
11426 if (!debug_line_str_hash)
11427 debug_line_str_hash
11428 = hash_table<indirect_string_hasher>::create_ggc (10);
11430 struct indirect_string_node *node;
11431 node = find_AT_string_in_table (str, debug_line_str_hash);
11432 set_indirect_string (node);
11433 node->form = form;
11434 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
11435 debug_line_str_section, "%s: %#x: \"%s\"",
11436 entry_kind, 0, node->str);
11437 break;
11438 default:
11439 gcc_unreachable ();
11443 /* Output the directory table and the file name table. We try to minimize
11444 the total amount of memory needed. A heuristic is used to avoid large
11445 slowdowns with many input files. */
11447 static void
11448 output_file_names (void)
11450 struct file_name_acquire_data fnad;
11451 int numfiles;
11452 struct file_info *files;
11453 struct dir_info *dirs;
11454 int *saved;
11455 int *savehere;
11456 int *backmap;
11457 int ndirs;
11458 int idx_offset;
11459 int i;
11461 if (!last_emitted_file)
11463 if (dwarf_version >= 5)
11465 dw2_asm_output_data (1, 0, "Directory entry format count");
11466 dw2_asm_output_data_uleb128 (0, "Directories count");
11467 dw2_asm_output_data (1, 0, "File name entry format count");
11468 dw2_asm_output_data_uleb128 (0, "File names count");
11470 else
11472 dw2_asm_output_data (1, 0, "End directory table");
11473 dw2_asm_output_data (1, 0, "End file name table");
11475 return;
11478 numfiles = last_emitted_file->emitted_number;
11480 /* Allocate the various arrays we need. */
11481 files = XALLOCAVEC (struct file_info, numfiles);
11482 dirs = XALLOCAVEC (struct dir_info, numfiles);
11484 fnad.files = files;
11485 fnad.used_files = 0;
11486 fnad.max_files = numfiles;
11487 file_table->traverse<file_name_acquire_data *, file_name_acquire> (&fnad);
11488 gcc_assert (fnad.used_files == fnad.max_files);
11490 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
11492 /* Find all the different directories used. */
11493 dirs[0].path = files[0].path;
11494 dirs[0].length = files[0].fname - files[0].path;
11495 dirs[0].prefix = -1;
11496 dirs[0].count = 1;
11497 dirs[0].dir_idx = 0;
11498 files[0].dir_idx = 0;
11499 ndirs = 1;
11501 for (i = 1; i < numfiles; i++)
11502 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
11503 && memcmp (dirs[ndirs - 1].path, files[i].path,
11504 dirs[ndirs - 1].length) == 0)
11506 /* Same directory as last entry. */
11507 files[i].dir_idx = ndirs - 1;
11508 ++dirs[ndirs - 1].count;
11510 else
11512 int j;
11514 /* This is a new directory. */
11515 dirs[ndirs].path = files[i].path;
11516 dirs[ndirs].length = files[i].fname - files[i].path;
11517 dirs[ndirs].count = 1;
11518 dirs[ndirs].dir_idx = ndirs;
11519 files[i].dir_idx = ndirs;
11521 /* Search for a prefix. */
11522 dirs[ndirs].prefix = -1;
11523 for (j = 0; j < ndirs; j++)
11524 if (dirs[j].length < dirs[ndirs].length
11525 && dirs[j].length > 1
11526 && (dirs[ndirs].prefix == -1
11527 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
11528 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
11529 dirs[ndirs].prefix = j;
11531 ++ndirs;
11534 /* Now to the actual work. We have to find a subset of the directories which
11535 allow expressing the file name using references to the directory table
11536 with the least amount of characters. We do not do an exhaustive search
11537 where we would have to check out every combination of every single
11538 possible prefix. Instead we use a heuristic which provides nearly optimal
11539 results in most cases and never is much off. */
11540 saved = XALLOCAVEC (int, ndirs);
11541 savehere = XALLOCAVEC (int, ndirs);
11543 memset (saved, '\0', ndirs * sizeof (saved[0]));
11544 for (i = 0; i < ndirs; i++)
11546 int j;
11547 int total;
11549 /* We can always save some space for the current directory. But this
11550 does not mean it will be enough to justify adding the directory. */
11551 savehere[i] = dirs[i].length;
11552 total = (savehere[i] - saved[i]) * dirs[i].count;
11554 for (j = i + 1; j < ndirs; j++)
11556 savehere[j] = 0;
11557 if (saved[j] < dirs[i].length)
11559 /* Determine whether the dirs[i] path is a prefix of the
11560 dirs[j] path. */
11561 int k;
11563 k = dirs[j].prefix;
11564 while (k != -1 && k != (int) i)
11565 k = dirs[k].prefix;
11567 if (k == (int) i)
11569 /* Yes it is. We can possibly save some memory by
11570 writing the filenames in dirs[j] relative to
11571 dirs[i]. */
11572 savehere[j] = dirs[i].length;
11573 total += (savehere[j] - saved[j]) * dirs[j].count;
11578 /* Check whether we can save enough to justify adding the dirs[i]
11579 directory. */
11580 if (total > dirs[i].length + 1)
11582 /* It's worthwhile adding. */
11583 for (j = i; j < ndirs; j++)
11584 if (savehere[j] > 0)
11586 /* Remember how much we saved for this directory so far. */
11587 saved[j] = savehere[j];
11589 /* Remember the prefix directory. */
11590 dirs[j].dir_idx = i;
11595 /* Emit the directory name table. */
11596 idx_offset = dirs[0].length > 0 ? 1 : 0;
11597 enum dwarf_form str_form = DW_FORM_string;
11598 enum dwarf_form idx_form = DW_FORM_udata;
11599 if (dwarf_version >= 5)
11601 const char *comp_dir = comp_dir_string ();
11602 if (comp_dir == NULL)
11603 comp_dir = "";
11604 dw2_asm_output_data (1, 1, "Directory entry format count");
11605 if (DWARF5_USE_DEBUG_LINE_STR)
11606 str_form = DW_FORM_line_strp;
11607 dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
11608 dw2_asm_output_data_uleb128 (str_form, "%s",
11609 get_DW_FORM_name (str_form));
11610 dw2_asm_output_data_uleb128 (ndirs + idx_offset, "Directories count");
11611 if (str_form == DW_FORM_string)
11613 dw2_asm_output_nstring (comp_dir, -1, "Directory Entry: %#x", 0);
11614 for (i = 1 - idx_offset; i < ndirs; i++)
11615 dw2_asm_output_nstring (dirs[i].path,
11616 dirs[i].length
11617 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
11618 "Directory Entry: %#x", i + idx_offset);
11620 else
11622 output_line_string (str_form, comp_dir, "Directory Entry", 0);
11623 for (i = 1 - idx_offset; i < ndirs; i++)
11625 const char *str
11626 = ggc_alloc_string (dirs[i].path,
11627 dirs[i].length
11628 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR);
11629 output_line_string (str_form, str, "Directory Entry",
11630 (unsigned) i + idx_offset);
11634 else
11636 for (i = 1 - idx_offset; i < ndirs; i++)
11637 dw2_asm_output_nstring (dirs[i].path,
11638 dirs[i].length
11639 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
11640 "Directory Entry: %#x", i + idx_offset);
11642 dw2_asm_output_data (1, 0, "End directory table");
11645 /* We have to emit them in the order of emitted_number since that's
11646 used in the debug info generation. To do this efficiently we
11647 generate a back-mapping of the indices first. */
11648 backmap = XALLOCAVEC (int, numfiles);
11649 for (i = 0; i < numfiles; i++)
11650 backmap[files[i].file_idx->emitted_number - 1] = i;
11652 if (dwarf_version >= 5)
11654 const char *filename0 = get_AT_string (comp_unit_die (), DW_AT_name);
11655 if (filename0 == NULL)
11656 filename0 = "";
11657 /* DW_LNCT_directory_index can use DW_FORM_udata, DW_FORM_data1 and
11658 DW_FORM_data2. Choose one based on the number of directories
11659 and how much space would they occupy in each encoding.
11660 If we have at most 256 directories, all indexes fit into
11661 a single byte, so DW_FORM_data1 is most compact (if there
11662 are at most 128 directories, DW_FORM_udata would be as
11663 compact as that, but not shorter and slower to decode). */
11664 if (ndirs + idx_offset <= 256)
11665 idx_form = DW_FORM_data1;
11666 /* If there are more than 65536 directories, we have to use
11667 DW_FORM_udata, DW_FORM_data2 can't refer to them.
11668 Otherwise, compute what space would occupy if all the indexes
11669 used DW_FORM_udata - sum - and compare that to how large would
11670 be DW_FORM_data2 encoding, and pick the more efficient one. */
11671 else if (ndirs + idx_offset <= 65536)
11673 unsigned HOST_WIDE_INT sum = 1;
11674 for (i = 0; i < numfiles; i++)
11676 int file_idx = backmap[i];
11677 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
11678 sum += size_of_uleb128 (dir_idx);
11680 if (sum >= HOST_WIDE_INT_UC (2) * (numfiles + 1))
11681 idx_form = DW_FORM_data2;
11683 #ifdef VMS_DEBUGGING_INFO
11684 dw2_asm_output_data (1, 4, "File name entry format count");
11685 #else
11686 dw2_asm_output_data (1, 2, "File name entry format count");
11687 #endif
11688 dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
11689 dw2_asm_output_data_uleb128 (str_form, "%s",
11690 get_DW_FORM_name (str_form));
11691 dw2_asm_output_data_uleb128 (DW_LNCT_directory_index,
11692 "DW_LNCT_directory_index");
11693 dw2_asm_output_data_uleb128 (idx_form, "%s",
11694 get_DW_FORM_name (idx_form));
11695 #ifdef VMS_DEBUGGING_INFO
11696 dw2_asm_output_data_uleb128 (DW_LNCT_timestamp, "DW_LNCT_timestamp");
11697 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
11698 dw2_asm_output_data_uleb128 (DW_LNCT_size, "DW_LNCT_size");
11699 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
11700 #endif
11701 dw2_asm_output_data_uleb128 (numfiles + 1, "File names count");
11703 output_line_string (str_form, filename0, "File Entry", 0);
11705 /* Include directory index. */
11706 if (dwarf_version >= 5 && idx_form != DW_FORM_udata)
11707 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
11708 0, NULL);
11709 else
11710 dw2_asm_output_data_uleb128 (0, NULL);
11712 #ifdef VMS_DEBUGGING_INFO
11713 dw2_asm_output_data_uleb128 (0, NULL);
11714 dw2_asm_output_data_uleb128 (0, NULL);
11715 #endif
11718 /* Now write all the file names. */
11719 for (i = 0; i < numfiles; i++)
11721 int file_idx = backmap[i];
11722 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
11724 #ifdef VMS_DEBUGGING_INFO
11725 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
11727 /* Setting these fields can lead to debugger miscomparisons,
11728 but VMS Debug requires them to be set correctly. */
11730 int ver;
11731 long long cdt;
11732 long siz;
11733 int maxfilelen = (strlen (files[file_idx].path)
11734 + dirs[dir_idx].length
11735 + MAX_VMS_VERSION_LEN + 1);
11736 char *filebuf = XALLOCAVEC (char, maxfilelen);
11738 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
11739 snprintf (filebuf, maxfilelen, "%s;%d",
11740 files[file_idx].path + dirs[dir_idx].length, ver);
11742 output_line_string (str_form, filebuf, "File Entry", (unsigned) i + 1);
11744 /* Include directory index. */
11745 if (dwarf_version >= 5 && idx_form != DW_FORM_udata)
11746 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
11747 dir_idx + idx_offset, NULL);
11748 else
11749 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
11751 /* Modification time. */
11752 dw2_asm_output_data_uleb128 ((vms_file_stats_name (files[file_idx].path,
11753 &cdt, 0, 0, 0) == 0)
11754 ? cdt : 0, NULL);
11756 /* File length in bytes. */
11757 dw2_asm_output_data_uleb128 ((vms_file_stats_name (files[file_idx].path,
11758 0, &siz, 0, 0) == 0)
11759 ? siz : 0, NULL);
11760 #else
11761 output_line_string (str_form,
11762 files[file_idx].path + dirs[dir_idx].length,
11763 "File Entry", (unsigned) i + 1);
11765 /* Include directory index. */
11766 if (dwarf_version >= 5 && idx_form != DW_FORM_udata)
11767 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
11768 dir_idx + idx_offset, NULL);
11769 else
11770 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
11772 if (dwarf_version >= 5)
11773 continue;
11775 /* Modification time. */
11776 dw2_asm_output_data_uleb128 (0, NULL);
11778 /* File length in bytes. */
11779 dw2_asm_output_data_uleb128 (0, NULL);
11780 #endif /* VMS_DEBUGGING_INFO */
11783 if (dwarf_version < 5)
11784 dw2_asm_output_data (1, 0, "End file name table");
11788 /* Output one line number table into the .debug_line section. */
11790 static void
11791 output_one_line_info_table (dw_line_info_table *table)
11793 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
11794 unsigned int current_line = 1;
11795 bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
11796 dw_line_info_entry *ent;
11797 size_t i;
11799 FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
11801 switch (ent->opcode)
11803 case LI_set_address:
11804 /* ??? Unfortunately, we have little choice here currently, and
11805 must always use the most general form. GCC does not know the
11806 address delta itself, so we can't use DW_LNS_advance_pc. Many
11807 ports do have length attributes which will give an upper bound
11808 on the address range. We could perhaps use length attributes
11809 to determine when it is safe to use DW_LNS_fixed_advance_pc. */
11810 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
11812 /* This can handle any delta. This takes
11813 4+DWARF2_ADDR_SIZE bytes. */
11814 dw2_asm_output_data (1, 0, "set address %s", line_label);
11815 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
11816 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
11817 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
11818 break;
11820 case LI_set_line:
11821 if (ent->val == current_line)
11823 /* We still need to start a new row, so output a copy insn. */
11824 dw2_asm_output_data (1, DW_LNS_copy,
11825 "copy line %u", current_line);
11827 else
11829 int line_offset = ent->val - current_line;
11830 int line_delta = line_offset - DWARF_LINE_BASE;
11832 current_line = ent->val;
11833 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
11835 /* This can handle deltas from -10 to 234, using the current
11836 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
11837 This takes 1 byte. */
11838 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
11839 "line %u", current_line);
11841 else
11843 /* This can handle any delta. This takes at least 4 bytes,
11844 depending on the value being encoded. */
11845 dw2_asm_output_data (1, DW_LNS_advance_line,
11846 "advance to line %u", current_line);
11847 dw2_asm_output_data_sleb128 (line_offset, NULL);
11848 dw2_asm_output_data (1, DW_LNS_copy, NULL);
11851 break;
11853 case LI_set_file:
11854 dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
11855 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
11856 break;
11858 case LI_set_column:
11859 dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
11860 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
11861 break;
11863 case LI_negate_stmt:
11864 current_is_stmt = !current_is_stmt;
11865 dw2_asm_output_data (1, DW_LNS_negate_stmt,
11866 "is_stmt %d", current_is_stmt);
11867 break;
11869 case LI_set_prologue_end:
11870 dw2_asm_output_data (1, DW_LNS_set_prologue_end,
11871 "set prologue end");
11872 break;
11874 case LI_set_epilogue_begin:
11875 dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
11876 "set epilogue begin");
11877 break;
11879 case LI_set_discriminator:
11880 dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
11881 dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
11882 dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
11883 dw2_asm_output_data_uleb128 (ent->val, NULL);
11884 break;
11888 /* Emit debug info for the address of the end of the table. */
11889 dw2_asm_output_data (1, 0, "set address %s", table->end_label);
11890 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
11891 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
11892 dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
11894 dw2_asm_output_data (1, 0, "end sequence");
11895 dw2_asm_output_data_uleb128 (1, NULL);
11896 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
11899 /* Output the source line number correspondence information. This
11900 information goes into the .debug_line section. */
11902 static void
11903 output_line_info (bool prologue_only)
11905 static unsigned int generation;
11906 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
11907 char p1[MAX_ARTIFICIAL_LABEL_BYTES], p2[MAX_ARTIFICIAL_LABEL_BYTES];
11908 bool saw_one = false;
11909 int opc;
11911 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, generation);
11912 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, generation);
11913 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, generation);
11914 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, generation++);
11916 if (!XCOFF_DEBUGGING_INFO)
11918 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11919 dw2_asm_output_data (4, 0xffffffff,
11920 "Initial length escape value indicating 64-bit DWARF extension");
11921 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
11922 "Length of Source Line Info");
11925 ASM_OUTPUT_LABEL (asm_out_file, l1);
11927 dw2_asm_output_data (2, dwarf_version, "DWARF Version");
11928 if (dwarf_version >= 5)
11930 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
11931 dw2_asm_output_data (1, 0, "Segment Size");
11933 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
11934 ASM_OUTPUT_LABEL (asm_out_file, p1);
11936 /* Define the architecture-dependent minimum instruction length (in bytes).
11937 In this implementation of DWARF, this field is used for information
11938 purposes only. Since GCC generates assembly language, we have no
11939 a priori knowledge of how many instruction bytes are generated for each
11940 source line, and therefore can use only the DW_LNE_set_address and
11941 DW_LNS_fixed_advance_pc line information commands. Accordingly, we fix
11942 this as '1', which is "correct enough" for all architectures,
11943 and don't let the target override. */
11944 dw2_asm_output_data (1, 1, "Minimum Instruction Length");
11946 if (dwarf_version >= 4)
11947 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
11948 "Maximum Operations Per Instruction");
11949 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
11950 "Default is_stmt_start flag");
11951 dw2_asm_output_data (1, DWARF_LINE_BASE,
11952 "Line Base Value (Special Opcodes)");
11953 dw2_asm_output_data (1, DWARF_LINE_RANGE,
11954 "Line Range Value (Special Opcodes)");
11955 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
11956 "Special Opcode Base");
11958 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
11960 int n_op_args;
11961 switch (opc)
11963 case DW_LNS_advance_pc:
11964 case DW_LNS_advance_line:
11965 case DW_LNS_set_file:
11966 case DW_LNS_set_column:
11967 case DW_LNS_fixed_advance_pc:
11968 case DW_LNS_set_isa:
11969 n_op_args = 1;
11970 break;
11971 default:
11972 n_op_args = 0;
11973 break;
11976 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
11977 opc, n_op_args);
11980 /* Write out the information about the files we use. */
11981 output_file_names ();
11982 ASM_OUTPUT_LABEL (asm_out_file, p2);
11983 if (prologue_only)
11985 /* Output the marker for the end of the line number info. */
11986 ASM_OUTPUT_LABEL (asm_out_file, l2);
11987 return;
11990 if (separate_line_info)
11992 dw_line_info_table *table;
11993 size_t i;
11995 FOR_EACH_VEC_ELT (*separate_line_info, i, table)
11996 if (table->in_use)
11998 output_one_line_info_table (table);
11999 saw_one = true;
12002 if (cold_text_section_line_info && cold_text_section_line_info->in_use)
12004 output_one_line_info_table (cold_text_section_line_info);
12005 saw_one = true;
12008 /* ??? Some Darwin linkers crash on a .debug_line section with no
12009 sequences. Further, merely a DW_LNE_end_sequence entry is not
12010 sufficient -- the address column must also be initialized.
12011 Make sure to output at least one set_address/end_sequence pair,
12012 choosing .text since that section is always present. */
12013 if (text_section_line_info->in_use || !saw_one)
12014 output_one_line_info_table (text_section_line_info);
12016 /* Output the marker for the end of the line number info. */
12017 ASM_OUTPUT_LABEL (asm_out_file, l2);
12020 /* Return true if DW_AT_endianity should be emitted according to REVERSE. */
12022 static inline bool
12023 need_endianity_attribute_p (bool reverse)
12025 return reverse && (dwarf_version >= 3 || !dwarf_strict);
12028 /* Given a pointer to a tree node for some base type, return a pointer to
12029 a DIE that describes the given type. REVERSE is true if the type is
12030 to be interpreted in the reverse storage order wrt the target order.
12032 This routine must only be called for GCC type nodes that correspond to
12033 Dwarf base (fundamental) types. */
12035 static dw_die_ref
12036 base_type_die (tree type, bool reverse)
12038 dw_die_ref base_type_result;
12039 enum dwarf_type encoding;
12040 bool fpt_used = false;
12041 struct fixed_point_type_info fpt_info;
12042 tree type_bias = NULL_TREE;
12044 if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
12045 return 0;
12047 /* If this is a subtype that should not be emitted as a subrange type,
12048 use the base type. See subrange_type_for_debug_p. */
12049 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
12050 type = TREE_TYPE (type);
12052 switch (TREE_CODE (type))
12054 case INTEGER_TYPE:
12055 if ((dwarf_version >= 4 || !dwarf_strict)
12056 && TYPE_NAME (type)
12057 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12058 && DECL_IS_BUILTIN (TYPE_NAME (type))
12059 && DECL_NAME (TYPE_NAME (type)))
12061 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
12062 if (strcmp (name, "char16_t") == 0
12063 || strcmp (name, "char32_t") == 0)
12065 encoding = DW_ATE_UTF;
12066 break;
12069 if ((dwarf_version >= 3 || !dwarf_strict)
12070 && lang_hooks.types.get_fixed_point_type_info)
12072 memset (&fpt_info, 0, sizeof (fpt_info));
12073 if (lang_hooks.types.get_fixed_point_type_info (type, &fpt_info))
12075 fpt_used = true;
12076 encoding = ((TYPE_UNSIGNED (type))
12077 ? DW_ATE_unsigned_fixed
12078 : DW_ATE_signed_fixed);
12079 break;
12082 if (TYPE_STRING_FLAG (type))
12084 if (TYPE_UNSIGNED (type))
12085 encoding = DW_ATE_unsigned_char;
12086 else
12087 encoding = DW_ATE_signed_char;
12089 else if (TYPE_UNSIGNED (type))
12090 encoding = DW_ATE_unsigned;
12091 else
12092 encoding = DW_ATE_signed;
12094 if (!dwarf_strict
12095 && lang_hooks.types.get_type_bias)
12096 type_bias = lang_hooks.types.get_type_bias (type);
12097 break;
12099 case REAL_TYPE:
12100 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
12102 if (dwarf_version >= 3 || !dwarf_strict)
12103 encoding = DW_ATE_decimal_float;
12104 else
12105 encoding = DW_ATE_lo_user;
12107 else
12108 encoding = DW_ATE_float;
12109 break;
12111 case FIXED_POINT_TYPE:
12112 if (!(dwarf_version >= 3 || !dwarf_strict))
12113 encoding = DW_ATE_lo_user;
12114 else if (TYPE_UNSIGNED (type))
12115 encoding = DW_ATE_unsigned_fixed;
12116 else
12117 encoding = DW_ATE_signed_fixed;
12118 break;
12120 /* Dwarf2 doesn't know anything about complex ints, so use
12121 a user defined type for it. */
12122 case COMPLEX_TYPE:
12123 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
12124 encoding = DW_ATE_complex_float;
12125 else
12126 encoding = DW_ATE_lo_user;
12127 break;
12129 case BOOLEAN_TYPE:
12130 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
12131 encoding = DW_ATE_boolean;
12132 break;
12134 default:
12135 /* No other TREE_CODEs are Dwarf fundamental types. */
12136 gcc_unreachable ();
12139 base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
12141 add_AT_unsigned (base_type_result, DW_AT_byte_size,
12142 int_size_in_bytes (type));
12143 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
12145 if (need_endianity_attribute_p (reverse))
12146 add_AT_unsigned (base_type_result, DW_AT_endianity,
12147 BYTES_BIG_ENDIAN ? DW_END_little : DW_END_big);
12149 add_alignment_attribute (base_type_result, type);
12151 if (fpt_used)
12153 switch (fpt_info.scale_factor_kind)
12155 case fixed_point_scale_factor_binary:
12156 add_AT_int (base_type_result, DW_AT_binary_scale,
12157 fpt_info.scale_factor.binary);
12158 break;
12160 case fixed_point_scale_factor_decimal:
12161 add_AT_int (base_type_result, DW_AT_decimal_scale,
12162 fpt_info.scale_factor.decimal);
12163 break;
12165 case fixed_point_scale_factor_arbitrary:
12166 /* Arbitrary scale factors cannot be described in standard DWARF,
12167 yet. */
12168 if (!dwarf_strict)
12170 /* Describe the scale factor as a rational constant. */
12171 const dw_die_ref scale_factor
12172 = new_die (DW_TAG_constant, comp_unit_die (), type);
12174 add_AT_unsigned (scale_factor, DW_AT_GNU_numerator,
12175 fpt_info.scale_factor.arbitrary.numerator);
12176 add_AT_int (scale_factor, DW_AT_GNU_denominator,
12177 fpt_info.scale_factor.arbitrary.denominator);
12179 add_AT_die_ref (base_type_result, DW_AT_small, scale_factor);
12181 break;
12183 default:
12184 gcc_unreachable ();
12188 if (type_bias)
12189 add_scalar_info (base_type_result, DW_AT_GNU_bias, type_bias,
12190 dw_scalar_form_constant
12191 | dw_scalar_form_exprloc
12192 | dw_scalar_form_reference,
12193 NULL);
12195 add_pubtype (type, base_type_result);
12197 return base_type_result;
12200 /* A C++ function with deduced return type can have a TEMPLATE_TYPE_PARM
12201 named 'auto' in its type: return true for it, false otherwise. */
12203 static inline bool
12204 is_cxx_auto (tree type)
12206 if (is_cxx ())
12208 tree name = TYPE_IDENTIFIER (type);
12209 if (name == get_identifier ("auto")
12210 || name == get_identifier ("decltype(auto)"))
12211 return true;
12213 return false;
12216 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
12217 given input type is a Dwarf "fundamental" type. Otherwise return null. */
12219 static inline int
12220 is_base_type (tree type)
12222 switch (TREE_CODE (type))
12224 case ERROR_MARK:
12225 case VOID_TYPE:
12226 case INTEGER_TYPE:
12227 case REAL_TYPE:
12228 case FIXED_POINT_TYPE:
12229 case COMPLEX_TYPE:
12230 case BOOLEAN_TYPE:
12231 case POINTER_BOUNDS_TYPE:
12232 return 1;
12234 case ARRAY_TYPE:
12235 case RECORD_TYPE:
12236 case UNION_TYPE:
12237 case QUAL_UNION_TYPE:
12238 case ENUMERAL_TYPE:
12239 case FUNCTION_TYPE:
12240 case METHOD_TYPE:
12241 case POINTER_TYPE:
12242 case REFERENCE_TYPE:
12243 case NULLPTR_TYPE:
12244 case OFFSET_TYPE:
12245 case LANG_TYPE:
12246 case VECTOR_TYPE:
12247 return 0;
12249 default:
12250 if (is_cxx_auto (type))
12251 return 0;
12252 gcc_unreachable ();
12255 return 0;
12258 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
12259 node, return the size in bits for the type if it is a constant, or else
12260 return the alignment for the type if the type's size is not constant, or
12261 else return BITS_PER_WORD if the type actually turns out to be an
12262 ERROR_MARK node. */
12264 static inline unsigned HOST_WIDE_INT
12265 simple_type_size_in_bits (const_tree type)
12267 if (TREE_CODE (type) == ERROR_MARK)
12268 return BITS_PER_WORD;
12269 else if (TYPE_SIZE (type) == NULL_TREE)
12270 return 0;
12271 else if (tree_fits_uhwi_p (TYPE_SIZE (type)))
12272 return tree_to_uhwi (TYPE_SIZE (type));
12273 else
12274 return TYPE_ALIGN (type);
12277 /* Similarly, but return an offset_int instead of UHWI. */
12279 static inline offset_int
12280 offset_int_type_size_in_bits (const_tree type)
12282 if (TREE_CODE (type) == ERROR_MARK)
12283 return BITS_PER_WORD;
12284 else if (TYPE_SIZE (type) == NULL_TREE)
12285 return 0;
12286 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
12287 return wi::to_offset (TYPE_SIZE (type));
12288 else
12289 return TYPE_ALIGN (type);
12292 /* Given a pointer to a tree node for a subrange type, return a pointer
12293 to a DIE that describes the given type. */
12295 static dw_die_ref
12296 subrange_type_die (tree type, tree low, tree high, tree bias,
12297 dw_die_ref context_die)
12299 dw_die_ref subrange_die;
12300 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
12302 if (context_die == NULL)
12303 context_die = comp_unit_die ();
12305 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
12307 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
12309 /* The size of the subrange type and its base type do not match,
12310 so we need to generate a size attribute for the subrange type. */
12311 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
12314 add_alignment_attribute (subrange_die, type);
12316 if (low)
12317 add_bound_info (subrange_die, DW_AT_lower_bound, low, NULL);
12318 if (high)
12319 add_bound_info (subrange_die, DW_AT_upper_bound, high, NULL);
12320 if (bias && !dwarf_strict)
12321 add_scalar_info (subrange_die, DW_AT_GNU_bias, bias,
12322 dw_scalar_form_constant
12323 | dw_scalar_form_exprloc
12324 | dw_scalar_form_reference,
12325 NULL);
12327 return subrange_die;
12330 /* Returns the (const and/or volatile) cv_qualifiers associated with
12331 the decl node. This will normally be augmented with the
12332 cv_qualifiers of the underlying type in add_type_attribute. */
12334 static int
12335 decl_quals (const_tree decl)
12337 return ((TREE_READONLY (decl)
12338 /* The C++ front-end correctly marks reference-typed
12339 variables as readonly, but from a language (and debug
12340 info) standpoint they are not const-qualified. */
12341 && TREE_CODE (TREE_TYPE (decl)) != REFERENCE_TYPE
12342 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED)
12343 | (TREE_THIS_VOLATILE (decl)
12344 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED));
12347 /* Determine the TYPE whose qualifiers match the largest strict subset
12348 of the given TYPE_QUALS, and return its qualifiers. Ignore all
12349 qualifiers outside QUAL_MASK. */
12351 static int
12352 get_nearest_type_subqualifiers (tree type, int type_quals, int qual_mask)
12354 tree t;
12355 int best_rank = 0, best_qual = 0, max_rank;
12357 type_quals &= qual_mask;
12358 max_rank = popcount_hwi (type_quals) - 1;
12360 for (t = TYPE_MAIN_VARIANT (type); t && best_rank < max_rank;
12361 t = TYPE_NEXT_VARIANT (t))
12363 int q = TYPE_QUALS (t) & qual_mask;
12365 if ((q & type_quals) == q && q != type_quals
12366 && check_base_type (t, type))
12368 int rank = popcount_hwi (q);
12370 if (rank > best_rank)
12372 best_rank = rank;
12373 best_qual = q;
12378 return best_qual;
12381 struct dwarf_qual_info_t { int q; enum dwarf_tag t; };
12382 static const dwarf_qual_info_t dwarf_qual_info[] =
12384 { TYPE_QUAL_CONST, DW_TAG_const_type },
12385 { TYPE_QUAL_VOLATILE, DW_TAG_volatile_type },
12386 { TYPE_QUAL_RESTRICT, DW_TAG_restrict_type },
12387 { TYPE_QUAL_ATOMIC, DW_TAG_atomic_type }
12389 static const unsigned int dwarf_qual_info_size
12390 = sizeof (dwarf_qual_info) / sizeof (dwarf_qual_info[0]);
12392 /* If DIE is a qualified DIE of some base DIE with the same parent,
12393 return the base DIE, otherwise return NULL. Set MASK to the
12394 qualifiers added compared to the returned DIE. */
12396 static dw_die_ref
12397 qualified_die_p (dw_die_ref die, int *mask, unsigned int depth)
12399 unsigned int i;
12400 for (i = 0; i < dwarf_qual_info_size; i++)
12401 if (die->die_tag == dwarf_qual_info[i].t)
12402 break;
12403 if (i == dwarf_qual_info_size)
12404 return NULL;
12405 if (vec_safe_length (die->die_attr) != 1)
12406 return NULL;
12407 dw_die_ref type = get_AT_ref (die, DW_AT_type);
12408 if (type == NULL || type->die_parent != die->die_parent)
12409 return NULL;
12410 *mask |= dwarf_qual_info[i].q;
12411 if (depth)
12413 dw_die_ref ret = qualified_die_p (type, mask, depth - 1);
12414 if (ret)
12415 return ret;
12417 return type;
12420 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
12421 entry that chains the modifiers specified by CV_QUALS in front of the
12422 given type. REVERSE is true if the type is to be interpreted in the
12423 reverse storage order wrt the target order. */
12425 static dw_die_ref
12426 modified_type_die (tree type, int cv_quals, bool reverse,
12427 dw_die_ref context_die)
12429 enum tree_code code = TREE_CODE (type);
12430 dw_die_ref mod_type_die;
12431 dw_die_ref sub_die = NULL;
12432 tree item_type = NULL;
12433 tree qualified_type;
12434 tree name, low, high;
12435 dw_die_ref mod_scope;
12436 /* Only these cv-qualifiers are currently handled. */
12437 const int cv_qual_mask = (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE
12438 | TYPE_QUAL_RESTRICT | TYPE_QUAL_ATOMIC);
12440 if (code == ERROR_MARK)
12441 return NULL;
12443 if (lang_hooks.types.get_debug_type)
12445 tree debug_type = lang_hooks.types.get_debug_type (type);
12447 if (debug_type != NULL_TREE && debug_type != type)
12448 return modified_type_die (debug_type, cv_quals, reverse, context_die);
12451 cv_quals &= cv_qual_mask;
12453 /* Don't emit DW_TAG_restrict_type for DWARFv2, since it is a type
12454 tag modifier (and not an attribute) old consumers won't be able
12455 to handle it. */
12456 if (dwarf_version < 3)
12457 cv_quals &= ~TYPE_QUAL_RESTRICT;
12459 /* Likewise for DW_TAG_atomic_type for DWARFv5. */
12460 if (dwarf_version < 5)
12461 cv_quals &= ~TYPE_QUAL_ATOMIC;
12463 /* See if we already have the appropriately qualified variant of
12464 this type. */
12465 qualified_type = get_qualified_type (type, cv_quals);
12467 if (qualified_type == sizetype)
12469 /* Try not to expose the internal sizetype type's name. */
12470 if (TYPE_NAME (qualified_type)
12471 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
12473 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
12475 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
12476 && (TYPE_PRECISION (t)
12477 == TYPE_PRECISION (qualified_type))
12478 && (TYPE_UNSIGNED (t)
12479 == TYPE_UNSIGNED (qualified_type)));
12480 qualified_type = t;
12482 else if (qualified_type == sizetype
12483 && TREE_CODE (sizetype) == TREE_CODE (size_type_node)
12484 && TYPE_PRECISION (sizetype) == TYPE_PRECISION (size_type_node)
12485 && TYPE_UNSIGNED (sizetype) == TYPE_UNSIGNED (size_type_node))
12486 qualified_type = size_type_node;
12490 /* If we do, then we can just use its DIE, if it exists. */
12491 if (qualified_type)
12493 mod_type_die = lookup_type_die (qualified_type);
12495 /* DW_AT_endianity doesn't come from a qualifier on the type. */
12496 if (mod_type_die
12497 && (!need_endianity_attribute_p (reverse)
12498 || !is_base_type (type)
12499 || get_AT_unsigned (mod_type_die, DW_AT_endianity)))
12500 return mod_type_die;
12503 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
12505 /* Handle C typedef types. */
12506 if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
12507 && !DECL_ARTIFICIAL (name))
12509 tree dtype = TREE_TYPE (name);
12511 if (qualified_type == dtype)
12513 /* For a named type, use the typedef. */
12514 gen_type_die (qualified_type, context_die);
12515 return lookup_type_die (qualified_type);
12517 else
12519 int dquals = TYPE_QUALS_NO_ADDR_SPACE (dtype);
12520 dquals &= cv_qual_mask;
12521 if ((dquals & ~cv_quals) != TYPE_UNQUALIFIED
12522 || (cv_quals == dquals && DECL_ORIGINAL_TYPE (name) != type))
12523 /* cv-unqualified version of named type. Just use
12524 the unnamed type to which it refers. */
12525 return modified_type_die (DECL_ORIGINAL_TYPE (name), cv_quals,
12526 reverse, context_die);
12527 /* Else cv-qualified version of named type; fall through. */
12531 mod_scope = scope_die_for (type, context_die);
12533 if (cv_quals)
12535 int sub_quals = 0, first_quals = 0;
12536 unsigned i;
12537 dw_die_ref first = NULL, last = NULL;
12539 /* Determine a lesser qualified type that most closely matches
12540 this one. Then generate DW_TAG_* entries for the remaining
12541 qualifiers. */
12542 sub_quals = get_nearest_type_subqualifiers (type, cv_quals,
12543 cv_qual_mask);
12544 if (sub_quals && use_debug_types)
12546 bool needed = false;
12547 /* If emitting type units, make sure the order of qualifiers
12548 is canonical. Thus, start from unqualified type if
12549 an earlier qualifier is missing in sub_quals, but some later
12550 one is present there. */
12551 for (i = 0; i < dwarf_qual_info_size; i++)
12552 if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
12553 needed = true;
12554 else if (needed && (dwarf_qual_info[i].q & cv_quals))
12556 sub_quals = 0;
12557 break;
12560 mod_type_die = modified_type_die (type, sub_quals, reverse, context_die);
12561 if (mod_scope && mod_type_die && mod_type_die->die_parent == mod_scope)
12563 /* As not all intermediate qualified DIEs have corresponding
12564 tree types, ensure that qualified DIEs in the same scope
12565 as their DW_AT_type are emitted after their DW_AT_type,
12566 only with other qualified DIEs for the same type possibly
12567 in between them. Determine the range of such qualified
12568 DIEs now (first being the base type, last being corresponding
12569 last qualified DIE for it). */
12570 unsigned int count = 0;
12571 first = qualified_die_p (mod_type_die, &first_quals,
12572 dwarf_qual_info_size);
12573 if (first == NULL)
12574 first = mod_type_die;
12575 gcc_assert ((first_quals & ~sub_quals) == 0);
12576 for (count = 0, last = first;
12577 count < (1U << dwarf_qual_info_size);
12578 count++, last = last->die_sib)
12580 int quals = 0;
12581 if (last == mod_scope->die_child)
12582 break;
12583 if (qualified_die_p (last->die_sib, &quals, dwarf_qual_info_size)
12584 != first)
12585 break;
12589 for (i = 0; i < dwarf_qual_info_size; i++)
12590 if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
12592 dw_die_ref d;
12593 if (first && first != last)
12595 for (d = first->die_sib; ; d = d->die_sib)
12597 int quals = 0;
12598 qualified_die_p (d, &quals, dwarf_qual_info_size);
12599 if (quals == (first_quals | dwarf_qual_info[i].q))
12600 break;
12601 if (d == last)
12603 d = NULL;
12604 break;
12607 if (d)
12609 mod_type_die = d;
12610 continue;
12613 if (first)
12615 d = ggc_cleared_alloc<die_node> ();
12616 d->die_tag = dwarf_qual_info[i].t;
12617 add_child_die_after (mod_scope, d, last);
12618 last = d;
12620 else
12621 d = new_die (dwarf_qual_info[i].t, mod_scope, type);
12622 if (mod_type_die)
12623 add_AT_die_ref (d, DW_AT_type, mod_type_die);
12624 mod_type_die = d;
12625 first_quals |= dwarf_qual_info[i].q;
12628 else if (code == POINTER_TYPE || code == REFERENCE_TYPE)
12630 dwarf_tag tag = DW_TAG_pointer_type;
12631 if (code == REFERENCE_TYPE)
12633 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
12634 tag = DW_TAG_rvalue_reference_type;
12635 else
12636 tag = DW_TAG_reference_type;
12638 mod_type_die = new_die (tag, mod_scope, type);
12640 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
12641 simple_type_size_in_bits (type) / BITS_PER_UNIT);
12642 add_alignment_attribute (mod_type_die, type);
12643 item_type = TREE_TYPE (type);
12645 addr_space_t as = TYPE_ADDR_SPACE (item_type);
12646 if (!ADDR_SPACE_GENERIC_P (as))
12648 int action = targetm.addr_space.debug (as);
12649 if (action >= 0)
12651 /* Positive values indicate an address_class. */
12652 add_AT_unsigned (mod_type_die, DW_AT_address_class, action);
12654 else
12656 /* Negative values indicate an (inverted) segment base reg. */
12657 dw_loc_descr_ref d
12658 = one_reg_loc_descriptor (~action, VAR_INIT_STATUS_INITIALIZED);
12659 add_AT_loc (mod_type_die, DW_AT_segment, d);
12663 else if (code == INTEGER_TYPE
12664 && TREE_TYPE (type) != NULL_TREE
12665 && subrange_type_for_debug_p (type, &low, &high))
12667 tree bias = NULL_TREE;
12668 if (lang_hooks.types.get_type_bias)
12669 bias = lang_hooks.types.get_type_bias (type);
12670 mod_type_die = subrange_type_die (type, low, high, bias, context_die);
12671 item_type = TREE_TYPE (type);
12673 else if (is_base_type (type))
12674 mod_type_die = base_type_die (type, reverse);
12675 else
12677 gen_type_die (type, context_die);
12679 /* We have to get the type_main_variant here (and pass that to the
12680 `lookup_type_die' routine) because the ..._TYPE node we have
12681 might simply be a *copy* of some original type node (where the
12682 copy was created to help us keep track of typedef names) and
12683 that copy might have a different TYPE_UID from the original
12684 ..._TYPE node. */
12685 if (TREE_CODE (type) == FUNCTION_TYPE
12686 || TREE_CODE (type) == METHOD_TYPE)
12688 /* For function/method types, can't just use type_main_variant here,
12689 because that can have different ref-qualifiers for C++,
12690 but try to canonicalize. */
12691 tree main = TYPE_MAIN_VARIANT (type);
12692 for (tree t = main; t; t = TYPE_NEXT_VARIANT (t))
12693 if (check_base_type (t, main) && check_lang_type (t, type))
12694 return lookup_type_die (t);
12695 return lookup_type_die (type);
12697 else if (TREE_CODE (type) != VECTOR_TYPE
12698 && TREE_CODE (type) != ARRAY_TYPE)
12699 return lookup_type_die (type_main_variant (type));
12700 else
12701 /* Vectors have the debugging information in the type,
12702 not the main variant. */
12703 return lookup_type_die (type);
12706 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
12707 don't output a DW_TAG_typedef, since there isn't one in the
12708 user's program; just attach a DW_AT_name to the type.
12709 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
12710 if the base type already has the same name. */
12711 if (name
12712 && ((TREE_CODE (name) != TYPE_DECL
12713 && (qualified_type == TYPE_MAIN_VARIANT (type)
12714 || (cv_quals == TYPE_UNQUALIFIED)))
12715 || (TREE_CODE (name) == TYPE_DECL
12716 && TREE_TYPE (name) == qualified_type
12717 && DECL_NAME (name))))
12719 if (TREE_CODE (name) == TYPE_DECL)
12720 /* Could just call add_name_and_src_coords_attributes here,
12721 but since this is a builtin type it doesn't have any
12722 useful source coordinates anyway. */
12723 name = DECL_NAME (name);
12724 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
12726 /* This probably indicates a bug. */
12727 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
12729 name = TYPE_IDENTIFIER (type);
12730 add_name_attribute (mod_type_die,
12731 name ? IDENTIFIER_POINTER (name) : "__unknown__");
12734 if (qualified_type)
12735 equate_type_number_to_die (qualified_type, mod_type_die);
12737 if (item_type)
12738 /* We must do this after the equate_type_number_to_die call, in case
12739 this is a recursive type. This ensures that the modified_type_die
12740 recursion will terminate even if the type is recursive. Recursive
12741 types are possible in Ada. */
12742 sub_die = modified_type_die (item_type,
12743 TYPE_QUALS_NO_ADDR_SPACE (item_type),
12744 reverse,
12745 context_die);
12747 if (sub_die != NULL)
12748 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
12750 add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
12751 if (TYPE_ARTIFICIAL (type))
12752 add_AT_flag (mod_type_die, DW_AT_artificial, 1);
12754 return mod_type_die;
12757 /* Generate DIEs for the generic parameters of T.
12758 T must be either a generic type or a generic function.
12759 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
12761 static void
12762 gen_generic_params_dies (tree t)
12764 tree parms, args;
12765 int parms_num, i;
12766 dw_die_ref die = NULL;
12767 int non_default;
12769 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
12770 return;
12772 if (TYPE_P (t))
12773 die = lookup_type_die (t);
12774 else if (DECL_P (t))
12775 die = lookup_decl_die (t);
12777 gcc_assert (die);
12779 parms = lang_hooks.get_innermost_generic_parms (t);
12780 if (!parms)
12781 /* T has no generic parameter. It means T is neither a generic type
12782 or function. End of story. */
12783 return;
12785 parms_num = TREE_VEC_LENGTH (parms);
12786 args = lang_hooks.get_innermost_generic_args (t);
12787 if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
12788 non_default = int_cst_value (TREE_CHAIN (args));
12789 else
12790 non_default = TREE_VEC_LENGTH (args);
12791 for (i = 0; i < parms_num; i++)
12793 tree parm, arg, arg_pack_elems;
12794 dw_die_ref parm_die;
12796 parm = TREE_VEC_ELT (parms, i);
12797 arg = TREE_VEC_ELT (args, i);
12798 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
12799 gcc_assert (parm && TREE_VALUE (parm) && arg);
12801 if (parm && TREE_VALUE (parm) && arg)
12803 /* If PARM represents a template parameter pack,
12804 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
12805 by DW_TAG_template_*_parameter DIEs for the argument
12806 pack elements of ARG. Note that ARG would then be
12807 an argument pack. */
12808 if (arg_pack_elems)
12809 parm_die = template_parameter_pack_die (TREE_VALUE (parm),
12810 arg_pack_elems,
12811 die);
12812 else
12813 parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
12814 true /* emit name */, die);
12815 if (i >= non_default)
12816 add_AT_flag (parm_die, DW_AT_default_value, 1);
12821 /* Create and return a DIE for PARM which should be
12822 the representation of a generic type parameter.
12823 For instance, in the C++ front end, PARM would be a template parameter.
12824 ARG is the argument to PARM.
12825 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
12826 name of the PARM.
12827 PARENT_DIE is the parent DIE which the new created DIE should be added to,
12828 as a child node. */
12830 static dw_die_ref
12831 generic_parameter_die (tree parm, tree arg,
12832 bool emit_name_p,
12833 dw_die_ref parent_die)
12835 dw_die_ref tmpl_die = NULL;
12836 const char *name = NULL;
12838 if (!parm || !DECL_NAME (parm) || !arg)
12839 return NULL;
12841 /* We support non-type generic parameters and arguments,
12842 type generic parameters and arguments, as well as
12843 generic generic parameters (a.k.a. template template parameters in C++)
12844 and arguments. */
12845 if (TREE_CODE (parm) == PARM_DECL)
12846 /* PARM is a nontype generic parameter */
12847 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
12848 else if (TREE_CODE (parm) == TYPE_DECL)
12849 /* PARM is a type generic parameter. */
12850 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
12851 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
12852 /* PARM is a generic generic parameter.
12853 Its DIE is a GNU extension. It shall have a
12854 DW_AT_name attribute to represent the name of the template template
12855 parameter, and a DW_AT_GNU_template_name attribute to represent the
12856 name of the template template argument. */
12857 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
12858 parent_die, parm);
12859 else
12860 gcc_unreachable ();
12862 if (tmpl_die)
12864 tree tmpl_type;
12866 /* If PARM is a generic parameter pack, it means we are
12867 emitting debug info for a template argument pack element.
12868 In other terms, ARG is a template argument pack element.
12869 In that case, we don't emit any DW_AT_name attribute for
12870 the die. */
12871 if (emit_name_p)
12873 name = IDENTIFIER_POINTER (DECL_NAME (parm));
12874 gcc_assert (name);
12875 add_AT_string (tmpl_die, DW_AT_name, name);
12878 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
12880 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
12881 TMPL_DIE should have a child DW_AT_type attribute that is set
12882 to the type of the argument to PARM, which is ARG.
12883 If PARM is a type generic parameter, TMPL_DIE should have a
12884 child DW_AT_type that is set to ARG. */
12885 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
12886 add_type_attribute (tmpl_die, tmpl_type,
12887 (TREE_THIS_VOLATILE (tmpl_type)
12888 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED),
12889 false, parent_die);
12891 else
12893 /* So TMPL_DIE is a DIE representing a
12894 a generic generic template parameter, a.k.a template template
12895 parameter in C++ and arg is a template. */
12897 /* The DW_AT_GNU_template_name attribute of the DIE must be set
12898 to the name of the argument. */
12899 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
12900 if (name)
12901 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
12904 if (TREE_CODE (parm) == PARM_DECL)
12905 /* So PARM is a non-type generic parameter.
12906 DWARF3 5.6.8 says we must set a DW_AT_const_value child
12907 attribute of TMPL_DIE which value represents the value
12908 of ARG.
12909 We must be careful here:
12910 The value of ARG might reference some function decls.
12911 We might currently be emitting debug info for a generic
12912 type and types are emitted before function decls, we don't
12913 know if the function decls referenced by ARG will actually be
12914 emitted after cgraph computations.
12915 So must defer the generation of the DW_AT_const_value to
12916 after cgraph is ready. */
12917 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
12920 return tmpl_die;
12923 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
12924 PARM_PACK must be a template parameter pack. The returned DIE
12925 will be child DIE of PARENT_DIE. */
12927 static dw_die_ref
12928 template_parameter_pack_die (tree parm_pack,
12929 tree parm_pack_args,
12930 dw_die_ref parent_die)
12932 dw_die_ref die;
12933 int j;
12935 gcc_assert (parent_die && parm_pack);
12937 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
12938 add_name_and_src_coords_attributes (die, parm_pack);
12939 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
12940 generic_parameter_die (parm_pack,
12941 TREE_VEC_ELT (parm_pack_args, j),
12942 false /* Don't emit DW_AT_name */,
12943 die);
12944 return die;
12947 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
12948 an enumerated type. */
12950 static inline int
12951 type_is_enum (const_tree type)
12953 return TREE_CODE (type) == ENUMERAL_TYPE;
12956 /* Return the DBX register number described by a given RTL node. */
12958 static unsigned int
12959 dbx_reg_number (const_rtx rtl)
12961 unsigned regno = REGNO (rtl);
12963 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
12965 #ifdef LEAF_REG_REMAP
12966 if (crtl->uses_only_leaf_regs)
12968 int leaf_reg = LEAF_REG_REMAP (regno);
12969 if (leaf_reg != -1)
12970 regno = (unsigned) leaf_reg;
12972 #endif
12974 regno = DBX_REGISTER_NUMBER (regno);
12975 gcc_assert (regno != INVALID_REGNUM);
12976 return regno;
12979 /* Optionally add a DW_OP_piece term to a location description expression.
12980 DW_OP_piece is only added if the location description expression already
12981 doesn't end with DW_OP_piece. */
12983 static void
12984 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
12986 dw_loc_descr_ref loc;
12988 if (*list_head != NULL)
12990 /* Find the end of the chain. */
12991 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
12994 if (loc->dw_loc_opc != DW_OP_piece)
12995 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
12999 /* Return a location descriptor that designates a machine register or
13000 zero if there is none. */
13002 static dw_loc_descr_ref
13003 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
13005 rtx regs;
13007 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
13008 return 0;
13010 /* We only use "frame base" when we're sure we're talking about the
13011 post-prologue local stack frame. We do this by *not* running
13012 register elimination until this point, and recognizing the special
13013 argument pointer and soft frame pointer rtx's.
13014 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
13015 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
13016 && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
13018 dw_loc_descr_ref result = NULL;
13020 if (dwarf_version >= 4 || !dwarf_strict)
13022 result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
13023 initialized);
13024 if (result)
13025 add_loc_descr (&result,
13026 new_loc_descr (DW_OP_stack_value, 0, 0));
13028 return result;
13031 regs = targetm.dwarf_register_span (rtl);
13033 if (REG_NREGS (rtl) > 1 || regs)
13034 return multiple_reg_loc_descriptor (rtl, regs, initialized);
13035 else
13037 unsigned int dbx_regnum = dbx_reg_number (rtl);
13038 if (dbx_regnum == IGNORED_DWARF_REGNUM)
13039 return 0;
13040 return one_reg_loc_descriptor (dbx_regnum, initialized);
13044 /* Return a location descriptor that designates a machine register for
13045 a given hard register number. */
13047 static dw_loc_descr_ref
13048 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
13050 dw_loc_descr_ref reg_loc_descr;
13052 if (regno <= 31)
13053 reg_loc_descr
13054 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
13055 else
13056 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
13058 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13059 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13061 return reg_loc_descr;
13064 /* Given an RTL of a register, return a location descriptor that
13065 designates a value that spans more than one register. */
13067 static dw_loc_descr_ref
13068 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
13069 enum var_init_status initialized)
13071 int size, i;
13072 dw_loc_descr_ref loc_result = NULL;
13074 /* Simple, contiguous registers. */
13075 if (regs == NULL_RTX)
13077 unsigned reg = REGNO (rtl);
13078 int nregs;
13080 #ifdef LEAF_REG_REMAP
13081 if (crtl->uses_only_leaf_regs)
13083 int leaf_reg = LEAF_REG_REMAP (reg);
13084 if (leaf_reg != -1)
13085 reg = (unsigned) leaf_reg;
13087 #endif
13089 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
13090 nregs = REG_NREGS (rtl);
13092 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
13094 loc_result = NULL;
13095 while (nregs--)
13097 dw_loc_descr_ref t;
13099 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
13100 VAR_INIT_STATUS_INITIALIZED);
13101 add_loc_descr (&loc_result, t);
13102 add_loc_descr_op_piece (&loc_result, size);
13103 ++reg;
13105 return loc_result;
13108 /* Now onto stupid register sets in non contiguous locations. */
13110 gcc_assert (GET_CODE (regs) == PARALLEL);
13112 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
13113 loc_result = NULL;
13115 for (i = 0; i < XVECLEN (regs, 0); ++i)
13117 dw_loc_descr_ref t;
13119 t = one_reg_loc_descriptor (dbx_reg_number (XVECEXP (regs, 0, i)),
13120 VAR_INIT_STATUS_INITIALIZED);
13121 add_loc_descr (&loc_result, t);
13122 add_loc_descr_op_piece (&loc_result, size);
13125 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13126 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13127 return loc_result;
13130 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
13132 /* Return a location descriptor that designates a constant i,
13133 as a compound operation from constant (i >> shift), constant shift
13134 and DW_OP_shl. */
13136 static dw_loc_descr_ref
13137 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
13139 dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
13140 add_loc_descr (&ret, int_loc_descriptor (shift));
13141 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
13142 return ret;
13145 /* Return a location descriptor that designates a constant. */
13147 static dw_loc_descr_ref
13148 int_loc_descriptor (HOST_WIDE_INT i)
13150 enum dwarf_location_atom op;
13152 /* Pick the smallest representation of a constant, rather than just
13153 defaulting to the LEB encoding. */
13154 if (i >= 0)
13156 int clz = clz_hwi (i);
13157 int ctz = ctz_hwi (i);
13158 if (i <= 31)
13159 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
13160 else if (i <= 0xff)
13161 op = DW_OP_const1u;
13162 else if (i <= 0xffff)
13163 op = DW_OP_const2u;
13164 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
13165 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
13166 /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
13167 DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
13168 while DW_OP_const4u is 5 bytes. */
13169 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
13170 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13171 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
13172 /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
13173 while DW_OP_const4u is 5 bytes. */
13174 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
13176 else if (DWARF2_ADDR_SIZE == 4 && i > 0x7fffffff
13177 && size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i)
13178 <= 4)
13180 /* As i >= 2**31, the double cast above will yield a negative number.
13181 Since wrapping is defined in DWARF expressions we can output big
13182 positive integers as small negative ones, regardless of the size
13183 of host wide ints.
13185 Here, since the evaluator will handle 32-bit values and since i >=
13186 2**31, we know it's going to be interpreted as a negative literal:
13187 store it this way if we can do better than 5 bytes this way. */
13188 return int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i);
13190 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
13191 op = DW_OP_const4u;
13193 /* Past this point, i >= 0x100000000 and thus DW_OP_constu will take at
13194 least 6 bytes: see if we can do better before falling back to it. */
13195 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13196 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
13197 /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes. */
13198 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
13199 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
13200 && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
13201 >= HOST_BITS_PER_WIDE_INT)
13202 /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
13203 DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes. */
13204 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
13205 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
13206 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
13207 && size_of_uleb128 (i) > 6)
13208 /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes. */
13209 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
13210 else
13211 op = DW_OP_constu;
13213 else
13215 if (i >= -0x80)
13216 op = DW_OP_const1s;
13217 else if (i >= -0x8000)
13218 op = DW_OP_const2s;
13219 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
13221 if (size_of_int_loc_descriptor (i) < 5)
13223 dw_loc_descr_ref ret = int_loc_descriptor (-i);
13224 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
13225 return ret;
13227 op = DW_OP_const4s;
13229 else
13231 if (size_of_int_loc_descriptor (i)
13232 < (unsigned long) 1 + size_of_sleb128 (i))
13234 dw_loc_descr_ref ret = int_loc_descriptor (-i);
13235 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
13236 return ret;
13238 op = DW_OP_consts;
13242 return new_loc_descr (op, i, 0);
13245 /* Likewise, for unsigned constants. */
13247 static dw_loc_descr_ref
13248 uint_loc_descriptor (unsigned HOST_WIDE_INT i)
13250 const unsigned HOST_WIDE_INT max_int = INTTYPE_MAXIMUM (HOST_WIDE_INT);
13251 const unsigned HOST_WIDE_INT max_uint
13252 = INTTYPE_MAXIMUM (unsigned HOST_WIDE_INT);
13254 /* If possible, use the clever signed constants handling. */
13255 if (i <= max_int)
13256 return int_loc_descriptor ((HOST_WIDE_INT) i);
13258 /* Here, we are left with positive numbers that cannot be represented as
13259 HOST_WIDE_INT, i.e.:
13260 max (HOST_WIDE_INT) < i <= max (unsigned HOST_WIDE_INT)
13262 Using DW_OP_const4/8/./u operation to encode them consumes a lot of bytes
13263 whereas may be better to output a negative integer: thanks to integer
13264 wrapping, we know that:
13265 x = x - 2 ** DWARF2_ADDR_SIZE
13266 = x - 2 * (max (HOST_WIDE_INT) + 1)
13267 So numbers close to max (unsigned HOST_WIDE_INT) could be represented as
13268 small negative integers. Let's try that in cases it will clearly improve
13269 the encoding: there is no gain turning DW_OP_const4u into
13270 DW_OP_const4s. */
13271 if (DWARF2_ADDR_SIZE * 8 == HOST_BITS_PER_WIDE_INT
13272 && ((DWARF2_ADDR_SIZE == 4 && i > max_uint - 0x8000)
13273 || (DWARF2_ADDR_SIZE == 8 && i > max_uint - 0x80000000)))
13275 const unsigned HOST_WIDE_INT first_shift = i - max_int - 1;
13277 /* Now, -1 < first_shift <= max (HOST_WIDE_INT)
13278 i.e. 0 <= first_shift <= max (HOST_WIDE_INT). */
13279 const HOST_WIDE_INT second_shift
13280 = (HOST_WIDE_INT) first_shift - (HOST_WIDE_INT) max_int - 1;
13282 /* So we finally have:
13283 -max (HOST_WIDE_INT) - 1 <= second_shift <= -1.
13284 i.e. min (HOST_WIDE_INT) <= second_shift < 0. */
13285 return int_loc_descriptor (second_shift);
13288 /* Last chance: fallback to a simple constant operation. */
13289 return new_loc_descr
13290 ((HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
13291 ? DW_OP_const4u
13292 : DW_OP_const8u,
13293 i, 0);
13296 /* Generate and return a location description that computes the unsigned
13297 comparison of the two stack top entries (a OP b where b is the top-most
13298 entry and a is the second one). The KIND of comparison can be LT_EXPR,
13299 LE_EXPR, GT_EXPR or GE_EXPR. */
13301 static dw_loc_descr_ref
13302 uint_comparison_loc_list (enum tree_code kind)
13304 enum dwarf_location_atom op, flip_op;
13305 dw_loc_descr_ref ret, bra_node, jmp_node, tmp;
13307 switch (kind)
13309 case LT_EXPR:
13310 op = DW_OP_lt;
13311 break;
13312 case LE_EXPR:
13313 op = DW_OP_le;
13314 break;
13315 case GT_EXPR:
13316 op = DW_OP_gt;
13317 break;
13318 case GE_EXPR:
13319 op = DW_OP_ge;
13320 break;
13321 default:
13322 gcc_unreachable ();
13325 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
13326 jmp_node = new_loc_descr (DW_OP_skip, 0, 0);
13328 /* Until DWARFv4, operations all work on signed integers. It is nevertheless
13329 possible to perform unsigned comparisons: we just have to distinguish
13330 three cases:
13332 1. when a and b have the same sign (as signed integers); then we should
13333 return: a OP(signed) b;
13335 2. when a is a negative signed integer while b is a positive one, then a
13336 is a greater unsigned integer than b; likewise when a and b's roles
13337 are flipped.
13339 So first, compare the sign of the two operands. */
13340 ret = new_loc_descr (DW_OP_over, 0, 0);
13341 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
13342 add_loc_descr (&ret, new_loc_descr (DW_OP_xor, 0, 0));
13343 /* If they have different signs (i.e. they have different sign bits), then
13344 the stack top value has now the sign bit set and thus it's smaller than
13345 zero. */
13346 add_loc_descr (&ret, new_loc_descr (DW_OP_lit0, 0, 0));
13347 add_loc_descr (&ret, new_loc_descr (DW_OP_lt, 0, 0));
13348 add_loc_descr (&ret, bra_node);
13350 /* We are in case 1. At this point, we know both operands have the same
13351 sign, to it's safe to use the built-in signed comparison. */
13352 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
13353 add_loc_descr (&ret, jmp_node);
13355 /* We are in case 2. Here, we know both operands do not have the same sign,
13356 so we have to flip the signed comparison. */
13357 flip_op = (kind == LT_EXPR || kind == LE_EXPR) ? DW_OP_gt : DW_OP_lt;
13358 tmp = new_loc_descr (flip_op, 0, 0);
13359 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13360 bra_node->dw_loc_oprnd1.v.val_loc = tmp;
13361 add_loc_descr (&ret, tmp);
13363 /* This dummy operation is necessary to make the two branches join. */
13364 tmp = new_loc_descr (DW_OP_nop, 0, 0);
13365 jmp_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13366 jmp_node->dw_loc_oprnd1.v.val_loc = tmp;
13367 add_loc_descr (&ret, tmp);
13369 return ret;
13372 /* Likewise, but takes the location description lists (might be destructive on
13373 them). Return NULL if either is NULL or if concatenation fails. */
13375 static dw_loc_list_ref
13376 loc_list_from_uint_comparison (dw_loc_list_ref left, dw_loc_list_ref right,
13377 enum tree_code kind)
13379 if (left == NULL || right == NULL)
13380 return NULL;
13382 add_loc_list (&left, right);
13383 if (left == NULL)
13384 return NULL;
13386 add_loc_descr_to_each (left, uint_comparison_loc_list (kind));
13387 return left;
13390 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
13391 without actually allocating it. */
13393 static unsigned long
13394 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
13396 return size_of_int_loc_descriptor (i >> shift)
13397 + size_of_int_loc_descriptor (shift)
13398 + 1;
13401 /* Return size_of_locs (int_loc_descriptor (i)) without
13402 actually allocating it. */
13404 static unsigned long
13405 size_of_int_loc_descriptor (HOST_WIDE_INT i)
13407 unsigned long s;
13409 if (i >= 0)
13411 int clz, ctz;
13412 if (i <= 31)
13413 return 1;
13414 else if (i <= 0xff)
13415 return 2;
13416 else if (i <= 0xffff)
13417 return 3;
13418 clz = clz_hwi (i);
13419 ctz = ctz_hwi (i);
13420 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
13421 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
13422 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13423 - clz - 5);
13424 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13425 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
13426 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13427 - clz - 8);
13428 else if (DWARF2_ADDR_SIZE == 4 && i > 0x7fffffff
13429 && size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i)
13430 <= 4)
13431 return size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i);
13432 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
13433 return 5;
13434 s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
13435 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13436 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
13437 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13438 - clz - 8);
13439 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
13440 && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
13441 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13442 - clz - 16);
13443 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
13444 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
13445 && s > 6)
13446 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13447 - clz - 32);
13448 else
13449 return 1 + s;
13451 else
13453 if (i >= -0x80)
13454 return 2;
13455 else if (i >= -0x8000)
13456 return 3;
13457 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
13459 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
13461 s = size_of_int_loc_descriptor (-i) + 1;
13462 if (s < 5)
13463 return s;
13465 return 5;
13467 else
13469 unsigned long r = 1 + size_of_sleb128 (i);
13470 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
13472 s = size_of_int_loc_descriptor (-i) + 1;
13473 if (s < r)
13474 return s;
13476 return r;
13481 /* Return loc description representing "address" of integer value.
13482 This can appear only as toplevel expression. */
13484 static dw_loc_descr_ref
13485 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
13487 int litsize;
13488 dw_loc_descr_ref loc_result = NULL;
13490 if (!(dwarf_version >= 4 || !dwarf_strict))
13491 return NULL;
13493 litsize = size_of_int_loc_descriptor (i);
13494 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
13495 is more compact. For DW_OP_stack_value we need:
13496 litsize + 1 (DW_OP_stack_value)
13497 and for DW_OP_implicit_value:
13498 1 (DW_OP_implicit_value) + 1 (length) + size. */
13499 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
13501 loc_result = int_loc_descriptor (i);
13502 add_loc_descr (&loc_result,
13503 new_loc_descr (DW_OP_stack_value, 0, 0));
13504 return loc_result;
13507 loc_result = new_loc_descr (DW_OP_implicit_value,
13508 size, 0);
13509 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
13510 loc_result->dw_loc_oprnd2.v.val_int = i;
13511 return loc_result;
13514 /* Return a location descriptor that designates a base+offset location. */
13516 static dw_loc_descr_ref
13517 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
13518 enum var_init_status initialized)
13520 unsigned int regno;
13521 dw_loc_descr_ref result;
13522 dw_fde_ref fde = cfun->fde;
13524 /* We only use "frame base" when we're sure we're talking about the
13525 post-prologue local stack frame. We do this by *not* running
13526 register elimination until this point, and recognizing the special
13527 argument pointer and soft frame pointer rtx's. */
13528 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
13530 rtx elim = (ira_use_lra_p
13531 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
13532 : eliminate_regs (reg, VOIDmode, NULL_RTX));
13534 if (elim != reg)
13536 if (GET_CODE (elim) == PLUS)
13538 offset += INTVAL (XEXP (elim, 1));
13539 elim = XEXP (elim, 0);
13541 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
13542 && (elim == hard_frame_pointer_rtx
13543 || elim == stack_pointer_rtx))
13544 || elim == (frame_pointer_needed
13545 ? hard_frame_pointer_rtx
13546 : stack_pointer_rtx));
13548 /* If drap register is used to align stack, use frame
13549 pointer + offset to access stack variables. If stack
13550 is aligned without drap, use stack pointer + offset to
13551 access stack variables. */
13552 if (crtl->stack_realign_tried
13553 && reg == frame_pointer_rtx)
13555 int base_reg
13556 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
13557 ? HARD_FRAME_POINTER_REGNUM
13558 : REGNO (elim));
13559 return new_reg_loc_descr (base_reg, offset);
13562 gcc_assert (frame_pointer_fb_offset_valid);
13563 offset += frame_pointer_fb_offset;
13564 return new_loc_descr (DW_OP_fbreg, offset, 0);
13568 regno = REGNO (reg);
13569 #ifdef LEAF_REG_REMAP
13570 if (crtl->uses_only_leaf_regs)
13572 int leaf_reg = LEAF_REG_REMAP (regno);
13573 if (leaf_reg != -1)
13574 regno = (unsigned) leaf_reg;
13576 #endif
13577 regno = DWARF_FRAME_REGNUM (regno);
13579 if (!optimize && fde
13580 && (fde->drap_reg == regno || fde->vdrap_reg == regno))
13582 /* Use cfa+offset to represent the location of arguments passed
13583 on the stack when drap is used to align stack.
13584 Only do this when not optimizing, for optimized code var-tracking
13585 is supposed to track where the arguments live and the register
13586 used as vdrap or drap in some spot might be used for something
13587 else in other part of the routine. */
13588 return new_loc_descr (DW_OP_fbreg, offset, 0);
13591 if (regno <= 31)
13592 result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
13593 offset, 0);
13594 else
13595 result = new_loc_descr (DW_OP_bregx, regno, offset);
13597 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13598 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13600 return result;
13603 /* Return true if this RTL expression describes a base+offset calculation. */
13605 static inline int
13606 is_based_loc (const_rtx rtl)
13608 return (GET_CODE (rtl) == PLUS
13609 && ((REG_P (XEXP (rtl, 0))
13610 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
13611 && CONST_INT_P (XEXP (rtl, 1)))));
13614 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
13615 failed. */
13617 static dw_loc_descr_ref
13618 tls_mem_loc_descriptor (rtx mem)
13620 tree base;
13621 dw_loc_descr_ref loc_result;
13623 if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
13624 return NULL;
13626 base = get_base_address (MEM_EXPR (mem));
13627 if (base == NULL
13628 || !VAR_P (base)
13629 || !DECL_THREAD_LOCAL_P (base))
13630 return NULL;
13632 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1, NULL);
13633 if (loc_result == NULL)
13634 return NULL;
13636 if (MEM_OFFSET (mem))
13637 loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
13639 return loc_result;
13642 /* Output debug info about reason why we failed to expand expression as dwarf
13643 expression. */
13645 static void
13646 expansion_failed (tree expr, rtx rtl, char const *reason)
13648 if (dump_file && (dump_flags & TDF_DETAILS))
13650 fprintf (dump_file, "Failed to expand as dwarf: ");
13651 if (expr)
13652 print_generic_expr (dump_file, expr, dump_flags);
13653 if (rtl)
13655 fprintf (dump_file, "\n");
13656 print_rtl (dump_file, rtl);
13658 fprintf (dump_file, "\nReason: %s\n", reason);
13662 /* Helper function for const_ok_for_output. */
13664 static bool
13665 const_ok_for_output_1 (rtx rtl)
13667 if (GET_CODE (rtl) == UNSPEC)
13669 /* If delegitimize_address couldn't do anything with the UNSPEC, assume
13670 we can't express it in the debug info. */
13671 /* Don't complain about TLS UNSPECs, those are just too hard to
13672 delegitimize. Note this could be a non-decl SYMBOL_REF such as
13673 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
13674 rather than DECL_THREAD_LOCAL_P is not just an optimization. */
13675 if (flag_checking
13676 && (XVECLEN (rtl, 0) == 0
13677 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
13678 || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE))
13679 inform (current_function_decl
13680 ? DECL_SOURCE_LOCATION (current_function_decl)
13681 : UNKNOWN_LOCATION,
13682 #if NUM_UNSPEC_VALUES > 0
13683 "non-delegitimized UNSPEC %s (%d) found in variable location",
13684 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
13685 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
13686 XINT (rtl, 1));
13687 #else
13688 "non-delegitimized UNSPEC %d found in variable location",
13689 XINT (rtl, 1));
13690 #endif
13691 expansion_failed (NULL_TREE, rtl,
13692 "UNSPEC hasn't been delegitimized.\n");
13693 return false;
13696 if (targetm.const_not_ok_for_debug_p (rtl))
13698 expansion_failed (NULL_TREE, rtl,
13699 "Expression rejected for debug by the backend.\n");
13700 return false;
13703 /* FIXME: Refer to PR60655. It is possible for simplification
13704 of rtl expressions in var tracking to produce such expressions.
13705 We should really identify / validate expressions
13706 enclosed in CONST that can be handled by assemblers on various
13707 targets and only handle legitimate cases here. */
13708 if (GET_CODE (rtl) != SYMBOL_REF)
13710 if (GET_CODE (rtl) == NOT)
13711 return false;
13712 return true;
13715 if (CONSTANT_POOL_ADDRESS_P (rtl))
13717 bool marked;
13718 get_pool_constant_mark (rtl, &marked);
13719 /* If all references to this pool constant were optimized away,
13720 it was not output and thus we can't represent it. */
13721 if (!marked)
13723 expansion_failed (NULL_TREE, rtl,
13724 "Constant was removed from constant pool.\n");
13725 return false;
13729 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
13730 return false;
13732 /* Avoid references to external symbols in debug info, on several targets
13733 the linker might even refuse to link when linking a shared library,
13734 and in many other cases the relocations for .debug_info/.debug_loc are
13735 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
13736 to be defined within the same shared library or executable are fine. */
13737 if (SYMBOL_REF_EXTERNAL_P (rtl))
13739 tree decl = SYMBOL_REF_DECL (rtl);
13741 if (decl == NULL || !targetm.binds_local_p (decl))
13743 expansion_failed (NULL_TREE, rtl,
13744 "Symbol not defined in current TU.\n");
13745 return false;
13749 return true;
13752 /* Return true if constant RTL can be emitted in DW_OP_addr or
13753 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
13754 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
13756 static bool
13757 const_ok_for_output (rtx rtl)
13759 if (GET_CODE (rtl) == SYMBOL_REF)
13760 return const_ok_for_output_1 (rtl);
13762 if (GET_CODE (rtl) == CONST)
13764 subrtx_var_iterator::array_type array;
13765 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 0), ALL)
13766 if (!const_ok_for_output_1 (*iter))
13767 return false;
13768 return true;
13771 return true;
13774 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
13775 if possible, NULL otherwise. */
13777 static dw_die_ref
13778 base_type_for_mode (machine_mode mode, bool unsignedp)
13780 dw_die_ref type_die;
13781 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
13783 if (type == NULL)
13784 return NULL;
13785 switch (TREE_CODE (type))
13787 case INTEGER_TYPE:
13788 case REAL_TYPE:
13789 break;
13790 default:
13791 return NULL;
13793 type_die = lookup_type_die (type);
13794 if (!type_die)
13795 type_die = modified_type_die (type, TYPE_UNQUALIFIED, false,
13796 comp_unit_die ());
13797 if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
13798 return NULL;
13799 return type_die;
13802 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
13803 type matching MODE, or, if MODE is narrower than or as wide as
13804 DWARF2_ADDR_SIZE, untyped. Return NULL if the conversion is not
13805 possible. */
13807 static dw_loc_descr_ref
13808 convert_descriptor_to_mode (machine_mode mode, dw_loc_descr_ref op)
13810 machine_mode outer_mode = mode;
13811 dw_die_ref type_die;
13812 dw_loc_descr_ref cvt;
13814 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
13816 add_loc_descr (&op, new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0));
13817 return op;
13819 type_die = base_type_for_mode (outer_mode, 1);
13820 if (type_die == NULL)
13821 return NULL;
13822 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
13823 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13824 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13825 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13826 add_loc_descr (&op, cvt);
13827 return op;
13830 /* Return location descriptor for comparison OP with operands OP0 and OP1. */
13832 static dw_loc_descr_ref
13833 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
13834 dw_loc_descr_ref op1)
13836 dw_loc_descr_ref ret = op0;
13837 add_loc_descr (&ret, op1);
13838 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
13839 if (STORE_FLAG_VALUE != 1)
13841 add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
13842 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
13844 return ret;
13847 /* Return location descriptor for signed comparison OP RTL. */
13849 static dw_loc_descr_ref
13850 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
13851 machine_mode mem_mode)
13853 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
13854 dw_loc_descr_ref op0, op1;
13855 int shift;
13857 if (op_mode == VOIDmode)
13858 op_mode = GET_MODE (XEXP (rtl, 1));
13859 if (op_mode == VOIDmode)
13860 return NULL;
13862 if (dwarf_strict
13863 && dwarf_version < 5
13864 && (!SCALAR_INT_MODE_P (op_mode)
13865 || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
13866 return NULL;
13868 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
13869 VAR_INIT_STATUS_INITIALIZED);
13870 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
13871 VAR_INIT_STATUS_INITIALIZED);
13873 if (op0 == NULL || op1 == NULL)
13874 return NULL;
13876 if (!SCALAR_INT_MODE_P (op_mode)
13877 || GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
13878 return compare_loc_descriptor (op, op0, op1);
13880 if (GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
13882 dw_die_ref type_die = base_type_for_mode (op_mode, 0);
13883 dw_loc_descr_ref cvt;
13885 if (type_die == NULL)
13886 return NULL;
13887 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
13888 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13889 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13890 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13891 add_loc_descr (&op0, cvt);
13892 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
13893 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13894 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13895 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13896 add_loc_descr (&op1, cvt);
13897 return compare_loc_descriptor (op, op0, op1);
13900 shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
13901 /* For eq/ne, if the operands are known to be zero-extended,
13902 there is no need to do the fancy shifting up. */
13903 if (op == DW_OP_eq || op == DW_OP_ne)
13905 dw_loc_descr_ref last0, last1;
13906 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
13908 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
13910 /* deref_size zero extends, and for constants we can check
13911 whether they are zero extended or not. */
13912 if (((last0->dw_loc_opc == DW_OP_deref_size
13913 && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
13914 || (CONST_INT_P (XEXP (rtl, 0))
13915 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
13916 == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
13917 && ((last1->dw_loc_opc == DW_OP_deref_size
13918 && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
13919 || (CONST_INT_P (XEXP (rtl, 1))
13920 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
13921 == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
13922 return compare_loc_descriptor (op, op0, op1);
13924 /* EQ/NE comparison against constant in narrower type than
13925 DWARF2_ADDR_SIZE can be performed either as
13926 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
13927 DW_OP_{eq,ne}
13929 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
13930 DW_OP_{eq,ne}. Pick whatever is shorter. */
13931 if (CONST_INT_P (XEXP (rtl, 1))
13932 && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
13933 && (size_of_int_loc_descriptor (shift) + 1
13934 + size_of_int_loc_descriptor (UINTVAL (XEXP (rtl, 1)) << shift)
13935 >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
13936 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
13937 & GET_MODE_MASK (op_mode))))
13939 add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
13940 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
13941 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
13942 & GET_MODE_MASK (op_mode));
13943 return compare_loc_descriptor (op, op0, op1);
13946 add_loc_descr (&op0, int_loc_descriptor (shift));
13947 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
13948 if (CONST_INT_P (XEXP (rtl, 1)))
13949 op1 = int_loc_descriptor (UINTVAL (XEXP (rtl, 1)) << shift);
13950 else
13952 add_loc_descr (&op1, int_loc_descriptor (shift));
13953 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
13955 return compare_loc_descriptor (op, op0, op1);
13958 /* Return location descriptor for unsigned comparison OP RTL. */
13960 static dw_loc_descr_ref
13961 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
13962 machine_mode mem_mode)
13964 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
13965 dw_loc_descr_ref op0, op1;
13967 if (op_mode == VOIDmode)
13968 op_mode = GET_MODE (XEXP (rtl, 1));
13969 if (op_mode == VOIDmode)
13970 return NULL;
13971 if (!SCALAR_INT_MODE_P (op_mode))
13972 return NULL;
13974 if (dwarf_strict
13975 && dwarf_version < 5
13976 && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
13977 return NULL;
13979 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
13980 VAR_INIT_STATUS_INITIALIZED);
13981 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
13982 VAR_INIT_STATUS_INITIALIZED);
13984 if (op0 == NULL || op1 == NULL)
13985 return NULL;
13987 if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
13989 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
13990 dw_loc_descr_ref last0, last1;
13991 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
13993 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
13995 if (CONST_INT_P (XEXP (rtl, 0)))
13996 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
13997 /* deref_size zero extends, so no need to mask it again. */
13998 else if (last0->dw_loc_opc != DW_OP_deref_size
13999 || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
14001 add_loc_descr (&op0, int_loc_descriptor (mask));
14002 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14004 if (CONST_INT_P (XEXP (rtl, 1)))
14005 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
14006 /* deref_size zero extends, so no need to mask it again. */
14007 else if (last1->dw_loc_opc != DW_OP_deref_size
14008 || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
14010 add_loc_descr (&op1, int_loc_descriptor (mask));
14011 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
14014 else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
14016 HOST_WIDE_INT bias = 1;
14017 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
14018 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14019 if (CONST_INT_P (XEXP (rtl, 1)))
14020 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
14021 + INTVAL (XEXP (rtl, 1)));
14022 else
14023 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
14024 bias, 0));
14026 return compare_loc_descriptor (op, op0, op1);
14029 /* Return location descriptor for {U,S}{MIN,MAX}. */
14031 static dw_loc_descr_ref
14032 minmax_loc_descriptor (rtx rtl, machine_mode mode,
14033 machine_mode mem_mode)
14035 enum dwarf_location_atom op;
14036 dw_loc_descr_ref op0, op1, ret;
14037 dw_loc_descr_ref bra_node, drop_node;
14039 if (dwarf_strict
14040 && dwarf_version < 5
14041 && (!SCALAR_INT_MODE_P (mode)
14042 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
14043 return NULL;
14045 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14046 VAR_INIT_STATUS_INITIALIZED);
14047 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14048 VAR_INIT_STATUS_INITIALIZED);
14050 if (op0 == NULL || op1 == NULL)
14051 return NULL;
14053 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
14054 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
14055 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
14056 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
14058 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
14060 HOST_WIDE_INT mask = GET_MODE_MASK (mode);
14061 add_loc_descr (&op0, int_loc_descriptor (mask));
14062 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14063 add_loc_descr (&op1, int_loc_descriptor (mask));
14064 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
14066 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
14068 HOST_WIDE_INT bias = 1;
14069 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
14070 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14071 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14074 else if (!SCALAR_INT_MODE_P (mode)
14075 && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
14077 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode)) * BITS_PER_UNIT;
14078 add_loc_descr (&op0, int_loc_descriptor (shift));
14079 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
14080 add_loc_descr (&op1, int_loc_descriptor (shift));
14081 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
14083 else if (SCALAR_INT_MODE_P (mode)
14084 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
14086 dw_die_ref type_die = base_type_for_mode (mode, 0);
14087 dw_loc_descr_ref cvt;
14088 if (type_die == NULL)
14089 return NULL;
14090 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14091 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14092 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14093 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14094 add_loc_descr (&op0, cvt);
14095 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14096 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14097 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14098 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14099 add_loc_descr (&op1, cvt);
14102 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
14103 op = DW_OP_lt;
14104 else
14105 op = DW_OP_gt;
14106 ret = op0;
14107 add_loc_descr (&ret, op1);
14108 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
14109 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14110 add_loc_descr (&ret, bra_node);
14111 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14112 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
14113 add_loc_descr (&ret, drop_node);
14114 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14115 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
14116 if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
14117 && SCALAR_INT_MODE_P (mode)
14118 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
14119 ret = convert_descriptor_to_mode (mode, ret);
14120 return ret;
14123 /* Helper function for mem_loc_descriptor. Perform OP binary op,
14124 but after converting arguments to type_die, afterwards
14125 convert back to unsigned. */
14127 static dw_loc_descr_ref
14128 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
14129 machine_mode mode, machine_mode mem_mode)
14131 dw_loc_descr_ref cvt, op0, op1;
14133 if (type_die == NULL)
14134 return NULL;
14135 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14136 VAR_INIT_STATUS_INITIALIZED);
14137 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14138 VAR_INIT_STATUS_INITIALIZED);
14139 if (op0 == NULL || op1 == NULL)
14140 return NULL;
14141 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14142 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14143 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14144 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14145 add_loc_descr (&op0, cvt);
14146 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14147 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14148 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14149 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14150 add_loc_descr (&op1, cvt);
14151 add_loc_descr (&op0, op1);
14152 add_loc_descr (&op0, new_loc_descr (op, 0, 0));
14153 return convert_descriptor_to_mode (mode, op0);
14156 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
14157 const0 is DW_OP_lit0 or corresponding typed constant,
14158 const1 is DW_OP_lit1 or corresponding typed constant
14159 and constMSB is constant with just the MSB bit set
14160 for the mode):
14161 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
14162 L1: const0 DW_OP_swap
14163 L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
14164 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
14165 L3: DW_OP_drop
14166 L4: DW_OP_nop
14168 CTZ is similar:
14169 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
14170 L1: const0 DW_OP_swap
14171 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
14172 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
14173 L3: DW_OP_drop
14174 L4: DW_OP_nop
14176 FFS is similar:
14177 DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
14178 L1: const1 DW_OP_swap
14179 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
14180 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
14181 L3: DW_OP_drop
14182 L4: DW_OP_nop */
14184 static dw_loc_descr_ref
14185 clz_loc_descriptor (rtx rtl, machine_mode mode,
14186 machine_mode mem_mode)
14188 dw_loc_descr_ref op0, ret, tmp;
14189 HOST_WIDE_INT valv;
14190 dw_loc_descr_ref l1jump, l1label;
14191 dw_loc_descr_ref l2jump, l2label;
14192 dw_loc_descr_ref l3jump, l3label;
14193 dw_loc_descr_ref l4jump, l4label;
14194 rtx msb;
14196 if (!SCALAR_INT_MODE_P (mode)
14197 || GET_MODE (XEXP (rtl, 0)) != mode)
14198 return NULL;
14200 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14201 VAR_INIT_STATUS_INITIALIZED);
14202 if (op0 == NULL)
14203 return NULL;
14204 ret = op0;
14205 if (GET_CODE (rtl) == CLZ)
14207 if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
14208 valv = GET_MODE_BITSIZE (mode);
14210 else if (GET_CODE (rtl) == FFS)
14211 valv = 0;
14212 else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
14213 valv = GET_MODE_BITSIZE (mode);
14214 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
14215 l1jump = new_loc_descr (DW_OP_bra, 0, 0);
14216 add_loc_descr (&ret, l1jump);
14217 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
14218 tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
14219 VAR_INIT_STATUS_INITIALIZED);
14220 if (tmp == NULL)
14221 return NULL;
14222 add_loc_descr (&ret, tmp);
14223 l4jump = new_loc_descr (DW_OP_skip, 0, 0);
14224 add_loc_descr (&ret, l4jump);
14225 l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
14226 ? const1_rtx : const0_rtx,
14227 mode, mem_mode,
14228 VAR_INIT_STATUS_INITIALIZED);
14229 if (l1label == NULL)
14230 return NULL;
14231 add_loc_descr (&ret, l1label);
14232 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14233 l2label = new_loc_descr (DW_OP_dup, 0, 0);
14234 add_loc_descr (&ret, l2label);
14235 if (GET_CODE (rtl) != CLZ)
14236 msb = const1_rtx;
14237 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
14238 msb = GEN_INT (HOST_WIDE_INT_1U
14239 << (GET_MODE_BITSIZE (mode) - 1));
14240 else
14241 msb = immed_wide_int_const
14242 (wi::set_bit_in_zero (GET_MODE_PRECISION (mode) - 1,
14243 GET_MODE_PRECISION (mode)), mode);
14244 if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
14245 tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
14246 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
14247 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
14248 else
14249 tmp = mem_loc_descriptor (msb, mode, mem_mode,
14250 VAR_INIT_STATUS_INITIALIZED);
14251 if (tmp == NULL)
14252 return NULL;
14253 add_loc_descr (&ret, tmp);
14254 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
14255 l3jump = new_loc_descr (DW_OP_bra, 0, 0);
14256 add_loc_descr (&ret, l3jump);
14257 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
14258 VAR_INIT_STATUS_INITIALIZED);
14259 if (tmp == NULL)
14260 return NULL;
14261 add_loc_descr (&ret, tmp);
14262 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
14263 ? DW_OP_shl : DW_OP_shr, 0, 0));
14264 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14265 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
14266 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14267 l2jump = new_loc_descr (DW_OP_skip, 0, 0);
14268 add_loc_descr (&ret, l2jump);
14269 l3label = new_loc_descr (DW_OP_drop, 0, 0);
14270 add_loc_descr (&ret, l3label);
14271 l4label = new_loc_descr (DW_OP_nop, 0, 0);
14272 add_loc_descr (&ret, l4label);
14273 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14274 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
14275 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14276 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
14277 l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14278 l3jump->dw_loc_oprnd1.v.val_loc = l3label;
14279 l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14280 l4jump->dw_loc_oprnd1.v.val_loc = l4label;
14281 return ret;
14284 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
14285 const1 is DW_OP_lit1 or corresponding typed constant):
14286 const0 DW_OP_swap
14287 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
14288 DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
14289 L2: DW_OP_drop
14291 PARITY is similar:
14292 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
14293 DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
14294 L2: DW_OP_drop */
14296 static dw_loc_descr_ref
14297 popcount_loc_descriptor (rtx rtl, machine_mode mode,
14298 machine_mode mem_mode)
14300 dw_loc_descr_ref op0, ret, tmp;
14301 dw_loc_descr_ref l1jump, l1label;
14302 dw_loc_descr_ref l2jump, l2label;
14304 if (!SCALAR_INT_MODE_P (mode)
14305 || GET_MODE (XEXP (rtl, 0)) != mode)
14306 return NULL;
14308 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14309 VAR_INIT_STATUS_INITIALIZED);
14310 if (op0 == NULL)
14311 return NULL;
14312 ret = op0;
14313 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
14314 VAR_INIT_STATUS_INITIALIZED);
14315 if (tmp == NULL)
14316 return NULL;
14317 add_loc_descr (&ret, tmp);
14318 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14319 l1label = new_loc_descr (DW_OP_dup, 0, 0);
14320 add_loc_descr (&ret, l1label);
14321 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
14322 add_loc_descr (&ret, l2jump);
14323 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
14324 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
14325 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
14326 VAR_INIT_STATUS_INITIALIZED);
14327 if (tmp == NULL)
14328 return NULL;
14329 add_loc_descr (&ret, tmp);
14330 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
14331 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
14332 ? DW_OP_plus : DW_OP_xor, 0, 0));
14333 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14334 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
14335 VAR_INIT_STATUS_INITIALIZED);
14336 add_loc_descr (&ret, tmp);
14337 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
14338 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
14339 add_loc_descr (&ret, l1jump);
14340 l2label = new_loc_descr (DW_OP_drop, 0, 0);
14341 add_loc_descr (&ret, l2label);
14342 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14343 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
14344 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14345 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
14346 return ret;
14349 /* BSWAP (constS is initial shift count, either 56 or 24):
14350 constS const0
14351 L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
14352 const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
14353 DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
14354 DW_OP_minus DW_OP_swap DW_OP_skip <L1>
14355 L2: DW_OP_drop DW_OP_swap DW_OP_drop */
14357 static dw_loc_descr_ref
14358 bswap_loc_descriptor (rtx rtl, machine_mode mode,
14359 machine_mode mem_mode)
14361 dw_loc_descr_ref op0, ret, tmp;
14362 dw_loc_descr_ref l1jump, l1label;
14363 dw_loc_descr_ref l2jump, l2label;
14365 if (!SCALAR_INT_MODE_P (mode)
14366 || BITS_PER_UNIT != 8
14367 || (GET_MODE_BITSIZE (mode) != 32
14368 && GET_MODE_BITSIZE (mode) != 64))
14369 return NULL;
14371 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14372 VAR_INIT_STATUS_INITIALIZED);
14373 if (op0 == NULL)
14374 return NULL;
14376 ret = op0;
14377 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
14378 mode, mem_mode,
14379 VAR_INIT_STATUS_INITIALIZED);
14380 if (tmp == NULL)
14381 return NULL;
14382 add_loc_descr (&ret, tmp);
14383 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
14384 VAR_INIT_STATUS_INITIALIZED);
14385 if (tmp == NULL)
14386 return NULL;
14387 add_loc_descr (&ret, tmp);
14388 l1label = new_loc_descr (DW_OP_pick, 2, 0);
14389 add_loc_descr (&ret, l1label);
14390 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
14391 mode, mem_mode,
14392 VAR_INIT_STATUS_INITIALIZED);
14393 add_loc_descr (&ret, tmp);
14394 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
14395 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
14396 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
14397 tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
14398 VAR_INIT_STATUS_INITIALIZED);
14399 if (tmp == NULL)
14400 return NULL;
14401 add_loc_descr (&ret, tmp);
14402 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
14403 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
14404 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
14405 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
14406 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14407 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
14408 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
14409 VAR_INIT_STATUS_INITIALIZED);
14410 add_loc_descr (&ret, tmp);
14411 add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
14412 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
14413 add_loc_descr (&ret, l2jump);
14414 tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
14415 VAR_INIT_STATUS_INITIALIZED);
14416 add_loc_descr (&ret, tmp);
14417 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
14418 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14419 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
14420 add_loc_descr (&ret, l1jump);
14421 l2label = new_loc_descr (DW_OP_drop, 0, 0);
14422 add_loc_descr (&ret, l2label);
14423 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14424 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
14425 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14426 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
14427 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14428 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
14429 return ret;
14432 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
14433 DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
14434 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
14435 DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
14437 ROTATERT is similar:
14438 DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
14439 DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
14440 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or */
14442 static dw_loc_descr_ref
14443 rotate_loc_descriptor (rtx rtl, machine_mode mode,
14444 machine_mode mem_mode)
14446 rtx rtlop1 = XEXP (rtl, 1);
14447 dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
14448 int i;
14450 if (!SCALAR_INT_MODE_P (mode))
14451 return NULL;
14453 if (GET_MODE (rtlop1) != VOIDmode
14454 && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
14455 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
14456 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14457 VAR_INIT_STATUS_INITIALIZED);
14458 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
14459 VAR_INIT_STATUS_INITIALIZED);
14460 if (op0 == NULL || op1 == NULL)
14461 return NULL;
14462 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
14463 for (i = 0; i < 2; i++)
14465 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
14466 mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
14467 mode, mem_mode,
14468 VAR_INIT_STATUS_INITIALIZED);
14469 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
14470 mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
14471 ? DW_OP_const4u
14472 : HOST_BITS_PER_WIDE_INT == 64
14473 ? DW_OP_const8u : DW_OP_constu,
14474 GET_MODE_MASK (mode), 0);
14475 else
14476 mask[i] = NULL;
14477 if (mask[i] == NULL)
14478 return NULL;
14479 add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
14481 ret = op0;
14482 add_loc_descr (&ret, op1);
14483 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
14484 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
14485 if (GET_CODE (rtl) == ROTATERT)
14487 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
14488 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
14489 GET_MODE_BITSIZE (mode), 0));
14491 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
14492 if (mask[0] != NULL)
14493 add_loc_descr (&ret, mask[0]);
14494 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
14495 if (mask[1] != NULL)
14497 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14498 add_loc_descr (&ret, mask[1]);
14499 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14501 if (GET_CODE (rtl) == ROTATE)
14503 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
14504 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
14505 GET_MODE_BITSIZE (mode), 0));
14507 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
14508 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
14509 return ret;
14512 /* Helper function for mem_loc_descriptor. Return DW_OP_GNU_parameter_ref
14513 for DEBUG_PARAMETER_REF RTL. */
14515 static dw_loc_descr_ref
14516 parameter_ref_descriptor (rtx rtl)
14518 dw_loc_descr_ref ret;
14519 dw_die_ref ref;
14521 if (dwarf_strict)
14522 return NULL;
14523 gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
14524 ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
14525 ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
14526 if (ref)
14528 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14529 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
14530 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
14532 else
14534 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
14535 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
14537 return ret;
14540 /* The following routine converts the RTL for a variable or parameter
14541 (resident in memory) into an equivalent Dwarf representation of a
14542 mechanism for getting the address of that same variable onto the top of a
14543 hypothetical "address evaluation" stack.
14545 When creating memory location descriptors, we are effectively transforming
14546 the RTL for a memory-resident object into its Dwarf postfix expression
14547 equivalent. This routine recursively descends an RTL tree, turning
14548 it into Dwarf postfix code as it goes.
14550 MODE is the mode that should be assumed for the rtl if it is VOIDmode.
14552 MEM_MODE is the mode of the memory reference, needed to handle some
14553 autoincrement addressing modes.
14555 Return 0 if we can't represent the location. */
14557 dw_loc_descr_ref
14558 mem_loc_descriptor (rtx rtl, machine_mode mode,
14559 machine_mode mem_mode,
14560 enum var_init_status initialized)
14562 dw_loc_descr_ref mem_loc_result = NULL;
14563 enum dwarf_location_atom op;
14564 dw_loc_descr_ref op0, op1;
14565 rtx inner = NULL_RTX;
14567 if (mode == VOIDmode)
14568 mode = GET_MODE (rtl);
14570 /* Note that for a dynamically sized array, the location we will generate a
14571 description of here will be the lowest numbered location which is
14572 actually within the array. That's *not* necessarily the same as the
14573 zeroth element of the array. */
14575 rtl = targetm.delegitimize_address (rtl);
14577 if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
14578 return NULL;
14580 switch (GET_CODE (rtl))
14582 case POST_INC:
14583 case POST_DEC:
14584 case POST_MODIFY:
14585 return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
14587 case SUBREG:
14588 /* The case of a subreg may arise when we have a local (register)
14589 variable or a formal (register) parameter which doesn't quite fill
14590 up an entire register. For now, just assume that it is
14591 legitimate to make the Dwarf info refer to the whole register which
14592 contains the given subreg. */
14593 if (!subreg_lowpart_p (rtl))
14594 break;
14595 inner = SUBREG_REG (rtl);
14596 /* FALLTHRU */
14597 case TRUNCATE:
14598 if (inner == NULL_RTX)
14599 inner = XEXP (rtl, 0);
14600 if (SCALAR_INT_MODE_P (mode)
14601 && SCALAR_INT_MODE_P (GET_MODE (inner))
14602 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
14603 #ifdef POINTERS_EXTEND_UNSIGNED
14604 || (mode == Pmode && mem_mode != VOIDmode)
14605 #endif
14607 && GET_MODE_SIZE (GET_MODE (inner)) <= DWARF2_ADDR_SIZE)
14609 mem_loc_result = mem_loc_descriptor (inner,
14610 GET_MODE (inner),
14611 mem_mode, initialized);
14612 break;
14614 if (dwarf_strict && dwarf_version < 5)
14615 break;
14616 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (inner)))
14617 break;
14618 if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (inner))
14619 && (!SCALAR_INT_MODE_P (mode)
14620 || !SCALAR_INT_MODE_P (GET_MODE (inner))))
14621 break;
14622 else
14624 dw_die_ref type_die;
14625 dw_loc_descr_ref cvt;
14627 mem_loc_result = mem_loc_descriptor (inner,
14628 GET_MODE (inner),
14629 mem_mode, initialized);
14630 if (mem_loc_result == NULL)
14631 break;
14632 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
14633 if (type_die == NULL)
14635 mem_loc_result = NULL;
14636 break;
14638 if (GET_MODE_SIZE (mode)
14639 != GET_MODE_SIZE (GET_MODE (inner)))
14640 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14641 else
14642 cvt = new_loc_descr (dwarf_OP (DW_OP_reinterpret), 0, 0);
14643 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14644 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14645 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14646 add_loc_descr (&mem_loc_result, cvt);
14647 if (SCALAR_INT_MODE_P (mode)
14648 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
14650 /* Convert it to untyped afterwards. */
14651 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14652 add_loc_descr (&mem_loc_result, cvt);
14655 break;
14657 case REG:
14658 if (! SCALAR_INT_MODE_P (mode)
14659 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
14660 && rtl != arg_pointer_rtx
14661 && rtl != frame_pointer_rtx
14662 #ifdef POINTERS_EXTEND_UNSIGNED
14663 && (mode != Pmode || mem_mode == VOIDmode)
14664 #endif
14667 dw_die_ref type_die;
14668 unsigned int dbx_regnum;
14670 if (dwarf_strict && dwarf_version < 5)
14671 break;
14672 if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
14673 break;
14674 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
14675 if (type_die == NULL)
14676 break;
14678 dbx_regnum = dbx_reg_number (rtl);
14679 if (dbx_regnum == IGNORED_DWARF_REGNUM)
14680 break;
14681 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_regval_type),
14682 dbx_regnum, 0);
14683 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
14684 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
14685 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
14686 break;
14688 /* Whenever a register number forms a part of the description of the
14689 method for calculating the (dynamic) address of a memory resident
14690 object, DWARF rules require the register number be referred to as
14691 a "base register". This distinction is not based in any way upon
14692 what category of register the hardware believes the given register
14693 belongs to. This is strictly DWARF terminology we're dealing with
14694 here. Note that in cases where the location of a memory-resident
14695 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
14696 OP_CONST (0)) the actual DWARF location descriptor that we generate
14697 may just be OP_BASEREG (basereg). This may look deceptively like
14698 the object in question was allocated to a register (rather than in
14699 memory) so DWARF consumers need to be aware of the subtle
14700 distinction between OP_REG and OP_BASEREG. */
14701 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
14702 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
14703 else if (stack_realign_drap
14704 && crtl->drap_reg
14705 && crtl->args.internal_arg_pointer == rtl
14706 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
14708 /* If RTL is internal_arg_pointer, which has been optimized
14709 out, use DRAP instead. */
14710 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
14711 VAR_INIT_STATUS_INITIALIZED);
14713 break;
14715 case SIGN_EXTEND:
14716 case ZERO_EXTEND:
14717 if (!SCALAR_INT_MODE_P (mode))
14718 break;
14719 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
14720 mem_mode, VAR_INIT_STATUS_INITIALIZED);
14721 if (op0 == 0)
14722 break;
14723 else if (GET_CODE (rtl) == ZERO_EXTEND
14724 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
14725 && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
14726 < HOST_BITS_PER_WIDE_INT
14727 /* If DW_OP_const{1,2,4}u won't be used, it is shorter
14728 to expand zero extend as two shifts instead of
14729 masking. */
14730 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= 4)
14732 machine_mode imode = GET_MODE (XEXP (rtl, 0));
14733 mem_loc_result = op0;
14734 add_loc_descr (&mem_loc_result,
14735 int_loc_descriptor (GET_MODE_MASK (imode)));
14736 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
14738 else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
14740 int shift = DWARF2_ADDR_SIZE
14741 - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
14742 shift *= BITS_PER_UNIT;
14743 if (GET_CODE (rtl) == SIGN_EXTEND)
14744 op = DW_OP_shra;
14745 else
14746 op = DW_OP_shr;
14747 mem_loc_result = op0;
14748 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
14749 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
14750 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
14751 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14753 else if (!dwarf_strict || dwarf_version >= 5)
14755 dw_die_ref type_die1, type_die2;
14756 dw_loc_descr_ref cvt;
14758 type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
14759 GET_CODE (rtl) == ZERO_EXTEND);
14760 if (type_die1 == NULL)
14761 break;
14762 type_die2 = base_type_for_mode (mode, 1);
14763 if (type_die2 == NULL)
14764 break;
14765 mem_loc_result = op0;
14766 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14767 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14768 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
14769 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14770 add_loc_descr (&mem_loc_result, cvt);
14771 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14772 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14773 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
14774 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14775 add_loc_descr (&mem_loc_result, cvt);
14777 break;
14779 case MEM:
14781 rtx new_rtl = avoid_constant_pool_reference (rtl);
14782 if (new_rtl != rtl)
14784 mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
14785 initialized);
14786 if (mem_loc_result != NULL)
14787 return mem_loc_result;
14790 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
14791 get_address_mode (rtl), mode,
14792 VAR_INIT_STATUS_INITIALIZED);
14793 if (mem_loc_result == NULL)
14794 mem_loc_result = tls_mem_loc_descriptor (rtl);
14795 if (mem_loc_result != NULL)
14797 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
14798 || !SCALAR_INT_MODE_P(mode))
14800 dw_die_ref type_die;
14801 dw_loc_descr_ref deref;
14803 if (dwarf_strict && dwarf_version < 5)
14804 return NULL;
14805 type_die
14806 = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
14807 if (type_die == NULL)
14808 return NULL;
14809 deref = new_loc_descr (dwarf_OP (DW_OP_deref_type),
14810 GET_MODE_SIZE (mode), 0);
14811 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
14812 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
14813 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
14814 add_loc_descr (&mem_loc_result, deref);
14816 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
14817 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
14818 else
14819 add_loc_descr (&mem_loc_result,
14820 new_loc_descr (DW_OP_deref_size,
14821 GET_MODE_SIZE (mode), 0));
14823 break;
14825 case LO_SUM:
14826 return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
14828 case LABEL_REF:
14829 /* Some ports can transform a symbol ref into a label ref, because
14830 the symbol ref is too far away and has to be dumped into a constant
14831 pool. */
14832 case CONST:
14833 case SYMBOL_REF:
14834 if (!SCALAR_INT_MODE_P (mode)
14835 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
14836 #ifdef POINTERS_EXTEND_UNSIGNED
14837 && (mode != Pmode || mem_mode == VOIDmode)
14838 #endif
14840 break;
14841 if (GET_CODE (rtl) == SYMBOL_REF
14842 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
14844 dw_loc_descr_ref temp;
14846 /* If this is not defined, we have no way to emit the data. */
14847 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
14848 break;
14850 temp = new_addr_loc_descr (rtl, dtprel_true);
14852 /* We check for DWARF 5 here because gdb did not implement
14853 DW_OP_form_tls_address until after 7.12. */
14854 mem_loc_result = new_loc_descr ((dwarf_version >= 5
14855 ? DW_OP_form_tls_address
14856 : DW_OP_GNU_push_tls_address),
14857 0, 0);
14858 add_loc_descr (&mem_loc_result, temp);
14860 break;
14863 if (!const_ok_for_output (rtl))
14865 if (GET_CODE (rtl) == CONST)
14866 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14867 initialized);
14868 break;
14871 symref:
14872 mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
14873 vec_safe_push (used_rtx_array, rtl);
14874 break;
14876 case CONCAT:
14877 case CONCATN:
14878 case VAR_LOCATION:
14879 case DEBUG_IMPLICIT_PTR:
14880 expansion_failed (NULL_TREE, rtl,
14881 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
14882 return 0;
14884 case ENTRY_VALUE:
14885 if (dwarf_strict && dwarf_version < 5)
14886 return NULL;
14887 if (REG_P (ENTRY_VALUE_EXP (rtl)))
14889 if (!SCALAR_INT_MODE_P (mode)
14890 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
14891 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
14892 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14893 else
14895 unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
14896 if (dbx_regnum == IGNORED_DWARF_REGNUM)
14897 return NULL;
14898 op0 = one_reg_loc_descriptor (dbx_regnum,
14899 VAR_INIT_STATUS_INITIALIZED);
14902 else if (MEM_P (ENTRY_VALUE_EXP (rtl))
14903 && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
14905 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
14906 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14907 if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
14908 return NULL;
14910 else
14911 gcc_unreachable ();
14912 if (op0 == NULL)
14913 return NULL;
14914 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_entry_value), 0, 0);
14915 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
14916 mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
14917 break;
14919 case DEBUG_PARAMETER_REF:
14920 mem_loc_result = parameter_ref_descriptor (rtl);
14921 break;
14923 case PRE_MODIFY:
14924 /* Extract the PLUS expression nested inside and fall into
14925 PLUS code below. */
14926 rtl = XEXP (rtl, 1);
14927 goto plus;
14929 case PRE_INC:
14930 case PRE_DEC:
14931 /* Turn these into a PLUS expression and fall into the PLUS code
14932 below. */
14933 rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
14934 gen_int_mode (GET_CODE (rtl) == PRE_INC
14935 ? GET_MODE_UNIT_SIZE (mem_mode)
14936 : -GET_MODE_UNIT_SIZE (mem_mode),
14937 mode));
14939 /* fall through */
14941 case PLUS:
14942 plus:
14943 if (is_based_loc (rtl)
14944 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
14945 || XEXP (rtl, 0) == arg_pointer_rtx
14946 || XEXP (rtl, 0) == frame_pointer_rtx)
14947 && SCALAR_INT_MODE_P (mode))
14948 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
14949 INTVAL (XEXP (rtl, 1)),
14950 VAR_INIT_STATUS_INITIALIZED);
14951 else
14953 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14954 VAR_INIT_STATUS_INITIALIZED);
14955 if (mem_loc_result == 0)
14956 break;
14958 if (CONST_INT_P (XEXP (rtl, 1))
14959 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
14960 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
14961 else
14963 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14964 VAR_INIT_STATUS_INITIALIZED);
14965 if (op1 == 0)
14966 return NULL;
14967 add_loc_descr (&mem_loc_result, op1);
14968 add_loc_descr (&mem_loc_result,
14969 new_loc_descr (DW_OP_plus, 0, 0));
14972 break;
14974 /* If a pseudo-reg is optimized away, it is possible for it to
14975 be replaced with a MEM containing a multiply or shift. */
14976 case MINUS:
14977 op = DW_OP_minus;
14978 goto do_binop;
14980 case MULT:
14981 op = DW_OP_mul;
14982 goto do_binop;
14984 case DIV:
14985 if ((!dwarf_strict || dwarf_version >= 5)
14986 && SCALAR_INT_MODE_P (mode)
14987 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
14989 mem_loc_result = typed_binop (DW_OP_div, rtl,
14990 base_type_for_mode (mode, 0),
14991 mode, mem_mode);
14992 break;
14994 op = DW_OP_div;
14995 goto do_binop;
14997 case UMOD:
14998 op = DW_OP_mod;
14999 goto do_binop;
15001 case ASHIFT:
15002 op = DW_OP_shl;
15003 goto do_shift;
15005 case ASHIFTRT:
15006 op = DW_OP_shra;
15007 goto do_shift;
15009 case LSHIFTRT:
15010 op = DW_OP_shr;
15011 goto do_shift;
15013 do_shift:
15014 if (!SCALAR_INT_MODE_P (mode))
15015 break;
15016 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15017 VAR_INIT_STATUS_INITIALIZED);
15019 rtx rtlop1 = XEXP (rtl, 1);
15020 if (GET_MODE (rtlop1) != VOIDmode
15021 && GET_MODE_BITSIZE (GET_MODE (rtlop1))
15022 < GET_MODE_BITSIZE (mode))
15023 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
15024 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
15025 VAR_INIT_STATUS_INITIALIZED);
15028 if (op0 == 0 || op1 == 0)
15029 break;
15031 mem_loc_result = op0;
15032 add_loc_descr (&mem_loc_result, op1);
15033 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15034 break;
15036 case AND:
15037 op = DW_OP_and;
15038 goto do_binop;
15040 case IOR:
15041 op = DW_OP_or;
15042 goto do_binop;
15044 case XOR:
15045 op = DW_OP_xor;
15046 goto do_binop;
15048 do_binop:
15049 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15050 VAR_INIT_STATUS_INITIALIZED);
15051 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15052 VAR_INIT_STATUS_INITIALIZED);
15054 if (op0 == 0 || op1 == 0)
15055 break;
15057 mem_loc_result = op0;
15058 add_loc_descr (&mem_loc_result, op1);
15059 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15060 break;
15062 case MOD:
15063 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
15064 && (!dwarf_strict || dwarf_version >= 5))
15066 mem_loc_result = typed_binop (DW_OP_mod, rtl,
15067 base_type_for_mode (mode, 0),
15068 mode, mem_mode);
15069 break;
15072 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15073 VAR_INIT_STATUS_INITIALIZED);
15074 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15075 VAR_INIT_STATUS_INITIALIZED);
15077 if (op0 == 0 || op1 == 0)
15078 break;
15080 mem_loc_result = op0;
15081 add_loc_descr (&mem_loc_result, op1);
15082 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
15083 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
15084 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
15085 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
15086 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
15087 break;
15089 case UDIV:
15090 if ((!dwarf_strict || dwarf_version >= 5)
15091 && SCALAR_INT_MODE_P (mode))
15093 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
15095 op = DW_OP_div;
15096 goto do_binop;
15098 mem_loc_result = typed_binop (DW_OP_div, rtl,
15099 base_type_for_mode (mode, 1),
15100 mode, mem_mode);
15102 break;
15104 case NOT:
15105 op = DW_OP_not;
15106 goto do_unop;
15108 case ABS:
15109 op = DW_OP_abs;
15110 goto do_unop;
15112 case NEG:
15113 op = DW_OP_neg;
15114 goto do_unop;
15116 do_unop:
15117 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15118 VAR_INIT_STATUS_INITIALIZED);
15120 if (op0 == 0)
15121 break;
15123 mem_loc_result = op0;
15124 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15125 break;
15127 case CONST_INT:
15128 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
15129 #ifdef POINTERS_EXTEND_UNSIGNED
15130 || (mode == Pmode
15131 && mem_mode != VOIDmode
15132 && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
15133 #endif
15136 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
15137 break;
15139 if ((!dwarf_strict || dwarf_version >= 5)
15140 && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
15141 || GET_MODE_BITSIZE (mode) == HOST_BITS_PER_DOUBLE_INT))
15143 dw_die_ref type_die = base_type_for_mode (mode, 1);
15144 machine_mode amode;
15145 if (type_die == NULL)
15146 return NULL;
15147 amode = mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT,
15148 MODE_INT, 0);
15149 if (INTVAL (rtl) >= 0
15150 && amode != BLKmode
15151 && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
15152 /* const DW_OP_convert <XXX> vs.
15153 DW_OP_const_type <XXX, 1, const>. */
15154 && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
15155 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (mode))
15157 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
15158 op0 = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15159 op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15160 op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15161 op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
15162 add_loc_descr (&mem_loc_result, op0);
15163 return mem_loc_result;
15165 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0,
15166 INTVAL (rtl));
15167 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15168 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15169 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
15170 if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
15171 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
15172 else
15174 mem_loc_result->dw_loc_oprnd2.val_class
15175 = dw_val_class_const_double;
15176 mem_loc_result->dw_loc_oprnd2.v.val_double
15177 = double_int::from_shwi (INTVAL (rtl));
15180 break;
15182 case CONST_DOUBLE:
15183 if (!dwarf_strict || dwarf_version >= 5)
15185 dw_die_ref type_die;
15187 /* Note that if TARGET_SUPPORTS_WIDE_INT == 0, a
15188 CONST_DOUBLE rtx could represent either a large integer
15189 or a floating-point constant. If TARGET_SUPPORTS_WIDE_INT != 0,
15190 the value is always a floating point constant.
15192 When it is an integer, a CONST_DOUBLE is used whenever
15193 the constant requires 2 HWIs to be adequately represented.
15194 We output CONST_DOUBLEs as blocks. */
15195 if (mode == VOIDmode
15196 || (GET_MODE (rtl) == VOIDmode
15197 && GET_MODE_BITSIZE (mode) != HOST_BITS_PER_DOUBLE_INT))
15198 break;
15199 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
15200 if (type_die == NULL)
15201 return NULL;
15202 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0, 0);
15203 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15204 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15205 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
15206 #if TARGET_SUPPORTS_WIDE_INT == 0
15207 if (!SCALAR_FLOAT_MODE_P (mode))
15209 mem_loc_result->dw_loc_oprnd2.val_class
15210 = dw_val_class_const_double;
15211 mem_loc_result->dw_loc_oprnd2.v.val_double
15212 = rtx_to_double_int (rtl);
15214 else
15215 #endif
15217 unsigned int length = GET_MODE_SIZE (mode);
15218 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
15220 insert_float (rtl, array);
15221 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
15222 mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
15223 mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
15224 mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
15227 break;
15229 case CONST_WIDE_INT:
15230 if (!dwarf_strict || dwarf_version >= 5)
15232 dw_die_ref type_die;
15234 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
15235 if (type_die == NULL)
15236 return NULL;
15237 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0, 0);
15238 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15239 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15240 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
15241 mem_loc_result->dw_loc_oprnd2.val_class
15242 = dw_val_class_wide_int;
15243 mem_loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
15244 *mem_loc_result->dw_loc_oprnd2.v.val_wide = rtx_mode_t (rtl, mode);
15246 break;
15248 case EQ:
15249 mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
15250 break;
15252 case GE:
15253 mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
15254 break;
15256 case GT:
15257 mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
15258 break;
15260 case LE:
15261 mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
15262 break;
15264 case LT:
15265 mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
15266 break;
15268 case NE:
15269 mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
15270 break;
15272 case GEU:
15273 mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
15274 break;
15276 case GTU:
15277 mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
15278 break;
15280 case LEU:
15281 mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
15282 break;
15284 case LTU:
15285 mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
15286 break;
15288 case UMIN:
15289 case UMAX:
15290 if (!SCALAR_INT_MODE_P (mode))
15291 break;
15292 /* FALLTHRU */
15293 case SMIN:
15294 case SMAX:
15295 mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
15296 break;
15298 case ZERO_EXTRACT:
15299 case SIGN_EXTRACT:
15300 if (CONST_INT_P (XEXP (rtl, 1))
15301 && CONST_INT_P (XEXP (rtl, 2))
15302 && ((unsigned) INTVAL (XEXP (rtl, 1))
15303 + (unsigned) INTVAL (XEXP (rtl, 2))
15304 <= GET_MODE_BITSIZE (mode))
15305 && SCALAR_INT_MODE_P (mode)
15306 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
15307 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
15309 int shift, size;
15310 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
15311 mem_mode, VAR_INIT_STATUS_INITIALIZED);
15312 if (op0 == 0)
15313 break;
15314 if (GET_CODE (rtl) == SIGN_EXTRACT)
15315 op = DW_OP_shra;
15316 else
15317 op = DW_OP_shr;
15318 mem_loc_result = op0;
15319 size = INTVAL (XEXP (rtl, 1));
15320 shift = INTVAL (XEXP (rtl, 2));
15321 if (BITS_BIG_ENDIAN)
15322 shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
15323 - shift - size;
15324 if (shift + size != (int) DWARF2_ADDR_SIZE)
15326 add_loc_descr (&mem_loc_result,
15327 int_loc_descriptor (DWARF2_ADDR_SIZE
15328 - shift - size));
15329 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
15331 if (size != (int) DWARF2_ADDR_SIZE)
15333 add_loc_descr (&mem_loc_result,
15334 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
15335 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15338 break;
15340 case IF_THEN_ELSE:
15342 dw_loc_descr_ref op2, bra_node, drop_node;
15343 op0 = mem_loc_descriptor (XEXP (rtl, 0),
15344 GET_MODE (XEXP (rtl, 0)) == VOIDmode
15345 ? word_mode : GET_MODE (XEXP (rtl, 0)),
15346 mem_mode, VAR_INIT_STATUS_INITIALIZED);
15347 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15348 VAR_INIT_STATUS_INITIALIZED);
15349 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
15350 VAR_INIT_STATUS_INITIALIZED);
15351 if (op0 == NULL || op1 == NULL || op2 == NULL)
15352 break;
15354 mem_loc_result = op1;
15355 add_loc_descr (&mem_loc_result, op2);
15356 add_loc_descr (&mem_loc_result, op0);
15357 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
15358 add_loc_descr (&mem_loc_result, bra_node);
15359 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
15360 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
15361 add_loc_descr (&mem_loc_result, drop_node);
15362 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15363 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
15365 break;
15367 case FLOAT_EXTEND:
15368 case FLOAT_TRUNCATE:
15369 case FLOAT:
15370 case UNSIGNED_FLOAT:
15371 case FIX:
15372 case UNSIGNED_FIX:
15373 if (!dwarf_strict || dwarf_version >= 5)
15375 dw_die_ref type_die;
15376 dw_loc_descr_ref cvt;
15378 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
15379 mem_mode, VAR_INIT_STATUS_INITIALIZED);
15380 if (op0 == NULL)
15381 break;
15382 if (SCALAR_INT_MODE_P (GET_MODE (XEXP (rtl, 0)))
15383 && (GET_CODE (rtl) == FLOAT
15384 || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
15385 <= DWARF2_ADDR_SIZE))
15387 type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
15388 GET_CODE (rtl) == UNSIGNED_FLOAT);
15389 if (type_die == NULL)
15390 break;
15391 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15392 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15393 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15394 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15395 add_loc_descr (&op0, cvt);
15397 type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
15398 if (type_die == NULL)
15399 break;
15400 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15401 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15402 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15403 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15404 add_loc_descr (&op0, cvt);
15405 if (SCALAR_INT_MODE_P (mode)
15406 && (GET_CODE (rtl) == FIX
15407 || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
15409 op0 = convert_descriptor_to_mode (mode, op0);
15410 if (op0 == NULL)
15411 break;
15413 mem_loc_result = op0;
15415 break;
15417 case CLZ:
15418 case CTZ:
15419 case FFS:
15420 mem_loc_result = clz_loc_descriptor (rtl, mode, mem_mode);
15421 break;
15423 case POPCOUNT:
15424 case PARITY:
15425 mem_loc_result = popcount_loc_descriptor (rtl, mode, mem_mode);
15426 break;
15428 case BSWAP:
15429 mem_loc_result = bswap_loc_descriptor (rtl, mode, mem_mode);
15430 break;
15432 case ROTATE:
15433 case ROTATERT:
15434 mem_loc_result = rotate_loc_descriptor (rtl, mode, mem_mode);
15435 break;
15437 case COMPARE:
15438 /* In theory, we could implement the above. */
15439 /* DWARF cannot represent the unsigned compare operations
15440 natively. */
15441 case SS_MULT:
15442 case US_MULT:
15443 case SS_DIV:
15444 case US_DIV:
15445 case SS_PLUS:
15446 case US_PLUS:
15447 case SS_MINUS:
15448 case US_MINUS:
15449 case SS_NEG:
15450 case US_NEG:
15451 case SS_ABS:
15452 case SS_ASHIFT:
15453 case US_ASHIFT:
15454 case SS_TRUNCATE:
15455 case US_TRUNCATE:
15456 case UNORDERED:
15457 case ORDERED:
15458 case UNEQ:
15459 case UNGE:
15460 case UNGT:
15461 case UNLE:
15462 case UNLT:
15463 case LTGT:
15464 case FRACT_CONVERT:
15465 case UNSIGNED_FRACT_CONVERT:
15466 case SAT_FRACT:
15467 case UNSIGNED_SAT_FRACT:
15468 case SQRT:
15469 case ASM_OPERANDS:
15470 case VEC_MERGE:
15471 case VEC_SELECT:
15472 case VEC_CONCAT:
15473 case VEC_DUPLICATE:
15474 case UNSPEC:
15475 case HIGH:
15476 case FMA:
15477 case STRICT_LOW_PART:
15478 case CONST_VECTOR:
15479 case CONST_FIXED:
15480 case CLRSB:
15481 case CLOBBER:
15482 /* If delegitimize_address couldn't do anything with the UNSPEC, we
15483 can't express it in the debug info. This can happen e.g. with some
15484 TLS UNSPECs. */
15485 break;
15487 case CONST_STRING:
15488 resolve_one_addr (&rtl);
15489 goto symref;
15491 /* RTL sequences inside PARALLEL record a series of DWARF operations for
15492 the expression. An UNSPEC rtx represents a raw DWARF operation,
15493 new_loc_descr is called for it to build the operation directly.
15494 Otherwise mem_loc_descriptor is called recursively. */
15495 case PARALLEL:
15497 int index = 0;
15498 dw_loc_descr_ref exp_result = NULL;
15500 for (; index < XVECLEN (rtl, 0); index++)
15502 rtx elem = XVECEXP (rtl, 0, index);
15503 if (GET_CODE (elem) == UNSPEC)
15505 /* Each DWARF operation UNSPEC contain two operands, if
15506 one operand is not used for the operation, const0_rtx is
15507 passed. */
15508 gcc_assert (XVECLEN (elem, 0) == 2);
15510 HOST_WIDE_INT dw_op = XINT (elem, 1);
15511 HOST_WIDE_INT oprnd1 = INTVAL (XVECEXP (elem, 0, 0));
15512 HOST_WIDE_INT oprnd2 = INTVAL (XVECEXP (elem, 0, 1));
15513 exp_result
15514 = new_loc_descr ((enum dwarf_location_atom) dw_op, oprnd1,
15515 oprnd2);
15517 else
15518 exp_result
15519 = mem_loc_descriptor (elem, mode, mem_mode,
15520 VAR_INIT_STATUS_INITIALIZED);
15522 if (!mem_loc_result)
15523 mem_loc_result = exp_result;
15524 else
15525 add_loc_descr (&mem_loc_result, exp_result);
15528 break;
15531 default:
15532 if (flag_checking)
15534 print_rtl (stderr, rtl);
15535 gcc_unreachable ();
15537 break;
15540 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
15541 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
15543 return mem_loc_result;
15546 /* Return a descriptor that describes the concatenation of two locations.
15547 This is typically a complex variable. */
15549 static dw_loc_descr_ref
15550 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
15552 dw_loc_descr_ref cc_loc_result = NULL;
15553 dw_loc_descr_ref x0_ref
15554 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15555 dw_loc_descr_ref x1_ref
15556 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15558 if (x0_ref == 0 || x1_ref == 0)
15559 return 0;
15561 cc_loc_result = x0_ref;
15562 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
15564 add_loc_descr (&cc_loc_result, x1_ref);
15565 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
15567 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
15568 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
15570 return cc_loc_result;
15573 /* Return a descriptor that describes the concatenation of N
15574 locations. */
15576 static dw_loc_descr_ref
15577 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
15579 unsigned int i;
15580 dw_loc_descr_ref cc_loc_result = NULL;
15581 unsigned int n = XVECLEN (concatn, 0);
15583 for (i = 0; i < n; ++i)
15585 dw_loc_descr_ref ref;
15586 rtx x = XVECEXP (concatn, 0, i);
15588 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15589 if (ref == NULL)
15590 return NULL;
15592 add_loc_descr (&cc_loc_result, ref);
15593 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
15596 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
15597 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
15599 return cc_loc_result;
15602 /* Helper function for loc_descriptor. Return DW_OP_implicit_pointer
15603 for DEBUG_IMPLICIT_PTR RTL. */
15605 static dw_loc_descr_ref
15606 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
15608 dw_loc_descr_ref ret;
15609 dw_die_ref ref;
15611 if (dwarf_strict && dwarf_version < 5)
15612 return NULL;
15613 gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
15614 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
15615 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
15616 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
15617 ret = new_loc_descr (dwarf_OP (DW_OP_implicit_pointer), 0, offset);
15618 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
15619 if (ref)
15621 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15622 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
15623 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
15625 else
15627 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
15628 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
15630 return ret;
15633 /* Output a proper Dwarf location descriptor for a variable or parameter
15634 which is either allocated in a register or in a memory location. For a
15635 register, we just generate an OP_REG and the register number. For a
15636 memory location we provide a Dwarf postfix expression describing how to
15637 generate the (dynamic) address of the object onto the address stack.
15639 MODE is mode of the decl if this loc_descriptor is going to be used in
15640 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
15641 allowed, VOIDmode otherwise.
15643 If we don't know how to describe it, return 0. */
15645 static dw_loc_descr_ref
15646 loc_descriptor (rtx rtl, machine_mode mode,
15647 enum var_init_status initialized)
15649 dw_loc_descr_ref loc_result = NULL;
15651 switch (GET_CODE (rtl))
15653 case SUBREG:
15654 /* The case of a subreg may arise when we have a local (register)
15655 variable or a formal (register) parameter which doesn't quite fill
15656 up an entire register. For now, just assume that it is
15657 legitimate to make the Dwarf info refer to the whole register which
15658 contains the given subreg. */
15659 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
15660 loc_result = loc_descriptor (SUBREG_REG (rtl),
15661 GET_MODE (SUBREG_REG (rtl)), initialized);
15662 else
15663 goto do_default;
15664 break;
15666 case REG:
15667 loc_result = reg_loc_descriptor (rtl, initialized);
15668 break;
15670 case MEM:
15671 loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
15672 GET_MODE (rtl), initialized);
15673 if (loc_result == NULL)
15674 loc_result = tls_mem_loc_descriptor (rtl);
15675 if (loc_result == NULL)
15677 rtx new_rtl = avoid_constant_pool_reference (rtl);
15678 if (new_rtl != rtl)
15679 loc_result = loc_descriptor (new_rtl, mode, initialized);
15681 break;
15683 case CONCAT:
15684 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
15685 initialized);
15686 break;
15688 case CONCATN:
15689 loc_result = concatn_loc_descriptor (rtl, initialized);
15690 break;
15692 case VAR_LOCATION:
15693 /* Single part. */
15694 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
15696 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
15697 if (GET_CODE (loc) == EXPR_LIST)
15698 loc = XEXP (loc, 0);
15699 loc_result = loc_descriptor (loc, mode, initialized);
15700 break;
15703 rtl = XEXP (rtl, 1);
15704 /* FALLTHRU */
15706 case PARALLEL:
15708 rtvec par_elems = XVEC (rtl, 0);
15709 int num_elem = GET_NUM_ELEM (par_elems);
15710 machine_mode mode;
15711 int i;
15713 /* Create the first one, so we have something to add to. */
15714 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
15715 VOIDmode, initialized);
15716 if (loc_result == NULL)
15717 return NULL;
15718 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
15719 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
15720 for (i = 1; i < num_elem; i++)
15722 dw_loc_descr_ref temp;
15724 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
15725 VOIDmode, initialized);
15726 if (temp == NULL)
15727 return NULL;
15728 add_loc_descr (&loc_result, temp);
15729 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
15730 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
15733 break;
15735 case CONST_INT:
15736 if (mode != VOIDmode && mode != BLKmode)
15737 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
15738 INTVAL (rtl));
15739 break;
15741 case CONST_DOUBLE:
15742 if (mode == VOIDmode)
15743 mode = GET_MODE (rtl);
15745 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
15747 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
15749 /* Note that a CONST_DOUBLE rtx could represent either an integer
15750 or a floating-point constant. A CONST_DOUBLE is used whenever
15751 the constant requires more than one word in order to be
15752 adequately represented. We output CONST_DOUBLEs as blocks. */
15753 loc_result = new_loc_descr (DW_OP_implicit_value,
15754 GET_MODE_SIZE (mode), 0);
15755 #if TARGET_SUPPORTS_WIDE_INT == 0
15756 if (!SCALAR_FLOAT_MODE_P (mode))
15758 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
15759 loc_result->dw_loc_oprnd2.v.val_double
15760 = rtx_to_double_int (rtl);
15762 else
15763 #endif
15765 unsigned int length = GET_MODE_SIZE (mode);
15766 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
15768 insert_float (rtl, array);
15769 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
15770 loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
15771 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
15772 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
15775 break;
15777 case CONST_WIDE_INT:
15778 if (mode == VOIDmode)
15779 mode = GET_MODE (rtl);
15781 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
15783 loc_result = new_loc_descr (DW_OP_implicit_value,
15784 GET_MODE_SIZE (mode), 0);
15785 loc_result->dw_loc_oprnd2.val_class = dw_val_class_wide_int;
15786 loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
15787 *loc_result->dw_loc_oprnd2.v.val_wide = rtx_mode_t (rtl, mode);
15789 break;
15791 case CONST_VECTOR:
15792 if (mode == VOIDmode)
15793 mode = GET_MODE (rtl);
15795 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
15797 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
15798 unsigned int length = CONST_VECTOR_NUNITS (rtl);
15799 unsigned char *array
15800 = ggc_vec_alloc<unsigned char> (length * elt_size);
15801 unsigned int i;
15802 unsigned char *p;
15803 machine_mode imode = GET_MODE_INNER (mode);
15805 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
15806 switch (GET_MODE_CLASS (mode))
15808 case MODE_VECTOR_INT:
15809 for (i = 0, p = array; i < length; i++, p += elt_size)
15811 rtx elt = CONST_VECTOR_ELT (rtl, i);
15812 insert_wide_int (rtx_mode_t (elt, imode), p, elt_size);
15814 break;
15816 case MODE_VECTOR_FLOAT:
15817 for (i = 0, p = array; i < length; i++, p += elt_size)
15819 rtx elt = CONST_VECTOR_ELT (rtl, i);
15820 insert_float (elt, p);
15822 break;
15824 default:
15825 gcc_unreachable ();
15828 loc_result = new_loc_descr (DW_OP_implicit_value,
15829 length * elt_size, 0);
15830 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
15831 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
15832 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
15833 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
15835 break;
15837 case CONST:
15838 if (mode == VOIDmode
15839 || CONST_SCALAR_INT_P (XEXP (rtl, 0))
15840 || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
15841 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
15843 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
15844 break;
15846 /* FALLTHROUGH */
15847 case SYMBOL_REF:
15848 if (!const_ok_for_output (rtl))
15849 break;
15850 /* FALLTHROUGH */
15851 case LABEL_REF:
15852 if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
15853 && (dwarf_version >= 4 || !dwarf_strict))
15855 loc_result = new_addr_loc_descr (rtl, dtprel_false);
15856 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
15857 vec_safe_push (used_rtx_array, rtl);
15859 break;
15861 case DEBUG_IMPLICIT_PTR:
15862 loc_result = implicit_ptr_descriptor (rtl, 0);
15863 break;
15865 case PLUS:
15866 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
15867 && CONST_INT_P (XEXP (rtl, 1)))
15869 loc_result
15870 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
15871 break;
15873 /* FALLTHRU */
15874 do_default:
15875 default:
15876 if ((SCALAR_INT_MODE_P (mode)
15877 && GET_MODE (rtl) == mode
15878 && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
15879 && dwarf_version >= 4)
15880 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
15882 /* Value expression. */
15883 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
15884 if (loc_result)
15885 add_loc_descr (&loc_result,
15886 new_loc_descr (DW_OP_stack_value, 0, 0));
15888 break;
15891 return loc_result;
15894 /* We need to figure out what section we should use as the base for the
15895 address ranges where a given location is valid.
15896 1. If this particular DECL has a section associated with it, use that.
15897 2. If this function has a section associated with it, use that.
15898 3. Otherwise, use the text section.
15899 XXX: If you split a variable across multiple sections, we won't notice. */
15901 static const char *
15902 secname_for_decl (const_tree decl)
15904 const char *secname;
15906 if (VAR_OR_FUNCTION_DECL_P (decl)
15907 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl) || TREE_STATIC (decl))
15908 && DECL_SECTION_NAME (decl))
15909 secname = DECL_SECTION_NAME (decl);
15910 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
15911 secname = DECL_SECTION_NAME (current_function_decl);
15912 else if (cfun && in_cold_section_p)
15913 secname = crtl->subsections.cold_section_label;
15914 else
15915 secname = text_section_label;
15917 return secname;
15920 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
15922 static bool
15923 decl_by_reference_p (tree decl)
15925 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
15926 || VAR_P (decl))
15927 && DECL_BY_REFERENCE (decl));
15930 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
15931 for VARLOC. */
15933 static dw_loc_descr_ref
15934 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
15935 enum var_init_status initialized)
15937 int have_address = 0;
15938 dw_loc_descr_ref descr;
15939 machine_mode mode;
15941 if (want_address != 2)
15943 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
15944 /* Single part. */
15945 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
15947 varloc = PAT_VAR_LOCATION_LOC (varloc);
15948 if (GET_CODE (varloc) == EXPR_LIST)
15949 varloc = XEXP (varloc, 0);
15950 mode = GET_MODE (varloc);
15951 if (MEM_P (varloc))
15953 rtx addr = XEXP (varloc, 0);
15954 descr = mem_loc_descriptor (addr, get_address_mode (varloc),
15955 mode, initialized);
15956 if (descr)
15957 have_address = 1;
15958 else
15960 rtx x = avoid_constant_pool_reference (varloc);
15961 if (x != varloc)
15962 descr = mem_loc_descriptor (x, mode, VOIDmode,
15963 initialized);
15966 else
15967 descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
15969 else
15970 return 0;
15972 else
15974 if (GET_CODE (varloc) == VAR_LOCATION)
15975 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
15976 else
15977 mode = DECL_MODE (loc);
15978 descr = loc_descriptor (varloc, mode, initialized);
15979 have_address = 1;
15982 if (!descr)
15983 return 0;
15985 if (want_address == 2 && !have_address
15986 && (dwarf_version >= 4 || !dwarf_strict))
15988 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
15990 expansion_failed (loc, NULL_RTX,
15991 "DWARF address size mismatch");
15992 return 0;
15994 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
15995 have_address = 1;
15997 /* Show if we can't fill the request for an address. */
15998 if (want_address && !have_address)
16000 expansion_failed (loc, NULL_RTX,
16001 "Want address and only have value");
16002 return 0;
16005 /* If we've got an address and don't want one, dereference. */
16006 if (!want_address && have_address)
16008 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
16009 enum dwarf_location_atom op;
16011 if (size > DWARF2_ADDR_SIZE || size == -1)
16013 expansion_failed (loc, NULL_RTX,
16014 "DWARF address size mismatch");
16015 return 0;
16017 else if (size == DWARF2_ADDR_SIZE)
16018 op = DW_OP_deref;
16019 else
16020 op = DW_OP_deref_size;
16022 add_loc_descr (&descr, new_loc_descr (op, size, 0));
16025 return descr;
16028 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
16029 if it is not possible. */
16031 static dw_loc_descr_ref
16032 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
16034 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
16035 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
16036 else if (dwarf_version >= 3 || !dwarf_strict)
16037 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
16038 else
16039 return NULL;
16042 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
16043 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
16045 static dw_loc_descr_ref
16046 dw_sra_loc_expr (tree decl, rtx loc)
16048 rtx p;
16049 unsigned HOST_WIDE_INT padsize = 0;
16050 dw_loc_descr_ref descr, *descr_tail;
16051 unsigned HOST_WIDE_INT decl_size;
16052 rtx varloc;
16053 enum var_init_status initialized;
16055 if (DECL_SIZE (decl) == NULL
16056 || !tree_fits_uhwi_p (DECL_SIZE (decl)))
16057 return NULL;
16059 decl_size = tree_to_uhwi (DECL_SIZE (decl));
16060 descr = NULL;
16061 descr_tail = &descr;
16063 for (p = loc; p; p = XEXP (p, 1))
16065 unsigned HOST_WIDE_INT bitsize = decl_piece_bitsize (p);
16066 rtx loc_note = *decl_piece_varloc_ptr (p);
16067 dw_loc_descr_ref cur_descr;
16068 dw_loc_descr_ref *tail, last = NULL;
16069 unsigned HOST_WIDE_INT opsize = 0;
16071 if (loc_note == NULL_RTX
16072 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
16074 padsize += bitsize;
16075 continue;
16077 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
16078 varloc = NOTE_VAR_LOCATION (loc_note);
16079 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
16080 if (cur_descr == NULL)
16082 padsize += bitsize;
16083 continue;
16086 /* Check that cur_descr either doesn't use
16087 DW_OP_*piece operations, or their sum is equal
16088 to bitsize. Otherwise we can't embed it. */
16089 for (tail = &cur_descr; *tail != NULL;
16090 tail = &(*tail)->dw_loc_next)
16091 if ((*tail)->dw_loc_opc == DW_OP_piece)
16093 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
16094 * BITS_PER_UNIT;
16095 last = *tail;
16097 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
16099 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
16100 last = *tail;
16103 if (last != NULL && opsize != bitsize)
16105 padsize += bitsize;
16106 /* Discard the current piece of the descriptor and release any
16107 addr_table entries it uses. */
16108 remove_loc_list_addr_table_entries (cur_descr);
16109 continue;
16112 /* If there is a hole, add DW_OP_*piece after empty DWARF
16113 expression, which means that those bits are optimized out. */
16114 if (padsize)
16116 if (padsize > decl_size)
16118 remove_loc_list_addr_table_entries (cur_descr);
16119 goto discard_descr;
16121 decl_size -= padsize;
16122 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
16123 if (*descr_tail == NULL)
16125 remove_loc_list_addr_table_entries (cur_descr);
16126 goto discard_descr;
16128 descr_tail = &(*descr_tail)->dw_loc_next;
16129 padsize = 0;
16131 *descr_tail = cur_descr;
16132 descr_tail = tail;
16133 if (bitsize > decl_size)
16134 goto discard_descr;
16135 decl_size -= bitsize;
16136 if (last == NULL)
16138 HOST_WIDE_INT offset = 0;
16139 if (GET_CODE (varloc) == VAR_LOCATION
16140 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
16142 varloc = PAT_VAR_LOCATION_LOC (varloc);
16143 if (GET_CODE (varloc) == EXPR_LIST)
16144 varloc = XEXP (varloc, 0);
16148 if (GET_CODE (varloc) == CONST
16149 || GET_CODE (varloc) == SIGN_EXTEND
16150 || GET_CODE (varloc) == ZERO_EXTEND)
16151 varloc = XEXP (varloc, 0);
16152 else if (GET_CODE (varloc) == SUBREG)
16153 varloc = SUBREG_REG (varloc);
16154 else
16155 break;
16157 while (1);
16158 /* DW_OP_bit_size offset should be zero for register
16159 or implicit location descriptions and empty location
16160 descriptions, but for memory addresses needs big endian
16161 adjustment. */
16162 if (MEM_P (varloc))
16164 unsigned HOST_WIDE_INT memsize
16165 = MEM_SIZE (varloc) * BITS_PER_UNIT;
16166 if (memsize != bitsize)
16168 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
16169 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
16170 goto discard_descr;
16171 if (memsize < bitsize)
16172 goto discard_descr;
16173 if (BITS_BIG_ENDIAN)
16174 offset = memsize - bitsize;
16178 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
16179 if (*descr_tail == NULL)
16180 goto discard_descr;
16181 descr_tail = &(*descr_tail)->dw_loc_next;
16185 /* If there were any non-empty expressions, add padding till the end of
16186 the decl. */
16187 if (descr != NULL && decl_size != 0)
16189 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
16190 if (*descr_tail == NULL)
16191 goto discard_descr;
16193 return descr;
16195 discard_descr:
16196 /* Discard the descriptor and release any addr_table entries it uses. */
16197 remove_loc_list_addr_table_entries (descr);
16198 return NULL;
16201 /* Return the dwarf representation of the location list LOC_LIST of
16202 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
16203 function. */
16205 static dw_loc_list_ref
16206 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
16208 const char *endname, *secname;
16209 rtx varloc;
16210 enum var_init_status initialized;
16211 struct var_loc_node *node;
16212 dw_loc_descr_ref descr;
16213 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
16214 dw_loc_list_ref list = NULL;
16215 dw_loc_list_ref *listp = &list;
16217 /* Now that we know what section we are using for a base,
16218 actually construct the list of locations.
16219 The first location information is what is passed to the
16220 function that creates the location list, and the remaining
16221 locations just get added on to that list.
16222 Note that we only know the start address for a location
16223 (IE location changes), so to build the range, we use
16224 the range [current location start, next location start].
16225 This means we have to special case the last node, and generate
16226 a range of [last location start, end of function label]. */
16228 secname = secname_for_decl (decl);
16230 for (node = loc_list->first; node; node = node->next)
16231 if (GET_CODE (node->loc) == EXPR_LIST
16232 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
16234 if (GET_CODE (node->loc) == EXPR_LIST)
16236 /* This requires DW_OP_{,bit_}piece, which is not usable
16237 inside DWARF expressions. */
16238 if (want_address != 2)
16239 continue;
16240 descr = dw_sra_loc_expr (decl, node->loc);
16241 if (descr == NULL)
16242 continue;
16244 else
16246 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
16247 varloc = NOTE_VAR_LOCATION (node->loc);
16248 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
16250 if (descr)
16252 bool range_across_switch = false;
16253 /* If section switch happens in between node->label
16254 and node->next->label (or end of function) and
16255 we can't emit it as a single entry list,
16256 emit two ranges, first one ending at the end
16257 of first partition and second one starting at the
16258 beginning of second partition. */
16259 if (node == loc_list->last_before_switch
16260 && (node != loc_list->first || loc_list->first->next)
16261 && current_function_decl)
16263 endname = cfun->fde->dw_fde_end;
16264 range_across_switch = true;
16266 /* The variable has a location between NODE->LABEL and
16267 NODE->NEXT->LABEL. */
16268 else if (node->next)
16269 endname = node->next->label;
16270 /* If the variable has a location at the last label
16271 it keeps its location until the end of function. */
16272 else if (!current_function_decl)
16273 endname = text_end_label;
16274 else
16276 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
16277 current_function_funcdef_no);
16278 endname = ggc_strdup (label_id);
16281 *listp = new_loc_list (descr, node->label, endname, secname);
16282 if (TREE_CODE (decl) == PARM_DECL
16283 && node == loc_list->first
16284 && NOTE_P (node->loc)
16285 && strcmp (node->label, endname) == 0)
16286 (*listp)->force = true;
16287 listp = &(*listp)->dw_loc_next;
16289 if (range_across_switch)
16291 if (GET_CODE (node->loc) == EXPR_LIST)
16292 descr = dw_sra_loc_expr (decl, node->loc);
16293 else
16295 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
16296 varloc = NOTE_VAR_LOCATION (node->loc);
16297 descr = dw_loc_list_1 (decl, varloc, want_address,
16298 initialized);
16300 gcc_assert (descr);
16301 /* The variable has a location between NODE->LABEL and
16302 NODE->NEXT->LABEL. */
16303 if (node->next)
16304 endname = node->next->label;
16305 else
16306 endname = cfun->fde->dw_fde_second_end;
16307 *listp = new_loc_list (descr,
16308 cfun->fde->dw_fde_second_begin,
16309 endname, secname);
16310 listp = &(*listp)->dw_loc_next;
16315 /* Try to avoid the overhead of a location list emitting a location
16316 expression instead, but only if we didn't have more than one
16317 location entry in the first place. If some entries were not
16318 representable, we don't want to pretend a single entry that was
16319 applies to the entire scope in which the variable is
16320 available. */
16321 if (list && loc_list->first->next)
16322 gen_llsym (list);
16324 return list;
16327 /* Return if the loc_list has only single element and thus can be represented
16328 as location description. */
16330 static bool
16331 single_element_loc_list_p (dw_loc_list_ref list)
16333 gcc_assert (!list->dw_loc_next || list->ll_symbol);
16334 return !list->ll_symbol;
16337 /* Duplicate a single element of location list. */
16339 static inline dw_loc_descr_ref
16340 copy_loc_descr (dw_loc_descr_ref ref)
16342 dw_loc_descr_ref copy = ggc_alloc<dw_loc_descr_node> ();
16343 memcpy (copy, ref, sizeof (dw_loc_descr_node));
16344 return copy;
16347 /* To each location in list LIST append loc descr REF. */
16349 static void
16350 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
16352 dw_loc_descr_ref copy;
16353 add_loc_descr (&list->expr, ref);
16354 list = list->dw_loc_next;
16355 while (list)
16357 copy = copy_loc_descr (ref);
16358 add_loc_descr (&list->expr, copy);
16359 while (copy->dw_loc_next)
16360 copy = copy->dw_loc_next = copy_loc_descr (copy->dw_loc_next);
16361 list = list->dw_loc_next;
16365 /* To each location in list LIST prepend loc descr REF. */
16367 static void
16368 prepend_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
16370 dw_loc_descr_ref copy;
16371 dw_loc_descr_ref ref_end = list->expr;
16372 add_loc_descr (&ref, list->expr);
16373 list->expr = ref;
16374 list = list->dw_loc_next;
16375 while (list)
16377 dw_loc_descr_ref end = list->expr;
16378 list->expr = copy = copy_loc_descr (ref);
16379 while (copy->dw_loc_next != ref_end)
16380 copy = copy->dw_loc_next = copy_loc_descr (copy->dw_loc_next);
16381 copy->dw_loc_next = end;
16382 list = list->dw_loc_next;
16386 /* Given two lists RET and LIST
16387 produce location list that is result of adding expression in LIST
16388 to expression in RET on each position in program.
16389 Might be destructive on both RET and LIST.
16391 TODO: We handle only simple cases of RET or LIST having at most one
16392 element. General case would involve sorting the lists in program order
16393 and merging them that will need some additional work.
16394 Adding that will improve quality of debug info especially for SRA-ed
16395 structures. */
16397 static void
16398 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
16400 if (!list)
16401 return;
16402 if (!*ret)
16404 *ret = list;
16405 return;
16407 if (!list->dw_loc_next)
16409 add_loc_descr_to_each (*ret, list->expr);
16410 return;
16412 if (!(*ret)->dw_loc_next)
16414 prepend_loc_descr_to_each (list, (*ret)->expr);
16415 *ret = list;
16416 return;
16418 expansion_failed (NULL_TREE, NULL_RTX,
16419 "Don't know how to merge two non-trivial"
16420 " location lists.\n");
16421 *ret = NULL;
16422 return;
16425 /* LOC is constant expression. Try a luck, look it up in constant
16426 pool and return its loc_descr of its address. */
16428 static dw_loc_descr_ref
16429 cst_pool_loc_descr (tree loc)
16431 /* Get an RTL for this, if something has been emitted. */
16432 rtx rtl = lookup_constant_def (loc);
16434 if (!rtl || !MEM_P (rtl))
16436 gcc_assert (!rtl);
16437 return 0;
16439 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
16441 /* TODO: We might get more coverage if we was actually delaying expansion
16442 of all expressions till end of compilation when constant pools are fully
16443 populated. */
16444 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
16446 expansion_failed (loc, NULL_RTX,
16447 "CST value in contant pool but not marked.");
16448 return 0;
16450 return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
16451 GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
16454 /* Return dw_loc_list representing address of addr_expr LOC
16455 by looking for inner INDIRECT_REF expression and turning
16456 it into simple arithmetics.
16458 See loc_list_from_tree for the meaning of CONTEXT. */
16460 static dw_loc_list_ref
16461 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev,
16462 loc_descr_context *context)
16464 tree obj, offset;
16465 HOST_WIDE_INT bitsize, bitpos, bytepos;
16466 machine_mode mode;
16467 int unsignedp, reversep, volatilep = 0;
16468 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
16470 obj = get_inner_reference (TREE_OPERAND (loc, 0),
16471 &bitsize, &bitpos, &offset, &mode,
16472 &unsignedp, &reversep, &volatilep);
16473 STRIP_NOPS (obj);
16474 if (bitpos % BITS_PER_UNIT)
16476 expansion_failed (loc, NULL_RTX, "bitfield access");
16477 return 0;
16479 if (!INDIRECT_REF_P (obj))
16481 expansion_failed (obj,
16482 NULL_RTX, "no indirect ref in inner refrence");
16483 return 0;
16485 if (!offset && !bitpos)
16486 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1,
16487 context);
16488 else if (toplev
16489 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
16490 && (dwarf_version >= 4 || !dwarf_strict))
16492 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0, context);
16493 if (!list_ret)
16494 return 0;
16495 if (offset)
16497 /* Variable offset. */
16498 list_ret1 = loc_list_from_tree (offset, 0, context);
16499 if (list_ret1 == 0)
16500 return 0;
16501 add_loc_list (&list_ret, list_ret1);
16502 if (!list_ret)
16503 return 0;
16504 add_loc_descr_to_each (list_ret,
16505 new_loc_descr (DW_OP_plus, 0, 0));
16507 bytepos = bitpos / BITS_PER_UNIT;
16508 if (bytepos > 0)
16509 add_loc_descr_to_each (list_ret,
16510 new_loc_descr (DW_OP_plus_uconst,
16511 bytepos, 0));
16512 else if (bytepos < 0)
16513 loc_list_plus_const (list_ret, bytepos);
16514 add_loc_descr_to_each (list_ret,
16515 new_loc_descr (DW_OP_stack_value, 0, 0));
16517 return list_ret;
16520 /* Set LOC to the next operation that is not a DW_OP_nop operation. In the case
16521 all operations from LOC are nops, move to the last one. Insert in NOPS all
16522 operations that are skipped. */
16524 static void
16525 loc_descr_to_next_no_nop (dw_loc_descr_ref &loc,
16526 hash_set<dw_loc_descr_ref> &nops)
16528 while (loc->dw_loc_next != NULL && loc->dw_loc_opc == DW_OP_nop)
16530 nops.add (loc);
16531 loc = loc->dw_loc_next;
16535 /* Helper for loc_descr_without_nops: free the location description operation
16536 P. */
16538 bool
16539 free_loc_descr (const dw_loc_descr_ref &loc, void *data ATTRIBUTE_UNUSED)
16541 ggc_free (loc);
16542 return true;
16545 /* Remove all DW_OP_nop operations from LOC except, if it exists, the one that
16546 finishes LOC. */
16548 static void
16549 loc_descr_without_nops (dw_loc_descr_ref &loc)
16551 if (loc->dw_loc_opc == DW_OP_nop && loc->dw_loc_next == NULL)
16552 return;
16554 /* Set of all DW_OP_nop operations we remove. */
16555 hash_set<dw_loc_descr_ref> nops;
16557 /* First, strip all prefix NOP operations in order to keep the head of the
16558 operations list. */
16559 loc_descr_to_next_no_nop (loc, nops);
16561 for (dw_loc_descr_ref cur = loc; cur != NULL;)
16563 /* For control flow operations: strip "prefix" nops in destination
16564 labels. */
16565 if (cur->dw_loc_oprnd1.val_class == dw_val_class_loc)
16566 loc_descr_to_next_no_nop (cur->dw_loc_oprnd1.v.val_loc, nops);
16567 if (cur->dw_loc_oprnd2.val_class == dw_val_class_loc)
16568 loc_descr_to_next_no_nop (cur->dw_loc_oprnd2.v.val_loc, nops);
16570 /* Do the same for the operations that follow, then move to the next
16571 iteration. */
16572 if (cur->dw_loc_next != NULL)
16573 loc_descr_to_next_no_nop (cur->dw_loc_next, nops);
16574 cur = cur->dw_loc_next;
16577 nops.traverse<void *, free_loc_descr> (NULL);
16581 struct dwarf_procedure_info;
16583 /* Helper structure for location descriptions generation. */
16584 struct loc_descr_context
16586 /* The type that is implicitly referenced by DW_OP_push_object_address, or
16587 NULL_TREE if DW_OP_push_object_address in invalid for this location
16588 description. This is used when processing PLACEHOLDER_EXPR nodes. */
16589 tree context_type;
16590 /* The ..._DECL node that should be translated as a
16591 DW_OP_push_object_address operation. */
16592 tree base_decl;
16593 /* Information about the DWARF procedure we are currently generating. NULL if
16594 we are not generating a DWARF procedure. */
16595 struct dwarf_procedure_info *dpi;
16596 /* True if integral PLACEHOLDER_EXPR stands for the first argument passed
16597 by consumer. Used for DW_TAG_generic_subrange attributes. */
16598 bool placeholder_arg;
16599 /* True if PLACEHOLDER_EXPR has been seen. */
16600 bool placeholder_seen;
16603 /* DWARF procedures generation
16605 DWARF expressions (aka. location descriptions) are used to encode variable
16606 things such as sizes or offsets. Such computations can have redundant parts
16607 that can be factorized in order to reduce the size of the output debug
16608 information. This is the whole point of DWARF procedures.
16610 Thanks to stor-layout.c, size and offset expressions in GENERIC trees are
16611 already factorized into functions ("size functions") in order to handle very
16612 big and complex types. Such functions are quite simple: they have integral
16613 arguments, they return an integral result and their body contains only a
16614 return statement with arithmetic expressions. This is the only kind of
16615 function we are interested in translating into DWARF procedures, here.
16617 DWARF expressions and DWARF procedure are executed using a stack, so we have
16618 to define some calling convention for them to interact. Let's say that:
16620 - Before calling a DWARF procedure, DWARF expressions must push on the stack
16621 all arguments in reverse order (right-to-left) so that when the DWARF
16622 procedure execution starts, the first argument is the top of the stack.
16624 - Then, when returning, the DWARF procedure must have consumed all arguments
16625 on the stack, must have pushed the result and touched nothing else.
16627 - Each integral argument and the result are integral types can be hold in a
16628 single stack slot.
16630 - We call "frame offset" the number of stack slots that are "under DWARF
16631 procedure control": it includes the arguments slots, the temporaries and
16632 the result slot. Thus, it is equal to the number of arguments when the
16633 procedure execution starts and must be equal to one (the result) when it
16634 returns. */
16636 /* Helper structure used when generating operations for a DWARF procedure. */
16637 struct dwarf_procedure_info
16639 /* The FUNCTION_DECL node corresponding to the DWARF procedure that is
16640 currently translated. */
16641 tree fndecl;
16642 /* The number of arguments FNDECL takes. */
16643 unsigned args_count;
16646 /* Return a pointer to a newly created DIE node for a DWARF procedure. Add
16647 LOCATION as its DW_AT_location attribute. If FNDECL is not NULL_TREE,
16648 equate it to this DIE. */
16650 static dw_die_ref
16651 new_dwarf_proc_die (dw_loc_descr_ref location, tree fndecl,
16652 dw_die_ref parent_die)
16654 dw_die_ref dwarf_proc_die;
16656 if ((dwarf_version < 3 && dwarf_strict)
16657 || location == NULL)
16658 return NULL;
16660 dwarf_proc_die = new_die (DW_TAG_dwarf_procedure, parent_die, fndecl);
16661 if (fndecl)
16662 equate_decl_number_to_die (fndecl, dwarf_proc_die);
16663 add_AT_loc (dwarf_proc_die, DW_AT_location, location);
16664 return dwarf_proc_die;
16667 /* Return whether TYPE is a supported type as a DWARF procedure argument
16668 type or return type (we handle only scalar types and pointer types that
16669 aren't wider than the DWARF expression evaluation stack. */
16671 static bool
16672 is_handled_procedure_type (tree type)
16674 return ((INTEGRAL_TYPE_P (type)
16675 || TREE_CODE (type) == OFFSET_TYPE
16676 || TREE_CODE (type) == POINTER_TYPE)
16677 && int_size_in_bytes (type) <= DWARF2_ADDR_SIZE);
16680 /* Helper for resolve_args_picking: do the same but stop when coming across
16681 visited nodes. For each node we visit, register in FRAME_OFFSETS the frame
16682 offset *before* evaluating the corresponding operation. */
16684 static bool
16685 resolve_args_picking_1 (dw_loc_descr_ref loc, unsigned initial_frame_offset,
16686 struct dwarf_procedure_info *dpi,
16687 hash_map<dw_loc_descr_ref, unsigned> &frame_offsets)
16689 /* The "frame_offset" identifier is already used to name a macro... */
16690 unsigned frame_offset_ = initial_frame_offset;
16691 dw_loc_descr_ref l;
16693 for (l = loc; l != NULL;)
16695 bool existed;
16696 unsigned &l_frame_offset = frame_offsets.get_or_insert (l, &existed);
16698 /* If we already met this node, there is nothing to compute anymore. */
16699 if (existed)
16701 /* Make sure that the stack size is consistent wherever the execution
16702 flow comes from. */
16703 gcc_assert ((unsigned) l_frame_offset == frame_offset_);
16704 break;
16706 l_frame_offset = frame_offset_;
16708 /* If needed, relocate the picking offset with respect to the frame
16709 offset. */
16710 if (l->frame_offset_rel)
16712 unsigned HOST_WIDE_INT off;
16713 switch (l->dw_loc_opc)
16715 case DW_OP_pick:
16716 off = l->dw_loc_oprnd1.v.val_unsigned;
16717 break;
16718 case DW_OP_dup:
16719 off = 0;
16720 break;
16721 case DW_OP_over:
16722 off = 1;
16723 break;
16724 default:
16725 gcc_unreachable ();
16727 /* frame_offset_ is the size of the current stack frame, including
16728 incoming arguments. Besides, the arguments are pushed
16729 right-to-left. Thus, in order to access the Nth argument from
16730 this operation node, the picking has to skip temporaries *plus*
16731 one stack slot per argument (0 for the first one, 1 for the second
16732 one, etc.).
16734 The targetted argument number (N) is already set as the operand,
16735 and the number of temporaries can be computed with:
16736 frame_offsets_ - dpi->args_count */
16737 off += frame_offset_ - dpi->args_count;
16739 /* DW_OP_pick handles only offsets from 0 to 255 (inclusive)... */
16740 if (off > 255)
16741 return false;
16743 if (off == 0)
16745 l->dw_loc_opc = DW_OP_dup;
16746 l->dw_loc_oprnd1.v.val_unsigned = 0;
16748 else if (off == 1)
16750 l->dw_loc_opc = DW_OP_over;
16751 l->dw_loc_oprnd1.v.val_unsigned = 0;
16753 else
16755 l->dw_loc_opc = DW_OP_pick;
16756 l->dw_loc_oprnd1.v.val_unsigned = off;
16760 /* Update frame_offset according to the effect the current operation has
16761 on the stack. */
16762 switch (l->dw_loc_opc)
16764 case DW_OP_deref:
16765 case DW_OP_swap:
16766 case DW_OP_rot:
16767 case DW_OP_abs:
16768 case DW_OP_neg:
16769 case DW_OP_not:
16770 case DW_OP_plus_uconst:
16771 case DW_OP_skip:
16772 case DW_OP_reg0:
16773 case DW_OP_reg1:
16774 case DW_OP_reg2:
16775 case DW_OP_reg3:
16776 case DW_OP_reg4:
16777 case DW_OP_reg5:
16778 case DW_OP_reg6:
16779 case DW_OP_reg7:
16780 case DW_OP_reg8:
16781 case DW_OP_reg9:
16782 case DW_OP_reg10:
16783 case DW_OP_reg11:
16784 case DW_OP_reg12:
16785 case DW_OP_reg13:
16786 case DW_OP_reg14:
16787 case DW_OP_reg15:
16788 case DW_OP_reg16:
16789 case DW_OP_reg17:
16790 case DW_OP_reg18:
16791 case DW_OP_reg19:
16792 case DW_OP_reg20:
16793 case DW_OP_reg21:
16794 case DW_OP_reg22:
16795 case DW_OP_reg23:
16796 case DW_OP_reg24:
16797 case DW_OP_reg25:
16798 case DW_OP_reg26:
16799 case DW_OP_reg27:
16800 case DW_OP_reg28:
16801 case DW_OP_reg29:
16802 case DW_OP_reg30:
16803 case DW_OP_reg31:
16804 case DW_OP_bregx:
16805 case DW_OP_piece:
16806 case DW_OP_deref_size:
16807 case DW_OP_nop:
16808 case DW_OP_bit_piece:
16809 case DW_OP_implicit_value:
16810 case DW_OP_stack_value:
16811 break;
16813 case DW_OP_addr:
16814 case DW_OP_const1u:
16815 case DW_OP_const1s:
16816 case DW_OP_const2u:
16817 case DW_OP_const2s:
16818 case DW_OP_const4u:
16819 case DW_OP_const4s:
16820 case DW_OP_const8u:
16821 case DW_OP_const8s:
16822 case DW_OP_constu:
16823 case DW_OP_consts:
16824 case DW_OP_dup:
16825 case DW_OP_over:
16826 case DW_OP_pick:
16827 case DW_OP_lit0:
16828 case DW_OP_lit1:
16829 case DW_OP_lit2:
16830 case DW_OP_lit3:
16831 case DW_OP_lit4:
16832 case DW_OP_lit5:
16833 case DW_OP_lit6:
16834 case DW_OP_lit7:
16835 case DW_OP_lit8:
16836 case DW_OP_lit9:
16837 case DW_OP_lit10:
16838 case DW_OP_lit11:
16839 case DW_OP_lit12:
16840 case DW_OP_lit13:
16841 case DW_OP_lit14:
16842 case DW_OP_lit15:
16843 case DW_OP_lit16:
16844 case DW_OP_lit17:
16845 case DW_OP_lit18:
16846 case DW_OP_lit19:
16847 case DW_OP_lit20:
16848 case DW_OP_lit21:
16849 case DW_OP_lit22:
16850 case DW_OP_lit23:
16851 case DW_OP_lit24:
16852 case DW_OP_lit25:
16853 case DW_OP_lit26:
16854 case DW_OP_lit27:
16855 case DW_OP_lit28:
16856 case DW_OP_lit29:
16857 case DW_OP_lit30:
16858 case DW_OP_lit31:
16859 case DW_OP_breg0:
16860 case DW_OP_breg1:
16861 case DW_OP_breg2:
16862 case DW_OP_breg3:
16863 case DW_OP_breg4:
16864 case DW_OP_breg5:
16865 case DW_OP_breg6:
16866 case DW_OP_breg7:
16867 case DW_OP_breg8:
16868 case DW_OP_breg9:
16869 case DW_OP_breg10:
16870 case DW_OP_breg11:
16871 case DW_OP_breg12:
16872 case DW_OP_breg13:
16873 case DW_OP_breg14:
16874 case DW_OP_breg15:
16875 case DW_OP_breg16:
16876 case DW_OP_breg17:
16877 case DW_OP_breg18:
16878 case DW_OP_breg19:
16879 case DW_OP_breg20:
16880 case DW_OP_breg21:
16881 case DW_OP_breg22:
16882 case DW_OP_breg23:
16883 case DW_OP_breg24:
16884 case DW_OP_breg25:
16885 case DW_OP_breg26:
16886 case DW_OP_breg27:
16887 case DW_OP_breg28:
16888 case DW_OP_breg29:
16889 case DW_OP_breg30:
16890 case DW_OP_breg31:
16891 case DW_OP_fbreg:
16892 case DW_OP_push_object_address:
16893 case DW_OP_call_frame_cfa:
16894 case DW_OP_GNU_variable_value:
16895 ++frame_offset_;
16896 break;
16898 case DW_OP_drop:
16899 case DW_OP_xderef:
16900 case DW_OP_and:
16901 case DW_OP_div:
16902 case DW_OP_minus:
16903 case DW_OP_mod:
16904 case DW_OP_mul:
16905 case DW_OP_or:
16906 case DW_OP_plus:
16907 case DW_OP_shl:
16908 case DW_OP_shr:
16909 case DW_OP_shra:
16910 case DW_OP_xor:
16911 case DW_OP_bra:
16912 case DW_OP_eq:
16913 case DW_OP_ge:
16914 case DW_OP_gt:
16915 case DW_OP_le:
16916 case DW_OP_lt:
16917 case DW_OP_ne:
16918 case DW_OP_regx:
16919 case DW_OP_xderef_size:
16920 --frame_offset_;
16921 break;
16923 case DW_OP_call2:
16924 case DW_OP_call4:
16925 case DW_OP_call_ref:
16927 dw_die_ref dwarf_proc = l->dw_loc_oprnd1.v.val_die_ref.die;
16928 int *stack_usage = dwarf_proc_stack_usage_map->get (dwarf_proc);
16930 if (stack_usage == NULL)
16931 return false;
16932 frame_offset_ += *stack_usage;
16933 break;
16936 case DW_OP_implicit_pointer:
16937 case DW_OP_entry_value:
16938 case DW_OP_const_type:
16939 case DW_OP_regval_type:
16940 case DW_OP_deref_type:
16941 case DW_OP_convert:
16942 case DW_OP_reinterpret:
16943 case DW_OP_form_tls_address:
16944 case DW_OP_GNU_push_tls_address:
16945 case DW_OP_GNU_uninit:
16946 case DW_OP_GNU_encoded_addr:
16947 case DW_OP_GNU_implicit_pointer:
16948 case DW_OP_GNU_entry_value:
16949 case DW_OP_GNU_const_type:
16950 case DW_OP_GNU_regval_type:
16951 case DW_OP_GNU_deref_type:
16952 case DW_OP_GNU_convert:
16953 case DW_OP_GNU_reinterpret:
16954 case DW_OP_GNU_parameter_ref:
16955 /* loc_list_from_tree will probably not output these operations for
16956 size functions, so assume they will not appear here. */
16957 /* Fall through... */
16959 default:
16960 gcc_unreachable ();
16963 /* Now, follow the control flow (except subroutine calls). */
16964 switch (l->dw_loc_opc)
16966 case DW_OP_bra:
16967 if (!resolve_args_picking_1 (l->dw_loc_next, frame_offset_, dpi,
16968 frame_offsets))
16969 return false;
16970 /* Fall through. */
16972 case DW_OP_skip:
16973 l = l->dw_loc_oprnd1.v.val_loc;
16974 break;
16976 case DW_OP_stack_value:
16977 return true;
16979 default:
16980 l = l->dw_loc_next;
16981 break;
16985 return true;
16988 /* Make a DFS over operations reachable through LOC (i.e. follow branch
16989 operations) in order to resolve the operand of DW_OP_pick operations that
16990 target DWARF procedure arguments (DPI). INITIAL_FRAME_OFFSET is the frame
16991 offset *before* LOC is executed. Return if all relocations were
16992 successful. */
16994 static bool
16995 resolve_args_picking (dw_loc_descr_ref loc, unsigned initial_frame_offset,
16996 struct dwarf_procedure_info *dpi)
16998 /* Associate to all visited operations the frame offset *before* evaluating
16999 this operation. */
17000 hash_map<dw_loc_descr_ref, unsigned> frame_offsets;
17002 return resolve_args_picking_1 (loc, initial_frame_offset, dpi,
17003 frame_offsets);
17006 /* Try to generate a DWARF procedure that computes the same result as FNDECL.
17007 Return NULL if it is not possible. */
17009 static dw_die_ref
17010 function_to_dwarf_procedure (tree fndecl)
17012 struct loc_descr_context ctx;
17013 struct dwarf_procedure_info dpi;
17014 dw_die_ref dwarf_proc_die;
17015 tree tree_body = DECL_SAVED_TREE (fndecl);
17016 dw_loc_descr_ref loc_body, epilogue;
17018 tree cursor;
17019 unsigned i;
17021 /* Do not generate multiple DWARF procedures for the same function
17022 declaration. */
17023 dwarf_proc_die = lookup_decl_die (fndecl);
17024 if (dwarf_proc_die != NULL)
17025 return dwarf_proc_die;
17027 /* DWARF procedures are available starting with the DWARFv3 standard. */
17028 if (dwarf_version < 3 && dwarf_strict)
17029 return NULL;
17031 /* We handle only functions for which we still have a body, that return a
17032 supported type and that takes arguments with supported types. Note that
17033 there is no point translating functions that return nothing. */
17034 if (tree_body == NULL_TREE
17035 || DECL_RESULT (fndecl) == NULL_TREE
17036 || !is_handled_procedure_type (TREE_TYPE (DECL_RESULT (fndecl))))
17037 return NULL;
17039 for (cursor = DECL_ARGUMENTS (fndecl);
17040 cursor != NULL_TREE;
17041 cursor = TREE_CHAIN (cursor))
17042 if (!is_handled_procedure_type (TREE_TYPE (cursor)))
17043 return NULL;
17045 /* Match only "expr" in: RETURN_EXPR (MODIFY_EXPR (RESULT_DECL, expr)). */
17046 if (TREE_CODE (tree_body) != RETURN_EXPR)
17047 return NULL;
17048 tree_body = TREE_OPERAND (tree_body, 0);
17049 if (TREE_CODE (tree_body) != MODIFY_EXPR
17050 || TREE_OPERAND (tree_body, 0) != DECL_RESULT (fndecl))
17051 return NULL;
17052 tree_body = TREE_OPERAND (tree_body, 1);
17054 /* Try to translate the body expression itself. Note that this will probably
17055 cause an infinite recursion if its call graph has a cycle. This is very
17056 unlikely for size functions, however, so don't bother with such things at
17057 the moment. */
17058 ctx.context_type = NULL_TREE;
17059 ctx.base_decl = NULL_TREE;
17060 ctx.dpi = &dpi;
17061 ctx.placeholder_arg = false;
17062 ctx.placeholder_seen = false;
17063 dpi.fndecl = fndecl;
17064 dpi.args_count = list_length (DECL_ARGUMENTS (fndecl));
17065 loc_body = loc_descriptor_from_tree (tree_body, 0, &ctx);
17066 if (!loc_body)
17067 return NULL;
17069 /* After evaluating all operands in "loc_body", we should still have on the
17070 stack all arguments plus the desired function result (top of the stack).
17071 Generate code in order to keep only the result in our stack frame. */
17072 epilogue = NULL;
17073 for (i = 0; i < dpi.args_count; ++i)
17075 dw_loc_descr_ref op_couple = new_loc_descr (DW_OP_swap, 0, 0);
17076 op_couple->dw_loc_next = new_loc_descr (DW_OP_drop, 0, 0);
17077 op_couple->dw_loc_next->dw_loc_next = epilogue;
17078 epilogue = op_couple;
17080 add_loc_descr (&loc_body, epilogue);
17081 if (!resolve_args_picking (loc_body, dpi.args_count, &dpi))
17082 return NULL;
17084 /* Trailing nops from loc_descriptor_from_tree (if any) cannot be removed
17085 because they are considered useful. Now there is an epilogue, they are
17086 not anymore, so give it another try. */
17087 loc_descr_without_nops (loc_body);
17089 /* fndecl may be used both as a regular DW_TAG_subprogram DIE and as
17090 a DW_TAG_dwarf_procedure, so we may have a conflict, here. It's unlikely,
17091 though, given that size functions do not come from source, so they should
17092 not have a dedicated DW_TAG_subprogram DIE. */
17093 dwarf_proc_die
17094 = new_dwarf_proc_die (loc_body, fndecl,
17095 get_context_die (DECL_CONTEXT (fndecl)));
17097 /* The called DWARF procedure consumes one stack slot per argument and
17098 returns one stack slot. */
17099 dwarf_proc_stack_usage_map->put (dwarf_proc_die, 1 - dpi.args_count);
17101 return dwarf_proc_die;
17105 /* Generate Dwarf location list representing LOC.
17106 If WANT_ADDRESS is false, expression computing LOC will be computed
17107 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
17108 if WANT_ADDRESS is 2, expression computing address useable in location
17109 will be returned (i.e. DW_OP_reg can be used
17110 to refer to register values).
17112 CONTEXT provides information to customize the location descriptions
17113 generation. Its context_type field specifies what type is implicitly
17114 referenced by DW_OP_push_object_address. If it is NULL_TREE, this operation
17115 will not be generated.
17117 Its DPI field determines whether we are generating a DWARF expression for a
17118 DWARF procedure, so PARM_DECL references are processed specifically.
17120 If CONTEXT is NULL, the behavior is the same as if context_type, base_decl
17121 and dpi fields were null. */
17123 static dw_loc_list_ref
17124 loc_list_from_tree_1 (tree loc, int want_address,
17125 struct loc_descr_context *context)
17127 dw_loc_descr_ref ret = NULL, ret1 = NULL;
17128 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
17129 int have_address = 0;
17130 enum dwarf_location_atom op;
17132 /* ??? Most of the time we do not take proper care for sign/zero
17133 extending the values properly. Hopefully this won't be a real
17134 problem... */
17136 if (context != NULL
17137 && context->base_decl == loc
17138 && want_address == 0)
17140 if (dwarf_version >= 3 || !dwarf_strict)
17141 return new_loc_list (new_loc_descr (DW_OP_push_object_address, 0, 0),
17142 NULL, NULL, NULL);
17143 else
17144 return NULL;
17147 switch (TREE_CODE (loc))
17149 case ERROR_MARK:
17150 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
17151 return 0;
17153 case PLACEHOLDER_EXPR:
17154 /* This case involves extracting fields from an object to determine the
17155 position of other fields. It is supposed to appear only as the first
17156 operand of COMPONENT_REF nodes and to reference precisely the type
17157 that the context allows. */
17158 if (context != NULL
17159 && TREE_TYPE (loc) == context->context_type
17160 && want_address >= 1)
17162 if (dwarf_version >= 3 || !dwarf_strict)
17164 ret = new_loc_descr (DW_OP_push_object_address, 0, 0);
17165 have_address = 1;
17166 break;
17168 else
17169 return NULL;
17171 /* For DW_TAG_generic_subrange attributes, PLACEHOLDER_EXPR stands for
17172 the single argument passed by consumer. */
17173 else if (context != NULL
17174 && context->placeholder_arg
17175 && INTEGRAL_TYPE_P (TREE_TYPE (loc))
17176 && want_address == 0)
17178 ret = new_loc_descr (DW_OP_pick, 0, 0);
17179 ret->frame_offset_rel = 1;
17180 context->placeholder_seen = true;
17181 break;
17183 else
17184 expansion_failed (loc, NULL_RTX,
17185 "PLACEHOLDER_EXPR for an unexpected type");
17186 break;
17188 case CALL_EXPR:
17190 const int nargs = call_expr_nargs (loc);
17191 tree callee = get_callee_fndecl (loc);
17192 int i;
17193 dw_die_ref dwarf_proc;
17195 if (callee == NULL_TREE)
17196 goto call_expansion_failed;
17198 /* We handle only functions that return an integer. */
17199 if (!is_handled_procedure_type (TREE_TYPE (TREE_TYPE (callee))))
17200 goto call_expansion_failed;
17202 dwarf_proc = function_to_dwarf_procedure (callee);
17203 if (dwarf_proc == NULL)
17204 goto call_expansion_failed;
17206 /* Evaluate arguments right-to-left so that the first argument will
17207 be the top-most one on the stack. */
17208 for (i = nargs - 1; i >= 0; --i)
17210 dw_loc_descr_ref loc_descr
17211 = loc_descriptor_from_tree (CALL_EXPR_ARG (loc, i), 0,
17212 context);
17214 if (loc_descr == NULL)
17215 goto call_expansion_failed;
17217 add_loc_descr (&ret, loc_descr);
17220 ret1 = new_loc_descr (DW_OP_call4, 0, 0);
17221 ret1->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
17222 ret1->dw_loc_oprnd1.v.val_die_ref.die = dwarf_proc;
17223 ret1->dw_loc_oprnd1.v.val_die_ref.external = 0;
17224 add_loc_descr (&ret, ret1);
17225 break;
17227 call_expansion_failed:
17228 expansion_failed (loc, NULL_RTX, "CALL_EXPR");
17229 /* There are no opcodes for these operations. */
17230 return 0;
17233 case PREINCREMENT_EXPR:
17234 case PREDECREMENT_EXPR:
17235 case POSTINCREMENT_EXPR:
17236 case POSTDECREMENT_EXPR:
17237 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
17238 /* There are no opcodes for these operations. */
17239 return 0;
17241 case ADDR_EXPR:
17242 /* If we already want an address, see if there is INDIRECT_REF inside
17243 e.g. for &this->field. */
17244 if (want_address)
17246 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
17247 (loc, want_address == 2, context);
17248 if (list_ret)
17249 have_address = 1;
17250 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
17251 && (ret = cst_pool_loc_descr (loc)))
17252 have_address = 1;
17254 /* Otherwise, process the argument and look for the address. */
17255 if (!list_ret && !ret)
17256 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 1, context);
17257 else
17259 if (want_address)
17260 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
17261 return NULL;
17263 break;
17265 case VAR_DECL:
17266 if (DECL_THREAD_LOCAL_P (loc))
17268 rtx rtl;
17269 enum dwarf_location_atom tls_op;
17270 enum dtprel_bool dtprel = dtprel_false;
17272 if (targetm.have_tls)
17274 /* If this is not defined, we have no way to emit the
17275 data. */
17276 if (!targetm.asm_out.output_dwarf_dtprel)
17277 return 0;
17279 /* The way DW_OP_GNU_push_tls_address is specified, we
17280 can only look up addresses of objects in the current
17281 module. We used DW_OP_addr as first op, but that's
17282 wrong, because DW_OP_addr is relocated by the debug
17283 info consumer, while DW_OP_GNU_push_tls_address
17284 operand shouldn't be. */
17285 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
17286 return 0;
17287 dtprel = dtprel_true;
17288 /* We check for DWARF 5 here because gdb did not implement
17289 DW_OP_form_tls_address until after 7.12. */
17290 tls_op = (dwarf_version >= 5 ? DW_OP_form_tls_address
17291 : DW_OP_GNU_push_tls_address);
17293 else
17295 if (!targetm.emutls.debug_form_tls_address
17296 || !(dwarf_version >= 3 || !dwarf_strict))
17297 return 0;
17298 /* We stuffed the control variable into the DECL_VALUE_EXPR
17299 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
17300 no longer appear in gimple code. We used the control
17301 variable in specific so that we could pick it up here. */
17302 loc = DECL_VALUE_EXPR (loc);
17303 tls_op = DW_OP_form_tls_address;
17306 rtl = rtl_for_decl_location (loc);
17307 if (rtl == NULL_RTX)
17308 return 0;
17310 if (!MEM_P (rtl))
17311 return 0;
17312 rtl = XEXP (rtl, 0);
17313 if (! CONSTANT_P (rtl))
17314 return 0;
17316 ret = new_addr_loc_descr (rtl, dtprel);
17317 ret1 = new_loc_descr (tls_op, 0, 0);
17318 add_loc_descr (&ret, ret1);
17320 have_address = 1;
17321 break;
17323 /* FALLTHRU */
17325 case PARM_DECL:
17326 if (context != NULL && context->dpi != NULL
17327 && DECL_CONTEXT (loc) == context->dpi->fndecl)
17329 /* We are generating code for a DWARF procedure and we want to access
17330 one of its arguments: find the appropriate argument offset and let
17331 the resolve_args_picking pass compute the offset that complies
17332 with the stack frame size. */
17333 unsigned i = 0;
17334 tree cursor;
17336 for (cursor = DECL_ARGUMENTS (context->dpi->fndecl);
17337 cursor != NULL_TREE && cursor != loc;
17338 cursor = TREE_CHAIN (cursor), ++i)
17340 /* If we are translating a DWARF procedure, all referenced parameters
17341 must belong to the current function. */
17342 gcc_assert (cursor != NULL_TREE);
17344 ret = new_loc_descr (DW_OP_pick, i, 0);
17345 ret->frame_offset_rel = 1;
17346 break;
17348 /* FALLTHRU */
17350 case RESULT_DECL:
17351 if (DECL_HAS_VALUE_EXPR_P (loc))
17352 return loc_list_from_tree_1 (DECL_VALUE_EXPR (loc),
17353 want_address, context);
17354 /* FALLTHRU */
17356 case FUNCTION_DECL:
17358 rtx rtl;
17359 var_loc_list *loc_list = lookup_decl_loc (loc);
17361 if (loc_list && loc_list->first)
17363 list_ret = dw_loc_list (loc_list, loc, want_address);
17364 have_address = want_address != 0;
17365 break;
17367 rtl = rtl_for_decl_location (loc);
17368 if (rtl == NULL_RTX)
17370 if (TREE_CODE (loc) != FUNCTION_DECL
17371 && early_dwarf
17372 && current_function_decl
17373 && want_address != 1
17374 && (INTEGRAL_TYPE_P (TREE_TYPE (loc))
17375 || POINTER_TYPE_P (TREE_TYPE (loc)))
17376 && DECL_CONTEXT (loc) == current_function_decl
17377 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (loc)))
17378 <= DWARF2_ADDR_SIZE))
17380 dw_die_ref ref = lookup_decl_die (loc);
17381 ret = new_loc_descr (DW_OP_GNU_variable_value, 0, 0);
17382 if (ref)
17384 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
17385 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
17386 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
17388 else
17390 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
17391 ret->dw_loc_oprnd1.v.val_decl_ref = loc;
17393 break;
17395 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
17396 return 0;
17398 else if (CONST_INT_P (rtl))
17400 HOST_WIDE_INT val = INTVAL (rtl);
17401 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
17402 val &= GET_MODE_MASK (DECL_MODE (loc));
17403 ret = int_loc_descriptor (val);
17405 else if (GET_CODE (rtl) == CONST_STRING)
17407 expansion_failed (loc, NULL_RTX, "CONST_STRING");
17408 return 0;
17410 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
17411 ret = new_addr_loc_descr (rtl, dtprel_false);
17412 else
17414 machine_mode mode, mem_mode;
17416 /* Certain constructs can only be represented at top-level. */
17417 if (want_address == 2)
17419 ret = loc_descriptor (rtl, VOIDmode,
17420 VAR_INIT_STATUS_INITIALIZED);
17421 have_address = 1;
17423 else
17425 mode = GET_MODE (rtl);
17426 mem_mode = VOIDmode;
17427 if (MEM_P (rtl))
17429 mem_mode = mode;
17430 mode = get_address_mode (rtl);
17431 rtl = XEXP (rtl, 0);
17432 have_address = 1;
17434 ret = mem_loc_descriptor (rtl, mode, mem_mode,
17435 VAR_INIT_STATUS_INITIALIZED);
17437 if (!ret)
17438 expansion_failed (loc, rtl,
17439 "failed to produce loc descriptor for rtl");
17442 break;
17444 case MEM_REF:
17445 if (!integer_zerop (TREE_OPERAND (loc, 1)))
17447 have_address = 1;
17448 goto do_plus;
17450 /* Fallthru. */
17451 case INDIRECT_REF:
17452 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17453 have_address = 1;
17454 break;
17456 case TARGET_MEM_REF:
17457 case SSA_NAME:
17458 case DEBUG_EXPR_DECL:
17459 return NULL;
17461 case COMPOUND_EXPR:
17462 return loc_list_from_tree_1 (TREE_OPERAND (loc, 1), want_address,
17463 context);
17465 CASE_CONVERT:
17466 case VIEW_CONVERT_EXPR:
17467 case SAVE_EXPR:
17468 case MODIFY_EXPR:
17469 case NON_LVALUE_EXPR:
17470 return loc_list_from_tree_1 (TREE_OPERAND (loc, 0), want_address,
17471 context);
17473 case COMPONENT_REF:
17474 case BIT_FIELD_REF:
17475 case ARRAY_REF:
17476 case ARRAY_RANGE_REF:
17477 case REALPART_EXPR:
17478 case IMAGPART_EXPR:
17480 tree obj, offset;
17481 HOST_WIDE_INT bitsize, bitpos, bytepos;
17482 machine_mode mode;
17483 int unsignedp, reversep, volatilep = 0;
17485 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
17486 &unsignedp, &reversep, &volatilep);
17488 gcc_assert (obj != loc);
17490 list_ret = loc_list_from_tree_1 (obj,
17491 want_address == 2
17492 && !bitpos && !offset ? 2 : 1,
17493 context);
17494 /* TODO: We can extract value of the small expression via shifting even
17495 for nonzero bitpos. */
17496 if (list_ret == 0)
17497 return 0;
17498 if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
17500 expansion_failed (loc, NULL_RTX,
17501 "bitfield access");
17502 return 0;
17505 if (offset != NULL_TREE)
17507 /* Variable offset. */
17508 list_ret1 = loc_list_from_tree_1 (offset, 0, context);
17509 if (list_ret1 == 0)
17510 return 0;
17511 add_loc_list (&list_ret, list_ret1);
17512 if (!list_ret)
17513 return 0;
17514 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
17517 bytepos = bitpos / BITS_PER_UNIT;
17518 if (bytepos > 0)
17519 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
17520 else if (bytepos < 0)
17521 loc_list_plus_const (list_ret, bytepos);
17523 have_address = 1;
17524 break;
17527 case INTEGER_CST:
17528 if ((want_address || !tree_fits_shwi_p (loc))
17529 && (ret = cst_pool_loc_descr (loc)))
17530 have_address = 1;
17531 else if (want_address == 2
17532 && tree_fits_shwi_p (loc)
17533 && (ret = address_of_int_loc_descriptor
17534 (int_size_in_bytes (TREE_TYPE (loc)),
17535 tree_to_shwi (loc))))
17536 have_address = 1;
17537 else if (tree_fits_shwi_p (loc))
17538 ret = int_loc_descriptor (tree_to_shwi (loc));
17539 else if (tree_fits_uhwi_p (loc))
17540 ret = uint_loc_descriptor (tree_to_uhwi (loc));
17541 else
17543 expansion_failed (loc, NULL_RTX,
17544 "Integer operand is not host integer");
17545 return 0;
17547 break;
17549 case CONSTRUCTOR:
17550 case REAL_CST:
17551 case STRING_CST:
17552 case COMPLEX_CST:
17553 if ((ret = cst_pool_loc_descr (loc)))
17554 have_address = 1;
17555 else if (TREE_CODE (loc) == CONSTRUCTOR)
17557 tree type = TREE_TYPE (loc);
17558 unsigned HOST_WIDE_INT size = int_size_in_bytes (type);
17559 unsigned HOST_WIDE_INT offset = 0;
17560 unsigned HOST_WIDE_INT cnt;
17561 constructor_elt *ce;
17563 if (TREE_CODE (type) == RECORD_TYPE)
17565 /* This is very limited, but it's enough to output
17566 pointers to member functions, as long as the
17567 referenced function is defined in the current
17568 translation unit. */
17569 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (loc), cnt, ce)
17571 tree val = ce->value;
17573 tree field = ce->index;
17575 if (val)
17576 STRIP_NOPS (val);
17578 if (!field || DECL_BIT_FIELD (field))
17580 expansion_failed (loc, NULL_RTX,
17581 "bitfield in record type constructor");
17582 size = offset = (unsigned HOST_WIDE_INT)-1;
17583 ret = NULL;
17584 break;
17587 HOST_WIDE_INT fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
17588 unsigned HOST_WIDE_INT pos = int_byte_position (field);
17589 gcc_assert (pos + fieldsize <= size);
17590 if (pos < offset)
17592 expansion_failed (loc, NULL_RTX,
17593 "out-of-order fields in record constructor");
17594 size = offset = (unsigned HOST_WIDE_INT)-1;
17595 ret = NULL;
17596 break;
17598 if (pos > offset)
17600 ret1 = new_loc_descr (DW_OP_piece, pos - offset, 0);
17601 add_loc_descr (&ret, ret1);
17602 offset = pos;
17604 if (val && fieldsize != 0)
17606 ret1 = loc_descriptor_from_tree (val, want_address, context);
17607 if (!ret1)
17609 expansion_failed (loc, NULL_RTX,
17610 "unsupported expression in field");
17611 size = offset = (unsigned HOST_WIDE_INT)-1;
17612 ret = NULL;
17613 break;
17615 add_loc_descr (&ret, ret1);
17617 if (fieldsize)
17619 ret1 = new_loc_descr (DW_OP_piece, fieldsize, 0);
17620 add_loc_descr (&ret, ret1);
17621 offset = pos + fieldsize;
17625 if (offset != size)
17627 ret1 = new_loc_descr (DW_OP_piece, size - offset, 0);
17628 add_loc_descr (&ret, ret1);
17629 offset = size;
17632 have_address = !!want_address;
17634 else
17635 expansion_failed (loc, NULL_RTX,
17636 "constructor of non-record type");
17638 else
17639 /* We can construct small constants here using int_loc_descriptor. */
17640 expansion_failed (loc, NULL_RTX,
17641 "constructor or constant not in constant pool");
17642 break;
17644 case TRUTH_AND_EXPR:
17645 case TRUTH_ANDIF_EXPR:
17646 case BIT_AND_EXPR:
17647 op = DW_OP_and;
17648 goto do_binop;
17650 case TRUTH_XOR_EXPR:
17651 case BIT_XOR_EXPR:
17652 op = DW_OP_xor;
17653 goto do_binop;
17655 case TRUTH_OR_EXPR:
17656 case TRUTH_ORIF_EXPR:
17657 case BIT_IOR_EXPR:
17658 op = DW_OP_or;
17659 goto do_binop;
17661 case FLOOR_DIV_EXPR:
17662 case CEIL_DIV_EXPR:
17663 case ROUND_DIV_EXPR:
17664 case TRUNC_DIV_EXPR:
17665 case EXACT_DIV_EXPR:
17666 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
17667 return 0;
17668 op = DW_OP_div;
17669 goto do_binop;
17671 case MINUS_EXPR:
17672 op = DW_OP_minus;
17673 goto do_binop;
17675 case FLOOR_MOD_EXPR:
17676 case CEIL_MOD_EXPR:
17677 case ROUND_MOD_EXPR:
17678 case TRUNC_MOD_EXPR:
17679 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
17681 op = DW_OP_mod;
17682 goto do_binop;
17684 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17685 list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
17686 if (list_ret == 0 || list_ret1 == 0)
17687 return 0;
17689 add_loc_list (&list_ret, list_ret1);
17690 if (list_ret == 0)
17691 return 0;
17692 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
17693 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
17694 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
17695 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
17696 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
17697 break;
17699 case MULT_EXPR:
17700 op = DW_OP_mul;
17701 goto do_binop;
17703 case LSHIFT_EXPR:
17704 op = DW_OP_shl;
17705 goto do_binop;
17707 case RSHIFT_EXPR:
17708 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
17709 goto do_binop;
17711 case POINTER_PLUS_EXPR:
17712 case PLUS_EXPR:
17713 do_plus:
17714 if (tree_fits_shwi_p (TREE_OPERAND (loc, 1)))
17716 /* Big unsigned numbers can fit in HOST_WIDE_INT but it may be
17717 smarter to encode their opposite. The DW_OP_plus_uconst operation
17718 takes 1 + X bytes, X being the size of the ULEB128 addend. On the
17719 other hand, a "<push literal>; DW_OP_minus" pattern takes 1 + Y
17720 bytes, Y being the size of the operation that pushes the opposite
17721 of the addend. So let's choose the smallest representation. */
17722 const tree tree_addend = TREE_OPERAND (loc, 1);
17723 offset_int wi_addend;
17724 HOST_WIDE_INT shwi_addend;
17725 dw_loc_descr_ref loc_naddend;
17727 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17728 if (list_ret == 0)
17729 return 0;
17731 /* Try to get the literal to push. It is the opposite of the addend,
17732 so as we rely on wrapping during DWARF evaluation, first decode
17733 the literal as a "DWARF-sized" signed number. */
17734 wi_addend = wi::to_offset (tree_addend);
17735 wi_addend = wi::sext (wi_addend, DWARF2_ADDR_SIZE * 8);
17736 shwi_addend = wi_addend.to_shwi ();
17737 loc_naddend = (shwi_addend != INTTYPE_MINIMUM (HOST_WIDE_INT))
17738 ? int_loc_descriptor (-shwi_addend)
17739 : NULL;
17741 if (loc_naddend != NULL
17742 && ((unsigned) size_of_uleb128 (shwi_addend)
17743 > size_of_loc_descr (loc_naddend)))
17745 add_loc_descr_to_each (list_ret, loc_naddend);
17746 add_loc_descr_to_each (list_ret,
17747 new_loc_descr (DW_OP_minus, 0, 0));
17749 else
17751 for (dw_loc_descr_ref loc_cur = loc_naddend; loc_cur != NULL; )
17753 loc_naddend = loc_cur;
17754 loc_cur = loc_cur->dw_loc_next;
17755 ggc_free (loc_naddend);
17757 loc_list_plus_const (list_ret, wi_addend.to_shwi ());
17759 break;
17762 op = DW_OP_plus;
17763 goto do_binop;
17765 case LE_EXPR:
17766 op = DW_OP_le;
17767 goto do_comp_binop;
17769 case GE_EXPR:
17770 op = DW_OP_ge;
17771 goto do_comp_binop;
17773 case LT_EXPR:
17774 op = DW_OP_lt;
17775 goto do_comp_binop;
17777 case GT_EXPR:
17778 op = DW_OP_gt;
17779 goto do_comp_binop;
17781 do_comp_binop:
17782 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
17784 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
17785 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
17786 list_ret = loc_list_from_uint_comparison (list_ret, list_ret1,
17787 TREE_CODE (loc));
17788 break;
17790 else
17791 goto do_binop;
17793 case EQ_EXPR:
17794 op = DW_OP_eq;
17795 goto do_binop;
17797 case NE_EXPR:
17798 op = DW_OP_ne;
17799 goto do_binop;
17801 do_binop:
17802 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17803 list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
17804 if (list_ret == 0 || list_ret1 == 0)
17805 return 0;
17807 add_loc_list (&list_ret, list_ret1);
17808 if (list_ret == 0)
17809 return 0;
17810 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
17811 break;
17813 case TRUTH_NOT_EXPR:
17814 case BIT_NOT_EXPR:
17815 op = DW_OP_not;
17816 goto do_unop;
17818 case ABS_EXPR:
17819 op = DW_OP_abs;
17820 goto do_unop;
17822 case NEGATE_EXPR:
17823 op = DW_OP_neg;
17824 goto do_unop;
17826 do_unop:
17827 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17828 if (list_ret == 0)
17829 return 0;
17831 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
17832 break;
17834 case MIN_EXPR:
17835 case MAX_EXPR:
17837 const enum tree_code code =
17838 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
17840 loc = build3 (COND_EXPR, TREE_TYPE (loc),
17841 build2 (code, integer_type_node,
17842 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
17843 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
17846 /* fall through */
17848 case COND_EXPR:
17850 dw_loc_descr_ref lhs
17851 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0, context);
17852 dw_loc_list_ref rhs
17853 = loc_list_from_tree_1 (TREE_OPERAND (loc, 2), 0, context);
17854 dw_loc_descr_ref bra_node, jump_node, tmp;
17856 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17857 if (list_ret == 0 || lhs == 0 || rhs == 0)
17858 return 0;
17860 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
17861 add_loc_descr_to_each (list_ret, bra_node);
17863 add_loc_list (&list_ret, rhs);
17864 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
17865 add_loc_descr_to_each (list_ret, jump_node);
17867 add_loc_descr_to_each (list_ret, lhs);
17868 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
17869 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
17871 /* ??? Need a node to point the skip at. Use a nop. */
17872 tmp = new_loc_descr (DW_OP_nop, 0, 0);
17873 add_loc_descr_to_each (list_ret, tmp);
17874 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
17875 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
17877 break;
17879 case FIX_TRUNC_EXPR:
17880 return 0;
17882 default:
17883 /* Leave front-end specific codes as simply unknown. This comes
17884 up, for instance, with the C STMT_EXPR. */
17885 if ((unsigned int) TREE_CODE (loc)
17886 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
17888 expansion_failed (loc, NULL_RTX,
17889 "language specific tree node");
17890 return 0;
17893 /* Otherwise this is a generic code; we should just lists all of
17894 these explicitly. We forgot one. */
17895 if (flag_checking)
17896 gcc_unreachable ();
17898 /* In a release build, we want to degrade gracefully: better to
17899 generate incomplete debugging information than to crash. */
17900 return NULL;
17903 if (!ret && !list_ret)
17904 return 0;
17906 if (want_address == 2 && !have_address
17907 && (dwarf_version >= 4 || !dwarf_strict))
17909 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
17911 expansion_failed (loc, NULL_RTX,
17912 "DWARF address size mismatch");
17913 return 0;
17915 if (ret)
17916 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
17917 else
17918 add_loc_descr_to_each (list_ret,
17919 new_loc_descr (DW_OP_stack_value, 0, 0));
17920 have_address = 1;
17922 /* Show if we can't fill the request for an address. */
17923 if (want_address && !have_address)
17925 expansion_failed (loc, NULL_RTX,
17926 "Want address and only have value");
17927 return 0;
17930 gcc_assert (!ret || !list_ret);
17932 /* If we've got an address and don't want one, dereference. */
17933 if (!want_address && have_address)
17935 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
17937 if (size > DWARF2_ADDR_SIZE || size == -1)
17939 expansion_failed (loc, NULL_RTX,
17940 "DWARF address size mismatch");
17941 return 0;
17943 else if (size == DWARF2_ADDR_SIZE)
17944 op = DW_OP_deref;
17945 else
17946 op = DW_OP_deref_size;
17948 if (ret)
17949 add_loc_descr (&ret, new_loc_descr (op, size, 0));
17950 else
17951 add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
17953 if (ret)
17954 list_ret = new_loc_list (ret, NULL, NULL, NULL);
17956 return list_ret;
17959 /* Likewise, but strip useless DW_OP_nop operations in the resulting
17960 expressions. */
17962 static dw_loc_list_ref
17963 loc_list_from_tree (tree loc, int want_address,
17964 struct loc_descr_context *context)
17966 dw_loc_list_ref result = loc_list_from_tree_1 (loc, want_address, context);
17968 for (dw_loc_list_ref loc_cur = result;
17969 loc_cur != NULL; loc_cur = loc_cur->dw_loc_next)
17970 loc_descr_without_nops (loc_cur->expr);
17971 return result;
17974 /* Same as above but return only single location expression. */
17975 static dw_loc_descr_ref
17976 loc_descriptor_from_tree (tree loc, int want_address,
17977 struct loc_descr_context *context)
17979 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address, context);
17980 if (!ret)
17981 return NULL;
17982 if (ret->dw_loc_next)
17984 expansion_failed (loc, NULL_RTX,
17985 "Location list where only loc descriptor needed");
17986 return NULL;
17988 return ret->expr;
17991 /* Given a value, round it up to the lowest multiple of `boundary'
17992 which is not less than the value itself. */
17994 static inline HOST_WIDE_INT
17995 ceiling (HOST_WIDE_INT value, unsigned int boundary)
17997 return (((value + boundary - 1) / boundary) * boundary);
18000 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
18001 pointer to the declared type for the relevant field variable, or return
18002 `integer_type_node' if the given node turns out to be an
18003 ERROR_MARK node. */
18005 static inline tree
18006 field_type (const_tree decl)
18008 tree type;
18010 if (TREE_CODE (decl) == ERROR_MARK)
18011 return integer_type_node;
18013 type = DECL_BIT_FIELD_TYPE (decl);
18014 if (type == NULL_TREE)
18015 type = TREE_TYPE (decl);
18017 return type;
18020 /* Given a pointer to a tree node, return the alignment in bits for
18021 it, or else return BITS_PER_WORD if the node actually turns out to
18022 be an ERROR_MARK node. */
18024 static inline unsigned
18025 simple_type_align_in_bits (const_tree type)
18027 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
18030 static inline unsigned
18031 simple_decl_align_in_bits (const_tree decl)
18033 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
18036 /* Return the result of rounding T up to ALIGN. */
18038 static inline offset_int
18039 round_up_to_align (const offset_int &t, unsigned int align)
18041 return wi::udiv_trunc (t + align - 1, align) * align;
18044 /* Compute the size of TYPE in bytes. If possible, return NULL and store the
18045 size as an integer constant in CST_SIZE. Otherwise, if possible, return a
18046 DWARF expression that computes the size. Return NULL and set CST_SIZE to -1
18047 if we fail to return the size in one of these two forms. */
18049 static dw_loc_descr_ref
18050 type_byte_size (const_tree type, HOST_WIDE_INT *cst_size)
18052 tree tree_size;
18053 struct loc_descr_context ctx;
18055 /* Return a constant integer in priority, if possible. */
18056 *cst_size = int_size_in_bytes (type);
18057 if (*cst_size != -1)
18058 return NULL;
18060 ctx.context_type = const_cast<tree> (type);
18061 ctx.base_decl = NULL_TREE;
18062 ctx.dpi = NULL;
18063 ctx.placeholder_arg = false;
18064 ctx.placeholder_seen = false;
18066 type = TYPE_MAIN_VARIANT (type);
18067 tree_size = TYPE_SIZE_UNIT (type);
18068 return ((tree_size != NULL_TREE)
18069 ? loc_descriptor_from_tree (tree_size, 0, &ctx)
18070 : NULL);
18073 /* Helper structure for RECORD_TYPE processing. */
18074 struct vlr_context
18076 /* Root RECORD_TYPE. It is needed to generate data member location
18077 descriptions in variable-length records (VLR), but also to cope with
18078 variants, which are composed of nested structures multiplexed with
18079 QUAL_UNION_TYPE nodes. Each time such a structure is passed to a
18080 function processing a FIELD_DECL, it is required to be non null. */
18081 tree struct_type;
18082 /* When generating a variant part in a RECORD_TYPE (i.e. a nested
18083 QUAL_UNION_TYPE), this holds an expression that computes the offset for
18084 this variant part as part of the root record (in storage units). For
18085 regular records, it must be NULL_TREE. */
18086 tree variant_part_offset;
18089 /* Given a pointer to a FIELD_DECL, compute the byte offset of the lowest
18090 addressed byte of the "containing object" for the given FIELD_DECL. If
18091 possible, return a native constant through CST_OFFSET (in which case NULL is
18092 returned); otherwise return a DWARF expression that computes the offset.
18094 Set *CST_OFFSET to 0 and return NULL if we are unable to determine what
18095 that offset is, either because the argument turns out to be a pointer to an
18096 ERROR_MARK node, or because the offset expression is too complex for us.
18098 CTX is required: see the comment for VLR_CONTEXT. */
18100 static dw_loc_descr_ref
18101 field_byte_offset (const_tree decl, struct vlr_context *ctx,
18102 HOST_WIDE_INT *cst_offset)
18104 tree tree_result;
18105 dw_loc_list_ref loc_result;
18107 *cst_offset = 0;
18109 if (TREE_CODE (decl) == ERROR_MARK)
18110 return NULL;
18111 else
18112 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
18114 /* We cannot handle variable bit offsets at the moment, so abort if it's the
18115 case. */
18116 if (TREE_CODE (DECL_FIELD_BIT_OFFSET (decl)) != INTEGER_CST)
18117 return NULL;
18119 #ifdef PCC_BITFIELD_TYPE_MATTERS
18120 /* We used to handle only constant offsets in all cases. Now, we handle
18121 properly dynamic byte offsets only when PCC bitfield type doesn't
18122 matter. */
18123 if (PCC_BITFIELD_TYPE_MATTERS
18124 && TREE_CODE (DECL_FIELD_OFFSET (decl)) == INTEGER_CST)
18126 offset_int object_offset_in_bits;
18127 offset_int object_offset_in_bytes;
18128 offset_int bitpos_int;
18129 tree type;
18130 tree field_size_tree;
18131 offset_int deepest_bitpos;
18132 offset_int field_size_in_bits;
18133 unsigned int type_align_in_bits;
18134 unsigned int decl_align_in_bits;
18135 offset_int type_size_in_bits;
18137 bitpos_int = wi::to_offset (bit_position (decl));
18138 type = field_type (decl);
18139 type_size_in_bits = offset_int_type_size_in_bits (type);
18140 type_align_in_bits = simple_type_align_in_bits (type);
18142 field_size_tree = DECL_SIZE (decl);
18144 /* The size could be unspecified if there was an error, or for
18145 a flexible array member. */
18146 if (!field_size_tree)
18147 field_size_tree = bitsize_zero_node;
18149 /* If the size of the field is not constant, use the type size. */
18150 if (TREE_CODE (field_size_tree) == INTEGER_CST)
18151 field_size_in_bits = wi::to_offset (field_size_tree);
18152 else
18153 field_size_in_bits = type_size_in_bits;
18155 decl_align_in_bits = simple_decl_align_in_bits (decl);
18157 /* The GCC front-end doesn't make any attempt to keep track of the
18158 starting bit offset (relative to the start of the containing
18159 structure type) of the hypothetical "containing object" for a
18160 bit-field. Thus, when computing the byte offset value for the
18161 start of the "containing object" of a bit-field, we must deduce
18162 this information on our own. This can be rather tricky to do in
18163 some cases. For example, handling the following structure type
18164 definition when compiling for an i386/i486 target (which only
18165 aligns long long's to 32-bit boundaries) can be very tricky:
18167 struct S { int field1; long long field2:31; };
18169 Fortunately, there is a simple rule-of-thumb which can be used
18170 in such cases. When compiling for an i386/i486, GCC will
18171 allocate 8 bytes for the structure shown above. It decides to
18172 do this based upon one simple rule for bit-field allocation.
18173 GCC allocates each "containing object" for each bit-field at
18174 the first (i.e. lowest addressed) legitimate alignment boundary
18175 (based upon the required minimum alignment for the declared
18176 type of the field) which it can possibly use, subject to the
18177 condition that there is still enough available space remaining
18178 in the containing object (when allocated at the selected point)
18179 to fully accommodate all of the bits of the bit-field itself.
18181 This simple rule makes it obvious why GCC allocates 8 bytes for
18182 each object of the structure type shown above. When looking
18183 for a place to allocate the "containing object" for `field2',
18184 the compiler simply tries to allocate a 64-bit "containing
18185 object" at each successive 32-bit boundary (starting at zero)
18186 until it finds a place to allocate that 64- bit field such that
18187 at least 31 contiguous (and previously unallocated) bits remain
18188 within that selected 64 bit field. (As it turns out, for the
18189 example above, the compiler finds it is OK to allocate the
18190 "containing object" 64-bit field at bit-offset zero within the
18191 structure type.)
18193 Here we attempt to work backwards from the limited set of facts
18194 we're given, and we try to deduce from those facts, where GCC
18195 must have believed that the containing object started (within
18196 the structure type). The value we deduce is then used (by the
18197 callers of this routine) to generate DW_AT_location and
18198 DW_AT_bit_offset attributes for fields (both bit-fields and, in
18199 the case of DW_AT_location, regular fields as well). */
18201 /* Figure out the bit-distance from the start of the structure to
18202 the "deepest" bit of the bit-field. */
18203 deepest_bitpos = bitpos_int + field_size_in_bits;
18205 /* This is the tricky part. Use some fancy footwork to deduce
18206 where the lowest addressed bit of the containing object must
18207 be. */
18208 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
18210 /* Round up to type_align by default. This works best for
18211 bitfields. */
18212 object_offset_in_bits
18213 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
18215 if (wi::gtu_p (object_offset_in_bits, bitpos_int))
18217 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
18219 /* Round up to decl_align instead. */
18220 object_offset_in_bits
18221 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
18224 object_offset_in_bytes
18225 = wi::lrshift (object_offset_in_bits, LOG2_BITS_PER_UNIT);
18226 if (ctx->variant_part_offset == NULL_TREE)
18228 *cst_offset = object_offset_in_bytes.to_shwi ();
18229 return NULL;
18231 tree_result = wide_int_to_tree (sizetype, object_offset_in_bytes);
18233 else
18234 #endif /* PCC_BITFIELD_TYPE_MATTERS */
18235 tree_result = byte_position (decl);
18237 if (ctx->variant_part_offset != NULL_TREE)
18238 tree_result = fold_build2 (PLUS_EXPR, TREE_TYPE (tree_result),
18239 ctx->variant_part_offset, tree_result);
18241 /* If the byte offset is a constant, it's simplier to handle a native
18242 constant rather than a DWARF expression. */
18243 if (TREE_CODE (tree_result) == INTEGER_CST)
18245 *cst_offset = wi::to_offset (tree_result).to_shwi ();
18246 return NULL;
18248 struct loc_descr_context loc_ctx = {
18249 ctx->struct_type, /* context_type */
18250 NULL_TREE, /* base_decl */
18251 NULL, /* dpi */
18252 false, /* placeholder_arg */
18253 false /* placeholder_seen */
18255 loc_result = loc_list_from_tree (tree_result, 0, &loc_ctx);
18257 /* We want a DWARF expression: abort if we only have a location list with
18258 multiple elements. */
18259 if (!loc_result || !single_element_loc_list_p (loc_result))
18260 return NULL;
18261 else
18262 return loc_result->expr;
18265 /* The following routines define various Dwarf attributes and any data
18266 associated with them. */
18268 /* Add a location description attribute value to a DIE.
18270 This emits location attributes suitable for whole variables and
18271 whole parameters. Note that the location attributes for struct fields are
18272 generated by the routine `data_member_location_attribute' below. */
18274 static inline void
18275 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
18276 dw_loc_list_ref descr)
18278 if (descr == 0)
18279 return;
18280 if (single_element_loc_list_p (descr))
18281 add_AT_loc (die, attr_kind, descr->expr);
18282 else
18283 add_AT_loc_list (die, attr_kind, descr);
18286 /* Add DW_AT_accessibility attribute to DIE if needed. */
18288 static void
18289 add_accessibility_attribute (dw_die_ref die, tree decl)
18291 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
18292 children, otherwise the default is DW_ACCESS_public. In DWARF2
18293 the default has always been DW_ACCESS_public. */
18294 if (TREE_PROTECTED (decl))
18295 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
18296 else if (TREE_PRIVATE (decl))
18298 if (dwarf_version == 2
18299 || die->die_parent == NULL
18300 || die->die_parent->die_tag != DW_TAG_class_type)
18301 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
18303 else if (dwarf_version > 2
18304 && die->die_parent
18305 && die->die_parent->die_tag == DW_TAG_class_type)
18306 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
18309 /* Attach the specialized form of location attribute used for data members of
18310 struct and union types. In the special case of a FIELD_DECL node which
18311 represents a bit-field, the "offset" part of this special location
18312 descriptor must indicate the distance in bytes from the lowest-addressed
18313 byte of the containing struct or union type to the lowest-addressed byte of
18314 the "containing object" for the bit-field. (See the `field_byte_offset'
18315 function above).
18317 For any given bit-field, the "containing object" is a hypothetical object
18318 (of some integral or enum type) within which the given bit-field lives. The
18319 type of this hypothetical "containing object" is always the same as the
18320 declared type of the individual bit-field itself (for GCC anyway... the
18321 DWARF spec doesn't actually mandate this). Note that it is the size (in
18322 bytes) of the hypothetical "containing object" which will be given in the
18323 DW_AT_byte_size attribute for this bit-field. (See the
18324 `byte_size_attribute' function below.) It is also used when calculating the
18325 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
18326 function below.)
18328 CTX is required: see the comment for VLR_CONTEXT. */
18330 static void
18331 add_data_member_location_attribute (dw_die_ref die,
18332 tree decl,
18333 struct vlr_context *ctx)
18335 HOST_WIDE_INT offset;
18336 dw_loc_descr_ref loc_descr = 0;
18338 if (TREE_CODE (decl) == TREE_BINFO)
18340 /* We're working on the TAG_inheritance for a base class. */
18341 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
18343 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
18344 aren't at a fixed offset from all (sub)objects of the same
18345 type. We need to extract the appropriate offset from our
18346 vtable. The following dwarf expression means
18348 BaseAddr = ObAddr + *((*ObAddr) - Offset)
18350 This is specific to the V3 ABI, of course. */
18352 dw_loc_descr_ref tmp;
18354 /* Make a copy of the object address. */
18355 tmp = new_loc_descr (DW_OP_dup, 0, 0);
18356 add_loc_descr (&loc_descr, tmp);
18358 /* Extract the vtable address. */
18359 tmp = new_loc_descr (DW_OP_deref, 0, 0);
18360 add_loc_descr (&loc_descr, tmp);
18362 /* Calculate the address of the offset. */
18363 offset = tree_to_shwi (BINFO_VPTR_FIELD (decl));
18364 gcc_assert (offset < 0);
18366 tmp = int_loc_descriptor (-offset);
18367 add_loc_descr (&loc_descr, tmp);
18368 tmp = new_loc_descr (DW_OP_minus, 0, 0);
18369 add_loc_descr (&loc_descr, tmp);
18371 /* Extract the offset. */
18372 tmp = new_loc_descr (DW_OP_deref, 0, 0);
18373 add_loc_descr (&loc_descr, tmp);
18375 /* Add it to the object address. */
18376 tmp = new_loc_descr (DW_OP_plus, 0, 0);
18377 add_loc_descr (&loc_descr, tmp);
18379 else
18380 offset = tree_to_shwi (BINFO_OFFSET (decl));
18382 else
18384 loc_descr = field_byte_offset (decl, ctx, &offset);
18386 /* If loc_descr is available then we know the field offset is dynamic.
18387 However, GDB does not handle dynamic field offsets very well at the
18388 moment. */
18389 if (loc_descr != NULL && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL)
18391 loc_descr = NULL;
18392 offset = 0;
18395 /* Data member location evalutation starts with the base address on the
18396 stack. Compute the field offset and add it to this base address. */
18397 else if (loc_descr != NULL)
18398 add_loc_descr (&loc_descr, new_loc_descr (DW_OP_plus, 0, 0));
18401 if (! loc_descr)
18403 /* While DW_AT_data_bit_offset has been added already in DWARF4,
18404 e.g. GDB only added support to it in November 2016. For DWARF5
18405 we need newer debug info consumers anyway. We might change this
18406 to dwarf_version >= 4 once most consumers catched up. */
18407 if (dwarf_version >= 5
18408 && TREE_CODE (decl) == FIELD_DECL
18409 && DECL_BIT_FIELD_TYPE (decl))
18411 tree off = bit_position (decl);
18412 if (tree_fits_uhwi_p (off) && get_AT (die, DW_AT_bit_size))
18414 remove_AT (die, DW_AT_byte_size);
18415 remove_AT (die, DW_AT_bit_offset);
18416 add_AT_unsigned (die, DW_AT_data_bit_offset, tree_to_uhwi (off));
18417 return;
18420 if (dwarf_version > 2)
18422 /* Don't need to output a location expression, just the constant. */
18423 if (offset < 0)
18424 add_AT_int (die, DW_AT_data_member_location, offset);
18425 else
18426 add_AT_unsigned (die, DW_AT_data_member_location, offset);
18427 return;
18429 else
18431 enum dwarf_location_atom op;
18433 /* The DWARF2 standard says that we should assume that the structure
18434 address is already on the stack, so we can specify a structure
18435 field address by using DW_OP_plus_uconst. */
18436 op = DW_OP_plus_uconst;
18437 loc_descr = new_loc_descr (op, offset, 0);
18441 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
18444 /* Writes integer values to dw_vec_const array. */
18446 static void
18447 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
18449 while (size != 0)
18451 *dest++ = val & 0xff;
18452 val >>= 8;
18453 --size;
18457 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
18459 static HOST_WIDE_INT
18460 extract_int (const unsigned char *src, unsigned int size)
18462 HOST_WIDE_INT val = 0;
18464 src += size;
18465 while (size != 0)
18467 val <<= 8;
18468 val |= *--src & 0xff;
18469 --size;
18471 return val;
18474 /* Writes wide_int values to dw_vec_const array. */
18476 static void
18477 insert_wide_int (const wide_int &val, unsigned char *dest, int elt_size)
18479 int i;
18481 if (elt_size <= HOST_BITS_PER_WIDE_INT/BITS_PER_UNIT)
18483 insert_int ((HOST_WIDE_INT) val.elt (0), elt_size, dest);
18484 return;
18487 /* We'd have to extend this code to support odd sizes. */
18488 gcc_assert (elt_size % (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT) == 0);
18490 int n = elt_size / (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
18492 if (WORDS_BIG_ENDIAN)
18493 for (i = n - 1; i >= 0; i--)
18495 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
18496 dest += sizeof (HOST_WIDE_INT);
18498 else
18499 for (i = 0; i < n; i++)
18501 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
18502 dest += sizeof (HOST_WIDE_INT);
18506 /* Writes floating point values to dw_vec_const array. */
18508 static void
18509 insert_float (const_rtx rtl, unsigned char *array)
18511 long val[4];
18512 int i;
18514 real_to_target (val, CONST_DOUBLE_REAL_VALUE (rtl), GET_MODE (rtl));
18516 /* real_to_target puts 32-bit pieces in each long. Pack them. */
18517 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
18519 insert_int (val[i], 4, array);
18520 array += 4;
18524 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
18525 does not have a "location" either in memory or in a register. These
18526 things can arise in GNU C when a constant is passed as an actual parameter
18527 to an inlined function. They can also arise in C++ where declared
18528 constants do not necessarily get memory "homes". */
18530 static bool
18531 add_const_value_attribute (dw_die_ref die, rtx rtl)
18533 switch (GET_CODE (rtl))
18535 case CONST_INT:
18537 HOST_WIDE_INT val = INTVAL (rtl);
18539 if (val < 0)
18540 add_AT_int (die, DW_AT_const_value, val);
18541 else
18542 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
18544 return true;
18546 case CONST_WIDE_INT:
18548 wide_int w1 = rtx_mode_t (rtl, MAX_MODE_INT);
18549 unsigned int prec = MIN (wi::min_precision (w1, UNSIGNED),
18550 (unsigned int)CONST_WIDE_INT_NUNITS (rtl) * HOST_BITS_PER_WIDE_INT);
18551 wide_int w = wi::zext (w1, prec);
18552 add_AT_wide (die, DW_AT_const_value, w);
18554 return true;
18556 case CONST_DOUBLE:
18557 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
18558 floating-point constant. A CONST_DOUBLE is used whenever the
18559 constant requires more than one word in order to be adequately
18560 represented. */
18562 machine_mode mode = GET_MODE (rtl);
18564 if (TARGET_SUPPORTS_WIDE_INT == 0 && !SCALAR_FLOAT_MODE_P (mode))
18565 add_AT_double (die, DW_AT_const_value,
18566 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
18567 else
18569 unsigned int length = GET_MODE_SIZE (mode);
18570 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
18572 insert_float (rtl, array);
18573 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
18576 return true;
18578 case CONST_VECTOR:
18580 machine_mode mode = GET_MODE (rtl);
18581 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
18582 unsigned int length = CONST_VECTOR_NUNITS (rtl);
18583 unsigned char *array
18584 = ggc_vec_alloc<unsigned char> (length * elt_size);
18585 unsigned int i;
18586 unsigned char *p;
18587 machine_mode imode = GET_MODE_INNER (mode);
18589 switch (GET_MODE_CLASS (mode))
18591 case MODE_VECTOR_INT:
18592 for (i = 0, p = array; i < length; i++, p += elt_size)
18594 rtx elt = CONST_VECTOR_ELT (rtl, i);
18595 insert_wide_int (rtx_mode_t (elt, imode), p, elt_size);
18597 break;
18599 case MODE_VECTOR_FLOAT:
18600 for (i = 0, p = array; i < length; i++, p += elt_size)
18602 rtx elt = CONST_VECTOR_ELT (rtl, i);
18603 insert_float (elt, p);
18605 break;
18607 default:
18608 gcc_unreachable ();
18611 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
18613 return true;
18615 case CONST_STRING:
18616 if (dwarf_version >= 4 || !dwarf_strict)
18618 dw_loc_descr_ref loc_result;
18619 resolve_one_addr (&rtl);
18620 rtl_addr:
18621 loc_result = new_addr_loc_descr (rtl, dtprel_false);
18622 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
18623 add_AT_loc (die, DW_AT_location, loc_result);
18624 vec_safe_push (used_rtx_array, rtl);
18625 return true;
18627 return false;
18629 case CONST:
18630 if (CONSTANT_P (XEXP (rtl, 0)))
18631 return add_const_value_attribute (die, XEXP (rtl, 0));
18632 /* FALLTHROUGH */
18633 case SYMBOL_REF:
18634 if (!const_ok_for_output (rtl))
18635 return false;
18636 /* FALLTHROUGH */
18637 case LABEL_REF:
18638 if (dwarf_version >= 4 || !dwarf_strict)
18639 goto rtl_addr;
18640 return false;
18642 case PLUS:
18643 /* In cases where an inlined instance of an inline function is passed
18644 the address of an `auto' variable (which is local to the caller) we
18645 can get a situation where the DECL_RTL of the artificial local
18646 variable (for the inlining) which acts as a stand-in for the
18647 corresponding formal parameter (of the inline function) will look
18648 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
18649 exactly a compile-time constant expression, but it isn't the address
18650 of the (artificial) local variable either. Rather, it represents the
18651 *value* which the artificial local variable always has during its
18652 lifetime. We currently have no way to represent such quasi-constant
18653 values in Dwarf, so for now we just punt and generate nothing. */
18654 return false;
18656 case HIGH:
18657 case CONST_FIXED:
18658 return false;
18660 case MEM:
18661 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
18662 && MEM_READONLY_P (rtl)
18663 && GET_MODE (rtl) == BLKmode)
18665 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
18666 return true;
18668 return false;
18670 default:
18671 /* No other kinds of rtx should be possible here. */
18672 gcc_unreachable ();
18674 return false;
18677 /* Determine whether the evaluation of EXPR references any variables
18678 or functions which aren't otherwise used (and therefore may not be
18679 output). */
18680 static tree
18681 reference_to_unused (tree * tp, int * walk_subtrees,
18682 void * data ATTRIBUTE_UNUSED)
18684 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
18685 *walk_subtrees = 0;
18687 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
18688 && ! TREE_ASM_WRITTEN (*tp))
18689 return *tp;
18690 /* ??? The C++ FE emits debug information for using decls, so
18691 putting gcc_unreachable here falls over. See PR31899. For now
18692 be conservative. */
18693 else if (!symtab->global_info_ready && VAR_OR_FUNCTION_DECL_P (*tp))
18694 return *tp;
18695 else if (VAR_P (*tp))
18697 varpool_node *node = varpool_node::get (*tp);
18698 if (!node || !node->definition)
18699 return *tp;
18701 else if (TREE_CODE (*tp) == FUNCTION_DECL
18702 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
18704 /* The call graph machinery must have finished analyzing,
18705 optimizing and gimplifying the CU by now.
18706 So if *TP has no call graph node associated
18707 to it, it means *TP will not be emitted. */
18708 if (!cgraph_node::get (*tp))
18709 return *tp;
18711 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
18712 return *tp;
18714 return NULL_TREE;
18717 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
18718 for use in a later add_const_value_attribute call. */
18720 static rtx
18721 rtl_for_decl_init (tree init, tree type)
18723 rtx rtl = NULL_RTX;
18725 STRIP_NOPS (init);
18727 /* If a variable is initialized with a string constant without embedded
18728 zeros, build CONST_STRING. */
18729 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
18731 tree enttype = TREE_TYPE (type);
18732 tree domain = TYPE_DOMAIN (type);
18733 machine_mode mode = TYPE_MODE (enttype);
18735 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
18736 && domain
18737 && integer_zerop (TYPE_MIN_VALUE (domain))
18738 && compare_tree_int (TYPE_MAX_VALUE (domain),
18739 TREE_STRING_LENGTH (init) - 1) == 0
18740 && ((size_t) TREE_STRING_LENGTH (init)
18741 == strlen (TREE_STRING_POINTER (init)) + 1))
18743 rtl = gen_rtx_CONST_STRING (VOIDmode,
18744 ggc_strdup (TREE_STRING_POINTER (init)));
18745 rtl = gen_rtx_MEM (BLKmode, rtl);
18746 MEM_READONLY_P (rtl) = 1;
18749 /* Other aggregates, and complex values, could be represented using
18750 CONCAT: FIXME! */
18751 else if (AGGREGATE_TYPE_P (type)
18752 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
18753 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
18754 || TREE_CODE (type) == COMPLEX_TYPE)
18756 /* Vectors only work if their mode is supported by the target.
18757 FIXME: generic vectors ought to work too. */
18758 else if (TREE_CODE (type) == VECTOR_TYPE
18759 && !VECTOR_MODE_P (TYPE_MODE (type)))
18761 /* If the initializer is something that we know will expand into an
18762 immediate RTL constant, expand it now. We must be careful not to
18763 reference variables which won't be output. */
18764 else if (initializer_constant_valid_p (init, type)
18765 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
18767 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
18768 possible. */
18769 if (TREE_CODE (type) == VECTOR_TYPE)
18770 switch (TREE_CODE (init))
18772 case VECTOR_CST:
18773 break;
18774 case CONSTRUCTOR:
18775 if (TREE_CONSTANT (init))
18777 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
18778 bool constant_p = true;
18779 tree value;
18780 unsigned HOST_WIDE_INT ix;
18782 /* Even when ctor is constant, it might contain non-*_CST
18783 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
18784 belong into VECTOR_CST nodes. */
18785 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
18786 if (!CONSTANT_CLASS_P (value))
18788 constant_p = false;
18789 break;
18792 if (constant_p)
18794 init = build_vector_from_ctor (type, elts);
18795 break;
18798 /* FALLTHRU */
18800 default:
18801 return NULL;
18804 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
18806 /* If expand_expr returns a MEM, it wasn't immediate. */
18807 gcc_assert (!rtl || !MEM_P (rtl));
18810 return rtl;
18813 /* Generate RTL for the variable DECL to represent its location. */
18815 static rtx
18816 rtl_for_decl_location (tree decl)
18818 rtx rtl;
18820 /* Here we have to decide where we are going to say the parameter "lives"
18821 (as far as the debugger is concerned). We only have a couple of
18822 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
18824 DECL_RTL normally indicates where the parameter lives during most of the
18825 activation of the function. If optimization is enabled however, this
18826 could be either NULL or else a pseudo-reg. Both of those cases indicate
18827 that the parameter doesn't really live anywhere (as far as the code
18828 generation parts of GCC are concerned) during most of the function's
18829 activation. That will happen (for example) if the parameter is never
18830 referenced within the function.
18832 We could just generate a location descriptor here for all non-NULL
18833 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
18834 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
18835 where DECL_RTL is NULL or is a pseudo-reg.
18837 Note however that we can only get away with using DECL_INCOMING_RTL as
18838 a backup substitute for DECL_RTL in certain limited cases. In cases
18839 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
18840 we can be sure that the parameter was passed using the same type as it is
18841 declared to have within the function, and that its DECL_INCOMING_RTL
18842 points us to a place where a value of that type is passed.
18844 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
18845 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
18846 because in these cases DECL_INCOMING_RTL points us to a value of some
18847 type which is *different* from the type of the parameter itself. Thus,
18848 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
18849 such cases, the debugger would end up (for example) trying to fetch a
18850 `float' from a place which actually contains the first part of a
18851 `double'. That would lead to really incorrect and confusing
18852 output at debug-time.
18854 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
18855 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
18856 are a couple of exceptions however. On little-endian machines we can
18857 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
18858 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
18859 an integral type that is smaller than TREE_TYPE (decl). These cases arise
18860 when (on a little-endian machine) a non-prototyped function has a
18861 parameter declared to be of type `short' or `char'. In such cases,
18862 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
18863 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
18864 passed `int' value. If the debugger then uses that address to fetch
18865 a `short' or a `char' (on a little-endian machine) the result will be
18866 the correct data, so we allow for such exceptional cases below.
18868 Note that our goal here is to describe the place where the given formal
18869 parameter lives during most of the function's activation (i.e. between the
18870 end of the prologue and the start of the epilogue). We'll do that as best
18871 as we can. Note however that if the given formal parameter is modified
18872 sometime during the execution of the function, then a stack backtrace (at
18873 debug-time) will show the function as having been called with the *new*
18874 value rather than the value which was originally passed in. This happens
18875 rarely enough that it is not a major problem, but it *is* a problem, and
18876 I'd like to fix it.
18878 A future version of dwarf2out.c may generate two additional attributes for
18879 any given DW_TAG_formal_parameter DIE which will describe the "passed
18880 type" and the "passed location" for the given formal parameter in addition
18881 to the attributes we now generate to indicate the "declared type" and the
18882 "active location" for each parameter. This additional set of attributes
18883 could be used by debuggers for stack backtraces. Separately, note that
18884 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
18885 This happens (for example) for inlined-instances of inline function formal
18886 parameters which are never referenced. This really shouldn't be
18887 happening. All PARM_DECL nodes should get valid non-NULL
18888 DECL_INCOMING_RTL values. FIXME. */
18890 /* Use DECL_RTL as the "location" unless we find something better. */
18891 rtl = DECL_RTL_IF_SET (decl);
18893 /* When generating abstract instances, ignore everything except
18894 constants, symbols living in memory, and symbols living in
18895 fixed registers. */
18896 if (! reload_completed)
18898 if (rtl
18899 && (CONSTANT_P (rtl)
18900 || (MEM_P (rtl)
18901 && CONSTANT_P (XEXP (rtl, 0)))
18902 || (REG_P (rtl)
18903 && VAR_P (decl)
18904 && TREE_STATIC (decl))))
18906 rtl = targetm.delegitimize_address (rtl);
18907 return rtl;
18909 rtl = NULL_RTX;
18911 else if (TREE_CODE (decl) == PARM_DECL)
18913 if (rtl == NULL_RTX
18914 || is_pseudo_reg (rtl)
18915 || (MEM_P (rtl)
18916 && is_pseudo_reg (XEXP (rtl, 0))
18917 && DECL_INCOMING_RTL (decl)
18918 && MEM_P (DECL_INCOMING_RTL (decl))
18919 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
18921 tree declared_type = TREE_TYPE (decl);
18922 tree passed_type = DECL_ARG_TYPE (decl);
18923 machine_mode dmode = TYPE_MODE (declared_type);
18924 machine_mode pmode = TYPE_MODE (passed_type);
18926 /* This decl represents a formal parameter which was optimized out.
18927 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
18928 all cases where (rtl == NULL_RTX) just below. */
18929 if (dmode == pmode)
18930 rtl = DECL_INCOMING_RTL (decl);
18931 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
18932 && SCALAR_INT_MODE_P (dmode)
18933 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
18934 && DECL_INCOMING_RTL (decl))
18936 rtx inc = DECL_INCOMING_RTL (decl);
18937 if (REG_P (inc))
18938 rtl = inc;
18939 else if (MEM_P (inc))
18941 if (BYTES_BIG_ENDIAN)
18942 rtl = adjust_address_nv (inc, dmode,
18943 GET_MODE_SIZE (pmode)
18944 - GET_MODE_SIZE (dmode));
18945 else
18946 rtl = inc;
18951 /* If the parm was passed in registers, but lives on the stack, then
18952 make a big endian correction if the mode of the type of the
18953 parameter is not the same as the mode of the rtl. */
18954 /* ??? This is the same series of checks that are made in dbxout.c before
18955 we reach the big endian correction code there. It isn't clear if all
18956 of these checks are necessary here, but keeping them all is the safe
18957 thing to do. */
18958 else if (MEM_P (rtl)
18959 && XEXP (rtl, 0) != const0_rtx
18960 && ! CONSTANT_P (XEXP (rtl, 0))
18961 /* Not passed in memory. */
18962 && !MEM_P (DECL_INCOMING_RTL (decl))
18963 /* Not passed by invisible reference. */
18964 && (!REG_P (XEXP (rtl, 0))
18965 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
18966 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
18967 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
18968 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
18969 #endif
18971 /* Big endian correction check. */
18972 && BYTES_BIG_ENDIAN
18973 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
18974 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
18975 < UNITS_PER_WORD))
18977 machine_mode addr_mode = get_address_mode (rtl);
18978 int offset = (UNITS_PER_WORD
18979 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
18981 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
18982 plus_constant (addr_mode, XEXP (rtl, 0), offset));
18985 else if (VAR_P (decl)
18986 && rtl
18987 && MEM_P (rtl)
18988 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
18989 && BYTES_BIG_ENDIAN)
18991 machine_mode addr_mode = get_address_mode (rtl);
18992 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
18993 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
18995 /* If a variable is declared "register" yet is smaller than
18996 a register, then if we store the variable to memory, it
18997 looks like we're storing a register-sized value, when in
18998 fact we are not. We need to adjust the offset of the
18999 storage location to reflect the actual value's bytes,
19000 else gdb will not be able to display it. */
19001 if (rsize > dsize)
19002 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
19003 plus_constant (addr_mode, XEXP (rtl, 0),
19004 rsize - dsize));
19007 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
19008 and will have been substituted directly into all expressions that use it.
19009 C does not have such a concept, but C++ and other languages do. */
19010 if (!rtl && VAR_P (decl) && DECL_INITIAL (decl))
19011 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
19013 if (rtl)
19014 rtl = targetm.delegitimize_address (rtl);
19016 /* If we don't look past the constant pool, we risk emitting a
19017 reference to a constant pool entry that isn't referenced from
19018 code, and thus is not emitted. */
19019 if (rtl)
19020 rtl = avoid_constant_pool_reference (rtl);
19022 /* Try harder to get a rtl. If this symbol ends up not being emitted
19023 in the current CU, resolve_addr will remove the expression referencing
19024 it. */
19025 if (rtl == NULL_RTX
19026 && VAR_P (decl)
19027 && !DECL_EXTERNAL (decl)
19028 && TREE_STATIC (decl)
19029 && DECL_NAME (decl)
19030 && !DECL_HARD_REGISTER (decl)
19031 && DECL_MODE (decl) != VOIDmode)
19033 rtl = make_decl_rtl_for_debug (decl);
19034 if (!MEM_P (rtl)
19035 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
19036 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
19037 rtl = NULL_RTX;
19040 return rtl;
19043 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
19044 returned. If so, the decl for the COMMON block is returned, and the
19045 value is the offset into the common block for the symbol. */
19047 static tree
19048 fortran_common (tree decl, HOST_WIDE_INT *value)
19050 tree val_expr, cvar;
19051 machine_mode mode;
19052 HOST_WIDE_INT bitsize, bitpos;
19053 tree offset;
19054 int unsignedp, reversep, volatilep = 0;
19056 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
19057 it does not have a value (the offset into the common area), or if it
19058 is thread local (as opposed to global) then it isn't common, and shouldn't
19059 be handled as such. */
19060 if (!VAR_P (decl)
19061 || !TREE_STATIC (decl)
19062 || !DECL_HAS_VALUE_EXPR_P (decl)
19063 || !is_fortran ())
19064 return NULL_TREE;
19066 val_expr = DECL_VALUE_EXPR (decl);
19067 if (TREE_CODE (val_expr) != COMPONENT_REF)
19068 return NULL_TREE;
19070 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset, &mode,
19071 &unsignedp, &reversep, &volatilep);
19073 if (cvar == NULL_TREE
19074 || !VAR_P (cvar)
19075 || DECL_ARTIFICIAL (cvar)
19076 || !TREE_PUBLIC (cvar))
19077 return NULL_TREE;
19079 *value = 0;
19080 if (offset != NULL)
19082 if (!tree_fits_shwi_p (offset))
19083 return NULL_TREE;
19084 *value = tree_to_shwi (offset);
19086 if (bitpos != 0)
19087 *value += bitpos / BITS_PER_UNIT;
19089 return cvar;
19092 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
19093 data attribute for a variable or a parameter. We generate the
19094 DW_AT_const_value attribute only in those cases where the given variable
19095 or parameter does not have a true "location" either in memory or in a
19096 register. This can happen (for example) when a constant is passed as an
19097 actual argument in a call to an inline function. (It's possible that
19098 these things can crop up in other ways also.) Note that one type of
19099 constant value which can be passed into an inlined function is a constant
19100 pointer. This can happen for example if an actual argument in an inlined
19101 function call evaluates to a compile-time constant address.
19103 CACHE_P is true if it is worth caching the location list for DECL,
19104 so that future calls can reuse it rather than regenerate it from scratch.
19105 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
19106 since we will need to refer to them each time the function is inlined. */
19108 static bool
19109 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p)
19111 rtx rtl;
19112 dw_loc_list_ref list;
19113 var_loc_list *loc_list;
19114 cached_dw_loc_list *cache;
19116 if (early_dwarf)
19117 return false;
19119 if (TREE_CODE (decl) == ERROR_MARK)
19120 return false;
19122 if (get_AT (die, DW_AT_location)
19123 || get_AT (die, DW_AT_const_value))
19124 return true;
19126 gcc_assert (VAR_P (decl) || TREE_CODE (decl) == PARM_DECL
19127 || TREE_CODE (decl) == RESULT_DECL);
19129 /* Try to get some constant RTL for this decl, and use that as the value of
19130 the location. */
19132 rtl = rtl_for_decl_location (decl);
19133 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
19134 && add_const_value_attribute (die, rtl))
19135 return true;
19137 /* See if we have single element location list that is equivalent to
19138 a constant value. That way we are better to use add_const_value_attribute
19139 rather than expanding constant value equivalent. */
19140 loc_list = lookup_decl_loc (decl);
19141 if (loc_list
19142 && loc_list->first
19143 && loc_list->first->next == NULL
19144 && NOTE_P (loc_list->first->loc)
19145 && NOTE_VAR_LOCATION (loc_list->first->loc)
19146 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
19148 struct var_loc_node *node;
19150 node = loc_list->first;
19151 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
19152 if (GET_CODE (rtl) == EXPR_LIST)
19153 rtl = XEXP (rtl, 0);
19154 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
19155 && add_const_value_attribute (die, rtl))
19156 return true;
19158 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
19159 list several times. See if we've already cached the contents. */
19160 list = NULL;
19161 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
19162 cache_p = false;
19163 if (cache_p)
19165 cache = cached_dw_loc_list_table->find_with_hash (decl, DECL_UID (decl));
19166 if (cache)
19167 list = cache->loc_list;
19169 if (list == NULL)
19171 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2,
19172 NULL);
19173 /* It is usually worth caching this result if the decl is from
19174 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
19175 if (cache_p && list && list->dw_loc_next)
19177 cached_dw_loc_list **slot
19178 = cached_dw_loc_list_table->find_slot_with_hash (decl,
19179 DECL_UID (decl),
19180 INSERT);
19181 cache = ggc_cleared_alloc<cached_dw_loc_list> ();
19182 cache->decl_id = DECL_UID (decl);
19183 cache->loc_list = list;
19184 *slot = cache;
19187 if (list)
19189 add_AT_location_description (die, DW_AT_location, list);
19190 return true;
19192 /* None of that worked, so it must not really have a location;
19193 try adding a constant value attribute from the DECL_INITIAL. */
19194 return tree_add_const_value_attribute_for_decl (die, decl);
19197 /* Helper function for tree_add_const_value_attribute. Natively encode
19198 initializer INIT into an array. Return true if successful. */
19200 static bool
19201 native_encode_initializer (tree init, unsigned char *array, int size)
19203 tree type;
19205 if (init == NULL_TREE)
19206 return false;
19208 STRIP_NOPS (init);
19209 switch (TREE_CODE (init))
19211 case STRING_CST:
19212 type = TREE_TYPE (init);
19213 if (TREE_CODE (type) == ARRAY_TYPE)
19215 tree enttype = TREE_TYPE (type);
19216 machine_mode mode = TYPE_MODE (enttype);
19218 if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
19219 return false;
19220 if (int_size_in_bytes (type) != size)
19221 return false;
19222 if (size > TREE_STRING_LENGTH (init))
19224 memcpy (array, TREE_STRING_POINTER (init),
19225 TREE_STRING_LENGTH (init));
19226 memset (array + TREE_STRING_LENGTH (init),
19227 '\0', size - TREE_STRING_LENGTH (init));
19229 else
19230 memcpy (array, TREE_STRING_POINTER (init), size);
19231 return true;
19233 return false;
19234 case CONSTRUCTOR:
19235 type = TREE_TYPE (init);
19236 if (int_size_in_bytes (type) != size)
19237 return false;
19238 if (TREE_CODE (type) == ARRAY_TYPE)
19240 HOST_WIDE_INT min_index;
19241 unsigned HOST_WIDE_INT cnt;
19242 int curpos = 0, fieldsize;
19243 constructor_elt *ce;
19245 if (TYPE_DOMAIN (type) == NULL_TREE
19246 || !tree_fits_shwi_p (TYPE_MIN_VALUE (TYPE_DOMAIN (type))))
19247 return false;
19249 fieldsize = int_size_in_bytes (TREE_TYPE (type));
19250 if (fieldsize <= 0)
19251 return false;
19253 min_index = tree_to_shwi (TYPE_MIN_VALUE (TYPE_DOMAIN (type)));
19254 memset (array, '\0', size);
19255 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
19257 tree val = ce->value;
19258 tree index = ce->index;
19259 int pos = curpos;
19260 if (index && TREE_CODE (index) == RANGE_EXPR)
19261 pos = (tree_to_shwi (TREE_OPERAND (index, 0)) - min_index)
19262 * fieldsize;
19263 else if (index)
19264 pos = (tree_to_shwi (index) - min_index) * fieldsize;
19266 if (val)
19268 STRIP_NOPS (val);
19269 if (!native_encode_initializer (val, array + pos, fieldsize))
19270 return false;
19272 curpos = pos + fieldsize;
19273 if (index && TREE_CODE (index) == RANGE_EXPR)
19275 int count = tree_to_shwi (TREE_OPERAND (index, 1))
19276 - tree_to_shwi (TREE_OPERAND (index, 0));
19277 while (count-- > 0)
19279 if (val)
19280 memcpy (array + curpos, array + pos, fieldsize);
19281 curpos += fieldsize;
19284 gcc_assert (curpos <= size);
19286 return true;
19288 else if (TREE_CODE (type) == RECORD_TYPE
19289 || TREE_CODE (type) == UNION_TYPE)
19291 tree field = NULL_TREE;
19292 unsigned HOST_WIDE_INT cnt;
19293 constructor_elt *ce;
19295 if (int_size_in_bytes (type) != size)
19296 return false;
19298 if (TREE_CODE (type) == RECORD_TYPE)
19299 field = TYPE_FIELDS (type);
19301 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
19303 tree val = ce->value;
19304 int pos, fieldsize;
19306 if (ce->index != 0)
19307 field = ce->index;
19309 if (val)
19310 STRIP_NOPS (val);
19312 if (field == NULL_TREE || DECL_BIT_FIELD (field))
19313 return false;
19315 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
19316 && TYPE_DOMAIN (TREE_TYPE (field))
19317 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
19318 return false;
19319 else if (DECL_SIZE_UNIT (field) == NULL_TREE
19320 || !tree_fits_shwi_p (DECL_SIZE_UNIT (field)))
19321 return false;
19322 fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
19323 pos = int_byte_position (field);
19324 gcc_assert (pos + fieldsize <= size);
19325 if (val && fieldsize != 0
19326 && !native_encode_initializer (val, array + pos, fieldsize))
19327 return false;
19329 return true;
19331 return false;
19332 case VIEW_CONVERT_EXPR:
19333 case NON_LVALUE_EXPR:
19334 return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
19335 default:
19336 return native_encode_expr (init, array, size) == size;
19340 /* Attach a DW_AT_const_value attribute to DIE. The value of the
19341 attribute is the const value T. */
19343 static bool
19344 tree_add_const_value_attribute (dw_die_ref die, tree t)
19346 tree init;
19347 tree type = TREE_TYPE (t);
19348 rtx rtl;
19350 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
19351 return false;
19353 init = t;
19354 gcc_assert (!DECL_P (init));
19356 if (! early_dwarf)
19358 rtl = rtl_for_decl_init (init, type);
19359 if (rtl)
19360 return add_const_value_attribute (die, rtl);
19362 /* If the host and target are sane, try harder. */
19363 if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
19364 && initializer_constant_valid_p (init, type))
19366 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
19367 if (size > 0 && (int) size == size)
19369 unsigned char *array = ggc_cleared_vec_alloc<unsigned char> (size);
19371 if (native_encode_initializer (init, array, size))
19373 add_AT_vec (die, DW_AT_const_value, size, 1, array);
19374 return true;
19376 ggc_free (array);
19379 return false;
19382 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
19383 attribute is the const value of T, where T is an integral constant
19384 variable with static storage duration
19385 (so it can't be a PARM_DECL or a RESULT_DECL). */
19387 static bool
19388 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
19391 if (!decl
19392 || (!VAR_P (decl) && TREE_CODE (decl) != CONST_DECL)
19393 || (VAR_P (decl) && !TREE_STATIC (decl)))
19394 return false;
19396 if (TREE_READONLY (decl)
19397 && ! TREE_THIS_VOLATILE (decl)
19398 && DECL_INITIAL (decl))
19399 /* OK */;
19400 else
19401 return false;
19403 /* Don't add DW_AT_const_value if abstract origin already has one. */
19404 if (get_AT (var_die, DW_AT_const_value))
19405 return false;
19407 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
19410 /* Convert the CFI instructions for the current function into a
19411 location list. This is used for DW_AT_frame_base when we targeting
19412 a dwarf2 consumer that does not support the dwarf3
19413 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
19414 expressions. */
19416 static dw_loc_list_ref
19417 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
19419 int ix;
19420 dw_fde_ref fde;
19421 dw_loc_list_ref list, *list_tail;
19422 dw_cfi_ref cfi;
19423 dw_cfa_location last_cfa, next_cfa;
19424 const char *start_label, *last_label, *section;
19425 dw_cfa_location remember;
19427 fde = cfun->fde;
19428 gcc_assert (fde != NULL);
19430 section = secname_for_decl (current_function_decl);
19431 list_tail = &list;
19432 list = NULL;
19434 memset (&next_cfa, 0, sizeof (next_cfa));
19435 next_cfa.reg = INVALID_REGNUM;
19436 remember = next_cfa;
19438 start_label = fde->dw_fde_begin;
19440 /* ??? Bald assumption that the CIE opcode list does not contain
19441 advance opcodes. */
19442 FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
19443 lookup_cfa_1 (cfi, &next_cfa, &remember);
19445 last_cfa = next_cfa;
19446 last_label = start_label;
19448 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
19450 /* If the first partition contained no CFI adjustments, the
19451 CIE opcodes apply to the whole first partition. */
19452 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19453 fde->dw_fde_begin, fde->dw_fde_end, section);
19454 list_tail =&(*list_tail)->dw_loc_next;
19455 start_label = last_label = fde->dw_fde_second_begin;
19458 FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
19460 switch (cfi->dw_cfi_opc)
19462 case DW_CFA_set_loc:
19463 case DW_CFA_advance_loc1:
19464 case DW_CFA_advance_loc2:
19465 case DW_CFA_advance_loc4:
19466 if (!cfa_equal_p (&last_cfa, &next_cfa))
19468 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19469 start_label, last_label, section);
19471 list_tail = &(*list_tail)->dw_loc_next;
19472 last_cfa = next_cfa;
19473 start_label = last_label;
19475 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
19476 break;
19478 case DW_CFA_advance_loc:
19479 /* The encoding is complex enough that we should never emit this. */
19480 gcc_unreachable ();
19482 default:
19483 lookup_cfa_1 (cfi, &next_cfa, &remember);
19484 break;
19486 if (ix + 1 == fde->dw_fde_switch_cfi_index)
19488 if (!cfa_equal_p (&last_cfa, &next_cfa))
19490 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19491 start_label, last_label, section);
19493 list_tail = &(*list_tail)->dw_loc_next;
19494 last_cfa = next_cfa;
19495 start_label = last_label;
19497 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19498 start_label, fde->dw_fde_end, section);
19499 list_tail = &(*list_tail)->dw_loc_next;
19500 start_label = last_label = fde->dw_fde_second_begin;
19504 if (!cfa_equal_p (&last_cfa, &next_cfa))
19506 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19507 start_label, last_label, section);
19508 list_tail = &(*list_tail)->dw_loc_next;
19509 start_label = last_label;
19512 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
19513 start_label,
19514 fde->dw_fde_second_begin
19515 ? fde->dw_fde_second_end : fde->dw_fde_end,
19516 section);
19518 if (list && list->dw_loc_next)
19519 gen_llsym (list);
19521 return list;
19524 /* Compute a displacement from the "steady-state frame pointer" to the
19525 frame base (often the same as the CFA), and store it in
19526 frame_pointer_fb_offset. OFFSET is added to the displacement
19527 before the latter is negated. */
19529 static void
19530 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
19532 rtx reg, elim;
19534 #ifdef FRAME_POINTER_CFA_OFFSET
19535 reg = frame_pointer_rtx;
19536 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
19537 #else
19538 reg = arg_pointer_rtx;
19539 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
19540 #endif
19542 elim = (ira_use_lra_p
19543 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
19544 : eliminate_regs (reg, VOIDmode, NULL_RTX));
19545 if (GET_CODE (elim) == PLUS)
19547 offset += INTVAL (XEXP (elim, 1));
19548 elim = XEXP (elim, 0);
19551 frame_pointer_fb_offset = -offset;
19553 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
19554 in which to eliminate. This is because it's stack pointer isn't
19555 directly accessible as a register within the ISA. To work around
19556 this, assume that while we cannot provide a proper value for
19557 frame_pointer_fb_offset, we won't need one either. */
19558 frame_pointer_fb_offset_valid
19559 = ((SUPPORTS_STACK_ALIGNMENT
19560 && (elim == hard_frame_pointer_rtx
19561 || elim == stack_pointer_rtx))
19562 || elim == (frame_pointer_needed
19563 ? hard_frame_pointer_rtx
19564 : stack_pointer_rtx));
19567 /* Generate a DW_AT_name attribute given some string value to be included as
19568 the value of the attribute. */
19570 static void
19571 add_name_attribute (dw_die_ref die, const char *name_string)
19573 if (name_string != NULL && *name_string != 0)
19575 if (demangle_name_func)
19576 name_string = (*demangle_name_func) (name_string);
19578 add_AT_string (die, DW_AT_name, name_string);
19582 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
19583 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
19584 of TYPE accordingly.
19586 ??? This is a temporary measure until after we're able to generate
19587 regular DWARF for the complex Ada type system. */
19589 static void
19590 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
19591 dw_die_ref context_die)
19593 tree dtype;
19594 dw_die_ref dtype_die;
19596 if (!lang_hooks.types.descriptive_type)
19597 return;
19599 dtype = lang_hooks.types.descriptive_type (type);
19600 if (!dtype)
19601 return;
19603 dtype_die = lookup_type_die (dtype);
19604 if (!dtype_die)
19606 gen_type_die (dtype, context_die);
19607 dtype_die = lookup_type_die (dtype);
19608 gcc_assert (dtype_die);
19611 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
19614 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir. */
19616 static const char *
19617 comp_dir_string (void)
19619 const char *wd;
19620 char *wd1;
19621 static const char *cached_wd = NULL;
19623 if (cached_wd != NULL)
19624 return cached_wd;
19626 wd = get_src_pwd ();
19627 if (wd == NULL)
19628 return NULL;
19630 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
19632 int wdlen;
19634 wdlen = strlen (wd);
19635 wd1 = ggc_vec_alloc<char> (wdlen + 2);
19636 strcpy (wd1, wd);
19637 wd1 [wdlen] = DIR_SEPARATOR;
19638 wd1 [wdlen + 1] = 0;
19639 wd = wd1;
19642 cached_wd = remap_debug_filename (wd);
19643 return cached_wd;
19646 /* Generate a DW_AT_comp_dir attribute for DIE. */
19648 static void
19649 add_comp_dir_attribute (dw_die_ref die)
19651 const char * wd = comp_dir_string ();
19652 if (wd != NULL)
19653 add_AT_string (die, DW_AT_comp_dir, wd);
19656 /* Given a tree node VALUE describing a scalar attribute ATTR (i.e. a bound, a
19657 pointer computation, ...), output a representation for that bound according
19658 to the accepted FORMS (see enum dw_scalar_form) and add it to DIE. See
19659 loc_list_from_tree for the meaning of CONTEXT. */
19661 static void
19662 add_scalar_info (dw_die_ref die, enum dwarf_attribute attr, tree value,
19663 int forms, struct loc_descr_context *context)
19665 dw_die_ref context_die, decl_die;
19666 dw_loc_list_ref list;
19667 bool strip_conversions = true;
19668 bool placeholder_seen = false;
19670 while (strip_conversions)
19671 switch (TREE_CODE (value))
19673 case ERROR_MARK:
19674 case SAVE_EXPR:
19675 return;
19677 CASE_CONVERT:
19678 case VIEW_CONVERT_EXPR:
19679 value = TREE_OPERAND (value, 0);
19680 break;
19682 default:
19683 strip_conversions = false;
19684 break;
19687 /* If possible and permitted, output the attribute as a constant. */
19688 if ((forms & dw_scalar_form_constant) != 0
19689 && TREE_CODE (value) == INTEGER_CST)
19691 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (value));
19693 /* If HOST_WIDE_INT is big enough then represent the bound as
19694 a constant value. We need to choose a form based on
19695 whether the type is signed or unsigned. We cannot just
19696 call add_AT_unsigned if the value itself is positive
19697 (add_AT_unsigned might add the unsigned value encoded as
19698 DW_FORM_data[1248]). Some DWARF consumers will lookup the
19699 bounds type and then sign extend any unsigned values found
19700 for signed types. This is needed only for
19701 DW_AT_{lower,upper}_bound, since for most other attributes,
19702 consumers will treat DW_FORM_data[1248] as unsigned values,
19703 regardless of the underlying type. */
19704 if (prec <= HOST_BITS_PER_WIDE_INT
19705 || tree_fits_uhwi_p (value))
19707 if (TYPE_UNSIGNED (TREE_TYPE (value)))
19708 add_AT_unsigned (die, attr, TREE_INT_CST_LOW (value));
19709 else
19710 add_AT_int (die, attr, TREE_INT_CST_LOW (value));
19712 else
19713 /* Otherwise represent the bound as an unsigned value with
19714 the precision of its type. The precision and signedness
19715 of the type will be necessary to re-interpret it
19716 unambiguously. */
19717 add_AT_wide (die, attr, value);
19718 return;
19721 /* Otherwise, if it's possible and permitted too, output a reference to
19722 another DIE. */
19723 if ((forms & dw_scalar_form_reference) != 0)
19725 tree decl = NULL_TREE;
19727 /* Some type attributes reference an outer type. For instance, the upper
19728 bound of an array may reference an embedding record (this happens in
19729 Ada). */
19730 if (TREE_CODE (value) == COMPONENT_REF
19731 && TREE_CODE (TREE_OPERAND (value, 0)) == PLACEHOLDER_EXPR
19732 && TREE_CODE (TREE_OPERAND (value, 1)) == FIELD_DECL)
19733 decl = TREE_OPERAND (value, 1);
19735 else if (VAR_P (value)
19736 || TREE_CODE (value) == PARM_DECL
19737 || TREE_CODE (value) == RESULT_DECL)
19738 decl = value;
19740 if (decl != NULL_TREE)
19742 dw_die_ref decl_die = lookup_decl_die (decl);
19744 /* ??? Can this happen, or should the variable have been bound
19745 first? Probably it can, since I imagine that we try to create
19746 the types of parameters in the order in which they exist in
19747 the list, and won't have created a forward reference to a
19748 later parameter. */
19749 if (decl_die != NULL)
19751 add_AT_die_ref (die, attr, decl_die);
19752 return;
19757 /* Last chance: try to create a stack operation procedure to evaluate the
19758 value. Do nothing if even that is not possible or permitted. */
19759 if ((forms & dw_scalar_form_exprloc) == 0)
19760 return;
19762 list = loc_list_from_tree (value, 2, context);
19763 if (context && context->placeholder_arg)
19765 placeholder_seen = context->placeholder_seen;
19766 context->placeholder_seen = false;
19768 if (list == NULL || single_element_loc_list_p (list))
19770 /* If this attribute is not a reference nor constant, it is
19771 a DWARF expression rather than location description. For that
19772 loc_list_from_tree (value, 0, &context) is needed. */
19773 dw_loc_list_ref list2 = loc_list_from_tree (value, 0, context);
19774 if (list2 && single_element_loc_list_p (list2))
19776 if (placeholder_seen)
19778 struct dwarf_procedure_info dpi;
19779 dpi.fndecl = NULL_TREE;
19780 dpi.args_count = 1;
19781 if (!resolve_args_picking (list2->expr, 1, &dpi))
19782 return;
19784 add_AT_loc (die, attr, list2->expr);
19785 return;
19789 /* If that failed to give a single element location list, fall back to
19790 outputting this as a reference... still if permitted. */
19791 if (list == NULL
19792 || (forms & dw_scalar_form_reference) == 0
19793 || placeholder_seen)
19794 return;
19796 if (current_function_decl == 0)
19797 context_die = comp_unit_die ();
19798 else
19799 context_die = lookup_decl_die (current_function_decl);
19801 decl_die = new_die (DW_TAG_variable, context_die, value);
19802 add_AT_flag (decl_die, DW_AT_artificial, 1);
19803 add_type_attribute (decl_die, TREE_TYPE (value), TYPE_QUAL_CONST, false,
19804 context_die);
19805 add_AT_location_description (decl_die, DW_AT_location, list);
19806 add_AT_die_ref (die, attr, decl_die);
19809 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
19810 default. */
19812 static int
19813 lower_bound_default (void)
19815 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
19817 case DW_LANG_C:
19818 case DW_LANG_C89:
19819 case DW_LANG_C99:
19820 case DW_LANG_C11:
19821 case DW_LANG_C_plus_plus:
19822 case DW_LANG_C_plus_plus_11:
19823 case DW_LANG_C_plus_plus_14:
19824 case DW_LANG_ObjC:
19825 case DW_LANG_ObjC_plus_plus:
19826 case DW_LANG_Java:
19827 return 0;
19828 case DW_LANG_Fortran77:
19829 case DW_LANG_Fortran90:
19830 case DW_LANG_Fortran95:
19831 case DW_LANG_Fortran03:
19832 case DW_LANG_Fortran08:
19833 return 1;
19834 case DW_LANG_UPC:
19835 case DW_LANG_D:
19836 case DW_LANG_Python:
19837 return dwarf_version >= 4 ? 0 : -1;
19838 case DW_LANG_Ada95:
19839 case DW_LANG_Ada83:
19840 case DW_LANG_Cobol74:
19841 case DW_LANG_Cobol85:
19842 case DW_LANG_Pascal83:
19843 case DW_LANG_Modula2:
19844 case DW_LANG_PLI:
19845 return dwarf_version >= 4 ? 1 : -1;
19846 default:
19847 return -1;
19851 /* Given a tree node describing an array bound (either lower or upper) output
19852 a representation for that bound. */
19854 static void
19855 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr,
19856 tree bound, struct loc_descr_context *context)
19858 int dflt;
19860 while (1)
19861 switch (TREE_CODE (bound))
19863 /* Strip all conversions. */
19864 CASE_CONVERT:
19865 case VIEW_CONVERT_EXPR:
19866 bound = TREE_OPERAND (bound, 0);
19867 break;
19869 /* All fixed-bounds are represented by INTEGER_CST nodes. Lower bounds
19870 are even omitted when they are the default. */
19871 case INTEGER_CST:
19872 /* If the value for this bound is the default one, we can even omit the
19873 attribute. */
19874 if (bound_attr == DW_AT_lower_bound
19875 && tree_fits_shwi_p (bound)
19876 && (dflt = lower_bound_default ()) != -1
19877 && tree_to_shwi (bound) == dflt)
19878 return;
19880 /* FALLTHRU */
19882 default:
19883 /* Because of the complex interaction there can be with other GNAT
19884 encodings, GDB isn't ready yet to handle proper DWARF description
19885 for self-referencial subrange bounds: let GNAT encodings do the
19886 magic in such a case. */
19887 if (is_ada ()
19888 && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL
19889 && contains_placeholder_p (bound))
19890 return;
19892 add_scalar_info (subrange_die, bound_attr, bound,
19893 dw_scalar_form_constant
19894 | dw_scalar_form_exprloc
19895 | dw_scalar_form_reference,
19896 context);
19897 return;
19901 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
19902 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
19903 Note that the block of subscript information for an array type also
19904 includes information about the element type of the given array type.
19906 This function reuses previously set type and bound information if
19907 available. */
19909 static void
19910 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
19912 unsigned dimension_number;
19913 tree lower, upper;
19914 dw_die_ref child = type_die->die_child;
19916 for (dimension_number = 0;
19917 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
19918 type = TREE_TYPE (type), dimension_number++)
19920 tree domain = TYPE_DOMAIN (type);
19922 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
19923 break;
19925 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
19926 and (in GNU C only) variable bounds. Handle all three forms
19927 here. */
19929 /* Find and reuse a previously generated DW_TAG_subrange_type if
19930 available.
19932 For multi-dimensional arrays, as we iterate through the
19933 various dimensions in the enclosing for loop above, we also
19934 iterate through the DIE children and pick at each
19935 DW_TAG_subrange_type previously generated (if available).
19936 Each child DW_TAG_subrange_type DIE describes the range of
19937 the current dimension. At this point we should have as many
19938 DW_TAG_subrange_type's as we have dimensions in the
19939 array. */
19940 dw_die_ref subrange_die = NULL;
19941 if (child)
19942 while (1)
19944 child = child->die_sib;
19945 if (child->die_tag == DW_TAG_subrange_type)
19946 subrange_die = child;
19947 if (child == type_die->die_child)
19949 /* If we wrapped around, stop looking next time. */
19950 child = NULL;
19951 break;
19953 if (child->die_tag == DW_TAG_subrange_type)
19954 break;
19956 if (!subrange_die)
19957 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
19959 if (domain)
19961 /* We have an array type with specified bounds. */
19962 lower = TYPE_MIN_VALUE (domain);
19963 upper = TYPE_MAX_VALUE (domain);
19965 /* Define the index type. */
19966 if (TREE_TYPE (domain)
19967 && !get_AT (subrange_die, DW_AT_type))
19969 /* ??? This is probably an Ada unnamed subrange type. Ignore the
19970 TREE_TYPE field. We can't emit debug info for this
19971 because it is an unnamed integral type. */
19972 if (TREE_CODE (domain) == INTEGER_TYPE
19973 && TYPE_NAME (domain) == NULL_TREE
19974 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
19975 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
19977 else
19978 add_type_attribute (subrange_die, TREE_TYPE (domain),
19979 TYPE_UNQUALIFIED, false, type_die);
19982 /* ??? If upper is NULL, the array has unspecified length,
19983 but it does have a lower bound. This happens with Fortran
19984 dimension arr(N:*)
19985 Since the debugger is definitely going to need to know N
19986 to produce useful results, go ahead and output the lower
19987 bound solo, and hope the debugger can cope. */
19989 if (!get_AT (subrange_die, DW_AT_lower_bound))
19990 add_bound_info (subrange_die, DW_AT_lower_bound, lower, NULL);
19991 if (upper && !get_AT (subrange_die, DW_AT_upper_bound))
19992 add_bound_info (subrange_die, DW_AT_upper_bound, upper, NULL);
19995 /* Otherwise we have an array type with an unspecified length. The
19996 DWARF-2 spec does not say how to handle this; let's just leave out the
19997 bounds. */
20001 /* Add a DW_AT_byte_size attribute to DIE with TREE_NODE's size. */
20003 static void
20004 add_byte_size_attribute (dw_die_ref die, tree tree_node)
20006 dw_die_ref decl_die;
20007 HOST_WIDE_INT size;
20008 dw_loc_descr_ref size_expr = NULL;
20010 switch (TREE_CODE (tree_node))
20012 case ERROR_MARK:
20013 size = 0;
20014 break;
20015 case ENUMERAL_TYPE:
20016 case RECORD_TYPE:
20017 case UNION_TYPE:
20018 case QUAL_UNION_TYPE:
20019 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
20020 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
20022 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
20023 return;
20025 size_expr = type_byte_size (tree_node, &size);
20026 break;
20027 case FIELD_DECL:
20028 /* For a data member of a struct or union, the DW_AT_byte_size is
20029 generally given as the number of bytes normally allocated for an
20030 object of the *declared* type of the member itself. This is true
20031 even for bit-fields. */
20032 size = int_size_in_bytes (field_type (tree_node));
20033 break;
20034 default:
20035 gcc_unreachable ();
20038 /* Support for dynamically-sized objects was introduced by DWARFv3.
20039 At the moment, GDB does not handle variable byte sizes very well,
20040 though. */
20041 if ((dwarf_version >= 3 || !dwarf_strict)
20042 && gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL
20043 && size_expr != NULL)
20044 add_AT_loc (die, DW_AT_byte_size, size_expr);
20046 /* Note that `size' might be -1 when we get to this point. If it is, that
20047 indicates that the byte size of the entity in question is variable and
20048 that we could not generate a DWARF expression that computes it. */
20049 if (size >= 0)
20050 add_AT_unsigned (die, DW_AT_byte_size, size);
20053 /* Add a DW_AT_alignment attribute to DIE with TREE_NODE's non-default
20054 alignment. */
20056 static void
20057 add_alignment_attribute (dw_die_ref die, tree tree_node)
20059 if (dwarf_version < 5 && dwarf_strict)
20060 return;
20062 unsigned align;
20064 if (DECL_P (tree_node))
20066 if (!DECL_USER_ALIGN (tree_node))
20067 return;
20069 align = DECL_ALIGN_UNIT (tree_node);
20071 else if (TYPE_P (tree_node))
20073 if (!TYPE_USER_ALIGN (tree_node))
20074 return;
20076 align = TYPE_ALIGN_UNIT (tree_node);
20078 else
20079 gcc_unreachable ();
20081 add_AT_unsigned (die, DW_AT_alignment, align);
20084 /* For a FIELD_DECL node which represents a bit-field, output an attribute
20085 which specifies the distance in bits from the highest order bit of the
20086 "containing object" for the bit-field to the highest order bit of the
20087 bit-field itself.
20089 For any given bit-field, the "containing object" is a hypothetical object
20090 (of some integral or enum type) within which the given bit-field lives. The
20091 type of this hypothetical "containing object" is always the same as the
20092 declared type of the individual bit-field itself. The determination of the
20093 exact location of the "containing object" for a bit-field is rather
20094 complicated. It's handled by the `field_byte_offset' function (above).
20096 CTX is required: see the comment for VLR_CONTEXT.
20098 Note that it is the size (in bytes) of the hypothetical "containing object"
20099 which will be given in the DW_AT_byte_size attribute for this bit-field.
20100 (See `byte_size_attribute' above). */
20102 static inline void
20103 add_bit_offset_attribute (dw_die_ref die, tree decl, struct vlr_context *ctx)
20105 HOST_WIDE_INT object_offset_in_bytes;
20106 tree original_type = DECL_BIT_FIELD_TYPE (decl);
20107 HOST_WIDE_INT bitpos_int;
20108 HOST_WIDE_INT highest_order_object_bit_offset;
20109 HOST_WIDE_INT highest_order_field_bit_offset;
20110 HOST_WIDE_INT bit_offset;
20112 field_byte_offset (decl, ctx, &object_offset_in_bytes);
20114 /* Must be a field and a bit field. */
20115 gcc_assert (original_type && TREE_CODE (decl) == FIELD_DECL);
20117 /* We can't yet handle bit-fields whose offsets are variable, so if we
20118 encounter such things, just return without generating any attribute
20119 whatsoever. Likewise for variable or too large size. */
20120 if (! tree_fits_shwi_p (bit_position (decl))
20121 || ! tree_fits_uhwi_p (DECL_SIZE (decl)))
20122 return;
20124 bitpos_int = int_bit_position (decl);
20126 /* Note that the bit offset is always the distance (in bits) from the
20127 highest-order bit of the "containing object" to the highest-order bit of
20128 the bit-field itself. Since the "high-order end" of any object or field
20129 is different on big-endian and little-endian machines, the computation
20130 below must take account of these differences. */
20131 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
20132 highest_order_field_bit_offset = bitpos_int;
20134 if (! BYTES_BIG_ENDIAN)
20136 highest_order_field_bit_offset += tree_to_shwi (DECL_SIZE (decl));
20137 highest_order_object_bit_offset +=
20138 simple_type_size_in_bits (original_type);
20141 bit_offset
20142 = (! BYTES_BIG_ENDIAN
20143 ? highest_order_object_bit_offset - highest_order_field_bit_offset
20144 : highest_order_field_bit_offset - highest_order_object_bit_offset);
20146 if (bit_offset < 0)
20147 add_AT_int (die, DW_AT_bit_offset, bit_offset);
20148 else
20149 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
20152 /* For a FIELD_DECL node which represents a bit field, output an attribute
20153 which specifies the length in bits of the given field. */
20155 static inline void
20156 add_bit_size_attribute (dw_die_ref die, tree decl)
20158 /* Must be a field and a bit field. */
20159 gcc_assert (TREE_CODE (decl) == FIELD_DECL
20160 && DECL_BIT_FIELD_TYPE (decl));
20162 if (tree_fits_uhwi_p (DECL_SIZE (decl)))
20163 add_AT_unsigned (die, DW_AT_bit_size, tree_to_uhwi (DECL_SIZE (decl)));
20166 /* If the compiled language is ANSI C, then add a 'prototyped'
20167 attribute, if arg types are given for the parameters of a function. */
20169 static inline void
20170 add_prototyped_attribute (dw_die_ref die, tree func_type)
20172 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
20174 case DW_LANG_C:
20175 case DW_LANG_C89:
20176 case DW_LANG_C99:
20177 case DW_LANG_C11:
20178 case DW_LANG_ObjC:
20179 if (prototype_p (func_type))
20180 add_AT_flag (die, DW_AT_prototyped, 1);
20181 break;
20182 default:
20183 break;
20187 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
20188 by looking in the type declaration, the object declaration equate table or
20189 the block mapping. */
20191 static inline dw_die_ref
20192 add_abstract_origin_attribute (dw_die_ref die, tree origin)
20194 dw_die_ref origin_die = NULL;
20196 if (TREE_CODE (origin) != FUNCTION_DECL
20197 && TREE_CODE (origin) != BLOCK)
20199 /* We may have gotten separated from the block for the inlined
20200 function, if we're in an exception handler or some such; make
20201 sure that the abstract function has been written out.
20203 Doing this for nested functions is wrong, however; functions are
20204 distinct units, and our context might not even be inline. */
20205 tree fn = origin;
20207 if (TYPE_P (fn))
20208 fn = TYPE_STUB_DECL (fn);
20210 fn = decl_function_context (fn);
20211 if (fn)
20212 dwarf2out_abstract_function (fn);
20215 if (DECL_P (origin))
20216 origin_die = lookup_decl_die (origin);
20217 else if (TYPE_P (origin))
20218 origin_die = lookup_type_die (origin);
20219 else if (TREE_CODE (origin) == BLOCK)
20220 origin_die = BLOCK_DIE (origin);
20222 /* XXX: Functions that are never lowered don't always have correct block
20223 trees (in the case of java, they simply have no block tree, in some other
20224 languages). For these functions, there is nothing we can really do to
20225 output correct debug info for inlined functions in all cases. Rather
20226 than die, we'll just produce deficient debug info now, in that we will
20227 have variables without a proper abstract origin. In the future, when all
20228 functions are lowered, we should re-add a gcc_assert (origin_die)
20229 here. */
20231 if (origin_die)
20232 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
20233 return origin_die;
20236 /* We do not currently support the pure_virtual attribute. */
20238 static inline void
20239 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
20241 if (DECL_VINDEX (func_decl))
20243 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
20245 if (tree_fits_shwi_p (DECL_VINDEX (func_decl)))
20246 add_AT_loc (die, DW_AT_vtable_elem_location,
20247 new_loc_descr (DW_OP_constu,
20248 tree_to_shwi (DECL_VINDEX (func_decl)),
20249 0));
20251 /* GNU extension: Record what type this method came from originally. */
20252 if (debug_info_level > DINFO_LEVEL_TERSE
20253 && DECL_CONTEXT (func_decl))
20254 add_AT_die_ref (die, DW_AT_containing_type,
20255 lookup_type_die (DECL_CONTEXT (func_decl)));
20259 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
20260 given decl. This used to be a vendor extension until after DWARF 4
20261 standardized it. */
20263 static void
20264 add_linkage_attr (dw_die_ref die, tree decl)
20266 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
20268 /* Mimic what assemble_name_raw does with a leading '*'. */
20269 if (name[0] == '*')
20270 name = &name[1];
20272 if (dwarf_version >= 4)
20273 add_AT_string (die, DW_AT_linkage_name, name);
20274 else
20275 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
20278 /* Add source coordinate attributes for the given decl. */
20280 static void
20281 add_src_coords_attributes (dw_die_ref die, tree decl)
20283 expanded_location s;
20285 if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
20286 return;
20287 s = expand_location (DECL_SOURCE_LOCATION (decl));
20288 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
20289 add_AT_unsigned (die, DW_AT_decl_line, s.line);
20290 if (debug_column_info && s.column)
20291 add_AT_unsigned (die, DW_AT_decl_column, s.column);
20294 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
20296 static void
20297 add_linkage_name_raw (dw_die_ref die, tree decl)
20299 /* Defer until we have an assembler name set. */
20300 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
20302 limbo_die_node *asm_name;
20304 asm_name = ggc_cleared_alloc<limbo_die_node> ();
20305 asm_name->die = die;
20306 asm_name->created_for = decl;
20307 asm_name->next = deferred_asm_name;
20308 deferred_asm_name = asm_name;
20310 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
20311 add_linkage_attr (die, decl);
20314 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl if desired. */
20316 static void
20317 add_linkage_name (dw_die_ref die, tree decl)
20319 if (debug_info_level > DINFO_LEVEL_NONE
20320 && VAR_OR_FUNCTION_DECL_P (decl)
20321 && TREE_PUBLIC (decl)
20322 && !(VAR_P (decl) && DECL_REGISTER (decl))
20323 && die->die_tag != DW_TAG_member)
20324 add_linkage_name_raw (die, decl);
20327 /* Add a DW_AT_name attribute and source coordinate attribute for the
20328 given decl, but only if it actually has a name. */
20330 static void
20331 add_name_and_src_coords_attributes (dw_die_ref die, tree decl,
20332 bool no_linkage_name)
20334 tree decl_name;
20336 decl_name = DECL_NAME (decl);
20337 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
20339 const char *name = dwarf2_name (decl, 0);
20340 if (name)
20341 add_name_attribute (die, name);
20342 if (! DECL_ARTIFICIAL (decl))
20343 add_src_coords_attributes (die, decl);
20345 if (!no_linkage_name)
20346 add_linkage_name (die, decl);
20349 #ifdef VMS_DEBUGGING_INFO
20350 /* Get the function's name, as described by its RTL. This may be different
20351 from the DECL_NAME name used in the source file. */
20352 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
20354 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
20355 XEXP (DECL_RTL (decl), 0), false);
20356 vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
20358 #endif /* VMS_DEBUGGING_INFO */
20361 /* Add VALUE as a DW_AT_discr_value attribute to DIE. */
20363 static void
20364 add_discr_value (dw_die_ref die, dw_discr_value *value)
20366 dw_attr_node attr;
20368 attr.dw_attr = DW_AT_discr_value;
20369 attr.dw_attr_val.val_class = dw_val_class_discr_value;
20370 attr.dw_attr_val.val_entry = NULL;
20371 attr.dw_attr_val.v.val_discr_value.pos = value->pos;
20372 if (value->pos)
20373 attr.dw_attr_val.v.val_discr_value.v.uval = value->v.uval;
20374 else
20375 attr.dw_attr_val.v.val_discr_value.v.sval = value->v.sval;
20376 add_dwarf_attr (die, &attr);
20379 /* Add DISCR_LIST as a DW_AT_discr_list to DIE. */
20381 static void
20382 add_discr_list (dw_die_ref die, dw_discr_list_ref discr_list)
20384 dw_attr_node attr;
20386 attr.dw_attr = DW_AT_discr_list;
20387 attr.dw_attr_val.val_class = dw_val_class_discr_list;
20388 attr.dw_attr_val.val_entry = NULL;
20389 attr.dw_attr_val.v.val_discr_list = discr_list;
20390 add_dwarf_attr (die, &attr);
20393 static inline dw_discr_list_ref
20394 AT_discr_list (dw_attr_node *attr)
20396 return attr->dw_attr_val.v.val_discr_list;
20399 #ifdef VMS_DEBUGGING_INFO
20400 /* Output the debug main pointer die for VMS */
20402 void
20403 dwarf2out_vms_debug_main_pointer (void)
20405 char label[MAX_ARTIFICIAL_LABEL_BYTES];
20406 dw_die_ref die;
20408 /* Allocate the VMS debug main subprogram die. */
20409 die = ggc_cleared_alloc<die_node> ();
20410 die->die_tag = DW_TAG_subprogram;
20411 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
20412 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
20413 current_function_funcdef_no);
20414 add_AT_lbl_id (die, DW_AT_entry_pc, label);
20416 /* Make it the first child of comp_unit_die (). */
20417 die->die_parent = comp_unit_die ();
20418 if (comp_unit_die ()->die_child)
20420 die->die_sib = comp_unit_die ()->die_child->die_sib;
20421 comp_unit_die ()->die_child->die_sib = die;
20423 else
20425 die->die_sib = die;
20426 comp_unit_die ()->die_child = die;
20429 #endif /* VMS_DEBUGGING_INFO */
20431 /* Push a new declaration scope. */
20433 static void
20434 push_decl_scope (tree scope)
20436 vec_safe_push (decl_scope_table, scope);
20439 /* Pop a declaration scope. */
20441 static inline void
20442 pop_decl_scope (void)
20444 decl_scope_table->pop ();
20447 /* walk_tree helper function for uses_local_type, below. */
20449 static tree
20450 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
20452 if (!TYPE_P (*tp))
20453 *walk_subtrees = 0;
20454 else
20456 tree name = TYPE_NAME (*tp);
20457 if (name && DECL_P (name) && decl_function_context (name))
20458 return *tp;
20460 return NULL_TREE;
20463 /* If TYPE involves a function-local type (including a local typedef to a
20464 non-local type), returns that type; otherwise returns NULL_TREE. */
20466 static tree
20467 uses_local_type (tree type)
20469 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
20470 return used;
20473 /* Return the DIE for the scope that immediately contains this type.
20474 Non-named types that do not involve a function-local type get global
20475 scope. Named types nested in namespaces or other types get their
20476 containing scope. All other types (i.e. function-local named types) get
20477 the current active scope. */
20479 static dw_die_ref
20480 scope_die_for (tree t, dw_die_ref context_die)
20482 dw_die_ref scope_die = NULL;
20483 tree containing_scope;
20485 /* Non-types always go in the current scope. */
20486 gcc_assert (TYPE_P (t));
20488 /* Use the scope of the typedef, rather than the scope of the type
20489 it refers to. */
20490 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
20491 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
20492 else
20493 containing_scope = TYPE_CONTEXT (t);
20495 /* Use the containing namespace if there is one. */
20496 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
20498 if (context_die == lookup_decl_die (containing_scope))
20499 /* OK */;
20500 else if (debug_info_level > DINFO_LEVEL_TERSE)
20501 context_die = get_context_die (containing_scope);
20502 else
20503 containing_scope = NULL_TREE;
20506 /* Ignore function type "scopes" from the C frontend. They mean that
20507 a tagged type is local to a parmlist of a function declarator, but
20508 that isn't useful to DWARF. */
20509 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
20510 containing_scope = NULL_TREE;
20512 if (SCOPE_FILE_SCOPE_P (containing_scope))
20514 /* If T uses a local type keep it local as well, to avoid references
20515 to function-local DIEs from outside the function. */
20516 if (current_function_decl && uses_local_type (t))
20517 scope_die = context_die;
20518 else
20519 scope_die = comp_unit_die ();
20521 else if (TYPE_P (containing_scope))
20523 /* For types, we can just look up the appropriate DIE. */
20524 if (debug_info_level > DINFO_LEVEL_TERSE)
20525 scope_die = get_context_die (containing_scope);
20526 else
20528 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
20529 if (scope_die == NULL)
20530 scope_die = comp_unit_die ();
20533 else
20534 scope_die = context_die;
20536 return scope_die;
20539 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
20541 static inline int
20542 local_scope_p (dw_die_ref context_die)
20544 for (; context_die; context_die = context_die->die_parent)
20545 if (context_die->die_tag == DW_TAG_inlined_subroutine
20546 || context_die->die_tag == DW_TAG_subprogram)
20547 return 1;
20549 return 0;
20552 /* Returns nonzero if CONTEXT_DIE is a class. */
20554 static inline int
20555 class_scope_p (dw_die_ref context_die)
20557 return (context_die
20558 && (context_die->die_tag == DW_TAG_structure_type
20559 || context_die->die_tag == DW_TAG_class_type
20560 || context_die->die_tag == DW_TAG_interface_type
20561 || context_die->die_tag == DW_TAG_union_type));
20564 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
20565 whether or not to treat a DIE in this context as a declaration. */
20567 static inline int
20568 class_or_namespace_scope_p (dw_die_ref context_die)
20570 return (class_scope_p (context_die)
20571 || (context_die && context_die->die_tag == DW_TAG_namespace));
20574 /* Many forms of DIEs require a "type description" attribute. This
20575 routine locates the proper "type descriptor" die for the type given
20576 by 'type' plus any additional qualifiers given by 'cv_quals', and
20577 adds a DW_AT_type attribute below the given die. */
20579 static void
20580 add_type_attribute (dw_die_ref object_die, tree type, int cv_quals,
20581 bool reverse, dw_die_ref context_die)
20583 enum tree_code code = TREE_CODE (type);
20584 dw_die_ref type_die = NULL;
20586 /* ??? If this type is an unnamed subrange type of an integral, floating-point
20587 or fixed-point type, use the inner type. This is because we have no
20588 support for unnamed types in base_type_die. This can happen if this is
20589 an Ada subrange type. Correct solution is emit a subrange type die. */
20590 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
20591 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
20592 type = TREE_TYPE (type), code = TREE_CODE (type);
20594 if (code == ERROR_MARK
20595 /* Handle a special case. For functions whose return type is void, we
20596 generate *no* type attribute. (Note that no object may have type
20597 `void', so this only applies to function return types). */
20598 || code == VOID_TYPE)
20599 return;
20601 type_die = modified_type_die (type,
20602 cv_quals | TYPE_QUALS_NO_ADDR_SPACE (type),
20603 reverse,
20604 context_die);
20606 if (type_die != NULL)
20607 add_AT_die_ref (object_die, DW_AT_type, type_die);
20610 /* Given an object die, add the calling convention attribute for the
20611 function call type. */
20612 static void
20613 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
20615 enum dwarf_calling_convention value = DW_CC_normal;
20617 value = ((enum dwarf_calling_convention)
20618 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
20620 if (is_fortran ()
20621 && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
20623 /* DWARF 2 doesn't provide a way to identify a program's source-level
20624 entry point. DW_AT_calling_convention attributes are only meant
20625 to describe functions' calling conventions. However, lacking a
20626 better way to signal the Fortran main program, we used this for
20627 a long time, following existing custom. Now, DWARF 4 has
20628 DW_AT_main_subprogram, which we add below, but some tools still
20629 rely on the old way, which we thus keep. */
20630 value = DW_CC_program;
20632 if (dwarf_version >= 4 || !dwarf_strict)
20633 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
20636 /* Only add the attribute if the backend requests it, and
20637 is not DW_CC_normal. */
20638 if (value && (value != DW_CC_normal))
20639 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
20642 /* Given a tree pointer to a struct, class, union, or enum type node, return
20643 a pointer to the (string) tag name for the given type, or zero if the type
20644 was declared without a tag. */
20646 static const char *
20647 type_tag (const_tree type)
20649 const char *name = 0;
20651 if (TYPE_NAME (type) != 0)
20653 tree t = 0;
20655 /* Find the IDENTIFIER_NODE for the type name. */
20656 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
20657 && !TYPE_NAMELESS (type))
20658 t = TYPE_NAME (type);
20660 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
20661 a TYPE_DECL node, regardless of whether or not a `typedef' was
20662 involved. */
20663 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
20664 && ! DECL_IGNORED_P (TYPE_NAME (type)))
20666 /* We want to be extra verbose. Don't call dwarf_name if
20667 DECL_NAME isn't set. The default hook for decl_printable_name
20668 doesn't like that, and in this context it's correct to return
20669 0, instead of "<anonymous>" or the like. */
20670 if (DECL_NAME (TYPE_NAME (type))
20671 && !DECL_NAMELESS (TYPE_NAME (type)))
20672 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
20675 /* Now get the name as a string, or invent one. */
20676 if (!name && t != 0)
20677 name = IDENTIFIER_POINTER (t);
20680 return (name == 0 || *name == '\0') ? 0 : name;
20683 /* Return the type associated with a data member, make a special check
20684 for bit field types. */
20686 static inline tree
20687 member_declared_type (const_tree member)
20689 return (DECL_BIT_FIELD_TYPE (member)
20690 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
20693 /* Get the decl's label, as described by its RTL. This may be different
20694 from the DECL_NAME name used in the source file. */
20696 #if 0
20697 static const char *
20698 decl_start_label (tree decl)
20700 rtx x;
20701 const char *fnname;
20703 x = DECL_RTL (decl);
20704 gcc_assert (MEM_P (x));
20706 x = XEXP (x, 0);
20707 gcc_assert (GET_CODE (x) == SYMBOL_REF);
20709 fnname = XSTR (x, 0);
20710 return fnname;
20712 #endif
20714 /* For variable-length arrays that have been previously generated, but
20715 may be incomplete due to missing subscript info, fill the subscript
20716 info. Return TRUE if this is one of those cases. */
20717 static bool
20718 fill_variable_array_bounds (tree type)
20720 if (TREE_ASM_WRITTEN (type)
20721 && TREE_CODE (type) == ARRAY_TYPE
20722 && variably_modified_type_p (type, NULL))
20724 dw_die_ref array_die = lookup_type_die (type);
20725 if (!array_die)
20726 return false;
20727 add_subscript_info (array_die, type, !is_ada ());
20728 return true;
20730 return false;
20733 /* These routines generate the internal representation of the DIE's for
20734 the compilation unit. Debugging information is collected by walking
20735 the declaration trees passed in from dwarf2out_decl(). */
20737 static void
20738 gen_array_type_die (tree type, dw_die_ref context_die)
20740 dw_die_ref array_die;
20742 /* GNU compilers represent multidimensional array types as sequences of one
20743 dimensional array types whose element types are themselves array types.
20744 We sometimes squish that down to a single array_type DIE with multiple
20745 subscripts in the Dwarf debugging info. The draft Dwarf specification
20746 say that we are allowed to do this kind of compression in C, because
20747 there is no difference between an array of arrays and a multidimensional
20748 array. We don't do this for Ada to remain as close as possible to the
20749 actual representation, which is especially important against the language
20750 flexibilty wrt arrays of variable size. */
20752 bool collapse_nested_arrays = !is_ada ();
20754 if (fill_variable_array_bounds (type))
20755 return;
20757 dw_die_ref scope_die = scope_die_for (type, context_die);
20758 tree element_type;
20760 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
20761 DW_TAG_string_type doesn't have DW_AT_type attribute). */
20762 if (TYPE_STRING_FLAG (type)
20763 && TREE_CODE (type) == ARRAY_TYPE
20764 && is_fortran ()
20765 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
20767 HOST_WIDE_INT size;
20769 array_die = new_die (DW_TAG_string_type, scope_die, type);
20770 add_name_attribute (array_die, type_tag (type));
20771 equate_type_number_to_die (type, array_die);
20772 size = int_size_in_bytes (type);
20773 if (size >= 0)
20774 add_AT_unsigned (array_die, DW_AT_byte_size, size);
20775 else if (TYPE_DOMAIN (type) != NULL_TREE
20776 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE)
20778 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
20779 tree rszdecl = szdecl;
20781 size = int_size_in_bytes (TREE_TYPE (szdecl));
20782 if (!DECL_P (szdecl))
20784 if (TREE_CODE (szdecl) == INDIRECT_REF
20785 && DECL_P (TREE_OPERAND (szdecl, 0)))
20787 rszdecl = TREE_OPERAND (szdecl, 0);
20788 if (int_size_in_bytes (TREE_TYPE (rszdecl))
20789 != DWARF2_ADDR_SIZE)
20790 size = 0;
20792 else
20793 size = 0;
20795 if (size > 0)
20797 dw_loc_list_ref loc
20798 = loc_list_from_tree (rszdecl, szdecl == rszdecl ? 2 : 0,
20799 NULL);
20800 if (loc)
20802 add_AT_location_description (array_die, DW_AT_string_length,
20803 loc);
20804 if (size != DWARF2_ADDR_SIZE)
20805 add_AT_unsigned (array_die, dwarf_version >= 5
20806 ? DW_AT_string_length_byte_size
20807 : DW_AT_byte_size, size);
20811 return;
20814 array_die = new_die (DW_TAG_array_type, scope_die, type);
20815 add_name_attribute (array_die, type_tag (type));
20816 equate_type_number_to_die (type, array_die);
20818 if (TREE_CODE (type) == VECTOR_TYPE)
20819 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
20821 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
20822 if (is_fortran ()
20823 && TREE_CODE (type) == ARRAY_TYPE
20824 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
20825 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
20826 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
20828 #if 0
20829 /* We default the array ordering. SDB will probably do
20830 the right things even if DW_AT_ordering is not present. It's not even
20831 an issue until we start to get into multidimensional arrays anyway. If
20832 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
20833 then we'll have to put the DW_AT_ordering attribute back in. (But if
20834 and when we find out that we need to put these in, we will only do so
20835 for multidimensional arrays. */
20836 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
20837 #endif
20839 if (TREE_CODE (type) == VECTOR_TYPE)
20841 /* For VECTOR_TYPEs we use an array die with appropriate bounds. */
20842 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
20843 add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node, NULL);
20844 add_bound_info (subrange_die, DW_AT_upper_bound,
20845 size_int (TYPE_VECTOR_SUBPARTS (type) - 1), NULL);
20847 else
20848 add_subscript_info (array_die, type, collapse_nested_arrays);
20850 /* Add representation of the type of the elements of this array type and
20851 emit the corresponding DIE if we haven't done it already. */
20852 element_type = TREE_TYPE (type);
20853 if (collapse_nested_arrays)
20854 while (TREE_CODE (element_type) == ARRAY_TYPE)
20856 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
20857 break;
20858 element_type = TREE_TYPE (element_type);
20861 add_type_attribute (array_die, element_type, TYPE_UNQUALIFIED,
20862 TREE_CODE (type) == ARRAY_TYPE
20863 && TYPE_REVERSE_STORAGE_ORDER (type),
20864 context_die);
20866 add_gnat_descriptive_type_attribute (array_die, type, context_die);
20867 if (TYPE_ARTIFICIAL (type))
20868 add_AT_flag (array_die, DW_AT_artificial, 1);
20870 if (get_AT (array_die, DW_AT_name))
20871 add_pubtype (type, array_die);
20873 add_alignment_attribute (array_die, type);
20876 /* This routine generates DIE for array with hidden descriptor, details
20877 are filled into *info by a langhook. */
20879 static void
20880 gen_descr_array_type_die (tree type, struct array_descr_info *info,
20881 dw_die_ref context_die)
20883 const dw_die_ref scope_die = scope_die_for (type, context_die);
20884 const dw_die_ref array_die = new_die (DW_TAG_array_type, scope_die, type);
20885 struct loc_descr_context context = { type, info->base_decl, NULL,
20886 false, false };
20887 enum dwarf_tag subrange_tag = DW_TAG_subrange_type;
20888 int dim;
20890 add_name_attribute (array_die, type_tag (type));
20891 equate_type_number_to_die (type, array_die);
20893 if (info->ndimensions > 1)
20894 switch (info->ordering)
20896 case array_descr_ordering_row_major:
20897 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
20898 break;
20899 case array_descr_ordering_column_major:
20900 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
20901 break;
20902 default:
20903 break;
20906 if (dwarf_version >= 3 || !dwarf_strict)
20908 if (info->data_location)
20909 add_scalar_info (array_die, DW_AT_data_location, info->data_location,
20910 dw_scalar_form_exprloc, &context);
20911 if (info->associated)
20912 add_scalar_info (array_die, DW_AT_associated, info->associated,
20913 dw_scalar_form_constant
20914 | dw_scalar_form_exprloc
20915 | dw_scalar_form_reference, &context);
20916 if (info->allocated)
20917 add_scalar_info (array_die, DW_AT_allocated, info->allocated,
20918 dw_scalar_form_constant
20919 | dw_scalar_form_exprloc
20920 | dw_scalar_form_reference, &context);
20921 if (info->stride)
20923 const enum dwarf_attribute attr
20924 = (info->stride_in_bits) ? DW_AT_bit_stride : DW_AT_byte_stride;
20925 const int forms
20926 = (info->stride_in_bits)
20927 ? dw_scalar_form_constant
20928 : (dw_scalar_form_constant
20929 | dw_scalar_form_exprloc
20930 | dw_scalar_form_reference);
20932 add_scalar_info (array_die, attr, info->stride, forms, &context);
20935 if (dwarf_version >= 5)
20937 if (info->rank)
20939 add_scalar_info (array_die, DW_AT_rank, info->rank,
20940 dw_scalar_form_constant
20941 | dw_scalar_form_exprloc, &context);
20942 subrange_tag = DW_TAG_generic_subrange;
20943 context.placeholder_arg = true;
20947 add_gnat_descriptive_type_attribute (array_die, type, context_die);
20949 for (dim = 0; dim < info->ndimensions; dim++)
20951 dw_die_ref subrange_die = new_die (subrange_tag, array_die, NULL);
20953 if (info->dimen[dim].bounds_type)
20954 add_type_attribute (subrange_die,
20955 info->dimen[dim].bounds_type, TYPE_UNQUALIFIED,
20956 false, context_die);
20957 if (info->dimen[dim].lower_bound)
20958 add_bound_info (subrange_die, DW_AT_lower_bound,
20959 info->dimen[dim].lower_bound, &context);
20960 if (info->dimen[dim].upper_bound)
20961 add_bound_info (subrange_die, DW_AT_upper_bound,
20962 info->dimen[dim].upper_bound, &context);
20963 if ((dwarf_version >= 3 || !dwarf_strict) && info->dimen[dim].stride)
20964 add_scalar_info (subrange_die, DW_AT_byte_stride,
20965 info->dimen[dim].stride,
20966 dw_scalar_form_constant
20967 | dw_scalar_form_exprloc
20968 | dw_scalar_form_reference,
20969 &context);
20972 gen_type_die (info->element_type, context_die);
20973 add_type_attribute (array_die, info->element_type, TYPE_UNQUALIFIED,
20974 TREE_CODE (type) == ARRAY_TYPE
20975 && TYPE_REVERSE_STORAGE_ORDER (type),
20976 context_die);
20978 if (get_AT (array_die, DW_AT_name))
20979 add_pubtype (type, array_die);
20981 add_alignment_attribute (array_die, type);
20984 #if 0
20985 static void
20986 gen_entry_point_die (tree decl, dw_die_ref context_die)
20988 tree origin = decl_ultimate_origin (decl);
20989 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
20991 if (origin != NULL)
20992 add_abstract_origin_attribute (decl_die, origin);
20993 else
20995 add_name_and_src_coords_attributes (decl_die, decl);
20996 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
20997 TYPE_UNQUALIFIED, false, context_die);
21000 if (DECL_ABSTRACT_P (decl))
21001 equate_decl_number_to_die (decl, decl_die);
21002 else
21003 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
21005 #endif
21007 /* Walk through the list of incomplete types again, trying once more to
21008 emit full debugging info for them. */
21010 static void
21011 retry_incomplete_types (void)
21013 set_early_dwarf s;
21014 int i;
21016 for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
21017 if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
21018 gen_type_die ((*incomplete_types)[i], comp_unit_die ());
21019 vec_safe_truncate (incomplete_types, 0);
21022 /* Determine what tag to use for a record type. */
21024 static enum dwarf_tag
21025 record_type_tag (tree type)
21027 if (! lang_hooks.types.classify_record)
21028 return DW_TAG_structure_type;
21030 switch (lang_hooks.types.classify_record (type))
21032 case RECORD_IS_STRUCT:
21033 return DW_TAG_structure_type;
21035 case RECORD_IS_CLASS:
21036 return DW_TAG_class_type;
21038 case RECORD_IS_INTERFACE:
21039 if (dwarf_version >= 3 || !dwarf_strict)
21040 return DW_TAG_interface_type;
21041 return DW_TAG_structure_type;
21043 default:
21044 gcc_unreachable ();
21048 /* Generate a DIE to represent an enumeration type. Note that these DIEs
21049 include all of the information about the enumeration values also. Each
21050 enumerated type name/value is listed as a child of the enumerated type
21051 DIE. */
21053 static dw_die_ref
21054 gen_enumeration_type_die (tree type, dw_die_ref context_die)
21056 dw_die_ref type_die = lookup_type_die (type);
21058 if (type_die == NULL)
21060 type_die = new_die (DW_TAG_enumeration_type,
21061 scope_die_for (type, context_die), type);
21062 equate_type_number_to_die (type, type_die);
21063 add_name_attribute (type_die, type_tag (type));
21064 if (dwarf_version >= 4 || !dwarf_strict)
21066 if (ENUM_IS_SCOPED (type))
21067 add_AT_flag (type_die, DW_AT_enum_class, 1);
21068 if (ENUM_IS_OPAQUE (type))
21069 add_AT_flag (type_die, DW_AT_declaration, 1);
21071 if (!dwarf_strict)
21072 add_AT_unsigned (type_die, DW_AT_encoding,
21073 TYPE_UNSIGNED (type)
21074 ? DW_ATE_unsigned
21075 : DW_ATE_signed);
21077 else if (! TYPE_SIZE (type))
21078 return type_die;
21079 else
21080 remove_AT (type_die, DW_AT_declaration);
21082 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
21083 given enum type is incomplete, do not generate the DW_AT_byte_size
21084 attribute or the DW_AT_element_list attribute. */
21085 if (TYPE_SIZE (type))
21087 tree link;
21089 TREE_ASM_WRITTEN (type) = 1;
21090 add_byte_size_attribute (type_die, type);
21091 add_alignment_attribute (type_die, type);
21092 if (dwarf_version >= 3 || !dwarf_strict)
21094 tree underlying = lang_hooks.types.enum_underlying_base_type (type);
21095 add_type_attribute (type_die, underlying, TYPE_UNQUALIFIED, false,
21096 context_die);
21098 if (TYPE_STUB_DECL (type) != NULL_TREE)
21100 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
21101 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
21104 /* If the first reference to this type was as the return type of an
21105 inline function, then it may not have a parent. Fix this now. */
21106 if (type_die->die_parent == NULL)
21107 add_child_die (scope_die_for (type, context_die), type_die);
21109 for (link = TYPE_VALUES (type);
21110 link != NULL; link = TREE_CHAIN (link))
21112 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
21113 tree value = TREE_VALUE (link);
21115 add_name_attribute (enum_die,
21116 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
21118 if (TREE_CODE (value) == CONST_DECL)
21119 value = DECL_INITIAL (value);
21121 if (simple_type_size_in_bits (TREE_TYPE (value))
21122 <= HOST_BITS_PER_WIDE_INT || tree_fits_shwi_p (value))
21124 /* For constant forms created by add_AT_unsigned DWARF
21125 consumers (GDB, elfutils, etc.) always zero extend
21126 the value. Only when the actual value is negative
21127 do we need to use add_AT_int to generate a constant
21128 form that can represent negative values. */
21129 HOST_WIDE_INT val = TREE_INT_CST_LOW (value);
21130 if (TYPE_UNSIGNED (TREE_TYPE (value)) || val >= 0)
21131 add_AT_unsigned (enum_die, DW_AT_const_value,
21132 (unsigned HOST_WIDE_INT) val);
21133 else
21134 add_AT_int (enum_die, DW_AT_const_value, val);
21136 else
21137 /* Enumeration constants may be wider than HOST_WIDE_INT. Handle
21138 that here. TODO: This should be re-worked to use correct
21139 signed/unsigned double tags for all cases. */
21140 add_AT_wide (enum_die, DW_AT_const_value, value);
21143 add_gnat_descriptive_type_attribute (type_die, type, context_die);
21144 if (TYPE_ARTIFICIAL (type))
21145 add_AT_flag (type_die, DW_AT_artificial, 1);
21147 else
21148 add_AT_flag (type_die, DW_AT_declaration, 1);
21150 add_alignment_attribute (type_die, type);
21152 add_pubtype (type, type_die);
21154 return type_die;
21157 /* Generate a DIE to represent either a real live formal parameter decl or to
21158 represent just the type of some formal parameter position in some function
21159 type.
21161 Note that this routine is a bit unusual because its argument may be a
21162 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
21163 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
21164 node. If it's the former then this function is being called to output a
21165 DIE to represent a formal parameter object (or some inlining thereof). If
21166 it's the latter, then this function is only being called to output a
21167 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
21168 argument type of some subprogram type.
21169 If EMIT_NAME_P is true, name and source coordinate attributes
21170 are emitted. */
21172 static dw_die_ref
21173 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
21174 dw_die_ref context_die)
21176 tree node_or_origin = node ? node : origin;
21177 tree ultimate_origin;
21178 dw_die_ref parm_die = NULL;
21180 if (TREE_CODE_CLASS (TREE_CODE (node_or_origin)) == tcc_declaration)
21182 parm_die = lookup_decl_die (node);
21184 /* If the contexts differ, we may not be talking about the same
21185 thing. */
21186 if (parm_die && parm_die->die_parent != context_die)
21188 if (!DECL_ABSTRACT_P (node))
21190 /* This can happen when creating an inlined instance, in
21191 which case we need to create a new DIE that will get
21192 annotated with DW_AT_abstract_origin. */
21193 parm_die = NULL;
21195 else
21197 /* FIXME: Reuse DIE even with a differing context.
21199 This can happen when calling
21200 dwarf2out_abstract_function to build debug info for
21201 the abstract instance of a function for which we have
21202 already generated a DIE in
21203 dwarf2out_early_global_decl.
21205 Once we remove dwarf2out_abstract_function, we should
21206 have a call to gcc_unreachable here. */
21210 if (parm_die && parm_die->die_parent == NULL)
21212 /* Check that parm_die already has the right attributes that
21213 we would have added below. If any attributes are
21214 missing, fall through to add them. */
21215 if (! DECL_ABSTRACT_P (node_or_origin)
21216 && !get_AT (parm_die, DW_AT_location)
21217 && !get_AT (parm_die, DW_AT_const_value))
21218 /* We are missing location info, and are about to add it. */
21220 else
21222 add_child_die (context_die, parm_die);
21223 return parm_die;
21228 /* If we have a previously generated DIE, use it, unless this is an
21229 concrete instance (origin != NULL), in which case we need a new
21230 DIE with a corresponding DW_AT_abstract_origin. */
21231 bool reusing_die;
21232 if (parm_die && origin == NULL)
21233 reusing_die = true;
21234 else
21236 parm_die = new_die (DW_TAG_formal_parameter, context_die, node);
21237 reusing_die = false;
21240 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
21242 case tcc_declaration:
21243 ultimate_origin = decl_ultimate_origin (node_or_origin);
21244 if (node || ultimate_origin)
21245 origin = ultimate_origin;
21247 if (reusing_die)
21248 goto add_location;
21250 if (origin != NULL)
21251 add_abstract_origin_attribute (parm_die, origin);
21252 else if (emit_name_p)
21253 add_name_and_src_coords_attributes (parm_die, node);
21254 if (origin == NULL
21255 || (! DECL_ABSTRACT_P (node_or_origin)
21256 && variably_modified_type_p (TREE_TYPE (node_or_origin),
21257 decl_function_context
21258 (node_or_origin))))
21260 tree type = TREE_TYPE (node_or_origin);
21261 if (decl_by_reference_p (node_or_origin))
21262 add_type_attribute (parm_die, TREE_TYPE (type),
21263 TYPE_UNQUALIFIED,
21264 false, context_die);
21265 else
21266 add_type_attribute (parm_die, type,
21267 decl_quals (node_or_origin),
21268 false, context_die);
21270 if (origin == NULL && DECL_ARTIFICIAL (node))
21271 add_AT_flag (parm_die, DW_AT_artificial, 1);
21272 add_location:
21273 if (node && node != origin)
21274 equate_decl_number_to_die (node, parm_die);
21275 if (! DECL_ABSTRACT_P (node_or_origin))
21276 add_location_or_const_value_attribute (parm_die, node_or_origin,
21277 node == NULL);
21279 break;
21281 case tcc_type:
21282 /* We were called with some kind of a ..._TYPE node. */
21283 add_type_attribute (parm_die, node_or_origin, TYPE_UNQUALIFIED, false,
21284 context_die);
21285 break;
21287 default:
21288 gcc_unreachable ();
21291 return parm_die;
21294 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
21295 children DW_TAG_formal_parameter DIEs representing the arguments of the
21296 parameter pack.
21298 PARM_PACK must be a function parameter pack.
21299 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
21300 must point to the subsequent arguments of the function PACK_ARG belongs to.
21301 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
21302 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
21303 following the last one for which a DIE was generated. */
21305 static dw_die_ref
21306 gen_formal_parameter_pack_die (tree parm_pack,
21307 tree pack_arg,
21308 dw_die_ref subr_die,
21309 tree *next_arg)
21311 tree arg;
21312 dw_die_ref parm_pack_die;
21314 gcc_assert (parm_pack
21315 && lang_hooks.function_parameter_pack_p (parm_pack)
21316 && subr_die);
21318 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
21319 add_src_coords_attributes (parm_pack_die, parm_pack);
21321 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
21323 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
21324 parm_pack))
21325 break;
21326 gen_formal_parameter_die (arg, NULL,
21327 false /* Don't emit name attribute. */,
21328 parm_pack_die);
21330 if (next_arg)
21331 *next_arg = arg;
21332 return parm_pack_die;
21335 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
21336 at the end of an (ANSI prototyped) formal parameters list. */
21338 static void
21339 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
21341 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
21344 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
21345 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
21346 parameters as specified in some function type specification (except for
21347 those which appear as part of a function *definition*). */
21349 static void
21350 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
21352 tree link;
21353 tree formal_type = NULL;
21354 tree first_parm_type;
21355 tree arg;
21357 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
21359 arg = DECL_ARGUMENTS (function_or_method_type);
21360 function_or_method_type = TREE_TYPE (function_or_method_type);
21362 else
21363 arg = NULL_TREE;
21365 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
21367 /* Make our first pass over the list of formal parameter types and output a
21368 DW_TAG_formal_parameter DIE for each one. */
21369 for (link = first_parm_type; link; )
21371 dw_die_ref parm_die;
21373 formal_type = TREE_VALUE (link);
21374 if (formal_type == void_type_node)
21375 break;
21377 /* Output a (nameless) DIE to represent the formal parameter itself. */
21378 if (!POINTER_BOUNDS_TYPE_P (formal_type))
21380 parm_die = gen_formal_parameter_die (formal_type, NULL,
21381 true /* Emit name attribute. */,
21382 context_die);
21383 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
21384 && link == first_parm_type)
21386 add_AT_flag (parm_die, DW_AT_artificial, 1);
21387 if (dwarf_version >= 3 || !dwarf_strict)
21388 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
21390 else if (arg && DECL_ARTIFICIAL (arg))
21391 add_AT_flag (parm_die, DW_AT_artificial, 1);
21394 link = TREE_CHAIN (link);
21395 if (arg)
21396 arg = DECL_CHAIN (arg);
21399 /* If this function type has an ellipsis, add a
21400 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
21401 if (formal_type != void_type_node)
21402 gen_unspecified_parameters_die (function_or_method_type, context_die);
21404 /* Make our second (and final) pass over the list of formal parameter types
21405 and output DIEs to represent those types (as necessary). */
21406 for (link = TYPE_ARG_TYPES (function_or_method_type);
21407 link && TREE_VALUE (link);
21408 link = TREE_CHAIN (link))
21409 gen_type_die (TREE_VALUE (link), context_die);
21412 /* We want to generate the DIE for TYPE so that we can generate the
21413 die for MEMBER, which has been defined; we will need to refer back
21414 to the member declaration nested within TYPE. If we're trying to
21415 generate minimal debug info for TYPE, processing TYPE won't do the
21416 trick; we need to attach the member declaration by hand. */
21418 static void
21419 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
21421 gen_type_die (type, context_die);
21423 /* If we're trying to avoid duplicate debug info, we may not have
21424 emitted the member decl for this function. Emit it now. */
21425 if (TYPE_STUB_DECL (type)
21426 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
21427 && ! lookup_decl_die (member))
21429 dw_die_ref type_die;
21430 gcc_assert (!decl_ultimate_origin (member));
21432 push_decl_scope (type);
21433 type_die = lookup_type_die_strip_naming_typedef (type);
21434 if (TREE_CODE (member) == FUNCTION_DECL)
21435 gen_subprogram_die (member, type_die);
21436 else if (TREE_CODE (member) == FIELD_DECL)
21438 /* Ignore the nameless fields that are used to skip bits but handle
21439 C++ anonymous unions and structs. */
21440 if (DECL_NAME (member) != NULL_TREE
21441 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
21442 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
21444 struct vlr_context vlr_ctx = {
21445 DECL_CONTEXT (member), /* struct_type */
21446 NULL_TREE /* variant_part_offset */
21448 gen_type_die (member_declared_type (member), type_die);
21449 gen_field_die (member, &vlr_ctx, type_die);
21452 else
21453 gen_variable_die (member, NULL_TREE, type_die);
21455 pop_decl_scope ();
21459 /* Forward declare these functions, because they are mutually recursive
21460 with their set_block_* pairing functions. */
21461 static void set_decl_origin_self (tree);
21462 static void set_decl_abstract_flags (tree, vec<tree> &);
21464 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
21465 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
21466 that it points to the node itself, thus indicating that the node is its
21467 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
21468 the given node is NULL, recursively descend the decl/block tree which
21469 it is the root of, and for each other ..._DECL or BLOCK node contained
21470 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
21471 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
21472 values to point to themselves. */
21474 static void
21475 set_block_origin_self (tree stmt)
21477 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
21479 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
21482 tree local_decl;
21484 for (local_decl = BLOCK_VARS (stmt);
21485 local_decl != NULL_TREE;
21486 local_decl = DECL_CHAIN (local_decl))
21487 /* Do not recurse on nested functions since the inlining status
21488 of parent and child can be different as per the DWARF spec. */
21489 if (TREE_CODE (local_decl) != FUNCTION_DECL
21490 && !DECL_EXTERNAL (local_decl))
21491 set_decl_origin_self (local_decl);
21495 tree subblock;
21497 for (subblock = BLOCK_SUBBLOCKS (stmt);
21498 subblock != NULL_TREE;
21499 subblock = BLOCK_CHAIN (subblock))
21500 set_block_origin_self (subblock); /* Recurse. */
21505 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
21506 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
21507 node to so that it points to the node itself, thus indicating that the
21508 node represents its own (abstract) origin. Additionally, if the
21509 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
21510 the decl/block tree of which the given node is the root of, and for
21511 each other ..._DECL or BLOCK node contained therein whose
21512 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
21513 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
21514 point to themselves. */
21516 static void
21517 set_decl_origin_self (tree decl)
21519 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
21521 DECL_ABSTRACT_ORIGIN (decl) = decl;
21522 if (TREE_CODE (decl) == FUNCTION_DECL)
21524 tree arg;
21526 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
21527 DECL_ABSTRACT_ORIGIN (arg) = arg;
21528 if (DECL_INITIAL (decl) != NULL_TREE
21529 && DECL_INITIAL (decl) != error_mark_node)
21530 set_block_origin_self (DECL_INITIAL (decl));
21535 /* Given a pointer to some BLOCK node, set the BLOCK_ABSTRACT flag to 1
21536 and if it wasn't 1 before, push it to abstract_vec vector.
21537 For all local decls and all local sub-blocks (recursively) do it
21538 too. */
21540 static void
21541 set_block_abstract_flags (tree stmt, vec<tree> &abstract_vec)
21543 tree local_decl;
21544 tree subblock;
21545 unsigned int i;
21547 if (!BLOCK_ABSTRACT (stmt))
21549 abstract_vec.safe_push (stmt);
21550 BLOCK_ABSTRACT (stmt) = 1;
21553 for (local_decl = BLOCK_VARS (stmt);
21554 local_decl != NULL_TREE;
21555 local_decl = DECL_CHAIN (local_decl))
21556 if (! DECL_EXTERNAL (local_decl))
21557 set_decl_abstract_flags (local_decl, abstract_vec);
21559 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
21561 local_decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
21562 if ((VAR_P (local_decl) && !TREE_STATIC (local_decl))
21563 || TREE_CODE (local_decl) == PARM_DECL)
21564 set_decl_abstract_flags (local_decl, abstract_vec);
21567 for (subblock = BLOCK_SUBBLOCKS (stmt);
21568 subblock != NULL_TREE;
21569 subblock = BLOCK_CHAIN (subblock))
21570 set_block_abstract_flags (subblock, abstract_vec);
21573 /* Given a pointer to some ..._DECL node, set DECL_ABSTRACT_P flag on it
21574 to 1 and if it wasn't 1 before, push to abstract_vec vector.
21575 In the case where the decl is a FUNCTION_DECL also set the abstract
21576 flags for all of the parameters, local vars, local
21577 blocks and sub-blocks (recursively). */
21579 static void
21580 set_decl_abstract_flags (tree decl, vec<tree> &abstract_vec)
21582 if (!DECL_ABSTRACT_P (decl))
21584 abstract_vec.safe_push (decl);
21585 DECL_ABSTRACT_P (decl) = 1;
21588 if (TREE_CODE (decl) == FUNCTION_DECL)
21590 tree arg;
21592 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
21593 if (!DECL_ABSTRACT_P (arg))
21595 abstract_vec.safe_push (arg);
21596 DECL_ABSTRACT_P (arg) = 1;
21598 if (DECL_INITIAL (decl) != NULL_TREE
21599 && DECL_INITIAL (decl) != error_mark_node)
21600 set_block_abstract_flags (DECL_INITIAL (decl), abstract_vec);
21604 /* Generate the DWARF2 info for the "abstract" instance of a function which we
21605 may later generate inlined and/or out-of-line instances of.
21607 FIXME: In the early-dwarf world, this function, and most of the
21608 DECL_ABSTRACT code should be obsoleted. The early DIE _is_
21609 the abstract instance. All we would need to do is annotate
21610 the early DIE with the appropriate DW_AT_inline in late
21611 dwarf (perhaps in gen_inlined_subroutine_die).
21613 However, we can't do this yet, because LTO streaming of DIEs
21614 has not been implemented yet. */
21616 static void
21617 dwarf2out_abstract_function (tree decl)
21619 dw_die_ref old_die;
21620 tree save_fn;
21621 tree context;
21622 hash_table<decl_loc_hasher> *old_decl_loc_table;
21623 hash_table<dw_loc_list_hasher> *old_cached_dw_loc_list_table;
21624 int old_call_site_count, old_tail_call_site_count;
21625 struct call_arg_loc_node *old_call_arg_locations;
21627 /* Make sure we have the actual abstract inline, not a clone. */
21628 decl = DECL_ORIGIN (decl);
21630 old_die = lookup_decl_die (decl);
21631 if (old_die && get_AT (old_die, DW_AT_inline))
21632 /* We've already generated the abstract instance. */
21633 return;
21635 /* We can be called while recursively when seeing block defining inlined subroutine
21636 DIE. Be sure to not clobber the outer location table nor use it or we would
21637 get locations in abstract instantces. */
21638 old_decl_loc_table = decl_loc_table;
21639 decl_loc_table = NULL;
21640 old_cached_dw_loc_list_table = cached_dw_loc_list_table;
21641 cached_dw_loc_list_table = NULL;
21642 old_call_arg_locations = call_arg_locations;
21643 call_arg_locations = NULL;
21644 old_call_site_count = call_site_count;
21645 call_site_count = -1;
21646 old_tail_call_site_count = tail_call_site_count;
21647 tail_call_site_count = -1;
21649 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
21650 we don't get confused by DECL_ABSTRACT_P. */
21651 if (debug_info_level > DINFO_LEVEL_TERSE)
21653 context = decl_class_context (decl);
21654 if (context)
21655 gen_type_die_for_member
21656 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
21659 /* Pretend we've just finished compiling this function. */
21660 save_fn = current_function_decl;
21661 current_function_decl = decl;
21663 auto_vec<tree, 64> abstract_vec;
21664 set_decl_abstract_flags (decl, abstract_vec);
21665 dwarf2out_decl (decl);
21666 unsigned int i;
21667 tree t;
21668 FOR_EACH_VEC_ELT (abstract_vec, i, t)
21669 if (TREE_CODE (t) == BLOCK)
21670 BLOCK_ABSTRACT (t) = 0;
21671 else
21672 DECL_ABSTRACT_P (t) = 0;
21674 current_function_decl = save_fn;
21675 decl_loc_table = old_decl_loc_table;
21676 cached_dw_loc_list_table = old_cached_dw_loc_list_table;
21677 call_arg_locations = old_call_arg_locations;
21678 call_site_count = old_call_site_count;
21679 tail_call_site_count = old_tail_call_site_count;
21682 /* Helper function of premark_used_types() which gets called through
21683 htab_traverse.
21685 Marks the DIE of a given type in *SLOT as perennial, so it never gets
21686 marked as unused by prune_unused_types. */
21688 bool
21689 premark_used_types_helper (tree const &type, void *)
21691 dw_die_ref die;
21693 die = lookup_type_die (type);
21694 if (die != NULL)
21695 die->die_perennial_p = 1;
21696 return true;
21699 /* Helper function of premark_types_used_by_global_vars which gets called
21700 through htab_traverse.
21702 Marks the DIE of a given type in *SLOT as perennial, so it never gets
21703 marked as unused by prune_unused_types. The DIE of the type is marked
21704 only if the global variable using the type will actually be emitted. */
21707 premark_types_used_by_global_vars_helper (types_used_by_vars_entry **slot,
21708 void *)
21710 struct types_used_by_vars_entry *entry;
21711 dw_die_ref die;
21713 entry = (struct types_used_by_vars_entry *) *slot;
21714 gcc_assert (entry->type != NULL
21715 && entry->var_decl != NULL);
21716 die = lookup_type_die (entry->type);
21717 if (die)
21719 /* Ask cgraph if the global variable really is to be emitted.
21720 If yes, then we'll keep the DIE of ENTRY->TYPE. */
21721 varpool_node *node = varpool_node::get (entry->var_decl);
21722 if (node && node->definition)
21724 die->die_perennial_p = 1;
21725 /* Keep the parent DIEs as well. */
21726 while ((die = die->die_parent) && die->die_perennial_p == 0)
21727 die->die_perennial_p = 1;
21730 return 1;
21733 /* Mark all members of used_types_hash as perennial. */
21735 static void
21736 premark_used_types (struct function *fun)
21738 if (fun && fun->used_types_hash)
21739 fun->used_types_hash->traverse<void *, premark_used_types_helper> (NULL);
21742 /* Mark all members of types_used_by_vars_entry as perennial. */
21744 static void
21745 premark_types_used_by_global_vars (void)
21747 if (types_used_by_vars_hash)
21748 types_used_by_vars_hash
21749 ->traverse<void *, premark_types_used_by_global_vars_helper> (NULL);
21752 /* Generate a DW_TAG_call_site DIE in function DECL under SUBR_DIE
21753 for CA_LOC call arg loc node. */
21755 static dw_die_ref
21756 gen_call_site_die (tree decl, dw_die_ref subr_die,
21757 struct call_arg_loc_node *ca_loc)
21759 dw_die_ref stmt_die = NULL, die;
21760 tree block = ca_loc->block;
21762 while (block
21763 && block != DECL_INITIAL (decl)
21764 && TREE_CODE (block) == BLOCK)
21766 stmt_die = BLOCK_DIE (block);
21767 if (stmt_die)
21768 break;
21769 block = BLOCK_SUPERCONTEXT (block);
21771 if (stmt_die == NULL)
21772 stmt_die = subr_die;
21773 die = new_die (dwarf_TAG (DW_TAG_call_site), stmt_die, NULL_TREE);
21774 add_AT_lbl_id (die, dwarf_AT (DW_AT_call_return_pc), ca_loc->label);
21775 if (ca_loc->tail_call_p)
21776 add_AT_flag (die, dwarf_AT (DW_AT_call_tail_call), 1);
21777 if (ca_loc->symbol_ref)
21779 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
21780 if (tdie)
21781 add_AT_die_ref (die, dwarf_AT (DW_AT_call_origin), tdie);
21782 else
21783 add_AT_addr (die, dwarf_AT (DW_AT_call_origin), ca_loc->symbol_ref,
21784 false);
21786 return die;
21789 /* Generate a DIE to represent a declared function (either file-scope or
21790 block-local). */
21792 static void
21793 gen_subprogram_die (tree decl, dw_die_ref context_die)
21795 tree origin = decl_ultimate_origin (decl);
21796 dw_die_ref subr_die;
21797 dw_die_ref old_die = lookup_decl_die (decl);
21799 /* This function gets called multiple times for different stages of
21800 the debug process. For example, for func() in this code:
21802 namespace S
21804 void func() { ... }
21807 ...we get called 4 times. Twice in early debug and twice in
21808 late debug:
21810 Early debug
21811 -----------
21813 1. Once while generating func() within the namespace. This is
21814 the declaration. The declaration bit below is set, as the
21815 context is the namespace.
21817 A new DIE will be generated with DW_AT_declaration set.
21819 2. Once for func() itself. This is the specification. The
21820 declaration bit below is clear as the context is the CU.
21822 We will use the cached DIE from (1) to create a new DIE with
21823 DW_AT_specification pointing to the declaration in (1).
21825 Late debug via rest_of_handle_final()
21826 -------------------------------------
21828 3. Once generating func() within the namespace. This is also the
21829 declaration, as in (1), but this time we will early exit below
21830 as we have a cached DIE and a declaration needs no additional
21831 annotations (no locations), as the source declaration line
21832 info is enough.
21834 4. Once for func() itself. As in (2), this is the specification,
21835 but this time we will re-use the cached DIE, and just annotate
21836 it with the location information that should now be available.
21838 For something without namespaces, but with abstract instances, we
21839 are also called a multiple times:
21841 class Base
21843 public:
21844 Base (); // constructor declaration (1)
21847 Base::Base () { } // constructor specification (2)
21849 Early debug
21850 -----------
21852 1. Once for the Base() constructor by virtue of it being a
21853 member of the Base class. This is done via
21854 rest_of_type_compilation.
21856 This is a declaration, so a new DIE will be created with
21857 DW_AT_declaration.
21859 2. Once for the Base() constructor definition, but this time
21860 while generating the abstract instance of the base
21861 constructor (__base_ctor) which is being generated via early
21862 debug of reachable functions.
21864 Even though we have a cached version of the declaration (1),
21865 we will create a DW_AT_specification of the declaration DIE
21866 in (1).
21868 3. Once for the __base_ctor itself, but this time, we generate
21869 an DW_AT_abstract_origin version of the DW_AT_specification in
21870 (2).
21872 Late debug via rest_of_handle_final
21873 -----------------------------------
21875 4. One final time for the __base_ctor (which will have a cached
21876 DIE with DW_AT_abstract_origin created in (3). This time,
21877 we will just annotate the location information now
21878 available.
21880 int declaration = (current_function_decl != decl
21881 || class_or_namespace_scope_p (context_die));
21883 /* Now that the C++ front end lazily declares artificial member fns, we
21884 might need to retrofit the declaration into its class. */
21885 if (!declaration && !origin && !old_die
21886 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
21887 && !class_or_namespace_scope_p (context_die)
21888 && debug_info_level > DINFO_LEVEL_TERSE)
21889 old_die = force_decl_die (decl);
21891 /* An inlined instance, tag a new DIE with DW_AT_abstract_origin. */
21892 if (origin != NULL)
21894 gcc_assert (!declaration || local_scope_p (context_die));
21896 /* Fixup die_parent for the abstract instance of a nested
21897 inline function. */
21898 if (old_die && old_die->die_parent == NULL)
21899 add_child_die (context_die, old_die);
21901 if (old_die && get_AT_ref (old_die, DW_AT_abstract_origin))
21903 /* If we have a DW_AT_abstract_origin we have a working
21904 cached version. */
21905 subr_die = old_die;
21907 else
21909 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
21910 add_abstract_origin_attribute (subr_die, origin);
21911 /* This is where the actual code for a cloned function is.
21912 Let's emit linkage name attribute for it. This helps
21913 debuggers to e.g, set breakpoints into
21914 constructors/destructors when the user asks "break
21915 K::K". */
21916 add_linkage_name (subr_die, decl);
21919 /* A cached copy, possibly from early dwarf generation. Reuse as
21920 much as possible. */
21921 else if (old_die)
21923 /* A declaration that has been previously dumped needs no
21924 additional information. */
21925 if (declaration)
21926 return;
21928 if (!get_AT_flag (old_die, DW_AT_declaration)
21929 /* We can have a normal definition following an inline one in the
21930 case of redefinition of GNU C extern inlines.
21931 It seems reasonable to use AT_specification in this case. */
21932 && !get_AT (old_die, DW_AT_inline))
21934 /* Detect and ignore this case, where we are trying to output
21935 something we have already output. */
21936 if (get_AT (old_die, DW_AT_low_pc)
21937 || get_AT (old_die, DW_AT_ranges))
21938 return;
21940 /* If we have no location information, this must be a
21941 partially generated DIE from early dwarf generation.
21942 Fall through and generate it. */
21945 /* If the definition comes from the same place as the declaration,
21946 maybe use the old DIE. We always want the DIE for this function
21947 that has the *_pc attributes to be under comp_unit_die so the
21948 debugger can find it. We also need to do this for abstract
21949 instances of inlines, since the spec requires the out-of-line copy
21950 to have the same parent. For local class methods, this doesn't
21951 apply; we just use the old DIE. */
21952 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
21953 struct dwarf_file_data * file_index = lookup_filename (s.file);
21954 if ((is_cu_die (old_die->die_parent)
21955 /* This condition fixes the inconsistency/ICE with the
21956 following Fortran test (or some derivative thereof) while
21957 building libgfortran:
21959 module some_m
21960 contains
21961 logical function funky (FLAG)
21962 funky = .true.
21963 end function
21964 end module
21966 || (old_die->die_parent
21967 && old_die->die_parent->die_tag == DW_TAG_module)
21968 || context_die == NULL)
21969 && (DECL_ARTIFICIAL (decl)
21970 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
21971 && (get_AT_unsigned (old_die, DW_AT_decl_line)
21972 == (unsigned) s.line)
21973 && (!debug_column_info
21974 || s.column == 0
21975 || (get_AT_unsigned (old_die, DW_AT_decl_column)
21976 == (unsigned) s.column)))))
21978 subr_die = old_die;
21980 /* Clear out the declaration attribute, but leave the
21981 parameters so they can be augmented with location
21982 information later. Unless this was a declaration, in
21983 which case, wipe out the nameless parameters and recreate
21984 them further down. */
21985 if (remove_AT (subr_die, DW_AT_declaration))
21988 remove_AT (subr_die, DW_AT_object_pointer);
21989 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
21992 /* Make a specification pointing to the previously built
21993 declaration. */
21994 else
21996 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
21997 add_AT_specification (subr_die, old_die);
21998 add_pubname (decl, subr_die);
21999 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
22000 add_AT_file (subr_die, DW_AT_decl_file, file_index);
22001 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
22002 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
22003 if (debug_column_info
22004 && s.column
22005 && (get_AT_unsigned (old_die, DW_AT_decl_column)
22006 != (unsigned) s.column))
22007 add_AT_unsigned (subr_die, DW_AT_decl_column, s.column);
22009 /* If the prototype had an 'auto' or 'decltype(auto)' return type,
22010 emit the real type on the definition die. */
22011 if (is_cxx () && debug_info_level > DINFO_LEVEL_TERSE)
22013 dw_die_ref die = get_AT_ref (old_die, DW_AT_type);
22014 if (die == auto_die || die == decltype_auto_die)
22015 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
22016 TYPE_UNQUALIFIED, false, context_die);
22019 /* When we process the method declaration, we haven't seen
22020 the out-of-class defaulted definition yet, so we have to
22021 recheck now. */
22022 if ((dwarf_version >= 5 || ! dwarf_strict)
22023 && !get_AT (subr_die, DW_AT_defaulted))
22025 int defaulted
22026 = lang_hooks.decls.decl_dwarf_attribute (decl,
22027 DW_AT_defaulted);
22028 if (defaulted != -1)
22030 /* Other values must have been handled before. */
22031 gcc_assert (defaulted == DW_DEFAULTED_out_of_class);
22032 add_AT_unsigned (subr_die, DW_AT_defaulted, defaulted);
22037 /* Create a fresh DIE for anything else. */
22038 else
22040 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
22042 if (TREE_PUBLIC (decl))
22043 add_AT_flag (subr_die, DW_AT_external, 1);
22045 add_name_and_src_coords_attributes (subr_die, decl);
22046 add_pubname (decl, subr_die);
22047 if (debug_info_level > DINFO_LEVEL_TERSE)
22049 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
22050 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
22051 TYPE_UNQUALIFIED, false, context_die);
22054 add_pure_or_virtual_attribute (subr_die, decl);
22055 if (DECL_ARTIFICIAL (decl))
22056 add_AT_flag (subr_die, DW_AT_artificial, 1);
22058 if (TREE_THIS_VOLATILE (decl) && (dwarf_version >= 5 || !dwarf_strict))
22059 add_AT_flag (subr_die, DW_AT_noreturn, 1);
22061 add_alignment_attribute (subr_die, decl);
22063 add_accessibility_attribute (subr_die, decl);
22066 /* Unless we have an existing non-declaration DIE, equate the new
22067 DIE. */
22068 if (!old_die || is_declaration_die (old_die))
22069 equate_decl_number_to_die (decl, subr_die);
22071 if (declaration)
22073 if (!old_die || !get_AT (old_die, DW_AT_inline))
22075 add_AT_flag (subr_die, DW_AT_declaration, 1);
22077 /* If this is an explicit function declaration then generate
22078 a DW_AT_explicit attribute. */
22079 if ((dwarf_version >= 3 || !dwarf_strict)
22080 && lang_hooks.decls.decl_dwarf_attribute (decl,
22081 DW_AT_explicit) == 1)
22082 add_AT_flag (subr_die, DW_AT_explicit, 1);
22084 /* If this is a C++11 deleted special function member then generate
22085 a DW_AT_deleted attribute. */
22086 if ((dwarf_version >= 5 || !dwarf_strict)
22087 && lang_hooks.decls.decl_dwarf_attribute (decl,
22088 DW_AT_deleted) == 1)
22089 add_AT_flag (subr_die, DW_AT_deleted, 1);
22091 /* If this is a C++11 defaulted special function member then
22092 generate a DW_AT_defaulted attribute. */
22093 if (dwarf_version >= 5 || !dwarf_strict)
22095 int defaulted
22096 = lang_hooks.decls.decl_dwarf_attribute (decl,
22097 DW_AT_defaulted);
22098 if (defaulted != -1)
22099 add_AT_unsigned (subr_die, DW_AT_defaulted, defaulted);
22102 /* If this is a C++11 non-static member function with & ref-qualifier
22103 then generate a DW_AT_reference attribute. */
22104 if ((dwarf_version >= 5 || !dwarf_strict)
22105 && lang_hooks.decls.decl_dwarf_attribute (decl,
22106 DW_AT_reference) == 1)
22107 add_AT_flag (subr_die, DW_AT_reference, 1);
22109 /* If this is a C++11 non-static member function with &&
22110 ref-qualifier then generate a DW_AT_reference attribute. */
22111 if ((dwarf_version >= 5 || !dwarf_strict)
22112 && lang_hooks.decls.decl_dwarf_attribute (decl,
22113 DW_AT_rvalue_reference)
22114 == 1)
22115 add_AT_flag (subr_die, DW_AT_rvalue_reference, 1);
22118 /* Tag abstract instances with DW_AT_inline. */
22119 else if (DECL_ABSTRACT_P (decl))
22121 if (DECL_DECLARED_INLINE_P (decl))
22123 if (cgraph_function_possibly_inlined_p (decl))
22124 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
22125 else
22126 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
22128 else
22130 if (cgraph_function_possibly_inlined_p (decl))
22131 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
22132 else
22133 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
22136 if (DECL_DECLARED_INLINE_P (decl)
22137 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
22138 add_AT_flag (subr_die, DW_AT_artificial, 1);
22140 /* For non DECL_EXTERNALs, if range information is available, fill
22141 the DIE with it. */
22142 else if (!DECL_EXTERNAL (decl) && !early_dwarf)
22144 HOST_WIDE_INT cfa_fb_offset;
22146 struct function *fun = DECL_STRUCT_FUNCTION (decl);
22148 if (!flag_reorder_blocks_and_partition)
22150 dw_fde_ref fde = fun->fde;
22151 if (fde->dw_fde_begin)
22153 /* We have already generated the labels. */
22154 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
22155 fde->dw_fde_end, false);
22157 else
22159 /* Create start/end labels and add the range. */
22160 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
22161 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
22162 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
22163 current_function_funcdef_no);
22164 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
22165 current_function_funcdef_no);
22166 add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
22167 false);
22170 #if VMS_DEBUGGING_INFO
22171 /* HP OpenVMS Industry Standard 64: DWARF Extensions
22172 Section 2.3 Prologue and Epilogue Attributes:
22173 When a breakpoint is set on entry to a function, it is generally
22174 desirable for execution to be suspended, not on the very first
22175 instruction of the function, but rather at a point after the
22176 function's frame has been set up, after any language defined local
22177 declaration processing has been completed, and before execution of
22178 the first statement of the function begins. Debuggers generally
22179 cannot properly determine where this point is. Similarly for a
22180 breakpoint set on exit from a function. The prologue and epilogue
22181 attributes allow a compiler to communicate the location(s) to use. */
22184 if (fde->dw_fde_vms_end_prologue)
22185 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
22186 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
22188 if (fde->dw_fde_vms_begin_epilogue)
22189 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
22190 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
22192 #endif
22195 else
22197 /* Generate pubnames entries for the split function code ranges. */
22198 dw_fde_ref fde = fun->fde;
22200 if (fde->dw_fde_second_begin)
22202 if (dwarf_version >= 3 || !dwarf_strict)
22204 /* We should use ranges for non-contiguous code section
22205 addresses. Use the actual code range for the initial
22206 section, since the HOT/COLD labels might precede an
22207 alignment offset. */
22208 bool range_list_added = false;
22209 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
22210 fde->dw_fde_end, &range_list_added,
22211 false);
22212 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
22213 fde->dw_fde_second_end,
22214 &range_list_added, false);
22215 if (range_list_added)
22216 add_ranges (NULL);
22218 else
22220 /* There is no real support in DW2 for this .. so we make
22221 a work-around. First, emit the pub name for the segment
22222 containing the function label. Then make and emit a
22223 simplified subprogram DIE for the second segment with the
22224 name pre-fixed by __hot/cold_sect_of_. We use the same
22225 linkage name for the second die so that gdb will find both
22226 sections when given "b foo". */
22227 const char *name = NULL;
22228 tree decl_name = DECL_NAME (decl);
22229 dw_die_ref seg_die;
22231 /* Do the 'primary' section. */
22232 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
22233 fde->dw_fde_end, false);
22235 /* Build a minimal DIE for the secondary section. */
22236 seg_die = new_die (DW_TAG_subprogram,
22237 subr_die->die_parent, decl);
22239 if (TREE_PUBLIC (decl))
22240 add_AT_flag (seg_die, DW_AT_external, 1);
22242 if (decl_name != NULL
22243 && IDENTIFIER_POINTER (decl_name) != NULL)
22245 name = dwarf2_name (decl, 1);
22246 if (! DECL_ARTIFICIAL (decl))
22247 add_src_coords_attributes (seg_die, decl);
22249 add_linkage_name (seg_die, decl);
22251 gcc_assert (name != NULL);
22252 add_pure_or_virtual_attribute (seg_die, decl);
22253 if (DECL_ARTIFICIAL (decl))
22254 add_AT_flag (seg_die, DW_AT_artificial, 1);
22256 name = concat ("__second_sect_of_", name, NULL);
22257 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
22258 fde->dw_fde_second_end, false);
22259 add_name_attribute (seg_die, name);
22260 if (want_pubnames ())
22261 add_pubname_string (name, seg_die);
22264 else
22265 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
22266 false);
22269 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
22271 /* We define the "frame base" as the function's CFA. This is more
22272 convenient for several reasons: (1) It's stable across the prologue
22273 and epilogue, which makes it better than just a frame pointer,
22274 (2) With dwarf3, there exists a one-byte encoding that allows us
22275 to reference the .debug_frame data by proxy, but failing that,
22276 (3) We can at least reuse the code inspection and interpretation
22277 code that determines the CFA position at various points in the
22278 function. */
22279 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
22281 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
22282 add_AT_loc (subr_die, DW_AT_frame_base, op);
22284 else
22286 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
22287 if (list->dw_loc_next)
22288 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
22289 else
22290 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
22293 /* Compute a displacement from the "steady-state frame pointer" to
22294 the CFA. The former is what all stack slots and argument slots
22295 will reference in the rtl; the latter is what we've told the
22296 debugger about. We'll need to adjust all frame_base references
22297 by this displacement. */
22298 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
22300 if (fun->static_chain_decl)
22302 /* DWARF requires here a location expression that computes the
22303 address of the enclosing subprogram's frame base. The machinery
22304 in tree-nested.c is supposed to store this specific address in the
22305 last field of the FRAME record. */
22306 const tree frame_type
22307 = TREE_TYPE (TREE_TYPE (fun->static_chain_decl));
22308 const tree fb_decl = tree_last (TYPE_FIELDS (frame_type));
22310 tree fb_expr
22311 = build1 (INDIRECT_REF, frame_type, fun->static_chain_decl);
22312 fb_expr = build3 (COMPONENT_REF, TREE_TYPE (fb_decl),
22313 fb_expr, fb_decl, NULL_TREE);
22315 add_AT_location_description (subr_die, DW_AT_static_link,
22316 loc_list_from_tree (fb_expr, 0, NULL));
22319 resolve_variable_values ();
22322 /* Generate child dies for template paramaters. */
22323 if (early_dwarf && debug_info_level > DINFO_LEVEL_TERSE)
22324 gen_generic_params_dies (decl);
22326 /* Now output descriptions of the arguments for this function. This gets
22327 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
22328 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
22329 `...' at the end of the formal parameter list. In order to find out if
22330 there was a trailing ellipsis or not, we must instead look at the type
22331 associated with the FUNCTION_DECL. This will be a node of type
22332 FUNCTION_TYPE. If the chain of type nodes hanging off of this
22333 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
22334 an ellipsis at the end. */
22336 /* In the case where we are describing a mere function declaration, all we
22337 need to do here (and all we *can* do here) is to describe the *types* of
22338 its formal parameters. */
22339 if (debug_info_level <= DINFO_LEVEL_TERSE)
22341 else if (declaration)
22342 gen_formal_types_die (decl, subr_die);
22343 else
22345 /* Generate DIEs to represent all known formal parameters. */
22346 tree parm = DECL_ARGUMENTS (decl);
22347 tree generic_decl = early_dwarf
22348 ? lang_hooks.decls.get_generic_function_decl (decl) : NULL;
22349 tree generic_decl_parm = generic_decl
22350 ? DECL_ARGUMENTS (generic_decl)
22351 : NULL;
22353 /* Now we want to walk the list of parameters of the function and
22354 emit their relevant DIEs.
22356 We consider the case of DECL being an instance of a generic function
22357 as well as it being a normal function.
22359 If DECL is an instance of a generic function we walk the
22360 parameters of the generic function declaration _and_ the parameters of
22361 DECL itself. This is useful because we want to emit specific DIEs for
22362 function parameter packs and those are declared as part of the
22363 generic function declaration. In that particular case,
22364 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
22365 That DIE has children DIEs representing the set of arguments
22366 of the pack. Note that the set of pack arguments can be empty.
22367 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
22368 children DIE.
22370 Otherwise, we just consider the parameters of DECL. */
22371 while (generic_decl_parm || parm)
22373 if (generic_decl_parm
22374 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
22375 gen_formal_parameter_pack_die (generic_decl_parm,
22376 parm, subr_die,
22377 &parm);
22378 else if (parm && !POINTER_BOUNDS_P (parm))
22380 dw_die_ref parm_die = gen_decl_die (parm, NULL, NULL, subr_die);
22382 if (parm == DECL_ARGUMENTS (decl)
22383 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
22384 && parm_die
22385 && (dwarf_version >= 3 || !dwarf_strict))
22386 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
22388 parm = DECL_CHAIN (parm);
22390 else if (parm)
22391 parm = DECL_CHAIN (parm);
22393 if (generic_decl_parm)
22394 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
22397 /* Decide whether we need an unspecified_parameters DIE at the end.
22398 There are 2 more cases to do this for: 1) the ansi ... declaration -
22399 this is detectable when the end of the arg list is not a
22400 void_type_node 2) an unprototyped function declaration (not a
22401 definition). This just means that we have no info about the
22402 parameters at all. */
22403 if (early_dwarf)
22405 if (prototype_p (TREE_TYPE (decl)))
22407 /* This is the prototyped case, check for.... */
22408 if (stdarg_p (TREE_TYPE (decl)))
22409 gen_unspecified_parameters_die (decl, subr_die);
22411 else if (DECL_INITIAL (decl) == NULL_TREE)
22412 gen_unspecified_parameters_die (decl, subr_die);
22416 if (subr_die != old_die)
22417 /* Add the calling convention attribute if requested. */
22418 add_calling_convention_attribute (subr_die, decl);
22420 /* Output Dwarf info for all of the stuff within the body of the function
22421 (if it has one - it may be just a declaration).
22423 OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
22424 a function. This BLOCK actually represents the outermost binding contour
22425 for the function, i.e. the contour in which the function's formal
22426 parameters and labels get declared. Curiously, it appears that the front
22427 end doesn't actually put the PARM_DECL nodes for the current function onto
22428 the BLOCK_VARS list for this outer scope, but are strung off of the
22429 DECL_ARGUMENTS list for the function instead.
22431 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
22432 the LABEL_DECL nodes for the function however, and we output DWARF info
22433 for those in decls_for_scope. Just within the `outer_scope' there will be
22434 a BLOCK node representing the function's outermost pair of curly braces,
22435 and any blocks used for the base and member initializers of a C++
22436 constructor function. */
22437 tree outer_scope = DECL_INITIAL (decl);
22438 if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
22440 int call_site_note_count = 0;
22441 int tail_call_site_note_count = 0;
22443 /* Emit a DW_TAG_variable DIE for a named return value. */
22444 if (DECL_NAME (DECL_RESULT (decl)))
22445 gen_decl_die (DECL_RESULT (decl), NULL, NULL, subr_die);
22447 /* The first time through decls_for_scope we will generate the
22448 DIEs for the locals. The second time, we fill in the
22449 location info. */
22450 decls_for_scope (outer_scope, subr_die);
22452 if (call_arg_locations && (!dwarf_strict || dwarf_version >= 5))
22454 struct call_arg_loc_node *ca_loc;
22455 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
22457 dw_die_ref die = NULL;
22458 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
22459 rtx arg, next_arg;
22461 for (arg = (ca_loc->call_arg_loc_note != NULL_RTX
22462 ? NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note)
22463 : NULL_RTX);
22464 arg; arg = next_arg)
22466 dw_loc_descr_ref reg, val;
22467 machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
22468 dw_die_ref cdie, tdie = NULL;
22470 next_arg = XEXP (arg, 1);
22471 if (REG_P (XEXP (XEXP (arg, 0), 0))
22472 && next_arg
22473 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
22474 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
22475 && REGNO (XEXP (XEXP (arg, 0), 0))
22476 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
22477 next_arg = XEXP (next_arg, 1);
22478 if (mode == VOIDmode)
22480 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
22481 if (mode == VOIDmode)
22482 mode = GET_MODE (XEXP (arg, 0));
22484 if (mode == VOIDmode || mode == BLKmode)
22485 continue;
22486 /* Get dynamic information about call target only if we
22487 have no static information: we cannot generate both
22488 DW_AT_call_origin and DW_AT_call_target
22489 attributes. */
22490 if (ca_loc->symbol_ref == NULL_RTX)
22492 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
22494 tloc = XEXP (XEXP (arg, 0), 1);
22495 continue;
22497 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
22498 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
22500 tlocc = XEXP (XEXP (arg, 0), 1);
22501 continue;
22504 reg = NULL;
22505 if (REG_P (XEXP (XEXP (arg, 0), 0)))
22506 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
22507 VAR_INIT_STATUS_INITIALIZED);
22508 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
22510 rtx mem = XEXP (XEXP (arg, 0), 0);
22511 reg = mem_loc_descriptor (XEXP (mem, 0),
22512 get_address_mode (mem),
22513 GET_MODE (mem),
22514 VAR_INIT_STATUS_INITIALIZED);
22516 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
22517 == DEBUG_PARAMETER_REF)
22519 tree tdecl
22520 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
22521 tdie = lookup_decl_die (tdecl);
22522 if (tdie == NULL)
22523 continue;
22525 else
22526 continue;
22527 if (reg == NULL
22528 && GET_CODE (XEXP (XEXP (arg, 0), 0))
22529 != DEBUG_PARAMETER_REF)
22530 continue;
22531 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
22532 VOIDmode,
22533 VAR_INIT_STATUS_INITIALIZED);
22534 if (val == NULL)
22535 continue;
22536 if (die == NULL)
22537 die = gen_call_site_die (decl, subr_die, ca_loc);
22538 cdie = new_die (dwarf_TAG (DW_TAG_call_site_parameter), die,
22539 NULL_TREE);
22540 if (reg != NULL)
22541 add_AT_loc (cdie, DW_AT_location, reg);
22542 else if (tdie != NULL)
22543 add_AT_die_ref (cdie, dwarf_AT (DW_AT_call_parameter),
22544 tdie);
22545 add_AT_loc (cdie, dwarf_AT (DW_AT_call_value), val);
22546 if (next_arg != XEXP (arg, 1))
22548 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
22549 if (mode == VOIDmode)
22550 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
22551 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
22552 0), 1),
22553 mode, VOIDmode,
22554 VAR_INIT_STATUS_INITIALIZED);
22555 if (val != NULL)
22556 add_AT_loc (cdie, dwarf_AT (DW_AT_call_data_value),
22557 val);
22560 if (die == NULL
22561 && (ca_loc->symbol_ref || tloc))
22562 die = gen_call_site_die (decl, subr_die, ca_loc);
22563 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
22565 dw_loc_descr_ref tval = NULL;
22567 if (tloc != NULL_RTX)
22568 tval = mem_loc_descriptor (tloc,
22569 GET_MODE (tloc) == VOIDmode
22570 ? Pmode : GET_MODE (tloc),
22571 VOIDmode,
22572 VAR_INIT_STATUS_INITIALIZED);
22573 if (tval)
22574 add_AT_loc (die, dwarf_AT (DW_AT_call_target), tval);
22575 else if (tlocc != NULL_RTX)
22577 tval = mem_loc_descriptor (tlocc,
22578 GET_MODE (tlocc) == VOIDmode
22579 ? Pmode : GET_MODE (tlocc),
22580 VOIDmode,
22581 VAR_INIT_STATUS_INITIALIZED);
22582 if (tval)
22583 add_AT_loc (die,
22584 dwarf_AT (DW_AT_call_target_clobbered),
22585 tval);
22588 if (die != NULL)
22590 call_site_note_count++;
22591 if (ca_loc->tail_call_p)
22592 tail_call_site_note_count++;
22596 call_arg_locations = NULL;
22597 call_arg_loc_last = NULL;
22598 if (tail_call_site_count >= 0
22599 && tail_call_site_count == tail_call_site_note_count
22600 && (!dwarf_strict || dwarf_version >= 5))
22602 if (call_site_count >= 0
22603 && call_site_count == call_site_note_count)
22604 add_AT_flag (subr_die, dwarf_AT (DW_AT_call_all_calls), 1);
22605 else
22606 add_AT_flag (subr_die, dwarf_AT (DW_AT_call_all_tail_calls), 1);
22608 call_site_count = -1;
22609 tail_call_site_count = -1;
22612 /* Mark used types after we have created DIEs for the functions scopes. */
22613 premark_used_types (DECL_STRUCT_FUNCTION (decl));
22616 /* Returns a hash value for X (which really is a die_struct). */
22618 hashval_t
22619 block_die_hasher::hash (die_struct *d)
22621 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
22624 /* Return nonzero if decl_id and die_parent of die_struct X is the same
22625 as decl_id and die_parent of die_struct Y. */
22627 bool
22628 block_die_hasher::equal (die_struct *x, die_struct *y)
22630 return x->decl_id == y->decl_id && x->die_parent == y->die_parent;
22633 /* Return TRUE if DECL, which may have been previously generated as
22634 OLD_DIE, is a candidate for a DW_AT_specification. DECLARATION is
22635 true if decl (or its origin) is either an extern declaration or a
22636 class/namespace scoped declaration.
22638 The declare_in_namespace support causes us to get two DIEs for one
22639 variable, both of which are declarations. We want to avoid
22640 considering one to be a specification, so we must test for
22641 DECLARATION and DW_AT_declaration. */
22642 static inline bool
22643 decl_will_get_specification_p (dw_die_ref old_die, tree decl, bool declaration)
22645 return (old_die && TREE_STATIC (decl) && !declaration
22646 && get_AT_flag (old_die, DW_AT_declaration) == 1);
22649 /* Return true if DECL is a local static. */
22651 static inline bool
22652 local_function_static (tree decl)
22654 gcc_assert (VAR_P (decl));
22655 return TREE_STATIC (decl)
22656 && DECL_CONTEXT (decl)
22657 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL;
22660 /* Generate a DIE to represent a declared data object.
22661 Either DECL or ORIGIN must be non-null. */
22663 static void
22664 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
22666 HOST_WIDE_INT off = 0;
22667 tree com_decl;
22668 tree decl_or_origin = decl ? decl : origin;
22669 tree ultimate_origin;
22670 dw_die_ref var_die;
22671 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
22672 bool declaration = (DECL_EXTERNAL (decl_or_origin)
22673 || class_or_namespace_scope_p (context_die));
22674 bool specialization_p = false;
22675 bool no_linkage_name = false;
22677 /* While C++ inline static data members have definitions inside of the
22678 class, force the first DIE to be a declaration, then let gen_member_die
22679 reparent it to the class context and call gen_variable_die again
22680 to create the outside of the class DIE for the definition. */
22681 if (!declaration
22682 && old_die == NULL
22683 && decl
22684 && DECL_CONTEXT (decl)
22685 && TYPE_P (DECL_CONTEXT (decl))
22686 && lang_hooks.decls.decl_dwarf_attribute (decl, DW_AT_inline) != -1)
22688 declaration = true;
22689 if (dwarf_version < 5)
22690 no_linkage_name = true;
22693 ultimate_origin = decl_ultimate_origin (decl_or_origin);
22694 if (decl || ultimate_origin)
22695 origin = ultimate_origin;
22696 com_decl = fortran_common (decl_or_origin, &off);
22698 /* Symbol in common gets emitted as a child of the common block, in the form
22699 of a data member. */
22700 if (com_decl)
22702 dw_die_ref com_die;
22703 dw_loc_list_ref loc = NULL;
22704 die_node com_die_arg;
22706 var_die = lookup_decl_die (decl_or_origin);
22707 if (var_die)
22709 if (! early_dwarf && get_AT (var_die, DW_AT_location) == NULL)
22711 loc = loc_list_from_tree (com_decl, off ? 1 : 2, NULL);
22712 if (loc)
22714 if (off)
22716 /* Optimize the common case. */
22717 if (single_element_loc_list_p (loc)
22718 && loc->expr->dw_loc_opc == DW_OP_addr
22719 && loc->expr->dw_loc_next == NULL
22720 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
22721 == SYMBOL_REF)
22723 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
22724 loc->expr->dw_loc_oprnd1.v.val_addr
22725 = plus_constant (GET_MODE (x), x , off);
22727 else
22728 loc_list_plus_const (loc, off);
22730 add_AT_location_description (var_die, DW_AT_location, loc);
22731 remove_AT (var_die, DW_AT_declaration);
22734 return;
22737 if (common_block_die_table == NULL)
22738 common_block_die_table = hash_table<block_die_hasher>::create_ggc (10);
22740 com_die_arg.decl_id = DECL_UID (com_decl);
22741 com_die_arg.die_parent = context_die;
22742 com_die = common_block_die_table->find (&com_die_arg);
22743 if (! early_dwarf)
22744 loc = loc_list_from_tree (com_decl, 2, NULL);
22745 if (com_die == NULL)
22747 const char *cnam
22748 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
22749 die_node **slot;
22751 com_die = new_die (DW_TAG_common_block, context_die, decl);
22752 add_name_and_src_coords_attributes (com_die, com_decl);
22753 if (loc)
22755 add_AT_location_description (com_die, DW_AT_location, loc);
22756 /* Avoid sharing the same loc descriptor between
22757 DW_TAG_common_block and DW_TAG_variable. */
22758 loc = loc_list_from_tree (com_decl, 2, NULL);
22760 else if (DECL_EXTERNAL (decl_or_origin))
22761 add_AT_flag (com_die, DW_AT_declaration, 1);
22762 if (want_pubnames ())
22763 add_pubname_string (cnam, com_die); /* ??? needed? */
22764 com_die->decl_id = DECL_UID (com_decl);
22765 slot = common_block_die_table->find_slot (com_die, INSERT);
22766 *slot = com_die;
22768 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
22770 add_AT_location_description (com_die, DW_AT_location, loc);
22771 loc = loc_list_from_tree (com_decl, 2, NULL);
22772 remove_AT (com_die, DW_AT_declaration);
22774 var_die = new_die (DW_TAG_variable, com_die, decl);
22775 add_name_and_src_coords_attributes (var_die, decl_or_origin);
22776 add_type_attribute (var_die, TREE_TYPE (decl_or_origin),
22777 decl_quals (decl_or_origin), false,
22778 context_die);
22779 add_alignment_attribute (var_die, decl);
22780 add_AT_flag (var_die, DW_AT_external, 1);
22781 if (loc)
22783 if (off)
22785 /* Optimize the common case. */
22786 if (single_element_loc_list_p (loc)
22787 && loc->expr->dw_loc_opc == DW_OP_addr
22788 && loc->expr->dw_loc_next == NULL
22789 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
22791 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
22792 loc->expr->dw_loc_oprnd1.v.val_addr
22793 = plus_constant (GET_MODE (x), x, off);
22795 else
22796 loc_list_plus_const (loc, off);
22798 add_AT_location_description (var_die, DW_AT_location, loc);
22800 else if (DECL_EXTERNAL (decl_or_origin))
22801 add_AT_flag (var_die, DW_AT_declaration, 1);
22802 if (decl)
22803 equate_decl_number_to_die (decl, var_die);
22804 return;
22807 if (old_die)
22809 if (declaration)
22811 /* A declaration that has been previously dumped, needs no
22812 further annotations, since it doesn't need location on
22813 the second pass. */
22814 return;
22816 else if (decl_will_get_specification_p (old_die, decl, declaration)
22817 && !get_AT (old_die, DW_AT_specification))
22819 /* Fall-thru so we can make a new variable die along with a
22820 DW_AT_specification. */
22822 else if (origin && old_die->die_parent != context_die)
22824 /* If we will be creating an inlined instance, we need a
22825 new DIE that will get annotated with
22826 DW_AT_abstract_origin. Clear things so we can get a
22827 new DIE. */
22828 gcc_assert (!DECL_ABSTRACT_P (decl));
22829 old_die = NULL;
22831 else
22833 /* If a DIE was dumped early, it still needs location info.
22834 Skip to where we fill the location bits. */
22835 var_die = old_die;
22836 goto gen_variable_die_location;
22840 /* For static data members, the declaration in the class is supposed
22841 to have DW_TAG_member tag in DWARF{3,4} and we emit it for compatibility
22842 also in DWARF2; the specification should still be DW_TAG_variable
22843 referencing the DW_TAG_member DIE. */
22844 if (declaration && class_scope_p (context_die) && dwarf_version < 5)
22845 var_die = new_die (DW_TAG_member, context_die, decl);
22846 else
22847 var_die = new_die (DW_TAG_variable, context_die, decl);
22849 if (origin != NULL)
22850 add_abstract_origin_attribute (var_die, origin);
22852 /* Loop unrolling can create multiple blocks that refer to the same
22853 static variable, so we must test for the DW_AT_declaration flag.
22855 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
22856 copy decls and set the DECL_ABSTRACT_P flag on them instead of
22857 sharing them.
22859 ??? Duplicated blocks have been rewritten to use .debug_ranges. */
22860 else if (decl_will_get_specification_p (old_die, decl, declaration))
22862 /* This is a definition of a C++ class level static. */
22863 add_AT_specification (var_die, old_die);
22864 specialization_p = true;
22865 if (DECL_NAME (decl))
22867 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
22868 struct dwarf_file_data * file_index = lookup_filename (s.file);
22870 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
22871 add_AT_file (var_die, DW_AT_decl_file, file_index);
22873 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
22874 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
22876 if (debug_column_info
22877 && s.column
22878 && (get_AT_unsigned (old_die, DW_AT_decl_column)
22879 != (unsigned) s.column))
22880 add_AT_unsigned (var_die, DW_AT_decl_column, s.column);
22882 if (old_die->die_tag == DW_TAG_member)
22883 add_linkage_name (var_die, decl);
22886 else
22887 add_name_and_src_coords_attributes (var_die, decl, no_linkage_name);
22889 if ((origin == NULL && !specialization_p)
22890 || (origin != NULL
22891 && !DECL_ABSTRACT_P (decl_or_origin)
22892 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
22893 decl_function_context
22894 (decl_or_origin))))
22896 tree type = TREE_TYPE (decl_or_origin);
22898 if (decl_by_reference_p (decl_or_origin))
22899 add_type_attribute (var_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
22900 context_die);
22901 else
22902 add_type_attribute (var_die, type, decl_quals (decl_or_origin), false,
22903 context_die);
22906 if (origin == NULL && !specialization_p)
22908 if (TREE_PUBLIC (decl))
22909 add_AT_flag (var_die, DW_AT_external, 1);
22911 if (DECL_ARTIFICIAL (decl))
22912 add_AT_flag (var_die, DW_AT_artificial, 1);
22914 add_alignment_attribute (var_die, decl);
22916 add_accessibility_attribute (var_die, decl);
22919 if (declaration)
22920 add_AT_flag (var_die, DW_AT_declaration, 1);
22922 if (decl && (DECL_ABSTRACT_P (decl)
22923 || !old_die || is_declaration_die (old_die)))
22924 equate_decl_number_to_die (decl, var_die);
22926 gen_variable_die_location:
22927 if (! declaration
22928 && (! DECL_ABSTRACT_P (decl_or_origin)
22929 /* Local static vars are shared between all clones/inlines,
22930 so emit DW_AT_location on the abstract DIE if DECL_RTL is
22931 already set. */
22932 || (VAR_P (decl_or_origin)
22933 && TREE_STATIC (decl_or_origin)
22934 && DECL_RTL_SET_P (decl_or_origin))))
22936 if (early_dwarf)
22937 add_pubname (decl_or_origin, var_die);
22938 else
22939 add_location_or_const_value_attribute (var_die, decl_or_origin,
22940 decl == NULL);
22942 else
22943 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
22945 if ((dwarf_version >= 4 || !dwarf_strict)
22946 && lang_hooks.decls.decl_dwarf_attribute (decl_or_origin,
22947 DW_AT_const_expr) == 1
22948 && !get_AT (var_die, DW_AT_const_expr)
22949 && !specialization_p)
22950 add_AT_flag (var_die, DW_AT_const_expr, 1);
22952 if (!dwarf_strict)
22954 int inl = lang_hooks.decls.decl_dwarf_attribute (decl_or_origin,
22955 DW_AT_inline);
22956 if (inl != -1
22957 && !get_AT (var_die, DW_AT_inline)
22958 && !specialization_p)
22959 add_AT_unsigned (var_die, DW_AT_inline, inl);
22963 /* Generate a DIE to represent a named constant. */
22965 static void
22966 gen_const_die (tree decl, dw_die_ref context_die)
22968 dw_die_ref const_die;
22969 tree type = TREE_TYPE (decl);
22971 const_die = lookup_decl_die (decl);
22972 if (const_die)
22973 return;
22975 const_die = new_die (DW_TAG_constant, context_die, decl);
22976 equate_decl_number_to_die (decl, const_die);
22977 add_name_and_src_coords_attributes (const_die, decl);
22978 add_type_attribute (const_die, type, TYPE_QUAL_CONST, false, context_die);
22979 if (TREE_PUBLIC (decl))
22980 add_AT_flag (const_die, DW_AT_external, 1);
22981 if (DECL_ARTIFICIAL (decl))
22982 add_AT_flag (const_die, DW_AT_artificial, 1);
22983 tree_add_const_value_attribute_for_decl (const_die, decl);
22986 /* Generate a DIE to represent a label identifier. */
22988 static void
22989 gen_label_die (tree decl, dw_die_ref context_die)
22991 tree origin = decl_ultimate_origin (decl);
22992 dw_die_ref lbl_die = lookup_decl_die (decl);
22993 rtx insn;
22994 char label[MAX_ARTIFICIAL_LABEL_BYTES];
22996 if (!lbl_die)
22998 lbl_die = new_die (DW_TAG_label, context_die, decl);
22999 equate_decl_number_to_die (decl, lbl_die);
23001 if (origin != NULL)
23002 add_abstract_origin_attribute (lbl_die, origin);
23003 else
23004 add_name_and_src_coords_attributes (lbl_die, decl);
23007 if (DECL_ABSTRACT_P (decl))
23008 equate_decl_number_to_die (decl, lbl_die);
23009 else if (! early_dwarf)
23011 insn = DECL_RTL_IF_SET (decl);
23013 /* Deleted labels are programmer specified labels which have been
23014 eliminated because of various optimizations. We still emit them
23015 here so that it is possible to put breakpoints on them. */
23016 if (insn
23017 && (LABEL_P (insn)
23018 || ((NOTE_P (insn)
23019 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
23021 /* When optimization is enabled (via -O) some parts of the compiler
23022 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
23023 represent source-level labels which were explicitly declared by
23024 the user. This really shouldn't be happening though, so catch
23025 it if it ever does happen. */
23026 gcc_assert (!as_a<rtx_insn *> (insn)->deleted ());
23028 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
23029 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
23031 else if (insn
23032 && NOTE_P (insn)
23033 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
23034 && CODE_LABEL_NUMBER (insn) != -1)
23036 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
23037 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
23042 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
23043 attributes to the DIE for a block STMT, to describe where the inlined
23044 function was called from. This is similar to add_src_coords_attributes. */
23046 static inline void
23047 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
23049 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
23051 if (dwarf_version >= 3 || !dwarf_strict)
23053 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
23054 add_AT_unsigned (die, DW_AT_call_line, s.line);
23055 if (debug_column_info && s.column)
23056 add_AT_unsigned (die, DW_AT_call_column, s.column);
23061 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
23062 Add low_pc and high_pc attributes to the DIE for a block STMT. */
23064 static inline void
23065 add_high_low_attributes (tree stmt, dw_die_ref die)
23067 char label[MAX_ARTIFICIAL_LABEL_BYTES];
23069 if (BLOCK_FRAGMENT_CHAIN (stmt)
23070 && (dwarf_version >= 3 || !dwarf_strict))
23072 tree chain, superblock = NULL_TREE;
23073 dw_die_ref pdie;
23074 dw_attr_node *attr = NULL;
23076 if (inlined_function_outer_scope_p (stmt))
23078 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
23079 BLOCK_NUMBER (stmt));
23080 add_AT_lbl_id (die, DW_AT_entry_pc, label);
23083 /* Optimize duplicate .debug_ranges lists or even tails of
23084 lists. If this BLOCK has same ranges as its supercontext,
23085 lookup DW_AT_ranges attribute in the supercontext (and
23086 recursively so), verify that the ranges_table contains the
23087 right values and use it instead of adding a new .debug_range. */
23088 for (chain = stmt, pdie = die;
23089 BLOCK_SAME_RANGE (chain);
23090 chain = BLOCK_SUPERCONTEXT (chain))
23092 dw_attr_node *new_attr;
23094 pdie = pdie->die_parent;
23095 if (pdie == NULL)
23096 break;
23097 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
23098 break;
23099 new_attr = get_AT (pdie, DW_AT_ranges);
23100 if (new_attr == NULL
23101 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
23102 break;
23103 attr = new_attr;
23104 superblock = BLOCK_SUPERCONTEXT (chain);
23106 if (attr != NULL
23107 && ((*ranges_table)[attr->dw_attr_val.v.val_offset].num
23108 == BLOCK_NUMBER (superblock))
23109 && BLOCK_FRAGMENT_CHAIN (superblock))
23111 unsigned long off = attr->dw_attr_val.v.val_offset;
23112 unsigned long supercnt = 0, thiscnt = 0;
23113 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
23114 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
23116 ++supercnt;
23117 gcc_checking_assert ((*ranges_table)[off + supercnt].num
23118 == BLOCK_NUMBER (chain));
23120 gcc_checking_assert ((*ranges_table)[off + supercnt + 1].num == 0);
23121 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
23122 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
23123 ++thiscnt;
23124 gcc_assert (supercnt >= thiscnt);
23125 add_AT_range_list (die, DW_AT_ranges, off + supercnt - thiscnt,
23126 false);
23127 note_rnglist_head (off + supercnt - thiscnt);
23128 return;
23131 unsigned int offset = add_ranges (stmt, true);
23132 add_AT_range_list (die, DW_AT_ranges, offset, false);
23133 note_rnglist_head (offset);
23135 bool prev_in_cold = BLOCK_IN_COLD_SECTION_P (stmt);
23136 chain = BLOCK_FRAGMENT_CHAIN (stmt);
23139 add_ranges (chain, prev_in_cold != BLOCK_IN_COLD_SECTION_P (chain));
23140 prev_in_cold = BLOCK_IN_COLD_SECTION_P (chain);
23141 chain = BLOCK_FRAGMENT_CHAIN (chain);
23143 while (chain);
23144 add_ranges (NULL);
23146 else
23148 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
23149 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
23150 BLOCK_NUMBER (stmt));
23151 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
23152 BLOCK_NUMBER (stmt));
23153 add_AT_low_high_pc (die, label, label_high, false);
23157 /* Generate a DIE for a lexical block. */
23159 static void
23160 gen_lexical_block_die (tree stmt, dw_die_ref context_die)
23162 dw_die_ref old_die = BLOCK_DIE (stmt);
23163 dw_die_ref stmt_die = NULL;
23164 if (!old_die)
23166 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
23167 BLOCK_DIE (stmt) = stmt_die;
23170 if (BLOCK_ABSTRACT (stmt))
23172 if (old_die)
23174 /* This must have been generated early and it won't even
23175 need location information since it's a DW_AT_inline
23176 function. */
23177 if (flag_checking)
23178 for (dw_die_ref c = context_die; c; c = c->die_parent)
23179 if (c->die_tag == DW_TAG_inlined_subroutine
23180 || c->die_tag == DW_TAG_subprogram)
23182 gcc_assert (get_AT (c, DW_AT_inline));
23183 break;
23185 return;
23188 else if (BLOCK_ABSTRACT_ORIGIN (stmt))
23190 /* If this is an inlined instance, create a new lexical die for
23191 anything below to attach DW_AT_abstract_origin to. */
23192 if (old_die)
23194 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
23195 BLOCK_DIE (stmt) = stmt_die;
23196 old_die = NULL;
23199 tree origin = block_ultimate_origin (stmt);
23200 if (origin != NULL_TREE && origin != stmt)
23201 add_abstract_origin_attribute (stmt_die, origin);
23204 if (old_die)
23205 stmt_die = old_die;
23207 /* A non abstract block whose blocks have already been reordered
23208 should have the instruction range for this block. If so, set the
23209 high/low attributes. */
23210 if (!early_dwarf && !BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
23212 gcc_assert (stmt_die);
23213 add_high_low_attributes (stmt, stmt_die);
23216 decls_for_scope (stmt, stmt_die);
23219 /* Generate a DIE for an inlined subprogram. */
23221 static void
23222 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die)
23224 tree decl;
23226 /* The instance of function that is effectively being inlined shall not
23227 be abstract. */
23228 gcc_assert (! BLOCK_ABSTRACT (stmt));
23230 decl = block_ultimate_origin (stmt);
23232 /* Make sure any inlined functions are known to be inlineable. */
23233 gcc_checking_assert (DECL_ABSTRACT_P (decl)
23234 || cgraph_function_possibly_inlined_p (decl));
23236 /* Emit info for the abstract instance first, if we haven't yet. We
23237 must emit this even if the block is abstract, otherwise when we
23238 emit the block below (or elsewhere), we may end up trying to emit
23239 a die whose origin die hasn't been emitted, and crashing. */
23240 dwarf2out_abstract_function (decl);
23242 if (! BLOCK_ABSTRACT (stmt))
23244 dw_die_ref subr_die
23245 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
23247 if (call_arg_locations)
23248 BLOCK_DIE (stmt) = subr_die;
23249 add_abstract_origin_attribute (subr_die, decl);
23250 if (TREE_ASM_WRITTEN (stmt))
23251 add_high_low_attributes (stmt, subr_die);
23252 add_call_src_coords_attributes (stmt, subr_die);
23254 decls_for_scope (stmt, subr_die);
23258 /* Generate a DIE for a field in a record, or structure. CTX is required: see
23259 the comment for VLR_CONTEXT. */
23261 static void
23262 gen_field_die (tree decl, struct vlr_context *ctx, dw_die_ref context_die)
23264 dw_die_ref decl_die;
23266 if (TREE_TYPE (decl) == error_mark_node)
23267 return;
23269 decl_die = new_die (DW_TAG_member, context_die, decl);
23270 add_name_and_src_coords_attributes (decl_die, decl);
23271 add_type_attribute (decl_die, member_declared_type (decl), decl_quals (decl),
23272 TYPE_REVERSE_STORAGE_ORDER (DECL_FIELD_CONTEXT (decl)),
23273 context_die);
23275 if (DECL_BIT_FIELD_TYPE (decl))
23277 add_byte_size_attribute (decl_die, decl);
23278 add_bit_size_attribute (decl_die, decl);
23279 add_bit_offset_attribute (decl_die, decl, ctx);
23282 add_alignment_attribute (decl_die, decl);
23284 /* If we have a variant part offset, then we are supposed to process a member
23285 of a QUAL_UNION_TYPE, which is how we represent variant parts in
23286 trees. */
23287 gcc_assert (ctx->variant_part_offset == NULL_TREE
23288 || TREE_CODE (DECL_FIELD_CONTEXT (decl)) != QUAL_UNION_TYPE);
23289 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
23290 add_data_member_location_attribute (decl_die, decl, ctx);
23292 if (DECL_ARTIFICIAL (decl))
23293 add_AT_flag (decl_die, DW_AT_artificial, 1);
23295 add_accessibility_attribute (decl_die, decl);
23297 /* Equate decl number to die, so that we can look up this decl later on. */
23298 equate_decl_number_to_die (decl, decl_die);
23301 #if 0
23302 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
23303 Use modified_type_die instead.
23304 We keep this code here just in case these types of DIEs may be needed to
23305 represent certain things in other languages (e.g. Pascal) someday. */
23307 static void
23308 gen_pointer_type_die (tree type, dw_die_ref context_die)
23310 dw_die_ref ptr_die
23311 = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
23313 equate_type_number_to_die (type, ptr_die);
23314 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
23315 context_die);
23316 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
23319 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
23320 Use modified_type_die instead.
23321 We keep this code here just in case these types of DIEs may be needed to
23322 represent certain things in other languages (e.g. Pascal) someday. */
23324 static void
23325 gen_reference_type_die (tree type, dw_die_ref context_die)
23327 dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
23329 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
23330 ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
23331 else
23332 ref_die = new_die (DW_TAG_reference_type, scope_die, type);
23334 equate_type_number_to_die (type, ref_die);
23335 add_type_attribute (ref_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
23336 context_die);
23337 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
23339 #endif
23341 /* Generate a DIE for a pointer to a member type. TYPE can be an
23342 OFFSET_TYPE, for a pointer to data member, or a RECORD_TYPE, for a
23343 pointer to member function. */
23345 static void
23346 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
23348 if (lookup_type_die (type))
23349 return;
23351 dw_die_ref ptr_die = new_die (DW_TAG_ptr_to_member_type,
23352 scope_die_for (type, context_die), type);
23354 equate_type_number_to_die (type, ptr_die);
23355 add_AT_die_ref (ptr_die, DW_AT_containing_type,
23356 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
23357 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
23358 context_die);
23359 add_alignment_attribute (ptr_die, type);
23361 if (TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE
23362 && TREE_CODE (TREE_TYPE (type)) != METHOD_TYPE)
23364 dw_loc_descr_ref op = new_loc_descr (DW_OP_plus, 0, 0);
23365 add_AT_loc (ptr_die, DW_AT_use_location, op);
23369 static char *producer_string;
23371 /* Return a heap allocated producer string including command line options
23372 if -grecord-gcc-switches. */
23374 static char *
23375 gen_producer_string (void)
23377 size_t j;
23378 auto_vec<const char *> switches;
23379 const char *language_string = lang_hooks.name;
23380 char *producer, *tail;
23381 const char *p;
23382 size_t len = dwarf_record_gcc_switches ? 0 : 3;
23383 size_t plen = strlen (language_string) + 1 + strlen (version_string);
23385 for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
23386 switch (save_decoded_options[j].opt_index)
23388 case OPT_o:
23389 case OPT_d:
23390 case OPT_dumpbase:
23391 case OPT_dumpdir:
23392 case OPT_auxbase:
23393 case OPT_auxbase_strip:
23394 case OPT_quiet:
23395 case OPT_version:
23396 case OPT_v:
23397 case OPT_w:
23398 case OPT_L:
23399 case OPT_D:
23400 case OPT_I:
23401 case OPT_U:
23402 case OPT_SPECIAL_unknown:
23403 case OPT_SPECIAL_ignore:
23404 case OPT_SPECIAL_program_name:
23405 case OPT_SPECIAL_input_file:
23406 case OPT_grecord_gcc_switches:
23407 case OPT_gno_record_gcc_switches:
23408 case OPT__output_pch_:
23409 case OPT_fdiagnostics_show_location_:
23410 case OPT_fdiagnostics_show_option:
23411 case OPT_fdiagnostics_show_caret:
23412 case OPT_fdiagnostics_color_:
23413 case OPT_fverbose_asm:
23414 case OPT____:
23415 case OPT__sysroot_:
23416 case OPT_nostdinc:
23417 case OPT_nostdinc__:
23418 case OPT_fpreprocessed:
23419 case OPT_fltrans_output_list_:
23420 case OPT_fresolution_:
23421 case OPT_fdebug_prefix_map_:
23422 /* Ignore these. */
23423 continue;
23424 default:
23425 if (cl_options[save_decoded_options[j].opt_index].flags
23426 & CL_NO_DWARF_RECORD)
23427 continue;
23428 gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
23429 == '-');
23430 switch (save_decoded_options[j].canonical_option[0][1])
23432 case 'M':
23433 case 'i':
23434 case 'W':
23435 continue;
23436 case 'f':
23437 if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
23438 "dump", 4) == 0)
23439 continue;
23440 break;
23441 default:
23442 break;
23444 switches.safe_push (save_decoded_options[j].orig_option_with_args_text);
23445 len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
23446 break;
23449 producer = XNEWVEC (char, plen + 1 + len + 1);
23450 tail = producer;
23451 sprintf (tail, "%s %s", language_string, version_string);
23452 tail += plen;
23454 FOR_EACH_VEC_ELT (switches, j, p)
23456 len = strlen (p);
23457 *tail = ' ';
23458 memcpy (tail + 1, p, len);
23459 tail += len + 1;
23462 *tail = '\0';
23463 return producer;
23466 /* Given a C and/or C++ language/version string return the "highest".
23467 C++ is assumed to be "higher" than C in this case. Used for merging
23468 LTO translation unit languages. */
23469 static const char *
23470 highest_c_language (const char *lang1, const char *lang2)
23472 if (strcmp ("GNU C++14", lang1) == 0 || strcmp ("GNU C++14", lang2) == 0)
23473 return "GNU C++14";
23474 if (strcmp ("GNU C++11", lang1) == 0 || strcmp ("GNU C++11", lang2) == 0)
23475 return "GNU C++11";
23476 if (strcmp ("GNU C++98", lang1) == 0 || strcmp ("GNU C++98", lang2) == 0)
23477 return "GNU C++98";
23479 if (strcmp ("GNU C11", lang1) == 0 || strcmp ("GNU C11", lang2) == 0)
23480 return "GNU C11";
23481 if (strcmp ("GNU C99", lang1) == 0 || strcmp ("GNU C99", lang2) == 0)
23482 return "GNU C99";
23483 if (strcmp ("GNU C89", lang1) == 0 || strcmp ("GNU C89", lang2) == 0)
23484 return "GNU C89";
23486 gcc_unreachable ();
23490 /* Generate the DIE for the compilation unit. */
23492 static dw_die_ref
23493 gen_compile_unit_die (const char *filename)
23495 dw_die_ref die;
23496 const char *language_string = lang_hooks.name;
23497 int language;
23499 die = new_die (DW_TAG_compile_unit, NULL, NULL);
23501 if (filename)
23503 add_name_attribute (die, filename);
23504 /* Don't add cwd for <built-in>. */
23505 if (filename[0] != '<')
23506 add_comp_dir_attribute (die);
23509 add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
23511 /* If our producer is LTO try to figure out a common language to use
23512 from the global list of translation units. */
23513 if (strcmp (language_string, "GNU GIMPLE") == 0)
23515 unsigned i;
23516 tree t;
23517 const char *common_lang = NULL;
23519 FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
23521 if (!TRANSLATION_UNIT_LANGUAGE (t))
23522 continue;
23523 if (!common_lang)
23524 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
23525 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
23527 else if (strncmp (common_lang, "GNU C", 5) == 0
23528 && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
23529 /* Mixing C and C++ is ok, use C++ in that case. */
23530 common_lang = highest_c_language (common_lang,
23531 TRANSLATION_UNIT_LANGUAGE (t));
23532 else
23534 /* Fall back to C. */
23535 common_lang = NULL;
23536 break;
23540 if (common_lang)
23541 language_string = common_lang;
23544 language = DW_LANG_C;
23545 if (strncmp (language_string, "GNU C", 5) == 0
23546 && ISDIGIT (language_string[5]))
23548 language = DW_LANG_C89;
23549 if (dwarf_version >= 3 || !dwarf_strict)
23551 if (strcmp (language_string, "GNU C89") != 0)
23552 language = DW_LANG_C99;
23554 if (dwarf_version >= 5 /* || !dwarf_strict */)
23555 if (strcmp (language_string, "GNU C11") == 0)
23556 language = DW_LANG_C11;
23559 else if (strncmp (language_string, "GNU C++", 7) == 0)
23561 language = DW_LANG_C_plus_plus;
23562 if (dwarf_version >= 5 /* || !dwarf_strict */)
23564 if (strcmp (language_string, "GNU C++11") == 0)
23565 language = DW_LANG_C_plus_plus_11;
23566 else if (strcmp (language_string, "GNU C++14") == 0)
23567 language = DW_LANG_C_plus_plus_14;
23570 else if (strcmp (language_string, "GNU F77") == 0)
23571 language = DW_LANG_Fortran77;
23572 else if (strcmp (language_string, "GNU Pascal") == 0)
23573 language = DW_LANG_Pascal83;
23574 else if (dwarf_version >= 3 || !dwarf_strict)
23576 if (strcmp (language_string, "GNU Ada") == 0)
23577 language = DW_LANG_Ada95;
23578 else if (strncmp (language_string, "GNU Fortran", 11) == 0)
23580 language = DW_LANG_Fortran95;
23581 if (dwarf_version >= 5 /* || !dwarf_strict */)
23583 if (strcmp (language_string, "GNU Fortran2003") == 0)
23584 language = DW_LANG_Fortran03;
23585 else if (strcmp (language_string, "GNU Fortran2008") == 0)
23586 language = DW_LANG_Fortran08;
23589 else if (strcmp (language_string, "GNU Java") == 0)
23590 language = DW_LANG_Java;
23591 else if (strcmp (language_string, "GNU Objective-C") == 0)
23592 language = DW_LANG_ObjC;
23593 else if (strcmp (language_string, "GNU Objective-C++") == 0)
23594 language = DW_LANG_ObjC_plus_plus;
23595 else if (dwarf_version >= 5 || !dwarf_strict)
23597 if (strcmp (language_string, "GNU Go") == 0)
23598 language = DW_LANG_Go;
23601 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
23602 else if (strncmp (language_string, "GNU Fortran", 11) == 0)
23603 language = DW_LANG_Fortran90;
23605 add_AT_unsigned (die, DW_AT_language, language);
23607 switch (language)
23609 case DW_LANG_Fortran77:
23610 case DW_LANG_Fortran90:
23611 case DW_LANG_Fortran95:
23612 case DW_LANG_Fortran03:
23613 case DW_LANG_Fortran08:
23614 /* Fortran has case insensitive identifiers and the front-end
23615 lowercases everything. */
23616 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
23617 break;
23618 default:
23619 /* The default DW_ID_case_sensitive doesn't need to be specified. */
23620 break;
23622 return die;
23625 /* Generate the DIE for a base class. */
23627 static void
23628 gen_inheritance_die (tree binfo, tree access, tree type,
23629 dw_die_ref context_die)
23631 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
23632 struct vlr_context ctx = { type, NULL };
23634 add_type_attribute (die, BINFO_TYPE (binfo), TYPE_UNQUALIFIED, false,
23635 context_die);
23636 add_data_member_location_attribute (die, binfo, &ctx);
23638 if (BINFO_VIRTUAL_P (binfo))
23639 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
23641 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
23642 children, otherwise the default is DW_ACCESS_public. In DWARF2
23643 the default has always been DW_ACCESS_private. */
23644 if (access == access_public_node)
23646 if (dwarf_version == 2
23647 || context_die->die_tag == DW_TAG_class_type)
23648 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
23650 else if (access == access_protected_node)
23651 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
23652 else if (dwarf_version > 2
23653 && context_die->die_tag != DW_TAG_class_type)
23654 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
23657 /* Return whether DECL is a FIELD_DECL that represents the variant part of a
23658 structure. */
23659 static bool
23660 is_variant_part (tree decl)
23662 return (TREE_CODE (decl) == FIELD_DECL
23663 && TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE);
23666 /* Check that OPERAND is a reference to a field in STRUCT_TYPE. If it is,
23667 return the FIELD_DECL. Return NULL_TREE otherwise. */
23669 static tree
23670 analyze_discr_in_predicate (tree operand, tree struct_type)
23672 bool continue_stripping = true;
23673 while (continue_stripping)
23674 switch (TREE_CODE (operand))
23676 CASE_CONVERT:
23677 operand = TREE_OPERAND (operand, 0);
23678 break;
23679 default:
23680 continue_stripping = false;
23681 break;
23684 /* Match field access to members of struct_type only. */
23685 if (TREE_CODE (operand) == COMPONENT_REF
23686 && TREE_CODE (TREE_OPERAND (operand, 0)) == PLACEHOLDER_EXPR
23687 && TREE_TYPE (TREE_OPERAND (operand, 0)) == struct_type
23688 && TREE_CODE (TREE_OPERAND (operand, 1)) == FIELD_DECL)
23689 return TREE_OPERAND (operand, 1);
23690 else
23691 return NULL_TREE;
23694 /* Check that SRC is a constant integer that can be represented as a native
23695 integer constant (either signed or unsigned). If so, store it into DEST and
23696 return true. Return false otherwise. */
23698 static bool
23699 get_discr_value (tree src, dw_discr_value *dest)
23701 bool is_unsigned = TYPE_UNSIGNED (TREE_TYPE (src));
23703 if (TREE_CODE (src) != INTEGER_CST
23704 || !(is_unsigned ? tree_fits_uhwi_p (src) : tree_fits_shwi_p (src)))
23705 return false;
23707 dest->pos = is_unsigned;
23708 if (is_unsigned)
23709 dest->v.uval = tree_to_uhwi (src);
23710 else
23711 dest->v.sval = tree_to_shwi (src);
23713 return true;
23716 /* Try to extract synthetic properties out of VARIANT_PART_DECL, which is a
23717 FIELD_DECL in STRUCT_TYPE that represents a variant part. If unsuccessful,
23718 store NULL_TREE in DISCR_DECL. Otherwise:
23720 - store the discriminant field in STRUCT_TYPE that controls the variant
23721 part to *DISCR_DECL
23723 - put in *DISCR_LISTS_P an array where for each variant, the item
23724 represents the corresponding matching list of discriminant values.
23726 - put in *DISCR_LISTS_LENGTH the number of variants, which is the size of
23727 the above array.
23729 Note that when the array is allocated (i.e. when the analysis is
23730 successful), it is up to the caller to free the array. */
23732 static void
23733 analyze_variants_discr (tree variant_part_decl,
23734 tree struct_type,
23735 tree *discr_decl,
23736 dw_discr_list_ref **discr_lists_p,
23737 unsigned *discr_lists_length)
23739 tree variant_part_type = TREE_TYPE (variant_part_decl);
23740 tree variant;
23741 dw_discr_list_ref *discr_lists;
23742 unsigned i;
23744 /* Compute how many variants there are in this variant part. */
23745 *discr_lists_length = 0;
23746 for (variant = TYPE_FIELDS (variant_part_type);
23747 variant != NULL_TREE;
23748 variant = DECL_CHAIN (variant))
23749 ++*discr_lists_length;
23751 *discr_decl = NULL_TREE;
23752 *discr_lists_p
23753 = (dw_discr_list_ref *) xcalloc (*discr_lists_length,
23754 sizeof (**discr_lists_p));
23755 discr_lists = *discr_lists_p;
23757 /* And then analyze all variants to extract discriminant information for all
23758 of them. This analysis is conservative: as soon as we detect something we
23759 do not support, abort everything and pretend we found nothing. */
23760 for (variant = TYPE_FIELDS (variant_part_type), i = 0;
23761 variant != NULL_TREE;
23762 variant = DECL_CHAIN (variant), ++i)
23764 tree match_expr = DECL_QUALIFIER (variant);
23766 /* Now, try to analyze the predicate and deduce a discriminant for
23767 it. */
23768 if (match_expr == boolean_true_node)
23769 /* Typically happens for the default variant: it matches all cases that
23770 previous variants rejected. Don't output any matching value for
23771 this one. */
23772 continue;
23774 /* The following loop tries to iterate over each discriminant
23775 possibility: single values or ranges. */
23776 while (match_expr != NULL_TREE)
23778 tree next_round_match_expr;
23779 tree candidate_discr = NULL_TREE;
23780 dw_discr_list_ref new_node = NULL;
23782 /* Possibilities are matched one after the other by nested
23783 TRUTH_ORIF_EXPR expressions. Process the current possibility and
23784 continue with the rest at next iteration. */
23785 if (TREE_CODE (match_expr) == TRUTH_ORIF_EXPR)
23787 next_round_match_expr = TREE_OPERAND (match_expr, 0);
23788 match_expr = TREE_OPERAND (match_expr, 1);
23790 else
23791 next_round_match_expr = NULL_TREE;
23793 if (match_expr == boolean_false_node)
23794 /* This sub-expression matches nothing: just wait for the next
23795 one. */
23798 else if (TREE_CODE (match_expr) == EQ_EXPR)
23800 /* We are matching: <discr_field> == <integer_cst>
23801 This sub-expression matches a single value. */
23802 tree integer_cst = TREE_OPERAND (match_expr, 1);
23804 candidate_discr
23805 = analyze_discr_in_predicate (TREE_OPERAND (match_expr, 0),
23806 struct_type);
23808 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
23809 if (!get_discr_value (integer_cst,
23810 &new_node->dw_discr_lower_bound))
23811 goto abort;
23812 new_node->dw_discr_range = false;
23815 else if (TREE_CODE (match_expr) == TRUTH_ANDIF_EXPR)
23817 /* We are matching:
23818 <discr_field> > <integer_cst>
23819 && <discr_field> < <integer_cst>.
23820 This sub-expression matches the range of values between the
23821 two matched integer constants. Note that comparisons can be
23822 inclusive or exclusive. */
23823 tree candidate_discr_1, candidate_discr_2;
23824 tree lower_cst, upper_cst;
23825 bool lower_cst_included, upper_cst_included;
23826 tree lower_op = TREE_OPERAND (match_expr, 0);
23827 tree upper_op = TREE_OPERAND (match_expr, 1);
23829 /* When the comparison is exclusive, the integer constant is not
23830 the discriminant range bound we are looking for: we will have
23831 to increment or decrement it. */
23832 if (TREE_CODE (lower_op) == GE_EXPR)
23833 lower_cst_included = true;
23834 else if (TREE_CODE (lower_op) == GT_EXPR)
23835 lower_cst_included = false;
23836 else
23837 goto abort;
23839 if (TREE_CODE (upper_op) == LE_EXPR)
23840 upper_cst_included = true;
23841 else if (TREE_CODE (upper_op) == LT_EXPR)
23842 upper_cst_included = false;
23843 else
23844 goto abort;
23846 /* Extract the discriminant from the first operand and check it
23847 is consistant with the same analysis in the second
23848 operand. */
23849 candidate_discr_1
23850 = analyze_discr_in_predicate (TREE_OPERAND (lower_op, 0),
23851 struct_type);
23852 candidate_discr_2
23853 = analyze_discr_in_predicate (TREE_OPERAND (upper_op, 0),
23854 struct_type);
23855 if (candidate_discr_1 == candidate_discr_2)
23856 candidate_discr = candidate_discr_1;
23857 else
23858 goto abort;
23860 /* Extract bounds from both. */
23861 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
23862 lower_cst = TREE_OPERAND (lower_op, 1);
23863 upper_cst = TREE_OPERAND (upper_op, 1);
23865 if (!lower_cst_included)
23866 lower_cst
23867 = fold_build2 (PLUS_EXPR, TREE_TYPE (lower_cst), lower_cst,
23868 build_int_cst (TREE_TYPE (lower_cst), 1));
23869 if (!upper_cst_included)
23870 upper_cst
23871 = fold_build2 (MINUS_EXPR, TREE_TYPE (upper_cst), upper_cst,
23872 build_int_cst (TREE_TYPE (upper_cst), 1));
23874 if (!get_discr_value (lower_cst,
23875 &new_node->dw_discr_lower_bound)
23876 || !get_discr_value (upper_cst,
23877 &new_node->dw_discr_upper_bound))
23878 goto abort;
23880 new_node->dw_discr_range = true;
23883 else
23884 /* Unsupported sub-expression: we cannot determine the set of
23885 matching discriminant values. Abort everything. */
23886 goto abort;
23888 /* If the discriminant info is not consistant with what we saw so
23889 far, consider the analysis failed and abort everything. */
23890 if (candidate_discr == NULL_TREE
23891 || (*discr_decl != NULL_TREE && candidate_discr != *discr_decl))
23892 goto abort;
23893 else
23894 *discr_decl = candidate_discr;
23896 if (new_node != NULL)
23898 new_node->dw_discr_next = discr_lists[i];
23899 discr_lists[i] = new_node;
23901 match_expr = next_round_match_expr;
23905 /* If we reach this point, we could match everything we were interested
23906 in. */
23907 return;
23909 abort:
23910 /* Clean all data structure and return no result. */
23911 free (*discr_lists_p);
23912 *discr_lists_p = NULL;
23913 *discr_decl = NULL_TREE;
23916 /* Generate a DIE to represent VARIANT_PART_DECL, a variant part that is part
23917 of STRUCT_TYPE, a record type. This new DIE is emitted as the next child
23918 under CONTEXT_DIE.
23920 Variant parts are supposed to be implemented as a FIELD_DECL whose type is a
23921 QUAL_UNION_TYPE: this is the VARIANT_PART_DECL parameter. The members for
23922 this type, which are record types, represent the available variants and each
23923 has a DECL_QUALIFIER attribute. The discriminant and the discriminant
23924 values are inferred from these attributes.
23926 In trees, the offsets for the fields inside these sub-records are relative
23927 to the variant part itself, whereas the corresponding DIEs should have
23928 offset attributes that are relative to the embedding record base address.
23929 This is why the caller must provide a VARIANT_PART_OFFSET expression: it
23930 must be an expression that computes the offset of the variant part to
23931 describe in DWARF. */
23933 static void
23934 gen_variant_part (tree variant_part_decl, struct vlr_context *vlr_ctx,
23935 dw_die_ref context_die)
23937 const tree variant_part_type = TREE_TYPE (variant_part_decl);
23938 tree variant_part_offset = vlr_ctx->variant_part_offset;
23939 struct loc_descr_context ctx = {
23940 vlr_ctx->struct_type, /* context_type */
23941 NULL_TREE, /* base_decl */
23942 NULL, /* dpi */
23943 false, /* placeholder_arg */
23944 false /* placeholder_seen */
23947 /* The FIELD_DECL node in STRUCT_TYPE that acts as the discriminant, or
23948 NULL_TREE if there is no such field. */
23949 tree discr_decl = NULL_TREE;
23950 dw_discr_list_ref *discr_lists;
23951 unsigned discr_lists_length = 0;
23952 unsigned i;
23954 dw_die_ref dwarf_proc_die = NULL;
23955 dw_die_ref variant_part_die
23956 = new_die (DW_TAG_variant_part, context_die, variant_part_type);
23958 equate_decl_number_to_die (variant_part_decl, variant_part_die);
23960 analyze_variants_discr (variant_part_decl, vlr_ctx->struct_type,
23961 &discr_decl, &discr_lists, &discr_lists_length);
23963 if (discr_decl != NULL_TREE)
23965 dw_die_ref discr_die = lookup_decl_die (discr_decl);
23967 if (discr_die)
23968 add_AT_die_ref (variant_part_die, DW_AT_discr, discr_die);
23969 else
23970 /* We have no DIE for the discriminant, so just discard all
23971 discrimimant information in the output. */
23972 discr_decl = NULL_TREE;
23975 /* If the offset for this variant part is more complex than a constant,
23976 create a DWARF procedure for it so that we will not have to generate DWARF
23977 expressions for it for each member. */
23978 if (TREE_CODE (variant_part_offset) != INTEGER_CST
23979 && (dwarf_version >= 3 || !dwarf_strict))
23981 const tree dwarf_proc_fndecl
23982 = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL, NULL_TREE,
23983 build_function_type (TREE_TYPE (variant_part_offset),
23984 NULL_TREE));
23985 const tree dwarf_proc_call = build_call_expr (dwarf_proc_fndecl, 0);
23986 const dw_loc_descr_ref dwarf_proc_body
23987 = loc_descriptor_from_tree (variant_part_offset, 0, &ctx);
23989 dwarf_proc_die = new_dwarf_proc_die (dwarf_proc_body,
23990 dwarf_proc_fndecl, context_die);
23991 if (dwarf_proc_die != NULL)
23992 variant_part_offset = dwarf_proc_call;
23995 /* Output DIEs for all variants. */
23996 i = 0;
23997 for (tree variant = TYPE_FIELDS (variant_part_type);
23998 variant != NULL_TREE;
23999 variant = DECL_CHAIN (variant), ++i)
24001 tree variant_type = TREE_TYPE (variant);
24002 dw_die_ref variant_die;
24004 /* All variants (i.e. members of a variant part) are supposed to be
24005 encoded as structures. Sub-variant parts are QUAL_UNION_TYPE fields
24006 under these records. */
24007 gcc_assert (TREE_CODE (variant_type) == RECORD_TYPE);
24009 variant_die = new_die (DW_TAG_variant, variant_part_die, variant_type);
24010 equate_decl_number_to_die (variant, variant_die);
24012 /* Output discriminant values this variant matches, if any. */
24013 if (discr_decl == NULL || discr_lists[i] == NULL)
24014 /* In the case we have discriminant information at all, this is
24015 probably the default variant: as the standard says, don't
24016 output any discriminant value/list attribute. */
24018 else if (discr_lists[i]->dw_discr_next == NULL
24019 && !discr_lists[i]->dw_discr_range)
24020 /* If there is only one accepted value, don't bother outputting a
24021 list. */
24022 add_discr_value (variant_die, &discr_lists[i]->dw_discr_lower_bound);
24023 else
24024 add_discr_list (variant_die, discr_lists[i]);
24026 for (tree member = TYPE_FIELDS (variant_type);
24027 member != NULL_TREE;
24028 member = DECL_CHAIN (member))
24030 struct vlr_context vlr_sub_ctx = {
24031 vlr_ctx->struct_type, /* struct_type */
24032 NULL /* variant_part_offset */
24034 if (is_variant_part (member))
24036 /* All offsets for fields inside variant parts are relative to
24037 the top-level embedding RECORD_TYPE's base address. On the
24038 other hand, offsets in GCC's types are relative to the
24039 nested-most variant part. So we have to sum offsets each time
24040 we recurse. */
24042 vlr_sub_ctx.variant_part_offset
24043 = fold_build2 (PLUS_EXPR, TREE_TYPE (variant_part_offset),
24044 variant_part_offset, byte_position (member));
24045 gen_variant_part (member, &vlr_sub_ctx, variant_die);
24047 else
24049 vlr_sub_ctx.variant_part_offset = variant_part_offset;
24050 gen_decl_die (member, NULL, &vlr_sub_ctx, variant_die);
24055 free (discr_lists);
24058 /* Generate a DIE for a class member. */
24060 static void
24061 gen_member_die (tree type, dw_die_ref context_die)
24063 tree member;
24064 tree binfo = TYPE_BINFO (type);
24065 dw_die_ref child;
24067 /* If this is not an incomplete type, output descriptions of each of its
24068 members. Note that as we output the DIEs necessary to represent the
24069 members of this record or union type, we will also be trying to output
24070 DIEs to represent the *types* of those members. However the `type'
24071 function (above) will specifically avoid generating type DIEs for member
24072 types *within* the list of member DIEs for this (containing) type except
24073 for those types (of members) which are explicitly marked as also being
24074 members of this (containing) type themselves. The g++ front- end can
24075 force any given type to be treated as a member of some other (containing)
24076 type by setting the TYPE_CONTEXT of the given (member) type to point to
24077 the TREE node representing the appropriate (containing) type. */
24079 /* First output info about the base classes. */
24080 if (binfo)
24082 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
24083 int i;
24084 tree base;
24086 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
24087 gen_inheritance_die (base,
24088 (accesses ? (*accesses)[i] : access_public_node),
24089 type,
24090 context_die);
24093 /* Now output info about the data members and type members. */
24094 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
24096 struct vlr_context vlr_ctx = { type, NULL_TREE };
24097 bool static_inline_p
24098 = (TREE_STATIC (member)
24099 && (lang_hooks.decls.decl_dwarf_attribute (member, DW_AT_inline)
24100 != -1));
24102 /* If we thought we were generating minimal debug info for TYPE
24103 and then changed our minds, some of the member declarations
24104 may have already been defined. Don't define them again, but
24105 do put them in the right order. */
24107 child = lookup_decl_die (member);
24108 if (child)
24110 /* Handle inline static data members, which only have in-class
24111 declarations. */
24112 dw_die_ref ref = NULL;
24113 if (child->die_tag == DW_TAG_variable
24114 && child->die_parent == comp_unit_die ())
24116 ref = get_AT_ref (child, DW_AT_specification);
24117 /* For C++17 inline static data members followed by redundant
24118 out of class redeclaration, we might get here with
24119 child being the DIE created for the out of class
24120 redeclaration and with its DW_AT_specification being
24121 the DIE created for in-class definition. We want to
24122 reparent the latter, and don't want to create another
24123 DIE with DW_AT_specification in that case, because
24124 we already have one. */
24125 if (ref
24126 && static_inline_p
24127 && ref->die_tag == DW_TAG_variable
24128 && ref->die_parent == comp_unit_die ()
24129 && get_AT (ref, DW_AT_specification) == NULL)
24131 child = ref;
24132 ref = NULL;
24133 static_inline_p = false;
24136 if (child->die_tag == DW_TAG_variable
24137 && child->die_parent == comp_unit_die ()
24138 && ref == NULL)
24140 reparent_child (child, context_die);
24141 if (dwarf_version < 5)
24142 child->die_tag = DW_TAG_member;
24144 else
24145 splice_child_die (context_die, child);
24148 /* Do not generate standard DWARF for variant parts if we are generating
24149 the corresponding GNAT encodings: DIEs generated for both would
24150 conflict in our mappings. */
24151 else if (is_variant_part (member)
24152 && gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL)
24154 vlr_ctx.variant_part_offset = byte_position (member);
24155 gen_variant_part (member, &vlr_ctx, context_die);
24157 else
24159 vlr_ctx.variant_part_offset = NULL_TREE;
24160 gen_decl_die (member, NULL, &vlr_ctx, context_die);
24163 /* For C++ inline static data members emit immediately a DW_TAG_variable
24164 DIE that will refer to that DW_TAG_member/DW_TAG_variable through
24165 DW_AT_specification. */
24166 if (static_inline_p)
24168 int old_extern = DECL_EXTERNAL (member);
24169 DECL_EXTERNAL (member) = 0;
24170 gen_decl_die (member, NULL, NULL, comp_unit_die ());
24171 DECL_EXTERNAL (member) = old_extern;
24175 /* We do not keep type methods in type variants. */
24176 gcc_assert (TYPE_MAIN_VARIANT (type) == type);
24177 /* Now output info about the function members (if any). */
24178 if (TYPE_METHODS (type) != error_mark_node)
24179 for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
24181 /* Don't include clones in the member list. */
24182 if (DECL_ABSTRACT_ORIGIN (member))
24183 continue;
24184 /* Nor constructors for anonymous classes. */
24185 if (DECL_ARTIFICIAL (member)
24186 && dwarf2_name (member, 0) == NULL)
24187 continue;
24189 child = lookup_decl_die (member);
24190 if (child)
24191 splice_child_die (context_die, child);
24192 else
24193 gen_decl_die (member, NULL, NULL, context_die);
24197 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
24198 is set, we pretend that the type was never defined, so we only get the
24199 member DIEs needed by later specification DIEs. */
24201 static void
24202 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
24203 enum debug_info_usage usage)
24205 if (TREE_ASM_WRITTEN (type))
24207 /* Fill in the bound of variable-length fields in late dwarf if
24208 still incomplete. */
24209 if (!early_dwarf && variably_modified_type_p (type, NULL))
24210 for (tree member = TYPE_FIELDS (type);
24211 member;
24212 member = DECL_CHAIN (member))
24213 fill_variable_array_bounds (TREE_TYPE (member));
24214 return;
24217 dw_die_ref type_die = lookup_type_die (type);
24218 dw_die_ref scope_die = 0;
24219 int nested = 0;
24220 int complete = (TYPE_SIZE (type)
24221 && (! TYPE_STUB_DECL (type)
24222 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
24223 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
24224 complete = complete && should_emit_struct_debug (type, usage);
24226 if (type_die && ! complete)
24227 return;
24229 if (TYPE_CONTEXT (type) != NULL_TREE
24230 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
24231 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
24232 nested = 1;
24234 scope_die = scope_die_for (type, context_die);
24236 /* Generate child dies for template paramaters. */
24237 if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
24238 schedule_generic_params_dies_gen (type);
24240 if (! type_die || (nested && is_cu_die (scope_die)))
24241 /* First occurrence of type or toplevel definition of nested class. */
24243 dw_die_ref old_die = type_die;
24245 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
24246 ? record_type_tag (type) : DW_TAG_union_type,
24247 scope_die, type);
24248 equate_type_number_to_die (type, type_die);
24249 if (old_die)
24250 add_AT_specification (type_die, old_die);
24251 else
24252 add_name_attribute (type_die, type_tag (type));
24254 else
24255 remove_AT (type_die, DW_AT_declaration);
24257 /* If this type has been completed, then give it a byte_size attribute and
24258 then give a list of members. */
24259 if (complete && !ns_decl)
24261 /* Prevent infinite recursion in cases where the type of some member of
24262 this type is expressed in terms of this type itself. */
24263 TREE_ASM_WRITTEN (type) = 1;
24264 add_byte_size_attribute (type_die, type);
24265 add_alignment_attribute (type_die, type);
24266 if (TYPE_STUB_DECL (type) != NULL_TREE)
24268 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
24269 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
24272 /* If the first reference to this type was as the return type of an
24273 inline function, then it may not have a parent. Fix this now. */
24274 if (type_die->die_parent == NULL)
24275 add_child_die (scope_die, type_die);
24277 push_decl_scope (type);
24278 gen_member_die (type, type_die);
24279 pop_decl_scope ();
24281 add_gnat_descriptive_type_attribute (type_die, type, context_die);
24282 if (TYPE_ARTIFICIAL (type))
24283 add_AT_flag (type_die, DW_AT_artificial, 1);
24285 /* GNU extension: Record what type our vtable lives in. */
24286 if (TYPE_VFIELD (type))
24288 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
24290 gen_type_die (vtype, context_die);
24291 add_AT_die_ref (type_die, DW_AT_containing_type,
24292 lookup_type_die (vtype));
24295 else
24297 add_AT_flag (type_die, DW_AT_declaration, 1);
24299 /* We don't need to do this for function-local types. */
24300 if (TYPE_STUB_DECL (type)
24301 && ! decl_function_context (TYPE_STUB_DECL (type)))
24302 vec_safe_push (incomplete_types, type);
24305 if (get_AT (type_die, DW_AT_name))
24306 add_pubtype (type, type_die);
24309 /* Generate a DIE for a subroutine _type_. */
24311 static void
24312 gen_subroutine_type_die (tree type, dw_die_ref context_die)
24314 tree return_type = TREE_TYPE (type);
24315 dw_die_ref subr_die
24316 = new_die (DW_TAG_subroutine_type,
24317 scope_die_for (type, context_die), type);
24319 equate_type_number_to_die (type, subr_die);
24320 add_prototyped_attribute (subr_die, type);
24321 add_type_attribute (subr_die, return_type, TYPE_UNQUALIFIED, false,
24322 context_die);
24323 add_alignment_attribute (subr_die, type);
24324 gen_formal_types_die (type, subr_die);
24326 if (get_AT (subr_die, DW_AT_name))
24327 add_pubtype (type, subr_die);
24328 if ((dwarf_version >= 5 || !dwarf_strict)
24329 && lang_hooks.types.type_dwarf_attribute (type, DW_AT_reference) != -1)
24330 add_AT_flag (subr_die, DW_AT_reference, 1);
24331 if ((dwarf_version >= 5 || !dwarf_strict)
24332 && lang_hooks.types.type_dwarf_attribute (type,
24333 DW_AT_rvalue_reference) != -1)
24334 add_AT_flag (subr_die, DW_AT_rvalue_reference, 1);
24337 /* Generate a DIE for a type definition. */
24339 static void
24340 gen_typedef_die (tree decl, dw_die_ref context_die)
24342 dw_die_ref type_die;
24343 tree origin;
24345 if (TREE_ASM_WRITTEN (decl))
24347 if (DECL_ORIGINAL_TYPE (decl))
24348 fill_variable_array_bounds (DECL_ORIGINAL_TYPE (decl));
24349 return;
24352 TREE_ASM_WRITTEN (decl) = 1;
24353 type_die = new_die (DW_TAG_typedef, context_die, decl);
24354 origin = decl_ultimate_origin (decl);
24355 if (origin != NULL)
24356 add_abstract_origin_attribute (type_die, origin);
24357 else
24359 tree type = TREE_TYPE (decl);
24361 if (type == error_mark_node)
24362 return;
24364 add_name_and_src_coords_attributes (type_die, decl);
24365 if (DECL_ORIGINAL_TYPE (decl))
24367 type = DECL_ORIGINAL_TYPE (decl);
24369 if (type == error_mark_node)
24370 return;
24372 gcc_assert (type != TREE_TYPE (decl));
24373 equate_type_number_to_die (TREE_TYPE (decl), type_die);
24375 else
24377 if (is_naming_typedef_decl (TYPE_NAME (type)))
24379 /* Here, we are in the case of decl being a typedef naming
24380 an anonymous type, e.g:
24381 typedef struct {...} foo;
24382 In that case TREE_TYPE (decl) is not a typedef variant
24383 type and TYPE_NAME of the anonymous type is set to the
24384 TYPE_DECL of the typedef. This construct is emitted by
24385 the C++ FE.
24387 TYPE is the anonymous struct named by the typedef
24388 DECL. As we need the DW_AT_type attribute of the
24389 DW_TAG_typedef to point to the DIE of TYPE, let's
24390 generate that DIE right away. add_type_attribute
24391 called below will then pick (via lookup_type_die) that
24392 anonymous struct DIE. */
24393 if (!TREE_ASM_WRITTEN (type))
24394 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
24396 /* This is a GNU Extension. We are adding a
24397 DW_AT_linkage_name attribute to the DIE of the
24398 anonymous struct TYPE. The value of that attribute
24399 is the name of the typedef decl naming the anonymous
24400 struct. This greatly eases the work of consumers of
24401 this debug info. */
24402 add_linkage_name_raw (lookup_type_die (type), decl);
24406 add_type_attribute (type_die, type, decl_quals (decl), false,
24407 context_die);
24409 if (is_naming_typedef_decl (decl))
24410 /* We want that all subsequent calls to lookup_type_die with
24411 TYPE in argument yield the DW_TAG_typedef we have just
24412 created. */
24413 equate_type_number_to_die (type, type_die);
24415 type = TREE_TYPE (decl);
24417 add_alignment_attribute (type_die, type);
24419 add_accessibility_attribute (type_die, decl);
24422 if (DECL_ABSTRACT_P (decl))
24423 equate_decl_number_to_die (decl, type_die);
24425 if (get_AT (type_die, DW_AT_name))
24426 add_pubtype (decl, type_die);
24429 /* Generate a DIE for a struct, class, enum or union type. */
24431 static void
24432 gen_tagged_type_die (tree type,
24433 dw_die_ref context_die,
24434 enum debug_info_usage usage)
24436 int need_pop;
24438 if (type == NULL_TREE
24439 || !is_tagged_type (type))
24440 return;
24442 if (TREE_ASM_WRITTEN (type))
24443 need_pop = 0;
24444 /* If this is a nested type whose containing class hasn't been written
24445 out yet, writing it out will cover this one, too. This does not apply
24446 to instantiations of member class templates; they need to be added to
24447 the containing class as they are generated. FIXME: This hurts the
24448 idea of combining type decls from multiple TUs, since we can't predict
24449 what set of template instantiations we'll get. */
24450 else if (TYPE_CONTEXT (type)
24451 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
24452 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
24454 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
24456 if (TREE_ASM_WRITTEN (type))
24457 return;
24459 /* If that failed, attach ourselves to the stub. */
24460 push_decl_scope (TYPE_CONTEXT (type));
24461 context_die = lookup_type_die (TYPE_CONTEXT (type));
24462 need_pop = 1;
24464 else if (TYPE_CONTEXT (type) != NULL_TREE
24465 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
24467 /* If this type is local to a function that hasn't been written
24468 out yet, use a NULL context for now; it will be fixed up in
24469 decls_for_scope. */
24470 context_die = lookup_decl_die (TYPE_CONTEXT (type));
24471 /* A declaration DIE doesn't count; nested types need to go in the
24472 specification. */
24473 if (context_die && is_declaration_die (context_die))
24474 context_die = NULL;
24475 need_pop = 0;
24477 else
24479 context_die = declare_in_namespace (type, context_die);
24480 need_pop = 0;
24483 if (TREE_CODE (type) == ENUMERAL_TYPE)
24485 /* This might have been written out by the call to
24486 declare_in_namespace. */
24487 if (!TREE_ASM_WRITTEN (type))
24488 gen_enumeration_type_die (type, context_die);
24490 else
24491 gen_struct_or_union_type_die (type, context_die, usage);
24493 if (need_pop)
24494 pop_decl_scope ();
24496 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
24497 it up if it is ever completed. gen_*_type_die will set it for us
24498 when appropriate. */
24501 /* Generate a type description DIE. */
24503 static void
24504 gen_type_die_with_usage (tree type, dw_die_ref context_die,
24505 enum debug_info_usage usage)
24507 struct array_descr_info info;
24509 if (type == NULL_TREE || type == error_mark_node)
24510 return;
24512 if (flag_checking && type)
24513 verify_type (type);
24515 if (TYPE_NAME (type) != NULL_TREE
24516 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
24517 && is_redundant_typedef (TYPE_NAME (type))
24518 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
24519 /* The DECL of this type is a typedef we don't want to emit debug
24520 info for but we want debug info for its underlying typedef.
24521 This can happen for e.g, the injected-class-name of a C++
24522 type. */
24523 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
24525 /* If TYPE is a typedef type variant, let's generate debug info
24526 for the parent typedef which TYPE is a type of. */
24527 if (typedef_variant_p (type))
24529 if (TREE_ASM_WRITTEN (type))
24530 return;
24532 /* Prevent broken recursion; we can't hand off to the same type. */
24533 gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
24535 /* Give typedefs the right scope. */
24536 context_die = scope_die_for (type, context_die);
24538 TREE_ASM_WRITTEN (type) = 1;
24540 gen_decl_die (TYPE_NAME (type), NULL, NULL, context_die);
24541 return;
24544 /* If type is an anonymous tagged type named by a typedef, let's
24545 generate debug info for the typedef. */
24546 if (is_naming_typedef_decl (TYPE_NAME (type)))
24548 /* Use the DIE of the containing namespace as the parent DIE of
24549 the type description DIE we want to generate. */
24550 if (DECL_CONTEXT (TYPE_NAME (type))
24551 && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
24552 context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
24554 gen_decl_die (TYPE_NAME (type), NULL, NULL, context_die);
24555 return;
24558 if (lang_hooks.types.get_debug_type)
24560 tree debug_type = lang_hooks.types.get_debug_type (type);
24562 if (debug_type != NULL_TREE && debug_type != type)
24564 gen_type_die_with_usage (debug_type, context_die, usage);
24565 return;
24569 /* We are going to output a DIE to represent the unqualified version
24570 of this type (i.e. without any const or volatile qualifiers) so
24571 get the main variant (i.e. the unqualified version) of this type
24572 now. (Vectors and arrays are special because the debugging info is in the
24573 cloned type itself. Similarly function/method types can contain extra
24574 ref-qualification). */
24575 if (TREE_CODE (type) == FUNCTION_TYPE
24576 || TREE_CODE (type) == METHOD_TYPE)
24578 /* For function/method types, can't use type_main_variant here,
24579 because that can have different ref-qualifiers for C++,
24580 but try to canonicalize. */
24581 tree main = TYPE_MAIN_VARIANT (type);
24582 for (tree t = main; t; t = TYPE_NEXT_VARIANT (t))
24584 if (check_base_type (t, main) && check_lang_type (t, type))
24586 type = t;
24587 break;
24591 else if (TREE_CODE (type) != VECTOR_TYPE
24592 && TREE_CODE (type) != ARRAY_TYPE)
24593 type = type_main_variant (type);
24595 /* If this is an array type with hidden descriptor, handle it first. */
24596 if (!TREE_ASM_WRITTEN (type)
24597 && lang_hooks.types.get_array_descr_info)
24599 memset (&info, 0, sizeof (info));
24600 if (lang_hooks.types.get_array_descr_info (type, &info))
24602 /* Fortran sometimes emits array types with no dimension. */
24603 gcc_assert (info.ndimensions >= 0
24604 && (info.ndimensions
24605 <= DWARF2OUT_ARRAY_DESCR_INFO_MAX_DIMEN));
24606 gen_descr_array_type_die (type, &info, context_die);
24607 TREE_ASM_WRITTEN (type) = 1;
24608 return;
24612 if (TREE_ASM_WRITTEN (type))
24614 /* Variable-length types may be incomplete even if
24615 TREE_ASM_WRITTEN. For such types, fall through to
24616 gen_array_type_die() and possibly fill in
24617 DW_AT_{upper,lower}_bound attributes. */
24618 if ((TREE_CODE (type) != ARRAY_TYPE
24619 && TREE_CODE (type) != RECORD_TYPE
24620 && TREE_CODE (type) != UNION_TYPE
24621 && TREE_CODE (type) != QUAL_UNION_TYPE)
24622 || !variably_modified_type_p (type, NULL))
24623 return;
24626 switch (TREE_CODE (type))
24628 case ERROR_MARK:
24629 break;
24631 case POINTER_TYPE:
24632 case REFERENCE_TYPE:
24633 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
24634 ensures that the gen_type_die recursion will terminate even if the
24635 type is recursive. Recursive types are possible in Ada. */
24636 /* ??? We could perhaps do this for all types before the switch
24637 statement. */
24638 TREE_ASM_WRITTEN (type) = 1;
24640 /* For these types, all that is required is that we output a DIE (or a
24641 set of DIEs) to represent the "basis" type. */
24642 gen_type_die_with_usage (TREE_TYPE (type), context_die,
24643 DINFO_USAGE_IND_USE);
24644 break;
24646 case OFFSET_TYPE:
24647 /* This code is used for C++ pointer-to-data-member types.
24648 Output a description of the relevant class type. */
24649 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
24650 DINFO_USAGE_IND_USE);
24652 /* Output a description of the type of the object pointed to. */
24653 gen_type_die_with_usage (TREE_TYPE (type), context_die,
24654 DINFO_USAGE_IND_USE);
24656 /* Now output a DIE to represent this pointer-to-data-member type
24657 itself. */
24658 gen_ptr_to_mbr_type_die (type, context_die);
24659 break;
24661 case FUNCTION_TYPE:
24662 /* Force out return type (in case it wasn't forced out already). */
24663 gen_type_die_with_usage (TREE_TYPE (type), context_die,
24664 DINFO_USAGE_DIR_USE);
24665 gen_subroutine_type_die (type, context_die);
24666 break;
24668 case METHOD_TYPE:
24669 /* Force out return type (in case it wasn't forced out already). */
24670 gen_type_die_with_usage (TREE_TYPE (type), context_die,
24671 DINFO_USAGE_DIR_USE);
24672 gen_subroutine_type_die (type, context_die);
24673 break;
24675 case ARRAY_TYPE:
24676 case VECTOR_TYPE:
24677 gen_array_type_die (type, context_die);
24678 break;
24680 case ENUMERAL_TYPE:
24681 case RECORD_TYPE:
24682 case UNION_TYPE:
24683 case QUAL_UNION_TYPE:
24684 gen_tagged_type_die (type, context_die, usage);
24685 return;
24687 case VOID_TYPE:
24688 case INTEGER_TYPE:
24689 case REAL_TYPE:
24690 case FIXED_POINT_TYPE:
24691 case COMPLEX_TYPE:
24692 case BOOLEAN_TYPE:
24693 case POINTER_BOUNDS_TYPE:
24694 /* No DIEs needed for fundamental types. */
24695 break;
24697 case NULLPTR_TYPE:
24698 case LANG_TYPE:
24699 /* Just use DW_TAG_unspecified_type. */
24701 dw_die_ref type_die = lookup_type_die (type);
24702 if (type_die == NULL)
24704 tree name = TYPE_IDENTIFIER (type);
24705 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (),
24706 type);
24707 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
24708 equate_type_number_to_die (type, type_die);
24711 break;
24713 default:
24714 if (is_cxx_auto (type))
24716 tree name = TYPE_IDENTIFIER (type);
24717 dw_die_ref *die = (name == get_identifier ("auto")
24718 ? &auto_die : &decltype_auto_die);
24719 if (!*die)
24721 *die = new_die (DW_TAG_unspecified_type,
24722 comp_unit_die (), NULL_TREE);
24723 add_name_attribute (*die, IDENTIFIER_POINTER (name));
24725 equate_type_number_to_die (type, *die);
24726 break;
24728 gcc_unreachable ();
24731 TREE_ASM_WRITTEN (type) = 1;
24734 static void
24735 gen_type_die (tree type, dw_die_ref context_die)
24737 if (type != error_mark_node)
24739 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
24740 if (flag_checking)
24742 dw_die_ref die = lookup_type_die (type);
24743 if (die)
24744 check_die (die);
24749 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
24750 things which are local to the given block. */
24752 static void
24753 gen_block_die (tree stmt, dw_die_ref context_die)
24755 int must_output_die = 0;
24756 bool inlined_func;
24758 /* Ignore blocks that are NULL. */
24759 if (stmt == NULL_TREE)
24760 return;
24762 inlined_func = inlined_function_outer_scope_p (stmt);
24764 /* If the block is one fragment of a non-contiguous block, do not
24765 process the variables, since they will have been done by the
24766 origin block. Do process subblocks. */
24767 if (BLOCK_FRAGMENT_ORIGIN (stmt))
24769 tree sub;
24771 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
24772 gen_block_die (sub, context_die);
24774 return;
24777 /* Determine if we need to output any Dwarf DIEs at all to represent this
24778 block. */
24779 if (inlined_func)
24780 /* The outer scopes for inlinings *must* always be represented. We
24781 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
24782 must_output_die = 1;
24783 else
24785 /* Determine if this block directly contains any "significant"
24786 local declarations which we will need to output DIEs for. */
24787 if (debug_info_level > DINFO_LEVEL_TERSE)
24788 /* We are not in terse mode so *any* local declaration counts
24789 as being a "significant" one. */
24790 must_output_die = ((BLOCK_VARS (stmt) != NULL
24791 || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
24792 && (TREE_USED (stmt)
24793 || TREE_ASM_WRITTEN (stmt)
24794 || BLOCK_ABSTRACT (stmt)));
24795 else if ((TREE_USED (stmt)
24796 || TREE_ASM_WRITTEN (stmt)
24797 || BLOCK_ABSTRACT (stmt))
24798 && !dwarf2out_ignore_block (stmt))
24799 must_output_die = 1;
24802 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
24803 DIE for any block which contains no significant local declarations at
24804 all. Rather, in such cases we just call `decls_for_scope' so that any
24805 needed Dwarf info for any sub-blocks will get properly generated. Note
24806 that in terse mode, our definition of what constitutes a "significant"
24807 local declaration gets restricted to include only inlined function
24808 instances and local (nested) function definitions. */
24809 if (must_output_die)
24811 if (inlined_func)
24813 /* If STMT block is abstract, that means we have been called
24814 indirectly from dwarf2out_abstract_function.
24815 That function rightfully marks the descendent blocks (of
24816 the abstract function it is dealing with) as being abstract,
24817 precisely to prevent us from emitting any
24818 DW_TAG_inlined_subroutine DIE as a descendent
24819 of an abstract function instance. So in that case, we should
24820 not call gen_inlined_subroutine_die.
24822 Later though, when cgraph asks dwarf2out to emit info
24823 for the concrete instance of the function decl into which
24824 the concrete instance of STMT got inlined, the later will lead
24825 to the generation of a DW_TAG_inlined_subroutine DIE. */
24826 if (! BLOCK_ABSTRACT (stmt))
24827 gen_inlined_subroutine_die (stmt, context_die);
24829 else
24830 gen_lexical_block_die (stmt, context_die);
24832 else
24833 decls_for_scope (stmt, context_die);
24836 /* Process variable DECL (or variable with origin ORIGIN) within
24837 block STMT and add it to CONTEXT_DIE. */
24838 static void
24839 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
24841 dw_die_ref die;
24842 tree decl_or_origin = decl ? decl : origin;
24844 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
24845 die = lookup_decl_die (decl_or_origin);
24846 else if (TREE_CODE (decl_or_origin) == TYPE_DECL)
24848 if (TYPE_DECL_IS_STUB (decl_or_origin))
24849 die = lookup_type_die (TREE_TYPE (decl_or_origin));
24850 else
24851 die = lookup_decl_die (decl_or_origin);
24852 /* Avoid re-creating the DIE late if it was optimized as unused early. */
24853 if (! die && ! early_dwarf)
24854 return;
24856 else
24857 die = NULL;
24859 if (die != NULL && die->die_parent == NULL)
24860 add_child_die (context_die, die);
24861 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
24863 if (early_dwarf)
24864 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
24865 stmt, context_die);
24867 else
24868 gen_decl_die (decl, origin, NULL, context_die);
24871 /* Generate all of the decls declared within a given scope and (recursively)
24872 all of its sub-blocks. */
24874 static void
24875 decls_for_scope (tree stmt, dw_die_ref context_die)
24877 tree decl;
24878 unsigned int i;
24879 tree subblocks;
24881 /* Ignore NULL blocks. */
24882 if (stmt == NULL_TREE)
24883 return;
24885 /* Output the DIEs to represent all of the data objects and typedefs
24886 declared directly within this block but not within any nested
24887 sub-blocks. Also, nested function and tag DIEs have been
24888 generated with a parent of NULL; fix that up now. We don't
24889 have to do this if we're at -g1. */
24890 if (debug_info_level > DINFO_LEVEL_TERSE)
24892 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
24893 process_scope_var (stmt, decl, NULL_TREE, context_die);
24894 /* BLOCK_NONLOCALIZED_VARs simply generate DIE stubs with abstract
24895 origin - avoid doing this twice as we have no good way to see
24896 if we've done it once already. */
24897 if (! early_dwarf)
24898 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
24900 decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
24901 if (decl == current_function_decl)
24902 /* Ignore declarations of the current function, while they
24903 are declarations, gen_subprogram_die would treat them
24904 as definitions again, because they are equal to
24905 current_function_decl and endlessly recurse. */;
24906 else if (TREE_CODE (decl) == FUNCTION_DECL)
24907 process_scope_var (stmt, decl, NULL_TREE, context_die);
24908 else
24909 process_scope_var (stmt, NULL_TREE, decl, context_die);
24913 /* Even if we're at -g1, we need to process the subblocks in order to get
24914 inlined call information. */
24916 /* Output the DIEs to represent all sub-blocks (and the items declared
24917 therein) of this block. */
24918 for (subblocks = BLOCK_SUBBLOCKS (stmt);
24919 subblocks != NULL;
24920 subblocks = BLOCK_CHAIN (subblocks))
24921 gen_block_die (subblocks, context_die);
24924 /* Is this a typedef we can avoid emitting? */
24926 bool
24927 is_redundant_typedef (const_tree decl)
24929 if (TYPE_DECL_IS_STUB (decl))
24930 return true;
24932 if (DECL_ARTIFICIAL (decl)
24933 && DECL_CONTEXT (decl)
24934 && is_tagged_type (DECL_CONTEXT (decl))
24935 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
24936 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
24937 /* Also ignore the artificial member typedef for the class name. */
24938 return true;
24940 return false;
24943 /* Return TRUE if TYPE is a typedef that names a type for linkage
24944 purposes. This kind of typedefs is produced by the C++ FE for
24945 constructs like:
24947 typedef struct {...} foo;
24949 In that case, there is no typedef variant type produced for foo.
24950 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
24951 struct type. */
24953 static bool
24954 is_naming_typedef_decl (const_tree decl)
24956 if (decl == NULL_TREE
24957 || TREE_CODE (decl) != TYPE_DECL
24958 || DECL_NAMELESS (decl)
24959 || !is_tagged_type (TREE_TYPE (decl))
24960 || DECL_IS_BUILTIN (decl)
24961 || is_redundant_typedef (decl)
24962 /* It looks like Ada produces TYPE_DECLs that are very similar
24963 to C++ naming typedefs but that have different
24964 semantics. Let's be specific to c++ for now. */
24965 || !is_cxx (decl))
24966 return FALSE;
24968 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
24969 && TYPE_NAME (TREE_TYPE (decl)) == decl
24970 && (TYPE_STUB_DECL (TREE_TYPE (decl))
24971 != TYPE_NAME (TREE_TYPE (decl))));
24974 /* Looks up the DIE for a context. */
24976 static inline dw_die_ref
24977 lookup_context_die (tree context)
24979 if (context)
24981 /* Find die that represents this context. */
24982 if (TYPE_P (context))
24984 context = TYPE_MAIN_VARIANT (context);
24985 dw_die_ref ctx = lookup_type_die (context);
24986 if (!ctx)
24987 return NULL;
24988 return strip_naming_typedef (context, ctx);
24990 else
24991 return lookup_decl_die (context);
24993 return comp_unit_die ();
24996 /* Returns the DIE for a context. */
24998 static inline dw_die_ref
24999 get_context_die (tree context)
25001 if (context)
25003 /* Find die that represents this context. */
25004 if (TYPE_P (context))
25006 context = TYPE_MAIN_VARIANT (context);
25007 return strip_naming_typedef (context, force_type_die (context));
25009 else
25010 return force_decl_die (context);
25012 return comp_unit_die ();
25015 /* Returns the DIE for decl. A DIE will always be returned. */
25017 static dw_die_ref
25018 force_decl_die (tree decl)
25020 dw_die_ref decl_die;
25021 unsigned saved_external_flag;
25022 tree save_fn = NULL_TREE;
25023 decl_die = lookup_decl_die (decl);
25024 if (!decl_die)
25026 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
25028 decl_die = lookup_decl_die (decl);
25029 if (decl_die)
25030 return decl_die;
25032 switch (TREE_CODE (decl))
25034 case FUNCTION_DECL:
25035 /* Clear current_function_decl, so that gen_subprogram_die thinks
25036 that this is a declaration. At this point, we just want to force
25037 declaration die. */
25038 save_fn = current_function_decl;
25039 current_function_decl = NULL_TREE;
25040 gen_subprogram_die (decl, context_die);
25041 current_function_decl = save_fn;
25042 break;
25044 case VAR_DECL:
25045 /* Set external flag to force declaration die. Restore it after
25046 gen_decl_die() call. */
25047 saved_external_flag = DECL_EXTERNAL (decl);
25048 DECL_EXTERNAL (decl) = 1;
25049 gen_decl_die (decl, NULL, NULL, context_die);
25050 DECL_EXTERNAL (decl) = saved_external_flag;
25051 break;
25053 case NAMESPACE_DECL:
25054 if (dwarf_version >= 3 || !dwarf_strict)
25055 dwarf2out_decl (decl);
25056 else
25057 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
25058 decl_die = comp_unit_die ();
25059 break;
25061 case TRANSLATION_UNIT_DECL:
25062 decl_die = comp_unit_die ();
25063 break;
25065 default:
25066 gcc_unreachable ();
25069 /* We should be able to find the DIE now. */
25070 if (!decl_die)
25071 decl_die = lookup_decl_die (decl);
25072 gcc_assert (decl_die);
25075 return decl_die;
25078 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
25079 always returned. */
25081 static dw_die_ref
25082 force_type_die (tree type)
25084 dw_die_ref type_die;
25086 type_die = lookup_type_die (type);
25087 if (!type_die)
25089 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
25091 type_die = modified_type_die (type, TYPE_QUALS_NO_ADDR_SPACE (type),
25092 false, context_die);
25093 gcc_assert (type_die);
25095 return type_die;
25098 /* Force out any required namespaces to be able to output DECL,
25099 and return the new context_die for it, if it's changed. */
25101 static dw_die_ref
25102 setup_namespace_context (tree thing, dw_die_ref context_die)
25104 tree context = (DECL_P (thing)
25105 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
25106 if (context && TREE_CODE (context) == NAMESPACE_DECL)
25107 /* Force out the namespace. */
25108 context_die = force_decl_die (context);
25110 return context_die;
25113 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
25114 type) within its namespace, if appropriate.
25116 For compatibility with older debuggers, namespace DIEs only contain
25117 declarations; all definitions are emitted at CU scope, with
25118 DW_AT_specification pointing to the declaration (like with class
25119 members). */
25121 static dw_die_ref
25122 declare_in_namespace (tree thing, dw_die_ref context_die)
25124 dw_die_ref ns_context;
25126 if (debug_info_level <= DINFO_LEVEL_TERSE)
25127 return context_die;
25129 /* External declarations in the local scope only need to be emitted
25130 once, not once in the namespace and once in the scope.
25132 This avoids declaring the `extern' below in the
25133 namespace DIE as well as in the innermost scope:
25135 namespace S
25137 int i=5;
25138 int foo()
25140 int i=8;
25141 extern int i;
25142 return i;
25146 if (DECL_P (thing) && DECL_EXTERNAL (thing) && local_scope_p (context_die))
25147 return context_die;
25149 /* If this decl is from an inlined function, then don't try to emit it in its
25150 namespace, as we will get confused. It would have already been emitted
25151 when the abstract instance of the inline function was emitted anyways. */
25152 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
25153 return context_die;
25155 ns_context = setup_namespace_context (thing, context_die);
25157 if (ns_context != context_die)
25159 if (is_fortran ())
25160 return ns_context;
25161 if (DECL_P (thing))
25162 gen_decl_die (thing, NULL, NULL, ns_context);
25163 else
25164 gen_type_die (thing, ns_context);
25166 return context_die;
25169 /* Generate a DIE for a namespace or namespace alias. */
25171 static void
25172 gen_namespace_die (tree decl, dw_die_ref context_die)
25174 dw_die_ref namespace_die;
25176 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
25177 they are an alias of. */
25178 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
25180 /* Output a real namespace or module. */
25181 context_die = setup_namespace_context (decl, comp_unit_die ());
25182 namespace_die = new_die (is_fortran ()
25183 ? DW_TAG_module : DW_TAG_namespace,
25184 context_die, decl);
25185 /* For Fortran modules defined in different CU don't add src coords. */
25186 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
25188 const char *name = dwarf2_name (decl, 0);
25189 if (name)
25190 add_name_attribute (namespace_die, name);
25192 else
25193 add_name_and_src_coords_attributes (namespace_die, decl);
25194 if (DECL_EXTERNAL (decl))
25195 add_AT_flag (namespace_die, DW_AT_declaration, 1);
25196 equate_decl_number_to_die (decl, namespace_die);
25198 else
25200 /* Output a namespace alias. */
25202 /* Force out the namespace we are an alias of, if necessary. */
25203 dw_die_ref origin_die
25204 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
25206 if (DECL_FILE_SCOPE_P (decl)
25207 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
25208 context_die = setup_namespace_context (decl, comp_unit_die ());
25209 /* Now create the namespace alias DIE. */
25210 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
25211 add_name_and_src_coords_attributes (namespace_die, decl);
25212 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
25213 equate_decl_number_to_die (decl, namespace_die);
25215 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
25216 if (want_pubnames ())
25217 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
25220 /* Generate Dwarf debug information for a decl described by DECL.
25221 The return value is currently only meaningful for PARM_DECLs,
25222 for all other decls it returns NULL.
25224 If DECL is a FIELD_DECL, CTX is required: see the comment for VLR_CONTEXT.
25225 It can be NULL otherwise. */
25227 static dw_die_ref
25228 gen_decl_die (tree decl, tree origin, struct vlr_context *ctx,
25229 dw_die_ref context_die)
25231 tree decl_or_origin = decl ? decl : origin;
25232 tree class_origin = NULL, ultimate_origin;
25234 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
25235 return NULL;
25237 /* Ignore pointer bounds decls. */
25238 if (DECL_P (decl_or_origin)
25239 && TREE_TYPE (decl_or_origin)
25240 && POINTER_BOUNDS_P (decl_or_origin))
25241 return NULL;
25243 switch (TREE_CODE (decl_or_origin))
25245 case ERROR_MARK:
25246 break;
25248 case CONST_DECL:
25249 if (!is_fortran () && !is_ada ())
25251 /* The individual enumerators of an enum type get output when we output
25252 the Dwarf representation of the relevant enum type itself. */
25253 break;
25256 /* Emit its type. */
25257 gen_type_die (TREE_TYPE (decl), context_die);
25259 /* And its containing namespace. */
25260 context_die = declare_in_namespace (decl, context_die);
25262 gen_const_die (decl, context_die);
25263 break;
25265 case FUNCTION_DECL:
25266 /* Don't output any DIEs to represent mere function declarations,
25267 unless they are class members or explicit block externs. */
25268 if (DECL_INITIAL (decl_or_origin) == NULL_TREE
25269 && DECL_FILE_SCOPE_P (decl_or_origin)
25270 && (current_function_decl == NULL_TREE
25271 || DECL_ARTIFICIAL (decl_or_origin)))
25272 break;
25274 #if 0
25275 /* FIXME */
25276 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
25277 on local redeclarations of global functions. That seems broken. */
25278 if (current_function_decl != decl)
25279 /* This is only a declaration. */;
25280 #endif
25282 /* If we're emitting a clone, emit info for the abstract instance. */
25283 if (origin || DECL_ORIGIN (decl) != decl)
25284 dwarf2out_abstract_function (origin
25285 ? DECL_ORIGIN (origin)
25286 : DECL_ABSTRACT_ORIGIN (decl));
25288 /* If we're emitting an out-of-line copy of an inline function,
25289 emit info for the abstract instance and set up to refer to it. */
25290 else if (cgraph_function_possibly_inlined_p (decl)
25291 && ! DECL_ABSTRACT_P (decl)
25292 && ! class_or_namespace_scope_p (context_die)
25293 /* dwarf2out_abstract_function won't emit a die if this is just
25294 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
25295 that case, because that works only if we have a die. */
25296 && DECL_INITIAL (decl) != NULL_TREE)
25298 dwarf2out_abstract_function (decl);
25299 set_decl_origin_self (decl);
25302 /* Otherwise we're emitting the primary DIE for this decl. */
25303 else if (debug_info_level > DINFO_LEVEL_TERSE)
25305 /* Before we describe the FUNCTION_DECL itself, make sure that we
25306 have its containing type. */
25307 if (!origin)
25308 origin = decl_class_context (decl);
25309 if (origin != NULL_TREE)
25310 gen_type_die (origin, context_die);
25312 /* And its return type. */
25313 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
25315 /* And its virtual context. */
25316 if (DECL_VINDEX (decl) != NULL_TREE)
25317 gen_type_die (DECL_CONTEXT (decl), context_die);
25319 /* Make sure we have a member DIE for decl. */
25320 if (origin != NULL_TREE)
25321 gen_type_die_for_member (origin, decl, context_die);
25323 /* And its containing namespace. */
25324 context_die = declare_in_namespace (decl, context_die);
25327 /* Now output a DIE to represent the function itself. */
25328 if (decl)
25329 gen_subprogram_die (decl, context_die);
25330 break;
25332 case TYPE_DECL:
25333 /* If we are in terse mode, don't generate any DIEs to represent any
25334 actual typedefs. */
25335 if (debug_info_level <= DINFO_LEVEL_TERSE)
25336 break;
25338 /* In the special case of a TYPE_DECL node representing the declaration
25339 of some type tag, if the given TYPE_DECL is marked as having been
25340 instantiated from some other (original) TYPE_DECL node (e.g. one which
25341 was generated within the original definition of an inline function) we
25342 used to generate a special (abbreviated) DW_TAG_structure_type,
25343 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
25344 should be actually referencing those DIEs, as variable DIEs with that
25345 type would be emitted already in the abstract origin, so it was always
25346 removed during unused type prunning. Don't add anything in this
25347 case. */
25348 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
25349 break;
25351 if (is_redundant_typedef (decl))
25352 gen_type_die (TREE_TYPE (decl), context_die);
25353 else
25354 /* Output a DIE to represent the typedef itself. */
25355 gen_typedef_die (decl, context_die);
25356 break;
25358 case LABEL_DECL:
25359 if (debug_info_level >= DINFO_LEVEL_NORMAL)
25360 gen_label_die (decl, context_die);
25361 break;
25363 case VAR_DECL:
25364 case RESULT_DECL:
25365 /* If we are in terse mode, don't generate any DIEs to represent any
25366 variable declarations or definitions. */
25367 if (debug_info_level <= DINFO_LEVEL_TERSE)
25368 break;
25370 /* Output any DIEs that are needed to specify the type of this data
25371 object. */
25372 if (decl_by_reference_p (decl_or_origin))
25373 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
25374 else
25375 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
25377 /* And its containing type. */
25378 class_origin = decl_class_context (decl_or_origin);
25379 if (class_origin != NULL_TREE)
25380 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
25382 /* And its containing namespace. */
25383 context_die = declare_in_namespace (decl_or_origin, context_die);
25385 /* Now output the DIE to represent the data object itself. This gets
25386 complicated because of the possibility that the VAR_DECL really
25387 represents an inlined instance of a formal parameter for an inline
25388 function. */
25389 ultimate_origin = decl_ultimate_origin (decl_or_origin);
25390 if (ultimate_origin != NULL_TREE
25391 && TREE_CODE (ultimate_origin) == PARM_DECL)
25392 gen_formal_parameter_die (decl, origin,
25393 true /* Emit name attribute. */,
25394 context_die);
25395 else
25396 gen_variable_die (decl, origin, context_die);
25397 break;
25399 case FIELD_DECL:
25400 gcc_assert (ctx != NULL && ctx->struct_type != NULL);
25401 /* Ignore the nameless fields that are used to skip bits but handle C++
25402 anonymous unions and structs. */
25403 if (DECL_NAME (decl) != NULL_TREE
25404 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
25405 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
25407 gen_type_die (member_declared_type (decl), context_die);
25408 gen_field_die (decl, ctx, context_die);
25410 break;
25412 case PARM_DECL:
25413 if (DECL_BY_REFERENCE (decl_or_origin))
25414 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
25415 else
25416 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
25417 return gen_formal_parameter_die (decl, origin,
25418 true /* Emit name attribute. */,
25419 context_die);
25421 case NAMESPACE_DECL:
25422 if (dwarf_version >= 3 || !dwarf_strict)
25423 gen_namespace_die (decl, context_die);
25424 break;
25426 case IMPORTED_DECL:
25427 dwarf2out_imported_module_or_decl_1 (decl, DECL_NAME (decl),
25428 DECL_CONTEXT (decl), context_die);
25429 break;
25431 case NAMELIST_DECL:
25432 gen_namelist_decl (DECL_NAME (decl), context_die,
25433 NAMELIST_DECL_ASSOCIATED_DECL (decl));
25434 break;
25436 default:
25437 /* Probably some frontend-internal decl. Assume we don't care. */
25438 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
25439 break;
25442 return NULL;
25445 /* Output initial debug information for global DECL. Called at the
25446 end of the parsing process.
25448 This is the initial debug generation process. As such, the DIEs
25449 generated may be incomplete. A later debug generation pass
25450 (dwarf2out_late_global_decl) will augment the information generated
25451 in this pass (e.g., with complete location info). */
25453 static void
25454 dwarf2out_early_global_decl (tree decl)
25456 set_early_dwarf s;
25458 /* gen_decl_die() will set DECL_ABSTRACT because
25459 cgraph_function_possibly_inlined_p() returns true. This is in
25460 turn will cause DW_AT_inline attributes to be set.
25462 This happens because at early dwarf generation, there is no
25463 cgraph information, causing cgraph_function_possibly_inlined_p()
25464 to return true. Trick cgraph_function_possibly_inlined_p()
25465 while we generate dwarf early. */
25466 bool save = symtab->global_info_ready;
25467 symtab->global_info_ready = true;
25469 /* We don't handle TYPE_DECLs. If required, they'll be reached via
25470 other DECLs and they can point to template types or other things
25471 that dwarf2out can't handle when done via dwarf2out_decl. */
25472 if (TREE_CODE (decl) != TYPE_DECL
25473 && TREE_CODE (decl) != PARM_DECL)
25475 tree save_fndecl = current_function_decl;
25476 if (TREE_CODE (decl) == FUNCTION_DECL)
25478 /* No cfun means the symbol has no body, so there's nothing
25479 to emit. */
25480 if (!DECL_STRUCT_FUNCTION (decl))
25481 goto early_decl_exit;
25483 /* For nested functions, make sure we have DIEs for the parents first
25484 so that all nested DIEs are generated at the proper scope in the
25485 first shot. */
25486 tree context = decl_function_context (decl);
25487 if (context != NULL && lookup_decl_die (context) == NULL)
25489 current_function_decl = context;
25490 dwarf2out_decl (context);
25493 current_function_decl = decl;
25495 dwarf2out_decl (decl);
25496 if (TREE_CODE (decl) == FUNCTION_DECL)
25497 current_function_decl = save_fndecl;
25499 early_decl_exit:
25500 symtab->global_info_ready = save;
25503 /* Output debug information for global decl DECL. Called from
25504 toplev.c after compilation proper has finished. */
25506 static void
25507 dwarf2out_late_global_decl (tree decl)
25509 /* Fill-in any location information we were unable to determine
25510 on the first pass. */
25511 if (VAR_P (decl) && !POINTER_BOUNDS_P (decl))
25513 dw_die_ref die = lookup_decl_die (decl);
25515 /* We have to generate early debug late for LTO. */
25516 if (! die && in_lto_p)
25518 dwarf2out_decl (decl);
25519 die = lookup_decl_die (decl);
25522 if (die)
25524 /* We get called via the symtab code invoking late_global_decl
25525 for symbols that are optimized out. Do not add locations
25526 for those. */
25527 varpool_node *node = varpool_node::get (decl);
25528 if (! node || ! node->definition)
25529 tree_add_const_value_attribute_for_decl (die, decl);
25530 else
25531 add_location_or_const_value_attribute (die, decl, false);
25536 /* Output debug information for type decl DECL. Called from toplev.c
25537 and from language front ends (to record built-in types). */
25538 static void
25539 dwarf2out_type_decl (tree decl, int local)
25541 if (!local)
25543 set_early_dwarf s;
25544 dwarf2out_decl (decl);
25548 /* Output debug information for imported module or decl DECL.
25549 NAME is non-NULL name in the lexical block if the decl has been renamed.
25550 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
25551 that DECL belongs to.
25552 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
25553 static void
25554 dwarf2out_imported_module_or_decl_1 (tree decl,
25555 tree name,
25556 tree lexical_block,
25557 dw_die_ref lexical_block_die)
25559 expanded_location xloc;
25560 dw_die_ref imported_die = NULL;
25561 dw_die_ref at_import_die;
25563 if (TREE_CODE (decl) == IMPORTED_DECL)
25565 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
25566 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
25567 gcc_assert (decl);
25569 else
25570 xloc = expand_location (input_location);
25572 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
25574 at_import_die = force_type_die (TREE_TYPE (decl));
25575 /* For namespace N { typedef void T; } using N::T; base_type_die
25576 returns NULL, but DW_TAG_imported_declaration requires
25577 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
25578 if (!at_import_die)
25580 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
25581 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
25582 at_import_die = lookup_type_die (TREE_TYPE (decl));
25583 gcc_assert (at_import_die);
25586 else
25588 at_import_die = lookup_decl_die (decl);
25589 if (!at_import_die)
25591 /* If we're trying to avoid duplicate debug info, we may not have
25592 emitted the member decl for this field. Emit it now. */
25593 if (TREE_CODE (decl) == FIELD_DECL)
25595 tree type = DECL_CONTEXT (decl);
25597 if (TYPE_CONTEXT (type)
25598 && TYPE_P (TYPE_CONTEXT (type))
25599 && !should_emit_struct_debug (TYPE_CONTEXT (type),
25600 DINFO_USAGE_DIR_USE))
25601 return;
25602 gen_type_die_for_member (type, decl,
25603 get_context_die (TYPE_CONTEXT (type)));
25605 if (TREE_CODE (decl) == NAMELIST_DECL)
25606 at_import_die = gen_namelist_decl (DECL_NAME (decl),
25607 get_context_die (DECL_CONTEXT (decl)),
25608 NULL_TREE);
25609 else
25610 at_import_die = force_decl_die (decl);
25614 if (TREE_CODE (decl) == NAMESPACE_DECL)
25616 if (dwarf_version >= 3 || !dwarf_strict)
25617 imported_die = new_die (DW_TAG_imported_module,
25618 lexical_block_die,
25619 lexical_block);
25620 else
25621 return;
25623 else
25624 imported_die = new_die (DW_TAG_imported_declaration,
25625 lexical_block_die,
25626 lexical_block);
25628 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
25629 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
25630 if (debug_column_info && xloc.column)
25631 add_AT_unsigned (imported_die, DW_AT_decl_column, xloc.column);
25632 if (name)
25633 add_AT_string (imported_die, DW_AT_name,
25634 IDENTIFIER_POINTER (name));
25635 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
25638 /* Output debug information for imported module or decl DECL.
25639 NAME is non-NULL name in context if the decl has been renamed.
25640 CHILD is true if decl is one of the renamed decls as part of
25641 importing whole module. */
25643 static void
25644 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
25645 bool child)
25647 /* dw_die_ref at_import_die; */
25648 dw_die_ref scope_die;
25650 if (debug_info_level <= DINFO_LEVEL_TERSE)
25651 return;
25653 gcc_assert (decl);
25655 set_early_dwarf s;
25657 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
25658 We need decl DIE for reference and scope die. First, get DIE for the decl
25659 itself. */
25661 /* Get the scope die for decl context. Use comp_unit_die for global module
25662 or decl. If die is not found for non globals, force new die. */
25663 if (context
25664 && TYPE_P (context)
25665 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
25666 return;
25668 scope_die = get_context_die (context);
25670 if (child)
25672 /* DW_TAG_imported_module was introduced in the DWARFv3 specification, so
25673 there is nothing we can do, here. */
25674 if (dwarf_version < 3 && dwarf_strict)
25675 return;
25677 gcc_assert (scope_die->die_child);
25678 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
25679 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
25680 scope_die = scope_die->die_child;
25683 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
25684 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
25687 /* Output debug information for namelists. */
25689 static dw_die_ref
25690 gen_namelist_decl (tree name, dw_die_ref scope_die, tree item_decls)
25692 dw_die_ref nml_die, nml_item_die, nml_item_ref_die;
25693 tree value;
25694 unsigned i;
25696 if (debug_info_level <= DINFO_LEVEL_TERSE)
25697 return NULL;
25699 gcc_assert (scope_die != NULL);
25700 nml_die = new_die (DW_TAG_namelist, scope_die, NULL);
25701 add_AT_string (nml_die, DW_AT_name, IDENTIFIER_POINTER (name));
25703 /* If there are no item_decls, we have a nondefining namelist, e.g.
25704 with USE association; hence, set DW_AT_declaration. */
25705 if (item_decls == NULL_TREE)
25707 add_AT_flag (nml_die, DW_AT_declaration, 1);
25708 return nml_die;
25711 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (item_decls), i, value)
25713 nml_item_ref_die = lookup_decl_die (value);
25714 if (!nml_item_ref_die)
25715 nml_item_ref_die = force_decl_die (value);
25717 nml_item_die = new_die (DW_TAG_namelist_item, nml_die, NULL);
25718 add_AT_die_ref (nml_item_die, DW_AT_namelist_items, nml_item_ref_die);
25720 return nml_die;
25724 /* Write the debugging output for DECL and return the DIE. */
25726 static void
25727 dwarf2out_decl (tree decl)
25729 dw_die_ref context_die = comp_unit_die ();
25731 switch (TREE_CODE (decl))
25733 case ERROR_MARK:
25734 return;
25736 case FUNCTION_DECL:
25737 /* What we would really like to do here is to filter out all mere
25738 file-scope declarations of file-scope functions which are never
25739 referenced later within this translation unit (and keep all of ones
25740 that *are* referenced later on) but we aren't clairvoyant, so we have
25741 no idea which functions will be referenced in the future (i.e. later
25742 on within the current translation unit). So here we just ignore all
25743 file-scope function declarations which are not also definitions. If
25744 and when the debugger needs to know something about these functions,
25745 it will have to hunt around and find the DWARF information associated
25746 with the definition of the function.
25748 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
25749 nodes represent definitions and which ones represent mere
25750 declarations. We have to check DECL_INITIAL instead. That's because
25751 the C front-end supports some weird semantics for "extern inline"
25752 function definitions. These can get inlined within the current
25753 translation unit (and thus, we need to generate Dwarf info for their
25754 abstract instances so that the Dwarf info for the concrete inlined
25755 instances can have something to refer to) but the compiler never
25756 generates any out-of-lines instances of such things (despite the fact
25757 that they *are* definitions).
25759 The important point is that the C front-end marks these "extern
25760 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
25761 them anyway. Note that the C++ front-end also plays some similar games
25762 for inline function definitions appearing within include files which
25763 also contain `#pragma interface' pragmas.
25765 If we are called from dwarf2out_abstract_function output a DIE
25766 anyway. We can end up here this way with early inlining and LTO
25767 where the inlined function is output in a different LTRANS unit
25768 or not at all. */
25769 if (DECL_INITIAL (decl) == NULL_TREE
25770 && ! DECL_ABSTRACT_P (decl))
25771 return;
25773 /* If we're a nested function, initially use a parent of NULL; if we're
25774 a plain function, this will be fixed up in decls_for_scope. If
25775 we're a method, it will be ignored, since we already have a DIE. */
25776 if (decl_function_context (decl)
25777 /* But if we're in terse mode, we don't care about scope. */
25778 && debug_info_level > DINFO_LEVEL_TERSE)
25779 context_die = NULL;
25780 break;
25782 case VAR_DECL:
25783 /* For local statics lookup proper context die. */
25784 if (local_function_static (decl))
25785 context_die = lookup_decl_die (DECL_CONTEXT (decl));
25787 /* If we are in terse mode, don't generate any DIEs to represent any
25788 variable declarations or definitions. */
25789 if (debug_info_level <= DINFO_LEVEL_TERSE)
25790 return;
25791 break;
25793 case CONST_DECL:
25794 if (debug_info_level <= DINFO_LEVEL_TERSE)
25795 return;
25796 if (!is_fortran () && !is_ada ())
25797 return;
25798 if (TREE_STATIC (decl) && decl_function_context (decl))
25799 context_die = lookup_decl_die (DECL_CONTEXT (decl));
25800 break;
25802 case NAMESPACE_DECL:
25803 case IMPORTED_DECL:
25804 if (debug_info_level <= DINFO_LEVEL_TERSE)
25805 return;
25806 if (lookup_decl_die (decl) != NULL)
25807 return;
25808 break;
25810 case TYPE_DECL:
25811 /* Don't emit stubs for types unless they are needed by other DIEs. */
25812 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
25813 return;
25815 /* Don't bother trying to generate any DIEs to represent any of the
25816 normal built-in types for the language we are compiling. */
25817 if (DECL_IS_BUILTIN (decl))
25818 return;
25820 /* If we are in terse mode, don't generate any DIEs for types. */
25821 if (debug_info_level <= DINFO_LEVEL_TERSE)
25822 return;
25824 /* If we're a function-scope tag, initially use a parent of NULL;
25825 this will be fixed up in decls_for_scope. */
25826 if (decl_function_context (decl))
25827 context_die = NULL;
25829 break;
25831 case NAMELIST_DECL:
25832 break;
25834 default:
25835 return;
25838 gen_decl_die (decl, NULL, NULL, context_die);
25840 if (flag_checking)
25842 dw_die_ref die = lookup_decl_die (decl);
25843 if (die)
25844 check_die (die);
25848 /* Write the debugging output for DECL. */
25850 static void
25851 dwarf2out_function_decl (tree decl)
25853 dwarf2out_decl (decl);
25854 call_arg_locations = NULL;
25855 call_arg_loc_last = NULL;
25856 call_site_count = -1;
25857 tail_call_site_count = -1;
25858 decl_loc_table->empty ();
25859 cached_dw_loc_list_table->empty ();
25862 /* Output a marker (i.e. a label) for the beginning of the generated code for
25863 a lexical block. */
25865 static void
25866 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
25867 unsigned int blocknum)
25869 switch_to_section (current_function_section ());
25870 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
25873 /* Output a marker (i.e. a label) for the end of the generated code for a
25874 lexical block. */
25876 static void
25877 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
25879 switch_to_section (current_function_section ());
25880 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
25883 /* Returns nonzero if it is appropriate not to emit any debugging
25884 information for BLOCK, because it doesn't contain any instructions.
25886 Don't allow this for blocks with nested functions or local classes
25887 as we would end up with orphans, and in the presence of scheduling
25888 we may end up calling them anyway. */
25890 static bool
25891 dwarf2out_ignore_block (const_tree block)
25893 tree decl;
25894 unsigned int i;
25896 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
25897 if (TREE_CODE (decl) == FUNCTION_DECL
25898 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
25899 return 0;
25900 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
25902 decl = BLOCK_NONLOCALIZED_VAR (block, i);
25903 if (TREE_CODE (decl) == FUNCTION_DECL
25904 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
25905 return 0;
25908 return 1;
25911 /* Hash table routines for file_hash. */
25913 bool
25914 dwarf_file_hasher::equal (dwarf_file_data *p1, const char *p2)
25916 return filename_cmp (p1->filename, p2) == 0;
25919 hashval_t
25920 dwarf_file_hasher::hash (dwarf_file_data *p)
25922 return htab_hash_string (p->filename);
25925 /* Lookup FILE_NAME (in the list of filenames that we know about here in
25926 dwarf2out.c) and return its "index". The index of each (known) filename is
25927 just a unique number which is associated with only that one filename. We
25928 need such numbers for the sake of generating labels (in the .debug_sfnames
25929 section) and references to those files numbers (in the .debug_srcinfo
25930 and .debug_macinfo sections). If the filename given as an argument is not
25931 found in our current list, add it to the list and assign it the next
25932 available unique index number. */
25934 static struct dwarf_file_data *
25935 lookup_filename (const char *file_name)
25937 struct dwarf_file_data * created;
25939 if (!file_name)
25940 return NULL;
25942 dwarf_file_data **slot
25943 = file_table->find_slot_with_hash (file_name, htab_hash_string (file_name),
25944 INSERT);
25945 if (*slot)
25946 return *slot;
25948 created = ggc_alloc<dwarf_file_data> ();
25949 created->filename = file_name;
25950 created->emitted_number = 0;
25951 *slot = created;
25952 return created;
25955 /* If the assembler will construct the file table, then translate the compiler
25956 internal file table number into the assembler file table number, and emit
25957 a .file directive if we haven't already emitted one yet. The file table
25958 numbers are different because we prune debug info for unused variables and
25959 types, which may include filenames. */
25961 static int
25962 maybe_emit_file (struct dwarf_file_data * fd)
25964 if (! fd->emitted_number)
25966 if (last_emitted_file)
25967 fd->emitted_number = last_emitted_file->emitted_number + 1;
25968 else
25969 fd->emitted_number = 1;
25970 last_emitted_file = fd;
25972 if (DWARF2_ASM_LINE_DEBUG_INFO)
25974 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
25975 output_quoted_string (asm_out_file,
25976 remap_debug_filename (fd->filename));
25977 fputc ('\n', asm_out_file);
25981 return fd->emitted_number;
25984 /* Schedule generation of a DW_AT_const_value attribute to DIE.
25985 That generation should happen after function debug info has been
25986 generated. The value of the attribute is the constant value of ARG. */
25988 static void
25989 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
25991 die_arg_entry entry;
25993 if (!die || !arg)
25994 return;
25996 gcc_assert (early_dwarf);
25998 if (!tmpl_value_parm_die_table)
25999 vec_alloc (tmpl_value_parm_die_table, 32);
26001 entry.die = die;
26002 entry.arg = arg;
26003 vec_safe_push (tmpl_value_parm_die_table, entry);
26006 /* Return TRUE if T is an instance of generic type, FALSE
26007 otherwise. */
26009 static bool
26010 generic_type_p (tree t)
26012 if (t == NULL_TREE || !TYPE_P (t))
26013 return false;
26014 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
26017 /* Schedule the generation of the generic parameter dies for the
26018 instance of generic type T. The proper generation itself is later
26019 done by gen_scheduled_generic_parms_dies. */
26021 static void
26022 schedule_generic_params_dies_gen (tree t)
26024 if (!generic_type_p (t))
26025 return;
26027 gcc_assert (early_dwarf);
26029 if (!generic_type_instances)
26030 vec_alloc (generic_type_instances, 256);
26032 vec_safe_push (generic_type_instances, t);
26035 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
26036 by append_entry_to_tmpl_value_parm_die_table. This function must
26037 be called after function DIEs have been generated. */
26039 static void
26040 gen_remaining_tmpl_value_param_die_attribute (void)
26042 if (tmpl_value_parm_die_table)
26044 unsigned i, j;
26045 die_arg_entry *e;
26047 /* We do this in two phases - first get the cases we can
26048 handle during early-finish, preserving those we cannot
26049 (containing symbolic constants where we don't yet know
26050 whether we are going to output the referenced symbols).
26051 For those we try again at late-finish. */
26052 j = 0;
26053 FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
26055 if (!tree_add_const_value_attribute (e->die, e->arg))
26057 dw_loc_descr_ref loc = NULL;
26058 if (! early_dwarf
26059 && (dwarf_version >= 5 || !dwarf_strict))
26060 loc = loc_descriptor_from_tree (e->arg, 2, NULL);
26061 if (loc)
26062 add_AT_loc (e->die, DW_AT_location, loc);
26063 else
26064 (*tmpl_value_parm_die_table)[j++] = *e;
26067 tmpl_value_parm_die_table->truncate (j);
26071 /* Generate generic parameters DIEs for instances of generic types
26072 that have been previously scheduled by
26073 schedule_generic_params_dies_gen. This function must be called
26074 after all the types of the CU have been laid out. */
26076 static void
26077 gen_scheduled_generic_parms_dies (void)
26079 unsigned i;
26080 tree t;
26082 if (!generic_type_instances)
26083 return;
26085 FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
26086 if (COMPLETE_TYPE_P (t))
26087 gen_generic_params_dies (t);
26089 generic_type_instances = NULL;
26093 /* Replace DW_AT_name for the decl with name. */
26095 static void
26096 dwarf2out_set_name (tree decl, tree name)
26098 dw_die_ref die;
26099 dw_attr_node *attr;
26100 const char *dname;
26102 die = TYPE_SYMTAB_DIE (decl);
26103 if (!die)
26104 return;
26106 dname = dwarf2_name (name, 0);
26107 if (!dname)
26108 return;
26110 attr = get_AT (die, DW_AT_name);
26111 if (attr)
26113 struct indirect_string_node *node;
26115 node = find_AT_string (dname);
26116 /* replace the string. */
26117 attr->dw_attr_val.v.val_str = node;
26120 else
26121 add_name_attribute (die, dname);
26124 /* True if before or during processing of the first function being emitted. */
26125 static bool in_first_function_p = true;
26126 /* True if loc_note during dwarf2out_var_location call might still be
26127 before first real instruction at address equal to .Ltext0. */
26128 static bool maybe_at_text_label_p = true;
26129 /* One above highest N where .LVLN label might be equal to .Ltext0 label. */
26130 static unsigned int first_loclabel_num_not_at_text_label;
26132 /* Called by the final INSN scan whenever we see a var location. We
26133 use it to drop labels in the right places, and throw the location in
26134 our lookup table. */
26136 static void
26137 dwarf2out_var_location (rtx_insn *loc_note)
26139 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
26140 struct var_loc_node *newloc;
26141 rtx_insn *next_real, *next_note;
26142 rtx_insn *call_insn = NULL;
26143 static const char *last_label;
26144 static const char *last_postcall_label;
26145 static bool last_in_cold_section_p;
26146 static rtx_insn *expected_next_loc_note;
26147 tree decl;
26148 bool var_loc_p;
26150 if (!NOTE_P (loc_note))
26152 if (CALL_P (loc_note))
26154 call_site_count++;
26155 if (SIBLING_CALL_P (loc_note))
26156 tail_call_site_count++;
26157 if (optimize == 0 && !flag_var_tracking)
26159 /* When the var-tracking pass is not running, there is no note
26160 for indirect calls whose target is compile-time known. In this
26161 case, process such calls specifically so that we generate call
26162 sites for them anyway. */
26163 rtx x = PATTERN (loc_note);
26164 if (GET_CODE (x) == PARALLEL)
26165 x = XVECEXP (x, 0, 0);
26166 if (GET_CODE (x) == SET)
26167 x = SET_SRC (x);
26168 if (GET_CODE (x) == CALL)
26169 x = XEXP (x, 0);
26170 if (!MEM_P (x)
26171 || GET_CODE (XEXP (x, 0)) != SYMBOL_REF
26172 || !SYMBOL_REF_DECL (XEXP (x, 0))
26173 || (TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0)))
26174 != FUNCTION_DECL))
26176 call_insn = loc_note;
26177 loc_note = NULL;
26178 var_loc_p = false;
26180 next_real = next_real_insn (call_insn);
26181 next_note = NULL;
26182 cached_next_real_insn = NULL;
26183 goto create_label;
26187 return;
26190 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
26191 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
26192 return;
26194 /* Optimize processing a large consecutive sequence of location
26195 notes so we don't spend too much time in next_real_insn. If the
26196 next insn is another location note, remember the next_real_insn
26197 calculation for next time. */
26198 next_real = cached_next_real_insn;
26199 if (next_real)
26201 if (expected_next_loc_note != loc_note)
26202 next_real = NULL;
26205 next_note = NEXT_INSN (loc_note);
26206 if (! next_note
26207 || next_note->deleted ()
26208 || ! NOTE_P (next_note)
26209 || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
26210 && NOTE_KIND (next_note) != NOTE_INSN_CALL_ARG_LOCATION))
26211 next_note = NULL;
26213 if (! next_real)
26214 next_real = next_real_insn (loc_note);
26216 if (next_note)
26218 expected_next_loc_note = next_note;
26219 cached_next_real_insn = next_real;
26221 else
26222 cached_next_real_insn = NULL;
26224 /* If there are no instructions which would be affected by this note,
26225 don't do anything. */
26226 if (var_loc_p
26227 && next_real == NULL_RTX
26228 && !NOTE_DURING_CALL_P (loc_note))
26229 return;
26231 create_label:
26233 if (next_real == NULL_RTX)
26234 next_real = get_last_insn ();
26236 /* If there were any real insns between note we processed last time
26237 and this note (or if it is the first note), clear
26238 last_{,postcall_}label so that they are not reused this time. */
26239 if (last_var_location_insn == NULL_RTX
26240 || last_var_location_insn != next_real
26241 || last_in_cold_section_p != in_cold_section_p)
26243 last_label = NULL;
26244 last_postcall_label = NULL;
26247 if (var_loc_p)
26249 decl = NOTE_VAR_LOCATION_DECL (loc_note);
26250 newloc = add_var_loc_to_decl (decl, loc_note,
26251 NOTE_DURING_CALL_P (loc_note)
26252 ? last_postcall_label : last_label);
26253 if (newloc == NULL)
26254 return;
26256 else
26258 decl = NULL_TREE;
26259 newloc = NULL;
26262 /* If there were no real insns between note we processed last time
26263 and this note, use the label we emitted last time. Otherwise
26264 create a new label and emit it. */
26265 if (last_label == NULL)
26267 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
26268 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
26269 loclabel_num++;
26270 last_label = ggc_strdup (loclabel);
26271 /* See if loclabel might be equal to .Ltext0. If yes,
26272 bump first_loclabel_num_not_at_text_label. */
26273 if (!have_multiple_function_sections
26274 && in_first_function_p
26275 && maybe_at_text_label_p)
26277 static rtx_insn *last_start;
26278 rtx_insn *insn;
26279 for (insn = loc_note; insn; insn = previous_insn (insn))
26280 if (insn == last_start)
26281 break;
26282 else if (!NONDEBUG_INSN_P (insn))
26283 continue;
26284 else
26286 rtx body = PATTERN (insn);
26287 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
26288 continue;
26289 /* Inline asm could occupy zero bytes. */
26290 else if (GET_CODE (body) == ASM_INPUT
26291 || asm_noperands (body) >= 0)
26292 continue;
26293 #ifdef HAVE_attr_length
26294 else if (get_attr_min_length (insn) == 0)
26295 continue;
26296 #endif
26297 else
26299 /* Assume insn has non-zero length. */
26300 maybe_at_text_label_p = false;
26301 break;
26304 if (maybe_at_text_label_p)
26306 last_start = loc_note;
26307 first_loclabel_num_not_at_text_label = loclabel_num;
26312 gcc_assert ((loc_note == NULL_RTX && call_insn != NULL_RTX)
26313 || (loc_note != NULL_RTX && call_insn == NULL_RTX));
26315 if (!var_loc_p)
26317 struct call_arg_loc_node *ca_loc
26318 = ggc_cleared_alloc<call_arg_loc_node> ();
26319 rtx_insn *prev
26320 = loc_note != NULL_RTX ? prev_real_insn (loc_note) : call_insn;
26322 ca_loc->call_arg_loc_note = loc_note;
26323 ca_loc->next = NULL;
26324 ca_loc->label = last_label;
26325 gcc_assert (prev
26326 && (CALL_P (prev)
26327 || (NONJUMP_INSN_P (prev)
26328 && GET_CODE (PATTERN (prev)) == SEQUENCE
26329 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
26330 if (!CALL_P (prev))
26331 prev = as_a <rtx_sequence *> (PATTERN (prev))->insn (0);
26332 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
26334 /* Look for a SYMBOL_REF in the "prev" instruction. */
26335 rtx x = get_call_rtx_from (PATTERN (prev));
26336 if (x)
26338 /* Try to get the call symbol, if any. */
26339 if (MEM_P (XEXP (x, 0)))
26340 x = XEXP (x, 0);
26341 /* First, look for a memory access to a symbol_ref. */
26342 if (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
26343 && SYMBOL_REF_DECL (XEXP (x, 0))
26344 && TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0))) == FUNCTION_DECL)
26345 ca_loc->symbol_ref = XEXP (x, 0);
26346 /* Otherwise, look at a compile-time known user-level function
26347 declaration. */
26348 else if (MEM_P (x)
26349 && MEM_EXPR (x)
26350 && TREE_CODE (MEM_EXPR (x)) == FUNCTION_DECL)
26351 ca_loc->symbol_ref = XEXP (DECL_RTL (MEM_EXPR (x)), 0);
26354 ca_loc->block = insn_scope (prev);
26355 if (call_arg_locations)
26356 call_arg_loc_last->next = ca_loc;
26357 else
26358 call_arg_locations = ca_loc;
26359 call_arg_loc_last = ca_loc;
26361 else if (loc_note != NULL_RTX && !NOTE_DURING_CALL_P (loc_note))
26362 newloc->label = last_label;
26363 else
26365 if (!last_postcall_label)
26367 sprintf (loclabel, "%s-1", last_label);
26368 last_postcall_label = ggc_strdup (loclabel);
26370 newloc->label = last_postcall_label;
26373 last_var_location_insn = next_real;
26374 last_in_cold_section_p = in_cold_section_p;
26377 /* Called from finalize_size_functions for size functions so that their body
26378 can be encoded in the debug info to describe the layout of variable-length
26379 structures. */
26381 static void
26382 dwarf2out_size_function (tree decl)
26384 function_to_dwarf_procedure (decl);
26387 /* Note in one location list that text section has changed. */
26390 var_location_switch_text_section_1 (var_loc_list **slot, void *)
26392 var_loc_list *list = *slot;
26393 if (list->first)
26394 list->last_before_switch
26395 = list->last->next ? list->last->next : list->last;
26396 return 1;
26399 /* Note in all location lists that text section has changed. */
26401 static void
26402 var_location_switch_text_section (void)
26404 if (decl_loc_table == NULL)
26405 return;
26407 decl_loc_table->traverse<void *, var_location_switch_text_section_1> (NULL);
26410 /* Create a new line number table. */
26412 static dw_line_info_table *
26413 new_line_info_table (void)
26415 dw_line_info_table *table;
26417 table = ggc_cleared_alloc<dw_line_info_table> ();
26418 table->file_num = 1;
26419 table->line_num = 1;
26420 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
26422 return table;
26425 /* Lookup the "current" table into which we emit line info, so
26426 that we don't have to do it for every source line. */
26428 static void
26429 set_cur_line_info_table (section *sec)
26431 dw_line_info_table *table;
26433 if (sec == text_section)
26434 table = text_section_line_info;
26435 else if (sec == cold_text_section)
26437 table = cold_text_section_line_info;
26438 if (!table)
26440 cold_text_section_line_info = table = new_line_info_table ();
26441 table->end_label = cold_end_label;
26444 else
26446 const char *end_label;
26448 if (flag_reorder_blocks_and_partition)
26450 if (in_cold_section_p)
26451 end_label = crtl->subsections.cold_section_end_label;
26452 else
26453 end_label = crtl->subsections.hot_section_end_label;
26455 else
26457 char label[MAX_ARTIFICIAL_LABEL_BYTES];
26458 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
26459 current_function_funcdef_no);
26460 end_label = ggc_strdup (label);
26463 table = new_line_info_table ();
26464 table->end_label = end_label;
26466 vec_safe_push (separate_line_info, table);
26469 if (DWARF2_ASM_LINE_DEBUG_INFO)
26470 table->is_stmt = (cur_line_info_table
26471 ? cur_line_info_table->is_stmt
26472 : DWARF_LINE_DEFAULT_IS_STMT_START);
26473 cur_line_info_table = table;
26477 /* We need to reset the locations at the beginning of each
26478 function. We can't do this in the end_function hook, because the
26479 declarations that use the locations won't have been output when
26480 that hook is called. Also compute have_multiple_function_sections here. */
26482 static void
26483 dwarf2out_begin_function (tree fun)
26485 section *sec = function_section (fun);
26487 if (sec != text_section)
26488 have_multiple_function_sections = true;
26490 if (flag_reorder_blocks_and_partition && !cold_text_section)
26492 gcc_assert (current_function_decl == fun);
26493 cold_text_section = unlikely_text_section ();
26494 switch_to_section (cold_text_section);
26495 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
26496 switch_to_section (sec);
26499 dwarf2out_note_section_used ();
26500 call_site_count = 0;
26501 tail_call_site_count = 0;
26503 set_cur_line_info_table (sec);
26506 /* Helper function of dwarf2out_end_function, called only after emitting
26507 the very first function into assembly. Check if some .debug_loc range
26508 might end with a .LVL* label that could be equal to .Ltext0.
26509 In that case we must force using absolute addresses in .debug_loc ranges,
26510 because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
26511 .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
26512 list terminator.
26513 Set have_multiple_function_sections to true in that case and
26514 terminate htab traversal. */
26517 find_empty_loc_ranges_at_text_label (var_loc_list **slot, int)
26519 var_loc_list *entry = *slot;
26520 struct var_loc_node *node;
26522 node = entry->first;
26523 if (node && node->next && node->next->label)
26525 unsigned int i;
26526 const char *label = node->next->label;
26527 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
26529 for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
26531 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
26532 if (strcmp (label, loclabel) == 0)
26534 have_multiple_function_sections = true;
26535 return 0;
26539 return 1;
26542 /* Hook called after emitting a function into assembly.
26543 This does something only for the very first function emitted. */
26545 static void
26546 dwarf2out_end_function (unsigned int)
26548 if (in_first_function_p
26549 && !have_multiple_function_sections
26550 && first_loclabel_num_not_at_text_label
26551 && decl_loc_table)
26552 decl_loc_table->traverse<int, find_empty_loc_ranges_at_text_label> (0);
26553 in_first_function_p = false;
26554 maybe_at_text_label_p = false;
26557 /* Temporary holder for dwarf2out_register_main_translation_unit. Used to let
26558 front-ends register a translation unit even before dwarf2out_init is
26559 called. */
26560 static tree main_translation_unit = NULL_TREE;
26562 /* Hook called by front-ends after they built their main translation unit.
26563 Associate comp_unit_die to UNIT. */
26565 static void
26566 dwarf2out_register_main_translation_unit (tree unit)
26568 gcc_assert (TREE_CODE (unit) == TRANSLATION_UNIT_DECL
26569 && main_translation_unit == NULL_TREE);
26570 main_translation_unit = unit;
26571 /* If dwarf2out_init has not been called yet, it will perform the association
26572 itself looking at main_translation_unit. */
26573 if (decl_die_table != NULL)
26574 equate_decl_number_to_die (unit, comp_unit_die ());
26577 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
26579 static void
26580 push_dw_line_info_entry (dw_line_info_table *table,
26581 enum dw_line_info_opcode opcode, unsigned int val)
26583 dw_line_info_entry e;
26584 e.opcode = opcode;
26585 e.val = val;
26586 vec_safe_push (table->entries, e);
26589 /* Output a label to mark the beginning of a source code line entry
26590 and record information relating to this source line, in
26591 'line_info_table' for later output of the .debug_line section. */
26592 /* ??? The discriminator parameter ought to be unsigned. */
26594 static void
26595 dwarf2out_source_line (unsigned int line, unsigned int column,
26596 const char *filename,
26597 int discriminator, bool is_stmt)
26599 unsigned int file_num;
26600 dw_line_info_table *table;
26602 if (debug_info_level < DINFO_LEVEL_TERSE || line == 0)
26603 return;
26605 /* The discriminator column was added in dwarf4. Simplify the below
26606 by simply removing it if we're not supposed to output it. */
26607 if (dwarf_version < 4 && dwarf_strict)
26608 discriminator = 0;
26610 if (!debug_column_info)
26611 column = 0;
26613 table = cur_line_info_table;
26614 file_num = maybe_emit_file (lookup_filename (filename));
26616 /* ??? TODO: Elide duplicate line number entries. Traditionally,
26617 the debugger has used the second (possibly duplicate) line number
26618 at the beginning of the function to mark the end of the prologue.
26619 We could eliminate any other duplicates within the function. For
26620 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
26621 that second line number entry. */
26622 /* Recall that this end-of-prologue indication is *not* the same thing
26623 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
26624 to which the hook corresponds, follows the last insn that was
26625 emitted by gen_prologue. What we need is to precede the first insn
26626 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
26627 insn that corresponds to something the user wrote. These may be
26628 very different locations once scheduling is enabled. */
26630 if (0 && file_num == table->file_num
26631 && line == table->line_num
26632 && column == table->column_num
26633 && discriminator == table->discrim_num
26634 && is_stmt == table->is_stmt)
26635 return;
26637 switch_to_section (current_function_section ());
26639 /* If requested, emit something human-readable. */
26640 if (flag_debug_asm)
26642 if (debug_column_info)
26643 fprintf (asm_out_file, "\t%s %s:%d:%d\n", ASM_COMMENT_START,
26644 filename, line, column);
26645 else
26646 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
26647 filename, line);
26650 if (DWARF2_ASM_LINE_DEBUG_INFO)
26652 /* Emit the .loc directive understood by GNU as. */
26653 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
26654 file_num, line, is_stmt, discriminator */
26655 fputs ("\t.loc ", asm_out_file);
26656 fprint_ul (asm_out_file, file_num);
26657 putc (' ', asm_out_file);
26658 fprint_ul (asm_out_file, line);
26659 putc (' ', asm_out_file);
26660 if (debug_column_info)
26661 fprint_ul (asm_out_file, column);
26662 else
26663 putc ('0', asm_out_file);
26665 if (is_stmt != table->is_stmt)
26667 fputs (" is_stmt ", asm_out_file);
26668 putc (is_stmt ? '1' : '0', asm_out_file);
26670 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
26672 gcc_assert (discriminator > 0);
26673 fputs (" discriminator ", asm_out_file);
26674 fprint_ul (asm_out_file, (unsigned long) discriminator);
26676 putc ('\n', asm_out_file);
26678 else
26680 unsigned int label_num = ++line_info_label_num;
26682 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
26684 push_dw_line_info_entry (table, LI_set_address, label_num);
26685 if (file_num != table->file_num)
26686 push_dw_line_info_entry (table, LI_set_file, file_num);
26687 if (discriminator != table->discrim_num)
26688 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
26689 if (is_stmt != table->is_stmt)
26690 push_dw_line_info_entry (table, LI_negate_stmt, 0);
26691 push_dw_line_info_entry (table, LI_set_line, line);
26692 if (debug_column_info)
26693 push_dw_line_info_entry (table, LI_set_column, column);
26696 table->file_num = file_num;
26697 table->line_num = line;
26698 table->column_num = column;
26699 table->discrim_num = discriminator;
26700 table->is_stmt = is_stmt;
26701 table->in_use = true;
26704 /* Record the beginning of a new source file. */
26706 static void
26707 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
26709 if (flag_eliminate_dwarf2_dups)
26711 /* Record the beginning of the file for break_out_includes. */
26712 dw_die_ref bincl_die;
26714 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
26715 add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
26718 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
26720 macinfo_entry e;
26721 e.code = DW_MACINFO_start_file;
26722 e.lineno = lineno;
26723 e.info = ggc_strdup (filename);
26724 vec_safe_push (macinfo_table, e);
26728 /* Record the end of a source file. */
26730 static void
26731 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
26733 if (flag_eliminate_dwarf2_dups)
26734 /* Record the end of the file for break_out_includes. */
26735 new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
26737 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
26739 macinfo_entry e;
26740 e.code = DW_MACINFO_end_file;
26741 e.lineno = lineno;
26742 e.info = NULL;
26743 vec_safe_push (macinfo_table, e);
26747 /* Called from debug_define in toplev.c. The `buffer' parameter contains
26748 the tail part of the directive line, i.e. the part which is past the
26749 initial whitespace, #, whitespace, directive-name, whitespace part. */
26751 static void
26752 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
26753 const char *buffer ATTRIBUTE_UNUSED)
26755 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
26757 macinfo_entry e;
26758 /* Insert a dummy first entry to be able to optimize the whole
26759 predefined macro block using DW_MACRO_import. */
26760 if (macinfo_table->is_empty () && lineno <= 1)
26762 e.code = 0;
26763 e.lineno = 0;
26764 e.info = NULL;
26765 vec_safe_push (macinfo_table, e);
26767 e.code = DW_MACINFO_define;
26768 e.lineno = lineno;
26769 e.info = ggc_strdup (buffer);
26770 vec_safe_push (macinfo_table, e);
26774 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
26775 the tail part of the directive line, i.e. the part which is past the
26776 initial whitespace, #, whitespace, directive-name, whitespace part. */
26778 static void
26779 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
26780 const char *buffer ATTRIBUTE_UNUSED)
26782 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
26784 macinfo_entry e;
26785 /* Insert a dummy first entry to be able to optimize the whole
26786 predefined macro block using DW_MACRO_import. */
26787 if (macinfo_table->is_empty () && lineno <= 1)
26789 e.code = 0;
26790 e.lineno = 0;
26791 e.info = NULL;
26792 vec_safe_push (macinfo_table, e);
26794 e.code = DW_MACINFO_undef;
26795 e.lineno = lineno;
26796 e.info = ggc_strdup (buffer);
26797 vec_safe_push (macinfo_table, e);
26801 /* Helpers to manipulate hash table of CUs. */
26803 struct macinfo_entry_hasher : nofree_ptr_hash <macinfo_entry>
26805 static inline hashval_t hash (const macinfo_entry *);
26806 static inline bool equal (const macinfo_entry *, const macinfo_entry *);
26809 inline hashval_t
26810 macinfo_entry_hasher::hash (const macinfo_entry *entry)
26812 return htab_hash_string (entry->info);
26815 inline bool
26816 macinfo_entry_hasher::equal (const macinfo_entry *entry1,
26817 const macinfo_entry *entry2)
26819 return !strcmp (entry1->info, entry2->info);
26822 typedef hash_table<macinfo_entry_hasher> macinfo_hash_type;
26824 /* Output a single .debug_macinfo entry. */
26826 static void
26827 output_macinfo_op (macinfo_entry *ref)
26829 int file_num;
26830 size_t len;
26831 struct indirect_string_node *node;
26832 char label[MAX_ARTIFICIAL_LABEL_BYTES];
26833 struct dwarf_file_data *fd;
26835 switch (ref->code)
26837 case DW_MACINFO_start_file:
26838 fd = lookup_filename (ref->info);
26839 file_num = maybe_emit_file (fd);
26840 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
26841 dw2_asm_output_data_uleb128 (ref->lineno,
26842 "Included from line number %lu",
26843 (unsigned long) ref->lineno);
26844 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
26845 break;
26846 case DW_MACINFO_end_file:
26847 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
26848 break;
26849 case DW_MACINFO_define:
26850 case DW_MACINFO_undef:
26851 len = strlen (ref->info) + 1;
26852 if (!dwarf_strict
26853 && len > DWARF_OFFSET_SIZE
26854 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
26855 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
26857 ref->code = ref->code == DW_MACINFO_define
26858 ? DW_MACRO_define_strp : DW_MACRO_undef_strp;
26859 output_macinfo_op (ref);
26860 return;
26862 dw2_asm_output_data (1, ref->code,
26863 ref->code == DW_MACINFO_define
26864 ? "Define macro" : "Undefine macro");
26865 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
26866 (unsigned long) ref->lineno);
26867 dw2_asm_output_nstring (ref->info, -1, "The macro");
26868 break;
26869 case DW_MACRO_define_strp:
26870 case DW_MACRO_undef_strp:
26871 node = find_AT_string (ref->info);
26872 gcc_assert (node
26873 && (node->form == DW_FORM_strp
26874 || node->form == DW_FORM_GNU_str_index));
26875 dw2_asm_output_data (1, ref->code,
26876 ref->code == DW_MACRO_define_strp
26877 ? "Define macro strp"
26878 : "Undefine macro strp");
26879 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
26880 (unsigned long) ref->lineno);
26881 if (node->form == DW_FORM_strp)
26882 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
26883 debug_str_section, "The macro: \"%s\"",
26884 ref->info);
26885 else
26886 dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
26887 ref->info);
26888 break;
26889 case DW_MACRO_import:
26890 dw2_asm_output_data (1, ref->code, "Import");
26891 ASM_GENERATE_INTERNAL_LABEL (label,
26892 DEBUG_MACRO_SECTION_LABEL, ref->lineno);
26893 dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
26894 break;
26895 default:
26896 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
26897 ASM_COMMENT_START, (unsigned long) ref->code);
26898 break;
26902 /* Attempt to make a sequence of define/undef macinfo ops shareable with
26903 other compilation unit .debug_macinfo sections. IDX is the first
26904 index of a define/undef, return the number of ops that should be
26905 emitted in a comdat .debug_macinfo section and emit
26906 a DW_MACRO_import entry referencing it.
26907 If the define/undef entry should be emitted normally, return 0. */
26909 static unsigned
26910 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
26911 macinfo_hash_type **macinfo_htab)
26913 macinfo_entry *first, *second, *cur, *inc;
26914 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
26915 unsigned char checksum[16];
26916 struct md5_ctx ctx;
26917 char *grp_name, *tail;
26918 const char *base;
26919 unsigned int i, count, encoded_filename_len, linebuf_len;
26920 macinfo_entry **slot;
26922 first = &(*macinfo_table)[idx];
26923 second = &(*macinfo_table)[idx + 1];
26925 /* Optimize only if there are at least two consecutive define/undef ops,
26926 and either all of them are before first DW_MACINFO_start_file
26927 with lineno {0,1} (i.e. predefined macro block), or all of them are
26928 in some included header file. */
26929 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
26930 return 0;
26931 if (vec_safe_is_empty (files))
26933 if (first->lineno > 1 || second->lineno > 1)
26934 return 0;
26936 else if (first->lineno == 0)
26937 return 0;
26939 /* Find the last define/undef entry that can be grouped together
26940 with first and at the same time compute md5 checksum of their
26941 codes, linenumbers and strings. */
26942 md5_init_ctx (&ctx);
26943 for (i = idx; macinfo_table->iterate (i, &cur); i++)
26944 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
26945 break;
26946 else if (vec_safe_is_empty (files) && cur->lineno > 1)
26947 break;
26948 else
26950 unsigned char code = cur->code;
26951 md5_process_bytes (&code, 1, &ctx);
26952 checksum_uleb128 (cur->lineno, &ctx);
26953 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
26955 md5_finish_ctx (&ctx, checksum);
26956 count = i - idx;
26958 /* From the containing include filename (if any) pick up just
26959 usable characters from its basename. */
26960 if (vec_safe_is_empty (files))
26961 base = "";
26962 else
26963 base = lbasename (files->last ().info);
26964 for (encoded_filename_len = 0, i = 0; base[i]; i++)
26965 if (ISIDNUM (base[i]) || base[i] == '.')
26966 encoded_filename_len++;
26967 /* Count . at the end. */
26968 if (encoded_filename_len)
26969 encoded_filename_len++;
26971 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
26972 linebuf_len = strlen (linebuf);
26974 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
26975 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
26976 + 16 * 2 + 1);
26977 memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
26978 tail = grp_name + 4;
26979 if (encoded_filename_len)
26981 for (i = 0; base[i]; i++)
26982 if (ISIDNUM (base[i]) || base[i] == '.')
26983 *tail++ = base[i];
26984 *tail++ = '.';
26986 memcpy (tail, linebuf, linebuf_len);
26987 tail += linebuf_len;
26988 *tail++ = '.';
26989 for (i = 0; i < 16; i++)
26990 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
26992 /* Construct a macinfo_entry for DW_MACRO_import
26993 in the empty vector entry before the first define/undef. */
26994 inc = &(*macinfo_table)[idx - 1];
26995 inc->code = DW_MACRO_import;
26996 inc->lineno = 0;
26997 inc->info = ggc_strdup (grp_name);
26998 if (!*macinfo_htab)
26999 *macinfo_htab = new macinfo_hash_type (10);
27000 /* Avoid emitting duplicates. */
27001 slot = (*macinfo_htab)->find_slot (inc, INSERT);
27002 if (*slot != NULL)
27004 inc->code = 0;
27005 inc->info = NULL;
27006 /* If such an entry has been used before, just emit
27007 a DW_MACRO_import op. */
27008 inc = *slot;
27009 output_macinfo_op (inc);
27010 /* And clear all macinfo_entry in the range to avoid emitting them
27011 in the second pass. */
27012 for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
27014 cur->code = 0;
27015 cur->info = NULL;
27018 else
27020 *slot = inc;
27021 inc->lineno = (*macinfo_htab)->elements ();
27022 output_macinfo_op (inc);
27024 return count;
27027 /* Save any strings needed by the macinfo table in the debug str
27028 table. All strings must be collected into the table by the time
27029 index_string is called. */
27031 static void
27032 save_macinfo_strings (void)
27034 unsigned len;
27035 unsigned i;
27036 macinfo_entry *ref;
27038 for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
27040 switch (ref->code)
27042 /* Match the logic in output_macinfo_op to decide on
27043 indirect strings. */
27044 case DW_MACINFO_define:
27045 case DW_MACINFO_undef:
27046 len = strlen (ref->info) + 1;
27047 if (!dwarf_strict
27048 && len > DWARF_OFFSET_SIZE
27049 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
27050 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
27051 set_indirect_string (find_AT_string (ref->info));
27052 break;
27053 case DW_MACRO_define_strp:
27054 case DW_MACRO_undef_strp:
27055 set_indirect_string (find_AT_string (ref->info));
27056 break;
27057 default:
27058 break;
27063 /* Output macinfo section(s). */
27065 static void
27066 output_macinfo (void)
27068 unsigned i;
27069 unsigned long length = vec_safe_length (macinfo_table);
27070 macinfo_entry *ref;
27071 vec<macinfo_entry, va_gc> *files = NULL;
27072 macinfo_hash_type *macinfo_htab = NULL;
27074 if (! length)
27075 return;
27077 /* output_macinfo* uses these interchangeably. */
27078 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_define
27079 && (int) DW_MACINFO_undef == (int) DW_MACRO_undef
27080 && (int) DW_MACINFO_start_file == (int) DW_MACRO_start_file
27081 && (int) DW_MACINFO_end_file == (int) DW_MACRO_end_file);
27083 /* For .debug_macro emit the section header. */
27084 if (!dwarf_strict || dwarf_version >= 5)
27086 dw2_asm_output_data (2, dwarf_version >= 5 ? 5 : 4,
27087 "DWARF macro version number");
27088 if (DWARF_OFFSET_SIZE == 8)
27089 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
27090 else
27091 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
27092 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
27093 (!dwarf_split_debug_info ? debug_line_section_label
27094 : debug_skeleton_line_section_label),
27095 debug_line_section, NULL);
27098 /* In the first loop, it emits the primary .debug_macinfo section
27099 and after each emitted op the macinfo_entry is cleared.
27100 If a longer range of define/undef ops can be optimized using
27101 DW_MACRO_import, the DW_MACRO_import op is emitted and kept in
27102 the vector before the first define/undef in the range and the
27103 whole range of define/undef ops is not emitted and kept. */
27104 for (i = 0; macinfo_table->iterate (i, &ref); i++)
27106 switch (ref->code)
27108 case DW_MACINFO_start_file:
27109 vec_safe_push (files, *ref);
27110 break;
27111 case DW_MACINFO_end_file:
27112 if (!vec_safe_is_empty (files))
27113 files->pop ();
27114 break;
27115 case DW_MACINFO_define:
27116 case DW_MACINFO_undef:
27117 if ((!dwarf_strict || dwarf_version >= 5)
27118 && HAVE_COMDAT_GROUP
27119 && vec_safe_length (files) != 1
27120 && i > 0
27121 && i + 1 < length
27122 && (*macinfo_table)[i - 1].code == 0)
27124 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
27125 if (count)
27127 i += count - 1;
27128 continue;
27131 break;
27132 case 0:
27133 /* A dummy entry may be inserted at the beginning to be able
27134 to optimize the whole block of predefined macros. */
27135 if (i == 0)
27136 continue;
27137 default:
27138 break;
27140 output_macinfo_op (ref);
27141 ref->info = NULL;
27142 ref->code = 0;
27145 if (!macinfo_htab)
27146 return;
27148 delete macinfo_htab;
27149 macinfo_htab = NULL;
27151 /* If any DW_MACRO_import were used, on those DW_MACRO_import entries
27152 terminate the current chain and switch to a new comdat .debug_macinfo
27153 section and emit the define/undef entries within it. */
27154 for (i = 0; macinfo_table->iterate (i, &ref); i++)
27155 switch (ref->code)
27157 case 0:
27158 continue;
27159 case DW_MACRO_import:
27161 char label[MAX_ARTIFICIAL_LABEL_BYTES];
27162 tree comdat_key = get_identifier (ref->info);
27163 /* Terminate the previous .debug_macinfo section. */
27164 dw2_asm_output_data (1, 0, "End compilation unit");
27165 targetm.asm_out.named_section (debug_macinfo_section_name,
27166 SECTION_DEBUG
27167 | SECTION_LINKONCE,
27168 comdat_key);
27169 ASM_GENERATE_INTERNAL_LABEL (label,
27170 DEBUG_MACRO_SECTION_LABEL,
27171 ref->lineno);
27172 ASM_OUTPUT_LABEL (asm_out_file, label);
27173 ref->code = 0;
27174 ref->info = NULL;
27175 dw2_asm_output_data (2, dwarf_version >= 5 ? 5 : 4,
27176 "DWARF macro version number");
27177 if (DWARF_OFFSET_SIZE == 8)
27178 dw2_asm_output_data (1, 1, "Flags: 64-bit");
27179 else
27180 dw2_asm_output_data (1, 0, "Flags: 32-bit");
27182 break;
27183 case DW_MACINFO_define:
27184 case DW_MACINFO_undef:
27185 output_macinfo_op (ref);
27186 ref->code = 0;
27187 ref->info = NULL;
27188 break;
27189 default:
27190 gcc_unreachable ();
27194 /* Initialize the various sections and labels for dwarf output. */
27196 static void
27197 init_sections_and_labels (void)
27199 if (!dwarf_split_debug_info)
27201 debug_info_section = get_section (DEBUG_INFO_SECTION,
27202 SECTION_DEBUG, NULL);
27203 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
27204 SECTION_DEBUG, NULL);
27205 debug_loc_section = get_section (dwarf_version >= 5
27206 ? DEBUG_LOCLISTS_SECTION
27207 : DEBUG_LOC_SECTION,
27208 SECTION_DEBUG, NULL);
27209 debug_macinfo_section_name
27210 = (dwarf_strict && dwarf_version < 5)
27211 ? DEBUG_MACINFO_SECTION : DEBUG_MACRO_SECTION;
27212 debug_macinfo_section = get_section (debug_macinfo_section_name,
27213 SECTION_DEBUG, NULL);
27215 else
27217 debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
27218 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
27219 debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
27220 SECTION_DEBUG | SECTION_EXCLUDE,
27221 NULL);
27222 debug_addr_section = get_section (DEBUG_ADDR_SECTION,
27223 SECTION_DEBUG, NULL);
27224 debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
27225 SECTION_DEBUG, NULL);
27226 debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
27227 SECTION_DEBUG, NULL);
27228 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
27229 DEBUG_SKELETON_ABBREV_SECTION_LABEL, 0);
27231 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections stay in
27232 the main .o, but the skeleton_line goes into the split off dwo. */
27233 debug_skeleton_line_section
27234 = get_section (DEBUG_DWO_LINE_SECTION,
27235 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
27236 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
27237 DEBUG_SKELETON_LINE_SECTION_LABEL, 0);
27238 debug_str_offsets_section = get_section (DEBUG_DWO_STR_OFFSETS_SECTION,
27239 SECTION_DEBUG | SECTION_EXCLUDE,
27240 NULL);
27241 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
27242 DEBUG_SKELETON_INFO_SECTION_LABEL, 0);
27243 debug_loc_section = get_section (dwarf_version >= 5
27244 ? DEBUG_DWO_LOCLISTS_SECTION
27245 : DEBUG_DWO_LOC_SECTION,
27246 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
27247 debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
27248 DEBUG_STR_DWO_SECTION_FLAGS, NULL);
27249 debug_macinfo_section_name
27250 = (dwarf_strict && dwarf_version < 5)
27251 ? DEBUG_DWO_MACINFO_SECTION : DEBUG_DWO_MACRO_SECTION;
27252 debug_macinfo_section = get_section (debug_macinfo_section_name,
27253 SECTION_DEBUG | SECTION_EXCLUDE,
27254 NULL);
27256 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
27257 SECTION_DEBUG, NULL);
27258 debug_line_section = get_section (DEBUG_LINE_SECTION,
27259 SECTION_DEBUG, NULL);
27260 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
27261 SECTION_DEBUG, NULL);
27262 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
27263 SECTION_DEBUG, NULL);
27264 debug_str_section = get_section (DEBUG_STR_SECTION,
27265 DEBUG_STR_SECTION_FLAGS, NULL);
27266 if (!dwarf_split_debug_info && !DWARF2_ASM_LINE_DEBUG_INFO)
27267 debug_line_str_section = get_section (DEBUG_LINE_STR_SECTION,
27268 DEBUG_STR_SECTION_FLAGS, NULL);
27270 debug_ranges_section = get_section (dwarf_version >= 5
27271 ? DEBUG_RNGLISTS_SECTION
27272 : DEBUG_RANGES_SECTION,
27273 SECTION_DEBUG, NULL);
27274 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
27275 SECTION_DEBUG, NULL);
27277 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
27278 DEBUG_ABBREV_SECTION_LABEL, 0);
27279 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
27280 DEBUG_INFO_SECTION_LABEL, 0);
27281 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
27282 DEBUG_LINE_SECTION_LABEL, 0);
27283 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
27284 DEBUG_RANGES_SECTION_LABEL, 0);
27285 if (dwarf_version >= 5 && dwarf_split_debug_info)
27286 ASM_GENERATE_INTERNAL_LABEL (ranges_base_label,
27287 DEBUG_RANGES_SECTION_LABEL, 1);
27288 ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
27289 DEBUG_ADDR_SECTION_LABEL, 0);
27290 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
27291 (dwarf_strict && dwarf_version < 5)
27292 ? DEBUG_MACINFO_SECTION_LABEL
27293 : DEBUG_MACRO_SECTION_LABEL, 0);
27294 ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL, 0);
27297 /* Set up for Dwarf output at the start of compilation. */
27299 static void
27300 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
27302 /* This option is currently broken, see (PR53118 and PR46102). */
27303 if (flag_eliminate_dwarf2_dups
27304 && strstr (lang_hooks.name, "C++"))
27306 warning (0, "-feliminate-dwarf2-dups is broken for C++, ignoring");
27307 flag_eliminate_dwarf2_dups = 0;
27310 /* Allocate the file_table. */
27311 file_table = hash_table<dwarf_file_hasher>::create_ggc (50);
27313 #ifndef DWARF2_LINENO_DEBUGGING_INFO
27314 /* Allocate the decl_die_table. */
27315 decl_die_table = hash_table<decl_die_hasher>::create_ggc (10);
27317 /* Allocate the decl_loc_table. */
27318 decl_loc_table = hash_table<decl_loc_hasher>::create_ggc (10);
27320 /* Allocate the cached_dw_loc_list_table. */
27321 cached_dw_loc_list_table = hash_table<dw_loc_list_hasher>::create_ggc (10);
27323 /* Allocate the initial hunk of the decl_scope_table. */
27324 vec_alloc (decl_scope_table, 256);
27326 /* Allocate the initial hunk of the abbrev_die_table. */
27327 vec_alloc (abbrev_die_table, 256);
27328 /* Zero-th entry is allocated, but unused. */
27329 abbrev_die_table->quick_push (NULL);
27331 /* Allocate the dwarf_proc_stack_usage_map. */
27332 dwarf_proc_stack_usage_map = new hash_map<dw_die_ref, int>;
27334 /* Allocate the pubtypes and pubnames vectors. */
27335 vec_alloc (pubname_table, 32);
27336 vec_alloc (pubtype_table, 32);
27338 vec_alloc (incomplete_types, 64);
27340 vec_alloc (used_rtx_array, 32);
27342 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
27343 vec_alloc (macinfo_table, 64);
27344 #endif
27346 /* If front-ends already registered a main translation unit but we were not
27347 ready to perform the association, do this now. */
27348 if (main_translation_unit != NULL_TREE)
27349 equate_decl_number_to_die (main_translation_unit, comp_unit_die ());
27352 /* Called before compile () starts outputtting functions, variables
27353 and toplevel asms into assembly. */
27355 static void
27356 dwarf2out_assembly_start (void)
27358 #ifndef DWARF2_LINENO_DEBUGGING_INFO
27359 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
27360 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
27361 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
27362 COLD_TEXT_SECTION_LABEL, 0);
27363 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
27365 switch_to_section (text_section);
27366 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
27367 #endif
27369 /* Make sure the line number table for .text always exists. */
27370 text_section_line_info = new_line_info_table ();
27371 text_section_line_info->end_label = text_end_label;
27373 #ifdef DWARF2_LINENO_DEBUGGING_INFO
27374 cur_line_info_table = text_section_line_info;
27375 #endif
27377 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
27378 && dwarf2out_do_cfi_asm ()
27379 && (!(flag_unwind_tables || flag_exceptions)
27380 || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
27381 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
27384 /* A helper function for dwarf2out_finish called through
27385 htab_traverse. Assign a string its index. All strings must be
27386 collected into the table by the time index_string is called,
27387 because the indexing code relies on htab_traverse to traverse nodes
27388 in the same order for each run. */
27391 index_string (indirect_string_node **h, unsigned int *index)
27393 indirect_string_node *node = *h;
27395 find_string_form (node);
27396 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
27398 gcc_assert (node->index == NO_INDEX_ASSIGNED);
27399 node->index = *index;
27400 *index += 1;
27402 return 1;
27405 /* A helper function for output_indirect_strings called through
27406 htab_traverse. Output the offset to a string and update the
27407 current offset. */
27410 output_index_string_offset (indirect_string_node **h, unsigned int *offset)
27412 indirect_string_node *node = *h;
27414 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
27416 /* Assert that this node has been assigned an index. */
27417 gcc_assert (node->index != NO_INDEX_ASSIGNED
27418 && node->index != NOT_INDEXED);
27419 dw2_asm_output_data (DWARF_OFFSET_SIZE, *offset,
27420 "indexed string 0x%x: %s", node->index, node->str);
27421 *offset += strlen (node->str) + 1;
27423 return 1;
27426 /* A helper function for dwarf2out_finish called through
27427 htab_traverse. Output the indexed string. */
27430 output_index_string (indirect_string_node **h, unsigned int *cur_idx)
27432 struct indirect_string_node *node = *h;
27434 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
27436 /* Assert that the strings are output in the same order as their
27437 indexes were assigned. */
27438 gcc_assert (*cur_idx == node->index);
27439 assemble_string (node->str, strlen (node->str) + 1);
27440 *cur_idx += 1;
27442 return 1;
27445 /* A helper function for dwarf2out_finish called through
27446 htab_traverse. Emit one queued .debug_str string. */
27449 output_indirect_string (indirect_string_node **h, enum dwarf_form form)
27451 struct indirect_string_node *node = *h;
27453 node->form = find_string_form (node);
27454 if (node->form == form && node->refcount > 0)
27456 ASM_OUTPUT_LABEL (asm_out_file, node->label);
27457 assemble_string (node->str, strlen (node->str) + 1);
27460 return 1;
27463 /* Output the indexed string table. */
27465 static void
27466 output_indirect_strings (void)
27468 switch_to_section (debug_str_section);
27469 if (!dwarf_split_debug_info)
27470 debug_str_hash->traverse<enum dwarf_form,
27471 output_indirect_string> (DW_FORM_strp);
27472 else
27474 unsigned int offset = 0;
27475 unsigned int cur_idx = 0;
27477 skeleton_debug_str_hash->traverse<enum dwarf_form,
27478 output_indirect_string> (DW_FORM_strp);
27480 switch_to_section (debug_str_offsets_section);
27481 debug_str_hash->traverse_noresize
27482 <unsigned int *, output_index_string_offset> (&offset);
27483 switch_to_section (debug_str_dwo_section);
27484 debug_str_hash->traverse_noresize<unsigned int *, output_index_string>
27485 (&cur_idx);
27489 /* Callback for htab_traverse to assign an index to an entry in the
27490 table, and to write that entry to the .debug_addr section. */
27493 output_addr_table_entry (addr_table_entry **slot, unsigned int *cur_index)
27495 addr_table_entry *entry = *slot;
27497 if (entry->refcount == 0)
27499 gcc_assert (entry->index == NO_INDEX_ASSIGNED
27500 || entry->index == NOT_INDEXED);
27501 return 1;
27504 gcc_assert (entry->index == *cur_index);
27505 (*cur_index)++;
27507 switch (entry->kind)
27509 case ate_kind_rtx:
27510 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
27511 "0x%x", entry->index);
27512 break;
27513 case ate_kind_rtx_dtprel:
27514 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
27515 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
27516 DWARF2_ADDR_SIZE,
27517 entry->addr.rtl);
27518 fputc ('\n', asm_out_file);
27519 break;
27520 case ate_kind_label:
27521 dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
27522 "0x%x", entry->index);
27523 break;
27524 default:
27525 gcc_unreachable ();
27527 return 1;
27530 /* Produce the .debug_addr section. */
27532 static void
27533 output_addr_table (void)
27535 unsigned int index = 0;
27536 if (addr_index_table == NULL || addr_index_table->size () == 0)
27537 return;
27539 switch_to_section (debug_addr_section);
27540 addr_index_table
27541 ->traverse_noresize<unsigned int *, output_addr_table_entry> (&index);
27544 #if ENABLE_ASSERT_CHECKING
27545 /* Verify that all marks are clear. */
27547 static void
27548 verify_marks_clear (dw_die_ref die)
27550 dw_die_ref c;
27552 gcc_assert (! die->die_mark);
27553 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
27555 #endif /* ENABLE_ASSERT_CHECKING */
27557 /* Clear the marks for a die and its children.
27558 Be cool if the mark isn't set. */
27560 static void
27561 prune_unmark_dies (dw_die_ref die)
27563 dw_die_ref c;
27565 if (die->die_mark)
27566 die->die_mark = 0;
27567 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
27570 /* Given LOC that is referenced by a DIE we're marking as used, find all
27571 referenced DWARF procedures it references and mark them as used. */
27573 static void
27574 prune_unused_types_walk_loc_descr (dw_loc_descr_ref loc)
27576 for (; loc != NULL; loc = loc->dw_loc_next)
27577 switch (loc->dw_loc_opc)
27579 case DW_OP_implicit_pointer:
27580 case DW_OP_convert:
27581 case DW_OP_reinterpret:
27582 case DW_OP_GNU_implicit_pointer:
27583 case DW_OP_GNU_convert:
27584 case DW_OP_GNU_reinterpret:
27585 if (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref)
27586 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
27587 break;
27588 case DW_OP_GNU_variable_value:
27589 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
27591 dw_die_ref ref
27592 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
27593 if (ref == NULL)
27594 break;
27595 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
27596 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
27597 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
27599 /* FALLTHRU */
27600 case DW_OP_call2:
27601 case DW_OP_call4:
27602 case DW_OP_call_ref:
27603 case DW_OP_const_type:
27604 case DW_OP_GNU_const_type:
27605 case DW_OP_GNU_parameter_ref:
27606 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref);
27607 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
27608 break;
27609 case DW_OP_regval_type:
27610 case DW_OP_deref_type:
27611 case DW_OP_GNU_regval_type:
27612 case DW_OP_GNU_deref_type:
27613 gcc_assert (loc->dw_loc_oprnd2.val_class == dw_val_class_die_ref);
27614 prune_unused_types_mark (loc->dw_loc_oprnd2.v.val_die_ref.die, 1);
27615 break;
27616 case DW_OP_entry_value:
27617 case DW_OP_GNU_entry_value:
27618 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_loc);
27619 prune_unused_types_walk_loc_descr (loc->dw_loc_oprnd1.v.val_loc);
27620 break;
27621 default:
27622 break;
27626 /* Given DIE that we're marking as used, find any other dies
27627 it references as attributes and mark them as used. */
27629 static void
27630 prune_unused_types_walk_attribs (dw_die_ref die)
27632 dw_attr_node *a;
27633 unsigned ix;
27635 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
27637 switch (AT_class (a))
27639 /* Make sure DWARF procedures referenced by location descriptions will
27640 get emitted. */
27641 case dw_val_class_loc:
27642 prune_unused_types_walk_loc_descr (AT_loc (a));
27643 break;
27644 case dw_val_class_loc_list:
27645 for (dw_loc_list_ref list = AT_loc_list (a);
27646 list != NULL;
27647 list = list->dw_loc_next)
27648 prune_unused_types_walk_loc_descr (list->expr);
27649 break;
27651 case dw_val_class_die_ref:
27652 /* A reference to another DIE.
27653 Make sure that it will get emitted.
27654 If it was broken out into a comdat group, don't follow it. */
27655 if (! AT_ref (a)->comdat_type_p
27656 || a->dw_attr == DW_AT_specification)
27657 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
27658 break;
27660 case dw_val_class_str:
27661 /* Set the string's refcount to 0 so that prune_unused_types_mark
27662 accounts properly for it. */
27663 a->dw_attr_val.v.val_str->refcount = 0;
27664 break;
27666 default:
27667 break;
27672 /* Mark the generic parameters and arguments children DIEs of DIE. */
27674 static void
27675 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
27677 dw_die_ref c;
27679 if (die == NULL || die->die_child == NULL)
27680 return;
27681 c = die->die_child;
27684 if (is_template_parameter (c))
27685 prune_unused_types_mark (c, 1);
27686 c = c->die_sib;
27687 } while (c && c != die->die_child);
27690 /* Mark DIE as being used. If DOKIDS is true, then walk down
27691 to DIE's children. */
27693 static void
27694 prune_unused_types_mark (dw_die_ref die, int dokids)
27696 dw_die_ref c;
27698 if (die->die_mark == 0)
27700 /* We haven't done this node yet. Mark it as used. */
27701 die->die_mark = 1;
27702 /* If this is the DIE of a generic type instantiation,
27703 mark the children DIEs that describe its generic parms and
27704 args. */
27705 prune_unused_types_mark_generic_parms_dies (die);
27707 /* We also have to mark its parents as used.
27708 (But we don't want to mark our parent's kids due to this,
27709 unless it is a class.) */
27710 if (die->die_parent)
27711 prune_unused_types_mark (die->die_parent,
27712 class_scope_p (die->die_parent));
27714 /* Mark any referenced nodes. */
27715 prune_unused_types_walk_attribs (die);
27717 /* If this node is a specification,
27718 also mark the definition, if it exists. */
27719 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
27720 prune_unused_types_mark (die->die_definition, 1);
27723 if (dokids && die->die_mark != 2)
27725 /* We need to walk the children, but haven't done so yet.
27726 Remember that we've walked the kids. */
27727 die->die_mark = 2;
27729 /* If this is an array type, we need to make sure our
27730 kids get marked, even if they're types. If we're
27731 breaking out types into comdat sections, do this
27732 for all type definitions. */
27733 if (die->die_tag == DW_TAG_array_type
27734 || (use_debug_types
27735 && is_type_die (die) && ! is_declaration_die (die)))
27736 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
27737 else
27738 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
27742 /* For local classes, look if any static member functions were emitted
27743 and if so, mark them. */
27745 static void
27746 prune_unused_types_walk_local_classes (dw_die_ref die)
27748 dw_die_ref c;
27750 if (die->die_mark == 2)
27751 return;
27753 switch (die->die_tag)
27755 case DW_TAG_structure_type:
27756 case DW_TAG_union_type:
27757 case DW_TAG_class_type:
27758 break;
27760 case DW_TAG_subprogram:
27761 if (!get_AT_flag (die, DW_AT_declaration)
27762 || die->die_definition != NULL)
27763 prune_unused_types_mark (die, 1);
27764 return;
27766 default:
27767 return;
27770 /* Mark children. */
27771 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
27774 /* Walk the tree DIE and mark types that we actually use. */
27776 static void
27777 prune_unused_types_walk (dw_die_ref die)
27779 dw_die_ref c;
27781 /* Don't do anything if this node is already marked and
27782 children have been marked as well. */
27783 if (die->die_mark == 2)
27784 return;
27786 switch (die->die_tag)
27788 case DW_TAG_structure_type:
27789 case DW_TAG_union_type:
27790 case DW_TAG_class_type:
27791 if (die->die_perennial_p)
27792 break;
27794 for (c = die->die_parent; c; c = c->die_parent)
27795 if (c->die_tag == DW_TAG_subprogram)
27796 break;
27798 /* Finding used static member functions inside of classes
27799 is needed just for local classes, because for other classes
27800 static member function DIEs with DW_AT_specification
27801 are emitted outside of the DW_TAG_*_type. If we ever change
27802 it, we'd need to call this even for non-local classes. */
27803 if (c)
27804 prune_unused_types_walk_local_classes (die);
27806 /* It's a type node --- don't mark it. */
27807 return;
27809 case DW_TAG_const_type:
27810 case DW_TAG_packed_type:
27811 case DW_TAG_pointer_type:
27812 case DW_TAG_reference_type:
27813 case DW_TAG_rvalue_reference_type:
27814 case DW_TAG_volatile_type:
27815 case DW_TAG_typedef:
27816 case DW_TAG_array_type:
27817 case DW_TAG_interface_type:
27818 case DW_TAG_friend:
27819 case DW_TAG_enumeration_type:
27820 case DW_TAG_subroutine_type:
27821 case DW_TAG_string_type:
27822 case DW_TAG_set_type:
27823 case DW_TAG_subrange_type:
27824 case DW_TAG_ptr_to_member_type:
27825 case DW_TAG_file_type:
27826 /* Type nodes are useful only when other DIEs reference them --- don't
27827 mark them. */
27828 /* FALLTHROUGH */
27830 case DW_TAG_dwarf_procedure:
27831 /* Likewise for DWARF procedures. */
27833 if (die->die_perennial_p)
27834 break;
27836 return;
27838 default:
27839 /* Mark everything else. */
27840 break;
27843 if (die->die_mark == 0)
27845 die->die_mark = 1;
27847 /* Now, mark any dies referenced from here. */
27848 prune_unused_types_walk_attribs (die);
27851 die->die_mark = 2;
27853 /* Mark children. */
27854 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
27857 /* Increment the string counts on strings referred to from DIE's
27858 attributes. */
27860 static void
27861 prune_unused_types_update_strings (dw_die_ref die)
27863 dw_attr_node *a;
27864 unsigned ix;
27866 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
27867 if (AT_class (a) == dw_val_class_str)
27869 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
27870 s->refcount++;
27871 /* Avoid unnecessarily putting strings that are used less than
27872 twice in the hash table. */
27873 if (s->refcount
27874 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
27876 indirect_string_node **slot
27877 = debug_str_hash->find_slot_with_hash (s->str,
27878 htab_hash_string (s->str),
27879 INSERT);
27880 gcc_assert (*slot == NULL);
27881 *slot = s;
27886 /* Mark DIE and its children as removed. */
27888 static void
27889 mark_removed (dw_die_ref die)
27891 dw_die_ref c;
27892 die->removed = true;
27893 FOR_EACH_CHILD (die, c, mark_removed (c));
27896 /* Remove from the tree DIE any dies that aren't marked. */
27898 static void
27899 prune_unused_types_prune (dw_die_ref die)
27901 dw_die_ref c;
27903 gcc_assert (die->die_mark);
27904 prune_unused_types_update_strings (die);
27906 if (! die->die_child)
27907 return;
27909 c = die->die_child;
27910 do {
27911 dw_die_ref prev = c, next;
27912 for (c = c->die_sib; ! c->die_mark; c = next)
27913 if (c == die->die_child)
27915 /* No marked children between 'prev' and the end of the list. */
27916 if (prev == c)
27917 /* No marked children at all. */
27918 die->die_child = NULL;
27919 else
27921 prev->die_sib = c->die_sib;
27922 die->die_child = prev;
27924 c->die_sib = NULL;
27925 mark_removed (c);
27926 return;
27928 else
27930 next = c->die_sib;
27931 c->die_sib = NULL;
27932 mark_removed (c);
27935 if (c != prev->die_sib)
27936 prev->die_sib = c;
27937 prune_unused_types_prune (c);
27938 } while (c != die->die_child);
27941 /* Remove dies representing declarations that we never use. */
27943 static void
27944 prune_unused_types (void)
27946 unsigned int i;
27947 limbo_die_node *node;
27948 comdat_type_node *ctnode;
27949 pubname_entry *pub;
27950 dw_die_ref base_type;
27952 #if ENABLE_ASSERT_CHECKING
27953 /* All the marks should already be clear. */
27954 verify_marks_clear (comp_unit_die ());
27955 for (node = limbo_die_list; node; node = node->next)
27956 verify_marks_clear (node->die);
27957 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
27958 verify_marks_clear (ctnode->root_die);
27959 #endif /* ENABLE_ASSERT_CHECKING */
27961 /* Mark types that are used in global variables. */
27962 premark_types_used_by_global_vars ();
27964 /* Set the mark on nodes that are actually used. */
27965 prune_unused_types_walk (comp_unit_die ());
27966 for (node = limbo_die_list; node; node = node->next)
27967 prune_unused_types_walk (node->die);
27968 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
27970 prune_unused_types_walk (ctnode->root_die);
27971 prune_unused_types_mark (ctnode->type_die, 1);
27974 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
27975 are unusual in that they are pubnames that are the children of pubtypes.
27976 They should only be marked via their parent DW_TAG_enumeration_type die,
27977 not as roots in themselves. */
27978 FOR_EACH_VEC_ELT (*pubname_table, i, pub)
27979 if (pub->die->die_tag != DW_TAG_enumerator)
27980 prune_unused_types_mark (pub->die, 1);
27981 for (i = 0; base_types.iterate (i, &base_type); i++)
27982 prune_unused_types_mark (base_type, 1);
27984 /* For -fvar-tracking-assignments, also set the mark on nodes that could be
27985 referenced by DW_TAG_call_site DW_AT_call_origin (i.e. direct call
27986 callees). */
27987 cgraph_node *cnode;
27988 FOR_EACH_FUNCTION (cnode)
27989 if (cnode->referred_to_p (false))
27991 dw_die_ref die = lookup_decl_die (cnode->decl);
27992 if (die == NULL || die->die_mark)
27993 continue;
27994 for (cgraph_edge *e = cnode->callers; e; e = e->next_caller)
27995 if (e->caller != cnode
27996 && opt_for_fn (e->caller->decl, flag_var_tracking_assignments))
27998 prune_unused_types_mark (die, 1);
27999 break;
28003 if (debug_str_hash)
28004 debug_str_hash->empty ();
28005 if (skeleton_debug_str_hash)
28006 skeleton_debug_str_hash->empty ();
28007 prune_unused_types_prune (comp_unit_die ());
28008 for (limbo_die_node **pnode = &limbo_die_list; *pnode; )
28010 node = *pnode;
28011 if (!node->die->die_mark)
28012 *pnode = node->next;
28013 else
28015 prune_unused_types_prune (node->die);
28016 pnode = &node->next;
28019 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
28020 prune_unused_types_prune (ctnode->root_die);
28022 /* Leave the marks clear. */
28023 prune_unmark_dies (comp_unit_die ());
28024 for (node = limbo_die_list; node; node = node->next)
28025 prune_unmark_dies (node->die);
28026 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
28027 prune_unmark_dies (ctnode->root_die);
28030 /* Helpers to manipulate hash table of comdat type units. */
28032 struct comdat_type_hasher : nofree_ptr_hash <comdat_type_node>
28034 static inline hashval_t hash (const comdat_type_node *);
28035 static inline bool equal (const comdat_type_node *, const comdat_type_node *);
28038 inline hashval_t
28039 comdat_type_hasher::hash (const comdat_type_node *type_node)
28041 hashval_t h;
28042 memcpy (&h, type_node->signature, sizeof (h));
28043 return h;
28046 inline bool
28047 comdat_type_hasher::equal (const comdat_type_node *type_node_1,
28048 const comdat_type_node *type_node_2)
28050 return (! memcmp (type_node_1->signature, type_node_2->signature,
28051 DWARF_TYPE_SIGNATURE_SIZE));
28054 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
28055 to the location it would have been added, should we know its
28056 DECL_ASSEMBLER_NAME when we added other attributes. This will
28057 probably improve compactness of debug info, removing equivalent
28058 abbrevs, and hide any differences caused by deferring the
28059 computation of the assembler name, triggered by e.g. PCH. */
28061 static inline void
28062 move_linkage_attr (dw_die_ref die)
28064 unsigned ix = vec_safe_length (die->die_attr);
28065 dw_attr_node linkage = (*die->die_attr)[ix - 1];
28067 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
28068 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
28070 while (--ix > 0)
28072 dw_attr_node *prev = &(*die->die_attr)[ix - 1];
28074 if (prev->dw_attr == DW_AT_decl_line
28075 || prev->dw_attr == DW_AT_decl_column
28076 || prev->dw_attr == DW_AT_name)
28077 break;
28080 if (ix != vec_safe_length (die->die_attr) - 1)
28082 die->die_attr->pop ();
28083 die->die_attr->quick_insert (ix, linkage);
28087 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
28088 referenced from typed stack ops and count how often they are used. */
28090 static void
28091 mark_base_types (dw_loc_descr_ref loc)
28093 dw_die_ref base_type = NULL;
28095 for (; loc; loc = loc->dw_loc_next)
28097 switch (loc->dw_loc_opc)
28099 case DW_OP_regval_type:
28100 case DW_OP_deref_type:
28101 case DW_OP_GNU_regval_type:
28102 case DW_OP_GNU_deref_type:
28103 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
28104 break;
28105 case DW_OP_convert:
28106 case DW_OP_reinterpret:
28107 case DW_OP_GNU_convert:
28108 case DW_OP_GNU_reinterpret:
28109 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
28110 continue;
28111 /* FALLTHRU */
28112 case DW_OP_const_type:
28113 case DW_OP_GNU_const_type:
28114 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
28115 break;
28116 case DW_OP_entry_value:
28117 case DW_OP_GNU_entry_value:
28118 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
28119 continue;
28120 default:
28121 continue;
28123 gcc_assert (base_type->die_parent == comp_unit_die ());
28124 if (base_type->die_mark)
28125 base_type->die_mark++;
28126 else
28128 base_types.safe_push (base_type);
28129 base_type->die_mark = 1;
28134 /* Comparison function for sorting marked base types. */
28136 static int
28137 base_type_cmp (const void *x, const void *y)
28139 dw_die_ref dx = *(const dw_die_ref *) x;
28140 dw_die_ref dy = *(const dw_die_ref *) y;
28141 unsigned int byte_size1, byte_size2;
28142 unsigned int encoding1, encoding2;
28143 unsigned int align1, align2;
28144 if (dx->die_mark > dy->die_mark)
28145 return -1;
28146 if (dx->die_mark < dy->die_mark)
28147 return 1;
28148 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
28149 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
28150 if (byte_size1 < byte_size2)
28151 return 1;
28152 if (byte_size1 > byte_size2)
28153 return -1;
28154 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
28155 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
28156 if (encoding1 < encoding2)
28157 return 1;
28158 if (encoding1 > encoding2)
28159 return -1;
28160 align1 = get_AT_unsigned (dx, DW_AT_alignment);
28161 align2 = get_AT_unsigned (dy, DW_AT_alignment);
28162 if (align1 < align2)
28163 return 1;
28164 if (align1 > align2)
28165 return -1;
28166 return 0;
28169 /* Move base types marked by mark_base_types as early as possible
28170 in the CU, sorted by decreasing usage count both to make the
28171 uleb128 references as small as possible and to make sure they
28172 will have die_offset already computed by calc_die_sizes when
28173 sizes of typed stack loc ops is computed. */
28175 static void
28176 move_marked_base_types (void)
28178 unsigned int i;
28179 dw_die_ref base_type, die, c;
28181 if (base_types.is_empty ())
28182 return;
28184 /* Sort by decreasing usage count, they will be added again in that
28185 order later on. */
28186 base_types.qsort (base_type_cmp);
28187 die = comp_unit_die ();
28188 c = die->die_child;
28191 dw_die_ref prev = c;
28192 c = c->die_sib;
28193 while (c->die_mark)
28195 remove_child_with_prev (c, prev);
28196 /* As base types got marked, there must be at least
28197 one node other than DW_TAG_base_type. */
28198 gcc_assert (die->die_child != NULL);
28199 c = prev->die_sib;
28202 while (c != die->die_child);
28203 gcc_assert (die->die_child);
28204 c = die->die_child;
28205 for (i = 0; base_types.iterate (i, &base_type); i++)
28207 base_type->die_mark = 0;
28208 base_type->die_sib = c->die_sib;
28209 c->die_sib = base_type;
28210 c = base_type;
28214 /* Helper function for resolve_addr, attempt to resolve
28215 one CONST_STRING, return true if successful. Similarly verify that
28216 SYMBOL_REFs refer to variables emitted in the current CU. */
28218 static bool
28219 resolve_one_addr (rtx *addr)
28221 rtx rtl = *addr;
28223 if (GET_CODE (rtl) == CONST_STRING)
28225 size_t len = strlen (XSTR (rtl, 0)) + 1;
28226 tree t = build_string (len, XSTR (rtl, 0));
28227 tree tlen = size_int (len - 1);
28228 TREE_TYPE (t)
28229 = build_array_type (char_type_node, build_index_type (tlen));
28230 rtl = lookup_constant_def (t);
28231 if (!rtl || !MEM_P (rtl))
28232 return false;
28233 rtl = XEXP (rtl, 0);
28234 if (GET_CODE (rtl) == SYMBOL_REF
28235 && SYMBOL_REF_DECL (rtl)
28236 && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
28237 return false;
28238 vec_safe_push (used_rtx_array, rtl);
28239 *addr = rtl;
28240 return true;
28243 if (GET_CODE (rtl) == SYMBOL_REF
28244 && SYMBOL_REF_DECL (rtl))
28246 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
28248 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
28249 return false;
28251 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
28252 return false;
28255 if (GET_CODE (rtl) == CONST)
28257 subrtx_ptr_iterator::array_type array;
28258 FOR_EACH_SUBRTX_PTR (iter, array, &XEXP (rtl, 0), ALL)
28259 if (!resolve_one_addr (*iter))
28260 return false;
28263 return true;
28266 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
28267 if possible, and create DW_TAG_dwarf_procedure that can be referenced
28268 from DW_OP_implicit_pointer if the string hasn't been seen yet. */
28270 static rtx
28271 string_cst_pool_decl (tree t)
28273 rtx rtl = output_constant_def (t, 1);
28274 unsigned char *array;
28275 dw_loc_descr_ref l;
28276 tree decl;
28277 size_t len;
28278 dw_die_ref ref;
28280 if (!rtl || !MEM_P (rtl))
28281 return NULL_RTX;
28282 rtl = XEXP (rtl, 0);
28283 if (GET_CODE (rtl) != SYMBOL_REF
28284 || SYMBOL_REF_DECL (rtl) == NULL_TREE)
28285 return NULL_RTX;
28287 decl = SYMBOL_REF_DECL (rtl);
28288 if (!lookup_decl_die (decl))
28290 len = TREE_STRING_LENGTH (t);
28291 vec_safe_push (used_rtx_array, rtl);
28292 ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
28293 array = ggc_vec_alloc<unsigned char> (len);
28294 memcpy (array, TREE_STRING_POINTER (t), len);
28295 l = new_loc_descr (DW_OP_implicit_value, len, 0);
28296 l->dw_loc_oprnd2.val_class = dw_val_class_vec;
28297 l->dw_loc_oprnd2.v.val_vec.length = len;
28298 l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
28299 l->dw_loc_oprnd2.v.val_vec.array = array;
28300 add_AT_loc (ref, DW_AT_location, l);
28301 equate_decl_number_to_die (decl, ref);
28303 return rtl;
28306 /* Helper function of resolve_addr_in_expr. LOC is
28307 a DW_OP_addr followed by DW_OP_stack_value, either at the start
28308 of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
28309 resolved. Replace it (both DW_OP_addr and DW_OP_stack_value)
28310 with DW_OP_implicit_pointer if possible
28311 and return true, if unsuccessful, return false. */
28313 static bool
28314 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
28316 rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
28317 HOST_WIDE_INT offset = 0;
28318 dw_die_ref ref = NULL;
28319 tree decl;
28321 if (GET_CODE (rtl) == CONST
28322 && GET_CODE (XEXP (rtl, 0)) == PLUS
28323 && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
28325 offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
28326 rtl = XEXP (XEXP (rtl, 0), 0);
28328 if (GET_CODE (rtl) == CONST_STRING)
28330 size_t len = strlen (XSTR (rtl, 0)) + 1;
28331 tree t = build_string (len, XSTR (rtl, 0));
28332 tree tlen = size_int (len - 1);
28334 TREE_TYPE (t)
28335 = build_array_type (char_type_node, build_index_type (tlen));
28336 rtl = string_cst_pool_decl (t);
28337 if (!rtl)
28338 return false;
28340 if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
28342 decl = SYMBOL_REF_DECL (rtl);
28343 if (VAR_P (decl) && !DECL_EXTERNAL (decl))
28345 ref = lookup_decl_die (decl);
28346 if (ref && (get_AT (ref, DW_AT_location)
28347 || get_AT (ref, DW_AT_const_value)))
28349 loc->dw_loc_opc = dwarf_OP (DW_OP_implicit_pointer);
28350 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
28351 loc->dw_loc_oprnd1.val_entry = NULL;
28352 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
28353 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
28354 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
28355 loc->dw_loc_oprnd2.v.val_int = offset;
28356 return true;
28360 return false;
28363 /* Helper function for resolve_addr, handle one location
28364 expression, return false if at least one CONST_STRING or SYMBOL_REF in
28365 the location list couldn't be resolved. */
28367 static bool
28368 resolve_addr_in_expr (dw_attr_node *a, dw_loc_descr_ref loc)
28370 dw_loc_descr_ref keep = NULL;
28371 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
28372 switch (loc->dw_loc_opc)
28374 case DW_OP_addr:
28375 if (!resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
28377 if ((prev == NULL
28378 || prev->dw_loc_opc == DW_OP_piece
28379 || prev->dw_loc_opc == DW_OP_bit_piece)
28380 && loc->dw_loc_next
28381 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
28382 && (!dwarf_strict || dwarf_version >= 5)
28383 && optimize_one_addr_into_implicit_ptr (loc))
28384 break;
28385 return false;
28387 break;
28388 case DW_OP_GNU_addr_index:
28389 case DW_OP_GNU_const_index:
28390 if (loc->dw_loc_opc == DW_OP_GNU_addr_index
28391 || (loc->dw_loc_opc == DW_OP_GNU_const_index && loc->dtprel))
28393 rtx rtl = loc->dw_loc_oprnd1.val_entry->addr.rtl;
28394 if (!resolve_one_addr (&rtl))
28395 return false;
28396 remove_addr_table_entry (loc->dw_loc_oprnd1.val_entry);
28397 loc->dw_loc_oprnd1.val_entry
28398 = add_addr_table_entry (rtl, ate_kind_rtx);
28400 break;
28401 case DW_OP_const4u:
28402 case DW_OP_const8u:
28403 if (loc->dtprel
28404 && !resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
28405 return false;
28406 break;
28407 case DW_OP_plus_uconst:
28408 if (size_of_loc_descr (loc)
28409 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
28411 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
28413 dw_loc_descr_ref repl
28414 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
28415 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
28416 add_loc_descr (&repl, loc->dw_loc_next);
28417 *loc = *repl;
28419 break;
28420 case DW_OP_implicit_value:
28421 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
28422 && !resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr))
28423 return false;
28424 break;
28425 case DW_OP_implicit_pointer:
28426 case DW_OP_GNU_implicit_pointer:
28427 case DW_OP_GNU_parameter_ref:
28428 case DW_OP_GNU_variable_value:
28429 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
28431 dw_die_ref ref
28432 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
28433 if (ref == NULL)
28434 return false;
28435 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
28436 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
28437 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
28439 if (loc->dw_loc_opc == DW_OP_GNU_variable_value)
28441 if (prev == NULL
28442 && loc->dw_loc_next == NULL
28443 && AT_class (a) == dw_val_class_loc)
28444 switch (a->dw_attr)
28446 /* Following attributes allow both exprloc and reference,
28447 so if the whole expression is DW_OP_GNU_variable_value
28448 alone we could transform it into reference. */
28449 case DW_AT_byte_size:
28450 case DW_AT_bit_size:
28451 case DW_AT_lower_bound:
28452 case DW_AT_upper_bound:
28453 case DW_AT_bit_stride:
28454 case DW_AT_count:
28455 case DW_AT_allocated:
28456 case DW_AT_associated:
28457 case DW_AT_byte_stride:
28458 a->dw_attr_val.val_class = dw_val_class_die_ref;
28459 a->dw_attr_val.val_entry = NULL;
28460 a->dw_attr_val.v.val_die_ref.die
28461 = loc->dw_loc_oprnd1.v.val_die_ref.die;
28462 a->dw_attr_val.v.val_die_ref.external = 0;
28463 return true;
28464 default:
28465 break;
28467 if (dwarf_strict)
28468 return false;
28470 break;
28471 case DW_OP_const_type:
28472 case DW_OP_regval_type:
28473 case DW_OP_deref_type:
28474 case DW_OP_convert:
28475 case DW_OP_reinterpret:
28476 case DW_OP_GNU_const_type:
28477 case DW_OP_GNU_regval_type:
28478 case DW_OP_GNU_deref_type:
28479 case DW_OP_GNU_convert:
28480 case DW_OP_GNU_reinterpret:
28481 while (loc->dw_loc_next
28482 && (loc->dw_loc_next->dw_loc_opc == DW_OP_convert
28483 || loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert))
28485 dw_die_ref base1, base2;
28486 unsigned enc1, enc2, size1, size2;
28487 if (loc->dw_loc_opc == DW_OP_regval_type
28488 || loc->dw_loc_opc == DW_OP_deref_type
28489 || loc->dw_loc_opc == DW_OP_GNU_regval_type
28490 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
28491 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
28492 else if (loc->dw_loc_oprnd1.val_class
28493 == dw_val_class_unsigned_const)
28494 break;
28495 else
28496 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
28497 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
28498 == dw_val_class_unsigned_const)
28499 break;
28500 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
28501 gcc_assert (base1->die_tag == DW_TAG_base_type
28502 && base2->die_tag == DW_TAG_base_type);
28503 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
28504 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
28505 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
28506 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
28507 if (size1 == size2
28508 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
28509 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
28510 && loc != keep)
28511 || enc1 == enc2))
28513 /* Optimize away next DW_OP_convert after
28514 adjusting LOC's base type die reference. */
28515 if (loc->dw_loc_opc == DW_OP_regval_type
28516 || loc->dw_loc_opc == DW_OP_deref_type
28517 || loc->dw_loc_opc == DW_OP_GNU_regval_type
28518 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
28519 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
28520 else
28521 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
28522 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
28523 continue;
28525 /* Don't change integer DW_OP_convert after e.g. floating
28526 point typed stack entry. */
28527 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
28528 keep = loc->dw_loc_next;
28529 break;
28531 break;
28532 default:
28533 break;
28535 return true;
28538 /* Helper function of resolve_addr. DIE had DW_AT_location of
28539 DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
28540 and DW_OP_addr couldn't be resolved. resolve_addr has already
28541 removed the DW_AT_location attribute. This function attempts to
28542 add a new DW_AT_location attribute with DW_OP_implicit_pointer
28543 to it or DW_AT_const_value attribute, if possible. */
28545 static void
28546 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
28548 if (!VAR_P (decl)
28549 || lookup_decl_die (decl) != die
28550 || DECL_EXTERNAL (decl)
28551 || !TREE_STATIC (decl)
28552 || DECL_INITIAL (decl) == NULL_TREE
28553 || DECL_P (DECL_INITIAL (decl))
28554 || get_AT (die, DW_AT_const_value))
28555 return;
28557 tree init = DECL_INITIAL (decl);
28558 HOST_WIDE_INT offset = 0;
28559 /* For variables that have been optimized away and thus
28560 don't have a memory location, see if we can emit
28561 DW_AT_const_value instead. */
28562 if (tree_add_const_value_attribute (die, init))
28563 return;
28564 if (dwarf_strict && dwarf_version < 5)
28565 return;
28566 /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
28567 and ADDR_EXPR refers to a decl that has DW_AT_location or
28568 DW_AT_const_value (but isn't addressable, otherwise
28569 resolving the original DW_OP_addr wouldn't fail), see if
28570 we can add DW_OP_implicit_pointer. */
28571 STRIP_NOPS (init);
28572 if (TREE_CODE (init) == POINTER_PLUS_EXPR
28573 && tree_fits_shwi_p (TREE_OPERAND (init, 1)))
28575 offset = tree_to_shwi (TREE_OPERAND (init, 1));
28576 init = TREE_OPERAND (init, 0);
28577 STRIP_NOPS (init);
28579 if (TREE_CODE (init) != ADDR_EXPR)
28580 return;
28581 if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
28582 && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
28583 || (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL
28584 && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
28585 && TREE_OPERAND (init, 0) != decl))
28587 dw_die_ref ref;
28588 dw_loc_descr_ref l;
28590 if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
28592 rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
28593 if (!rtl)
28594 return;
28595 decl = SYMBOL_REF_DECL (rtl);
28597 else
28598 decl = TREE_OPERAND (init, 0);
28599 ref = lookup_decl_die (decl);
28600 if (ref == NULL
28601 || (!get_AT (ref, DW_AT_location)
28602 && !get_AT (ref, DW_AT_const_value)))
28603 return;
28604 l = new_loc_descr (dwarf_OP (DW_OP_implicit_pointer), 0, offset);
28605 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
28606 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
28607 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
28608 add_AT_loc (die, DW_AT_location, l);
28612 /* Return NULL if l is a DWARF expression, or first op that is not
28613 valid DWARF expression. */
28615 static dw_loc_descr_ref
28616 non_dwarf_expression (dw_loc_descr_ref l)
28618 while (l)
28620 if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
28621 return l;
28622 switch (l->dw_loc_opc)
28624 case DW_OP_regx:
28625 case DW_OP_implicit_value:
28626 case DW_OP_stack_value:
28627 case DW_OP_implicit_pointer:
28628 case DW_OP_GNU_implicit_pointer:
28629 case DW_OP_GNU_parameter_ref:
28630 case DW_OP_piece:
28631 case DW_OP_bit_piece:
28632 return l;
28633 default:
28634 break;
28636 l = l->dw_loc_next;
28638 return NULL;
28641 /* Return adjusted copy of EXPR:
28642 If it is empty DWARF expression, return it.
28643 If it is valid non-empty DWARF expression,
28644 return copy of EXPR with DW_OP_deref appended to it.
28645 If it is DWARF expression followed by DW_OP_reg{N,x}, return
28646 copy of the DWARF expression with DW_OP_breg{N,x} <0> appended.
28647 If it is DWARF expression followed by DW_OP_stack_value, return
28648 copy of the DWARF expression without anything appended.
28649 Otherwise, return NULL. */
28651 static dw_loc_descr_ref
28652 copy_deref_exprloc (dw_loc_descr_ref expr)
28654 dw_loc_descr_ref tail = NULL;
28656 if (expr == NULL)
28657 return NULL;
28659 dw_loc_descr_ref l = non_dwarf_expression (expr);
28660 if (l && l->dw_loc_next)
28661 return NULL;
28663 if (l)
28665 if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
28666 tail = new_loc_descr ((enum dwarf_location_atom)
28667 (DW_OP_breg0 + (l->dw_loc_opc - DW_OP_reg0)),
28668 0, 0);
28669 else
28670 switch (l->dw_loc_opc)
28672 case DW_OP_regx:
28673 tail = new_loc_descr (DW_OP_bregx,
28674 l->dw_loc_oprnd1.v.val_unsigned, 0);
28675 break;
28676 case DW_OP_stack_value:
28677 break;
28678 default:
28679 return NULL;
28682 else
28683 tail = new_loc_descr (DW_OP_deref, 0, 0);
28685 dw_loc_descr_ref ret = NULL, *p = &ret;
28686 while (expr != l)
28688 *p = new_loc_descr (expr->dw_loc_opc, 0, 0);
28689 (*p)->dw_loc_oprnd1 = expr->dw_loc_oprnd1;
28690 (*p)->dw_loc_oprnd2 = expr->dw_loc_oprnd2;
28691 p = &(*p)->dw_loc_next;
28692 expr = expr->dw_loc_next;
28694 *p = tail;
28695 return ret;
28698 /* For DW_AT_string_length attribute with DW_OP_GNU_variable_value
28699 reference to a variable or argument, adjust it if needed and return:
28700 -1 if the DW_AT_string_length attribute and DW_AT_{string_length_,}byte_size
28701 attribute if present should be removed
28702 0 keep the attribute perhaps with minor modifications, no need to rescan
28703 1 if the attribute has been successfully adjusted. */
28705 static int
28706 optimize_string_length (dw_attr_node *a)
28708 dw_loc_descr_ref l = AT_loc (a), lv;
28709 dw_die_ref die;
28710 if (l->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
28712 tree decl = l->dw_loc_oprnd1.v.val_decl_ref;
28713 die = lookup_decl_die (decl);
28714 if (die)
28716 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
28717 l->dw_loc_oprnd1.v.val_die_ref.die = die;
28718 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
28720 else
28721 return -1;
28723 else
28724 die = l->dw_loc_oprnd1.v.val_die_ref.die;
28726 /* DWARF5 allows reference class, so we can then reference the DIE.
28727 Only do this for DW_OP_GNU_variable_value DW_OP_stack_value. */
28728 if (l->dw_loc_next != NULL && dwarf_version >= 5)
28730 a->dw_attr_val.val_class = dw_val_class_die_ref;
28731 a->dw_attr_val.val_entry = NULL;
28732 a->dw_attr_val.v.val_die_ref.die = die;
28733 a->dw_attr_val.v.val_die_ref.external = 0;
28734 return 0;
28737 dw_attr_node *av = get_AT (die, DW_AT_location);
28738 dw_loc_list_ref d;
28739 bool non_dwarf_expr = false;
28741 if (av == NULL)
28742 return dwarf_strict ? -1 : 0;
28743 switch (AT_class (av))
28745 case dw_val_class_loc_list:
28746 for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
28747 if (d->expr && non_dwarf_expression (d->expr))
28748 non_dwarf_expr = true;
28749 break;
28750 case dw_val_class_loc:
28751 lv = AT_loc (av);
28752 if (lv == NULL)
28753 return dwarf_strict ? -1 : 0;
28754 if (non_dwarf_expression (lv))
28755 non_dwarf_expr = true;
28756 break;
28757 default:
28758 return dwarf_strict ? -1 : 0;
28761 /* If it is safe to transform DW_OP_GNU_variable_value DW_OP_stack_value
28762 into DW_OP_call4 or DW_OP_GNU_variable_value into
28763 DW_OP_call4 DW_OP_deref, do so. */
28764 if (!non_dwarf_expr
28765 && (l->dw_loc_next != NULL || AT_class (av) == dw_val_class_loc))
28767 l->dw_loc_opc = DW_OP_call4;
28768 if (l->dw_loc_next)
28769 l->dw_loc_next = NULL;
28770 else
28771 l->dw_loc_next = new_loc_descr (DW_OP_deref, 0, 0);
28772 return 0;
28775 /* For DW_OP_GNU_variable_value DW_OP_stack_value, we can just
28776 copy over the DW_AT_location attribute from die to a. */
28777 if (l->dw_loc_next != NULL)
28779 a->dw_attr_val = av->dw_attr_val;
28780 return 1;
28783 dw_loc_list_ref list, *p;
28784 switch (AT_class (av))
28786 case dw_val_class_loc_list:
28787 p = &list;
28788 list = NULL;
28789 for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
28791 lv = copy_deref_exprloc (d->expr);
28792 if (lv)
28794 *p = new_loc_list (lv, d->begin, d->end, d->section);
28795 p = &(*p)->dw_loc_next;
28797 else if (!dwarf_strict && d->expr)
28798 return 0;
28800 if (list == NULL)
28801 return dwarf_strict ? -1 : 0;
28802 a->dw_attr_val.val_class = dw_val_class_loc_list;
28803 gen_llsym (list);
28804 *AT_loc_list_ptr (a) = list;
28805 return 1;
28806 case dw_val_class_loc:
28807 lv = copy_deref_exprloc (AT_loc (av));
28808 if (lv == NULL)
28809 return dwarf_strict ? -1 : 0;
28810 a->dw_attr_val.v.val_loc = lv;
28811 return 1;
28812 default:
28813 gcc_unreachable ();
28817 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
28818 an address in .rodata section if the string literal is emitted there,
28819 or remove the containing location list or replace DW_AT_const_value
28820 with DW_AT_location and empty location expression, if it isn't found
28821 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
28822 to something that has been emitted in the current CU. */
28824 static void
28825 resolve_addr (dw_die_ref die)
28827 dw_die_ref c;
28828 dw_attr_node *a;
28829 dw_loc_list_ref *curr, *start, loc;
28830 unsigned ix;
28831 bool remove_AT_byte_size = false;
28833 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
28834 switch (AT_class (a))
28836 case dw_val_class_loc_list:
28837 start = curr = AT_loc_list_ptr (a);
28838 loc = *curr;
28839 gcc_assert (loc);
28840 /* The same list can be referenced more than once. See if we have
28841 already recorded the result from a previous pass. */
28842 if (loc->replaced)
28843 *curr = loc->dw_loc_next;
28844 else if (!loc->resolved_addr)
28846 /* As things stand, we do not expect or allow one die to
28847 reference a suffix of another die's location list chain.
28848 References must be identical or completely separate.
28849 There is therefore no need to cache the result of this
28850 pass on any list other than the first; doing so
28851 would lead to unnecessary writes. */
28852 while (*curr)
28854 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
28855 if (!resolve_addr_in_expr (a, (*curr)->expr))
28857 dw_loc_list_ref next = (*curr)->dw_loc_next;
28858 dw_loc_descr_ref l = (*curr)->expr;
28860 if (next && (*curr)->ll_symbol)
28862 gcc_assert (!next->ll_symbol);
28863 next->ll_symbol = (*curr)->ll_symbol;
28865 if (dwarf_split_debug_info)
28866 remove_loc_list_addr_table_entries (l);
28867 *curr = next;
28869 else
28871 mark_base_types ((*curr)->expr);
28872 curr = &(*curr)->dw_loc_next;
28875 if (loc == *start)
28876 loc->resolved_addr = 1;
28877 else
28879 loc->replaced = 1;
28880 loc->dw_loc_next = *start;
28883 if (!*start)
28885 remove_AT (die, a->dw_attr);
28886 ix--;
28888 break;
28889 case dw_val_class_loc:
28891 dw_loc_descr_ref l = AT_loc (a);
28892 /* DW_OP_GNU_variable_value DW_OP_stack_value or
28893 DW_OP_GNU_variable_value in DW_AT_string_length can be converted
28894 into DW_OP_call4 or DW_OP_call4 DW_OP_deref, which is standard
28895 DWARF4 unlike DW_OP_GNU_variable_value. Or for DWARF5
28896 DW_OP_GNU_variable_value DW_OP_stack_value can be replaced
28897 with DW_FORM_ref referencing the same DIE as
28898 DW_OP_GNU_variable_value used to reference. */
28899 if (a->dw_attr == DW_AT_string_length
28900 && l
28901 && l->dw_loc_opc == DW_OP_GNU_variable_value
28902 && (l->dw_loc_next == NULL
28903 || (l->dw_loc_next->dw_loc_next == NULL
28904 && l->dw_loc_next->dw_loc_opc == DW_OP_stack_value)))
28906 switch (optimize_string_length (a))
28908 case -1:
28909 remove_AT (die, a->dw_attr);
28910 ix--;
28911 /* If we drop DW_AT_string_length, we need to drop also
28912 DW_AT_{string_length_,}byte_size. */
28913 remove_AT_byte_size = true;
28914 continue;
28915 default:
28916 break;
28917 case 1:
28918 /* Even if we keep the optimized DW_AT_string_length,
28919 it might have changed AT_class, so process it again. */
28920 ix--;
28921 continue;
28924 /* For -gdwarf-2 don't attempt to optimize
28925 DW_AT_data_member_location containing
28926 DW_OP_plus_uconst - older consumers might
28927 rely on it being that op instead of a more complex,
28928 but shorter, location description. */
28929 if ((dwarf_version > 2
28930 || a->dw_attr != DW_AT_data_member_location
28931 || l == NULL
28932 || l->dw_loc_opc != DW_OP_plus_uconst
28933 || l->dw_loc_next != NULL)
28934 && !resolve_addr_in_expr (a, l))
28936 if (dwarf_split_debug_info)
28937 remove_loc_list_addr_table_entries (l);
28938 if (l != NULL
28939 && l->dw_loc_next == NULL
28940 && l->dw_loc_opc == DW_OP_addr
28941 && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
28942 && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
28943 && a->dw_attr == DW_AT_location)
28945 tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
28946 remove_AT (die, a->dw_attr);
28947 ix--;
28948 optimize_location_into_implicit_ptr (die, decl);
28949 break;
28951 if (a->dw_attr == DW_AT_string_length)
28952 /* If we drop DW_AT_string_length, we need to drop also
28953 DW_AT_{string_length_,}byte_size. */
28954 remove_AT_byte_size = true;
28955 remove_AT (die, a->dw_attr);
28956 ix--;
28958 else
28959 mark_base_types (l);
28961 break;
28962 case dw_val_class_addr:
28963 if (a->dw_attr == DW_AT_const_value
28964 && !resolve_one_addr (&a->dw_attr_val.v.val_addr))
28966 if (AT_index (a) != NOT_INDEXED)
28967 remove_addr_table_entry (a->dw_attr_val.val_entry);
28968 remove_AT (die, a->dw_attr);
28969 ix--;
28971 if ((die->die_tag == DW_TAG_call_site
28972 && a->dw_attr == DW_AT_call_origin)
28973 || (die->die_tag == DW_TAG_GNU_call_site
28974 && a->dw_attr == DW_AT_abstract_origin))
28976 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
28977 dw_die_ref tdie = lookup_decl_die (tdecl);
28978 dw_die_ref cdie;
28979 if (tdie == NULL
28980 && DECL_EXTERNAL (tdecl)
28981 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE
28982 && (cdie = lookup_context_die (DECL_CONTEXT (tdecl))))
28984 dw_die_ref pdie = cdie;
28985 /* Make sure we don't add these DIEs into type units.
28986 We could emit skeleton DIEs for context (namespaces,
28987 outer structs/classes) and a skeleton DIE for the
28988 innermost context with DW_AT_signature pointing to the
28989 type unit. See PR78835. */
28990 while (pdie && pdie->die_tag != DW_TAG_type_unit)
28991 pdie = pdie->die_parent;
28992 if (pdie == NULL)
28994 /* Creating a full DIE for tdecl is overly expensive and
28995 at this point even wrong when in the LTO phase
28996 as it can end up generating new type DIEs we didn't
28997 output and thus optimize_external_refs will crash. */
28998 tdie = new_die (DW_TAG_subprogram, cdie, NULL_TREE);
28999 add_AT_flag (tdie, DW_AT_external, 1);
29000 add_AT_flag (tdie, DW_AT_declaration, 1);
29001 add_linkage_attr (tdie, tdecl);
29002 add_name_and_src_coords_attributes (tdie, tdecl);
29003 equate_decl_number_to_die (tdecl, tdie);
29006 if (tdie)
29008 a->dw_attr_val.val_class = dw_val_class_die_ref;
29009 a->dw_attr_val.v.val_die_ref.die = tdie;
29010 a->dw_attr_val.v.val_die_ref.external = 0;
29012 else
29014 if (AT_index (a) != NOT_INDEXED)
29015 remove_addr_table_entry (a->dw_attr_val.val_entry);
29016 remove_AT (die, a->dw_attr);
29017 ix--;
29020 break;
29021 default:
29022 break;
29025 if (remove_AT_byte_size)
29026 remove_AT (die, dwarf_version >= 5
29027 ? DW_AT_string_length_byte_size
29028 : DW_AT_byte_size);
29030 FOR_EACH_CHILD (die, c, resolve_addr (c));
29033 /* Helper routines for optimize_location_lists.
29034 This pass tries to share identical local lists in .debug_loc
29035 section. */
29037 /* Iteratively hash operands of LOC opcode into HSTATE. */
29039 static void
29040 hash_loc_operands (dw_loc_descr_ref loc, inchash::hash &hstate)
29042 dw_val_ref val1 = &loc->dw_loc_oprnd1;
29043 dw_val_ref val2 = &loc->dw_loc_oprnd2;
29045 switch (loc->dw_loc_opc)
29047 case DW_OP_const4u:
29048 case DW_OP_const8u:
29049 if (loc->dtprel)
29050 goto hash_addr;
29051 /* FALLTHRU */
29052 case DW_OP_const1u:
29053 case DW_OP_const1s:
29054 case DW_OP_const2u:
29055 case DW_OP_const2s:
29056 case DW_OP_const4s:
29057 case DW_OP_const8s:
29058 case DW_OP_constu:
29059 case DW_OP_consts:
29060 case DW_OP_pick:
29061 case DW_OP_plus_uconst:
29062 case DW_OP_breg0:
29063 case DW_OP_breg1:
29064 case DW_OP_breg2:
29065 case DW_OP_breg3:
29066 case DW_OP_breg4:
29067 case DW_OP_breg5:
29068 case DW_OP_breg6:
29069 case DW_OP_breg7:
29070 case DW_OP_breg8:
29071 case DW_OP_breg9:
29072 case DW_OP_breg10:
29073 case DW_OP_breg11:
29074 case DW_OP_breg12:
29075 case DW_OP_breg13:
29076 case DW_OP_breg14:
29077 case DW_OP_breg15:
29078 case DW_OP_breg16:
29079 case DW_OP_breg17:
29080 case DW_OP_breg18:
29081 case DW_OP_breg19:
29082 case DW_OP_breg20:
29083 case DW_OP_breg21:
29084 case DW_OP_breg22:
29085 case DW_OP_breg23:
29086 case DW_OP_breg24:
29087 case DW_OP_breg25:
29088 case DW_OP_breg26:
29089 case DW_OP_breg27:
29090 case DW_OP_breg28:
29091 case DW_OP_breg29:
29092 case DW_OP_breg30:
29093 case DW_OP_breg31:
29094 case DW_OP_regx:
29095 case DW_OP_fbreg:
29096 case DW_OP_piece:
29097 case DW_OP_deref_size:
29098 case DW_OP_xderef_size:
29099 hstate.add_object (val1->v.val_int);
29100 break;
29101 case DW_OP_skip:
29102 case DW_OP_bra:
29104 int offset;
29106 gcc_assert (val1->val_class == dw_val_class_loc);
29107 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
29108 hstate.add_object (offset);
29110 break;
29111 case DW_OP_implicit_value:
29112 hstate.add_object (val1->v.val_unsigned);
29113 switch (val2->val_class)
29115 case dw_val_class_const:
29116 hstate.add_object (val2->v.val_int);
29117 break;
29118 case dw_val_class_vec:
29120 unsigned int elt_size = val2->v.val_vec.elt_size;
29121 unsigned int len = val2->v.val_vec.length;
29123 hstate.add_int (elt_size);
29124 hstate.add_int (len);
29125 hstate.add (val2->v.val_vec.array, len * elt_size);
29127 break;
29128 case dw_val_class_const_double:
29129 hstate.add_object (val2->v.val_double.low);
29130 hstate.add_object (val2->v.val_double.high);
29131 break;
29132 case dw_val_class_wide_int:
29133 hstate.add (val2->v.val_wide->get_val (),
29134 get_full_len (*val2->v.val_wide)
29135 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
29136 break;
29137 case dw_val_class_addr:
29138 inchash::add_rtx (val2->v.val_addr, hstate);
29139 break;
29140 default:
29141 gcc_unreachable ();
29143 break;
29144 case DW_OP_bregx:
29145 case DW_OP_bit_piece:
29146 hstate.add_object (val1->v.val_int);
29147 hstate.add_object (val2->v.val_int);
29148 break;
29149 case DW_OP_addr:
29150 hash_addr:
29151 if (loc->dtprel)
29153 unsigned char dtprel = 0xd1;
29154 hstate.add_object (dtprel);
29156 inchash::add_rtx (val1->v.val_addr, hstate);
29157 break;
29158 case DW_OP_GNU_addr_index:
29159 case DW_OP_GNU_const_index:
29161 if (loc->dtprel)
29163 unsigned char dtprel = 0xd1;
29164 hstate.add_object (dtprel);
29166 inchash::add_rtx (val1->val_entry->addr.rtl, hstate);
29168 break;
29169 case DW_OP_implicit_pointer:
29170 case DW_OP_GNU_implicit_pointer:
29171 hstate.add_int (val2->v.val_int);
29172 break;
29173 case DW_OP_entry_value:
29174 case DW_OP_GNU_entry_value:
29175 hstate.add_object (val1->v.val_loc);
29176 break;
29177 case DW_OP_regval_type:
29178 case DW_OP_deref_type:
29179 case DW_OP_GNU_regval_type:
29180 case DW_OP_GNU_deref_type:
29182 unsigned int byte_size
29183 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
29184 unsigned int encoding
29185 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
29186 hstate.add_object (val1->v.val_int);
29187 hstate.add_object (byte_size);
29188 hstate.add_object (encoding);
29190 break;
29191 case DW_OP_convert:
29192 case DW_OP_reinterpret:
29193 case DW_OP_GNU_convert:
29194 case DW_OP_GNU_reinterpret:
29195 if (val1->val_class == dw_val_class_unsigned_const)
29197 hstate.add_object (val1->v.val_unsigned);
29198 break;
29200 /* FALLTHRU */
29201 case DW_OP_const_type:
29202 case DW_OP_GNU_const_type:
29204 unsigned int byte_size
29205 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
29206 unsigned int encoding
29207 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
29208 hstate.add_object (byte_size);
29209 hstate.add_object (encoding);
29210 if (loc->dw_loc_opc != DW_OP_const_type
29211 && loc->dw_loc_opc != DW_OP_GNU_const_type)
29212 break;
29213 hstate.add_object (val2->val_class);
29214 switch (val2->val_class)
29216 case dw_val_class_const:
29217 hstate.add_object (val2->v.val_int);
29218 break;
29219 case dw_val_class_vec:
29221 unsigned int elt_size = val2->v.val_vec.elt_size;
29222 unsigned int len = val2->v.val_vec.length;
29224 hstate.add_object (elt_size);
29225 hstate.add_object (len);
29226 hstate.add (val2->v.val_vec.array, len * elt_size);
29228 break;
29229 case dw_val_class_const_double:
29230 hstate.add_object (val2->v.val_double.low);
29231 hstate.add_object (val2->v.val_double.high);
29232 break;
29233 case dw_val_class_wide_int:
29234 hstate.add (val2->v.val_wide->get_val (),
29235 get_full_len (*val2->v.val_wide)
29236 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
29237 break;
29238 default:
29239 gcc_unreachable ();
29242 break;
29244 default:
29245 /* Other codes have no operands. */
29246 break;
29250 /* Iteratively hash the whole DWARF location expression LOC into HSTATE. */
29252 static inline void
29253 hash_locs (dw_loc_descr_ref loc, inchash::hash &hstate)
29255 dw_loc_descr_ref l;
29256 bool sizes_computed = false;
29257 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
29258 size_of_locs (loc);
29260 for (l = loc; l != NULL; l = l->dw_loc_next)
29262 enum dwarf_location_atom opc = l->dw_loc_opc;
29263 hstate.add_object (opc);
29264 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
29266 size_of_locs (loc);
29267 sizes_computed = true;
29269 hash_loc_operands (l, hstate);
29273 /* Compute hash of the whole location list LIST_HEAD. */
29275 static inline void
29276 hash_loc_list (dw_loc_list_ref list_head)
29278 dw_loc_list_ref curr = list_head;
29279 inchash::hash hstate;
29281 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
29283 hstate.add (curr->begin, strlen (curr->begin) + 1);
29284 hstate.add (curr->end, strlen (curr->end) + 1);
29285 if (curr->section)
29286 hstate.add (curr->section, strlen (curr->section) + 1);
29287 hash_locs (curr->expr, hstate);
29289 list_head->hash = hstate.end ();
29292 /* Return true if X and Y opcodes have the same operands. */
29294 static inline bool
29295 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
29297 dw_val_ref valx1 = &x->dw_loc_oprnd1;
29298 dw_val_ref valx2 = &x->dw_loc_oprnd2;
29299 dw_val_ref valy1 = &y->dw_loc_oprnd1;
29300 dw_val_ref valy2 = &y->dw_loc_oprnd2;
29302 switch (x->dw_loc_opc)
29304 case DW_OP_const4u:
29305 case DW_OP_const8u:
29306 if (x->dtprel)
29307 goto hash_addr;
29308 /* FALLTHRU */
29309 case DW_OP_const1u:
29310 case DW_OP_const1s:
29311 case DW_OP_const2u:
29312 case DW_OP_const2s:
29313 case DW_OP_const4s:
29314 case DW_OP_const8s:
29315 case DW_OP_constu:
29316 case DW_OP_consts:
29317 case DW_OP_pick:
29318 case DW_OP_plus_uconst:
29319 case DW_OP_breg0:
29320 case DW_OP_breg1:
29321 case DW_OP_breg2:
29322 case DW_OP_breg3:
29323 case DW_OP_breg4:
29324 case DW_OP_breg5:
29325 case DW_OP_breg6:
29326 case DW_OP_breg7:
29327 case DW_OP_breg8:
29328 case DW_OP_breg9:
29329 case DW_OP_breg10:
29330 case DW_OP_breg11:
29331 case DW_OP_breg12:
29332 case DW_OP_breg13:
29333 case DW_OP_breg14:
29334 case DW_OP_breg15:
29335 case DW_OP_breg16:
29336 case DW_OP_breg17:
29337 case DW_OP_breg18:
29338 case DW_OP_breg19:
29339 case DW_OP_breg20:
29340 case DW_OP_breg21:
29341 case DW_OP_breg22:
29342 case DW_OP_breg23:
29343 case DW_OP_breg24:
29344 case DW_OP_breg25:
29345 case DW_OP_breg26:
29346 case DW_OP_breg27:
29347 case DW_OP_breg28:
29348 case DW_OP_breg29:
29349 case DW_OP_breg30:
29350 case DW_OP_breg31:
29351 case DW_OP_regx:
29352 case DW_OP_fbreg:
29353 case DW_OP_piece:
29354 case DW_OP_deref_size:
29355 case DW_OP_xderef_size:
29356 return valx1->v.val_int == valy1->v.val_int;
29357 case DW_OP_skip:
29358 case DW_OP_bra:
29359 /* If splitting debug info, the use of DW_OP_GNU_addr_index
29360 can cause irrelevant differences in dw_loc_addr. */
29361 gcc_assert (valx1->val_class == dw_val_class_loc
29362 && valy1->val_class == dw_val_class_loc
29363 && (dwarf_split_debug_info
29364 || x->dw_loc_addr == y->dw_loc_addr));
29365 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
29366 case DW_OP_implicit_value:
29367 if (valx1->v.val_unsigned != valy1->v.val_unsigned
29368 || valx2->val_class != valy2->val_class)
29369 return false;
29370 switch (valx2->val_class)
29372 case dw_val_class_const:
29373 return valx2->v.val_int == valy2->v.val_int;
29374 case dw_val_class_vec:
29375 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
29376 && valx2->v.val_vec.length == valy2->v.val_vec.length
29377 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
29378 valx2->v.val_vec.elt_size
29379 * valx2->v.val_vec.length) == 0;
29380 case dw_val_class_const_double:
29381 return valx2->v.val_double.low == valy2->v.val_double.low
29382 && valx2->v.val_double.high == valy2->v.val_double.high;
29383 case dw_val_class_wide_int:
29384 return *valx2->v.val_wide == *valy2->v.val_wide;
29385 case dw_val_class_addr:
29386 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
29387 default:
29388 gcc_unreachable ();
29390 case DW_OP_bregx:
29391 case DW_OP_bit_piece:
29392 return valx1->v.val_int == valy1->v.val_int
29393 && valx2->v.val_int == valy2->v.val_int;
29394 case DW_OP_addr:
29395 hash_addr:
29396 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
29397 case DW_OP_GNU_addr_index:
29398 case DW_OP_GNU_const_index:
29400 rtx ax1 = valx1->val_entry->addr.rtl;
29401 rtx ay1 = valy1->val_entry->addr.rtl;
29402 return rtx_equal_p (ax1, ay1);
29404 case DW_OP_implicit_pointer:
29405 case DW_OP_GNU_implicit_pointer:
29406 return valx1->val_class == dw_val_class_die_ref
29407 && valx1->val_class == valy1->val_class
29408 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
29409 && valx2->v.val_int == valy2->v.val_int;
29410 case DW_OP_entry_value:
29411 case DW_OP_GNU_entry_value:
29412 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
29413 case DW_OP_const_type:
29414 case DW_OP_GNU_const_type:
29415 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
29416 || valx2->val_class != valy2->val_class)
29417 return false;
29418 switch (valx2->val_class)
29420 case dw_val_class_const:
29421 return valx2->v.val_int == valy2->v.val_int;
29422 case dw_val_class_vec:
29423 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
29424 && valx2->v.val_vec.length == valy2->v.val_vec.length
29425 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
29426 valx2->v.val_vec.elt_size
29427 * valx2->v.val_vec.length) == 0;
29428 case dw_val_class_const_double:
29429 return valx2->v.val_double.low == valy2->v.val_double.low
29430 && valx2->v.val_double.high == valy2->v.val_double.high;
29431 case dw_val_class_wide_int:
29432 return *valx2->v.val_wide == *valy2->v.val_wide;
29433 default:
29434 gcc_unreachable ();
29436 case DW_OP_regval_type:
29437 case DW_OP_deref_type:
29438 case DW_OP_GNU_regval_type:
29439 case DW_OP_GNU_deref_type:
29440 return valx1->v.val_int == valy1->v.val_int
29441 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
29442 case DW_OP_convert:
29443 case DW_OP_reinterpret:
29444 case DW_OP_GNU_convert:
29445 case DW_OP_GNU_reinterpret:
29446 if (valx1->val_class != valy1->val_class)
29447 return false;
29448 if (valx1->val_class == dw_val_class_unsigned_const)
29449 return valx1->v.val_unsigned == valy1->v.val_unsigned;
29450 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
29451 case DW_OP_GNU_parameter_ref:
29452 return valx1->val_class == dw_val_class_die_ref
29453 && valx1->val_class == valy1->val_class
29454 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
29455 default:
29456 /* Other codes have no operands. */
29457 return true;
29461 /* Return true if DWARF location expressions X and Y are the same. */
29463 static inline bool
29464 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
29466 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
29467 if (x->dw_loc_opc != y->dw_loc_opc
29468 || x->dtprel != y->dtprel
29469 || !compare_loc_operands (x, y))
29470 break;
29471 return x == NULL && y == NULL;
29474 /* Hashtable helpers. */
29476 struct loc_list_hasher : nofree_ptr_hash <dw_loc_list_struct>
29478 static inline hashval_t hash (const dw_loc_list_struct *);
29479 static inline bool equal (const dw_loc_list_struct *,
29480 const dw_loc_list_struct *);
29483 /* Return precomputed hash of location list X. */
29485 inline hashval_t
29486 loc_list_hasher::hash (const dw_loc_list_struct *x)
29488 return x->hash;
29491 /* Return true if location lists A and B are the same. */
29493 inline bool
29494 loc_list_hasher::equal (const dw_loc_list_struct *a,
29495 const dw_loc_list_struct *b)
29497 if (a == b)
29498 return 1;
29499 if (a->hash != b->hash)
29500 return 0;
29501 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
29502 if (strcmp (a->begin, b->begin) != 0
29503 || strcmp (a->end, b->end) != 0
29504 || (a->section == NULL) != (b->section == NULL)
29505 || (a->section && strcmp (a->section, b->section) != 0)
29506 || !compare_locs (a->expr, b->expr))
29507 break;
29508 return a == NULL && b == NULL;
29511 typedef hash_table<loc_list_hasher> loc_list_hash_type;
29514 /* Recursively optimize location lists referenced from DIE
29515 children and share them whenever possible. */
29517 static void
29518 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type *htab)
29520 dw_die_ref c;
29521 dw_attr_node *a;
29522 unsigned ix;
29523 dw_loc_list_struct **slot;
29525 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
29526 if (AT_class (a) == dw_val_class_loc_list)
29528 dw_loc_list_ref list = AT_loc_list (a);
29529 /* TODO: perform some optimizations here, before hashing
29530 it and storing into the hash table. */
29531 hash_loc_list (list);
29532 slot = htab->find_slot_with_hash (list, list->hash, INSERT);
29533 if (*slot == NULL)
29534 *slot = list;
29535 else
29536 a->dw_attr_val.v.val_loc_list = *slot;
29539 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
29543 /* Recursively assign each location list a unique index into the debug_addr
29544 section. */
29546 static void
29547 index_location_lists (dw_die_ref die)
29549 dw_die_ref c;
29550 dw_attr_node *a;
29551 unsigned ix;
29553 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
29554 if (AT_class (a) == dw_val_class_loc_list)
29556 dw_loc_list_ref list = AT_loc_list (a);
29557 dw_loc_list_ref curr;
29558 for (curr = list; curr != NULL; curr = curr->dw_loc_next)
29560 /* Don't index an entry that has already been indexed
29561 or won't be output. */
29562 if (curr->begin_entry != NULL
29563 || (strcmp (curr->begin, curr->end) == 0 && !curr->force))
29564 continue;
29566 curr->begin_entry
29567 = add_addr_table_entry (xstrdup (curr->begin), ate_kind_label);
29571 FOR_EACH_CHILD (die, c, index_location_lists (c));
29574 /* Optimize location lists referenced from DIE
29575 children and share them whenever possible. */
29577 static void
29578 optimize_location_lists (dw_die_ref die)
29580 loc_list_hash_type htab (500);
29581 optimize_location_lists_1 (die, &htab);
29584 /* Traverse the limbo die list, and add parent/child links. The only
29585 dies without parents that should be here are concrete instances of
29586 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
29587 For concrete instances, we can get the parent die from the abstract
29588 instance. */
29590 static void
29591 flush_limbo_die_list (void)
29593 limbo_die_node *node;
29595 /* get_context_die calls force_decl_die, which can put new DIEs on the
29596 limbo list in LTO mode when nested functions are put in a different
29597 partition than that of their parent function. */
29598 while ((node = limbo_die_list))
29600 dw_die_ref die = node->die;
29601 limbo_die_list = node->next;
29603 if (die->die_parent == NULL)
29605 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
29607 if (origin && origin->die_parent)
29608 add_child_die (origin->die_parent, die);
29609 else if (is_cu_die (die))
29611 else if (seen_error ())
29612 /* It's OK to be confused by errors in the input. */
29613 add_child_die (comp_unit_die (), die);
29614 else
29616 /* In certain situations, the lexical block containing a
29617 nested function can be optimized away, which results
29618 in the nested function die being orphaned. Likewise
29619 with the return type of that nested function. Force
29620 this to be a child of the containing function.
29622 It may happen that even the containing function got fully
29623 inlined and optimized out. In that case we are lost and
29624 assign the empty child. This should not be big issue as
29625 the function is likely unreachable too. */
29626 gcc_assert (node->created_for);
29628 if (DECL_P (node->created_for))
29629 origin = get_context_die (DECL_CONTEXT (node->created_for));
29630 else if (TYPE_P (node->created_for))
29631 origin = scope_die_for (node->created_for, comp_unit_die ());
29632 else
29633 origin = comp_unit_die ();
29635 add_child_die (origin, die);
29641 /* Output stuff that dwarf requires at the end of every file,
29642 and generate the DWARF-2 debugging info. */
29644 static void
29645 dwarf2out_finish (const char *)
29647 comdat_type_node *ctnode;
29648 dw_die_ref main_comp_unit_die;
29649 unsigned char checksum[16];
29651 /* Flush out any latecomers to the limbo party. */
29652 flush_limbo_die_list ();
29654 if (flag_checking)
29656 verify_die (comp_unit_die ());
29657 for (limbo_die_node *node = cu_die_list; node; node = node->next)
29658 verify_die (node->die);
29661 /* We shouldn't have any symbols with delayed asm names for
29662 DIEs generated after early finish. */
29663 gcc_assert (deferred_asm_name == NULL);
29665 gen_remaining_tmpl_value_param_die_attribute ();
29667 #if ENABLE_ASSERT_CHECKING
29669 dw_die_ref die = comp_unit_die (), c;
29670 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
29672 #endif
29673 resolve_addr (comp_unit_die ());
29674 move_marked_base_types ();
29676 /* Initialize sections and labels used for actual assembler output. */
29677 init_sections_and_labels ();
29679 /* Traverse the DIE's and add sibling attributes to those DIE's that
29680 have children. */
29681 add_sibling_attributes (comp_unit_die ());
29682 limbo_die_node *node;
29683 for (node = cu_die_list; node; node = node->next)
29684 add_sibling_attributes (node->die);
29685 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
29686 add_sibling_attributes (ctnode->root_die);
29688 /* When splitting DWARF info, we put some attributes in the
29689 skeleton compile_unit DIE that remains in the .o, while
29690 most attributes go in the DWO compile_unit_die. */
29691 if (dwarf_split_debug_info)
29693 limbo_die_node *cu;
29694 main_comp_unit_die = gen_compile_unit_die (NULL);
29695 if (dwarf_version >= 5)
29696 main_comp_unit_die->die_tag = DW_TAG_skeleton_unit;
29697 cu = limbo_die_list;
29698 gcc_assert (cu->die == main_comp_unit_die);
29699 limbo_die_list = limbo_die_list->next;
29700 cu->next = cu_die_list;
29701 cu_die_list = cu;
29703 else
29704 main_comp_unit_die = comp_unit_die ();
29706 /* Output a terminator label for the .text section. */
29707 switch_to_section (text_section);
29708 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
29709 if (cold_text_section)
29711 switch_to_section (cold_text_section);
29712 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
29715 /* We can only use the low/high_pc attributes if all of the code was
29716 in .text. */
29717 if (!have_multiple_function_sections
29718 || (dwarf_version < 3 && dwarf_strict))
29720 /* Don't add if the CU has no associated code. */
29721 if (text_section_used)
29722 add_AT_low_high_pc (main_comp_unit_die, text_section_label,
29723 text_end_label, true);
29725 else
29727 unsigned fde_idx;
29728 dw_fde_ref fde;
29729 bool range_list_added = false;
29731 if (text_section_used)
29732 add_ranges_by_labels (main_comp_unit_die, text_section_label,
29733 text_end_label, &range_list_added, true);
29734 if (cold_text_section_used)
29735 add_ranges_by_labels (main_comp_unit_die, cold_text_section_label,
29736 cold_end_label, &range_list_added, true);
29738 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
29740 if (DECL_IGNORED_P (fde->decl))
29741 continue;
29742 if (!fde->in_std_section)
29743 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
29744 fde->dw_fde_end, &range_list_added,
29745 true);
29746 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
29747 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
29748 fde->dw_fde_second_end, &range_list_added,
29749 true);
29752 if (range_list_added)
29754 /* We need to give .debug_loc and .debug_ranges an appropriate
29755 "base address". Use zero so that these addresses become
29756 absolute. Historically, we've emitted the unexpected
29757 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
29758 Emit both to give time for other tools to adapt. */
29759 add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
29760 if (! dwarf_strict && dwarf_version < 4)
29761 add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
29763 add_ranges (NULL);
29767 if (debug_info_level >= DINFO_LEVEL_TERSE)
29768 add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
29769 debug_line_section_label);
29771 if (have_macinfo)
29772 add_AT_macptr (comp_unit_die (),
29773 dwarf_version >= 5 ? DW_AT_macros
29774 : dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros,
29775 macinfo_section_label);
29777 if (dwarf_split_debug_info)
29779 if (have_location_lists)
29781 if (dwarf_version >= 5)
29782 add_AT_loclistsptr (comp_unit_die (), DW_AT_loclists_base,
29783 loc_section_label);
29784 /* optimize_location_lists calculates the size of the lists,
29785 so index them first, and assign indices to the entries.
29786 Although optimize_location_lists will remove entries from
29787 the table, it only does so for duplicates, and therefore
29788 only reduces ref_counts to 1. */
29789 index_location_lists (comp_unit_die ());
29792 if (addr_index_table != NULL)
29794 unsigned int index = 0;
29795 addr_index_table
29796 ->traverse_noresize<unsigned int *, index_addr_table_entry>
29797 (&index);
29801 loc_list_idx = 0;
29802 if (have_location_lists)
29804 optimize_location_lists (comp_unit_die ());
29805 /* And finally assign indexes to the entries for -gsplit-dwarf. */
29806 if (dwarf_version >= 5 && dwarf_split_debug_info)
29807 assign_location_list_indexes (comp_unit_die ());
29810 save_macinfo_strings ();
29812 if (dwarf_split_debug_info)
29814 unsigned int index = 0;
29816 /* Add attributes common to skeleton compile_units and
29817 type_units. Because these attributes include strings, it
29818 must be done before freezing the string table. Top-level
29819 skeleton die attrs are added when the skeleton type unit is
29820 created, so ensure it is created by this point. */
29821 add_top_level_skeleton_die_attrs (main_comp_unit_die);
29822 debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
29825 /* Output all of the compilation units. We put the main one last so that
29826 the offsets are available to output_pubnames. */
29827 for (node = cu_die_list; node; node = node->next)
29828 output_comp_unit (node->die, 0, NULL);
29830 hash_table<comdat_type_hasher> comdat_type_table (100);
29831 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
29833 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
29835 /* Don't output duplicate types. */
29836 if (*slot != HTAB_EMPTY_ENTRY)
29837 continue;
29839 /* Add a pointer to the line table for the main compilation unit
29840 so that the debugger can make sense of DW_AT_decl_file
29841 attributes. */
29842 if (debug_info_level >= DINFO_LEVEL_TERSE)
29843 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
29844 (!dwarf_split_debug_info
29845 ? debug_line_section_label
29846 : debug_skeleton_line_section_label));
29848 output_comdat_type_unit (ctnode);
29849 *slot = ctnode;
29852 /* The AT_pubnames attribute needs to go in all skeleton dies, including
29853 both the main_cu and all skeleton TUs. Making this call unconditional
29854 would end up either adding a second copy of the AT_pubnames attribute, or
29855 requiring a special case in add_top_level_skeleton_die_attrs. */
29856 if (!dwarf_split_debug_info)
29857 add_AT_pubnames (comp_unit_die ());
29859 if (dwarf_split_debug_info)
29861 int mark;
29862 struct md5_ctx ctx;
29864 if (dwarf_version >= 5 && !vec_safe_is_empty (ranges_table))
29865 index_rnglists ();
29867 /* Compute a checksum of the comp_unit to use as the dwo_id. */
29868 md5_init_ctx (&ctx);
29869 mark = 0;
29870 die_checksum (comp_unit_die (), &ctx, &mark);
29871 unmark_all_dies (comp_unit_die ());
29872 md5_finish_ctx (&ctx, checksum);
29874 if (dwarf_version < 5)
29876 /* Use the first 8 bytes of the checksum as the dwo_id,
29877 and add it to both comp-unit DIEs. */
29878 add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
29879 add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
29882 /* Add the base offset of the ranges table to the skeleton
29883 comp-unit DIE. */
29884 if (!vec_safe_is_empty (ranges_table))
29886 if (dwarf_version >= 5)
29887 add_AT_lineptr (main_comp_unit_die, DW_AT_rnglists_base,
29888 ranges_base_label);
29889 else
29890 add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
29891 ranges_section_label);
29894 switch_to_section (debug_addr_section);
29895 ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
29896 output_addr_table ();
29899 /* Output the main compilation unit if non-empty or if .debug_macinfo
29900 or .debug_macro will be emitted. */
29901 output_comp_unit (comp_unit_die (), have_macinfo,
29902 dwarf_split_debug_info ? checksum : NULL);
29904 if (dwarf_split_debug_info && info_section_emitted)
29905 output_skeleton_debug_sections (main_comp_unit_die, checksum);
29907 /* Output the abbreviation table. */
29908 if (vec_safe_length (abbrev_die_table) != 1)
29910 switch_to_section (debug_abbrev_section);
29911 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
29912 output_abbrev_section ();
29915 /* Output location list section if necessary. */
29916 if (have_location_lists)
29918 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
29919 char l2[MAX_ARTIFICIAL_LABEL_BYTES];
29920 /* Output the location lists info. */
29921 switch_to_section (debug_loc_section);
29922 if (dwarf_version >= 5)
29924 ASM_GENERATE_INTERNAL_LABEL (l1, DEBUG_LOC_SECTION_LABEL, 1);
29925 ASM_GENERATE_INTERNAL_LABEL (l2, DEBUG_LOC_SECTION_LABEL, 2);
29926 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
29927 dw2_asm_output_data (4, 0xffffffff,
29928 "Initial length escape value indicating "
29929 "64-bit DWARF extension");
29930 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
29931 "Length of Location Lists");
29932 ASM_OUTPUT_LABEL (asm_out_file, l1);
29933 dw2_asm_output_data (2, dwarf_version, "DWARF Version");
29934 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
29935 dw2_asm_output_data (1, 0, "Segment Size");
29936 dw2_asm_output_data (4, dwarf_split_debug_info ? loc_list_idx : 0,
29937 "Offset Entry Count");
29939 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
29940 if (dwarf_version >= 5 && dwarf_split_debug_info)
29942 unsigned int save_loc_list_idx = loc_list_idx;
29943 loc_list_idx = 0;
29944 output_loclists_offsets (comp_unit_die ());
29945 gcc_assert (save_loc_list_idx == loc_list_idx);
29947 output_location_lists (comp_unit_die ());
29948 if (dwarf_version >= 5)
29949 ASM_OUTPUT_LABEL (asm_out_file, l2);
29952 output_pubtables ();
29954 /* Output the address range information if a CU (.debug_info section)
29955 was emitted. We output an empty table even if we had no functions
29956 to put in it. This because the consumer has no way to tell the
29957 difference between an empty table that we omitted and failure to
29958 generate a table that would have contained data. */
29959 if (info_section_emitted)
29961 switch_to_section (debug_aranges_section);
29962 output_aranges ();
29965 /* Output ranges section if necessary. */
29966 if (!vec_safe_is_empty (ranges_table))
29968 if (dwarf_version >= 5)
29969 output_rnglists ();
29970 else
29971 output_ranges ();
29974 /* Have to end the macro section. */
29975 if (have_macinfo)
29977 switch_to_section (debug_macinfo_section);
29978 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
29979 output_macinfo ();
29980 dw2_asm_output_data (1, 0, "End compilation unit");
29983 /* Output the source line correspondence table. We must do this
29984 even if there is no line information. Otherwise, on an empty
29985 translation unit, we will generate a present, but empty,
29986 .debug_info section. IRIX 6.5 `nm' will then complain when
29987 examining the file. This is done late so that any filenames
29988 used by the debug_info section are marked as 'used'. */
29989 switch_to_section (debug_line_section);
29990 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
29991 if (! DWARF2_ASM_LINE_DEBUG_INFO)
29992 output_line_info (false);
29994 if (dwarf_split_debug_info && info_section_emitted)
29996 switch_to_section (debug_skeleton_line_section);
29997 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
29998 output_line_info (true);
30001 /* If we emitted any indirect strings, output the string table too. */
30002 if (debug_str_hash || skeleton_debug_str_hash)
30003 output_indirect_strings ();
30004 if (debug_line_str_hash)
30006 switch_to_section (debug_line_str_section);
30007 const enum dwarf_form form = DW_FORM_line_strp;
30008 debug_line_str_hash->traverse<enum dwarf_form,
30009 output_indirect_string> (form);
30013 /* Returns a hash value for X (which really is a variable_value_struct). */
30015 inline hashval_t
30016 variable_value_hasher::hash (variable_value_struct *x)
30018 return (hashval_t) x->decl_id;
30021 /* Return nonzero if decl_id of variable_value_struct X is the same as
30022 UID of decl Y. */
30024 inline bool
30025 variable_value_hasher::equal (variable_value_struct *x, tree y)
30027 return x->decl_id == DECL_UID (y);
30030 /* Helper function for resolve_variable_value, handle
30031 DW_OP_GNU_variable_value in one location expression.
30032 Return true if exprloc has been changed into loclist. */
30034 static bool
30035 resolve_variable_value_in_expr (dw_attr_node *a, dw_loc_descr_ref loc)
30037 dw_loc_descr_ref next;
30038 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = next)
30040 next = loc->dw_loc_next;
30041 if (loc->dw_loc_opc != DW_OP_GNU_variable_value
30042 || loc->dw_loc_oprnd1.val_class != dw_val_class_decl_ref)
30043 continue;
30045 tree decl = loc->dw_loc_oprnd1.v.val_decl_ref;
30046 if (DECL_CONTEXT (decl) != current_function_decl)
30047 continue;
30049 dw_die_ref ref = lookup_decl_die (decl);
30050 if (ref)
30052 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30053 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
30054 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
30055 continue;
30057 dw_loc_list_ref l = loc_list_from_tree (decl, 0, NULL);
30058 if (l == NULL)
30059 continue;
30060 if (l->dw_loc_next)
30062 if (AT_class (a) != dw_val_class_loc)
30063 continue;
30064 switch (a->dw_attr)
30066 /* Following attributes allow both exprloc and loclist
30067 classes, so we can change them into a loclist. */
30068 case DW_AT_location:
30069 case DW_AT_string_length:
30070 case DW_AT_return_addr:
30071 case DW_AT_data_member_location:
30072 case DW_AT_frame_base:
30073 case DW_AT_segment:
30074 case DW_AT_static_link:
30075 case DW_AT_use_location:
30076 case DW_AT_vtable_elem_location:
30077 if (prev)
30079 prev->dw_loc_next = NULL;
30080 prepend_loc_descr_to_each (l, AT_loc (a));
30082 if (next)
30083 add_loc_descr_to_each (l, next);
30084 a->dw_attr_val.val_class = dw_val_class_loc_list;
30085 a->dw_attr_val.val_entry = NULL;
30086 a->dw_attr_val.v.val_loc_list = l;
30087 have_location_lists = true;
30088 return true;
30089 /* Following attributes allow both exprloc and reference,
30090 so if the whole expression is DW_OP_GNU_variable_value alone
30091 we could transform it into reference. */
30092 case DW_AT_byte_size:
30093 case DW_AT_bit_size:
30094 case DW_AT_lower_bound:
30095 case DW_AT_upper_bound:
30096 case DW_AT_bit_stride:
30097 case DW_AT_count:
30098 case DW_AT_allocated:
30099 case DW_AT_associated:
30100 case DW_AT_byte_stride:
30101 if (prev == NULL && next == NULL)
30102 break;
30103 /* FALLTHRU */
30104 default:
30105 if (dwarf_strict)
30106 continue;
30107 break;
30109 /* Create DW_TAG_variable that we can refer to. */
30110 ref = gen_decl_die (decl, NULL_TREE, NULL,
30111 lookup_decl_die (current_function_decl));
30112 if (ref)
30114 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30115 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
30116 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
30118 continue;
30120 if (prev)
30122 prev->dw_loc_next = l->expr;
30123 add_loc_descr (&prev->dw_loc_next, next);
30124 free_loc_descr (loc, NULL);
30125 next = prev->dw_loc_next;
30127 else
30129 memcpy (loc, l->expr, sizeof (dw_loc_descr_node));
30130 add_loc_descr (&loc, next);
30131 next = loc;
30133 loc = prev;
30135 return false;
30138 /* Attempt to resolve DW_OP_GNU_variable_value using loc_list_from_tree. */
30140 static void
30141 resolve_variable_value (dw_die_ref die)
30143 dw_attr_node *a;
30144 dw_loc_list_ref loc;
30145 unsigned ix;
30147 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
30148 switch (AT_class (a))
30150 case dw_val_class_loc:
30151 if (!resolve_variable_value_in_expr (a, AT_loc (a)))
30152 break;
30153 /* FALLTHRU */
30154 case dw_val_class_loc_list:
30155 loc = AT_loc_list (a);
30156 gcc_assert (loc);
30157 for (; loc; loc = loc->dw_loc_next)
30158 resolve_variable_value_in_expr (a, loc->expr);
30159 break;
30160 default:
30161 break;
30165 /* Attempt to optimize DW_OP_GNU_variable_value refering to
30166 temporaries in the current function. */
30168 static void
30169 resolve_variable_values (void)
30171 if (!variable_value_hash || !current_function_decl)
30172 return;
30174 struct variable_value_struct *node
30175 = variable_value_hash->find_with_hash (current_function_decl,
30176 DECL_UID (current_function_decl));
30178 if (node == NULL)
30179 return;
30181 unsigned int i;
30182 dw_die_ref die;
30183 FOR_EACH_VEC_SAFE_ELT (node->dies, i, die)
30184 resolve_variable_value (die);
30187 /* Helper function for note_variable_value, handle one location
30188 expression. */
30190 static void
30191 note_variable_value_in_expr (dw_die_ref die, dw_loc_descr_ref loc)
30193 for (; loc; loc = loc->dw_loc_next)
30194 if (loc->dw_loc_opc == DW_OP_GNU_variable_value
30195 && loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
30197 tree decl = loc->dw_loc_oprnd1.v.val_decl_ref;
30198 dw_die_ref ref = lookup_decl_die (decl);
30199 if (ref)
30201 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30202 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
30203 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
30205 if (VAR_P (decl)
30206 && DECL_CONTEXT (decl)
30207 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL
30208 && lookup_decl_die (DECL_CONTEXT (decl)))
30210 if (!variable_value_hash)
30211 variable_value_hash
30212 = hash_table<variable_value_hasher>::create_ggc (10);
30214 tree fndecl = DECL_CONTEXT (decl);
30215 struct variable_value_struct *node;
30216 struct variable_value_struct **slot
30217 = variable_value_hash->find_slot_with_hash (fndecl,
30218 DECL_UID (fndecl),
30219 INSERT);
30220 if (*slot == NULL)
30222 node = ggc_cleared_alloc<variable_value_struct> ();
30223 node->decl_id = DECL_UID (fndecl);
30224 *slot = node;
30226 else
30227 node = *slot;
30229 vec_safe_push (node->dies, die);
30234 /* Walk the tree DIE and note DIEs with DW_OP_GNU_variable_value still
30235 with dw_val_class_decl_ref operand. */
30237 static void
30238 note_variable_value (dw_die_ref die)
30240 dw_die_ref c;
30241 dw_attr_node *a;
30242 dw_loc_list_ref loc;
30243 unsigned ix;
30245 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
30246 switch (AT_class (a))
30248 case dw_val_class_loc_list:
30249 loc = AT_loc_list (a);
30250 gcc_assert (loc);
30251 if (!loc->noted_variable_value)
30253 loc->noted_variable_value = 1;
30254 for (; loc; loc = loc->dw_loc_next)
30255 note_variable_value_in_expr (die, loc->expr);
30257 break;
30258 case dw_val_class_loc:
30259 note_variable_value_in_expr (die, AT_loc (a));
30260 break;
30261 default:
30262 break;
30265 /* Mark children. */
30266 FOR_EACH_CHILD (die, c, note_variable_value (c));
30269 /* Perform any cleanups needed after the early debug generation pass
30270 has run. */
30272 static void
30273 dwarf2out_early_finish (const char *filename)
30275 set_early_dwarf s;
30277 /* PCH might result in DW_AT_producer string being restored from the
30278 header compilation, so always fill it with empty string initially
30279 and overwrite only here. */
30280 dw_attr_node *producer = get_AT (comp_unit_die (), DW_AT_producer);
30281 producer_string = gen_producer_string ();
30282 producer->dw_attr_val.v.val_str->refcount--;
30283 producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
30285 /* Add the name for the main input file now. We delayed this from
30286 dwarf2out_init to avoid complications with PCH. */
30287 add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
30288 add_comp_dir_attribute (comp_unit_die ());
30290 /* When emitting DWARF5 .debug_line_str, move DW_AT_name and
30291 DW_AT_comp_dir into .debug_line_str section. */
30292 if (!DWARF2_ASM_LINE_DEBUG_INFO
30293 && dwarf_version >= 5
30294 && DWARF5_USE_DEBUG_LINE_STR)
30296 for (int i = 0; i < 2; i++)
30298 dw_attr_node *a = get_AT (comp_unit_die (),
30299 i ? DW_AT_comp_dir : DW_AT_name);
30300 if (a == NULL
30301 || AT_class (a) != dw_val_class_str
30302 || strlen (AT_string (a)) + 1 <= DWARF_OFFSET_SIZE)
30303 continue;
30305 if (! debug_line_str_hash)
30306 debug_line_str_hash
30307 = hash_table<indirect_string_hasher>::create_ggc (10);
30309 struct indirect_string_node *node
30310 = find_AT_string_in_table (AT_string (a), debug_line_str_hash);
30311 set_indirect_string (node);
30312 node->form = DW_FORM_line_strp;
30313 a->dw_attr_val.v.val_str->refcount--;
30314 a->dw_attr_val.v.val_str = node;
30318 /* With LTO early dwarf was really finished at compile-time, so make
30319 sure to adjust the phase after annotating the LTRANS CU DIE. */
30320 if (in_lto_p)
30322 early_dwarf_finished = true;
30323 return;
30326 /* Walk through the list of incomplete types again, trying once more to
30327 emit full debugging info for them. */
30328 retry_incomplete_types ();
30330 /* The point here is to flush out the limbo list so that it is empty
30331 and we don't need to stream it for LTO. */
30332 flush_limbo_die_list ();
30334 gen_scheduled_generic_parms_dies ();
30335 gen_remaining_tmpl_value_param_die_attribute ();
30337 /* Add DW_AT_linkage_name for all deferred DIEs. */
30338 for (limbo_die_node *node = deferred_asm_name; node; node = node->next)
30340 tree decl = node->created_for;
30341 if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
30342 /* A missing DECL_ASSEMBLER_NAME can be a constant DIE that
30343 ended up in deferred_asm_name before we knew it was
30344 constant and never written to disk. */
30345 && DECL_ASSEMBLER_NAME (decl))
30347 add_linkage_attr (node->die, decl);
30348 move_linkage_attr (node->die);
30351 deferred_asm_name = NULL;
30353 if (flag_eliminate_unused_debug_types)
30354 prune_unused_types ();
30356 /* Generate separate COMDAT sections for type DIEs. */
30357 if (use_debug_types)
30359 break_out_comdat_types (comp_unit_die ());
30361 /* Each new type_unit DIE was added to the limbo die list when created.
30362 Since these have all been added to comdat_type_list, clear the
30363 limbo die list. */
30364 limbo_die_list = NULL;
30366 /* For each new comdat type unit, copy declarations for incomplete
30367 types to make the new unit self-contained (i.e., no direct
30368 references to the main compile unit). */
30369 for (comdat_type_node *ctnode = comdat_type_list;
30370 ctnode != NULL; ctnode = ctnode->next)
30371 copy_decls_for_unworthy_types (ctnode->root_die);
30372 copy_decls_for_unworthy_types (comp_unit_die ());
30374 /* In the process of copying declarations from one unit to another,
30375 we may have left some declarations behind that are no longer
30376 referenced. Prune them. */
30377 prune_unused_types ();
30380 /* Generate separate CUs for each of the include files we've seen.
30381 They will go into limbo_die_list and from there to cu_die_list. */
30382 if (flag_eliminate_dwarf2_dups)
30384 gcc_assert (limbo_die_list == NULL);
30385 break_out_includes (comp_unit_die ());
30386 limbo_die_node *cu;
30387 while ((cu = limbo_die_list))
30389 limbo_die_list = cu->next;
30390 cu->next = cu_die_list;
30391 cu_die_list = cu;
30395 /* Traverse the DIE's and note DIEs with DW_OP_GNU_variable_value still
30396 with dw_val_class_decl_ref operand. */
30397 note_variable_value (comp_unit_die ());
30398 for (limbo_die_node *node = cu_die_list; node; node = node->next)
30399 note_variable_value (node->die);
30400 for (comdat_type_node *ctnode = comdat_type_list; ctnode != NULL;
30401 ctnode = ctnode->next)
30402 note_variable_value (ctnode->root_die);
30403 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
30404 note_variable_value (node->die);
30406 /* The early debug phase is now finished. */
30407 early_dwarf_finished = true;
30410 /* Reset all state within dwarf2out.c so that we can rerun the compiler
30411 within the same process. For use by toplev::finalize. */
30413 void
30414 dwarf2out_c_finalize (void)
30416 last_var_location_insn = NULL;
30417 cached_next_real_insn = NULL;
30418 used_rtx_array = NULL;
30419 incomplete_types = NULL;
30420 decl_scope_table = NULL;
30421 debug_info_section = NULL;
30422 debug_skeleton_info_section = NULL;
30423 debug_abbrev_section = NULL;
30424 debug_skeleton_abbrev_section = NULL;
30425 debug_aranges_section = NULL;
30426 debug_addr_section = NULL;
30427 debug_macinfo_section = NULL;
30428 debug_line_section = NULL;
30429 debug_skeleton_line_section = NULL;
30430 debug_loc_section = NULL;
30431 debug_pubnames_section = NULL;
30432 debug_pubtypes_section = NULL;
30433 debug_str_section = NULL;
30434 debug_line_str_section = NULL;
30435 debug_str_dwo_section = NULL;
30436 debug_str_offsets_section = NULL;
30437 debug_ranges_section = NULL;
30438 debug_frame_section = NULL;
30439 fde_vec = NULL;
30440 debug_str_hash = NULL;
30441 debug_line_str_hash = NULL;
30442 skeleton_debug_str_hash = NULL;
30443 dw2_string_counter = 0;
30444 have_multiple_function_sections = false;
30445 text_section_used = false;
30446 cold_text_section_used = false;
30447 cold_text_section = NULL;
30448 current_unit_personality = NULL;
30450 early_dwarf = false;
30451 early_dwarf_finished = false;
30453 next_die_offset = 0;
30454 single_comp_unit_die = NULL;
30455 comdat_type_list = NULL;
30456 limbo_die_list = NULL;
30457 file_table = NULL;
30458 decl_die_table = NULL;
30459 common_block_die_table = NULL;
30460 decl_loc_table = NULL;
30461 call_arg_locations = NULL;
30462 call_arg_loc_last = NULL;
30463 call_site_count = -1;
30464 tail_call_site_count = -1;
30465 cached_dw_loc_list_table = NULL;
30466 abbrev_die_table = NULL;
30467 delete dwarf_proc_stack_usage_map;
30468 dwarf_proc_stack_usage_map = NULL;
30469 line_info_label_num = 0;
30470 cur_line_info_table = NULL;
30471 text_section_line_info = NULL;
30472 cold_text_section_line_info = NULL;
30473 separate_line_info = NULL;
30474 info_section_emitted = false;
30475 pubname_table = NULL;
30476 pubtype_table = NULL;
30477 macinfo_table = NULL;
30478 ranges_table = NULL;
30479 ranges_by_label = NULL;
30480 rnglist_idx = 0;
30481 have_location_lists = false;
30482 loclabel_num = 0;
30483 poc_label_num = 0;
30484 last_emitted_file = NULL;
30485 label_num = 0;
30486 tmpl_value_parm_die_table = NULL;
30487 generic_type_instances = NULL;
30488 frame_pointer_fb_offset = 0;
30489 frame_pointer_fb_offset_valid = false;
30490 base_types.release ();
30491 XDELETEVEC (producer_string);
30492 producer_string = NULL;
30495 #include "gt-dwarf2out.h"