[ARM] PR target/79911: Invalid vec_select arguments
[official-gcc.git] / gcc / dwarf2out.c
blob0bbb90ed3aa19a61548532f315cb1ec3e1f52245
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_allocated;
6637 dw_attr_node *at_artificial;
6638 dw_attr_node *at_associated;
6639 dw_attr_node *at_binary_scale;
6640 dw_attr_node *at_bit_offset;
6641 dw_attr_node *at_bit_size;
6642 dw_attr_node *at_bit_stride;
6643 dw_attr_node *at_byte_size;
6644 dw_attr_node *at_byte_stride;
6645 dw_attr_node *at_const_value;
6646 dw_attr_node *at_containing_type;
6647 dw_attr_node *at_count;
6648 dw_attr_node *at_data_location;
6649 dw_attr_node *at_data_member_location;
6650 dw_attr_node *at_decimal_scale;
6651 dw_attr_node *at_decimal_sign;
6652 dw_attr_node *at_default_value;
6653 dw_attr_node *at_digit_count;
6654 dw_attr_node *at_discr;
6655 dw_attr_node *at_discr_list;
6656 dw_attr_node *at_discr_value;
6657 dw_attr_node *at_encoding;
6658 dw_attr_node *at_endianity;
6659 dw_attr_node *at_explicit;
6660 dw_attr_node *at_is_optional;
6661 dw_attr_node *at_location;
6662 dw_attr_node *at_lower_bound;
6663 dw_attr_node *at_mutable;
6664 dw_attr_node *at_ordering;
6665 dw_attr_node *at_picture_string;
6666 dw_attr_node *at_prototyped;
6667 dw_attr_node *at_small;
6668 dw_attr_node *at_segment;
6669 dw_attr_node *at_string_length;
6670 dw_attr_node *at_string_length_bit_size;
6671 dw_attr_node *at_string_length_byte_size;
6672 dw_attr_node *at_threads_scaled;
6673 dw_attr_node *at_upper_bound;
6674 dw_attr_node *at_use_location;
6675 dw_attr_node *at_use_UTF8;
6676 dw_attr_node *at_variable_parameter;
6677 dw_attr_node *at_virtuality;
6678 dw_attr_node *at_visibility;
6679 dw_attr_node *at_vtable_elem_location;
6682 /* Collect the attributes that we will want to use for the checksum. */
6684 static void
6685 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
6687 dw_attr_node *a;
6688 unsigned ix;
6690 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6692 switch (a->dw_attr)
6694 case DW_AT_name:
6695 attrs->at_name = a;
6696 break;
6697 case DW_AT_type:
6698 attrs->at_type = a;
6699 break;
6700 case DW_AT_friend:
6701 attrs->at_friend = a;
6702 break;
6703 case DW_AT_accessibility:
6704 attrs->at_accessibility = a;
6705 break;
6706 case DW_AT_address_class:
6707 attrs->at_address_class = a;
6708 break;
6709 case DW_AT_allocated:
6710 attrs->at_allocated = a;
6711 break;
6712 case DW_AT_artificial:
6713 attrs->at_artificial = a;
6714 break;
6715 case DW_AT_associated:
6716 attrs->at_associated = a;
6717 break;
6718 case DW_AT_binary_scale:
6719 attrs->at_binary_scale = a;
6720 break;
6721 case DW_AT_bit_offset:
6722 attrs->at_bit_offset = a;
6723 break;
6724 case DW_AT_bit_size:
6725 attrs->at_bit_size = a;
6726 break;
6727 case DW_AT_bit_stride:
6728 attrs->at_bit_stride = a;
6729 break;
6730 case DW_AT_byte_size:
6731 attrs->at_byte_size = a;
6732 break;
6733 case DW_AT_byte_stride:
6734 attrs->at_byte_stride = a;
6735 break;
6736 case DW_AT_const_value:
6737 attrs->at_const_value = a;
6738 break;
6739 case DW_AT_containing_type:
6740 attrs->at_containing_type = a;
6741 break;
6742 case DW_AT_count:
6743 attrs->at_count = a;
6744 break;
6745 case DW_AT_data_location:
6746 attrs->at_data_location = a;
6747 break;
6748 case DW_AT_data_member_location:
6749 attrs->at_data_member_location = a;
6750 break;
6751 case DW_AT_decimal_scale:
6752 attrs->at_decimal_scale = a;
6753 break;
6754 case DW_AT_decimal_sign:
6755 attrs->at_decimal_sign = a;
6756 break;
6757 case DW_AT_default_value:
6758 attrs->at_default_value = a;
6759 break;
6760 case DW_AT_digit_count:
6761 attrs->at_digit_count = a;
6762 break;
6763 case DW_AT_discr:
6764 attrs->at_discr = a;
6765 break;
6766 case DW_AT_discr_list:
6767 attrs->at_discr_list = a;
6768 break;
6769 case DW_AT_discr_value:
6770 attrs->at_discr_value = a;
6771 break;
6772 case DW_AT_encoding:
6773 attrs->at_encoding = a;
6774 break;
6775 case DW_AT_endianity:
6776 attrs->at_endianity = a;
6777 break;
6778 case DW_AT_explicit:
6779 attrs->at_explicit = a;
6780 break;
6781 case DW_AT_is_optional:
6782 attrs->at_is_optional = a;
6783 break;
6784 case DW_AT_location:
6785 attrs->at_location = a;
6786 break;
6787 case DW_AT_lower_bound:
6788 attrs->at_lower_bound = a;
6789 break;
6790 case DW_AT_mutable:
6791 attrs->at_mutable = a;
6792 break;
6793 case DW_AT_ordering:
6794 attrs->at_ordering = a;
6795 break;
6796 case DW_AT_picture_string:
6797 attrs->at_picture_string = a;
6798 break;
6799 case DW_AT_prototyped:
6800 attrs->at_prototyped = a;
6801 break;
6802 case DW_AT_small:
6803 attrs->at_small = a;
6804 break;
6805 case DW_AT_segment:
6806 attrs->at_segment = a;
6807 break;
6808 case DW_AT_string_length:
6809 attrs->at_string_length = a;
6810 break;
6811 case DW_AT_string_length_bit_size:
6812 attrs->at_string_length_bit_size = a;
6813 break;
6814 case DW_AT_string_length_byte_size:
6815 attrs->at_string_length_byte_size = a;
6816 break;
6817 case DW_AT_threads_scaled:
6818 attrs->at_threads_scaled = a;
6819 break;
6820 case DW_AT_upper_bound:
6821 attrs->at_upper_bound = a;
6822 break;
6823 case DW_AT_use_location:
6824 attrs->at_use_location = a;
6825 break;
6826 case DW_AT_use_UTF8:
6827 attrs->at_use_UTF8 = a;
6828 break;
6829 case DW_AT_variable_parameter:
6830 attrs->at_variable_parameter = a;
6831 break;
6832 case DW_AT_virtuality:
6833 attrs->at_virtuality = a;
6834 break;
6835 case DW_AT_visibility:
6836 attrs->at_visibility = a;
6837 break;
6838 case DW_AT_vtable_elem_location:
6839 attrs->at_vtable_elem_location = a;
6840 break;
6841 default:
6842 break;
6847 /* Calculate the checksum of a DIE, using an ordered subset of attributes. */
6849 static void
6850 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6852 dw_die_ref c;
6853 dw_die_ref decl;
6854 struct checksum_attributes attrs;
6856 CHECKSUM_ULEB128 ('D');
6857 CHECKSUM_ULEB128 (die->die_tag);
6859 memset (&attrs, 0, sizeof (attrs));
6861 decl = get_AT_ref (die, DW_AT_specification);
6862 if (decl != NULL)
6863 collect_checksum_attributes (&attrs, decl);
6864 collect_checksum_attributes (&attrs, die);
6866 CHECKSUM_ATTR (attrs.at_name);
6867 CHECKSUM_ATTR (attrs.at_accessibility);
6868 CHECKSUM_ATTR (attrs.at_address_class);
6869 CHECKSUM_ATTR (attrs.at_allocated);
6870 CHECKSUM_ATTR (attrs.at_artificial);
6871 CHECKSUM_ATTR (attrs.at_associated);
6872 CHECKSUM_ATTR (attrs.at_binary_scale);
6873 CHECKSUM_ATTR (attrs.at_bit_offset);
6874 CHECKSUM_ATTR (attrs.at_bit_size);
6875 CHECKSUM_ATTR (attrs.at_bit_stride);
6876 CHECKSUM_ATTR (attrs.at_byte_size);
6877 CHECKSUM_ATTR (attrs.at_byte_stride);
6878 CHECKSUM_ATTR (attrs.at_const_value);
6879 CHECKSUM_ATTR (attrs.at_containing_type);
6880 CHECKSUM_ATTR (attrs.at_count);
6881 CHECKSUM_ATTR (attrs.at_data_location);
6882 CHECKSUM_ATTR (attrs.at_data_member_location);
6883 CHECKSUM_ATTR (attrs.at_decimal_scale);
6884 CHECKSUM_ATTR (attrs.at_decimal_sign);
6885 CHECKSUM_ATTR (attrs.at_default_value);
6886 CHECKSUM_ATTR (attrs.at_digit_count);
6887 CHECKSUM_ATTR (attrs.at_discr);
6888 CHECKSUM_ATTR (attrs.at_discr_list);
6889 CHECKSUM_ATTR (attrs.at_discr_value);
6890 CHECKSUM_ATTR (attrs.at_encoding);
6891 CHECKSUM_ATTR (attrs.at_endianity);
6892 CHECKSUM_ATTR (attrs.at_explicit);
6893 CHECKSUM_ATTR (attrs.at_is_optional);
6894 CHECKSUM_ATTR (attrs.at_location);
6895 CHECKSUM_ATTR (attrs.at_lower_bound);
6896 CHECKSUM_ATTR (attrs.at_mutable);
6897 CHECKSUM_ATTR (attrs.at_ordering);
6898 CHECKSUM_ATTR (attrs.at_picture_string);
6899 CHECKSUM_ATTR (attrs.at_prototyped);
6900 CHECKSUM_ATTR (attrs.at_small);
6901 CHECKSUM_ATTR (attrs.at_segment);
6902 CHECKSUM_ATTR (attrs.at_string_length);
6903 CHECKSUM_ATTR (attrs.at_string_length_bit_size);
6904 CHECKSUM_ATTR (attrs.at_string_length_byte_size);
6905 CHECKSUM_ATTR (attrs.at_threads_scaled);
6906 CHECKSUM_ATTR (attrs.at_upper_bound);
6907 CHECKSUM_ATTR (attrs.at_use_location);
6908 CHECKSUM_ATTR (attrs.at_use_UTF8);
6909 CHECKSUM_ATTR (attrs.at_variable_parameter);
6910 CHECKSUM_ATTR (attrs.at_virtuality);
6911 CHECKSUM_ATTR (attrs.at_visibility);
6912 CHECKSUM_ATTR (attrs.at_vtable_elem_location);
6913 CHECKSUM_ATTR (attrs.at_type);
6914 CHECKSUM_ATTR (attrs.at_friend);
6916 /* Checksum the child DIEs. */
6917 c = die->die_child;
6918 if (c) do {
6919 dw_attr_node *name_attr;
6921 c = c->die_sib;
6922 name_attr = get_AT (c, DW_AT_name);
6923 if (is_template_instantiation (c))
6925 /* Ignore instantiations of member type and function templates. */
6927 else if (name_attr != NULL
6928 && (is_type_die (c) || c->die_tag == DW_TAG_subprogram))
6930 /* Use a shallow checksum for named nested types and member
6931 functions. */
6932 CHECKSUM_ULEB128 ('S');
6933 CHECKSUM_ULEB128 (c->die_tag);
6934 CHECKSUM_STRING (AT_string (name_attr));
6936 else
6938 /* Use a deep checksum for other children. */
6939 /* Mark this DIE so it gets processed when unmarking. */
6940 if (c->die_mark == 0)
6941 c->die_mark = -1;
6942 die_checksum_ordered (c, ctx, mark);
6944 } while (c != die->die_child);
6946 CHECKSUM_ULEB128 (0);
6949 /* Add a type name and tag to a hash. */
6950 static void
6951 die_odr_checksum (int tag, const char *name, md5_ctx *ctx)
6953 CHECKSUM_ULEB128 (tag);
6954 CHECKSUM_STRING (name);
6957 #undef CHECKSUM
6958 #undef CHECKSUM_STRING
6959 #undef CHECKSUM_ATTR
6960 #undef CHECKSUM_LEB128
6961 #undef CHECKSUM_ULEB128
6963 /* Generate the type signature for DIE. This is computed by generating an
6964 MD5 checksum over the DIE's tag, its relevant attributes, and its
6965 children. Attributes that are references to other DIEs are processed
6966 by recursion, using the MARK field to prevent infinite recursion.
6967 If the DIE is nested inside a namespace or another type, we also
6968 need to include that context in the signature. The lower 64 bits
6969 of the resulting MD5 checksum comprise the signature. */
6971 static void
6972 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
6974 int mark;
6975 const char *name;
6976 unsigned char checksum[16];
6977 struct md5_ctx ctx;
6978 dw_die_ref decl;
6979 dw_die_ref parent;
6981 name = get_AT_string (die, DW_AT_name);
6982 decl = get_AT_ref (die, DW_AT_specification);
6983 parent = get_die_parent (die);
6985 /* First, compute a signature for just the type name (and its surrounding
6986 context, if any. This is stored in the type unit DIE for link-time
6987 ODR (one-definition rule) checking. */
6989 if (is_cxx () && name != NULL)
6991 md5_init_ctx (&ctx);
6993 /* Checksum the names of surrounding namespaces and structures. */
6994 if (parent != NULL)
6995 checksum_die_context (parent, &ctx);
6997 /* Checksum the current DIE. */
6998 die_odr_checksum (die->die_tag, name, &ctx);
6999 md5_finish_ctx (&ctx, checksum);
7001 add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
7004 /* Next, compute the complete type signature. */
7006 md5_init_ctx (&ctx);
7007 mark = 1;
7008 die->die_mark = mark;
7010 /* Checksum the names of surrounding namespaces and structures. */
7011 if (parent != NULL)
7012 checksum_die_context (parent, &ctx);
7014 /* Checksum the DIE and its children. */
7015 die_checksum_ordered (die, &ctx, &mark);
7016 unmark_all_dies (die);
7017 md5_finish_ctx (&ctx, checksum);
7019 /* Store the signature in the type node and link the type DIE and the
7020 type node together. */
7021 memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
7022 DWARF_TYPE_SIGNATURE_SIZE);
7023 die->comdat_type_p = true;
7024 die->die_id.die_type_node = type_node;
7025 type_node->type_die = die;
7027 /* If the DIE is a specification, link its declaration to the type node
7028 as well. */
7029 if (decl != NULL)
7031 decl->comdat_type_p = true;
7032 decl->die_id.die_type_node = type_node;
7036 /* Do the location expressions look same? */
7037 static inline int
7038 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
7040 return loc1->dw_loc_opc == loc2->dw_loc_opc
7041 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
7042 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
7045 /* Do the values look the same? */
7046 static int
7047 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
7049 dw_loc_descr_ref loc1, loc2;
7050 rtx r1, r2;
7052 if (v1->val_class != v2->val_class)
7053 return 0;
7055 switch (v1->val_class)
7057 case dw_val_class_const:
7058 case dw_val_class_const_implicit:
7059 return v1->v.val_int == v2->v.val_int;
7060 case dw_val_class_unsigned_const:
7061 case dw_val_class_unsigned_const_implicit:
7062 return v1->v.val_unsigned == v2->v.val_unsigned;
7063 case dw_val_class_const_double:
7064 return v1->v.val_double.high == v2->v.val_double.high
7065 && v1->v.val_double.low == v2->v.val_double.low;
7066 case dw_val_class_wide_int:
7067 return *v1->v.val_wide == *v2->v.val_wide;
7068 case dw_val_class_vec:
7069 if (v1->v.val_vec.length != v2->v.val_vec.length
7070 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
7071 return 0;
7072 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
7073 v1->v.val_vec.length * v1->v.val_vec.elt_size))
7074 return 0;
7075 return 1;
7076 case dw_val_class_flag:
7077 return v1->v.val_flag == v2->v.val_flag;
7078 case dw_val_class_str:
7079 return !strcmp (v1->v.val_str->str, v2->v.val_str->str);
7081 case dw_val_class_addr:
7082 r1 = v1->v.val_addr;
7083 r2 = v2->v.val_addr;
7084 if (GET_CODE (r1) != GET_CODE (r2))
7085 return 0;
7086 return !rtx_equal_p (r1, r2);
7088 case dw_val_class_offset:
7089 return v1->v.val_offset == v2->v.val_offset;
7091 case dw_val_class_loc:
7092 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
7093 loc1 && loc2;
7094 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
7095 if (!same_loc_p (loc1, loc2, mark))
7096 return 0;
7097 return !loc1 && !loc2;
7099 case dw_val_class_die_ref:
7100 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
7102 case dw_val_class_fde_ref:
7103 case dw_val_class_vms_delta:
7104 case dw_val_class_lbl_id:
7105 case dw_val_class_lineptr:
7106 case dw_val_class_macptr:
7107 case dw_val_class_loclistsptr:
7108 case dw_val_class_high_pc:
7109 return 1;
7111 case dw_val_class_file:
7112 case dw_val_class_file_implicit:
7113 return v1->v.val_file == v2->v.val_file;
7115 case dw_val_class_data8:
7116 return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
7118 default:
7119 return 1;
7123 /* Do the attributes look the same? */
7125 static int
7126 same_attr_p (dw_attr_node *at1, dw_attr_node *at2, int *mark)
7128 if (at1->dw_attr != at2->dw_attr)
7129 return 0;
7131 /* We don't care that this was compiled with a different compiler
7132 snapshot; if the output is the same, that's what matters. */
7133 if (at1->dw_attr == DW_AT_producer)
7134 return 1;
7136 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
7139 /* Do the dies look the same? */
7141 static int
7142 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
7144 dw_die_ref c1, c2;
7145 dw_attr_node *a1;
7146 unsigned ix;
7148 /* To avoid infinite recursion. */
7149 if (die1->die_mark)
7150 return die1->die_mark == die2->die_mark;
7151 die1->die_mark = die2->die_mark = ++(*mark);
7153 if (die1->die_tag != die2->die_tag)
7154 return 0;
7156 if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
7157 return 0;
7159 FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
7160 if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
7161 return 0;
7163 c1 = die1->die_child;
7164 c2 = die2->die_child;
7165 if (! c1)
7167 if (c2)
7168 return 0;
7170 else
7171 for (;;)
7173 if (!same_die_p (c1, c2, mark))
7174 return 0;
7175 c1 = c1->die_sib;
7176 c2 = c2->die_sib;
7177 if (c1 == die1->die_child)
7179 if (c2 == die2->die_child)
7180 break;
7181 else
7182 return 0;
7186 return 1;
7189 /* Do the dies look the same? Wrapper around same_die_p. */
7191 static int
7192 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
7194 int mark = 0;
7195 int ret = same_die_p (die1, die2, &mark);
7197 unmark_all_dies (die1);
7198 unmark_all_dies (die2);
7200 return ret;
7203 /* The prefix to attach to symbols on DIEs in the current comdat debug
7204 info section. */
7205 static const char *comdat_symbol_id;
7207 /* The index of the current symbol within the current comdat CU. */
7208 static unsigned int comdat_symbol_number;
7210 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
7211 children, and set comdat_symbol_id accordingly. */
7213 static void
7214 compute_section_prefix (dw_die_ref unit_die)
7216 const char *die_name = get_AT_string (unit_die, DW_AT_name);
7217 const char *base = die_name ? lbasename (die_name) : "anonymous";
7218 char *name = XALLOCAVEC (char, strlen (base) + 64);
7219 char *p;
7220 int i, mark;
7221 unsigned char checksum[16];
7222 struct md5_ctx ctx;
7224 /* Compute the checksum of the DIE, then append part of it as hex digits to
7225 the name filename of the unit. */
7227 md5_init_ctx (&ctx);
7228 mark = 0;
7229 die_checksum (unit_die, &ctx, &mark);
7230 unmark_all_dies (unit_die);
7231 md5_finish_ctx (&ctx, checksum);
7233 sprintf (name, "%s.", base);
7234 clean_symbol_name (name);
7236 p = name + strlen (name);
7237 for (i = 0; i < 4; i++)
7239 sprintf (p, "%.2x", checksum[i]);
7240 p += 2;
7243 comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
7244 comdat_symbol_number = 0;
7247 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
7249 static int
7250 is_type_die (dw_die_ref die)
7252 switch (die->die_tag)
7254 case DW_TAG_array_type:
7255 case DW_TAG_class_type:
7256 case DW_TAG_interface_type:
7257 case DW_TAG_enumeration_type:
7258 case DW_TAG_pointer_type:
7259 case DW_TAG_reference_type:
7260 case DW_TAG_rvalue_reference_type:
7261 case DW_TAG_string_type:
7262 case DW_TAG_structure_type:
7263 case DW_TAG_subroutine_type:
7264 case DW_TAG_union_type:
7265 case DW_TAG_ptr_to_member_type:
7266 case DW_TAG_set_type:
7267 case DW_TAG_subrange_type:
7268 case DW_TAG_base_type:
7269 case DW_TAG_const_type:
7270 case DW_TAG_file_type:
7271 case DW_TAG_packed_type:
7272 case DW_TAG_volatile_type:
7273 case DW_TAG_typedef:
7274 return 1;
7275 default:
7276 return 0;
7280 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
7281 Basically, we want to choose the bits that are likely to be shared between
7282 compilations (types) and leave out the bits that are specific to individual
7283 compilations (functions). */
7285 static int
7286 is_comdat_die (dw_die_ref c)
7288 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
7289 we do for stabs. The advantage is a greater likelihood of sharing between
7290 objects that don't include headers in the same order (and therefore would
7291 put the base types in a different comdat). jason 8/28/00 */
7293 if (c->die_tag == DW_TAG_base_type)
7294 return 0;
7296 if (c->die_tag == DW_TAG_pointer_type
7297 || c->die_tag == DW_TAG_reference_type
7298 || c->die_tag == DW_TAG_rvalue_reference_type
7299 || c->die_tag == DW_TAG_const_type
7300 || c->die_tag == DW_TAG_volatile_type)
7302 dw_die_ref t = get_AT_ref (c, DW_AT_type);
7304 return t ? is_comdat_die (t) : 0;
7307 return is_type_die (c);
7310 /* Returns 1 iff C is the sort of DIE that might be referred to from another
7311 compilation unit. */
7313 static int
7314 is_symbol_die (dw_die_ref c)
7316 return (is_type_die (c)
7317 || is_declaration_die (c)
7318 || c->die_tag == DW_TAG_namespace
7319 || c->die_tag == DW_TAG_module);
7322 /* Returns true iff C is a compile-unit DIE. */
7324 static inline bool
7325 is_cu_die (dw_die_ref c)
7327 return c && (c->die_tag == DW_TAG_compile_unit
7328 || c->die_tag == DW_TAG_skeleton_unit);
7331 /* Returns true iff C is a unit DIE of some sort. */
7333 static inline bool
7334 is_unit_die (dw_die_ref c)
7336 return c && (c->die_tag == DW_TAG_compile_unit
7337 || c->die_tag == DW_TAG_partial_unit
7338 || c->die_tag == DW_TAG_type_unit
7339 || c->die_tag == DW_TAG_skeleton_unit);
7342 /* Returns true iff C is a namespace DIE. */
7344 static inline bool
7345 is_namespace_die (dw_die_ref c)
7347 return c && c->die_tag == DW_TAG_namespace;
7350 /* Returns true iff C is a class or structure DIE. */
7352 static inline bool
7353 is_class_die (dw_die_ref c)
7355 return c && (c->die_tag == DW_TAG_class_type
7356 || c->die_tag == DW_TAG_structure_type);
7359 /* Return non-zero if this DIE is a template parameter. */
7361 static inline bool
7362 is_template_parameter (dw_die_ref die)
7364 switch (die->die_tag)
7366 case DW_TAG_template_type_param:
7367 case DW_TAG_template_value_param:
7368 case DW_TAG_GNU_template_template_param:
7369 case DW_TAG_GNU_template_parameter_pack:
7370 return true;
7371 default:
7372 return false;
7376 /* Return non-zero if this DIE represents a template instantiation. */
7378 static inline bool
7379 is_template_instantiation (dw_die_ref die)
7381 dw_die_ref c;
7383 if (!is_type_die (die) && die->die_tag != DW_TAG_subprogram)
7384 return false;
7385 FOR_EACH_CHILD (die, c, if (is_template_parameter (c)) return true);
7386 return false;
7389 static char *
7390 gen_internal_sym (const char *prefix)
7392 char buf[MAX_ARTIFICIAL_LABEL_BYTES];
7394 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
7395 return xstrdup (buf);
7398 /* Assign symbols to all worthy DIEs under DIE. */
7400 static void
7401 assign_symbol_names (dw_die_ref die)
7403 dw_die_ref c;
7405 if (is_symbol_die (die) && !die->comdat_type_p)
7407 if (comdat_symbol_id)
7409 char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
7411 sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
7412 comdat_symbol_id, comdat_symbol_number++);
7413 die->die_id.die_symbol = xstrdup (p);
7415 else
7416 die->die_id.die_symbol = gen_internal_sym ("LDIE");
7419 FOR_EACH_CHILD (die, c, assign_symbol_names (c));
7422 struct cu_hash_table_entry
7424 dw_die_ref cu;
7425 unsigned min_comdat_num, max_comdat_num;
7426 struct cu_hash_table_entry *next;
7429 /* Helpers to manipulate hash table of CUs. */
7431 struct cu_hash_table_entry_hasher : pointer_hash <cu_hash_table_entry>
7433 typedef die_struct *compare_type;
7434 static inline hashval_t hash (const cu_hash_table_entry *);
7435 static inline bool equal (const cu_hash_table_entry *, const die_struct *);
7436 static inline void remove (cu_hash_table_entry *);
7439 inline hashval_t
7440 cu_hash_table_entry_hasher::hash (const cu_hash_table_entry *entry)
7442 return htab_hash_string (entry->cu->die_id.die_symbol);
7445 inline bool
7446 cu_hash_table_entry_hasher::equal (const cu_hash_table_entry *entry1,
7447 const die_struct *entry2)
7449 return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
7452 inline void
7453 cu_hash_table_entry_hasher::remove (cu_hash_table_entry *entry)
7455 struct cu_hash_table_entry *next;
7457 while (entry)
7459 next = entry->next;
7460 free (entry);
7461 entry = next;
7465 typedef hash_table<cu_hash_table_entry_hasher> cu_hash_type;
7467 /* Check whether we have already seen this CU and set up SYM_NUM
7468 accordingly. */
7469 static int
7470 check_duplicate_cu (dw_die_ref cu, cu_hash_type *htable, unsigned int *sym_num)
7472 struct cu_hash_table_entry dummy;
7473 struct cu_hash_table_entry **slot, *entry, *last = &dummy;
7475 dummy.max_comdat_num = 0;
7477 slot = htable->find_slot_with_hash (cu,
7478 htab_hash_string (cu->die_id.die_symbol),
7479 INSERT);
7480 entry = *slot;
7482 for (; entry; last = entry, entry = entry->next)
7484 if (same_die_p_wrap (cu, entry->cu))
7485 break;
7488 if (entry)
7490 *sym_num = entry->min_comdat_num;
7491 return 1;
7494 entry = XCNEW (struct cu_hash_table_entry);
7495 entry->cu = cu;
7496 entry->min_comdat_num = *sym_num = last->max_comdat_num;
7497 entry->next = *slot;
7498 *slot = entry;
7500 return 0;
7503 /* Record SYM_NUM to record of CU in HTABLE. */
7504 static void
7505 record_comdat_symbol_number (dw_die_ref cu, cu_hash_type *htable,
7506 unsigned int sym_num)
7508 struct cu_hash_table_entry **slot, *entry;
7510 slot = htable->find_slot_with_hash (cu,
7511 htab_hash_string (cu->die_id.die_symbol),
7512 NO_INSERT);
7513 entry = *slot;
7515 entry->max_comdat_num = sym_num;
7518 /* Traverse the DIE (which is always comp_unit_die), and set up
7519 additional compilation units for each of the include files we see
7520 bracketed by BINCL/EINCL. */
7522 static void
7523 break_out_includes (dw_die_ref die)
7525 dw_die_ref c;
7526 dw_die_ref unit = NULL;
7527 limbo_die_node *node, **pnode;
7529 c = die->die_child;
7530 if (c) do {
7531 dw_die_ref prev = c;
7532 c = c->die_sib;
7533 while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
7534 || (unit && is_comdat_die (c)))
7536 dw_die_ref next = c->die_sib;
7538 /* This DIE is for a secondary CU; remove it from the main one. */
7539 remove_child_with_prev (c, prev);
7541 if (c->die_tag == DW_TAG_GNU_BINCL)
7542 unit = push_new_compile_unit (unit, c);
7543 else if (c->die_tag == DW_TAG_GNU_EINCL)
7544 unit = pop_compile_unit (unit);
7545 else
7546 add_child_die (unit, c);
7547 c = next;
7548 if (c == die->die_child)
7549 break;
7551 } while (c != die->die_child);
7553 #if 0
7554 /* We can only use this in debugging, since the frontend doesn't check
7555 to make sure that we leave every include file we enter. */
7556 gcc_assert (!unit);
7557 #endif
7559 assign_symbol_names (die);
7560 cu_hash_type cu_hash_table (10);
7561 for (node = limbo_die_list, pnode = &limbo_die_list;
7562 node;
7563 node = node->next)
7565 int is_dupl;
7567 compute_section_prefix (node->die);
7568 is_dupl = check_duplicate_cu (node->die, &cu_hash_table,
7569 &comdat_symbol_number);
7570 assign_symbol_names (node->die);
7571 if (is_dupl)
7572 *pnode = node->next;
7573 else
7575 pnode = &node->next;
7576 record_comdat_symbol_number (node->die, &cu_hash_table,
7577 comdat_symbol_number);
7582 /* Return non-zero if this DIE is a declaration. */
7584 static int
7585 is_declaration_die (dw_die_ref die)
7587 dw_attr_node *a;
7588 unsigned ix;
7590 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7591 if (a->dw_attr == DW_AT_declaration)
7592 return 1;
7594 return 0;
7597 /* Return non-zero if this DIE is nested inside a subprogram. */
7599 static int
7600 is_nested_in_subprogram (dw_die_ref die)
7602 dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
7604 if (decl == NULL)
7605 decl = die;
7606 return local_scope_p (decl);
7609 /* Return non-zero if this DIE contains a defining declaration of a
7610 subprogram. */
7612 static int
7613 contains_subprogram_definition (dw_die_ref die)
7615 dw_die_ref c;
7617 if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
7618 return 1;
7619 FOR_EACH_CHILD (die, c, if (contains_subprogram_definition (c)) return 1);
7620 return 0;
7623 /* Return non-zero if this is a type DIE that should be moved to a
7624 COMDAT .debug_types section or .debug_info section with DW_UT_*type
7625 unit type. */
7627 static int
7628 should_move_die_to_comdat (dw_die_ref die)
7630 switch (die->die_tag)
7632 case DW_TAG_class_type:
7633 case DW_TAG_structure_type:
7634 case DW_TAG_enumeration_type:
7635 case DW_TAG_union_type:
7636 /* Don't move declarations, inlined instances, types nested in a
7637 subprogram, or types that contain subprogram definitions. */
7638 if (is_declaration_die (die)
7639 || get_AT (die, DW_AT_abstract_origin)
7640 || is_nested_in_subprogram (die)
7641 || contains_subprogram_definition (die))
7642 return 0;
7643 return 1;
7644 case DW_TAG_array_type:
7645 case DW_TAG_interface_type:
7646 case DW_TAG_pointer_type:
7647 case DW_TAG_reference_type:
7648 case DW_TAG_rvalue_reference_type:
7649 case DW_TAG_string_type:
7650 case DW_TAG_subroutine_type:
7651 case DW_TAG_ptr_to_member_type:
7652 case DW_TAG_set_type:
7653 case DW_TAG_subrange_type:
7654 case DW_TAG_base_type:
7655 case DW_TAG_const_type:
7656 case DW_TAG_file_type:
7657 case DW_TAG_packed_type:
7658 case DW_TAG_volatile_type:
7659 case DW_TAG_typedef:
7660 default:
7661 return 0;
7665 /* Make a clone of DIE. */
7667 static dw_die_ref
7668 clone_die (dw_die_ref die)
7670 dw_die_ref clone;
7671 dw_attr_node *a;
7672 unsigned ix;
7674 clone = ggc_cleared_alloc<die_node> ();
7675 clone->die_tag = die->die_tag;
7677 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7678 add_dwarf_attr (clone, a);
7680 return clone;
7683 /* Make a clone of the tree rooted at DIE. */
7685 static dw_die_ref
7686 clone_tree (dw_die_ref die)
7688 dw_die_ref c;
7689 dw_die_ref clone = clone_die (die);
7691 FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree (c)));
7693 return clone;
7696 /* Make a clone of DIE as a declaration. */
7698 static dw_die_ref
7699 clone_as_declaration (dw_die_ref die)
7701 dw_die_ref clone;
7702 dw_die_ref decl;
7703 dw_attr_node *a;
7704 unsigned ix;
7706 /* If the DIE is already a declaration, just clone it. */
7707 if (is_declaration_die (die))
7708 return clone_die (die);
7710 /* If the DIE is a specification, just clone its declaration DIE. */
7711 decl = get_AT_ref (die, DW_AT_specification);
7712 if (decl != NULL)
7714 clone = clone_die (decl);
7715 if (die->comdat_type_p)
7716 add_AT_die_ref (clone, DW_AT_signature, die);
7717 return clone;
7720 clone = ggc_cleared_alloc<die_node> ();
7721 clone->die_tag = die->die_tag;
7723 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7725 /* We don't want to copy over all attributes.
7726 For example we don't want DW_AT_byte_size because otherwise we will no
7727 longer have a declaration and GDB will treat it as a definition. */
7729 switch (a->dw_attr)
7731 case DW_AT_abstract_origin:
7732 case DW_AT_artificial:
7733 case DW_AT_containing_type:
7734 case DW_AT_external:
7735 case DW_AT_name:
7736 case DW_AT_type:
7737 case DW_AT_virtuality:
7738 case DW_AT_linkage_name:
7739 case DW_AT_MIPS_linkage_name:
7740 add_dwarf_attr (clone, a);
7741 break;
7742 case DW_AT_byte_size:
7743 case DW_AT_alignment:
7744 default:
7745 break;
7749 if (die->comdat_type_p)
7750 add_AT_die_ref (clone, DW_AT_signature, die);
7752 add_AT_flag (clone, DW_AT_declaration, 1);
7753 return clone;
7757 /* Structure to map a DIE in one CU to its copy in a comdat type unit. */
7759 struct decl_table_entry
7761 dw_die_ref orig;
7762 dw_die_ref copy;
7765 /* Helpers to manipulate hash table of copied declarations. */
7767 /* Hashtable helpers. */
7769 struct decl_table_entry_hasher : free_ptr_hash <decl_table_entry>
7771 typedef die_struct *compare_type;
7772 static inline hashval_t hash (const decl_table_entry *);
7773 static inline bool equal (const decl_table_entry *, const die_struct *);
7776 inline hashval_t
7777 decl_table_entry_hasher::hash (const decl_table_entry *entry)
7779 return htab_hash_pointer (entry->orig);
7782 inline bool
7783 decl_table_entry_hasher::equal (const decl_table_entry *entry1,
7784 const die_struct *entry2)
7786 return entry1->orig == entry2;
7789 typedef hash_table<decl_table_entry_hasher> decl_hash_type;
7791 /* Copy DIE and its ancestors, up to, but not including, the compile unit
7792 or type unit entry, to a new tree. Adds the new tree to UNIT and returns
7793 a pointer to the copy of DIE. If DECL_TABLE is provided, it is used
7794 to check if the ancestor has already been copied into UNIT. */
7796 static dw_die_ref
7797 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die,
7798 decl_hash_type *decl_table)
7800 dw_die_ref parent = die->die_parent;
7801 dw_die_ref new_parent = unit;
7802 dw_die_ref copy;
7803 decl_table_entry **slot = NULL;
7804 struct decl_table_entry *entry = NULL;
7806 if (decl_table)
7808 /* Check if the entry has already been copied to UNIT. */
7809 slot = decl_table->find_slot_with_hash (die, htab_hash_pointer (die),
7810 INSERT);
7811 if (*slot != HTAB_EMPTY_ENTRY)
7813 entry = *slot;
7814 return entry->copy;
7817 /* Record in DECL_TABLE that DIE has been copied to UNIT. */
7818 entry = XCNEW (struct decl_table_entry);
7819 entry->orig = die;
7820 entry->copy = NULL;
7821 *slot = entry;
7824 if (parent != NULL)
7826 dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
7827 if (spec != NULL)
7828 parent = spec;
7829 if (!is_unit_die (parent))
7830 new_parent = copy_ancestor_tree (unit, parent, decl_table);
7833 copy = clone_as_declaration (die);
7834 add_child_die (new_parent, copy);
7836 if (decl_table)
7838 /* Record the pointer to the copy. */
7839 entry->copy = copy;
7842 return copy;
7844 /* Copy the declaration context to the new type unit DIE. This includes
7845 any surrounding namespace or type declarations. If the DIE has an
7846 AT_specification attribute, it also includes attributes and children
7847 attached to the specification, and returns a pointer to the original
7848 parent of the declaration DIE. Returns NULL otherwise. */
7850 static dw_die_ref
7851 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
7853 dw_die_ref decl;
7854 dw_die_ref new_decl;
7855 dw_die_ref orig_parent = NULL;
7857 decl = get_AT_ref (die, DW_AT_specification);
7858 if (decl == NULL)
7859 decl = die;
7860 else
7862 unsigned ix;
7863 dw_die_ref c;
7864 dw_attr_node *a;
7866 /* The original DIE will be changed to a declaration, and must
7867 be moved to be a child of the original declaration DIE. */
7868 orig_parent = decl->die_parent;
7870 /* Copy the type node pointer from the new DIE to the original
7871 declaration DIE so we can forward references later. */
7872 decl->comdat_type_p = true;
7873 decl->die_id.die_type_node = die->die_id.die_type_node;
7875 remove_AT (die, DW_AT_specification);
7877 FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
7879 if (a->dw_attr != DW_AT_name
7880 && a->dw_attr != DW_AT_declaration
7881 && a->dw_attr != DW_AT_external)
7882 add_dwarf_attr (die, a);
7885 FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree (c)));
7888 if (decl->die_parent != NULL
7889 && !is_unit_die (decl->die_parent))
7891 new_decl = copy_ancestor_tree (unit, decl, NULL);
7892 if (new_decl != NULL)
7894 remove_AT (new_decl, DW_AT_signature);
7895 add_AT_specification (die, new_decl);
7899 return orig_parent;
7902 /* Generate the skeleton ancestor tree for the given NODE, then clone
7903 the DIE and add the clone into the tree. */
7905 static void
7906 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
7908 if (node->new_die != NULL)
7909 return;
7911 node->new_die = clone_as_declaration (node->old_die);
7913 if (node->parent != NULL)
7915 generate_skeleton_ancestor_tree (node->parent);
7916 add_child_die (node->parent->new_die, node->new_die);
7920 /* Generate a skeleton tree of DIEs containing any declarations that are
7921 found in the original tree. We traverse the tree looking for declaration
7922 DIEs, and construct the skeleton from the bottom up whenever we find one. */
7924 static void
7925 generate_skeleton_bottom_up (skeleton_chain_node *parent)
7927 skeleton_chain_node node;
7928 dw_die_ref c;
7929 dw_die_ref first;
7930 dw_die_ref prev = NULL;
7931 dw_die_ref next = NULL;
7933 node.parent = parent;
7935 first = c = parent->old_die->die_child;
7936 if (c)
7937 next = c->die_sib;
7938 if (c) do {
7939 if (prev == NULL || prev->die_sib == c)
7940 prev = c;
7941 c = next;
7942 next = (c == first ? NULL : c->die_sib);
7943 node.old_die = c;
7944 node.new_die = NULL;
7945 if (is_declaration_die (c))
7947 if (is_template_instantiation (c))
7949 /* Instantiated templates do not need to be cloned into the
7950 type unit. Just move the DIE and its children back to
7951 the skeleton tree (in the main CU). */
7952 remove_child_with_prev (c, prev);
7953 add_child_die (parent->new_die, c);
7954 c = prev;
7956 else if (c->comdat_type_p)
7958 /* This is the skeleton of earlier break_out_comdat_types
7959 type. Clone the existing DIE, but keep the children
7960 under the original (which is in the main CU). */
7961 dw_die_ref clone = clone_die (c);
7963 replace_child (c, clone, prev);
7964 generate_skeleton_ancestor_tree (parent);
7965 add_child_die (parent->new_die, c);
7966 c = clone;
7967 continue;
7969 else
7971 /* Clone the existing DIE, move the original to the skeleton
7972 tree (which is in the main CU), and put the clone, with
7973 all the original's children, where the original came from
7974 (which is about to be moved to the type unit). */
7975 dw_die_ref clone = clone_die (c);
7976 move_all_children (c, clone);
7978 /* If the original has a DW_AT_object_pointer attribute,
7979 it would now point to a child DIE just moved to the
7980 cloned tree, so we need to remove that attribute from
7981 the original. */
7982 remove_AT (c, DW_AT_object_pointer);
7984 replace_child (c, clone, prev);
7985 generate_skeleton_ancestor_tree (parent);
7986 add_child_die (parent->new_die, c);
7987 node.old_die = clone;
7988 node.new_die = c;
7989 c = clone;
7992 generate_skeleton_bottom_up (&node);
7993 } while (next != NULL);
7996 /* Wrapper function for generate_skeleton_bottom_up. */
7998 static dw_die_ref
7999 generate_skeleton (dw_die_ref die)
8001 skeleton_chain_node node;
8003 node.old_die = die;
8004 node.new_die = NULL;
8005 node.parent = NULL;
8007 /* If this type definition is nested inside another type,
8008 and is not an instantiation of a template, always leave
8009 at least a declaration in its place. */
8010 if (die->die_parent != NULL
8011 && is_type_die (die->die_parent)
8012 && !is_template_instantiation (die))
8013 node.new_die = clone_as_declaration (die);
8015 generate_skeleton_bottom_up (&node);
8016 return node.new_die;
8019 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
8020 declaration. The original DIE is moved to a new compile unit so that
8021 existing references to it follow it to the new location. If any of the
8022 original DIE's descendants is a declaration, we need to replace the
8023 original DIE with a skeleton tree and move the declarations back into the
8024 skeleton tree. */
8026 static dw_die_ref
8027 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
8028 dw_die_ref prev)
8030 dw_die_ref skeleton, orig_parent;
8032 /* Copy the declaration context to the type unit DIE. If the returned
8033 ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
8034 that DIE. */
8035 orig_parent = copy_declaration_context (unit, child);
8037 skeleton = generate_skeleton (child);
8038 if (skeleton == NULL)
8039 remove_child_with_prev (child, prev);
8040 else
8042 skeleton->comdat_type_p = true;
8043 skeleton->die_id.die_type_node = child->die_id.die_type_node;
8045 /* If the original DIE was a specification, we need to put
8046 the skeleton under the parent DIE of the declaration.
8047 This leaves the original declaration in the tree, but
8048 it will be pruned later since there are no longer any
8049 references to it. */
8050 if (orig_parent != NULL)
8052 remove_child_with_prev (child, prev);
8053 add_child_die (orig_parent, skeleton);
8055 else
8056 replace_child (child, skeleton, prev);
8059 return skeleton;
8062 static void
8063 copy_dwarf_procs_ref_in_attrs (dw_die_ref die,
8064 comdat_type_node *type_node,
8065 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs);
8067 /* Helper for copy_dwarf_procs_ref_in_dies. Make a copy of the DIE DWARF
8068 procedure, put it under TYPE_NODE and return the copy. Continue looking for
8069 DWARF procedure references in the DW_AT_location attribute. */
8071 static dw_die_ref
8072 copy_dwarf_procedure (dw_die_ref die,
8073 comdat_type_node *type_node,
8074 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8076 gcc_assert (die->die_tag == DW_TAG_dwarf_procedure);
8078 /* DWARF procedures are not supposed to have children... */
8079 gcc_assert (die->die_child == NULL);
8081 /* ... and they are supposed to have only one attribute: DW_AT_location. */
8082 gcc_assert (vec_safe_length (die->die_attr) == 1
8083 && ((*die->die_attr)[0].dw_attr == DW_AT_location));
8085 /* Do not copy more than once DWARF procedures. */
8086 bool existed;
8087 dw_die_ref &die_copy = copied_dwarf_procs.get_or_insert (die, &existed);
8088 if (existed)
8089 return die_copy;
8091 die_copy = clone_die (die);
8092 add_child_die (type_node->root_die, die_copy);
8093 copy_dwarf_procs_ref_in_attrs (die_copy, type_node, copied_dwarf_procs);
8094 return die_copy;
8097 /* Helper for copy_dwarf_procs_ref_in_dies. Look for references to DWARF
8098 procedures in DIE's attributes. */
8100 static void
8101 copy_dwarf_procs_ref_in_attrs (dw_die_ref die,
8102 comdat_type_node *type_node,
8103 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8105 dw_attr_node *a;
8106 unsigned i;
8108 FOR_EACH_VEC_SAFE_ELT (die->die_attr, i, a)
8110 dw_loc_descr_ref loc;
8112 if (a->dw_attr_val.val_class != dw_val_class_loc)
8113 continue;
8115 for (loc = a->dw_attr_val.v.val_loc; loc != NULL; loc = loc->dw_loc_next)
8117 switch (loc->dw_loc_opc)
8119 case DW_OP_call2:
8120 case DW_OP_call4:
8121 case DW_OP_call_ref:
8122 gcc_assert (loc->dw_loc_oprnd1.val_class
8123 == dw_val_class_die_ref);
8124 loc->dw_loc_oprnd1.v.val_die_ref.die
8125 = copy_dwarf_procedure (loc->dw_loc_oprnd1.v.val_die_ref.die,
8126 type_node,
8127 copied_dwarf_procs);
8129 default:
8130 break;
8136 /* Copy DWARF procedures that are referenced by the DIE tree to TREE_NODE and
8137 rewrite references to point to the copies.
8139 References are looked for in DIE's attributes and recursively in all its
8140 children attributes that are location descriptions. COPIED_DWARF_PROCS is a
8141 mapping from old DWARF procedures to their copy. It is used not to copy
8142 twice the same DWARF procedure under TYPE_NODE. */
8144 static void
8145 copy_dwarf_procs_ref_in_dies (dw_die_ref die,
8146 comdat_type_node *type_node,
8147 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8149 dw_die_ref c;
8151 copy_dwarf_procs_ref_in_attrs (die, type_node, copied_dwarf_procs);
8152 FOR_EACH_CHILD (die, c, copy_dwarf_procs_ref_in_dies (c,
8153 type_node,
8154 copied_dwarf_procs));
8157 /* Traverse the DIE and set up additional .debug_types or .debug_info
8158 DW_UT_*type sections for each type worthy of being placed in a COMDAT
8159 section. */
8161 static void
8162 break_out_comdat_types (dw_die_ref die)
8164 dw_die_ref c;
8165 dw_die_ref first;
8166 dw_die_ref prev = NULL;
8167 dw_die_ref next = NULL;
8168 dw_die_ref unit = NULL;
8170 first = c = die->die_child;
8171 if (c)
8172 next = c->die_sib;
8173 if (c) do {
8174 if (prev == NULL || prev->die_sib == c)
8175 prev = c;
8176 c = next;
8177 next = (c == first ? NULL : c->die_sib);
8178 if (should_move_die_to_comdat (c))
8180 dw_die_ref replacement;
8181 comdat_type_node *type_node;
8183 /* Break out nested types into their own type units. */
8184 break_out_comdat_types (c);
8186 /* Create a new type unit DIE as the root for the new tree, and
8187 add it to the list of comdat types. */
8188 unit = new_die (DW_TAG_type_unit, NULL, NULL);
8189 add_AT_unsigned (unit, DW_AT_language,
8190 get_AT_unsigned (comp_unit_die (), DW_AT_language));
8191 type_node = ggc_cleared_alloc<comdat_type_node> ();
8192 type_node->root_die = unit;
8193 type_node->next = comdat_type_list;
8194 comdat_type_list = type_node;
8196 /* Generate the type signature. */
8197 generate_type_signature (c, type_node);
8199 /* Copy the declaration context, attributes, and children of the
8200 declaration into the new type unit DIE, then remove this DIE
8201 from the main CU (or replace it with a skeleton if necessary). */
8202 replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
8203 type_node->skeleton_die = replacement;
8205 /* Add the DIE to the new compunit. */
8206 add_child_die (unit, c);
8208 /* Types can reference DWARF procedures for type size or data location
8209 expressions. Calls in DWARF expressions cannot target procedures
8210 that are not in the same section. So we must copy DWARF procedures
8211 along with this type and then rewrite references to them. */
8212 hash_map<dw_die_ref, dw_die_ref> copied_dwarf_procs;
8213 copy_dwarf_procs_ref_in_dies (c, type_node, copied_dwarf_procs);
8215 if (replacement != NULL)
8216 c = replacement;
8218 else if (c->die_tag == DW_TAG_namespace
8219 || c->die_tag == DW_TAG_class_type
8220 || c->die_tag == DW_TAG_structure_type
8221 || c->die_tag == DW_TAG_union_type)
8223 /* Look for nested types that can be broken out. */
8224 break_out_comdat_types (c);
8226 } while (next != NULL);
8229 /* Like clone_tree, but copy DW_TAG_subprogram DIEs as declarations.
8230 Enter all the cloned children into the hash table decl_table. */
8232 static dw_die_ref
8233 clone_tree_partial (dw_die_ref die, decl_hash_type *decl_table)
8235 dw_die_ref c;
8236 dw_die_ref clone;
8237 struct decl_table_entry *entry;
8238 decl_table_entry **slot;
8240 if (die->die_tag == DW_TAG_subprogram)
8241 clone = clone_as_declaration (die);
8242 else
8243 clone = clone_die (die);
8245 slot = decl_table->find_slot_with_hash (die,
8246 htab_hash_pointer (die), INSERT);
8248 /* Assert that DIE isn't in the hash table yet. If it would be there
8249 before, the ancestors would be necessarily there as well, therefore
8250 clone_tree_partial wouldn't be called. */
8251 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
8253 entry = XCNEW (struct decl_table_entry);
8254 entry->orig = die;
8255 entry->copy = clone;
8256 *slot = entry;
8258 if (die->die_tag != DW_TAG_subprogram)
8259 FOR_EACH_CHILD (die, c,
8260 add_child_die (clone, clone_tree_partial (c, decl_table)));
8262 return clone;
8265 /* Walk the DIE and its children, looking for references to incomplete
8266 or trivial types that are unmarked (i.e., that are not in the current
8267 type_unit). */
8269 static void
8270 copy_decls_walk (dw_die_ref unit, dw_die_ref die, decl_hash_type *decl_table)
8272 dw_die_ref c;
8273 dw_attr_node *a;
8274 unsigned ix;
8276 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8278 if (AT_class (a) == dw_val_class_die_ref)
8280 dw_die_ref targ = AT_ref (a);
8281 decl_table_entry **slot;
8282 struct decl_table_entry *entry;
8284 if (targ->die_mark != 0 || targ->comdat_type_p)
8285 continue;
8287 slot = decl_table->find_slot_with_hash (targ,
8288 htab_hash_pointer (targ),
8289 INSERT);
8291 if (*slot != HTAB_EMPTY_ENTRY)
8293 /* TARG has already been copied, so we just need to
8294 modify the reference to point to the copy. */
8295 entry = *slot;
8296 a->dw_attr_val.v.val_die_ref.die = entry->copy;
8298 else
8300 dw_die_ref parent = unit;
8301 dw_die_ref copy = clone_die (targ);
8303 /* Record in DECL_TABLE that TARG has been copied.
8304 Need to do this now, before the recursive call,
8305 because DECL_TABLE may be expanded and SLOT
8306 would no longer be a valid pointer. */
8307 entry = XCNEW (struct decl_table_entry);
8308 entry->orig = targ;
8309 entry->copy = copy;
8310 *slot = entry;
8312 /* If TARG is not a declaration DIE, we need to copy its
8313 children. */
8314 if (!is_declaration_die (targ))
8316 FOR_EACH_CHILD (
8317 targ, c,
8318 add_child_die (copy,
8319 clone_tree_partial (c, decl_table)));
8322 /* Make sure the cloned tree is marked as part of the
8323 type unit. */
8324 mark_dies (copy);
8326 /* If TARG has surrounding context, copy its ancestor tree
8327 into the new type unit. */
8328 if (targ->die_parent != NULL
8329 && !is_unit_die (targ->die_parent))
8330 parent = copy_ancestor_tree (unit, targ->die_parent,
8331 decl_table);
8333 add_child_die (parent, copy);
8334 a->dw_attr_val.v.val_die_ref.die = copy;
8336 /* Make sure the newly-copied DIE is walked. If it was
8337 installed in a previously-added context, it won't
8338 get visited otherwise. */
8339 if (parent != unit)
8341 /* Find the highest point of the newly-added tree,
8342 mark each node along the way, and walk from there. */
8343 parent->die_mark = 1;
8344 while (parent->die_parent
8345 && parent->die_parent->die_mark == 0)
8347 parent = parent->die_parent;
8348 parent->die_mark = 1;
8350 copy_decls_walk (unit, parent, decl_table);
8356 FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
8359 /* Copy declarations for "unworthy" types into the new comdat section.
8360 Incomplete types, modified types, and certain other types aren't broken
8361 out into comdat sections of their own, so they don't have a signature,
8362 and we need to copy the declaration into the same section so that we
8363 don't have an external reference. */
8365 static void
8366 copy_decls_for_unworthy_types (dw_die_ref unit)
8368 mark_dies (unit);
8369 decl_hash_type decl_table (10);
8370 copy_decls_walk (unit, unit, &decl_table);
8371 unmark_dies (unit);
8374 /* Traverse the DIE and add a sibling attribute if it may have the
8375 effect of speeding up access to siblings. To save some space,
8376 avoid generating sibling attributes for DIE's without children. */
8378 static void
8379 add_sibling_attributes (dw_die_ref die)
8381 dw_die_ref c;
8383 if (! die->die_child)
8384 return;
8386 if (die->die_parent && die != die->die_parent->die_child)
8387 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
8389 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
8392 /* Output all location lists for the DIE and its children. */
8394 static void
8395 output_location_lists (dw_die_ref die)
8397 dw_die_ref c;
8398 dw_attr_node *a;
8399 unsigned ix;
8401 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8402 if (AT_class (a) == dw_val_class_loc_list)
8403 output_loc_list (AT_loc_list (a));
8405 FOR_EACH_CHILD (die, c, output_location_lists (c));
8408 /* During assign_location_list_indexes and output_loclists_offset the
8409 current index, after it the number of assigned indexes (i.e. how
8410 large the .debug_loclists* offset table should be). */
8411 static unsigned int loc_list_idx;
8413 /* Output all location list offsets for the DIE and its children. */
8415 static void
8416 output_loclists_offsets (dw_die_ref die)
8418 dw_die_ref c;
8419 dw_attr_node *a;
8420 unsigned ix;
8422 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8423 if (AT_class (a) == dw_val_class_loc_list)
8425 dw_loc_list_ref l = AT_loc_list (a);
8426 if (l->offset_emitted)
8427 continue;
8428 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l->ll_symbol,
8429 loc_section_label, NULL);
8430 gcc_assert (l->hash == loc_list_idx);
8431 loc_list_idx++;
8432 l->offset_emitted = true;
8435 FOR_EACH_CHILD (die, c, output_loclists_offsets (c));
8438 /* Recursively set indexes of location lists. */
8440 static void
8441 assign_location_list_indexes (dw_die_ref die)
8443 dw_die_ref c;
8444 dw_attr_node *a;
8445 unsigned ix;
8447 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8448 if (AT_class (a) == dw_val_class_loc_list)
8450 dw_loc_list_ref list = AT_loc_list (a);
8451 if (!list->num_assigned)
8453 list->num_assigned = true;
8454 list->hash = loc_list_idx++;
8458 FOR_EACH_CHILD (die, c, assign_location_list_indexes (c));
8461 /* We want to limit the number of external references, because they are
8462 larger than local references: a relocation takes multiple words, and
8463 even a sig8 reference is always eight bytes, whereas a local reference
8464 can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
8465 So if we encounter multiple external references to the same type DIE, we
8466 make a local typedef stub for it and redirect all references there.
8468 This is the element of the hash table for keeping track of these
8469 references. */
8471 struct external_ref
8473 dw_die_ref type;
8474 dw_die_ref stub;
8475 unsigned n_refs;
8478 /* Hashtable helpers. */
8480 struct external_ref_hasher : free_ptr_hash <external_ref>
8482 static inline hashval_t hash (const external_ref *);
8483 static inline bool equal (const external_ref *, const external_ref *);
8486 inline hashval_t
8487 external_ref_hasher::hash (const external_ref *r)
8489 dw_die_ref die = r->type;
8490 hashval_t h = 0;
8492 /* We can't use the address of the DIE for hashing, because
8493 that will make the order of the stub DIEs non-deterministic. */
8494 if (! die->comdat_type_p)
8495 /* We have a symbol; use it to compute a hash. */
8496 h = htab_hash_string (die->die_id.die_symbol);
8497 else
8499 /* We have a type signature; use a subset of the bits as the hash.
8500 The 8-byte signature is at least as large as hashval_t. */
8501 comdat_type_node *type_node = die->die_id.die_type_node;
8502 memcpy (&h, type_node->signature, sizeof (h));
8504 return h;
8507 inline bool
8508 external_ref_hasher::equal (const external_ref *r1, const external_ref *r2)
8510 return r1->type == r2->type;
8513 typedef hash_table<external_ref_hasher> external_ref_hash_type;
8515 /* Return a pointer to the external_ref for references to DIE. */
8517 static struct external_ref *
8518 lookup_external_ref (external_ref_hash_type *map, dw_die_ref die)
8520 struct external_ref ref, *ref_p;
8521 external_ref **slot;
8523 ref.type = die;
8524 slot = map->find_slot (&ref, INSERT);
8525 if (*slot != HTAB_EMPTY_ENTRY)
8526 return *slot;
8528 ref_p = XCNEW (struct external_ref);
8529 ref_p->type = die;
8530 *slot = ref_p;
8531 return ref_p;
8534 /* Subroutine of optimize_external_refs, below.
8536 If we see a type skeleton, record it as our stub. If we see external
8537 references, remember how many we've seen. */
8539 static void
8540 optimize_external_refs_1 (dw_die_ref die, external_ref_hash_type *map)
8542 dw_die_ref c;
8543 dw_attr_node *a;
8544 unsigned ix;
8545 struct external_ref *ref_p;
8547 if (is_type_die (die)
8548 && (c = get_AT_ref (die, DW_AT_signature)))
8550 /* This is a local skeleton; use it for local references. */
8551 ref_p = lookup_external_ref (map, c);
8552 ref_p->stub = die;
8555 /* Scan the DIE references, and remember any that refer to DIEs from
8556 other CUs (i.e. those which are not marked). */
8557 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8558 if (AT_class (a) == dw_val_class_die_ref
8559 && (c = AT_ref (a))->die_mark == 0
8560 && is_type_die (c))
8562 ref_p = lookup_external_ref (map, c);
8563 ref_p->n_refs++;
8566 FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
8569 /* htab_traverse callback function for optimize_external_refs, below. SLOT
8570 points to an external_ref, DATA is the CU we're processing. If we don't
8571 already have a local stub, and we have multiple refs, build a stub. */
8574 dwarf2_build_local_stub (external_ref **slot, dw_die_ref data)
8576 struct external_ref *ref_p = *slot;
8578 if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
8580 /* We have multiple references to this type, so build a small stub.
8581 Both of these forms are a bit dodgy from the perspective of the
8582 DWARF standard, since technically they should have names. */
8583 dw_die_ref cu = data;
8584 dw_die_ref type = ref_p->type;
8585 dw_die_ref stub = NULL;
8587 if (type->comdat_type_p)
8589 /* If we refer to this type via sig8, use AT_signature. */
8590 stub = new_die (type->die_tag, cu, NULL_TREE);
8591 add_AT_die_ref (stub, DW_AT_signature, type);
8593 else
8595 /* Otherwise, use a typedef with no name. */
8596 stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
8597 add_AT_die_ref (stub, DW_AT_type, type);
8600 stub->die_mark++;
8601 ref_p->stub = stub;
8603 return 1;
8606 /* DIE is a unit; look through all the DIE references to see if there are
8607 any external references to types, and if so, create local stubs for
8608 them which will be applied in build_abbrev_table. This is useful because
8609 references to local DIEs are smaller. */
8611 static external_ref_hash_type *
8612 optimize_external_refs (dw_die_ref die)
8614 external_ref_hash_type *map = new external_ref_hash_type (10);
8615 optimize_external_refs_1 (die, map);
8616 map->traverse <dw_die_ref, dwarf2_build_local_stub> (die);
8617 return map;
8620 /* The following 3 variables are temporaries that are computed only during the
8621 build_abbrev_table call and used and released during the following
8622 optimize_abbrev_table call. */
8624 /* First abbrev_id that can be optimized based on usage. */
8625 static unsigned int abbrev_opt_start;
8627 /* Maximum abbrev_id of a base type plus one (we can't optimize DIEs with
8628 abbrev_id smaller than this, because they must be already sized
8629 during build_abbrev_table). */
8630 static unsigned int abbrev_opt_base_type_end;
8632 /* Vector of usage counts during build_abbrev_table. Indexed by
8633 abbrev_id - abbrev_opt_start. */
8634 static vec<unsigned int> abbrev_usage_count;
8636 /* Vector of all DIEs added with die_abbrev >= abbrev_opt_start. */
8637 static vec<dw_die_ref> sorted_abbrev_dies;
8639 /* The format of each DIE (and its attribute value pairs) is encoded in an
8640 abbreviation table. This routine builds the abbreviation table and assigns
8641 a unique abbreviation id for each abbreviation entry. The children of each
8642 die are visited recursively. */
8644 static void
8645 build_abbrev_table (dw_die_ref die, external_ref_hash_type *extern_map)
8647 unsigned int abbrev_id = 0;
8648 dw_die_ref c;
8649 dw_attr_node *a;
8650 unsigned ix;
8651 dw_die_ref abbrev;
8653 /* Scan the DIE references, and replace any that refer to
8654 DIEs from other CUs (i.e. those which are not marked) with
8655 the local stubs we built in optimize_external_refs. */
8656 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8657 if (AT_class (a) == dw_val_class_die_ref
8658 && (c = AT_ref (a))->die_mark == 0)
8660 struct external_ref *ref_p;
8661 gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
8663 ref_p = lookup_external_ref (extern_map, c);
8664 if (ref_p->stub && ref_p->stub != die)
8665 change_AT_die_ref (a, ref_p->stub);
8666 else
8667 /* We aren't changing this reference, so mark it external. */
8668 set_AT_ref_external (a, 1);
8671 FOR_EACH_VEC_SAFE_ELT (abbrev_die_table, abbrev_id, abbrev)
8673 dw_attr_node *die_a, *abbrev_a;
8674 unsigned ix;
8675 bool ok = true;
8677 if (abbrev_id == 0)
8678 continue;
8679 if (abbrev->die_tag != die->die_tag)
8680 continue;
8681 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
8682 continue;
8684 if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
8685 continue;
8687 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
8689 abbrev_a = &(*abbrev->die_attr)[ix];
8690 if ((abbrev_a->dw_attr != die_a->dw_attr)
8691 || (value_format (abbrev_a) != value_format (die_a)))
8693 ok = false;
8694 break;
8697 if (ok)
8698 break;
8701 if (abbrev_id >= vec_safe_length (abbrev_die_table))
8703 vec_safe_push (abbrev_die_table, die);
8704 if (abbrev_opt_start)
8705 abbrev_usage_count.safe_push (0);
8707 if (abbrev_opt_start && abbrev_id >= abbrev_opt_start)
8709 abbrev_usage_count[abbrev_id - abbrev_opt_start]++;
8710 sorted_abbrev_dies.safe_push (die);
8713 die->die_abbrev = abbrev_id;
8714 FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
8717 /* Callback function for sorted_abbrev_dies vector sorting. We sort
8718 by die_abbrev's usage count, from the most commonly used
8719 abbreviation to the least. */
8721 static int
8722 die_abbrev_cmp (const void *p1, const void *p2)
8724 dw_die_ref die1 = *(const dw_die_ref *) p1;
8725 dw_die_ref die2 = *(const dw_die_ref *) p2;
8727 gcc_checking_assert (die1->die_abbrev >= abbrev_opt_start);
8728 gcc_checking_assert (die2->die_abbrev >= abbrev_opt_start);
8730 if (die1->die_abbrev >= abbrev_opt_base_type_end
8731 && die2->die_abbrev >= abbrev_opt_base_type_end)
8733 if (abbrev_usage_count[die1->die_abbrev - abbrev_opt_start]
8734 > abbrev_usage_count[die2->die_abbrev - abbrev_opt_start])
8735 return -1;
8736 if (abbrev_usage_count[die1->die_abbrev - abbrev_opt_start]
8737 < abbrev_usage_count[die2->die_abbrev - abbrev_opt_start])
8738 return 1;
8741 /* Stabilize the sort. */
8742 if (die1->die_abbrev < die2->die_abbrev)
8743 return -1;
8744 if (die1->die_abbrev > die2->die_abbrev)
8745 return 1;
8747 return 0;
8750 /* Convert dw_val_class_const and dw_val_class_unsigned_const class attributes
8751 of DIEs in between sorted_abbrev_dies[first_id] and abbrev_dies[end_id - 1]
8752 into dw_val_class_const_implicit or
8753 dw_val_class_unsigned_const_implicit. */
8755 static void
8756 optimize_implicit_const (unsigned int first_id, unsigned int end,
8757 vec<bool> &implicit_consts)
8759 /* It never makes sense if there is just one DIE using the abbreviation. */
8760 if (end < first_id + 2)
8761 return;
8763 dw_attr_node *a;
8764 unsigned ix, i;
8765 dw_die_ref die = sorted_abbrev_dies[first_id];
8766 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8767 if (implicit_consts[ix])
8769 enum dw_val_class new_class = dw_val_class_none;
8770 switch (AT_class (a))
8772 case dw_val_class_unsigned_const:
8773 if ((HOST_WIDE_INT) AT_unsigned (a) < 0)
8774 continue;
8776 /* The .debug_abbrev section will grow by
8777 size_of_sleb128 (AT_unsigned (a)) and we avoid the constants
8778 in all the DIEs using that abbreviation. */
8779 if (constant_size (AT_unsigned (a)) * (end - first_id)
8780 <= (unsigned) size_of_sleb128 (AT_unsigned (a)))
8781 continue;
8783 new_class = dw_val_class_unsigned_const_implicit;
8784 break;
8786 case dw_val_class_const:
8787 new_class = dw_val_class_const_implicit;
8788 break;
8790 case dw_val_class_file:
8791 new_class = dw_val_class_file_implicit;
8792 break;
8794 default:
8795 continue;
8797 for (i = first_id; i < end; i++)
8798 (*sorted_abbrev_dies[i]->die_attr)[ix].dw_attr_val.val_class
8799 = new_class;
8803 /* Attempt to optimize abbreviation table from abbrev_opt_start
8804 abbreviation above. */
8806 static void
8807 optimize_abbrev_table (void)
8809 if (abbrev_opt_start
8810 && vec_safe_length (abbrev_die_table) > abbrev_opt_start
8811 && (dwarf_version >= 5 || vec_safe_length (abbrev_die_table) > 127))
8813 auto_vec<bool, 32> implicit_consts;
8814 sorted_abbrev_dies.qsort (die_abbrev_cmp);
8816 unsigned int abbrev_id = abbrev_opt_start - 1;
8817 unsigned int first_id = ~0U;
8818 unsigned int last_abbrev_id = 0;
8819 unsigned int i;
8820 dw_die_ref die;
8821 if (abbrev_opt_base_type_end > abbrev_opt_start)
8822 abbrev_id = abbrev_opt_base_type_end - 1;
8823 /* Reassign abbreviation ids from abbrev_opt_start above, so that
8824 most commonly used abbreviations come first. */
8825 FOR_EACH_VEC_ELT (sorted_abbrev_dies, i, die)
8827 dw_attr_node *a;
8828 unsigned ix;
8830 /* If calc_base_type_die_sizes has been called, the CU and
8831 base types after it can't be optimized, because we've already
8832 calculated their DIE offsets. We've sorted them first. */
8833 if (die->die_abbrev < abbrev_opt_base_type_end)
8834 continue;
8835 if (die->die_abbrev != last_abbrev_id)
8837 last_abbrev_id = die->die_abbrev;
8838 if (dwarf_version >= 5 && first_id != ~0U)
8839 optimize_implicit_const (first_id, i, implicit_consts);
8840 abbrev_id++;
8841 (*abbrev_die_table)[abbrev_id] = die;
8842 if (dwarf_version >= 5)
8844 first_id = i;
8845 implicit_consts.truncate (0);
8847 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8848 switch (AT_class (a))
8850 case dw_val_class_const:
8851 case dw_val_class_unsigned_const:
8852 case dw_val_class_file:
8853 implicit_consts.safe_push (true);
8854 break;
8855 default:
8856 implicit_consts.safe_push (false);
8857 break;
8861 else if (dwarf_version >= 5)
8863 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8864 if (!implicit_consts[ix])
8865 continue;
8866 else
8868 dw_attr_node *other_a
8869 = &(*(*abbrev_die_table)[abbrev_id]->die_attr)[ix];
8870 if (!dw_val_equal_p (&a->dw_attr_val,
8871 &other_a->dw_attr_val))
8872 implicit_consts[ix] = false;
8875 die->die_abbrev = abbrev_id;
8877 gcc_assert (abbrev_id == vec_safe_length (abbrev_die_table) - 1);
8878 if (dwarf_version >= 5 && first_id != ~0U)
8879 optimize_implicit_const (first_id, i, implicit_consts);
8882 abbrev_opt_start = 0;
8883 abbrev_opt_base_type_end = 0;
8884 abbrev_usage_count.release ();
8885 sorted_abbrev_dies.release ();
8888 /* Return the power-of-two number of bytes necessary to represent VALUE. */
8890 static int
8891 constant_size (unsigned HOST_WIDE_INT value)
8893 int log;
8895 if (value == 0)
8896 log = 0;
8897 else
8898 log = floor_log2 (value);
8900 log = log / 8;
8901 log = 1 << (floor_log2 (log) + 1);
8903 return log;
8906 /* Return the size of a DIE as it is represented in the
8907 .debug_info section. */
8909 static unsigned long
8910 size_of_die (dw_die_ref die)
8912 unsigned long size = 0;
8913 dw_attr_node *a;
8914 unsigned ix;
8915 enum dwarf_form form;
8917 size += size_of_uleb128 (die->die_abbrev);
8918 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8920 switch (AT_class (a))
8922 case dw_val_class_addr:
8923 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8925 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8926 size += size_of_uleb128 (AT_index (a));
8928 else
8929 size += DWARF2_ADDR_SIZE;
8930 break;
8931 case dw_val_class_offset:
8932 size += DWARF_OFFSET_SIZE;
8933 break;
8934 case dw_val_class_loc:
8936 unsigned long lsize = size_of_locs (AT_loc (a));
8938 /* Block length. */
8939 if (dwarf_version >= 4)
8940 size += size_of_uleb128 (lsize);
8941 else
8942 size += constant_size (lsize);
8943 size += lsize;
8945 break;
8946 case dw_val_class_loc_list:
8947 if (dwarf_split_debug_info && dwarf_version >= 5)
8949 gcc_assert (AT_loc_list (a)->num_assigned);
8950 size += size_of_uleb128 (AT_loc_list (a)->hash);
8952 else
8953 size += DWARF_OFFSET_SIZE;
8954 break;
8955 case dw_val_class_range_list:
8956 if (value_format (a) == DW_FORM_rnglistx)
8958 gcc_assert (rnglist_idx);
8959 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
8960 size += size_of_uleb128 (r->idx);
8962 else
8963 size += DWARF_OFFSET_SIZE;
8964 break;
8965 case dw_val_class_const:
8966 size += size_of_sleb128 (AT_int (a));
8967 break;
8968 case dw_val_class_unsigned_const:
8970 int csize = constant_size (AT_unsigned (a));
8971 if (dwarf_version == 3
8972 && a->dw_attr == DW_AT_data_member_location
8973 && csize >= 4)
8974 size += size_of_uleb128 (AT_unsigned (a));
8975 else
8976 size += csize;
8978 break;
8979 case dw_val_class_const_implicit:
8980 case dw_val_class_unsigned_const_implicit:
8981 case dw_val_class_file_implicit:
8982 /* These occupy no size in the DIE, just an extra sleb128 in
8983 .debug_abbrev. */
8984 break;
8985 case dw_val_class_const_double:
8986 size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
8987 if (HOST_BITS_PER_WIDE_INT >= DWARF_LARGEST_DATA_FORM_BITS)
8988 size++; /* block */
8989 break;
8990 case dw_val_class_wide_int:
8991 size += (get_full_len (*a->dw_attr_val.v.val_wide)
8992 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
8993 if (get_full_len (*a->dw_attr_val.v.val_wide)
8994 * HOST_BITS_PER_WIDE_INT > DWARF_LARGEST_DATA_FORM_BITS)
8995 size++; /* block */
8996 break;
8997 case dw_val_class_vec:
8998 size += constant_size (a->dw_attr_val.v.val_vec.length
8999 * a->dw_attr_val.v.val_vec.elt_size)
9000 + a->dw_attr_val.v.val_vec.length
9001 * a->dw_attr_val.v.val_vec.elt_size; /* block */
9002 break;
9003 case dw_val_class_flag:
9004 if (dwarf_version >= 4)
9005 /* Currently all add_AT_flag calls pass in 1 as last argument,
9006 so DW_FORM_flag_present can be used. If that ever changes,
9007 we'll need to use DW_FORM_flag and have some optimization
9008 in build_abbrev_table that will change those to
9009 DW_FORM_flag_present if it is set to 1 in all DIEs using
9010 the same abbrev entry. */
9011 gcc_assert (a->dw_attr_val.v.val_flag == 1);
9012 else
9013 size += 1;
9014 break;
9015 case dw_val_class_die_ref:
9016 if (AT_ref_external (a))
9018 /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
9019 we use DW_FORM_ref_addr. In DWARF2, DW_FORM_ref_addr
9020 is sized by target address length, whereas in DWARF3
9021 it's always sized as an offset. */
9022 if (use_debug_types)
9023 size += DWARF_TYPE_SIGNATURE_SIZE;
9024 else if (dwarf_version == 2)
9025 size += DWARF2_ADDR_SIZE;
9026 else
9027 size += DWARF_OFFSET_SIZE;
9029 else
9030 size += DWARF_OFFSET_SIZE;
9031 break;
9032 case dw_val_class_fde_ref:
9033 size += DWARF_OFFSET_SIZE;
9034 break;
9035 case dw_val_class_lbl_id:
9036 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
9038 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
9039 size += size_of_uleb128 (AT_index (a));
9041 else
9042 size += DWARF2_ADDR_SIZE;
9043 break;
9044 case dw_val_class_lineptr:
9045 case dw_val_class_macptr:
9046 case dw_val_class_loclistsptr:
9047 size += DWARF_OFFSET_SIZE;
9048 break;
9049 case dw_val_class_str:
9050 form = AT_string_form (a);
9051 if (form == DW_FORM_strp || form == DW_FORM_line_strp)
9052 size += DWARF_OFFSET_SIZE;
9053 else if (form == DW_FORM_GNU_str_index)
9054 size += size_of_uleb128 (AT_index (a));
9055 else
9056 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
9057 break;
9058 case dw_val_class_file:
9059 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
9060 break;
9061 case dw_val_class_data8:
9062 size += 8;
9063 break;
9064 case dw_val_class_vms_delta:
9065 size += DWARF_OFFSET_SIZE;
9066 break;
9067 case dw_val_class_high_pc:
9068 size += DWARF2_ADDR_SIZE;
9069 break;
9070 case dw_val_class_discr_value:
9071 size += size_of_discr_value (&a->dw_attr_val.v.val_discr_value);
9072 break;
9073 case dw_val_class_discr_list:
9075 unsigned block_size = size_of_discr_list (AT_discr_list (a));
9077 /* This is a block, so we have the block length and then its
9078 data. */
9079 size += constant_size (block_size) + block_size;
9081 break;
9082 default:
9083 gcc_unreachable ();
9087 return size;
9090 /* Size the debugging information associated with a given DIE. Visits the
9091 DIE's children recursively. Updates the global variable next_die_offset, on
9092 each time through. Uses the current value of next_die_offset to update the
9093 die_offset field in each DIE. */
9095 static void
9096 calc_die_sizes (dw_die_ref die)
9098 dw_die_ref c;
9100 gcc_assert (die->die_offset == 0
9101 || (unsigned long int) die->die_offset == next_die_offset);
9102 die->die_offset = next_die_offset;
9103 next_die_offset += size_of_die (die);
9105 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
9107 if (die->die_child != NULL)
9108 /* Count the null byte used to terminate sibling lists. */
9109 next_die_offset += 1;
9112 /* Size just the base type children at the start of the CU.
9113 This is needed because build_abbrev needs to size locs
9114 and sizing of type based stack ops needs to know die_offset
9115 values for the base types. */
9117 static void
9118 calc_base_type_die_sizes (void)
9120 unsigned long die_offset = (dwarf_split_debug_info
9121 ? DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
9122 : DWARF_COMPILE_UNIT_HEADER_SIZE);
9123 unsigned int i;
9124 dw_die_ref base_type;
9125 #if ENABLE_ASSERT_CHECKING
9126 dw_die_ref prev = comp_unit_die ()->die_child;
9127 #endif
9129 die_offset += size_of_die (comp_unit_die ());
9130 for (i = 0; base_types.iterate (i, &base_type); i++)
9132 #if ENABLE_ASSERT_CHECKING
9133 gcc_assert (base_type->die_offset == 0
9134 && prev->die_sib == base_type
9135 && base_type->die_child == NULL
9136 && base_type->die_abbrev);
9137 prev = base_type;
9138 #endif
9139 if (abbrev_opt_start
9140 && base_type->die_abbrev >= abbrev_opt_base_type_end)
9141 abbrev_opt_base_type_end = base_type->die_abbrev + 1;
9142 base_type->die_offset = die_offset;
9143 die_offset += size_of_die (base_type);
9147 /* Set the marks for a die and its children. We do this so
9148 that we know whether or not a reference needs to use FORM_ref_addr; only
9149 DIEs in the same CU will be marked. We used to clear out the offset
9150 and use that as the flag, but ran into ordering problems. */
9152 static void
9153 mark_dies (dw_die_ref die)
9155 dw_die_ref c;
9157 gcc_assert (!die->die_mark);
9159 die->die_mark = 1;
9160 FOR_EACH_CHILD (die, c, mark_dies (c));
9163 /* Clear the marks for a die and its children. */
9165 static void
9166 unmark_dies (dw_die_ref die)
9168 dw_die_ref c;
9170 if (! use_debug_types)
9171 gcc_assert (die->die_mark);
9173 die->die_mark = 0;
9174 FOR_EACH_CHILD (die, c, unmark_dies (c));
9177 /* Clear the marks for a die, its children and referred dies. */
9179 static void
9180 unmark_all_dies (dw_die_ref die)
9182 dw_die_ref c;
9183 dw_attr_node *a;
9184 unsigned ix;
9186 if (!die->die_mark)
9187 return;
9188 die->die_mark = 0;
9190 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
9192 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9193 if (AT_class (a) == dw_val_class_die_ref)
9194 unmark_all_dies (AT_ref (a));
9197 /* Calculate if the entry should appear in the final output file. It may be
9198 from a pruned a type. */
9200 static bool
9201 include_pubname_in_output (vec<pubname_entry, va_gc> *table, pubname_entry *p)
9203 /* By limiting gnu pubnames to definitions only, gold can generate a
9204 gdb index without entries for declarations, which don't include
9205 enough information to be useful. */
9206 if (debug_generate_pub_sections == 2 && is_declaration_die (p->die))
9207 return false;
9209 if (table == pubname_table)
9211 /* Enumerator names are part of the pubname table, but the
9212 parent DW_TAG_enumeration_type die may have been pruned.
9213 Don't output them if that is the case. */
9214 if (p->die->die_tag == DW_TAG_enumerator &&
9215 (p->die->die_parent == NULL
9216 || !p->die->die_parent->die_perennial_p))
9217 return false;
9219 /* Everything else in the pubname table is included. */
9220 return true;
9223 /* The pubtypes table shouldn't include types that have been
9224 pruned. */
9225 return (p->die->die_offset != 0
9226 || !flag_eliminate_unused_debug_types);
9229 /* Return the size of the .debug_pubnames or .debug_pubtypes table
9230 generated for the compilation unit. */
9232 static unsigned long
9233 size_of_pubnames (vec<pubname_entry, va_gc> *names)
9235 unsigned long size;
9236 unsigned i;
9237 pubname_entry *p;
9238 int space_for_flags = (debug_generate_pub_sections == 2) ? 1 : 0;
9240 size = DWARF_PUBNAMES_HEADER_SIZE;
9241 FOR_EACH_VEC_ELT (*names, i, p)
9242 if (include_pubname_in_output (names, p))
9243 size += strlen (p->name) + DWARF_OFFSET_SIZE + 1 + space_for_flags;
9245 size += DWARF_OFFSET_SIZE;
9246 return size;
9249 /* Return the size of the information in the .debug_aranges section. */
9251 static unsigned long
9252 size_of_aranges (void)
9254 unsigned long size;
9256 size = DWARF_ARANGES_HEADER_SIZE;
9258 /* Count the address/length pair for this compilation unit. */
9259 if (text_section_used)
9260 size += 2 * DWARF2_ADDR_SIZE;
9261 if (cold_text_section_used)
9262 size += 2 * DWARF2_ADDR_SIZE;
9263 if (have_multiple_function_sections)
9265 unsigned fde_idx;
9266 dw_fde_ref fde;
9268 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
9270 if (DECL_IGNORED_P (fde->decl))
9271 continue;
9272 if (!fde->in_std_section)
9273 size += 2 * DWARF2_ADDR_SIZE;
9274 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
9275 size += 2 * DWARF2_ADDR_SIZE;
9279 /* Count the two zero words used to terminated the address range table. */
9280 size += 2 * DWARF2_ADDR_SIZE;
9281 return size;
9284 /* Select the encoding of an attribute value. */
9286 static enum dwarf_form
9287 value_format (dw_attr_node *a)
9289 switch (AT_class (a))
9291 case dw_val_class_addr:
9292 /* Only very few attributes allow DW_FORM_addr. */
9293 switch (a->dw_attr)
9295 case DW_AT_low_pc:
9296 case DW_AT_high_pc:
9297 case DW_AT_entry_pc:
9298 case DW_AT_trampoline:
9299 return (AT_index (a) == NOT_INDEXED
9300 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
9301 default:
9302 break;
9304 switch (DWARF2_ADDR_SIZE)
9306 case 1:
9307 return DW_FORM_data1;
9308 case 2:
9309 return DW_FORM_data2;
9310 case 4:
9311 return DW_FORM_data4;
9312 case 8:
9313 return DW_FORM_data8;
9314 default:
9315 gcc_unreachable ();
9317 case dw_val_class_loc_list:
9318 if (dwarf_split_debug_info
9319 && dwarf_version >= 5
9320 && AT_loc_list (a)->num_assigned)
9321 return DW_FORM_loclistx;
9322 /* FALLTHRU */
9323 case dw_val_class_range_list:
9324 /* For range lists in DWARF 5, use DW_FORM_rnglistx from .debug_info.dwo
9325 but in .debug_info use DW_FORM_sec_offset, which is shorter if we
9326 care about sizes of .debug* sections in shared libraries and
9327 executables and don't take into account relocations that affect just
9328 relocatable objects - for DW_FORM_rnglistx we'd have to emit offset
9329 table in the .debug_rnglists section. */
9330 if (dwarf_split_debug_info
9331 && dwarf_version >= 5
9332 && AT_class (a) == dw_val_class_range_list
9333 && rnglist_idx
9334 && a->dw_attr_val.val_entry != RELOCATED_OFFSET)
9335 return DW_FORM_rnglistx;
9336 if (dwarf_version >= 4)
9337 return DW_FORM_sec_offset;
9338 /* FALLTHRU */
9339 case dw_val_class_vms_delta:
9340 case dw_val_class_offset:
9341 switch (DWARF_OFFSET_SIZE)
9343 case 4:
9344 return DW_FORM_data4;
9345 case 8:
9346 return DW_FORM_data8;
9347 default:
9348 gcc_unreachable ();
9350 case dw_val_class_loc:
9351 if (dwarf_version >= 4)
9352 return DW_FORM_exprloc;
9353 switch (constant_size (size_of_locs (AT_loc (a))))
9355 case 1:
9356 return DW_FORM_block1;
9357 case 2:
9358 return DW_FORM_block2;
9359 case 4:
9360 return DW_FORM_block4;
9361 default:
9362 gcc_unreachable ();
9364 case dw_val_class_const:
9365 return DW_FORM_sdata;
9366 case dw_val_class_unsigned_const:
9367 switch (constant_size (AT_unsigned (a)))
9369 case 1:
9370 return DW_FORM_data1;
9371 case 2:
9372 return DW_FORM_data2;
9373 case 4:
9374 /* In DWARF3 DW_AT_data_member_location with
9375 DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
9376 constant, so we need to use DW_FORM_udata if we need
9377 a large constant. */
9378 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
9379 return DW_FORM_udata;
9380 return DW_FORM_data4;
9381 case 8:
9382 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
9383 return DW_FORM_udata;
9384 return DW_FORM_data8;
9385 default:
9386 gcc_unreachable ();
9388 case dw_val_class_const_implicit:
9389 case dw_val_class_unsigned_const_implicit:
9390 case dw_val_class_file_implicit:
9391 return DW_FORM_implicit_const;
9392 case dw_val_class_const_double:
9393 switch (HOST_BITS_PER_WIDE_INT)
9395 case 8:
9396 return DW_FORM_data2;
9397 case 16:
9398 return DW_FORM_data4;
9399 case 32:
9400 return DW_FORM_data8;
9401 case 64:
9402 if (dwarf_version >= 5)
9403 return DW_FORM_data16;
9404 /* FALLTHRU */
9405 default:
9406 return DW_FORM_block1;
9408 case dw_val_class_wide_int:
9409 switch (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT)
9411 case 8:
9412 return DW_FORM_data1;
9413 case 16:
9414 return DW_FORM_data2;
9415 case 32:
9416 return DW_FORM_data4;
9417 case 64:
9418 return DW_FORM_data8;
9419 case 128:
9420 if (dwarf_version >= 5)
9421 return DW_FORM_data16;
9422 /* FALLTHRU */
9423 default:
9424 return DW_FORM_block1;
9426 case dw_val_class_vec:
9427 switch (constant_size (a->dw_attr_val.v.val_vec.length
9428 * a->dw_attr_val.v.val_vec.elt_size))
9430 case 1:
9431 return DW_FORM_block1;
9432 case 2:
9433 return DW_FORM_block2;
9434 case 4:
9435 return DW_FORM_block4;
9436 default:
9437 gcc_unreachable ();
9439 case dw_val_class_flag:
9440 if (dwarf_version >= 4)
9442 /* Currently all add_AT_flag calls pass in 1 as last argument,
9443 so DW_FORM_flag_present can be used. If that ever changes,
9444 we'll need to use DW_FORM_flag and have some optimization
9445 in build_abbrev_table that will change those to
9446 DW_FORM_flag_present if it is set to 1 in all DIEs using
9447 the same abbrev entry. */
9448 gcc_assert (a->dw_attr_val.v.val_flag == 1);
9449 return DW_FORM_flag_present;
9451 return DW_FORM_flag;
9452 case dw_val_class_die_ref:
9453 if (AT_ref_external (a))
9454 return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
9455 else
9456 return DW_FORM_ref;
9457 case dw_val_class_fde_ref:
9458 return DW_FORM_data;
9459 case dw_val_class_lbl_id:
9460 return (AT_index (a) == NOT_INDEXED
9461 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
9462 case dw_val_class_lineptr:
9463 case dw_val_class_macptr:
9464 case dw_val_class_loclistsptr:
9465 return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
9466 case dw_val_class_str:
9467 return AT_string_form (a);
9468 case dw_val_class_file:
9469 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
9471 case 1:
9472 return DW_FORM_data1;
9473 case 2:
9474 return DW_FORM_data2;
9475 case 4:
9476 return DW_FORM_data4;
9477 default:
9478 gcc_unreachable ();
9481 case dw_val_class_data8:
9482 return DW_FORM_data8;
9484 case dw_val_class_high_pc:
9485 switch (DWARF2_ADDR_SIZE)
9487 case 1:
9488 return DW_FORM_data1;
9489 case 2:
9490 return DW_FORM_data2;
9491 case 4:
9492 return DW_FORM_data4;
9493 case 8:
9494 return DW_FORM_data8;
9495 default:
9496 gcc_unreachable ();
9499 case dw_val_class_discr_value:
9500 return (a->dw_attr_val.v.val_discr_value.pos
9501 ? DW_FORM_udata
9502 : DW_FORM_sdata);
9503 case dw_val_class_discr_list:
9504 switch (constant_size (size_of_discr_list (AT_discr_list (a))))
9506 case 1:
9507 return DW_FORM_block1;
9508 case 2:
9509 return DW_FORM_block2;
9510 case 4:
9511 return DW_FORM_block4;
9512 default:
9513 gcc_unreachable ();
9516 default:
9517 gcc_unreachable ();
9521 /* Output the encoding of an attribute value. */
9523 static void
9524 output_value_format (dw_attr_node *a)
9526 enum dwarf_form form = value_format (a);
9528 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
9531 /* Given a die and id, produce the appropriate abbreviations. */
9533 static void
9534 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
9536 unsigned ix;
9537 dw_attr_node *a_attr;
9539 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
9540 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
9541 dwarf_tag_name (abbrev->die_tag));
9543 if (abbrev->die_child != NULL)
9544 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
9545 else
9546 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
9548 for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
9550 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
9551 dwarf_attr_name (a_attr->dw_attr));
9552 output_value_format (a_attr);
9553 if (value_format (a_attr) == DW_FORM_implicit_const)
9555 if (AT_class (a_attr) == dw_val_class_file_implicit)
9557 int f = maybe_emit_file (a_attr->dw_attr_val.v.val_file);
9558 const char *filename = a_attr->dw_attr_val.v.val_file->filename;
9559 dw2_asm_output_data_sleb128 (f, "(%s)", filename);
9561 else
9562 dw2_asm_output_data_sleb128 (a_attr->dw_attr_val.v.val_int, NULL);
9566 dw2_asm_output_data (1, 0, NULL);
9567 dw2_asm_output_data (1, 0, NULL);
9571 /* Output the .debug_abbrev section which defines the DIE abbreviation
9572 table. */
9574 static void
9575 output_abbrev_section (void)
9577 unsigned int abbrev_id;
9578 dw_die_ref abbrev;
9580 FOR_EACH_VEC_SAFE_ELT (abbrev_die_table, abbrev_id, abbrev)
9581 if (abbrev_id != 0)
9582 output_die_abbrevs (abbrev_id, abbrev);
9584 /* Terminate the table. */
9585 dw2_asm_output_data (1, 0, NULL);
9588 /* Output a symbol we can use to refer to this DIE from another CU. */
9590 static inline void
9591 output_die_symbol (dw_die_ref die)
9593 const char *sym = die->die_id.die_symbol;
9595 gcc_assert (!die->comdat_type_p);
9597 if (sym == 0)
9598 return;
9600 if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
9601 /* We make these global, not weak; if the target doesn't support
9602 .linkonce, it doesn't support combining the sections, so debugging
9603 will break. */
9604 targetm.asm_out.globalize_label (asm_out_file, sym);
9606 ASM_OUTPUT_LABEL (asm_out_file, sym);
9609 /* Return a new location list, given the begin and end range, and the
9610 expression. */
9612 static inline dw_loc_list_ref
9613 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
9614 const char *section)
9616 dw_loc_list_ref retlist = ggc_cleared_alloc<dw_loc_list_node> ();
9618 retlist->begin = begin;
9619 retlist->begin_entry = NULL;
9620 retlist->end = end;
9621 retlist->expr = expr;
9622 retlist->section = section;
9624 return retlist;
9627 /* Generate a new internal symbol for this location list node, if it
9628 hasn't got one yet. */
9630 static inline void
9631 gen_llsym (dw_loc_list_ref list)
9633 gcc_assert (!list->ll_symbol);
9634 list->ll_symbol = gen_internal_sym ("LLST");
9637 /* Output the location list given to us. */
9639 static void
9640 output_loc_list (dw_loc_list_ref list_head)
9642 if (list_head->emitted)
9643 return;
9644 list_head->emitted = true;
9646 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
9648 dw_loc_list_ref curr = list_head;
9649 const char *last_section = NULL;
9650 const char *base_label = NULL;
9652 /* Walk the location list, and output each range + expression. */
9653 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
9655 unsigned long size;
9656 /* Don't output an entry that starts and ends at the same address. */
9657 if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
9658 continue;
9659 size = size_of_locs (curr->expr);
9660 /* If the expression is too large, drop it on the floor. We could
9661 perhaps put it into DW_TAG_dwarf_procedure and refer to that
9662 in the expression, but >= 64KB expressions for a single value
9663 in a single range are unlikely very useful. */
9664 if (dwarf_version < 5 && size > 0xffff)
9665 continue;
9666 if (dwarf_version >= 5)
9668 if (dwarf_split_debug_info)
9670 /* For -gsplit-dwarf, emit DW_LLE_starx_length, which has
9671 uleb128 index into .debug_addr and uleb128 length. */
9672 dw2_asm_output_data (1, DW_LLE_startx_length,
9673 "DW_LLE_startx_length (%s)",
9674 list_head->ll_symbol);
9675 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
9676 "Location list range start index "
9677 "(%s)", curr->begin);
9678 /* FIXME: This will ICE ifndef HAVE_AS_LEB128.
9679 For that case we probably need to emit DW_LLE_startx_endx,
9680 but we'd need 2 .debug_addr entries rather than just one. */
9681 dw2_asm_output_delta_uleb128 (curr->end, curr->begin,
9682 "Location list length (%s)",
9683 list_head->ll_symbol);
9685 else if (!have_multiple_function_sections && HAVE_AS_LEB128)
9687 /* If all code is in .text section, the base address is
9688 already provided by the CU attributes. Use
9689 DW_LLE_offset_pair where both addresses are uleb128 encoded
9690 offsets against that base. */
9691 dw2_asm_output_data (1, DW_LLE_offset_pair,
9692 "DW_LLE_offset_pair (%s)",
9693 list_head->ll_symbol);
9694 dw2_asm_output_delta_uleb128 (curr->begin, curr->section,
9695 "Location list begin address (%s)",
9696 list_head->ll_symbol);
9697 dw2_asm_output_delta_uleb128 (curr->end, curr->section,
9698 "Location list end address (%s)",
9699 list_head->ll_symbol);
9701 else if (HAVE_AS_LEB128)
9703 /* Otherwise, find out how many consecutive entries could share
9704 the same base entry. If just one, emit DW_LLE_start_length,
9705 otherwise emit DW_LLE_base_address for the base address
9706 followed by a series of DW_LLE_offset_pair. */
9707 if (last_section == NULL || curr->section != last_section)
9709 dw_loc_list_ref curr2;
9710 for (curr2 = curr->dw_loc_next; curr2 != NULL;
9711 curr2 = curr2->dw_loc_next)
9713 if (strcmp (curr2->begin, curr2->end) == 0
9714 && !curr2->force)
9715 continue;
9716 break;
9718 if (curr2 == NULL || curr->section != curr2->section)
9719 last_section = NULL;
9720 else
9722 last_section = curr->section;
9723 base_label = curr->begin;
9724 dw2_asm_output_data (1, DW_LLE_base_address,
9725 "DW_LLE_base_address (%s)",
9726 list_head->ll_symbol);
9727 dw2_asm_output_addr (DWARF2_ADDR_SIZE, base_label,
9728 "Base address (%s)",
9729 list_head->ll_symbol);
9732 /* Only one entry with the same base address. Use
9733 DW_LLE_start_length with absolute address and uleb128
9734 length. */
9735 if (last_section == NULL)
9737 dw2_asm_output_data (1, DW_LLE_start_length,
9738 "DW_LLE_start_length (%s)",
9739 list_head->ll_symbol);
9740 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
9741 "Location list begin address (%s)",
9742 list_head->ll_symbol);
9743 dw2_asm_output_delta_uleb128 (curr->end, curr->begin,
9744 "Location list length "
9745 "(%s)", list_head->ll_symbol);
9747 /* Otherwise emit DW_LLE_offset_pair, relative to above emitted
9748 DW_LLE_base_address. */
9749 else
9751 dw2_asm_output_data (1, DW_LLE_offset_pair,
9752 "DW_LLE_offset_pair (%s)",
9753 list_head->ll_symbol);
9754 dw2_asm_output_delta_uleb128 (curr->begin, base_label,
9755 "Location list begin address "
9756 "(%s)", list_head->ll_symbol);
9757 dw2_asm_output_delta_uleb128 (curr->end, base_label,
9758 "Location list end address "
9759 "(%s)", list_head->ll_symbol);
9762 /* The assembler does not support .uleb128 directive. Emit
9763 DW_LLE_start_end with a pair of absolute addresses. */
9764 else
9766 dw2_asm_output_data (1, DW_LLE_start_end,
9767 "DW_LLE_start_end (%s)",
9768 list_head->ll_symbol);
9769 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
9770 "Location list begin address (%s)",
9771 list_head->ll_symbol);
9772 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
9773 "Location list end address (%s)",
9774 list_head->ll_symbol);
9777 else if (dwarf_split_debug_info)
9779 /* For -gsplit-dwarf -gdwarf-{2,3,4} emit index into .debug_addr
9780 and 4 byte length. */
9781 dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
9782 "Location list start/length entry (%s)",
9783 list_head->ll_symbol);
9784 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
9785 "Location list range start index (%s)",
9786 curr->begin);
9787 /* The length field is 4 bytes. If we ever need to support
9788 an 8-byte length, we can add a new DW_LLE code or fall back
9789 to DW_LLE_GNU_start_end_entry. */
9790 dw2_asm_output_delta (4, curr->end, curr->begin,
9791 "Location list range length (%s)",
9792 list_head->ll_symbol);
9794 else if (!have_multiple_function_sections)
9796 /* Pair of relative addresses against start of text section. */
9797 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
9798 "Location list begin address (%s)",
9799 list_head->ll_symbol);
9800 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
9801 "Location list end address (%s)",
9802 list_head->ll_symbol);
9804 else
9806 /* Pair of absolute addresses. */
9807 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
9808 "Location list begin address (%s)",
9809 list_head->ll_symbol);
9810 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
9811 "Location list end address (%s)",
9812 list_head->ll_symbol);
9815 /* Output the block length for this list of location operations. */
9816 if (dwarf_version >= 5)
9817 dw2_asm_output_data_uleb128 (size, "Location expression size");
9818 else
9820 gcc_assert (size <= 0xffff);
9821 dw2_asm_output_data (2, size, "Location expression size");
9824 output_loc_sequence (curr->expr, -1);
9827 /* And finally list termination. */
9828 if (dwarf_version >= 5)
9829 dw2_asm_output_data (1, DW_LLE_end_of_list,
9830 "DW_LLE_end_of_list (%s)", list_head->ll_symbol);
9831 else if (dwarf_split_debug_info)
9832 dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
9833 "Location list terminator (%s)",
9834 list_head->ll_symbol);
9835 else
9837 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
9838 "Location list terminator begin (%s)",
9839 list_head->ll_symbol);
9840 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
9841 "Location list terminator end (%s)",
9842 list_head->ll_symbol);
9846 /* Output a range_list offset into the .debug_ranges or .debug_rnglists
9847 section. Emit a relocated reference if val_entry is NULL, otherwise,
9848 emit an indirect reference. */
9850 static void
9851 output_range_list_offset (dw_attr_node *a)
9853 const char *name = dwarf_attr_name (a->dw_attr);
9855 if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
9857 if (dwarf_version >= 5)
9859 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
9860 dw2_asm_output_offset (DWARF_OFFSET_SIZE, r->label,
9861 debug_ranges_section, "%s", name);
9863 else
9865 char *p = strchr (ranges_section_label, '\0');
9866 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
9867 a->dw_attr_val.v.val_offset * 2 * DWARF2_ADDR_SIZE);
9868 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
9869 debug_ranges_section, "%s", name);
9870 *p = '\0';
9873 else if (dwarf_version >= 5)
9875 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
9876 gcc_assert (rnglist_idx);
9877 dw2_asm_output_data_uleb128 (r->idx, "%s", name);
9879 else
9880 dw2_asm_output_data (DWARF_OFFSET_SIZE,
9881 a->dw_attr_val.v.val_offset * 2 * DWARF2_ADDR_SIZE,
9882 "%s (offset from %s)", name, ranges_section_label);
9885 /* Output the offset into the debug_loc section. */
9887 static void
9888 output_loc_list_offset (dw_attr_node *a)
9890 char *sym = AT_loc_list (a)->ll_symbol;
9892 gcc_assert (sym);
9893 if (!dwarf_split_debug_info)
9894 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
9895 "%s", dwarf_attr_name (a->dw_attr));
9896 else if (dwarf_version >= 5)
9898 gcc_assert (AT_loc_list (a)->num_assigned);
9899 dw2_asm_output_data_uleb128 (AT_loc_list (a)->hash, "%s (%s)",
9900 dwarf_attr_name (a->dw_attr),
9901 sym);
9903 else
9904 dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
9905 "%s", dwarf_attr_name (a->dw_attr));
9908 /* Output an attribute's index or value appropriately. */
9910 static void
9911 output_attr_index_or_value (dw_attr_node *a)
9913 const char *name = dwarf_attr_name (a->dw_attr);
9915 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
9917 dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
9918 return;
9920 switch (AT_class (a))
9922 case dw_val_class_addr:
9923 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
9924 break;
9925 case dw_val_class_high_pc:
9926 case dw_val_class_lbl_id:
9927 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
9928 break;
9929 default:
9930 gcc_unreachable ();
9934 /* Output a type signature. */
9936 static inline void
9937 output_signature (const char *sig, const char *name)
9939 int i;
9941 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9942 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
9945 /* Output a discriminant value. */
9947 static inline void
9948 output_discr_value (dw_discr_value *discr_value, const char *name)
9950 if (discr_value->pos)
9951 dw2_asm_output_data_uleb128 (discr_value->v.uval, "%s", name);
9952 else
9953 dw2_asm_output_data_sleb128 (discr_value->v.sval, "%s", name);
9956 /* Output the DIE and its attributes. Called recursively to generate
9957 the definitions of each child DIE. */
9959 static void
9960 output_die (dw_die_ref die)
9962 dw_attr_node *a;
9963 dw_die_ref c;
9964 unsigned long size;
9965 unsigned ix;
9967 /* If someone in another CU might refer to us, set up a symbol for
9968 them to point to. */
9969 if (! die->comdat_type_p && die->die_id.die_symbol)
9970 output_die_symbol (die);
9972 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
9973 (unsigned long)die->die_offset,
9974 dwarf_tag_name (die->die_tag));
9976 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9978 const char *name = dwarf_attr_name (a->dw_attr);
9980 switch (AT_class (a))
9982 case dw_val_class_addr:
9983 output_attr_index_or_value (a);
9984 break;
9986 case dw_val_class_offset:
9987 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
9988 "%s", name);
9989 break;
9991 case dw_val_class_range_list:
9992 output_range_list_offset (a);
9993 break;
9995 case dw_val_class_loc:
9996 size = size_of_locs (AT_loc (a));
9998 /* Output the block length for this list of location operations. */
9999 if (dwarf_version >= 4)
10000 dw2_asm_output_data_uleb128 (size, "%s", name);
10001 else
10002 dw2_asm_output_data (constant_size (size), size, "%s", name);
10004 output_loc_sequence (AT_loc (a), -1);
10005 break;
10007 case dw_val_class_const:
10008 /* ??? It would be slightly more efficient to use a scheme like is
10009 used for unsigned constants below, but gdb 4.x does not sign
10010 extend. Gdb 5.x does sign extend. */
10011 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
10012 break;
10014 case dw_val_class_unsigned_const:
10016 int csize = constant_size (AT_unsigned (a));
10017 if (dwarf_version == 3
10018 && a->dw_attr == DW_AT_data_member_location
10019 && csize >= 4)
10020 dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
10021 else
10022 dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
10024 break;
10026 case dw_val_class_const_implicit:
10027 if (flag_debug_asm)
10028 fprintf (asm_out_file, "\t\t\t%s %s ("
10029 HOST_WIDE_INT_PRINT_DEC ")\n",
10030 ASM_COMMENT_START, name, AT_int (a));
10031 break;
10033 case dw_val_class_unsigned_const_implicit:
10034 if (flag_debug_asm)
10035 fprintf (asm_out_file, "\t\t\t%s %s ("
10036 HOST_WIDE_INT_PRINT_HEX ")\n",
10037 ASM_COMMENT_START, name, AT_unsigned (a));
10038 break;
10040 case dw_val_class_const_double:
10042 unsigned HOST_WIDE_INT first, second;
10044 if (HOST_BITS_PER_WIDE_INT >= DWARF_LARGEST_DATA_FORM_BITS)
10045 dw2_asm_output_data (1,
10046 HOST_BITS_PER_DOUBLE_INT
10047 / HOST_BITS_PER_CHAR,
10048 NULL);
10050 if (WORDS_BIG_ENDIAN)
10052 first = a->dw_attr_val.v.val_double.high;
10053 second = a->dw_attr_val.v.val_double.low;
10055 else
10057 first = a->dw_attr_val.v.val_double.low;
10058 second = a->dw_attr_val.v.val_double.high;
10061 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
10062 first, "%s", name);
10063 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
10064 second, NULL);
10066 break;
10068 case dw_val_class_wide_int:
10070 int i;
10071 int len = get_full_len (*a->dw_attr_val.v.val_wide);
10072 int l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
10073 if (len * HOST_BITS_PER_WIDE_INT > DWARF_LARGEST_DATA_FORM_BITS)
10074 dw2_asm_output_data (1, get_full_len (*a->dw_attr_val.v.val_wide)
10075 * l, NULL);
10077 if (WORDS_BIG_ENDIAN)
10078 for (i = len - 1; i >= 0; --i)
10080 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
10081 "%s", name);
10082 name = "";
10084 else
10085 for (i = 0; i < len; ++i)
10087 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
10088 "%s", name);
10089 name = "";
10092 break;
10094 case dw_val_class_vec:
10096 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
10097 unsigned int len = a->dw_attr_val.v.val_vec.length;
10098 unsigned int i;
10099 unsigned char *p;
10101 dw2_asm_output_data (constant_size (len * elt_size),
10102 len * elt_size, "%s", name);
10103 if (elt_size > sizeof (HOST_WIDE_INT))
10105 elt_size /= 2;
10106 len *= 2;
10108 for (i = 0, p = (unsigned char *) a->dw_attr_val.v.val_vec.array;
10109 i < len;
10110 i++, p += elt_size)
10111 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
10112 "fp or vector constant word %u", i);
10113 break;
10116 case dw_val_class_flag:
10117 if (dwarf_version >= 4)
10119 /* Currently all add_AT_flag calls pass in 1 as last argument,
10120 so DW_FORM_flag_present can be used. If that ever changes,
10121 we'll need to use DW_FORM_flag and have some optimization
10122 in build_abbrev_table that will change those to
10123 DW_FORM_flag_present if it is set to 1 in all DIEs using
10124 the same abbrev entry. */
10125 gcc_assert (AT_flag (a) == 1);
10126 if (flag_debug_asm)
10127 fprintf (asm_out_file, "\t\t\t%s %s\n",
10128 ASM_COMMENT_START, name);
10129 break;
10131 dw2_asm_output_data (1, AT_flag (a), "%s", name);
10132 break;
10134 case dw_val_class_loc_list:
10135 output_loc_list_offset (a);
10136 break;
10138 case dw_val_class_die_ref:
10139 if (AT_ref_external (a))
10141 if (AT_ref (a)->comdat_type_p)
10143 comdat_type_node *type_node
10144 = AT_ref (a)->die_id.die_type_node;
10146 gcc_assert (type_node);
10147 output_signature (type_node->signature, name);
10149 else
10151 const char *sym = AT_ref (a)->die_id.die_symbol;
10152 int size;
10154 gcc_assert (sym);
10155 /* In DWARF2, DW_FORM_ref_addr is sized by target address
10156 length, whereas in DWARF3 it's always sized as an
10157 offset. */
10158 if (dwarf_version == 2)
10159 size = DWARF2_ADDR_SIZE;
10160 else
10161 size = DWARF_OFFSET_SIZE;
10162 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
10163 name);
10166 else
10168 gcc_assert (AT_ref (a)->die_offset);
10169 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
10170 "%s", name);
10172 break;
10174 case dw_val_class_fde_ref:
10176 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
10178 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
10179 a->dw_attr_val.v.val_fde_index * 2);
10180 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
10181 "%s", name);
10183 break;
10185 case dw_val_class_vms_delta:
10186 #ifdef ASM_OUTPUT_DWARF_VMS_DELTA
10187 dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
10188 AT_vms_delta2 (a), AT_vms_delta1 (a),
10189 "%s", name);
10190 #else
10191 dw2_asm_output_delta (DWARF_OFFSET_SIZE,
10192 AT_vms_delta2 (a), AT_vms_delta1 (a),
10193 "%s", name);
10194 #endif
10195 break;
10197 case dw_val_class_lbl_id:
10198 output_attr_index_or_value (a);
10199 break;
10201 case dw_val_class_lineptr:
10202 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
10203 debug_line_section, "%s", name);
10204 break;
10206 case dw_val_class_macptr:
10207 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
10208 debug_macinfo_section, "%s", name);
10209 break;
10211 case dw_val_class_loclistsptr:
10212 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
10213 debug_loc_section, "%s", name);
10214 break;
10216 case dw_val_class_str:
10217 if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
10218 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
10219 a->dw_attr_val.v.val_str->label,
10220 debug_str_section,
10221 "%s: \"%s\"", name, AT_string (a));
10222 else if (a->dw_attr_val.v.val_str->form == DW_FORM_line_strp)
10223 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
10224 a->dw_attr_val.v.val_str->label,
10225 debug_line_str_section,
10226 "%s: \"%s\"", name, AT_string (a));
10227 else if (a->dw_attr_val.v.val_str->form == DW_FORM_GNU_str_index)
10228 dw2_asm_output_data_uleb128 (AT_index (a),
10229 "%s: \"%s\"", name, AT_string (a));
10230 else
10231 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
10232 break;
10234 case dw_val_class_file:
10236 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
10238 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
10239 a->dw_attr_val.v.val_file->filename);
10240 break;
10243 case dw_val_class_file_implicit:
10244 if (flag_debug_asm)
10245 fprintf (asm_out_file, "\t\t\t%s %s (%d, %s)\n",
10246 ASM_COMMENT_START, name,
10247 maybe_emit_file (a->dw_attr_val.v.val_file),
10248 a->dw_attr_val.v.val_file->filename);
10249 break;
10251 case dw_val_class_data8:
10253 int i;
10255 for (i = 0; i < 8; i++)
10256 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
10257 i == 0 ? "%s" : NULL, name);
10258 break;
10261 case dw_val_class_high_pc:
10262 dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
10263 get_AT_low_pc (die), "DW_AT_high_pc");
10264 break;
10266 case dw_val_class_discr_value:
10267 output_discr_value (&a->dw_attr_val.v.val_discr_value, name);
10268 break;
10270 case dw_val_class_discr_list:
10272 dw_discr_list_ref list = AT_discr_list (a);
10273 const int size = size_of_discr_list (list);
10275 /* This is a block, so output its length first. */
10276 dw2_asm_output_data (constant_size (size), size,
10277 "%s: block size", name);
10279 for (; list != NULL; list = list->dw_discr_next)
10281 /* One byte for the discriminant value descriptor, and then as
10282 many LEB128 numbers as required. */
10283 if (list->dw_discr_range)
10284 dw2_asm_output_data (1, DW_DSC_range,
10285 "%s: DW_DSC_range", name);
10286 else
10287 dw2_asm_output_data (1, DW_DSC_label,
10288 "%s: DW_DSC_label", name);
10290 output_discr_value (&list->dw_discr_lower_bound, name);
10291 if (list->dw_discr_range)
10292 output_discr_value (&list->dw_discr_upper_bound, name);
10294 break;
10297 default:
10298 gcc_unreachable ();
10302 FOR_EACH_CHILD (die, c, output_die (c));
10304 /* Add null byte to terminate sibling list. */
10305 if (die->die_child != NULL)
10306 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
10307 (unsigned long) die->die_offset);
10310 /* Output the compilation unit that appears at the beginning of the
10311 .debug_info section, and precedes the DIE descriptions. */
10313 static void
10314 output_compilation_unit_header (enum dwarf_unit_type ut)
10316 if (!XCOFF_DEBUGGING_INFO)
10318 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10319 dw2_asm_output_data (4, 0xffffffff,
10320 "Initial length escape value indicating 64-bit DWARF extension");
10321 dw2_asm_output_data (DWARF_OFFSET_SIZE,
10322 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
10323 "Length of Compilation Unit Info");
10326 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
10327 if (dwarf_version >= 5)
10329 const char *name;
10330 switch (ut)
10332 case DW_UT_compile: name = "DW_UT_compile"; break;
10333 case DW_UT_type: name = "DW_UT_type"; break;
10334 case DW_UT_split_compile: name = "DW_UT_split_compile"; break;
10335 case DW_UT_split_type: name = "DW_UT_split_type"; break;
10336 default: gcc_unreachable ();
10338 dw2_asm_output_data (1, ut, "%s", name);
10339 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10341 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
10342 debug_abbrev_section,
10343 "Offset Into Abbrev. Section");
10344 if (dwarf_version < 5)
10345 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10348 /* Output the compilation unit DIE and its children. */
10350 static void
10351 output_comp_unit (dw_die_ref die, int output_if_empty,
10352 const unsigned char *dwo_id)
10354 const char *secname, *oldsym;
10355 char *tmp;
10357 /* Unless we are outputting main CU, we may throw away empty ones. */
10358 if (!output_if_empty && die->die_child == NULL)
10359 return;
10361 /* Even if there are no children of this DIE, we must output the information
10362 about the compilation unit. Otherwise, on an empty translation unit, we
10363 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
10364 will then complain when examining the file. First mark all the DIEs in
10365 this CU so we know which get local refs. */
10366 mark_dies (die);
10368 external_ref_hash_type *extern_map = optimize_external_refs (die);
10370 /* For now, optimize only the main CU, in order to optimize the rest
10371 we'd need to see all of them earlier. Leave the rest for post-linking
10372 tools like DWZ. */
10373 if (die == comp_unit_die ())
10374 abbrev_opt_start = vec_safe_length (abbrev_die_table);
10376 build_abbrev_table (die, extern_map);
10378 optimize_abbrev_table ();
10380 delete extern_map;
10382 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
10383 next_die_offset = (dwo_id
10384 ? DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
10385 : DWARF_COMPILE_UNIT_HEADER_SIZE);
10386 calc_die_sizes (die);
10388 oldsym = die->die_id.die_symbol;
10389 if (oldsym)
10391 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
10393 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
10394 secname = tmp;
10395 die->die_id.die_symbol = NULL;
10396 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
10398 else
10400 switch_to_section (debug_info_section);
10401 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
10402 info_section_emitted = true;
10405 /* Output debugging information. */
10406 output_compilation_unit_header (dwo_id
10407 ? DW_UT_split_compile : DW_UT_compile);
10408 if (dwarf_version >= 5)
10410 if (dwo_id != NULL)
10411 for (int i = 0; i < 8; i++)
10412 dw2_asm_output_data (1, dwo_id[i], i == 0 ? "DWO id" : NULL);
10414 output_die (die);
10416 /* Leave the marks on the main CU, so we can check them in
10417 output_pubnames. */
10418 if (oldsym)
10420 unmark_dies (die);
10421 die->die_id.die_symbol = oldsym;
10425 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
10426 and .debug_pubtypes. This is configured per-target, but can be
10427 overridden by the -gpubnames or -gno-pubnames options. */
10429 static inline bool
10430 want_pubnames (void)
10432 if (debug_info_level <= DINFO_LEVEL_TERSE)
10433 return false;
10434 if (debug_generate_pub_sections != -1)
10435 return debug_generate_pub_sections;
10436 return targetm.want_debug_pub_sections;
10439 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes. */
10441 static void
10442 add_AT_pubnames (dw_die_ref die)
10444 if (want_pubnames ())
10445 add_AT_flag (die, DW_AT_GNU_pubnames, 1);
10448 /* Add a string attribute value to a skeleton DIE. */
10450 static inline void
10451 add_skeleton_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
10452 const char *str)
10454 dw_attr_node attr;
10455 struct indirect_string_node *node;
10457 if (! skeleton_debug_str_hash)
10458 skeleton_debug_str_hash
10459 = hash_table<indirect_string_hasher>::create_ggc (10);
10461 node = find_AT_string_in_table (str, skeleton_debug_str_hash);
10462 find_string_form (node);
10463 if (node->form == DW_FORM_GNU_str_index)
10464 node->form = DW_FORM_strp;
10466 attr.dw_attr = attr_kind;
10467 attr.dw_attr_val.val_class = dw_val_class_str;
10468 attr.dw_attr_val.val_entry = NULL;
10469 attr.dw_attr_val.v.val_str = node;
10470 add_dwarf_attr (die, &attr);
10473 /* Helper function to generate top-level dies for skeleton debug_info and
10474 debug_types. */
10476 static void
10477 add_top_level_skeleton_die_attrs (dw_die_ref die)
10479 const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
10480 const char *comp_dir = comp_dir_string ();
10482 add_skeleton_AT_string (die, dwarf_AT (DW_AT_dwo_name), dwo_file_name);
10483 if (comp_dir != NULL)
10484 add_skeleton_AT_string (die, DW_AT_comp_dir, comp_dir);
10485 add_AT_pubnames (die);
10486 add_AT_lineptr (die, DW_AT_GNU_addr_base, debug_addr_section_label);
10489 /* Output skeleton debug sections that point to the dwo file. */
10491 static void
10492 output_skeleton_debug_sections (dw_die_ref comp_unit,
10493 const unsigned char *dwo_id)
10495 /* These attributes will be found in the full debug_info section. */
10496 remove_AT (comp_unit, DW_AT_producer);
10497 remove_AT (comp_unit, DW_AT_language);
10499 switch_to_section (debug_skeleton_info_section);
10500 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
10502 /* Produce the skeleton compilation-unit header. This one differs enough from
10503 a normal CU header that it's better not to call output_compilation_unit
10504 header. */
10505 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10506 dw2_asm_output_data (4, 0xffffffff,
10507 "Initial length escape value indicating 64-bit "
10508 "DWARF extension");
10510 dw2_asm_output_data (DWARF_OFFSET_SIZE,
10511 DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
10512 - DWARF_INITIAL_LENGTH_SIZE
10513 + size_of_die (comp_unit),
10514 "Length of Compilation Unit Info");
10515 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
10516 if (dwarf_version >= 5)
10518 dw2_asm_output_data (1, DW_UT_skeleton, "DW_UT_skeleton");
10519 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10521 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_abbrev_section_label,
10522 debug_skeleton_abbrev_section,
10523 "Offset Into Abbrev. Section");
10524 if (dwarf_version < 5)
10525 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10526 else
10527 for (int i = 0; i < 8; i++)
10528 dw2_asm_output_data (1, dwo_id[i], i == 0 ? "DWO id" : NULL);
10530 comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
10531 output_die (comp_unit);
10533 /* Build the skeleton debug_abbrev section. */
10534 switch_to_section (debug_skeleton_abbrev_section);
10535 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
10537 output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
10539 dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
10542 /* Output a comdat type unit DIE and its children. */
10544 static void
10545 output_comdat_type_unit (comdat_type_node *node)
10547 const char *secname;
10548 char *tmp;
10549 int i;
10550 #if defined (OBJECT_FORMAT_ELF)
10551 tree comdat_key;
10552 #endif
10554 /* First mark all the DIEs in this CU so we know which get local refs. */
10555 mark_dies (node->root_die);
10557 external_ref_hash_type *extern_map = optimize_external_refs (node->root_die);
10559 build_abbrev_table (node->root_die, extern_map);
10561 delete extern_map;
10562 extern_map = NULL;
10564 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
10565 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
10566 calc_die_sizes (node->root_die);
10568 #if defined (OBJECT_FORMAT_ELF)
10569 if (dwarf_version >= 5)
10571 if (!dwarf_split_debug_info)
10572 secname = ".debug_info";
10573 else
10574 secname = ".debug_info.dwo";
10576 else if (!dwarf_split_debug_info)
10577 secname = ".debug_types";
10578 else
10579 secname = ".debug_types.dwo";
10581 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
10582 sprintf (tmp, dwarf_version >= 5 ? "wi." : "wt.");
10583 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
10584 sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
10585 comdat_key = get_identifier (tmp);
10586 targetm.asm_out.named_section (secname,
10587 SECTION_DEBUG | SECTION_LINKONCE,
10588 comdat_key);
10589 #else
10590 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
10591 sprintf (tmp, (dwarf_version >= 5
10592 ? ".gnu.linkonce.wi." : ".gnu.linkonce.wt."));
10593 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
10594 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
10595 secname = tmp;
10596 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
10597 #endif
10599 /* Output debugging information. */
10600 output_compilation_unit_header (dwarf_split_debug_info
10601 ? DW_UT_split_type : DW_UT_type);
10602 output_signature (node->signature, "Type Signature");
10603 dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
10604 "Offset to Type DIE");
10605 output_die (node->root_die);
10607 unmark_dies (node->root_die);
10610 /* Return the DWARF2/3 pubname associated with a decl. */
10612 static const char *
10613 dwarf2_name (tree decl, int scope)
10615 if (DECL_NAMELESS (decl))
10616 return NULL;
10617 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
10620 /* Add a new entry to .debug_pubnames if appropriate. */
10622 static void
10623 add_pubname_string (const char *str, dw_die_ref die)
10625 pubname_entry e;
10627 e.die = die;
10628 e.name = xstrdup (str);
10629 vec_safe_push (pubname_table, e);
10632 static void
10633 add_pubname (tree decl, dw_die_ref die)
10635 if (!want_pubnames ())
10636 return;
10638 /* Don't add items to the table when we expect that the consumer will have
10639 just read the enclosing die. For example, if the consumer is looking at a
10640 class_member, it will either be inside the class already, or will have just
10641 looked up the class to find the member. Either way, searching the class is
10642 faster than searching the index. */
10643 if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
10644 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
10646 const char *name = dwarf2_name (decl, 1);
10648 if (name)
10649 add_pubname_string (name, die);
10653 /* Add an enumerator to the pubnames section. */
10655 static void
10656 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
10658 pubname_entry e;
10660 gcc_assert (scope_name);
10661 e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
10662 e.die = die;
10663 vec_safe_push (pubname_table, e);
10666 /* Add a new entry to .debug_pubtypes if appropriate. */
10668 static void
10669 add_pubtype (tree decl, dw_die_ref die)
10671 pubname_entry e;
10673 if (!want_pubnames ())
10674 return;
10676 if ((TREE_PUBLIC (decl)
10677 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
10678 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
10680 tree scope = NULL;
10681 const char *scope_name = "";
10682 const char *sep = is_cxx () ? "::" : ".";
10683 const char *name;
10685 scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
10686 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
10688 scope_name = lang_hooks.dwarf_name (scope, 1);
10689 if (scope_name != NULL && scope_name[0] != '\0')
10690 scope_name = concat (scope_name, sep, NULL);
10691 else
10692 scope_name = "";
10695 if (TYPE_P (decl))
10696 name = type_tag (decl);
10697 else
10698 name = lang_hooks.dwarf_name (decl, 1);
10700 /* If we don't have a name for the type, there's no point in adding
10701 it to the table. */
10702 if (name != NULL && name[0] != '\0')
10704 e.die = die;
10705 e.name = concat (scope_name, name, NULL);
10706 vec_safe_push (pubtype_table, e);
10709 /* Although it might be more consistent to add the pubinfo for the
10710 enumerators as their dies are created, they should only be added if the
10711 enum type meets the criteria above. So rather than re-check the parent
10712 enum type whenever an enumerator die is created, just output them all
10713 here. This isn't protected by the name conditional because anonymous
10714 enums don't have names. */
10715 if (die->die_tag == DW_TAG_enumeration_type)
10717 dw_die_ref c;
10719 FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
10724 /* Output a single entry in the pubnames table. */
10726 static void
10727 output_pubname (dw_offset die_offset, pubname_entry *entry)
10729 dw_die_ref die = entry->die;
10730 int is_static = get_AT_flag (die, DW_AT_external) ? 0 : 1;
10732 dw2_asm_output_data (DWARF_OFFSET_SIZE, die_offset, "DIE offset");
10734 if (debug_generate_pub_sections == 2)
10736 /* This logic follows gdb's method for determining the value of the flag
10737 byte. */
10738 uint32_t flags = GDB_INDEX_SYMBOL_KIND_NONE;
10739 switch (die->die_tag)
10741 case DW_TAG_typedef:
10742 case DW_TAG_base_type:
10743 case DW_TAG_subrange_type:
10744 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
10745 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
10746 break;
10747 case DW_TAG_enumerator:
10748 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
10749 GDB_INDEX_SYMBOL_KIND_VARIABLE);
10750 if (!is_cxx () && !is_java ())
10751 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
10752 break;
10753 case DW_TAG_subprogram:
10754 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
10755 GDB_INDEX_SYMBOL_KIND_FUNCTION);
10756 if (!is_ada ())
10757 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
10758 break;
10759 case DW_TAG_constant:
10760 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
10761 GDB_INDEX_SYMBOL_KIND_VARIABLE);
10762 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
10763 break;
10764 case DW_TAG_variable:
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_namespace:
10770 case DW_TAG_imported_declaration:
10771 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
10772 break;
10773 case DW_TAG_class_type:
10774 case DW_TAG_interface_type:
10775 case DW_TAG_structure_type:
10776 case DW_TAG_union_type:
10777 case DW_TAG_enumeration_type:
10778 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
10779 if (!is_cxx () && !is_java ())
10780 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
10781 break;
10782 default:
10783 /* An unusual tag. Leave the flag-byte empty. */
10784 break;
10786 dw2_asm_output_data (1, flags >> GDB_INDEX_CU_BITSIZE,
10787 "GDB-index flags");
10790 dw2_asm_output_nstring (entry->name, -1, "external name");
10794 /* Output the public names table used to speed up access to externally
10795 visible names; or the public types table used to find type definitions. */
10797 static void
10798 output_pubnames (vec<pubname_entry, va_gc> *names)
10800 unsigned i;
10801 unsigned long pubnames_length = size_of_pubnames (names);
10802 pubname_entry *pub;
10804 if (!XCOFF_DEBUGGING_INFO)
10806 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10807 dw2_asm_output_data (4, 0xffffffff,
10808 "Initial length escape value indicating 64-bit DWARF extension");
10809 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
10810 "Pub Info Length");
10813 /* Version number for pubnames/pubtypes is independent of dwarf version. */
10814 dw2_asm_output_data (2, 2, "DWARF Version");
10816 if (dwarf_split_debug_info)
10817 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
10818 debug_skeleton_info_section,
10819 "Offset of Compilation Unit Info");
10820 else
10821 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
10822 debug_info_section,
10823 "Offset of Compilation Unit Info");
10824 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
10825 "Compilation Unit Length");
10827 FOR_EACH_VEC_ELT (*names, i, pub)
10829 if (include_pubname_in_output (names, pub))
10831 dw_offset die_offset = pub->die->die_offset;
10833 /* We shouldn't see pubnames for DIEs outside of the main CU. */
10834 if (names == pubname_table && pub->die->die_tag != DW_TAG_enumerator)
10835 gcc_assert (pub->die->die_mark);
10837 /* If we're putting types in their own .debug_types sections,
10838 the .debug_pubtypes table will still point to the compile
10839 unit (not the type unit), so we want to use the offset of
10840 the skeleton DIE (if there is one). */
10841 if (pub->die->comdat_type_p && names == pubtype_table)
10843 comdat_type_node *type_node = pub->die->die_id.die_type_node;
10845 if (type_node != NULL)
10846 die_offset = (type_node->skeleton_die != NULL
10847 ? type_node->skeleton_die->die_offset
10848 : comp_unit_die ()->die_offset);
10851 output_pubname (die_offset, pub);
10855 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
10858 /* Output public names and types tables if necessary. */
10860 static void
10861 output_pubtables (void)
10863 if (!want_pubnames () || !info_section_emitted)
10864 return;
10866 switch_to_section (debug_pubnames_section);
10867 output_pubnames (pubname_table);
10868 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
10869 It shouldn't hurt to emit it always, since pure DWARF2 consumers
10870 simply won't look for the section. */
10871 switch_to_section (debug_pubtypes_section);
10872 output_pubnames (pubtype_table);
10876 /* Output the information that goes into the .debug_aranges table.
10877 Namely, define the beginning and ending address range of the
10878 text section generated for this compilation unit. */
10880 static void
10881 output_aranges (void)
10883 unsigned i;
10884 unsigned long aranges_length = size_of_aranges ();
10886 if (!XCOFF_DEBUGGING_INFO)
10888 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10889 dw2_asm_output_data (4, 0xffffffff,
10890 "Initial length escape value indicating 64-bit DWARF extension");
10891 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
10892 "Length of Address Ranges Info");
10895 /* Version number for aranges is still 2, even up to DWARF5. */
10896 dw2_asm_output_data (2, 2, "DWARF Version");
10897 if (dwarf_split_debug_info)
10898 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
10899 debug_skeleton_info_section,
10900 "Offset of Compilation Unit Info");
10901 else
10902 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
10903 debug_info_section,
10904 "Offset of Compilation Unit Info");
10905 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
10906 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
10908 /* We need to align to twice the pointer size here. */
10909 if (DWARF_ARANGES_PAD_SIZE)
10911 /* Pad using a 2 byte words so that padding is correct for any
10912 pointer size. */
10913 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
10914 2 * DWARF2_ADDR_SIZE);
10915 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
10916 dw2_asm_output_data (2, 0, NULL);
10919 /* It is necessary not to output these entries if the sections were
10920 not used; if the sections were not used, the length will be 0 and
10921 the address may end up as 0 if the section is discarded by ld
10922 --gc-sections, leaving an invalid (0, 0) entry that can be
10923 confused with the terminator. */
10924 if (text_section_used)
10926 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
10927 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
10928 text_section_label, "Length");
10930 if (cold_text_section_used)
10932 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
10933 "Address");
10934 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
10935 cold_text_section_label, "Length");
10938 if (have_multiple_function_sections)
10940 unsigned fde_idx;
10941 dw_fde_ref fde;
10943 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
10945 if (DECL_IGNORED_P (fde->decl))
10946 continue;
10947 if (!fde->in_std_section)
10949 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
10950 "Address");
10951 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
10952 fde->dw_fde_begin, "Length");
10954 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
10956 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
10957 "Address");
10958 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
10959 fde->dw_fde_second_begin, "Length");
10964 /* Output the terminator words. */
10965 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
10966 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
10969 /* Add a new entry to .debug_ranges. Return its index into
10970 ranges_table vector. */
10972 static unsigned int
10973 add_ranges_num (int num, bool maybe_new_sec)
10975 dw_ranges r = { NULL, num, 0, maybe_new_sec };
10976 vec_safe_push (ranges_table, r);
10977 return vec_safe_length (ranges_table) - 1;
10980 /* Add a new entry to .debug_ranges corresponding to a block, or a
10981 range terminator if BLOCK is NULL. MAYBE_NEW_SEC is true if
10982 this entry might be in a different section from previous range. */
10984 static unsigned int
10985 add_ranges (const_tree block, bool maybe_new_sec)
10987 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0, maybe_new_sec);
10990 /* Note that (*rnglist_table)[offset] is either a head of a rnglist
10991 chain, or middle entry of a chain that will be directly referred to. */
10993 static void
10994 note_rnglist_head (unsigned int offset)
10996 if (dwarf_version < 5 || (*ranges_table)[offset].label)
10997 return;
10998 (*ranges_table)[offset].label = gen_internal_sym ("LLRL");
11001 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
11002 When using dwarf_split_debug_info, address attributes in dies destined
11003 for the final executable should be direct references--setting the
11004 parameter force_direct ensures this behavior. */
11006 static void
11007 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
11008 bool *added, bool force_direct)
11010 unsigned int in_use = vec_safe_length (ranges_by_label);
11011 unsigned int offset;
11012 dw_ranges_by_label rbl = { begin, end };
11013 vec_safe_push (ranges_by_label, rbl);
11014 offset = add_ranges_num (-(int)in_use - 1, true);
11015 if (!*added)
11017 add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
11018 *added = true;
11019 note_rnglist_head (offset);
11023 /* Emit .debug_ranges section. */
11025 static void
11026 output_ranges (void)
11028 unsigned i;
11029 static const char *const start_fmt = "Offset %#x";
11030 const char *fmt = start_fmt;
11031 dw_ranges *r;
11033 switch_to_section (debug_ranges_section);
11034 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
11035 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11037 int block_num = r->num;
11039 if (block_num > 0)
11041 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
11042 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
11044 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
11045 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
11047 /* If all code is in the text section, then the compilation
11048 unit base address defaults to DW_AT_low_pc, which is the
11049 base of the text section. */
11050 if (!have_multiple_function_sections)
11052 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
11053 text_section_label,
11054 fmt, i * 2 * DWARF2_ADDR_SIZE);
11055 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
11056 text_section_label, NULL);
11059 /* Otherwise, the compilation unit base address is zero,
11060 which allows us to use absolute addresses, and not worry
11061 about whether the target supports cross-section
11062 arithmetic. */
11063 else
11065 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11066 fmt, i * 2 * DWARF2_ADDR_SIZE);
11067 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
11070 fmt = NULL;
11073 /* Negative block_num stands for an index into ranges_by_label. */
11074 else if (block_num < 0)
11076 int lab_idx = - block_num - 1;
11078 if (!have_multiple_function_sections)
11080 gcc_unreachable ();
11081 #if 0
11082 /* If we ever use add_ranges_by_labels () for a single
11083 function section, all we have to do is to take out
11084 the #if 0 above. */
11085 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11086 (*ranges_by_label)[lab_idx].begin,
11087 text_section_label,
11088 fmt, i * 2 * DWARF2_ADDR_SIZE);
11089 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11090 (*ranges_by_label)[lab_idx].end,
11091 text_section_label, NULL);
11092 #endif
11094 else
11096 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11097 (*ranges_by_label)[lab_idx].begin,
11098 fmt, i * 2 * DWARF2_ADDR_SIZE);
11099 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11100 (*ranges_by_label)[lab_idx].end,
11101 NULL);
11104 else
11106 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11107 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11108 fmt = start_fmt;
11113 /* Non-zero if .debug_line_str should be used for .debug_line section
11114 strings or strings that are likely shareable with those. */
11115 #define DWARF5_USE_DEBUG_LINE_STR \
11116 (!DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET \
11117 && (DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) != 0 \
11118 /* FIXME: there is no .debug_line_str.dwo section, \
11119 for -gsplit-dwarf we should use DW_FORM_strx instead. */ \
11120 && !dwarf_split_debug_info)
11122 /* Assign .debug_rnglists indexes. */
11124 static void
11125 index_rnglists (void)
11127 unsigned i;
11128 dw_ranges *r;
11130 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11131 if (r->label)
11132 r->idx = rnglist_idx++;
11135 /* Emit .debug_rnglists section. */
11137 static void
11138 output_rnglists (void)
11140 unsigned i;
11141 dw_ranges *r;
11142 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
11143 char l2[MAX_ARTIFICIAL_LABEL_BYTES];
11144 char basebuf[MAX_ARTIFICIAL_LABEL_BYTES];
11146 switch_to_section (debug_ranges_section);
11147 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
11148 ASM_GENERATE_INTERNAL_LABEL (l1, DEBUG_RANGES_SECTION_LABEL, 2);
11149 ASM_GENERATE_INTERNAL_LABEL (l2, DEBUG_RANGES_SECTION_LABEL, 3);
11150 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11151 dw2_asm_output_data (4, 0xffffffff,
11152 "Initial length escape value indicating "
11153 "64-bit DWARF extension");
11154 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
11155 "Length of Range Lists");
11156 ASM_OUTPUT_LABEL (asm_out_file, l1);
11157 dw2_asm_output_data (2, dwarf_version, "DWARF Version");
11158 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
11159 dw2_asm_output_data (1, 0, "Segment Size");
11160 /* Emit the offset table only for -gsplit-dwarf. If we don't care
11161 about relocation sizes and primarily care about the size of .debug*
11162 sections in linked shared libraries and executables, then
11163 the offset table plus corresponding DW_FORM_rnglistx uleb128 indexes
11164 into it are usually larger than just DW_FORM_sec_offset offsets
11165 into the .debug_rnglists section. */
11166 dw2_asm_output_data (4, dwarf_split_debug_info ? rnglist_idx : 0,
11167 "Offset Entry Count");
11168 if (dwarf_split_debug_info)
11170 ASM_OUTPUT_LABEL (asm_out_file, ranges_base_label);
11171 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11172 if (r->label)
11173 dw2_asm_output_delta (DWARF_OFFSET_SIZE, r->label,
11174 ranges_base_label, NULL);
11177 const char *lab = "";
11178 unsigned int len = vec_safe_length (ranges_table);
11179 const char *base = NULL;
11180 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11182 int block_num = r->num;
11184 if (r->label)
11186 ASM_OUTPUT_LABEL (asm_out_file, r->label);
11187 lab = r->label;
11189 if (HAVE_AS_LEB128 && (r->label || r->maybe_new_sec))
11190 base = NULL;
11191 if (block_num > 0)
11193 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
11194 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
11196 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
11197 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
11199 if (HAVE_AS_LEB128)
11201 /* If all code is in the text section, then the compilation
11202 unit base address defaults to DW_AT_low_pc, which is the
11203 base of the text section. */
11204 if (!have_multiple_function_sections)
11206 dw2_asm_output_data (1, DW_RLE_offset_pair,
11207 "DW_RLE_offset_pair (%s)", lab);
11208 dw2_asm_output_delta_uleb128 (blabel, text_section_label,
11209 "Range begin address (%s)", lab);
11210 dw2_asm_output_delta_uleb128 (elabel, text_section_label,
11211 "Range end address (%s)", lab);
11212 continue;
11214 if (base == NULL)
11216 dw_ranges *r2 = NULL;
11217 if (i < len - 1)
11218 r2 = &(*ranges_table)[i + 1];
11219 if (r2
11220 && r2->num != 0
11221 && r2->label == NULL
11222 && !r2->maybe_new_sec)
11224 dw2_asm_output_data (1, DW_RLE_base_address,
11225 "DW_RLE_base_address (%s)", lab);
11226 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11227 "Base address (%s)", lab);
11228 strcpy (basebuf, blabel);
11229 base = basebuf;
11232 if (base)
11234 dw2_asm_output_data (1, DW_RLE_offset_pair,
11235 "DW_RLE_offset_pair (%s)", lab);
11236 dw2_asm_output_delta_uleb128 (blabel, base,
11237 "Range begin address (%s)", lab);
11238 dw2_asm_output_delta_uleb128 (elabel, base,
11239 "Range end address (%s)", lab);
11240 continue;
11242 dw2_asm_output_data (1, DW_RLE_start_length,
11243 "DW_RLE_start_length (%s)", lab);
11244 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11245 "Range begin address (%s)", lab);
11246 dw2_asm_output_delta_uleb128 (elabel, blabel,
11247 "Range length (%s)", lab);
11249 else
11251 dw2_asm_output_data (1, DW_RLE_start_end,
11252 "DW_RLE_start_end (%s)", lab);
11253 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11254 "Range begin address (%s)", lab);
11255 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel,
11256 "Range end address (%s)", lab);
11260 /* Negative block_num stands for an index into ranges_by_label. */
11261 else if (block_num < 0)
11263 int lab_idx = - block_num - 1;
11264 const char *blabel = (*ranges_by_label)[lab_idx].begin;
11265 const char *elabel = (*ranges_by_label)[lab_idx].end;
11267 if (!have_multiple_function_sections)
11268 gcc_unreachable ();
11269 if (HAVE_AS_LEB128)
11271 dw2_asm_output_data (1, DW_RLE_start_length,
11272 "DW_RLE_start_length (%s)", lab);
11273 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11274 "Range begin address (%s)", lab);
11275 dw2_asm_output_delta_uleb128 (elabel, blabel,
11276 "Range length (%s)", lab);
11278 else
11280 dw2_asm_output_data (1, DW_RLE_start_end,
11281 "DW_RLE_start_end (%s)", lab);
11282 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11283 "Range begin address (%s)", lab);
11284 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel,
11285 "Range end address (%s)", lab);
11288 else
11289 dw2_asm_output_data (1, DW_RLE_end_of_list,
11290 "DW_RLE_end_of_list (%s)", lab);
11292 ASM_OUTPUT_LABEL (asm_out_file, l2);
11295 /* Data structure containing information about input files. */
11296 struct file_info
11298 const char *path; /* Complete file name. */
11299 const char *fname; /* File name part. */
11300 int length; /* Length of entire string. */
11301 struct dwarf_file_data * file_idx; /* Index in input file table. */
11302 int dir_idx; /* Index in directory table. */
11305 /* Data structure containing information about directories with source
11306 files. */
11307 struct dir_info
11309 const char *path; /* Path including directory name. */
11310 int length; /* Path length. */
11311 int prefix; /* Index of directory entry which is a prefix. */
11312 int count; /* Number of files in this directory. */
11313 int dir_idx; /* Index of directory used as base. */
11316 /* Callback function for file_info comparison. We sort by looking at
11317 the directories in the path. */
11319 static int
11320 file_info_cmp (const void *p1, const void *p2)
11322 const struct file_info *const s1 = (const struct file_info *) p1;
11323 const struct file_info *const s2 = (const struct file_info *) p2;
11324 const unsigned char *cp1;
11325 const unsigned char *cp2;
11327 /* Take care of file names without directories. We need to make sure that
11328 we return consistent values to qsort since some will get confused if
11329 we return the same value when identical operands are passed in opposite
11330 orders. So if neither has a directory, return 0 and otherwise return
11331 1 or -1 depending on which one has the directory. */
11332 if ((s1->path == s1->fname || s2->path == s2->fname))
11333 return (s2->path == s2->fname) - (s1->path == s1->fname);
11335 cp1 = (const unsigned char *) s1->path;
11336 cp2 = (const unsigned char *) s2->path;
11338 while (1)
11340 ++cp1;
11341 ++cp2;
11342 /* Reached the end of the first path? If so, handle like above. */
11343 if ((cp1 == (const unsigned char *) s1->fname)
11344 || (cp2 == (const unsigned char *) s2->fname))
11345 return ((cp2 == (const unsigned char *) s2->fname)
11346 - (cp1 == (const unsigned char *) s1->fname));
11348 /* Character of current path component the same? */
11349 else if (*cp1 != *cp2)
11350 return *cp1 - *cp2;
11354 struct file_name_acquire_data
11356 struct file_info *files;
11357 int used_files;
11358 int max_files;
11361 /* Traversal function for the hash table. */
11364 file_name_acquire (dwarf_file_data **slot, file_name_acquire_data *fnad)
11366 struct dwarf_file_data *d = *slot;
11367 struct file_info *fi;
11368 const char *f;
11370 gcc_assert (fnad->max_files >= d->emitted_number);
11372 if (! d->emitted_number)
11373 return 1;
11375 gcc_assert (fnad->max_files != fnad->used_files);
11377 fi = fnad->files + fnad->used_files++;
11379 /* Skip all leading "./". */
11380 f = d->filename;
11381 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
11382 f += 2;
11384 /* Create a new array entry. */
11385 fi->path = f;
11386 fi->length = strlen (f);
11387 fi->file_idx = d;
11389 /* Search for the file name part. */
11390 f = strrchr (f, DIR_SEPARATOR);
11391 #if defined (DIR_SEPARATOR_2)
11393 char *g = strrchr (fi->path, DIR_SEPARATOR_2);
11395 if (g != NULL)
11397 if (f == NULL || f < g)
11398 f = g;
11401 #endif
11403 fi->fname = f == NULL ? fi->path : f + 1;
11404 return 1;
11407 /* Helper function for output_file_names. Emit a FORM encoded
11408 string STR, with assembly comment start ENTRY_KIND and
11409 index IDX */
11411 static void
11412 output_line_string (enum dwarf_form form, const char *str,
11413 const char *entry_kind, unsigned int idx)
11415 switch (form)
11417 case DW_FORM_string:
11418 dw2_asm_output_nstring (str, -1, "%s: %#x", entry_kind, idx);
11419 break;
11420 case DW_FORM_line_strp:
11421 if (!debug_line_str_hash)
11422 debug_line_str_hash
11423 = hash_table<indirect_string_hasher>::create_ggc (10);
11425 struct indirect_string_node *node;
11426 node = find_AT_string_in_table (str, debug_line_str_hash);
11427 set_indirect_string (node);
11428 node->form = form;
11429 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
11430 debug_line_str_section, "%s: %#x: \"%s\"",
11431 entry_kind, 0, node->str);
11432 break;
11433 default:
11434 gcc_unreachable ();
11438 /* Output the directory table and the file name table. We try to minimize
11439 the total amount of memory needed. A heuristic is used to avoid large
11440 slowdowns with many input files. */
11442 static void
11443 output_file_names (void)
11445 struct file_name_acquire_data fnad;
11446 int numfiles;
11447 struct file_info *files;
11448 struct dir_info *dirs;
11449 int *saved;
11450 int *savehere;
11451 int *backmap;
11452 int ndirs;
11453 int idx_offset;
11454 int i;
11456 if (!last_emitted_file)
11458 if (dwarf_version >= 5)
11460 dw2_asm_output_data (1, 0, "Directory entry format count");
11461 dw2_asm_output_data_uleb128 (0, "Directories count");
11462 dw2_asm_output_data (1, 0, "File name entry format count");
11463 dw2_asm_output_data_uleb128 (0, "File names count");
11465 else
11467 dw2_asm_output_data (1, 0, "End directory table");
11468 dw2_asm_output_data (1, 0, "End file name table");
11470 return;
11473 numfiles = last_emitted_file->emitted_number;
11475 /* Allocate the various arrays we need. */
11476 files = XALLOCAVEC (struct file_info, numfiles);
11477 dirs = XALLOCAVEC (struct dir_info, numfiles);
11479 fnad.files = files;
11480 fnad.used_files = 0;
11481 fnad.max_files = numfiles;
11482 file_table->traverse<file_name_acquire_data *, file_name_acquire> (&fnad);
11483 gcc_assert (fnad.used_files == fnad.max_files);
11485 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
11487 /* Find all the different directories used. */
11488 dirs[0].path = files[0].path;
11489 dirs[0].length = files[0].fname - files[0].path;
11490 dirs[0].prefix = -1;
11491 dirs[0].count = 1;
11492 dirs[0].dir_idx = 0;
11493 files[0].dir_idx = 0;
11494 ndirs = 1;
11496 for (i = 1; i < numfiles; i++)
11497 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
11498 && memcmp (dirs[ndirs - 1].path, files[i].path,
11499 dirs[ndirs - 1].length) == 0)
11501 /* Same directory as last entry. */
11502 files[i].dir_idx = ndirs - 1;
11503 ++dirs[ndirs - 1].count;
11505 else
11507 int j;
11509 /* This is a new directory. */
11510 dirs[ndirs].path = files[i].path;
11511 dirs[ndirs].length = files[i].fname - files[i].path;
11512 dirs[ndirs].count = 1;
11513 dirs[ndirs].dir_idx = ndirs;
11514 files[i].dir_idx = ndirs;
11516 /* Search for a prefix. */
11517 dirs[ndirs].prefix = -1;
11518 for (j = 0; j < ndirs; j++)
11519 if (dirs[j].length < dirs[ndirs].length
11520 && dirs[j].length > 1
11521 && (dirs[ndirs].prefix == -1
11522 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
11523 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
11524 dirs[ndirs].prefix = j;
11526 ++ndirs;
11529 /* Now to the actual work. We have to find a subset of the directories which
11530 allow expressing the file name using references to the directory table
11531 with the least amount of characters. We do not do an exhaustive search
11532 where we would have to check out every combination of every single
11533 possible prefix. Instead we use a heuristic which provides nearly optimal
11534 results in most cases and never is much off. */
11535 saved = XALLOCAVEC (int, ndirs);
11536 savehere = XALLOCAVEC (int, ndirs);
11538 memset (saved, '\0', ndirs * sizeof (saved[0]));
11539 for (i = 0; i < ndirs; i++)
11541 int j;
11542 int total;
11544 /* We can always save some space for the current directory. But this
11545 does not mean it will be enough to justify adding the directory. */
11546 savehere[i] = dirs[i].length;
11547 total = (savehere[i] - saved[i]) * dirs[i].count;
11549 for (j = i + 1; j < ndirs; j++)
11551 savehere[j] = 0;
11552 if (saved[j] < dirs[i].length)
11554 /* Determine whether the dirs[i] path is a prefix of the
11555 dirs[j] path. */
11556 int k;
11558 k = dirs[j].prefix;
11559 while (k != -1 && k != (int) i)
11560 k = dirs[k].prefix;
11562 if (k == (int) i)
11564 /* Yes it is. We can possibly save some memory by
11565 writing the filenames in dirs[j] relative to
11566 dirs[i]. */
11567 savehere[j] = dirs[i].length;
11568 total += (savehere[j] - saved[j]) * dirs[j].count;
11573 /* Check whether we can save enough to justify adding the dirs[i]
11574 directory. */
11575 if (total > dirs[i].length + 1)
11577 /* It's worthwhile adding. */
11578 for (j = i; j < ndirs; j++)
11579 if (savehere[j] > 0)
11581 /* Remember how much we saved for this directory so far. */
11582 saved[j] = savehere[j];
11584 /* Remember the prefix directory. */
11585 dirs[j].dir_idx = i;
11590 /* Emit the directory name table. */
11591 idx_offset = dirs[0].length > 0 ? 1 : 0;
11592 enum dwarf_form str_form = DW_FORM_string;
11593 enum dwarf_form idx_form = DW_FORM_udata;
11594 if (dwarf_version >= 5)
11596 const char *comp_dir = comp_dir_string ();
11597 if (comp_dir == NULL)
11598 comp_dir = "";
11599 dw2_asm_output_data (1, 1, "Directory entry format count");
11600 if (DWARF5_USE_DEBUG_LINE_STR)
11601 str_form = DW_FORM_line_strp;
11602 dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
11603 dw2_asm_output_data_uleb128 (str_form, "%s",
11604 get_DW_FORM_name (str_form));
11605 dw2_asm_output_data_uleb128 (ndirs + idx_offset, "Directories count");
11606 if (str_form == DW_FORM_string)
11608 dw2_asm_output_nstring (comp_dir, -1, "Directory Entry: %#x", 0);
11609 for (i = 1 - idx_offset; i < ndirs; i++)
11610 dw2_asm_output_nstring (dirs[i].path,
11611 dirs[i].length
11612 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
11613 "Directory Entry: %#x", i + idx_offset);
11615 else
11617 output_line_string (str_form, comp_dir, "Directory Entry", 0);
11618 for (i = 1 - idx_offset; i < ndirs; i++)
11620 const char *str
11621 = ggc_alloc_string (dirs[i].path,
11622 dirs[i].length
11623 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR);
11624 output_line_string (str_form, str, "Directory Entry",
11625 (unsigned) i + idx_offset);
11629 else
11631 for (i = 1 - idx_offset; i < ndirs; i++)
11632 dw2_asm_output_nstring (dirs[i].path,
11633 dirs[i].length
11634 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
11635 "Directory Entry: %#x", i + idx_offset);
11637 dw2_asm_output_data (1, 0, "End directory table");
11640 /* We have to emit them in the order of emitted_number since that's
11641 used in the debug info generation. To do this efficiently we
11642 generate a back-mapping of the indices first. */
11643 backmap = XALLOCAVEC (int, numfiles);
11644 for (i = 0; i < numfiles; i++)
11645 backmap[files[i].file_idx->emitted_number - 1] = i;
11647 if (dwarf_version >= 5)
11649 const char *filename0 = get_AT_string (comp_unit_die (), DW_AT_name);
11650 if (filename0 == NULL)
11651 filename0 = "";
11652 /* DW_LNCT_directory_index can use DW_FORM_udata, DW_FORM_data1 and
11653 DW_FORM_data2. Choose one based on the number of directories
11654 and how much space would they occupy in each encoding.
11655 If we have at most 256 directories, all indexes fit into
11656 a single byte, so DW_FORM_data1 is most compact (if there
11657 are at most 128 directories, DW_FORM_udata would be as
11658 compact as that, but not shorter and slower to decode). */
11659 if (ndirs + idx_offset <= 256)
11660 idx_form = DW_FORM_data1;
11661 /* If there are more than 65536 directories, we have to use
11662 DW_FORM_udata, DW_FORM_data2 can't refer to them.
11663 Otherwise, compute what space would occupy if all the indexes
11664 used DW_FORM_udata - sum - and compare that to how large would
11665 be DW_FORM_data2 encoding, and pick the more efficient one. */
11666 else if (ndirs + idx_offset <= 65536)
11668 unsigned HOST_WIDE_INT sum = 1;
11669 for (i = 0; i < numfiles; i++)
11671 int file_idx = backmap[i];
11672 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
11673 sum += size_of_uleb128 (dir_idx);
11675 if (sum >= HOST_WIDE_INT_UC (2) * (numfiles + 1))
11676 idx_form = DW_FORM_data2;
11678 #ifdef VMS_DEBUGGING_INFO
11679 dw2_asm_output_data (1, 4, "File name entry format count");
11680 #else
11681 dw2_asm_output_data (1, 2, "File name entry format count");
11682 #endif
11683 dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
11684 dw2_asm_output_data_uleb128 (str_form, "%s",
11685 get_DW_FORM_name (str_form));
11686 dw2_asm_output_data_uleb128 (DW_LNCT_directory_index,
11687 "DW_LNCT_directory_index");
11688 dw2_asm_output_data_uleb128 (idx_form, "%s",
11689 get_DW_FORM_name (idx_form));
11690 #ifdef VMS_DEBUGGING_INFO
11691 dw2_asm_output_data_uleb128 (DW_LNCT_timestamp, "DW_LNCT_timestamp");
11692 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
11693 dw2_asm_output_data_uleb128 (DW_LNCT_size, "DW_LNCT_size");
11694 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
11695 #endif
11696 dw2_asm_output_data_uleb128 (numfiles + 1, "File names count");
11698 output_line_string (str_form, filename0, "File Entry", 0);
11700 /* Include directory index. */
11701 if (dwarf_version >= 5 && idx_form != DW_FORM_udata)
11702 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
11703 0, NULL);
11704 else
11705 dw2_asm_output_data_uleb128 (0, NULL);
11707 #ifdef VMS_DEBUGGING_INFO
11708 dw2_asm_output_data_uleb128 (0, NULL);
11709 dw2_asm_output_data_uleb128 (0, NULL);
11710 #endif
11713 /* Now write all the file names. */
11714 for (i = 0; i < numfiles; i++)
11716 int file_idx = backmap[i];
11717 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
11719 #ifdef VMS_DEBUGGING_INFO
11720 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
11722 /* Setting these fields can lead to debugger miscomparisons,
11723 but VMS Debug requires them to be set correctly. */
11725 int ver;
11726 long long cdt;
11727 long siz;
11728 int maxfilelen = (strlen (files[file_idx].path)
11729 + dirs[dir_idx].length
11730 + MAX_VMS_VERSION_LEN + 1);
11731 char *filebuf = XALLOCAVEC (char, maxfilelen);
11733 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
11734 snprintf (filebuf, maxfilelen, "%s;%d",
11735 files[file_idx].path + dirs[dir_idx].length, ver);
11737 output_line_string (str_form, filebuf, "File Entry", (unsigned) i + 1);
11739 /* Include directory index. */
11740 if (dwarf_version >= 5 && idx_form != DW_FORM_udata)
11741 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
11742 dir_idx + idx_offset, NULL);
11743 else
11744 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
11746 /* Modification time. */
11747 dw2_asm_output_data_uleb128 ((vms_file_stats_name (files[file_idx].path,
11748 &cdt, 0, 0, 0) == 0)
11749 ? cdt : 0, NULL);
11751 /* File length in bytes. */
11752 dw2_asm_output_data_uleb128 ((vms_file_stats_name (files[file_idx].path,
11753 0, &siz, 0, 0) == 0)
11754 ? siz : 0, NULL);
11755 #else
11756 output_line_string (str_form,
11757 files[file_idx].path + dirs[dir_idx].length,
11758 "File Entry", (unsigned) i + 1);
11760 /* Include directory index. */
11761 if (dwarf_version >= 5 && idx_form != DW_FORM_udata)
11762 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
11763 dir_idx + idx_offset, NULL);
11764 else
11765 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
11767 if (dwarf_version >= 5)
11768 continue;
11770 /* Modification time. */
11771 dw2_asm_output_data_uleb128 (0, NULL);
11773 /* File length in bytes. */
11774 dw2_asm_output_data_uleb128 (0, NULL);
11775 #endif /* VMS_DEBUGGING_INFO */
11778 if (dwarf_version < 5)
11779 dw2_asm_output_data (1, 0, "End file name table");
11783 /* Output one line number table into the .debug_line section. */
11785 static void
11786 output_one_line_info_table (dw_line_info_table *table)
11788 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
11789 unsigned int current_line = 1;
11790 bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
11791 dw_line_info_entry *ent;
11792 size_t i;
11794 FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
11796 switch (ent->opcode)
11798 case LI_set_address:
11799 /* ??? Unfortunately, we have little choice here currently, and
11800 must always use the most general form. GCC does not know the
11801 address delta itself, so we can't use DW_LNS_advance_pc. Many
11802 ports do have length attributes which will give an upper bound
11803 on the address range. We could perhaps use length attributes
11804 to determine when it is safe to use DW_LNS_fixed_advance_pc. */
11805 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
11807 /* This can handle any delta. This takes
11808 4+DWARF2_ADDR_SIZE bytes. */
11809 dw2_asm_output_data (1, 0, "set address %s", line_label);
11810 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
11811 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
11812 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
11813 break;
11815 case LI_set_line:
11816 if (ent->val == current_line)
11818 /* We still need to start a new row, so output a copy insn. */
11819 dw2_asm_output_data (1, DW_LNS_copy,
11820 "copy line %u", current_line);
11822 else
11824 int line_offset = ent->val - current_line;
11825 int line_delta = line_offset - DWARF_LINE_BASE;
11827 current_line = ent->val;
11828 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
11830 /* This can handle deltas from -10 to 234, using the current
11831 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
11832 This takes 1 byte. */
11833 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
11834 "line %u", current_line);
11836 else
11838 /* This can handle any delta. This takes at least 4 bytes,
11839 depending on the value being encoded. */
11840 dw2_asm_output_data (1, DW_LNS_advance_line,
11841 "advance to line %u", current_line);
11842 dw2_asm_output_data_sleb128 (line_offset, NULL);
11843 dw2_asm_output_data (1, DW_LNS_copy, NULL);
11846 break;
11848 case LI_set_file:
11849 dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
11850 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
11851 break;
11853 case LI_set_column:
11854 dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
11855 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
11856 break;
11858 case LI_negate_stmt:
11859 current_is_stmt = !current_is_stmt;
11860 dw2_asm_output_data (1, DW_LNS_negate_stmt,
11861 "is_stmt %d", current_is_stmt);
11862 break;
11864 case LI_set_prologue_end:
11865 dw2_asm_output_data (1, DW_LNS_set_prologue_end,
11866 "set prologue end");
11867 break;
11869 case LI_set_epilogue_begin:
11870 dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
11871 "set epilogue begin");
11872 break;
11874 case LI_set_discriminator:
11875 dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
11876 dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
11877 dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
11878 dw2_asm_output_data_uleb128 (ent->val, NULL);
11879 break;
11883 /* Emit debug info for the address of the end of the table. */
11884 dw2_asm_output_data (1, 0, "set address %s", table->end_label);
11885 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
11886 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
11887 dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
11889 dw2_asm_output_data (1, 0, "end sequence");
11890 dw2_asm_output_data_uleb128 (1, NULL);
11891 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
11894 /* Output the source line number correspondence information. This
11895 information goes into the .debug_line section. */
11897 static void
11898 output_line_info (bool prologue_only)
11900 static unsigned int generation;
11901 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
11902 char p1[MAX_ARTIFICIAL_LABEL_BYTES], p2[MAX_ARTIFICIAL_LABEL_BYTES];
11903 bool saw_one = false;
11904 int opc;
11906 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, generation);
11907 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, generation);
11908 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, generation);
11909 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, generation++);
11911 if (!XCOFF_DEBUGGING_INFO)
11913 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11914 dw2_asm_output_data (4, 0xffffffff,
11915 "Initial length escape value indicating 64-bit DWARF extension");
11916 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
11917 "Length of Source Line Info");
11920 ASM_OUTPUT_LABEL (asm_out_file, l1);
11922 dw2_asm_output_data (2, dwarf_version, "DWARF Version");
11923 if (dwarf_version >= 5)
11925 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
11926 dw2_asm_output_data (1, 0, "Segment Size");
11928 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
11929 ASM_OUTPUT_LABEL (asm_out_file, p1);
11931 /* Define the architecture-dependent minimum instruction length (in bytes).
11932 In this implementation of DWARF, this field is used for information
11933 purposes only. Since GCC generates assembly language, we have no
11934 a priori knowledge of how many instruction bytes are generated for each
11935 source line, and therefore can use only the DW_LNE_set_address and
11936 DW_LNS_fixed_advance_pc line information commands. Accordingly, we fix
11937 this as '1', which is "correct enough" for all architectures,
11938 and don't let the target override. */
11939 dw2_asm_output_data (1, 1, "Minimum Instruction Length");
11941 if (dwarf_version >= 4)
11942 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
11943 "Maximum Operations Per Instruction");
11944 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
11945 "Default is_stmt_start flag");
11946 dw2_asm_output_data (1, DWARF_LINE_BASE,
11947 "Line Base Value (Special Opcodes)");
11948 dw2_asm_output_data (1, DWARF_LINE_RANGE,
11949 "Line Range Value (Special Opcodes)");
11950 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
11951 "Special Opcode Base");
11953 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
11955 int n_op_args;
11956 switch (opc)
11958 case DW_LNS_advance_pc:
11959 case DW_LNS_advance_line:
11960 case DW_LNS_set_file:
11961 case DW_LNS_set_column:
11962 case DW_LNS_fixed_advance_pc:
11963 case DW_LNS_set_isa:
11964 n_op_args = 1;
11965 break;
11966 default:
11967 n_op_args = 0;
11968 break;
11971 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
11972 opc, n_op_args);
11975 /* Write out the information about the files we use. */
11976 output_file_names ();
11977 ASM_OUTPUT_LABEL (asm_out_file, p2);
11978 if (prologue_only)
11980 /* Output the marker for the end of the line number info. */
11981 ASM_OUTPUT_LABEL (asm_out_file, l2);
11982 return;
11985 if (separate_line_info)
11987 dw_line_info_table *table;
11988 size_t i;
11990 FOR_EACH_VEC_ELT (*separate_line_info, i, table)
11991 if (table->in_use)
11993 output_one_line_info_table (table);
11994 saw_one = true;
11997 if (cold_text_section_line_info && cold_text_section_line_info->in_use)
11999 output_one_line_info_table (cold_text_section_line_info);
12000 saw_one = true;
12003 /* ??? Some Darwin linkers crash on a .debug_line section with no
12004 sequences. Further, merely a DW_LNE_end_sequence entry is not
12005 sufficient -- the address column must also be initialized.
12006 Make sure to output at least one set_address/end_sequence pair,
12007 choosing .text since that section is always present. */
12008 if (text_section_line_info->in_use || !saw_one)
12009 output_one_line_info_table (text_section_line_info);
12011 /* Output the marker for the end of the line number info. */
12012 ASM_OUTPUT_LABEL (asm_out_file, l2);
12015 /* Return true if DW_AT_endianity should be emitted according to REVERSE. */
12017 static inline bool
12018 need_endianity_attribute_p (bool reverse)
12020 return reverse && (dwarf_version >= 3 || !dwarf_strict);
12023 /* Given a pointer to a tree node for some base type, return a pointer to
12024 a DIE that describes the given type. REVERSE is true if the type is
12025 to be interpreted in the reverse storage order wrt the target order.
12027 This routine must only be called for GCC type nodes that correspond to
12028 Dwarf base (fundamental) types. */
12030 static dw_die_ref
12031 base_type_die (tree type, bool reverse)
12033 dw_die_ref base_type_result;
12034 enum dwarf_type encoding;
12035 bool fpt_used = false;
12036 struct fixed_point_type_info fpt_info;
12037 tree type_bias = NULL_TREE;
12039 if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
12040 return 0;
12042 /* If this is a subtype that should not be emitted as a subrange type,
12043 use the base type. See subrange_type_for_debug_p. */
12044 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
12045 type = TREE_TYPE (type);
12047 switch (TREE_CODE (type))
12049 case INTEGER_TYPE:
12050 if ((dwarf_version >= 4 || !dwarf_strict)
12051 && TYPE_NAME (type)
12052 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12053 && DECL_IS_BUILTIN (TYPE_NAME (type))
12054 && DECL_NAME (TYPE_NAME (type)))
12056 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
12057 if (strcmp (name, "char16_t") == 0
12058 || strcmp (name, "char32_t") == 0)
12060 encoding = DW_ATE_UTF;
12061 break;
12064 if ((dwarf_version >= 3 || !dwarf_strict)
12065 && lang_hooks.types.get_fixed_point_type_info)
12067 memset (&fpt_info, 0, sizeof (fpt_info));
12068 if (lang_hooks.types.get_fixed_point_type_info (type, &fpt_info))
12070 fpt_used = true;
12071 encoding = ((TYPE_UNSIGNED (type))
12072 ? DW_ATE_unsigned_fixed
12073 : DW_ATE_signed_fixed);
12074 break;
12077 if (TYPE_STRING_FLAG (type))
12079 if (TYPE_UNSIGNED (type))
12080 encoding = DW_ATE_unsigned_char;
12081 else
12082 encoding = DW_ATE_signed_char;
12084 else if (TYPE_UNSIGNED (type))
12085 encoding = DW_ATE_unsigned;
12086 else
12087 encoding = DW_ATE_signed;
12089 if (!dwarf_strict
12090 && lang_hooks.types.get_type_bias)
12091 type_bias = lang_hooks.types.get_type_bias (type);
12092 break;
12094 case REAL_TYPE:
12095 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
12097 if (dwarf_version >= 3 || !dwarf_strict)
12098 encoding = DW_ATE_decimal_float;
12099 else
12100 encoding = DW_ATE_lo_user;
12102 else
12103 encoding = DW_ATE_float;
12104 break;
12106 case FIXED_POINT_TYPE:
12107 if (!(dwarf_version >= 3 || !dwarf_strict))
12108 encoding = DW_ATE_lo_user;
12109 else if (TYPE_UNSIGNED (type))
12110 encoding = DW_ATE_unsigned_fixed;
12111 else
12112 encoding = DW_ATE_signed_fixed;
12113 break;
12115 /* Dwarf2 doesn't know anything about complex ints, so use
12116 a user defined type for it. */
12117 case COMPLEX_TYPE:
12118 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
12119 encoding = DW_ATE_complex_float;
12120 else
12121 encoding = DW_ATE_lo_user;
12122 break;
12124 case BOOLEAN_TYPE:
12125 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
12126 encoding = DW_ATE_boolean;
12127 break;
12129 default:
12130 /* No other TREE_CODEs are Dwarf fundamental types. */
12131 gcc_unreachable ();
12134 base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
12136 add_AT_unsigned (base_type_result, DW_AT_byte_size,
12137 int_size_in_bytes (type));
12138 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
12140 if (need_endianity_attribute_p (reverse))
12141 add_AT_unsigned (base_type_result, DW_AT_endianity,
12142 BYTES_BIG_ENDIAN ? DW_END_little : DW_END_big);
12144 add_alignment_attribute (base_type_result, type);
12146 if (fpt_used)
12148 switch (fpt_info.scale_factor_kind)
12150 case fixed_point_scale_factor_binary:
12151 add_AT_int (base_type_result, DW_AT_binary_scale,
12152 fpt_info.scale_factor.binary);
12153 break;
12155 case fixed_point_scale_factor_decimal:
12156 add_AT_int (base_type_result, DW_AT_decimal_scale,
12157 fpt_info.scale_factor.decimal);
12158 break;
12160 case fixed_point_scale_factor_arbitrary:
12161 /* Arbitrary scale factors cannot be described in standard DWARF,
12162 yet. */
12163 if (!dwarf_strict)
12165 /* Describe the scale factor as a rational constant. */
12166 const dw_die_ref scale_factor
12167 = new_die (DW_TAG_constant, comp_unit_die (), type);
12169 add_AT_unsigned (scale_factor, DW_AT_GNU_numerator,
12170 fpt_info.scale_factor.arbitrary.numerator);
12171 add_AT_int (scale_factor, DW_AT_GNU_denominator,
12172 fpt_info.scale_factor.arbitrary.denominator);
12174 add_AT_die_ref (base_type_result, DW_AT_small, scale_factor);
12176 break;
12178 default:
12179 gcc_unreachable ();
12183 if (type_bias)
12184 add_scalar_info (base_type_result, DW_AT_GNU_bias, type_bias,
12185 dw_scalar_form_constant
12186 | dw_scalar_form_exprloc
12187 | dw_scalar_form_reference,
12188 NULL);
12190 add_pubtype (type, base_type_result);
12192 return base_type_result;
12195 /* A C++ function with deduced return type can have a TEMPLATE_TYPE_PARM
12196 named 'auto' in its type: return true for it, false otherwise. */
12198 static inline bool
12199 is_cxx_auto (tree type)
12201 if (is_cxx ())
12203 tree name = TYPE_IDENTIFIER (type);
12204 if (name == get_identifier ("auto")
12205 || name == get_identifier ("decltype(auto)"))
12206 return true;
12208 return false;
12211 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
12212 given input type is a Dwarf "fundamental" type. Otherwise return null. */
12214 static inline int
12215 is_base_type (tree type)
12217 switch (TREE_CODE (type))
12219 case ERROR_MARK:
12220 case VOID_TYPE:
12221 case INTEGER_TYPE:
12222 case REAL_TYPE:
12223 case FIXED_POINT_TYPE:
12224 case COMPLEX_TYPE:
12225 case BOOLEAN_TYPE:
12226 case POINTER_BOUNDS_TYPE:
12227 return 1;
12229 case ARRAY_TYPE:
12230 case RECORD_TYPE:
12231 case UNION_TYPE:
12232 case QUAL_UNION_TYPE:
12233 case ENUMERAL_TYPE:
12234 case FUNCTION_TYPE:
12235 case METHOD_TYPE:
12236 case POINTER_TYPE:
12237 case REFERENCE_TYPE:
12238 case NULLPTR_TYPE:
12239 case OFFSET_TYPE:
12240 case LANG_TYPE:
12241 case VECTOR_TYPE:
12242 return 0;
12244 default:
12245 if (is_cxx_auto (type))
12246 return 0;
12247 gcc_unreachable ();
12250 return 0;
12253 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
12254 node, return the size in bits for the type if it is a constant, or else
12255 return the alignment for the type if the type's size is not constant, or
12256 else return BITS_PER_WORD if the type actually turns out to be an
12257 ERROR_MARK node. */
12259 static inline unsigned HOST_WIDE_INT
12260 simple_type_size_in_bits (const_tree type)
12262 if (TREE_CODE (type) == ERROR_MARK)
12263 return BITS_PER_WORD;
12264 else if (TYPE_SIZE (type) == NULL_TREE)
12265 return 0;
12266 else if (tree_fits_uhwi_p (TYPE_SIZE (type)))
12267 return tree_to_uhwi (TYPE_SIZE (type));
12268 else
12269 return TYPE_ALIGN (type);
12272 /* Similarly, but return an offset_int instead of UHWI. */
12274 static inline offset_int
12275 offset_int_type_size_in_bits (const_tree type)
12277 if (TREE_CODE (type) == ERROR_MARK)
12278 return BITS_PER_WORD;
12279 else if (TYPE_SIZE (type) == NULL_TREE)
12280 return 0;
12281 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
12282 return wi::to_offset (TYPE_SIZE (type));
12283 else
12284 return TYPE_ALIGN (type);
12287 /* Given a pointer to a tree node for a subrange type, return a pointer
12288 to a DIE that describes the given type. */
12290 static dw_die_ref
12291 subrange_type_die (tree type, tree low, tree high, tree bias,
12292 dw_die_ref context_die)
12294 dw_die_ref subrange_die;
12295 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
12297 if (context_die == NULL)
12298 context_die = comp_unit_die ();
12300 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
12302 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
12304 /* The size of the subrange type and its base type do not match,
12305 so we need to generate a size attribute for the subrange type. */
12306 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
12309 add_alignment_attribute (subrange_die, type);
12311 if (low)
12312 add_bound_info (subrange_die, DW_AT_lower_bound, low, NULL);
12313 if (high)
12314 add_bound_info (subrange_die, DW_AT_upper_bound, high, NULL);
12315 if (bias && !dwarf_strict)
12316 add_scalar_info (subrange_die, DW_AT_GNU_bias, bias,
12317 dw_scalar_form_constant
12318 | dw_scalar_form_exprloc
12319 | dw_scalar_form_reference,
12320 NULL);
12322 return subrange_die;
12325 /* Returns the (const and/or volatile) cv_qualifiers associated with
12326 the decl node. This will normally be augmented with the
12327 cv_qualifiers of the underlying type in add_type_attribute. */
12329 static int
12330 decl_quals (const_tree decl)
12332 return ((TREE_READONLY (decl)
12333 /* The C++ front-end correctly marks reference-typed
12334 variables as readonly, but from a language (and debug
12335 info) standpoint they are not const-qualified. */
12336 && TREE_CODE (TREE_TYPE (decl)) != REFERENCE_TYPE
12337 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED)
12338 | (TREE_THIS_VOLATILE (decl)
12339 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED));
12342 /* Determine the TYPE whose qualifiers match the largest strict subset
12343 of the given TYPE_QUALS, and return its qualifiers. Ignore all
12344 qualifiers outside QUAL_MASK. */
12346 static int
12347 get_nearest_type_subqualifiers (tree type, int type_quals, int qual_mask)
12349 tree t;
12350 int best_rank = 0, best_qual = 0, max_rank;
12352 type_quals &= qual_mask;
12353 max_rank = popcount_hwi (type_quals) - 1;
12355 for (t = TYPE_MAIN_VARIANT (type); t && best_rank < max_rank;
12356 t = TYPE_NEXT_VARIANT (t))
12358 int q = TYPE_QUALS (t) & qual_mask;
12360 if ((q & type_quals) == q && q != type_quals
12361 && check_base_type (t, type))
12363 int rank = popcount_hwi (q);
12365 if (rank > best_rank)
12367 best_rank = rank;
12368 best_qual = q;
12373 return best_qual;
12376 struct dwarf_qual_info_t { int q; enum dwarf_tag t; };
12377 static const dwarf_qual_info_t dwarf_qual_info[] =
12379 { TYPE_QUAL_CONST, DW_TAG_const_type },
12380 { TYPE_QUAL_VOLATILE, DW_TAG_volatile_type },
12381 { TYPE_QUAL_RESTRICT, DW_TAG_restrict_type },
12382 { TYPE_QUAL_ATOMIC, DW_TAG_atomic_type }
12384 static const unsigned int dwarf_qual_info_size
12385 = sizeof (dwarf_qual_info) / sizeof (dwarf_qual_info[0]);
12387 /* If DIE is a qualified DIE of some base DIE with the same parent,
12388 return the base DIE, otherwise return NULL. Set MASK to the
12389 qualifiers added compared to the returned DIE. */
12391 static dw_die_ref
12392 qualified_die_p (dw_die_ref die, int *mask, unsigned int depth)
12394 unsigned int i;
12395 for (i = 0; i < dwarf_qual_info_size; i++)
12396 if (die->die_tag == dwarf_qual_info[i].t)
12397 break;
12398 if (i == dwarf_qual_info_size)
12399 return NULL;
12400 if (vec_safe_length (die->die_attr) != 1)
12401 return NULL;
12402 dw_die_ref type = get_AT_ref (die, DW_AT_type);
12403 if (type == NULL || type->die_parent != die->die_parent)
12404 return NULL;
12405 *mask |= dwarf_qual_info[i].q;
12406 if (depth)
12408 dw_die_ref ret = qualified_die_p (type, mask, depth - 1);
12409 if (ret)
12410 return ret;
12412 return type;
12415 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
12416 entry that chains the modifiers specified by CV_QUALS in front of the
12417 given type. REVERSE is true if the type is to be interpreted in the
12418 reverse storage order wrt the target order. */
12420 static dw_die_ref
12421 modified_type_die (tree type, int cv_quals, bool reverse,
12422 dw_die_ref context_die)
12424 enum tree_code code = TREE_CODE (type);
12425 dw_die_ref mod_type_die;
12426 dw_die_ref sub_die = NULL;
12427 tree item_type = NULL;
12428 tree qualified_type;
12429 tree name, low, high;
12430 dw_die_ref mod_scope;
12431 /* Only these cv-qualifiers are currently handled. */
12432 const int cv_qual_mask = (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE
12433 | TYPE_QUAL_RESTRICT | TYPE_QUAL_ATOMIC);
12435 if (code == ERROR_MARK)
12436 return NULL;
12438 if (lang_hooks.types.get_debug_type)
12440 tree debug_type = lang_hooks.types.get_debug_type (type);
12442 if (debug_type != NULL_TREE && debug_type != type)
12443 return modified_type_die (debug_type, cv_quals, reverse, context_die);
12446 cv_quals &= cv_qual_mask;
12448 /* Don't emit DW_TAG_restrict_type for DWARFv2, since it is a type
12449 tag modifier (and not an attribute) old consumers won't be able
12450 to handle it. */
12451 if (dwarf_version < 3)
12452 cv_quals &= ~TYPE_QUAL_RESTRICT;
12454 /* Likewise for DW_TAG_atomic_type for DWARFv5. */
12455 if (dwarf_version < 5)
12456 cv_quals &= ~TYPE_QUAL_ATOMIC;
12458 /* See if we already have the appropriately qualified variant of
12459 this type. */
12460 qualified_type = get_qualified_type (type, cv_quals);
12462 if (qualified_type == sizetype
12463 && TYPE_NAME (qualified_type)
12464 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
12466 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
12468 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
12469 && TYPE_PRECISION (t)
12470 == TYPE_PRECISION (qualified_type)
12471 && TYPE_UNSIGNED (t)
12472 == TYPE_UNSIGNED (qualified_type));
12473 qualified_type = t;
12476 /* If we do, then we can just use its DIE, if it exists. */
12477 if (qualified_type)
12479 mod_type_die = lookup_type_die (qualified_type);
12481 /* DW_AT_endianity doesn't come from a qualifier on the type. */
12482 if (mod_type_die
12483 && (!need_endianity_attribute_p (reverse)
12484 || !is_base_type (type)
12485 || get_AT_unsigned (mod_type_die, DW_AT_endianity)))
12486 return mod_type_die;
12489 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
12491 /* Handle C typedef types. */
12492 if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
12493 && !DECL_ARTIFICIAL (name))
12495 tree dtype = TREE_TYPE (name);
12497 if (qualified_type == dtype)
12499 /* For a named type, use the typedef. */
12500 gen_type_die (qualified_type, context_die);
12501 return lookup_type_die (qualified_type);
12503 else
12505 int dquals = TYPE_QUALS_NO_ADDR_SPACE (dtype);
12506 dquals &= cv_qual_mask;
12507 if ((dquals & ~cv_quals) != TYPE_UNQUALIFIED
12508 || (cv_quals == dquals && DECL_ORIGINAL_TYPE (name) != type))
12509 /* cv-unqualified version of named type. Just use
12510 the unnamed type to which it refers. */
12511 return modified_type_die (DECL_ORIGINAL_TYPE (name), cv_quals,
12512 reverse, context_die);
12513 /* Else cv-qualified version of named type; fall through. */
12517 mod_scope = scope_die_for (type, context_die);
12519 if (cv_quals)
12521 int sub_quals = 0, first_quals = 0;
12522 unsigned i;
12523 dw_die_ref first = NULL, last = NULL;
12525 /* Determine a lesser qualified type that most closely matches
12526 this one. Then generate DW_TAG_* entries for the remaining
12527 qualifiers. */
12528 sub_quals = get_nearest_type_subqualifiers (type, cv_quals,
12529 cv_qual_mask);
12530 if (sub_quals && use_debug_types)
12532 bool needed = false;
12533 /* If emitting type units, make sure the order of qualifiers
12534 is canonical. Thus, start from unqualified type if
12535 an earlier qualifier is missing in sub_quals, but some later
12536 one is present there. */
12537 for (i = 0; i < dwarf_qual_info_size; i++)
12538 if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
12539 needed = true;
12540 else if (needed && (dwarf_qual_info[i].q & cv_quals))
12542 sub_quals = 0;
12543 break;
12546 mod_type_die = modified_type_die (type, sub_quals, reverse, context_die);
12547 if (mod_scope && mod_type_die && mod_type_die->die_parent == mod_scope)
12549 /* As not all intermediate qualified DIEs have corresponding
12550 tree types, ensure that qualified DIEs in the same scope
12551 as their DW_AT_type are emitted after their DW_AT_type,
12552 only with other qualified DIEs for the same type possibly
12553 in between them. Determine the range of such qualified
12554 DIEs now (first being the base type, last being corresponding
12555 last qualified DIE for it). */
12556 unsigned int count = 0;
12557 first = qualified_die_p (mod_type_die, &first_quals,
12558 dwarf_qual_info_size);
12559 if (first == NULL)
12560 first = mod_type_die;
12561 gcc_assert ((first_quals & ~sub_quals) == 0);
12562 for (count = 0, last = first;
12563 count < (1U << dwarf_qual_info_size);
12564 count++, last = last->die_sib)
12566 int quals = 0;
12567 if (last == mod_scope->die_child)
12568 break;
12569 if (qualified_die_p (last->die_sib, &quals, dwarf_qual_info_size)
12570 != first)
12571 break;
12575 for (i = 0; i < dwarf_qual_info_size; i++)
12576 if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
12578 dw_die_ref d;
12579 if (first && first != last)
12581 for (d = first->die_sib; ; d = d->die_sib)
12583 int quals = 0;
12584 qualified_die_p (d, &quals, dwarf_qual_info_size);
12585 if (quals == (first_quals | dwarf_qual_info[i].q))
12586 break;
12587 if (d == last)
12589 d = NULL;
12590 break;
12593 if (d)
12595 mod_type_die = d;
12596 continue;
12599 if (first)
12601 d = ggc_cleared_alloc<die_node> ();
12602 d->die_tag = dwarf_qual_info[i].t;
12603 add_child_die_after (mod_scope, d, last);
12604 last = d;
12606 else
12607 d = new_die (dwarf_qual_info[i].t, mod_scope, type);
12608 if (mod_type_die)
12609 add_AT_die_ref (d, DW_AT_type, mod_type_die);
12610 mod_type_die = d;
12611 first_quals |= dwarf_qual_info[i].q;
12614 else if (code == POINTER_TYPE || code == REFERENCE_TYPE)
12616 dwarf_tag tag = DW_TAG_pointer_type;
12617 if (code == REFERENCE_TYPE)
12619 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
12620 tag = DW_TAG_rvalue_reference_type;
12621 else
12622 tag = DW_TAG_reference_type;
12624 mod_type_die = new_die (tag, mod_scope, type);
12626 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
12627 simple_type_size_in_bits (type) / BITS_PER_UNIT);
12628 add_alignment_attribute (mod_type_die, type);
12629 item_type = TREE_TYPE (type);
12631 addr_space_t as = TYPE_ADDR_SPACE (item_type);
12632 if (!ADDR_SPACE_GENERIC_P (as))
12634 int action = targetm.addr_space.debug (as);
12635 if (action >= 0)
12637 /* Positive values indicate an address_class. */
12638 add_AT_unsigned (mod_type_die, DW_AT_address_class, action);
12640 else
12642 /* Negative values indicate an (inverted) segment base reg. */
12643 dw_loc_descr_ref d
12644 = one_reg_loc_descriptor (~action, VAR_INIT_STATUS_INITIALIZED);
12645 add_AT_loc (mod_type_die, DW_AT_segment, d);
12649 else if (code == INTEGER_TYPE
12650 && TREE_TYPE (type) != NULL_TREE
12651 && subrange_type_for_debug_p (type, &low, &high))
12653 tree bias = NULL_TREE;
12654 if (lang_hooks.types.get_type_bias)
12655 bias = lang_hooks.types.get_type_bias (type);
12656 mod_type_die = subrange_type_die (type, low, high, bias, context_die);
12657 item_type = TREE_TYPE (type);
12659 else if (is_base_type (type))
12660 mod_type_die = base_type_die (type, reverse);
12661 else
12663 gen_type_die (type, context_die);
12665 /* We have to get the type_main_variant here (and pass that to the
12666 `lookup_type_die' routine) because the ..._TYPE node we have
12667 might simply be a *copy* of some original type node (where the
12668 copy was created to help us keep track of typedef names) and
12669 that copy might have a different TYPE_UID from the original
12670 ..._TYPE node. */
12671 if (TREE_CODE (type) == FUNCTION_TYPE
12672 || TREE_CODE (type) == METHOD_TYPE)
12674 /* For function/method types, can't just use type_main_variant here,
12675 because that can have different ref-qualifiers for C++,
12676 but try to canonicalize. */
12677 tree main = TYPE_MAIN_VARIANT (type);
12678 for (tree t = main; t; t = TYPE_NEXT_VARIANT (t))
12679 if (check_base_type (t, main) && check_lang_type (t, type))
12680 return lookup_type_die (t);
12681 return lookup_type_die (type);
12683 else if (TREE_CODE (type) != VECTOR_TYPE
12684 && TREE_CODE (type) != ARRAY_TYPE)
12685 return lookup_type_die (type_main_variant (type));
12686 else
12687 /* Vectors have the debugging information in the type,
12688 not the main variant. */
12689 return lookup_type_die (type);
12692 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
12693 don't output a DW_TAG_typedef, since there isn't one in the
12694 user's program; just attach a DW_AT_name to the type.
12695 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
12696 if the base type already has the same name. */
12697 if (name
12698 && ((TREE_CODE (name) != TYPE_DECL
12699 && (qualified_type == TYPE_MAIN_VARIANT (type)
12700 || (cv_quals == TYPE_UNQUALIFIED)))
12701 || (TREE_CODE (name) == TYPE_DECL
12702 && TREE_TYPE (name) == qualified_type
12703 && DECL_NAME (name))))
12705 if (TREE_CODE (name) == TYPE_DECL)
12706 /* Could just call add_name_and_src_coords_attributes here,
12707 but since this is a builtin type it doesn't have any
12708 useful source coordinates anyway. */
12709 name = DECL_NAME (name);
12710 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
12712 /* This probably indicates a bug. */
12713 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
12715 name = TYPE_IDENTIFIER (type);
12716 add_name_attribute (mod_type_die,
12717 name ? IDENTIFIER_POINTER (name) : "__unknown__");
12720 if (qualified_type)
12721 equate_type_number_to_die (qualified_type, mod_type_die);
12723 if (item_type)
12724 /* We must do this after the equate_type_number_to_die call, in case
12725 this is a recursive type. This ensures that the modified_type_die
12726 recursion will terminate even if the type is recursive. Recursive
12727 types are possible in Ada. */
12728 sub_die = modified_type_die (item_type,
12729 TYPE_QUALS_NO_ADDR_SPACE (item_type),
12730 reverse,
12731 context_die);
12733 if (sub_die != NULL)
12734 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
12736 add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
12737 if (TYPE_ARTIFICIAL (type))
12738 add_AT_flag (mod_type_die, DW_AT_artificial, 1);
12740 return mod_type_die;
12743 /* Generate DIEs for the generic parameters of T.
12744 T must be either a generic type or a generic function.
12745 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
12747 static void
12748 gen_generic_params_dies (tree t)
12750 tree parms, args;
12751 int parms_num, i;
12752 dw_die_ref die = NULL;
12753 int non_default;
12755 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
12756 return;
12758 if (TYPE_P (t))
12759 die = lookup_type_die (t);
12760 else if (DECL_P (t))
12761 die = lookup_decl_die (t);
12763 gcc_assert (die);
12765 parms = lang_hooks.get_innermost_generic_parms (t);
12766 if (!parms)
12767 /* T has no generic parameter. It means T is neither a generic type
12768 or function. End of story. */
12769 return;
12771 parms_num = TREE_VEC_LENGTH (parms);
12772 args = lang_hooks.get_innermost_generic_args (t);
12773 if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
12774 non_default = int_cst_value (TREE_CHAIN (args));
12775 else
12776 non_default = TREE_VEC_LENGTH (args);
12777 for (i = 0; i < parms_num; i++)
12779 tree parm, arg, arg_pack_elems;
12780 dw_die_ref parm_die;
12782 parm = TREE_VEC_ELT (parms, i);
12783 arg = TREE_VEC_ELT (args, i);
12784 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
12785 gcc_assert (parm && TREE_VALUE (parm) && arg);
12787 if (parm && TREE_VALUE (parm) && arg)
12789 /* If PARM represents a template parameter pack,
12790 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
12791 by DW_TAG_template_*_parameter DIEs for the argument
12792 pack elements of ARG. Note that ARG would then be
12793 an argument pack. */
12794 if (arg_pack_elems)
12795 parm_die = template_parameter_pack_die (TREE_VALUE (parm),
12796 arg_pack_elems,
12797 die);
12798 else
12799 parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
12800 true /* emit name */, die);
12801 if (i >= non_default)
12802 add_AT_flag (parm_die, DW_AT_default_value, 1);
12807 /* Create and return a DIE for PARM which should be
12808 the representation of a generic type parameter.
12809 For instance, in the C++ front end, PARM would be a template parameter.
12810 ARG is the argument to PARM.
12811 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
12812 name of the PARM.
12813 PARENT_DIE is the parent DIE which the new created DIE should be added to,
12814 as a child node. */
12816 static dw_die_ref
12817 generic_parameter_die (tree parm, tree arg,
12818 bool emit_name_p,
12819 dw_die_ref parent_die)
12821 dw_die_ref tmpl_die = NULL;
12822 const char *name = NULL;
12824 if (!parm || !DECL_NAME (parm) || !arg)
12825 return NULL;
12827 /* We support non-type generic parameters and arguments,
12828 type generic parameters and arguments, as well as
12829 generic generic parameters (a.k.a. template template parameters in C++)
12830 and arguments. */
12831 if (TREE_CODE (parm) == PARM_DECL)
12832 /* PARM is a nontype generic parameter */
12833 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
12834 else if (TREE_CODE (parm) == TYPE_DECL)
12835 /* PARM is a type generic parameter. */
12836 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
12837 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
12838 /* PARM is a generic generic parameter.
12839 Its DIE is a GNU extension. It shall have a
12840 DW_AT_name attribute to represent the name of the template template
12841 parameter, and a DW_AT_GNU_template_name attribute to represent the
12842 name of the template template argument. */
12843 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
12844 parent_die, parm);
12845 else
12846 gcc_unreachable ();
12848 if (tmpl_die)
12850 tree tmpl_type;
12852 /* If PARM is a generic parameter pack, it means we are
12853 emitting debug info for a template argument pack element.
12854 In other terms, ARG is a template argument pack element.
12855 In that case, we don't emit any DW_AT_name attribute for
12856 the die. */
12857 if (emit_name_p)
12859 name = IDENTIFIER_POINTER (DECL_NAME (parm));
12860 gcc_assert (name);
12861 add_AT_string (tmpl_die, DW_AT_name, name);
12864 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
12866 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
12867 TMPL_DIE should have a child DW_AT_type attribute that is set
12868 to the type of the argument to PARM, which is ARG.
12869 If PARM is a type generic parameter, TMPL_DIE should have a
12870 child DW_AT_type that is set to ARG. */
12871 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
12872 add_type_attribute (tmpl_die, tmpl_type,
12873 (TREE_THIS_VOLATILE (tmpl_type)
12874 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED),
12875 false, parent_die);
12877 else
12879 /* So TMPL_DIE is a DIE representing a
12880 a generic generic template parameter, a.k.a template template
12881 parameter in C++ and arg is a template. */
12883 /* The DW_AT_GNU_template_name attribute of the DIE must be set
12884 to the name of the argument. */
12885 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
12886 if (name)
12887 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
12890 if (TREE_CODE (parm) == PARM_DECL)
12891 /* So PARM is a non-type generic parameter.
12892 DWARF3 5.6.8 says we must set a DW_AT_const_value child
12893 attribute of TMPL_DIE which value represents the value
12894 of ARG.
12895 We must be careful here:
12896 The value of ARG might reference some function decls.
12897 We might currently be emitting debug info for a generic
12898 type and types are emitted before function decls, we don't
12899 know if the function decls referenced by ARG will actually be
12900 emitted after cgraph computations.
12901 So must defer the generation of the DW_AT_const_value to
12902 after cgraph is ready. */
12903 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
12906 return tmpl_die;
12909 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
12910 PARM_PACK must be a template parameter pack. The returned DIE
12911 will be child DIE of PARENT_DIE. */
12913 static dw_die_ref
12914 template_parameter_pack_die (tree parm_pack,
12915 tree parm_pack_args,
12916 dw_die_ref parent_die)
12918 dw_die_ref die;
12919 int j;
12921 gcc_assert (parent_die && parm_pack);
12923 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
12924 add_name_and_src_coords_attributes (die, parm_pack);
12925 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
12926 generic_parameter_die (parm_pack,
12927 TREE_VEC_ELT (parm_pack_args, j),
12928 false /* Don't emit DW_AT_name */,
12929 die);
12930 return die;
12933 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
12934 an enumerated type. */
12936 static inline int
12937 type_is_enum (const_tree type)
12939 return TREE_CODE (type) == ENUMERAL_TYPE;
12942 /* Return the DBX register number described by a given RTL node. */
12944 static unsigned int
12945 dbx_reg_number (const_rtx rtl)
12947 unsigned regno = REGNO (rtl);
12949 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
12951 #ifdef LEAF_REG_REMAP
12952 if (crtl->uses_only_leaf_regs)
12954 int leaf_reg = LEAF_REG_REMAP (regno);
12955 if (leaf_reg != -1)
12956 regno = (unsigned) leaf_reg;
12958 #endif
12960 regno = DBX_REGISTER_NUMBER (regno);
12961 gcc_assert (regno != INVALID_REGNUM);
12962 return regno;
12965 /* Optionally add a DW_OP_piece term to a location description expression.
12966 DW_OP_piece is only added if the location description expression already
12967 doesn't end with DW_OP_piece. */
12969 static void
12970 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
12972 dw_loc_descr_ref loc;
12974 if (*list_head != NULL)
12976 /* Find the end of the chain. */
12977 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
12980 if (loc->dw_loc_opc != DW_OP_piece)
12981 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
12985 /* Return a location descriptor that designates a machine register or
12986 zero if there is none. */
12988 static dw_loc_descr_ref
12989 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
12991 rtx regs;
12993 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
12994 return 0;
12996 /* We only use "frame base" when we're sure we're talking about the
12997 post-prologue local stack frame. We do this by *not* running
12998 register elimination until this point, and recognizing the special
12999 argument pointer and soft frame pointer rtx's.
13000 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
13001 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
13002 && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
13004 dw_loc_descr_ref result = NULL;
13006 if (dwarf_version >= 4 || !dwarf_strict)
13008 result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
13009 initialized);
13010 if (result)
13011 add_loc_descr (&result,
13012 new_loc_descr (DW_OP_stack_value, 0, 0));
13014 return result;
13017 regs = targetm.dwarf_register_span (rtl);
13019 if (REG_NREGS (rtl) > 1 || regs)
13020 return multiple_reg_loc_descriptor (rtl, regs, initialized);
13021 else
13023 unsigned int dbx_regnum = dbx_reg_number (rtl);
13024 if (dbx_regnum == IGNORED_DWARF_REGNUM)
13025 return 0;
13026 return one_reg_loc_descriptor (dbx_regnum, initialized);
13030 /* Return a location descriptor that designates a machine register for
13031 a given hard register number. */
13033 static dw_loc_descr_ref
13034 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
13036 dw_loc_descr_ref reg_loc_descr;
13038 if (regno <= 31)
13039 reg_loc_descr
13040 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
13041 else
13042 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
13044 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13045 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13047 return reg_loc_descr;
13050 /* Given an RTL of a register, return a location descriptor that
13051 designates a value that spans more than one register. */
13053 static dw_loc_descr_ref
13054 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
13055 enum var_init_status initialized)
13057 int size, i;
13058 dw_loc_descr_ref loc_result = NULL;
13060 /* Simple, contiguous registers. */
13061 if (regs == NULL_RTX)
13063 unsigned reg = REGNO (rtl);
13064 int nregs;
13066 #ifdef LEAF_REG_REMAP
13067 if (crtl->uses_only_leaf_regs)
13069 int leaf_reg = LEAF_REG_REMAP (reg);
13070 if (leaf_reg != -1)
13071 reg = (unsigned) leaf_reg;
13073 #endif
13075 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
13076 nregs = REG_NREGS (rtl);
13078 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
13080 loc_result = NULL;
13081 while (nregs--)
13083 dw_loc_descr_ref t;
13085 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
13086 VAR_INIT_STATUS_INITIALIZED);
13087 add_loc_descr (&loc_result, t);
13088 add_loc_descr_op_piece (&loc_result, size);
13089 ++reg;
13091 return loc_result;
13094 /* Now onto stupid register sets in non contiguous locations. */
13096 gcc_assert (GET_CODE (regs) == PARALLEL);
13098 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
13099 loc_result = NULL;
13101 for (i = 0; i < XVECLEN (regs, 0); ++i)
13103 dw_loc_descr_ref t;
13105 t = one_reg_loc_descriptor (dbx_reg_number (XVECEXP (regs, 0, i)),
13106 VAR_INIT_STATUS_INITIALIZED);
13107 add_loc_descr (&loc_result, t);
13108 add_loc_descr_op_piece (&loc_result, size);
13111 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13112 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13113 return loc_result;
13116 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
13118 /* Return a location descriptor that designates a constant i,
13119 as a compound operation from constant (i >> shift), constant shift
13120 and DW_OP_shl. */
13122 static dw_loc_descr_ref
13123 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
13125 dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
13126 add_loc_descr (&ret, int_loc_descriptor (shift));
13127 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
13128 return ret;
13131 /* Return a location descriptor that designates a constant. */
13133 static dw_loc_descr_ref
13134 int_loc_descriptor (HOST_WIDE_INT i)
13136 enum dwarf_location_atom op;
13138 /* Pick the smallest representation of a constant, rather than just
13139 defaulting to the LEB encoding. */
13140 if (i >= 0)
13142 int clz = clz_hwi (i);
13143 int ctz = ctz_hwi (i);
13144 if (i <= 31)
13145 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
13146 else if (i <= 0xff)
13147 op = DW_OP_const1u;
13148 else if (i <= 0xffff)
13149 op = DW_OP_const2u;
13150 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
13151 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
13152 /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
13153 DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
13154 while DW_OP_const4u is 5 bytes. */
13155 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
13156 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13157 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
13158 /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
13159 while DW_OP_const4u is 5 bytes. */
13160 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
13162 else if (DWARF2_ADDR_SIZE == 4 && i > 0x7fffffff
13163 && size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i)
13164 <= 4)
13166 /* As i >= 2**31, the double cast above will yield a negative number.
13167 Since wrapping is defined in DWARF expressions we can output big
13168 positive integers as small negative ones, regardless of the size
13169 of host wide ints.
13171 Here, since the evaluator will handle 32-bit values and since i >=
13172 2**31, we know it's going to be interpreted as a negative literal:
13173 store it this way if we can do better than 5 bytes this way. */
13174 return int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i);
13176 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
13177 op = DW_OP_const4u;
13179 /* Past this point, i >= 0x100000000 and thus DW_OP_constu will take at
13180 least 6 bytes: see if we can do better before falling back to it. */
13181 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13182 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
13183 /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes. */
13184 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
13185 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
13186 && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
13187 >= HOST_BITS_PER_WIDE_INT)
13188 /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
13189 DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes. */
13190 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
13191 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
13192 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
13193 && size_of_uleb128 (i) > 6)
13194 /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes. */
13195 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
13196 else
13197 op = DW_OP_constu;
13199 else
13201 if (i >= -0x80)
13202 op = DW_OP_const1s;
13203 else if (i >= -0x8000)
13204 op = DW_OP_const2s;
13205 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
13207 if (size_of_int_loc_descriptor (i) < 5)
13209 dw_loc_descr_ref ret = int_loc_descriptor (-i);
13210 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
13211 return ret;
13213 op = DW_OP_const4s;
13215 else
13217 if (size_of_int_loc_descriptor (i)
13218 < (unsigned long) 1 + size_of_sleb128 (i))
13220 dw_loc_descr_ref ret = int_loc_descriptor (-i);
13221 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
13222 return ret;
13224 op = DW_OP_consts;
13228 return new_loc_descr (op, i, 0);
13231 /* Likewise, for unsigned constants. */
13233 static dw_loc_descr_ref
13234 uint_loc_descriptor (unsigned HOST_WIDE_INT i)
13236 const unsigned HOST_WIDE_INT max_int = INTTYPE_MAXIMUM (HOST_WIDE_INT);
13237 const unsigned HOST_WIDE_INT max_uint
13238 = INTTYPE_MAXIMUM (unsigned HOST_WIDE_INT);
13240 /* If possible, use the clever signed constants handling. */
13241 if (i <= max_int)
13242 return int_loc_descriptor ((HOST_WIDE_INT) i);
13244 /* Here, we are left with positive numbers that cannot be represented as
13245 HOST_WIDE_INT, i.e.:
13246 max (HOST_WIDE_INT) < i <= max (unsigned HOST_WIDE_INT)
13248 Using DW_OP_const4/8/./u operation to encode them consumes a lot of bytes
13249 whereas may be better to output a negative integer: thanks to integer
13250 wrapping, we know that:
13251 x = x - 2 ** DWARF2_ADDR_SIZE
13252 = x - 2 * (max (HOST_WIDE_INT) + 1)
13253 So numbers close to max (unsigned HOST_WIDE_INT) could be represented as
13254 small negative integers. Let's try that in cases it will clearly improve
13255 the encoding: there is no gain turning DW_OP_const4u into
13256 DW_OP_const4s. */
13257 if (DWARF2_ADDR_SIZE * 8 == HOST_BITS_PER_WIDE_INT
13258 && ((DWARF2_ADDR_SIZE == 4 && i > max_uint - 0x8000)
13259 || (DWARF2_ADDR_SIZE == 8 && i > max_uint - 0x80000000)))
13261 const unsigned HOST_WIDE_INT first_shift = i - max_int - 1;
13263 /* Now, -1 < first_shift <= max (HOST_WIDE_INT)
13264 i.e. 0 <= first_shift <= max (HOST_WIDE_INT). */
13265 const HOST_WIDE_INT second_shift
13266 = (HOST_WIDE_INT) first_shift - (HOST_WIDE_INT) max_int - 1;
13268 /* So we finally have:
13269 -max (HOST_WIDE_INT) - 1 <= second_shift <= -1.
13270 i.e. min (HOST_WIDE_INT) <= second_shift < 0. */
13271 return int_loc_descriptor (second_shift);
13274 /* Last chance: fallback to a simple constant operation. */
13275 return new_loc_descr
13276 ((HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
13277 ? DW_OP_const4u
13278 : DW_OP_const8u,
13279 i, 0);
13282 /* Generate and return a location description that computes the unsigned
13283 comparison of the two stack top entries (a OP b where b is the top-most
13284 entry and a is the second one). The KIND of comparison can be LT_EXPR,
13285 LE_EXPR, GT_EXPR or GE_EXPR. */
13287 static dw_loc_descr_ref
13288 uint_comparison_loc_list (enum tree_code kind)
13290 enum dwarf_location_atom op, flip_op;
13291 dw_loc_descr_ref ret, bra_node, jmp_node, tmp;
13293 switch (kind)
13295 case LT_EXPR:
13296 op = DW_OP_lt;
13297 break;
13298 case LE_EXPR:
13299 op = DW_OP_le;
13300 break;
13301 case GT_EXPR:
13302 op = DW_OP_gt;
13303 break;
13304 case GE_EXPR:
13305 op = DW_OP_ge;
13306 break;
13307 default:
13308 gcc_unreachable ();
13311 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
13312 jmp_node = new_loc_descr (DW_OP_skip, 0, 0);
13314 /* Until DWARFv4, operations all work on signed integers. It is nevertheless
13315 possible to perform unsigned comparisons: we just have to distinguish
13316 three cases:
13318 1. when a and b have the same sign (as signed integers); then we should
13319 return: a OP(signed) b;
13321 2. when a is a negative signed integer while b is a positive one, then a
13322 is a greater unsigned integer than b; likewise when a and b's roles
13323 are flipped.
13325 So first, compare the sign of the two operands. */
13326 ret = new_loc_descr (DW_OP_over, 0, 0);
13327 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
13328 add_loc_descr (&ret, new_loc_descr (DW_OP_xor, 0, 0));
13329 /* If they have different signs (i.e. they have different sign bits), then
13330 the stack top value has now the sign bit set and thus it's smaller than
13331 zero. */
13332 add_loc_descr (&ret, new_loc_descr (DW_OP_lit0, 0, 0));
13333 add_loc_descr (&ret, new_loc_descr (DW_OP_lt, 0, 0));
13334 add_loc_descr (&ret, bra_node);
13336 /* We are in case 1. At this point, we know both operands have the same
13337 sign, to it's safe to use the built-in signed comparison. */
13338 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
13339 add_loc_descr (&ret, jmp_node);
13341 /* We are in case 2. Here, we know both operands do not have the same sign,
13342 so we have to flip the signed comparison. */
13343 flip_op = (kind == LT_EXPR || kind == LE_EXPR) ? DW_OP_gt : DW_OP_lt;
13344 tmp = new_loc_descr (flip_op, 0, 0);
13345 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13346 bra_node->dw_loc_oprnd1.v.val_loc = tmp;
13347 add_loc_descr (&ret, tmp);
13349 /* This dummy operation is necessary to make the two branches join. */
13350 tmp = new_loc_descr (DW_OP_nop, 0, 0);
13351 jmp_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13352 jmp_node->dw_loc_oprnd1.v.val_loc = tmp;
13353 add_loc_descr (&ret, tmp);
13355 return ret;
13358 /* Likewise, but takes the location description lists (might be destructive on
13359 them). Return NULL if either is NULL or if concatenation fails. */
13361 static dw_loc_list_ref
13362 loc_list_from_uint_comparison (dw_loc_list_ref left, dw_loc_list_ref right,
13363 enum tree_code kind)
13365 if (left == NULL || right == NULL)
13366 return NULL;
13368 add_loc_list (&left, right);
13369 if (left == NULL)
13370 return NULL;
13372 add_loc_descr_to_each (left, uint_comparison_loc_list (kind));
13373 return left;
13376 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
13377 without actually allocating it. */
13379 static unsigned long
13380 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
13382 return size_of_int_loc_descriptor (i >> shift)
13383 + size_of_int_loc_descriptor (shift)
13384 + 1;
13387 /* Return size_of_locs (int_loc_descriptor (i)) without
13388 actually allocating it. */
13390 static unsigned long
13391 size_of_int_loc_descriptor (HOST_WIDE_INT i)
13393 unsigned long s;
13395 if (i >= 0)
13397 int clz, ctz;
13398 if (i <= 31)
13399 return 1;
13400 else if (i <= 0xff)
13401 return 2;
13402 else if (i <= 0xffff)
13403 return 3;
13404 clz = clz_hwi (i);
13405 ctz = ctz_hwi (i);
13406 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
13407 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
13408 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13409 - clz - 5);
13410 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13411 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
13412 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13413 - clz - 8);
13414 else if (DWARF2_ADDR_SIZE == 4 && i > 0x7fffffff
13415 && size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i)
13416 <= 4)
13417 return size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i);
13418 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
13419 return 5;
13420 s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
13421 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13422 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
13423 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13424 - clz - 8);
13425 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
13426 && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
13427 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13428 - clz - 16);
13429 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
13430 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
13431 && s > 6)
13432 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13433 - clz - 32);
13434 else
13435 return 1 + s;
13437 else
13439 if (i >= -0x80)
13440 return 2;
13441 else if (i >= -0x8000)
13442 return 3;
13443 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
13445 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
13447 s = size_of_int_loc_descriptor (-i) + 1;
13448 if (s < 5)
13449 return s;
13451 return 5;
13453 else
13455 unsigned long r = 1 + size_of_sleb128 (i);
13456 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
13458 s = size_of_int_loc_descriptor (-i) + 1;
13459 if (s < r)
13460 return s;
13462 return r;
13467 /* Return loc description representing "address" of integer value.
13468 This can appear only as toplevel expression. */
13470 static dw_loc_descr_ref
13471 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
13473 int litsize;
13474 dw_loc_descr_ref loc_result = NULL;
13476 if (!(dwarf_version >= 4 || !dwarf_strict))
13477 return NULL;
13479 litsize = size_of_int_loc_descriptor (i);
13480 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
13481 is more compact. For DW_OP_stack_value we need:
13482 litsize + 1 (DW_OP_stack_value)
13483 and for DW_OP_implicit_value:
13484 1 (DW_OP_implicit_value) + 1 (length) + size. */
13485 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
13487 loc_result = int_loc_descriptor (i);
13488 add_loc_descr (&loc_result,
13489 new_loc_descr (DW_OP_stack_value, 0, 0));
13490 return loc_result;
13493 loc_result = new_loc_descr (DW_OP_implicit_value,
13494 size, 0);
13495 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
13496 loc_result->dw_loc_oprnd2.v.val_int = i;
13497 return loc_result;
13500 /* Return a location descriptor that designates a base+offset location. */
13502 static dw_loc_descr_ref
13503 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
13504 enum var_init_status initialized)
13506 unsigned int regno;
13507 dw_loc_descr_ref result;
13508 dw_fde_ref fde = cfun->fde;
13510 /* We only use "frame base" when we're sure we're talking about the
13511 post-prologue local stack frame. We do this by *not* running
13512 register elimination until this point, and recognizing the special
13513 argument pointer and soft frame pointer rtx's. */
13514 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
13516 rtx elim = (ira_use_lra_p
13517 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
13518 : eliminate_regs (reg, VOIDmode, NULL_RTX));
13520 if (elim != reg)
13522 if (GET_CODE (elim) == PLUS)
13524 offset += INTVAL (XEXP (elim, 1));
13525 elim = XEXP (elim, 0);
13527 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
13528 && (elim == hard_frame_pointer_rtx
13529 || elim == stack_pointer_rtx))
13530 || elim == (frame_pointer_needed
13531 ? hard_frame_pointer_rtx
13532 : stack_pointer_rtx));
13534 /* If drap register is used to align stack, use frame
13535 pointer + offset to access stack variables. If stack
13536 is aligned without drap, use stack pointer + offset to
13537 access stack variables. */
13538 if (crtl->stack_realign_tried
13539 && reg == frame_pointer_rtx)
13541 int base_reg
13542 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
13543 ? HARD_FRAME_POINTER_REGNUM
13544 : REGNO (elim));
13545 return new_reg_loc_descr (base_reg, offset);
13548 gcc_assert (frame_pointer_fb_offset_valid);
13549 offset += frame_pointer_fb_offset;
13550 return new_loc_descr (DW_OP_fbreg, offset, 0);
13554 regno = REGNO (reg);
13555 #ifdef LEAF_REG_REMAP
13556 if (crtl->uses_only_leaf_regs)
13558 int leaf_reg = LEAF_REG_REMAP (regno);
13559 if (leaf_reg != -1)
13560 regno = (unsigned) leaf_reg;
13562 #endif
13563 regno = DWARF_FRAME_REGNUM (regno);
13565 if (!optimize && fde
13566 && (fde->drap_reg == regno || fde->vdrap_reg == regno))
13568 /* Use cfa+offset to represent the location of arguments passed
13569 on the stack when drap is used to align stack.
13570 Only do this when not optimizing, for optimized code var-tracking
13571 is supposed to track where the arguments live and the register
13572 used as vdrap or drap in some spot might be used for something
13573 else in other part of the routine. */
13574 return new_loc_descr (DW_OP_fbreg, offset, 0);
13577 if (regno <= 31)
13578 result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
13579 offset, 0);
13580 else
13581 result = new_loc_descr (DW_OP_bregx, regno, offset);
13583 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13584 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13586 return result;
13589 /* Return true if this RTL expression describes a base+offset calculation. */
13591 static inline int
13592 is_based_loc (const_rtx rtl)
13594 return (GET_CODE (rtl) == PLUS
13595 && ((REG_P (XEXP (rtl, 0))
13596 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
13597 && CONST_INT_P (XEXP (rtl, 1)))));
13600 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
13601 failed. */
13603 static dw_loc_descr_ref
13604 tls_mem_loc_descriptor (rtx mem)
13606 tree base;
13607 dw_loc_descr_ref loc_result;
13609 if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
13610 return NULL;
13612 base = get_base_address (MEM_EXPR (mem));
13613 if (base == NULL
13614 || !VAR_P (base)
13615 || !DECL_THREAD_LOCAL_P (base))
13616 return NULL;
13618 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1, NULL);
13619 if (loc_result == NULL)
13620 return NULL;
13622 if (MEM_OFFSET (mem))
13623 loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
13625 return loc_result;
13628 /* Output debug info about reason why we failed to expand expression as dwarf
13629 expression. */
13631 static void
13632 expansion_failed (tree expr, rtx rtl, char const *reason)
13634 if (dump_file && (dump_flags & TDF_DETAILS))
13636 fprintf (dump_file, "Failed to expand as dwarf: ");
13637 if (expr)
13638 print_generic_expr (dump_file, expr, dump_flags);
13639 if (rtl)
13641 fprintf (dump_file, "\n");
13642 print_rtl (dump_file, rtl);
13644 fprintf (dump_file, "\nReason: %s\n", reason);
13648 /* Helper function for const_ok_for_output. */
13650 static bool
13651 const_ok_for_output_1 (rtx rtl)
13653 if (GET_CODE (rtl) == UNSPEC)
13655 /* If delegitimize_address couldn't do anything with the UNSPEC, assume
13656 we can't express it in the debug info. */
13657 /* Don't complain about TLS UNSPECs, those are just too hard to
13658 delegitimize. Note this could be a non-decl SYMBOL_REF such as
13659 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
13660 rather than DECL_THREAD_LOCAL_P is not just an optimization. */
13661 if (flag_checking
13662 && (XVECLEN (rtl, 0) == 0
13663 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
13664 || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE))
13665 inform (current_function_decl
13666 ? DECL_SOURCE_LOCATION (current_function_decl)
13667 : UNKNOWN_LOCATION,
13668 #if NUM_UNSPEC_VALUES > 0
13669 "non-delegitimized UNSPEC %s (%d) found in variable location",
13670 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
13671 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
13672 XINT (rtl, 1));
13673 #else
13674 "non-delegitimized UNSPEC %d found in variable location",
13675 XINT (rtl, 1));
13676 #endif
13677 expansion_failed (NULL_TREE, rtl,
13678 "UNSPEC hasn't been delegitimized.\n");
13679 return false;
13682 if (targetm.const_not_ok_for_debug_p (rtl))
13684 expansion_failed (NULL_TREE, rtl,
13685 "Expression rejected for debug by the backend.\n");
13686 return false;
13689 /* FIXME: Refer to PR60655. It is possible for simplification
13690 of rtl expressions in var tracking to produce such expressions.
13691 We should really identify / validate expressions
13692 enclosed in CONST that can be handled by assemblers on various
13693 targets and only handle legitimate cases here. */
13694 if (GET_CODE (rtl) != SYMBOL_REF)
13696 if (GET_CODE (rtl) == NOT)
13697 return false;
13698 return true;
13701 if (CONSTANT_POOL_ADDRESS_P (rtl))
13703 bool marked;
13704 get_pool_constant_mark (rtl, &marked);
13705 /* If all references to this pool constant were optimized away,
13706 it was not output and thus we can't represent it. */
13707 if (!marked)
13709 expansion_failed (NULL_TREE, rtl,
13710 "Constant was removed from constant pool.\n");
13711 return false;
13715 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
13716 return false;
13718 /* Avoid references to external symbols in debug info, on several targets
13719 the linker might even refuse to link when linking a shared library,
13720 and in many other cases the relocations for .debug_info/.debug_loc are
13721 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
13722 to be defined within the same shared library or executable are fine. */
13723 if (SYMBOL_REF_EXTERNAL_P (rtl))
13725 tree decl = SYMBOL_REF_DECL (rtl);
13727 if (decl == NULL || !targetm.binds_local_p (decl))
13729 expansion_failed (NULL_TREE, rtl,
13730 "Symbol not defined in current TU.\n");
13731 return false;
13735 return true;
13738 /* Return true if constant RTL can be emitted in DW_OP_addr or
13739 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
13740 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
13742 static bool
13743 const_ok_for_output (rtx rtl)
13745 if (GET_CODE (rtl) == SYMBOL_REF)
13746 return const_ok_for_output_1 (rtl);
13748 if (GET_CODE (rtl) == CONST)
13750 subrtx_var_iterator::array_type array;
13751 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 0), ALL)
13752 if (!const_ok_for_output_1 (*iter))
13753 return false;
13754 return true;
13757 return true;
13760 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
13761 if possible, NULL otherwise. */
13763 static dw_die_ref
13764 base_type_for_mode (machine_mode mode, bool unsignedp)
13766 dw_die_ref type_die;
13767 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
13769 if (type == NULL)
13770 return NULL;
13771 switch (TREE_CODE (type))
13773 case INTEGER_TYPE:
13774 case REAL_TYPE:
13775 break;
13776 default:
13777 return NULL;
13779 type_die = lookup_type_die (type);
13780 if (!type_die)
13781 type_die = modified_type_die (type, TYPE_UNQUALIFIED, false,
13782 comp_unit_die ());
13783 if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
13784 return NULL;
13785 return type_die;
13788 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
13789 type matching MODE, or, if MODE is narrower than or as wide as
13790 DWARF2_ADDR_SIZE, untyped. Return NULL if the conversion is not
13791 possible. */
13793 static dw_loc_descr_ref
13794 convert_descriptor_to_mode (machine_mode mode, dw_loc_descr_ref op)
13796 machine_mode outer_mode = mode;
13797 dw_die_ref type_die;
13798 dw_loc_descr_ref cvt;
13800 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
13802 add_loc_descr (&op, new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0));
13803 return op;
13805 type_die = base_type_for_mode (outer_mode, 1);
13806 if (type_die == NULL)
13807 return NULL;
13808 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
13809 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13810 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13811 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13812 add_loc_descr (&op, cvt);
13813 return op;
13816 /* Return location descriptor for comparison OP with operands OP0 and OP1. */
13818 static dw_loc_descr_ref
13819 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
13820 dw_loc_descr_ref op1)
13822 dw_loc_descr_ref ret = op0;
13823 add_loc_descr (&ret, op1);
13824 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
13825 if (STORE_FLAG_VALUE != 1)
13827 add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
13828 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
13830 return ret;
13833 /* Return location descriptor for signed comparison OP RTL. */
13835 static dw_loc_descr_ref
13836 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
13837 machine_mode mem_mode)
13839 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
13840 dw_loc_descr_ref op0, op1;
13841 int shift;
13843 if (op_mode == VOIDmode)
13844 op_mode = GET_MODE (XEXP (rtl, 1));
13845 if (op_mode == VOIDmode)
13846 return NULL;
13848 if (dwarf_strict
13849 && dwarf_version < 5
13850 && (!SCALAR_INT_MODE_P (op_mode)
13851 || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
13852 return NULL;
13854 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
13855 VAR_INIT_STATUS_INITIALIZED);
13856 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
13857 VAR_INIT_STATUS_INITIALIZED);
13859 if (op0 == NULL || op1 == NULL)
13860 return NULL;
13862 if (!SCALAR_INT_MODE_P (op_mode)
13863 || GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
13864 return compare_loc_descriptor (op, op0, op1);
13866 if (GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
13868 dw_die_ref type_die = base_type_for_mode (op_mode, 0);
13869 dw_loc_descr_ref cvt;
13871 if (type_die == NULL)
13872 return NULL;
13873 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
13874 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13875 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13876 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13877 add_loc_descr (&op0, cvt);
13878 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
13879 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13880 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13881 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13882 add_loc_descr (&op1, cvt);
13883 return compare_loc_descriptor (op, op0, op1);
13886 shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
13887 /* For eq/ne, if the operands are known to be zero-extended,
13888 there is no need to do the fancy shifting up. */
13889 if (op == DW_OP_eq || op == DW_OP_ne)
13891 dw_loc_descr_ref last0, last1;
13892 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
13894 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
13896 /* deref_size zero extends, and for constants we can check
13897 whether they are zero extended or not. */
13898 if (((last0->dw_loc_opc == DW_OP_deref_size
13899 && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
13900 || (CONST_INT_P (XEXP (rtl, 0))
13901 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
13902 == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
13903 && ((last1->dw_loc_opc == DW_OP_deref_size
13904 && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
13905 || (CONST_INT_P (XEXP (rtl, 1))
13906 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
13907 == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
13908 return compare_loc_descriptor (op, op0, op1);
13910 /* EQ/NE comparison against constant in narrower type than
13911 DWARF2_ADDR_SIZE can be performed either as
13912 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
13913 DW_OP_{eq,ne}
13915 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
13916 DW_OP_{eq,ne}. Pick whatever is shorter. */
13917 if (CONST_INT_P (XEXP (rtl, 1))
13918 && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
13919 && (size_of_int_loc_descriptor (shift) + 1
13920 + size_of_int_loc_descriptor (UINTVAL (XEXP (rtl, 1)) << shift)
13921 >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
13922 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
13923 & GET_MODE_MASK (op_mode))))
13925 add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
13926 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
13927 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
13928 & GET_MODE_MASK (op_mode));
13929 return compare_loc_descriptor (op, op0, op1);
13932 add_loc_descr (&op0, int_loc_descriptor (shift));
13933 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
13934 if (CONST_INT_P (XEXP (rtl, 1)))
13935 op1 = int_loc_descriptor (UINTVAL (XEXP (rtl, 1)) << shift);
13936 else
13938 add_loc_descr (&op1, int_loc_descriptor (shift));
13939 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
13941 return compare_loc_descriptor (op, op0, op1);
13944 /* Return location descriptor for unsigned comparison OP RTL. */
13946 static dw_loc_descr_ref
13947 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
13948 machine_mode mem_mode)
13950 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
13951 dw_loc_descr_ref op0, op1;
13953 if (op_mode == VOIDmode)
13954 op_mode = GET_MODE (XEXP (rtl, 1));
13955 if (op_mode == VOIDmode)
13956 return NULL;
13957 if (!SCALAR_INT_MODE_P (op_mode))
13958 return NULL;
13960 if (dwarf_strict
13961 && dwarf_version < 5
13962 && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
13963 return NULL;
13965 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
13966 VAR_INIT_STATUS_INITIALIZED);
13967 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
13968 VAR_INIT_STATUS_INITIALIZED);
13970 if (op0 == NULL || op1 == NULL)
13971 return NULL;
13973 if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
13975 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
13976 dw_loc_descr_ref last0, last1;
13977 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
13979 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
13981 if (CONST_INT_P (XEXP (rtl, 0)))
13982 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
13983 /* deref_size zero extends, so no need to mask it again. */
13984 else if (last0->dw_loc_opc != DW_OP_deref_size
13985 || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
13987 add_loc_descr (&op0, int_loc_descriptor (mask));
13988 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
13990 if (CONST_INT_P (XEXP (rtl, 1)))
13991 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
13992 /* deref_size zero extends, so no need to mask it again. */
13993 else if (last1->dw_loc_opc != DW_OP_deref_size
13994 || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
13996 add_loc_descr (&op1, int_loc_descriptor (mask));
13997 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
14000 else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
14002 HOST_WIDE_INT bias = 1;
14003 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
14004 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14005 if (CONST_INT_P (XEXP (rtl, 1)))
14006 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
14007 + INTVAL (XEXP (rtl, 1)));
14008 else
14009 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
14010 bias, 0));
14012 return compare_loc_descriptor (op, op0, op1);
14015 /* Return location descriptor for {U,S}{MIN,MAX}. */
14017 static dw_loc_descr_ref
14018 minmax_loc_descriptor (rtx rtl, machine_mode mode,
14019 machine_mode mem_mode)
14021 enum dwarf_location_atom op;
14022 dw_loc_descr_ref op0, op1, ret;
14023 dw_loc_descr_ref bra_node, drop_node;
14025 if (dwarf_strict
14026 && dwarf_version < 5
14027 && (!SCALAR_INT_MODE_P (mode)
14028 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
14029 return NULL;
14031 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14032 VAR_INIT_STATUS_INITIALIZED);
14033 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14034 VAR_INIT_STATUS_INITIALIZED);
14036 if (op0 == NULL || op1 == NULL)
14037 return NULL;
14039 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
14040 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
14041 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
14042 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
14044 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
14046 HOST_WIDE_INT mask = GET_MODE_MASK (mode);
14047 add_loc_descr (&op0, int_loc_descriptor (mask));
14048 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14049 add_loc_descr (&op1, int_loc_descriptor (mask));
14050 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
14052 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
14054 HOST_WIDE_INT bias = 1;
14055 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
14056 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14057 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14060 else if (!SCALAR_INT_MODE_P (mode)
14061 && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
14063 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode)) * BITS_PER_UNIT;
14064 add_loc_descr (&op0, int_loc_descriptor (shift));
14065 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
14066 add_loc_descr (&op1, int_loc_descriptor (shift));
14067 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
14069 else if (SCALAR_INT_MODE_P (mode)
14070 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
14072 dw_die_ref type_die = base_type_for_mode (mode, 0);
14073 dw_loc_descr_ref cvt;
14074 if (type_die == NULL)
14075 return NULL;
14076 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14077 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14078 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14079 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14080 add_loc_descr (&op0, cvt);
14081 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14082 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14083 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14084 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14085 add_loc_descr (&op1, cvt);
14088 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
14089 op = DW_OP_lt;
14090 else
14091 op = DW_OP_gt;
14092 ret = op0;
14093 add_loc_descr (&ret, op1);
14094 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
14095 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14096 add_loc_descr (&ret, bra_node);
14097 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14098 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
14099 add_loc_descr (&ret, drop_node);
14100 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14101 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
14102 if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
14103 && SCALAR_INT_MODE_P (mode)
14104 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
14105 ret = convert_descriptor_to_mode (mode, ret);
14106 return ret;
14109 /* Helper function for mem_loc_descriptor. Perform OP binary op,
14110 but after converting arguments to type_die, afterwards
14111 convert back to unsigned. */
14113 static dw_loc_descr_ref
14114 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
14115 machine_mode mode, machine_mode mem_mode)
14117 dw_loc_descr_ref cvt, op0, op1;
14119 if (type_die == NULL)
14120 return NULL;
14121 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14122 VAR_INIT_STATUS_INITIALIZED);
14123 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14124 VAR_INIT_STATUS_INITIALIZED);
14125 if (op0 == NULL || op1 == NULL)
14126 return NULL;
14127 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14128 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14129 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14130 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14131 add_loc_descr (&op0, cvt);
14132 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14133 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14134 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14135 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14136 add_loc_descr (&op1, cvt);
14137 add_loc_descr (&op0, op1);
14138 add_loc_descr (&op0, new_loc_descr (op, 0, 0));
14139 return convert_descriptor_to_mode (mode, op0);
14142 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
14143 const0 is DW_OP_lit0 or corresponding typed constant,
14144 const1 is DW_OP_lit1 or corresponding typed constant
14145 and constMSB is constant with just the MSB bit set
14146 for the mode):
14147 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
14148 L1: const0 DW_OP_swap
14149 L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
14150 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
14151 L3: DW_OP_drop
14152 L4: DW_OP_nop
14154 CTZ is similar:
14155 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
14156 L1: const0 DW_OP_swap
14157 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
14158 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
14159 L3: DW_OP_drop
14160 L4: DW_OP_nop
14162 FFS is similar:
14163 DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
14164 L1: const1 DW_OP_swap
14165 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
14166 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
14167 L3: DW_OP_drop
14168 L4: DW_OP_nop */
14170 static dw_loc_descr_ref
14171 clz_loc_descriptor (rtx rtl, machine_mode mode,
14172 machine_mode mem_mode)
14174 dw_loc_descr_ref op0, ret, tmp;
14175 HOST_WIDE_INT valv;
14176 dw_loc_descr_ref l1jump, l1label;
14177 dw_loc_descr_ref l2jump, l2label;
14178 dw_loc_descr_ref l3jump, l3label;
14179 dw_loc_descr_ref l4jump, l4label;
14180 rtx msb;
14182 if (!SCALAR_INT_MODE_P (mode)
14183 || GET_MODE (XEXP (rtl, 0)) != mode)
14184 return NULL;
14186 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14187 VAR_INIT_STATUS_INITIALIZED);
14188 if (op0 == NULL)
14189 return NULL;
14190 ret = op0;
14191 if (GET_CODE (rtl) == CLZ)
14193 if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
14194 valv = GET_MODE_BITSIZE (mode);
14196 else if (GET_CODE (rtl) == FFS)
14197 valv = 0;
14198 else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
14199 valv = GET_MODE_BITSIZE (mode);
14200 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
14201 l1jump = new_loc_descr (DW_OP_bra, 0, 0);
14202 add_loc_descr (&ret, l1jump);
14203 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
14204 tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
14205 VAR_INIT_STATUS_INITIALIZED);
14206 if (tmp == NULL)
14207 return NULL;
14208 add_loc_descr (&ret, tmp);
14209 l4jump = new_loc_descr (DW_OP_skip, 0, 0);
14210 add_loc_descr (&ret, l4jump);
14211 l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
14212 ? const1_rtx : const0_rtx,
14213 mode, mem_mode,
14214 VAR_INIT_STATUS_INITIALIZED);
14215 if (l1label == NULL)
14216 return NULL;
14217 add_loc_descr (&ret, l1label);
14218 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14219 l2label = new_loc_descr (DW_OP_dup, 0, 0);
14220 add_loc_descr (&ret, l2label);
14221 if (GET_CODE (rtl) != CLZ)
14222 msb = const1_rtx;
14223 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
14224 msb = GEN_INT (HOST_WIDE_INT_1U
14225 << (GET_MODE_BITSIZE (mode) - 1));
14226 else
14227 msb = immed_wide_int_const
14228 (wi::set_bit_in_zero (GET_MODE_PRECISION (mode) - 1,
14229 GET_MODE_PRECISION (mode)), mode);
14230 if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
14231 tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
14232 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
14233 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
14234 else
14235 tmp = mem_loc_descriptor (msb, mode, mem_mode,
14236 VAR_INIT_STATUS_INITIALIZED);
14237 if (tmp == NULL)
14238 return NULL;
14239 add_loc_descr (&ret, tmp);
14240 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
14241 l3jump = new_loc_descr (DW_OP_bra, 0, 0);
14242 add_loc_descr (&ret, l3jump);
14243 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
14244 VAR_INIT_STATUS_INITIALIZED);
14245 if (tmp == NULL)
14246 return NULL;
14247 add_loc_descr (&ret, tmp);
14248 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
14249 ? DW_OP_shl : DW_OP_shr, 0, 0));
14250 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14251 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
14252 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14253 l2jump = new_loc_descr (DW_OP_skip, 0, 0);
14254 add_loc_descr (&ret, l2jump);
14255 l3label = new_loc_descr (DW_OP_drop, 0, 0);
14256 add_loc_descr (&ret, l3label);
14257 l4label = new_loc_descr (DW_OP_nop, 0, 0);
14258 add_loc_descr (&ret, l4label);
14259 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14260 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
14261 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14262 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
14263 l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14264 l3jump->dw_loc_oprnd1.v.val_loc = l3label;
14265 l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14266 l4jump->dw_loc_oprnd1.v.val_loc = l4label;
14267 return ret;
14270 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
14271 const1 is DW_OP_lit1 or corresponding typed constant):
14272 const0 DW_OP_swap
14273 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
14274 DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
14275 L2: DW_OP_drop
14277 PARITY is similar:
14278 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
14279 DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
14280 L2: DW_OP_drop */
14282 static dw_loc_descr_ref
14283 popcount_loc_descriptor (rtx rtl, machine_mode mode,
14284 machine_mode mem_mode)
14286 dw_loc_descr_ref op0, ret, tmp;
14287 dw_loc_descr_ref l1jump, l1label;
14288 dw_loc_descr_ref l2jump, l2label;
14290 if (!SCALAR_INT_MODE_P (mode)
14291 || GET_MODE (XEXP (rtl, 0)) != mode)
14292 return NULL;
14294 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14295 VAR_INIT_STATUS_INITIALIZED);
14296 if (op0 == NULL)
14297 return NULL;
14298 ret = op0;
14299 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
14300 VAR_INIT_STATUS_INITIALIZED);
14301 if (tmp == NULL)
14302 return NULL;
14303 add_loc_descr (&ret, tmp);
14304 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14305 l1label = new_loc_descr (DW_OP_dup, 0, 0);
14306 add_loc_descr (&ret, l1label);
14307 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
14308 add_loc_descr (&ret, l2jump);
14309 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
14310 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
14311 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
14312 VAR_INIT_STATUS_INITIALIZED);
14313 if (tmp == NULL)
14314 return NULL;
14315 add_loc_descr (&ret, tmp);
14316 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
14317 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
14318 ? DW_OP_plus : DW_OP_xor, 0, 0));
14319 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14320 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
14321 VAR_INIT_STATUS_INITIALIZED);
14322 add_loc_descr (&ret, tmp);
14323 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
14324 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
14325 add_loc_descr (&ret, l1jump);
14326 l2label = new_loc_descr (DW_OP_drop, 0, 0);
14327 add_loc_descr (&ret, l2label);
14328 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14329 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
14330 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14331 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
14332 return ret;
14335 /* BSWAP (constS is initial shift count, either 56 or 24):
14336 constS const0
14337 L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
14338 const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
14339 DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
14340 DW_OP_minus DW_OP_swap DW_OP_skip <L1>
14341 L2: DW_OP_drop DW_OP_swap DW_OP_drop */
14343 static dw_loc_descr_ref
14344 bswap_loc_descriptor (rtx rtl, machine_mode mode,
14345 machine_mode mem_mode)
14347 dw_loc_descr_ref op0, ret, tmp;
14348 dw_loc_descr_ref l1jump, l1label;
14349 dw_loc_descr_ref l2jump, l2label;
14351 if (!SCALAR_INT_MODE_P (mode)
14352 || BITS_PER_UNIT != 8
14353 || (GET_MODE_BITSIZE (mode) != 32
14354 && GET_MODE_BITSIZE (mode) != 64))
14355 return NULL;
14357 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14358 VAR_INIT_STATUS_INITIALIZED);
14359 if (op0 == NULL)
14360 return NULL;
14362 ret = op0;
14363 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
14364 mode, mem_mode,
14365 VAR_INIT_STATUS_INITIALIZED);
14366 if (tmp == NULL)
14367 return NULL;
14368 add_loc_descr (&ret, tmp);
14369 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
14370 VAR_INIT_STATUS_INITIALIZED);
14371 if (tmp == NULL)
14372 return NULL;
14373 add_loc_descr (&ret, tmp);
14374 l1label = new_loc_descr (DW_OP_pick, 2, 0);
14375 add_loc_descr (&ret, l1label);
14376 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
14377 mode, mem_mode,
14378 VAR_INIT_STATUS_INITIALIZED);
14379 add_loc_descr (&ret, tmp);
14380 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
14381 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
14382 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
14383 tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
14384 VAR_INIT_STATUS_INITIALIZED);
14385 if (tmp == NULL)
14386 return NULL;
14387 add_loc_descr (&ret, tmp);
14388 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
14389 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
14390 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
14391 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
14392 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14393 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
14394 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
14395 VAR_INIT_STATUS_INITIALIZED);
14396 add_loc_descr (&ret, tmp);
14397 add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
14398 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
14399 add_loc_descr (&ret, l2jump);
14400 tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
14401 VAR_INIT_STATUS_INITIALIZED);
14402 add_loc_descr (&ret, tmp);
14403 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
14404 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14405 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
14406 add_loc_descr (&ret, l1jump);
14407 l2label = new_loc_descr (DW_OP_drop, 0, 0);
14408 add_loc_descr (&ret, l2label);
14409 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14410 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
14411 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14412 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
14413 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14414 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
14415 return ret;
14418 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
14419 DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
14420 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
14421 DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
14423 ROTATERT is similar:
14424 DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
14425 DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
14426 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or */
14428 static dw_loc_descr_ref
14429 rotate_loc_descriptor (rtx rtl, machine_mode mode,
14430 machine_mode mem_mode)
14432 rtx rtlop1 = XEXP (rtl, 1);
14433 dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
14434 int i;
14436 if (!SCALAR_INT_MODE_P (mode))
14437 return NULL;
14439 if (GET_MODE (rtlop1) != VOIDmode
14440 && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
14441 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
14442 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14443 VAR_INIT_STATUS_INITIALIZED);
14444 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
14445 VAR_INIT_STATUS_INITIALIZED);
14446 if (op0 == NULL || op1 == NULL)
14447 return NULL;
14448 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
14449 for (i = 0; i < 2; i++)
14451 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
14452 mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
14453 mode, mem_mode,
14454 VAR_INIT_STATUS_INITIALIZED);
14455 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
14456 mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
14457 ? DW_OP_const4u
14458 : HOST_BITS_PER_WIDE_INT == 64
14459 ? DW_OP_const8u : DW_OP_constu,
14460 GET_MODE_MASK (mode), 0);
14461 else
14462 mask[i] = NULL;
14463 if (mask[i] == NULL)
14464 return NULL;
14465 add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
14467 ret = op0;
14468 add_loc_descr (&ret, op1);
14469 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
14470 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
14471 if (GET_CODE (rtl) == ROTATERT)
14473 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
14474 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
14475 GET_MODE_BITSIZE (mode), 0));
14477 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
14478 if (mask[0] != NULL)
14479 add_loc_descr (&ret, mask[0]);
14480 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
14481 if (mask[1] != NULL)
14483 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14484 add_loc_descr (&ret, mask[1]);
14485 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14487 if (GET_CODE (rtl) == ROTATE)
14489 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
14490 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
14491 GET_MODE_BITSIZE (mode), 0));
14493 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
14494 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
14495 return ret;
14498 /* Helper function for mem_loc_descriptor. Return DW_OP_GNU_parameter_ref
14499 for DEBUG_PARAMETER_REF RTL. */
14501 static dw_loc_descr_ref
14502 parameter_ref_descriptor (rtx rtl)
14504 dw_loc_descr_ref ret;
14505 dw_die_ref ref;
14507 if (dwarf_strict)
14508 return NULL;
14509 gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
14510 ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
14511 ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
14512 if (ref)
14514 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14515 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
14516 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
14518 else
14520 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
14521 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
14523 return ret;
14526 /* The following routine converts the RTL for a variable or parameter
14527 (resident in memory) into an equivalent Dwarf representation of a
14528 mechanism for getting the address of that same variable onto the top of a
14529 hypothetical "address evaluation" stack.
14531 When creating memory location descriptors, we are effectively transforming
14532 the RTL for a memory-resident object into its Dwarf postfix expression
14533 equivalent. This routine recursively descends an RTL tree, turning
14534 it into Dwarf postfix code as it goes.
14536 MODE is the mode that should be assumed for the rtl if it is VOIDmode.
14538 MEM_MODE is the mode of the memory reference, needed to handle some
14539 autoincrement addressing modes.
14541 Return 0 if we can't represent the location. */
14543 dw_loc_descr_ref
14544 mem_loc_descriptor (rtx rtl, machine_mode mode,
14545 machine_mode mem_mode,
14546 enum var_init_status initialized)
14548 dw_loc_descr_ref mem_loc_result = NULL;
14549 enum dwarf_location_atom op;
14550 dw_loc_descr_ref op0, op1;
14551 rtx inner = NULL_RTX;
14553 if (mode == VOIDmode)
14554 mode = GET_MODE (rtl);
14556 /* Note that for a dynamically sized array, the location we will generate a
14557 description of here will be the lowest numbered location which is
14558 actually within the array. That's *not* necessarily the same as the
14559 zeroth element of the array. */
14561 rtl = targetm.delegitimize_address (rtl);
14563 if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
14564 return NULL;
14566 switch (GET_CODE (rtl))
14568 case POST_INC:
14569 case POST_DEC:
14570 case POST_MODIFY:
14571 return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
14573 case SUBREG:
14574 /* The case of a subreg may arise when we have a local (register)
14575 variable or a formal (register) parameter which doesn't quite fill
14576 up an entire register. For now, just assume that it is
14577 legitimate to make the Dwarf info refer to the whole register which
14578 contains the given subreg. */
14579 if (!subreg_lowpart_p (rtl))
14580 break;
14581 inner = SUBREG_REG (rtl);
14582 /* FALLTHRU */
14583 case TRUNCATE:
14584 if (inner == NULL_RTX)
14585 inner = XEXP (rtl, 0);
14586 if (SCALAR_INT_MODE_P (mode)
14587 && SCALAR_INT_MODE_P (GET_MODE (inner))
14588 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
14589 #ifdef POINTERS_EXTEND_UNSIGNED
14590 || (mode == Pmode && mem_mode != VOIDmode)
14591 #endif
14593 && GET_MODE_SIZE (GET_MODE (inner)) <= DWARF2_ADDR_SIZE)
14595 mem_loc_result = mem_loc_descriptor (inner,
14596 GET_MODE (inner),
14597 mem_mode, initialized);
14598 break;
14600 if (dwarf_strict && dwarf_version < 5)
14601 break;
14602 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (inner)))
14603 break;
14604 if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (inner))
14605 && (!SCALAR_INT_MODE_P (mode)
14606 || !SCALAR_INT_MODE_P (GET_MODE (inner))))
14607 break;
14608 else
14610 dw_die_ref type_die;
14611 dw_loc_descr_ref cvt;
14613 mem_loc_result = mem_loc_descriptor (inner,
14614 GET_MODE (inner),
14615 mem_mode, initialized);
14616 if (mem_loc_result == NULL)
14617 break;
14618 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
14619 if (type_die == NULL)
14621 mem_loc_result = NULL;
14622 break;
14624 if (GET_MODE_SIZE (mode)
14625 != GET_MODE_SIZE (GET_MODE (inner)))
14626 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14627 else
14628 cvt = new_loc_descr (dwarf_OP (DW_OP_reinterpret), 0, 0);
14629 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14630 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14631 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14632 add_loc_descr (&mem_loc_result, cvt);
14633 if (SCALAR_INT_MODE_P (mode)
14634 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
14636 /* Convert it to untyped afterwards. */
14637 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14638 add_loc_descr (&mem_loc_result, cvt);
14641 break;
14643 case REG:
14644 if (! SCALAR_INT_MODE_P (mode)
14645 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
14646 && rtl != arg_pointer_rtx
14647 && rtl != frame_pointer_rtx
14648 #ifdef POINTERS_EXTEND_UNSIGNED
14649 && (mode != Pmode || mem_mode == VOIDmode)
14650 #endif
14653 dw_die_ref type_die;
14654 unsigned int dbx_regnum;
14656 if (dwarf_strict && dwarf_version < 5)
14657 break;
14658 if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
14659 break;
14660 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
14661 if (type_die == NULL)
14662 break;
14664 dbx_regnum = dbx_reg_number (rtl);
14665 if (dbx_regnum == IGNORED_DWARF_REGNUM)
14666 break;
14667 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_regval_type),
14668 dbx_regnum, 0);
14669 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
14670 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
14671 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
14672 break;
14674 /* Whenever a register number forms a part of the description of the
14675 method for calculating the (dynamic) address of a memory resident
14676 object, DWARF rules require the register number be referred to as
14677 a "base register". This distinction is not based in any way upon
14678 what category of register the hardware believes the given register
14679 belongs to. This is strictly DWARF terminology we're dealing with
14680 here. Note that in cases where the location of a memory-resident
14681 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
14682 OP_CONST (0)) the actual DWARF location descriptor that we generate
14683 may just be OP_BASEREG (basereg). This may look deceptively like
14684 the object in question was allocated to a register (rather than in
14685 memory) so DWARF consumers need to be aware of the subtle
14686 distinction between OP_REG and OP_BASEREG. */
14687 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
14688 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
14689 else if (stack_realign_drap
14690 && crtl->drap_reg
14691 && crtl->args.internal_arg_pointer == rtl
14692 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
14694 /* If RTL is internal_arg_pointer, which has been optimized
14695 out, use DRAP instead. */
14696 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
14697 VAR_INIT_STATUS_INITIALIZED);
14699 break;
14701 case SIGN_EXTEND:
14702 case ZERO_EXTEND:
14703 if (!SCALAR_INT_MODE_P (mode))
14704 break;
14705 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
14706 mem_mode, VAR_INIT_STATUS_INITIALIZED);
14707 if (op0 == 0)
14708 break;
14709 else if (GET_CODE (rtl) == ZERO_EXTEND
14710 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
14711 && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
14712 < HOST_BITS_PER_WIDE_INT
14713 /* If DW_OP_const{1,2,4}u won't be used, it is shorter
14714 to expand zero extend as two shifts instead of
14715 masking. */
14716 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= 4)
14718 machine_mode imode = GET_MODE (XEXP (rtl, 0));
14719 mem_loc_result = op0;
14720 add_loc_descr (&mem_loc_result,
14721 int_loc_descriptor (GET_MODE_MASK (imode)));
14722 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
14724 else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
14726 int shift = DWARF2_ADDR_SIZE
14727 - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
14728 shift *= BITS_PER_UNIT;
14729 if (GET_CODE (rtl) == SIGN_EXTEND)
14730 op = DW_OP_shra;
14731 else
14732 op = DW_OP_shr;
14733 mem_loc_result = op0;
14734 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
14735 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
14736 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
14737 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14739 else if (!dwarf_strict || dwarf_version >= 5)
14741 dw_die_ref type_die1, type_die2;
14742 dw_loc_descr_ref cvt;
14744 type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
14745 GET_CODE (rtl) == ZERO_EXTEND);
14746 if (type_die1 == NULL)
14747 break;
14748 type_die2 = base_type_for_mode (mode, 1);
14749 if (type_die2 == NULL)
14750 break;
14751 mem_loc_result = op0;
14752 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14753 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14754 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
14755 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14756 add_loc_descr (&mem_loc_result, cvt);
14757 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14758 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14759 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
14760 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14761 add_loc_descr (&mem_loc_result, cvt);
14763 break;
14765 case MEM:
14767 rtx new_rtl = avoid_constant_pool_reference (rtl);
14768 if (new_rtl != rtl)
14770 mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
14771 initialized);
14772 if (mem_loc_result != NULL)
14773 return mem_loc_result;
14776 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
14777 get_address_mode (rtl), mode,
14778 VAR_INIT_STATUS_INITIALIZED);
14779 if (mem_loc_result == NULL)
14780 mem_loc_result = tls_mem_loc_descriptor (rtl);
14781 if (mem_loc_result != NULL)
14783 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
14784 || !SCALAR_INT_MODE_P(mode))
14786 dw_die_ref type_die;
14787 dw_loc_descr_ref deref;
14789 if (dwarf_strict && dwarf_version < 5)
14790 return NULL;
14791 type_die
14792 = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
14793 if (type_die == NULL)
14794 return NULL;
14795 deref = new_loc_descr (dwarf_OP (DW_OP_deref_type),
14796 GET_MODE_SIZE (mode), 0);
14797 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
14798 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
14799 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
14800 add_loc_descr (&mem_loc_result, deref);
14802 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
14803 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
14804 else
14805 add_loc_descr (&mem_loc_result,
14806 new_loc_descr (DW_OP_deref_size,
14807 GET_MODE_SIZE (mode), 0));
14809 break;
14811 case LO_SUM:
14812 return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
14814 case LABEL_REF:
14815 /* Some ports can transform a symbol ref into a label ref, because
14816 the symbol ref is too far away and has to be dumped into a constant
14817 pool. */
14818 case CONST:
14819 case SYMBOL_REF:
14820 if (!SCALAR_INT_MODE_P (mode)
14821 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
14822 #ifdef POINTERS_EXTEND_UNSIGNED
14823 && (mode != Pmode || mem_mode == VOIDmode)
14824 #endif
14826 break;
14827 if (GET_CODE (rtl) == SYMBOL_REF
14828 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
14830 dw_loc_descr_ref temp;
14832 /* If this is not defined, we have no way to emit the data. */
14833 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
14834 break;
14836 temp = new_addr_loc_descr (rtl, dtprel_true);
14838 /* We check for DWARF 5 here because gdb did not implement
14839 DW_OP_form_tls_address until after 7.12. */
14840 mem_loc_result = new_loc_descr ((dwarf_version >= 5
14841 ? DW_OP_form_tls_address
14842 : DW_OP_GNU_push_tls_address),
14843 0, 0);
14844 add_loc_descr (&mem_loc_result, temp);
14846 break;
14849 if (!const_ok_for_output (rtl))
14851 if (GET_CODE (rtl) == CONST)
14852 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14853 initialized);
14854 break;
14857 symref:
14858 mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
14859 vec_safe_push (used_rtx_array, rtl);
14860 break;
14862 case CONCAT:
14863 case CONCATN:
14864 case VAR_LOCATION:
14865 case DEBUG_IMPLICIT_PTR:
14866 expansion_failed (NULL_TREE, rtl,
14867 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
14868 return 0;
14870 case ENTRY_VALUE:
14871 if (dwarf_strict && dwarf_version < 5)
14872 return NULL;
14873 if (REG_P (ENTRY_VALUE_EXP (rtl)))
14875 if (!SCALAR_INT_MODE_P (mode)
14876 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
14877 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
14878 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14879 else
14881 unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
14882 if (dbx_regnum == IGNORED_DWARF_REGNUM)
14883 return NULL;
14884 op0 = one_reg_loc_descriptor (dbx_regnum,
14885 VAR_INIT_STATUS_INITIALIZED);
14888 else if (MEM_P (ENTRY_VALUE_EXP (rtl))
14889 && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
14891 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
14892 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14893 if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
14894 return NULL;
14896 else
14897 gcc_unreachable ();
14898 if (op0 == NULL)
14899 return NULL;
14900 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_entry_value), 0, 0);
14901 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
14902 mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
14903 break;
14905 case DEBUG_PARAMETER_REF:
14906 mem_loc_result = parameter_ref_descriptor (rtl);
14907 break;
14909 case PRE_MODIFY:
14910 /* Extract the PLUS expression nested inside and fall into
14911 PLUS code below. */
14912 rtl = XEXP (rtl, 1);
14913 goto plus;
14915 case PRE_INC:
14916 case PRE_DEC:
14917 /* Turn these into a PLUS expression and fall into the PLUS code
14918 below. */
14919 rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
14920 gen_int_mode (GET_CODE (rtl) == PRE_INC
14921 ? GET_MODE_UNIT_SIZE (mem_mode)
14922 : -GET_MODE_UNIT_SIZE (mem_mode),
14923 mode));
14925 /* fall through */
14927 case PLUS:
14928 plus:
14929 if (is_based_loc (rtl)
14930 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
14931 || XEXP (rtl, 0) == arg_pointer_rtx
14932 || XEXP (rtl, 0) == frame_pointer_rtx)
14933 && SCALAR_INT_MODE_P (mode))
14934 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
14935 INTVAL (XEXP (rtl, 1)),
14936 VAR_INIT_STATUS_INITIALIZED);
14937 else
14939 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14940 VAR_INIT_STATUS_INITIALIZED);
14941 if (mem_loc_result == 0)
14942 break;
14944 if (CONST_INT_P (XEXP (rtl, 1))
14945 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
14946 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
14947 else
14949 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14950 VAR_INIT_STATUS_INITIALIZED);
14951 if (op1 == 0)
14952 return NULL;
14953 add_loc_descr (&mem_loc_result, op1);
14954 add_loc_descr (&mem_loc_result,
14955 new_loc_descr (DW_OP_plus, 0, 0));
14958 break;
14960 /* If a pseudo-reg is optimized away, it is possible for it to
14961 be replaced with a MEM containing a multiply or shift. */
14962 case MINUS:
14963 op = DW_OP_minus;
14964 goto do_binop;
14966 case MULT:
14967 op = DW_OP_mul;
14968 goto do_binop;
14970 case DIV:
14971 if ((!dwarf_strict || dwarf_version >= 5)
14972 && SCALAR_INT_MODE_P (mode)
14973 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
14975 mem_loc_result = typed_binop (DW_OP_div, rtl,
14976 base_type_for_mode (mode, 0),
14977 mode, mem_mode);
14978 break;
14980 op = DW_OP_div;
14981 goto do_binop;
14983 case UMOD:
14984 op = DW_OP_mod;
14985 goto do_binop;
14987 case ASHIFT:
14988 op = DW_OP_shl;
14989 goto do_shift;
14991 case ASHIFTRT:
14992 op = DW_OP_shra;
14993 goto do_shift;
14995 case LSHIFTRT:
14996 op = DW_OP_shr;
14997 goto do_shift;
14999 do_shift:
15000 if (!SCALAR_INT_MODE_P (mode))
15001 break;
15002 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15003 VAR_INIT_STATUS_INITIALIZED);
15005 rtx rtlop1 = XEXP (rtl, 1);
15006 if (GET_MODE (rtlop1) != VOIDmode
15007 && GET_MODE_BITSIZE (GET_MODE (rtlop1))
15008 < GET_MODE_BITSIZE (mode))
15009 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
15010 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
15011 VAR_INIT_STATUS_INITIALIZED);
15014 if (op0 == 0 || op1 == 0)
15015 break;
15017 mem_loc_result = op0;
15018 add_loc_descr (&mem_loc_result, op1);
15019 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15020 break;
15022 case AND:
15023 op = DW_OP_and;
15024 goto do_binop;
15026 case IOR:
15027 op = DW_OP_or;
15028 goto do_binop;
15030 case XOR:
15031 op = DW_OP_xor;
15032 goto do_binop;
15034 do_binop:
15035 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15036 VAR_INIT_STATUS_INITIALIZED);
15037 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15038 VAR_INIT_STATUS_INITIALIZED);
15040 if (op0 == 0 || op1 == 0)
15041 break;
15043 mem_loc_result = op0;
15044 add_loc_descr (&mem_loc_result, op1);
15045 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15046 break;
15048 case MOD:
15049 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
15050 && (!dwarf_strict || dwarf_version >= 5))
15052 mem_loc_result = typed_binop (DW_OP_mod, rtl,
15053 base_type_for_mode (mode, 0),
15054 mode, mem_mode);
15055 break;
15058 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15059 VAR_INIT_STATUS_INITIALIZED);
15060 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15061 VAR_INIT_STATUS_INITIALIZED);
15063 if (op0 == 0 || op1 == 0)
15064 break;
15066 mem_loc_result = op0;
15067 add_loc_descr (&mem_loc_result, op1);
15068 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
15069 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
15070 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
15071 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
15072 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
15073 break;
15075 case UDIV:
15076 if ((!dwarf_strict || dwarf_version >= 5)
15077 && SCALAR_INT_MODE_P (mode))
15079 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
15081 op = DW_OP_div;
15082 goto do_binop;
15084 mem_loc_result = typed_binop (DW_OP_div, rtl,
15085 base_type_for_mode (mode, 1),
15086 mode, mem_mode);
15088 break;
15090 case NOT:
15091 op = DW_OP_not;
15092 goto do_unop;
15094 case ABS:
15095 op = DW_OP_abs;
15096 goto do_unop;
15098 case NEG:
15099 op = DW_OP_neg;
15100 goto do_unop;
15102 do_unop:
15103 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15104 VAR_INIT_STATUS_INITIALIZED);
15106 if (op0 == 0)
15107 break;
15109 mem_loc_result = op0;
15110 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15111 break;
15113 case CONST_INT:
15114 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
15115 #ifdef POINTERS_EXTEND_UNSIGNED
15116 || (mode == Pmode
15117 && mem_mode != VOIDmode
15118 && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
15119 #endif
15122 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
15123 break;
15125 if ((!dwarf_strict || dwarf_version >= 5)
15126 && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
15127 || GET_MODE_BITSIZE (mode) == HOST_BITS_PER_DOUBLE_INT))
15129 dw_die_ref type_die = base_type_for_mode (mode, 1);
15130 machine_mode amode;
15131 if (type_die == NULL)
15132 return NULL;
15133 amode = mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT,
15134 MODE_INT, 0);
15135 if (INTVAL (rtl) >= 0
15136 && amode != BLKmode
15137 && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
15138 /* const DW_OP_convert <XXX> vs.
15139 DW_OP_const_type <XXX, 1, const>. */
15140 && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
15141 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (mode))
15143 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
15144 op0 = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15145 op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15146 op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15147 op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
15148 add_loc_descr (&mem_loc_result, op0);
15149 return mem_loc_result;
15151 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0,
15152 INTVAL (rtl));
15153 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15154 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15155 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
15156 if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
15157 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
15158 else
15160 mem_loc_result->dw_loc_oprnd2.val_class
15161 = dw_val_class_const_double;
15162 mem_loc_result->dw_loc_oprnd2.v.val_double
15163 = double_int::from_shwi (INTVAL (rtl));
15166 break;
15168 case CONST_DOUBLE:
15169 if (!dwarf_strict || dwarf_version >= 5)
15171 dw_die_ref type_die;
15173 /* Note that if TARGET_SUPPORTS_WIDE_INT == 0, a
15174 CONST_DOUBLE rtx could represent either a large integer
15175 or a floating-point constant. If TARGET_SUPPORTS_WIDE_INT != 0,
15176 the value is always a floating point constant.
15178 When it is an integer, a CONST_DOUBLE is used whenever
15179 the constant requires 2 HWIs to be adequately represented.
15180 We output CONST_DOUBLEs as blocks. */
15181 if (mode == VOIDmode
15182 || (GET_MODE (rtl) == VOIDmode
15183 && GET_MODE_BITSIZE (mode) != HOST_BITS_PER_DOUBLE_INT))
15184 break;
15185 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
15186 if (type_die == NULL)
15187 return NULL;
15188 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0, 0);
15189 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15190 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15191 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
15192 #if TARGET_SUPPORTS_WIDE_INT == 0
15193 if (!SCALAR_FLOAT_MODE_P (mode))
15195 mem_loc_result->dw_loc_oprnd2.val_class
15196 = dw_val_class_const_double;
15197 mem_loc_result->dw_loc_oprnd2.v.val_double
15198 = rtx_to_double_int (rtl);
15200 else
15201 #endif
15203 unsigned int length = GET_MODE_SIZE (mode);
15204 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
15206 insert_float (rtl, array);
15207 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
15208 mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
15209 mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
15210 mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
15213 break;
15215 case CONST_WIDE_INT:
15216 if (!dwarf_strict || dwarf_version >= 5)
15218 dw_die_ref type_die;
15220 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
15221 if (type_die == NULL)
15222 return NULL;
15223 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0, 0);
15224 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15225 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15226 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
15227 mem_loc_result->dw_loc_oprnd2.val_class
15228 = dw_val_class_wide_int;
15229 mem_loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
15230 *mem_loc_result->dw_loc_oprnd2.v.val_wide = rtx_mode_t (rtl, mode);
15232 break;
15234 case EQ:
15235 mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
15236 break;
15238 case GE:
15239 mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
15240 break;
15242 case GT:
15243 mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
15244 break;
15246 case LE:
15247 mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
15248 break;
15250 case LT:
15251 mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
15252 break;
15254 case NE:
15255 mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
15256 break;
15258 case GEU:
15259 mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
15260 break;
15262 case GTU:
15263 mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
15264 break;
15266 case LEU:
15267 mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
15268 break;
15270 case LTU:
15271 mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
15272 break;
15274 case UMIN:
15275 case UMAX:
15276 if (!SCALAR_INT_MODE_P (mode))
15277 break;
15278 /* FALLTHRU */
15279 case SMIN:
15280 case SMAX:
15281 mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
15282 break;
15284 case ZERO_EXTRACT:
15285 case SIGN_EXTRACT:
15286 if (CONST_INT_P (XEXP (rtl, 1))
15287 && CONST_INT_P (XEXP (rtl, 2))
15288 && ((unsigned) INTVAL (XEXP (rtl, 1))
15289 + (unsigned) INTVAL (XEXP (rtl, 2))
15290 <= GET_MODE_BITSIZE (mode))
15291 && SCALAR_INT_MODE_P (mode)
15292 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
15293 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
15295 int shift, size;
15296 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
15297 mem_mode, VAR_INIT_STATUS_INITIALIZED);
15298 if (op0 == 0)
15299 break;
15300 if (GET_CODE (rtl) == SIGN_EXTRACT)
15301 op = DW_OP_shra;
15302 else
15303 op = DW_OP_shr;
15304 mem_loc_result = op0;
15305 size = INTVAL (XEXP (rtl, 1));
15306 shift = INTVAL (XEXP (rtl, 2));
15307 if (BITS_BIG_ENDIAN)
15308 shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
15309 - shift - size;
15310 if (shift + size != (int) DWARF2_ADDR_SIZE)
15312 add_loc_descr (&mem_loc_result,
15313 int_loc_descriptor (DWARF2_ADDR_SIZE
15314 - shift - size));
15315 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
15317 if (size != (int) DWARF2_ADDR_SIZE)
15319 add_loc_descr (&mem_loc_result,
15320 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
15321 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15324 break;
15326 case IF_THEN_ELSE:
15328 dw_loc_descr_ref op2, bra_node, drop_node;
15329 op0 = mem_loc_descriptor (XEXP (rtl, 0),
15330 GET_MODE (XEXP (rtl, 0)) == VOIDmode
15331 ? word_mode : GET_MODE (XEXP (rtl, 0)),
15332 mem_mode, VAR_INIT_STATUS_INITIALIZED);
15333 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15334 VAR_INIT_STATUS_INITIALIZED);
15335 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
15336 VAR_INIT_STATUS_INITIALIZED);
15337 if (op0 == NULL || op1 == NULL || op2 == NULL)
15338 break;
15340 mem_loc_result = op1;
15341 add_loc_descr (&mem_loc_result, op2);
15342 add_loc_descr (&mem_loc_result, op0);
15343 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
15344 add_loc_descr (&mem_loc_result, bra_node);
15345 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
15346 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
15347 add_loc_descr (&mem_loc_result, drop_node);
15348 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15349 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
15351 break;
15353 case FLOAT_EXTEND:
15354 case FLOAT_TRUNCATE:
15355 case FLOAT:
15356 case UNSIGNED_FLOAT:
15357 case FIX:
15358 case UNSIGNED_FIX:
15359 if (!dwarf_strict || dwarf_version >= 5)
15361 dw_die_ref type_die;
15362 dw_loc_descr_ref cvt;
15364 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
15365 mem_mode, VAR_INIT_STATUS_INITIALIZED);
15366 if (op0 == NULL)
15367 break;
15368 if (SCALAR_INT_MODE_P (GET_MODE (XEXP (rtl, 0)))
15369 && (GET_CODE (rtl) == FLOAT
15370 || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
15371 <= DWARF2_ADDR_SIZE))
15373 type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
15374 GET_CODE (rtl) == UNSIGNED_FLOAT);
15375 if (type_die == NULL)
15376 break;
15377 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15378 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15379 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15380 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15381 add_loc_descr (&op0, cvt);
15383 type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
15384 if (type_die == NULL)
15385 break;
15386 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15387 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15388 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15389 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15390 add_loc_descr (&op0, cvt);
15391 if (SCALAR_INT_MODE_P (mode)
15392 && (GET_CODE (rtl) == FIX
15393 || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
15395 op0 = convert_descriptor_to_mode (mode, op0);
15396 if (op0 == NULL)
15397 break;
15399 mem_loc_result = op0;
15401 break;
15403 case CLZ:
15404 case CTZ:
15405 case FFS:
15406 mem_loc_result = clz_loc_descriptor (rtl, mode, mem_mode);
15407 break;
15409 case POPCOUNT:
15410 case PARITY:
15411 mem_loc_result = popcount_loc_descriptor (rtl, mode, mem_mode);
15412 break;
15414 case BSWAP:
15415 mem_loc_result = bswap_loc_descriptor (rtl, mode, mem_mode);
15416 break;
15418 case ROTATE:
15419 case ROTATERT:
15420 mem_loc_result = rotate_loc_descriptor (rtl, mode, mem_mode);
15421 break;
15423 case COMPARE:
15424 /* In theory, we could implement the above. */
15425 /* DWARF cannot represent the unsigned compare operations
15426 natively. */
15427 case SS_MULT:
15428 case US_MULT:
15429 case SS_DIV:
15430 case US_DIV:
15431 case SS_PLUS:
15432 case US_PLUS:
15433 case SS_MINUS:
15434 case US_MINUS:
15435 case SS_NEG:
15436 case US_NEG:
15437 case SS_ABS:
15438 case SS_ASHIFT:
15439 case US_ASHIFT:
15440 case SS_TRUNCATE:
15441 case US_TRUNCATE:
15442 case UNORDERED:
15443 case ORDERED:
15444 case UNEQ:
15445 case UNGE:
15446 case UNGT:
15447 case UNLE:
15448 case UNLT:
15449 case LTGT:
15450 case FRACT_CONVERT:
15451 case UNSIGNED_FRACT_CONVERT:
15452 case SAT_FRACT:
15453 case UNSIGNED_SAT_FRACT:
15454 case SQRT:
15455 case ASM_OPERANDS:
15456 case VEC_MERGE:
15457 case VEC_SELECT:
15458 case VEC_CONCAT:
15459 case VEC_DUPLICATE:
15460 case UNSPEC:
15461 case HIGH:
15462 case FMA:
15463 case STRICT_LOW_PART:
15464 case CONST_VECTOR:
15465 case CONST_FIXED:
15466 case CLRSB:
15467 case CLOBBER:
15468 /* If delegitimize_address couldn't do anything with the UNSPEC, we
15469 can't express it in the debug info. This can happen e.g. with some
15470 TLS UNSPECs. */
15471 break;
15473 case CONST_STRING:
15474 resolve_one_addr (&rtl);
15475 goto symref;
15477 /* RTL sequences inside PARALLEL record a series of DWARF operations for
15478 the expression. An UNSPEC rtx represents a raw DWARF operation,
15479 new_loc_descr is called for it to build the operation directly.
15480 Otherwise mem_loc_descriptor is called recursively. */
15481 case PARALLEL:
15483 int index = 0;
15484 dw_loc_descr_ref exp_result = NULL;
15486 for (; index < XVECLEN (rtl, 0); index++)
15488 rtx elem = XVECEXP (rtl, 0, index);
15489 if (GET_CODE (elem) == UNSPEC)
15491 /* Each DWARF operation UNSPEC contain two operands, if
15492 one operand is not used for the operation, const0_rtx is
15493 passed. */
15494 gcc_assert (XVECLEN (elem, 0) == 2);
15496 HOST_WIDE_INT dw_op = XINT (elem, 1);
15497 HOST_WIDE_INT oprnd1 = INTVAL (XVECEXP (elem, 0, 0));
15498 HOST_WIDE_INT oprnd2 = INTVAL (XVECEXP (elem, 0, 1));
15499 exp_result
15500 = new_loc_descr ((enum dwarf_location_atom) dw_op, oprnd1,
15501 oprnd2);
15503 else
15504 exp_result
15505 = mem_loc_descriptor (elem, mode, mem_mode,
15506 VAR_INIT_STATUS_INITIALIZED);
15508 if (!mem_loc_result)
15509 mem_loc_result = exp_result;
15510 else
15511 add_loc_descr (&mem_loc_result, exp_result);
15514 break;
15517 default:
15518 if (flag_checking)
15520 print_rtl (stderr, rtl);
15521 gcc_unreachable ();
15523 break;
15526 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
15527 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
15529 return mem_loc_result;
15532 /* Return a descriptor that describes the concatenation of two locations.
15533 This is typically a complex variable. */
15535 static dw_loc_descr_ref
15536 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
15538 dw_loc_descr_ref cc_loc_result = NULL;
15539 dw_loc_descr_ref x0_ref
15540 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15541 dw_loc_descr_ref x1_ref
15542 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15544 if (x0_ref == 0 || x1_ref == 0)
15545 return 0;
15547 cc_loc_result = x0_ref;
15548 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
15550 add_loc_descr (&cc_loc_result, x1_ref);
15551 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
15553 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
15554 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
15556 return cc_loc_result;
15559 /* Return a descriptor that describes the concatenation of N
15560 locations. */
15562 static dw_loc_descr_ref
15563 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
15565 unsigned int i;
15566 dw_loc_descr_ref cc_loc_result = NULL;
15567 unsigned int n = XVECLEN (concatn, 0);
15569 for (i = 0; i < n; ++i)
15571 dw_loc_descr_ref ref;
15572 rtx x = XVECEXP (concatn, 0, i);
15574 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15575 if (ref == NULL)
15576 return NULL;
15578 add_loc_descr (&cc_loc_result, ref);
15579 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
15582 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
15583 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
15585 return cc_loc_result;
15588 /* Helper function for loc_descriptor. Return DW_OP_implicit_pointer
15589 for DEBUG_IMPLICIT_PTR RTL. */
15591 static dw_loc_descr_ref
15592 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
15594 dw_loc_descr_ref ret;
15595 dw_die_ref ref;
15597 if (dwarf_strict && dwarf_version < 5)
15598 return NULL;
15599 gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
15600 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
15601 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
15602 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
15603 ret = new_loc_descr (dwarf_OP (DW_OP_implicit_pointer), 0, offset);
15604 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
15605 if (ref)
15607 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15608 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
15609 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
15611 else
15613 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
15614 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
15616 return ret;
15619 /* Output a proper Dwarf location descriptor for a variable or parameter
15620 which is either allocated in a register or in a memory location. For a
15621 register, we just generate an OP_REG and the register number. For a
15622 memory location we provide a Dwarf postfix expression describing how to
15623 generate the (dynamic) address of the object onto the address stack.
15625 MODE is mode of the decl if this loc_descriptor is going to be used in
15626 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
15627 allowed, VOIDmode otherwise.
15629 If we don't know how to describe it, return 0. */
15631 static dw_loc_descr_ref
15632 loc_descriptor (rtx rtl, machine_mode mode,
15633 enum var_init_status initialized)
15635 dw_loc_descr_ref loc_result = NULL;
15637 switch (GET_CODE (rtl))
15639 case SUBREG:
15640 /* The case of a subreg may arise when we have a local (register)
15641 variable or a formal (register) parameter which doesn't quite fill
15642 up an entire register. For now, just assume that it is
15643 legitimate to make the Dwarf info refer to the whole register which
15644 contains the given subreg. */
15645 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
15646 loc_result = loc_descriptor (SUBREG_REG (rtl),
15647 GET_MODE (SUBREG_REG (rtl)), initialized);
15648 else
15649 goto do_default;
15650 break;
15652 case REG:
15653 loc_result = reg_loc_descriptor (rtl, initialized);
15654 break;
15656 case MEM:
15657 loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
15658 GET_MODE (rtl), initialized);
15659 if (loc_result == NULL)
15660 loc_result = tls_mem_loc_descriptor (rtl);
15661 if (loc_result == NULL)
15663 rtx new_rtl = avoid_constant_pool_reference (rtl);
15664 if (new_rtl != rtl)
15665 loc_result = loc_descriptor (new_rtl, mode, initialized);
15667 break;
15669 case CONCAT:
15670 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
15671 initialized);
15672 break;
15674 case CONCATN:
15675 loc_result = concatn_loc_descriptor (rtl, initialized);
15676 break;
15678 case VAR_LOCATION:
15679 /* Single part. */
15680 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
15682 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
15683 if (GET_CODE (loc) == EXPR_LIST)
15684 loc = XEXP (loc, 0);
15685 loc_result = loc_descriptor (loc, mode, initialized);
15686 break;
15689 rtl = XEXP (rtl, 1);
15690 /* FALLTHRU */
15692 case PARALLEL:
15694 rtvec par_elems = XVEC (rtl, 0);
15695 int num_elem = GET_NUM_ELEM (par_elems);
15696 machine_mode mode;
15697 int i;
15699 /* Create the first one, so we have something to add to. */
15700 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
15701 VOIDmode, initialized);
15702 if (loc_result == NULL)
15703 return NULL;
15704 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
15705 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
15706 for (i = 1; i < num_elem; i++)
15708 dw_loc_descr_ref temp;
15710 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
15711 VOIDmode, initialized);
15712 if (temp == NULL)
15713 return NULL;
15714 add_loc_descr (&loc_result, temp);
15715 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
15716 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
15719 break;
15721 case CONST_INT:
15722 if (mode != VOIDmode && mode != BLKmode)
15723 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
15724 INTVAL (rtl));
15725 break;
15727 case CONST_DOUBLE:
15728 if (mode == VOIDmode)
15729 mode = GET_MODE (rtl);
15731 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
15733 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
15735 /* Note that a CONST_DOUBLE rtx could represent either an integer
15736 or a floating-point constant. A CONST_DOUBLE is used whenever
15737 the constant requires more than one word in order to be
15738 adequately represented. We output CONST_DOUBLEs as blocks. */
15739 loc_result = new_loc_descr (DW_OP_implicit_value,
15740 GET_MODE_SIZE (mode), 0);
15741 #if TARGET_SUPPORTS_WIDE_INT == 0
15742 if (!SCALAR_FLOAT_MODE_P (mode))
15744 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
15745 loc_result->dw_loc_oprnd2.v.val_double
15746 = rtx_to_double_int (rtl);
15748 else
15749 #endif
15751 unsigned int length = GET_MODE_SIZE (mode);
15752 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
15754 insert_float (rtl, array);
15755 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
15756 loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
15757 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
15758 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
15761 break;
15763 case CONST_WIDE_INT:
15764 if (mode == VOIDmode)
15765 mode = GET_MODE (rtl);
15767 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
15769 loc_result = new_loc_descr (DW_OP_implicit_value,
15770 GET_MODE_SIZE (mode), 0);
15771 loc_result->dw_loc_oprnd2.val_class = dw_val_class_wide_int;
15772 loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
15773 *loc_result->dw_loc_oprnd2.v.val_wide = rtx_mode_t (rtl, mode);
15775 break;
15777 case CONST_VECTOR:
15778 if (mode == VOIDmode)
15779 mode = GET_MODE (rtl);
15781 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
15783 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
15784 unsigned int length = CONST_VECTOR_NUNITS (rtl);
15785 unsigned char *array
15786 = ggc_vec_alloc<unsigned char> (length * elt_size);
15787 unsigned int i;
15788 unsigned char *p;
15789 machine_mode imode = GET_MODE_INNER (mode);
15791 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
15792 switch (GET_MODE_CLASS (mode))
15794 case MODE_VECTOR_INT:
15795 for (i = 0, p = array; i < length; i++, p += elt_size)
15797 rtx elt = CONST_VECTOR_ELT (rtl, i);
15798 insert_wide_int (rtx_mode_t (elt, imode), p, elt_size);
15800 break;
15802 case MODE_VECTOR_FLOAT:
15803 for (i = 0, p = array; i < length; i++, p += elt_size)
15805 rtx elt = CONST_VECTOR_ELT (rtl, i);
15806 insert_float (elt, p);
15808 break;
15810 default:
15811 gcc_unreachable ();
15814 loc_result = new_loc_descr (DW_OP_implicit_value,
15815 length * elt_size, 0);
15816 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
15817 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
15818 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
15819 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
15821 break;
15823 case CONST:
15824 if (mode == VOIDmode
15825 || CONST_SCALAR_INT_P (XEXP (rtl, 0))
15826 || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
15827 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
15829 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
15830 break;
15832 /* FALLTHROUGH */
15833 case SYMBOL_REF:
15834 if (!const_ok_for_output (rtl))
15835 break;
15836 /* FALLTHROUGH */
15837 case LABEL_REF:
15838 if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
15839 && (dwarf_version >= 4 || !dwarf_strict))
15841 loc_result = new_addr_loc_descr (rtl, dtprel_false);
15842 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
15843 vec_safe_push (used_rtx_array, rtl);
15845 break;
15847 case DEBUG_IMPLICIT_PTR:
15848 loc_result = implicit_ptr_descriptor (rtl, 0);
15849 break;
15851 case PLUS:
15852 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
15853 && CONST_INT_P (XEXP (rtl, 1)))
15855 loc_result
15856 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
15857 break;
15859 /* FALLTHRU */
15860 do_default:
15861 default:
15862 if ((SCALAR_INT_MODE_P (mode)
15863 && GET_MODE (rtl) == mode
15864 && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
15865 && dwarf_version >= 4)
15866 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
15868 /* Value expression. */
15869 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
15870 if (loc_result)
15871 add_loc_descr (&loc_result,
15872 new_loc_descr (DW_OP_stack_value, 0, 0));
15874 break;
15877 return loc_result;
15880 /* We need to figure out what section we should use as the base for the
15881 address ranges where a given location is valid.
15882 1. If this particular DECL has a section associated with it, use that.
15883 2. If this function has a section associated with it, use that.
15884 3. Otherwise, use the text section.
15885 XXX: If you split a variable across multiple sections, we won't notice. */
15887 static const char *
15888 secname_for_decl (const_tree decl)
15890 const char *secname;
15892 if (VAR_OR_FUNCTION_DECL_P (decl)
15893 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl) || TREE_STATIC (decl))
15894 && DECL_SECTION_NAME (decl))
15895 secname = DECL_SECTION_NAME (decl);
15896 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
15897 secname = DECL_SECTION_NAME (current_function_decl);
15898 else if (cfun && in_cold_section_p)
15899 secname = crtl->subsections.cold_section_label;
15900 else
15901 secname = text_section_label;
15903 return secname;
15906 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
15908 static bool
15909 decl_by_reference_p (tree decl)
15911 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
15912 || VAR_P (decl))
15913 && DECL_BY_REFERENCE (decl));
15916 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
15917 for VARLOC. */
15919 static dw_loc_descr_ref
15920 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
15921 enum var_init_status initialized)
15923 int have_address = 0;
15924 dw_loc_descr_ref descr;
15925 machine_mode mode;
15927 if (want_address != 2)
15929 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
15930 /* Single part. */
15931 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
15933 varloc = PAT_VAR_LOCATION_LOC (varloc);
15934 if (GET_CODE (varloc) == EXPR_LIST)
15935 varloc = XEXP (varloc, 0);
15936 mode = GET_MODE (varloc);
15937 if (MEM_P (varloc))
15939 rtx addr = XEXP (varloc, 0);
15940 descr = mem_loc_descriptor (addr, get_address_mode (varloc),
15941 mode, initialized);
15942 if (descr)
15943 have_address = 1;
15944 else
15946 rtx x = avoid_constant_pool_reference (varloc);
15947 if (x != varloc)
15948 descr = mem_loc_descriptor (x, mode, VOIDmode,
15949 initialized);
15952 else
15953 descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
15955 else
15956 return 0;
15958 else
15960 if (GET_CODE (varloc) == VAR_LOCATION)
15961 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
15962 else
15963 mode = DECL_MODE (loc);
15964 descr = loc_descriptor (varloc, mode, initialized);
15965 have_address = 1;
15968 if (!descr)
15969 return 0;
15971 if (want_address == 2 && !have_address
15972 && (dwarf_version >= 4 || !dwarf_strict))
15974 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
15976 expansion_failed (loc, NULL_RTX,
15977 "DWARF address size mismatch");
15978 return 0;
15980 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
15981 have_address = 1;
15983 /* Show if we can't fill the request for an address. */
15984 if (want_address && !have_address)
15986 expansion_failed (loc, NULL_RTX,
15987 "Want address and only have value");
15988 return 0;
15991 /* If we've got an address and don't want one, dereference. */
15992 if (!want_address && have_address)
15994 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
15995 enum dwarf_location_atom op;
15997 if (size > DWARF2_ADDR_SIZE || size == -1)
15999 expansion_failed (loc, NULL_RTX,
16000 "DWARF address size mismatch");
16001 return 0;
16003 else if (size == DWARF2_ADDR_SIZE)
16004 op = DW_OP_deref;
16005 else
16006 op = DW_OP_deref_size;
16008 add_loc_descr (&descr, new_loc_descr (op, size, 0));
16011 return descr;
16014 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
16015 if it is not possible. */
16017 static dw_loc_descr_ref
16018 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
16020 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
16021 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
16022 else if (dwarf_version >= 3 || !dwarf_strict)
16023 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
16024 else
16025 return NULL;
16028 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
16029 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
16031 static dw_loc_descr_ref
16032 dw_sra_loc_expr (tree decl, rtx loc)
16034 rtx p;
16035 unsigned HOST_WIDE_INT padsize = 0;
16036 dw_loc_descr_ref descr, *descr_tail;
16037 unsigned HOST_WIDE_INT decl_size;
16038 rtx varloc;
16039 enum var_init_status initialized;
16041 if (DECL_SIZE (decl) == NULL
16042 || !tree_fits_uhwi_p (DECL_SIZE (decl)))
16043 return NULL;
16045 decl_size = tree_to_uhwi (DECL_SIZE (decl));
16046 descr = NULL;
16047 descr_tail = &descr;
16049 for (p = loc; p; p = XEXP (p, 1))
16051 unsigned HOST_WIDE_INT bitsize = decl_piece_bitsize (p);
16052 rtx loc_note = *decl_piece_varloc_ptr (p);
16053 dw_loc_descr_ref cur_descr;
16054 dw_loc_descr_ref *tail, last = NULL;
16055 unsigned HOST_WIDE_INT opsize = 0;
16057 if (loc_note == NULL_RTX
16058 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
16060 padsize += bitsize;
16061 continue;
16063 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
16064 varloc = NOTE_VAR_LOCATION (loc_note);
16065 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
16066 if (cur_descr == NULL)
16068 padsize += bitsize;
16069 continue;
16072 /* Check that cur_descr either doesn't use
16073 DW_OP_*piece operations, or their sum is equal
16074 to bitsize. Otherwise we can't embed it. */
16075 for (tail = &cur_descr; *tail != NULL;
16076 tail = &(*tail)->dw_loc_next)
16077 if ((*tail)->dw_loc_opc == DW_OP_piece)
16079 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
16080 * BITS_PER_UNIT;
16081 last = *tail;
16083 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
16085 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
16086 last = *tail;
16089 if (last != NULL && opsize != bitsize)
16091 padsize += bitsize;
16092 /* Discard the current piece of the descriptor and release any
16093 addr_table entries it uses. */
16094 remove_loc_list_addr_table_entries (cur_descr);
16095 continue;
16098 /* If there is a hole, add DW_OP_*piece after empty DWARF
16099 expression, which means that those bits are optimized out. */
16100 if (padsize)
16102 if (padsize > decl_size)
16104 remove_loc_list_addr_table_entries (cur_descr);
16105 goto discard_descr;
16107 decl_size -= padsize;
16108 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
16109 if (*descr_tail == NULL)
16111 remove_loc_list_addr_table_entries (cur_descr);
16112 goto discard_descr;
16114 descr_tail = &(*descr_tail)->dw_loc_next;
16115 padsize = 0;
16117 *descr_tail = cur_descr;
16118 descr_tail = tail;
16119 if (bitsize > decl_size)
16120 goto discard_descr;
16121 decl_size -= bitsize;
16122 if (last == NULL)
16124 HOST_WIDE_INT offset = 0;
16125 if (GET_CODE (varloc) == VAR_LOCATION
16126 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
16128 varloc = PAT_VAR_LOCATION_LOC (varloc);
16129 if (GET_CODE (varloc) == EXPR_LIST)
16130 varloc = XEXP (varloc, 0);
16134 if (GET_CODE (varloc) == CONST
16135 || GET_CODE (varloc) == SIGN_EXTEND
16136 || GET_CODE (varloc) == ZERO_EXTEND)
16137 varloc = XEXP (varloc, 0);
16138 else if (GET_CODE (varloc) == SUBREG)
16139 varloc = SUBREG_REG (varloc);
16140 else
16141 break;
16143 while (1);
16144 /* DW_OP_bit_size offset should be zero for register
16145 or implicit location descriptions and empty location
16146 descriptions, but for memory addresses needs big endian
16147 adjustment. */
16148 if (MEM_P (varloc))
16150 unsigned HOST_WIDE_INT memsize
16151 = MEM_SIZE (varloc) * BITS_PER_UNIT;
16152 if (memsize != bitsize)
16154 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
16155 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
16156 goto discard_descr;
16157 if (memsize < bitsize)
16158 goto discard_descr;
16159 if (BITS_BIG_ENDIAN)
16160 offset = memsize - bitsize;
16164 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
16165 if (*descr_tail == NULL)
16166 goto discard_descr;
16167 descr_tail = &(*descr_tail)->dw_loc_next;
16171 /* If there were any non-empty expressions, add padding till the end of
16172 the decl. */
16173 if (descr != NULL && decl_size != 0)
16175 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
16176 if (*descr_tail == NULL)
16177 goto discard_descr;
16179 return descr;
16181 discard_descr:
16182 /* Discard the descriptor and release any addr_table entries it uses. */
16183 remove_loc_list_addr_table_entries (descr);
16184 return NULL;
16187 /* Return the dwarf representation of the location list LOC_LIST of
16188 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
16189 function. */
16191 static dw_loc_list_ref
16192 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
16194 const char *endname, *secname;
16195 rtx varloc;
16196 enum var_init_status initialized;
16197 struct var_loc_node *node;
16198 dw_loc_descr_ref descr;
16199 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
16200 dw_loc_list_ref list = NULL;
16201 dw_loc_list_ref *listp = &list;
16203 /* Now that we know what section we are using for a base,
16204 actually construct the list of locations.
16205 The first location information is what is passed to the
16206 function that creates the location list, and the remaining
16207 locations just get added on to that list.
16208 Note that we only know the start address for a location
16209 (IE location changes), so to build the range, we use
16210 the range [current location start, next location start].
16211 This means we have to special case the last node, and generate
16212 a range of [last location start, end of function label]. */
16214 secname = secname_for_decl (decl);
16216 for (node = loc_list->first; node; node = node->next)
16217 if (GET_CODE (node->loc) == EXPR_LIST
16218 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
16220 if (GET_CODE (node->loc) == EXPR_LIST)
16222 /* This requires DW_OP_{,bit_}piece, which is not usable
16223 inside DWARF expressions. */
16224 if (want_address != 2)
16225 continue;
16226 descr = dw_sra_loc_expr (decl, node->loc);
16227 if (descr == NULL)
16228 continue;
16230 else
16232 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
16233 varloc = NOTE_VAR_LOCATION (node->loc);
16234 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
16236 if (descr)
16238 bool range_across_switch = false;
16239 /* If section switch happens in between node->label
16240 and node->next->label (or end of function) and
16241 we can't emit it as a single entry list,
16242 emit two ranges, first one ending at the end
16243 of first partition and second one starting at the
16244 beginning of second partition. */
16245 if (node == loc_list->last_before_switch
16246 && (node != loc_list->first || loc_list->first->next)
16247 && current_function_decl)
16249 endname = cfun->fde->dw_fde_end;
16250 range_across_switch = true;
16252 /* The variable has a location between NODE->LABEL and
16253 NODE->NEXT->LABEL. */
16254 else if (node->next)
16255 endname = node->next->label;
16256 /* If the variable has a location at the last label
16257 it keeps its location until the end of function. */
16258 else if (!current_function_decl)
16259 endname = text_end_label;
16260 else
16262 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
16263 current_function_funcdef_no);
16264 endname = ggc_strdup (label_id);
16267 *listp = new_loc_list (descr, node->label, endname, secname);
16268 if (TREE_CODE (decl) == PARM_DECL
16269 && node == loc_list->first
16270 && NOTE_P (node->loc)
16271 && strcmp (node->label, endname) == 0)
16272 (*listp)->force = true;
16273 listp = &(*listp)->dw_loc_next;
16275 if (range_across_switch)
16277 if (GET_CODE (node->loc) == EXPR_LIST)
16278 descr = dw_sra_loc_expr (decl, node->loc);
16279 else
16281 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
16282 varloc = NOTE_VAR_LOCATION (node->loc);
16283 descr = dw_loc_list_1 (decl, varloc, want_address,
16284 initialized);
16286 gcc_assert (descr);
16287 /* The variable has a location between NODE->LABEL and
16288 NODE->NEXT->LABEL. */
16289 if (node->next)
16290 endname = node->next->label;
16291 else
16292 endname = cfun->fde->dw_fde_second_end;
16293 *listp = new_loc_list (descr,
16294 cfun->fde->dw_fde_second_begin,
16295 endname, secname);
16296 listp = &(*listp)->dw_loc_next;
16301 /* Try to avoid the overhead of a location list emitting a location
16302 expression instead, but only if we didn't have more than one
16303 location entry in the first place. If some entries were not
16304 representable, we don't want to pretend a single entry that was
16305 applies to the entire scope in which the variable is
16306 available. */
16307 if (list && loc_list->first->next)
16308 gen_llsym (list);
16310 return list;
16313 /* Return if the loc_list has only single element and thus can be represented
16314 as location description. */
16316 static bool
16317 single_element_loc_list_p (dw_loc_list_ref list)
16319 gcc_assert (!list->dw_loc_next || list->ll_symbol);
16320 return !list->ll_symbol;
16323 /* Duplicate a single element of location list. */
16325 static inline dw_loc_descr_ref
16326 copy_loc_descr (dw_loc_descr_ref ref)
16328 dw_loc_descr_ref copy = ggc_alloc<dw_loc_descr_node> ();
16329 memcpy (copy, ref, sizeof (dw_loc_descr_node));
16330 return copy;
16333 /* To each location in list LIST append loc descr REF. */
16335 static void
16336 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
16338 dw_loc_descr_ref copy;
16339 add_loc_descr (&list->expr, ref);
16340 list = list->dw_loc_next;
16341 while (list)
16343 copy = copy_loc_descr (ref);
16344 add_loc_descr (&list->expr, copy);
16345 while (copy->dw_loc_next)
16346 copy = copy->dw_loc_next = copy_loc_descr (copy->dw_loc_next);
16347 list = list->dw_loc_next;
16351 /* To each location in list LIST prepend loc descr REF. */
16353 static void
16354 prepend_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
16356 dw_loc_descr_ref copy;
16357 dw_loc_descr_ref ref_end = list->expr;
16358 add_loc_descr (&ref, list->expr);
16359 list->expr = ref;
16360 list = list->dw_loc_next;
16361 while (list)
16363 dw_loc_descr_ref end = list->expr;
16364 list->expr = copy = copy_loc_descr (ref);
16365 while (copy->dw_loc_next != ref_end)
16366 copy = copy->dw_loc_next = copy_loc_descr (copy->dw_loc_next);
16367 copy->dw_loc_next = end;
16368 list = list->dw_loc_next;
16372 /* Given two lists RET and LIST
16373 produce location list that is result of adding expression in LIST
16374 to expression in RET on each position in program.
16375 Might be destructive on both RET and LIST.
16377 TODO: We handle only simple cases of RET or LIST having at most one
16378 element. General case would involve sorting the lists in program order
16379 and merging them that will need some additional work.
16380 Adding that will improve quality of debug info especially for SRA-ed
16381 structures. */
16383 static void
16384 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
16386 if (!list)
16387 return;
16388 if (!*ret)
16390 *ret = list;
16391 return;
16393 if (!list->dw_loc_next)
16395 add_loc_descr_to_each (*ret, list->expr);
16396 return;
16398 if (!(*ret)->dw_loc_next)
16400 prepend_loc_descr_to_each (list, (*ret)->expr);
16401 *ret = list;
16402 return;
16404 expansion_failed (NULL_TREE, NULL_RTX,
16405 "Don't know how to merge two non-trivial"
16406 " location lists.\n");
16407 *ret = NULL;
16408 return;
16411 /* LOC is constant expression. Try a luck, look it up in constant
16412 pool and return its loc_descr of its address. */
16414 static dw_loc_descr_ref
16415 cst_pool_loc_descr (tree loc)
16417 /* Get an RTL for this, if something has been emitted. */
16418 rtx rtl = lookup_constant_def (loc);
16420 if (!rtl || !MEM_P (rtl))
16422 gcc_assert (!rtl);
16423 return 0;
16425 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
16427 /* TODO: We might get more coverage if we was actually delaying expansion
16428 of all expressions till end of compilation when constant pools are fully
16429 populated. */
16430 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
16432 expansion_failed (loc, NULL_RTX,
16433 "CST value in contant pool but not marked.");
16434 return 0;
16436 return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
16437 GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
16440 /* Return dw_loc_list representing address of addr_expr LOC
16441 by looking for inner INDIRECT_REF expression and turning
16442 it into simple arithmetics.
16444 See loc_list_from_tree for the meaning of CONTEXT. */
16446 static dw_loc_list_ref
16447 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev,
16448 loc_descr_context *context)
16450 tree obj, offset;
16451 HOST_WIDE_INT bitsize, bitpos, bytepos;
16452 machine_mode mode;
16453 int unsignedp, reversep, volatilep = 0;
16454 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
16456 obj = get_inner_reference (TREE_OPERAND (loc, 0),
16457 &bitsize, &bitpos, &offset, &mode,
16458 &unsignedp, &reversep, &volatilep);
16459 STRIP_NOPS (obj);
16460 if (bitpos % BITS_PER_UNIT)
16462 expansion_failed (loc, NULL_RTX, "bitfield access");
16463 return 0;
16465 if (!INDIRECT_REF_P (obj))
16467 expansion_failed (obj,
16468 NULL_RTX, "no indirect ref in inner refrence");
16469 return 0;
16471 if (!offset && !bitpos)
16472 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1,
16473 context);
16474 else if (toplev
16475 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
16476 && (dwarf_version >= 4 || !dwarf_strict))
16478 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0, context);
16479 if (!list_ret)
16480 return 0;
16481 if (offset)
16483 /* Variable offset. */
16484 list_ret1 = loc_list_from_tree (offset, 0, context);
16485 if (list_ret1 == 0)
16486 return 0;
16487 add_loc_list (&list_ret, list_ret1);
16488 if (!list_ret)
16489 return 0;
16490 add_loc_descr_to_each (list_ret,
16491 new_loc_descr (DW_OP_plus, 0, 0));
16493 bytepos = bitpos / BITS_PER_UNIT;
16494 if (bytepos > 0)
16495 add_loc_descr_to_each (list_ret,
16496 new_loc_descr (DW_OP_plus_uconst,
16497 bytepos, 0));
16498 else if (bytepos < 0)
16499 loc_list_plus_const (list_ret, bytepos);
16500 add_loc_descr_to_each (list_ret,
16501 new_loc_descr (DW_OP_stack_value, 0, 0));
16503 return list_ret;
16506 /* Set LOC to the next operation that is not a DW_OP_nop operation. In the case
16507 all operations from LOC are nops, move to the last one. Insert in NOPS all
16508 operations that are skipped. */
16510 static void
16511 loc_descr_to_next_no_nop (dw_loc_descr_ref &loc,
16512 hash_set<dw_loc_descr_ref> &nops)
16514 while (loc->dw_loc_next != NULL && loc->dw_loc_opc == DW_OP_nop)
16516 nops.add (loc);
16517 loc = loc->dw_loc_next;
16521 /* Helper for loc_descr_without_nops: free the location description operation
16522 P. */
16524 bool
16525 free_loc_descr (const dw_loc_descr_ref &loc, void *data ATTRIBUTE_UNUSED)
16527 ggc_free (loc);
16528 return true;
16531 /* Remove all DW_OP_nop operations from LOC except, if it exists, the one that
16532 finishes LOC. */
16534 static void
16535 loc_descr_without_nops (dw_loc_descr_ref &loc)
16537 if (loc->dw_loc_opc == DW_OP_nop && loc->dw_loc_next == NULL)
16538 return;
16540 /* Set of all DW_OP_nop operations we remove. */
16541 hash_set<dw_loc_descr_ref> nops;
16543 /* First, strip all prefix NOP operations in order to keep the head of the
16544 operations list. */
16545 loc_descr_to_next_no_nop (loc, nops);
16547 for (dw_loc_descr_ref cur = loc; cur != NULL;)
16549 /* For control flow operations: strip "prefix" nops in destination
16550 labels. */
16551 if (cur->dw_loc_oprnd1.val_class == dw_val_class_loc)
16552 loc_descr_to_next_no_nop (cur->dw_loc_oprnd1.v.val_loc, nops);
16553 if (cur->dw_loc_oprnd2.val_class == dw_val_class_loc)
16554 loc_descr_to_next_no_nop (cur->dw_loc_oprnd2.v.val_loc, nops);
16556 /* Do the same for the operations that follow, then move to the next
16557 iteration. */
16558 if (cur->dw_loc_next != NULL)
16559 loc_descr_to_next_no_nop (cur->dw_loc_next, nops);
16560 cur = cur->dw_loc_next;
16563 nops.traverse<void *, free_loc_descr> (NULL);
16567 struct dwarf_procedure_info;
16569 /* Helper structure for location descriptions generation. */
16570 struct loc_descr_context
16572 /* The type that is implicitly referenced by DW_OP_push_object_address, or
16573 NULL_TREE if DW_OP_push_object_address in invalid for this location
16574 description. This is used when processing PLACEHOLDER_EXPR nodes. */
16575 tree context_type;
16576 /* The ..._DECL node that should be translated as a
16577 DW_OP_push_object_address operation. */
16578 tree base_decl;
16579 /* Information about the DWARF procedure we are currently generating. NULL if
16580 we are not generating a DWARF procedure. */
16581 struct dwarf_procedure_info *dpi;
16582 /* True if integral PLACEHOLDER_EXPR stands for the first argument passed
16583 by consumer. Used for DW_TAG_generic_subrange attributes. */
16584 bool placeholder_arg;
16585 /* True if PLACEHOLDER_EXPR has been seen. */
16586 bool placeholder_seen;
16589 /* DWARF procedures generation
16591 DWARF expressions (aka. location descriptions) are used to encode variable
16592 things such as sizes or offsets. Such computations can have redundant parts
16593 that can be factorized in order to reduce the size of the output debug
16594 information. This is the whole point of DWARF procedures.
16596 Thanks to stor-layout.c, size and offset expressions in GENERIC trees are
16597 already factorized into functions ("size functions") in order to handle very
16598 big and complex types. Such functions are quite simple: they have integral
16599 arguments, they return an integral result and their body contains only a
16600 return statement with arithmetic expressions. This is the only kind of
16601 function we are interested in translating into DWARF procedures, here.
16603 DWARF expressions and DWARF procedure are executed using a stack, so we have
16604 to define some calling convention for them to interact. Let's say that:
16606 - Before calling a DWARF procedure, DWARF expressions must push on the stack
16607 all arguments in reverse order (right-to-left) so that when the DWARF
16608 procedure execution starts, the first argument is the top of the stack.
16610 - Then, when returning, the DWARF procedure must have consumed all arguments
16611 on the stack, must have pushed the result and touched nothing else.
16613 - Each integral argument and the result are integral types can be hold in a
16614 single stack slot.
16616 - We call "frame offset" the number of stack slots that are "under DWARF
16617 procedure control": it includes the arguments slots, the temporaries and
16618 the result slot. Thus, it is equal to the number of arguments when the
16619 procedure execution starts and must be equal to one (the result) when it
16620 returns. */
16622 /* Helper structure used when generating operations for a DWARF procedure. */
16623 struct dwarf_procedure_info
16625 /* The FUNCTION_DECL node corresponding to the DWARF procedure that is
16626 currently translated. */
16627 tree fndecl;
16628 /* The number of arguments FNDECL takes. */
16629 unsigned args_count;
16632 /* Return a pointer to a newly created DIE node for a DWARF procedure. Add
16633 LOCATION as its DW_AT_location attribute. If FNDECL is not NULL_TREE,
16634 equate it to this DIE. */
16636 static dw_die_ref
16637 new_dwarf_proc_die (dw_loc_descr_ref location, tree fndecl,
16638 dw_die_ref parent_die)
16640 dw_die_ref dwarf_proc_die;
16642 if ((dwarf_version < 3 && dwarf_strict)
16643 || location == NULL)
16644 return NULL;
16646 dwarf_proc_die = new_die (DW_TAG_dwarf_procedure, parent_die, fndecl);
16647 if (fndecl)
16648 equate_decl_number_to_die (fndecl, dwarf_proc_die);
16649 add_AT_loc (dwarf_proc_die, DW_AT_location, location);
16650 return dwarf_proc_die;
16653 /* Return whether TYPE is a supported type as a DWARF procedure argument
16654 type or return type (we handle only scalar types and pointer types that
16655 aren't wider than the DWARF expression evaluation stack. */
16657 static bool
16658 is_handled_procedure_type (tree type)
16660 return ((INTEGRAL_TYPE_P (type)
16661 || TREE_CODE (type) == OFFSET_TYPE
16662 || TREE_CODE (type) == POINTER_TYPE)
16663 && int_size_in_bytes (type) <= DWARF2_ADDR_SIZE);
16666 /* Helper for resolve_args_picking: do the same but stop when coming across
16667 visited nodes. For each node we visit, register in FRAME_OFFSETS the frame
16668 offset *before* evaluating the corresponding operation. */
16670 static bool
16671 resolve_args_picking_1 (dw_loc_descr_ref loc, unsigned initial_frame_offset,
16672 struct dwarf_procedure_info *dpi,
16673 hash_map<dw_loc_descr_ref, unsigned> &frame_offsets)
16675 /* The "frame_offset" identifier is already used to name a macro... */
16676 unsigned frame_offset_ = initial_frame_offset;
16677 dw_loc_descr_ref l;
16679 for (l = loc; l != NULL;)
16681 bool existed;
16682 unsigned &l_frame_offset = frame_offsets.get_or_insert (l, &existed);
16684 /* If we already met this node, there is nothing to compute anymore. */
16685 if (existed)
16687 /* Make sure that the stack size is consistent wherever the execution
16688 flow comes from. */
16689 gcc_assert ((unsigned) l_frame_offset == frame_offset_);
16690 break;
16692 l_frame_offset = frame_offset_;
16694 /* If needed, relocate the picking offset with respect to the frame
16695 offset. */
16696 if (l->frame_offset_rel)
16698 unsigned HOST_WIDE_INT off;
16699 switch (l->dw_loc_opc)
16701 case DW_OP_pick:
16702 off = l->dw_loc_oprnd1.v.val_unsigned;
16703 break;
16704 case DW_OP_dup:
16705 off = 0;
16706 break;
16707 case DW_OP_over:
16708 off = 1;
16709 break;
16710 default:
16711 gcc_unreachable ();
16713 /* frame_offset_ is the size of the current stack frame, including
16714 incoming arguments. Besides, the arguments are pushed
16715 right-to-left. Thus, in order to access the Nth argument from
16716 this operation node, the picking has to skip temporaries *plus*
16717 one stack slot per argument (0 for the first one, 1 for the second
16718 one, etc.).
16720 The targetted argument number (N) is already set as the operand,
16721 and the number of temporaries can be computed with:
16722 frame_offsets_ - dpi->args_count */
16723 off += frame_offset_ - dpi->args_count;
16725 /* DW_OP_pick handles only offsets from 0 to 255 (inclusive)... */
16726 if (off > 255)
16727 return false;
16729 if (off == 0)
16731 l->dw_loc_opc = DW_OP_dup;
16732 l->dw_loc_oprnd1.v.val_unsigned = 0;
16734 else if (off == 1)
16736 l->dw_loc_opc = DW_OP_over;
16737 l->dw_loc_oprnd1.v.val_unsigned = 0;
16739 else
16741 l->dw_loc_opc = DW_OP_pick;
16742 l->dw_loc_oprnd1.v.val_unsigned = off;
16746 /* Update frame_offset according to the effect the current operation has
16747 on the stack. */
16748 switch (l->dw_loc_opc)
16750 case DW_OP_deref:
16751 case DW_OP_swap:
16752 case DW_OP_rot:
16753 case DW_OP_abs:
16754 case DW_OP_neg:
16755 case DW_OP_not:
16756 case DW_OP_plus_uconst:
16757 case DW_OP_skip:
16758 case DW_OP_reg0:
16759 case DW_OP_reg1:
16760 case DW_OP_reg2:
16761 case DW_OP_reg3:
16762 case DW_OP_reg4:
16763 case DW_OP_reg5:
16764 case DW_OP_reg6:
16765 case DW_OP_reg7:
16766 case DW_OP_reg8:
16767 case DW_OP_reg9:
16768 case DW_OP_reg10:
16769 case DW_OP_reg11:
16770 case DW_OP_reg12:
16771 case DW_OP_reg13:
16772 case DW_OP_reg14:
16773 case DW_OP_reg15:
16774 case DW_OP_reg16:
16775 case DW_OP_reg17:
16776 case DW_OP_reg18:
16777 case DW_OP_reg19:
16778 case DW_OP_reg20:
16779 case DW_OP_reg21:
16780 case DW_OP_reg22:
16781 case DW_OP_reg23:
16782 case DW_OP_reg24:
16783 case DW_OP_reg25:
16784 case DW_OP_reg26:
16785 case DW_OP_reg27:
16786 case DW_OP_reg28:
16787 case DW_OP_reg29:
16788 case DW_OP_reg30:
16789 case DW_OP_reg31:
16790 case DW_OP_bregx:
16791 case DW_OP_piece:
16792 case DW_OP_deref_size:
16793 case DW_OP_nop:
16794 case DW_OP_bit_piece:
16795 case DW_OP_implicit_value:
16796 case DW_OP_stack_value:
16797 break;
16799 case DW_OP_addr:
16800 case DW_OP_const1u:
16801 case DW_OP_const1s:
16802 case DW_OP_const2u:
16803 case DW_OP_const2s:
16804 case DW_OP_const4u:
16805 case DW_OP_const4s:
16806 case DW_OP_const8u:
16807 case DW_OP_const8s:
16808 case DW_OP_constu:
16809 case DW_OP_consts:
16810 case DW_OP_dup:
16811 case DW_OP_over:
16812 case DW_OP_pick:
16813 case DW_OP_lit0:
16814 case DW_OP_lit1:
16815 case DW_OP_lit2:
16816 case DW_OP_lit3:
16817 case DW_OP_lit4:
16818 case DW_OP_lit5:
16819 case DW_OP_lit6:
16820 case DW_OP_lit7:
16821 case DW_OP_lit8:
16822 case DW_OP_lit9:
16823 case DW_OP_lit10:
16824 case DW_OP_lit11:
16825 case DW_OP_lit12:
16826 case DW_OP_lit13:
16827 case DW_OP_lit14:
16828 case DW_OP_lit15:
16829 case DW_OP_lit16:
16830 case DW_OP_lit17:
16831 case DW_OP_lit18:
16832 case DW_OP_lit19:
16833 case DW_OP_lit20:
16834 case DW_OP_lit21:
16835 case DW_OP_lit22:
16836 case DW_OP_lit23:
16837 case DW_OP_lit24:
16838 case DW_OP_lit25:
16839 case DW_OP_lit26:
16840 case DW_OP_lit27:
16841 case DW_OP_lit28:
16842 case DW_OP_lit29:
16843 case DW_OP_lit30:
16844 case DW_OP_lit31:
16845 case DW_OP_breg0:
16846 case DW_OP_breg1:
16847 case DW_OP_breg2:
16848 case DW_OP_breg3:
16849 case DW_OP_breg4:
16850 case DW_OP_breg5:
16851 case DW_OP_breg6:
16852 case DW_OP_breg7:
16853 case DW_OP_breg8:
16854 case DW_OP_breg9:
16855 case DW_OP_breg10:
16856 case DW_OP_breg11:
16857 case DW_OP_breg12:
16858 case DW_OP_breg13:
16859 case DW_OP_breg14:
16860 case DW_OP_breg15:
16861 case DW_OP_breg16:
16862 case DW_OP_breg17:
16863 case DW_OP_breg18:
16864 case DW_OP_breg19:
16865 case DW_OP_breg20:
16866 case DW_OP_breg21:
16867 case DW_OP_breg22:
16868 case DW_OP_breg23:
16869 case DW_OP_breg24:
16870 case DW_OP_breg25:
16871 case DW_OP_breg26:
16872 case DW_OP_breg27:
16873 case DW_OP_breg28:
16874 case DW_OP_breg29:
16875 case DW_OP_breg30:
16876 case DW_OP_breg31:
16877 case DW_OP_fbreg:
16878 case DW_OP_push_object_address:
16879 case DW_OP_call_frame_cfa:
16880 case DW_OP_GNU_variable_value:
16881 ++frame_offset_;
16882 break;
16884 case DW_OP_drop:
16885 case DW_OP_xderef:
16886 case DW_OP_and:
16887 case DW_OP_div:
16888 case DW_OP_minus:
16889 case DW_OP_mod:
16890 case DW_OP_mul:
16891 case DW_OP_or:
16892 case DW_OP_plus:
16893 case DW_OP_shl:
16894 case DW_OP_shr:
16895 case DW_OP_shra:
16896 case DW_OP_xor:
16897 case DW_OP_bra:
16898 case DW_OP_eq:
16899 case DW_OP_ge:
16900 case DW_OP_gt:
16901 case DW_OP_le:
16902 case DW_OP_lt:
16903 case DW_OP_ne:
16904 case DW_OP_regx:
16905 case DW_OP_xderef_size:
16906 --frame_offset_;
16907 break;
16909 case DW_OP_call2:
16910 case DW_OP_call4:
16911 case DW_OP_call_ref:
16913 dw_die_ref dwarf_proc = l->dw_loc_oprnd1.v.val_die_ref.die;
16914 int *stack_usage = dwarf_proc_stack_usage_map->get (dwarf_proc);
16916 if (stack_usage == NULL)
16917 return false;
16918 frame_offset_ += *stack_usage;
16919 break;
16922 case DW_OP_implicit_pointer:
16923 case DW_OP_entry_value:
16924 case DW_OP_const_type:
16925 case DW_OP_regval_type:
16926 case DW_OP_deref_type:
16927 case DW_OP_convert:
16928 case DW_OP_reinterpret:
16929 case DW_OP_form_tls_address:
16930 case DW_OP_GNU_push_tls_address:
16931 case DW_OP_GNU_uninit:
16932 case DW_OP_GNU_encoded_addr:
16933 case DW_OP_GNU_implicit_pointer:
16934 case DW_OP_GNU_entry_value:
16935 case DW_OP_GNU_const_type:
16936 case DW_OP_GNU_regval_type:
16937 case DW_OP_GNU_deref_type:
16938 case DW_OP_GNU_convert:
16939 case DW_OP_GNU_reinterpret:
16940 case DW_OP_GNU_parameter_ref:
16941 /* loc_list_from_tree will probably not output these operations for
16942 size functions, so assume they will not appear here. */
16943 /* Fall through... */
16945 default:
16946 gcc_unreachable ();
16949 /* Now, follow the control flow (except subroutine calls). */
16950 switch (l->dw_loc_opc)
16952 case DW_OP_bra:
16953 if (!resolve_args_picking_1 (l->dw_loc_next, frame_offset_, dpi,
16954 frame_offsets))
16955 return false;
16956 /* Fall through. */
16958 case DW_OP_skip:
16959 l = l->dw_loc_oprnd1.v.val_loc;
16960 break;
16962 case DW_OP_stack_value:
16963 return true;
16965 default:
16966 l = l->dw_loc_next;
16967 break;
16971 return true;
16974 /* Make a DFS over operations reachable through LOC (i.e. follow branch
16975 operations) in order to resolve the operand of DW_OP_pick operations that
16976 target DWARF procedure arguments (DPI). INITIAL_FRAME_OFFSET is the frame
16977 offset *before* LOC is executed. Return if all relocations were
16978 successful. */
16980 static bool
16981 resolve_args_picking (dw_loc_descr_ref loc, unsigned initial_frame_offset,
16982 struct dwarf_procedure_info *dpi)
16984 /* Associate to all visited operations the frame offset *before* evaluating
16985 this operation. */
16986 hash_map<dw_loc_descr_ref, unsigned> frame_offsets;
16988 return resolve_args_picking_1 (loc, initial_frame_offset, dpi,
16989 frame_offsets);
16992 /* Try to generate a DWARF procedure that computes the same result as FNDECL.
16993 Return NULL if it is not possible. */
16995 static dw_die_ref
16996 function_to_dwarf_procedure (tree fndecl)
16998 struct loc_descr_context ctx;
16999 struct dwarf_procedure_info dpi;
17000 dw_die_ref dwarf_proc_die;
17001 tree tree_body = DECL_SAVED_TREE (fndecl);
17002 dw_loc_descr_ref loc_body, epilogue;
17004 tree cursor;
17005 unsigned i;
17007 /* Do not generate multiple DWARF procedures for the same function
17008 declaration. */
17009 dwarf_proc_die = lookup_decl_die (fndecl);
17010 if (dwarf_proc_die != NULL)
17011 return dwarf_proc_die;
17013 /* DWARF procedures are available starting with the DWARFv3 standard. */
17014 if (dwarf_version < 3 && dwarf_strict)
17015 return NULL;
17017 /* We handle only functions for which we still have a body, that return a
17018 supported type and that takes arguments with supported types. Note that
17019 there is no point translating functions that return nothing. */
17020 if (tree_body == NULL_TREE
17021 || DECL_RESULT (fndecl) == NULL_TREE
17022 || !is_handled_procedure_type (TREE_TYPE (DECL_RESULT (fndecl))))
17023 return NULL;
17025 for (cursor = DECL_ARGUMENTS (fndecl);
17026 cursor != NULL_TREE;
17027 cursor = TREE_CHAIN (cursor))
17028 if (!is_handled_procedure_type (TREE_TYPE (cursor)))
17029 return NULL;
17031 /* Match only "expr" in: RETURN_EXPR (MODIFY_EXPR (RESULT_DECL, expr)). */
17032 if (TREE_CODE (tree_body) != RETURN_EXPR)
17033 return NULL;
17034 tree_body = TREE_OPERAND (tree_body, 0);
17035 if (TREE_CODE (tree_body) != MODIFY_EXPR
17036 || TREE_OPERAND (tree_body, 0) != DECL_RESULT (fndecl))
17037 return NULL;
17038 tree_body = TREE_OPERAND (tree_body, 1);
17040 /* Try to translate the body expression itself. Note that this will probably
17041 cause an infinite recursion if its call graph has a cycle. This is very
17042 unlikely for size functions, however, so don't bother with such things at
17043 the moment. */
17044 ctx.context_type = NULL_TREE;
17045 ctx.base_decl = NULL_TREE;
17046 ctx.dpi = &dpi;
17047 ctx.placeholder_arg = false;
17048 ctx.placeholder_seen = false;
17049 dpi.fndecl = fndecl;
17050 dpi.args_count = list_length (DECL_ARGUMENTS (fndecl));
17051 loc_body = loc_descriptor_from_tree (tree_body, 0, &ctx);
17052 if (!loc_body)
17053 return NULL;
17055 /* After evaluating all operands in "loc_body", we should still have on the
17056 stack all arguments plus the desired function result (top of the stack).
17057 Generate code in order to keep only the result in our stack frame. */
17058 epilogue = NULL;
17059 for (i = 0; i < dpi.args_count; ++i)
17061 dw_loc_descr_ref op_couple = new_loc_descr (DW_OP_swap, 0, 0);
17062 op_couple->dw_loc_next = new_loc_descr (DW_OP_drop, 0, 0);
17063 op_couple->dw_loc_next->dw_loc_next = epilogue;
17064 epilogue = op_couple;
17066 add_loc_descr (&loc_body, epilogue);
17067 if (!resolve_args_picking (loc_body, dpi.args_count, &dpi))
17068 return NULL;
17070 /* Trailing nops from loc_descriptor_from_tree (if any) cannot be removed
17071 because they are considered useful. Now there is an epilogue, they are
17072 not anymore, so give it another try. */
17073 loc_descr_without_nops (loc_body);
17075 /* fndecl may be used both as a regular DW_TAG_subprogram DIE and as
17076 a DW_TAG_dwarf_procedure, so we may have a conflict, here. It's unlikely,
17077 though, given that size functions do not come from source, so they should
17078 not have a dedicated DW_TAG_subprogram DIE. */
17079 dwarf_proc_die
17080 = new_dwarf_proc_die (loc_body, fndecl,
17081 get_context_die (DECL_CONTEXT (fndecl)));
17083 /* The called DWARF procedure consumes one stack slot per argument and
17084 returns one stack slot. */
17085 dwarf_proc_stack_usage_map->put (dwarf_proc_die, 1 - dpi.args_count);
17087 return dwarf_proc_die;
17091 /* Generate Dwarf location list representing LOC.
17092 If WANT_ADDRESS is false, expression computing LOC will be computed
17093 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
17094 if WANT_ADDRESS is 2, expression computing address useable in location
17095 will be returned (i.e. DW_OP_reg can be used
17096 to refer to register values).
17098 CONTEXT provides information to customize the location descriptions
17099 generation. Its context_type field specifies what type is implicitly
17100 referenced by DW_OP_push_object_address. If it is NULL_TREE, this operation
17101 will not be generated.
17103 Its DPI field determines whether we are generating a DWARF expression for a
17104 DWARF procedure, so PARM_DECL references are processed specifically.
17106 If CONTEXT is NULL, the behavior is the same as if context_type, base_decl
17107 and dpi fields were null. */
17109 static dw_loc_list_ref
17110 loc_list_from_tree_1 (tree loc, int want_address,
17111 struct loc_descr_context *context)
17113 dw_loc_descr_ref ret = NULL, ret1 = NULL;
17114 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
17115 int have_address = 0;
17116 enum dwarf_location_atom op;
17118 /* ??? Most of the time we do not take proper care for sign/zero
17119 extending the values properly. Hopefully this won't be a real
17120 problem... */
17122 if (context != NULL
17123 && context->base_decl == loc
17124 && want_address == 0)
17126 if (dwarf_version >= 3 || !dwarf_strict)
17127 return new_loc_list (new_loc_descr (DW_OP_push_object_address, 0, 0),
17128 NULL, NULL, NULL);
17129 else
17130 return NULL;
17133 switch (TREE_CODE (loc))
17135 case ERROR_MARK:
17136 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
17137 return 0;
17139 case PLACEHOLDER_EXPR:
17140 /* This case involves extracting fields from an object to determine the
17141 position of other fields. It is supposed to appear only as the first
17142 operand of COMPONENT_REF nodes and to reference precisely the type
17143 that the context allows. */
17144 if (context != NULL
17145 && TREE_TYPE (loc) == context->context_type
17146 && want_address >= 1)
17148 if (dwarf_version >= 3 || !dwarf_strict)
17150 ret = new_loc_descr (DW_OP_push_object_address, 0, 0);
17151 have_address = 1;
17152 break;
17154 else
17155 return NULL;
17157 /* For DW_TAG_generic_subrange attributes, PLACEHOLDER_EXPR stands for
17158 the single argument passed by consumer. */
17159 else if (context != NULL
17160 && context->placeholder_arg
17161 && INTEGRAL_TYPE_P (TREE_TYPE (loc))
17162 && want_address == 0)
17164 ret = new_loc_descr (DW_OP_pick, 0, 0);
17165 ret->frame_offset_rel = 1;
17166 context->placeholder_seen = true;
17167 break;
17169 else
17170 expansion_failed (loc, NULL_RTX,
17171 "PLACEHOLDER_EXPR for an unexpected type");
17172 break;
17174 case CALL_EXPR:
17176 const int nargs = call_expr_nargs (loc);
17177 tree callee = get_callee_fndecl (loc);
17178 int i;
17179 dw_die_ref dwarf_proc;
17181 if (callee == NULL_TREE)
17182 goto call_expansion_failed;
17184 /* We handle only functions that return an integer. */
17185 if (!is_handled_procedure_type (TREE_TYPE (TREE_TYPE (callee))))
17186 goto call_expansion_failed;
17188 dwarf_proc = function_to_dwarf_procedure (callee);
17189 if (dwarf_proc == NULL)
17190 goto call_expansion_failed;
17192 /* Evaluate arguments right-to-left so that the first argument will
17193 be the top-most one on the stack. */
17194 for (i = nargs - 1; i >= 0; --i)
17196 dw_loc_descr_ref loc_descr
17197 = loc_descriptor_from_tree (CALL_EXPR_ARG (loc, i), 0,
17198 context);
17200 if (loc_descr == NULL)
17201 goto call_expansion_failed;
17203 add_loc_descr (&ret, loc_descr);
17206 ret1 = new_loc_descr (DW_OP_call4, 0, 0);
17207 ret1->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
17208 ret1->dw_loc_oprnd1.v.val_die_ref.die = dwarf_proc;
17209 ret1->dw_loc_oprnd1.v.val_die_ref.external = 0;
17210 add_loc_descr (&ret, ret1);
17211 break;
17213 call_expansion_failed:
17214 expansion_failed (loc, NULL_RTX, "CALL_EXPR");
17215 /* There are no opcodes for these operations. */
17216 return 0;
17219 case PREINCREMENT_EXPR:
17220 case PREDECREMENT_EXPR:
17221 case POSTINCREMENT_EXPR:
17222 case POSTDECREMENT_EXPR:
17223 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
17224 /* There are no opcodes for these operations. */
17225 return 0;
17227 case ADDR_EXPR:
17228 /* If we already want an address, see if there is INDIRECT_REF inside
17229 e.g. for &this->field. */
17230 if (want_address)
17232 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
17233 (loc, want_address == 2, context);
17234 if (list_ret)
17235 have_address = 1;
17236 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
17237 && (ret = cst_pool_loc_descr (loc)))
17238 have_address = 1;
17240 /* Otherwise, process the argument and look for the address. */
17241 if (!list_ret && !ret)
17242 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 1, context);
17243 else
17245 if (want_address)
17246 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
17247 return NULL;
17249 break;
17251 case VAR_DECL:
17252 if (DECL_THREAD_LOCAL_P (loc))
17254 rtx rtl;
17255 enum dwarf_location_atom tls_op;
17256 enum dtprel_bool dtprel = dtprel_false;
17258 if (targetm.have_tls)
17260 /* If this is not defined, we have no way to emit the
17261 data. */
17262 if (!targetm.asm_out.output_dwarf_dtprel)
17263 return 0;
17265 /* The way DW_OP_GNU_push_tls_address is specified, we
17266 can only look up addresses of objects in the current
17267 module. We used DW_OP_addr as first op, but that's
17268 wrong, because DW_OP_addr is relocated by the debug
17269 info consumer, while DW_OP_GNU_push_tls_address
17270 operand shouldn't be. */
17271 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
17272 return 0;
17273 dtprel = dtprel_true;
17274 /* We check for DWARF 5 here because gdb did not implement
17275 DW_OP_form_tls_address until after 7.12. */
17276 tls_op = (dwarf_version >= 5 ? DW_OP_form_tls_address
17277 : DW_OP_GNU_push_tls_address);
17279 else
17281 if (!targetm.emutls.debug_form_tls_address
17282 || !(dwarf_version >= 3 || !dwarf_strict))
17283 return 0;
17284 /* We stuffed the control variable into the DECL_VALUE_EXPR
17285 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
17286 no longer appear in gimple code. We used the control
17287 variable in specific so that we could pick it up here. */
17288 loc = DECL_VALUE_EXPR (loc);
17289 tls_op = DW_OP_form_tls_address;
17292 rtl = rtl_for_decl_location (loc);
17293 if (rtl == NULL_RTX)
17294 return 0;
17296 if (!MEM_P (rtl))
17297 return 0;
17298 rtl = XEXP (rtl, 0);
17299 if (! CONSTANT_P (rtl))
17300 return 0;
17302 ret = new_addr_loc_descr (rtl, dtprel);
17303 ret1 = new_loc_descr (tls_op, 0, 0);
17304 add_loc_descr (&ret, ret1);
17306 have_address = 1;
17307 break;
17309 /* FALLTHRU */
17311 case PARM_DECL:
17312 if (context != NULL && context->dpi != NULL
17313 && DECL_CONTEXT (loc) == context->dpi->fndecl)
17315 /* We are generating code for a DWARF procedure and we want to access
17316 one of its arguments: find the appropriate argument offset and let
17317 the resolve_args_picking pass compute the offset that complies
17318 with the stack frame size. */
17319 unsigned i = 0;
17320 tree cursor;
17322 for (cursor = DECL_ARGUMENTS (context->dpi->fndecl);
17323 cursor != NULL_TREE && cursor != loc;
17324 cursor = TREE_CHAIN (cursor), ++i)
17326 /* If we are translating a DWARF procedure, all referenced parameters
17327 must belong to the current function. */
17328 gcc_assert (cursor != NULL_TREE);
17330 ret = new_loc_descr (DW_OP_pick, i, 0);
17331 ret->frame_offset_rel = 1;
17332 break;
17334 /* FALLTHRU */
17336 case RESULT_DECL:
17337 if (DECL_HAS_VALUE_EXPR_P (loc))
17338 return loc_list_from_tree_1 (DECL_VALUE_EXPR (loc),
17339 want_address, context);
17340 /* FALLTHRU */
17342 case FUNCTION_DECL:
17344 rtx rtl;
17345 var_loc_list *loc_list = lookup_decl_loc (loc);
17347 if (loc_list && loc_list->first)
17349 list_ret = dw_loc_list (loc_list, loc, want_address);
17350 have_address = want_address != 0;
17351 break;
17353 rtl = rtl_for_decl_location (loc);
17354 if (rtl == NULL_RTX)
17356 if (TREE_CODE (loc) != FUNCTION_DECL
17357 && early_dwarf
17358 && current_function_decl
17359 && want_address != 1
17360 && (INTEGRAL_TYPE_P (TREE_TYPE (loc))
17361 || POINTER_TYPE_P (TREE_TYPE (loc)))
17362 && DECL_CONTEXT (loc) == current_function_decl
17363 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (loc)))
17364 <= DWARF2_ADDR_SIZE))
17366 dw_die_ref ref = lookup_decl_die (loc);
17367 ret = new_loc_descr (DW_OP_GNU_variable_value, 0, 0);
17368 if (ref)
17370 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
17371 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
17372 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
17374 else
17376 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
17377 ret->dw_loc_oprnd1.v.val_decl_ref = loc;
17379 break;
17381 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
17382 return 0;
17384 else if (CONST_INT_P (rtl))
17386 HOST_WIDE_INT val = INTVAL (rtl);
17387 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
17388 val &= GET_MODE_MASK (DECL_MODE (loc));
17389 ret = int_loc_descriptor (val);
17391 else if (GET_CODE (rtl) == CONST_STRING)
17393 expansion_failed (loc, NULL_RTX, "CONST_STRING");
17394 return 0;
17396 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
17397 ret = new_addr_loc_descr (rtl, dtprel_false);
17398 else
17400 machine_mode mode, mem_mode;
17402 /* Certain constructs can only be represented at top-level. */
17403 if (want_address == 2)
17405 ret = loc_descriptor (rtl, VOIDmode,
17406 VAR_INIT_STATUS_INITIALIZED);
17407 have_address = 1;
17409 else
17411 mode = GET_MODE (rtl);
17412 mem_mode = VOIDmode;
17413 if (MEM_P (rtl))
17415 mem_mode = mode;
17416 mode = get_address_mode (rtl);
17417 rtl = XEXP (rtl, 0);
17418 have_address = 1;
17420 ret = mem_loc_descriptor (rtl, mode, mem_mode,
17421 VAR_INIT_STATUS_INITIALIZED);
17423 if (!ret)
17424 expansion_failed (loc, rtl,
17425 "failed to produce loc descriptor for rtl");
17428 break;
17430 case MEM_REF:
17431 if (!integer_zerop (TREE_OPERAND (loc, 1)))
17433 have_address = 1;
17434 goto do_plus;
17436 /* Fallthru. */
17437 case INDIRECT_REF:
17438 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17439 have_address = 1;
17440 break;
17442 case TARGET_MEM_REF:
17443 case SSA_NAME:
17444 case DEBUG_EXPR_DECL:
17445 return NULL;
17447 case COMPOUND_EXPR:
17448 return loc_list_from_tree_1 (TREE_OPERAND (loc, 1), want_address,
17449 context);
17451 CASE_CONVERT:
17452 case VIEW_CONVERT_EXPR:
17453 case SAVE_EXPR:
17454 case MODIFY_EXPR:
17455 case NON_LVALUE_EXPR:
17456 return loc_list_from_tree_1 (TREE_OPERAND (loc, 0), want_address,
17457 context);
17459 case COMPONENT_REF:
17460 case BIT_FIELD_REF:
17461 case ARRAY_REF:
17462 case ARRAY_RANGE_REF:
17463 case REALPART_EXPR:
17464 case IMAGPART_EXPR:
17466 tree obj, offset;
17467 HOST_WIDE_INT bitsize, bitpos, bytepos;
17468 machine_mode mode;
17469 int unsignedp, reversep, volatilep = 0;
17471 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
17472 &unsignedp, &reversep, &volatilep);
17474 gcc_assert (obj != loc);
17476 list_ret = loc_list_from_tree_1 (obj,
17477 want_address == 2
17478 && !bitpos && !offset ? 2 : 1,
17479 context);
17480 /* TODO: We can extract value of the small expression via shifting even
17481 for nonzero bitpos. */
17482 if (list_ret == 0)
17483 return 0;
17484 if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
17486 expansion_failed (loc, NULL_RTX,
17487 "bitfield access");
17488 return 0;
17491 if (offset != NULL_TREE)
17493 /* Variable offset. */
17494 list_ret1 = loc_list_from_tree_1 (offset, 0, context);
17495 if (list_ret1 == 0)
17496 return 0;
17497 add_loc_list (&list_ret, list_ret1);
17498 if (!list_ret)
17499 return 0;
17500 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
17503 bytepos = bitpos / BITS_PER_UNIT;
17504 if (bytepos > 0)
17505 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
17506 else if (bytepos < 0)
17507 loc_list_plus_const (list_ret, bytepos);
17509 have_address = 1;
17510 break;
17513 case INTEGER_CST:
17514 if ((want_address || !tree_fits_shwi_p (loc))
17515 && (ret = cst_pool_loc_descr (loc)))
17516 have_address = 1;
17517 else if (want_address == 2
17518 && tree_fits_shwi_p (loc)
17519 && (ret = address_of_int_loc_descriptor
17520 (int_size_in_bytes (TREE_TYPE (loc)),
17521 tree_to_shwi (loc))))
17522 have_address = 1;
17523 else if (tree_fits_shwi_p (loc))
17524 ret = int_loc_descriptor (tree_to_shwi (loc));
17525 else if (tree_fits_uhwi_p (loc))
17526 ret = uint_loc_descriptor (tree_to_uhwi (loc));
17527 else
17529 expansion_failed (loc, NULL_RTX,
17530 "Integer operand is not host integer");
17531 return 0;
17533 break;
17535 case CONSTRUCTOR:
17536 case REAL_CST:
17537 case STRING_CST:
17538 case COMPLEX_CST:
17539 if ((ret = cst_pool_loc_descr (loc)))
17540 have_address = 1;
17541 else if (TREE_CODE (loc) == CONSTRUCTOR)
17543 tree type = TREE_TYPE (loc);
17544 unsigned HOST_WIDE_INT size = int_size_in_bytes (type);
17545 unsigned HOST_WIDE_INT offset = 0;
17546 unsigned HOST_WIDE_INT cnt;
17547 constructor_elt *ce;
17549 if (TREE_CODE (type) == RECORD_TYPE)
17551 /* This is very limited, but it's enough to output
17552 pointers to member functions, as long as the
17553 referenced function is defined in the current
17554 translation unit. */
17555 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (loc), cnt, ce)
17557 tree val = ce->value;
17559 tree field = ce->index;
17561 if (val)
17562 STRIP_NOPS (val);
17564 if (!field || DECL_BIT_FIELD (field))
17566 expansion_failed (loc, NULL_RTX,
17567 "bitfield in record type constructor");
17568 size = offset = (unsigned HOST_WIDE_INT)-1;
17569 ret = NULL;
17570 break;
17573 HOST_WIDE_INT fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
17574 unsigned HOST_WIDE_INT pos = int_byte_position (field);
17575 gcc_assert (pos + fieldsize <= size);
17576 if (pos < offset)
17578 expansion_failed (loc, NULL_RTX,
17579 "out-of-order fields in record constructor");
17580 size = offset = (unsigned HOST_WIDE_INT)-1;
17581 ret = NULL;
17582 break;
17584 if (pos > offset)
17586 ret1 = new_loc_descr (DW_OP_piece, pos - offset, 0);
17587 add_loc_descr (&ret, ret1);
17588 offset = pos;
17590 if (val && fieldsize != 0)
17592 ret1 = loc_descriptor_from_tree (val, want_address, context);
17593 if (!ret1)
17595 expansion_failed (loc, NULL_RTX,
17596 "unsupported expression in field");
17597 size = offset = (unsigned HOST_WIDE_INT)-1;
17598 ret = NULL;
17599 break;
17601 add_loc_descr (&ret, ret1);
17603 if (fieldsize)
17605 ret1 = new_loc_descr (DW_OP_piece, fieldsize, 0);
17606 add_loc_descr (&ret, ret1);
17607 offset = pos + fieldsize;
17611 if (offset != size)
17613 ret1 = new_loc_descr (DW_OP_piece, size - offset, 0);
17614 add_loc_descr (&ret, ret1);
17615 offset = size;
17618 have_address = !!want_address;
17620 else
17621 expansion_failed (loc, NULL_RTX,
17622 "constructor of non-record type");
17624 else
17625 /* We can construct small constants here using int_loc_descriptor. */
17626 expansion_failed (loc, NULL_RTX,
17627 "constructor or constant not in constant pool");
17628 break;
17630 case TRUTH_AND_EXPR:
17631 case TRUTH_ANDIF_EXPR:
17632 case BIT_AND_EXPR:
17633 op = DW_OP_and;
17634 goto do_binop;
17636 case TRUTH_XOR_EXPR:
17637 case BIT_XOR_EXPR:
17638 op = DW_OP_xor;
17639 goto do_binop;
17641 case TRUTH_OR_EXPR:
17642 case TRUTH_ORIF_EXPR:
17643 case BIT_IOR_EXPR:
17644 op = DW_OP_or;
17645 goto do_binop;
17647 case FLOOR_DIV_EXPR:
17648 case CEIL_DIV_EXPR:
17649 case ROUND_DIV_EXPR:
17650 case TRUNC_DIV_EXPR:
17651 case EXACT_DIV_EXPR:
17652 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
17653 return 0;
17654 op = DW_OP_div;
17655 goto do_binop;
17657 case MINUS_EXPR:
17658 op = DW_OP_minus;
17659 goto do_binop;
17661 case FLOOR_MOD_EXPR:
17662 case CEIL_MOD_EXPR:
17663 case ROUND_MOD_EXPR:
17664 case TRUNC_MOD_EXPR:
17665 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
17667 op = DW_OP_mod;
17668 goto do_binop;
17670 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17671 list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
17672 if (list_ret == 0 || list_ret1 == 0)
17673 return 0;
17675 add_loc_list (&list_ret, list_ret1);
17676 if (list_ret == 0)
17677 return 0;
17678 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
17679 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
17680 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
17681 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
17682 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
17683 break;
17685 case MULT_EXPR:
17686 op = DW_OP_mul;
17687 goto do_binop;
17689 case LSHIFT_EXPR:
17690 op = DW_OP_shl;
17691 goto do_binop;
17693 case RSHIFT_EXPR:
17694 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
17695 goto do_binop;
17697 case POINTER_PLUS_EXPR:
17698 case PLUS_EXPR:
17699 do_plus:
17700 if (tree_fits_shwi_p (TREE_OPERAND (loc, 1)))
17702 /* Big unsigned numbers can fit in HOST_WIDE_INT but it may be
17703 smarter to encode their opposite. The DW_OP_plus_uconst operation
17704 takes 1 + X bytes, X being the size of the ULEB128 addend. On the
17705 other hand, a "<push literal>; DW_OP_minus" pattern takes 1 + Y
17706 bytes, Y being the size of the operation that pushes the opposite
17707 of the addend. So let's choose the smallest representation. */
17708 const tree tree_addend = TREE_OPERAND (loc, 1);
17709 offset_int wi_addend;
17710 HOST_WIDE_INT shwi_addend;
17711 dw_loc_descr_ref loc_naddend;
17713 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17714 if (list_ret == 0)
17715 return 0;
17717 /* Try to get the literal to push. It is the opposite of the addend,
17718 so as we rely on wrapping during DWARF evaluation, first decode
17719 the literal as a "DWARF-sized" signed number. */
17720 wi_addend = wi::to_offset (tree_addend);
17721 wi_addend = wi::sext (wi_addend, DWARF2_ADDR_SIZE * 8);
17722 shwi_addend = wi_addend.to_shwi ();
17723 loc_naddend = (shwi_addend != INTTYPE_MINIMUM (HOST_WIDE_INT))
17724 ? int_loc_descriptor (-shwi_addend)
17725 : NULL;
17727 if (loc_naddend != NULL
17728 && ((unsigned) size_of_uleb128 (shwi_addend)
17729 > size_of_loc_descr (loc_naddend)))
17731 add_loc_descr_to_each (list_ret, loc_naddend);
17732 add_loc_descr_to_each (list_ret,
17733 new_loc_descr (DW_OP_minus, 0, 0));
17735 else
17737 for (dw_loc_descr_ref loc_cur = loc_naddend; loc_cur != NULL; )
17739 loc_naddend = loc_cur;
17740 loc_cur = loc_cur->dw_loc_next;
17741 ggc_free (loc_naddend);
17743 loc_list_plus_const (list_ret, wi_addend.to_shwi ());
17745 break;
17748 op = DW_OP_plus;
17749 goto do_binop;
17751 case LE_EXPR:
17752 op = DW_OP_le;
17753 goto do_comp_binop;
17755 case GE_EXPR:
17756 op = DW_OP_ge;
17757 goto do_comp_binop;
17759 case LT_EXPR:
17760 op = DW_OP_lt;
17761 goto do_comp_binop;
17763 case GT_EXPR:
17764 op = DW_OP_gt;
17765 goto do_comp_binop;
17767 do_comp_binop:
17768 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
17770 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
17771 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
17772 list_ret = loc_list_from_uint_comparison (list_ret, list_ret1,
17773 TREE_CODE (loc));
17774 break;
17776 else
17777 goto do_binop;
17779 case EQ_EXPR:
17780 op = DW_OP_eq;
17781 goto do_binop;
17783 case NE_EXPR:
17784 op = DW_OP_ne;
17785 goto do_binop;
17787 do_binop:
17788 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17789 list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
17790 if (list_ret == 0 || list_ret1 == 0)
17791 return 0;
17793 add_loc_list (&list_ret, list_ret1);
17794 if (list_ret == 0)
17795 return 0;
17796 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
17797 break;
17799 case TRUTH_NOT_EXPR:
17800 case BIT_NOT_EXPR:
17801 op = DW_OP_not;
17802 goto do_unop;
17804 case ABS_EXPR:
17805 op = DW_OP_abs;
17806 goto do_unop;
17808 case NEGATE_EXPR:
17809 op = DW_OP_neg;
17810 goto do_unop;
17812 do_unop:
17813 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17814 if (list_ret == 0)
17815 return 0;
17817 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
17818 break;
17820 case MIN_EXPR:
17821 case MAX_EXPR:
17823 const enum tree_code code =
17824 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
17826 loc = build3 (COND_EXPR, TREE_TYPE (loc),
17827 build2 (code, integer_type_node,
17828 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
17829 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
17832 /* fall through */
17834 case COND_EXPR:
17836 dw_loc_descr_ref lhs
17837 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0, context);
17838 dw_loc_list_ref rhs
17839 = loc_list_from_tree_1 (TREE_OPERAND (loc, 2), 0, context);
17840 dw_loc_descr_ref bra_node, jump_node, tmp;
17842 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17843 if (list_ret == 0 || lhs == 0 || rhs == 0)
17844 return 0;
17846 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
17847 add_loc_descr_to_each (list_ret, bra_node);
17849 add_loc_list (&list_ret, rhs);
17850 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
17851 add_loc_descr_to_each (list_ret, jump_node);
17853 add_loc_descr_to_each (list_ret, lhs);
17854 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
17855 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
17857 /* ??? Need a node to point the skip at. Use a nop. */
17858 tmp = new_loc_descr (DW_OP_nop, 0, 0);
17859 add_loc_descr_to_each (list_ret, tmp);
17860 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
17861 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
17863 break;
17865 case FIX_TRUNC_EXPR:
17866 return 0;
17868 default:
17869 /* Leave front-end specific codes as simply unknown. This comes
17870 up, for instance, with the C STMT_EXPR. */
17871 if ((unsigned int) TREE_CODE (loc)
17872 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
17874 expansion_failed (loc, NULL_RTX,
17875 "language specific tree node");
17876 return 0;
17879 /* Otherwise this is a generic code; we should just lists all of
17880 these explicitly. We forgot one. */
17881 if (flag_checking)
17882 gcc_unreachable ();
17884 /* In a release build, we want to degrade gracefully: better to
17885 generate incomplete debugging information than to crash. */
17886 return NULL;
17889 if (!ret && !list_ret)
17890 return 0;
17892 if (want_address == 2 && !have_address
17893 && (dwarf_version >= 4 || !dwarf_strict))
17895 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
17897 expansion_failed (loc, NULL_RTX,
17898 "DWARF address size mismatch");
17899 return 0;
17901 if (ret)
17902 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
17903 else
17904 add_loc_descr_to_each (list_ret,
17905 new_loc_descr (DW_OP_stack_value, 0, 0));
17906 have_address = 1;
17908 /* Show if we can't fill the request for an address. */
17909 if (want_address && !have_address)
17911 expansion_failed (loc, NULL_RTX,
17912 "Want address and only have value");
17913 return 0;
17916 gcc_assert (!ret || !list_ret);
17918 /* If we've got an address and don't want one, dereference. */
17919 if (!want_address && have_address)
17921 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
17923 if (size > DWARF2_ADDR_SIZE || size == -1)
17925 expansion_failed (loc, NULL_RTX,
17926 "DWARF address size mismatch");
17927 return 0;
17929 else if (size == DWARF2_ADDR_SIZE)
17930 op = DW_OP_deref;
17931 else
17932 op = DW_OP_deref_size;
17934 if (ret)
17935 add_loc_descr (&ret, new_loc_descr (op, size, 0));
17936 else
17937 add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
17939 if (ret)
17940 list_ret = new_loc_list (ret, NULL, NULL, NULL);
17942 return list_ret;
17945 /* Likewise, but strip useless DW_OP_nop operations in the resulting
17946 expressions. */
17948 static dw_loc_list_ref
17949 loc_list_from_tree (tree loc, int want_address,
17950 struct loc_descr_context *context)
17952 dw_loc_list_ref result = loc_list_from_tree_1 (loc, want_address, context);
17954 for (dw_loc_list_ref loc_cur = result;
17955 loc_cur != NULL; loc_cur = loc_cur->dw_loc_next)
17956 loc_descr_without_nops (loc_cur->expr);
17957 return result;
17960 /* Same as above but return only single location expression. */
17961 static dw_loc_descr_ref
17962 loc_descriptor_from_tree (tree loc, int want_address,
17963 struct loc_descr_context *context)
17965 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address, context);
17966 if (!ret)
17967 return NULL;
17968 if (ret->dw_loc_next)
17970 expansion_failed (loc, NULL_RTX,
17971 "Location list where only loc descriptor needed");
17972 return NULL;
17974 return ret->expr;
17977 /* Given a value, round it up to the lowest multiple of `boundary'
17978 which is not less than the value itself. */
17980 static inline HOST_WIDE_INT
17981 ceiling (HOST_WIDE_INT value, unsigned int boundary)
17983 return (((value + boundary - 1) / boundary) * boundary);
17986 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
17987 pointer to the declared type for the relevant field variable, or return
17988 `integer_type_node' if the given node turns out to be an
17989 ERROR_MARK node. */
17991 static inline tree
17992 field_type (const_tree decl)
17994 tree type;
17996 if (TREE_CODE (decl) == ERROR_MARK)
17997 return integer_type_node;
17999 type = DECL_BIT_FIELD_TYPE (decl);
18000 if (type == NULL_TREE)
18001 type = TREE_TYPE (decl);
18003 return type;
18006 /* Given a pointer to a tree node, return the alignment in bits for
18007 it, or else return BITS_PER_WORD if the node actually turns out to
18008 be an ERROR_MARK node. */
18010 static inline unsigned
18011 simple_type_align_in_bits (const_tree type)
18013 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
18016 static inline unsigned
18017 simple_decl_align_in_bits (const_tree decl)
18019 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
18022 /* Return the result of rounding T up to ALIGN. */
18024 static inline offset_int
18025 round_up_to_align (const offset_int &t, unsigned int align)
18027 return wi::udiv_trunc (t + align - 1, align) * align;
18030 /* Compute the size of TYPE in bytes. If possible, return NULL and store the
18031 size as an integer constant in CST_SIZE. Otherwise, if possible, return a
18032 DWARF expression that computes the size. Return NULL and set CST_SIZE to -1
18033 if we fail to return the size in one of these two forms. */
18035 static dw_loc_descr_ref
18036 type_byte_size (const_tree type, HOST_WIDE_INT *cst_size)
18038 tree tree_size;
18039 struct loc_descr_context ctx;
18041 /* Return a constant integer in priority, if possible. */
18042 *cst_size = int_size_in_bytes (type);
18043 if (*cst_size != -1)
18044 return NULL;
18046 ctx.context_type = const_cast<tree> (type);
18047 ctx.base_decl = NULL_TREE;
18048 ctx.dpi = NULL;
18049 ctx.placeholder_arg = false;
18050 ctx.placeholder_seen = false;
18052 type = TYPE_MAIN_VARIANT (type);
18053 tree_size = TYPE_SIZE_UNIT (type);
18054 return ((tree_size != NULL_TREE)
18055 ? loc_descriptor_from_tree (tree_size, 0, &ctx)
18056 : NULL);
18059 /* Helper structure for RECORD_TYPE processing. */
18060 struct vlr_context
18062 /* Root RECORD_TYPE. It is needed to generate data member location
18063 descriptions in variable-length records (VLR), but also to cope with
18064 variants, which are composed of nested structures multiplexed with
18065 QUAL_UNION_TYPE nodes. Each time such a structure is passed to a
18066 function processing a FIELD_DECL, it is required to be non null. */
18067 tree struct_type;
18068 /* When generating a variant part in a RECORD_TYPE (i.e. a nested
18069 QUAL_UNION_TYPE), this holds an expression that computes the offset for
18070 this variant part as part of the root record (in storage units). For
18071 regular records, it must be NULL_TREE. */
18072 tree variant_part_offset;
18075 /* Given a pointer to a FIELD_DECL, compute the byte offset of the lowest
18076 addressed byte of the "containing object" for the given FIELD_DECL. If
18077 possible, return a native constant through CST_OFFSET (in which case NULL is
18078 returned); otherwise return a DWARF expression that computes the offset.
18080 Set *CST_OFFSET to 0 and return NULL if we are unable to determine what
18081 that offset is, either because the argument turns out to be a pointer to an
18082 ERROR_MARK node, or because the offset expression is too complex for us.
18084 CTX is required: see the comment for VLR_CONTEXT. */
18086 static dw_loc_descr_ref
18087 field_byte_offset (const_tree decl, struct vlr_context *ctx,
18088 HOST_WIDE_INT *cst_offset)
18090 tree tree_result;
18091 dw_loc_list_ref loc_result;
18093 *cst_offset = 0;
18095 if (TREE_CODE (decl) == ERROR_MARK)
18096 return NULL;
18097 else
18098 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
18100 /* We cannot handle variable bit offsets at the moment, so abort if it's the
18101 case. */
18102 if (TREE_CODE (DECL_FIELD_BIT_OFFSET (decl)) != INTEGER_CST)
18103 return NULL;
18105 #ifdef PCC_BITFIELD_TYPE_MATTERS
18106 /* We used to handle only constant offsets in all cases. Now, we handle
18107 properly dynamic byte offsets only when PCC bitfield type doesn't
18108 matter. */
18109 if (PCC_BITFIELD_TYPE_MATTERS
18110 && TREE_CODE (DECL_FIELD_OFFSET (decl)) == INTEGER_CST)
18112 offset_int object_offset_in_bits;
18113 offset_int object_offset_in_bytes;
18114 offset_int bitpos_int;
18115 tree type;
18116 tree field_size_tree;
18117 offset_int deepest_bitpos;
18118 offset_int field_size_in_bits;
18119 unsigned int type_align_in_bits;
18120 unsigned int decl_align_in_bits;
18121 offset_int type_size_in_bits;
18123 bitpos_int = wi::to_offset (bit_position (decl));
18124 type = field_type (decl);
18125 type_size_in_bits = offset_int_type_size_in_bits (type);
18126 type_align_in_bits = simple_type_align_in_bits (type);
18128 field_size_tree = DECL_SIZE (decl);
18130 /* The size could be unspecified if there was an error, or for
18131 a flexible array member. */
18132 if (!field_size_tree)
18133 field_size_tree = bitsize_zero_node;
18135 /* If the size of the field is not constant, use the type size. */
18136 if (TREE_CODE (field_size_tree) == INTEGER_CST)
18137 field_size_in_bits = wi::to_offset (field_size_tree);
18138 else
18139 field_size_in_bits = type_size_in_bits;
18141 decl_align_in_bits = simple_decl_align_in_bits (decl);
18143 /* The GCC front-end doesn't make any attempt to keep track of the
18144 starting bit offset (relative to the start of the containing
18145 structure type) of the hypothetical "containing object" for a
18146 bit-field. Thus, when computing the byte offset value for the
18147 start of the "containing object" of a bit-field, we must deduce
18148 this information on our own. This can be rather tricky to do in
18149 some cases. For example, handling the following structure type
18150 definition when compiling for an i386/i486 target (which only
18151 aligns long long's to 32-bit boundaries) can be very tricky:
18153 struct S { int field1; long long field2:31; };
18155 Fortunately, there is a simple rule-of-thumb which can be used
18156 in such cases. When compiling for an i386/i486, GCC will
18157 allocate 8 bytes for the structure shown above. It decides to
18158 do this based upon one simple rule for bit-field allocation.
18159 GCC allocates each "containing object" for each bit-field at
18160 the first (i.e. lowest addressed) legitimate alignment boundary
18161 (based upon the required minimum alignment for the declared
18162 type of the field) which it can possibly use, subject to the
18163 condition that there is still enough available space remaining
18164 in the containing object (when allocated at the selected point)
18165 to fully accommodate all of the bits of the bit-field itself.
18167 This simple rule makes it obvious why GCC allocates 8 bytes for
18168 each object of the structure type shown above. When looking
18169 for a place to allocate the "containing object" for `field2',
18170 the compiler simply tries to allocate a 64-bit "containing
18171 object" at each successive 32-bit boundary (starting at zero)
18172 until it finds a place to allocate that 64- bit field such that
18173 at least 31 contiguous (and previously unallocated) bits remain
18174 within that selected 64 bit field. (As it turns out, for the
18175 example above, the compiler finds it is OK to allocate the
18176 "containing object" 64-bit field at bit-offset zero within the
18177 structure type.)
18179 Here we attempt to work backwards from the limited set of facts
18180 we're given, and we try to deduce from those facts, where GCC
18181 must have believed that the containing object started (within
18182 the structure type). The value we deduce is then used (by the
18183 callers of this routine) to generate DW_AT_location and
18184 DW_AT_bit_offset attributes for fields (both bit-fields and, in
18185 the case of DW_AT_location, regular fields as well). */
18187 /* Figure out the bit-distance from the start of the structure to
18188 the "deepest" bit of the bit-field. */
18189 deepest_bitpos = bitpos_int + field_size_in_bits;
18191 /* This is the tricky part. Use some fancy footwork to deduce
18192 where the lowest addressed bit of the containing object must
18193 be. */
18194 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
18196 /* Round up to type_align by default. This works best for
18197 bitfields. */
18198 object_offset_in_bits
18199 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
18201 if (wi::gtu_p (object_offset_in_bits, bitpos_int))
18203 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
18205 /* Round up to decl_align instead. */
18206 object_offset_in_bits
18207 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
18210 object_offset_in_bytes
18211 = wi::lrshift (object_offset_in_bits, LOG2_BITS_PER_UNIT);
18212 if (ctx->variant_part_offset == NULL_TREE)
18214 *cst_offset = object_offset_in_bytes.to_shwi ();
18215 return NULL;
18217 tree_result = wide_int_to_tree (sizetype, object_offset_in_bytes);
18219 else
18220 #endif /* PCC_BITFIELD_TYPE_MATTERS */
18221 tree_result = byte_position (decl);
18223 if (ctx->variant_part_offset != NULL_TREE)
18224 tree_result = fold_build2 (PLUS_EXPR, TREE_TYPE (tree_result),
18225 ctx->variant_part_offset, tree_result);
18227 /* If the byte offset is a constant, it's simplier to handle a native
18228 constant rather than a DWARF expression. */
18229 if (TREE_CODE (tree_result) == INTEGER_CST)
18231 *cst_offset = wi::to_offset (tree_result).to_shwi ();
18232 return NULL;
18234 struct loc_descr_context loc_ctx = {
18235 ctx->struct_type, /* context_type */
18236 NULL_TREE, /* base_decl */
18237 NULL, /* dpi */
18238 false, /* placeholder_arg */
18239 false /* placeholder_seen */
18241 loc_result = loc_list_from_tree (tree_result, 0, &loc_ctx);
18243 /* We want a DWARF expression: abort if we only have a location list with
18244 multiple elements. */
18245 if (!loc_result || !single_element_loc_list_p (loc_result))
18246 return NULL;
18247 else
18248 return loc_result->expr;
18251 /* The following routines define various Dwarf attributes and any data
18252 associated with them. */
18254 /* Add a location description attribute value to a DIE.
18256 This emits location attributes suitable for whole variables and
18257 whole parameters. Note that the location attributes for struct fields are
18258 generated by the routine `data_member_location_attribute' below. */
18260 static inline void
18261 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
18262 dw_loc_list_ref descr)
18264 if (descr == 0)
18265 return;
18266 if (single_element_loc_list_p (descr))
18267 add_AT_loc (die, attr_kind, descr->expr);
18268 else
18269 add_AT_loc_list (die, attr_kind, descr);
18272 /* Add DW_AT_accessibility attribute to DIE if needed. */
18274 static void
18275 add_accessibility_attribute (dw_die_ref die, tree decl)
18277 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
18278 children, otherwise the default is DW_ACCESS_public. In DWARF2
18279 the default has always been DW_ACCESS_public. */
18280 if (TREE_PROTECTED (decl))
18281 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
18282 else if (TREE_PRIVATE (decl))
18284 if (dwarf_version == 2
18285 || die->die_parent == NULL
18286 || die->die_parent->die_tag != DW_TAG_class_type)
18287 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
18289 else if (dwarf_version > 2
18290 && die->die_parent
18291 && die->die_parent->die_tag == DW_TAG_class_type)
18292 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
18295 /* Attach the specialized form of location attribute used for data members of
18296 struct and union types. In the special case of a FIELD_DECL node which
18297 represents a bit-field, the "offset" part of this special location
18298 descriptor must indicate the distance in bytes from the lowest-addressed
18299 byte of the containing struct or union type to the lowest-addressed byte of
18300 the "containing object" for the bit-field. (See the `field_byte_offset'
18301 function above).
18303 For any given bit-field, the "containing object" is a hypothetical object
18304 (of some integral or enum type) within which the given bit-field lives. The
18305 type of this hypothetical "containing object" is always the same as the
18306 declared type of the individual bit-field itself (for GCC anyway... the
18307 DWARF spec doesn't actually mandate this). Note that it is the size (in
18308 bytes) of the hypothetical "containing object" which will be given in the
18309 DW_AT_byte_size attribute for this bit-field. (See the
18310 `byte_size_attribute' function below.) It is also used when calculating the
18311 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
18312 function below.)
18314 CTX is required: see the comment for VLR_CONTEXT. */
18316 static void
18317 add_data_member_location_attribute (dw_die_ref die,
18318 tree decl,
18319 struct vlr_context *ctx)
18321 HOST_WIDE_INT offset;
18322 dw_loc_descr_ref loc_descr = 0;
18324 if (TREE_CODE (decl) == TREE_BINFO)
18326 /* We're working on the TAG_inheritance for a base class. */
18327 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
18329 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
18330 aren't at a fixed offset from all (sub)objects of the same
18331 type. We need to extract the appropriate offset from our
18332 vtable. The following dwarf expression means
18334 BaseAddr = ObAddr + *((*ObAddr) - Offset)
18336 This is specific to the V3 ABI, of course. */
18338 dw_loc_descr_ref tmp;
18340 /* Make a copy of the object address. */
18341 tmp = new_loc_descr (DW_OP_dup, 0, 0);
18342 add_loc_descr (&loc_descr, tmp);
18344 /* Extract the vtable address. */
18345 tmp = new_loc_descr (DW_OP_deref, 0, 0);
18346 add_loc_descr (&loc_descr, tmp);
18348 /* Calculate the address of the offset. */
18349 offset = tree_to_shwi (BINFO_VPTR_FIELD (decl));
18350 gcc_assert (offset < 0);
18352 tmp = int_loc_descriptor (-offset);
18353 add_loc_descr (&loc_descr, tmp);
18354 tmp = new_loc_descr (DW_OP_minus, 0, 0);
18355 add_loc_descr (&loc_descr, tmp);
18357 /* Extract the offset. */
18358 tmp = new_loc_descr (DW_OP_deref, 0, 0);
18359 add_loc_descr (&loc_descr, tmp);
18361 /* Add it to the object address. */
18362 tmp = new_loc_descr (DW_OP_plus, 0, 0);
18363 add_loc_descr (&loc_descr, tmp);
18365 else
18366 offset = tree_to_shwi (BINFO_OFFSET (decl));
18368 else
18370 loc_descr = field_byte_offset (decl, ctx, &offset);
18372 /* If loc_descr is available then we know the field offset is dynamic.
18373 However, GDB does not handle dynamic field offsets very well at the
18374 moment. */
18375 if (loc_descr != NULL && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL)
18377 loc_descr = NULL;
18378 offset = 0;
18381 /* Data member location evalutation starts with the base address on the
18382 stack. Compute the field offset and add it to this base address. */
18383 else if (loc_descr != NULL)
18384 add_loc_descr (&loc_descr, new_loc_descr (DW_OP_plus, 0, 0));
18387 if (! loc_descr)
18389 /* While DW_AT_data_bit_offset has been added already in DWARF4,
18390 e.g. GDB only added support to it in November 2016. For DWARF5
18391 we need newer debug info consumers anyway. We might change this
18392 to dwarf_version >= 4 once most consumers catched up. */
18393 if (dwarf_version >= 5
18394 && TREE_CODE (decl) == FIELD_DECL
18395 && DECL_BIT_FIELD_TYPE (decl))
18397 tree off = bit_position (decl);
18398 if (tree_fits_uhwi_p (off) && get_AT (die, DW_AT_bit_size))
18400 remove_AT (die, DW_AT_byte_size);
18401 remove_AT (die, DW_AT_bit_offset);
18402 add_AT_unsigned (die, DW_AT_data_bit_offset, tree_to_uhwi (off));
18403 return;
18406 if (dwarf_version > 2)
18408 /* Don't need to output a location expression, just the constant. */
18409 if (offset < 0)
18410 add_AT_int (die, DW_AT_data_member_location, offset);
18411 else
18412 add_AT_unsigned (die, DW_AT_data_member_location, offset);
18413 return;
18415 else
18417 enum dwarf_location_atom op;
18419 /* The DWARF2 standard says that we should assume that the structure
18420 address is already on the stack, so we can specify a structure
18421 field address by using DW_OP_plus_uconst. */
18422 op = DW_OP_plus_uconst;
18423 loc_descr = new_loc_descr (op, offset, 0);
18427 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
18430 /* Writes integer values to dw_vec_const array. */
18432 static void
18433 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
18435 while (size != 0)
18437 *dest++ = val & 0xff;
18438 val >>= 8;
18439 --size;
18443 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
18445 static HOST_WIDE_INT
18446 extract_int (const unsigned char *src, unsigned int size)
18448 HOST_WIDE_INT val = 0;
18450 src += size;
18451 while (size != 0)
18453 val <<= 8;
18454 val |= *--src & 0xff;
18455 --size;
18457 return val;
18460 /* Writes wide_int values to dw_vec_const array. */
18462 static void
18463 insert_wide_int (const wide_int &val, unsigned char *dest, int elt_size)
18465 int i;
18467 if (elt_size <= HOST_BITS_PER_WIDE_INT/BITS_PER_UNIT)
18469 insert_int ((HOST_WIDE_INT) val.elt (0), elt_size, dest);
18470 return;
18473 /* We'd have to extend this code to support odd sizes. */
18474 gcc_assert (elt_size % (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT) == 0);
18476 int n = elt_size / (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
18478 if (WORDS_BIG_ENDIAN)
18479 for (i = n - 1; i >= 0; i--)
18481 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
18482 dest += sizeof (HOST_WIDE_INT);
18484 else
18485 for (i = 0; i < n; i++)
18487 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
18488 dest += sizeof (HOST_WIDE_INT);
18492 /* Writes floating point values to dw_vec_const array. */
18494 static void
18495 insert_float (const_rtx rtl, unsigned char *array)
18497 long val[4];
18498 int i;
18500 real_to_target (val, CONST_DOUBLE_REAL_VALUE (rtl), GET_MODE (rtl));
18502 /* real_to_target puts 32-bit pieces in each long. Pack them. */
18503 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
18505 insert_int (val[i], 4, array);
18506 array += 4;
18510 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
18511 does not have a "location" either in memory or in a register. These
18512 things can arise in GNU C when a constant is passed as an actual parameter
18513 to an inlined function. They can also arise in C++ where declared
18514 constants do not necessarily get memory "homes". */
18516 static bool
18517 add_const_value_attribute (dw_die_ref die, rtx rtl)
18519 switch (GET_CODE (rtl))
18521 case CONST_INT:
18523 HOST_WIDE_INT val = INTVAL (rtl);
18525 if (val < 0)
18526 add_AT_int (die, DW_AT_const_value, val);
18527 else
18528 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
18530 return true;
18532 case CONST_WIDE_INT:
18534 wide_int w1 = rtx_mode_t (rtl, MAX_MODE_INT);
18535 unsigned int prec = MIN (wi::min_precision (w1, UNSIGNED),
18536 (unsigned int)CONST_WIDE_INT_NUNITS (rtl) * HOST_BITS_PER_WIDE_INT);
18537 wide_int w = wi::zext (w1, prec);
18538 add_AT_wide (die, DW_AT_const_value, w);
18540 return true;
18542 case CONST_DOUBLE:
18543 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
18544 floating-point constant. A CONST_DOUBLE is used whenever the
18545 constant requires more than one word in order to be adequately
18546 represented. */
18548 machine_mode mode = GET_MODE (rtl);
18550 if (TARGET_SUPPORTS_WIDE_INT == 0 && !SCALAR_FLOAT_MODE_P (mode))
18551 add_AT_double (die, DW_AT_const_value,
18552 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
18553 else
18555 unsigned int length = GET_MODE_SIZE (mode);
18556 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
18558 insert_float (rtl, array);
18559 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
18562 return true;
18564 case CONST_VECTOR:
18566 machine_mode mode = GET_MODE (rtl);
18567 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
18568 unsigned int length = CONST_VECTOR_NUNITS (rtl);
18569 unsigned char *array
18570 = ggc_vec_alloc<unsigned char> (length * elt_size);
18571 unsigned int i;
18572 unsigned char *p;
18573 machine_mode imode = GET_MODE_INNER (mode);
18575 switch (GET_MODE_CLASS (mode))
18577 case MODE_VECTOR_INT:
18578 for (i = 0, p = array; i < length; i++, p += elt_size)
18580 rtx elt = CONST_VECTOR_ELT (rtl, i);
18581 insert_wide_int (rtx_mode_t (elt, imode), p, elt_size);
18583 break;
18585 case MODE_VECTOR_FLOAT:
18586 for (i = 0, p = array; i < length; i++, p += elt_size)
18588 rtx elt = CONST_VECTOR_ELT (rtl, i);
18589 insert_float (elt, p);
18591 break;
18593 default:
18594 gcc_unreachable ();
18597 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
18599 return true;
18601 case CONST_STRING:
18602 if (dwarf_version >= 4 || !dwarf_strict)
18604 dw_loc_descr_ref loc_result;
18605 resolve_one_addr (&rtl);
18606 rtl_addr:
18607 loc_result = new_addr_loc_descr (rtl, dtprel_false);
18608 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
18609 add_AT_loc (die, DW_AT_location, loc_result);
18610 vec_safe_push (used_rtx_array, rtl);
18611 return true;
18613 return false;
18615 case CONST:
18616 if (CONSTANT_P (XEXP (rtl, 0)))
18617 return add_const_value_attribute (die, XEXP (rtl, 0));
18618 /* FALLTHROUGH */
18619 case SYMBOL_REF:
18620 if (!const_ok_for_output (rtl))
18621 return false;
18622 /* FALLTHROUGH */
18623 case LABEL_REF:
18624 if (dwarf_version >= 4 || !dwarf_strict)
18625 goto rtl_addr;
18626 return false;
18628 case PLUS:
18629 /* In cases where an inlined instance of an inline function is passed
18630 the address of an `auto' variable (which is local to the caller) we
18631 can get a situation where the DECL_RTL of the artificial local
18632 variable (for the inlining) which acts as a stand-in for the
18633 corresponding formal parameter (of the inline function) will look
18634 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
18635 exactly a compile-time constant expression, but it isn't the address
18636 of the (artificial) local variable either. Rather, it represents the
18637 *value* which the artificial local variable always has during its
18638 lifetime. We currently have no way to represent such quasi-constant
18639 values in Dwarf, so for now we just punt and generate nothing. */
18640 return false;
18642 case HIGH:
18643 case CONST_FIXED:
18644 return false;
18646 case MEM:
18647 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
18648 && MEM_READONLY_P (rtl)
18649 && GET_MODE (rtl) == BLKmode)
18651 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
18652 return true;
18654 return false;
18656 default:
18657 /* No other kinds of rtx should be possible here. */
18658 gcc_unreachable ();
18660 return false;
18663 /* Determine whether the evaluation of EXPR references any variables
18664 or functions which aren't otherwise used (and therefore may not be
18665 output). */
18666 static tree
18667 reference_to_unused (tree * tp, int * walk_subtrees,
18668 void * data ATTRIBUTE_UNUSED)
18670 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
18671 *walk_subtrees = 0;
18673 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
18674 && ! TREE_ASM_WRITTEN (*tp))
18675 return *tp;
18676 /* ??? The C++ FE emits debug information for using decls, so
18677 putting gcc_unreachable here falls over. See PR31899. For now
18678 be conservative. */
18679 else if (!symtab->global_info_ready && VAR_OR_FUNCTION_DECL_P (*tp))
18680 return *tp;
18681 else if (VAR_P (*tp))
18683 varpool_node *node = varpool_node::get (*tp);
18684 if (!node || !node->definition)
18685 return *tp;
18687 else if (TREE_CODE (*tp) == FUNCTION_DECL
18688 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
18690 /* The call graph machinery must have finished analyzing,
18691 optimizing and gimplifying the CU by now.
18692 So if *TP has no call graph node associated
18693 to it, it means *TP will not be emitted. */
18694 if (!cgraph_node::get (*tp))
18695 return *tp;
18697 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
18698 return *tp;
18700 return NULL_TREE;
18703 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
18704 for use in a later add_const_value_attribute call. */
18706 static rtx
18707 rtl_for_decl_init (tree init, tree type)
18709 rtx rtl = NULL_RTX;
18711 STRIP_NOPS (init);
18713 /* If a variable is initialized with a string constant without embedded
18714 zeros, build CONST_STRING. */
18715 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
18717 tree enttype = TREE_TYPE (type);
18718 tree domain = TYPE_DOMAIN (type);
18719 machine_mode mode = TYPE_MODE (enttype);
18721 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
18722 && domain
18723 && integer_zerop (TYPE_MIN_VALUE (domain))
18724 && compare_tree_int (TYPE_MAX_VALUE (domain),
18725 TREE_STRING_LENGTH (init) - 1) == 0
18726 && ((size_t) TREE_STRING_LENGTH (init)
18727 == strlen (TREE_STRING_POINTER (init)) + 1))
18729 rtl = gen_rtx_CONST_STRING (VOIDmode,
18730 ggc_strdup (TREE_STRING_POINTER (init)));
18731 rtl = gen_rtx_MEM (BLKmode, rtl);
18732 MEM_READONLY_P (rtl) = 1;
18735 /* Other aggregates, and complex values, could be represented using
18736 CONCAT: FIXME! */
18737 else if (AGGREGATE_TYPE_P (type)
18738 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
18739 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
18740 || TREE_CODE (type) == COMPLEX_TYPE)
18742 /* Vectors only work if their mode is supported by the target.
18743 FIXME: generic vectors ought to work too. */
18744 else if (TREE_CODE (type) == VECTOR_TYPE
18745 && !VECTOR_MODE_P (TYPE_MODE (type)))
18747 /* If the initializer is something that we know will expand into an
18748 immediate RTL constant, expand it now. We must be careful not to
18749 reference variables which won't be output. */
18750 else if (initializer_constant_valid_p (init, type)
18751 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
18753 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
18754 possible. */
18755 if (TREE_CODE (type) == VECTOR_TYPE)
18756 switch (TREE_CODE (init))
18758 case VECTOR_CST:
18759 break;
18760 case CONSTRUCTOR:
18761 if (TREE_CONSTANT (init))
18763 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
18764 bool constant_p = true;
18765 tree value;
18766 unsigned HOST_WIDE_INT ix;
18768 /* Even when ctor is constant, it might contain non-*_CST
18769 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
18770 belong into VECTOR_CST nodes. */
18771 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
18772 if (!CONSTANT_CLASS_P (value))
18774 constant_p = false;
18775 break;
18778 if (constant_p)
18780 init = build_vector_from_ctor (type, elts);
18781 break;
18784 /* FALLTHRU */
18786 default:
18787 return NULL;
18790 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
18792 /* If expand_expr returns a MEM, it wasn't immediate. */
18793 gcc_assert (!rtl || !MEM_P (rtl));
18796 return rtl;
18799 /* Generate RTL for the variable DECL to represent its location. */
18801 static rtx
18802 rtl_for_decl_location (tree decl)
18804 rtx rtl;
18806 /* Here we have to decide where we are going to say the parameter "lives"
18807 (as far as the debugger is concerned). We only have a couple of
18808 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
18810 DECL_RTL normally indicates where the parameter lives during most of the
18811 activation of the function. If optimization is enabled however, this
18812 could be either NULL or else a pseudo-reg. Both of those cases indicate
18813 that the parameter doesn't really live anywhere (as far as the code
18814 generation parts of GCC are concerned) during most of the function's
18815 activation. That will happen (for example) if the parameter is never
18816 referenced within the function.
18818 We could just generate a location descriptor here for all non-NULL
18819 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
18820 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
18821 where DECL_RTL is NULL or is a pseudo-reg.
18823 Note however that we can only get away with using DECL_INCOMING_RTL as
18824 a backup substitute for DECL_RTL in certain limited cases. In cases
18825 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
18826 we can be sure that the parameter was passed using the same type as it is
18827 declared to have within the function, and that its DECL_INCOMING_RTL
18828 points us to a place where a value of that type is passed.
18830 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
18831 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
18832 because in these cases DECL_INCOMING_RTL points us to a value of some
18833 type which is *different* from the type of the parameter itself. Thus,
18834 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
18835 such cases, the debugger would end up (for example) trying to fetch a
18836 `float' from a place which actually contains the first part of a
18837 `double'. That would lead to really incorrect and confusing
18838 output at debug-time.
18840 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
18841 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
18842 are a couple of exceptions however. On little-endian machines we can
18843 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
18844 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
18845 an integral type that is smaller than TREE_TYPE (decl). These cases arise
18846 when (on a little-endian machine) a non-prototyped function has a
18847 parameter declared to be of type `short' or `char'. In such cases,
18848 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
18849 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
18850 passed `int' value. If the debugger then uses that address to fetch
18851 a `short' or a `char' (on a little-endian machine) the result will be
18852 the correct data, so we allow for such exceptional cases below.
18854 Note that our goal here is to describe the place where the given formal
18855 parameter lives during most of the function's activation (i.e. between the
18856 end of the prologue and the start of the epilogue). We'll do that as best
18857 as we can. Note however that if the given formal parameter is modified
18858 sometime during the execution of the function, then a stack backtrace (at
18859 debug-time) will show the function as having been called with the *new*
18860 value rather than the value which was originally passed in. This happens
18861 rarely enough that it is not a major problem, but it *is* a problem, and
18862 I'd like to fix it.
18864 A future version of dwarf2out.c may generate two additional attributes for
18865 any given DW_TAG_formal_parameter DIE which will describe the "passed
18866 type" and the "passed location" for the given formal parameter in addition
18867 to the attributes we now generate to indicate the "declared type" and the
18868 "active location" for each parameter. This additional set of attributes
18869 could be used by debuggers for stack backtraces. Separately, note that
18870 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
18871 This happens (for example) for inlined-instances of inline function formal
18872 parameters which are never referenced. This really shouldn't be
18873 happening. All PARM_DECL nodes should get valid non-NULL
18874 DECL_INCOMING_RTL values. FIXME. */
18876 /* Use DECL_RTL as the "location" unless we find something better. */
18877 rtl = DECL_RTL_IF_SET (decl);
18879 /* When generating abstract instances, ignore everything except
18880 constants, symbols living in memory, and symbols living in
18881 fixed registers. */
18882 if (! reload_completed)
18884 if (rtl
18885 && (CONSTANT_P (rtl)
18886 || (MEM_P (rtl)
18887 && CONSTANT_P (XEXP (rtl, 0)))
18888 || (REG_P (rtl)
18889 && VAR_P (decl)
18890 && TREE_STATIC (decl))))
18892 rtl = targetm.delegitimize_address (rtl);
18893 return rtl;
18895 rtl = NULL_RTX;
18897 else if (TREE_CODE (decl) == PARM_DECL)
18899 if (rtl == NULL_RTX
18900 || is_pseudo_reg (rtl)
18901 || (MEM_P (rtl)
18902 && is_pseudo_reg (XEXP (rtl, 0))
18903 && DECL_INCOMING_RTL (decl)
18904 && MEM_P (DECL_INCOMING_RTL (decl))
18905 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
18907 tree declared_type = TREE_TYPE (decl);
18908 tree passed_type = DECL_ARG_TYPE (decl);
18909 machine_mode dmode = TYPE_MODE (declared_type);
18910 machine_mode pmode = TYPE_MODE (passed_type);
18912 /* This decl represents a formal parameter which was optimized out.
18913 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
18914 all cases where (rtl == NULL_RTX) just below. */
18915 if (dmode == pmode)
18916 rtl = DECL_INCOMING_RTL (decl);
18917 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
18918 && SCALAR_INT_MODE_P (dmode)
18919 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
18920 && DECL_INCOMING_RTL (decl))
18922 rtx inc = DECL_INCOMING_RTL (decl);
18923 if (REG_P (inc))
18924 rtl = inc;
18925 else if (MEM_P (inc))
18927 if (BYTES_BIG_ENDIAN)
18928 rtl = adjust_address_nv (inc, dmode,
18929 GET_MODE_SIZE (pmode)
18930 - GET_MODE_SIZE (dmode));
18931 else
18932 rtl = inc;
18937 /* If the parm was passed in registers, but lives on the stack, then
18938 make a big endian correction if the mode of the type of the
18939 parameter is not the same as the mode of the rtl. */
18940 /* ??? This is the same series of checks that are made in dbxout.c before
18941 we reach the big endian correction code there. It isn't clear if all
18942 of these checks are necessary here, but keeping them all is the safe
18943 thing to do. */
18944 else if (MEM_P (rtl)
18945 && XEXP (rtl, 0) != const0_rtx
18946 && ! CONSTANT_P (XEXP (rtl, 0))
18947 /* Not passed in memory. */
18948 && !MEM_P (DECL_INCOMING_RTL (decl))
18949 /* Not passed by invisible reference. */
18950 && (!REG_P (XEXP (rtl, 0))
18951 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
18952 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
18953 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
18954 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
18955 #endif
18957 /* Big endian correction check. */
18958 && BYTES_BIG_ENDIAN
18959 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
18960 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
18961 < UNITS_PER_WORD))
18963 machine_mode addr_mode = get_address_mode (rtl);
18964 int offset = (UNITS_PER_WORD
18965 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
18967 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
18968 plus_constant (addr_mode, XEXP (rtl, 0), offset));
18971 else if (VAR_P (decl)
18972 && rtl
18973 && MEM_P (rtl)
18974 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
18975 && BYTES_BIG_ENDIAN)
18977 machine_mode addr_mode = get_address_mode (rtl);
18978 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
18979 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
18981 /* If a variable is declared "register" yet is smaller than
18982 a register, then if we store the variable to memory, it
18983 looks like we're storing a register-sized value, when in
18984 fact we are not. We need to adjust the offset of the
18985 storage location to reflect the actual value's bytes,
18986 else gdb will not be able to display it. */
18987 if (rsize > dsize)
18988 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
18989 plus_constant (addr_mode, XEXP (rtl, 0),
18990 rsize - dsize));
18993 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
18994 and will have been substituted directly into all expressions that use it.
18995 C does not have such a concept, but C++ and other languages do. */
18996 if (!rtl && VAR_P (decl) && DECL_INITIAL (decl))
18997 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
18999 if (rtl)
19000 rtl = targetm.delegitimize_address (rtl);
19002 /* If we don't look past the constant pool, we risk emitting a
19003 reference to a constant pool entry that isn't referenced from
19004 code, and thus is not emitted. */
19005 if (rtl)
19006 rtl = avoid_constant_pool_reference (rtl);
19008 /* Try harder to get a rtl. If this symbol ends up not being emitted
19009 in the current CU, resolve_addr will remove the expression referencing
19010 it. */
19011 if (rtl == NULL_RTX
19012 && VAR_P (decl)
19013 && !DECL_EXTERNAL (decl)
19014 && TREE_STATIC (decl)
19015 && DECL_NAME (decl)
19016 && !DECL_HARD_REGISTER (decl)
19017 && DECL_MODE (decl) != VOIDmode)
19019 rtl = make_decl_rtl_for_debug (decl);
19020 if (!MEM_P (rtl)
19021 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
19022 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
19023 rtl = NULL_RTX;
19026 return rtl;
19029 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
19030 returned. If so, the decl for the COMMON block is returned, and the
19031 value is the offset into the common block for the symbol. */
19033 static tree
19034 fortran_common (tree decl, HOST_WIDE_INT *value)
19036 tree val_expr, cvar;
19037 machine_mode mode;
19038 HOST_WIDE_INT bitsize, bitpos;
19039 tree offset;
19040 int unsignedp, reversep, volatilep = 0;
19042 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
19043 it does not have a value (the offset into the common area), or if it
19044 is thread local (as opposed to global) then it isn't common, and shouldn't
19045 be handled as such. */
19046 if (!VAR_P (decl)
19047 || !TREE_STATIC (decl)
19048 || !DECL_HAS_VALUE_EXPR_P (decl)
19049 || !is_fortran ())
19050 return NULL_TREE;
19052 val_expr = DECL_VALUE_EXPR (decl);
19053 if (TREE_CODE (val_expr) != COMPONENT_REF)
19054 return NULL_TREE;
19056 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset, &mode,
19057 &unsignedp, &reversep, &volatilep);
19059 if (cvar == NULL_TREE
19060 || !VAR_P (cvar)
19061 || DECL_ARTIFICIAL (cvar)
19062 || !TREE_PUBLIC (cvar))
19063 return NULL_TREE;
19065 *value = 0;
19066 if (offset != NULL)
19068 if (!tree_fits_shwi_p (offset))
19069 return NULL_TREE;
19070 *value = tree_to_shwi (offset);
19072 if (bitpos != 0)
19073 *value += bitpos / BITS_PER_UNIT;
19075 return cvar;
19078 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
19079 data attribute for a variable or a parameter. We generate the
19080 DW_AT_const_value attribute only in those cases where the given variable
19081 or parameter does not have a true "location" either in memory or in a
19082 register. This can happen (for example) when a constant is passed as an
19083 actual argument in a call to an inline function. (It's possible that
19084 these things can crop up in other ways also.) Note that one type of
19085 constant value which can be passed into an inlined function is a constant
19086 pointer. This can happen for example if an actual argument in an inlined
19087 function call evaluates to a compile-time constant address.
19089 CACHE_P is true if it is worth caching the location list for DECL,
19090 so that future calls can reuse it rather than regenerate it from scratch.
19091 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
19092 since we will need to refer to them each time the function is inlined. */
19094 static bool
19095 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p)
19097 rtx rtl;
19098 dw_loc_list_ref list;
19099 var_loc_list *loc_list;
19100 cached_dw_loc_list *cache;
19102 if (early_dwarf)
19103 return false;
19105 if (TREE_CODE (decl) == ERROR_MARK)
19106 return false;
19108 if (get_AT (die, DW_AT_location)
19109 || get_AT (die, DW_AT_const_value))
19110 return true;
19112 gcc_assert (VAR_P (decl) || TREE_CODE (decl) == PARM_DECL
19113 || TREE_CODE (decl) == RESULT_DECL);
19115 /* Try to get some constant RTL for this decl, and use that as the value of
19116 the location. */
19118 rtl = rtl_for_decl_location (decl);
19119 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
19120 && add_const_value_attribute (die, rtl))
19121 return true;
19123 /* See if we have single element location list that is equivalent to
19124 a constant value. That way we are better to use add_const_value_attribute
19125 rather than expanding constant value equivalent. */
19126 loc_list = lookup_decl_loc (decl);
19127 if (loc_list
19128 && loc_list->first
19129 && loc_list->first->next == NULL
19130 && NOTE_P (loc_list->first->loc)
19131 && NOTE_VAR_LOCATION (loc_list->first->loc)
19132 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
19134 struct var_loc_node *node;
19136 node = loc_list->first;
19137 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
19138 if (GET_CODE (rtl) == EXPR_LIST)
19139 rtl = XEXP (rtl, 0);
19140 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
19141 && add_const_value_attribute (die, rtl))
19142 return true;
19144 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
19145 list several times. See if we've already cached the contents. */
19146 list = NULL;
19147 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
19148 cache_p = false;
19149 if (cache_p)
19151 cache = cached_dw_loc_list_table->find_with_hash (decl, DECL_UID (decl));
19152 if (cache)
19153 list = cache->loc_list;
19155 if (list == NULL)
19157 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2,
19158 NULL);
19159 /* It is usually worth caching this result if the decl is from
19160 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
19161 if (cache_p && list && list->dw_loc_next)
19163 cached_dw_loc_list **slot
19164 = cached_dw_loc_list_table->find_slot_with_hash (decl,
19165 DECL_UID (decl),
19166 INSERT);
19167 cache = ggc_cleared_alloc<cached_dw_loc_list> ();
19168 cache->decl_id = DECL_UID (decl);
19169 cache->loc_list = list;
19170 *slot = cache;
19173 if (list)
19175 add_AT_location_description (die, DW_AT_location, list);
19176 return true;
19178 /* None of that worked, so it must not really have a location;
19179 try adding a constant value attribute from the DECL_INITIAL. */
19180 return tree_add_const_value_attribute_for_decl (die, decl);
19183 /* Helper function for tree_add_const_value_attribute. Natively encode
19184 initializer INIT into an array. Return true if successful. */
19186 static bool
19187 native_encode_initializer (tree init, unsigned char *array, int size)
19189 tree type;
19191 if (init == NULL_TREE)
19192 return false;
19194 STRIP_NOPS (init);
19195 switch (TREE_CODE (init))
19197 case STRING_CST:
19198 type = TREE_TYPE (init);
19199 if (TREE_CODE (type) == ARRAY_TYPE)
19201 tree enttype = TREE_TYPE (type);
19202 machine_mode mode = TYPE_MODE (enttype);
19204 if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
19205 return false;
19206 if (int_size_in_bytes (type) != size)
19207 return false;
19208 if (size > TREE_STRING_LENGTH (init))
19210 memcpy (array, TREE_STRING_POINTER (init),
19211 TREE_STRING_LENGTH (init));
19212 memset (array + TREE_STRING_LENGTH (init),
19213 '\0', size - TREE_STRING_LENGTH (init));
19215 else
19216 memcpy (array, TREE_STRING_POINTER (init), size);
19217 return true;
19219 return false;
19220 case CONSTRUCTOR:
19221 type = TREE_TYPE (init);
19222 if (int_size_in_bytes (type) != size)
19223 return false;
19224 if (TREE_CODE (type) == ARRAY_TYPE)
19226 HOST_WIDE_INT min_index;
19227 unsigned HOST_WIDE_INT cnt;
19228 int curpos = 0, fieldsize;
19229 constructor_elt *ce;
19231 if (TYPE_DOMAIN (type) == NULL_TREE
19232 || !tree_fits_shwi_p (TYPE_MIN_VALUE (TYPE_DOMAIN (type))))
19233 return false;
19235 fieldsize = int_size_in_bytes (TREE_TYPE (type));
19236 if (fieldsize <= 0)
19237 return false;
19239 min_index = tree_to_shwi (TYPE_MIN_VALUE (TYPE_DOMAIN (type)));
19240 memset (array, '\0', size);
19241 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
19243 tree val = ce->value;
19244 tree index = ce->index;
19245 int pos = curpos;
19246 if (index && TREE_CODE (index) == RANGE_EXPR)
19247 pos = (tree_to_shwi (TREE_OPERAND (index, 0)) - min_index)
19248 * fieldsize;
19249 else if (index)
19250 pos = (tree_to_shwi (index) - min_index) * fieldsize;
19252 if (val)
19254 STRIP_NOPS (val);
19255 if (!native_encode_initializer (val, array + pos, fieldsize))
19256 return false;
19258 curpos = pos + fieldsize;
19259 if (index && TREE_CODE (index) == RANGE_EXPR)
19261 int count = tree_to_shwi (TREE_OPERAND (index, 1))
19262 - tree_to_shwi (TREE_OPERAND (index, 0));
19263 while (count-- > 0)
19265 if (val)
19266 memcpy (array + curpos, array + pos, fieldsize);
19267 curpos += fieldsize;
19270 gcc_assert (curpos <= size);
19272 return true;
19274 else if (TREE_CODE (type) == RECORD_TYPE
19275 || TREE_CODE (type) == UNION_TYPE)
19277 tree field = NULL_TREE;
19278 unsigned HOST_WIDE_INT cnt;
19279 constructor_elt *ce;
19281 if (int_size_in_bytes (type) != size)
19282 return false;
19284 if (TREE_CODE (type) == RECORD_TYPE)
19285 field = TYPE_FIELDS (type);
19287 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
19289 tree val = ce->value;
19290 int pos, fieldsize;
19292 if (ce->index != 0)
19293 field = ce->index;
19295 if (val)
19296 STRIP_NOPS (val);
19298 if (field == NULL_TREE || DECL_BIT_FIELD (field))
19299 return false;
19301 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
19302 && TYPE_DOMAIN (TREE_TYPE (field))
19303 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
19304 return false;
19305 else if (DECL_SIZE_UNIT (field) == NULL_TREE
19306 || !tree_fits_shwi_p (DECL_SIZE_UNIT (field)))
19307 return false;
19308 fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
19309 pos = int_byte_position (field);
19310 gcc_assert (pos + fieldsize <= size);
19311 if (val && fieldsize != 0
19312 && !native_encode_initializer (val, array + pos, fieldsize))
19313 return false;
19315 return true;
19317 return false;
19318 case VIEW_CONVERT_EXPR:
19319 case NON_LVALUE_EXPR:
19320 return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
19321 default:
19322 return native_encode_expr (init, array, size) == size;
19326 /* Attach a DW_AT_const_value attribute to DIE. The value of the
19327 attribute is the const value T. */
19329 static bool
19330 tree_add_const_value_attribute (dw_die_ref die, tree t)
19332 tree init;
19333 tree type = TREE_TYPE (t);
19334 rtx rtl;
19336 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
19337 return false;
19339 init = t;
19340 gcc_assert (!DECL_P (init));
19342 if (! early_dwarf)
19344 rtl = rtl_for_decl_init (init, type);
19345 if (rtl)
19346 return add_const_value_attribute (die, rtl);
19348 /* If the host and target are sane, try harder. */
19349 if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
19350 && initializer_constant_valid_p (init, type))
19352 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
19353 if (size > 0 && (int) size == size)
19355 unsigned char *array = ggc_cleared_vec_alloc<unsigned char> (size);
19357 if (native_encode_initializer (init, array, size))
19359 add_AT_vec (die, DW_AT_const_value, size, 1, array);
19360 return true;
19362 ggc_free (array);
19365 return false;
19368 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
19369 attribute is the const value of T, where T is an integral constant
19370 variable with static storage duration
19371 (so it can't be a PARM_DECL or a RESULT_DECL). */
19373 static bool
19374 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
19377 if (!decl
19378 || (!VAR_P (decl) && TREE_CODE (decl) != CONST_DECL)
19379 || (VAR_P (decl) && !TREE_STATIC (decl)))
19380 return false;
19382 if (TREE_READONLY (decl)
19383 && ! TREE_THIS_VOLATILE (decl)
19384 && DECL_INITIAL (decl))
19385 /* OK */;
19386 else
19387 return false;
19389 /* Don't add DW_AT_const_value if abstract origin already has one. */
19390 if (get_AT (var_die, DW_AT_const_value))
19391 return false;
19393 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
19396 /* Convert the CFI instructions for the current function into a
19397 location list. This is used for DW_AT_frame_base when we targeting
19398 a dwarf2 consumer that does not support the dwarf3
19399 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
19400 expressions. */
19402 static dw_loc_list_ref
19403 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
19405 int ix;
19406 dw_fde_ref fde;
19407 dw_loc_list_ref list, *list_tail;
19408 dw_cfi_ref cfi;
19409 dw_cfa_location last_cfa, next_cfa;
19410 const char *start_label, *last_label, *section;
19411 dw_cfa_location remember;
19413 fde = cfun->fde;
19414 gcc_assert (fde != NULL);
19416 section = secname_for_decl (current_function_decl);
19417 list_tail = &list;
19418 list = NULL;
19420 memset (&next_cfa, 0, sizeof (next_cfa));
19421 next_cfa.reg = INVALID_REGNUM;
19422 remember = next_cfa;
19424 start_label = fde->dw_fde_begin;
19426 /* ??? Bald assumption that the CIE opcode list does not contain
19427 advance opcodes. */
19428 FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
19429 lookup_cfa_1 (cfi, &next_cfa, &remember);
19431 last_cfa = next_cfa;
19432 last_label = start_label;
19434 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
19436 /* If the first partition contained no CFI adjustments, the
19437 CIE opcodes apply to the whole first partition. */
19438 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19439 fde->dw_fde_begin, fde->dw_fde_end, section);
19440 list_tail =&(*list_tail)->dw_loc_next;
19441 start_label = last_label = fde->dw_fde_second_begin;
19444 FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
19446 switch (cfi->dw_cfi_opc)
19448 case DW_CFA_set_loc:
19449 case DW_CFA_advance_loc1:
19450 case DW_CFA_advance_loc2:
19451 case DW_CFA_advance_loc4:
19452 if (!cfa_equal_p (&last_cfa, &next_cfa))
19454 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19455 start_label, last_label, section);
19457 list_tail = &(*list_tail)->dw_loc_next;
19458 last_cfa = next_cfa;
19459 start_label = last_label;
19461 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
19462 break;
19464 case DW_CFA_advance_loc:
19465 /* The encoding is complex enough that we should never emit this. */
19466 gcc_unreachable ();
19468 default:
19469 lookup_cfa_1 (cfi, &next_cfa, &remember);
19470 break;
19472 if (ix + 1 == fde->dw_fde_switch_cfi_index)
19474 if (!cfa_equal_p (&last_cfa, &next_cfa))
19476 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19477 start_label, last_label, section);
19479 list_tail = &(*list_tail)->dw_loc_next;
19480 last_cfa = next_cfa;
19481 start_label = last_label;
19483 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19484 start_label, fde->dw_fde_end, section);
19485 list_tail = &(*list_tail)->dw_loc_next;
19486 start_label = last_label = fde->dw_fde_second_begin;
19490 if (!cfa_equal_p (&last_cfa, &next_cfa))
19492 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19493 start_label, last_label, section);
19494 list_tail = &(*list_tail)->dw_loc_next;
19495 start_label = last_label;
19498 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
19499 start_label,
19500 fde->dw_fde_second_begin
19501 ? fde->dw_fde_second_end : fde->dw_fde_end,
19502 section);
19504 if (list && list->dw_loc_next)
19505 gen_llsym (list);
19507 return list;
19510 /* Compute a displacement from the "steady-state frame pointer" to the
19511 frame base (often the same as the CFA), and store it in
19512 frame_pointer_fb_offset. OFFSET is added to the displacement
19513 before the latter is negated. */
19515 static void
19516 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
19518 rtx reg, elim;
19520 #ifdef FRAME_POINTER_CFA_OFFSET
19521 reg = frame_pointer_rtx;
19522 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
19523 #else
19524 reg = arg_pointer_rtx;
19525 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
19526 #endif
19528 elim = (ira_use_lra_p
19529 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
19530 : eliminate_regs (reg, VOIDmode, NULL_RTX));
19531 if (GET_CODE (elim) == PLUS)
19533 offset += INTVAL (XEXP (elim, 1));
19534 elim = XEXP (elim, 0);
19537 frame_pointer_fb_offset = -offset;
19539 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
19540 in which to eliminate. This is because it's stack pointer isn't
19541 directly accessible as a register within the ISA. To work around
19542 this, assume that while we cannot provide a proper value for
19543 frame_pointer_fb_offset, we won't need one either. */
19544 frame_pointer_fb_offset_valid
19545 = ((SUPPORTS_STACK_ALIGNMENT
19546 && (elim == hard_frame_pointer_rtx
19547 || elim == stack_pointer_rtx))
19548 || elim == (frame_pointer_needed
19549 ? hard_frame_pointer_rtx
19550 : stack_pointer_rtx));
19553 /* Generate a DW_AT_name attribute given some string value to be included as
19554 the value of the attribute. */
19556 static void
19557 add_name_attribute (dw_die_ref die, const char *name_string)
19559 if (name_string != NULL && *name_string != 0)
19561 if (demangle_name_func)
19562 name_string = (*demangle_name_func) (name_string);
19564 add_AT_string (die, DW_AT_name, name_string);
19568 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
19569 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
19570 of TYPE accordingly.
19572 ??? This is a temporary measure until after we're able to generate
19573 regular DWARF for the complex Ada type system. */
19575 static void
19576 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
19577 dw_die_ref context_die)
19579 tree dtype;
19580 dw_die_ref dtype_die;
19582 if (!lang_hooks.types.descriptive_type)
19583 return;
19585 dtype = lang_hooks.types.descriptive_type (type);
19586 if (!dtype)
19587 return;
19589 dtype_die = lookup_type_die (dtype);
19590 if (!dtype_die)
19592 gen_type_die (dtype, context_die);
19593 dtype_die = lookup_type_die (dtype);
19594 gcc_assert (dtype_die);
19597 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
19600 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir. */
19602 static const char *
19603 comp_dir_string (void)
19605 const char *wd;
19606 char *wd1;
19607 static const char *cached_wd = NULL;
19609 if (cached_wd != NULL)
19610 return cached_wd;
19612 wd = get_src_pwd ();
19613 if (wd == NULL)
19614 return NULL;
19616 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
19618 int wdlen;
19620 wdlen = strlen (wd);
19621 wd1 = ggc_vec_alloc<char> (wdlen + 2);
19622 strcpy (wd1, wd);
19623 wd1 [wdlen] = DIR_SEPARATOR;
19624 wd1 [wdlen + 1] = 0;
19625 wd = wd1;
19628 cached_wd = remap_debug_filename (wd);
19629 return cached_wd;
19632 /* Generate a DW_AT_comp_dir attribute for DIE. */
19634 static void
19635 add_comp_dir_attribute (dw_die_ref die)
19637 const char * wd = comp_dir_string ();
19638 if (wd != NULL)
19639 add_AT_string (die, DW_AT_comp_dir, wd);
19642 /* Given a tree node VALUE describing a scalar attribute ATTR (i.e. a bound, a
19643 pointer computation, ...), output a representation for that bound according
19644 to the accepted FORMS (see enum dw_scalar_form) and add it to DIE. See
19645 loc_list_from_tree for the meaning of CONTEXT. */
19647 static void
19648 add_scalar_info (dw_die_ref die, enum dwarf_attribute attr, tree value,
19649 int forms, struct loc_descr_context *context)
19651 dw_die_ref context_die, decl_die;
19652 dw_loc_list_ref list;
19653 bool strip_conversions = true;
19654 bool placeholder_seen = false;
19656 while (strip_conversions)
19657 switch (TREE_CODE (value))
19659 case ERROR_MARK:
19660 case SAVE_EXPR:
19661 return;
19663 CASE_CONVERT:
19664 case VIEW_CONVERT_EXPR:
19665 value = TREE_OPERAND (value, 0);
19666 break;
19668 default:
19669 strip_conversions = false;
19670 break;
19673 /* If possible and permitted, output the attribute as a constant. */
19674 if ((forms & dw_scalar_form_constant) != 0
19675 && TREE_CODE (value) == INTEGER_CST)
19677 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (value));
19679 /* If HOST_WIDE_INT is big enough then represent the bound as
19680 a constant value. We need to choose a form based on
19681 whether the type is signed or unsigned. We cannot just
19682 call add_AT_unsigned if the value itself is positive
19683 (add_AT_unsigned might add the unsigned value encoded as
19684 DW_FORM_data[1248]). Some DWARF consumers will lookup the
19685 bounds type and then sign extend any unsigned values found
19686 for signed types. This is needed only for
19687 DW_AT_{lower,upper}_bound, since for most other attributes,
19688 consumers will treat DW_FORM_data[1248] as unsigned values,
19689 regardless of the underlying type. */
19690 if (prec <= HOST_BITS_PER_WIDE_INT
19691 || tree_fits_uhwi_p (value))
19693 if (TYPE_UNSIGNED (TREE_TYPE (value)))
19694 add_AT_unsigned (die, attr, TREE_INT_CST_LOW (value));
19695 else
19696 add_AT_int (die, attr, TREE_INT_CST_LOW (value));
19698 else
19699 /* Otherwise represent the bound as an unsigned value with
19700 the precision of its type. The precision and signedness
19701 of the type will be necessary to re-interpret it
19702 unambiguously. */
19703 add_AT_wide (die, attr, value);
19704 return;
19707 /* Otherwise, if it's possible and permitted too, output a reference to
19708 another DIE. */
19709 if ((forms & dw_scalar_form_reference) != 0)
19711 tree decl = NULL_TREE;
19713 /* Some type attributes reference an outer type. For instance, the upper
19714 bound of an array may reference an embedding record (this happens in
19715 Ada). */
19716 if (TREE_CODE (value) == COMPONENT_REF
19717 && TREE_CODE (TREE_OPERAND (value, 0)) == PLACEHOLDER_EXPR
19718 && TREE_CODE (TREE_OPERAND (value, 1)) == FIELD_DECL)
19719 decl = TREE_OPERAND (value, 1);
19721 else if (VAR_P (value)
19722 || TREE_CODE (value) == PARM_DECL
19723 || TREE_CODE (value) == RESULT_DECL)
19724 decl = value;
19726 if (decl != NULL_TREE)
19728 dw_die_ref decl_die = lookup_decl_die (decl);
19730 /* ??? Can this happen, or should the variable have been bound
19731 first? Probably it can, since I imagine that we try to create
19732 the types of parameters in the order in which they exist in
19733 the list, and won't have created a forward reference to a
19734 later parameter. */
19735 if (decl_die != NULL)
19737 add_AT_die_ref (die, attr, decl_die);
19738 return;
19743 /* Last chance: try to create a stack operation procedure to evaluate the
19744 value. Do nothing if even that is not possible or permitted. */
19745 if ((forms & dw_scalar_form_exprloc) == 0)
19746 return;
19748 list = loc_list_from_tree (value, 2, context);
19749 if (context && context->placeholder_arg)
19751 placeholder_seen = context->placeholder_seen;
19752 context->placeholder_seen = false;
19754 if (list == NULL || single_element_loc_list_p (list))
19756 /* If this attribute is not a reference nor constant, it is
19757 a DWARF expression rather than location description. For that
19758 loc_list_from_tree (value, 0, &context) is needed. */
19759 dw_loc_list_ref list2 = loc_list_from_tree (value, 0, context);
19760 if (list2 && single_element_loc_list_p (list2))
19762 if (placeholder_seen)
19764 struct dwarf_procedure_info dpi;
19765 dpi.fndecl = NULL_TREE;
19766 dpi.args_count = 1;
19767 if (!resolve_args_picking (list2->expr, 1, &dpi))
19768 return;
19770 add_AT_loc (die, attr, list2->expr);
19771 return;
19775 /* If that failed to give a single element location list, fall back to
19776 outputting this as a reference... still if permitted. */
19777 if (list == NULL
19778 || (forms & dw_scalar_form_reference) == 0
19779 || placeholder_seen)
19780 return;
19782 if (current_function_decl == 0)
19783 context_die = comp_unit_die ();
19784 else
19785 context_die = lookup_decl_die (current_function_decl);
19787 decl_die = new_die (DW_TAG_variable, context_die, value);
19788 add_AT_flag (decl_die, DW_AT_artificial, 1);
19789 add_type_attribute (decl_die, TREE_TYPE (value), TYPE_QUAL_CONST, false,
19790 context_die);
19791 add_AT_location_description (decl_die, DW_AT_location, list);
19792 add_AT_die_ref (die, attr, decl_die);
19795 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
19796 default. */
19798 static int
19799 lower_bound_default (void)
19801 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
19803 case DW_LANG_C:
19804 case DW_LANG_C89:
19805 case DW_LANG_C99:
19806 case DW_LANG_C11:
19807 case DW_LANG_C_plus_plus:
19808 case DW_LANG_C_plus_plus_11:
19809 case DW_LANG_C_plus_plus_14:
19810 case DW_LANG_ObjC:
19811 case DW_LANG_ObjC_plus_plus:
19812 case DW_LANG_Java:
19813 return 0;
19814 case DW_LANG_Fortran77:
19815 case DW_LANG_Fortran90:
19816 case DW_LANG_Fortran95:
19817 case DW_LANG_Fortran03:
19818 case DW_LANG_Fortran08:
19819 return 1;
19820 case DW_LANG_UPC:
19821 case DW_LANG_D:
19822 case DW_LANG_Python:
19823 return dwarf_version >= 4 ? 0 : -1;
19824 case DW_LANG_Ada95:
19825 case DW_LANG_Ada83:
19826 case DW_LANG_Cobol74:
19827 case DW_LANG_Cobol85:
19828 case DW_LANG_Pascal83:
19829 case DW_LANG_Modula2:
19830 case DW_LANG_PLI:
19831 return dwarf_version >= 4 ? 1 : -1;
19832 default:
19833 return -1;
19837 /* Given a tree node describing an array bound (either lower or upper) output
19838 a representation for that bound. */
19840 static void
19841 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr,
19842 tree bound, struct loc_descr_context *context)
19844 int dflt;
19846 while (1)
19847 switch (TREE_CODE (bound))
19849 /* Strip all conversions. */
19850 CASE_CONVERT:
19851 case VIEW_CONVERT_EXPR:
19852 bound = TREE_OPERAND (bound, 0);
19853 break;
19855 /* All fixed-bounds are represented by INTEGER_CST nodes. Lower bounds
19856 are even omitted when they are the default. */
19857 case INTEGER_CST:
19858 /* If the value for this bound is the default one, we can even omit the
19859 attribute. */
19860 if (bound_attr == DW_AT_lower_bound
19861 && tree_fits_shwi_p (bound)
19862 && (dflt = lower_bound_default ()) != -1
19863 && tree_to_shwi (bound) == dflt)
19864 return;
19866 /* FALLTHRU */
19868 default:
19869 /* Because of the complex interaction there can be with other GNAT
19870 encodings, GDB isn't ready yet to handle proper DWARF description
19871 for self-referencial subrange bounds: let GNAT encodings do the
19872 magic in such a case. */
19873 if (is_ada ()
19874 && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL
19875 && contains_placeholder_p (bound))
19876 return;
19878 add_scalar_info (subrange_die, bound_attr, bound,
19879 dw_scalar_form_constant
19880 | dw_scalar_form_exprloc
19881 | dw_scalar_form_reference,
19882 context);
19883 return;
19887 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
19888 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
19889 Note that the block of subscript information for an array type also
19890 includes information about the element type of the given array type.
19892 This function reuses previously set type and bound information if
19893 available. */
19895 static void
19896 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
19898 unsigned dimension_number;
19899 tree lower, upper;
19900 dw_die_ref child = type_die->die_child;
19902 for (dimension_number = 0;
19903 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
19904 type = TREE_TYPE (type), dimension_number++)
19906 tree domain = TYPE_DOMAIN (type);
19908 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
19909 break;
19911 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
19912 and (in GNU C only) variable bounds. Handle all three forms
19913 here. */
19915 /* Find and reuse a previously generated DW_TAG_subrange_type if
19916 available.
19918 For multi-dimensional arrays, as we iterate through the
19919 various dimensions in the enclosing for loop above, we also
19920 iterate through the DIE children and pick at each
19921 DW_TAG_subrange_type previously generated (if available).
19922 Each child DW_TAG_subrange_type DIE describes the range of
19923 the current dimension. At this point we should have as many
19924 DW_TAG_subrange_type's as we have dimensions in the
19925 array. */
19926 dw_die_ref subrange_die = NULL;
19927 if (child)
19928 while (1)
19930 child = child->die_sib;
19931 if (child->die_tag == DW_TAG_subrange_type)
19932 subrange_die = child;
19933 if (child == type_die->die_child)
19935 /* If we wrapped around, stop looking next time. */
19936 child = NULL;
19937 break;
19939 if (child->die_tag == DW_TAG_subrange_type)
19940 break;
19942 if (!subrange_die)
19943 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
19945 if (domain)
19947 /* We have an array type with specified bounds. */
19948 lower = TYPE_MIN_VALUE (domain);
19949 upper = TYPE_MAX_VALUE (domain);
19951 /* Define the index type. */
19952 if (TREE_TYPE (domain)
19953 && !get_AT (subrange_die, DW_AT_type))
19955 /* ??? This is probably an Ada unnamed subrange type. Ignore the
19956 TREE_TYPE field. We can't emit debug info for this
19957 because it is an unnamed integral type. */
19958 if (TREE_CODE (domain) == INTEGER_TYPE
19959 && TYPE_NAME (domain) == NULL_TREE
19960 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
19961 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
19963 else
19964 add_type_attribute (subrange_die, TREE_TYPE (domain),
19965 TYPE_UNQUALIFIED, false, type_die);
19968 /* ??? If upper is NULL, the array has unspecified length,
19969 but it does have a lower bound. This happens with Fortran
19970 dimension arr(N:*)
19971 Since the debugger is definitely going to need to know N
19972 to produce useful results, go ahead and output the lower
19973 bound solo, and hope the debugger can cope. */
19975 if (!get_AT (subrange_die, DW_AT_lower_bound))
19976 add_bound_info (subrange_die, DW_AT_lower_bound, lower, NULL);
19977 if (upper && !get_AT (subrange_die, DW_AT_upper_bound))
19978 add_bound_info (subrange_die, DW_AT_upper_bound, upper, NULL);
19981 /* Otherwise we have an array type with an unspecified length. The
19982 DWARF-2 spec does not say how to handle this; let's just leave out the
19983 bounds. */
19987 /* Add a DW_AT_byte_size attribute to DIE with TREE_NODE's size. */
19989 static void
19990 add_byte_size_attribute (dw_die_ref die, tree tree_node)
19992 dw_die_ref decl_die;
19993 HOST_WIDE_INT size;
19994 dw_loc_descr_ref size_expr = NULL;
19996 switch (TREE_CODE (tree_node))
19998 case ERROR_MARK:
19999 size = 0;
20000 break;
20001 case ENUMERAL_TYPE:
20002 case RECORD_TYPE:
20003 case UNION_TYPE:
20004 case QUAL_UNION_TYPE:
20005 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
20006 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
20008 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
20009 return;
20011 size_expr = type_byte_size (tree_node, &size);
20012 break;
20013 case FIELD_DECL:
20014 /* For a data member of a struct or union, the DW_AT_byte_size is
20015 generally given as the number of bytes normally allocated for an
20016 object of the *declared* type of the member itself. This is true
20017 even for bit-fields. */
20018 size = int_size_in_bytes (field_type (tree_node));
20019 break;
20020 default:
20021 gcc_unreachable ();
20024 /* Support for dynamically-sized objects was introduced by DWARFv3.
20025 At the moment, GDB does not handle variable byte sizes very well,
20026 though. */
20027 if ((dwarf_version >= 3 || !dwarf_strict)
20028 && gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL
20029 && size_expr != NULL)
20030 add_AT_loc (die, DW_AT_byte_size, size_expr);
20032 /* Note that `size' might be -1 when we get to this point. If it is, that
20033 indicates that the byte size of the entity in question is variable and
20034 that we could not generate a DWARF expression that computes it. */
20035 if (size >= 0)
20036 add_AT_unsigned (die, DW_AT_byte_size, size);
20039 /* Add a DW_AT_alignment attribute to DIE with TREE_NODE's non-default
20040 alignment. */
20042 static void
20043 add_alignment_attribute (dw_die_ref die, tree tree_node)
20045 if (dwarf_version < 5 && dwarf_strict)
20046 return;
20048 unsigned align;
20050 if (DECL_P (tree_node))
20052 if (!DECL_USER_ALIGN (tree_node))
20053 return;
20055 align = DECL_ALIGN_UNIT (tree_node);
20057 else if (TYPE_P (tree_node))
20059 if (!TYPE_USER_ALIGN (tree_node))
20060 return;
20062 align = TYPE_ALIGN_UNIT (tree_node);
20064 else
20065 gcc_unreachable ();
20067 add_AT_unsigned (die, DW_AT_alignment, align);
20070 /* For a FIELD_DECL node which represents a bit-field, output an attribute
20071 which specifies the distance in bits from the highest order bit of the
20072 "containing object" for the bit-field to the highest order bit of the
20073 bit-field itself.
20075 For any given bit-field, the "containing object" is a hypothetical object
20076 (of some integral or enum type) within which the given bit-field lives. The
20077 type of this hypothetical "containing object" is always the same as the
20078 declared type of the individual bit-field itself. The determination of the
20079 exact location of the "containing object" for a bit-field is rather
20080 complicated. It's handled by the `field_byte_offset' function (above).
20082 CTX is required: see the comment for VLR_CONTEXT.
20084 Note that it is the size (in bytes) of the hypothetical "containing object"
20085 which will be given in the DW_AT_byte_size attribute for this bit-field.
20086 (See `byte_size_attribute' above). */
20088 static inline void
20089 add_bit_offset_attribute (dw_die_ref die, tree decl, struct vlr_context *ctx)
20091 HOST_WIDE_INT object_offset_in_bytes;
20092 tree original_type = DECL_BIT_FIELD_TYPE (decl);
20093 HOST_WIDE_INT bitpos_int;
20094 HOST_WIDE_INT highest_order_object_bit_offset;
20095 HOST_WIDE_INT highest_order_field_bit_offset;
20096 HOST_WIDE_INT bit_offset;
20098 field_byte_offset (decl, ctx, &object_offset_in_bytes);
20100 /* Must be a field and a bit field. */
20101 gcc_assert (original_type && TREE_CODE (decl) == FIELD_DECL);
20103 /* We can't yet handle bit-fields whose offsets are variable, so if we
20104 encounter such things, just return without generating any attribute
20105 whatsoever. Likewise for variable or too large size. */
20106 if (! tree_fits_shwi_p (bit_position (decl))
20107 || ! tree_fits_uhwi_p (DECL_SIZE (decl)))
20108 return;
20110 bitpos_int = int_bit_position (decl);
20112 /* Note that the bit offset is always the distance (in bits) from the
20113 highest-order bit of the "containing object" to the highest-order bit of
20114 the bit-field itself. Since the "high-order end" of any object or field
20115 is different on big-endian and little-endian machines, the computation
20116 below must take account of these differences. */
20117 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
20118 highest_order_field_bit_offset = bitpos_int;
20120 if (! BYTES_BIG_ENDIAN)
20122 highest_order_field_bit_offset += tree_to_shwi (DECL_SIZE (decl));
20123 highest_order_object_bit_offset +=
20124 simple_type_size_in_bits (original_type);
20127 bit_offset
20128 = (! BYTES_BIG_ENDIAN
20129 ? highest_order_object_bit_offset - highest_order_field_bit_offset
20130 : highest_order_field_bit_offset - highest_order_object_bit_offset);
20132 if (bit_offset < 0)
20133 add_AT_int (die, DW_AT_bit_offset, bit_offset);
20134 else
20135 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
20138 /* For a FIELD_DECL node which represents a bit field, output an attribute
20139 which specifies the length in bits of the given field. */
20141 static inline void
20142 add_bit_size_attribute (dw_die_ref die, tree decl)
20144 /* Must be a field and a bit field. */
20145 gcc_assert (TREE_CODE (decl) == FIELD_DECL
20146 && DECL_BIT_FIELD_TYPE (decl));
20148 if (tree_fits_uhwi_p (DECL_SIZE (decl)))
20149 add_AT_unsigned (die, DW_AT_bit_size, tree_to_uhwi (DECL_SIZE (decl)));
20152 /* If the compiled language is ANSI C, then add a 'prototyped'
20153 attribute, if arg types are given for the parameters of a function. */
20155 static inline void
20156 add_prototyped_attribute (dw_die_ref die, tree func_type)
20158 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
20160 case DW_LANG_C:
20161 case DW_LANG_C89:
20162 case DW_LANG_C99:
20163 case DW_LANG_C11:
20164 case DW_LANG_ObjC:
20165 if (prototype_p (func_type))
20166 add_AT_flag (die, DW_AT_prototyped, 1);
20167 break;
20168 default:
20169 break;
20173 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
20174 by looking in the type declaration, the object declaration equate table or
20175 the block mapping. */
20177 static inline dw_die_ref
20178 add_abstract_origin_attribute (dw_die_ref die, tree origin)
20180 dw_die_ref origin_die = NULL;
20182 if (TREE_CODE (origin) != FUNCTION_DECL
20183 && TREE_CODE (origin) != BLOCK)
20185 /* We may have gotten separated from the block for the inlined
20186 function, if we're in an exception handler or some such; make
20187 sure that the abstract function has been written out.
20189 Doing this for nested functions is wrong, however; functions are
20190 distinct units, and our context might not even be inline. */
20191 tree fn = origin;
20193 if (TYPE_P (fn))
20194 fn = TYPE_STUB_DECL (fn);
20196 fn = decl_function_context (fn);
20197 if (fn)
20198 dwarf2out_abstract_function (fn);
20201 if (DECL_P (origin))
20202 origin_die = lookup_decl_die (origin);
20203 else if (TYPE_P (origin))
20204 origin_die = lookup_type_die (origin);
20205 else if (TREE_CODE (origin) == BLOCK)
20206 origin_die = BLOCK_DIE (origin);
20208 /* XXX: Functions that are never lowered don't always have correct block
20209 trees (in the case of java, they simply have no block tree, in some other
20210 languages). For these functions, there is nothing we can really do to
20211 output correct debug info for inlined functions in all cases. Rather
20212 than die, we'll just produce deficient debug info now, in that we will
20213 have variables without a proper abstract origin. In the future, when all
20214 functions are lowered, we should re-add a gcc_assert (origin_die)
20215 here. */
20217 if (origin_die)
20218 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
20219 return origin_die;
20222 /* We do not currently support the pure_virtual attribute. */
20224 static inline void
20225 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
20227 if (DECL_VINDEX (func_decl))
20229 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
20231 if (tree_fits_shwi_p (DECL_VINDEX (func_decl)))
20232 add_AT_loc (die, DW_AT_vtable_elem_location,
20233 new_loc_descr (DW_OP_constu,
20234 tree_to_shwi (DECL_VINDEX (func_decl)),
20235 0));
20237 /* GNU extension: Record what type this method came from originally. */
20238 if (debug_info_level > DINFO_LEVEL_TERSE
20239 && DECL_CONTEXT (func_decl))
20240 add_AT_die_ref (die, DW_AT_containing_type,
20241 lookup_type_die (DECL_CONTEXT (func_decl)));
20245 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
20246 given decl. This used to be a vendor extension until after DWARF 4
20247 standardized it. */
20249 static void
20250 add_linkage_attr (dw_die_ref die, tree decl)
20252 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
20254 /* Mimic what assemble_name_raw does with a leading '*'. */
20255 if (name[0] == '*')
20256 name = &name[1];
20258 if (dwarf_version >= 4)
20259 add_AT_string (die, DW_AT_linkage_name, name);
20260 else
20261 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
20264 /* Add source coordinate attributes for the given decl. */
20266 static void
20267 add_src_coords_attributes (dw_die_ref die, tree decl)
20269 expanded_location s;
20271 if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
20272 return;
20273 s = expand_location (DECL_SOURCE_LOCATION (decl));
20274 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
20275 add_AT_unsigned (die, DW_AT_decl_line, s.line);
20276 if (debug_column_info && s.column)
20277 add_AT_unsigned (die, DW_AT_decl_column, s.column);
20280 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
20282 static void
20283 add_linkage_name_raw (dw_die_ref die, tree decl)
20285 /* Defer until we have an assembler name set. */
20286 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
20288 limbo_die_node *asm_name;
20290 asm_name = ggc_cleared_alloc<limbo_die_node> ();
20291 asm_name->die = die;
20292 asm_name->created_for = decl;
20293 asm_name->next = deferred_asm_name;
20294 deferred_asm_name = asm_name;
20296 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
20297 add_linkage_attr (die, decl);
20300 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl if desired. */
20302 static void
20303 add_linkage_name (dw_die_ref die, tree decl)
20305 if (debug_info_level > DINFO_LEVEL_NONE
20306 && VAR_OR_FUNCTION_DECL_P (decl)
20307 && TREE_PUBLIC (decl)
20308 && !(VAR_P (decl) && DECL_REGISTER (decl))
20309 && die->die_tag != DW_TAG_member)
20310 add_linkage_name_raw (die, decl);
20313 /* Add a DW_AT_name attribute and source coordinate attribute for the
20314 given decl, but only if it actually has a name. */
20316 static void
20317 add_name_and_src_coords_attributes (dw_die_ref die, tree decl,
20318 bool no_linkage_name)
20320 tree decl_name;
20322 decl_name = DECL_NAME (decl);
20323 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
20325 const char *name = dwarf2_name (decl, 0);
20326 if (name)
20327 add_name_attribute (die, name);
20328 if (! DECL_ARTIFICIAL (decl))
20329 add_src_coords_attributes (die, decl);
20331 if (!no_linkage_name)
20332 add_linkage_name (die, decl);
20335 #ifdef VMS_DEBUGGING_INFO
20336 /* Get the function's name, as described by its RTL. This may be different
20337 from the DECL_NAME name used in the source file. */
20338 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
20340 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
20341 XEXP (DECL_RTL (decl), 0), false);
20342 vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
20344 #endif /* VMS_DEBUGGING_INFO */
20347 /* Add VALUE as a DW_AT_discr_value attribute to DIE. */
20349 static void
20350 add_discr_value (dw_die_ref die, dw_discr_value *value)
20352 dw_attr_node attr;
20354 attr.dw_attr = DW_AT_discr_value;
20355 attr.dw_attr_val.val_class = dw_val_class_discr_value;
20356 attr.dw_attr_val.val_entry = NULL;
20357 attr.dw_attr_val.v.val_discr_value.pos = value->pos;
20358 if (value->pos)
20359 attr.dw_attr_val.v.val_discr_value.v.uval = value->v.uval;
20360 else
20361 attr.dw_attr_val.v.val_discr_value.v.sval = value->v.sval;
20362 add_dwarf_attr (die, &attr);
20365 /* Add DISCR_LIST as a DW_AT_discr_list to DIE. */
20367 static void
20368 add_discr_list (dw_die_ref die, dw_discr_list_ref discr_list)
20370 dw_attr_node attr;
20372 attr.dw_attr = DW_AT_discr_list;
20373 attr.dw_attr_val.val_class = dw_val_class_discr_list;
20374 attr.dw_attr_val.val_entry = NULL;
20375 attr.dw_attr_val.v.val_discr_list = discr_list;
20376 add_dwarf_attr (die, &attr);
20379 static inline dw_discr_list_ref
20380 AT_discr_list (dw_attr_node *attr)
20382 return attr->dw_attr_val.v.val_discr_list;
20385 #ifdef VMS_DEBUGGING_INFO
20386 /* Output the debug main pointer die for VMS */
20388 void
20389 dwarf2out_vms_debug_main_pointer (void)
20391 char label[MAX_ARTIFICIAL_LABEL_BYTES];
20392 dw_die_ref die;
20394 /* Allocate the VMS debug main subprogram die. */
20395 die = ggc_cleared_alloc<die_node> ();
20396 die->die_tag = DW_TAG_subprogram;
20397 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
20398 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
20399 current_function_funcdef_no);
20400 add_AT_lbl_id (die, DW_AT_entry_pc, label);
20402 /* Make it the first child of comp_unit_die (). */
20403 die->die_parent = comp_unit_die ();
20404 if (comp_unit_die ()->die_child)
20406 die->die_sib = comp_unit_die ()->die_child->die_sib;
20407 comp_unit_die ()->die_child->die_sib = die;
20409 else
20411 die->die_sib = die;
20412 comp_unit_die ()->die_child = die;
20415 #endif /* VMS_DEBUGGING_INFO */
20417 /* Push a new declaration scope. */
20419 static void
20420 push_decl_scope (tree scope)
20422 vec_safe_push (decl_scope_table, scope);
20425 /* Pop a declaration scope. */
20427 static inline void
20428 pop_decl_scope (void)
20430 decl_scope_table->pop ();
20433 /* walk_tree helper function for uses_local_type, below. */
20435 static tree
20436 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
20438 if (!TYPE_P (*tp))
20439 *walk_subtrees = 0;
20440 else
20442 tree name = TYPE_NAME (*tp);
20443 if (name && DECL_P (name) && decl_function_context (name))
20444 return *tp;
20446 return NULL_TREE;
20449 /* If TYPE involves a function-local type (including a local typedef to a
20450 non-local type), returns that type; otherwise returns NULL_TREE. */
20452 static tree
20453 uses_local_type (tree type)
20455 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
20456 return used;
20459 /* Return the DIE for the scope that immediately contains this type.
20460 Non-named types that do not involve a function-local type get global
20461 scope. Named types nested in namespaces or other types get their
20462 containing scope. All other types (i.e. function-local named types) get
20463 the current active scope. */
20465 static dw_die_ref
20466 scope_die_for (tree t, dw_die_ref context_die)
20468 dw_die_ref scope_die = NULL;
20469 tree containing_scope;
20471 /* Non-types always go in the current scope. */
20472 gcc_assert (TYPE_P (t));
20474 /* Use the scope of the typedef, rather than the scope of the type
20475 it refers to. */
20476 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
20477 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
20478 else
20479 containing_scope = TYPE_CONTEXT (t);
20481 /* Use the containing namespace if there is one. */
20482 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
20484 if (context_die == lookup_decl_die (containing_scope))
20485 /* OK */;
20486 else if (debug_info_level > DINFO_LEVEL_TERSE)
20487 context_die = get_context_die (containing_scope);
20488 else
20489 containing_scope = NULL_TREE;
20492 /* Ignore function type "scopes" from the C frontend. They mean that
20493 a tagged type is local to a parmlist of a function declarator, but
20494 that isn't useful to DWARF. */
20495 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
20496 containing_scope = NULL_TREE;
20498 if (SCOPE_FILE_SCOPE_P (containing_scope))
20500 /* If T uses a local type keep it local as well, to avoid references
20501 to function-local DIEs from outside the function. */
20502 if (current_function_decl && uses_local_type (t))
20503 scope_die = context_die;
20504 else
20505 scope_die = comp_unit_die ();
20507 else if (TYPE_P (containing_scope))
20509 /* For types, we can just look up the appropriate DIE. */
20510 if (debug_info_level > DINFO_LEVEL_TERSE)
20511 scope_die = get_context_die (containing_scope);
20512 else
20514 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
20515 if (scope_die == NULL)
20516 scope_die = comp_unit_die ();
20519 else
20520 scope_die = context_die;
20522 return scope_die;
20525 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
20527 static inline int
20528 local_scope_p (dw_die_ref context_die)
20530 for (; context_die; context_die = context_die->die_parent)
20531 if (context_die->die_tag == DW_TAG_inlined_subroutine
20532 || context_die->die_tag == DW_TAG_subprogram)
20533 return 1;
20535 return 0;
20538 /* Returns nonzero if CONTEXT_DIE is a class. */
20540 static inline int
20541 class_scope_p (dw_die_ref context_die)
20543 return (context_die
20544 && (context_die->die_tag == DW_TAG_structure_type
20545 || context_die->die_tag == DW_TAG_class_type
20546 || context_die->die_tag == DW_TAG_interface_type
20547 || context_die->die_tag == DW_TAG_union_type));
20550 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
20551 whether or not to treat a DIE in this context as a declaration. */
20553 static inline int
20554 class_or_namespace_scope_p (dw_die_ref context_die)
20556 return (class_scope_p (context_die)
20557 || (context_die && context_die->die_tag == DW_TAG_namespace));
20560 /* Many forms of DIEs require a "type description" attribute. This
20561 routine locates the proper "type descriptor" die for the type given
20562 by 'type' plus any additional qualifiers given by 'cv_quals', and
20563 adds a DW_AT_type attribute below the given die. */
20565 static void
20566 add_type_attribute (dw_die_ref object_die, tree type, int cv_quals,
20567 bool reverse, dw_die_ref context_die)
20569 enum tree_code code = TREE_CODE (type);
20570 dw_die_ref type_die = NULL;
20572 /* ??? If this type is an unnamed subrange type of an integral, floating-point
20573 or fixed-point type, use the inner type. This is because we have no
20574 support for unnamed types in base_type_die. This can happen if this is
20575 an Ada subrange type. Correct solution is emit a subrange type die. */
20576 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
20577 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
20578 type = TREE_TYPE (type), code = TREE_CODE (type);
20580 if (code == ERROR_MARK
20581 /* Handle a special case. For functions whose return type is void, we
20582 generate *no* type attribute. (Note that no object may have type
20583 `void', so this only applies to function return types). */
20584 || code == VOID_TYPE)
20585 return;
20587 type_die = modified_type_die (type,
20588 cv_quals | TYPE_QUALS_NO_ADDR_SPACE (type),
20589 reverse,
20590 context_die);
20592 if (type_die != NULL)
20593 add_AT_die_ref (object_die, DW_AT_type, type_die);
20596 /* Given an object die, add the calling convention attribute for the
20597 function call type. */
20598 static void
20599 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
20601 enum dwarf_calling_convention value = DW_CC_normal;
20603 value = ((enum dwarf_calling_convention)
20604 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
20606 if (is_fortran ()
20607 && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
20609 /* DWARF 2 doesn't provide a way to identify a program's source-level
20610 entry point. DW_AT_calling_convention attributes are only meant
20611 to describe functions' calling conventions. However, lacking a
20612 better way to signal the Fortran main program, we used this for
20613 a long time, following existing custom. Now, DWARF 4 has
20614 DW_AT_main_subprogram, which we add below, but some tools still
20615 rely on the old way, which we thus keep. */
20616 value = DW_CC_program;
20618 if (dwarf_version >= 4 || !dwarf_strict)
20619 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
20622 /* Only add the attribute if the backend requests it, and
20623 is not DW_CC_normal. */
20624 if (value && (value != DW_CC_normal))
20625 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
20628 /* Given a tree pointer to a struct, class, union, or enum type node, return
20629 a pointer to the (string) tag name for the given type, or zero if the type
20630 was declared without a tag. */
20632 static const char *
20633 type_tag (const_tree type)
20635 const char *name = 0;
20637 if (TYPE_NAME (type) != 0)
20639 tree t = 0;
20641 /* Find the IDENTIFIER_NODE for the type name. */
20642 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
20643 && !TYPE_NAMELESS (type))
20644 t = TYPE_NAME (type);
20646 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
20647 a TYPE_DECL node, regardless of whether or not a `typedef' was
20648 involved. */
20649 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
20650 && ! DECL_IGNORED_P (TYPE_NAME (type)))
20652 /* We want to be extra verbose. Don't call dwarf_name if
20653 DECL_NAME isn't set. The default hook for decl_printable_name
20654 doesn't like that, and in this context it's correct to return
20655 0, instead of "<anonymous>" or the like. */
20656 if (DECL_NAME (TYPE_NAME (type))
20657 && !DECL_NAMELESS (TYPE_NAME (type)))
20658 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
20661 /* Now get the name as a string, or invent one. */
20662 if (!name && t != 0)
20663 name = IDENTIFIER_POINTER (t);
20666 return (name == 0 || *name == '\0') ? 0 : name;
20669 /* Return the type associated with a data member, make a special check
20670 for bit field types. */
20672 static inline tree
20673 member_declared_type (const_tree member)
20675 return (DECL_BIT_FIELD_TYPE (member)
20676 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
20679 /* Get the decl's label, as described by its RTL. This may be different
20680 from the DECL_NAME name used in the source file. */
20682 #if 0
20683 static const char *
20684 decl_start_label (tree decl)
20686 rtx x;
20687 const char *fnname;
20689 x = DECL_RTL (decl);
20690 gcc_assert (MEM_P (x));
20692 x = XEXP (x, 0);
20693 gcc_assert (GET_CODE (x) == SYMBOL_REF);
20695 fnname = XSTR (x, 0);
20696 return fnname;
20698 #endif
20700 /* For variable-length arrays that have been previously generated, but
20701 may be incomplete due to missing subscript info, fill the subscript
20702 info. Return TRUE if this is one of those cases. */
20703 static bool
20704 fill_variable_array_bounds (tree type)
20706 if (TREE_ASM_WRITTEN (type)
20707 && TREE_CODE (type) == ARRAY_TYPE
20708 && variably_modified_type_p (type, NULL))
20710 dw_die_ref array_die = lookup_type_die (type);
20711 if (!array_die)
20712 return false;
20713 add_subscript_info (array_die, type, !is_ada ());
20714 return true;
20716 return false;
20719 /* These routines generate the internal representation of the DIE's for
20720 the compilation unit. Debugging information is collected by walking
20721 the declaration trees passed in from dwarf2out_decl(). */
20723 static void
20724 gen_array_type_die (tree type, dw_die_ref context_die)
20726 dw_die_ref array_die;
20728 /* GNU compilers represent multidimensional array types as sequences of one
20729 dimensional array types whose element types are themselves array types.
20730 We sometimes squish that down to a single array_type DIE with multiple
20731 subscripts in the Dwarf debugging info. The draft Dwarf specification
20732 say that we are allowed to do this kind of compression in C, because
20733 there is no difference between an array of arrays and a multidimensional
20734 array. We don't do this for Ada to remain as close as possible to the
20735 actual representation, which is especially important against the language
20736 flexibilty wrt arrays of variable size. */
20738 bool collapse_nested_arrays = !is_ada ();
20740 if (fill_variable_array_bounds (type))
20741 return;
20743 dw_die_ref scope_die = scope_die_for (type, context_die);
20744 tree element_type;
20746 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
20747 DW_TAG_string_type doesn't have DW_AT_type attribute). */
20748 if (TYPE_STRING_FLAG (type)
20749 && TREE_CODE (type) == ARRAY_TYPE
20750 && is_fortran ()
20751 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
20753 HOST_WIDE_INT size;
20755 array_die = new_die (DW_TAG_string_type, scope_die, type);
20756 add_name_attribute (array_die, type_tag (type));
20757 equate_type_number_to_die (type, array_die);
20758 size = int_size_in_bytes (type);
20759 if (size >= 0)
20760 add_AT_unsigned (array_die, DW_AT_byte_size, size);
20761 else if (TYPE_DOMAIN (type) != NULL_TREE
20762 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE)
20764 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
20765 tree rszdecl = szdecl;
20767 size = int_size_in_bytes (TREE_TYPE (szdecl));
20768 if (!DECL_P (szdecl))
20770 if (TREE_CODE (szdecl) == INDIRECT_REF
20771 && DECL_P (TREE_OPERAND (szdecl, 0)))
20773 rszdecl = TREE_OPERAND (szdecl, 0);
20774 if (int_size_in_bytes (TREE_TYPE (rszdecl))
20775 != DWARF2_ADDR_SIZE)
20776 size = 0;
20778 else
20779 size = 0;
20781 if (size > 0)
20783 dw_loc_list_ref loc
20784 = loc_list_from_tree (rszdecl, szdecl == rszdecl ? 2 : 0,
20785 NULL);
20786 if (loc)
20788 add_AT_location_description (array_die, DW_AT_string_length,
20789 loc);
20790 if (size != DWARF2_ADDR_SIZE)
20791 add_AT_unsigned (array_die, dwarf_version >= 5
20792 ? DW_AT_string_length_byte_size
20793 : DW_AT_byte_size, size);
20797 return;
20800 array_die = new_die (DW_TAG_array_type, scope_die, type);
20801 add_name_attribute (array_die, type_tag (type));
20802 equate_type_number_to_die (type, array_die);
20804 if (TREE_CODE (type) == VECTOR_TYPE)
20805 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
20807 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
20808 if (is_fortran ()
20809 && TREE_CODE (type) == ARRAY_TYPE
20810 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
20811 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
20812 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
20814 #if 0
20815 /* We default the array ordering. SDB will probably do
20816 the right things even if DW_AT_ordering is not present. It's not even
20817 an issue until we start to get into multidimensional arrays anyway. If
20818 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
20819 then we'll have to put the DW_AT_ordering attribute back in. (But if
20820 and when we find out that we need to put these in, we will only do so
20821 for multidimensional arrays. */
20822 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
20823 #endif
20825 if (TREE_CODE (type) == VECTOR_TYPE)
20827 /* For VECTOR_TYPEs we use an array die with appropriate bounds. */
20828 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
20829 add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node, NULL);
20830 add_bound_info (subrange_die, DW_AT_upper_bound,
20831 size_int (TYPE_VECTOR_SUBPARTS (type) - 1), NULL);
20833 else
20834 add_subscript_info (array_die, type, collapse_nested_arrays);
20836 /* Add representation of the type of the elements of this array type and
20837 emit the corresponding DIE if we haven't done it already. */
20838 element_type = TREE_TYPE (type);
20839 if (collapse_nested_arrays)
20840 while (TREE_CODE (element_type) == ARRAY_TYPE)
20842 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
20843 break;
20844 element_type = TREE_TYPE (element_type);
20847 add_type_attribute (array_die, element_type, TYPE_UNQUALIFIED,
20848 TREE_CODE (type) == ARRAY_TYPE
20849 && TYPE_REVERSE_STORAGE_ORDER (type),
20850 context_die);
20852 add_gnat_descriptive_type_attribute (array_die, type, context_die);
20853 if (TYPE_ARTIFICIAL (type))
20854 add_AT_flag (array_die, DW_AT_artificial, 1);
20856 if (get_AT (array_die, DW_AT_name))
20857 add_pubtype (type, array_die);
20859 add_alignment_attribute (array_die, type);
20862 /* This routine generates DIE for array with hidden descriptor, details
20863 are filled into *info by a langhook. */
20865 static void
20866 gen_descr_array_type_die (tree type, struct array_descr_info *info,
20867 dw_die_ref context_die)
20869 const dw_die_ref scope_die = scope_die_for (type, context_die);
20870 const dw_die_ref array_die = new_die (DW_TAG_array_type, scope_die, type);
20871 struct loc_descr_context context = { type, info->base_decl, NULL,
20872 false, false };
20873 enum dwarf_tag subrange_tag = DW_TAG_subrange_type;
20874 int dim;
20876 add_name_attribute (array_die, type_tag (type));
20877 equate_type_number_to_die (type, array_die);
20879 if (info->ndimensions > 1)
20880 switch (info->ordering)
20882 case array_descr_ordering_row_major:
20883 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
20884 break;
20885 case array_descr_ordering_column_major:
20886 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
20887 break;
20888 default:
20889 break;
20892 if (dwarf_version >= 3 || !dwarf_strict)
20894 if (info->data_location)
20895 add_scalar_info (array_die, DW_AT_data_location, info->data_location,
20896 dw_scalar_form_exprloc, &context);
20897 if (info->associated)
20898 add_scalar_info (array_die, DW_AT_associated, info->associated,
20899 dw_scalar_form_constant
20900 | dw_scalar_form_exprloc
20901 | dw_scalar_form_reference, &context);
20902 if (info->allocated)
20903 add_scalar_info (array_die, DW_AT_allocated, info->allocated,
20904 dw_scalar_form_constant
20905 | dw_scalar_form_exprloc
20906 | dw_scalar_form_reference, &context);
20907 if (info->stride)
20909 const enum dwarf_attribute attr
20910 = (info->stride_in_bits) ? DW_AT_bit_stride : DW_AT_byte_stride;
20911 const int forms
20912 = (info->stride_in_bits)
20913 ? dw_scalar_form_constant
20914 : (dw_scalar_form_constant
20915 | dw_scalar_form_exprloc
20916 | dw_scalar_form_reference);
20918 add_scalar_info (array_die, attr, info->stride, forms, &context);
20921 if (dwarf_version >= 5)
20923 if (info->rank)
20925 add_scalar_info (array_die, DW_AT_rank, info->rank,
20926 dw_scalar_form_constant
20927 | dw_scalar_form_exprloc, &context);
20928 subrange_tag = DW_TAG_generic_subrange;
20929 context.placeholder_arg = true;
20933 add_gnat_descriptive_type_attribute (array_die, type, context_die);
20935 for (dim = 0; dim < info->ndimensions; dim++)
20937 dw_die_ref subrange_die = new_die (subrange_tag, array_die, NULL);
20939 if (info->dimen[dim].bounds_type)
20940 add_type_attribute (subrange_die,
20941 info->dimen[dim].bounds_type, TYPE_UNQUALIFIED,
20942 false, context_die);
20943 if (info->dimen[dim].lower_bound)
20944 add_bound_info (subrange_die, DW_AT_lower_bound,
20945 info->dimen[dim].lower_bound, &context);
20946 if (info->dimen[dim].upper_bound)
20947 add_bound_info (subrange_die, DW_AT_upper_bound,
20948 info->dimen[dim].upper_bound, &context);
20949 if ((dwarf_version >= 3 || !dwarf_strict) && info->dimen[dim].stride)
20950 add_scalar_info (subrange_die, DW_AT_byte_stride,
20951 info->dimen[dim].stride,
20952 dw_scalar_form_constant
20953 | dw_scalar_form_exprloc
20954 | dw_scalar_form_reference,
20955 &context);
20958 gen_type_die (info->element_type, context_die);
20959 add_type_attribute (array_die, info->element_type, TYPE_UNQUALIFIED,
20960 TREE_CODE (type) == ARRAY_TYPE
20961 && TYPE_REVERSE_STORAGE_ORDER (type),
20962 context_die);
20964 if (get_AT (array_die, DW_AT_name))
20965 add_pubtype (type, array_die);
20967 add_alignment_attribute (array_die, type);
20970 #if 0
20971 static void
20972 gen_entry_point_die (tree decl, dw_die_ref context_die)
20974 tree origin = decl_ultimate_origin (decl);
20975 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
20977 if (origin != NULL)
20978 add_abstract_origin_attribute (decl_die, origin);
20979 else
20981 add_name_and_src_coords_attributes (decl_die, decl);
20982 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
20983 TYPE_UNQUALIFIED, false, context_die);
20986 if (DECL_ABSTRACT_P (decl))
20987 equate_decl_number_to_die (decl, decl_die);
20988 else
20989 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
20991 #endif
20993 /* Walk through the list of incomplete types again, trying once more to
20994 emit full debugging info for them. */
20996 static void
20997 retry_incomplete_types (void)
20999 set_early_dwarf s;
21000 int i;
21002 for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
21003 if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
21004 gen_type_die ((*incomplete_types)[i], comp_unit_die ());
21005 vec_safe_truncate (incomplete_types, 0);
21008 /* Determine what tag to use for a record type. */
21010 static enum dwarf_tag
21011 record_type_tag (tree type)
21013 if (! lang_hooks.types.classify_record)
21014 return DW_TAG_structure_type;
21016 switch (lang_hooks.types.classify_record (type))
21018 case RECORD_IS_STRUCT:
21019 return DW_TAG_structure_type;
21021 case RECORD_IS_CLASS:
21022 return DW_TAG_class_type;
21024 case RECORD_IS_INTERFACE:
21025 if (dwarf_version >= 3 || !dwarf_strict)
21026 return DW_TAG_interface_type;
21027 return DW_TAG_structure_type;
21029 default:
21030 gcc_unreachable ();
21034 /* Generate a DIE to represent an enumeration type. Note that these DIEs
21035 include all of the information about the enumeration values also. Each
21036 enumerated type name/value is listed as a child of the enumerated type
21037 DIE. */
21039 static dw_die_ref
21040 gen_enumeration_type_die (tree type, dw_die_ref context_die)
21042 dw_die_ref type_die = lookup_type_die (type);
21044 if (type_die == NULL)
21046 type_die = new_die (DW_TAG_enumeration_type,
21047 scope_die_for (type, context_die), type);
21048 equate_type_number_to_die (type, type_die);
21049 add_name_attribute (type_die, type_tag (type));
21050 if (dwarf_version >= 4 || !dwarf_strict)
21052 if (ENUM_IS_SCOPED (type))
21053 add_AT_flag (type_die, DW_AT_enum_class, 1);
21054 if (ENUM_IS_OPAQUE (type))
21055 add_AT_flag (type_die, DW_AT_declaration, 1);
21057 if (!dwarf_strict)
21058 add_AT_unsigned (type_die, DW_AT_encoding,
21059 TYPE_UNSIGNED (type)
21060 ? DW_ATE_unsigned
21061 : DW_ATE_signed);
21063 else if (! TYPE_SIZE (type))
21064 return type_die;
21065 else
21066 remove_AT (type_die, DW_AT_declaration);
21068 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
21069 given enum type is incomplete, do not generate the DW_AT_byte_size
21070 attribute or the DW_AT_element_list attribute. */
21071 if (TYPE_SIZE (type))
21073 tree link;
21075 TREE_ASM_WRITTEN (type) = 1;
21076 add_byte_size_attribute (type_die, type);
21077 add_alignment_attribute (type_die, type);
21078 if (dwarf_version >= 3 || !dwarf_strict)
21080 tree underlying = lang_hooks.types.enum_underlying_base_type (type);
21081 add_type_attribute (type_die, underlying, TYPE_UNQUALIFIED, false,
21082 context_die);
21084 if (TYPE_STUB_DECL (type) != NULL_TREE)
21086 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
21087 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
21090 /* If the first reference to this type was as the return type of an
21091 inline function, then it may not have a parent. Fix this now. */
21092 if (type_die->die_parent == NULL)
21093 add_child_die (scope_die_for (type, context_die), type_die);
21095 for (link = TYPE_VALUES (type);
21096 link != NULL; link = TREE_CHAIN (link))
21098 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
21099 tree value = TREE_VALUE (link);
21101 add_name_attribute (enum_die,
21102 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
21104 if (TREE_CODE (value) == CONST_DECL)
21105 value = DECL_INITIAL (value);
21107 if (simple_type_size_in_bits (TREE_TYPE (value))
21108 <= HOST_BITS_PER_WIDE_INT || tree_fits_shwi_p (value))
21110 /* For constant forms created by add_AT_unsigned DWARF
21111 consumers (GDB, elfutils, etc.) always zero extend
21112 the value. Only when the actual value is negative
21113 do we need to use add_AT_int to generate a constant
21114 form that can represent negative values. */
21115 HOST_WIDE_INT val = TREE_INT_CST_LOW (value);
21116 if (TYPE_UNSIGNED (TREE_TYPE (value)) || val >= 0)
21117 add_AT_unsigned (enum_die, DW_AT_const_value,
21118 (unsigned HOST_WIDE_INT) val);
21119 else
21120 add_AT_int (enum_die, DW_AT_const_value, val);
21122 else
21123 /* Enumeration constants may be wider than HOST_WIDE_INT. Handle
21124 that here. TODO: This should be re-worked to use correct
21125 signed/unsigned double tags for all cases. */
21126 add_AT_wide (enum_die, DW_AT_const_value, value);
21129 add_gnat_descriptive_type_attribute (type_die, type, context_die);
21130 if (TYPE_ARTIFICIAL (type))
21131 add_AT_flag (type_die, DW_AT_artificial, 1);
21133 else
21134 add_AT_flag (type_die, DW_AT_declaration, 1);
21136 add_alignment_attribute (type_die, type);
21138 add_pubtype (type, type_die);
21140 return type_die;
21143 /* Generate a DIE to represent either a real live formal parameter decl or to
21144 represent just the type of some formal parameter position in some function
21145 type.
21147 Note that this routine is a bit unusual because its argument may be a
21148 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
21149 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
21150 node. If it's the former then this function is being called to output a
21151 DIE to represent a formal parameter object (or some inlining thereof). If
21152 it's the latter, then this function is only being called to output a
21153 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
21154 argument type of some subprogram type.
21155 If EMIT_NAME_P is true, name and source coordinate attributes
21156 are emitted. */
21158 static dw_die_ref
21159 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
21160 dw_die_ref context_die)
21162 tree node_or_origin = node ? node : origin;
21163 tree ultimate_origin;
21164 dw_die_ref parm_die = NULL;
21166 if (TREE_CODE_CLASS (TREE_CODE (node_or_origin)) == tcc_declaration)
21168 parm_die = lookup_decl_die (node);
21170 /* If the contexts differ, we may not be talking about the same
21171 thing. */
21172 if (parm_die && parm_die->die_parent != context_die)
21174 if (!DECL_ABSTRACT_P (node))
21176 /* This can happen when creating an inlined instance, in
21177 which case we need to create a new DIE that will get
21178 annotated with DW_AT_abstract_origin. */
21179 parm_die = NULL;
21181 else
21183 /* FIXME: Reuse DIE even with a differing context.
21185 This can happen when calling
21186 dwarf2out_abstract_function to build debug info for
21187 the abstract instance of a function for which we have
21188 already generated a DIE in
21189 dwarf2out_early_global_decl.
21191 Once we remove dwarf2out_abstract_function, we should
21192 have a call to gcc_unreachable here. */
21196 if (parm_die && parm_die->die_parent == NULL)
21198 /* Check that parm_die already has the right attributes that
21199 we would have added below. If any attributes are
21200 missing, fall through to add them. */
21201 if (! DECL_ABSTRACT_P (node_or_origin)
21202 && !get_AT (parm_die, DW_AT_location)
21203 && !get_AT (parm_die, DW_AT_const_value))
21204 /* We are missing location info, and are about to add it. */
21206 else
21208 add_child_die (context_die, parm_die);
21209 return parm_die;
21214 /* If we have a previously generated DIE, use it, unless this is an
21215 concrete instance (origin != NULL), in which case we need a new
21216 DIE with a corresponding DW_AT_abstract_origin. */
21217 bool reusing_die;
21218 if (parm_die && origin == NULL)
21219 reusing_die = true;
21220 else
21222 parm_die = new_die (DW_TAG_formal_parameter, context_die, node);
21223 reusing_die = false;
21226 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
21228 case tcc_declaration:
21229 ultimate_origin = decl_ultimate_origin (node_or_origin);
21230 if (node || ultimate_origin)
21231 origin = ultimate_origin;
21233 if (reusing_die)
21234 goto add_location;
21236 if (origin != NULL)
21237 add_abstract_origin_attribute (parm_die, origin);
21238 else if (emit_name_p)
21239 add_name_and_src_coords_attributes (parm_die, node);
21240 if (origin == NULL
21241 || (! DECL_ABSTRACT_P (node_or_origin)
21242 && variably_modified_type_p (TREE_TYPE (node_or_origin),
21243 decl_function_context
21244 (node_or_origin))))
21246 tree type = TREE_TYPE (node_or_origin);
21247 if (decl_by_reference_p (node_or_origin))
21248 add_type_attribute (parm_die, TREE_TYPE (type),
21249 TYPE_UNQUALIFIED,
21250 false, context_die);
21251 else
21252 add_type_attribute (parm_die, type,
21253 decl_quals (node_or_origin),
21254 false, context_die);
21256 if (origin == NULL && DECL_ARTIFICIAL (node))
21257 add_AT_flag (parm_die, DW_AT_artificial, 1);
21258 add_location:
21259 if (node && node != origin)
21260 equate_decl_number_to_die (node, parm_die);
21261 if (! DECL_ABSTRACT_P (node_or_origin))
21262 add_location_or_const_value_attribute (parm_die, node_or_origin,
21263 node == NULL);
21265 break;
21267 case tcc_type:
21268 /* We were called with some kind of a ..._TYPE node. */
21269 add_type_attribute (parm_die, node_or_origin, TYPE_UNQUALIFIED, false,
21270 context_die);
21271 break;
21273 default:
21274 gcc_unreachable ();
21277 return parm_die;
21280 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
21281 children DW_TAG_formal_parameter DIEs representing the arguments of the
21282 parameter pack.
21284 PARM_PACK must be a function parameter pack.
21285 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
21286 must point to the subsequent arguments of the function PACK_ARG belongs to.
21287 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
21288 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
21289 following the last one for which a DIE was generated. */
21291 static dw_die_ref
21292 gen_formal_parameter_pack_die (tree parm_pack,
21293 tree pack_arg,
21294 dw_die_ref subr_die,
21295 tree *next_arg)
21297 tree arg;
21298 dw_die_ref parm_pack_die;
21300 gcc_assert (parm_pack
21301 && lang_hooks.function_parameter_pack_p (parm_pack)
21302 && subr_die);
21304 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
21305 add_src_coords_attributes (parm_pack_die, parm_pack);
21307 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
21309 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
21310 parm_pack))
21311 break;
21312 gen_formal_parameter_die (arg, NULL,
21313 false /* Don't emit name attribute. */,
21314 parm_pack_die);
21316 if (next_arg)
21317 *next_arg = arg;
21318 return parm_pack_die;
21321 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
21322 at the end of an (ANSI prototyped) formal parameters list. */
21324 static void
21325 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
21327 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
21330 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
21331 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
21332 parameters as specified in some function type specification (except for
21333 those which appear as part of a function *definition*). */
21335 static void
21336 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
21338 tree link;
21339 tree formal_type = NULL;
21340 tree first_parm_type;
21341 tree arg;
21343 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
21345 arg = DECL_ARGUMENTS (function_or_method_type);
21346 function_or_method_type = TREE_TYPE (function_or_method_type);
21348 else
21349 arg = NULL_TREE;
21351 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
21353 /* Make our first pass over the list of formal parameter types and output a
21354 DW_TAG_formal_parameter DIE for each one. */
21355 for (link = first_parm_type; link; )
21357 dw_die_ref parm_die;
21359 formal_type = TREE_VALUE (link);
21360 if (formal_type == void_type_node)
21361 break;
21363 /* Output a (nameless) DIE to represent the formal parameter itself. */
21364 if (!POINTER_BOUNDS_TYPE_P (formal_type))
21366 parm_die = gen_formal_parameter_die (formal_type, NULL,
21367 true /* Emit name attribute. */,
21368 context_die);
21369 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
21370 && link == first_parm_type)
21372 add_AT_flag (parm_die, DW_AT_artificial, 1);
21373 if (dwarf_version >= 3 || !dwarf_strict)
21374 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
21376 else if (arg && DECL_ARTIFICIAL (arg))
21377 add_AT_flag (parm_die, DW_AT_artificial, 1);
21380 link = TREE_CHAIN (link);
21381 if (arg)
21382 arg = DECL_CHAIN (arg);
21385 /* If this function type has an ellipsis, add a
21386 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
21387 if (formal_type != void_type_node)
21388 gen_unspecified_parameters_die (function_or_method_type, context_die);
21390 /* Make our second (and final) pass over the list of formal parameter types
21391 and output DIEs to represent those types (as necessary). */
21392 for (link = TYPE_ARG_TYPES (function_or_method_type);
21393 link && TREE_VALUE (link);
21394 link = TREE_CHAIN (link))
21395 gen_type_die (TREE_VALUE (link), context_die);
21398 /* We want to generate the DIE for TYPE so that we can generate the
21399 die for MEMBER, which has been defined; we will need to refer back
21400 to the member declaration nested within TYPE. If we're trying to
21401 generate minimal debug info for TYPE, processing TYPE won't do the
21402 trick; we need to attach the member declaration by hand. */
21404 static void
21405 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
21407 gen_type_die (type, context_die);
21409 /* If we're trying to avoid duplicate debug info, we may not have
21410 emitted the member decl for this function. Emit it now. */
21411 if (TYPE_STUB_DECL (type)
21412 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
21413 && ! lookup_decl_die (member))
21415 dw_die_ref type_die;
21416 gcc_assert (!decl_ultimate_origin (member));
21418 push_decl_scope (type);
21419 type_die = lookup_type_die_strip_naming_typedef (type);
21420 if (TREE_CODE (member) == FUNCTION_DECL)
21421 gen_subprogram_die (member, type_die);
21422 else if (TREE_CODE (member) == FIELD_DECL)
21424 /* Ignore the nameless fields that are used to skip bits but handle
21425 C++ anonymous unions and structs. */
21426 if (DECL_NAME (member) != NULL_TREE
21427 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
21428 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
21430 struct vlr_context vlr_ctx = {
21431 DECL_CONTEXT (member), /* struct_type */
21432 NULL_TREE /* variant_part_offset */
21434 gen_type_die (member_declared_type (member), type_die);
21435 gen_field_die (member, &vlr_ctx, type_die);
21438 else
21439 gen_variable_die (member, NULL_TREE, type_die);
21441 pop_decl_scope ();
21445 /* Forward declare these functions, because they are mutually recursive
21446 with their set_block_* pairing functions. */
21447 static void set_decl_origin_self (tree);
21448 static void set_decl_abstract_flags (tree, vec<tree> &);
21450 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
21451 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
21452 that it points to the node itself, thus indicating that the node is its
21453 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
21454 the given node is NULL, recursively descend the decl/block tree which
21455 it is the root of, and for each other ..._DECL or BLOCK node contained
21456 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
21457 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
21458 values to point to themselves. */
21460 static void
21461 set_block_origin_self (tree stmt)
21463 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
21465 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
21468 tree local_decl;
21470 for (local_decl = BLOCK_VARS (stmt);
21471 local_decl != NULL_TREE;
21472 local_decl = DECL_CHAIN (local_decl))
21473 /* Do not recurse on nested functions since the inlining status
21474 of parent and child can be different as per the DWARF spec. */
21475 if (TREE_CODE (local_decl) != FUNCTION_DECL
21476 && !DECL_EXTERNAL (local_decl))
21477 set_decl_origin_self (local_decl);
21481 tree subblock;
21483 for (subblock = BLOCK_SUBBLOCKS (stmt);
21484 subblock != NULL_TREE;
21485 subblock = BLOCK_CHAIN (subblock))
21486 set_block_origin_self (subblock); /* Recurse. */
21491 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
21492 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
21493 node to so that it points to the node itself, thus indicating that the
21494 node represents its own (abstract) origin. Additionally, if the
21495 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
21496 the decl/block tree of which the given node is the root of, and for
21497 each other ..._DECL or BLOCK node contained therein whose
21498 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
21499 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
21500 point to themselves. */
21502 static void
21503 set_decl_origin_self (tree decl)
21505 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
21507 DECL_ABSTRACT_ORIGIN (decl) = decl;
21508 if (TREE_CODE (decl) == FUNCTION_DECL)
21510 tree arg;
21512 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
21513 DECL_ABSTRACT_ORIGIN (arg) = arg;
21514 if (DECL_INITIAL (decl) != NULL_TREE
21515 && DECL_INITIAL (decl) != error_mark_node)
21516 set_block_origin_self (DECL_INITIAL (decl));
21521 /* Given a pointer to some BLOCK node, set the BLOCK_ABSTRACT flag to 1
21522 and if it wasn't 1 before, push it to abstract_vec vector.
21523 For all local decls and all local sub-blocks (recursively) do it
21524 too. */
21526 static void
21527 set_block_abstract_flags (tree stmt, vec<tree> &abstract_vec)
21529 tree local_decl;
21530 tree subblock;
21531 unsigned int i;
21533 if (!BLOCK_ABSTRACT (stmt))
21535 abstract_vec.safe_push (stmt);
21536 BLOCK_ABSTRACT (stmt) = 1;
21539 for (local_decl = BLOCK_VARS (stmt);
21540 local_decl != NULL_TREE;
21541 local_decl = DECL_CHAIN (local_decl))
21542 if (! DECL_EXTERNAL (local_decl))
21543 set_decl_abstract_flags (local_decl, abstract_vec);
21545 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
21547 local_decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
21548 if ((VAR_P (local_decl) && !TREE_STATIC (local_decl))
21549 || TREE_CODE (local_decl) == PARM_DECL)
21550 set_decl_abstract_flags (local_decl, abstract_vec);
21553 for (subblock = BLOCK_SUBBLOCKS (stmt);
21554 subblock != NULL_TREE;
21555 subblock = BLOCK_CHAIN (subblock))
21556 set_block_abstract_flags (subblock, abstract_vec);
21559 /* Given a pointer to some ..._DECL node, set DECL_ABSTRACT_P flag on it
21560 to 1 and if it wasn't 1 before, push to abstract_vec vector.
21561 In the case where the decl is a FUNCTION_DECL also set the abstract
21562 flags for all of the parameters, local vars, local
21563 blocks and sub-blocks (recursively). */
21565 static void
21566 set_decl_abstract_flags (tree decl, vec<tree> &abstract_vec)
21568 if (!DECL_ABSTRACT_P (decl))
21570 abstract_vec.safe_push (decl);
21571 DECL_ABSTRACT_P (decl) = 1;
21574 if (TREE_CODE (decl) == FUNCTION_DECL)
21576 tree arg;
21578 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
21579 if (!DECL_ABSTRACT_P (arg))
21581 abstract_vec.safe_push (arg);
21582 DECL_ABSTRACT_P (arg) = 1;
21584 if (DECL_INITIAL (decl) != NULL_TREE
21585 && DECL_INITIAL (decl) != error_mark_node)
21586 set_block_abstract_flags (DECL_INITIAL (decl), abstract_vec);
21590 /* Generate the DWARF2 info for the "abstract" instance of a function which we
21591 may later generate inlined and/or out-of-line instances of.
21593 FIXME: In the early-dwarf world, this function, and most of the
21594 DECL_ABSTRACT code should be obsoleted. The early DIE _is_
21595 the abstract instance. All we would need to do is annotate
21596 the early DIE with the appropriate DW_AT_inline in late
21597 dwarf (perhaps in gen_inlined_subroutine_die).
21599 However, we can't do this yet, because LTO streaming of DIEs
21600 has not been implemented yet. */
21602 static void
21603 dwarf2out_abstract_function (tree decl)
21605 dw_die_ref old_die;
21606 tree save_fn;
21607 tree context;
21608 hash_table<decl_loc_hasher> *old_decl_loc_table;
21609 hash_table<dw_loc_list_hasher> *old_cached_dw_loc_list_table;
21610 int old_call_site_count, old_tail_call_site_count;
21611 struct call_arg_loc_node *old_call_arg_locations;
21613 /* Make sure we have the actual abstract inline, not a clone. */
21614 decl = DECL_ORIGIN (decl);
21616 old_die = lookup_decl_die (decl);
21617 if (old_die && get_AT (old_die, DW_AT_inline))
21618 /* We've already generated the abstract instance. */
21619 return;
21621 /* We can be called while recursively when seeing block defining inlined subroutine
21622 DIE. Be sure to not clobber the outer location table nor use it or we would
21623 get locations in abstract instantces. */
21624 old_decl_loc_table = decl_loc_table;
21625 decl_loc_table = NULL;
21626 old_cached_dw_loc_list_table = cached_dw_loc_list_table;
21627 cached_dw_loc_list_table = NULL;
21628 old_call_arg_locations = call_arg_locations;
21629 call_arg_locations = NULL;
21630 old_call_site_count = call_site_count;
21631 call_site_count = -1;
21632 old_tail_call_site_count = tail_call_site_count;
21633 tail_call_site_count = -1;
21635 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
21636 we don't get confused by DECL_ABSTRACT_P. */
21637 if (debug_info_level > DINFO_LEVEL_TERSE)
21639 context = decl_class_context (decl);
21640 if (context)
21641 gen_type_die_for_member
21642 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
21645 /* Pretend we've just finished compiling this function. */
21646 save_fn = current_function_decl;
21647 current_function_decl = decl;
21649 auto_vec<tree, 64> abstract_vec;
21650 set_decl_abstract_flags (decl, abstract_vec);
21651 dwarf2out_decl (decl);
21652 unsigned int i;
21653 tree t;
21654 FOR_EACH_VEC_ELT (abstract_vec, i, t)
21655 if (TREE_CODE (t) == BLOCK)
21656 BLOCK_ABSTRACT (t) = 0;
21657 else
21658 DECL_ABSTRACT_P (t) = 0;
21660 current_function_decl = save_fn;
21661 decl_loc_table = old_decl_loc_table;
21662 cached_dw_loc_list_table = old_cached_dw_loc_list_table;
21663 call_arg_locations = old_call_arg_locations;
21664 call_site_count = old_call_site_count;
21665 tail_call_site_count = old_tail_call_site_count;
21668 /* Helper function of premark_used_types() which gets called through
21669 htab_traverse.
21671 Marks the DIE of a given type in *SLOT as perennial, so it never gets
21672 marked as unused by prune_unused_types. */
21674 bool
21675 premark_used_types_helper (tree const &type, void *)
21677 dw_die_ref die;
21679 die = lookup_type_die (type);
21680 if (die != NULL)
21681 die->die_perennial_p = 1;
21682 return true;
21685 /* Helper function of premark_types_used_by_global_vars which gets called
21686 through htab_traverse.
21688 Marks the DIE of a given type in *SLOT as perennial, so it never gets
21689 marked as unused by prune_unused_types. The DIE of the type is marked
21690 only if the global variable using the type will actually be emitted. */
21693 premark_types_used_by_global_vars_helper (types_used_by_vars_entry **slot,
21694 void *)
21696 struct types_used_by_vars_entry *entry;
21697 dw_die_ref die;
21699 entry = (struct types_used_by_vars_entry *) *slot;
21700 gcc_assert (entry->type != NULL
21701 && entry->var_decl != NULL);
21702 die = lookup_type_die (entry->type);
21703 if (die)
21705 /* Ask cgraph if the global variable really is to be emitted.
21706 If yes, then we'll keep the DIE of ENTRY->TYPE. */
21707 varpool_node *node = varpool_node::get (entry->var_decl);
21708 if (node && node->definition)
21710 die->die_perennial_p = 1;
21711 /* Keep the parent DIEs as well. */
21712 while ((die = die->die_parent) && die->die_perennial_p == 0)
21713 die->die_perennial_p = 1;
21716 return 1;
21719 /* Mark all members of used_types_hash as perennial. */
21721 static void
21722 premark_used_types (struct function *fun)
21724 if (fun && fun->used_types_hash)
21725 fun->used_types_hash->traverse<void *, premark_used_types_helper> (NULL);
21728 /* Mark all members of types_used_by_vars_entry as perennial. */
21730 static void
21731 premark_types_used_by_global_vars (void)
21733 if (types_used_by_vars_hash)
21734 types_used_by_vars_hash
21735 ->traverse<void *, premark_types_used_by_global_vars_helper> (NULL);
21738 /* Generate a DW_TAG_call_site DIE in function DECL under SUBR_DIE
21739 for CA_LOC call arg loc node. */
21741 static dw_die_ref
21742 gen_call_site_die (tree decl, dw_die_ref subr_die,
21743 struct call_arg_loc_node *ca_loc)
21745 dw_die_ref stmt_die = NULL, die;
21746 tree block = ca_loc->block;
21748 while (block
21749 && block != DECL_INITIAL (decl)
21750 && TREE_CODE (block) == BLOCK)
21752 stmt_die = BLOCK_DIE (block);
21753 if (stmt_die)
21754 break;
21755 block = BLOCK_SUPERCONTEXT (block);
21757 if (stmt_die == NULL)
21758 stmt_die = subr_die;
21759 die = new_die (dwarf_TAG (DW_TAG_call_site), stmt_die, NULL_TREE);
21760 add_AT_lbl_id (die, dwarf_AT (DW_AT_call_return_pc), ca_loc->label);
21761 if (ca_loc->tail_call_p)
21762 add_AT_flag (die, dwarf_AT (DW_AT_call_tail_call), 1);
21763 if (ca_loc->symbol_ref)
21765 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
21766 if (tdie)
21767 add_AT_die_ref (die, dwarf_AT (DW_AT_call_origin), tdie);
21768 else
21769 add_AT_addr (die, dwarf_AT (DW_AT_call_origin), ca_loc->symbol_ref,
21770 false);
21772 return die;
21775 /* Generate a DIE to represent a declared function (either file-scope or
21776 block-local). */
21778 static void
21779 gen_subprogram_die (tree decl, dw_die_ref context_die)
21781 tree origin = decl_ultimate_origin (decl);
21782 dw_die_ref subr_die;
21783 dw_die_ref old_die = lookup_decl_die (decl);
21785 /* This function gets called multiple times for different stages of
21786 the debug process. For example, for func() in this code:
21788 namespace S
21790 void func() { ... }
21793 ...we get called 4 times. Twice in early debug and twice in
21794 late debug:
21796 Early debug
21797 -----------
21799 1. Once while generating func() within the namespace. This is
21800 the declaration. The declaration bit below is set, as the
21801 context is the namespace.
21803 A new DIE will be generated with DW_AT_declaration set.
21805 2. Once for func() itself. This is the specification. The
21806 declaration bit below is clear as the context is the CU.
21808 We will use the cached DIE from (1) to create a new DIE with
21809 DW_AT_specification pointing to the declaration in (1).
21811 Late debug via rest_of_handle_final()
21812 -------------------------------------
21814 3. Once generating func() within the namespace. This is also the
21815 declaration, as in (1), but this time we will early exit below
21816 as we have a cached DIE and a declaration needs no additional
21817 annotations (no locations), as the source declaration line
21818 info is enough.
21820 4. Once for func() itself. As in (2), this is the specification,
21821 but this time we will re-use the cached DIE, and just annotate
21822 it with the location information that should now be available.
21824 For something without namespaces, but with abstract instances, we
21825 are also called a multiple times:
21827 class Base
21829 public:
21830 Base (); // constructor declaration (1)
21833 Base::Base () { } // constructor specification (2)
21835 Early debug
21836 -----------
21838 1. Once for the Base() constructor by virtue of it being a
21839 member of the Base class. This is done via
21840 rest_of_type_compilation.
21842 This is a declaration, so a new DIE will be created with
21843 DW_AT_declaration.
21845 2. Once for the Base() constructor definition, but this time
21846 while generating the abstract instance of the base
21847 constructor (__base_ctor) which is being generated via early
21848 debug of reachable functions.
21850 Even though we have a cached version of the declaration (1),
21851 we will create a DW_AT_specification of the declaration DIE
21852 in (1).
21854 3. Once for the __base_ctor itself, but this time, we generate
21855 an DW_AT_abstract_origin version of the DW_AT_specification in
21856 (2).
21858 Late debug via rest_of_handle_final
21859 -----------------------------------
21861 4. One final time for the __base_ctor (which will have a cached
21862 DIE with DW_AT_abstract_origin created in (3). This time,
21863 we will just annotate the location information now
21864 available.
21866 int declaration = (current_function_decl != decl
21867 || class_or_namespace_scope_p (context_die));
21869 /* Now that the C++ front end lazily declares artificial member fns, we
21870 might need to retrofit the declaration into its class. */
21871 if (!declaration && !origin && !old_die
21872 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
21873 && !class_or_namespace_scope_p (context_die)
21874 && debug_info_level > DINFO_LEVEL_TERSE)
21875 old_die = force_decl_die (decl);
21877 /* An inlined instance, tag a new DIE with DW_AT_abstract_origin. */
21878 if (origin != NULL)
21880 gcc_assert (!declaration || local_scope_p (context_die));
21882 /* Fixup die_parent for the abstract instance of a nested
21883 inline function. */
21884 if (old_die && old_die->die_parent == NULL)
21885 add_child_die (context_die, old_die);
21887 if (old_die && get_AT_ref (old_die, DW_AT_abstract_origin))
21889 /* If we have a DW_AT_abstract_origin we have a working
21890 cached version. */
21891 subr_die = old_die;
21893 else
21895 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
21896 add_abstract_origin_attribute (subr_die, origin);
21897 /* This is where the actual code for a cloned function is.
21898 Let's emit linkage name attribute for it. This helps
21899 debuggers to e.g, set breakpoints into
21900 constructors/destructors when the user asks "break
21901 K::K". */
21902 add_linkage_name (subr_die, decl);
21905 /* A cached copy, possibly from early dwarf generation. Reuse as
21906 much as possible. */
21907 else if (old_die)
21909 /* A declaration that has been previously dumped needs no
21910 additional information. */
21911 if (declaration)
21912 return;
21914 if (!get_AT_flag (old_die, DW_AT_declaration)
21915 /* We can have a normal definition following an inline one in the
21916 case of redefinition of GNU C extern inlines.
21917 It seems reasonable to use AT_specification in this case. */
21918 && !get_AT (old_die, DW_AT_inline))
21920 /* Detect and ignore this case, where we are trying to output
21921 something we have already output. */
21922 if (get_AT (old_die, DW_AT_low_pc)
21923 || get_AT (old_die, DW_AT_ranges))
21924 return;
21926 /* If we have no location information, this must be a
21927 partially generated DIE from early dwarf generation.
21928 Fall through and generate it. */
21931 /* If the definition comes from the same place as the declaration,
21932 maybe use the old DIE. We always want the DIE for this function
21933 that has the *_pc attributes to be under comp_unit_die so the
21934 debugger can find it. We also need to do this for abstract
21935 instances of inlines, since the spec requires the out-of-line copy
21936 to have the same parent. For local class methods, this doesn't
21937 apply; we just use the old DIE. */
21938 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
21939 struct dwarf_file_data * file_index = lookup_filename (s.file);
21940 if ((is_cu_die (old_die->die_parent)
21941 /* This condition fixes the inconsistency/ICE with the
21942 following Fortran test (or some derivative thereof) while
21943 building libgfortran:
21945 module some_m
21946 contains
21947 logical function funky (FLAG)
21948 funky = .true.
21949 end function
21950 end module
21952 || (old_die->die_parent
21953 && old_die->die_parent->die_tag == DW_TAG_module)
21954 || context_die == NULL)
21955 && (DECL_ARTIFICIAL (decl)
21956 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
21957 && (get_AT_unsigned (old_die, DW_AT_decl_line)
21958 == (unsigned) s.line)
21959 && (!debug_column_info
21960 || s.column == 0
21961 || (get_AT_unsigned (old_die, DW_AT_decl_column)
21962 == (unsigned) s.column)))))
21964 subr_die = old_die;
21966 /* Clear out the declaration attribute, but leave the
21967 parameters so they can be augmented with location
21968 information later. Unless this was a declaration, in
21969 which case, wipe out the nameless parameters and recreate
21970 them further down. */
21971 if (remove_AT (subr_die, DW_AT_declaration))
21974 remove_AT (subr_die, DW_AT_object_pointer);
21975 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
21978 /* Make a specification pointing to the previously built
21979 declaration. */
21980 else
21982 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
21983 add_AT_specification (subr_die, old_die);
21984 add_pubname (decl, subr_die);
21985 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
21986 add_AT_file (subr_die, DW_AT_decl_file, file_index);
21987 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
21988 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
21989 if (debug_column_info
21990 && s.column
21991 && (get_AT_unsigned (old_die, DW_AT_decl_column)
21992 != (unsigned) s.column))
21993 add_AT_unsigned (subr_die, DW_AT_decl_column, s.column);
21995 /* If the prototype had an 'auto' or 'decltype(auto)' return type,
21996 emit the real type on the definition die. */
21997 if (is_cxx () && debug_info_level > DINFO_LEVEL_TERSE)
21999 dw_die_ref die = get_AT_ref (old_die, DW_AT_type);
22000 if (die == auto_die || die == decltype_auto_die)
22001 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
22002 TYPE_UNQUALIFIED, false, context_die);
22005 /* When we process the method declaration, we haven't seen
22006 the out-of-class defaulted definition yet, so we have to
22007 recheck now. */
22008 if ((dwarf_version >= 5 || ! dwarf_strict)
22009 && !get_AT (subr_die, DW_AT_defaulted))
22011 int defaulted
22012 = lang_hooks.decls.decl_dwarf_attribute (decl,
22013 DW_AT_defaulted);
22014 if (defaulted != -1)
22016 /* Other values must have been handled before. */
22017 gcc_assert (defaulted == DW_DEFAULTED_out_of_class);
22018 add_AT_unsigned (subr_die, DW_AT_defaulted, defaulted);
22023 /* Create a fresh DIE for anything else. */
22024 else
22026 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
22028 if (TREE_PUBLIC (decl))
22029 add_AT_flag (subr_die, DW_AT_external, 1);
22031 add_name_and_src_coords_attributes (subr_die, decl);
22032 add_pubname (decl, subr_die);
22033 if (debug_info_level > DINFO_LEVEL_TERSE)
22035 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
22036 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
22037 TYPE_UNQUALIFIED, false, context_die);
22040 add_pure_or_virtual_attribute (subr_die, decl);
22041 if (DECL_ARTIFICIAL (decl))
22042 add_AT_flag (subr_die, DW_AT_artificial, 1);
22044 if (TREE_THIS_VOLATILE (decl) && (dwarf_version >= 5 || !dwarf_strict))
22045 add_AT_flag (subr_die, DW_AT_noreturn, 1);
22047 add_alignment_attribute (subr_die, decl);
22049 add_accessibility_attribute (subr_die, decl);
22052 /* Unless we have an existing non-declaration DIE, equate the new
22053 DIE. */
22054 if (!old_die || is_declaration_die (old_die))
22055 equate_decl_number_to_die (decl, subr_die);
22057 if (declaration)
22059 if (!old_die || !get_AT (old_die, DW_AT_inline))
22061 add_AT_flag (subr_die, DW_AT_declaration, 1);
22063 /* If this is an explicit function declaration then generate
22064 a DW_AT_explicit attribute. */
22065 if ((dwarf_version >= 3 || !dwarf_strict)
22066 && lang_hooks.decls.decl_dwarf_attribute (decl,
22067 DW_AT_explicit) == 1)
22068 add_AT_flag (subr_die, DW_AT_explicit, 1);
22070 /* If this is a C++11 deleted special function member then generate
22071 a DW_AT_deleted attribute. */
22072 if ((dwarf_version >= 5 || !dwarf_strict)
22073 && lang_hooks.decls.decl_dwarf_attribute (decl,
22074 DW_AT_deleted) == 1)
22075 add_AT_flag (subr_die, DW_AT_deleted, 1);
22077 /* If this is a C++11 defaulted special function member then
22078 generate a DW_AT_defaulted attribute. */
22079 if (dwarf_version >= 5 || !dwarf_strict)
22081 int defaulted
22082 = lang_hooks.decls.decl_dwarf_attribute (decl,
22083 DW_AT_defaulted);
22084 if (defaulted != -1)
22085 add_AT_unsigned (subr_die, DW_AT_defaulted, defaulted);
22088 /* If this is a C++11 non-static member function with & ref-qualifier
22089 then generate a DW_AT_reference attribute. */
22090 if ((dwarf_version >= 5 || !dwarf_strict)
22091 && lang_hooks.decls.decl_dwarf_attribute (decl,
22092 DW_AT_reference) == 1)
22093 add_AT_flag (subr_die, DW_AT_reference, 1);
22095 /* If this is a C++11 non-static member function with &&
22096 ref-qualifier then generate a DW_AT_reference attribute. */
22097 if ((dwarf_version >= 5 || !dwarf_strict)
22098 && lang_hooks.decls.decl_dwarf_attribute (decl,
22099 DW_AT_rvalue_reference)
22100 == 1)
22101 add_AT_flag (subr_die, DW_AT_rvalue_reference, 1);
22104 /* Tag abstract instances with DW_AT_inline. */
22105 else if (DECL_ABSTRACT_P (decl))
22107 if (DECL_DECLARED_INLINE_P (decl))
22109 if (cgraph_function_possibly_inlined_p (decl))
22110 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
22111 else
22112 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
22114 else
22116 if (cgraph_function_possibly_inlined_p (decl))
22117 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
22118 else
22119 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
22122 if (DECL_DECLARED_INLINE_P (decl)
22123 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
22124 add_AT_flag (subr_die, DW_AT_artificial, 1);
22126 /* For non DECL_EXTERNALs, if range information is available, fill
22127 the DIE with it. */
22128 else if (!DECL_EXTERNAL (decl) && !early_dwarf)
22130 HOST_WIDE_INT cfa_fb_offset;
22132 struct function *fun = DECL_STRUCT_FUNCTION (decl);
22134 if (!flag_reorder_blocks_and_partition)
22136 dw_fde_ref fde = fun->fde;
22137 if (fde->dw_fde_begin)
22139 /* We have already generated the labels. */
22140 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
22141 fde->dw_fde_end, false);
22143 else
22145 /* Create start/end labels and add the range. */
22146 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
22147 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
22148 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
22149 current_function_funcdef_no);
22150 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
22151 current_function_funcdef_no);
22152 add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
22153 false);
22156 #if VMS_DEBUGGING_INFO
22157 /* HP OpenVMS Industry Standard 64: DWARF Extensions
22158 Section 2.3 Prologue and Epilogue Attributes:
22159 When a breakpoint is set on entry to a function, it is generally
22160 desirable for execution to be suspended, not on the very first
22161 instruction of the function, but rather at a point after the
22162 function's frame has been set up, after any language defined local
22163 declaration processing has been completed, and before execution of
22164 the first statement of the function begins. Debuggers generally
22165 cannot properly determine where this point is. Similarly for a
22166 breakpoint set on exit from a function. The prologue and epilogue
22167 attributes allow a compiler to communicate the location(s) to use. */
22170 if (fde->dw_fde_vms_end_prologue)
22171 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
22172 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
22174 if (fde->dw_fde_vms_begin_epilogue)
22175 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
22176 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
22178 #endif
22181 else
22183 /* Generate pubnames entries for the split function code ranges. */
22184 dw_fde_ref fde = fun->fde;
22186 if (fde->dw_fde_second_begin)
22188 if (dwarf_version >= 3 || !dwarf_strict)
22190 /* We should use ranges for non-contiguous code section
22191 addresses. Use the actual code range for the initial
22192 section, since the HOT/COLD labels might precede an
22193 alignment offset. */
22194 bool range_list_added = false;
22195 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
22196 fde->dw_fde_end, &range_list_added,
22197 false);
22198 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
22199 fde->dw_fde_second_end,
22200 &range_list_added, false);
22201 if (range_list_added)
22202 add_ranges (NULL);
22204 else
22206 /* There is no real support in DW2 for this .. so we make
22207 a work-around. First, emit the pub name for the segment
22208 containing the function label. Then make and emit a
22209 simplified subprogram DIE for the second segment with the
22210 name pre-fixed by __hot/cold_sect_of_. We use the same
22211 linkage name for the second die so that gdb will find both
22212 sections when given "b foo". */
22213 const char *name = NULL;
22214 tree decl_name = DECL_NAME (decl);
22215 dw_die_ref seg_die;
22217 /* Do the 'primary' section. */
22218 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
22219 fde->dw_fde_end, false);
22221 /* Build a minimal DIE for the secondary section. */
22222 seg_die = new_die (DW_TAG_subprogram,
22223 subr_die->die_parent, decl);
22225 if (TREE_PUBLIC (decl))
22226 add_AT_flag (seg_die, DW_AT_external, 1);
22228 if (decl_name != NULL
22229 && IDENTIFIER_POINTER (decl_name) != NULL)
22231 name = dwarf2_name (decl, 1);
22232 if (! DECL_ARTIFICIAL (decl))
22233 add_src_coords_attributes (seg_die, decl);
22235 add_linkage_name (seg_die, decl);
22237 gcc_assert (name != NULL);
22238 add_pure_or_virtual_attribute (seg_die, decl);
22239 if (DECL_ARTIFICIAL (decl))
22240 add_AT_flag (seg_die, DW_AT_artificial, 1);
22242 name = concat ("__second_sect_of_", name, NULL);
22243 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
22244 fde->dw_fde_second_end, false);
22245 add_name_attribute (seg_die, name);
22246 if (want_pubnames ())
22247 add_pubname_string (name, seg_die);
22250 else
22251 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
22252 false);
22255 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
22257 /* We define the "frame base" as the function's CFA. This is more
22258 convenient for several reasons: (1) It's stable across the prologue
22259 and epilogue, which makes it better than just a frame pointer,
22260 (2) With dwarf3, there exists a one-byte encoding that allows us
22261 to reference the .debug_frame data by proxy, but failing that,
22262 (3) We can at least reuse the code inspection and interpretation
22263 code that determines the CFA position at various points in the
22264 function. */
22265 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
22267 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
22268 add_AT_loc (subr_die, DW_AT_frame_base, op);
22270 else
22272 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
22273 if (list->dw_loc_next)
22274 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
22275 else
22276 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
22279 /* Compute a displacement from the "steady-state frame pointer" to
22280 the CFA. The former is what all stack slots and argument slots
22281 will reference in the rtl; the latter is what we've told the
22282 debugger about. We'll need to adjust all frame_base references
22283 by this displacement. */
22284 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
22286 if (fun->static_chain_decl)
22288 /* DWARF requires here a location expression that computes the
22289 address of the enclosing subprogram's frame base. The machinery
22290 in tree-nested.c is supposed to store this specific address in the
22291 last field of the FRAME record. */
22292 const tree frame_type
22293 = TREE_TYPE (TREE_TYPE (fun->static_chain_decl));
22294 const tree fb_decl = tree_last (TYPE_FIELDS (frame_type));
22296 tree fb_expr
22297 = build1 (INDIRECT_REF, frame_type, fun->static_chain_decl);
22298 fb_expr = build3 (COMPONENT_REF, TREE_TYPE (fb_decl),
22299 fb_expr, fb_decl, NULL_TREE);
22301 add_AT_location_description (subr_die, DW_AT_static_link,
22302 loc_list_from_tree (fb_expr, 0, NULL));
22305 resolve_variable_values ();
22308 /* Generate child dies for template paramaters. */
22309 if (early_dwarf && debug_info_level > DINFO_LEVEL_TERSE)
22310 gen_generic_params_dies (decl);
22312 /* Now output descriptions of the arguments for this function. This gets
22313 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
22314 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
22315 `...' at the end of the formal parameter list. In order to find out if
22316 there was a trailing ellipsis or not, we must instead look at the type
22317 associated with the FUNCTION_DECL. This will be a node of type
22318 FUNCTION_TYPE. If the chain of type nodes hanging off of this
22319 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
22320 an ellipsis at the end. */
22322 /* In the case where we are describing a mere function declaration, all we
22323 need to do here (and all we *can* do here) is to describe the *types* of
22324 its formal parameters. */
22325 if (debug_info_level <= DINFO_LEVEL_TERSE)
22327 else if (declaration)
22328 gen_formal_types_die (decl, subr_die);
22329 else
22331 /* Generate DIEs to represent all known formal parameters. */
22332 tree parm = DECL_ARGUMENTS (decl);
22333 tree generic_decl = early_dwarf
22334 ? lang_hooks.decls.get_generic_function_decl (decl) : NULL;
22335 tree generic_decl_parm = generic_decl
22336 ? DECL_ARGUMENTS (generic_decl)
22337 : NULL;
22339 /* Now we want to walk the list of parameters of the function and
22340 emit their relevant DIEs.
22342 We consider the case of DECL being an instance of a generic function
22343 as well as it being a normal function.
22345 If DECL is an instance of a generic function we walk the
22346 parameters of the generic function declaration _and_ the parameters of
22347 DECL itself. This is useful because we want to emit specific DIEs for
22348 function parameter packs and those are declared as part of the
22349 generic function declaration. In that particular case,
22350 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
22351 That DIE has children DIEs representing the set of arguments
22352 of the pack. Note that the set of pack arguments can be empty.
22353 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
22354 children DIE.
22356 Otherwise, we just consider the parameters of DECL. */
22357 while (generic_decl_parm || parm)
22359 if (generic_decl_parm
22360 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
22361 gen_formal_parameter_pack_die (generic_decl_parm,
22362 parm, subr_die,
22363 &parm);
22364 else if (parm && !POINTER_BOUNDS_P (parm))
22366 dw_die_ref parm_die = gen_decl_die (parm, NULL, NULL, subr_die);
22368 if (parm == DECL_ARGUMENTS (decl)
22369 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
22370 && parm_die
22371 && (dwarf_version >= 3 || !dwarf_strict))
22372 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
22374 parm = DECL_CHAIN (parm);
22376 else if (parm)
22377 parm = DECL_CHAIN (parm);
22379 if (generic_decl_parm)
22380 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
22383 /* Decide whether we need an unspecified_parameters DIE at the end.
22384 There are 2 more cases to do this for: 1) the ansi ... declaration -
22385 this is detectable when the end of the arg list is not a
22386 void_type_node 2) an unprototyped function declaration (not a
22387 definition). This just means that we have no info about the
22388 parameters at all. */
22389 if (early_dwarf)
22391 if (prototype_p (TREE_TYPE (decl)))
22393 /* This is the prototyped case, check for.... */
22394 if (stdarg_p (TREE_TYPE (decl)))
22395 gen_unspecified_parameters_die (decl, subr_die);
22397 else if (DECL_INITIAL (decl) == NULL_TREE)
22398 gen_unspecified_parameters_die (decl, subr_die);
22402 if (subr_die != old_die)
22403 /* Add the calling convention attribute if requested. */
22404 add_calling_convention_attribute (subr_die, decl);
22406 /* Output Dwarf info for all of the stuff within the body of the function
22407 (if it has one - it may be just a declaration).
22409 OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
22410 a function. This BLOCK actually represents the outermost binding contour
22411 for the function, i.e. the contour in which the function's formal
22412 parameters and labels get declared. Curiously, it appears that the front
22413 end doesn't actually put the PARM_DECL nodes for the current function onto
22414 the BLOCK_VARS list for this outer scope, but are strung off of the
22415 DECL_ARGUMENTS list for the function instead.
22417 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
22418 the LABEL_DECL nodes for the function however, and we output DWARF info
22419 for those in decls_for_scope. Just within the `outer_scope' there will be
22420 a BLOCK node representing the function's outermost pair of curly braces,
22421 and any blocks used for the base and member initializers of a C++
22422 constructor function. */
22423 tree outer_scope = DECL_INITIAL (decl);
22424 if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
22426 int call_site_note_count = 0;
22427 int tail_call_site_note_count = 0;
22429 /* Emit a DW_TAG_variable DIE for a named return value. */
22430 if (DECL_NAME (DECL_RESULT (decl)))
22431 gen_decl_die (DECL_RESULT (decl), NULL, NULL, subr_die);
22433 /* The first time through decls_for_scope we will generate the
22434 DIEs for the locals. The second time, we fill in the
22435 location info. */
22436 decls_for_scope (outer_scope, subr_die);
22438 if (call_arg_locations && (!dwarf_strict || dwarf_version >= 5))
22440 struct call_arg_loc_node *ca_loc;
22441 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
22443 dw_die_ref die = NULL;
22444 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
22445 rtx arg, next_arg;
22447 for (arg = (ca_loc->call_arg_loc_note != NULL_RTX
22448 ? NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note)
22449 : NULL_RTX);
22450 arg; arg = next_arg)
22452 dw_loc_descr_ref reg, val;
22453 machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
22454 dw_die_ref cdie, tdie = NULL;
22456 next_arg = XEXP (arg, 1);
22457 if (REG_P (XEXP (XEXP (arg, 0), 0))
22458 && next_arg
22459 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
22460 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
22461 && REGNO (XEXP (XEXP (arg, 0), 0))
22462 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
22463 next_arg = XEXP (next_arg, 1);
22464 if (mode == VOIDmode)
22466 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
22467 if (mode == VOIDmode)
22468 mode = GET_MODE (XEXP (arg, 0));
22470 if (mode == VOIDmode || mode == BLKmode)
22471 continue;
22472 /* Get dynamic information about call target only if we
22473 have no static information: we cannot generate both
22474 DW_AT_call_origin and DW_AT_call_target
22475 attributes. */
22476 if (ca_loc->symbol_ref == NULL_RTX)
22478 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
22480 tloc = XEXP (XEXP (arg, 0), 1);
22481 continue;
22483 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
22484 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
22486 tlocc = XEXP (XEXP (arg, 0), 1);
22487 continue;
22490 reg = NULL;
22491 if (REG_P (XEXP (XEXP (arg, 0), 0)))
22492 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
22493 VAR_INIT_STATUS_INITIALIZED);
22494 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
22496 rtx mem = XEXP (XEXP (arg, 0), 0);
22497 reg = mem_loc_descriptor (XEXP (mem, 0),
22498 get_address_mode (mem),
22499 GET_MODE (mem),
22500 VAR_INIT_STATUS_INITIALIZED);
22502 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
22503 == DEBUG_PARAMETER_REF)
22505 tree tdecl
22506 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
22507 tdie = lookup_decl_die (tdecl);
22508 if (tdie == NULL)
22509 continue;
22511 else
22512 continue;
22513 if (reg == NULL
22514 && GET_CODE (XEXP (XEXP (arg, 0), 0))
22515 != DEBUG_PARAMETER_REF)
22516 continue;
22517 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
22518 VOIDmode,
22519 VAR_INIT_STATUS_INITIALIZED);
22520 if (val == NULL)
22521 continue;
22522 if (die == NULL)
22523 die = gen_call_site_die (decl, subr_die, ca_loc);
22524 cdie = new_die (dwarf_TAG (DW_TAG_call_site_parameter), die,
22525 NULL_TREE);
22526 if (reg != NULL)
22527 add_AT_loc (cdie, DW_AT_location, reg);
22528 else if (tdie != NULL)
22529 add_AT_die_ref (cdie, dwarf_AT (DW_AT_call_parameter),
22530 tdie);
22531 add_AT_loc (cdie, dwarf_AT (DW_AT_call_value), val);
22532 if (next_arg != XEXP (arg, 1))
22534 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
22535 if (mode == VOIDmode)
22536 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
22537 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
22538 0), 1),
22539 mode, VOIDmode,
22540 VAR_INIT_STATUS_INITIALIZED);
22541 if (val != NULL)
22542 add_AT_loc (cdie, dwarf_AT (DW_AT_call_data_value),
22543 val);
22546 if (die == NULL
22547 && (ca_loc->symbol_ref || tloc))
22548 die = gen_call_site_die (decl, subr_die, ca_loc);
22549 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
22551 dw_loc_descr_ref tval = NULL;
22553 if (tloc != NULL_RTX)
22554 tval = mem_loc_descriptor (tloc,
22555 GET_MODE (tloc) == VOIDmode
22556 ? Pmode : GET_MODE (tloc),
22557 VOIDmode,
22558 VAR_INIT_STATUS_INITIALIZED);
22559 if (tval)
22560 add_AT_loc (die, dwarf_AT (DW_AT_call_target), tval);
22561 else if (tlocc != NULL_RTX)
22563 tval = mem_loc_descriptor (tlocc,
22564 GET_MODE (tlocc) == VOIDmode
22565 ? Pmode : GET_MODE (tlocc),
22566 VOIDmode,
22567 VAR_INIT_STATUS_INITIALIZED);
22568 if (tval)
22569 add_AT_loc (die,
22570 dwarf_AT (DW_AT_call_target_clobbered),
22571 tval);
22574 if (die != NULL)
22576 call_site_note_count++;
22577 if (ca_loc->tail_call_p)
22578 tail_call_site_note_count++;
22582 call_arg_locations = NULL;
22583 call_arg_loc_last = NULL;
22584 if (tail_call_site_count >= 0
22585 && tail_call_site_count == tail_call_site_note_count
22586 && (!dwarf_strict || dwarf_version >= 5))
22588 if (call_site_count >= 0
22589 && call_site_count == call_site_note_count)
22590 add_AT_flag (subr_die, dwarf_AT (DW_AT_call_all_calls), 1);
22591 else
22592 add_AT_flag (subr_die, dwarf_AT (DW_AT_call_all_tail_calls), 1);
22594 call_site_count = -1;
22595 tail_call_site_count = -1;
22598 /* Mark used types after we have created DIEs for the functions scopes. */
22599 premark_used_types (DECL_STRUCT_FUNCTION (decl));
22602 /* Returns a hash value for X (which really is a die_struct). */
22604 hashval_t
22605 block_die_hasher::hash (die_struct *d)
22607 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
22610 /* Return nonzero if decl_id and die_parent of die_struct X is the same
22611 as decl_id and die_parent of die_struct Y. */
22613 bool
22614 block_die_hasher::equal (die_struct *x, die_struct *y)
22616 return x->decl_id == y->decl_id && x->die_parent == y->die_parent;
22619 /* Return TRUE if DECL, which may have been previously generated as
22620 OLD_DIE, is a candidate for a DW_AT_specification. DECLARATION is
22621 true if decl (or its origin) is either an extern declaration or a
22622 class/namespace scoped declaration.
22624 The declare_in_namespace support causes us to get two DIEs for one
22625 variable, both of which are declarations. We want to avoid
22626 considering one to be a specification, so we must test for
22627 DECLARATION and DW_AT_declaration. */
22628 static inline bool
22629 decl_will_get_specification_p (dw_die_ref old_die, tree decl, bool declaration)
22631 return (old_die && TREE_STATIC (decl) && !declaration
22632 && get_AT_flag (old_die, DW_AT_declaration) == 1);
22635 /* Return true if DECL is a local static. */
22637 static inline bool
22638 local_function_static (tree decl)
22640 gcc_assert (VAR_P (decl));
22641 return TREE_STATIC (decl)
22642 && DECL_CONTEXT (decl)
22643 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL;
22646 /* Generate a DIE to represent a declared data object.
22647 Either DECL or ORIGIN must be non-null. */
22649 static void
22650 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
22652 HOST_WIDE_INT off = 0;
22653 tree com_decl;
22654 tree decl_or_origin = decl ? decl : origin;
22655 tree ultimate_origin;
22656 dw_die_ref var_die;
22657 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
22658 bool declaration = (DECL_EXTERNAL (decl_or_origin)
22659 || class_or_namespace_scope_p (context_die));
22660 bool specialization_p = false;
22661 bool no_linkage_name = false;
22663 /* While C++ inline static data members have definitions inside of the
22664 class, force the first DIE to be a declaration, then let gen_member_die
22665 reparent it to the class context and call gen_variable_die again
22666 to create the outside of the class DIE for the definition. */
22667 if (!declaration
22668 && old_die == NULL
22669 && decl
22670 && DECL_CONTEXT (decl)
22671 && TYPE_P (DECL_CONTEXT (decl))
22672 && lang_hooks.decls.decl_dwarf_attribute (decl, DW_AT_inline) != -1)
22674 declaration = true;
22675 if (dwarf_version < 5)
22676 no_linkage_name = true;
22679 ultimate_origin = decl_ultimate_origin (decl_or_origin);
22680 if (decl || ultimate_origin)
22681 origin = ultimate_origin;
22682 com_decl = fortran_common (decl_or_origin, &off);
22684 /* Symbol in common gets emitted as a child of the common block, in the form
22685 of a data member. */
22686 if (com_decl)
22688 dw_die_ref com_die;
22689 dw_loc_list_ref loc = NULL;
22690 die_node com_die_arg;
22692 var_die = lookup_decl_die (decl_or_origin);
22693 if (var_die)
22695 if (! early_dwarf && get_AT (var_die, DW_AT_location) == NULL)
22697 loc = loc_list_from_tree (com_decl, off ? 1 : 2, NULL);
22698 if (loc)
22700 if (off)
22702 /* Optimize the common case. */
22703 if (single_element_loc_list_p (loc)
22704 && loc->expr->dw_loc_opc == DW_OP_addr
22705 && loc->expr->dw_loc_next == NULL
22706 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
22707 == SYMBOL_REF)
22709 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
22710 loc->expr->dw_loc_oprnd1.v.val_addr
22711 = plus_constant (GET_MODE (x), x , off);
22713 else
22714 loc_list_plus_const (loc, off);
22716 add_AT_location_description (var_die, DW_AT_location, loc);
22717 remove_AT (var_die, DW_AT_declaration);
22720 return;
22723 if (common_block_die_table == NULL)
22724 common_block_die_table = hash_table<block_die_hasher>::create_ggc (10);
22726 com_die_arg.decl_id = DECL_UID (com_decl);
22727 com_die_arg.die_parent = context_die;
22728 com_die = common_block_die_table->find (&com_die_arg);
22729 if (! early_dwarf)
22730 loc = loc_list_from_tree (com_decl, 2, NULL);
22731 if (com_die == NULL)
22733 const char *cnam
22734 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
22735 die_node **slot;
22737 com_die = new_die (DW_TAG_common_block, context_die, decl);
22738 add_name_and_src_coords_attributes (com_die, com_decl);
22739 if (loc)
22741 add_AT_location_description (com_die, DW_AT_location, loc);
22742 /* Avoid sharing the same loc descriptor between
22743 DW_TAG_common_block and DW_TAG_variable. */
22744 loc = loc_list_from_tree (com_decl, 2, NULL);
22746 else if (DECL_EXTERNAL (decl_or_origin))
22747 add_AT_flag (com_die, DW_AT_declaration, 1);
22748 if (want_pubnames ())
22749 add_pubname_string (cnam, com_die); /* ??? needed? */
22750 com_die->decl_id = DECL_UID (com_decl);
22751 slot = common_block_die_table->find_slot (com_die, INSERT);
22752 *slot = com_die;
22754 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
22756 add_AT_location_description (com_die, DW_AT_location, loc);
22757 loc = loc_list_from_tree (com_decl, 2, NULL);
22758 remove_AT (com_die, DW_AT_declaration);
22760 var_die = new_die (DW_TAG_variable, com_die, decl);
22761 add_name_and_src_coords_attributes (var_die, decl_or_origin);
22762 add_type_attribute (var_die, TREE_TYPE (decl_or_origin),
22763 decl_quals (decl_or_origin), false,
22764 context_die);
22765 add_alignment_attribute (var_die, decl);
22766 add_AT_flag (var_die, DW_AT_external, 1);
22767 if (loc)
22769 if (off)
22771 /* Optimize the common case. */
22772 if (single_element_loc_list_p (loc)
22773 && loc->expr->dw_loc_opc == DW_OP_addr
22774 && loc->expr->dw_loc_next == NULL
22775 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
22777 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
22778 loc->expr->dw_loc_oprnd1.v.val_addr
22779 = plus_constant (GET_MODE (x), x, off);
22781 else
22782 loc_list_plus_const (loc, off);
22784 add_AT_location_description (var_die, DW_AT_location, loc);
22786 else if (DECL_EXTERNAL (decl_or_origin))
22787 add_AT_flag (var_die, DW_AT_declaration, 1);
22788 if (decl)
22789 equate_decl_number_to_die (decl, var_die);
22790 return;
22793 if (old_die)
22795 if (declaration)
22797 /* A declaration that has been previously dumped, needs no
22798 further annotations, since it doesn't need location on
22799 the second pass. */
22800 return;
22802 else if (decl_will_get_specification_p (old_die, decl, declaration)
22803 && !get_AT (old_die, DW_AT_specification))
22805 /* Fall-thru so we can make a new variable die along with a
22806 DW_AT_specification. */
22808 else if (origin && old_die->die_parent != context_die)
22810 /* If we will be creating an inlined instance, we need a
22811 new DIE that will get annotated with
22812 DW_AT_abstract_origin. Clear things so we can get a
22813 new DIE. */
22814 gcc_assert (!DECL_ABSTRACT_P (decl));
22815 old_die = NULL;
22817 else
22819 /* If a DIE was dumped early, it still needs location info.
22820 Skip to where we fill the location bits. */
22821 var_die = old_die;
22822 goto gen_variable_die_location;
22826 /* For static data members, the declaration in the class is supposed
22827 to have DW_TAG_member tag in DWARF{3,4} and we emit it for compatibility
22828 also in DWARF2; the specification should still be DW_TAG_variable
22829 referencing the DW_TAG_member DIE. */
22830 if (declaration && class_scope_p (context_die) && dwarf_version < 5)
22831 var_die = new_die (DW_TAG_member, context_die, decl);
22832 else
22833 var_die = new_die (DW_TAG_variable, context_die, decl);
22835 if (origin != NULL)
22836 add_abstract_origin_attribute (var_die, origin);
22838 /* Loop unrolling can create multiple blocks that refer to the same
22839 static variable, so we must test for the DW_AT_declaration flag.
22841 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
22842 copy decls and set the DECL_ABSTRACT_P flag on them instead of
22843 sharing them.
22845 ??? Duplicated blocks have been rewritten to use .debug_ranges. */
22846 else if (decl_will_get_specification_p (old_die, decl, declaration))
22848 /* This is a definition of a C++ class level static. */
22849 add_AT_specification (var_die, old_die);
22850 specialization_p = true;
22851 if (DECL_NAME (decl))
22853 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
22854 struct dwarf_file_data * file_index = lookup_filename (s.file);
22856 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
22857 add_AT_file (var_die, DW_AT_decl_file, file_index);
22859 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
22860 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
22862 if (debug_column_info
22863 && s.column
22864 && (get_AT_unsigned (old_die, DW_AT_decl_column)
22865 != (unsigned) s.column))
22866 add_AT_unsigned (var_die, DW_AT_decl_column, s.column);
22868 if (old_die->die_tag == DW_TAG_member)
22869 add_linkage_name (var_die, decl);
22872 else
22873 add_name_and_src_coords_attributes (var_die, decl, no_linkage_name);
22875 if ((origin == NULL && !specialization_p)
22876 || (origin != NULL
22877 && !DECL_ABSTRACT_P (decl_or_origin)
22878 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
22879 decl_function_context
22880 (decl_or_origin))))
22882 tree type = TREE_TYPE (decl_or_origin);
22884 if (decl_by_reference_p (decl_or_origin))
22885 add_type_attribute (var_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
22886 context_die);
22887 else
22888 add_type_attribute (var_die, type, decl_quals (decl_or_origin), false,
22889 context_die);
22892 if (origin == NULL && !specialization_p)
22894 if (TREE_PUBLIC (decl))
22895 add_AT_flag (var_die, DW_AT_external, 1);
22897 if (DECL_ARTIFICIAL (decl))
22898 add_AT_flag (var_die, DW_AT_artificial, 1);
22900 add_alignment_attribute (var_die, decl);
22902 add_accessibility_attribute (var_die, decl);
22905 if (declaration)
22906 add_AT_flag (var_die, DW_AT_declaration, 1);
22908 if (decl && (DECL_ABSTRACT_P (decl)
22909 || !old_die || is_declaration_die (old_die)))
22910 equate_decl_number_to_die (decl, var_die);
22912 gen_variable_die_location:
22913 if (! declaration
22914 && (! DECL_ABSTRACT_P (decl_or_origin)
22915 /* Local static vars are shared between all clones/inlines,
22916 so emit DW_AT_location on the abstract DIE if DECL_RTL is
22917 already set. */
22918 || (VAR_P (decl_or_origin)
22919 && TREE_STATIC (decl_or_origin)
22920 && DECL_RTL_SET_P (decl_or_origin))))
22922 if (early_dwarf)
22923 add_pubname (decl_or_origin, var_die);
22924 else
22925 add_location_or_const_value_attribute (var_die, decl_or_origin,
22926 decl == NULL);
22928 else
22929 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
22931 if ((dwarf_version >= 4 || !dwarf_strict)
22932 && lang_hooks.decls.decl_dwarf_attribute (decl_or_origin,
22933 DW_AT_const_expr) == 1
22934 && !get_AT (var_die, DW_AT_const_expr)
22935 && !specialization_p)
22936 add_AT_flag (var_die, DW_AT_const_expr, 1);
22938 if (!dwarf_strict)
22940 int inl = lang_hooks.decls.decl_dwarf_attribute (decl_or_origin,
22941 DW_AT_inline);
22942 if (inl != -1
22943 && !get_AT (var_die, DW_AT_inline)
22944 && !specialization_p)
22945 add_AT_unsigned (var_die, DW_AT_inline, inl);
22949 /* Generate a DIE to represent a named constant. */
22951 static void
22952 gen_const_die (tree decl, dw_die_ref context_die)
22954 dw_die_ref const_die;
22955 tree type = TREE_TYPE (decl);
22957 const_die = lookup_decl_die (decl);
22958 if (const_die)
22959 return;
22961 const_die = new_die (DW_TAG_constant, context_die, decl);
22962 equate_decl_number_to_die (decl, const_die);
22963 add_name_and_src_coords_attributes (const_die, decl);
22964 add_type_attribute (const_die, type, TYPE_QUAL_CONST, false, context_die);
22965 if (TREE_PUBLIC (decl))
22966 add_AT_flag (const_die, DW_AT_external, 1);
22967 if (DECL_ARTIFICIAL (decl))
22968 add_AT_flag (const_die, DW_AT_artificial, 1);
22969 tree_add_const_value_attribute_for_decl (const_die, decl);
22972 /* Generate a DIE to represent a label identifier. */
22974 static void
22975 gen_label_die (tree decl, dw_die_ref context_die)
22977 tree origin = decl_ultimate_origin (decl);
22978 dw_die_ref lbl_die = lookup_decl_die (decl);
22979 rtx insn;
22980 char label[MAX_ARTIFICIAL_LABEL_BYTES];
22982 if (!lbl_die)
22984 lbl_die = new_die (DW_TAG_label, context_die, decl);
22985 equate_decl_number_to_die (decl, lbl_die);
22987 if (origin != NULL)
22988 add_abstract_origin_attribute (lbl_die, origin);
22989 else
22990 add_name_and_src_coords_attributes (lbl_die, decl);
22993 if (DECL_ABSTRACT_P (decl))
22994 equate_decl_number_to_die (decl, lbl_die);
22995 else if (! early_dwarf)
22997 insn = DECL_RTL_IF_SET (decl);
22999 /* Deleted labels are programmer specified labels which have been
23000 eliminated because of various optimizations. We still emit them
23001 here so that it is possible to put breakpoints on them. */
23002 if (insn
23003 && (LABEL_P (insn)
23004 || ((NOTE_P (insn)
23005 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
23007 /* When optimization is enabled (via -O) some parts of the compiler
23008 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
23009 represent source-level labels which were explicitly declared by
23010 the user. This really shouldn't be happening though, so catch
23011 it if it ever does happen. */
23012 gcc_assert (!as_a<rtx_insn *> (insn)->deleted ());
23014 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
23015 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
23017 else if (insn
23018 && NOTE_P (insn)
23019 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
23020 && CODE_LABEL_NUMBER (insn) != -1)
23022 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
23023 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
23028 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
23029 attributes to the DIE for a block STMT, to describe where the inlined
23030 function was called from. This is similar to add_src_coords_attributes. */
23032 static inline void
23033 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
23035 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
23037 if (dwarf_version >= 3 || !dwarf_strict)
23039 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
23040 add_AT_unsigned (die, DW_AT_call_line, s.line);
23041 if (debug_column_info && s.column)
23042 add_AT_unsigned (die, DW_AT_call_column, s.column);
23047 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
23048 Add low_pc and high_pc attributes to the DIE for a block STMT. */
23050 static inline void
23051 add_high_low_attributes (tree stmt, dw_die_ref die)
23053 char label[MAX_ARTIFICIAL_LABEL_BYTES];
23055 if (BLOCK_FRAGMENT_CHAIN (stmt)
23056 && (dwarf_version >= 3 || !dwarf_strict))
23058 tree chain, superblock = NULL_TREE;
23059 dw_die_ref pdie;
23060 dw_attr_node *attr = NULL;
23062 if (inlined_function_outer_scope_p (stmt))
23064 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
23065 BLOCK_NUMBER (stmt));
23066 add_AT_lbl_id (die, DW_AT_entry_pc, label);
23069 /* Optimize duplicate .debug_ranges lists or even tails of
23070 lists. If this BLOCK has same ranges as its supercontext,
23071 lookup DW_AT_ranges attribute in the supercontext (and
23072 recursively so), verify that the ranges_table contains the
23073 right values and use it instead of adding a new .debug_range. */
23074 for (chain = stmt, pdie = die;
23075 BLOCK_SAME_RANGE (chain);
23076 chain = BLOCK_SUPERCONTEXT (chain))
23078 dw_attr_node *new_attr;
23080 pdie = pdie->die_parent;
23081 if (pdie == NULL)
23082 break;
23083 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
23084 break;
23085 new_attr = get_AT (pdie, DW_AT_ranges);
23086 if (new_attr == NULL
23087 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
23088 break;
23089 attr = new_attr;
23090 superblock = BLOCK_SUPERCONTEXT (chain);
23092 if (attr != NULL
23093 && ((*ranges_table)[attr->dw_attr_val.v.val_offset].num
23094 == BLOCK_NUMBER (superblock))
23095 && BLOCK_FRAGMENT_CHAIN (superblock))
23097 unsigned long off = attr->dw_attr_val.v.val_offset;
23098 unsigned long supercnt = 0, thiscnt = 0;
23099 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
23100 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
23102 ++supercnt;
23103 gcc_checking_assert ((*ranges_table)[off + supercnt].num
23104 == BLOCK_NUMBER (chain));
23106 gcc_checking_assert ((*ranges_table)[off + supercnt + 1].num == 0);
23107 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
23108 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
23109 ++thiscnt;
23110 gcc_assert (supercnt >= thiscnt);
23111 add_AT_range_list (die, DW_AT_ranges, off + supercnt - thiscnt,
23112 false);
23113 note_rnglist_head (off + supercnt - thiscnt);
23114 return;
23117 unsigned int offset = add_ranges (stmt, true);
23118 add_AT_range_list (die, DW_AT_ranges, offset, false);
23119 note_rnglist_head (offset);
23121 bool prev_in_cold = BLOCK_IN_COLD_SECTION_P (stmt);
23122 chain = BLOCK_FRAGMENT_CHAIN (stmt);
23125 add_ranges (chain, prev_in_cold != BLOCK_IN_COLD_SECTION_P (chain));
23126 prev_in_cold = BLOCK_IN_COLD_SECTION_P (chain);
23127 chain = BLOCK_FRAGMENT_CHAIN (chain);
23129 while (chain);
23130 add_ranges (NULL);
23132 else
23134 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
23135 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
23136 BLOCK_NUMBER (stmt));
23137 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
23138 BLOCK_NUMBER (stmt));
23139 add_AT_low_high_pc (die, label, label_high, false);
23143 /* Generate a DIE for a lexical block. */
23145 static void
23146 gen_lexical_block_die (tree stmt, dw_die_ref context_die)
23148 dw_die_ref old_die = BLOCK_DIE (stmt);
23149 dw_die_ref stmt_die = NULL;
23150 if (!old_die)
23152 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
23153 BLOCK_DIE (stmt) = stmt_die;
23156 if (BLOCK_ABSTRACT (stmt))
23158 if (old_die)
23160 /* This must have been generated early and it won't even
23161 need location information since it's a DW_AT_inline
23162 function. */
23163 if (flag_checking)
23164 for (dw_die_ref c = context_die; c; c = c->die_parent)
23165 if (c->die_tag == DW_TAG_inlined_subroutine
23166 || c->die_tag == DW_TAG_subprogram)
23168 gcc_assert (get_AT (c, DW_AT_inline));
23169 break;
23171 return;
23174 else if (BLOCK_ABSTRACT_ORIGIN (stmt))
23176 /* If this is an inlined instance, create a new lexical die for
23177 anything below to attach DW_AT_abstract_origin to. */
23178 if (old_die)
23180 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
23181 BLOCK_DIE (stmt) = stmt_die;
23182 old_die = NULL;
23185 tree origin = block_ultimate_origin (stmt);
23186 if (origin != NULL_TREE && origin != stmt)
23187 add_abstract_origin_attribute (stmt_die, origin);
23190 if (old_die)
23191 stmt_die = old_die;
23193 /* A non abstract block whose blocks have already been reordered
23194 should have the instruction range for this block. If so, set the
23195 high/low attributes. */
23196 if (!early_dwarf && !BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
23198 gcc_assert (stmt_die);
23199 add_high_low_attributes (stmt, stmt_die);
23202 decls_for_scope (stmt, stmt_die);
23205 /* Generate a DIE for an inlined subprogram. */
23207 static void
23208 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die)
23210 tree decl;
23212 /* The instance of function that is effectively being inlined shall not
23213 be abstract. */
23214 gcc_assert (! BLOCK_ABSTRACT (stmt));
23216 decl = block_ultimate_origin (stmt);
23218 /* Make sure any inlined functions are known to be inlineable. */
23219 gcc_checking_assert (DECL_ABSTRACT_P (decl)
23220 || cgraph_function_possibly_inlined_p (decl));
23222 /* Emit info for the abstract instance first, if we haven't yet. We
23223 must emit this even if the block is abstract, otherwise when we
23224 emit the block below (or elsewhere), we may end up trying to emit
23225 a die whose origin die hasn't been emitted, and crashing. */
23226 dwarf2out_abstract_function (decl);
23228 if (! BLOCK_ABSTRACT (stmt))
23230 dw_die_ref subr_die
23231 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
23233 if (call_arg_locations)
23234 BLOCK_DIE (stmt) = subr_die;
23235 add_abstract_origin_attribute (subr_die, decl);
23236 if (TREE_ASM_WRITTEN (stmt))
23237 add_high_low_attributes (stmt, subr_die);
23238 add_call_src_coords_attributes (stmt, subr_die);
23240 decls_for_scope (stmt, subr_die);
23244 /* Generate a DIE for a field in a record, or structure. CTX is required: see
23245 the comment for VLR_CONTEXT. */
23247 static void
23248 gen_field_die (tree decl, struct vlr_context *ctx, dw_die_ref context_die)
23250 dw_die_ref decl_die;
23252 if (TREE_TYPE (decl) == error_mark_node)
23253 return;
23255 decl_die = new_die (DW_TAG_member, context_die, decl);
23256 add_name_and_src_coords_attributes (decl_die, decl);
23257 add_type_attribute (decl_die, member_declared_type (decl), decl_quals (decl),
23258 TYPE_REVERSE_STORAGE_ORDER (DECL_FIELD_CONTEXT (decl)),
23259 context_die);
23261 if (DECL_BIT_FIELD_TYPE (decl))
23263 add_byte_size_attribute (decl_die, decl);
23264 add_bit_size_attribute (decl_die, decl);
23265 add_bit_offset_attribute (decl_die, decl, ctx);
23268 add_alignment_attribute (decl_die, decl);
23270 /* If we have a variant part offset, then we are supposed to process a member
23271 of a QUAL_UNION_TYPE, which is how we represent variant parts in
23272 trees. */
23273 gcc_assert (ctx->variant_part_offset == NULL_TREE
23274 || TREE_CODE (DECL_FIELD_CONTEXT (decl)) != QUAL_UNION_TYPE);
23275 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
23276 add_data_member_location_attribute (decl_die, decl, ctx);
23278 if (DECL_ARTIFICIAL (decl))
23279 add_AT_flag (decl_die, DW_AT_artificial, 1);
23281 add_accessibility_attribute (decl_die, decl);
23283 /* Equate decl number to die, so that we can look up this decl later on. */
23284 equate_decl_number_to_die (decl, decl_die);
23287 #if 0
23288 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
23289 Use modified_type_die instead.
23290 We keep this code here just in case these types of DIEs may be needed to
23291 represent certain things in other languages (e.g. Pascal) someday. */
23293 static void
23294 gen_pointer_type_die (tree type, dw_die_ref context_die)
23296 dw_die_ref ptr_die
23297 = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
23299 equate_type_number_to_die (type, ptr_die);
23300 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
23301 context_die);
23302 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
23305 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
23306 Use modified_type_die instead.
23307 We keep this code here just in case these types of DIEs may be needed to
23308 represent certain things in other languages (e.g. Pascal) someday. */
23310 static void
23311 gen_reference_type_die (tree type, dw_die_ref context_die)
23313 dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
23315 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
23316 ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
23317 else
23318 ref_die = new_die (DW_TAG_reference_type, scope_die, type);
23320 equate_type_number_to_die (type, ref_die);
23321 add_type_attribute (ref_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
23322 context_die);
23323 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
23325 #endif
23327 /* Generate a DIE for a pointer to a member type. TYPE can be an
23328 OFFSET_TYPE, for a pointer to data member, or a RECORD_TYPE, for a
23329 pointer to member function. */
23331 static void
23332 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
23334 if (lookup_type_die (type))
23335 return;
23337 dw_die_ref ptr_die = new_die (DW_TAG_ptr_to_member_type,
23338 scope_die_for (type, context_die), type);
23340 equate_type_number_to_die (type, ptr_die);
23341 add_AT_die_ref (ptr_die, DW_AT_containing_type,
23342 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
23343 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
23344 context_die);
23345 add_alignment_attribute (ptr_die, type);
23347 if (TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE
23348 && TREE_CODE (TREE_TYPE (type)) != METHOD_TYPE)
23350 dw_loc_descr_ref op = new_loc_descr (DW_OP_plus, 0, 0);
23351 add_AT_loc (ptr_die, DW_AT_use_location, op);
23355 static char *producer_string;
23357 /* Return a heap allocated producer string including command line options
23358 if -grecord-gcc-switches. */
23360 static char *
23361 gen_producer_string (void)
23363 size_t j;
23364 auto_vec<const char *> switches;
23365 const char *language_string = lang_hooks.name;
23366 char *producer, *tail;
23367 const char *p;
23368 size_t len = dwarf_record_gcc_switches ? 0 : 3;
23369 size_t plen = strlen (language_string) + 1 + strlen (version_string);
23371 for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
23372 switch (save_decoded_options[j].opt_index)
23374 case OPT_o:
23375 case OPT_d:
23376 case OPT_dumpbase:
23377 case OPT_dumpdir:
23378 case OPT_auxbase:
23379 case OPT_auxbase_strip:
23380 case OPT_quiet:
23381 case OPT_version:
23382 case OPT_v:
23383 case OPT_w:
23384 case OPT_L:
23385 case OPT_D:
23386 case OPT_I:
23387 case OPT_U:
23388 case OPT_SPECIAL_unknown:
23389 case OPT_SPECIAL_ignore:
23390 case OPT_SPECIAL_program_name:
23391 case OPT_SPECIAL_input_file:
23392 case OPT_grecord_gcc_switches:
23393 case OPT_gno_record_gcc_switches:
23394 case OPT__output_pch_:
23395 case OPT_fdiagnostics_show_location_:
23396 case OPT_fdiagnostics_show_option:
23397 case OPT_fdiagnostics_show_caret:
23398 case OPT_fdiagnostics_color_:
23399 case OPT_fverbose_asm:
23400 case OPT____:
23401 case OPT__sysroot_:
23402 case OPT_nostdinc:
23403 case OPT_nostdinc__:
23404 case OPT_fpreprocessed:
23405 case OPT_fltrans_output_list_:
23406 case OPT_fresolution_:
23407 case OPT_fdebug_prefix_map_:
23408 /* Ignore these. */
23409 continue;
23410 default:
23411 if (cl_options[save_decoded_options[j].opt_index].flags
23412 & CL_NO_DWARF_RECORD)
23413 continue;
23414 gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
23415 == '-');
23416 switch (save_decoded_options[j].canonical_option[0][1])
23418 case 'M':
23419 case 'i':
23420 case 'W':
23421 continue;
23422 case 'f':
23423 if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
23424 "dump", 4) == 0)
23425 continue;
23426 break;
23427 default:
23428 break;
23430 switches.safe_push (save_decoded_options[j].orig_option_with_args_text);
23431 len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
23432 break;
23435 producer = XNEWVEC (char, plen + 1 + len + 1);
23436 tail = producer;
23437 sprintf (tail, "%s %s", language_string, version_string);
23438 tail += plen;
23440 FOR_EACH_VEC_ELT (switches, j, p)
23442 len = strlen (p);
23443 *tail = ' ';
23444 memcpy (tail + 1, p, len);
23445 tail += len + 1;
23448 *tail = '\0';
23449 return producer;
23452 /* Given a C and/or C++ language/version string return the "highest".
23453 C++ is assumed to be "higher" than C in this case. Used for merging
23454 LTO translation unit languages. */
23455 static const char *
23456 highest_c_language (const char *lang1, const char *lang2)
23458 if (strcmp ("GNU C++14", lang1) == 0 || strcmp ("GNU C++14", lang2) == 0)
23459 return "GNU C++14";
23460 if (strcmp ("GNU C++11", lang1) == 0 || strcmp ("GNU C++11", lang2) == 0)
23461 return "GNU C++11";
23462 if (strcmp ("GNU C++98", lang1) == 0 || strcmp ("GNU C++98", lang2) == 0)
23463 return "GNU C++98";
23465 if (strcmp ("GNU C11", lang1) == 0 || strcmp ("GNU C11", lang2) == 0)
23466 return "GNU C11";
23467 if (strcmp ("GNU C99", lang1) == 0 || strcmp ("GNU C99", lang2) == 0)
23468 return "GNU C99";
23469 if (strcmp ("GNU C89", lang1) == 0 || strcmp ("GNU C89", lang2) == 0)
23470 return "GNU C89";
23472 gcc_unreachable ();
23476 /* Generate the DIE for the compilation unit. */
23478 static dw_die_ref
23479 gen_compile_unit_die (const char *filename)
23481 dw_die_ref die;
23482 const char *language_string = lang_hooks.name;
23483 int language;
23485 die = new_die (DW_TAG_compile_unit, NULL, NULL);
23487 if (filename)
23489 add_name_attribute (die, filename);
23490 /* Don't add cwd for <built-in>. */
23491 if (filename[0] != '<')
23492 add_comp_dir_attribute (die);
23495 add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
23497 /* If our producer is LTO try to figure out a common language to use
23498 from the global list of translation units. */
23499 if (strcmp (language_string, "GNU GIMPLE") == 0)
23501 unsigned i;
23502 tree t;
23503 const char *common_lang = NULL;
23505 FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
23507 if (!TRANSLATION_UNIT_LANGUAGE (t))
23508 continue;
23509 if (!common_lang)
23510 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
23511 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
23513 else if (strncmp (common_lang, "GNU C", 5) == 0
23514 && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
23515 /* Mixing C and C++ is ok, use C++ in that case. */
23516 common_lang = highest_c_language (common_lang,
23517 TRANSLATION_UNIT_LANGUAGE (t));
23518 else
23520 /* Fall back to C. */
23521 common_lang = NULL;
23522 break;
23526 if (common_lang)
23527 language_string = common_lang;
23530 language = DW_LANG_C;
23531 if (strncmp (language_string, "GNU C", 5) == 0
23532 && ISDIGIT (language_string[5]))
23534 language = DW_LANG_C89;
23535 if (dwarf_version >= 3 || !dwarf_strict)
23537 if (strcmp (language_string, "GNU C89") != 0)
23538 language = DW_LANG_C99;
23540 if (dwarf_version >= 5 /* || !dwarf_strict */)
23541 if (strcmp (language_string, "GNU C11") == 0)
23542 language = DW_LANG_C11;
23545 else if (strncmp (language_string, "GNU C++", 7) == 0)
23547 language = DW_LANG_C_plus_plus;
23548 if (dwarf_version >= 5 /* || !dwarf_strict */)
23550 if (strcmp (language_string, "GNU C++11") == 0)
23551 language = DW_LANG_C_plus_plus_11;
23552 else if (strcmp (language_string, "GNU C++14") == 0)
23553 language = DW_LANG_C_plus_plus_14;
23556 else if (strcmp (language_string, "GNU F77") == 0)
23557 language = DW_LANG_Fortran77;
23558 else if (strcmp (language_string, "GNU Pascal") == 0)
23559 language = DW_LANG_Pascal83;
23560 else if (dwarf_version >= 3 || !dwarf_strict)
23562 if (strcmp (language_string, "GNU Ada") == 0)
23563 language = DW_LANG_Ada95;
23564 else if (strncmp (language_string, "GNU Fortran", 11) == 0)
23566 language = DW_LANG_Fortran95;
23567 if (dwarf_version >= 5 /* || !dwarf_strict */)
23569 if (strcmp (language_string, "GNU Fortran2003") == 0)
23570 language = DW_LANG_Fortran03;
23571 else if (strcmp (language_string, "GNU Fortran2008") == 0)
23572 language = DW_LANG_Fortran08;
23575 else if (strcmp (language_string, "GNU Java") == 0)
23576 language = DW_LANG_Java;
23577 else if (strcmp (language_string, "GNU Objective-C") == 0)
23578 language = DW_LANG_ObjC;
23579 else if (strcmp (language_string, "GNU Objective-C++") == 0)
23580 language = DW_LANG_ObjC_plus_plus;
23581 else if (dwarf_version >= 5 || !dwarf_strict)
23583 if (strcmp (language_string, "GNU Go") == 0)
23584 language = DW_LANG_Go;
23587 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
23588 else if (strncmp (language_string, "GNU Fortran", 11) == 0)
23589 language = DW_LANG_Fortran90;
23591 add_AT_unsigned (die, DW_AT_language, language);
23593 switch (language)
23595 case DW_LANG_Fortran77:
23596 case DW_LANG_Fortran90:
23597 case DW_LANG_Fortran95:
23598 case DW_LANG_Fortran03:
23599 case DW_LANG_Fortran08:
23600 /* Fortran has case insensitive identifiers and the front-end
23601 lowercases everything. */
23602 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
23603 break;
23604 default:
23605 /* The default DW_ID_case_sensitive doesn't need to be specified. */
23606 break;
23608 return die;
23611 /* Generate the DIE for a base class. */
23613 static void
23614 gen_inheritance_die (tree binfo, tree access, tree type,
23615 dw_die_ref context_die)
23617 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
23618 struct vlr_context ctx = { type, NULL };
23620 add_type_attribute (die, BINFO_TYPE (binfo), TYPE_UNQUALIFIED, false,
23621 context_die);
23622 add_data_member_location_attribute (die, binfo, &ctx);
23624 if (BINFO_VIRTUAL_P (binfo))
23625 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
23627 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
23628 children, otherwise the default is DW_ACCESS_public. In DWARF2
23629 the default has always been DW_ACCESS_private. */
23630 if (access == access_public_node)
23632 if (dwarf_version == 2
23633 || context_die->die_tag == DW_TAG_class_type)
23634 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
23636 else if (access == access_protected_node)
23637 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
23638 else if (dwarf_version > 2
23639 && context_die->die_tag != DW_TAG_class_type)
23640 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
23643 /* Return whether DECL is a FIELD_DECL that represents the variant part of a
23644 structure. */
23645 static bool
23646 is_variant_part (tree decl)
23648 return (TREE_CODE (decl) == FIELD_DECL
23649 && TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE);
23652 /* Check that OPERAND is a reference to a field in STRUCT_TYPE. If it is,
23653 return the FIELD_DECL. Return NULL_TREE otherwise. */
23655 static tree
23656 analyze_discr_in_predicate (tree operand, tree struct_type)
23658 bool continue_stripping = true;
23659 while (continue_stripping)
23660 switch (TREE_CODE (operand))
23662 CASE_CONVERT:
23663 operand = TREE_OPERAND (operand, 0);
23664 break;
23665 default:
23666 continue_stripping = false;
23667 break;
23670 /* Match field access to members of struct_type only. */
23671 if (TREE_CODE (operand) == COMPONENT_REF
23672 && TREE_CODE (TREE_OPERAND (operand, 0)) == PLACEHOLDER_EXPR
23673 && TREE_TYPE (TREE_OPERAND (operand, 0)) == struct_type
23674 && TREE_CODE (TREE_OPERAND (operand, 1)) == FIELD_DECL)
23675 return TREE_OPERAND (operand, 1);
23676 else
23677 return NULL_TREE;
23680 /* Check that SRC is a constant integer that can be represented as a native
23681 integer constant (either signed or unsigned). If so, store it into DEST and
23682 return true. Return false otherwise. */
23684 static bool
23685 get_discr_value (tree src, dw_discr_value *dest)
23687 bool is_unsigned = TYPE_UNSIGNED (TREE_TYPE (src));
23689 if (TREE_CODE (src) != INTEGER_CST
23690 || !(is_unsigned ? tree_fits_uhwi_p (src) : tree_fits_shwi_p (src)))
23691 return false;
23693 dest->pos = is_unsigned;
23694 if (is_unsigned)
23695 dest->v.uval = tree_to_uhwi (src);
23696 else
23697 dest->v.sval = tree_to_shwi (src);
23699 return true;
23702 /* Try to extract synthetic properties out of VARIANT_PART_DECL, which is a
23703 FIELD_DECL in STRUCT_TYPE that represents a variant part. If unsuccessful,
23704 store NULL_TREE in DISCR_DECL. Otherwise:
23706 - store the discriminant field in STRUCT_TYPE that controls the variant
23707 part to *DISCR_DECL
23709 - put in *DISCR_LISTS_P an array where for each variant, the item
23710 represents the corresponding matching list of discriminant values.
23712 - put in *DISCR_LISTS_LENGTH the number of variants, which is the size of
23713 the above array.
23715 Note that when the array is allocated (i.e. when the analysis is
23716 successful), it is up to the caller to free the array. */
23718 static void
23719 analyze_variants_discr (tree variant_part_decl,
23720 tree struct_type,
23721 tree *discr_decl,
23722 dw_discr_list_ref **discr_lists_p,
23723 unsigned *discr_lists_length)
23725 tree variant_part_type = TREE_TYPE (variant_part_decl);
23726 tree variant;
23727 dw_discr_list_ref *discr_lists;
23728 unsigned i;
23730 /* Compute how many variants there are in this variant part. */
23731 *discr_lists_length = 0;
23732 for (variant = TYPE_FIELDS (variant_part_type);
23733 variant != NULL_TREE;
23734 variant = DECL_CHAIN (variant))
23735 ++*discr_lists_length;
23737 *discr_decl = NULL_TREE;
23738 *discr_lists_p
23739 = (dw_discr_list_ref *) xcalloc (*discr_lists_length,
23740 sizeof (**discr_lists_p));
23741 discr_lists = *discr_lists_p;
23743 /* And then analyze all variants to extract discriminant information for all
23744 of them. This analysis is conservative: as soon as we detect something we
23745 do not support, abort everything and pretend we found nothing. */
23746 for (variant = TYPE_FIELDS (variant_part_type), i = 0;
23747 variant != NULL_TREE;
23748 variant = DECL_CHAIN (variant), ++i)
23750 tree match_expr = DECL_QUALIFIER (variant);
23752 /* Now, try to analyze the predicate and deduce a discriminant for
23753 it. */
23754 if (match_expr == boolean_true_node)
23755 /* Typically happens for the default variant: it matches all cases that
23756 previous variants rejected. Don't output any matching value for
23757 this one. */
23758 continue;
23760 /* The following loop tries to iterate over each discriminant
23761 possibility: single values or ranges. */
23762 while (match_expr != NULL_TREE)
23764 tree next_round_match_expr;
23765 tree candidate_discr = NULL_TREE;
23766 dw_discr_list_ref new_node = NULL;
23768 /* Possibilities are matched one after the other by nested
23769 TRUTH_ORIF_EXPR expressions. Process the current possibility and
23770 continue with the rest at next iteration. */
23771 if (TREE_CODE (match_expr) == TRUTH_ORIF_EXPR)
23773 next_round_match_expr = TREE_OPERAND (match_expr, 0);
23774 match_expr = TREE_OPERAND (match_expr, 1);
23776 else
23777 next_round_match_expr = NULL_TREE;
23779 if (match_expr == boolean_false_node)
23780 /* This sub-expression matches nothing: just wait for the next
23781 one. */
23784 else if (TREE_CODE (match_expr) == EQ_EXPR)
23786 /* We are matching: <discr_field> == <integer_cst>
23787 This sub-expression matches a single value. */
23788 tree integer_cst = TREE_OPERAND (match_expr, 1);
23790 candidate_discr
23791 = analyze_discr_in_predicate (TREE_OPERAND (match_expr, 0),
23792 struct_type);
23794 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
23795 if (!get_discr_value (integer_cst,
23796 &new_node->dw_discr_lower_bound))
23797 goto abort;
23798 new_node->dw_discr_range = false;
23801 else if (TREE_CODE (match_expr) == TRUTH_ANDIF_EXPR)
23803 /* We are matching:
23804 <discr_field> > <integer_cst>
23805 && <discr_field> < <integer_cst>.
23806 This sub-expression matches the range of values between the
23807 two matched integer constants. Note that comparisons can be
23808 inclusive or exclusive. */
23809 tree candidate_discr_1, candidate_discr_2;
23810 tree lower_cst, upper_cst;
23811 bool lower_cst_included, upper_cst_included;
23812 tree lower_op = TREE_OPERAND (match_expr, 0);
23813 tree upper_op = TREE_OPERAND (match_expr, 1);
23815 /* When the comparison is exclusive, the integer constant is not
23816 the discriminant range bound we are looking for: we will have
23817 to increment or decrement it. */
23818 if (TREE_CODE (lower_op) == GE_EXPR)
23819 lower_cst_included = true;
23820 else if (TREE_CODE (lower_op) == GT_EXPR)
23821 lower_cst_included = false;
23822 else
23823 goto abort;
23825 if (TREE_CODE (upper_op) == LE_EXPR)
23826 upper_cst_included = true;
23827 else if (TREE_CODE (upper_op) == LT_EXPR)
23828 upper_cst_included = false;
23829 else
23830 goto abort;
23832 /* Extract the discriminant from the first operand and check it
23833 is consistant with the same analysis in the second
23834 operand. */
23835 candidate_discr_1
23836 = analyze_discr_in_predicate (TREE_OPERAND (lower_op, 0),
23837 struct_type);
23838 candidate_discr_2
23839 = analyze_discr_in_predicate (TREE_OPERAND (upper_op, 0),
23840 struct_type);
23841 if (candidate_discr_1 == candidate_discr_2)
23842 candidate_discr = candidate_discr_1;
23843 else
23844 goto abort;
23846 /* Extract bounds from both. */
23847 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
23848 lower_cst = TREE_OPERAND (lower_op, 1);
23849 upper_cst = TREE_OPERAND (upper_op, 1);
23851 if (!lower_cst_included)
23852 lower_cst
23853 = fold_build2 (PLUS_EXPR, TREE_TYPE (lower_cst), lower_cst,
23854 build_int_cst (TREE_TYPE (lower_cst), 1));
23855 if (!upper_cst_included)
23856 upper_cst
23857 = fold_build2 (MINUS_EXPR, TREE_TYPE (upper_cst), upper_cst,
23858 build_int_cst (TREE_TYPE (upper_cst), 1));
23860 if (!get_discr_value (lower_cst,
23861 &new_node->dw_discr_lower_bound)
23862 || !get_discr_value (upper_cst,
23863 &new_node->dw_discr_upper_bound))
23864 goto abort;
23866 new_node->dw_discr_range = true;
23869 else
23870 /* Unsupported sub-expression: we cannot determine the set of
23871 matching discriminant values. Abort everything. */
23872 goto abort;
23874 /* If the discriminant info is not consistant with what we saw so
23875 far, consider the analysis failed and abort everything. */
23876 if (candidate_discr == NULL_TREE
23877 || (*discr_decl != NULL_TREE && candidate_discr != *discr_decl))
23878 goto abort;
23879 else
23880 *discr_decl = candidate_discr;
23882 if (new_node != NULL)
23884 new_node->dw_discr_next = discr_lists[i];
23885 discr_lists[i] = new_node;
23887 match_expr = next_round_match_expr;
23891 /* If we reach this point, we could match everything we were interested
23892 in. */
23893 return;
23895 abort:
23896 /* Clean all data structure and return no result. */
23897 free (*discr_lists_p);
23898 *discr_lists_p = NULL;
23899 *discr_decl = NULL_TREE;
23902 /* Generate a DIE to represent VARIANT_PART_DECL, a variant part that is part
23903 of STRUCT_TYPE, a record type. This new DIE is emitted as the next child
23904 under CONTEXT_DIE.
23906 Variant parts are supposed to be implemented as a FIELD_DECL whose type is a
23907 QUAL_UNION_TYPE: this is the VARIANT_PART_DECL parameter. The members for
23908 this type, which are record types, represent the available variants and each
23909 has a DECL_QUALIFIER attribute. The discriminant and the discriminant
23910 values are inferred from these attributes.
23912 In trees, the offsets for the fields inside these sub-records are relative
23913 to the variant part itself, whereas the corresponding DIEs should have
23914 offset attributes that are relative to the embedding record base address.
23915 This is why the caller must provide a VARIANT_PART_OFFSET expression: it
23916 must be an expression that computes the offset of the variant part to
23917 describe in DWARF. */
23919 static void
23920 gen_variant_part (tree variant_part_decl, struct vlr_context *vlr_ctx,
23921 dw_die_ref context_die)
23923 const tree variant_part_type = TREE_TYPE (variant_part_decl);
23924 tree variant_part_offset = vlr_ctx->variant_part_offset;
23925 struct loc_descr_context ctx = {
23926 vlr_ctx->struct_type, /* context_type */
23927 NULL_TREE, /* base_decl */
23928 NULL, /* dpi */
23929 false, /* placeholder_arg */
23930 false /* placeholder_seen */
23933 /* The FIELD_DECL node in STRUCT_TYPE that acts as the discriminant, or
23934 NULL_TREE if there is no such field. */
23935 tree discr_decl = NULL_TREE;
23936 dw_discr_list_ref *discr_lists;
23937 unsigned discr_lists_length = 0;
23938 unsigned i;
23940 dw_die_ref dwarf_proc_die = NULL;
23941 dw_die_ref variant_part_die
23942 = new_die (DW_TAG_variant_part, context_die, variant_part_type);
23944 equate_decl_number_to_die (variant_part_decl, variant_part_die);
23946 analyze_variants_discr (variant_part_decl, vlr_ctx->struct_type,
23947 &discr_decl, &discr_lists, &discr_lists_length);
23949 if (discr_decl != NULL_TREE)
23951 dw_die_ref discr_die = lookup_decl_die (discr_decl);
23953 if (discr_die)
23954 add_AT_die_ref (variant_part_die, DW_AT_discr, discr_die);
23955 else
23956 /* We have no DIE for the discriminant, so just discard all
23957 discrimimant information in the output. */
23958 discr_decl = NULL_TREE;
23961 /* If the offset for this variant part is more complex than a constant,
23962 create a DWARF procedure for it so that we will not have to generate DWARF
23963 expressions for it for each member. */
23964 if (TREE_CODE (variant_part_offset) != INTEGER_CST
23965 && (dwarf_version >= 3 || !dwarf_strict))
23967 const tree dwarf_proc_fndecl
23968 = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL, NULL_TREE,
23969 build_function_type (TREE_TYPE (variant_part_offset),
23970 NULL_TREE));
23971 const tree dwarf_proc_call = build_call_expr (dwarf_proc_fndecl, 0);
23972 const dw_loc_descr_ref dwarf_proc_body
23973 = loc_descriptor_from_tree (variant_part_offset, 0, &ctx);
23975 dwarf_proc_die = new_dwarf_proc_die (dwarf_proc_body,
23976 dwarf_proc_fndecl, context_die);
23977 if (dwarf_proc_die != NULL)
23978 variant_part_offset = dwarf_proc_call;
23981 /* Output DIEs for all variants. */
23982 i = 0;
23983 for (tree variant = TYPE_FIELDS (variant_part_type);
23984 variant != NULL_TREE;
23985 variant = DECL_CHAIN (variant), ++i)
23987 tree variant_type = TREE_TYPE (variant);
23988 dw_die_ref variant_die;
23990 /* All variants (i.e. members of a variant part) are supposed to be
23991 encoded as structures. Sub-variant parts are QUAL_UNION_TYPE fields
23992 under these records. */
23993 gcc_assert (TREE_CODE (variant_type) == RECORD_TYPE);
23995 variant_die = new_die (DW_TAG_variant, variant_part_die, variant_type);
23996 equate_decl_number_to_die (variant, variant_die);
23998 /* Output discriminant values this variant matches, if any. */
23999 if (discr_decl == NULL || discr_lists[i] == NULL)
24000 /* In the case we have discriminant information at all, this is
24001 probably the default variant: as the standard says, don't
24002 output any discriminant value/list attribute. */
24004 else if (discr_lists[i]->dw_discr_next == NULL
24005 && !discr_lists[i]->dw_discr_range)
24006 /* If there is only one accepted value, don't bother outputting a
24007 list. */
24008 add_discr_value (variant_die, &discr_lists[i]->dw_discr_lower_bound);
24009 else
24010 add_discr_list (variant_die, discr_lists[i]);
24012 for (tree member = TYPE_FIELDS (variant_type);
24013 member != NULL_TREE;
24014 member = DECL_CHAIN (member))
24016 struct vlr_context vlr_sub_ctx = {
24017 vlr_ctx->struct_type, /* struct_type */
24018 NULL /* variant_part_offset */
24020 if (is_variant_part (member))
24022 /* All offsets for fields inside variant parts are relative to
24023 the top-level embedding RECORD_TYPE's base address. On the
24024 other hand, offsets in GCC's types are relative to the
24025 nested-most variant part. So we have to sum offsets each time
24026 we recurse. */
24028 vlr_sub_ctx.variant_part_offset
24029 = fold_build2 (PLUS_EXPR, TREE_TYPE (variant_part_offset),
24030 variant_part_offset, byte_position (member));
24031 gen_variant_part (member, &vlr_sub_ctx, variant_die);
24033 else
24035 vlr_sub_ctx.variant_part_offset = variant_part_offset;
24036 gen_decl_die (member, NULL, &vlr_sub_ctx, variant_die);
24041 free (discr_lists);
24044 /* Generate a DIE for a class member. */
24046 static void
24047 gen_member_die (tree type, dw_die_ref context_die)
24049 tree member;
24050 tree binfo = TYPE_BINFO (type);
24051 dw_die_ref child;
24053 /* If this is not an incomplete type, output descriptions of each of its
24054 members. Note that as we output the DIEs necessary to represent the
24055 members of this record or union type, we will also be trying to output
24056 DIEs to represent the *types* of those members. However the `type'
24057 function (above) will specifically avoid generating type DIEs for member
24058 types *within* the list of member DIEs for this (containing) type except
24059 for those types (of members) which are explicitly marked as also being
24060 members of this (containing) type themselves. The g++ front- end can
24061 force any given type to be treated as a member of some other (containing)
24062 type by setting the TYPE_CONTEXT of the given (member) type to point to
24063 the TREE node representing the appropriate (containing) type. */
24065 /* First output info about the base classes. */
24066 if (binfo)
24068 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
24069 int i;
24070 tree base;
24072 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
24073 gen_inheritance_die (base,
24074 (accesses ? (*accesses)[i] : access_public_node),
24075 type,
24076 context_die);
24079 /* Now output info about the data members and type members. */
24080 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
24082 struct vlr_context vlr_ctx = { type, NULL_TREE };
24084 /* If we thought we were generating minimal debug info for TYPE
24085 and then changed our minds, some of the member declarations
24086 may have already been defined. Don't define them again, but
24087 do put them in the right order. */
24089 child = lookup_decl_die (member);
24090 if (child)
24092 /* Handle inline static data members, which only have in-class
24093 declarations. */
24094 if (child->die_tag == DW_TAG_variable
24095 && child->die_parent == comp_unit_die ()
24096 && get_AT (child, DW_AT_specification) == NULL)
24098 reparent_child (child, context_die);
24099 if (dwarf_version < 5)
24100 child->die_tag = DW_TAG_member;
24102 else
24103 splice_child_die (context_die, child);
24106 /* Do not generate standard DWARF for variant parts if we are generating
24107 the corresponding GNAT encodings: DIEs generated for both would
24108 conflict in our mappings. */
24109 else if (is_variant_part (member)
24110 && gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL)
24112 vlr_ctx.variant_part_offset = byte_position (member);
24113 gen_variant_part (member, &vlr_ctx, context_die);
24115 else
24117 vlr_ctx.variant_part_offset = NULL_TREE;
24118 gen_decl_die (member, NULL, &vlr_ctx, context_die);
24121 /* For C++ inline static data members emit immediately a DW_TAG_variable
24122 DIE that will refer to that DW_TAG_member/DW_TAG_variable through
24123 DW_AT_specification. */
24124 if (TREE_STATIC (member)
24125 && (lang_hooks.decls.decl_dwarf_attribute (member, DW_AT_inline)
24126 != -1))
24128 int old_extern = DECL_EXTERNAL (member);
24129 DECL_EXTERNAL (member) = 0;
24130 gen_decl_die (member, NULL, NULL, comp_unit_die ());
24131 DECL_EXTERNAL (member) = old_extern;
24135 /* We do not keep type methods in type variants. */
24136 gcc_assert (TYPE_MAIN_VARIANT (type) == type);
24137 /* Now output info about the function members (if any). */
24138 if (TYPE_METHODS (type) != error_mark_node)
24139 for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
24141 /* Don't include clones in the member list. */
24142 if (DECL_ABSTRACT_ORIGIN (member))
24143 continue;
24144 /* Nor constructors for anonymous classes. */
24145 if (DECL_ARTIFICIAL (member)
24146 && dwarf2_name (member, 0) == NULL)
24147 continue;
24149 child = lookup_decl_die (member);
24150 if (child)
24151 splice_child_die (context_die, child);
24152 else
24153 gen_decl_die (member, NULL, NULL, context_die);
24157 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
24158 is set, we pretend that the type was never defined, so we only get the
24159 member DIEs needed by later specification DIEs. */
24161 static void
24162 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
24163 enum debug_info_usage usage)
24165 if (TREE_ASM_WRITTEN (type))
24167 /* Fill in the bound of variable-length fields in late dwarf if
24168 still incomplete. */
24169 if (!early_dwarf && variably_modified_type_p (type, NULL))
24170 for (tree member = TYPE_FIELDS (type);
24171 member;
24172 member = DECL_CHAIN (member))
24173 fill_variable_array_bounds (TREE_TYPE (member));
24174 return;
24177 dw_die_ref type_die = lookup_type_die (type);
24178 dw_die_ref scope_die = 0;
24179 int nested = 0;
24180 int complete = (TYPE_SIZE (type)
24181 && (! TYPE_STUB_DECL (type)
24182 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
24183 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
24184 complete = complete && should_emit_struct_debug (type, usage);
24186 if (type_die && ! complete)
24187 return;
24189 if (TYPE_CONTEXT (type) != NULL_TREE
24190 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
24191 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
24192 nested = 1;
24194 scope_die = scope_die_for (type, context_die);
24196 /* Generate child dies for template paramaters. */
24197 if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
24198 schedule_generic_params_dies_gen (type);
24200 if (! type_die || (nested && is_cu_die (scope_die)))
24201 /* First occurrence of type or toplevel definition of nested class. */
24203 dw_die_ref old_die = type_die;
24205 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
24206 ? record_type_tag (type) : DW_TAG_union_type,
24207 scope_die, type);
24208 equate_type_number_to_die (type, type_die);
24209 if (old_die)
24210 add_AT_specification (type_die, old_die);
24211 else
24212 add_name_attribute (type_die, type_tag (type));
24214 else
24215 remove_AT (type_die, DW_AT_declaration);
24217 /* If this type has been completed, then give it a byte_size attribute and
24218 then give a list of members. */
24219 if (complete && !ns_decl)
24221 /* Prevent infinite recursion in cases where the type of some member of
24222 this type is expressed in terms of this type itself. */
24223 TREE_ASM_WRITTEN (type) = 1;
24224 add_byte_size_attribute (type_die, type);
24225 add_alignment_attribute (type_die, type);
24226 if (TYPE_STUB_DECL (type) != NULL_TREE)
24228 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
24229 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
24232 /* If the first reference to this type was as the return type of an
24233 inline function, then it may not have a parent. Fix this now. */
24234 if (type_die->die_parent == NULL)
24235 add_child_die (scope_die, type_die);
24237 push_decl_scope (type);
24238 gen_member_die (type, type_die);
24239 pop_decl_scope ();
24241 add_gnat_descriptive_type_attribute (type_die, type, context_die);
24242 if (TYPE_ARTIFICIAL (type))
24243 add_AT_flag (type_die, DW_AT_artificial, 1);
24245 /* GNU extension: Record what type our vtable lives in. */
24246 if (TYPE_VFIELD (type))
24248 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
24250 gen_type_die (vtype, context_die);
24251 add_AT_die_ref (type_die, DW_AT_containing_type,
24252 lookup_type_die (vtype));
24255 else
24257 add_AT_flag (type_die, DW_AT_declaration, 1);
24259 /* We don't need to do this for function-local types. */
24260 if (TYPE_STUB_DECL (type)
24261 && ! decl_function_context (TYPE_STUB_DECL (type)))
24262 vec_safe_push (incomplete_types, type);
24265 if (get_AT (type_die, DW_AT_name))
24266 add_pubtype (type, type_die);
24269 /* Generate a DIE for a subroutine _type_. */
24271 static void
24272 gen_subroutine_type_die (tree type, dw_die_ref context_die)
24274 tree return_type = TREE_TYPE (type);
24275 dw_die_ref subr_die
24276 = new_die (DW_TAG_subroutine_type,
24277 scope_die_for (type, context_die), type);
24279 equate_type_number_to_die (type, subr_die);
24280 add_prototyped_attribute (subr_die, type);
24281 add_type_attribute (subr_die, return_type, TYPE_UNQUALIFIED, false,
24282 context_die);
24283 add_alignment_attribute (subr_die, type);
24284 gen_formal_types_die (type, subr_die);
24286 if (get_AT (subr_die, DW_AT_name))
24287 add_pubtype (type, subr_die);
24288 if ((dwarf_version >= 5 || !dwarf_strict)
24289 && lang_hooks.types.type_dwarf_attribute (type, DW_AT_reference) != -1)
24290 add_AT_flag (subr_die, DW_AT_reference, 1);
24291 if ((dwarf_version >= 5 || !dwarf_strict)
24292 && lang_hooks.types.type_dwarf_attribute (type,
24293 DW_AT_rvalue_reference) != -1)
24294 add_AT_flag (subr_die, DW_AT_rvalue_reference, 1);
24297 /* Generate a DIE for a type definition. */
24299 static void
24300 gen_typedef_die (tree decl, dw_die_ref context_die)
24302 dw_die_ref type_die;
24303 tree origin;
24305 if (TREE_ASM_WRITTEN (decl))
24307 if (DECL_ORIGINAL_TYPE (decl))
24308 fill_variable_array_bounds (DECL_ORIGINAL_TYPE (decl));
24309 return;
24312 TREE_ASM_WRITTEN (decl) = 1;
24313 type_die = new_die (DW_TAG_typedef, context_die, decl);
24314 origin = decl_ultimate_origin (decl);
24315 if (origin != NULL)
24316 add_abstract_origin_attribute (type_die, origin);
24317 else
24319 tree type = TREE_TYPE (decl);
24321 if (type == error_mark_node)
24322 return;
24324 add_name_and_src_coords_attributes (type_die, decl);
24325 if (DECL_ORIGINAL_TYPE (decl))
24327 type = DECL_ORIGINAL_TYPE (decl);
24329 if (type == error_mark_node)
24330 return;
24332 gcc_assert (type != TREE_TYPE (decl));
24333 equate_type_number_to_die (TREE_TYPE (decl), type_die);
24335 else
24337 if (is_naming_typedef_decl (TYPE_NAME (type)))
24339 /* Here, we are in the case of decl being a typedef naming
24340 an anonymous type, e.g:
24341 typedef struct {...} foo;
24342 In that case TREE_TYPE (decl) is not a typedef variant
24343 type and TYPE_NAME of the anonymous type is set to the
24344 TYPE_DECL of the typedef. This construct is emitted by
24345 the C++ FE.
24347 TYPE is the anonymous struct named by the typedef
24348 DECL. As we need the DW_AT_type attribute of the
24349 DW_TAG_typedef to point to the DIE of TYPE, let's
24350 generate that DIE right away. add_type_attribute
24351 called below will then pick (via lookup_type_die) that
24352 anonymous struct DIE. */
24353 if (!TREE_ASM_WRITTEN (type))
24354 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
24356 /* This is a GNU Extension. We are adding a
24357 DW_AT_linkage_name attribute to the DIE of the
24358 anonymous struct TYPE. The value of that attribute
24359 is the name of the typedef decl naming the anonymous
24360 struct. This greatly eases the work of consumers of
24361 this debug info. */
24362 add_linkage_name_raw (lookup_type_die (type), decl);
24366 add_type_attribute (type_die, type, decl_quals (decl), false,
24367 context_die);
24369 if (is_naming_typedef_decl (decl))
24370 /* We want that all subsequent calls to lookup_type_die with
24371 TYPE in argument yield the DW_TAG_typedef we have just
24372 created. */
24373 equate_type_number_to_die (type, type_die);
24375 type = TREE_TYPE (decl);
24377 add_alignment_attribute (type_die, type);
24379 add_accessibility_attribute (type_die, decl);
24382 if (DECL_ABSTRACT_P (decl))
24383 equate_decl_number_to_die (decl, type_die);
24385 if (get_AT (type_die, DW_AT_name))
24386 add_pubtype (decl, type_die);
24389 /* Generate a DIE for a struct, class, enum or union type. */
24391 static void
24392 gen_tagged_type_die (tree type,
24393 dw_die_ref context_die,
24394 enum debug_info_usage usage)
24396 int need_pop;
24398 if (type == NULL_TREE
24399 || !is_tagged_type (type))
24400 return;
24402 if (TREE_ASM_WRITTEN (type))
24403 need_pop = 0;
24404 /* If this is a nested type whose containing class hasn't been written
24405 out yet, writing it out will cover this one, too. This does not apply
24406 to instantiations of member class templates; they need to be added to
24407 the containing class as they are generated. FIXME: This hurts the
24408 idea of combining type decls from multiple TUs, since we can't predict
24409 what set of template instantiations we'll get. */
24410 else if (TYPE_CONTEXT (type)
24411 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
24412 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
24414 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
24416 if (TREE_ASM_WRITTEN (type))
24417 return;
24419 /* If that failed, attach ourselves to the stub. */
24420 push_decl_scope (TYPE_CONTEXT (type));
24421 context_die = lookup_type_die (TYPE_CONTEXT (type));
24422 need_pop = 1;
24424 else if (TYPE_CONTEXT (type) != NULL_TREE
24425 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
24427 /* If this type is local to a function that hasn't been written
24428 out yet, use a NULL context for now; it will be fixed up in
24429 decls_for_scope. */
24430 context_die = lookup_decl_die (TYPE_CONTEXT (type));
24431 /* A declaration DIE doesn't count; nested types need to go in the
24432 specification. */
24433 if (context_die && is_declaration_die (context_die))
24434 context_die = NULL;
24435 need_pop = 0;
24437 else
24439 context_die = declare_in_namespace (type, context_die);
24440 need_pop = 0;
24443 if (TREE_CODE (type) == ENUMERAL_TYPE)
24445 /* This might have been written out by the call to
24446 declare_in_namespace. */
24447 if (!TREE_ASM_WRITTEN (type))
24448 gen_enumeration_type_die (type, context_die);
24450 else
24451 gen_struct_or_union_type_die (type, context_die, usage);
24453 if (need_pop)
24454 pop_decl_scope ();
24456 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
24457 it up if it is ever completed. gen_*_type_die will set it for us
24458 when appropriate. */
24461 /* Generate a type description DIE. */
24463 static void
24464 gen_type_die_with_usage (tree type, dw_die_ref context_die,
24465 enum debug_info_usage usage)
24467 struct array_descr_info info;
24469 if (type == NULL_TREE || type == error_mark_node)
24470 return;
24472 if (flag_checking && type)
24473 verify_type (type);
24475 if (TYPE_NAME (type) != NULL_TREE
24476 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
24477 && is_redundant_typedef (TYPE_NAME (type))
24478 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
24479 /* The DECL of this type is a typedef we don't want to emit debug
24480 info for but we want debug info for its underlying typedef.
24481 This can happen for e.g, the injected-class-name of a C++
24482 type. */
24483 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
24485 /* If TYPE is a typedef type variant, let's generate debug info
24486 for the parent typedef which TYPE is a type of. */
24487 if (typedef_variant_p (type))
24489 if (TREE_ASM_WRITTEN (type))
24490 return;
24492 /* Prevent broken recursion; we can't hand off to the same type. */
24493 gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
24495 /* Give typedefs the right scope. */
24496 context_die = scope_die_for (type, context_die);
24498 TREE_ASM_WRITTEN (type) = 1;
24500 gen_decl_die (TYPE_NAME (type), NULL, NULL, context_die);
24501 return;
24504 /* If type is an anonymous tagged type named by a typedef, let's
24505 generate debug info for the typedef. */
24506 if (is_naming_typedef_decl (TYPE_NAME (type)))
24508 /* Use the DIE of the containing namespace as the parent DIE of
24509 the type description DIE we want to generate. */
24510 if (DECL_CONTEXT (TYPE_NAME (type))
24511 && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
24512 context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
24514 gen_decl_die (TYPE_NAME (type), NULL, NULL, context_die);
24515 return;
24518 if (lang_hooks.types.get_debug_type)
24520 tree debug_type = lang_hooks.types.get_debug_type (type);
24522 if (debug_type != NULL_TREE && debug_type != type)
24524 gen_type_die_with_usage (debug_type, context_die, usage);
24525 return;
24529 /* We are going to output a DIE to represent the unqualified version
24530 of this type (i.e. without any const or volatile qualifiers) so
24531 get the main variant (i.e. the unqualified version) of this type
24532 now. (Vectors and arrays are special because the debugging info is in the
24533 cloned type itself. Similarly function/method types can contain extra
24534 ref-qualification). */
24535 if (TREE_CODE (type) == FUNCTION_TYPE
24536 || TREE_CODE (type) == METHOD_TYPE)
24538 /* For function/method types, can't use type_main_variant here,
24539 because that can have different ref-qualifiers for C++,
24540 but try to canonicalize. */
24541 tree main = TYPE_MAIN_VARIANT (type);
24542 for (tree t = main; t; t = TYPE_NEXT_VARIANT (t))
24544 if (check_base_type (t, main) && check_lang_type (t, type))
24546 type = t;
24547 break;
24551 else if (TREE_CODE (type) != VECTOR_TYPE
24552 && TREE_CODE (type) != ARRAY_TYPE)
24553 type = type_main_variant (type);
24555 /* If this is an array type with hidden descriptor, handle it first. */
24556 if (!TREE_ASM_WRITTEN (type)
24557 && lang_hooks.types.get_array_descr_info)
24559 memset (&info, 0, sizeof (info));
24560 if (lang_hooks.types.get_array_descr_info (type, &info))
24562 /* Fortran sometimes emits array types with no dimension. */
24563 gcc_assert (info.ndimensions >= 0
24564 && (info.ndimensions
24565 <= DWARF2OUT_ARRAY_DESCR_INFO_MAX_DIMEN));
24566 gen_descr_array_type_die (type, &info, context_die);
24567 TREE_ASM_WRITTEN (type) = 1;
24568 return;
24572 if (TREE_ASM_WRITTEN (type))
24574 /* Variable-length types may be incomplete even if
24575 TREE_ASM_WRITTEN. For such types, fall through to
24576 gen_array_type_die() and possibly fill in
24577 DW_AT_{upper,lower}_bound attributes. */
24578 if ((TREE_CODE (type) != ARRAY_TYPE
24579 && TREE_CODE (type) != RECORD_TYPE
24580 && TREE_CODE (type) != UNION_TYPE
24581 && TREE_CODE (type) != QUAL_UNION_TYPE)
24582 || !variably_modified_type_p (type, NULL))
24583 return;
24586 switch (TREE_CODE (type))
24588 case ERROR_MARK:
24589 break;
24591 case POINTER_TYPE:
24592 case REFERENCE_TYPE:
24593 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
24594 ensures that the gen_type_die recursion will terminate even if the
24595 type is recursive. Recursive types are possible in Ada. */
24596 /* ??? We could perhaps do this for all types before the switch
24597 statement. */
24598 TREE_ASM_WRITTEN (type) = 1;
24600 /* For these types, all that is required is that we output a DIE (or a
24601 set of DIEs) to represent the "basis" type. */
24602 gen_type_die_with_usage (TREE_TYPE (type), context_die,
24603 DINFO_USAGE_IND_USE);
24604 break;
24606 case OFFSET_TYPE:
24607 /* This code is used for C++ pointer-to-data-member types.
24608 Output a description of the relevant class type. */
24609 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
24610 DINFO_USAGE_IND_USE);
24612 /* Output a description of the type of the object pointed to. */
24613 gen_type_die_with_usage (TREE_TYPE (type), context_die,
24614 DINFO_USAGE_IND_USE);
24616 /* Now output a DIE to represent this pointer-to-data-member type
24617 itself. */
24618 gen_ptr_to_mbr_type_die (type, context_die);
24619 break;
24621 case FUNCTION_TYPE:
24622 /* Force out return type (in case it wasn't forced out already). */
24623 gen_type_die_with_usage (TREE_TYPE (type), context_die,
24624 DINFO_USAGE_DIR_USE);
24625 gen_subroutine_type_die (type, context_die);
24626 break;
24628 case METHOD_TYPE:
24629 /* Force out return type (in case it wasn't forced out already). */
24630 gen_type_die_with_usage (TREE_TYPE (type), context_die,
24631 DINFO_USAGE_DIR_USE);
24632 gen_subroutine_type_die (type, context_die);
24633 break;
24635 case ARRAY_TYPE:
24636 case VECTOR_TYPE:
24637 gen_array_type_die (type, context_die);
24638 break;
24640 case ENUMERAL_TYPE:
24641 case RECORD_TYPE:
24642 case UNION_TYPE:
24643 case QUAL_UNION_TYPE:
24644 gen_tagged_type_die (type, context_die, usage);
24645 return;
24647 case VOID_TYPE:
24648 case INTEGER_TYPE:
24649 case REAL_TYPE:
24650 case FIXED_POINT_TYPE:
24651 case COMPLEX_TYPE:
24652 case BOOLEAN_TYPE:
24653 case POINTER_BOUNDS_TYPE:
24654 /* No DIEs needed for fundamental types. */
24655 break;
24657 case NULLPTR_TYPE:
24658 case LANG_TYPE:
24659 /* Just use DW_TAG_unspecified_type. */
24661 dw_die_ref type_die = lookup_type_die (type);
24662 if (type_die == NULL)
24664 tree name = TYPE_IDENTIFIER (type);
24665 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (),
24666 type);
24667 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
24668 equate_type_number_to_die (type, type_die);
24671 break;
24673 default:
24674 if (is_cxx_auto (type))
24676 tree name = TYPE_IDENTIFIER (type);
24677 dw_die_ref *die = (name == get_identifier ("auto")
24678 ? &auto_die : &decltype_auto_die);
24679 if (!*die)
24681 *die = new_die (DW_TAG_unspecified_type,
24682 comp_unit_die (), NULL_TREE);
24683 add_name_attribute (*die, IDENTIFIER_POINTER (name));
24685 equate_type_number_to_die (type, *die);
24686 break;
24688 gcc_unreachable ();
24691 TREE_ASM_WRITTEN (type) = 1;
24694 static void
24695 gen_type_die (tree type, dw_die_ref context_die)
24697 if (type != error_mark_node)
24699 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
24700 if (flag_checking)
24702 dw_die_ref die = lookup_type_die (type);
24703 if (die)
24704 check_die (die);
24709 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
24710 things which are local to the given block. */
24712 static void
24713 gen_block_die (tree stmt, dw_die_ref context_die)
24715 int must_output_die = 0;
24716 bool inlined_func;
24718 /* Ignore blocks that are NULL. */
24719 if (stmt == NULL_TREE)
24720 return;
24722 inlined_func = inlined_function_outer_scope_p (stmt);
24724 /* If the block is one fragment of a non-contiguous block, do not
24725 process the variables, since they will have been done by the
24726 origin block. Do process subblocks. */
24727 if (BLOCK_FRAGMENT_ORIGIN (stmt))
24729 tree sub;
24731 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
24732 gen_block_die (sub, context_die);
24734 return;
24737 /* Determine if we need to output any Dwarf DIEs at all to represent this
24738 block. */
24739 if (inlined_func)
24740 /* The outer scopes for inlinings *must* always be represented. We
24741 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
24742 must_output_die = 1;
24743 else
24745 /* Determine if this block directly contains any "significant"
24746 local declarations which we will need to output DIEs for. */
24747 if (debug_info_level > DINFO_LEVEL_TERSE)
24748 /* We are not in terse mode so *any* local declaration counts
24749 as being a "significant" one. */
24750 must_output_die = ((BLOCK_VARS (stmt) != NULL
24751 || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
24752 && (TREE_USED (stmt)
24753 || TREE_ASM_WRITTEN (stmt)
24754 || BLOCK_ABSTRACT (stmt)));
24755 else if ((TREE_USED (stmt)
24756 || TREE_ASM_WRITTEN (stmt)
24757 || BLOCK_ABSTRACT (stmt))
24758 && !dwarf2out_ignore_block (stmt))
24759 must_output_die = 1;
24762 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
24763 DIE for any block which contains no significant local declarations at
24764 all. Rather, in such cases we just call `decls_for_scope' so that any
24765 needed Dwarf info for any sub-blocks will get properly generated. Note
24766 that in terse mode, our definition of what constitutes a "significant"
24767 local declaration gets restricted to include only inlined function
24768 instances and local (nested) function definitions. */
24769 if (must_output_die)
24771 if (inlined_func)
24773 /* If STMT block is abstract, that means we have been called
24774 indirectly from dwarf2out_abstract_function.
24775 That function rightfully marks the descendent blocks (of
24776 the abstract function it is dealing with) as being abstract,
24777 precisely to prevent us from emitting any
24778 DW_TAG_inlined_subroutine DIE as a descendent
24779 of an abstract function instance. So in that case, we should
24780 not call gen_inlined_subroutine_die.
24782 Later though, when cgraph asks dwarf2out to emit info
24783 for the concrete instance of the function decl into which
24784 the concrete instance of STMT got inlined, the later will lead
24785 to the generation of a DW_TAG_inlined_subroutine DIE. */
24786 if (! BLOCK_ABSTRACT (stmt))
24787 gen_inlined_subroutine_die (stmt, context_die);
24789 else
24790 gen_lexical_block_die (stmt, context_die);
24792 else
24793 decls_for_scope (stmt, context_die);
24796 /* Process variable DECL (or variable with origin ORIGIN) within
24797 block STMT and add it to CONTEXT_DIE. */
24798 static void
24799 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
24801 dw_die_ref die;
24802 tree decl_or_origin = decl ? decl : origin;
24804 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
24805 die = lookup_decl_die (decl_or_origin);
24806 else if (TREE_CODE (decl_or_origin) == TYPE_DECL)
24808 if (TYPE_DECL_IS_STUB (decl_or_origin))
24809 die = lookup_type_die (TREE_TYPE (decl_or_origin));
24810 else
24811 die = lookup_decl_die (decl_or_origin);
24812 /* Avoid re-creating the DIE late if it was optimized as unused early. */
24813 if (! die && ! early_dwarf)
24814 return;
24816 else
24817 die = NULL;
24819 if (die != NULL && die->die_parent == NULL)
24820 add_child_die (context_die, die);
24821 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
24823 if (early_dwarf)
24824 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
24825 stmt, context_die);
24827 else
24828 gen_decl_die (decl, origin, NULL, context_die);
24831 /* Generate all of the decls declared within a given scope and (recursively)
24832 all of its sub-blocks. */
24834 static void
24835 decls_for_scope (tree stmt, dw_die_ref context_die)
24837 tree decl;
24838 unsigned int i;
24839 tree subblocks;
24841 /* Ignore NULL blocks. */
24842 if (stmt == NULL_TREE)
24843 return;
24845 /* Output the DIEs to represent all of the data objects and typedefs
24846 declared directly within this block but not within any nested
24847 sub-blocks. Also, nested function and tag DIEs have been
24848 generated with a parent of NULL; fix that up now. We don't
24849 have to do this if we're at -g1. */
24850 if (debug_info_level > DINFO_LEVEL_TERSE)
24852 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
24853 process_scope_var (stmt, decl, NULL_TREE, context_die);
24854 /* BLOCK_NONLOCALIZED_VARs simply generate DIE stubs with abstract
24855 origin - avoid doing this twice as we have no good way to see
24856 if we've done it once already. */
24857 if (! early_dwarf)
24858 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
24859 process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
24860 context_die);
24863 /* Even if we're at -g1, we need to process the subblocks in order to get
24864 inlined call information. */
24866 /* Output the DIEs to represent all sub-blocks (and the items declared
24867 therein) of this block. */
24868 for (subblocks = BLOCK_SUBBLOCKS (stmt);
24869 subblocks != NULL;
24870 subblocks = BLOCK_CHAIN (subblocks))
24871 gen_block_die (subblocks, context_die);
24874 /* Is this a typedef we can avoid emitting? */
24876 bool
24877 is_redundant_typedef (const_tree decl)
24879 if (TYPE_DECL_IS_STUB (decl))
24880 return true;
24882 if (DECL_ARTIFICIAL (decl)
24883 && DECL_CONTEXT (decl)
24884 && is_tagged_type (DECL_CONTEXT (decl))
24885 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
24886 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
24887 /* Also ignore the artificial member typedef for the class name. */
24888 return true;
24890 return false;
24893 /* Return TRUE if TYPE is a typedef that names a type for linkage
24894 purposes. This kind of typedefs is produced by the C++ FE for
24895 constructs like:
24897 typedef struct {...} foo;
24899 In that case, there is no typedef variant type produced for foo.
24900 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
24901 struct type. */
24903 static bool
24904 is_naming_typedef_decl (const_tree decl)
24906 if (decl == NULL_TREE
24907 || TREE_CODE (decl) != TYPE_DECL
24908 || DECL_NAMELESS (decl)
24909 || !is_tagged_type (TREE_TYPE (decl))
24910 || DECL_IS_BUILTIN (decl)
24911 || is_redundant_typedef (decl)
24912 /* It looks like Ada produces TYPE_DECLs that are very similar
24913 to C++ naming typedefs but that have different
24914 semantics. Let's be specific to c++ for now. */
24915 || !is_cxx (decl))
24916 return FALSE;
24918 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
24919 && TYPE_NAME (TREE_TYPE (decl)) == decl
24920 && (TYPE_STUB_DECL (TREE_TYPE (decl))
24921 != TYPE_NAME (TREE_TYPE (decl))));
24924 /* Looks up the DIE for a context. */
24926 static inline dw_die_ref
24927 lookup_context_die (tree context)
24929 if (context)
24931 /* Find die that represents this context. */
24932 if (TYPE_P (context))
24934 context = TYPE_MAIN_VARIANT (context);
24935 dw_die_ref ctx = lookup_type_die (context);
24936 if (!ctx)
24937 return NULL;
24938 return strip_naming_typedef (context, ctx);
24940 else
24941 return lookup_decl_die (context);
24943 return comp_unit_die ();
24946 /* Returns the DIE for a context. */
24948 static inline dw_die_ref
24949 get_context_die (tree context)
24951 if (context)
24953 /* Find die that represents this context. */
24954 if (TYPE_P (context))
24956 context = TYPE_MAIN_VARIANT (context);
24957 return strip_naming_typedef (context, force_type_die (context));
24959 else
24960 return force_decl_die (context);
24962 return comp_unit_die ();
24965 /* Returns the DIE for decl. A DIE will always be returned. */
24967 static dw_die_ref
24968 force_decl_die (tree decl)
24970 dw_die_ref decl_die;
24971 unsigned saved_external_flag;
24972 tree save_fn = NULL_TREE;
24973 decl_die = lookup_decl_die (decl);
24974 if (!decl_die)
24976 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
24978 decl_die = lookup_decl_die (decl);
24979 if (decl_die)
24980 return decl_die;
24982 switch (TREE_CODE (decl))
24984 case FUNCTION_DECL:
24985 /* Clear current_function_decl, so that gen_subprogram_die thinks
24986 that this is a declaration. At this point, we just want to force
24987 declaration die. */
24988 save_fn = current_function_decl;
24989 current_function_decl = NULL_TREE;
24990 gen_subprogram_die (decl, context_die);
24991 current_function_decl = save_fn;
24992 break;
24994 case VAR_DECL:
24995 /* Set external flag to force declaration die. Restore it after
24996 gen_decl_die() call. */
24997 saved_external_flag = DECL_EXTERNAL (decl);
24998 DECL_EXTERNAL (decl) = 1;
24999 gen_decl_die (decl, NULL, NULL, context_die);
25000 DECL_EXTERNAL (decl) = saved_external_flag;
25001 break;
25003 case NAMESPACE_DECL:
25004 if (dwarf_version >= 3 || !dwarf_strict)
25005 dwarf2out_decl (decl);
25006 else
25007 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
25008 decl_die = comp_unit_die ();
25009 break;
25011 case TRANSLATION_UNIT_DECL:
25012 decl_die = comp_unit_die ();
25013 break;
25015 default:
25016 gcc_unreachable ();
25019 /* We should be able to find the DIE now. */
25020 if (!decl_die)
25021 decl_die = lookup_decl_die (decl);
25022 gcc_assert (decl_die);
25025 return decl_die;
25028 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
25029 always returned. */
25031 static dw_die_ref
25032 force_type_die (tree type)
25034 dw_die_ref type_die;
25036 type_die = lookup_type_die (type);
25037 if (!type_die)
25039 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
25041 type_die = modified_type_die (type, TYPE_QUALS_NO_ADDR_SPACE (type),
25042 false, context_die);
25043 gcc_assert (type_die);
25045 return type_die;
25048 /* Force out any required namespaces to be able to output DECL,
25049 and return the new context_die for it, if it's changed. */
25051 static dw_die_ref
25052 setup_namespace_context (tree thing, dw_die_ref context_die)
25054 tree context = (DECL_P (thing)
25055 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
25056 if (context && TREE_CODE (context) == NAMESPACE_DECL)
25057 /* Force out the namespace. */
25058 context_die = force_decl_die (context);
25060 return context_die;
25063 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
25064 type) within its namespace, if appropriate.
25066 For compatibility with older debuggers, namespace DIEs only contain
25067 declarations; all definitions are emitted at CU scope, with
25068 DW_AT_specification pointing to the declaration (like with class
25069 members). */
25071 static dw_die_ref
25072 declare_in_namespace (tree thing, dw_die_ref context_die)
25074 dw_die_ref ns_context;
25076 if (debug_info_level <= DINFO_LEVEL_TERSE)
25077 return context_die;
25079 /* External declarations in the local scope only need to be emitted
25080 once, not once in the namespace and once in the scope.
25082 This avoids declaring the `extern' below in the
25083 namespace DIE as well as in the innermost scope:
25085 namespace S
25087 int i=5;
25088 int foo()
25090 int i=8;
25091 extern int i;
25092 return i;
25096 if (DECL_P (thing) && DECL_EXTERNAL (thing) && local_scope_p (context_die))
25097 return context_die;
25099 /* If this decl is from an inlined function, then don't try to emit it in its
25100 namespace, as we will get confused. It would have already been emitted
25101 when the abstract instance of the inline function was emitted anyways. */
25102 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
25103 return context_die;
25105 ns_context = setup_namespace_context (thing, context_die);
25107 if (ns_context != context_die)
25109 if (is_fortran ())
25110 return ns_context;
25111 if (DECL_P (thing))
25112 gen_decl_die (thing, NULL, NULL, ns_context);
25113 else
25114 gen_type_die (thing, ns_context);
25116 return context_die;
25119 /* Generate a DIE for a namespace or namespace alias. */
25121 static void
25122 gen_namespace_die (tree decl, dw_die_ref context_die)
25124 dw_die_ref namespace_die;
25126 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
25127 they are an alias of. */
25128 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
25130 /* Output a real namespace or module. */
25131 context_die = setup_namespace_context (decl, comp_unit_die ());
25132 namespace_die = new_die (is_fortran ()
25133 ? DW_TAG_module : DW_TAG_namespace,
25134 context_die, decl);
25135 /* For Fortran modules defined in different CU don't add src coords. */
25136 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
25138 const char *name = dwarf2_name (decl, 0);
25139 if (name)
25140 add_name_attribute (namespace_die, name);
25142 else
25143 add_name_and_src_coords_attributes (namespace_die, decl);
25144 if (DECL_EXTERNAL (decl))
25145 add_AT_flag (namespace_die, DW_AT_declaration, 1);
25146 equate_decl_number_to_die (decl, namespace_die);
25148 else
25150 /* Output a namespace alias. */
25152 /* Force out the namespace we are an alias of, if necessary. */
25153 dw_die_ref origin_die
25154 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
25156 if (DECL_FILE_SCOPE_P (decl)
25157 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
25158 context_die = setup_namespace_context (decl, comp_unit_die ());
25159 /* Now create the namespace alias DIE. */
25160 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
25161 add_name_and_src_coords_attributes (namespace_die, decl);
25162 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
25163 equate_decl_number_to_die (decl, namespace_die);
25165 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
25166 if (want_pubnames ())
25167 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
25170 /* Generate Dwarf debug information for a decl described by DECL.
25171 The return value is currently only meaningful for PARM_DECLs,
25172 for all other decls it returns NULL.
25174 If DECL is a FIELD_DECL, CTX is required: see the comment for VLR_CONTEXT.
25175 It can be NULL otherwise. */
25177 static dw_die_ref
25178 gen_decl_die (tree decl, tree origin, struct vlr_context *ctx,
25179 dw_die_ref context_die)
25181 tree decl_or_origin = decl ? decl : origin;
25182 tree class_origin = NULL, ultimate_origin;
25184 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
25185 return NULL;
25187 /* Ignore pointer bounds decls. */
25188 if (DECL_P (decl_or_origin)
25189 && TREE_TYPE (decl_or_origin)
25190 && POINTER_BOUNDS_P (decl_or_origin))
25191 return NULL;
25193 switch (TREE_CODE (decl_or_origin))
25195 case ERROR_MARK:
25196 break;
25198 case CONST_DECL:
25199 if (!is_fortran () && !is_ada ())
25201 /* The individual enumerators of an enum type get output when we output
25202 the Dwarf representation of the relevant enum type itself. */
25203 break;
25206 /* Emit its type. */
25207 gen_type_die (TREE_TYPE (decl), context_die);
25209 /* And its containing namespace. */
25210 context_die = declare_in_namespace (decl, context_die);
25212 gen_const_die (decl, context_die);
25213 break;
25215 case FUNCTION_DECL:
25216 /* Don't output any DIEs to represent mere function declarations,
25217 unless they are class members or explicit block externs. */
25218 if (DECL_INITIAL (decl_or_origin) == NULL_TREE
25219 && DECL_FILE_SCOPE_P (decl_or_origin)
25220 && (current_function_decl == NULL_TREE
25221 || DECL_ARTIFICIAL (decl_or_origin)))
25222 break;
25224 #if 0
25225 /* FIXME */
25226 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
25227 on local redeclarations of global functions. That seems broken. */
25228 if (current_function_decl != decl)
25229 /* This is only a declaration. */;
25230 #endif
25232 /* If we're emitting a clone, emit info for the abstract instance. */
25233 if (origin || DECL_ORIGIN (decl) != decl)
25234 dwarf2out_abstract_function (origin
25235 ? DECL_ORIGIN (origin)
25236 : DECL_ABSTRACT_ORIGIN (decl));
25238 /* If we're emitting an out-of-line copy of an inline function,
25239 emit info for the abstract instance and set up to refer to it. */
25240 else if (cgraph_function_possibly_inlined_p (decl)
25241 && ! DECL_ABSTRACT_P (decl)
25242 && ! class_or_namespace_scope_p (context_die)
25243 /* dwarf2out_abstract_function won't emit a die if this is just
25244 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
25245 that case, because that works only if we have a die. */
25246 && DECL_INITIAL (decl) != NULL_TREE)
25248 dwarf2out_abstract_function (decl);
25249 set_decl_origin_self (decl);
25252 /* Otherwise we're emitting the primary DIE for this decl. */
25253 else if (debug_info_level > DINFO_LEVEL_TERSE)
25255 /* Before we describe the FUNCTION_DECL itself, make sure that we
25256 have its containing type. */
25257 if (!origin)
25258 origin = decl_class_context (decl);
25259 if (origin != NULL_TREE)
25260 gen_type_die (origin, context_die);
25262 /* And its return type. */
25263 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
25265 /* And its virtual context. */
25266 if (DECL_VINDEX (decl) != NULL_TREE)
25267 gen_type_die (DECL_CONTEXT (decl), context_die);
25269 /* Make sure we have a member DIE for decl. */
25270 if (origin != NULL_TREE)
25271 gen_type_die_for_member (origin, decl, context_die);
25273 /* And its containing namespace. */
25274 context_die = declare_in_namespace (decl, context_die);
25277 /* Now output a DIE to represent the function itself. */
25278 if (decl)
25279 gen_subprogram_die (decl, context_die);
25280 break;
25282 case TYPE_DECL:
25283 /* If we are in terse mode, don't generate any DIEs to represent any
25284 actual typedefs. */
25285 if (debug_info_level <= DINFO_LEVEL_TERSE)
25286 break;
25288 /* In the special case of a TYPE_DECL node representing the declaration
25289 of some type tag, if the given TYPE_DECL is marked as having been
25290 instantiated from some other (original) TYPE_DECL node (e.g. one which
25291 was generated within the original definition of an inline function) we
25292 used to generate a special (abbreviated) DW_TAG_structure_type,
25293 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
25294 should be actually referencing those DIEs, as variable DIEs with that
25295 type would be emitted already in the abstract origin, so it was always
25296 removed during unused type prunning. Don't add anything in this
25297 case. */
25298 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
25299 break;
25301 if (is_redundant_typedef (decl))
25302 gen_type_die (TREE_TYPE (decl), context_die);
25303 else
25304 /* Output a DIE to represent the typedef itself. */
25305 gen_typedef_die (decl, context_die);
25306 break;
25308 case LABEL_DECL:
25309 if (debug_info_level >= DINFO_LEVEL_NORMAL)
25310 gen_label_die (decl, context_die);
25311 break;
25313 case VAR_DECL:
25314 case RESULT_DECL:
25315 /* If we are in terse mode, don't generate any DIEs to represent any
25316 variable declarations or definitions. */
25317 if (debug_info_level <= DINFO_LEVEL_TERSE)
25318 break;
25320 /* Output any DIEs that are needed to specify the type of this data
25321 object. */
25322 if (decl_by_reference_p (decl_or_origin))
25323 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
25324 else
25325 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
25327 /* And its containing type. */
25328 class_origin = decl_class_context (decl_or_origin);
25329 if (class_origin != NULL_TREE)
25330 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
25332 /* And its containing namespace. */
25333 context_die = declare_in_namespace (decl_or_origin, context_die);
25335 /* Now output the DIE to represent the data object itself. This gets
25336 complicated because of the possibility that the VAR_DECL really
25337 represents an inlined instance of a formal parameter for an inline
25338 function. */
25339 ultimate_origin = decl_ultimate_origin (decl_or_origin);
25340 if (ultimate_origin != NULL_TREE
25341 && TREE_CODE (ultimate_origin) == PARM_DECL)
25342 gen_formal_parameter_die (decl, origin,
25343 true /* Emit name attribute. */,
25344 context_die);
25345 else
25346 gen_variable_die (decl, origin, context_die);
25347 break;
25349 case FIELD_DECL:
25350 gcc_assert (ctx != NULL && ctx->struct_type != NULL);
25351 /* Ignore the nameless fields that are used to skip bits but handle C++
25352 anonymous unions and structs. */
25353 if (DECL_NAME (decl) != NULL_TREE
25354 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
25355 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
25357 gen_type_die (member_declared_type (decl), context_die);
25358 gen_field_die (decl, ctx, context_die);
25360 break;
25362 case PARM_DECL:
25363 if (DECL_BY_REFERENCE (decl_or_origin))
25364 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
25365 else
25366 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
25367 return gen_formal_parameter_die (decl, origin,
25368 true /* Emit name attribute. */,
25369 context_die);
25371 case NAMESPACE_DECL:
25372 if (dwarf_version >= 3 || !dwarf_strict)
25373 gen_namespace_die (decl, context_die);
25374 break;
25376 case IMPORTED_DECL:
25377 dwarf2out_imported_module_or_decl_1 (decl, DECL_NAME (decl),
25378 DECL_CONTEXT (decl), context_die);
25379 break;
25381 case NAMELIST_DECL:
25382 gen_namelist_decl (DECL_NAME (decl), context_die,
25383 NAMELIST_DECL_ASSOCIATED_DECL (decl));
25384 break;
25386 default:
25387 /* Probably some frontend-internal decl. Assume we don't care. */
25388 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
25389 break;
25392 return NULL;
25395 /* Output initial debug information for global DECL. Called at the
25396 end of the parsing process.
25398 This is the initial debug generation process. As such, the DIEs
25399 generated may be incomplete. A later debug generation pass
25400 (dwarf2out_late_global_decl) will augment the information generated
25401 in this pass (e.g., with complete location info). */
25403 static void
25404 dwarf2out_early_global_decl (tree decl)
25406 set_early_dwarf s;
25408 /* gen_decl_die() will set DECL_ABSTRACT because
25409 cgraph_function_possibly_inlined_p() returns true. This is in
25410 turn will cause DW_AT_inline attributes to be set.
25412 This happens because at early dwarf generation, there is no
25413 cgraph information, causing cgraph_function_possibly_inlined_p()
25414 to return true. Trick cgraph_function_possibly_inlined_p()
25415 while we generate dwarf early. */
25416 bool save = symtab->global_info_ready;
25417 symtab->global_info_ready = true;
25419 /* We don't handle TYPE_DECLs. If required, they'll be reached via
25420 other DECLs and they can point to template types or other things
25421 that dwarf2out can't handle when done via dwarf2out_decl. */
25422 if (TREE_CODE (decl) != TYPE_DECL
25423 && TREE_CODE (decl) != PARM_DECL)
25425 tree save_fndecl = current_function_decl;
25426 if (TREE_CODE (decl) == FUNCTION_DECL)
25428 /* No cfun means the symbol has no body, so there's nothing
25429 to emit. */
25430 if (!DECL_STRUCT_FUNCTION (decl))
25431 goto early_decl_exit;
25433 /* For nested functions, make sure we have DIEs for the parents first
25434 so that all nested DIEs are generated at the proper scope in the
25435 first shot. */
25436 tree context = decl_function_context (decl);
25437 if (context != NULL && lookup_decl_die (context) == NULL)
25439 current_function_decl = context;
25440 dwarf2out_decl (context);
25443 current_function_decl = decl;
25445 dwarf2out_decl (decl);
25446 if (TREE_CODE (decl) == FUNCTION_DECL)
25447 current_function_decl = save_fndecl;
25449 early_decl_exit:
25450 symtab->global_info_ready = save;
25453 /* Output debug information for global decl DECL. Called from
25454 toplev.c after compilation proper has finished. */
25456 static void
25457 dwarf2out_late_global_decl (tree decl)
25459 /* Fill-in any location information we were unable to determine
25460 on the first pass. */
25461 if (VAR_P (decl) && !POINTER_BOUNDS_P (decl))
25463 dw_die_ref die = lookup_decl_die (decl);
25465 /* We have to generate early debug late for LTO. */
25466 if (! die && in_lto_p)
25468 dwarf2out_decl (decl);
25469 die = lookup_decl_die (decl);
25472 if (die)
25474 /* We get called via the symtab code invoking late_global_decl
25475 for symbols that are optimized out. Do not add locations
25476 for those. */
25477 varpool_node *node = varpool_node::get (decl);
25478 if (! node || ! node->definition)
25479 tree_add_const_value_attribute_for_decl (die, decl);
25480 else
25481 add_location_or_const_value_attribute (die, decl, false);
25486 /* Output debug information for type decl DECL. Called from toplev.c
25487 and from language front ends (to record built-in types). */
25488 static void
25489 dwarf2out_type_decl (tree decl, int local)
25491 if (!local)
25493 set_early_dwarf s;
25494 dwarf2out_decl (decl);
25498 /* Output debug information for imported module or decl DECL.
25499 NAME is non-NULL name in the lexical block if the decl has been renamed.
25500 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
25501 that DECL belongs to.
25502 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
25503 static void
25504 dwarf2out_imported_module_or_decl_1 (tree decl,
25505 tree name,
25506 tree lexical_block,
25507 dw_die_ref lexical_block_die)
25509 expanded_location xloc;
25510 dw_die_ref imported_die = NULL;
25511 dw_die_ref at_import_die;
25513 if (TREE_CODE (decl) == IMPORTED_DECL)
25515 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
25516 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
25517 gcc_assert (decl);
25519 else
25520 xloc = expand_location (input_location);
25522 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
25524 at_import_die = force_type_die (TREE_TYPE (decl));
25525 /* For namespace N { typedef void T; } using N::T; base_type_die
25526 returns NULL, but DW_TAG_imported_declaration requires
25527 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
25528 if (!at_import_die)
25530 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
25531 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
25532 at_import_die = lookup_type_die (TREE_TYPE (decl));
25533 gcc_assert (at_import_die);
25536 else
25538 at_import_die = lookup_decl_die (decl);
25539 if (!at_import_die)
25541 /* If we're trying to avoid duplicate debug info, we may not have
25542 emitted the member decl for this field. Emit it now. */
25543 if (TREE_CODE (decl) == FIELD_DECL)
25545 tree type = DECL_CONTEXT (decl);
25547 if (TYPE_CONTEXT (type)
25548 && TYPE_P (TYPE_CONTEXT (type))
25549 && !should_emit_struct_debug (TYPE_CONTEXT (type),
25550 DINFO_USAGE_DIR_USE))
25551 return;
25552 gen_type_die_for_member (type, decl,
25553 get_context_die (TYPE_CONTEXT (type)));
25555 if (TREE_CODE (decl) == NAMELIST_DECL)
25556 at_import_die = gen_namelist_decl (DECL_NAME (decl),
25557 get_context_die (DECL_CONTEXT (decl)),
25558 NULL_TREE);
25559 else
25560 at_import_die = force_decl_die (decl);
25564 if (TREE_CODE (decl) == NAMESPACE_DECL)
25566 if (dwarf_version >= 3 || !dwarf_strict)
25567 imported_die = new_die (DW_TAG_imported_module,
25568 lexical_block_die,
25569 lexical_block);
25570 else
25571 return;
25573 else
25574 imported_die = new_die (DW_TAG_imported_declaration,
25575 lexical_block_die,
25576 lexical_block);
25578 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
25579 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
25580 if (debug_column_info && xloc.column)
25581 add_AT_unsigned (imported_die, DW_AT_decl_column, xloc.column);
25582 if (name)
25583 add_AT_string (imported_die, DW_AT_name,
25584 IDENTIFIER_POINTER (name));
25585 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
25588 /* Output debug information for imported module or decl DECL.
25589 NAME is non-NULL name in context if the decl has been renamed.
25590 CHILD is true if decl is one of the renamed decls as part of
25591 importing whole module. */
25593 static void
25594 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
25595 bool child)
25597 /* dw_die_ref at_import_die; */
25598 dw_die_ref scope_die;
25600 if (debug_info_level <= DINFO_LEVEL_TERSE)
25601 return;
25603 gcc_assert (decl);
25605 set_early_dwarf s;
25607 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
25608 We need decl DIE for reference and scope die. First, get DIE for the decl
25609 itself. */
25611 /* Get the scope die for decl context. Use comp_unit_die for global module
25612 or decl. If die is not found for non globals, force new die. */
25613 if (context
25614 && TYPE_P (context)
25615 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
25616 return;
25618 scope_die = get_context_die (context);
25620 if (child)
25622 /* DW_TAG_imported_module was introduced in the DWARFv3 specification, so
25623 there is nothing we can do, here. */
25624 if (dwarf_version < 3 && dwarf_strict)
25625 return;
25627 gcc_assert (scope_die->die_child);
25628 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
25629 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
25630 scope_die = scope_die->die_child;
25633 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
25634 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
25637 /* Output debug information for namelists. */
25639 static dw_die_ref
25640 gen_namelist_decl (tree name, dw_die_ref scope_die, tree item_decls)
25642 dw_die_ref nml_die, nml_item_die, nml_item_ref_die;
25643 tree value;
25644 unsigned i;
25646 if (debug_info_level <= DINFO_LEVEL_TERSE)
25647 return NULL;
25649 gcc_assert (scope_die != NULL);
25650 nml_die = new_die (DW_TAG_namelist, scope_die, NULL);
25651 add_AT_string (nml_die, DW_AT_name, IDENTIFIER_POINTER (name));
25653 /* If there are no item_decls, we have a nondefining namelist, e.g.
25654 with USE association; hence, set DW_AT_declaration. */
25655 if (item_decls == NULL_TREE)
25657 add_AT_flag (nml_die, DW_AT_declaration, 1);
25658 return nml_die;
25661 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (item_decls), i, value)
25663 nml_item_ref_die = lookup_decl_die (value);
25664 if (!nml_item_ref_die)
25665 nml_item_ref_die = force_decl_die (value);
25667 nml_item_die = new_die (DW_TAG_namelist_item, nml_die, NULL);
25668 add_AT_die_ref (nml_item_die, DW_AT_namelist_items, nml_item_ref_die);
25670 return nml_die;
25674 /* Write the debugging output for DECL and return the DIE. */
25676 static void
25677 dwarf2out_decl (tree decl)
25679 dw_die_ref context_die = comp_unit_die ();
25681 switch (TREE_CODE (decl))
25683 case ERROR_MARK:
25684 return;
25686 case FUNCTION_DECL:
25687 /* What we would really like to do here is to filter out all mere
25688 file-scope declarations of file-scope functions which are never
25689 referenced later within this translation unit (and keep all of ones
25690 that *are* referenced later on) but we aren't clairvoyant, so we have
25691 no idea which functions will be referenced in the future (i.e. later
25692 on within the current translation unit). So here we just ignore all
25693 file-scope function declarations which are not also definitions. If
25694 and when the debugger needs to know something about these functions,
25695 it will have to hunt around and find the DWARF information associated
25696 with the definition of the function.
25698 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
25699 nodes represent definitions and which ones represent mere
25700 declarations. We have to check DECL_INITIAL instead. That's because
25701 the C front-end supports some weird semantics for "extern inline"
25702 function definitions. These can get inlined within the current
25703 translation unit (and thus, we need to generate Dwarf info for their
25704 abstract instances so that the Dwarf info for the concrete inlined
25705 instances can have something to refer to) but the compiler never
25706 generates any out-of-lines instances of such things (despite the fact
25707 that they *are* definitions).
25709 The important point is that the C front-end marks these "extern
25710 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
25711 them anyway. Note that the C++ front-end also plays some similar games
25712 for inline function definitions appearing within include files which
25713 also contain `#pragma interface' pragmas.
25715 If we are called from dwarf2out_abstract_function output a DIE
25716 anyway. We can end up here this way with early inlining and LTO
25717 where the inlined function is output in a different LTRANS unit
25718 or not at all. */
25719 if (DECL_INITIAL (decl) == NULL_TREE
25720 && ! DECL_ABSTRACT_P (decl))
25721 return;
25723 /* If we're a nested function, initially use a parent of NULL; if we're
25724 a plain function, this will be fixed up in decls_for_scope. If
25725 we're a method, it will be ignored, since we already have a DIE. */
25726 if (decl_function_context (decl)
25727 /* But if we're in terse mode, we don't care about scope. */
25728 && debug_info_level > DINFO_LEVEL_TERSE)
25729 context_die = NULL;
25730 break;
25732 case VAR_DECL:
25733 /* For local statics lookup proper context die. */
25734 if (local_function_static (decl))
25735 context_die = lookup_decl_die (DECL_CONTEXT (decl));
25737 /* If we are in terse mode, don't generate any DIEs to represent any
25738 variable declarations or definitions. */
25739 if (debug_info_level <= DINFO_LEVEL_TERSE)
25740 return;
25741 break;
25743 case CONST_DECL:
25744 if (debug_info_level <= DINFO_LEVEL_TERSE)
25745 return;
25746 if (!is_fortran () && !is_ada ())
25747 return;
25748 if (TREE_STATIC (decl) && decl_function_context (decl))
25749 context_die = lookup_decl_die (DECL_CONTEXT (decl));
25750 break;
25752 case NAMESPACE_DECL:
25753 case IMPORTED_DECL:
25754 if (debug_info_level <= DINFO_LEVEL_TERSE)
25755 return;
25756 if (lookup_decl_die (decl) != NULL)
25757 return;
25758 break;
25760 case TYPE_DECL:
25761 /* Don't emit stubs for types unless they are needed by other DIEs. */
25762 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
25763 return;
25765 /* Don't bother trying to generate any DIEs to represent any of the
25766 normal built-in types for the language we are compiling. */
25767 if (DECL_IS_BUILTIN (decl))
25768 return;
25770 /* If we are in terse mode, don't generate any DIEs for types. */
25771 if (debug_info_level <= DINFO_LEVEL_TERSE)
25772 return;
25774 /* If we're a function-scope tag, initially use a parent of NULL;
25775 this will be fixed up in decls_for_scope. */
25776 if (decl_function_context (decl))
25777 context_die = NULL;
25779 break;
25781 case NAMELIST_DECL:
25782 break;
25784 default:
25785 return;
25788 gen_decl_die (decl, NULL, NULL, context_die);
25790 if (flag_checking)
25792 dw_die_ref die = lookup_decl_die (decl);
25793 if (die)
25794 check_die (die);
25798 /* Write the debugging output for DECL. */
25800 static void
25801 dwarf2out_function_decl (tree decl)
25803 dwarf2out_decl (decl);
25804 call_arg_locations = NULL;
25805 call_arg_loc_last = NULL;
25806 call_site_count = -1;
25807 tail_call_site_count = -1;
25808 decl_loc_table->empty ();
25809 cached_dw_loc_list_table->empty ();
25812 /* Output a marker (i.e. a label) for the beginning of the generated code for
25813 a lexical block. */
25815 static void
25816 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
25817 unsigned int blocknum)
25819 switch_to_section (current_function_section ());
25820 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
25823 /* Output a marker (i.e. a label) for the end of the generated code for a
25824 lexical block. */
25826 static void
25827 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
25829 switch_to_section (current_function_section ());
25830 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
25833 /* Returns nonzero if it is appropriate not to emit any debugging
25834 information for BLOCK, because it doesn't contain any instructions.
25836 Don't allow this for blocks with nested functions or local classes
25837 as we would end up with orphans, and in the presence of scheduling
25838 we may end up calling them anyway. */
25840 static bool
25841 dwarf2out_ignore_block (const_tree block)
25843 tree decl;
25844 unsigned int i;
25846 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
25847 if (TREE_CODE (decl) == FUNCTION_DECL
25848 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
25849 return 0;
25850 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
25852 decl = BLOCK_NONLOCALIZED_VAR (block, i);
25853 if (TREE_CODE (decl) == FUNCTION_DECL
25854 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
25855 return 0;
25858 return 1;
25861 /* Hash table routines for file_hash. */
25863 bool
25864 dwarf_file_hasher::equal (dwarf_file_data *p1, const char *p2)
25866 return filename_cmp (p1->filename, p2) == 0;
25869 hashval_t
25870 dwarf_file_hasher::hash (dwarf_file_data *p)
25872 return htab_hash_string (p->filename);
25875 /* Lookup FILE_NAME (in the list of filenames that we know about here in
25876 dwarf2out.c) and return its "index". The index of each (known) filename is
25877 just a unique number which is associated with only that one filename. We
25878 need such numbers for the sake of generating labels (in the .debug_sfnames
25879 section) and references to those files numbers (in the .debug_srcinfo
25880 and .debug_macinfo sections). If the filename given as an argument is not
25881 found in our current list, add it to the list and assign it the next
25882 available unique index number. */
25884 static struct dwarf_file_data *
25885 lookup_filename (const char *file_name)
25887 struct dwarf_file_data * created;
25889 if (!file_name)
25890 return NULL;
25892 dwarf_file_data **slot
25893 = file_table->find_slot_with_hash (file_name, htab_hash_string (file_name),
25894 INSERT);
25895 if (*slot)
25896 return *slot;
25898 created = ggc_alloc<dwarf_file_data> ();
25899 created->filename = file_name;
25900 created->emitted_number = 0;
25901 *slot = created;
25902 return created;
25905 /* If the assembler will construct the file table, then translate the compiler
25906 internal file table number into the assembler file table number, and emit
25907 a .file directive if we haven't already emitted one yet. The file table
25908 numbers are different because we prune debug info for unused variables and
25909 types, which may include filenames. */
25911 static int
25912 maybe_emit_file (struct dwarf_file_data * fd)
25914 if (! fd->emitted_number)
25916 if (last_emitted_file)
25917 fd->emitted_number = last_emitted_file->emitted_number + 1;
25918 else
25919 fd->emitted_number = 1;
25920 last_emitted_file = fd;
25922 if (DWARF2_ASM_LINE_DEBUG_INFO)
25924 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
25925 output_quoted_string (asm_out_file,
25926 remap_debug_filename (fd->filename));
25927 fputc ('\n', asm_out_file);
25931 return fd->emitted_number;
25934 /* Schedule generation of a DW_AT_const_value attribute to DIE.
25935 That generation should happen after function debug info has been
25936 generated. The value of the attribute is the constant value of ARG. */
25938 static void
25939 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
25941 die_arg_entry entry;
25943 if (!die || !arg)
25944 return;
25946 gcc_assert (early_dwarf);
25948 if (!tmpl_value_parm_die_table)
25949 vec_alloc (tmpl_value_parm_die_table, 32);
25951 entry.die = die;
25952 entry.arg = arg;
25953 vec_safe_push (tmpl_value_parm_die_table, entry);
25956 /* Return TRUE if T is an instance of generic type, FALSE
25957 otherwise. */
25959 static bool
25960 generic_type_p (tree t)
25962 if (t == NULL_TREE || !TYPE_P (t))
25963 return false;
25964 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
25967 /* Schedule the generation of the generic parameter dies for the
25968 instance of generic type T. The proper generation itself is later
25969 done by gen_scheduled_generic_parms_dies. */
25971 static void
25972 schedule_generic_params_dies_gen (tree t)
25974 if (!generic_type_p (t))
25975 return;
25977 gcc_assert (early_dwarf);
25979 if (!generic_type_instances)
25980 vec_alloc (generic_type_instances, 256);
25982 vec_safe_push (generic_type_instances, t);
25985 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
25986 by append_entry_to_tmpl_value_parm_die_table. This function must
25987 be called after function DIEs have been generated. */
25989 static void
25990 gen_remaining_tmpl_value_param_die_attribute (void)
25992 if (tmpl_value_parm_die_table)
25994 unsigned i, j;
25995 die_arg_entry *e;
25997 /* We do this in two phases - first get the cases we can
25998 handle during early-finish, preserving those we cannot
25999 (containing symbolic constants where we don't yet know
26000 whether we are going to output the referenced symbols).
26001 For those we try again at late-finish. */
26002 j = 0;
26003 FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
26005 if (!tree_add_const_value_attribute (e->die, e->arg))
26007 dw_loc_descr_ref loc = NULL;
26008 if (! early_dwarf
26009 && (dwarf_version >= 5 || !dwarf_strict))
26010 loc = loc_descriptor_from_tree (e->arg, 2, NULL);
26011 if (loc)
26012 add_AT_loc (e->die, DW_AT_location, loc);
26013 else
26014 (*tmpl_value_parm_die_table)[j++] = *e;
26017 tmpl_value_parm_die_table->truncate (j);
26021 /* Generate generic parameters DIEs for instances of generic types
26022 that have been previously scheduled by
26023 schedule_generic_params_dies_gen. This function must be called
26024 after all the types of the CU have been laid out. */
26026 static void
26027 gen_scheduled_generic_parms_dies (void)
26029 unsigned i;
26030 tree t;
26032 if (!generic_type_instances)
26033 return;
26035 FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
26036 if (COMPLETE_TYPE_P (t))
26037 gen_generic_params_dies (t);
26039 generic_type_instances = NULL;
26043 /* Replace DW_AT_name for the decl with name. */
26045 static void
26046 dwarf2out_set_name (tree decl, tree name)
26048 dw_die_ref die;
26049 dw_attr_node *attr;
26050 const char *dname;
26052 die = TYPE_SYMTAB_DIE (decl);
26053 if (!die)
26054 return;
26056 dname = dwarf2_name (name, 0);
26057 if (!dname)
26058 return;
26060 attr = get_AT (die, DW_AT_name);
26061 if (attr)
26063 struct indirect_string_node *node;
26065 node = find_AT_string (dname);
26066 /* replace the string. */
26067 attr->dw_attr_val.v.val_str = node;
26070 else
26071 add_name_attribute (die, dname);
26074 /* True if before or during processing of the first function being emitted. */
26075 static bool in_first_function_p = true;
26076 /* True if loc_note during dwarf2out_var_location call might still be
26077 before first real instruction at address equal to .Ltext0. */
26078 static bool maybe_at_text_label_p = true;
26079 /* One above highest N where .LVLN label might be equal to .Ltext0 label. */
26080 static unsigned int first_loclabel_num_not_at_text_label;
26082 /* Called by the final INSN scan whenever we see a var location. We
26083 use it to drop labels in the right places, and throw the location in
26084 our lookup table. */
26086 static void
26087 dwarf2out_var_location (rtx_insn *loc_note)
26089 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
26090 struct var_loc_node *newloc;
26091 rtx_insn *next_real, *next_note;
26092 rtx_insn *call_insn = NULL;
26093 static const char *last_label;
26094 static const char *last_postcall_label;
26095 static bool last_in_cold_section_p;
26096 static rtx_insn *expected_next_loc_note;
26097 tree decl;
26098 bool var_loc_p;
26100 if (!NOTE_P (loc_note))
26102 if (CALL_P (loc_note))
26104 call_site_count++;
26105 if (SIBLING_CALL_P (loc_note))
26106 tail_call_site_count++;
26107 if (optimize == 0 && !flag_var_tracking)
26109 /* When the var-tracking pass is not running, there is no note
26110 for indirect calls whose target is compile-time known. In this
26111 case, process such calls specifically so that we generate call
26112 sites for them anyway. */
26113 rtx x = PATTERN (loc_note);
26114 if (GET_CODE (x) == PARALLEL)
26115 x = XVECEXP (x, 0, 0);
26116 if (GET_CODE (x) == SET)
26117 x = SET_SRC (x);
26118 if (GET_CODE (x) == CALL)
26119 x = XEXP (x, 0);
26120 if (!MEM_P (x)
26121 || GET_CODE (XEXP (x, 0)) != SYMBOL_REF
26122 || !SYMBOL_REF_DECL (XEXP (x, 0))
26123 || (TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0)))
26124 != FUNCTION_DECL))
26126 call_insn = loc_note;
26127 loc_note = NULL;
26128 var_loc_p = false;
26130 next_real = next_real_insn (call_insn);
26131 next_note = NULL;
26132 cached_next_real_insn = NULL;
26133 goto create_label;
26137 return;
26140 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
26141 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
26142 return;
26144 /* Optimize processing a large consecutive sequence of location
26145 notes so we don't spend too much time in next_real_insn. If the
26146 next insn is another location note, remember the next_real_insn
26147 calculation for next time. */
26148 next_real = cached_next_real_insn;
26149 if (next_real)
26151 if (expected_next_loc_note != loc_note)
26152 next_real = NULL;
26155 next_note = NEXT_INSN (loc_note);
26156 if (! next_note
26157 || next_note->deleted ()
26158 || ! NOTE_P (next_note)
26159 || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
26160 && NOTE_KIND (next_note) != NOTE_INSN_CALL_ARG_LOCATION))
26161 next_note = NULL;
26163 if (! next_real)
26164 next_real = next_real_insn (loc_note);
26166 if (next_note)
26168 expected_next_loc_note = next_note;
26169 cached_next_real_insn = next_real;
26171 else
26172 cached_next_real_insn = NULL;
26174 /* If there are no instructions which would be affected by this note,
26175 don't do anything. */
26176 if (var_loc_p
26177 && next_real == NULL_RTX
26178 && !NOTE_DURING_CALL_P (loc_note))
26179 return;
26181 create_label:
26183 if (next_real == NULL_RTX)
26184 next_real = get_last_insn ();
26186 /* If there were any real insns between note we processed last time
26187 and this note (or if it is the first note), clear
26188 last_{,postcall_}label so that they are not reused this time. */
26189 if (last_var_location_insn == NULL_RTX
26190 || last_var_location_insn != next_real
26191 || last_in_cold_section_p != in_cold_section_p)
26193 last_label = NULL;
26194 last_postcall_label = NULL;
26197 if (var_loc_p)
26199 decl = NOTE_VAR_LOCATION_DECL (loc_note);
26200 newloc = add_var_loc_to_decl (decl, loc_note,
26201 NOTE_DURING_CALL_P (loc_note)
26202 ? last_postcall_label : last_label);
26203 if (newloc == NULL)
26204 return;
26206 else
26208 decl = NULL_TREE;
26209 newloc = NULL;
26212 /* If there were no real insns between note we processed last time
26213 and this note, use the label we emitted last time. Otherwise
26214 create a new label and emit it. */
26215 if (last_label == NULL)
26217 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
26218 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
26219 loclabel_num++;
26220 last_label = ggc_strdup (loclabel);
26221 /* See if loclabel might be equal to .Ltext0. If yes,
26222 bump first_loclabel_num_not_at_text_label. */
26223 if (!have_multiple_function_sections
26224 && in_first_function_p
26225 && maybe_at_text_label_p)
26227 static rtx_insn *last_start;
26228 rtx_insn *insn;
26229 for (insn = loc_note; insn; insn = previous_insn (insn))
26230 if (insn == last_start)
26231 break;
26232 else if (!NONDEBUG_INSN_P (insn))
26233 continue;
26234 else
26236 rtx body = PATTERN (insn);
26237 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
26238 continue;
26239 /* Inline asm could occupy zero bytes. */
26240 else if (GET_CODE (body) == ASM_INPUT
26241 || asm_noperands (body) >= 0)
26242 continue;
26243 #ifdef HAVE_attr_length
26244 else if (get_attr_min_length (insn) == 0)
26245 continue;
26246 #endif
26247 else
26249 /* Assume insn has non-zero length. */
26250 maybe_at_text_label_p = false;
26251 break;
26254 if (maybe_at_text_label_p)
26256 last_start = loc_note;
26257 first_loclabel_num_not_at_text_label = loclabel_num;
26262 gcc_assert ((loc_note == NULL_RTX && call_insn != NULL_RTX)
26263 || (loc_note != NULL_RTX && call_insn == NULL_RTX));
26265 if (!var_loc_p)
26267 struct call_arg_loc_node *ca_loc
26268 = ggc_cleared_alloc<call_arg_loc_node> ();
26269 rtx_insn *prev
26270 = loc_note != NULL_RTX ? prev_real_insn (loc_note) : call_insn;
26272 ca_loc->call_arg_loc_note = loc_note;
26273 ca_loc->next = NULL;
26274 ca_loc->label = last_label;
26275 gcc_assert (prev
26276 && (CALL_P (prev)
26277 || (NONJUMP_INSN_P (prev)
26278 && GET_CODE (PATTERN (prev)) == SEQUENCE
26279 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
26280 if (!CALL_P (prev))
26281 prev = as_a <rtx_sequence *> (PATTERN (prev))->insn (0);
26282 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
26284 /* Look for a SYMBOL_REF in the "prev" instruction. */
26285 rtx x = get_call_rtx_from (PATTERN (prev));
26286 if (x)
26288 /* Try to get the call symbol, if any. */
26289 if (MEM_P (XEXP (x, 0)))
26290 x = XEXP (x, 0);
26291 /* First, look for a memory access to a symbol_ref. */
26292 if (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
26293 && SYMBOL_REF_DECL (XEXP (x, 0))
26294 && TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0))) == FUNCTION_DECL)
26295 ca_loc->symbol_ref = XEXP (x, 0);
26296 /* Otherwise, look at a compile-time known user-level function
26297 declaration. */
26298 else if (MEM_P (x)
26299 && MEM_EXPR (x)
26300 && TREE_CODE (MEM_EXPR (x)) == FUNCTION_DECL)
26301 ca_loc->symbol_ref = XEXP (DECL_RTL (MEM_EXPR (x)), 0);
26304 ca_loc->block = insn_scope (prev);
26305 if (call_arg_locations)
26306 call_arg_loc_last->next = ca_loc;
26307 else
26308 call_arg_locations = ca_loc;
26309 call_arg_loc_last = ca_loc;
26311 else if (loc_note != NULL_RTX && !NOTE_DURING_CALL_P (loc_note))
26312 newloc->label = last_label;
26313 else
26315 if (!last_postcall_label)
26317 sprintf (loclabel, "%s-1", last_label);
26318 last_postcall_label = ggc_strdup (loclabel);
26320 newloc->label = last_postcall_label;
26323 last_var_location_insn = next_real;
26324 last_in_cold_section_p = in_cold_section_p;
26327 /* Called from finalize_size_functions for size functions so that their body
26328 can be encoded in the debug info to describe the layout of variable-length
26329 structures. */
26331 static void
26332 dwarf2out_size_function (tree decl)
26334 function_to_dwarf_procedure (decl);
26337 /* Note in one location list that text section has changed. */
26340 var_location_switch_text_section_1 (var_loc_list **slot, void *)
26342 var_loc_list *list = *slot;
26343 if (list->first)
26344 list->last_before_switch
26345 = list->last->next ? list->last->next : list->last;
26346 return 1;
26349 /* Note in all location lists that text section has changed. */
26351 static void
26352 var_location_switch_text_section (void)
26354 if (decl_loc_table == NULL)
26355 return;
26357 decl_loc_table->traverse<void *, var_location_switch_text_section_1> (NULL);
26360 /* Create a new line number table. */
26362 static dw_line_info_table *
26363 new_line_info_table (void)
26365 dw_line_info_table *table;
26367 table = ggc_cleared_alloc<dw_line_info_table> ();
26368 table->file_num = 1;
26369 table->line_num = 1;
26370 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
26372 return table;
26375 /* Lookup the "current" table into which we emit line info, so
26376 that we don't have to do it for every source line. */
26378 static void
26379 set_cur_line_info_table (section *sec)
26381 dw_line_info_table *table;
26383 if (sec == text_section)
26384 table = text_section_line_info;
26385 else if (sec == cold_text_section)
26387 table = cold_text_section_line_info;
26388 if (!table)
26390 cold_text_section_line_info = table = new_line_info_table ();
26391 table->end_label = cold_end_label;
26394 else
26396 const char *end_label;
26398 if (flag_reorder_blocks_and_partition)
26400 if (in_cold_section_p)
26401 end_label = crtl->subsections.cold_section_end_label;
26402 else
26403 end_label = crtl->subsections.hot_section_end_label;
26405 else
26407 char label[MAX_ARTIFICIAL_LABEL_BYTES];
26408 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
26409 current_function_funcdef_no);
26410 end_label = ggc_strdup (label);
26413 table = new_line_info_table ();
26414 table->end_label = end_label;
26416 vec_safe_push (separate_line_info, table);
26419 if (DWARF2_ASM_LINE_DEBUG_INFO)
26420 table->is_stmt = (cur_line_info_table
26421 ? cur_line_info_table->is_stmt
26422 : DWARF_LINE_DEFAULT_IS_STMT_START);
26423 cur_line_info_table = table;
26427 /* We need to reset the locations at the beginning of each
26428 function. We can't do this in the end_function hook, because the
26429 declarations that use the locations won't have been output when
26430 that hook is called. Also compute have_multiple_function_sections here. */
26432 static void
26433 dwarf2out_begin_function (tree fun)
26435 section *sec = function_section (fun);
26437 if (sec != text_section)
26438 have_multiple_function_sections = true;
26440 if (flag_reorder_blocks_and_partition && !cold_text_section)
26442 gcc_assert (current_function_decl == fun);
26443 cold_text_section = unlikely_text_section ();
26444 switch_to_section (cold_text_section);
26445 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
26446 switch_to_section (sec);
26449 dwarf2out_note_section_used ();
26450 call_site_count = 0;
26451 tail_call_site_count = 0;
26453 set_cur_line_info_table (sec);
26456 /* Helper function of dwarf2out_end_function, called only after emitting
26457 the very first function into assembly. Check if some .debug_loc range
26458 might end with a .LVL* label that could be equal to .Ltext0.
26459 In that case we must force using absolute addresses in .debug_loc ranges,
26460 because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
26461 .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
26462 list terminator.
26463 Set have_multiple_function_sections to true in that case and
26464 terminate htab traversal. */
26467 find_empty_loc_ranges_at_text_label (var_loc_list **slot, int)
26469 var_loc_list *entry = *slot;
26470 struct var_loc_node *node;
26472 node = entry->first;
26473 if (node && node->next && node->next->label)
26475 unsigned int i;
26476 const char *label = node->next->label;
26477 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
26479 for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
26481 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
26482 if (strcmp (label, loclabel) == 0)
26484 have_multiple_function_sections = true;
26485 return 0;
26489 return 1;
26492 /* Hook called after emitting a function into assembly.
26493 This does something only for the very first function emitted. */
26495 static void
26496 dwarf2out_end_function (unsigned int)
26498 if (in_first_function_p
26499 && !have_multiple_function_sections
26500 && first_loclabel_num_not_at_text_label
26501 && decl_loc_table)
26502 decl_loc_table->traverse<int, find_empty_loc_ranges_at_text_label> (0);
26503 in_first_function_p = false;
26504 maybe_at_text_label_p = false;
26507 /* Temporary holder for dwarf2out_register_main_translation_unit. Used to let
26508 front-ends register a translation unit even before dwarf2out_init is
26509 called. */
26510 static tree main_translation_unit = NULL_TREE;
26512 /* Hook called by front-ends after they built their main translation unit.
26513 Associate comp_unit_die to UNIT. */
26515 static void
26516 dwarf2out_register_main_translation_unit (tree unit)
26518 gcc_assert (TREE_CODE (unit) == TRANSLATION_UNIT_DECL
26519 && main_translation_unit == NULL_TREE);
26520 main_translation_unit = unit;
26521 /* If dwarf2out_init has not been called yet, it will perform the association
26522 itself looking at main_translation_unit. */
26523 if (decl_die_table != NULL)
26524 equate_decl_number_to_die (unit, comp_unit_die ());
26527 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
26529 static void
26530 push_dw_line_info_entry (dw_line_info_table *table,
26531 enum dw_line_info_opcode opcode, unsigned int val)
26533 dw_line_info_entry e;
26534 e.opcode = opcode;
26535 e.val = val;
26536 vec_safe_push (table->entries, e);
26539 /* Output a label to mark the beginning of a source code line entry
26540 and record information relating to this source line, in
26541 'line_info_table' for later output of the .debug_line section. */
26542 /* ??? The discriminator parameter ought to be unsigned. */
26544 static void
26545 dwarf2out_source_line (unsigned int line, unsigned int column,
26546 const char *filename,
26547 int discriminator, bool is_stmt)
26549 unsigned int file_num;
26550 dw_line_info_table *table;
26552 if (debug_info_level < DINFO_LEVEL_TERSE || line == 0)
26553 return;
26555 /* The discriminator column was added in dwarf4. Simplify the below
26556 by simply removing it if we're not supposed to output it. */
26557 if (dwarf_version < 4 && dwarf_strict)
26558 discriminator = 0;
26560 if (!debug_column_info)
26561 column = 0;
26563 table = cur_line_info_table;
26564 file_num = maybe_emit_file (lookup_filename (filename));
26566 /* ??? TODO: Elide duplicate line number entries. Traditionally,
26567 the debugger has used the second (possibly duplicate) line number
26568 at the beginning of the function to mark the end of the prologue.
26569 We could eliminate any other duplicates within the function. For
26570 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
26571 that second line number entry. */
26572 /* Recall that this end-of-prologue indication is *not* the same thing
26573 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
26574 to which the hook corresponds, follows the last insn that was
26575 emitted by gen_prologue. What we need is to precede the first insn
26576 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
26577 insn that corresponds to something the user wrote. These may be
26578 very different locations once scheduling is enabled. */
26580 if (0 && file_num == table->file_num
26581 && line == table->line_num
26582 && column == table->column_num
26583 && discriminator == table->discrim_num
26584 && is_stmt == table->is_stmt)
26585 return;
26587 switch_to_section (current_function_section ());
26589 /* If requested, emit something human-readable. */
26590 if (flag_debug_asm)
26592 if (debug_column_info)
26593 fprintf (asm_out_file, "\t%s %s:%d:%d\n", ASM_COMMENT_START,
26594 filename, line, column);
26595 else
26596 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
26597 filename, line);
26600 if (DWARF2_ASM_LINE_DEBUG_INFO)
26602 /* Emit the .loc directive understood by GNU as. */
26603 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
26604 file_num, line, is_stmt, discriminator */
26605 fputs ("\t.loc ", asm_out_file);
26606 fprint_ul (asm_out_file, file_num);
26607 putc (' ', asm_out_file);
26608 fprint_ul (asm_out_file, line);
26609 putc (' ', asm_out_file);
26610 if (debug_column_info)
26611 fprint_ul (asm_out_file, column);
26612 else
26613 putc ('0', asm_out_file);
26615 if (is_stmt != table->is_stmt)
26617 fputs (" is_stmt ", asm_out_file);
26618 putc (is_stmt ? '1' : '0', asm_out_file);
26620 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
26622 gcc_assert (discriminator > 0);
26623 fputs (" discriminator ", asm_out_file);
26624 fprint_ul (asm_out_file, (unsigned long) discriminator);
26626 putc ('\n', asm_out_file);
26628 else
26630 unsigned int label_num = ++line_info_label_num;
26632 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
26634 push_dw_line_info_entry (table, LI_set_address, label_num);
26635 if (file_num != table->file_num)
26636 push_dw_line_info_entry (table, LI_set_file, file_num);
26637 if (discriminator != table->discrim_num)
26638 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
26639 if (is_stmt != table->is_stmt)
26640 push_dw_line_info_entry (table, LI_negate_stmt, 0);
26641 push_dw_line_info_entry (table, LI_set_line, line);
26642 if (debug_column_info)
26643 push_dw_line_info_entry (table, LI_set_column, column);
26646 table->file_num = file_num;
26647 table->line_num = line;
26648 table->column_num = column;
26649 table->discrim_num = discriminator;
26650 table->is_stmt = is_stmt;
26651 table->in_use = true;
26654 /* Record the beginning of a new source file. */
26656 static void
26657 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
26659 if (flag_eliminate_dwarf2_dups)
26661 /* Record the beginning of the file for break_out_includes. */
26662 dw_die_ref bincl_die;
26664 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
26665 add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
26668 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
26670 macinfo_entry e;
26671 e.code = DW_MACINFO_start_file;
26672 e.lineno = lineno;
26673 e.info = ggc_strdup (filename);
26674 vec_safe_push (macinfo_table, e);
26678 /* Record the end of a source file. */
26680 static void
26681 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
26683 if (flag_eliminate_dwarf2_dups)
26684 /* Record the end of the file for break_out_includes. */
26685 new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
26687 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
26689 macinfo_entry e;
26690 e.code = DW_MACINFO_end_file;
26691 e.lineno = lineno;
26692 e.info = NULL;
26693 vec_safe_push (macinfo_table, e);
26697 /* Called from debug_define in toplev.c. The `buffer' parameter contains
26698 the tail part of the directive line, i.e. the part which is past the
26699 initial whitespace, #, whitespace, directive-name, whitespace part. */
26701 static void
26702 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
26703 const char *buffer ATTRIBUTE_UNUSED)
26705 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
26707 macinfo_entry e;
26708 /* Insert a dummy first entry to be able to optimize the whole
26709 predefined macro block using DW_MACRO_import. */
26710 if (macinfo_table->is_empty () && lineno <= 1)
26712 e.code = 0;
26713 e.lineno = 0;
26714 e.info = NULL;
26715 vec_safe_push (macinfo_table, e);
26717 e.code = DW_MACINFO_define;
26718 e.lineno = lineno;
26719 e.info = ggc_strdup (buffer);
26720 vec_safe_push (macinfo_table, e);
26724 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
26725 the tail part of the directive line, i.e. the part which is past the
26726 initial whitespace, #, whitespace, directive-name, whitespace part. */
26728 static void
26729 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
26730 const char *buffer ATTRIBUTE_UNUSED)
26732 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
26734 macinfo_entry e;
26735 /* Insert a dummy first entry to be able to optimize the whole
26736 predefined macro block using DW_MACRO_import. */
26737 if (macinfo_table->is_empty () && lineno <= 1)
26739 e.code = 0;
26740 e.lineno = 0;
26741 e.info = NULL;
26742 vec_safe_push (macinfo_table, e);
26744 e.code = DW_MACINFO_undef;
26745 e.lineno = lineno;
26746 e.info = ggc_strdup (buffer);
26747 vec_safe_push (macinfo_table, e);
26751 /* Helpers to manipulate hash table of CUs. */
26753 struct macinfo_entry_hasher : nofree_ptr_hash <macinfo_entry>
26755 static inline hashval_t hash (const macinfo_entry *);
26756 static inline bool equal (const macinfo_entry *, const macinfo_entry *);
26759 inline hashval_t
26760 macinfo_entry_hasher::hash (const macinfo_entry *entry)
26762 return htab_hash_string (entry->info);
26765 inline bool
26766 macinfo_entry_hasher::equal (const macinfo_entry *entry1,
26767 const macinfo_entry *entry2)
26769 return !strcmp (entry1->info, entry2->info);
26772 typedef hash_table<macinfo_entry_hasher> macinfo_hash_type;
26774 /* Output a single .debug_macinfo entry. */
26776 static void
26777 output_macinfo_op (macinfo_entry *ref)
26779 int file_num;
26780 size_t len;
26781 struct indirect_string_node *node;
26782 char label[MAX_ARTIFICIAL_LABEL_BYTES];
26783 struct dwarf_file_data *fd;
26785 switch (ref->code)
26787 case DW_MACINFO_start_file:
26788 fd = lookup_filename (ref->info);
26789 file_num = maybe_emit_file (fd);
26790 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
26791 dw2_asm_output_data_uleb128 (ref->lineno,
26792 "Included from line number %lu",
26793 (unsigned long) ref->lineno);
26794 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
26795 break;
26796 case DW_MACINFO_end_file:
26797 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
26798 break;
26799 case DW_MACINFO_define:
26800 case DW_MACINFO_undef:
26801 len = strlen (ref->info) + 1;
26802 if (!dwarf_strict
26803 && len > DWARF_OFFSET_SIZE
26804 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
26805 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
26807 ref->code = ref->code == DW_MACINFO_define
26808 ? DW_MACRO_define_strp : DW_MACRO_undef_strp;
26809 output_macinfo_op (ref);
26810 return;
26812 dw2_asm_output_data (1, ref->code,
26813 ref->code == DW_MACINFO_define
26814 ? "Define macro" : "Undefine macro");
26815 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
26816 (unsigned long) ref->lineno);
26817 dw2_asm_output_nstring (ref->info, -1, "The macro");
26818 break;
26819 case DW_MACRO_define_strp:
26820 case DW_MACRO_undef_strp:
26821 node = find_AT_string (ref->info);
26822 gcc_assert (node
26823 && (node->form == DW_FORM_strp
26824 || node->form == DW_FORM_GNU_str_index));
26825 dw2_asm_output_data (1, ref->code,
26826 ref->code == DW_MACRO_define_strp
26827 ? "Define macro strp"
26828 : "Undefine macro strp");
26829 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
26830 (unsigned long) ref->lineno);
26831 if (node->form == DW_FORM_strp)
26832 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
26833 debug_str_section, "The macro: \"%s\"",
26834 ref->info);
26835 else
26836 dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
26837 ref->info);
26838 break;
26839 case DW_MACRO_import:
26840 dw2_asm_output_data (1, ref->code, "Import");
26841 ASM_GENERATE_INTERNAL_LABEL (label,
26842 DEBUG_MACRO_SECTION_LABEL, ref->lineno);
26843 dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
26844 break;
26845 default:
26846 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
26847 ASM_COMMENT_START, (unsigned long) ref->code);
26848 break;
26852 /* Attempt to make a sequence of define/undef macinfo ops shareable with
26853 other compilation unit .debug_macinfo sections. IDX is the first
26854 index of a define/undef, return the number of ops that should be
26855 emitted in a comdat .debug_macinfo section and emit
26856 a DW_MACRO_import entry referencing it.
26857 If the define/undef entry should be emitted normally, return 0. */
26859 static unsigned
26860 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
26861 macinfo_hash_type **macinfo_htab)
26863 macinfo_entry *first, *second, *cur, *inc;
26864 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
26865 unsigned char checksum[16];
26866 struct md5_ctx ctx;
26867 char *grp_name, *tail;
26868 const char *base;
26869 unsigned int i, count, encoded_filename_len, linebuf_len;
26870 macinfo_entry **slot;
26872 first = &(*macinfo_table)[idx];
26873 second = &(*macinfo_table)[idx + 1];
26875 /* Optimize only if there are at least two consecutive define/undef ops,
26876 and either all of them are before first DW_MACINFO_start_file
26877 with lineno {0,1} (i.e. predefined macro block), or all of them are
26878 in some included header file. */
26879 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
26880 return 0;
26881 if (vec_safe_is_empty (files))
26883 if (first->lineno > 1 || second->lineno > 1)
26884 return 0;
26886 else if (first->lineno == 0)
26887 return 0;
26889 /* Find the last define/undef entry that can be grouped together
26890 with first and at the same time compute md5 checksum of their
26891 codes, linenumbers and strings. */
26892 md5_init_ctx (&ctx);
26893 for (i = idx; macinfo_table->iterate (i, &cur); i++)
26894 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
26895 break;
26896 else if (vec_safe_is_empty (files) && cur->lineno > 1)
26897 break;
26898 else
26900 unsigned char code = cur->code;
26901 md5_process_bytes (&code, 1, &ctx);
26902 checksum_uleb128 (cur->lineno, &ctx);
26903 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
26905 md5_finish_ctx (&ctx, checksum);
26906 count = i - idx;
26908 /* From the containing include filename (if any) pick up just
26909 usable characters from its basename. */
26910 if (vec_safe_is_empty (files))
26911 base = "";
26912 else
26913 base = lbasename (files->last ().info);
26914 for (encoded_filename_len = 0, i = 0; base[i]; i++)
26915 if (ISIDNUM (base[i]) || base[i] == '.')
26916 encoded_filename_len++;
26917 /* Count . at the end. */
26918 if (encoded_filename_len)
26919 encoded_filename_len++;
26921 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
26922 linebuf_len = strlen (linebuf);
26924 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
26925 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
26926 + 16 * 2 + 1);
26927 memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
26928 tail = grp_name + 4;
26929 if (encoded_filename_len)
26931 for (i = 0; base[i]; i++)
26932 if (ISIDNUM (base[i]) || base[i] == '.')
26933 *tail++ = base[i];
26934 *tail++ = '.';
26936 memcpy (tail, linebuf, linebuf_len);
26937 tail += linebuf_len;
26938 *tail++ = '.';
26939 for (i = 0; i < 16; i++)
26940 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
26942 /* Construct a macinfo_entry for DW_MACRO_import
26943 in the empty vector entry before the first define/undef. */
26944 inc = &(*macinfo_table)[idx - 1];
26945 inc->code = DW_MACRO_import;
26946 inc->lineno = 0;
26947 inc->info = ggc_strdup (grp_name);
26948 if (!*macinfo_htab)
26949 *macinfo_htab = new macinfo_hash_type (10);
26950 /* Avoid emitting duplicates. */
26951 slot = (*macinfo_htab)->find_slot (inc, INSERT);
26952 if (*slot != NULL)
26954 inc->code = 0;
26955 inc->info = NULL;
26956 /* If such an entry has been used before, just emit
26957 a DW_MACRO_import op. */
26958 inc = *slot;
26959 output_macinfo_op (inc);
26960 /* And clear all macinfo_entry in the range to avoid emitting them
26961 in the second pass. */
26962 for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
26964 cur->code = 0;
26965 cur->info = NULL;
26968 else
26970 *slot = inc;
26971 inc->lineno = (*macinfo_htab)->elements ();
26972 output_macinfo_op (inc);
26974 return count;
26977 /* Save any strings needed by the macinfo table in the debug str
26978 table. All strings must be collected into the table by the time
26979 index_string is called. */
26981 static void
26982 save_macinfo_strings (void)
26984 unsigned len;
26985 unsigned i;
26986 macinfo_entry *ref;
26988 for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
26990 switch (ref->code)
26992 /* Match the logic in output_macinfo_op to decide on
26993 indirect strings. */
26994 case DW_MACINFO_define:
26995 case DW_MACINFO_undef:
26996 len = strlen (ref->info) + 1;
26997 if (!dwarf_strict
26998 && len > DWARF_OFFSET_SIZE
26999 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
27000 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
27001 set_indirect_string (find_AT_string (ref->info));
27002 break;
27003 case DW_MACRO_define_strp:
27004 case DW_MACRO_undef_strp:
27005 set_indirect_string (find_AT_string (ref->info));
27006 break;
27007 default:
27008 break;
27013 /* Output macinfo section(s). */
27015 static void
27016 output_macinfo (void)
27018 unsigned i;
27019 unsigned long length = vec_safe_length (macinfo_table);
27020 macinfo_entry *ref;
27021 vec<macinfo_entry, va_gc> *files = NULL;
27022 macinfo_hash_type *macinfo_htab = NULL;
27024 if (! length)
27025 return;
27027 /* output_macinfo* uses these interchangeably. */
27028 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_define
27029 && (int) DW_MACINFO_undef == (int) DW_MACRO_undef
27030 && (int) DW_MACINFO_start_file == (int) DW_MACRO_start_file
27031 && (int) DW_MACINFO_end_file == (int) DW_MACRO_end_file);
27033 /* For .debug_macro emit the section header. */
27034 if (!dwarf_strict || dwarf_version >= 5)
27036 dw2_asm_output_data (2, dwarf_version >= 5 ? 5 : 4,
27037 "DWARF macro version number");
27038 if (DWARF_OFFSET_SIZE == 8)
27039 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
27040 else
27041 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
27042 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
27043 (!dwarf_split_debug_info ? debug_line_section_label
27044 : debug_skeleton_line_section_label),
27045 debug_line_section, NULL);
27048 /* In the first loop, it emits the primary .debug_macinfo section
27049 and after each emitted op the macinfo_entry is cleared.
27050 If a longer range of define/undef ops can be optimized using
27051 DW_MACRO_import, the DW_MACRO_import op is emitted and kept in
27052 the vector before the first define/undef in the range and the
27053 whole range of define/undef ops is not emitted and kept. */
27054 for (i = 0; macinfo_table->iterate (i, &ref); i++)
27056 switch (ref->code)
27058 case DW_MACINFO_start_file:
27059 vec_safe_push (files, *ref);
27060 break;
27061 case DW_MACINFO_end_file:
27062 if (!vec_safe_is_empty (files))
27063 files->pop ();
27064 break;
27065 case DW_MACINFO_define:
27066 case DW_MACINFO_undef:
27067 if ((!dwarf_strict || dwarf_version >= 5)
27068 && HAVE_COMDAT_GROUP
27069 && vec_safe_length (files) != 1
27070 && i > 0
27071 && i + 1 < length
27072 && (*macinfo_table)[i - 1].code == 0)
27074 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
27075 if (count)
27077 i += count - 1;
27078 continue;
27081 break;
27082 case 0:
27083 /* A dummy entry may be inserted at the beginning to be able
27084 to optimize the whole block of predefined macros. */
27085 if (i == 0)
27086 continue;
27087 default:
27088 break;
27090 output_macinfo_op (ref);
27091 ref->info = NULL;
27092 ref->code = 0;
27095 if (!macinfo_htab)
27096 return;
27098 delete macinfo_htab;
27099 macinfo_htab = NULL;
27101 /* If any DW_MACRO_import were used, on those DW_MACRO_import entries
27102 terminate the current chain and switch to a new comdat .debug_macinfo
27103 section and emit the define/undef entries within it. */
27104 for (i = 0; macinfo_table->iterate (i, &ref); i++)
27105 switch (ref->code)
27107 case 0:
27108 continue;
27109 case DW_MACRO_import:
27111 char label[MAX_ARTIFICIAL_LABEL_BYTES];
27112 tree comdat_key = get_identifier (ref->info);
27113 /* Terminate the previous .debug_macinfo section. */
27114 dw2_asm_output_data (1, 0, "End compilation unit");
27115 targetm.asm_out.named_section (debug_macinfo_section_name,
27116 SECTION_DEBUG
27117 | SECTION_LINKONCE,
27118 comdat_key);
27119 ASM_GENERATE_INTERNAL_LABEL (label,
27120 DEBUG_MACRO_SECTION_LABEL,
27121 ref->lineno);
27122 ASM_OUTPUT_LABEL (asm_out_file, label);
27123 ref->code = 0;
27124 ref->info = NULL;
27125 dw2_asm_output_data (2, dwarf_version >= 5 ? 5 : 4,
27126 "DWARF macro version number");
27127 if (DWARF_OFFSET_SIZE == 8)
27128 dw2_asm_output_data (1, 1, "Flags: 64-bit");
27129 else
27130 dw2_asm_output_data (1, 0, "Flags: 32-bit");
27132 break;
27133 case DW_MACINFO_define:
27134 case DW_MACINFO_undef:
27135 output_macinfo_op (ref);
27136 ref->code = 0;
27137 ref->info = NULL;
27138 break;
27139 default:
27140 gcc_unreachable ();
27144 /* Initialize the various sections and labels for dwarf output. */
27146 static void
27147 init_sections_and_labels (void)
27149 if (!dwarf_split_debug_info)
27151 debug_info_section = get_section (DEBUG_INFO_SECTION,
27152 SECTION_DEBUG, NULL);
27153 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
27154 SECTION_DEBUG, NULL);
27155 debug_loc_section = get_section (dwarf_version >= 5
27156 ? DEBUG_LOCLISTS_SECTION
27157 : DEBUG_LOC_SECTION,
27158 SECTION_DEBUG, NULL);
27159 debug_macinfo_section_name
27160 = (dwarf_strict && dwarf_version < 5)
27161 ? DEBUG_MACINFO_SECTION : DEBUG_MACRO_SECTION;
27162 debug_macinfo_section = get_section (debug_macinfo_section_name,
27163 SECTION_DEBUG, NULL);
27165 else
27167 debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
27168 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
27169 debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
27170 SECTION_DEBUG | SECTION_EXCLUDE,
27171 NULL);
27172 debug_addr_section = get_section (DEBUG_ADDR_SECTION,
27173 SECTION_DEBUG, NULL);
27174 debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
27175 SECTION_DEBUG, NULL);
27176 debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
27177 SECTION_DEBUG, NULL);
27178 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
27179 DEBUG_SKELETON_ABBREV_SECTION_LABEL, 0);
27181 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections stay in
27182 the main .o, but the skeleton_line goes into the split off dwo. */
27183 debug_skeleton_line_section
27184 = get_section (DEBUG_DWO_LINE_SECTION,
27185 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
27186 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
27187 DEBUG_SKELETON_LINE_SECTION_LABEL, 0);
27188 debug_str_offsets_section = get_section (DEBUG_DWO_STR_OFFSETS_SECTION,
27189 SECTION_DEBUG | SECTION_EXCLUDE,
27190 NULL);
27191 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
27192 DEBUG_SKELETON_INFO_SECTION_LABEL, 0);
27193 debug_loc_section = get_section (dwarf_version >= 5
27194 ? DEBUG_DWO_LOCLISTS_SECTION
27195 : DEBUG_DWO_LOC_SECTION,
27196 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
27197 debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
27198 DEBUG_STR_DWO_SECTION_FLAGS, NULL);
27199 debug_macinfo_section_name
27200 = (dwarf_strict && dwarf_version < 5)
27201 ? DEBUG_DWO_MACINFO_SECTION : DEBUG_DWO_MACRO_SECTION;
27202 debug_macinfo_section = get_section (debug_macinfo_section_name,
27203 SECTION_DEBUG | SECTION_EXCLUDE,
27204 NULL);
27206 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
27207 SECTION_DEBUG, NULL);
27208 debug_line_section = get_section (DEBUG_LINE_SECTION,
27209 SECTION_DEBUG, NULL);
27210 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
27211 SECTION_DEBUG, NULL);
27212 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
27213 SECTION_DEBUG, NULL);
27214 debug_str_section = get_section (DEBUG_STR_SECTION,
27215 DEBUG_STR_SECTION_FLAGS, NULL);
27216 if (!dwarf_split_debug_info && !DWARF2_ASM_LINE_DEBUG_INFO)
27217 debug_line_str_section = get_section (DEBUG_LINE_STR_SECTION,
27218 DEBUG_STR_SECTION_FLAGS, NULL);
27220 debug_ranges_section = get_section (dwarf_version >= 5
27221 ? DEBUG_RNGLISTS_SECTION
27222 : DEBUG_RANGES_SECTION,
27223 SECTION_DEBUG, NULL);
27224 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
27225 SECTION_DEBUG, NULL);
27227 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
27228 DEBUG_ABBREV_SECTION_LABEL, 0);
27229 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
27230 DEBUG_INFO_SECTION_LABEL, 0);
27231 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
27232 DEBUG_LINE_SECTION_LABEL, 0);
27233 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
27234 DEBUG_RANGES_SECTION_LABEL, 0);
27235 if (dwarf_version >= 5 && dwarf_split_debug_info)
27236 ASM_GENERATE_INTERNAL_LABEL (ranges_base_label,
27237 DEBUG_RANGES_SECTION_LABEL, 1);
27238 ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
27239 DEBUG_ADDR_SECTION_LABEL, 0);
27240 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
27241 (dwarf_strict && dwarf_version < 5)
27242 ? DEBUG_MACINFO_SECTION_LABEL
27243 : DEBUG_MACRO_SECTION_LABEL, 0);
27244 ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL, 0);
27247 /* Set up for Dwarf output at the start of compilation. */
27249 static void
27250 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
27252 /* This option is currently broken, see (PR53118 and PR46102). */
27253 if (flag_eliminate_dwarf2_dups
27254 && strstr (lang_hooks.name, "C++"))
27256 warning (0, "-feliminate-dwarf2-dups is broken for C++, ignoring");
27257 flag_eliminate_dwarf2_dups = 0;
27260 /* Allocate the file_table. */
27261 file_table = hash_table<dwarf_file_hasher>::create_ggc (50);
27263 #ifndef DWARF2_LINENO_DEBUGGING_INFO
27264 /* Allocate the decl_die_table. */
27265 decl_die_table = hash_table<decl_die_hasher>::create_ggc (10);
27267 /* Allocate the decl_loc_table. */
27268 decl_loc_table = hash_table<decl_loc_hasher>::create_ggc (10);
27270 /* Allocate the cached_dw_loc_list_table. */
27271 cached_dw_loc_list_table = hash_table<dw_loc_list_hasher>::create_ggc (10);
27273 /* Allocate the initial hunk of the decl_scope_table. */
27274 vec_alloc (decl_scope_table, 256);
27276 /* Allocate the initial hunk of the abbrev_die_table. */
27277 vec_alloc (abbrev_die_table, 256);
27278 /* Zero-th entry is allocated, but unused. */
27279 abbrev_die_table->quick_push (NULL);
27281 /* Allocate the dwarf_proc_stack_usage_map. */
27282 dwarf_proc_stack_usage_map = new hash_map<dw_die_ref, int>;
27284 /* Allocate the pubtypes and pubnames vectors. */
27285 vec_alloc (pubname_table, 32);
27286 vec_alloc (pubtype_table, 32);
27288 vec_alloc (incomplete_types, 64);
27290 vec_alloc (used_rtx_array, 32);
27292 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
27293 vec_alloc (macinfo_table, 64);
27294 #endif
27296 /* If front-ends already registered a main translation unit but we were not
27297 ready to perform the association, do this now. */
27298 if (main_translation_unit != NULL_TREE)
27299 equate_decl_number_to_die (main_translation_unit, comp_unit_die ());
27302 /* Called before compile () starts outputtting functions, variables
27303 and toplevel asms into assembly. */
27305 static void
27306 dwarf2out_assembly_start (void)
27308 #ifndef DWARF2_LINENO_DEBUGGING_INFO
27309 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
27310 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
27311 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
27312 COLD_TEXT_SECTION_LABEL, 0);
27313 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
27315 switch_to_section (text_section);
27316 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
27317 #endif
27319 /* Make sure the line number table for .text always exists. */
27320 text_section_line_info = new_line_info_table ();
27321 text_section_line_info->end_label = text_end_label;
27323 #ifdef DWARF2_LINENO_DEBUGGING_INFO
27324 cur_line_info_table = text_section_line_info;
27325 #endif
27327 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
27328 && dwarf2out_do_cfi_asm ()
27329 && (!(flag_unwind_tables || flag_exceptions)
27330 || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
27331 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
27334 /* A helper function for dwarf2out_finish called through
27335 htab_traverse. Assign a string its index. All strings must be
27336 collected into the table by the time index_string is called,
27337 because the indexing code relies on htab_traverse to traverse nodes
27338 in the same order for each run. */
27341 index_string (indirect_string_node **h, unsigned int *index)
27343 indirect_string_node *node = *h;
27345 find_string_form (node);
27346 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
27348 gcc_assert (node->index == NO_INDEX_ASSIGNED);
27349 node->index = *index;
27350 *index += 1;
27352 return 1;
27355 /* A helper function for output_indirect_strings called through
27356 htab_traverse. Output the offset to a string and update the
27357 current offset. */
27360 output_index_string_offset (indirect_string_node **h, unsigned int *offset)
27362 indirect_string_node *node = *h;
27364 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
27366 /* Assert that this node has been assigned an index. */
27367 gcc_assert (node->index != NO_INDEX_ASSIGNED
27368 && node->index != NOT_INDEXED);
27369 dw2_asm_output_data (DWARF_OFFSET_SIZE, *offset,
27370 "indexed string 0x%x: %s", node->index, node->str);
27371 *offset += strlen (node->str) + 1;
27373 return 1;
27376 /* A helper function for dwarf2out_finish called through
27377 htab_traverse. Output the indexed string. */
27380 output_index_string (indirect_string_node **h, unsigned int *cur_idx)
27382 struct indirect_string_node *node = *h;
27384 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
27386 /* Assert that the strings are output in the same order as their
27387 indexes were assigned. */
27388 gcc_assert (*cur_idx == node->index);
27389 assemble_string (node->str, strlen (node->str) + 1);
27390 *cur_idx += 1;
27392 return 1;
27395 /* A helper function for dwarf2out_finish called through
27396 htab_traverse. Emit one queued .debug_str string. */
27399 output_indirect_string (indirect_string_node **h, enum dwarf_form form)
27401 struct indirect_string_node *node = *h;
27403 node->form = find_string_form (node);
27404 if (node->form == form && node->refcount > 0)
27406 ASM_OUTPUT_LABEL (asm_out_file, node->label);
27407 assemble_string (node->str, strlen (node->str) + 1);
27410 return 1;
27413 /* Output the indexed string table. */
27415 static void
27416 output_indirect_strings (void)
27418 switch_to_section (debug_str_section);
27419 if (!dwarf_split_debug_info)
27420 debug_str_hash->traverse<enum dwarf_form,
27421 output_indirect_string> (DW_FORM_strp);
27422 else
27424 unsigned int offset = 0;
27425 unsigned int cur_idx = 0;
27427 skeleton_debug_str_hash->traverse<enum dwarf_form,
27428 output_indirect_string> (DW_FORM_strp);
27430 switch_to_section (debug_str_offsets_section);
27431 debug_str_hash->traverse_noresize
27432 <unsigned int *, output_index_string_offset> (&offset);
27433 switch_to_section (debug_str_dwo_section);
27434 debug_str_hash->traverse_noresize<unsigned int *, output_index_string>
27435 (&cur_idx);
27439 /* Callback for htab_traverse to assign an index to an entry in the
27440 table, and to write that entry to the .debug_addr section. */
27443 output_addr_table_entry (addr_table_entry **slot, unsigned int *cur_index)
27445 addr_table_entry *entry = *slot;
27447 if (entry->refcount == 0)
27449 gcc_assert (entry->index == NO_INDEX_ASSIGNED
27450 || entry->index == NOT_INDEXED);
27451 return 1;
27454 gcc_assert (entry->index == *cur_index);
27455 (*cur_index)++;
27457 switch (entry->kind)
27459 case ate_kind_rtx:
27460 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
27461 "0x%x", entry->index);
27462 break;
27463 case ate_kind_rtx_dtprel:
27464 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
27465 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
27466 DWARF2_ADDR_SIZE,
27467 entry->addr.rtl);
27468 fputc ('\n', asm_out_file);
27469 break;
27470 case ate_kind_label:
27471 dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
27472 "0x%x", entry->index);
27473 break;
27474 default:
27475 gcc_unreachable ();
27477 return 1;
27480 /* Produce the .debug_addr section. */
27482 static void
27483 output_addr_table (void)
27485 unsigned int index = 0;
27486 if (addr_index_table == NULL || addr_index_table->size () == 0)
27487 return;
27489 switch_to_section (debug_addr_section);
27490 addr_index_table
27491 ->traverse_noresize<unsigned int *, output_addr_table_entry> (&index);
27494 #if ENABLE_ASSERT_CHECKING
27495 /* Verify that all marks are clear. */
27497 static void
27498 verify_marks_clear (dw_die_ref die)
27500 dw_die_ref c;
27502 gcc_assert (! die->die_mark);
27503 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
27505 #endif /* ENABLE_ASSERT_CHECKING */
27507 /* Clear the marks for a die and its children.
27508 Be cool if the mark isn't set. */
27510 static void
27511 prune_unmark_dies (dw_die_ref die)
27513 dw_die_ref c;
27515 if (die->die_mark)
27516 die->die_mark = 0;
27517 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
27520 /* Given LOC that is referenced by a DIE we're marking as used, find all
27521 referenced DWARF procedures it references and mark them as used. */
27523 static void
27524 prune_unused_types_walk_loc_descr (dw_loc_descr_ref loc)
27526 for (; loc != NULL; loc = loc->dw_loc_next)
27527 switch (loc->dw_loc_opc)
27529 case DW_OP_implicit_pointer:
27530 case DW_OP_convert:
27531 case DW_OP_reinterpret:
27532 case DW_OP_GNU_implicit_pointer:
27533 case DW_OP_GNU_convert:
27534 case DW_OP_GNU_reinterpret:
27535 if (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref)
27536 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
27537 break;
27538 case DW_OP_GNU_variable_value:
27539 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
27541 dw_die_ref ref
27542 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
27543 if (ref == NULL)
27544 break;
27545 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
27546 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
27547 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
27549 /* FALLTHRU */
27550 case DW_OP_call2:
27551 case DW_OP_call4:
27552 case DW_OP_call_ref:
27553 case DW_OP_const_type:
27554 case DW_OP_GNU_const_type:
27555 case DW_OP_GNU_parameter_ref:
27556 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref);
27557 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
27558 break;
27559 case DW_OP_regval_type:
27560 case DW_OP_deref_type:
27561 case DW_OP_GNU_regval_type:
27562 case DW_OP_GNU_deref_type:
27563 gcc_assert (loc->dw_loc_oprnd2.val_class == dw_val_class_die_ref);
27564 prune_unused_types_mark (loc->dw_loc_oprnd2.v.val_die_ref.die, 1);
27565 break;
27566 case DW_OP_entry_value:
27567 case DW_OP_GNU_entry_value:
27568 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_loc);
27569 prune_unused_types_walk_loc_descr (loc->dw_loc_oprnd1.v.val_loc);
27570 break;
27571 default:
27572 break;
27576 /* Given DIE that we're marking as used, find any other dies
27577 it references as attributes and mark them as used. */
27579 static void
27580 prune_unused_types_walk_attribs (dw_die_ref die)
27582 dw_attr_node *a;
27583 unsigned ix;
27585 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
27587 switch (AT_class (a))
27589 /* Make sure DWARF procedures referenced by location descriptions will
27590 get emitted. */
27591 case dw_val_class_loc:
27592 prune_unused_types_walk_loc_descr (AT_loc (a));
27593 break;
27594 case dw_val_class_loc_list:
27595 for (dw_loc_list_ref list = AT_loc_list (a);
27596 list != NULL;
27597 list = list->dw_loc_next)
27598 prune_unused_types_walk_loc_descr (list->expr);
27599 break;
27601 case dw_val_class_die_ref:
27602 /* A reference to another DIE.
27603 Make sure that it will get emitted.
27604 If it was broken out into a comdat group, don't follow it. */
27605 if (! AT_ref (a)->comdat_type_p
27606 || a->dw_attr == DW_AT_specification)
27607 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
27608 break;
27610 case dw_val_class_str:
27611 /* Set the string's refcount to 0 so that prune_unused_types_mark
27612 accounts properly for it. */
27613 a->dw_attr_val.v.val_str->refcount = 0;
27614 break;
27616 default:
27617 break;
27622 /* Mark the generic parameters and arguments children DIEs of DIE. */
27624 static void
27625 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
27627 dw_die_ref c;
27629 if (die == NULL || die->die_child == NULL)
27630 return;
27631 c = die->die_child;
27634 if (is_template_parameter (c))
27635 prune_unused_types_mark (c, 1);
27636 c = c->die_sib;
27637 } while (c && c != die->die_child);
27640 /* Mark DIE as being used. If DOKIDS is true, then walk down
27641 to DIE's children. */
27643 static void
27644 prune_unused_types_mark (dw_die_ref die, int dokids)
27646 dw_die_ref c;
27648 if (die->die_mark == 0)
27650 /* We haven't done this node yet. Mark it as used. */
27651 die->die_mark = 1;
27652 /* If this is the DIE of a generic type instantiation,
27653 mark the children DIEs that describe its generic parms and
27654 args. */
27655 prune_unused_types_mark_generic_parms_dies (die);
27657 /* We also have to mark its parents as used.
27658 (But we don't want to mark our parent's kids due to this,
27659 unless it is a class.) */
27660 if (die->die_parent)
27661 prune_unused_types_mark (die->die_parent,
27662 class_scope_p (die->die_parent));
27664 /* Mark any referenced nodes. */
27665 prune_unused_types_walk_attribs (die);
27667 /* If this node is a specification,
27668 also mark the definition, if it exists. */
27669 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
27670 prune_unused_types_mark (die->die_definition, 1);
27673 if (dokids && die->die_mark != 2)
27675 /* We need to walk the children, but haven't done so yet.
27676 Remember that we've walked the kids. */
27677 die->die_mark = 2;
27679 /* If this is an array type, we need to make sure our
27680 kids get marked, even if they're types. If we're
27681 breaking out types into comdat sections, do this
27682 for all type definitions. */
27683 if (die->die_tag == DW_TAG_array_type
27684 || (use_debug_types
27685 && is_type_die (die) && ! is_declaration_die (die)))
27686 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
27687 else
27688 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
27692 /* For local classes, look if any static member functions were emitted
27693 and if so, mark them. */
27695 static void
27696 prune_unused_types_walk_local_classes (dw_die_ref die)
27698 dw_die_ref c;
27700 if (die->die_mark == 2)
27701 return;
27703 switch (die->die_tag)
27705 case DW_TAG_structure_type:
27706 case DW_TAG_union_type:
27707 case DW_TAG_class_type:
27708 break;
27710 case DW_TAG_subprogram:
27711 if (!get_AT_flag (die, DW_AT_declaration)
27712 || die->die_definition != NULL)
27713 prune_unused_types_mark (die, 1);
27714 return;
27716 default:
27717 return;
27720 /* Mark children. */
27721 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
27724 /* Walk the tree DIE and mark types that we actually use. */
27726 static void
27727 prune_unused_types_walk (dw_die_ref die)
27729 dw_die_ref c;
27731 /* Don't do anything if this node is already marked and
27732 children have been marked as well. */
27733 if (die->die_mark == 2)
27734 return;
27736 switch (die->die_tag)
27738 case DW_TAG_structure_type:
27739 case DW_TAG_union_type:
27740 case DW_TAG_class_type:
27741 if (die->die_perennial_p)
27742 break;
27744 for (c = die->die_parent; c; c = c->die_parent)
27745 if (c->die_tag == DW_TAG_subprogram)
27746 break;
27748 /* Finding used static member functions inside of classes
27749 is needed just for local classes, because for other classes
27750 static member function DIEs with DW_AT_specification
27751 are emitted outside of the DW_TAG_*_type. If we ever change
27752 it, we'd need to call this even for non-local classes. */
27753 if (c)
27754 prune_unused_types_walk_local_classes (die);
27756 /* It's a type node --- don't mark it. */
27757 return;
27759 case DW_TAG_const_type:
27760 case DW_TAG_packed_type:
27761 case DW_TAG_pointer_type:
27762 case DW_TAG_reference_type:
27763 case DW_TAG_rvalue_reference_type:
27764 case DW_TAG_volatile_type:
27765 case DW_TAG_typedef:
27766 case DW_TAG_array_type:
27767 case DW_TAG_interface_type:
27768 case DW_TAG_friend:
27769 case DW_TAG_enumeration_type:
27770 case DW_TAG_subroutine_type:
27771 case DW_TAG_string_type:
27772 case DW_TAG_set_type:
27773 case DW_TAG_subrange_type:
27774 case DW_TAG_ptr_to_member_type:
27775 case DW_TAG_file_type:
27776 /* Type nodes are useful only when other DIEs reference them --- don't
27777 mark them. */
27778 /* FALLTHROUGH */
27780 case DW_TAG_dwarf_procedure:
27781 /* Likewise for DWARF procedures. */
27783 if (die->die_perennial_p)
27784 break;
27786 return;
27788 default:
27789 /* Mark everything else. */
27790 break;
27793 if (die->die_mark == 0)
27795 die->die_mark = 1;
27797 /* Now, mark any dies referenced from here. */
27798 prune_unused_types_walk_attribs (die);
27801 die->die_mark = 2;
27803 /* Mark children. */
27804 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
27807 /* Increment the string counts on strings referred to from DIE's
27808 attributes. */
27810 static void
27811 prune_unused_types_update_strings (dw_die_ref die)
27813 dw_attr_node *a;
27814 unsigned ix;
27816 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
27817 if (AT_class (a) == dw_val_class_str)
27819 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
27820 s->refcount++;
27821 /* Avoid unnecessarily putting strings that are used less than
27822 twice in the hash table. */
27823 if (s->refcount
27824 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
27826 indirect_string_node **slot
27827 = debug_str_hash->find_slot_with_hash (s->str,
27828 htab_hash_string (s->str),
27829 INSERT);
27830 gcc_assert (*slot == NULL);
27831 *slot = s;
27836 /* Mark DIE and its children as removed. */
27838 static void
27839 mark_removed (dw_die_ref die)
27841 dw_die_ref c;
27842 die->removed = true;
27843 FOR_EACH_CHILD (die, c, mark_removed (c));
27846 /* Remove from the tree DIE any dies that aren't marked. */
27848 static void
27849 prune_unused_types_prune (dw_die_ref die)
27851 dw_die_ref c;
27853 gcc_assert (die->die_mark);
27854 prune_unused_types_update_strings (die);
27856 if (! die->die_child)
27857 return;
27859 c = die->die_child;
27860 do {
27861 dw_die_ref prev = c, next;
27862 for (c = c->die_sib; ! c->die_mark; c = next)
27863 if (c == die->die_child)
27865 /* No marked children between 'prev' and the end of the list. */
27866 if (prev == c)
27867 /* No marked children at all. */
27868 die->die_child = NULL;
27869 else
27871 prev->die_sib = c->die_sib;
27872 die->die_child = prev;
27874 c->die_sib = NULL;
27875 mark_removed (c);
27876 return;
27878 else
27880 next = c->die_sib;
27881 c->die_sib = NULL;
27882 mark_removed (c);
27885 if (c != prev->die_sib)
27886 prev->die_sib = c;
27887 prune_unused_types_prune (c);
27888 } while (c != die->die_child);
27891 /* Remove dies representing declarations that we never use. */
27893 static void
27894 prune_unused_types (void)
27896 unsigned int i;
27897 limbo_die_node *node;
27898 comdat_type_node *ctnode;
27899 pubname_entry *pub;
27900 dw_die_ref base_type;
27902 #if ENABLE_ASSERT_CHECKING
27903 /* All the marks should already be clear. */
27904 verify_marks_clear (comp_unit_die ());
27905 for (node = limbo_die_list; node; node = node->next)
27906 verify_marks_clear (node->die);
27907 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
27908 verify_marks_clear (ctnode->root_die);
27909 #endif /* ENABLE_ASSERT_CHECKING */
27911 /* Mark types that are used in global variables. */
27912 premark_types_used_by_global_vars ();
27914 /* Set the mark on nodes that are actually used. */
27915 prune_unused_types_walk (comp_unit_die ());
27916 for (node = limbo_die_list; node; node = node->next)
27917 prune_unused_types_walk (node->die);
27918 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
27920 prune_unused_types_walk (ctnode->root_die);
27921 prune_unused_types_mark (ctnode->type_die, 1);
27924 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
27925 are unusual in that they are pubnames that are the children of pubtypes.
27926 They should only be marked via their parent DW_TAG_enumeration_type die,
27927 not as roots in themselves. */
27928 FOR_EACH_VEC_ELT (*pubname_table, i, pub)
27929 if (pub->die->die_tag != DW_TAG_enumerator)
27930 prune_unused_types_mark (pub->die, 1);
27931 for (i = 0; base_types.iterate (i, &base_type); i++)
27932 prune_unused_types_mark (base_type, 1);
27934 /* For -fvar-tracking-assignments, also set the mark on nodes that could be
27935 referenced by DW_TAG_call_site DW_AT_call_origin (i.e. direct call
27936 callees). */
27937 cgraph_node *cnode;
27938 FOR_EACH_FUNCTION (cnode)
27939 if (cnode->referred_to_p (false))
27941 dw_die_ref die = lookup_decl_die (cnode->decl);
27942 if (die == NULL || die->die_mark)
27943 continue;
27944 for (cgraph_edge *e = cnode->callers; e; e = e->next_caller)
27945 if (e->caller != cnode
27946 && opt_for_fn (e->caller->decl, flag_var_tracking_assignments))
27948 prune_unused_types_mark (die, 1);
27949 break;
27953 if (debug_str_hash)
27954 debug_str_hash->empty ();
27955 if (skeleton_debug_str_hash)
27956 skeleton_debug_str_hash->empty ();
27957 prune_unused_types_prune (comp_unit_die ());
27958 for (limbo_die_node **pnode = &limbo_die_list; *pnode; )
27960 node = *pnode;
27961 if (!node->die->die_mark)
27962 *pnode = node->next;
27963 else
27965 prune_unused_types_prune (node->die);
27966 pnode = &node->next;
27969 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
27970 prune_unused_types_prune (ctnode->root_die);
27972 /* Leave the marks clear. */
27973 prune_unmark_dies (comp_unit_die ());
27974 for (node = limbo_die_list; node; node = node->next)
27975 prune_unmark_dies (node->die);
27976 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
27977 prune_unmark_dies (ctnode->root_die);
27980 /* Helpers to manipulate hash table of comdat type units. */
27982 struct comdat_type_hasher : nofree_ptr_hash <comdat_type_node>
27984 static inline hashval_t hash (const comdat_type_node *);
27985 static inline bool equal (const comdat_type_node *, const comdat_type_node *);
27988 inline hashval_t
27989 comdat_type_hasher::hash (const comdat_type_node *type_node)
27991 hashval_t h;
27992 memcpy (&h, type_node->signature, sizeof (h));
27993 return h;
27996 inline bool
27997 comdat_type_hasher::equal (const comdat_type_node *type_node_1,
27998 const comdat_type_node *type_node_2)
28000 return (! memcmp (type_node_1->signature, type_node_2->signature,
28001 DWARF_TYPE_SIGNATURE_SIZE));
28004 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
28005 to the location it would have been added, should we know its
28006 DECL_ASSEMBLER_NAME when we added other attributes. This will
28007 probably improve compactness of debug info, removing equivalent
28008 abbrevs, and hide any differences caused by deferring the
28009 computation of the assembler name, triggered by e.g. PCH. */
28011 static inline void
28012 move_linkage_attr (dw_die_ref die)
28014 unsigned ix = vec_safe_length (die->die_attr);
28015 dw_attr_node linkage = (*die->die_attr)[ix - 1];
28017 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
28018 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
28020 while (--ix > 0)
28022 dw_attr_node *prev = &(*die->die_attr)[ix - 1];
28024 if (prev->dw_attr == DW_AT_decl_line
28025 || prev->dw_attr == DW_AT_decl_column
28026 || prev->dw_attr == DW_AT_name)
28027 break;
28030 if (ix != vec_safe_length (die->die_attr) - 1)
28032 die->die_attr->pop ();
28033 die->die_attr->quick_insert (ix, linkage);
28037 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
28038 referenced from typed stack ops and count how often they are used. */
28040 static void
28041 mark_base_types (dw_loc_descr_ref loc)
28043 dw_die_ref base_type = NULL;
28045 for (; loc; loc = loc->dw_loc_next)
28047 switch (loc->dw_loc_opc)
28049 case DW_OP_regval_type:
28050 case DW_OP_deref_type:
28051 case DW_OP_GNU_regval_type:
28052 case DW_OP_GNU_deref_type:
28053 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
28054 break;
28055 case DW_OP_convert:
28056 case DW_OP_reinterpret:
28057 case DW_OP_GNU_convert:
28058 case DW_OP_GNU_reinterpret:
28059 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
28060 continue;
28061 /* FALLTHRU */
28062 case DW_OP_const_type:
28063 case DW_OP_GNU_const_type:
28064 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
28065 break;
28066 case DW_OP_entry_value:
28067 case DW_OP_GNU_entry_value:
28068 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
28069 continue;
28070 default:
28071 continue;
28073 gcc_assert (base_type->die_parent == comp_unit_die ());
28074 if (base_type->die_mark)
28075 base_type->die_mark++;
28076 else
28078 base_types.safe_push (base_type);
28079 base_type->die_mark = 1;
28084 /* Comparison function for sorting marked base types. */
28086 static int
28087 base_type_cmp (const void *x, const void *y)
28089 dw_die_ref dx = *(const dw_die_ref *) x;
28090 dw_die_ref dy = *(const dw_die_ref *) y;
28091 unsigned int byte_size1, byte_size2;
28092 unsigned int encoding1, encoding2;
28093 unsigned int align1, align2;
28094 if (dx->die_mark > dy->die_mark)
28095 return -1;
28096 if (dx->die_mark < dy->die_mark)
28097 return 1;
28098 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
28099 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
28100 if (byte_size1 < byte_size2)
28101 return 1;
28102 if (byte_size1 > byte_size2)
28103 return -1;
28104 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
28105 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
28106 if (encoding1 < encoding2)
28107 return 1;
28108 if (encoding1 > encoding2)
28109 return -1;
28110 align1 = get_AT_unsigned (dx, DW_AT_alignment);
28111 align2 = get_AT_unsigned (dy, DW_AT_alignment);
28112 if (align1 < align2)
28113 return 1;
28114 if (align1 > align2)
28115 return -1;
28116 return 0;
28119 /* Move base types marked by mark_base_types as early as possible
28120 in the CU, sorted by decreasing usage count both to make the
28121 uleb128 references as small as possible and to make sure they
28122 will have die_offset already computed by calc_die_sizes when
28123 sizes of typed stack loc ops is computed. */
28125 static void
28126 move_marked_base_types (void)
28128 unsigned int i;
28129 dw_die_ref base_type, die, c;
28131 if (base_types.is_empty ())
28132 return;
28134 /* Sort by decreasing usage count, they will be added again in that
28135 order later on. */
28136 base_types.qsort (base_type_cmp);
28137 die = comp_unit_die ();
28138 c = die->die_child;
28141 dw_die_ref prev = c;
28142 c = c->die_sib;
28143 while (c->die_mark)
28145 remove_child_with_prev (c, prev);
28146 /* As base types got marked, there must be at least
28147 one node other than DW_TAG_base_type. */
28148 gcc_assert (die->die_child != NULL);
28149 c = prev->die_sib;
28152 while (c != die->die_child);
28153 gcc_assert (die->die_child);
28154 c = die->die_child;
28155 for (i = 0; base_types.iterate (i, &base_type); i++)
28157 base_type->die_mark = 0;
28158 base_type->die_sib = c->die_sib;
28159 c->die_sib = base_type;
28160 c = base_type;
28164 /* Helper function for resolve_addr, attempt to resolve
28165 one CONST_STRING, return true if successful. Similarly verify that
28166 SYMBOL_REFs refer to variables emitted in the current CU. */
28168 static bool
28169 resolve_one_addr (rtx *addr)
28171 rtx rtl = *addr;
28173 if (GET_CODE (rtl) == CONST_STRING)
28175 size_t len = strlen (XSTR (rtl, 0)) + 1;
28176 tree t = build_string (len, XSTR (rtl, 0));
28177 tree tlen = size_int (len - 1);
28178 TREE_TYPE (t)
28179 = build_array_type (char_type_node, build_index_type (tlen));
28180 rtl = lookup_constant_def (t);
28181 if (!rtl || !MEM_P (rtl))
28182 return false;
28183 rtl = XEXP (rtl, 0);
28184 if (GET_CODE (rtl) == SYMBOL_REF
28185 && SYMBOL_REF_DECL (rtl)
28186 && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
28187 return false;
28188 vec_safe_push (used_rtx_array, rtl);
28189 *addr = rtl;
28190 return true;
28193 if (GET_CODE (rtl) == SYMBOL_REF
28194 && SYMBOL_REF_DECL (rtl))
28196 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
28198 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
28199 return false;
28201 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
28202 return false;
28205 if (GET_CODE (rtl) == CONST)
28207 subrtx_ptr_iterator::array_type array;
28208 FOR_EACH_SUBRTX_PTR (iter, array, &XEXP (rtl, 0), ALL)
28209 if (!resolve_one_addr (*iter))
28210 return false;
28213 return true;
28216 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
28217 if possible, and create DW_TAG_dwarf_procedure that can be referenced
28218 from DW_OP_implicit_pointer if the string hasn't been seen yet. */
28220 static rtx
28221 string_cst_pool_decl (tree t)
28223 rtx rtl = output_constant_def (t, 1);
28224 unsigned char *array;
28225 dw_loc_descr_ref l;
28226 tree decl;
28227 size_t len;
28228 dw_die_ref ref;
28230 if (!rtl || !MEM_P (rtl))
28231 return NULL_RTX;
28232 rtl = XEXP (rtl, 0);
28233 if (GET_CODE (rtl) != SYMBOL_REF
28234 || SYMBOL_REF_DECL (rtl) == NULL_TREE)
28235 return NULL_RTX;
28237 decl = SYMBOL_REF_DECL (rtl);
28238 if (!lookup_decl_die (decl))
28240 len = TREE_STRING_LENGTH (t);
28241 vec_safe_push (used_rtx_array, rtl);
28242 ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
28243 array = ggc_vec_alloc<unsigned char> (len);
28244 memcpy (array, TREE_STRING_POINTER (t), len);
28245 l = new_loc_descr (DW_OP_implicit_value, len, 0);
28246 l->dw_loc_oprnd2.val_class = dw_val_class_vec;
28247 l->dw_loc_oprnd2.v.val_vec.length = len;
28248 l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
28249 l->dw_loc_oprnd2.v.val_vec.array = array;
28250 add_AT_loc (ref, DW_AT_location, l);
28251 equate_decl_number_to_die (decl, ref);
28253 return rtl;
28256 /* Helper function of resolve_addr_in_expr. LOC is
28257 a DW_OP_addr followed by DW_OP_stack_value, either at the start
28258 of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
28259 resolved. Replace it (both DW_OP_addr and DW_OP_stack_value)
28260 with DW_OP_implicit_pointer if possible
28261 and return true, if unsuccessful, return false. */
28263 static bool
28264 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
28266 rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
28267 HOST_WIDE_INT offset = 0;
28268 dw_die_ref ref = NULL;
28269 tree decl;
28271 if (GET_CODE (rtl) == CONST
28272 && GET_CODE (XEXP (rtl, 0)) == PLUS
28273 && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
28275 offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
28276 rtl = XEXP (XEXP (rtl, 0), 0);
28278 if (GET_CODE (rtl) == CONST_STRING)
28280 size_t len = strlen (XSTR (rtl, 0)) + 1;
28281 tree t = build_string (len, XSTR (rtl, 0));
28282 tree tlen = size_int (len - 1);
28284 TREE_TYPE (t)
28285 = build_array_type (char_type_node, build_index_type (tlen));
28286 rtl = string_cst_pool_decl (t);
28287 if (!rtl)
28288 return false;
28290 if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
28292 decl = SYMBOL_REF_DECL (rtl);
28293 if (VAR_P (decl) && !DECL_EXTERNAL (decl))
28295 ref = lookup_decl_die (decl);
28296 if (ref && (get_AT (ref, DW_AT_location)
28297 || get_AT (ref, DW_AT_const_value)))
28299 loc->dw_loc_opc = dwarf_OP (DW_OP_implicit_pointer);
28300 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
28301 loc->dw_loc_oprnd1.val_entry = NULL;
28302 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
28303 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
28304 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
28305 loc->dw_loc_oprnd2.v.val_int = offset;
28306 return true;
28310 return false;
28313 /* Helper function for resolve_addr, handle one location
28314 expression, return false if at least one CONST_STRING or SYMBOL_REF in
28315 the location list couldn't be resolved. */
28317 static bool
28318 resolve_addr_in_expr (dw_attr_node *a, dw_loc_descr_ref loc)
28320 dw_loc_descr_ref keep = NULL;
28321 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
28322 switch (loc->dw_loc_opc)
28324 case DW_OP_addr:
28325 if (!resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
28327 if ((prev == NULL
28328 || prev->dw_loc_opc == DW_OP_piece
28329 || prev->dw_loc_opc == DW_OP_bit_piece)
28330 && loc->dw_loc_next
28331 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
28332 && (!dwarf_strict || dwarf_version >= 5)
28333 && optimize_one_addr_into_implicit_ptr (loc))
28334 break;
28335 return false;
28337 break;
28338 case DW_OP_GNU_addr_index:
28339 case DW_OP_GNU_const_index:
28340 if (loc->dw_loc_opc == DW_OP_GNU_addr_index
28341 || (loc->dw_loc_opc == DW_OP_GNU_const_index && loc->dtprel))
28343 rtx rtl = loc->dw_loc_oprnd1.val_entry->addr.rtl;
28344 if (!resolve_one_addr (&rtl))
28345 return false;
28346 remove_addr_table_entry (loc->dw_loc_oprnd1.val_entry);
28347 loc->dw_loc_oprnd1.val_entry
28348 = add_addr_table_entry (rtl, ate_kind_rtx);
28350 break;
28351 case DW_OP_const4u:
28352 case DW_OP_const8u:
28353 if (loc->dtprel
28354 && !resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
28355 return false;
28356 break;
28357 case DW_OP_plus_uconst:
28358 if (size_of_loc_descr (loc)
28359 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
28361 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
28363 dw_loc_descr_ref repl
28364 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
28365 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
28366 add_loc_descr (&repl, loc->dw_loc_next);
28367 *loc = *repl;
28369 break;
28370 case DW_OP_implicit_value:
28371 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
28372 && !resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr))
28373 return false;
28374 break;
28375 case DW_OP_implicit_pointer:
28376 case DW_OP_GNU_implicit_pointer:
28377 case DW_OP_GNU_parameter_ref:
28378 case DW_OP_GNU_variable_value:
28379 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
28381 dw_die_ref ref
28382 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
28383 if (ref == NULL)
28384 return false;
28385 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
28386 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
28387 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
28389 if (loc->dw_loc_opc == DW_OP_GNU_variable_value)
28391 if (prev == NULL
28392 && loc->dw_loc_next == NULL
28393 && AT_class (a) == dw_val_class_loc)
28394 switch (a->dw_attr)
28396 /* Following attributes allow both exprloc and reference,
28397 so if the whole expression is DW_OP_GNU_variable_value
28398 alone we could transform it into reference. */
28399 case DW_AT_byte_size:
28400 case DW_AT_bit_size:
28401 case DW_AT_lower_bound:
28402 case DW_AT_upper_bound:
28403 case DW_AT_bit_stride:
28404 case DW_AT_count:
28405 case DW_AT_allocated:
28406 case DW_AT_associated:
28407 case DW_AT_byte_stride:
28408 a->dw_attr_val.val_class = dw_val_class_die_ref;
28409 a->dw_attr_val.val_entry = NULL;
28410 a->dw_attr_val.v.val_die_ref.die
28411 = loc->dw_loc_oprnd1.v.val_die_ref.die;
28412 a->dw_attr_val.v.val_die_ref.external = 0;
28413 return true;
28414 default:
28415 break;
28417 if (dwarf_strict)
28418 return false;
28420 break;
28421 case DW_OP_const_type:
28422 case DW_OP_regval_type:
28423 case DW_OP_deref_type:
28424 case DW_OP_convert:
28425 case DW_OP_reinterpret:
28426 case DW_OP_GNU_const_type:
28427 case DW_OP_GNU_regval_type:
28428 case DW_OP_GNU_deref_type:
28429 case DW_OP_GNU_convert:
28430 case DW_OP_GNU_reinterpret:
28431 while (loc->dw_loc_next
28432 && (loc->dw_loc_next->dw_loc_opc == DW_OP_convert
28433 || loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert))
28435 dw_die_ref base1, base2;
28436 unsigned enc1, enc2, size1, size2;
28437 if (loc->dw_loc_opc == DW_OP_regval_type
28438 || loc->dw_loc_opc == DW_OP_deref_type
28439 || loc->dw_loc_opc == DW_OP_GNU_regval_type
28440 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
28441 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
28442 else if (loc->dw_loc_oprnd1.val_class
28443 == dw_val_class_unsigned_const)
28444 break;
28445 else
28446 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
28447 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
28448 == dw_val_class_unsigned_const)
28449 break;
28450 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
28451 gcc_assert (base1->die_tag == DW_TAG_base_type
28452 && base2->die_tag == DW_TAG_base_type);
28453 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
28454 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
28455 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
28456 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
28457 if (size1 == size2
28458 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
28459 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
28460 && loc != keep)
28461 || enc1 == enc2))
28463 /* Optimize away next DW_OP_convert after
28464 adjusting LOC's base type die reference. */
28465 if (loc->dw_loc_opc == DW_OP_regval_type
28466 || loc->dw_loc_opc == DW_OP_deref_type
28467 || loc->dw_loc_opc == DW_OP_GNU_regval_type
28468 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
28469 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
28470 else
28471 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
28472 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
28473 continue;
28475 /* Don't change integer DW_OP_convert after e.g. floating
28476 point typed stack entry. */
28477 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
28478 keep = loc->dw_loc_next;
28479 break;
28481 break;
28482 default:
28483 break;
28485 return true;
28488 /* Helper function of resolve_addr. DIE had DW_AT_location of
28489 DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
28490 and DW_OP_addr couldn't be resolved. resolve_addr has already
28491 removed the DW_AT_location attribute. This function attempts to
28492 add a new DW_AT_location attribute with DW_OP_implicit_pointer
28493 to it or DW_AT_const_value attribute, if possible. */
28495 static void
28496 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
28498 if (!VAR_P (decl)
28499 || lookup_decl_die (decl) != die
28500 || DECL_EXTERNAL (decl)
28501 || !TREE_STATIC (decl)
28502 || DECL_INITIAL (decl) == NULL_TREE
28503 || DECL_P (DECL_INITIAL (decl))
28504 || get_AT (die, DW_AT_const_value))
28505 return;
28507 tree init = DECL_INITIAL (decl);
28508 HOST_WIDE_INT offset = 0;
28509 /* For variables that have been optimized away and thus
28510 don't have a memory location, see if we can emit
28511 DW_AT_const_value instead. */
28512 if (tree_add_const_value_attribute (die, init))
28513 return;
28514 if (dwarf_strict && dwarf_version < 5)
28515 return;
28516 /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
28517 and ADDR_EXPR refers to a decl that has DW_AT_location or
28518 DW_AT_const_value (but isn't addressable, otherwise
28519 resolving the original DW_OP_addr wouldn't fail), see if
28520 we can add DW_OP_implicit_pointer. */
28521 STRIP_NOPS (init);
28522 if (TREE_CODE (init) == POINTER_PLUS_EXPR
28523 && tree_fits_shwi_p (TREE_OPERAND (init, 1)))
28525 offset = tree_to_shwi (TREE_OPERAND (init, 1));
28526 init = TREE_OPERAND (init, 0);
28527 STRIP_NOPS (init);
28529 if (TREE_CODE (init) != ADDR_EXPR)
28530 return;
28531 if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
28532 && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
28533 || (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL
28534 && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
28535 && TREE_OPERAND (init, 0) != decl))
28537 dw_die_ref ref;
28538 dw_loc_descr_ref l;
28540 if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
28542 rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
28543 if (!rtl)
28544 return;
28545 decl = SYMBOL_REF_DECL (rtl);
28547 else
28548 decl = TREE_OPERAND (init, 0);
28549 ref = lookup_decl_die (decl);
28550 if (ref == NULL
28551 || (!get_AT (ref, DW_AT_location)
28552 && !get_AT (ref, DW_AT_const_value)))
28553 return;
28554 l = new_loc_descr (dwarf_OP (DW_OP_implicit_pointer), 0, offset);
28555 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
28556 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
28557 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
28558 add_AT_loc (die, DW_AT_location, l);
28562 /* Return NULL if l is a DWARF expression, or first op that is not
28563 valid DWARF expression. */
28565 static dw_loc_descr_ref
28566 non_dwarf_expression (dw_loc_descr_ref l)
28568 while (l)
28570 if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
28571 return l;
28572 switch (l->dw_loc_opc)
28574 case DW_OP_regx:
28575 case DW_OP_implicit_value:
28576 case DW_OP_stack_value:
28577 case DW_OP_implicit_pointer:
28578 case DW_OP_GNU_implicit_pointer:
28579 case DW_OP_GNU_parameter_ref:
28580 case DW_OP_piece:
28581 case DW_OP_bit_piece:
28582 return l;
28583 default:
28584 break;
28586 l = l->dw_loc_next;
28588 return NULL;
28591 /* Return adjusted copy of EXPR:
28592 If it is empty DWARF expression, return it.
28593 If it is valid non-empty DWARF expression,
28594 return copy of EXPR with DW_OP_deref appended to it.
28595 If it is DWARF expression followed by DW_OP_reg{N,x}, return
28596 copy of the DWARF expression with DW_OP_breg{N,x} <0> appended.
28597 If it is DWARF expression followed by DW_OP_stack_value, return
28598 copy of the DWARF expression without anything appended.
28599 Otherwise, return NULL. */
28601 static dw_loc_descr_ref
28602 copy_deref_exprloc (dw_loc_descr_ref expr)
28604 dw_loc_descr_ref tail = NULL;
28606 if (expr == NULL)
28607 return NULL;
28609 dw_loc_descr_ref l = non_dwarf_expression (expr);
28610 if (l && l->dw_loc_next)
28611 return NULL;
28613 if (l)
28615 if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
28616 tail = new_loc_descr ((enum dwarf_location_atom)
28617 (DW_OP_breg0 + (l->dw_loc_opc - DW_OP_reg0)),
28618 0, 0);
28619 else
28620 switch (l->dw_loc_opc)
28622 case DW_OP_regx:
28623 tail = new_loc_descr (DW_OP_bregx,
28624 l->dw_loc_oprnd1.v.val_unsigned, 0);
28625 break;
28626 case DW_OP_stack_value:
28627 break;
28628 default:
28629 return NULL;
28632 else
28633 tail = new_loc_descr (DW_OP_deref, 0, 0);
28635 dw_loc_descr_ref ret = NULL, *p = &ret;
28636 while (expr != l)
28638 *p = new_loc_descr (expr->dw_loc_opc, 0, 0);
28639 (*p)->dw_loc_oprnd1 = expr->dw_loc_oprnd1;
28640 (*p)->dw_loc_oprnd2 = expr->dw_loc_oprnd2;
28641 p = &(*p)->dw_loc_next;
28642 expr = expr->dw_loc_next;
28644 *p = tail;
28645 return ret;
28648 /* For DW_AT_string_length attribute with DW_OP_GNU_variable_value
28649 reference to a variable or argument, adjust it if needed and return:
28650 -1 if the DW_AT_string_length attribute and DW_AT_{string_length_,}byte_size
28651 attribute if present should be removed
28652 0 keep the attribute perhaps with minor modifications, no need to rescan
28653 1 if the attribute has been successfully adjusted. */
28655 static int
28656 optimize_string_length (dw_attr_node *a)
28658 dw_loc_descr_ref l = AT_loc (a), lv;
28659 dw_die_ref die;
28660 if (l->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
28662 tree decl = l->dw_loc_oprnd1.v.val_decl_ref;
28663 die = lookup_decl_die (decl);
28664 if (die)
28666 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
28667 l->dw_loc_oprnd1.v.val_die_ref.die = die;
28668 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
28670 else
28671 return -1;
28673 else
28674 die = l->dw_loc_oprnd1.v.val_die_ref.die;
28676 /* DWARF5 allows reference class, so we can then reference the DIE.
28677 Only do this for DW_OP_GNU_variable_value DW_OP_stack_value. */
28678 if (l->dw_loc_next != NULL && dwarf_version >= 5)
28680 a->dw_attr_val.val_class = dw_val_class_die_ref;
28681 a->dw_attr_val.val_entry = NULL;
28682 a->dw_attr_val.v.val_die_ref.die = die;
28683 a->dw_attr_val.v.val_die_ref.external = 0;
28684 return 0;
28687 dw_attr_node *av = get_AT (die, DW_AT_location);
28688 dw_loc_list_ref d;
28689 bool non_dwarf_expr = false;
28691 if (av == NULL)
28692 return dwarf_strict ? -1 : 0;
28693 switch (AT_class (av))
28695 case dw_val_class_loc_list:
28696 for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
28697 if (d->expr && non_dwarf_expression (d->expr))
28698 non_dwarf_expr = true;
28699 break;
28700 case dw_val_class_loc:
28701 lv = AT_loc (av);
28702 if (lv == NULL)
28703 return dwarf_strict ? -1 : 0;
28704 if (non_dwarf_expression (lv))
28705 non_dwarf_expr = true;
28706 break;
28707 default:
28708 return dwarf_strict ? -1 : 0;
28711 /* If it is safe to transform DW_OP_GNU_variable_value DW_OP_stack_value
28712 into DW_OP_call4 or DW_OP_GNU_variable_value into
28713 DW_OP_call4 DW_OP_deref, do so. */
28714 if (!non_dwarf_expr
28715 && (l->dw_loc_next != NULL || AT_class (av) == dw_val_class_loc))
28717 l->dw_loc_opc = DW_OP_call4;
28718 if (l->dw_loc_next)
28719 l->dw_loc_next = NULL;
28720 else
28721 l->dw_loc_next = new_loc_descr (DW_OP_deref, 0, 0);
28722 return 0;
28725 /* For DW_OP_GNU_variable_value DW_OP_stack_value, we can just
28726 copy over the DW_AT_location attribute from die to a. */
28727 if (l->dw_loc_next != NULL)
28729 a->dw_attr_val = av->dw_attr_val;
28730 return 1;
28733 dw_loc_list_ref list, *p;
28734 switch (AT_class (av))
28736 case dw_val_class_loc_list:
28737 p = &list;
28738 list = NULL;
28739 for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
28741 lv = copy_deref_exprloc (d->expr);
28742 if (lv)
28744 *p = new_loc_list (lv, d->begin, d->end, d->section);
28745 p = &(*p)->dw_loc_next;
28747 else if (!dwarf_strict && d->expr)
28748 return 0;
28750 if (list == NULL)
28751 return dwarf_strict ? -1 : 0;
28752 a->dw_attr_val.val_class = dw_val_class_loc_list;
28753 gen_llsym (list);
28754 *AT_loc_list_ptr (a) = list;
28755 return 1;
28756 case dw_val_class_loc:
28757 lv = copy_deref_exprloc (AT_loc (av));
28758 if (lv == NULL)
28759 return dwarf_strict ? -1 : 0;
28760 a->dw_attr_val.v.val_loc = lv;
28761 return 1;
28762 default:
28763 gcc_unreachable ();
28767 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
28768 an address in .rodata section if the string literal is emitted there,
28769 or remove the containing location list or replace DW_AT_const_value
28770 with DW_AT_location and empty location expression, if it isn't found
28771 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
28772 to something that has been emitted in the current CU. */
28774 static void
28775 resolve_addr (dw_die_ref die)
28777 dw_die_ref c;
28778 dw_attr_node *a;
28779 dw_loc_list_ref *curr, *start, loc;
28780 unsigned ix;
28781 bool remove_AT_byte_size = false;
28783 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
28784 switch (AT_class (a))
28786 case dw_val_class_loc_list:
28787 start = curr = AT_loc_list_ptr (a);
28788 loc = *curr;
28789 gcc_assert (loc);
28790 /* The same list can be referenced more than once. See if we have
28791 already recorded the result from a previous pass. */
28792 if (loc->replaced)
28793 *curr = loc->dw_loc_next;
28794 else if (!loc->resolved_addr)
28796 /* As things stand, we do not expect or allow one die to
28797 reference a suffix of another die's location list chain.
28798 References must be identical or completely separate.
28799 There is therefore no need to cache the result of this
28800 pass on any list other than the first; doing so
28801 would lead to unnecessary writes. */
28802 while (*curr)
28804 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
28805 if (!resolve_addr_in_expr (a, (*curr)->expr))
28807 dw_loc_list_ref next = (*curr)->dw_loc_next;
28808 dw_loc_descr_ref l = (*curr)->expr;
28810 if (next && (*curr)->ll_symbol)
28812 gcc_assert (!next->ll_symbol);
28813 next->ll_symbol = (*curr)->ll_symbol;
28815 if (dwarf_split_debug_info)
28816 remove_loc_list_addr_table_entries (l);
28817 *curr = next;
28819 else
28821 mark_base_types ((*curr)->expr);
28822 curr = &(*curr)->dw_loc_next;
28825 if (loc == *start)
28826 loc->resolved_addr = 1;
28827 else
28829 loc->replaced = 1;
28830 loc->dw_loc_next = *start;
28833 if (!*start)
28835 remove_AT (die, a->dw_attr);
28836 ix--;
28838 break;
28839 case dw_val_class_loc:
28841 dw_loc_descr_ref l = AT_loc (a);
28842 /* DW_OP_GNU_variable_value DW_OP_stack_value or
28843 DW_OP_GNU_variable_value in DW_AT_string_length can be converted
28844 into DW_OP_call4 or DW_OP_call4 DW_OP_deref, which is standard
28845 DWARF4 unlike DW_OP_GNU_variable_value. Or for DWARF5
28846 DW_OP_GNU_variable_value DW_OP_stack_value can be replaced
28847 with DW_FORM_ref referencing the same DIE as
28848 DW_OP_GNU_variable_value used to reference. */
28849 if (a->dw_attr == DW_AT_string_length
28850 && l
28851 && l->dw_loc_opc == DW_OP_GNU_variable_value
28852 && (l->dw_loc_next == NULL
28853 || (l->dw_loc_next->dw_loc_next == NULL
28854 && l->dw_loc_next->dw_loc_opc == DW_OP_stack_value)))
28856 switch (optimize_string_length (a))
28858 case -1:
28859 remove_AT (die, a->dw_attr);
28860 ix--;
28861 /* If we drop DW_AT_string_length, we need to drop also
28862 DW_AT_{string_length_,}byte_size. */
28863 remove_AT_byte_size = true;
28864 continue;
28865 default:
28866 break;
28867 case 1:
28868 /* Even if we keep the optimized DW_AT_string_length,
28869 it might have changed AT_class, so process it again. */
28870 ix--;
28871 continue;
28874 /* For -gdwarf-2 don't attempt to optimize
28875 DW_AT_data_member_location containing
28876 DW_OP_plus_uconst - older consumers might
28877 rely on it being that op instead of a more complex,
28878 but shorter, location description. */
28879 if ((dwarf_version > 2
28880 || a->dw_attr != DW_AT_data_member_location
28881 || l == NULL
28882 || l->dw_loc_opc != DW_OP_plus_uconst
28883 || l->dw_loc_next != NULL)
28884 && !resolve_addr_in_expr (a, l))
28886 if (dwarf_split_debug_info)
28887 remove_loc_list_addr_table_entries (l);
28888 if (l != NULL
28889 && l->dw_loc_next == NULL
28890 && l->dw_loc_opc == DW_OP_addr
28891 && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
28892 && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
28893 && a->dw_attr == DW_AT_location)
28895 tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
28896 remove_AT (die, a->dw_attr);
28897 ix--;
28898 optimize_location_into_implicit_ptr (die, decl);
28899 break;
28901 if (a->dw_attr == DW_AT_string_length)
28902 /* If we drop DW_AT_string_length, we need to drop also
28903 DW_AT_{string_length_,}byte_size. */
28904 remove_AT_byte_size = true;
28905 remove_AT (die, a->dw_attr);
28906 ix--;
28908 else
28909 mark_base_types (l);
28911 break;
28912 case dw_val_class_addr:
28913 if (a->dw_attr == DW_AT_const_value
28914 && !resolve_one_addr (&a->dw_attr_val.v.val_addr))
28916 if (AT_index (a) != NOT_INDEXED)
28917 remove_addr_table_entry (a->dw_attr_val.val_entry);
28918 remove_AT (die, a->dw_attr);
28919 ix--;
28921 if ((die->die_tag == DW_TAG_call_site
28922 && a->dw_attr == DW_AT_call_origin)
28923 || (die->die_tag == DW_TAG_GNU_call_site
28924 && a->dw_attr == DW_AT_abstract_origin))
28926 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
28927 dw_die_ref tdie = lookup_decl_die (tdecl);
28928 dw_die_ref cdie;
28929 if (tdie == NULL
28930 && DECL_EXTERNAL (tdecl)
28931 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE
28932 && (cdie = lookup_context_die (DECL_CONTEXT (tdecl))))
28934 dw_die_ref pdie = cdie;
28935 /* Make sure we don't add these DIEs into type units.
28936 We could emit skeleton DIEs for context (namespaces,
28937 outer structs/classes) and a skeleton DIE for the
28938 innermost context with DW_AT_signature pointing to the
28939 type unit. See PR78835. */
28940 while (pdie && pdie->die_tag != DW_TAG_type_unit)
28941 pdie = pdie->die_parent;
28942 if (pdie == NULL)
28944 /* Creating a full DIE for tdecl is overly expensive and
28945 at this point even wrong when in the LTO phase
28946 as it can end up generating new type DIEs we didn't
28947 output and thus optimize_external_refs will crash. */
28948 tdie = new_die (DW_TAG_subprogram, cdie, NULL_TREE);
28949 add_AT_flag (tdie, DW_AT_external, 1);
28950 add_AT_flag (tdie, DW_AT_declaration, 1);
28951 add_linkage_attr (tdie, tdecl);
28952 add_name_and_src_coords_attributes (tdie, tdecl);
28953 equate_decl_number_to_die (tdecl, tdie);
28956 if (tdie)
28958 a->dw_attr_val.val_class = dw_val_class_die_ref;
28959 a->dw_attr_val.v.val_die_ref.die = tdie;
28960 a->dw_attr_val.v.val_die_ref.external = 0;
28962 else
28964 if (AT_index (a) != NOT_INDEXED)
28965 remove_addr_table_entry (a->dw_attr_val.val_entry);
28966 remove_AT (die, a->dw_attr);
28967 ix--;
28970 break;
28971 default:
28972 break;
28975 if (remove_AT_byte_size)
28976 remove_AT (die, dwarf_version >= 5
28977 ? DW_AT_string_length_byte_size
28978 : DW_AT_byte_size);
28980 FOR_EACH_CHILD (die, c, resolve_addr (c));
28983 /* Helper routines for optimize_location_lists.
28984 This pass tries to share identical local lists in .debug_loc
28985 section. */
28987 /* Iteratively hash operands of LOC opcode into HSTATE. */
28989 static void
28990 hash_loc_operands (dw_loc_descr_ref loc, inchash::hash &hstate)
28992 dw_val_ref val1 = &loc->dw_loc_oprnd1;
28993 dw_val_ref val2 = &loc->dw_loc_oprnd2;
28995 switch (loc->dw_loc_opc)
28997 case DW_OP_const4u:
28998 case DW_OP_const8u:
28999 if (loc->dtprel)
29000 goto hash_addr;
29001 /* FALLTHRU */
29002 case DW_OP_const1u:
29003 case DW_OP_const1s:
29004 case DW_OP_const2u:
29005 case DW_OP_const2s:
29006 case DW_OP_const4s:
29007 case DW_OP_const8s:
29008 case DW_OP_constu:
29009 case DW_OP_consts:
29010 case DW_OP_pick:
29011 case DW_OP_plus_uconst:
29012 case DW_OP_breg0:
29013 case DW_OP_breg1:
29014 case DW_OP_breg2:
29015 case DW_OP_breg3:
29016 case DW_OP_breg4:
29017 case DW_OP_breg5:
29018 case DW_OP_breg6:
29019 case DW_OP_breg7:
29020 case DW_OP_breg8:
29021 case DW_OP_breg9:
29022 case DW_OP_breg10:
29023 case DW_OP_breg11:
29024 case DW_OP_breg12:
29025 case DW_OP_breg13:
29026 case DW_OP_breg14:
29027 case DW_OP_breg15:
29028 case DW_OP_breg16:
29029 case DW_OP_breg17:
29030 case DW_OP_breg18:
29031 case DW_OP_breg19:
29032 case DW_OP_breg20:
29033 case DW_OP_breg21:
29034 case DW_OP_breg22:
29035 case DW_OP_breg23:
29036 case DW_OP_breg24:
29037 case DW_OP_breg25:
29038 case DW_OP_breg26:
29039 case DW_OP_breg27:
29040 case DW_OP_breg28:
29041 case DW_OP_breg29:
29042 case DW_OP_breg30:
29043 case DW_OP_breg31:
29044 case DW_OP_regx:
29045 case DW_OP_fbreg:
29046 case DW_OP_piece:
29047 case DW_OP_deref_size:
29048 case DW_OP_xderef_size:
29049 hstate.add_object (val1->v.val_int);
29050 break;
29051 case DW_OP_skip:
29052 case DW_OP_bra:
29054 int offset;
29056 gcc_assert (val1->val_class == dw_val_class_loc);
29057 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
29058 hstate.add_object (offset);
29060 break;
29061 case DW_OP_implicit_value:
29062 hstate.add_object (val1->v.val_unsigned);
29063 switch (val2->val_class)
29065 case dw_val_class_const:
29066 hstate.add_object (val2->v.val_int);
29067 break;
29068 case dw_val_class_vec:
29070 unsigned int elt_size = val2->v.val_vec.elt_size;
29071 unsigned int len = val2->v.val_vec.length;
29073 hstate.add_int (elt_size);
29074 hstate.add_int (len);
29075 hstate.add (val2->v.val_vec.array, len * elt_size);
29077 break;
29078 case dw_val_class_const_double:
29079 hstate.add_object (val2->v.val_double.low);
29080 hstate.add_object (val2->v.val_double.high);
29081 break;
29082 case dw_val_class_wide_int:
29083 hstate.add (val2->v.val_wide->get_val (),
29084 get_full_len (*val2->v.val_wide)
29085 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
29086 break;
29087 case dw_val_class_addr:
29088 inchash::add_rtx (val2->v.val_addr, hstate);
29089 break;
29090 default:
29091 gcc_unreachable ();
29093 break;
29094 case DW_OP_bregx:
29095 case DW_OP_bit_piece:
29096 hstate.add_object (val1->v.val_int);
29097 hstate.add_object (val2->v.val_int);
29098 break;
29099 case DW_OP_addr:
29100 hash_addr:
29101 if (loc->dtprel)
29103 unsigned char dtprel = 0xd1;
29104 hstate.add_object (dtprel);
29106 inchash::add_rtx (val1->v.val_addr, hstate);
29107 break;
29108 case DW_OP_GNU_addr_index:
29109 case DW_OP_GNU_const_index:
29111 if (loc->dtprel)
29113 unsigned char dtprel = 0xd1;
29114 hstate.add_object (dtprel);
29116 inchash::add_rtx (val1->val_entry->addr.rtl, hstate);
29118 break;
29119 case DW_OP_implicit_pointer:
29120 case DW_OP_GNU_implicit_pointer:
29121 hstate.add_int (val2->v.val_int);
29122 break;
29123 case DW_OP_entry_value:
29124 case DW_OP_GNU_entry_value:
29125 hstate.add_object (val1->v.val_loc);
29126 break;
29127 case DW_OP_regval_type:
29128 case DW_OP_deref_type:
29129 case DW_OP_GNU_regval_type:
29130 case DW_OP_GNU_deref_type:
29132 unsigned int byte_size
29133 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
29134 unsigned int encoding
29135 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
29136 hstate.add_object (val1->v.val_int);
29137 hstate.add_object (byte_size);
29138 hstate.add_object (encoding);
29140 break;
29141 case DW_OP_convert:
29142 case DW_OP_reinterpret:
29143 case DW_OP_GNU_convert:
29144 case DW_OP_GNU_reinterpret:
29145 if (val1->val_class == dw_val_class_unsigned_const)
29147 hstate.add_object (val1->v.val_unsigned);
29148 break;
29150 /* FALLTHRU */
29151 case DW_OP_const_type:
29152 case DW_OP_GNU_const_type:
29154 unsigned int byte_size
29155 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
29156 unsigned int encoding
29157 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
29158 hstate.add_object (byte_size);
29159 hstate.add_object (encoding);
29160 if (loc->dw_loc_opc != DW_OP_const_type
29161 && loc->dw_loc_opc != DW_OP_GNU_const_type)
29162 break;
29163 hstate.add_object (val2->val_class);
29164 switch (val2->val_class)
29166 case dw_val_class_const:
29167 hstate.add_object (val2->v.val_int);
29168 break;
29169 case dw_val_class_vec:
29171 unsigned int elt_size = val2->v.val_vec.elt_size;
29172 unsigned int len = val2->v.val_vec.length;
29174 hstate.add_object (elt_size);
29175 hstate.add_object (len);
29176 hstate.add (val2->v.val_vec.array, len * elt_size);
29178 break;
29179 case dw_val_class_const_double:
29180 hstate.add_object (val2->v.val_double.low);
29181 hstate.add_object (val2->v.val_double.high);
29182 break;
29183 case dw_val_class_wide_int:
29184 hstate.add (val2->v.val_wide->get_val (),
29185 get_full_len (*val2->v.val_wide)
29186 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
29187 break;
29188 default:
29189 gcc_unreachable ();
29192 break;
29194 default:
29195 /* Other codes have no operands. */
29196 break;
29200 /* Iteratively hash the whole DWARF location expression LOC into HSTATE. */
29202 static inline void
29203 hash_locs (dw_loc_descr_ref loc, inchash::hash &hstate)
29205 dw_loc_descr_ref l;
29206 bool sizes_computed = false;
29207 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
29208 size_of_locs (loc);
29210 for (l = loc; l != NULL; l = l->dw_loc_next)
29212 enum dwarf_location_atom opc = l->dw_loc_opc;
29213 hstate.add_object (opc);
29214 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
29216 size_of_locs (loc);
29217 sizes_computed = true;
29219 hash_loc_operands (l, hstate);
29223 /* Compute hash of the whole location list LIST_HEAD. */
29225 static inline void
29226 hash_loc_list (dw_loc_list_ref list_head)
29228 dw_loc_list_ref curr = list_head;
29229 inchash::hash hstate;
29231 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
29233 hstate.add (curr->begin, strlen (curr->begin) + 1);
29234 hstate.add (curr->end, strlen (curr->end) + 1);
29235 if (curr->section)
29236 hstate.add (curr->section, strlen (curr->section) + 1);
29237 hash_locs (curr->expr, hstate);
29239 list_head->hash = hstate.end ();
29242 /* Return true if X and Y opcodes have the same operands. */
29244 static inline bool
29245 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
29247 dw_val_ref valx1 = &x->dw_loc_oprnd1;
29248 dw_val_ref valx2 = &x->dw_loc_oprnd2;
29249 dw_val_ref valy1 = &y->dw_loc_oprnd1;
29250 dw_val_ref valy2 = &y->dw_loc_oprnd2;
29252 switch (x->dw_loc_opc)
29254 case DW_OP_const4u:
29255 case DW_OP_const8u:
29256 if (x->dtprel)
29257 goto hash_addr;
29258 /* FALLTHRU */
29259 case DW_OP_const1u:
29260 case DW_OP_const1s:
29261 case DW_OP_const2u:
29262 case DW_OP_const2s:
29263 case DW_OP_const4s:
29264 case DW_OP_const8s:
29265 case DW_OP_constu:
29266 case DW_OP_consts:
29267 case DW_OP_pick:
29268 case DW_OP_plus_uconst:
29269 case DW_OP_breg0:
29270 case DW_OP_breg1:
29271 case DW_OP_breg2:
29272 case DW_OP_breg3:
29273 case DW_OP_breg4:
29274 case DW_OP_breg5:
29275 case DW_OP_breg6:
29276 case DW_OP_breg7:
29277 case DW_OP_breg8:
29278 case DW_OP_breg9:
29279 case DW_OP_breg10:
29280 case DW_OP_breg11:
29281 case DW_OP_breg12:
29282 case DW_OP_breg13:
29283 case DW_OP_breg14:
29284 case DW_OP_breg15:
29285 case DW_OP_breg16:
29286 case DW_OP_breg17:
29287 case DW_OP_breg18:
29288 case DW_OP_breg19:
29289 case DW_OP_breg20:
29290 case DW_OP_breg21:
29291 case DW_OP_breg22:
29292 case DW_OP_breg23:
29293 case DW_OP_breg24:
29294 case DW_OP_breg25:
29295 case DW_OP_breg26:
29296 case DW_OP_breg27:
29297 case DW_OP_breg28:
29298 case DW_OP_breg29:
29299 case DW_OP_breg30:
29300 case DW_OP_breg31:
29301 case DW_OP_regx:
29302 case DW_OP_fbreg:
29303 case DW_OP_piece:
29304 case DW_OP_deref_size:
29305 case DW_OP_xderef_size:
29306 return valx1->v.val_int == valy1->v.val_int;
29307 case DW_OP_skip:
29308 case DW_OP_bra:
29309 /* If splitting debug info, the use of DW_OP_GNU_addr_index
29310 can cause irrelevant differences in dw_loc_addr. */
29311 gcc_assert (valx1->val_class == dw_val_class_loc
29312 && valy1->val_class == dw_val_class_loc
29313 && (dwarf_split_debug_info
29314 || x->dw_loc_addr == y->dw_loc_addr));
29315 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
29316 case DW_OP_implicit_value:
29317 if (valx1->v.val_unsigned != valy1->v.val_unsigned
29318 || valx2->val_class != valy2->val_class)
29319 return false;
29320 switch (valx2->val_class)
29322 case dw_val_class_const:
29323 return valx2->v.val_int == valy2->v.val_int;
29324 case dw_val_class_vec:
29325 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
29326 && valx2->v.val_vec.length == valy2->v.val_vec.length
29327 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
29328 valx2->v.val_vec.elt_size
29329 * valx2->v.val_vec.length) == 0;
29330 case dw_val_class_const_double:
29331 return valx2->v.val_double.low == valy2->v.val_double.low
29332 && valx2->v.val_double.high == valy2->v.val_double.high;
29333 case dw_val_class_wide_int:
29334 return *valx2->v.val_wide == *valy2->v.val_wide;
29335 case dw_val_class_addr:
29336 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
29337 default:
29338 gcc_unreachable ();
29340 case DW_OP_bregx:
29341 case DW_OP_bit_piece:
29342 return valx1->v.val_int == valy1->v.val_int
29343 && valx2->v.val_int == valy2->v.val_int;
29344 case DW_OP_addr:
29345 hash_addr:
29346 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
29347 case DW_OP_GNU_addr_index:
29348 case DW_OP_GNU_const_index:
29350 rtx ax1 = valx1->val_entry->addr.rtl;
29351 rtx ay1 = valy1->val_entry->addr.rtl;
29352 return rtx_equal_p (ax1, ay1);
29354 case DW_OP_implicit_pointer:
29355 case DW_OP_GNU_implicit_pointer:
29356 return valx1->val_class == dw_val_class_die_ref
29357 && valx1->val_class == valy1->val_class
29358 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
29359 && valx2->v.val_int == valy2->v.val_int;
29360 case DW_OP_entry_value:
29361 case DW_OP_GNU_entry_value:
29362 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
29363 case DW_OP_const_type:
29364 case DW_OP_GNU_const_type:
29365 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
29366 || valx2->val_class != valy2->val_class)
29367 return false;
29368 switch (valx2->val_class)
29370 case dw_val_class_const:
29371 return valx2->v.val_int == valy2->v.val_int;
29372 case dw_val_class_vec:
29373 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
29374 && valx2->v.val_vec.length == valy2->v.val_vec.length
29375 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
29376 valx2->v.val_vec.elt_size
29377 * valx2->v.val_vec.length) == 0;
29378 case dw_val_class_const_double:
29379 return valx2->v.val_double.low == valy2->v.val_double.low
29380 && valx2->v.val_double.high == valy2->v.val_double.high;
29381 case dw_val_class_wide_int:
29382 return *valx2->v.val_wide == *valy2->v.val_wide;
29383 default:
29384 gcc_unreachable ();
29386 case DW_OP_regval_type:
29387 case DW_OP_deref_type:
29388 case DW_OP_GNU_regval_type:
29389 case DW_OP_GNU_deref_type:
29390 return valx1->v.val_int == valy1->v.val_int
29391 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
29392 case DW_OP_convert:
29393 case DW_OP_reinterpret:
29394 case DW_OP_GNU_convert:
29395 case DW_OP_GNU_reinterpret:
29396 if (valx1->val_class != valy1->val_class)
29397 return false;
29398 if (valx1->val_class == dw_val_class_unsigned_const)
29399 return valx1->v.val_unsigned == valy1->v.val_unsigned;
29400 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
29401 case DW_OP_GNU_parameter_ref:
29402 return valx1->val_class == dw_val_class_die_ref
29403 && valx1->val_class == valy1->val_class
29404 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
29405 default:
29406 /* Other codes have no operands. */
29407 return true;
29411 /* Return true if DWARF location expressions X and Y are the same. */
29413 static inline bool
29414 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
29416 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
29417 if (x->dw_loc_opc != y->dw_loc_opc
29418 || x->dtprel != y->dtprel
29419 || !compare_loc_operands (x, y))
29420 break;
29421 return x == NULL && y == NULL;
29424 /* Hashtable helpers. */
29426 struct loc_list_hasher : nofree_ptr_hash <dw_loc_list_struct>
29428 static inline hashval_t hash (const dw_loc_list_struct *);
29429 static inline bool equal (const dw_loc_list_struct *,
29430 const dw_loc_list_struct *);
29433 /* Return precomputed hash of location list X. */
29435 inline hashval_t
29436 loc_list_hasher::hash (const dw_loc_list_struct *x)
29438 return x->hash;
29441 /* Return true if location lists A and B are the same. */
29443 inline bool
29444 loc_list_hasher::equal (const dw_loc_list_struct *a,
29445 const dw_loc_list_struct *b)
29447 if (a == b)
29448 return 1;
29449 if (a->hash != b->hash)
29450 return 0;
29451 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
29452 if (strcmp (a->begin, b->begin) != 0
29453 || strcmp (a->end, b->end) != 0
29454 || (a->section == NULL) != (b->section == NULL)
29455 || (a->section && strcmp (a->section, b->section) != 0)
29456 || !compare_locs (a->expr, b->expr))
29457 break;
29458 return a == NULL && b == NULL;
29461 typedef hash_table<loc_list_hasher> loc_list_hash_type;
29464 /* Recursively optimize location lists referenced from DIE
29465 children and share them whenever possible. */
29467 static void
29468 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type *htab)
29470 dw_die_ref c;
29471 dw_attr_node *a;
29472 unsigned ix;
29473 dw_loc_list_struct **slot;
29475 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
29476 if (AT_class (a) == dw_val_class_loc_list)
29478 dw_loc_list_ref list = AT_loc_list (a);
29479 /* TODO: perform some optimizations here, before hashing
29480 it and storing into the hash table. */
29481 hash_loc_list (list);
29482 slot = htab->find_slot_with_hash (list, list->hash, INSERT);
29483 if (*slot == NULL)
29484 *slot = list;
29485 else
29486 a->dw_attr_val.v.val_loc_list = *slot;
29489 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
29493 /* Recursively assign each location list a unique index into the debug_addr
29494 section. */
29496 static void
29497 index_location_lists (dw_die_ref die)
29499 dw_die_ref c;
29500 dw_attr_node *a;
29501 unsigned ix;
29503 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
29504 if (AT_class (a) == dw_val_class_loc_list)
29506 dw_loc_list_ref list = AT_loc_list (a);
29507 dw_loc_list_ref curr;
29508 for (curr = list; curr != NULL; curr = curr->dw_loc_next)
29510 /* Don't index an entry that has already been indexed
29511 or won't be output. */
29512 if (curr->begin_entry != NULL
29513 || (strcmp (curr->begin, curr->end) == 0 && !curr->force))
29514 continue;
29516 curr->begin_entry
29517 = add_addr_table_entry (xstrdup (curr->begin), ate_kind_label);
29521 FOR_EACH_CHILD (die, c, index_location_lists (c));
29524 /* Optimize location lists referenced from DIE
29525 children and share them whenever possible. */
29527 static void
29528 optimize_location_lists (dw_die_ref die)
29530 loc_list_hash_type htab (500);
29531 optimize_location_lists_1 (die, &htab);
29534 /* Traverse the limbo die list, and add parent/child links. The only
29535 dies without parents that should be here are concrete instances of
29536 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
29537 For concrete instances, we can get the parent die from the abstract
29538 instance. */
29540 static void
29541 flush_limbo_die_list (void)
29543 limbo_die_node *node;
29545 /* get_context_die calls force_decl_die, which can put new DIEs on the
29546 limbo list in LTO mode when nested functions are put in a different
29547 partition than that of their parent function. */
29548 while ((node = limbo_die_list))
29550 dw_die_ref die = node->die;
29551 limbo_die_list = node->next;
29553 if (die->die_parent == NULL)
29555 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
29557 if (origin && origin->die_parent)
29558 add_child_die (origin->die_parent, die);
29559 else if (is_cu_die (die))
29561 else if (seen_error ())
29562 /* It's OK to be confused by errors in the input. */
29563 add_child_die (comp_unit_die (), die);
29564 else
29566 /* In certain situations, the lexical block containing a
29567 nested function can be optimized away, which results
29568 in the nested function die being orphaned. Likewise
29569 with the return type of that nested function. Force
29570 this to be a child of the containing function.
29572 It may happen that even the containing function got fully
29573 inlined and optimized out. In that case we are lost and
29574 assign the empty child. This should not be big issue as
29575 the function is likely unreachable too. */
29576 gcc_assert (node->created_for);
29578 if (DECL_P (node->created_for))
29579 origin = get_context_die (DECL_CONTEXT (node->created_for));
29580 else if (TYPE_P (node->created_for))
29581 origin = scope_die_for (node->created_for, comp_unit_die ());
29582 else
29583 origin = comp_unit_die ();
29585 add_child_die (origin, die);
29591 /* Output stuff that dwarf requires at the end of every file,
29592 and generate the DWARF-2 debugging info. */
29594 static void
29595 dwarf2out_finish (const char *)
29597 comdat_type_node *ctnode;
29598 dw_die_ref main_comp_unit_die;
29599 unsigned char checksum[16];
29601 /* Flush out any latecomers to the limbo party. */
29602 flush_limbo_die_list ();
29604 if (flag_checking)
29606 verify_die (comp_unit_die ());
29607 for (limbo_die_node *node = cu_die_list; node; node = node->next)
29608 verify_die (node->die);
29611 /* We shouldn't have any symbols with delayed asm names for
29612 DIEs generated after early finish. */
29613 gcc_assert (deferred_asm_name == NULL);
29615 gen_remaining_tmpl_value_param_die_attribute ();
29617 #if ENABLE_ASSERT_CHECKING
29619 dw_die_ref die = comp_unit_die (), c;
29620 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
29622 #endif
29623 resolve_addr (comp_unit_die ());
29624 move_marked_base_types ();
29626 /* Initialize sections and labels used for actual assembler output. */
29627 init_sections_and_labels ();
29629 /* Traverse the DIE's and add sibling attributes to those DIE's that
29630 have children. */
29631 add_sibling_attributes (comp_unit_die ());
29632 limbo_die_node *node;
29633 for (node = cu_die_list; node; node = node->next)
29634 add_sibling_attributes (node->die);
29635 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
29636 add_sibling_attributes (ctnode->root_die);
29638 /* When splitting DWARF info, we put some attributes in the
29639 skeleton compile_unit DIE that remains in the .o, while
29640 most attributes go in the DWO compile_unit_die. */
29641 if (dwarf_split_debug_info)
29643 limbo_die_node *cu;
29644 main_comp_unit_die = gen_compile_unit_die (NULL);
29645 if (dwarf_version >= 5)
29646 main_comp_unit_die->die_tag = DW_TAG_skeleton_unit;
29647 cu = limbo_die_list;
29648 gcc_assert (cu->die == main_comp_unit_die);
29649 limbo_die_list = limbo_die_list->next;
29650 cu->next = cu_die_list;
29651 cu_die_list = cu;
29653 else
29654 main_comp_unit_die = comp_unit_die ();
29656 /* Output a terminator label for the .text section. */
29657 switch_to_section (text_section);
29658 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
29659 if (cold_text_section)
29661 switch_to_section (cold_text_section);
29662 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
29665 /* We can only use the low/high_pc attributes if all of the code was
29666 in .text. */
29667 if (!have_multiple_function_sections
29668 || (dwarf_version < 3 && dwarf_strict))
29670 /* Don't add if the CU has no associated code. */
29671 if (text_section_used)
29672 add_AT_low_high_pc (main_comp_unit_die, text_section_label,
29673 text_end_label, true);
29675 else
29677 unsigned fde_idx;
29678 dw_fde_ref fde;
29679 bool range_list_added = false;
29681 if (text_section_used)
29682 add_ranges_by_labels (main_comp_unit_die, text_section_label,
29683 text_end_label, &range_list_added, true);
29684 if (cold_text_section_used)
29685 add_ranges_by_labels (main_comp_unit_die, cold_text_section_label,
29686 cold_end_label, &range_list_added, true);
29688 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
29690 if (DECL_IGNORED_P (fde->decl))
29691 continue;
29692 if (!fde->in_std_section)
29693 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
29694 fde->dw_fde_end, &range_list_added,
29695 true);
29696 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
29697 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
29698 fde->dw_fde_second_end, &range_list_added,
29699 true);
29702 if (range_list_added)
29704 /* We need to give .debug_loc and .debug_ranges an appropriate
29705 "base address". Use zero so that these addresses become
29706 absolute. Historically, we've emitted the unexpected
29707 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
29708 Emit both to give time for other tools to adapt. */
29709 add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
29710 if (! dwarf_strict && dwarf_version < 4)
29711 add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
29713 add_ranges (NULL);
29717 if (debug_info_level >= DINFO_LEVEL_TERSE)
29718 add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
29719 debug_line_section_label);
29721 if (have_macinfo)
29722 add_AT_macptr (comp_unit_die (),
29723 dwarf_version >= 5 ? DW_AT_macros
29724 : dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros,
29725 macinfo_section_label);
29727 if (dwarf_split_debug_info)
29729 if (have_location_lists)
29731 if (dwarf_version >= 5)
29732 add_AT_loclistsptr (comp_unit_die (), DW_AT_loclists_base,
29733 loc_section_label);
29734 /* optimize_location_lists calculates the size of the lists,
29735 so index them first, and assign indices to the entries.
29736 Although optimize_location_lists will remove entries from
29737 the table, it only does so for duplicates, and therefore
29738 only reduces ref_counts to 1. */
29739 index_location_lists (comp_unit_die ());
29742 if (addr_index_table != NULL)
29744 unsigned int index = 0;
29745 addr_index_table
29746 ->traverse_noresize<unsigned int *, index_addr_table_entry>
29747 (&index);
29751 loc_list_idx = 0;
29752 if (have_location_lists)
29754 optimize_location_lists (comp_unit_die ());
29755 /* And finally assign indexes to the entries for -gsplit-dwarf. */
29756 if (dwarf_version >= 5 && dwarf_split_debug_info)
29757 assign_location_list_indexes (comp_unit_die ());
29760 save_macinfo_strings ();
29762 if (dwarf_split_debug_info)
29764 unsigned int index = 0;
29766 /* Add attributes common to skeleton compile_units and
29767 type_units. Because these attributes include strings, it
29768 must be done before freezing the string table. Top-level
29769 skeleton die attrs are added when the skeleton type unit is
29770 created, so ensure it is created by this point. */
29771 add_top_level_skeleton_die_attrs (main_comp_unit_die);
29772 debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
29775 /* Output all of the compilation units. We put the main one last so that
29776 the offsets are available to output_pubnames. */
29777 for (node = cu_die_list; node; node = node->next)
29778 output_comp_unit (node->die, 0, NULL);
29780 hash_table<comdat_type_hasher> comdat_type_table (100);
29781 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
29783 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
29785 /* Don't output duplicate types. */
29786 if (*slot != HTAB_EMPTY_ENTRY)
29787 continue;
29789 /* Add a pointer to the line table for the main compilation unit
29790 so that the debugger can make sense of DW_AT_decl_file
29791 attributes. */
29792 if (debug_info_level >= DINFO_LEVEL_TERSE)
29793 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
29794 (!dwarf_split_debug_info
29795 ? debug_line_section_label
29796 : debug_skeleton_line_section_label));
29798 output_comdat_type_unit (ctnode);
29799 *slot = ctnode;
29802 /* The AT_pubnames attribute needs to go in all skeleton dies, including
29803 both the main_cu and all skeleton TUs. Making this call unconditional
29804 would end up either adding a second copy of the AT_pubnames attribute, or
29805 requiring a special case in add_top_level_skeleton_die_attrs. */
29806 if (!dwarf_split_debug_info)
29807 add_AT_pubnames (comp_unit_die ());
29809 if (dwarf_split_debug_info)
29811 int mark;
29812 struct md5_ctx ctx;
29814 if (dwarf_version >= 5 && !vec_safe_is_empty (ranges_table))
29815 index_rnglists ();
29817 /* Compute a checksum of the comp_unit to use as the dwo_id. */
29818 md5_init_ctx (&ctx);
29819 mark = 0;
29820 die_checksum (comp_unit_die (), &ctx, &mark);
29821 unmark_all_dies (comp_unit_die ());
29822 md5_finish_ctx (&ctx, checksum);
29824 if (dwarf_version < 5)
29826 /* Use the first 8 bytes of the checksum as the dwo_id,
29827 and add it to both comp-unit DIEs. */
29828 add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
29829 add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
29832 /* Add the base offset of the ranges table to the skeleton
29833 comp-unit DIE. */
29834 if (!vec_safe_is_empty (ranges_table))
29836 if (dwarf_version >= 5)
29837 add_AT_lineptr (main_comp_unit_die, DW_AT_rnglists_base,
29838 ranges_base_label);
29839 else
29840 add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
29841 ranges_section_label);
29844 switch_to_section (debug_addr_section);
29845 ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
29846 output_addr_table ();
29849 /* Output the main compilation unit if non-empty or if .debug_macinfo
29850 or .debug_macro will be emitted. */
29851 output_comp_unit (comp_unit_die (), have_macinfo,
29852 dwarf_split_debug_info ? checksum : NULL);
29854 if (dwarf_split_debug_info && info_section_emitted)
29855 output_skeleton_debug_sections (main_comp_unit_die, checksum);
29857 /* Output the abbreviation table. */
29858 if (vec_safe_length (abbrev_die_table) != 1)
29860 switch_to_section (debug_abbrev_section);
29861 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
29862 output_abbrev_section ();
29865 /* Output location list section if necessary. */
29866 if (have_location_lists)
29868 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
29869 char l2[MAX_ARTIFICIAL_LABEL_BYTES];
29870 /* Output the location lists info. */
29871 switch_to_section (debug_loc_section);
29872 if (dwarf_version >= 5)
29874 ASM_GENERATE_INTERNAL_LABEL (l1, DEBUG_LOC_SECTION_LABEL, 1);
29875 ASM_GENERATE_INTERNAL_LABEL (l2, DEBUG_LOC_SECTION_LABEL, 2);
29876 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
29877 dw2_asm_output_data (4, 0xffffffff,
29878 "Initial length escape value indicating "
29879 "64-bit DWARF extension");
29880 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
29881 "Length of Location Lists");
29882 ASM_OUTPUT_LABEL (asm_out_file, l1);
29883 dw2_asm_output_data (2, dwarf_version, "DWARF Version");
29884 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
29885 dw2_asm_output_data (1, 0, "Segment Size");
29886 dw2_asm_output_data (4, dwarf_split_debug_info ? loc_list_idx : 0,
29887 "Offset Entry Count");
29889 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
29890 if (dwarf_version >= 5 && dwarf_split_debug_info)
29892 unsigned int save_loc_list_idx = loc_list_idx;
29893 loc_list_idx = 0;
29894 output_loclists_offsets (comp_unit_die ());
29895 gcc_assert (save_loc_list_idx == loc_list_idx);
29897 output_location_lists (comp_unit_die ());
29898 if (dwarf_version >= 5)
29899 ASM_OUTPUT_LABEL (asm_out_file, l2);
29902 output_pubtables ();
29904 /* Output the address range information if a CU (.debug_info section)
29905 was emitted. We output an empty table even if we had no functions
29906 to put in it. This because the consumer has no way to tell the
29907 difference between an empty table that we omitted and failure to
29908 generate a table that would have contained data. */
29909 if (info_section_emitted)
29911 switch_to_section (debug_aranges_section);
29912 output_aranges ();
29915 /* Output ranges section if necessary. */
29916 if (!vec_safe_is_empty (ranges_table))
29918 if (dwarf_version >= 5)
29919 output_rnglists ();
29920 else
29921 output_ranges ();
29924 /* Have to end the macro section. */
29925 if (have_macinfo)
29927 switch_to_section (debug_macinfo_section);
29928 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
29929 output_macinfo ();
29930 dw2_asm_output_data (1, 0, "End compilation unit");
29933 /* Output the source line correspondence table. We must do this
29934 even if there is no line information. Otherwise, on an empty
29935 translation unit, we will generate a present, but empty,
29936 .debug_info section. IRIX 6.5 `nm' will then complain when
29937 examining the file. This is done late so that any filenames
29938 used by the debug_info section are marked as 'used'. */
29939 switch_to_section (debug_line_section);
29940 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
29941 if (! DWARF2_ASM_LINE_DEBUG_INFO)
29942 output_line_info (false);
29944 if (dwarf_split_debug_info && info_section_emitted)
29946 switch_to_section (debug_skeleton_line_section);
29947 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
29948 output_line_info (true);
29951 /* If we emitted any indirect strings, output the string table too. */
29952 if (debug_str_hash || skeleton_debug_str_hash)
29953 output_indirect_strings ();
29954 if (debug_line_str_hash)
29956 switch_to_section (debug_line_str_section);
29957 const enum dwarf_form form = DW_FORM_line_strp;
29958 debug_line_str_hash->traverse<enum dwarf_form,
29959 output_indirect_string> (form);
29963 /* Returns a hash value for X (which really is a variable_value_struct). */
29965 inline hashval_t
29966 variable_value_hasher::hash (variable_value_struct *x)
29968 return (hashval_t) x->decl_id;
29971 /* Return nonzero if decl_id of variable_value_struct X is the same as
29972 UID of decl Y. */
29974 inline bool
29975 variable_value_hasher::equal (variable_value_struct *x, tree y)
29977 return x->decl_id == DECL_UID (y);
29980 /* Helper function for resolve_variable_value, handle
29981 DW_OP_GNU_variable_value in one location expression.
29982 Return true if exprloc has been changed into loclist. */
29984 static bool
29985 resolve_variable_value_in_expr (dw_attr_node *a, dw_loc_descr_ref loc)
29987 dw_loc_descr_ref next;
29988 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = next)
29990 next = loc->dw_loc_next;
29991 if (loc->dw_loc_opc != DW_OP_GNU_variable_value
29992 || loc->dw_loc_oprnd1.val_class != dw_val_class_decl_ref)
29993 continue;
29995 tree decl = loc->dw_loc_oprnd1.v.val_decl_ref;
29996 if (DECL_CONTEXT (decl) != current_function_decl)
29997 continue;
29999 dw_die_ref ref = lookup_decl_die (decl);
30000 if (ref)
30002 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30003 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
30004 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
30005 continue;
30007 dw_loc_list_ref l = loc_list_from_tree (decl, 0, NULL);
30008 if (l == NULL)
30009 continue;
30010 if (l->dw_loc_next)
30012 if (AT_class (a) != dw_val_class_loc)
30013 continue;
30014 switch (a->dw_attr)
30016 /* Following attributes allow both exprloc and loclist
30017 classes, so we can change them into a loclist. */
30018 case DW_AT_location:
30019 case DW_AT_string_length:
30020 case DW_AT_return_addr:
30021 case DW_AT_data_member_location:
30022 case DW_AT_frame_base:
30023 case DW_AT_segment:
30024 case DW_AT_static_link:
30025 case DW_AT_use_location:
30026 case DW_AT_vtable_elem_location:
30027 if (prev)
30029 prev->dw_loc_next = NULL;
30030 prepend_loc_descr_to_each (l, AT_loc (a));
30032 if (next)
30033 add_loc_descr_to_each (l, next);
30034 a->dw_attr_val.val_class = dw_val_class_loc_list;
30035 a->dw_attr_val.val_entry = NULL;
30036 a->dw_attr_val.v.val_loc_list = l;
30037 have_location_lists = true;
30038 return true;
30039 /* Following attributes allow both exprloc and reference,
30040 so if the whole expression is DW_OP_GNU_variable_value alone
30041 we could transform it into reference. */
30042 case DW_AT_byte_size:
30043 case DW_AT_bit_size:
30044 case DW_AT_lower_bound:
30045 case DW_AT_upper_bound:
30046 case DW_AT_bit_stride:
30047 case DW_AT_count:
30048 case DW_AT_allocated:
30049 case DW_AT_associated:
30050 case DW_AT_byte_stride:
30051 if (prev == NULL && next == NULL)
30052 break;
30053 /* FALLTHRU */
30054 default:
30055 if (dwarf_strict)
30056 continue;
30057 break;
30059 /* Create DW_TAG_variable that we can refer to. */
30060 ref = gen_decl_die (decl, NULL_TREE, NULL,
30061 lookup_decl_die (current_function_decl));
30062 if (ref)
30064 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30065 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
30066 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
30068 continue;
30070 if (prev)
30072 prev->dw_loc_next = l->expr;
30073 add_loc_descr (&prev->dw_loc_next, next);
30074 free_loc_descr (loc, NULL);
30075 next = prev->dw_loc_next;
30077 else
30079 memcpy (loc, l->expr, sizeof (dw_loc_descr_node));
30080 add_loc_descr (&loc, next);
30081 next = loc;
30083 loc = prev;
30085 return false;
30088 /* Attempt to resolve DW_OP_GNU_variable_value using loc_list_from_tree. */
30090 static void
30091 resolve_variable_value (dw_die_ref die)
30093 dw_attr_node *a;
30094 dw_loc_list_ref loc;
30095 unsigned ix;
30097 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
30098 switch (AT_class (a))
30100 case dw_val_class_loc:
30101 if (!resolve_variable_value_in_expr (a, AT_loc (a)))
30102 break;
30103 /* FALLTHRU */
30104 case dw_val_class_loc_list:
30105 loc = AT_loc_list (a);
30106 gcc_assert (loc);
30107 for (; loc; loc = loc->dw_loc_next)
30108 resolve_variable_value_in_expr (a, loc->expr);
30109 break;
30110 default:
30111 break;
30115 /* Attempt to optimize DW_OP_GNU_variable_value refering to
30116 temporaries in the current function. */
30118 static void
30119 resolve_variable_values (void)
30121 if (!variable_value_hash || !current_function_decl)
30122 return;
30124 struct variable_value_struct *node
30125 = variable_value_hash->find_with_hash (current_function_decl,
30126 DECL_UID (current_function_decl));
30128 if (node == NULL)
30129 return;
30131 unsigned int i;
30132 dw_die_ref die;
30133 FOR_EACH_VEC_SAFE_ELT (node->dies, i, die)
30134 resolve_variable_value (die);
30137 /* Helper function for note_variable_value, handle one location
30138 expression. */
30140 static void
30141 note_variable_value_in_expr (dw_die_ref die, dw_loc_descr_ref loc)
30143 for (; loc; loc = loc->dw_loc_next)
30144 if (loc->dw_loc_opc == DW_OP_GNU_variable_value
30145 && loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
30147 tree decl = loc->dw_loc_oprnd1.v.val_decl_ref;
30148 dw_die_ref ref = lookup_decl_die (decl);
30149 if (ref)
30151 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30152 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
30153 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
30155 if (VAR_P (decl)
30156 && DECL_CONTEXT (decl)
30157 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL
30158 && lookup_decl_die (DECL_CONTEXT (decl)))
30160 if (!variable_value_hash)
30161 variable_value_hash
30162 = hash_table<variable_value_hasher>::create_ggc (10);
30164 tree fndecl = DECL_CONTEXT (decl);
30165 struct variable_value_struct *node;
30166 struct variable_value_struct **slot
30167 = variable_value_hash->find_slot_with_hash (fndecl,
30168 DECL_UID (fndecl),
30169 INSERT);
30170 if (*slot == NULL)
30172 node = ggc_cleared_alloc<variable_value_struct> ();
30173 node->decl_id = DECL_UID (fndecl);
30174 *slot = node;
30176 else
30177 node = *slot;
30179 vec_safe_push (node->dies, die);
30184 /* Walk the tree DIE and note DIEs with DW_OP_GNU_variable_value still
30185 with dw_val_class_decl_ref operand. */
30187 static void
30188 note_variable_value (dw_die_ref die)
30190 dw_die_ref c;
30191 dw_attr_node *a;
30192 dw_loc_list_ref loc;
30193 unsigned ix;
30195 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
30196 switch (AT_class (a))
30198 case dw_val_class_loc_list:
30199 loc = AT_loc_list (a);
30200 gcc_assert (loc);
30201 if (!loc->noted_variable_value)
30203 loc->noted_variable_value = 1;
30204 for (; loc; loc = loc->dw_loc_next)
30205 note_variable_value_in_expr (die, loc->expr);
30207 break;
30208 case dw_val_class_loc:
30209 note_variable_value_in_expr (die, AT_loc (a));
30210 break;
30211 default:
30212 break;
30215 /* Mark children. */
30216 FOR_EACH_CHILD (die, c, note_variable_value (c));
30219 /* Perform any cleanups needed after the early debug generation pass
30220 has run. */
30222 static void
30223 dwarf2out_early_finish (const char *filename)
30225 set_early_dwarf s;
30227 /* PCH might result in DW_AT_producer string being restored from the
30228 header compilation, so always fill it with empty string initially
30229 and overwrite only here. */
30230 dw_attr_node *producer = get_AT (comp_unit_die (), DW_AT_producer);
30231 producer_string = gen_producer_string ();
30232 producer->dw_attr_val.v.val_str->refcount--;
30233 producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
30235 /* Add the name for the main input file now. We delayed this from
30236 dwarf2out_init to avoid complications with PCH. */
30237 add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
30238 add_comp_dir_attribute (comp_unit_die ());
30240 /* When emitting DWARF5 .debug_line_str, move DW_AT_name and
30241 DW_AT_comp_dir into .debug_line_str section. */
30242 if (!DWARF2_ASM_LINE_DEBUG_INFO
30243 && dwarf_version >= 5
30244 && DWARF5_USE_DEBUG_LINE_STR)
30246 for (int i = 0; i < 2; i++)
30248 dw_attr_node *a = get_AT (comp_unit_die (),
30249 i ? DW_AT_comp_dir : DW_AT_name);
30250 if (a == NULL
30251 || AT_class (a) != dw_val_class_str
30252 || strlen (AT_string (a)) + 1 <= DWARF_OFFSET_SIZE)
30253 continue;
30255 if (! debug_line_str_hash)
30256 debug_line_str_hash
30257 = hash_table<indirect_string_hasher>::create_ggc (10);
30259 struct indirect_string_node *node
30260 = find_AT_string_in_table (AT_string (a), debug_line_str_hash);
30261 set_indirect_string (node);
30262 node->form = DW_FORM_line_strp;
30263 a->dw_attr_val.v.val_str->refcount--;
30264 a->dw_attr_val.v.val_str = node;
30268 /* With LTO early dwarf was really finished at compile-time, so make
30269 sure to adjust the phase after annotating the LTRANS CU DIE. */
30270 if (in_lto_p)
30272 early_dwarf_finished = true;
30273 return;
30276 /* Walk through the list of incomplete types again, trying once more to
30277 emit full debugging info for them. */
30278 retry_incomplete_types ();
30280 /* The point here is to flush out the limbo list so that it is empty
30281 and we don't need to stream it for LTO. */
30282 flush_limbo_die_list ();
30284 gen_scheduled_generic_parms_dies ();
30285 gen_remaining_tmpl_value_param_die_attribute ();
30287 /* Add DW_AT_linkage_name for all deferred DIEs. */
30288 for (limbo_die_node *node = deferred_asm_name; node; node = node->next)
30290 tree decl = node->created_for;
30291 if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
30292 /* A missing DECL_ASSEMBLER_NAME can be a constant DIE that
30293 ended up in deferred_asm_name before we knew it was
30294 constant and never written to disk. */
30295 && DECL_ASSEMBLER_NAME (decl))
30297 add_linkage_attr (node->die, decl);
30298 move_linkage_attr (node->die);
30301 deferred_asm_name = NULL;
30303 if (flag_eliminate_unused_debug_types)
30304 prune_unused_types ();
30306 /* Generate separate COMDAT sections for type DIEs. */
30307 if (use_debug_types)
30309 break_out_comdat_types (comp_unit_die ());
30311 /* Each new type_unit DIE was added to the limbo die list when created.
30312 Since these have all been added to comdat_type_list, clear the
30313 limbo die list. */
30314 limbo_die_list = NULL;
30316 /* For each new comdat type unit, copy declarations for incomplete
30317 types to make the new unit self-contained (i.e., no direct
30318 references to the main compile unit). */
30319 for (comdat_type_node *ctnode = comdat_type_list;
30320 ctnode != NULL; ctnode = ctnode->next)
30321 copy_decls_for_unworthy_types (ctnode->root_die);
30322 copy_decls_for_unworthy_types (comp_unit_die ());
30324 /* In the process of copying declarations from one unit to another,
30325 we may have left some declarations behind that are no longer
30326 referenced. Prune them. */
30327 prune_unused_types ();
30330 /* Generate separate CUs for each of the include files we've seen.
30331 They will go into limbo_die_list and from there to cu_die_list. */
30332 if (flag_eliminate_dwarf2_dups)
30334 gcc_assert (limbo_die_list == NULL);
30335 break_out_includes (comp_unit_die ());
30336 limbo_die_node *cu;
30337 while ((cu = limbo_die_list))
30339 limbo_die_list = cu->next;
30340 cu->next = cu_die_list;
30341 cu_die_list = cu;
30345 /* Traverse the DIE's and note DIEs with DW_OP_GNU_variable_value still
30346 with dw_val_class_decl_ref operand. */
30347 note_variable_value (comp_unit_die ());
30348 for (limbo_die_node *node = cu_die_list; node; node = node->next)
30349 note_variable_value (node->die);
30350 for (comdat_type_node *ctnode = comdat_type_list; ctnode != NULL;
30351 ctnode = ctnode->next)
30352 note_variable_value (ctnode->root_die);
30353 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
30354 note_variable_value (node->die);
30356 /* The early debug phase is now finished. */
30357 early_dwarf_finished = true;
30360 /* Reset all state within dwarf2out.c so that we can rerun the compiler
30361 within the same process. For use by toplev::finalize. */
30363 void
30364 dwarf2out_c_finalize (void)
30366 last_var_location_insn = NULL;
30367 cached_next_real_insn = NULL;
30368 used_rtx_array = NULL;
30369 incomplete_types = NULL;
30370 decl_scope_table = NULL;
30371 debug_info_section = NULL;
30372 debug_skeleton_info_section = NULL;
30373 debug_abbrev_section = NULL;
30374 debug_skeleton_abbrev_section = NULL;
30375 debug_aranges_section = NULL;
30376 debug_addr_section = NULL;
30377 debug_macinfo_section = NULL;
30378 debug_line_section = NULL;
30379 debug_skeleton_line_section = NULL;
30380 debug_loc_section = NULL;
30381 debug_pubnames_section = NULL;
30382 debug_pubtypes_section = NULL;
30383 debug_str_section = NULL;
30384 debug_line_str_section = NULL;
30385 debug_str_dwo_section = NULL;
30386 debug_str_offsets_section = NULL;
30387 debug_ranges_section = NULL;
30388 debug_frame_section = NULL;
30389 fde_vec = NULL;
30390 debug_str_hash = NULL;
30391 debug_line_str_hash = NULL;
30392 skeleton_debug_str_hash = NULL;
30393 dw2_string_counter = 0;
30394 have_multiple_function_sections = false;
30395 text_section_used = false;
30396 cold_text_section_used = false;
30397 cold_text_section = NULL;
30398 current_unit_personality = NULL;
30400 early_dwarf = false;
30401 early_dwarf_finished = false;
30403 next_die_offset = 0;
30404 single_comp_unit_die = NULL;
30405 comdat_type_list = NULL;
30406 limbo_die_list = NULL;
30407 file_table = NULL;
30408 decl_die_table = NULL;
30409 common_block_die_table = NULL;
30410 decl_loc_table = NULL;
30411 call_arg_locations = NULL;
30412 call_arg_loc_last = NULL;
30413 call_site_count = -1;
30414 tail_call_site_count = -1;
30415 cached_dw_loc_list_table = NULL;
30416 abbrev_die_table = NULL;
30417 delete dwarf_proc_stack_usage_map;
30418 dwarf_proc_stack_usage_map = NULL;
30419 line_info_label_num = 0;
30420 cur_line_info_table = NULL;
30421 text_section_line_info = NULL;
30422 cold_text_section_line_info = NULL;
30423 separate_line_info = NULL;
30424 info_section_emitted = false;
30425 pubname_table = NULL;
30426 pubtype_table = NULL;
30427 macinfo_table = NULL;
30428 ranges_table = NULL;
30429 ranges_by_label = NULL;
30430 rnglist_idx = 0;
30431 have_location_lists = false;
30432 loclabel_num = 0;
30433 poc_label_num = 0;
30434 last_emitted_file = NULL;
30435 label_num = 0;
30436 tmpl_value_parm_die_table = NULL;
30437 generic_type_instances = NULL;
30438 frame_pointer_fb_offset = 0;
30439 frame_pointer_fb_offset_valid = false;
30440 base_types.release ();
30441 XDELETEVEC (producer_string);
30442 producer_string = NULL;
30445 #include "gt-dwarf2out.h"