Small ChangeLog tweak.
[official-gcc.git] / gcc / dwarf2out.c
blob5ff45eb4efdc611ec38621fcb4872e4c227d7b1f
1 /* Output Dwarf2 format symbol table information from GCC.
2 Copyright (C) 1992-2017 Free Software Foundation, Inc.
3 Contributed by Gary Funck (gary@intrepid.com).
4 Derived from DWARF 1 implementation of Ron Guilmette (rfg@monkeys.com).
5 Extensively modified by Jason Merrill (jason@cygnus.com).
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
23 /* TODO: Emit .debug_line header even when there are no functions, since
24 the file numbers are used by .debug_info. Alternately, leave
25 out locations for types and decls.
26 Avoid talking about ctors and op= for PODs.
27 Factor out common prologue sequences into multiple CIEs. */
29 /* The first part of this file deals with the DWARF 2 frame unwind
30 information, which is also used by the GCC efficient exception handling
31 mechanism. The second part, controlled only by an #ifdef
32 DWARF2_DEBUGGING_INFO, deals with the other DWARF 2 debugging
33 information. */
35 /* DWARF2 Abbreviation Glossary:
37 CFA = Canonical Frame Address
38 a fixed address on the stack which identifies a call frame.
39 We define it to be the value of SP just before the call insn.
40 The CFA register and offset, which may change during the course
41 of the function, are used to calculate its value at runtime.
43 CFI = Call Frame Instruction
44 an instruction for the DWARF2 abstract machine
46 CIE = Common Information Entry
47 information describing information common to one or more FDEs
49 DIE = Debugging Information Entry
51 FDE = Frame Description Entry
52 information describing the stack call frame, in particular,
53 how to restore registers
55 DW_CFA_... = DWARF2 CFA call frame instruction
56 DW_TAG_... = DWARF2 DIE tag */
58 #include "config.h"
59 #include "system.h"
60 #include "coretypes.h"
61 #include "target.h"
62 #include "function.h"
63 #include "rtl.h"
64 #include "tree.h"
65 #include "memmodel.h"
66 #include "tm_p.h"
67 #include "stringpool.h"
68 #include "insn-config.h"
69 #include "ira.h"
70 #include "cgraph.h"
71 #include "diagnostic.h"
72 #include "fold-const.h"
73 #include "stor-layout.h"
74 #include "varasm.h"
75 #include "version.h"
76 #include "flags.h"
77 #include "rtlhash.h"
78 #include "reload.h"
79 #include "output.h"
80 #include "expr.h"
81 #include "dwarf2out.h"
82 #include "dwarf2asm.h"
83 #include "toplev.h"
84 #include "md5.h"
85 #include "tree-pretty-print.h"
86 #include "debug.h"
87 #include "common/common-target.h"
88 #include "langhooks.h"
89 #include "lra.h"
90 #include "dumpfile.h"
91 #include "opts.h"
92 #include "tree-dfa.h"
93 #include "gdb/gdb-index.h"
94 #include "rtl-iter.h"
96 static void dwarf2out_source_line (unsigned int, unsigned int, const char *,
97 int, bool);
98 static rtx_insn *last_var_location_insn;
99 static rtx_insn *cached_next_real_insn;
100 static void dwarf2out_decl (tree);
102 #ifndef XCOFF_DEBUGGING_INFO
103 #define XCOFF_DEBUGGING_INFO 0
104 #endif
106 #ifndef HAVE_XCOFF_DWARF_EXTRAS
107 #define HAVE_XCOFF_DWARF_EXTRAS 0
108 #endif
110 #ifdef VMS_DEBUGGING_INFO
111 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
113 /* Define this macro to be a nonzero value if the directory specifications
114 which are output in the debug info should end with a separator. */
115 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
116 /* Define this macro to evaluate to a nonzero value if GCC should refrain
117 from generating indirect strings in DWARF2 debug information, for instance
118 if your target is stuck with an old version of GDB that is unable to
119 process them properly or uses VMS Debug. */
120 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
121 #else
122 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
123 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
124 #endif
126 /* ??? Poison these here until it can be done generically. They've been
127 totally replaced in this file; make sure it stays that way. */
128 #undef DWARF2_UNWIND_INFO
129 #undef DWARF2_FRAME_INFO
130 #if (GCC_VERSION >= 3000)
131 #pragma GCC poison DWARF2_UNWIND_INFO DWARF2_FRAME_INFO
132 #endif
134 /* The size of the target's pointer type. */
135 #ifndef PTR_SIZE
136 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
137 #endif
139 /* Array of RTXes referenced by the debugging information, which therefore
140 must be kept around forever. */
141 static GTY(()) vec<rtx, va_gc> *used_rtx_array;
143 /* A pointer to the base of a list of incomplete types which might be
144 completed at some later time. incomplete_types_list needs to be a
145 vec<tree, va_gc> *because we want to tell the garbage collector about
146 it. */
147 static GTY(()) vec<tree, va_gc> *incomplete_types;
149 /* A pointer to the base of a table of references to declaration
150 scopes. This table is a display which tracks the nesting
151 of declaration scopes at the current scope and containing
152 scopes. This table is used to find the proper place to
153 define type declaration DIE's. */
154 static GTY(()) vec<tree, va_gc> *decl_scope_table;
156 /* Pointers to various DWARF2 sections. */
157 static GTY(()) section *debug_info_section;
158 static GTY(()) section *debug_skeleton_info_section;
159 static GTY(()) section *debug_abbrev_section;
160 static GTY(()) section *debug_skeleton_abbrev_section;
161 static GTY(()) section *debug_aranges_section;
162 static GTY(()) section *debug_addr_section;
163 static GTY(()) section *debug_macinfo_section;
164 static const char *debug_macinfo_section_name;
165 static GTY(()) section *debug_line_section;
166 static GTY(()) section *debug_skeleton_line_section;
167 static GTY(()) section *debug_loc_section;
168 static GTY(()) section *debug_pubnames_section;
169 static GTY(()) section *debug_pubtypes_section;
170 static GTY(()) section *debug_str_section;
171 static GTY(()) section *debug_line_str_section;
172 static GTY(()) section *debug_str_dwo_section;
173 static GTY(()) section *debug_str_offsets_section;
174 static GTY(()) section *debug_ranges_section;
175 static GTY(()) section *debug_frame_section;
177 /* Maximum size (in bytes) of an artificially generated label. */
178 #define MAX_ARTIFICIAL_LABEL_BYTES 30
180 /* According to the (draft) DWARF 3 specification, the initial length
181 should either be 4 or 12 bytes. When it's 12 bytes, the first 4
182 bytes are 0xffffffff, followed by the length stored in the next 8
183 bytes.
185 However, the SGI/MIPS ABI uses an initial length which is equal to
186 DWARF_OFFSET_SIZE. It is defined (elsewhere) accordingly. */
188 #ifndef DWARF_INITIAL_LENGTH_SIZE
189 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
190 #endif
192 /* Round SIZE up to the nearest BOUNDARY. */
193 #define DWARF_ROUND(SIZE,BOUNDARY) \
194 ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
196 /* CIE identifier. */
197 #if HOST_BITS_PER_WIDE_INT >= 64
198 #define DWARF_CIE_ID \
199 (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
200 #else
201 #define DWARF_CIE_ID DW_CIE_ID
202 #endif
205 /* A vector for a table that contains frame description
206 information for each routine. */
207 #define NOT_INDEXED (-1U)
208 #define NO_INDEX_ASSIGNED (-2U)
210 static GTY(()) vec<dw_fde_ref, va_gc> *fde_vec;
212 struct GTY((for_user)) indirect_string_node {
213 const char *str;
214 unsigned int refcount;
215 enum dwarf_form form;
216 char *label;
217 unsigned int index;
220 struct indirect_string_hasher : ggc_ptr_hash<indirect_string_node>
222 typedef const char *compare_type;
224 static hashval_t hash (indirect_string_node *);
225 static bool equal (indirect_string_node *, const char *);
228 static GTY (()) hash_table<indirect_string_hasher> *debug_str_hash;
230 static GTY (()) hash_table<indirect_string_hasher> *debug_line_str_hash;
232 /* With split_debug_info, both the comp_dir and dwo_name go in the
233 main object file, rather than the dwo, similar to the force_direct
234 parameter elsewhere but with additional complications:
236 1) The string is needed in both the main object file and the dwo.
237 That is, the comp_dir and dwo_name will appear in both places.
239 2) Strings can use four forms: DW_FORM_string, DW_FORM_strp,
240 DW_FORM_line_strp or DW_FORM_GNU_str_index.
242 3) GCC chooses the form to use late, depending on the size and
243 reference count.
245 Rather than forcing the all debug string handling functions and
246 callers to deal with these complications, simply use a separate,
247 special-cased string table for any attribute that should go in the
248 main object file. This limits the complexity to just the places
249 that need it. */
251 static GTY (()) hash_table<indirect_string_hasher> *skeleton_debug_str_hash;
253 static GTY(()) int dw2_string_counter;
255 /* True if the compilation unit places functions in more than one section. */
256 static GTY(()) bool have_multiple_function_sections = false;
258 /* Whether the default text and cold text sections have been used at all. */
260 static GTY(()) bool text_section_used = false;
261 static GTY(()) bool cold_text_section_used = false;
263 /* The default cold text section. */
264 static GTY(()) section *cold_text_section;
266 /* The DIE for C++14 'auto' in a function return type. */
267 static GTY(()) dw_die_ref auto_die;
269 /* The DIE for C++14 'decltype(auto)' in a function return type. */
270 static GTY(()) dw_die_ref decltype_auto_die;
272 /* Forward declarations for functions defined in this file. */
274 static void output_call_frame_info (int);
275 static void dwarf2out_note_section_used (void);
277 /* Personality decl of current unit. Used only when assembler does not support
278 personality CFI. */
279 static GTY(()) rtx current_unit_personality;
281 /* .debug_rnglists next index. */
282 static unsigned int rnglist_idx;
284 /* Data and reference forms for relocatable data. */
285 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
286 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
288 #ifndef DEBUG_FRAME_SECTION
289 #define DEBUG_FRAME_SECTION ".debug_frame"
290 #endif
292 #ifndef FUNC_BEGIN_LABEL
293 #define FUNC_BEGIN_LABEL "LFB"
294 #endif
296 #ifndef FUNC_END_LABEL
297 #define FUNC_END_LABEL "LFE"
298 #endif
300 #ifndef PROLOGUE_END_LABEL
301 #define PROLOGUE_END_LABEL "LPE"
302 #endif
304 #ifndef EPILOGUE_BEGIN_LABEL
305 #define EPILOGUE_BEGIN_LABEL "LEB"
306 #endif
308 #ifndef FRAME_BEGIN_LABEL
309 #define FRAME_BEGIN_LABEL "Lframe"
310 #endif
311 #define CIE_AFTER_SIZE_LABEL "LSCIE"
312 #define CIE_END_LABEL "LECIE"
313 #define FDE_LABEL "LSFDE"
314 #define FDE_AFTER_SIZE_LABEL "LASFDE"
315 #define FDE_END_LABEL "LEFDE"
316 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
317 #define LINE_NUMBER_END_LABEL "LELT"
318 #define LN_PROLOG_AS_LABEL "LASLTP"
319 #define LN_PROLOG_END_LABEL "LELTP"
320 #define DIE_LABEL_PREFIX "DW"
322 /* Match the base name of a file to the base name of a compilation unit. */
324 static int
325 matches_main_base (const char *path)
327 /* Cache the last query. */
328 static const char *last_path = NULL;
329 static int last_match = 0;
330 if (path != last_path)
332 const char *base;
333 int length = base_of_path (path, &base);
334 last_path = path;
335 last_match = (length == main_input_baselength
336 && memcmp (base, main_input_basename, length) == 0);
338 return last_match;
341 #ifdef DEBUG_DEBUG_STRUCT
343 static int
344 dump_struct_debug (tree type, enum debug_info_usage usage,
345 enum debug_struct_file criterion, int generic,
346 int matches, int result)
348 /* Find the type name. */
349 tree type_decl = TYPE_STUB_DECL (type);
350 tree t = type_decl;
351 const char *name = 0;
352 if (TREE_CODE (t) == TYPE_DECL)
353 t = DECL_NAME (t);
354 if (t)
355 name = IDENTIFIER_POINTER (t);
357 fprintf (stderr, " struct %d %s %s %s %s %d %p %s\n",
358 criterion,
359 DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
360 matches ? "bas" : "hdr",
361 generic ? "gen" : "ord",
362 usage == DINFO_USAGE_DFN ? ";" :
363 usage == DINFO_USAGE_DIR_USE ? "." : "*",
364 result,
365 (void*) type_decl, name);
366 return result;
368 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
369 dump_struct_debug (type, usage, criterion, generic, matches, result)
371 #else
373 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
374 (result)
376 #endif
378 /* Get the number of HOST_WIDE_INTs needed to represent the precision
379 of the number. Some constants have a large uniform precision, so
380 we get the precision needed for the actual value of the number. */
382 static unsigned int
383 get_full_len (const wide_int &op)
385 int prec = wi::min_precision (op, UNSIGNED);
386 return ((prec + HOST_BITS_PER_WIDE_INT - 1)
387 / HOST_BITS_PER_WIDE_INT);
390 static bool
391 should_emit_struct_debug (tree type, enum debug_info_usage usage)
393 enum debug_struct_file criterion;
394 tree type_decl;
395 bool generic = lang_hooks.types.generic_p (type);
397 if (generic)
398 criterion = debug_struct_generic[usage];
399 else
400 criterion = debug_struct_ordinary[usage];
402 if (criterion == DINFO_STRUCT_FILE_NONE)
403 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
404 if (criterion == DINFO_STRUCT_FILE_ANY)
405 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
407 type_decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type));
409 if (type_decl != NULL)
411 if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
412 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
414 if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
415 return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
418 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
421 /* Switch [BACK] to eh_frame_section. If we don't have an eh_frame_section,
422 switch to the data section instead, and write out a synthetic start label
423 for collect2 the first time around. */
425 static void
426 switch_to_eh_frame_section (bool back ATTRIBUTE_UNUSED)
428 if (eh_frame_section == 0)
430 int flags;
432 if (EH_TABLES_CAN_BE_READ_ONLY)
434 int fde_encoding;
435 int per_encoding;
436 int lsda_encoding;
438 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
439 /*global=*/0);
440 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
441 /*global=*/1);
442 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
443 /*global=*/0);
444 flags = ((! flag_pic
445 || ((fde_encoding & 0x70) != DW_EH_PE_absptr
446 && (fde_encoding & 0x70) != DW_EH_PE_aligned
447 && (per_encoding & 0x70) != DW_EH_PE_absptr
448 && (per_encoding & 0x70) != DW_EH_PE_aligned
449 && (lsda_encoding & 0x70) != DW_EH_PE_absptr
450 && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
451 ? 0 : SECTION_WRITE);
453 else
454 flags = SECTION_WRITE;
456 #ifdef EH_FRAME_SECTION_NAME
457 eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
458 #else
459 eh_frame_section = ((flags == SECTION_WRITE)
460 ? data_section : readonly_data_section);
461 #endif /* EH_FRAME_SECTION_NAME */
464 switch_to_section (eh_frame_section);
466 #ifdef EH_FRAME_THROUGH_COLLECT2
467 /* We have no special eh_frame section. Emit special labels to guide
468 collect2. */
469 if (!back)
471 tree label = get_file_function_name ("F");
472 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
473 targetm.asm_out.globalize_label (asm_out_file,
474 IDENTIFIER_POINTER (label));
475 ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
477 #endif
480 /* Switch [BACK] to the eh or debug frame table section, depending on
481 FOR_EH. */
483 static void
484 switch_to_frame_table_section (int for_eh, bool back)
486 if (for_eh)
487 switch_to_eh_frame_section (back);
488 else
490 if (!debug_frame_section)
491 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
492 SECTION_DEBUG, NULL);
493 switch_to_section (debug_frame_section);
497 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used. */
499 enum dw_cfi_oprnd_type
500 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
502 switch (cfi)
504 case DW_CFA_nop:
505 case DW_CFA_GNU_window_save:
506 case DW_CFA_remember_state:
507 case DW_CFA_restore_state:
508 return dw_cfi_oprnd_unused;
510 case DW_CFA_set_loc:
511 case DW_CFA_advance_loc1:
512 case DW_CFA_advance_loc2:
513 case DW_CFA_advance_loc4:
514 case DW_CFA_MIPS_advance_loc8:
515 return dw_cfi_oprnd_addr;
517 case DW_CFA_offset:
518 case DW_CFA_offset_extended:
519 case DW_CFA_def_cfa:
520 case DW_CFA_offset_extended_sf:
521 case DW_CFA_def_cfa_sf:
522 case DW_CFA_restore:
523 case DW_CFA_restore_extended:
524 case DW_CFA_undefined:
525 case DW_CFA_same_value:
526 case DW_CFA_def_cfa_register:
527 case DW_CFA_register:
528 case DW_CFA_expression:
529 case DW_CFA_val_expression:
530 return dw_cfi_oprnd_reg_num;
532 case DW_CFA_def_cfa_offset:
533 case DW_CFA_GNU_args_size:
534 case DW_CFA_def_cfa_offset_sf:
535 return dw_cfi_oprnd_offset;
537 case DW_CFA_def_cfa_expression:
538 return dw_cfi_oprnd_loc;
540 default:
541 gcc_unreachable ();
545 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used. */
547 enum dw_cfi_oprnd_type
548 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
550 switch (cfi)
552 case DW_CFA_def_cfa:
553 case DW_CFA_def_cfa_sf:
554 case DW_CFA_offset:
555 case DW_CFA_offset_extended_sf:
556 case DW_CFA_offset_extended:
557 return dw_cfi_oprnd_offset;
559 case DW_CFA_register:
560 return dw_cfi_oprnd_reg_num;
562 case DW_CFA_expression:
563 case DW_CFA_val_expression:
564 return dw_cfi_oprnd_loc;
566 default:
567 return dw_cfi_oprnd_unused;
571 /* Output one FDE. */
573 static void
574 output_fde (dw_fde_ref fde, bool for_eh, bool second,
575 char *section_start_label, int fde_encoding, char *augmentation,
576 bool any_lsda_needed, int lsda_encoding)
578 const char *begin, *end;
579 static unsigned int j;
580 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
582 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
583 /* empty */ 0);
584 targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
585 for_eh + j);
586 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
587 ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
588 if (!XCOFF_DEBUGGING_INFO || for_eh)
590 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
591 dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
592 " indicating 64-bit DWARF extension");
593 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
594 "FDE Length");
596 ASM_OUTPUT_LABEL (asm_out_file, l1);
598 if (for_eh)
599 dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
600 else
601 dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
602 debug_frame_section, "FDE CIE offset");
604 begin = second ? fde->dw_fde_second_begin : fde->dw_fde_begin;
605 end = second ? fde->dw_fde_second_end : fde->dw_fde_end;
607 if (for_eh)
609 rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
610 SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
611 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
612 "FDE initial location");
613 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
614 end, begin, "FDE address range");
616 else
618 dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
619 dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
622 if (augmentation[0])
624 if (any_lsda_needed)
626 int size = size_of_encoded_value (lsda_encoding);
628 if (lsda_encoding == DW_EH_PE_aligned)
630 int offset = ( 4 /* Length */
631 + 4 /* CIE offset */
632 + 2 * size_of_encoded_value (fde_encoding)
633 + 1 /* Augmentation size */ );
634 int pad = -offset & (PTR_SIZE - 1);
636 size += pad;
637 gcc_assert (size_of_uleb128 (size) == 1);
640 dw2_asm_output_data_uleb128 (size, "Augmentation size");
642 if (fde->uses_eh_lsda)
644 ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
645 fde->funcdef_number);
646 dw2_asm_output_encoded_addr_rtx (lsda_encoding,
647 gen_rtx_SYMBOL_REF (Pmode, l1),
648 false,
649 "Language Specific Data Area");
651 else
653 if (lsda_encoding == DW_EH_PE_aligned)
654 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
655 dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
656 "Language Specific Data Area (none)");
659 else
660 dw2_asm_output_data_uleb128 (0, "Augmentation size");
663 /* Loop through the Call Frame Instructions associated with this FDE. */
664 fde->dw_fde_current_label = begin;
666 size_t from, until, i;
668 from = 0;
669 until = vec_safe_length (fde->dw_fde_cfi);
671 if (fde->dw_fde_second_begin == NULL)
673 else if (!second)
674 until = fde->dw_fde_switch_cfi_index;
675 else
676 from = fde->dw_fde_switch_cfi_index;
678 for (i = from; i < until; i++)
679 output_cfi ((*fde->dw_fde_cfi)[i], fde, for_eh);
682 /* If we are to emit a ref/link from function bodies to their frame tables,
683 do it now. This is typically performed to make sure that tables
684 associated with functions are dragged with them and not discarded in
685 garbage collecting links. We need to do this on a per function basis to
686 cope with -ffunction-sections. */
688 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
689 /* Switch to the function section, emit the ref to the tables, and
690 switch *back* into the table section. */
691 switch_to_section (function_section (fde->decl));
692 ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
693 switch_to_frame_table_section (for_eh, true);
694 #endif
696 /* Pad the FDE out to an address sized boundary. */
697 ASM_OUTPUT_ALIGN (asm_out_file,
698 floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
699 ASM_OUTPUT_LABEL (asm_out_file, l2);
701 j += 2;
704 /* Return true if frame description entry FDE is needed for EH. */
706 static bool
707 fde_needed_for_eh_p (dw_fde_ref fde)
709 if (flag_asynchronous_unwind_tables)
710 return true;
712 if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
713 return true;
715 if (fde->uses_eh_lsda)
716 return true;
718 /* If exceptions are enabled, we have collected nothrow info. */
719 if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
720 return false;
722 return true;
725 /* Output the call frame information used to record information
726 that relates to calculating the frame pointer, and records the
727 location of saved registers. */
729 static void
730 output_call_frame_info (int for_eh)
732 unsigned int i;
733 dw_fde_ref fde;
734 dw_cfi_ref cfi;
735 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
736 char section_start_label[MAX_ARTIFICIAL_LABEL_BYTES];
737 bool any_lsda_needed = false;
738 char augmentation[6];
739 int augmentation_size;
740 int fde_encoding = DW_EH_PE_absptr;
741 int per_encoding = DW_EH_PE_absptr;
742 int lsda_encoding = DW_EH_PE_absptr;
743 int return_reg;
744 rtx personality = NULL;
745 int dw_cie_version;
747 /* Don't emit a CIE if there won't be any FDEs. */
748 if (!fde_vec)
749 return;
751 /* Nothing to do if the assembler's doing it all. */
752 if (dwarf2out_do_cfi_asm ())
753 return;
755 /* If we don't have any functions we'll want to unwind out of, don't emit
756 any EH unwind information. If we make FDEs linkonce, we may have to
757 emit an empty label for an FDE that wouldn't otherwise be emitted. We
758 want to avoid having an FDE kept around when the function it refers to
759 is discarded. Example where this matters: a primary function template
760 in C++ requires EH information, an explicit specialization doesn't. */
761 if (for_eh)
763 bool any_eh_needed = false;
765 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
767 if (fde->uses_eh_lsda)
768 any_eh_needed = any_lsda_needed = true;
769 else if (fde_needed_for_eh_p (fde))
770 any_eh_needed = true;
771 else if (TARGET_USES_WEAK_UNWIND_INFO)
772 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, 1, 1);
775 if (!any_eh_needed)
776 return;
779 /* We're going to be generating comments, so turn on app. */
780 if (flag_debug_asm)
781 app_enable ();
783 /* Switch to the proper frame section, first time. */
784 switch_to_frame_table_section (for_eh, false);
786 ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
787 ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
789 /* Output the CIE. */
790 ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
791 ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
792 if (!XCOFF_DEBUGGING_INFO || for_eh)
794 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
795 dw2_asm_output_data (4, 0xffffffff,
796 "Initial length escape value indicating 64-bit DWARF extension");
797 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
798 "Length of Common Information Entry");
800 ASM_OUTPUT_LABEL (asm_out_file, l1);
802 /* Now that the CIE pointer is PC-relative for EH,
803 use 0 to identify the CIE. */
804 dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
805 (for_eh ? 0 : DWARF_CIE_ID),
806 "CIE Identifier Tag");
808 /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
809 use CIE version 1, unless that would produce incorrect results
810 due to overflowing the return register column. */
811 return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
812 dw_cie_version = 1;
813 if (return_reg >= 256 || dwarf_version > 2)
814 dw_cie_version = 3;
815 dw2_asm_output_data (1, dw_cie_version, "CIE Version");
817 augmentation[0] = 0;
818 augmentation_size = 0;
820 personality = current_unit_personality;
821 if (for_eh)
823 char *p;
825 /* Augmentation:
826 z Indicates that a uleb128 is present to size the
827 augmentation section.
828 L Indicates the encoding (and thus presence) of
829 an LSDA pointer in the FDE augmentation.
830 R Indicates a non-default pointer encoding for
831 FDE code pointers.
832 P Indicates the presence of an encoding + language
833 personality routine in the CIE augmentation. */
835 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
836 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
837 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
839 p = augmentation + 1;
840 if (personality)
842 *p++ = 'P';
843 augmentation_size += 1 + size_of_encoded_value (per_encoding);
844 assemble_external_libcall (personality);
846 if (any_lsda_needed)
848 *p++ = 'L';
849 augmentation_size += 1;
851 if (fde_encoding != DW_EH_PE_absptr)
853 *p++ = 'R';
854 augmentation_size += 1;
856 if (p > augmentation + 1)
858 augmentation[0] = 'z';
859 *p = '\0';
862 /* Ug. Some platforms can't do unaligned dynamic relocations at all. */
863 if (personality && per_encoding == DW_EH_PE_aligned)
865 int offset = ( 4 /* Length */
866 + 4 /* CIE Id */
867 + 1 /* CIE version */
868 + strlen (augmentation) + 1 /* Augmentation */
869 + size_of_uleb128 (1) /* Code alignment */
870 + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
871 + 1 /* RA column */
872 + 1 /* Augmentation size */
873 + 1 /* Personality encoding */ );
874 int pad = -offset & (PTR_SIZE - 1);
876 augmentation_size += pad;
878 /* Augmentations should be small, so there's scarce need to
879 iterate for a solution. Die if we exceed one uleb128 byte. */
880 gcc_assert (size_of_uleb128 (augmentation_size) == 1);
884 dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
885 if (dw_cie_version >= 4)
887 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
888 dw2_asm_output_data (1, 0, "CIE Segment Size");
890 dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
891 dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
892 "CIE Data Alignment Factor");
894 if (dw_cie_version == 1)
895 dw2_asm_output_data (1, return_reg, "CIE RA Column");
896 else
897 dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
899 if (augmentation[0])
901 dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
902 if (personality)
904 dw2_asm_output_data (1, per_encoding, "Personality (%s)",
905 eh_data_format_name (per_encoding));
906 dw2_asm_output_encoded_addr_rtx (per_encoding,
907 personality,
908 true, NULL);
911 if (any_lsda_needed)
912 dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
913 eh_data_format_name (lsda_encoding));
915 if (fde_encoding != DW_EH_PE_absptr)
916 dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
917 eh_data_format_name (fde_encoding));
920 FOR_EACH_VEC_ELT (*cie_cfi_vec, i, cfi)
921 output_cfi (cfi, NULL, for_eh);
923 /* Pad the CIE out to an address sized boundary. */
924 ASM_OUTPUT_ALIGN (asm_out_file,
925 floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
926 ASM_OUTPUT_LABEL (asm_out_file, l2);
928 /* Loop through all of the FDE's. */
929 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
931 unsigned int k;
933 /* Don't emit EH unwind info for leaf functions that don't need it. */
934 if (for_eh && !fde_needed_for_eh_p (fde))
935 continue;
937 for (k = 0; k < (fde->dw_fde_second_begin ? 2 : 1); k++)
938 output_fde (fde, for_eh, k, section_start_label, fde_encoding,
939 augmentation, any_lsda_needed, lsda_encoding);
942 if (for_eh && targetm.terminate_dw2_eh_frame_info)
943 dw2_asm_output_data (4, 0, "End of Table");
945 /* Turn off app to make assembly quicker. */
946 if (flag_debug_asm)
947 app_disable ();
950 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed. */
952 static void
953 dwarf2out_do_cfi_startproc (bool second)
955 int enc;
956 rtx ref;
957 rtx personality = get_personality_function (current_function_decl);
959 fprintf (asm_out_file, "\t.cfi_startproc\n");
961 if (personality)
963 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
964 ref = personality;
966 /* ??? The GAS support isn't entirely consistent. We have to
967 handle indirect support ourselves, but PC-relative is done
968 in the assembler. Further, the assembler can't handle any
969 of the weirder relocation types. */
970 if (enc & DW_EH_PE_indirect)
971 ref = dw2_force_const_mem (ref, true);
973 fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
974 output_addr_const (asm_out_file, ref);
975 fputc ('\n', asm_out_file);
978 if (crtl->uses_eh_lsda)
980 char lab[MAX_ARTIFICIAL_LABEL_BYTES];
982 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
983 ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
984 current_function_funcdef_no);
985 ref = gen_rtx_SYMBOL_REF (Pmode, lab);
986 SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
988 if (enc & DW_EH_PE_indirect)
989 ref = dw2_force_const_mem (ref, true);
991 fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
992 output_addr_const (asm_out_file, ref);
993 fputc ('\n', asm_out_file);
997 /* Allocate CURRENT_FDE. Immediately initialize all we can, noting that
998 this allocation may be done before pass_final. */
1000 dw_fde_ref
1001 dwarf2out_alloc_current_fde (void)
1003 dw_fde_ref fde;
1005 fde = ggc_cleared_alloc<dw_fde_node> ();
1006 fde->decl = current_function_decl;
1007 fde->funcdef_number = current_function_funcdef_no;
1008 fde->fde_index = vec_safe_length (fde_vec);
1009 fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
1010 fde->uses_eh_lsda = crtl->uses_eh_lsda;
1011 fde->nothrow = crtl->nothrow;
1012 fde->drap_reg = INVALID_REGNUM;
1013 fde->vdrap_reg = INVALID_REGNUM;
1015 /* Record the FDE associated with this function. */
1016 cfun->fde = fde;
1017 vec_safe_push (fde_vec, fde);
1019 return fde;
1022 /* Output a marker (i.e. a label) for the beginning of a function, before
1023 the prologue. */
1025 void
1026 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
1027 unsigned int column ATTRIBUTE_UNUSED,
1028 const char *file ATTRIBUTE_UNUSED)
1030 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1031 char * dup_label;
1032 dw_fde_ref fde;
1033 section *fnsec;
1034 bool do_frame;
1036 current_function_func_begin_label = NULL;
1038 do_frame = dwarf2out_do_frame ();
1040 /* ??? current_function_func_begin_label is also used by except.c for
1041 call-site information. We must emit this label if it might be used. */
1042 if (!do_frame
1043 && (!flag_exceptions
1044 || targetm_common.except_unwind_info (&global_options) == UI_SJLJ))
1045 return;
1047 fnsec = function_section (current_function_decl);
1048 switch_to_section (fnsec);
1049 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
1050 current_function_funcdef_no);
1051 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
1052 current_function_funcdef_no);
1053 dup_label = xstrdup (label);
1054 current_function_func_begin_label = dup_label;
1056 /* We can elide the fde allocation if we're not emitting debug info. */
1057 if (!do_frame)
1058 return;
1060 /* Cater to the various TARGET_ASM_OUTPUT_MI_THUNK implementations that
1061 emit insns as rtx but bypass the bulk of rest_of_compilation, which
1062 would include pass_dwarf2_frame. If we've not created the FDE yet,
1063 do so now. */
1064 fde = cfun->fde;
1065 if (fde == NULL)
1066 fde = dwarf2out_alloc_current_fde ();
1068 /* Initialize the bits of CURRENT_FDE that were not available earlier. */
1069 fde->dw_fde_begin = dup_label;
1070 fde->dw_fde_current_label = dup_label;
1071 fde->in_std_section = (fnsec == text_section
1072 || (cold_text_section && fnsec == cold_text_section));
1074 /* We only want to output line number information for the genuine dwarf2
1075 prologue case, not the eh frame case. */
1076 #ifdef DWARF2_DEBUGGING_INFO
1077 if (file)
1078 dwarf2out_source_line (line, column, file, 0, true);
1079 #endif
1081 if (dwarf2out_do_cfi_asm ())
1082 dwarf2out_do_cfi_startproc (false);
1083 else
1085 rtx personality = get_personality_function (current_function_decl);
1086 if (!current_unit_personality)
1087 current_unit_personality = personality;
1089 /* We cannot keep a current personality per function as without CFI
1090 asm, at the point where we emit the CFI data, there is no current
1091 function anymore. */
1092 if (personality && current_unit_personality != personality)
1093 sorry ("multiple EH personalities are supported only with assemblers "
1094 "supporting .cfi_personality directive");
1098 /* Output a marker (i.e. a label) for the end of the generated code
1099 for a function prologue. This gets called *after* the prologue code has
1100 been generated. */
1102 void
1103 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
1104 const char *file ATTRIBUTE_UNUSED)
1106 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1108 /* Output a label to mark the endpoint of the code generated for this
1109 function. */
1110 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
1111 current_function_funcdef_no);
1112 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
1113 current_function_funcdef_no);
1114 cfun->fde->dw_fde_vms_end_prologue = xstrdup (label);
1117 /* Output a marker (i.e. a label) for the beginning of the generated code
1118 for a function epilogue. This gets called *before* the prologue code has
1119 been generated. */
1121 void
1122 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1123 const char *file ATTRIBUTE_UNUSED)
1125 dw_fde_ref fde = cfun->fde;
1126 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1128 if (fde->dw_fde_vms_begin_epilogue)
1129 return;
1131 /* Output a label to mark the endpoint of the code generated for this
1132 function. */
1133 ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
1134 current_function_funcdef_no);
1135 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
1136 current_function_funcdef_no);
1137 fde->dw_fde_vms_begin_epilogue = xstrdup (label);
1140 /* Output a marker (i.e. a label) for the absolute end of the generated code
1141 for a function definition. This gets called *after* the epilogue code has
1142 been generated. */
1144 void
1145 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1146 const char *file ATTRIBUTE_UNUSED)
1148 dw_fde_ref fde;
1149 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1151 last_var_location_insn = NULL;
1152 cached_next_real_insn = NULL;
1154 if (dwarf2out_do_cfi_asm ())
1155 fprintf (asm_out_file, "\t.cfi_endproc\n");
1157 /* Output a label to mark the endpoint of the code generated for this
1158 function. */
1159 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
1160 current_function_funcdef_no);
1161 ASM_OUTPUT_LABEL (asm_out_file, label);
1162 fde = cfun->fde;
1163 gcc_assert (fde != NULL);
1164 if (fde->dw_fde_second_begin == NULL)
1165 fde->dw_fde_end = xstrdup (label);
1168 void
1169 dwarf2out_frame_finish (void)
1171 /* Output call frame information. */
1172 if (targetm.debug_unwind_info () == UI_DWARF2)
1173 output_call_frame_info (0);
1175 /* Output another copy for the unwinder. */
1176 if ((flag_unwind_tables || flag_exceptions)
1177 && targetm_common.except_unwind_info (&global_options) == UI_DWARF2)
1178 output_call_frame_info (1);
1181 /* Note that the current function section is being used for code. */
1183 static void
1184 dwarf2out_note_section_used (void)
1186 section *sec = current_function_section ();
1187 if (sec == text_section)
1188 text_section_used = true;
1189 else if (sec == cold_text_section)
1190 cold_text_section_used = true;
1193 static void var_location_switch_text_section (void);
1194 static void set_cur_line_info_table (section *);
1196 void
1197 dwarf2out_switch_text_section (void)
1199 section *sect;
1200 dw_fde_ref fde = cfun->fde;
1202 gcc_assert (cfun && fde && fde->dw_fde_second_begin == NULL);
1204 if (!in_cold_section_p)
1206 fde->dw_fde_end = crtl->subsections.cold_section_end_label;
1207 fde->dw_fde_second_begin = crtl->subsections.hot_section_label;
1208 fde->dw_fde_second_end = crtl->subsections.hot_section_end_label;
1210 else
1212 fde->dw_fde_end = crtl->subsections.hot_section_end_label;
1213 fde->dw_fde_second_begin = crtl->subsections.cold_section_label;
1214 fde->dw_fde_second_end = crtl->subsections.cold_section_end_label;
1216 have_multiple_function_sections = true;
1218 /* There is no need to mark used sections when not debugging. */
1219 if (cold_text_section != NULL)
1220 dwarf2out_note_section_used ();
1222 if (dwarf2out_do_cfi_asm ())
1223 fprintf (asm_out_file, "\t.cfi_endproc\n");
1225 /* Now do the real section switch. */
1226 sect = current_function_section ();
1227 switch_to_section (sect);
1229 fde->second_in_std_section
1230 = (sect == text_section
1231 || (cold_text_section && sect == cold_text_section));
1233 if (dwarf2out_do_cfi_asm ())
1234 dwarf2out_do_cfi_startproc (true);
1236 var_location_switch_text_section ();
1238 if (cold_text_section != NULL)
1239 set_cur_line_info_table (sect);
1242 /* And now, the subset of the debugging information support code necessary
1243 for emitting location expressions. */
1245 /* Data about a single source file. */
1246 struct GTY((for_user)) dwarf_file_data {
1247 const char * filename;
1248 int emitted_number;
1251 /* Describe an entry into the .debug_addr section. */
1253 enum ate_kind {
1254 ate_kind_rtx,
1255 ate_kind_rtx_dtprel,
1256 ate_kind_label
1259 struct GTY((for_user)) addr_table_entry {
1260 enum ate_kind kind;
1261 unsigned int refcount;
1262 unsigned int index;
1263 union addr_table_entry_struct_union
1265 rtx GTY ((tag ("0"))) rtl;
1266 char * GTY ((tag ("1"))) label;
1268 GTY ((desc ("%1.kind"))) addr;
1271 /* Location lists are ranges + location descriptions for that range,
1272 so you can track variables that are in different places over
1273 their entire life. */
1274 typedef struct GTY(()) dw_loc_list_struct {
1275 dw_loc_list_ref dw_loc_next;
1276 const char *begin; /* Label and addr_entry for start of range */
1277 addr_table_entry *begin_entry;
1278 const char *end; /* Label for end of range */
1279 char *ll_symbol; /* Label for beginning of location list.
1280 Only on head of list */
1281 const char *section; /* Section this loclist is relative to */
1282 dw_loc_descr_ref expr;
1283 hashval_t hash;
1284 /* True if all addresses in this and subsequent lists are known to be
1285 resolved. */
1286 bool resolved_addr;
1287 /* True if this list has been replaced by dw_loc_next. */
1288 bool replaced;
1289 /* True if it has been emitted into .debug_loc* / .debug_loclists*
1290 section. */
1291 unsigned char emitted : 1;
1292 /* True if hash field is index rather than hash value. */
1293 unsigned char num_assigned : 1;
1294 /* True if .debug_loclists.dwo offset has been emitted for it already. */
1295 unsigned char offset_emitted : 1;
1296 /* True if note_variable_value_in_expr has been called on it. */
1297 unsigned char noted_variable_value : 1;
1298 /* True if the range should be emitted even if begin and end
1299 are the same. */
1300 bool force;
1301 } dw_loc_list_node;
1303 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
1304 static dw_loc_descr_ref uint_loc_descriptor (unsigned HOST_WIDE_INT);
1306 /* Convert a DWARF stack opcode into its string name. */
1308 static const char *
1309 dwarf_stack_op_name (unsigned int op)
1311 const char *name = get_DW_OP_name (op);
1313 if (name != NULL)
1314 return name;
1316 return "OP_<unknown>";
1319 /* Return a pointer to a newly allocated location description. Location
1320 descriptions are simple expression terms that can be strung
1321 together to form more complicated location (address) descriptions. */
1323 static inline dw_loc_descr_ref
1324 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
1325 unsigned HOST_WIDE_INT oprnd2)
1327 dw_loc_descr_ref descr = ggc_cleared_alloc<dw_loc_descr_node> ();
1329 descr->dw_loc_opc = op;
1330 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
1331 descr->dw_loc_oprnd1.val_entry = NULL;
1332 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
1333 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
1334 descr->dw_loc_oprnd2.val_entry = NULL;
1335 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
1337 return descr;
1340 /* Return a pointer to a newly allocated location description for
1341 REG and OFFSET. */
1343 static inline dw_loc_descr_ref
1344 new_reg_loc_descr (unsigned int reg, unsigned HOST_WIDE_INT offset)
1346 if (reg <= 31)
1347 return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
1348 offset, 0);
1349 else
1350 return new_loc_descr (DW_OP_bregx, reg, offset);
1353 /* Add a location description term to a location description expression. */
1355 static inline void
1356 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
1358 dw_loc_descr_ref *d;
1360 /* Find the end of the chain. */
1361 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
1364 *d = descr;
1367 /* Compare two location operands for exact equality. */
1369 static bool
1370 dw_val_equal_p (dw_val_node *a, dw_val_node *b)
1372 if (a->val_class != b->val_class)
1373 return false;
1374 switch (a->val_class)
1376 case dw_val_class_none:
1377 return true;
1378 case dw_val_class_addr:
1379 return rtx_equal_p (a->v.val_addr, b->v.val_addr);
1381 case dw_val_class_offset:
1382 case dw_val_class_unsigned_const:
1383 case dw_val_class_const:
1384 case dw_val_class_unsigned_const_implicit:
1385 case dw_val_class_const_implicit:
1386 case dw_val_class_range_list:
1387 /* These are all HOST_WIDE_INT, signed or unsigned. */
1388 return a->v.val_unsigned == b->v.val_unsigned;
1390 case dw_val_class_loc:
1391 return a->v.val_loc == b->v.val_loc;
1392 case dw_val_class_loc_list:
1393 return a->v.val_loc_list == b->v.val_loc_list;
1394 case dw_val_class_die_ref:
1395 return a->v.val_die_ref.die == b->v.val_die_ref.die;
1396 case dw_val_class_fde_ref:
1397 return a->v.val_fde_index == b->v.val_fde_index;
1398 case dw_val_class_lbl_id:
1399 case dw_val_class_lineptr:
1400 case dw_val_class_macptr:
1401 case dw_val_class_loclistsptr:
1402 case dw_val_class_high_pc:
1403 return strcmp (a->v.val_lbl_id, b->v.val_lbl_id) == 0;
1404 case dw_val_class_str:
1405 return a->v.val_str == b->v.val_str;
1406 case dw_val_class_flag:
1407 return a->v.val_flag == b->v.val_flag;
1408 case dw_val_class_file:
1409 case dw_val_class_file_implicit:
1410 return a->v.val_file == b->v.val_file;
1411 case dw_val_class_decl_ref:
1412 return a->v.val_decl_ref == b->v.val_decl_ref;
1414 case dw_val_class_const_double:
1415 return (a->v.val_double.high == b->v.val_double.high
1416 && a->v.val_double.low == b->v.val_double.low);
1418 case dw_val_class_wide_int:
1419 return *a->v.val_wide == *b->v.val_wide;
1421 case dw_val_class_vec:
1423 size_t a_len = a->v.val_vec.elt_size * a->v.val_vec.length;
1424 size_t b_len = b->v.val_vec.elt_size * b->v.val_vec.length;
1426 return (a_len == b_len
1427 && !memcmp (a->v.val_vec.array, b->v.val_vec.array, a_len));
1430 case dw_val_class_data8:
1431 return memcmp (a->v.val_data8, b->v.val_data8, 8) == 0;
1433 case dw_val_class_vms_delta:
1434 return (!strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1)
1435 && !strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1));
1437 case dw_val_class_discr_value:
1438 return (a->v.val_discr_value.pos == b->v.val_discr_value.pos
1439 && a->v.val_discr_value.v.uval == b->v.val_discr_value.v.uval);
1440 case dw_val_class_discr_list:
1441 /* It makes no sense comparing two discriminant value lists. */
1442 return false;
1444 gcc_unreachable ();
1447 /* Compare two location atoms for exact equality. */
1449 static bool
1450 loc_descr_equal_p_1 (dw_loc_descr_ref a, dw_loc_descr_ref b)
1452 if (a->dw_loc_opc != b->dw_loc_opc)
1453 return false;
1455 /* ??? This is only ever set for DW_OP_constNu, for N equal to the
1456 address size, but since we always allocate cleared storage it
1457 should be zero for other types of locations. */
1458 if (a->dtprel != b->dtprel)
1459 return false;
1461 return (dw_val_equal_p (&a->dw_loc_oprnd1, &b->dw_loc_oprnd1)
1462 && dw_val_equal_p (&a->dw_loc_oprnd2, &b->dw_loc_oprnd2));
1465 /* Compare two complete location expressions for exact equality. */
1467 bool
1468 loc_descr_equal_p (dw_loc_descr_ref a, dw_loc_descr_ref b)
1470 while (1)
1472 if (a == b)
1473 return true;
1474 if (a == NULL || b == NULL)
1475 return false;
1476 if (!loc_descr_equal_p_1 (a, b))
1477 return false;
1479 a = a->dw_loc_next;
1480 b = b->dw_loc_next;
1485 /* Add a constant OFFSET to a location expression. */
1487 static void
1488 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
1490 dw_loc_descr_ref loc;
1491 HOST_WIDE_INT *p;
1493 gcc_assert (*list_head != NULL);
1495 if (!offset)
1496 return;
1498 /* Find the end of the chain. */
1499 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
1502 p = NULL;
1503 if (loc->dw_loc_opc == DW_OP_fbreg
1504 || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
1505 p = &loc->dw_loc_oprnd1.v.val_int;
1506 else if (loc->dw_loc_opc == DW_OP_bregx)
1507 p = &loc->dw_loc_oprnd2.v.val_int;
1509 /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
1510 offset. Don't optimize if an signed integer overflow would happen. */
1511 if (p != NULL
1512 && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
1513 || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
1514 *p += offset;
1516 else if (offset > 0)
1517 loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
1519 else
1521 loc->dw_loc_next
1522 = uint_loc_descriptor (-(unsigned HOST_WIDE_INT) offset);
1523 add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
1527 /* Add a constant OFFSET to a location list. */
1529 static void
1530 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
1532 dw_loc_list_ref d;
1533 for (d = list_head; d != NULL; d = d->dw_loc_next)
1534 loc_descr_plus_const (&d->expr, offset);
1537 #define DWARF_REF_SIZE \
1538 (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
1540 /* The number of bits that can be encoded by largest DW_FORM_dataN.
1541 In DWARF4 and earlier it is DW_FORM_data8 with 64 bits, in DWARF5
1542 DW_FORM_data16 with 128 bits. */
1543 #define DWARF_LARGEST_DATA_FORM_BITS \
1544 (dwarf_version >= 5 ? 128 : 64)
1546 /* Utility inline function for construction of ops that were GNU extension
1547 before DWARF 5. */
1548 static inline enum dwarf_location_atom
1549 dwarf_OP (enum dwarf_location_atom op)
1551 switch (op)
1553 case DW_OP_implicit_pointer:
1554 if (dwarf_version < 5)
1555 return DW_OP_GNU_implicit_pointer;
1556 break;
1558 case DW_OP_entry_value:
1559 if (dwarf_version < 5)
1560 return DW_OP_GNU_entry_value;
1561 break;
1563 case DW_OP_const_type:
1564 if (dwarf_version < 5)
1565 return DW_OP_GNU_const_type;
1566 break;
1568 case DW_OP_regval_type:
1569 if (dwarf_version < 5)
1570 return DW_OP_GNU_regval_type;
1571 break;
1573 case DW_OP_deref_type:
1574 if (dwarf_version < 5)
1575 return DW_OP_GNU_deref_type;
1576 break;
1578 case DW_OP_convert:
1579 if (dwarf_version < 5)
1580 return DW_OP_GNU_convert;
1581 break;
1583 case DW_OP_reinterpret:
1584 if (dwarf_version < 5)
1585 return DW_OP_GNU_reinterpret;
1586 break;
1588 default:
1589 break;
1591 return op;
1594 /* Similarly for attributes. */
1595 static inline enum dwarf_attribute
1596 dwarf_AT (enum dwarf_attribute at)
1598 switch (at)
1600 case DW_AT_call_return_pc:
1601 if (dwarf_version < 5)
1602 return DW_AT_low_pc;
1603 break;
1605 case DW_AT_call_tail_call:
1606 if (dwarf_version < 5)
1607 return DW_AT_GNU_tail_call;
1608 break;
1610 case DW_AT_call_origin:
1611 if (dwarf_version < 5)
1612 return DW_AT_abstract_origin;
1613 break;
1615 case DW_AT_call_target:
1616 if (dwarf_version < 5)
1617 return DW_AT_GNU_call_site_target;
1618 break;
1620 case DW_AT_call_target_clobbered:
1621 if (dwarf_version < 5)
1622 return DW_AT_GNU_call_site_target_clobbered;
1623 break;
1625 case DW_AT_call_parameter:
1626 if (dwarf_version < 5)
1627 return DW_AT_abstract_origin;
1628 break;
1630 case DW_AT_call_value:
1631 if (dwarf_version < 5)
1632 return DW_AT_GNU_call_site_value;
1633 break;
1635 case DW_AT_call_data_value:
1636 if (dwarf_version < 5)
1637 return DW_AT_GNU_call_site_data_value;
1638 break;
1640 case DW_AT_call_all_calls:
1641 if (dwarf_version < 5)
1642 return DW_AT_GNU_all_call_sites;
1643 break;
1645 case DW_AT_call_all_tail_calls:
1646 if (dwarf_version < 5)
1647 return DW_AT_GNU_all_tail_call_sites;
1648 break;
1650 case DW_AT_dwo_name:
1651 if (dwarf_version < 5)
1652 return DW_AT_GNU_dwo_name;
1653 break;
1655 default:
1656 break;
1658 return at;
1661 /* And similarly for tags. */
1662 static inline enum dwarf_tag
1663 dwarf_TAG (enum dwarf_tag tag)
1665 switch (tag)
1667 case DW_TAG_call_site:
1668 if (dwarf_version < 5)
1669 return DW_TAG_GNU_call_site;
1670 break;
1672 case DW_TAG_call_site_parameter:
1673 if (dwarf_version < 5)
1674 return DW_TAG_GNU_call_site_parameter;
1675 break;
1677 default:
1678 break;
1680 return tag;
1683 static unsigned long int get_base_type_offset (dw_die_ref);
1685 /* Return the size of a location descriptor. */
1687 static unsigned long
1688 size_of_loc_descr (dw_loc_descr_ref loc)
1690 unsigned long size = 1;
1692 switch (loc->dw_loc_opc)
1694 case DW_OP_addr:
1695 size += DWARF2_ADDR_SIZE;
1696 break;
1697 case DW_OP_GNU_addr_index:
1698 case DW_OP_GNU_const_index:
1699 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1700 size += size_of_uleb128 (loc->dw_loc_oprnd1.val_entry->index);
1701 break;
1702 case DW_OP_const1u:
1703 case DW_OP_const1s:
1704 size += 1;
1705 break;
1706 case DW_OP_const2u:
1707 case DW_OP_const2s:
1708 size += 2;
1709 break;
1710 case DW_OP_const4u:
1711 case DW_OP_const4s:
1712 size += 4;
1713 break;
1714 case DW_OP_const8u:
1715 case DW_OP_const8s:
1716 size += 8;
1717 break;
1718 case DW_OP_constu:
1719 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1720 break;
1721 case DW_OP_consts:
1722 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1723 break;
1724 case DW_OP_pick:
1725 size += 1;
1726 break;
1727 case DW_OP_plus_uconst:
1728 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1729 break;
1730 case DW_OP_skip:
1731 case DW_OP_bra:
1732 size += 2;
1733 break;
1734 case DW_OP_breg0:
1735 case DW_OP_breg1:
1736 case DW_OP_breg2:
1737 case DW_OP_breg3:
1738 case DW_OP_breg4:
1739 case DW_OP_breg5:
1740 case DW_OP_breg6:
1741 case DW_OP_breg7:
1742 case DW_OP_breg8:
1743 case DW_OP_breg9:
1744 case DW_OP_breg10:
1745 case DW_OP_breg11:
1746 case DW_OP_breg12:
1747 case DW_OP_breg13:
1748 case DW_OP_breg14:
1749 case DW_OP_breg15:
1750 case DW_OP_breg16:
1751 case DW_OP_breg17:
1752 case DW_OP_breg18:
1753 case DW_OP_breg19:
1754 case DW_OP_breg20:
1755 case DW_OP_breg21:
1756 case DW_OP_breg22:
1757 case DW_OP_breg23:
1758 case DW_OP_breg24:
1759 case DW_OP_breg25:
1760 case DW_OP_breg26:
1761 case DW_OP_breg27:
1762 case DW_OP_breg28:
1763 case DW_OP_breg29:
1764 case DW_OP_breg30:
1765 case DW_OP_breg31:
1766 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1767 break;
1768 case DW_OP_regx:
1769 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1770 break;
1771 case DW_OP_fbreg:
1772 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1773 break;
1774 case DW_OP_bregx:
1775 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1776 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1777 break;
1778 case DW_OP_piece:
1779 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1780 break;
1781 case DW_OP_bit_piece:
1782 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1783 size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
1784 break;
1785 case DW_OP_deref_size:
1786 case DW_OP_xderef_size:
1787 size += 1;
1788 break;
1789 case DW_OP_call2:
1790 size += 2;
1791 break;
1792 case DW_OP_call4:
1793 size += 4;
1794 break;
1795 case DW_OP_call_ref:
1796 case DW_OP_GNU_variable_value:
1797 size += DWARF_REF_SIZE;
1798 break;
1799 case DW_OP_implicit_value:
1800 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1801 + loc->dw_loc_oprnd1.v.val_unsigned;
1802 break;
1803 case DW_OP_implicit_pointer:
1804 case DW_OP_GNU_implicit_pointer:
1805 size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1806 break;
1807 case DW_OP_entry_value:
1808 case DW_OP_GNU_entry_value:
1810 unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
1811 size += size_of_uleb128 (op_size) + op_size;
1812 break;
1814 case DW_OP_const_type:
1815 case DW_OP_GNU_const_type:
1817 unsigned long o
1818 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1819 size += size_of_uleb128 (o) + 1;
1820 switch (loc->dw_loc_oprnd2.val_class)
1822 case dw_val_class_vec:
1823 size += loc->dw_loc_oprnd2.v.val_vec.length
1824 * loc->dw_loc_oprnd2.v.val_vec.elt_size;
1825 break;
1826 case dw_val_class_const:
1827 size += HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
1828 break;
1829 case dw_val_class_const_double:
1830 size += HOST_BITS_PER_DOUBLE_INT / BITS_PER_UNIT;
1831 break;
1832 case dw_val_class_wide_int:
1833 size += (get_full_len (*loc->dw_loc_oprnd2.v.val_wide)
1834 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
1835 break;
1836 default:
1837 gcc_unreachable ();
1839 break;
1841 case DW_OP_regval_type:
1842 case DW_OP_GNU_regval_type:
1844 unsigned long o
1845 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1846 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1847 + size_of_uleb128 (o);
1849 break;
1850 case DW_OP_deref_type:
1851 case DW_OP_GNU_deref_type:
1853 unsigned long o
1854 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1855 size += 1 + size_of_uleb128 (o);
1857 break;
1858 case DW_OP_convert:
1859 case DW_OP_reinterpret:
1860 case DW_OP_GNU_convert:
1861 case DW_OP_GNU_reinterpret:
1862 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
1863 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1864 else
1866 unsigned long o
1867 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1868 size += size_of_uleb128 (o);
1870 break;
1871 case DW_OP_GNU_parameter_ref:
1872 size += 4;
1873 break;
1874 default:
1875 break;
1878 return size;
1881 /* Return the size of a series of location descriptors. */
1883 unsigned long
1884 size_of_locs (dw_loc_descr_ref loc)
1886 dw_loc_descr_ref l;
1887 unsigned long size;
1889 /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
1890 field, to avoid writing to a PCH file. */
1891 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1893 if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
1894 break;
1895 size += size_of_loc_descr (l);
1897 if (! l)
1898 return size;
1900 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1902 l->dw_loc_addr = size;
1903 size += size_of_loc_descr (l);
1906 return size;
1909 /* Return the size of the value in a DW_AT_discr_value attribute. */
1911 static int
1912 size_of_discr_value (dw_discr_value *discr_value)
1914 if (discr_value->pos)
1915 return size_of_uleb128 (discr_value->v.uval);
1916 else
1917 return size_of_sleb128 (discr_value->v.sval);
1920 /* Return the size of the value in a DW_AT_discr_list attribute. */
1922 static int
1923 size_of_discr_list (dw_discr_list_ref discr_list)
1925 int size = 0;
1927 for (dw_discr_list_ref list = discr_list;
1928 list != NULL;
1929 list = list->dw_discr_next)
1931 /* One byte for the discriminant value descriptor, and then one or two
1932 LEB128 numbers, depending on whether it's a single case label or a
1933 range label. */
1934 size += 1;
1935 size += size_of_discr_value (&list->dw_discr_lower_bound);
1936 if (list->dw_discr_range != 0)
1937 size += size_of_discr_value (&list->dw_discr_upper_bound);
1939 return size;
1942 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
1943 static void get_ref_die_offset_label (char *, dw_die_ref);
1944 static unsigned long int get_ref_die_offset (dw_die_ref);
1946 /* Output location description stack opcode's operands (if any).
1947 The for_eh_or_skip parameter controls whether register numbers are
1948 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
1949 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
1950 info). This should be suppressed for the cases that have not been converted
1951 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
1953 static void
1954 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
1956 dw_val_ref val1 = &loc->dw_loc_oprnd1;
1957 dw_val_ref val2 = &loc->dw_loc_oprnd2;
1959 switch (loc->dw_loc_opc)
1961 #ifdef DWARF2_DEBUGGING_INFO
1962 case DW_OP_const2u:
1963 case DW_OP_const2s:
1964 dw2_asm_output_data (2, val1->v.val_int, NULL);
1965 break;
1966 case DW_OP_const4u:
1967 if (loc->dtprel)
1969 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1970 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
1971 val1->v.val_addr);
1972 fputc ('\n', asm_out_file);
1973 break;
1975 /* FALLTHRU */
1976 case DW_OP_const4s:
1977 dw2_asm_output_data (4, val1->v.val_int, NULL);
1978 break;
1979 case DW_OP_const8u:
1980 if (loc->dtprel)
1982 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1983 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
1984 val1->v.val_addr);
1985 fputc ('\n', asm_out_file);
1986 break;
1988 /* FALLTHRU */
1989 case DW_OP_const8s:
1990 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
1991 dw2_asm_output_data (8, val1->v.val_int, NULL);
1992 break;
1993 case DW_OP_skip:
1994 case DW_OP_bra:
1996 int offset;
1998 gcc_assert (val1->val_class == dw_val_class_loc);
1999 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2001 dw2_asm_output_data (2, offset, NULL);
2003 break;
2004 case DW_OP_implicit_value:
2005 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2006 switch (val2->val_class)
2008 case dw_val_class_const:
2009 dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
2010 break;
2011 case dw_val_class_vec:
2013 unsigned int elt_size = val2->v.val_vec.elt_size;
2014 unsigned int len = val2->v.val_vec.length;
2015 unsigned int i;
2016 unsigned char *p;
2018 if (elt_size > sizeof (HOST_WIDE_INT))
2020 elt_size /= 2;
2021 len *= 2;
2023 for (i = 0, p = (unsigned char *) val2->v.val_vec.array;
2024 i < len;
2025 i++, p += elt_size)
2026 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2027 "fp or vector constant word %u", i);
2029 break;
2030 case dw_val_class_const_double:
2032 unsigned HOST_WIDE_INT first, second;
2034 if (WORDS_BIG_ENDIAN)
2036 first = val2->v.val_double.high;
2037 second = val2->v.val_double.low;
2039 else
2041 first = val2->v.val_double.low;
2042 second = val2->v.val_double.high;
2044 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2045 first, NULL);
2046 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2047 second, NULL);
2049 break;
2050 case dw_val_class_wide_int:
2052 int i;
2053 int len = get_full_len (*val2->v.val_wide);
2054 if (WORDS_BIG_ENDIAN)
2055 for (i = len - 1; i >= 0; --i)
2056 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2057 val2->v.val_wide->elt (i), NULL);
2058 else
2059 for (i = 0; i < len; ++i)
2060 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2061 val2->v.val_wide->elt (i), NULL);
2063 break;
2064 case dw_val_class_addr:
2065 gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
2066 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
2067 break;
2068 default:
2069 gcc_unreachable ();
2071 break;
2072 #else
2073 case DW_OP_const2u:
2074 case DW_OP_const2s:
2075 case DW_OP_const4u:
2076 case DW_OP_const4s:
2077 case DW_OP_const8u:
2078 case DW_OP_const8s:
2079 case DW_OP_skip:
2080 case DW_OP_bra:
2081 case DW_OP_implicit_value:
2082 /* We currently don't make any attempt to make sure these are
2083 aligned properly like we do for the main unwind info, so
2084 don't support emitting things larger than a byte if we're
2085 only doing unwinding. */
2086 gcc_unreachable ();
2087 #endif
2088 case DW_OP_const1u:
2089 case DW_OP_const1s:
2090 dw2_asm_output_data (1, val1->v.val_int, NULL);
2091 break;
2092 case DW_OP_constu:
2093 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2094 break;
2095 case DW_OP_consts:
2096 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2097 break;
2098 case DW_OP_pick:
2099 dw2_asm_output_data (1, val1->v.val_int, NULL);
2100 break;
2101 case DW_OP_plus_uconst:
2102 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2103 break;
2104 case DW_OP_breg0:
2105 case DW_OP_breg1:
2106 case DW_OP_breg2:
2107 case DW_OP_breg3:
2108 case DW_OP_breg4:
2109 case DW_OP_breg5:
2110 case DW_OP_breg6:
2111 case DW_OP_breg7:
2112 case DW_OP_breg8:
2113 case DW_OP_breg9:
2114 case DW_OP_breg10:
2115 case DW_OP_breg11:
2116 case DW_OP_breg12:
2117 case DW_OP_breg13:
2118 case DW_OP_breg14:
2119 case DW_OP_breg15:
2120 case DW_OP_breg16:
2121 case DW_OP_breg17:
2122 case DW_OP_breg18:
2123 case DW_OP_breg19:
2124 case DW_OP_breg20:
2125 case DW_OP_breg21:
2126 case DW_OP_breg22:
2127 case DW_OP_breg23:
2128 case DW_OP_breg24:
2129 case DW_OP_breg25:
2130 case DW_OP_breg26:
2131 case DW_OP_breg27:
2132 case DW_OP_breg28:
2133 case DW_OP_breg29:
2134 case DW_OP_breg30:
2135 case DW_OP_breg31:
2136 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2137 break;
2138 case DW_OP_regx:
2140 unsigned r = val1->v.val_unsigned;
2141 if (for_eh_or_skip >= 0)
2142 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2143 gcc_assert (size_of_uleb128 (r)
2144 == size_of_uleb128 (val1->v.val_unsigned));
2145 dw2_asm_output_data_uleb128 (r, NULL);
2147 break;
2148 case DW_OP_fbreg:
2149 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2150 break;
2151 case DW_OP_bregx:
2153 unsigned r = val1->v.val_unsigned;
2154 if (for_eh_or_skip >= 0)
2155 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2156 gcc_assert (size_of_uleb128 (r)
2157 == size_of_uleb128 (val1->v.val_unsigned));
2158 dw2_asm_output_data_uleb128 (r, NULL);
2159 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2161 break;
2162 case DW_OP_piece:
2163 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2164 break;
2165 case DW_OP_bit_piece:
2166 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2167 dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
2168 break;
2169 case DW_OP_deref_size:
2170 case DW_OP_xderef_size:
2171 dw2_asm_output_data (1, val1->v.val_int, NULL);
2172 break;
2174 case DW_OP_addr:
2175 if (loc->dtprel)
2177 if (targetm.asm_out.output_dwarf_dtprel)
2179 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
2180 DWARF2_ADDR_SIZE,
2181 val1->v.val_addr);
2182 fputc ('\n', asm_out_file);
2184 else
2185 gcc_unreachable ();
2187 else
2189 #ifdef DWARF2_DEBUGGING_INFO
2190 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
2191 #else
2192 gcc_unreachable ();
2193 #endif
2195 break;
2197 case DW_OP_GNU_addr_index:
2198 case DW_OP_GNU_const_index:
2199 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
2200 dw2_asm_output_data_uleb128 (loc->dw_loc_oprnd1.val_entry->index,
2201 "(index into .debug_addr)");
2202 break;
2204 case DW_OP_call2:
2205 case DW_OP_call4:
2207 unsigned long die_offset
2208 = get_ref_die_offset (val1->v.val_die_ref.die);
2209 /* Make sure the offset has been computed and that we can encode it as
2210 an operand. */
2211 gcc_assert (die_offset > 0
2212 && die_offset <= (loc->dw_loc_opc == DW_OP_call2
2213 ? 0xffff
2214 : 0xffffffff));
2215 dw2_asm_output_data ((loc->dw_loc_opc == DW_OP_call2) ? 2 : 4,
2216 die_offset, NULL);
2218 break;
2220 case DW_OP_call_ref:
2221 case DW_OP_GNU_variable_value:
2223 char label[MAX_ARTIFICIAL_LABEL_BYTES
2224 + HOST_BITS_PER_WIDE_INT / 2 + 2];
2225 gcc_assert (val1->val_class == dw_val_class_die_ref);
2226 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2227 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2229 break;
2231 case DW_OP_implicit_pointer:
2232 case DW_OP_GNU_implicit_pointer:
2234 char label[MAX_ARTIFICIAL_LABEL_BYTES
2235 + HOST_BITS_PER_WIDE_INT / 2 + 2];
2236 gcc_assert (val1->val_class == dw_val_class_die_ref);
2237 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2238 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2239 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2241 break;
2243 case DW_OP_entry_value:
2244 case DW_OP_GNU_entry_value:
2245 dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
2246 output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
2247 break;
2249 case DW_OP_const_type:
2250 case DW_OP_GNU_const_type:
2252 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
2253 gcc_assert (o);
2254 dw2_asm_output_data_uleb128 (o, NULL);
2255 switch (val2->val_class)
2257 case dw_val_class_const:
2258 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2259 dw2_asm_output_data (1, l, NULL);
2260 dw2_asm_output_data (l, val2->v.val_int, NULL);
2261 break;
2262 case dw_val_class_vec:
2264 unsigned int elt_size = val2->v.val_vec.elt_size;
2265 unsigned int len = val2->v.val_vec.length;
2266 unsigned int i;
2267 unsigned char *p;
2269 l = len * elt_size;
2270 dw2_asm_output_data (1, l, NULL);
2271 if (elt_size > sizeof (HOST_WIDE_INT))
2273 elt_size /= 2;
2274 len *= 2;
2276 for (i = 0, p = (unsigned char *) val2->v.val_vec.array;
2277 i < len;
2278 i++, p += elt_size)
2279 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2280 "fp or vector constant word %u", i);
2282 break;
2283 case dw_val_class_const_double:
2285 unsigned HOST_WIDE_INT first, second;
2286 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2288 dw2_asm_output_data (1, 2 * l, NULL);
2289 if (WORDS_BIG_ENDIAN)
2291 first = val2->v.val_double.high;
2292 second = val2->v.val_double.low;
2294 else
2296 first = val2->v.val_double.low;
2297 second = val2->v.val_double.high;
2299 dw2_asm_output_data (l, first, NULL);
2300 dw2_asm_output_data (l, second, NULL);
2302 break;
2303 case dw_val_class_wide_int:
2305 int i;
2306 int len = get_full_len (*val2->v.val_wide);
2307 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2309 dw2_asm_output_data (1, len * l, NULL);
2310 if (WORDS_BIG_ENDIAN)
2311 for (i = len - 1; i >= 0; --i)
2312 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2313 else
2314 for (i = 0; i < len; ++i)
2315 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2317 break;
2318 default:
2319 gcc_unreachable ();
2322 break;
2323 case DW_OP_regval_type:
2324 case DW_OP_GNU_regval_type:
2326 unsigned r = val1->v.val_unsigned;
2327 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2328 gcc_assert (o);
2329 if (for_eh_or_skip >= 0)
2331 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2332 gcc_assert (size_of_uleb128 (r)
2333 == size_of_uleb128 (val1->v.val_unsigned));
2335 dw2_asm_output_data_uleb128 (r, NULL);
2336 dw2_asm_output_data_uleb128 (o, NULL);
2338 break;
2339 case DW_OP_deref_type:
2340 case DW_OP_GNU_deref_type:
2342 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2343 gcc_assert (o);
2344 dw2_asm_output_data (1, val1->v.val_int, NULL);
2345 dw2_asm_output_data_uleb128 (o, NULL);
2347 break;
2348 case DW_OP_convert:
2349 case DW_OP_reinterpret:
2350 case DW_OP_GNU_convert:
2351 case DW_OP_GNU_reinterpret:
2352 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2353 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2354 else
2356 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
2357 gcc_assert (o);
2358 dw2_asm_output_data_uleb128 (o, NULL);
2360 break;
2362 case DW_OP_GNU_parameter_ref:
2364 unsigned long o;
2365 gcc_assert (val1->val_class == dw_val_class_die_ref);
2366 o = get_ref_die_offset (val1->v.val_die_ref.die);
2367 dw2_asm_output_data (4, o, NULL);
2369 break;
2371 default:
2372 /* Other codes have no operands. */
2373 break;
2377 /* Output a sequence of location operations.
2378 The for_eh_or_skip parameter controls whether register numbers are
2379 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2380 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2381 info). This should be suppressed for the cases that have not been converted
2382 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
2384 void
2385 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
2387 for (; loc != NULL; loc = loc->dw_loc_next)
2389 enum dwarf_location_atom opc = loc->dw_loc_opc;
2390 /* Output the opcode. */
2391 if (for_eh_or_skip >= 0
2392 && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2394 unsigned r = (opc - DW_OP_breg0);
2395 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2396 gcc_assert (r <= 31);
2397 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2399 else if (for_eh_or_skip >= 0
2400 && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2402 unsigned r = (opc - DW_OP_reg0);
2403 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2404 gcc_assert (r <= 31);
2405 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2408 dw2_asm_output_data (1, opc,
2409 "%s", dwarf_stack_op_name (opc));
2411 /* Output the operand(s) (if any). */
2412 output_loc_operands (loc, for_eh_or_skip);
2416 /* Output location description stack opcode's operands (if any).
2417 The output is single bytes on a line, suitable for .cfi_escape. */
2419 static void
2420 output_loc_operands_raw (dw_loc_descr_ref loc)
2422 dw_val_ref val1 = &loc->dw_loc_oprnd1;
2423 dw_val_ref val2 = &loc->dw_loc_oprnd2;
2425 switch (loc->dw_loc_opc)
2427 case DW_OP_addr:
2428 case DW_OP_GNU_addr_index:
2429 case DW_OP_GNU_const_index:
2430 case DW_OP_implicit_value:
2431 /* We cannot output addresses in .cfi_escape, only bytes. */
2432 gcc_unreachable ();
2434 case DW_OP_const1u:
2435 case DW_OP_const1s:
2436 case DW_OP_pick:
2437 case DW_OP_deref_size:
2438 case DW_OP_xderef_size:
2439 fputc (',', asm_out_file);
2440 dw2_asm_output_data_raw (1, val1->v.val_int);
2441 break;
2443 case DW_OP_const2u:
2444 case DW_OP_const2s:
2445 fputc (',', asm_out_file);
2446 dw2_asm_output_data_raw (2, val1->v.val_int);
2447 break;
2449 case DW_OP_const4u:
2450 case DW_OP_const4s:
2451 fputc (',', asm_out_file);
2452 dw2_asm_output_data_raw (4, val1->v.val_int);
2453 break;
2455 case DW_OP_const8u:
2456 case DW_OP_const8s:
2457 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2458 fputc (',', asm_out_file);
2459 dw2_asm_output_data_raw (8, val1->v.val_int);
2460 break;
2462 case DW_OP_skip:
2463 case DW_OP_bra:
2465 int offset;
2467 gcc_assert (val1->val_class == dw_val_class_loc);
2468 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2470 fputc (',', asm_out_file);
2471 dw2_asm_output_data_raw (2, offset);
2473 break;
2475 case DW_OP_regx:
2477 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2478 gcc_assert (size_of_uleb128 (r)
2479 == size_of_uleb128 (val1->v.val_unsigned));
2480 fputc (',', asm_out_file);
2481 dw2_asm_output_data_uleb128_raw (r);
2483 break;
2485 case DW_OP_constu:
2486 case DW_OP_plus_uconst:
2487 case DW_OP_piece:
2488 fputc (',', asm_out_file);
2489 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2490 break;
2492 case DW_OP_bit_piece:
2493 fputc (',', asm_out_file);
2494 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2495 dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
2496 break;
2498 case DW_OP_consts:
2499 case DW_OP_breg0:
2500 case DW_OP_breg1:
2501 case DW_OP_breg2:
2502 case DW_OP_breg3:
2503 case DW_OP_breg4:
2504 case DW_OP_breg5:
2505 case DW_OP_breg6:
2506 case DW_OP_breg7:
2507 case DW_OP_breg8:
2508 case DW_OP_breg9:
2509 case DW_OP_breg10:
2510 case DW_OP_breg11:
2511 case DW_OP_breg12:
2512 case DW_OP_breg13:
2513 case DW_OP_breg14:
2514 case DW_OP_breg15:
2515 case DW_OP_breg16:
2516 case DW_OP_breg17:
2517 case DW_OP_breg18:
2518 case DW_OP_breg19:
2519 case DW_OP_breg20:
2520 case DW_OP_breg21:
2521 case DW_OP_breg22:
2522 case DW_OP_breg23:
2523 case DW_OP_breg24:
2524 case DW_OP_breg25:
2525 case DW_OP_breg26:
2526 case DW_OP_breg27:
2527 case DW_OP_breg28:
2528 case DW_OP_breg29:
2529 case DW_OP_breg30:
2530 case DW_OP_breg31:
2531 case DW_OP_fbreg:
2532 fputc (',', asm_out_file);
2533 dw2_asm_output_data_sleb128_raw (val1->v.val_int);
2534 break;
2536 case DW_OP_bregx:
2538 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2539 gcc_assert (size_of_uleb128 (r)
2540 == size_of_uleb128 (val1->v.val_unsigned));
2541 fputc (',', asm_out_file);
2542 dw2_asm_output_data_uleb128_raw (r);
2543 fputc (',', asm_out_file);
2544 dw2_asm_output_data_sleb128_raw (val2->v.val_int);
2546 break;
2548 case DW_OP_implicit_pointer:
2549 case DW_OP_entry_value:
2550 case DW_OP_const_type:
2551 case DW_OP_regval_type:
2552 case DW_OP_deref_type:
2553 case DW_OP_convert:
2554 case DW_OP_reinterpret:
2555 case DW_OP_GNU_implicit_pointer:
2556 case DW_OP_GNU_entry_value:
2557 case DW_OP_GNU_const_type:
2558 case DW_OP_GNU_regval_type:
2559 case DW_OP_GNU_deref_type:
2560 case DW_OP_GNU_convert:
2561 case DW_OP_GNU_reinterpret:
2562 case DW_OP_GNU_parameter_ref:
2563 gcc_unreachable ();
2564 break;
2566 default:
2567 /* Other codes have no operands. */
2568 break;
2572 void
2573 output_loc_sequence_raw (dw_loc_descr_ref loc)
2575 while (1)
2577 enum dwarf_location_atom opc = loc->dw_loc_opc;
2578 /* Output the opcode. */
2579 if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2581 unsigned r = (opc - DW_OP_breg0);
2582 r = DWARF2_FRAME_REG_OUT (r, 1);
2583 gcc_assert (r <= 31);
2584 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2586 else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2588 unsigned r = (opc - DW_OP_reg0);
2589 r = DWARF2_FRAME_REG_OUT (r, 1);
2590 gcc_assert (r <= 31);
2591 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2593 /* Output the opcode. */
2594 fprintf (asm_out_file, "%#x", opc);
2595 output_loc_operands_raw (loc);
2597 if (!loc->dw_loc_next)
2598 break;
2599 loc = loc->dw_loc_next;
2601 fputc (',', asm_out_file);
2605 /* This function builds a dwarf location descriptor sequence from a
2606 dw_cfa_location, adding the given OFFSET to the result of the
2607 expression. */
2609 struct dw_loc_descr_node *
2610 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
2612 struct dw_loc_descr_node *head, *tmp;
2614 offset += cfa->offset;
2616 if (cfa->indirect)
2618 head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
2619 head->dw_loc_oprnd1.val_class = dw_val_class_const;
2620 head->dw_loc_oprnd1.val_entry = NULL;
2621 tmp = new_loc_descr (DW_OP_deref, 0, 0);
2622 add_loc_descr (&head, tmp);
2623 if (offset != 0)
2625 tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
2626 add_loc_descr (&head, tmp);
2629 else
2630 head = new_reg_loc_descr (cfa->reg, offset);
2632 return head;
2635 /* This function builds a dwarf location descriptor sequence for
2636 the address at OFFSET from the CFA when stack is aligned to
2637 ALIGNMENT byte. */
2639 struct dw_loc_descr_node *
2640 build_cfa_aligned_loc (dw_cfa_location *cfa,
2641 HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
2643 struct dw_loc_descr_node *head;
2644 unsigned int dwarf_fp
2645 = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
2647 /* When CFA is defined as FP+OFFSET, emulate stack alignment. */
2648 if (cfa->reg == HARD_FRAME_POINTER_REGNUM && cfa->indirect == 0)
2650 head = new_reg_loc_descr (dwarf_fp, 0);
2651 add_loc_descr (&head, int_loc_descriptor (alignment));
2652 add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
2653 loc_descr_plus_const (&head, offset);
2655 else
2656 head = new_reg_loc_descr (dwarf_fp, offset);
2657 return head;
2660 /* And now, the support for symbolic debugging information. */
2662 /* .debug_str support. */
2664 static void dwarf2out_init (const char *);
2665 static void dwarf2out_finish (const char *);
2666 static void dwarf2out_early_finish (const char *);
2667 static void dwarf2out_assembly_start (void);
2668 static void dwarf2out_define (unsigned int, const char *);
2669 static void dwarf2out_undef (unsigned int, const char *);
2670 static void dwarf2out_start_source_file (unsigned, const char *);
2671 static void dwarf2out_end_source_file (unsigned);
2672 static void dwarf2out_function_decl (tree);
2673 static void dwarf2out_begin_block (unsigned, unsigned);
2674 static void dwarf2out_end_block (unsigned, unsigned);
2675 static bool dwarf2out_ignore_block (const_tree);
2676 static void dwarf2out_early_global_decl (tree);
2677 static void dwarf2out_late_global_decl (tree);
2678 static void dwarf2out_type_decl (tree, int);
2679 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
2680 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
2681 dw_die_ref);
2682 static void dwarf2out_abstract_function (tree);
2683 static void dwarf2out_var_location (rtx_insn *);
2684 static void dwarf2out_size_function (tree);
2685 static void dwarf2out_begin_function (tree);
2686 static void dwarf2out_end_function (unsigned int);
2687 static void dwarf2out_register_main_translation_unit (tree unit);
2688 static void dwarf2out_set_name (tree, tree);
2690 /* The debug hooks structure. */
2692 const struct gcc_debug_hooks dwarf2_debug_hooks =
2694 dwarf2out_init,
2695 dwarf2out_finish,
2696 dwarf2out_early_finish,
2697 dwarf2out_assembly_start,
2698 dwarf2out_define,
2699 dwarf2out_undef,
2700 dwarf2out_start_source_file,
2701 dwarf2out_end_source_file,
2702 dwarf2out_begin_block,
2703 dwarf2out_end_block,
2704 dwarf2out_ignore_block,
2705 dwarf2out_source_line,
2706 dwarf2out_begin_prologue,
2707 #if VMS_DEBUGGING_INFO
2708 dwarf2out_vms_end_prologue,
2709 dwarf2out_vms_begin_epilogue,
2710 #else
2711 debug_nothing_int_charstar,
2712 debug_nothing_int_charstar,
2713 #endif
2714 dwarf2out_end_epilogue,
2715 dwarf2out_begin_function,
2716 dwarf2out_end_function, /* end_function */
2717 dwarf2out_register_main_translation_unit,
2718 dwarf2out_function_decl, /* function_decl */
2719 dwarf2out_early_global_decl,
2720 dwarf2out_late_global_decl,
2721 dwarf2out_type_decl, /* type_decl */
2722 dwarf2out_imported_module_or_decl,
2723 debug_nothing_tree, /* deferred_inline_function */
2724 /* The DWARF 2 backend tries to reduce debugging bloat by not
2725 emitting the abstract description of inline functions until
2726 something tries to reference them. */
2727 dwarf2out_abstract_function, /* outlining_inline_function */
2728 debug_nothing_rtx_code_label, /* label */
2729 debug_nothing_int, /* handle_pch */
2730 dwarf2out_var_location,
2731 dwarf2out_size_function, /* size_function */
2732 dwarf2out_switch_text_section,
2733 dwarf2out_set_name,
2734 1, /* start_end_main_source_file */
2735 TYPE_SYMTAB_IS_DIE /* tree_type_symtab_field */
2738 const struct gcc_debug_hooks dwarf2_lineno_debug_hooks =
2740 dwarf2out_init,
2741 debug_nothing_charstar,
2742 debug_nothing_charstar,
2743 dwarf2out_assembly_start,
2744 debug_nothing_int_charstar,
2745 debug_nothing_int_charstar,
2746 debug_nothing_int_charstar,
2747 debug_nothing_int,
2748 debug_nothing_int_int, /* begin_block */
2749 debug_nothing_int_int, /* end_block */
2750 debug_true_const_tree, /* ignore_block */
2751 dwarf2out_source_line, /* source_line */
2752 debug_nothing_int_int_charstar, /* begin_prologue */
2753 debug_nothing_int_charstar, /* end_prologue */
2754 debug_nothing_int_charstar, /* begin_epilogue */
2755 debug_nothing_int_charstar, /* end_epilogue */
2756 debug_nothing_tree, /* begin_function */
2757 debug_nothing_int, /* end_function */
2758 debug_nothing_tree, /* register_main_translation_unit */
2759 debug_nothing_tree, /* function_decl */
2760 debug_nothing_tree, /* early_global_decl */
2761 debug_nothing_tree, /* late_global_decl */
2762 debug_nothing_tree_int, /* type_decl */
2763 debug_nothing_tree_tree_tree_bool, /* imported_module_or_decl */
2764 debug_nothing_tree, /* deferred_inline_function */
2765 debug_nothing_tree, /* outlining_inline_function */
2766 debug_nothing_rtx_code_label, /* label */
2767 debug_nothing_int, /* handle_pch */
2768 debug_nothing_rtx_insn, /* var_location */
2769 debug_nothing_tree, /* size_function */
2770 debug_nothing_void, /* switch_text_section */
2771 debug_nothing_tree_tree, /* set_name */
2772 0, /* start_end_main_source_file */
2773 TYPE_SYMTAB_IS_ADDRESS /* tree_type_symtab_field */
2776 /* NOTE: In the comments in this file, many references are made to
2777 "Debugging Information Entries". This term is abbreviated as `DIE'
2778 throughout the remainder of this file. */
2780 /* An internal representation of the DWARF output is built, and then
2781 walked to generate the DWARF debugging info. The walk of the internal
2782 representation is done after the entire program has been compiled.
2783 The types below are used to describe the internal representation. */
2785 /* Whether to put type DIEs into their own section .debug_types instead
2786 of making them part of the .debug_info section. Only supported for
2787 Dwarf V4 or higher and the user didn't disable them through
2788 -fno-debug-types-section. It is more efficient to put them in a
2789 separate comdat sections since the linker will then be able to
2790 remove duplicates. But not all tools support .debug_types sections
2791 yet. For Dwarf V5 or higher .debug_types doesn't exist any more,
2792 it is DW_UT_type unit type in .debug_info section. */
2794 #define use_debug_types (dwarf_version >= 4 && flag_debug_types_section)
2796 /* Various DIE's use offsets relative to the beginning of the
2797 .debug_info section to refer to each other. */
2799 typedef long int dw_offset;
2801 struct comdat_type_node;
2803 /* The entries in the line_info table more-or-less mirror the opcodes
2804 that are used in the real dwarf line table. Arrays of these entries
2805 are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
2806 supported. */
2808 enum dw_line_info_opcode {
2809 /* Emit DW_LNE_set_address; the operand is the label index. */
2810 LI_set_address,
2812 /* Emit a row to the matrix with the given line. This may be done
2813 via any combination of DW_LNS_copy, DW_LNS_advance_line, and
2814 special opcodes. */
2815 LI_set_line,
2817 /* Emit a DW_LNS_set_file. */
2818 LI_set_file,
2820 /* Emit a DW_LNS_set_column. */
2821 LI_set_column,
2823 /* Emit a DW_LNS_negate_stmt; the operand is ignored. */
2824 LI_negate_stmt,
2826 /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored. */
2827 LI_set_prologue_end,
2828 LI_set_epilogue_begin,
2830 /* Emit a DW_LNE_set_discriminator. */
2831 LI_set_discriminator
2834 typedef struct GTY(()) dw_line_info_struct {
2835 enum dw_line_info_opcode opcode;
2836 unsigned int val;
2837 } dw_line_info_entry;
2840 struct GTY(()) dw_line_info_table {
2841 /* The label that marks the end of this section. */
2842 const char *end_label;
2844 /* The values for the last row of the matrix, as collected in the table.
2845 These are used to minimize the changes to the next row. */
2846 unsigned int file_num;
2847 unsigned int line_num;
2848 unsigned int column_num;
2849 int discrim_num;
2850 bool is_stmt;
2851 bool in_use;
2853 vec<dw_line_info_entry, va_gc> *entries;
2857 /* Each DIE attribute has a field specifying the attribute kind,
2858 a link to the next attribute in the chain, and an attribute value.
2859 Attributes are typically linked below the DIE they modify. */
2861 typedef struct GTY(()) dw_attr_struct {
2862 enum dwarf_attribute dw_attr;
2863 dw_val_node dw_attr_val;
2865 dw_attr_node;
2868 /* The Debugging Information Entry (DIE) structure. DIEs form a tree.
2869 The children of each node form a circular list linked by
2870 die_sib. die_child points to the node *before* the "first" child node. */
2872 typedef struct GTY((chain_circular ("%h.die_sib"), for_user)) die_struct {
2873 union die_symbol_or_type_node
2875 const char * GTY ((tag ("0"))) die_symbol;
2876 comdat_type_node *GTY ((tag ("1"))) die_type_node;
2878 GTY ((desc ("%0.comdat_type_p"))) die_id;
2879 vec<dw_attr_node, va_gc> *die_attr;
2880 dw_die_ref die_parent;
2881 dw_die_ref die_child;
2882 dw_die_ref die_sib;
2883 dw_die_ref die_definition; /* ref from a specification to its definition */
2884 dw_offset die_offset;
2885 unsigned long die_abbrev;
2886 int die_mark;
2887 unsigned int decl_id;
2888 enum dwarf_tag die_tag;
2889 /* Die is used and must not be pruned as unused. */
2890 BOOL_BITFIELD die_perennial_p : 1;
2891 BOOL_BITFIELD comdat_type_p : 1; /* DIE has a type signature */
2892 /* Whether this DIE was removed from the DIE tree, for example via
2893 prune_unused_types. We don't consider those present from the
2894 DIE lookup routines. */
2895 BOOL_BITFIELD removed : 1;
2896 /* Lots of spare bits. */
2898 die_node;
2900 /* Set to TRUE while dwarf2out_early_global_decl is running. */
2901 static bool early_dwarf;
2902 static bool early_dwarf_finished;
2903 struct set_early_dwarf {
2904 bool saved;
2905 set_early_dwarf () : saved(early_dwarf)
2907 gcc_assert (! early_dwarf_finished);
2908 early_dwarf = true;
2910 ~set_early_dwarf () { early_dwarf = saved; }
2913 /* Evaluate 'expr' while 'c' is set to each child of DIE in order. */
2914 #define FOR_EACH_CHILD(die, c, expr) do { \
2915 c = die->die_child; \
2916 if (c) do { \
2917 c = c->die_sib; \
2918 expr; \
2919 } while (c != die->die_child); \
2920 } while (0)
2922 /* The pubname structure */
2924 typedef struct GTY(()) pubname_struct {
2925 dw_die_ref die;
2926 const char *name;
2928 pubname_entry;
2931 struct GTY(()) dw_ranges {
2932 const char *label;
2933 /* If this is positive, it's a block number, otherwise it's a
2934 bitwise-negated index into dw_ranges_by_label. */
2935 int num;
2936 /* Index for the range list for DW_FORM_rnglistx. */
2937 unsigned int idx : 31;
2938 /* True if this range might be possibly in a different section
2939 from previous entry. */
2940 unsigned int maybe_new_sec : 1;
2943 /* A structure to hold a macinfo entry. */
2945 typedef struct GTY(()) macinfo_struct {
2946 unsigned char code;
2947 unsigned HOST_WIDE_INT lineno;
2948 const char *info;
2950 macinfo_entry;
2953 struct GTY(()) dw_ranges_by_label {
2954 const char *begin;
2955 const char *end;
2958 /* The comdat type node structure. */
2959 struct GTY(()) comdat_type_node
2961 dw_die_ref root_die;
2962 dw_die_ref type_die;
2963 dw_die_ref skeleton_die;
2964 char signature[DWARF_TYPE_SIGNATURE_SIZE];
2965 comdat_type_node *next;
2968 /* A list of DIEs for which we can't determine ancestry (parent_die
2969 field) just yet. Later in dwarf2out_finish we will fill in the
2970 missing bits. */
2971 typedef struct GTY(()) limbo_die_struct {
2972 dw_die_ref die;
2973 /* The tree for which this DIE was created. We use this to
2974 determine ancestry later. */
2975 tree created_for;
2976 struct limbo_die_struct *next;
2978 limbo_die_node;
2980 typedef struct skeleton_chain_struct
2982 dw_die_ref old_die;
2983 dw_die_ref new_die;
2984 struct skeleton_chain_struct *parent;
2986 skeleton_chain_node;
2988 /* Define a macro which returns nonzero for a TYPE_DECL which was
2989 implicitly generated for a type.
2991 Note that, unlike the C front-end (which generates a NULL named
2992 TYPE_DECL node for each complete tagged type, each array type,
2993 and each function type node created) the C++ front-end generates
2994 a _named_ TYPE_DECL node for each tagged type node created.
2995 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
2996 generate a DW_TAG_typedef DIE for them. Likewise with the Ada
2997 front-end, but for each type, tagged or not. */
2999 #define TYPE_DECL_IS_STUB(decl) \
3000 (DECL_NAME (decl) == NULL_TREE \
3001 || (DECL_ARTIFICIAL (decl) \
3002 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
3003 /* This is necessary for stub decls that \
3004 appear in nested inline functions. */ \
3005 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
3006 && (decl_ultimate_origin (decl) \
3007 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
3009 /* Information concerning the compilation unit's programming
3010 language, and compiler version. */
3012 /* Fixed size portion of the DWARF compilation unit header. */
3013 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
3014 (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE \
3015 + (dwarf_version >= 5 ? 4 : 3))
3017 /* Fixed size portion of the DWARF comdat type unit header. */
3018 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
3019 (DWARF_COMPILE_UNIT_HEADER_SIZE \
3020 + DWARF_TYPE_SIGNATURE_SIZE + DWARF_OFFSET_SIZE)
3022 /* Fixed size portion of the DWARF skeleton compilation unit header. */
3023 #define DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE \
3024 (DWARF_COMPILE_UNIT_HEADER_SIZE + (dwarf_version >= 5 ? 8 : 0))
3026 /* Fixed size portion of public names info. */
3027 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
3029 /* Fixed size portion of the address range info. */
3030 #define DWARF_ARANGES_HEADER_SIZE \
3031 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
3032 DWARF2_ADDR_SIZE * 2) \
3033 - DWARF_INITIAL_LENGTH_SIZE)
3035 /* Size of padding portion in the address range info. It must be
3036 aligned to twice the pointer size. */
3037 #define DWARF_ARANGES_PAD_SIZE \
3038 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
3039 DWARF2_ADDR_SIZE * 2) \
3040 - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
3042 /* Use assembler line directives if available. */
3043 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
3044 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
3045 #define DWARF2_ASM_LINE_DEBUG_INFO 1
3046 #else
3047 #define DWARF2_ASM_LINE_DEBUG_INFO 0
3048 #endif
3049 #endif
3051 /* Minimum line offset in a special line info. opcode.
3052 This value was chosen to give a reasonable range of values. */
3053 #define DWARF_LINE_BASE -10
3055 /* First special line opcode - leave room for the standard opcodes. */
3056 #define DWARF_LINE_OPCODE_BASE ((int)DW_LNS_set_isa + 1)
3058 /* Range of line offsets in a special line info. opcode. */
3059 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
3061 /* Flag that indicates the initial value of the is_stmt_start flag.
3062 In the present implementation, we do not mark any lines as
3063 the beginning of a source statement, because that information
3064 is not made available by the GCC front-end. */
3065 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
3067 /* Maximum number of operations per instruction bundle. */
3068 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
3069 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
3070 #endif
3072 /* This location is used by calc_die_sizes() to keep track
3073 the offset of each DIE within the .debug_info section. */
3074 static unsigned long next_die_offset;
3076 /* Record the root of the DIE's built for the current compilation unit. */
3077 static GTY(()) dw_die_ref single_comp_unit_die;
3079 /* A list of type DIEs that have been separated into comdat sections. */
3080 static GTY(()) comdat_type_node *comdat_type_list;
3082 /* A list of CU DIEs that have been separated. */
3083 static GTY(()) limbo_die_node *cu_die_list;
3085 /* A list of DIEs with a NULL parent waiting to be relocated. */
3086 static GTY(()) limbo_die_node *limbo_die_list;
3088 /* A list of DIEs for which we may have to generate
3089 DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set. */
3090 static GTY(()) limbo_die_node *deferred_asm_name;
3092 struct dwarf_file_hasher : ggc_ptr_hash<dwarf_file_data>
3094 typedef const char *compare_type;
3096 static hashval_t hash (dwarf_file_data *);
3097 static bool equal (dwarf_file_data *, const char *);
3100 /* Filenames referenced by this compilation unit. */
3101 static GTY(()) hash_table<dwarf_file_hasher> *file_table;
3103 struct decl_die_hasher : ggc_ptr_hash<die_node>
3105 typedef tree compare_type;
3107 static hashval_t hash (die_node *);
3108 static bool equal (die_node *, tree);
3110 /* A hash table of references to DIE's that describe declarations.
3111 The key is a DECL_UID() which is a unique number identifying each decl. */
3112 static GTY (()) hash_table<decl_die_hasher> *decl_die_table;
3114 struct GTY ((for_user)) variable_value_struct {
3115 unsigned int decl_id;
3116 vec<dw_die_ref, va_gc> *dies;
3119 struct variable_value_hasher : ggc_ptr_hash<variable_value_struct>
3121 typedef tree compare_type;
3123 static hashval_t hash (variable_value_struct *);
3124 static bool equal (variable_value_struct *, tree);
3126 /* A hash table of DIEs that contain DW_OP_GNU_variable_value with
3127 dw_val_class_decl_ref class, indexed by FUNCTION_DECLs which is
3128 DECL_CONTEXT of the referenced VAR_DECLs. */
3129 static GTY (()) hash_table<variable_value_hasher> *variable_value_hash;
3131 struct block_die_hasher : ggc_ptr_hash<die_struct>
3133 static hashval_t hash (die_struct *);
3134 static bool equal (die_struct *, die_struct *);
3137 /* A hash table of references to DIE's that describe COMMON blocks.
3138 The key is DECL_UID() ^ die_parent. */
3139 static GTY (()) hash_table<block_die_hasher> *common_block_die_table;
3141 typedef struct GTY(()) die_arg_entry_struct {
3142 dw_die_ref die;
3143 tree arg;
3144 } die_arg_entry;
3147 /* Node of the variable location list. */
3148 struct GTY ((chain_next ("%h.next"))) var_loc_node {
3149 /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
3150 EXPR_LIST chain. For small bitsizes, bitsize is encoded
3151 in mode of the EXPR_LIST node and first EXPR_LIST operand
3152 is either NOTE_INSN_VAR_LOCATION for a piece with a known
3153 location or NULL for padding. For larger bitsizes,
3154 mode is 0 and first operand is a CONCAT with bitsize
3155 as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
3156 NULL as second operand. */
3157 rtx GTY (()) loc;
3158 const char * GTY (()) label;
3159 struct var_loc_node * GTY (()) next;
3162 /* Variable location list. */
3163 struct GTY ((for_user)) var_loc_list_def {
3164 struct var_loc_node * GTY (()) first;
3166 /* Pointer to the last but one or last element of the
3167 chained list. If the list is empty, both first and
3168 last are NULL, if the list contains just one node
3169 or the last node certainly is not redundant, it points
3170 to the last node, otherwise points to the last but one.
3171 Do not mark it for GC because it is marked through the chain. */
3172 struct var_loc_node * GTY ((skip ("%h"))) last;
3174 /* Pointer to the last element before section switch,
3175 if NULL, either sections weren't switched or first
3176 is after section switch. */
3177 struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
3179 /* DECL_UID of the variable decl. */
3180 unsigned int decl_id;
3182 typedef struct var_loc_list_def var_loc_list;
3184 /* Call argument location list. */
3185 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
3186 rtx GTY (()) call_arg_loc_note;
3187 const char * GTY (()) label;
3188 tree GTY (()) block;
3189 bool tail_call_p;
3190 rtx GTY (()) symbol_ref;
3191 struct call_arg_loc_node * GTY (()) next;
3195 struct decl_loc_hasher : ggc_ptr_hash<var_loc_list>
3197 typedef const_tree compare_type;
3199 static hashval_t hash (var_loc_list *);
3200 static bool equal (var_loc_list *, const_tree);
3203 /* Table of decl location linked lists. */
3204 static GTY (()) hash_table<decl_loc_hasher> *decl_loc_table;
3206 /* Head and tail of call_arg_loc chain. */
3207 static GTY (()) struct call_arg_loc_node *call_arg_locations;
3208 static struct call_arg_loc_node *call_arg_loc_last;
3210 /* Number of call sites in the current function. */
3211 static int call_site_count = -1;
3212 /* Number of tail call sites in the current function. */
3213 static int tail_call_site_count = -1;
3215 /* A cached location list. */
3216 struct GTY ((for_user)) cached_dw_loc_list_def {
3217 /* The DECL_UID of the decl that this entry describes. */
3218 unsigned int decl_id;
3220 /* The cached location list. */
3221 dw_loc_list_ref loc_list;
3223 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
3225 struct dw_loc_list_hasher : ggc_ptr_hash<cached_dw_loc_list>
3228 typedef const_tree compare_type;
3230 static hashval_t hash (cached_dw_loc_list *);
3231 static bool equal (cached_dw_loc_list *, const_tree);
3234 /* Table of cached location lists. */
3235 static GTY (()) hash_table<dw_loc_list_hasher> *cached_dw_loc_list_table;
3237 /* A vector of references to DIE's that are uniquely identified by their tag,
3238 presence/absence of children DIE's, and list of attribute/value pairs. */
3239 static GTY(()) vec<dw_die_ref, va_gc> *abbrev_die_table;
3241 /* A hash map to remember the stack usage for DWARF procedures. The value
3242 stored is the stack size difference between before the DWARF procedure
3243 invokation and after it returned. In other words, for a DWARF procedure
3244 that consumes N stack slots and that pushes M ones, this stores M - N. */
3245 static hash_map<dw_die_ref, int> *dwarf_proc_stack_usage_map;
3247 /* A global counter for generating labels for line number data. */
3248 static unsigned int line_info_label_num;
3250 /* The current table to which we should emit line number information
3251 for the current function. This will be set up at the beginning of
3252 assembly for the function. */
3253 static GTY(()) dw_line_info_table *cur_line_info_table;
3255 /* The two default tables of line number info. */
3256 static GTY(()) dw_line_info_table *text_section_line_info;
3257 static GTY(()) dw_line_info_table *cold_text_section_line_info;
3259 /* The set of all non-default tables of line number info. */
3260 static GTY(()) vec<dw_line_info_table *, va_gc> *separate_line_info;
3262 /* A flag to tell pubnames/types export if there is an info section to
3263 refer to. */
3264 static bool info_section_emitted;
3266 /* A pointer to the base of a table that contains a list of publicly
3267 accessible names. */
3268 static GTY (()) vec<pubname_entry, va_gc> *pubname_table;
3270 /* A pointer to the base of a table that contains a list of publicly
3271 accessible types. */
3272 static GTY (()) vec<pubname_entry, va_gc> *pubtype_table;
3274 /* A pointer to the base of a table that contains a list of macro
3275 defines/undefines (and file start/end markers). */
3276 static GTY (()) vec<macinfo_entry, va_gc> *macinfo_table;
3278 /* True if .debug_macinfo or .debug_macros section is going to be
3279 emitted. */
3280 #define have_macinfo \
3281 ((!XCOFF_DEBUGGING_INFO || HAVE_XCOFF_DWARF_EXTRAS) \
3282 && debug_info_level >= DINFO_LEVEL_VERBOSE \
3283 && !macinfo_table->is_empty ())
3285 /* Vector of dies for which we should generate .debug_ranges info. */
3286 static GTY (()) vec<dw_ranges, va_gc> *ranges_table;
3288 /* Vector of pairs of labels referenced in ranges_table. */
3289 static GTY (()) vec<dw_ranges_by_label, va_gc> *ranges_by_label;
3291 /* Whether we have location lists that need outputting */
3292 static GTY(()) bool have_location_lists;
3294 /* Unique label counter. */
3295 static GTY(()) unsigned int loclabel_num;
3297 /* Unique label counter for point-of-call tables. */
3298 static GTY(()) unsigned int poc_label_num;
3300 /* The last file entry emitted by maybe_emit_file(). */
3301 static GTY(()) struct dwarf_file_data * last_emitted_file;
3303 /* Number of internal labels generated by gen_internal_sym(). */
3304 static GTY(()) int label_num;
3306 static GTY(()) vec<die_arg_entry, va_gc> *tmpl_value_parm_die_table;
3308 /* Instances of generic types for which we need to generate debug
3309 info that describe their generic parameters and arguments. That
3310 generation needs to happen once all types are properly laid out so
3311 we do it at the end of compilation. */
3312 static GTY(()) vec<tree, va_gc> *generic_type_instances;
3314 /* Offset from the "steady-state frame pointer" to the frame base,
3315 within the current function. */
3316 static HOST_WIDE_INT frame_pointer_fb_offset;
3317 static bool frame_pointer_fb_offset_valid;
3319 static vec<dw_die_ref> base_types;
3321 /* Flags to represent a set of attribute classes for attributes that represent
3322 a scalar value (bounds, pointers, ...). */
3323 enum dw_scalar_form
3325 dw_scalar_form_constant = 0x01,
3326 dw_scalar_form_exprloc = 0x02,
3327 dw_scalar_form_reference = 0x04
3330 /* Forward declarations for functions defined in this file. */
3332 static int is_pseudo_reg (const_rtx);
3333 static tree type_main_variant (tree);
3334 static int is_tagged_type (const_tree);
3335 static const char *dwarf_tag_name (unsigned);
3336 static const char *dwarf_attr_name (unsigned);
3337 static const char *dwarf_form_name (unsigned);
3338 static tree decl_ultimate_origin (const_tree);
3339 static tree decl_class_context (tree);
3340 static void add_dwarf_attr (dw_die_ref, dw_attr_node *);
3341 static inline enum dw_val_class AT_class (dw_attr_node *);
3342 static inline unsigned int AT_index (dw_attr_node *);
3343 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
3344 static inline unsigned AT_flag (dw_attr_node *);
3345 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
3346 static inline HOST_WIDE_INT AT_int (dw_attr_node *);
3347 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
3348 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_node *);
3349 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
3350 HOST_WIDE_INT, unsigned HOST_WIDE_INT);
3351 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
3352 unsigned int, unsigned char *);
3353 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
3354 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
3355 static inline const char *AT_string (dw_attr_node *);
3356 static enum dwarf_form AT_string_form (dw_attr_node *);
3357 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
3358 static void add_AT_specification (dw_die_ref, dw_die_ref);
3359 static inline dw_die_ref AT_ref (dw_attr_node *);
3360 static inline int AT_ref_external (dw_attr_node *);
3361 static inline void set_AT_ref_external (dw_attr_node *, int);
3362 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
3363 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
3364 static inline dw_loc_descr_ref AT_loc (dw_attr_node *);
3365 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
3366 dw_loc_list_ref);
3367 static inline dw_loc_list_ref AT_loc_list (dw_attr_node *);
3368 static addr_table_entry *add_addr_table_entry (void *, enum ate_kind);
3369 static void remove_addr_table_entry (addr_table_entry *);
3370 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx, bool);
3371 static inline rtx AT_addr (dw_attr_node *);
3372 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
3373 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
3374 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
3375 static void add_AT_loclistsptr (dw_die_ref, enum dwarf_attribute,
3376 const char *);
3377 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
3378 unsigned HOST_WIDE_INT);
3379 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
3380 unsigned long, bool);
3381 static inline const char *AT_lbl (dw_attr_node *);
3382 static dw_attr_node *get_AT (dw_die_ref, enum dwarf_attribute);
3383 static const char *get_AT_low_pc (dw_die_ref);
3384 static const char *get_AT_hi_pc (dw_die_ref);
3385 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
3386 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
3387 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
3388 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
3389 static bool is_cxx (void);
3390 static bool is_cxx (const_tree);
3391 static bool is_fortran (void);
3392 static bool is_ada (void);
3393 static bool remove_AT (dw_die_ref, enum dwarf_attribute);
3394 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
3395 static void add_child_die (dw_die_ref, dw_die_ref);
3396 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
3397 static dw_die_ref lookup_type_die (tree);
3398 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
3399 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
3400 static void equate_type_number_to_die (tree, dw_die_ref);
3401 static dw_die_ref lookup_decl_die (tree);
3402 static var_loc_list *lookup_decl_loc (const_tree);
3403 static void equate_decl_number_to_die (tree, dw_die_ref);
3404 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
3405 static void print_spaces (FILE *);
3406 static void print_die (dw_die_ref, FILE *);
3407 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
3408 static dw_die_ref pop_compile_unit (dw_die_ref);
3409 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3410 static void attr_checksum (dw_attr_node *, struct md5_ctx *, int *);
3411 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3412 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
3413 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
3414 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
3415 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_node *,
3416 struct md5_ctx *, int *);
3417 struct checksum_attributes;
3418 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
3419 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
3420 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
3421 static void generate_type_signature (dw_die_ref, comdat_type_node *);
3422 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3423 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
3424 static int same_attr_p (dw_attr_node *, dw_attr_node *, int *);
3425 static int same_die_p (dw_die_ref, dw_die_ref, int *);
3426 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
3427 static void compute_section_prefix (dw_die_ref);
3428 static int is_type_die (dw_die_ref);
3429 static int is_comdat_die (dw_die_ref);
3430 static int is_symbol_die (dw_die_ref);
3431 static inline bool is_template_instantiation (dw_die_ref);
3432 static void assign_symbol_names (dw_die_ref);
3433 static void break_out_includes (dw_die_ref);
3434 static int is_declaration_die (dw_die_ref);
3435 static int should_move_die_to_comdat (dw_die_ref);
3436 static dw_die_ref clone_as_declaration (dw_die_ref);
3437 static dw_die_ref clone_die (dw_die_ref);
3438 static dw_die_ref clone_tree (dw_die_ref);
3439 static dw_die_ref copy_declaration_context (dw_die_ref, dw_die_ref);
3440 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
3441 static void generate_skeleton_bottom_up (skeleton_chain_node *);
3442 static dw_die_ref generate_skeleton (dw_die_ref);
3443 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
3444 dw_die_ref,
3445 dw_die_ref);
3446 static void break_out_comdat_types (dw_die_ref);
3447 static void copy_decls_for_unworthy_types (dw_die_ref);
3449 static void add_sibling_attributes (dw_die_ref);
3450 static void output_location_lists (dw_die_ref);
3451 static int constant_size (unsigned HOST_WIDE_INT);
3452 static unsigned long size_of_die (dw_die_ref);
3453 static void calc_die_sizes (dw_die_ref);
3454 static void calc_base_type_die_sizes (void);
3455 static void mark_dies (dw_die_ref);
3456 static void unmark_dies (dw_die_ref);
3457 static void unmark_all_dies (dw_die_ref);
3458 static unsigned long size_of_pubnames (vec<pubname_entry, va_gc> *);
3459 static unsigned long size_of_aranges (void);
3460 static enum dwarf_form value_format (dw_attr_node *);
3461 static void output_value_format (dw_attr_node *);
3462 static void output_abbrev_section (void);
3463 static void output_die_abbrevs (unsigned long, dw_die_ref);
3464 static void output_die_symbol (dw_die_ref);
3465 static void output_die (dw_die_ref);
3466 static void output_compilation_unit_header (enum dwarf_unit_type);
3467 static void output_comp_unit (dw_die_ref, int, const unsigned char *);
3468 static void output_comdat_type_unit (comdat_type_node *);
3469 static const char *dwarf2_name (tree, int);
3470 static void add_pubname (tree, dw_die_ref);
3471 static void add_enumerator_pubname (const char *, dw_die_ref);
3472 static void add_pubname_string (const char *, dw_die_ref);
3473 static void add_pubtype (tree, dw_die_ref);
3474 static void output_pubnames (vec<pubname_entry, va_gc> *);
3475 static void output_aranges (void);
3476 static unsigned int add_ranges (const_tree, bool = false);
3477 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3478 bool *, bool);
3479 static void output_ranges (void);
3480 static dw_line_info_table *new_line_info_table (void);
3481 static void output_line_info (bool);
3482 static void output_file_names (void);
3483 static dw_die_ref base_type_die (tree, bool);
3484 static int is_base_type (tree);
3485 static dw_die_ref subrange_type_die (tree, tree, tree, tree, dw_die_ref);
3486 static int decl_quals (const_tree);
3487 static dw_die_ref modified_type_die (tree, int, bool, dw_die_ref);
3488 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3489 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3490 static int type_is_enum (const_tree);
3491 static unsigned int dbx_reg_number (const_rtx);
3492 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3493 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3494 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3495 enum var_init_status);
3496 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3497 enum var_init_status);
3498 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
3499 enum var_init_status);
3500 static int is_based_loc (const_rtx);
3501 static bool resolve_one_addr (rtx *);
3502 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3503 enum var_init_status);
3504 static dw_loc_descr_ref loc_descriptor (rtx, machine_mode mode,
3505 enum var_init_status);
3506 struct loc_descr_context;
3507 static void add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref);
3508 static void add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list);
3509 static dw_loc_list_ref loc_list_from_tree (tree, int,
3510 struct loc_descr_context *);
3511 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int,
3512 struct loc_descr_context *);
3513 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
3514 static tree field_type (const_tree);
3515 static unsigned int simple_type_align_in_bits (const_tree);
3516 static unsigned int simple_decl_align_in_bits (const_tree);
3517 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3518 struct vlr_context;
3519 static dw_loc_descr_ref field_byte_offset (const_tree, struct vlr_context *,
3520 HOST_WIDE_INT *);
3521 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3522 dw_loc_list_ref);
3523 static void add_data_member_location_attribute (dw_die_ref, tree,
3524 struct vlr_context *);
3525 static bool add_const_value_attribute (dw_die_ref, rtx);
3526 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3527 static void insert_wide_int (const wide_int &, unsigned char *, int);
3528 static void insert_float (const_rtx, unsigned char *);
3529 static rtx rtl_for_decl_location (tree);
3530 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool);
3531 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3532 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3533 static void add_name_attribute (dw_die_ref, const char *);
3534 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3535 static void add_comp_dir_attribute (dw_die_ref);
3536 static void add_scalar_info (dw_die_ref, enum dwarf_attribute, tree, int,
3537 struct loc_descr_context *);
3538 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree,
3539 struct loc_descr_context *);
3540 static void add_subscript_info (dw_die_ref, tree, bool);
3541 static void add_byte_size_attribute (dw_die_ref, tree);
3542 static void add_alignment_attribute (dw_die_ref, tree);
3543 static inline void add_bit_offset_attribute (dw_die_ref, tree,
3544 struct vlr_context *);
3545 static void add_bit_size_attribute (dw_die_ref, tree);
3546 static void add_prototyped_attribute (dw_die_ref, tree);
3547 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
3548 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3549 static void add_src_coords_attributes (dw_die_ref, tree);
3550 static void add_name_and_src_coords_attributes (dw_die_ref, tree, bool = false);
3551 static void add_discr_value (dw_die_ref, dw_discr_value *);
3552 static void add_discr_list (dw_die_ref, dw_discr_list_ref);
3553 static inline dw_discr_list_ref AT_discr_list (dw_attr_node *);
3554 static void push_decl_scope (tree);
3555 static void pop_decl_scope (void);
3556 static dw_die_ref scope_die_for (tree, dw_die_ref);
3557 static inline int local_scope_p (dw_die_ref);
3558 static inline int class_scope_p (dw_die_ref);
3559 static inline int class_or_namespace_scope_p (dw_die_ref);
3560 static void add_type_attribute (dw_die_ref, tree, int, bool, dw_die_ref);
3561 static void add_calling_convention_attribute (dw_die_ref, tree);
3562 static const char *type_tag (const_tree);
3563 static tree member_declared_type (const_tree);
3564 #if 0
3565 static const char *decl_start_label (tree);
3566 #endif
3567 static void gen_array_type_die (tree, dw_die_ref);
3568 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3569 #if 0
3570 static void gen_entry_point_die (tree, dw_die_ref);
3571 #endif
3572 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3573 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3574 static dw_die_ref gen_formal_parameter_pack_die (tree, tree, dw_die_ref, tree*);
3575 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3576 static void gen_formal_types_die (tree, dw_die_ref);
3577 static void gen_subprogram_die (tree, dw_die_ref);
3578 static void gen_variable_die (tree, tree, dw_die_ref);
3579 static void gen_const_die (tree, dw_die_ref);
3580 static void gen_label_die (tree, dw_die_ref);
3581 static void gen_lexical_block_die (tree, dw_die_ref);
3582 static void gen_inlined_subroutine_die (tree, dw_die_ref);
3583 static void gen_field_die (tree, struct vlr_context *, dw_die_ref);
3584 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3585 static dw_die_ref gen_compile_unit_die (const char *);
3586 static void gen_inheritance_die (tree, tree, tree, dw_die_ref);
3587 static void gen_member_die (tree, dw_die_ref);
3588 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3589 enum debug_info_usage);
3590 static void gen_subroutine_type_die (tree, dw_die_ref);
3591 static void gen_typedef_die (tree, dw_die_ref);
3592 static void gen_type_die (tree, dw_die_ref);
3593 static void gen_block_die (tree, dw_die_ref);
3594 static void decls_for_scope (tree, dw_die_ref);
3595 static bool is_naming_typedef_decl (const_tree);
3596 static inline dw_die_ref get_context_die (tree);
3597 static void gen_namespace_die (tree, dw_die_ref);
3598 static dw_die_ref gen_namelist_decl (tree, dw_die_ref, tree);
3599 static dw_die_ref gen_decl_die (tree, tree, struct vlr_context *, dw_die_ref);
3600 static dw_die_ref force_decl_die (tree);
3601 static dw_die_ref force_type_die (tree);
3602 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3603 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3604 static struct dwarf_file_data * lookup_filename (const char *);
3605 static void retry_incomplete_types (void);
3606 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3607 static void gen_generic_params_dies (tree);
3608 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
3609 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
3610 static void splice_child_die (dw_die_ref, dw_die_ref);
3611 static int file_info_cmp (const void *, const void *);
3612 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
3613 const char *, const char *);
3614 static void output_loc_list (dw_loc_list_ref);
3615 static char *gen_internal_sym (const char *);
3616 static bool want_pubnames (void);
3618 static void prune_unmark_dies (dw_die_ref);
3619 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3620 static void prune_unused_types_mark (dw_die_ref, int);
3621 static void prune_unused_types_walk (dw_die_ref);
3622 static void prune_unused_types_walk_attribs (dw_die_ref);
3623 static void prune_unused_types_prune (dw_die_ref);
3624 static void prune_unused_types (void);
3625 static int maybe_emit_file (struct dwarf_file_data *fd);
3626 static inline const char *AT_vms_delta1 (dw_attr_node *);
3627 static inline const char *AT_vms_delta2 (dw_attr_node *);
3628 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
3629 const char *, const char *);
3630 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
3631 static void gen_remaining_tmpl_value_param_die_attribute (void);
3632 static bool generic_type_p (tree);
3633 static void schedule_generic_params_dies_gen (tree t);
3634 static void gen_scheduled_generic_parms_dies (void);
3635 static void resolve_variable_values (void);
3637 static const char *comp_dir_string (void);
3639 static void hash_loc_operands (dw_loc_descr_ref, inchash::hash &);
3641 /* enum for tracking thread-local variables whose address is really an offset
3642 relative to the TLS pointer, which will need link-time relocation, but will
3643 not need relocation by the DWARF consumer. */
3645 enum dtprel_bool
3647 dtprel_false = 0,
3648 dtprel_true = 1
3651 /* Return the operator to use for an address of a variable. For dtprel_true, we
3652 use DW_OP_const*. For regular variables, which need both link-time
3653 relocation and consumer-level relocation (e.g., to account for shared objects
3654 loaded at a random address), we use DW_OP_addr*. */
3656 static inline enum dwarf_location_atom
3657 dw_addr_op (enum dtprel_bool dtprel)
3659 if (dtprel == dtprel_true)
3660 return (dwarf_split_debug_info ? DW_OP_GNU_const_index
3661 : (DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u));
3662 else
3663 return dwarf_split_debug_info ? DW_OP_GNU_addr_index : DW_OP_addr;
3666 /* Return a pointer to a newly allocated address location description. If
3667 dwarf_split_debug_info is true, then record the address with the appropriate
3668 relocation. */
3669 static inline dw_loc_descr_ref
3670 new_addr_loc_descr (rtx addr, enum dtprel_bool dtprel)
3672 dw_loc_descr_ref ref = new_loc_descr (dw_addr_op (dtprel), 0, 0);
3674 ref->dw_loc_oprnd1.val_class = dw_val_class_addr;
3675 ref->dw_loc_oprnd1.v.val_addr = addr;
3676 ref->dtprel = dtprel;
3677 if (dwarf_split_debug_info)
3678 ref->dw_loc_oprnd1.val_entry
3679 = add_addr_table_entry (addr,
3680 dtprel ? ate_kind_rtx_dtprel : ate_kind_rtx);
3681 else
3682 ref->dw_loc_oprnd1.val_entry = NULL;
3684 return ref;
3687 /* Section names used to hold DWARF debugging information. */
3689 #ifndef DEBUG_INFO_SECTION
3690 #define DEBUG_INFO_SECTION ".debug_info"
3691 #endif
3692 #ifndef DEBUG_DWO_INFO_SECTION
3693 #define DEBUG_DWO_INFO_SECTION ".debug_info.dwo"
3694 #endif
3695 #ifndef DEBUG_ABBREV_SECTION
3696 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
3697 #endif
3698 #ifndef DEBUG_DWO_ABBREV_SECTION
3699 #define DEBUG_DWO_ABBREV_SECTION ".debug_abbrev.dwo"
3700 #endif
3701 #ifndef DEBUG_ARANGES_SECTION
3702 #define DEBUG_ARANGES_SECTION ".debug_aranges"
3703 #endif
3704 #ifndef DEBUG_ADDR_SECTION
3705 #define DEBUG_ADDR_SECTION ".debug_addr"
3706 #endif
3707 #ifndef DEBUG_MACINFO_SECTION
3708 #define DEBUG_MACINFO_SECTION ".debug_macinfo"
3709 #endif
3710 #ifndef DEBUG_DWO_MACINFO_SECTION
3711 #define DEBUG_DWO_MACINFO_SECTION ".debug_macinfo.dwo"
3712 #endif
3713 #ifndef DEBUG_DWO_MACRO_SECTION
3714 #define DEBUG_DWO_MACRO_SECTION ".debug_macro.dwo"
3715 #endif
3716 #ifndef DEBUG_MACRO_SECTION
3717 #define DEBUG_MACRO_SECTION ".debug_macro"
3718 #endif
3719 #ifndef DEBUG_LINE_SECTION
3720 #define DEBUG_LINE_SECTION ".debug_line"
3721 #endif
3722 #ifndef DEBUG_DWO_LINE_SECTION
3723 #define DEBUG_DWO_LINE_SECTION ".debug_line.dwo"
3724 #endif
3725 #ifndef DEBUG_LOC_SECTION
3726 #define DEBUG_LOC_SECTION ".debug_loc"
3727 #endif
3728 #ifndef DEBUG_DWO_LOC_SECTION
3729 #define DEBUG_DWO_LOC_SECTION ".debug_loc.dwo"
3730 #endif
3731 #ifndef DEBUG_LOCLISTS_SECTION
3732 #define DEBUG_LOCLISTS_SECTION ".debug_loclists"
3733 #endif
3734 #ifndef DEBUG_DWO_LOCLISTS_SECTION
3735 #define DEBUG_DWO_LOCLISTS_SECTION ".debug_loclists.dwo"
3736 #endif
3737 #ifndef DEBUG_PUBNAMES_SECTION
3738 #define DEBUG_PUBNAMES_SECTION \
3739 ((debug_generate_pub_sections == 2) \
3740 ? ".debug_gnu_pubnames" : ".debug_pubnames")
3741 #endif
3742 #ifndef DEBUG_PUBTYPES_SECTION
3743 #define DEBUG_PUBTYPES_SECTION \
3744 ((debug_generate_pub_sections == 2) \
3745 ? ".debug_gnu_pubtypes" : ".debug_pubtypes")
3746 #endif
3747 #ifndef DEBUG_STR_OFFSETS_SECTION
3748 #define DEBUG_STR_OFFSETS_SECTION ".debug_str_offsets"
3749 #endif
3750 #ifndef DEBUG_DWO_STR_OFFSETS_SECTION
3751 #define DEBUG_DWO_STR_OFFSETS_SECTION ".debug_str_offsets.dwo"
3752 #endif
3753 #ifndef DEBUG_STR_DWO_SECTION
3754 #define DEBUG_STR_DWO_SECTION ".debug_str.dwo"
3755 #endif
3756 #ifndef DEBUG_STR_SECTION
3757 #define DEBUG_STR_SECTION ".debug_str"
3758 #endif
3759 #ifndef DEBUG_RANGES_SECTION
3760 #define DEBUG_RANGES_SECTION ".debug_ranges"
3761 #endif
3762 #ifndef DEBUG_RNGLISTS_SECTION
3763 #define DEBUG_RNGLISTS_SECTION ".debug_rnglists"
3764 #endif
3765 #ifndef DEBUG_LINE_STR_SECTION
3766 #define DEBUG_LINE_STR_SECTION ".debug_line_str"
3767 #endif
3769 /* Standard ELF section names for compiled code and data. */
3770 #ifndef TEXT_SECTION_NAME
3771 #define TEXT_SECTION_NAME ".text"
3772 #endif
3774 /* Section flags for .debug_str section. */
3775 #define DEBUG_STR_SECTION_FLAGS \
3776 (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings \
3777 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
3778 : SECTION_DEBUG)
3780 /* Section flags for .debug_str.dwo section. */
3781 #define DEBUG_STR_DWO_SECTION_FLAGS (SECTION_DEBUG | SECTION_EXCLUDE)
3783 /* Labels we insert at beginning sections we can reference instead of
3784 the section names themselves. */
3786 #ifndef TEXT_SECTION_LABEL
3787 #define TEXT_SECTION_LABEL "Ltext"
3788 #endif
3789 #ifndef COLD_TEXT_SECTION_LABEL
3790 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
3791 #endif
3792 #ifndef DEBUG_LINE_SECTION_LABEL
3793 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
3794 #endif
3795 #ifndef DEBUG_SKELETON_LINE_SECTION_LABEL
3796 #define DEBUG_SKELETON_LINE_SECTION_LABEL "Lskeleton_debug_line"
3797 #endif
3798 #ifndef DEBUG_INFO_SECTION_LABEL
3799 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
3800 #endif
3801 #ifndef DEBUG_SKELETON_INFO_SECTION_LABEL
3802 #define DEBUG_SKELETON_INFO_SECTION_LABEL "Lskeleton_debug_info"
3803 #endif
3804 #ifndef DEBUG_ABBREV_SECTION_LABEL
3805 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
3806 #endif
3807 #ifndef DEBUG_SKELETON_ABBREV_SECTION_LABEL
3808 #define DEBUG_SKELETON_ABBREV_SECTION_LABEL "Lskeleton_debug_abbrev"
3809 #endif
3810 #ifndef DEBUG_ADDR_SECTION_LABEL
3811 #define DEBUG_ADDR_SECTION_LABEL "Ldebug_addr"
3812 #endif
3813 #ifndef DEBUG_LOC_SECTION_LABEL
3814 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
3815 #endif
3816 #ifndef DEBUG_RANGES_SECTION_LABEL
3817 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
3818 #endif
3819 #ifndef DEBUG_MACINFO_SECTION_LABEL
3820 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
3821 #endif
3822 #ifndef DEBUG_MACRO_SECTION_LABEL
3823 #define DEBUG_MACRO_SECTION_LABEL "Ldebug_macro"
3824 #endif
3825 #define SKELETON_COMP_DIE_ABBREV 1
3826 #define SKELETON_TYPE_DIE_ABBREV 2
3828 /* Definitions of defaults for formats and names of various special
3829 (artificial) labels which may be generated within this file (when the -g
3830 options is used and DWARF2_DEBUGGING_INFO is in effect.
3831 If necessary, these may be overridden from within the tm.h file, but
3832 typically, overriding these defaults is unnecessary. */
3834 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3835 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3836 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3837 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3838 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3839 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3840 static char debug_skeleton_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3841 static char debug_skeleton_abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3842 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3843 static char debug_addr_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3844 static char debug_skeleton_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3845 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3846 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3847 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3848 static char ranges_base_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3850 #ifndef TEXT_END_LABEL
3851 #define TEXT_END_LABEL "Letext"
3852 #endif
3853 #ifndef COLD_END_LABEL
3854 #define COLD_END_LABEL "Letext_cold"
3855 #endif
3856 #ifndef BLOCK_BEGIN_LABEL
3857 #define BLOCK_BEGIN_LABEL "LBB"
3858 #endif
3859 #ifndef BLOCK_END_LABEL
3860 #define BLOCK_END_LABEL "LBE"
3861 #endif
3862 #ifndef LINE_CODE_LABEL
3863 #define LINE_CODE_LABEL "LM"
3864 #endif
3867 /* Return the root of the DIE's built for the current compilation unit. */
3868 static dw_die_ref
3869 comp_unit_die (void)
3871 if (!single_comp_unit_die)
3872 single_comp_unit_die = gen_compile_unit_die (NULL);
3873 return single_comp_unit_die;
3876 /* We allow a language front-end to designate a function that is to be
3877 called to "demangle" any name before it is put into a DIE. */
3879 static const char *(*demangle_name_func) (const char *);
3881 void
3882 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
3884 demangle_name_func = func;
3887 /* Test if rtl node points to a pseudo register. */
3889 static inline int
3890 is_pseudo_reg (const_rtx rtl)
3892 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
3893 || (GET_CODE (rtl) == SUBREG
3894 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
3897 /* Return a reference to a type, with its const and volatile qualifiers
3898 removed. */
3900 static inline tree
3901 type_main_variant (tree type)
3903 type = TYPE_MAIN_VARIANT (type);
3905 /* ??? There really should be only one main variant among any group of
3906 variants of a given type (and all of the MAIN_VARIANT values for all
3907 members of the group should point to that one type) but sometimes the C
3908 front-end messes this up for array types, so we work around that bug
3909 here. */
3910 if (TREE_CODE (type) == ARRAY_TYPE)
3911 while (type != TYPE_MAIN_VARIANT (type))
3912 type = TYPE_MAIN_VARIANT (type);
3914 return type;
3917 /* Return nonzero if the given type node represents a tagged type. */
3919 static inline int
3920 is_tagged_type (const_tree type)
3922 enum tree_code code = TREE_CODE (type);
3924 return (code == RECORD_TYPE || code == UNION_TYPE
3925 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
3928 /* Set label to debug_info_section_label + die_offset of a DIE reference. */
3930 static void
3931 get_ref_die_offset_label (char *label, dw_die_ref ref)
3933 sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
3936 /* Return die_offset of a DIE reference to a base type. */
3938 static unsigned long int
3939 get_base_type_offset (dw_die_ref ref)
3941 if (ref->die_offset)
3942 return ref->die_offset;
3943 if (comp_unit_die ()->die_abbrev)
3945 calc_base_type_die_sizes ();
3946 gcc_assert (ref->die_offset);
3948 return ref->die_offset;
3951 /* Return die_offset of a DIE reference other than base type. */
3953 static unsigned long int
3954 get_ref_die_offset (dw_die_ref ref)
3956 gcc_assert (ref->die_offset);
3957 return ref->die_offset;
3960 /* Convert a DIE tag into its string name. */
3962 static const char *
3963 dwarf_tag_name (unsigned int tag)
3965 const char *name = get_DW_TAG_name (tag);
3967 if (name != NULL)
3968 return name;
3970 return "DW_TAG_<unknown>";
3973 /* Convert a DWARF attribute code into its string name. */
3975 static const char *
3976 dwarf_attr_name (unsigned int attr)
3978 const char *name;
3980 switch (attr)
3982 #if VMS_DEBUGGING_INFO
3983 case DW_AT_HP_prologue:
3984 return "DW_AT_HP_prologue";
3985 #else
3986 case DW_AT_MIPS_loop_unroll_factor:
3987 return "DW_AT_MIPS_loop_unroll_factor";
3988 #endif
3990 #if VMS_DEBUGGING_INFO
3991 case DW_AT_HP_epilogue:
3992 return "DW_AT_HP_epilogue";
3993 #else
3994 case DW_AT_MIPS_stride:
3995 return "DW_AT_MIPS_stride";
3996 #endif
3999 name = get_DW_AT_name (attr);
4001 if (name != NULL)
4002 return name;
4004 return "DW_AT_<unknown>";
4007 /* Convert a DWARF value form code into its string name. */
4009 static const char *
4010 dwarf_form_name (unsigned int form)
4012 const char *name = get_DW_FORM_name (form);
4014 if (name != NULL)
4015 return name;
4017 return "DW_FORM_<unknown>";
4020 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
4021 instance of an inlined instance of a decl which is local to an inline
4022 function, so we have to trace all of the way back through the origin chain
4023 to find out what sort of node actually served as the original seed for the
4024 given block. */
4026 static tree
4027 decl_ultimate_origin (const_tree decl)
4029 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
4030 return NULL_TREE;
4032 /* DECL_ABSTRACT_ORIGIN can point to itself; ignore that if
4033 we're trying to output the abstract instance of this function. */
4034 if (DECL_ABSTRACT_P (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
4035 return NULL_TREE;
4037 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
4038 most distant ancestor, this should never happen. */
4039 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
4041 return DECL_ABSTRACT_ORIGIN (decl);
4044 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
4045 of a virtual function may refer to a base class, so we check the 'this'
4046 parameter. */
4048 static tree
4049 decl_class_context (tree decl)
4051 tree context = NULL_TREE;
4053 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
4054 context = DECL_CONTEXT (decl);
4055 else
4056 context = TYPE_MAIN_VARIANT
4057 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
4059 if (context && !TYPE_P (context))
4060 context = NULL_TREE;
4062 return context;
4065 /* Add an attribute/value pair to a DIE. */
4067 static inline void
4068 add_dwarf_attr (dw_die_ref die, dw_attr_node *attr)
4070 /* Maybe this should be an assert? */
4071 if (die == NULL)
4072 return;
4074 vec_safe_reserve (die->die_attr, 1);
4075 vec_safe_push (die->die_attr, *attr);
4078 static inline enum dw_val_class
4079 AT_class (dw_attr_node *a)
4081 return a->dw_attr_val.val_class;
4084 /* Return the index for any attribute that will be referenced with a
4085 DW_FORM_GNU_addr_index or DW_FORM_GNU_str_index. String indices
4086 are stored in dw_attr_val.v.val_str for reference counting
4087 pruning. */
4089 static inline unsigned int
4090 AT_index (dw_attr_node *a)
4092 if (AT_class (a) == dw_val_class_str)
4093 return a->dw_attr_val.v.val_str->index;
4094 else if (a->dw_attr_val.val_entry != NULL)
4095 return a->dw_attr_val.val_entry->index;
4096 return NOT_INDEXED;
4099 /* Add a flag value attribute to a DIE. */
4101 static inline void
4102 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
4104 dw_attr_node attr;
4106 attr.dw_attr = attr_kind;
4107 attr.dw_attr_val.val_class = dw_val_class_flag;
4108 attr.dw_attr_val.val_entry = NULL;
4109 attr.dw_attr_val.v.val_flag = flag;
4110 add_dwarf_attr (die, &attr);
4113 static inline unsigned
4114 AT_flag (dw_attr_node *a)
4116 gcc_assert (a && AT_class (a) == dw_val_class_flag);
4117 return a->dw_attr_val.v.val_flag;
4120 /* Add a signed integer attribute value to a DIE. */
4122 static inline void
4123 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
4125 dw_attr_node attr;
4127 attr.dw_attr = attr_kind;
4128 attr.dw_attr_val.val_class = dw_val_class_const;
4129 attr.dw_attr_val.val_entry = NULL;
4130 attr.dw_attr_val.v.val_int = int_val;
4131 add_dwarf_attr (die, &attr);
4134 static inline HOST_WIDE_INT
4135 AT_int (dw_attr_node *a)
4137 gcc_assert (a && (AT_class (a) == dw_val_class_const
4138 || AT_class (a) == dw_val_class_const_implicit));
4139 return a->dw_attr_val.v.val_int;
4142 /* Add an unsigned integer attribute value to a DIE. */
4144 static inline void
4145 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
4146 unsigned HOST_WIDE_INT unsigned_val)
4148 dw_attr_node attr;
4150 attr.dw_attr = attr_kind;
4151 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
4152 attr.dw_attr_val.val_entry = NULL;
4153 attr.dw_attr_val.v.val_unsigned = unsigned_val;
4154 add_dwarf_attr (die, &attr);
4157 static inline unsigned HOST_WIDE_INT
4158 AT_unsigned (dw_attr_node *a)
4160 gcc_assert (a && (AT_class (a) == dw_val_class_unsigned_const
4161 || AT_class (a) == dw_val_class_unsigned_const_implicit));
4162 return a->dw_attr_val.v.val_unsigned;
4165 /* Add an unsigned wide integer attribute value to a DIE. */
4167 static inline void
4168 add_AT_wide (dw_die_ref die, enum dwarf_attribute attr_kind,
4169 const wide_int& w)
4171 dw_attr_node attr;
4173 attr.dw_attr = attr_kind;
4174 attr.dw_attr_val.val_class = dw_val_class_wide_int;
4175 attr.dw_attr_val.val_entry = NULL;
4176 attr.dw_attr_val.v.val_wide = ggc_alloc<wide_int> ();
4177 *attr.dw_attr_val.v.val_wide = w;
4178 add_dwarf_attr (die, &attr);
4181 /* Add an unsigned double integer attribute value to a DIE. */
4183 static inline void
4184 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
4185 HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
4187 dw_attr_node attr;
4189 attr.dw_attr = attr_kind;
4190 attr.dw_attr_val.val_class = dw_val_class_const_double;
4191 attr.dw_attr_val.val_entry = NULL;
4192 attr.dw_attr_val.v.val_double.high = high;
4193 attr.dw_attr_val.v.val_double.low = low;
4194 add_dwarf_attr (die, &attr);
4197 /* Add a floating point attribute value to a DIE and return it. */
4199 static inline void
4200 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
4201 unsigned int length, unsigned int elt_size, unsigned char *array)
4203 dw_attr_node attr;
4205 attr.dw_attr = attr_kind;
4206 attr.dw_attr_val.val_class = dw_val_class_vec;
4207 attr.dw_attr_val.val_entry = NULL;
4208 attr.dw_attr_val.v.val_vec.length = length;
4209 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
4210 attr.dw_attr_val.v.val_vec.array = array;
4211 add_dwarf_attr (die, &attr);
4214 /* Add an 8-byte data attribute value to a DIE. */
4216 static inline void
4217 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
4218 unsigned char data8[8])
4220 dw_attr_node attr;
4222 attr.dw_attr = attr_kind;
4223 attr.dw_attr_val.val_class = dw_val_class_data8;
4224 attr.dw_attr_val.val_entry = NULL;
4225 memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
4226 add_dwarf_attr (die, &attr);
4229 /* Add DW_AT_low_pc and DW_AT_high_pc to a DIE. When using
4230 dwarf_split_debug_info, address attributes in dies destined for the
4231 final executable have force_direct set to avoid using indexed
4232 references. */
4234 static inline void
4235 add_AT_low_high_pc (dw_die_ref die, const char *lbl_low, const char *lbl_high,
4236 bool force_direct)
4238 dw_attr_node attr;
4239 char * lbl_id;
4241 lbl_id = xstrdup (lbl_low);
4242 attr.dw_attr = DW_AT_low_pc;
4243 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4244 attr.dw_attr_val.v.val_lbl_id = lbl_id;
4245 if (dwarf_split_debug_info && !force_direct)
4246 attr.dw_attr_val.val_entry
4247 = add_addr_table_entry (lbl_id, ate_kind_label);
4248 else
4249 attr.dw_attr_val.val_entry = NULL;
4250 add_dwarf_attr (die, &attr);
4252 attr.dw_attr = DW_AT_high_pc;
4253 if (dwarf_version < 4)
4254 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4255 else
4256 attr.dw_attr_val.val_class = dw_val_class_high_pc;
4257 lbl_id = xstrdup (lbl_high);
4258 attr.dw_attr_val.v.val_lbl_id = lbl_id;
4259 if (attr.dw_attr_val.val_class == dw_val_class_lbl_id
4260 && dwarf_split_debug_info && !force_direct)
4261 attr.dw_attr_val.val_entry
4262 = add_addr_table_entry (lbl_id, ate_kind_label);
4263 else
4264 attr.dw_attr_val.val_entry = NULL;
4265 add_dwarf_attr (die, &attr);
4268 /* Hash and equality functions for debug_str_hash. */
4270 hashval_t
4271 indirect_string_hasher::hash (indirect_string_node *x)
4273 return htab_hash_string (x->str);
4276 bool
4277 indirect_string_hasher::equal (indirect_string_node *x1, const char *x2)
4279 return strcmp (x1->str, x2) == 0;
4282 /* Add STR to the given string hash table. */
4284 static struct indirect_string_node *
4285 find_AT_string_in_table (const char *str,
4286 hash_table<indirect_string_hasher> *table)
4288 struct indirect_string_node *node;
4290 indirect_string_node **slot
4291 = table->find_slot_with_hash (str, htab_hash_string (str), INSERT);
4292 if (*slot == NULL)
4294 node = ggc_cleared_alloc<indirect_string_node> ();
4295 node->str = ggc_strdup (str);
4296 *slot = node;
4298 else
4299 node = *slot;
4301 node->refcount++;
4302 return node;
4305 /* Add STR to the indirect string hash table. */
4307 static struct indirect_string_node *
4308 find_AT_string (const char *str)
4310 if (! debug_str_hash)
4311 debug_str_hash = hash_table<indirect_string_hasher>::create_ggc (10);
4313 return find_AT_string_in_table (str, debug_str_hash);
4316 /* Add a string attribute value to a DIE. */
4318 static inline void
4319 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
4321 dw_attr_node attr;
4322 struct indirect_string_node *node;
4324 node = find_AT_string (str);
4326 attr.dw_attr = attr_kind;
4327 attr.dw_attr_val.val_class = dw_val_class_str;
4328 attr.dw_attr_val.val_entry = NULL;
4329 attr.dw_attr_val.v.val_str = node;
4330 add_dwarf_attr (die, &attr);
4333 static inline const char *
4334 AT_string (dw_attr_node *a)
4336 gcc_assert (a && AT_class (a) == dw_val_class_str);
4337 return a->dw_attr_val.v.val_str->str;
4340 /* Call this function directly to bypass AT_string_form's logic to put
4341 the string inline in the die. */
4343 static void
4344 set_indirect_string (struct indirect_string_node *node)
4346 char label[MAX_ARTIFICIAL_LABEL_BYTES];
4347 /* Already indirect is a no op. */
4348 if (node->form == DW_FORM_strp
4349 || node->form == DW_FORM_line_strp
4350 || node->form == DW_FORM_GNU_str_index)
4352 gcc_assert (node->label);
4353 return;
4355 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
4356 ++dw2_string_counter;
4357 node->label = xstrdup (label);
4359 if (!dwarf_split_debug_info)
4361 node->form = DW_FORM_strp;
4362 node->index = NOT_INDEXED;
4364 else
4366 node->form = DW_FORM_GNU_str_index;
4367 node->index = NO_INDEX_ASSIGNED;
4371 /* Find out whether a string should be output inline in DIE
4372 or out-of-line in .debug_str section. */
4374 static enum dwarf_form
4375 find_string_form (struct indirect_string_node *node)
4377 unsigned int len;
4379 if (node->form)
4380 return node->form;
4382 len = strlen (node->str) + 1;
4384 /* If the string is shorter or equal to the size of the reference, it is
4385 always better to put it inline. */
4386 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
4387 return node->form = DW_FORM_string;
4389 /* If we cannot expect the linker to merge strings in .debug_str
4390 section, only put it into .debug_str if it is worth even in this
4391 single module. */
4392 if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
4393 || ((debug_str_section->common.flags & SECTION_MERGE) == 0
4394 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
4395 return node->form = DW_FORM_string;
4397 set_indirect_string (node);
4399 return node->form;
4402 /* Find out whether the string referenced from the attribute should be
4403 output inline in DIE or out-of-line in .debug_str section. */
4405 static enum dwarf_form
4406 AT_string_form (dw_attr_node *a)
4408 gcc_assert (a && AT_class (a) == dw_val_class_str);
4409 return find_string_form (a->dw_attr_val.v.val_str);
4412 /* Add a DIE reference attribute value to a DIE. */
4414 static inline void
4415 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
4417 dw_attr_node attr;
4418 gcc_checking_assert (targ_die != NULL);
4420 /* With LTO we can end up trying to reference something we didn't create
4421 a DIE for. Avoid crashing later on a NULL referenced DIE. */
4422 if (targ_die == NULL)
4423 return;
4425 attr.dw_attr = attr_kind;
4426 attr.dw_attr_val.val_class = dw_val_class_die_ref;
4427 attr.dw_attr_val.val_entry = NULL;
4428 attr.dw_attr_val.v.val_die_ref.die = targ_die;
4429 attr.dw_attr_val.v.val_die_ref.external = 0;
4430 add_dwarf_attr (die, &attr);
4433 /* Change DIE reference REF to point to NEW_DIE instead. */
4435 static inline void
4436 change_AT_die_ref (dw_attr_node *ref, dw_die_ref new_die)
4438 gcc_assert (ref->dw_attr_val.val_class == dw_val_class_die_ref);
4439 ref->dw_attr_val.v.val_die_ref.die = new_die;
4440 ref->dw_attr_val.v.val_die_ref.external = 0;
4443 /* Add an AT_specification attribute to a DIE, and also make the back
4444 pointer from the specification to the definition. */
4446 static inline void
4447 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4449 add_AT_die_ref (die, DW_AT_specification, targ_die);
4450 gcc_assert (!targ_die->die_definition);
4451 targ_die->die_definition = die;
4454 static inline dw_die_ref
4455 AT_ref (dw_attr_node *a)
4457 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4458 return a->dw_attr_val.v.val_die_ref.die;
4461 static inline int
4462 AT_ref_external (dw_attr_node *a)
4464 if (a && AT_class (a) == dw_val_class_die_ref)
4465 return a->dw_attr_val.v.val_die_ref.external;
4467 return 0;
4470 static inline void
4471 set_AT_ref_external (dw_attr_node *a, int i)
4473 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4474 a->dw_attr_val.v.val_die_ref.external = i;
4477 /* Add an FDE reference attribute value to a DIE. */
4479 static inline void
4480 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
4482 dw_attr_node attr;
4484 attr.dw_attr = attr_kind;
4485 attr.dw_attr_val.val_class = dw_val_class_fde_ref;
4486 attr.dw_attr_val.val_entry = NULL;
4487 attr.dw_attr_val.v.val_fde_index = targ_fde;
4488 add_dwarf_attr (die, &attr);
4491 /* Add a location description attribute value to a DIE. */
4493 static inline void
4494 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4496 dw_attr_node attr;
4498 attr.dw_attr = attr_kind;
4499 attr.dw_attr_val.val_class = dw_val_class_loc;
4500 attr.dw_attr_val.val_entry = NULL;
4501 attr.dw_attr_val.v.val_loc = loc;
4502 add_dwarf_attr (die, &attr);
4505 static inline dw_loc_descr_ref
4506 AT_loc (dw_attr_node *a)
4508 gcc_assert (a && AT_class (a) == dw_val_class_loc);
4509 return a->dw_attr_val.v.val_loc;
4512 static inline void
4513 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4515 dw_attr_node attr;
4517 if (XCOFF_DEBUGGING_INFO && !HAVE_XCOFF_DWARF_EXTRAS)
4518 return;
4520 attr.dw_attr = attr_kind;
4521 attr.dw_attr_val.val_class = dw_val_class_loc_list;
4522 attr.dw_attr_val.val_entry = NULL;
4523 attr.dw_attr_val.v.val_loc_list = loc_list;
4524 add_dwarf_attr (die, &attr);
4525 have_location_lists = true;
4528 static inline dw_loc_list_ref
4529 AT_loc_list (dw_attr_node *a)
4531 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4532 return a->dw_attr_val.v.val_loc_list;
4535 static inline dw_loc_list_ref *
4536 AT_loc_list_ptr (dw_attr_node *a)
4538 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4539 return &a->dw_attr_val.v.val_loc_list;
4542 struct addr_hasher : ggc_ptr_hash<addr_table_entry>
4544 static hashval_t hash (addr_table_entry *);
4545 static bool equal (addr_table_entry *, addr_table_entry *);
4548 /* Table of entries into the .debug_addr section. */
4550 static GTY (()) hash_table<addr_hasher> *addr_index_table;
4552 /* Hash an address_table_entry. */
4554 hashval_t
4555 addr_hasher::hash (addr_table_entry *a)
4557 inchash::hash hstate;
4558 switch (a->kind)
4560 case ate_kind_rtx:
4561 hstate.add_int (0);
4562 break;
4563 case ate_kind_rtx_dtprel:
4564 hstate.add_int (1);
4565 break;
4566 case ate_kind_label:
4567 return htab_hash_string (a->addr.label);
4568 default:
4569 gcc_unreachable ();
4571 inchash::add_rtx (a->addr.rtl, hstate);
4572 return hstate.end ();
4575 /* Determine equality for two address_table_entries. */
4577 bool
4578 addr_hasher::equal (addr_table_entry *a1, addr_table_entry *a2)
4580 if (a1->kind != a2->kind)
4581 return 0;
4582 switch (a1->kind)
4584 case ate_kind_rtx:
4585 case ate_kind_rtx_dtprel:
4586 return rtx_equal_p (a1->addr.rtl, a2->addr.rtl);
4587 case ate_kind_label:
4588 return strcmp (a1->addr.label, a2->addr.label) == 0;
4589 default:
4590 gcc_unreachable ();
4594 /* Initialize an addr_table_entry. */
4596 void
4597 init_addr_table_entry (addr_table_entry *e, enum ate_kind kind, void *addr)
4599 e->kind = kind;
4600 switch (kind)
4602 case ate_kind_rtx:
4603 case ate_kind_rtx_dtprel:
4604 e->addr.rtl = (rtx) addr;
4605 break;
4606 case ate_kind_label:
4607 e->addr.label = (char *) addr;
4608 break;
4610 e->refcount = 0;
4611 e->index = NO_INDEX_ASSIGNED;
4614 /* Add attr to the address table entry to the table. Defer setting an
4615 index until output time. */
4617 static addr_table_entry *
4618 add_addr_table_entry (void *addr, enum ate_kind kind)
4620 addr_table_entry *node;
4621 addr_table_entry finder;
4623 gcc_assert (dwarf_split_debug_info);
4624 if (! addr_index_table)
4625 addr_index_table = hash_table<addr_hasher>::create_ggc (10);
4626 init_addr_table_entry (&finder, kind, addr);
4627 addr_table_entry **slot = addr_index_table->find_slot (&finder, INSERT);
4629 if (*slot == HTAB_EMPTY_ENTRY)
4631 node = ggc_cleared_alloc<addr_table_entry> ();
4632 init_addr_table_entry (node, kind, addr);
4633 *slot = node;
4635 else
4636 node = *slot;
4638 node->refcount++;
4639 return node;
4642 /* Remove an entry from the addr table by decrementing its refcount.
4643 Strictly, decrementing the refcount would be enough, but the
4644 assertion that the entry is actually in the table has found
4645 bugs. */
4647 static void
4648 remove_addr_table_entry (addr_table_entry *entry)
4650 gcc_assert (dwarf_split_debug_info && addr_index_table);
4651 /* After an index is assigned, the table is frozen. */
4652 gcc_assert (entry->refcount > 0 && entry->index == NO_INDEX_ASSIGNED);
4653 entry->refcount--;
4656 /* Given a location list, remove all addresses it refers to from the
4657 address_table. */
4659 static void
4660 remove_loc_list_addr_table_entries (dw_loc_descr_ref descr)
4662 for (; descr; descr = descr->dw_loc_next)
4663 if (descr->dw_loc_oprnd1.val_entry != NULL)
4665 gcc_assert (descr->dw_loc_oprnd1.val_entry->index == NO_INDEX_ASSIGNED);
4666 remove_addr_table_entry (descr->dw_loc_oprnd1.val_entry);
4670 /* A helper function for dwarf2out_finish called through
4671 htab_traverse. Assign an addr_table_entry its index. All entries
4672 must be collected into the table when this function is called,
4673 because the indexing code relies on htab_traverse to traverse nodes
4674 in the same order for each run. */
4677 index_addr_table_entry (addr_table_entry **h, unsigned int *index)
4679 addr_table_entry *node = *h;
4681 /* Don't index unreferenced nodes. */
4682 if (node->refcount == 0)
4683 return 1;
4685 gcc_assert (node->index == NO_INDEX_ASSIGNED);
4686 node->index = *index;
4687 *index += 1;
4689 return 1;
4692 /* Add an address constant attribute value to a DIE. When using
4693 dwarf_split_debug_info, address attributes in dies destined for the
4694 final executable should be direct references--setting the parameter
4695 force_direct ensures this behavior. */
4697 static inline void
4698 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr,
4699 bool force_direct)
4701 dw_attr_node attr;
4703 attr.dw_attr = attr_kind;
4704 attr.dw_attr_val.val_class = dw_val_class_addr;
4705 attr.dw_attr_val.v.val_addr = addr;
4706 if (dwarf_split_debug_info && !force_direct)
4707 attr.dw_attr_val.val_entry = add_addr_table_entry (addr, ate_kind_rtx);
4708 else
4709 attr.dw_attr_val.val_entry = NULL;
4710 add_dwarf_attr (die, &attr);
4713 /* Get the RTX from to an address DIE attribute. */
4715 static inline rtx
4716 AT_addr (dw_attr_node *a)
4718 gcc_assert (a && AT_class (a) == dw_val_class_addr);
4719 return a->dw_attr_val.v.val_addr;
4722 /* Add a file attribute value to a DIE. */
4724 static inline void
4725 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
4726 struct dwarf_file_data *fd)
4728 dw_attr_node attr;
4730 attr.dw_attr = attr_kind;
4731 attr.dw_attr_val.val_class = dw_val_class_file;
4732 attr.dw_attr_val.val_entry = NULL;
4733 attr.dw_attr_val.v.val_file = fd;
4734 add_dwarf_attr (die, &attr);
4737 /* Get the dwarf_file_data from a file DIE attribute. */
4739 static inline struct dwarf_file_data *
4740 AT_file (dw_attr_node *a)
4742 gcc_assert (a && (AT_class (a) == dw_val_class_file
4743 || AT_class (a) == dw_val_class_file_implicit));
4744 return a->dw_attr_val.v.val_file;
4747 /* Add a vms delta attribute value to a DIE. */
4749 static inline void
4750 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
4751 const char *lbl1, const char *lbl2)
4753 dw_attr_node attr;
4755 attr.dw_attr = attr_kind;
4756 attr.dw_attr_val.val_class = dw_val_class_vms_delta;
4757 attr.dw_attr_val.val_entry = NULL;
4758 attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
4759 attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
4760 add_dwarf_attr (die, &attr);
4763 /* Add a label identifier attribute value to a DIE. */
4765 static inline void
4766 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind,
4767 const char *lbl_id)
4769 dw_attr_node attr;
4771 attr.dw_attr = attr_kind;
4772 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4773 attr.dw_attr_val.val_entry = NULL;
4774 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
4775 if (dwarf_split_debug_info)
4776 attr.dw_attr_val.val_entry
4777 = add_addr_table_entry (attr.dw_attr_val.v.val_lbl_id,
4778 ate_kind_label);
4779 add_dwarf_attr (die, &attr);
4782 /* Add a section offset attribute value to a DIE, an offset into the
4783 debug_line section. */
4785 static inline void
4786 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4787 const char *label)
4789 dw_attr_node attr;
4791 attr.dw_attr = attr_kind;
4792 attr.dw_attr_val.val_class = dw_val_class_lineptr;
4793 attr.dw_attr_val.val_entry = NULL;
4794 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4795 add_dwarf_attr (die, &attr);
4798 /* Add a section offset attribute value to a DIE, an offset into the
4799 debug_loclists section. */
4801 static inline void
4802 add_AT_loclistsptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4803 const char *label)
4805 dw_attr_node attr;
4807 attr.dw_attr = attr_kind;
4808 attr.dw_attr_val.val_class = dw_val_class_loclistsptr;
4809 attr.dw_attr_val.val_entry = NULL;
4810 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4811 add_dwarf_attr (die, &attr);
4814 /* Add a section offset attribute value to a DIE, an offset into the
4815 debug_macinfo section. */
4817 static inline void
4818 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4819 const char *label)
4821 dw_attr_node attr;
4823 attr.dw_attr = attr_kind;
4824 attr.dw_attr_val.val_class = dw_val_class_macptr;
4825 attr.dw_attr_val.val_entry = NULL;
4826 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4827 add_dwarf_attr (die, &attr);
4830 /* Add an offset attribute value to a DIE. */
4832 static inline void
4833 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
4834 unsigned HOST_WIDE_INT offset)
4836 dw_attr_node attr;
4838 attr.dw_attr = attr_kind;
4839 attr.dw_attr_val.val_class = dw_val_class_offset;
4840 attr.dw_attr_val.val_entry = NULL;
4841 attr.dw_attr_val.v.val_offset = offset;
4842 add_dwarf_attr (die, &attr);
4845 /* Add a range_list attribute value to a DIE. When using
4846 dwarf_split_debug_info, address attributes in dies destined for the
4847 final executable should be direct references--setting the parameter
4848 force_direct ensures this behavior. */
4850 #define UNRELOCATED_OFFSET ((addr_table_entry *) 1)
4851 #define RELOCATED_OFFSET (NULL)
4853 static void
4854 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
4855 long unsigned int offset, bool force_direct)
4857 dw_attr_node attr;
4859 attr.dw_attr = attr_kind;
4860 attr.dw_attr_val.val_class = dw_val_class_range_list;
4861 /* For the range_list attribute, use val_entry to store whether the
4862 offset should follow split-debug-info or normal semantics. This
4863 value is read in output_range_list_offset. */
4864 if (dwarf_split_debug_info && !force_direct)
4865 attr.dw_attr_val.val_entry = UNRELOCATED_OFFSET;
4866 else
4867 attr.dw_attr_val.val_entry = RELOCATED_OFFSET;
4868 attr.dw_attr_val.v.val_offset = offset;
4869 add_dwarf_attr (die, &attr);
4872 /* Return the start label of a delta attribute. */
4874 static inline const char *
4875 AT_vms_delta1 (dw_attr_node *a)
4877 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4878 return a->dw_attr_val.v.val_vms_delta.lbl1;
4881 /* Return the end label of a delta attribute. */
4883 static inline const char *
4884 AT_vms_delta2 (dw_attr_node *a)
4886 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4887 return a->dw_attr_val.v.val_vms_delta.lbl2;
4890 static inline const char *
4891 AT_lbl (dw_attr_node *a)
4893 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
4894 || AT_class (a) == dw_val_class_lineptr
4895 || AT_class (a) == dw_val_class_macptr
4896 || AT_class (a) == dw_val_class_loclistsptr
4897 || AT_class (a) == dw_val_class_high_pc));
4898 return a->dw_attr_val.v.val_lbl_id;
4901 /* Get the attribute of type attr_kind. */
4903 static dw_attr_node *
4904 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4906 dw_attr_node *a;
4907 unsigned ix;
4908 dw_die_ref spec = NULL;
4910 if (! die)
4911 return NULL;
4913 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4914 if (a->dw_attr == attr_kind)
4915 return a;
4916 else if (a->dw_attr == DW_AT_specification
4917 || a->dw_attr == DW_AT_abstract_origin)
4918 spec = AT_ref (a);
4920 if (spec)
4921 return get_AT (spec, attr_kind);
4923 return NULL;
4926 /* Returns the parent of the declaration of DIE. */
4928 static dw_die_ref
4929 get_die_parent (dw_die_ref die)
4931 dw_die_ref t;
4933 if (!die)
4934 return NULL;
4936 if ((t = get_AT_ref (die, DW_AT_abstract_origin))
4937 || (t = get_AT_ref (die, DW_AT_specification)))
4938 die = t;
4940 return die->die_parent;
4943 /* Return the "low pc" attribute value, typically associated with a subprogram
4944 DIE. Return null if the "low pc" attribute is either not present, or if it
4945 cannot be represented as an assembler label identifier. */
4947 static inline const char *
4948 get_AT_low_pc (dw_die_ref die)
4950 dw_attr_node *a = get_AT (die, DW_AT_low_pc);
4952 return a ? AT_lbl (a) : NULL;
4955 /* Return the "high pc" attribute value, typically associated with a subprogram
4956 DIE. Return null if the "high pc" attribute is either not present, or if it
4957 cannot be represented as an assembler label identifier. */
4959 static inline const char *
4960 get_AT_hi_pc (dw_die_ref die)
4962 dw_attr_node *a = get_AT (die, DW_AT_high_pc);
4964 return a ? AT_lbl (a) : NULL;
4967 /* Return the value of the string attribute designated by ATTR_KIND, or
4968 NULL if it is not present. */
4970 static inline const char *
4971 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
4973 dw_attr_node *a = get_AT (die, attr_kind);
4975 return a ? AT_string (a) : NULL;
4978 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
4979 if it is not present. */
4981 static inline int
4982 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
4984 dw_attr_node *a = get_AT (die, attr_kind);
4986 return a ? AT_flag (a) : 0;
4989 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
4990 if it is not present. */
4992 static inline unsigned
4993 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
4995 dw_attr_node *a = get_AT (die, attr_kind);
4997 return a ? AT_unsigned (a) : 0;
5000 static inline dw_die_ref
5001 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
5003 dw_attr_node *a = get_AT (die, attr_kind);
5005 return a ? AT_ref (a) : NULL;
5008 static inline struct dwarf_file_data *
5009 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
5011 dw_attr_node *a = get_AT (die, attr_kind);
5013 return a ? AT_file (a) : NULL;
5016 /* Return TRUE if the language is C++. */
5018 static inline bool
5019 is_cxx (void)
5021 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5023 return (lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus
5024 || lang == DW_LANG_C_plus_plus_11 || lang == DW_LANG_C_plus_plus_14);
5027 /* Return TRUE if DECL was created by the C++ frontend. */
5029 static bool
5030 is_cxx (const_tree decl)
5032 if (in_lto_p)
5034 const_tree context = decl;
5035 while (context && TREE_CODE (context) != TRANSLATION_UNIT_DECL)
5037 if (TREE_CODE (context) == BLOCK)
5038 context = BLOCK_SUPERCONTEXT (context);
5039 else
5040 context = get_containing_scope (context);
5042 if (context && TRANSLATION_UNIT_LANGUAGE (context))
5043 return strncmp (TRANSLATION_UNIT_LANGUAGE (context), "GNU C++", 7) == 0;
5045 return is_cxx ();
5048 /* Return TRUE if the language is Java. */
5050 static inline bool
5051 is_java (void)
5053 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5055 return lang == DW_LANG_Java;
5058 /* Return TRUE if the language is Fortran. */
5060 static inline bool
5061 is_fortran (void)
5063 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5065 return (lang == DW_LANG_Fortran77
5066 || lang == DW_LANG_Fortran90
5067 || lang == DW_LANG_Fortran95
5068 || lang == DW_LANG_Fortran03
5069 || lang == DW_LANG_Fortran08);
5072 /* Return TRUE if the language is Ada. */
5074 static inline bool
5075 is_ada (void)
5077 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5079 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
5082 /* Remove the specified attribute if present. Return TRUE if removal
5083 was successful. */
5085 static bool
5086 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5088 dw_attr_node *a;
5089 unsigned ix;
5091 if (! die)
5092 return false;
5094 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5095 if (a->dw_attr == attr_kind)
5097 if (AT_class (a) == dw_val_class_str)
5098 if (a->dw_attr_val.v.val_str->refcount)
5099 a->dw_attr_val.v.val_str->refcount--;
5101 /* vec::ordered_remove should help reduce the number of abbrevs
5102 that are needed. */
5103 die->die_attr->ordered_remove (ix);
5104 return true;
5106 return false;
5109 /* Remove CHILD from its parent. PREV must have the property that
5110 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
5112 static void
5113 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
5115 gcc_assert (child->die_parent == prev->die_parent);
5116 gcc_assert (prev->die_sib == child);
5117 if (prev == child)
5119 gcc_assert (child->die_parent->die_child == child);
5120 prev = NULL;
5122 else
5123 prev->die_sib = child->die_sib;
5124 if (child->die_parent->die_child == child)
5125 child->die_parent->die_child = prev;
5126 child->die_sib = NULL;
5129 /* Replace OLD_CHILD with NEW_CHILD. PREV must have the property that
5130 PREV->DIE_SIB == OLD_CHILD. Does not alter OLD_CHILD. */
5132 static void
5133 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
5135 dw_die_ref parent = old_child->die_parent;
5137 gcc_assert (parent == prev->die_parent);
5138 gcc_assert (prev->die_sib == old_child);
5140 new_child->die_parent = parent;
5141 if (prev == old_child)
5143 gcc_assert (parent->die_child == old_child);
5144 new_child->die_sib = new_child;
5146 else
5148 prev->die_sib = new_child;
5149 new_child->die_sib = old_child->die_sib;
5151 if (old_child->die_parent->die_child == old_child)
5152 old_child->die_parent->die_child = new_child;
5153 old_child->die_sib = NULL;
5156 /* Move all children from OLD_PARENT to NEW_PARENT. */
5158 static void
5159 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
5161 dw_die_ref c;
5162 new_parent->die_child = old_parent->die_child;
5163 old_parent->die_child = NULL;
5164 FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
5167 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
5168 matches TAG. */
5170 static void
5171 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
5173 dw_die_ref c;
5175 c = die->die_child;
5176 if (c) do {
5177 dw_die_ref prev = c;
5178 c = c->die_sib;
5179 while (c->die_tag == tag)
5181 remove_child_with_prev (c, prev);
5182 c->die_parent = NULL;
5183 /* Might have removed every child. */
5184 if (die->die_child == NULL)
5185 return;
5186 c = prev->die_sib;
5188 } while (c != die->die_child);
5191 /* Add a CHILD_DIE as the last child of DIE. */
5193 static void
5194 add_child_die (dw_die_ref die, dw_die_ref child_die)
5196 /* FIXME this should probably be an assert. */
5197 if (! die || ! child_die)
5198 return;
5199 gcc_assert (die != child_die);
5201 child_die->die_parent = die;
5202 if (die->die_child)
5204 child_die->die_sib = die->die_child->die_sib;
5205 die->die_child->die_sib = child_die;
5207 else
5208 child_die->die_sib = child_die;
5209 die->die_child = child_die;
5212 /* Like add_child_die, but put CHILD_DIE after AFTER_DIE. */
5214 static void
5215 add_child_die_after (dw_die_ref die, dw_die_ref child_die,
5216 dw_die_ref after_die)
5218 gcc_assert (die
5219 && child_die
5220 && after_die
5221 && die->die_child
5222 && die != child_die);
5224 child_die->die_parent = die;
5225 child_die->die_sib = after_die->die_sib;
5226 after_die->die_sib = child_die;
5227 if (die->die_child == after_die)
5228 die->die_child = child_die;
5231 /* Unassociate CHILD from its parent, and make its parent be
5232 NEW_PARENT. */
5234 static void
5235 reparent_child (dw_die_ref child, dw_die_ref new_parent)
5237 for (dw_die_ref p = child->die_parent->die_child; ; p = p->die_sib)
5238 if (p->die_sib == child)
5240 remove_child_with_prev (child, p);
5241 break;
5243 add_child_die (new_parent, child);
5246 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
5247 is the specification, to the end of PARENT's list of children.
5248 This is done by removing and re-adding it. */
5250 static void
5251 splice_child_die (dw_die_ref parent, dw_die_ref child)
5253 /* We want the declaration DIE from inside the class, not the
5254 specification DIE at toplevel. */
5255 if (child->die_parent != parent)
5257 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
5259 if (tmp)
5260 child = tmp;
5263 gcc_assert (child->die_parent == parent
5264 || (child->die_parent
5265 == get_AT_ref (parent, DW_AT_specification)));
5267 reparent_child (child, parent);
5270 /* Create and return a new die with a parent of PARENT_DIE. If
5271 PARENT_DIE is NULL, the new DIE is placed in limbo and an
5272 associated tree T must be supplied to determine parenthood
5273 later. */
5275 static inline dw_die_ref
5276 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
5278 dw_die_ref die = ggc_cleared_alloc<die_node> ();
5280 die->die_tag = tag_value;
5282 if (parent_die != NULL)
5283 add_child_die (parent_die, die);
5284 else
5286 limbo_die_node *limbo_node;
5288 /* No DIEs created after early dwarf should end up in limbo,
5289 because the limbo list should not persist past LTO
5290 streaming. */
5291 if (tag_value != DW_TAG_compile_unit
5292 /* These are allowed because they're generated while
5293 breaking out COMDAT units late. */
5294 && tag_value != DW_TAG_type_unit
5295 && tag_value != DW_TAG_skeleton_unit
5296 && !early_dwarf
5297 /* Allow nested functions to live in limbo because they will
5298 only temporarily live there, as decls_for_scope will fix
5299 them up. */
5300 && (TREE_CODE (t) != FUNCTION_DECL
5301 || !decl_function_context (t))
5302 /* Same as nested functions above but for types. Types that
5303 are local to a function will be fixed in
5304 decls_for_scope. */
5305 && (!RECORD_OR_UNION_TYPE_P (t)
5306 || !TYPE_CONTEXT (t)
5307 || TREE_CODE (TYPE_CONTEXT (t)) != FUNCTION_DECL)
5308 /* FIXME debug-early: Allow late limbo DIE creation for LTO,
5309 especially in the ltrans stage, but once we implement LTO
5310 dwarf streaming, we should remove this exception. */
5311 && !in_lto_p)
5313 fprintf (stderr, "symbol ended up in limbo too late:");
5314 debug_generic_stmt (t);
5315 gcc_unreachable ();
5318 limbo_node = ggc_cleared_alloc<limbo_die_node> ();
5319 limbo_node->die = die;
5320 limbo_node->created_for = t;
5321 limbo_node->next = limbo_die_list;
5322 limbo_die_list = limbo_node;
5325 return die;
5328 /* Return the DIE associated with the given type specifier. */
5330 static inline dw_die_ref
5331 lookup_type_die (tree type)
5333 dw_die_ref die = TYPE_SYMTAB_DIE (type);
5334 if (die && die->removed)
5336 TYPE_SYMTAB_DIE (type) = NULL;
5337 return NULL;
5339 return die;
5342 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
5343 anonymous type named by the typedef TYPE_DIE, return the DIE of the
5344 anonymous type instead the one of the naming typedef. */
5346 static inline dw_die_ref
5347 strip_naming_typedef (tree type, dw_die_ref type_die)
5349 if (type
5350 && TREE_CODE (type) == RECORD_TYPE
5351 && type_die
5352 && type_die->die_tag == DW_TAG_typedef
5353 && is_naming_typedef_decl (TYPE_NAME (type)))
5354 type_die = get_AT_ref (type_die, DW_AT_type);
5355 return type_die;
5358 /* Like lookup_type_die, but if type is an anonymous type named by a
5359 typedef[1], return the DIE of the anonymous type instead the one of
5360 the naming typedef. This is because in gen_typedef_die, we did
5361 equate the anonymous struct named by the typedef with the DIE of
5362 the naming typedef. So by default, lookup_type_die on an anonymous
5363 struct yields the DIE of the naming typedef.
5365 [1]: Read the comment of is_naming_typedef_decl to learn about what
5366 a naming typedef is. */
5368 static inline dw_die_ref
5369 lookup_type_die_strip_naming_typedef (tree type)
5371 dw_die_ref die = lookup_type_die (type);
5372 return strip_naming_typedef (type, die);
5375 /* Equate a DIE to a given type specifier. */
5377 static inline void
5378 equate_type_number_to_die (tree type, dw_die_ref type_die)
5380 TYPE_SYMTAB_DIE (type) = type_die;
5383 /* Returns a hash value for X (which really is a die_struct). */
5385 inline hashval_t
5386 decl_die_hasher::hash (die_node *x)
5388 return (hashval_t) x->decl_id;
5391 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
5393 inline bool
5394 decl_die_hasher::equal (die_node *x, tree y)
5396 return (x->decl_id == DECL_UID (y));
5399 /* Return the DIE associated with a given declaration. */
5401 static inline dw_die_ref
5402 lookup_decl_die (tree decl)
5404 dw_die_ref *die = decl_die_table->find_slot_with_hash (decl, DECL_UID (decl),
5405 NO_INSERT);
5406 if (!die)
5407 return NULL;
5408 if ((*die)->removed)
5410 decl_die_table->clear_slot (die);
5411 return NULL;
5413 return *die;
5416 /* Returns a hash value for X (which really is a var_loc_list). */
5418 inline hashval_t
5419 decl_loc_hasher::hash (var_loc_list *x)
5421 return (hashval_t) x->decl_id;
5424 /* Return nonzero if decl_id of var_loc_list X is the same as
5425 UID of decl *Y. */
5427 inline bool
5428 decl_loc_hasher::equal (var_loc_list *x, const_tree y)
5430 return (x->decl_id == DECL_UID (y));
5433 /* Return the var_loc list associated with a given declaration. */
5435 static inline var_loc_list *
5436 lookup_decl_loc (const_tree decl)
5438 if (!decl_loc_table)
5439 return NULL;
5440 return decl_loc_table->find_with_hash (decl, DECL_UID (decl));
5443 /* Returns a hash value for X (which really is a cached_dw_loc_list_list). */
5445 inline hashval_t
5446 dw_loc_list_hasher::hash (cached_dw_loc_list *x)
5448 return (hashval_t) x->decl_id;
5451 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
5452 UID of decl *Y. */
5454 inline bool
5455 dw_loc_list_hasher::equal (cached_dw_loc_list *x, const_tree y)
5457 return (x->decl_id == DECL_UID (y));
5460 /* Equate a DIE to a particular declaration. */
5462 static void
5463 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
5465 unsigned int decl_id = DECL_UID (decl);
5467 *decl_die_table->find_slot_with_hash (decl, decl_id, INSERT) = decl_die;
5468 decl_die->decl_id = decl_id;
5471 /* Return how many bits covers PIECE EXPR_LIST. */
5473 static HOST_WIDE_INT
5474 decl_piece_bitsize (rtx piece)
5476 int ret = (int) GET_MODE (piece);
5477 if (ret)
5478 return ret;
5479 gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
5480 && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
5481 return INTVAL (XEXP (XEXP (piece, 0), 0));
5484 /* Return pointer to the location of location note in PIECE EXPR_LIST. */
5486 static rtx *
5487 decl_piece_varloc_ptr (rtx piece)
5489 if ((int) GET_MODE (piece))
5490 return &XEXP (piece, 0);
5491 else
5492 return &XEXP (XEXP (piece, 0), 1);
5495 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
5496 Next is the chain of following piece nodes. */
5498 static rtx_expr_list *
5499 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
5501 if (bitsize > 0 && bitsize <= (int) MAX_MACHINE_MODE)
5502 return alloc_EXPR_LIST (bitsize, loc_note, next);
5503 else
5504 return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
5505 GEN_INT (bitsize),
5506 loc_note), next);
5509 /* Return rtx that should be stored into loc field for
5510 LOC_NOTE and BITPOS/BITSIZE. */
5512 static rtx
5513 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
5514 HOST_WIDE_INT bitsize)
5516 if (bitsize != -1)
5518 loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
5519 if (bitpos != 0)
5520 loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
5522 return loc_note;
5525 /* This function either modifies location piece list *DEST in
5526 place (if SRC and INNER is NULL), or copies location piece list
5527 *SRC to *DEST while modifying it. Location BITPOS is modified
5528 to contain LOC_NOTE, any pieces overlapping it are removed resp.
5529 not copied and if needed some padding around it is added.
5530 When modifying in place, DEST should point to EXPR_LIST where
5531 earlier pieces cover PIECE_BITPOS bits, when copying SRC points
5532 to the start of the whole list and INNER points to the EXPR_LIST
5533 where earlier pieces cover PIECE_BITPOS bits. */
5535 static void
5536 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
5537 HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
5538 HOST_WIDE_INT bitsize, rtx loc_note)
5540 HOST_WIDE_INT diff;
5541 bool copy = inner != NULL;
5543 if (copy)
5545 /* First copy all nodes preceding the current bitpos. */
5546 while (src != inner)
5548 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5549 decl_piece_bitsize (*src), NULL_RTX);
5550 dest = &XEXP (*dest, 1);
5551 src = &XEXP (*src, 1);
5554 /* Add padding if needed. */
5555 if (bitpos != piece_bitpos)
5557 *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
5558 copy ? NULL_RTX : *dest);
5559 dest = &XEXP (*dest, 1);
5561 else if (*dest && decl_piece_bitsize (*dest) == bitsize)
5563 gcc_assert (!copy);
5564 /* A piece with correct bitpos and bitsize already exist,
5565 just update the location for it and return. */
5566 *decl_piece_varloc_ptr (*dest) = loc_note;
5567 return;
5569 /* Add the piece that changed. */
5570 *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
5571 dest = &XEXP (*dest, 1);
5572 /* Skip over pieces that overlap it. */
5573 diff = bitpos - piece_bitpos + bitsize;
5574 if (!copy)
5575 src = dest;
5576 while (diff > 0 && *src)
5578 rtx piece = *src;
5579 diff -= decl_piece_bitsize (piece);
5580 if (copy)
5581 src = &XEXP (piece, 1);
5582 else
5584 *src = XEXP (piece, 1);
5585 free_EXPR_LIST_node (piece);
5588 /* Add padding if needed. */
5589 if (diff < 0 && *src)
5591 if (!copy)
5592 dest = src;
5593 *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
5594 dest = &XEXP (*dest, 1);
5596 if (!copy)
5597 return;
5598 /* Finally copy all nodes following it. */
5599 while (*src)
5601 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5602 decl_piece_bitsize (*src), NULL_RTX);
5603 dest = &XEXP (*dest, 1);
5604 src = &XEXP (*src, 1);
5608 /* Add a variable location node to the linked list for DECL. */
5610 static struct var_loc_node *
5611 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
5613 unsigned int decl_id;
5614 var_loc_list *temp;
5615 struct var_loc_node *loc = NULL;
5616 HOST_WIDE_INT bitsize = -1, bitpos = -1;
5618 if (VAR_P (decl) && DECL_HAS_DEBUG_EXPR_P (decl))
5620 tree realdecl = DECL_DEBUG_EXPR (decl);
5621 if (handled_component_p (realdecl)
5622 || (TREE_CODE (realdecl) == MEM_REF
5623 && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR))
5625 HOST_WIDE_INT maxsize;
5626 bool reverse;
5627 tree innerdecl
5628 = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize,
5629 &reverse);
5630 if (!DECL_P (innerdecl)
5631 || DECL_IGNORED_P (innerdecl)
5632 || TREE_STATIC (innerdecl)
5633 || bitsize <= 0
5634 || bitpos + bitsize > 256
5635 || bitsize != maxsize)
5636 return NULL;
5637 decl = innerdecl;
5641 decl_id = DECL_UID (decl);
5642 var_loc_list **slot
5643 = decl_loc_table->find_slot_with_hash (decl, decl_id, INSERT);
5644 if (*slot == NULL)
5646 temp = ggc_cleared_alloc<var_loc_list> ();
5647 temp->decl_id = decl_id;
5648 *slot = temp;
5650 else
5651 temp = *slot;
5653 /* For PARM_DECLs try to keep around the original incoming value,
5654 even if that means we'll emit a zero-range .debug_loc entry. */
5655 if (temp->last
5656 && temp->first == temp->last
5657 && TREE_CODE (decl) == PARM_DECL
5658 && NOTE_P (temp->first->loc)
5659 && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
5660 && DECL_INCOMING_RTL (decl)
5661 && NOTE_VAR_LOCATION_LOC (temp->first->loc)
5662 && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
5663 == GET_CODE (DECL_INCOMING_RTL (decl))
5664 && prev_real_insn (as_a<rtx_insn *> (temp->first->loc)) == NULL_RTX
5665 && (bitsize != -1
5666 || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
5667 NOTE_VAR_LOCATION_LOC (loc_note))
5668 || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
5669 != NOTE_VAR_LOCATION_STATUS (loc_note))))
5671 loc = ggc_cleared_alloc<var_loc_node> ();
5672 temp->first->next = loc;
5673 temp->last = loc;
5674 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5676 else if (temp->last)
5678 struct var_loc_node *last = temp->last, *unused = NULL;
5679 rtx *piece_loc = NULL, last_loc_note;
5680 HOST_WIDE_INT piece_bitpos = 0;
5681 if (last->next)
5683 last = last->next;
5684 gcc_assert (last->next == NULL);
5686 if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
5688 piece_loc = &last->loc;
5691 HOST_WIDE_INT cur_bitsize = decl_piece_bitsize (*piece_loc);
5692 if (piece_bitpos + cur_bitsize > bitpos)
5693 break;
5694 piece_bitpos += cur_bitsize;
5695 piece_loc = &XEXP (*piece_loc, 1);
5697 while (*piece_loc);
5699 /* TEMP->LAST here is either pointer to the last but one or
5700 last element in the chained list, LAST is pointer to the
5701 last element. */
5702 if (label && strcmp (last->label, label) == 0)
5704 /* For SRA optimized variables if there weren't any real
5705 insns since last note, just modify the last node. */
5706 if (piece_loc != NULL)
5708 adjust_piece_list (piece_loc, NULL, NULL,
5709 bitpos, piece_bitpos, bitsize, loc_note);
5710 return NULL;
5712 /* If the last note doesn't cover any instructions, remove it. */
5713 if (temp->last != last)
5715 temp->last->next = NULL;
5716 unused = last;
5717 last = temp->last;
5718 gcc_assert (strcmp (last->label, label) != 0);
5720 else
5722 gcc_assert (temp->first == temp->last
5723 || (temp->first->next == temp->last
5724 && TREE_CODE (decl) == PARM_DECL));
5725 memset (temp->last, '\0', sizeof (*temp->last));
5726 temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
5727 return temp->last;
5730 if (bitsize == -1 && NOTE_P (last->loc))
5731 last_loc_note = last->loc;
5732 else if (piece_loc != NULL
5733 && *piece_loc != NULL_RTX
5734 && piece_bitpos == bitpos
5735 && decl_piece_bitsize (*piece_loc) == bitsize)
5736 last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
5737 else
5738 last_loc_note = NULL_RTX;
5739 /* If the current location is the same as the end of the list,
5740 and either both or neither of the locations is uninitialized,
5741 we have nothing to do. */
5742 if (last_loc_note == NULL_RTX
5743 || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
5744 NOTE_VAR_LOCATION_LOC (loc_note)))
5745 || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5746 != NOTE_VAR_LOCATION_STATUS (loc_note))
5747 && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5748 == VAR_INIT_STATUS_UNINITIALIZED)
5749 || (NOTE_VAR_LOCATION_STATUS (loc_note)
5750 == VAR_INIT_STATUS_UNINITIALIZED))))
5752 /* Add LOC to the end of list and update LAST. If the last
5753 element of the list has been removed above, reuse its
5754 memory for the new node, otherwise allocate a new one. */
5755 if (unused)
5757 loc = unused;
5758 memset (loc, '\0', sizeof (*loc));
5760 else
5761 loc = ggc_cleared_alloc<var_loc_node> ();
5762 if (bitsize == -1 || piece_loc == NULL)
5763 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5764 else
5765 adjust_piece_list (&loc->loc, &last->loc, piece_loc,
5766 bitpos, piece_bitpos, bitsize, loc_note);
5767 last->next = loc;
5768 /* Ensure TEMP->LAST will point either to the new last but one
5769 element of the chain, or to the last element in it. */
5770 if (last != temp->last)
5771 temp->last = last;
5773 else if (unused)
5774 ggc_free (unused);
5776 else
5778 loc = ggc_cleared_alloc<var_loc_node> ();
5779 temp->first = loc;
5780 temp->last = loc;
5781 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5783 return loc;
5786 /* Keep track of the number of spaces used to indent the
5787 output of the debugging routines that print the structure of
5788 the DIE internal representation. */
5789 static int print_indent;
5791 /* Indent the line the number of spaces given by print_indent. */
5793 static inline void
5794 print_spaces (FILE *outfile)
5796 fprintf (outfile, "%*s", print_indent, "");
5799 /* Print a type signature in hex. */
5801 static inline void
5802 print_signature (FILE *outfile, char *sig)
5804 int i;
5806 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
5807 fprintf (outfile, "%02x", sig[i] & 0xff);
5810 static inline void
5811 print_discr_value (FILE *outfile, dw_discr_value *discr_value)
5813 if (discr_value->pos)
5814 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, discr_value->v.sval);
5815 else
5816 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, discr_value->v.uval);
5819 static void print_loc_descr (dw_loc_descr_ref, FILE *);
5821 /* Print the value associated to the VAL DWARF value node to OUTFILE. If
5822 RECURSE, output location descriptor operations. */
5824 static void
5825 print_dw_val (dw_val_node *val, bool recurse, FILE *outfile)
5827 switch (val->val_class)
5829 case dw_val_class_addr:
5830 fprintf (outfile, "address");
5831 break;
5832 case dw_val_class_offset:
5833 fprintf (outfile, "offset");
5834 break;
5835 case dw_val_class_loc:
5836 fprintf (outfile, "location descriptor");
5837 if (val->v.val_loc == NULL)
5838 fprintf (outfile, " -> <null>\n");
5839 else if (recurse)
5841 fprintf (outfile, ":\n");
5842 print_indent += 4;
5843 print_loc_descr (val->v.val_loc, outfile);
5844 print_indent -= 4;
5846 else
5847 fprintf (outfile, " (%p)\n", (void *) val->v.val_loc);
5848 break;
5849 case dw_val_class_loc_list:
5850 fprintf (outfile, "location list -> label:%s",
5851 val->v.val_loc_list->ll_symbol);
5852 break;
5853 case dw_val_class_range_list:
5854 fprintf (outfile, "range list");
5855 break;
5856 case dw_val_class_const:
5857 case dw_val_class_const_implicit:
5858 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, val->v.val_int);
5859 break;
5860 case dw_val_class_unsigned_const:
5861 case dw_val_class_unsigned_const_implicit:
5862 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, val->v.val_unsigned);
5863 break;
5864 case dw_val_class_const_double:
5865 fprintf (outfile, "constant (" HOST_WIDE_INT_PRINT_DEC","\
5866 HOST_WIDE_INT_PRINT_UNSIGNED")",
5867 val->v.val_double.high,
5868 val->v.val_double.low);
5869 break;
5870 case dw_val_class_wide_int:
5872 int i = val->v.val_wide->get_len ();
5873 fprintf (outfile, "constant (");
5874 gcc_assert (i > 0);
5875 if (val->v.val_wide->elt (i - 1) == 0)
5876 fprintf (outfile, "0x");
5877 fprintf (outfile, HOST_WIDE_INT_PRINT_HEX,
5878 val->v.val_wide->elt (--i));
5879 while (--i >= 0)
5880 fprintf (outfile, HOST_WIDE_INT_PRINT_PADDED_HEX,
5881 val->v.val_wide->elt (i));
5882 fprintf (outfile, ")");
5883 break;
5885 case dw_val_class_vec:
5886 fprintf (outfile, "floating-point or vector constant");
5887 break;
5888 case dw_val_class_flag:
5889 fprintf (outfile, "%u", val->v.val_flag);
5890 break;
5891 case dw_val_class_die_ref:
5892 if (val->v.val_die_ref.die != NULL)
5894 dw_die_ref die = val->v.val_die_ref.die;
5896 if (die->comdat_type_p)
5898 fprintf (outfile, "die -> signature: ");
5899 print_signature (outfile,
5900 die->die_id.die_type_node->signature);
5902 else if (die->die_id.die_symbol)
5903 fprintf (outfile, "die -> label: %s", die->die_id.die_symbol);
5904 else
5905 fprintf (outfile, "die -> %ld", die->die_offset);
5906 fprintf (outfile, " (%p)", (void *) die);
5908 else
5909 fprintf (outfile, "die -> <null>");
5910 break;
5911 case dw_val_class_vms_delta:
5912 fprintf (outfile, "delta: @slotcount(%s-%s)",
5913 val->v.val_vms_delta.lbl2, val->v.val_vms_delta.lbl1);
5914 break;
5915 case dw_val_class_lbl_id:
5916 case dw_val_class_lineptr:
5917 case dw_val_class_macptr:
5918 case dw_val_class_loclistsptr:
5919 case dw_val_class_high_pc:
5920 fprintf (outfile, "label: %s", val->v.val_lbl_id);
5921 break;
5922 case dw_val_class_str:
5923 if (val->v.val_str->str != NULL)
5924 fprintf (outfile, "\"%s\"", val->v.val_str->str);
5925 else
5926 fprintf (outfile, "<null>");
5927 break;
5928 case dw_val_class_file:
5929 case dw_val_class_file_implicit:
5930 fprintf (outfile, "\"%s\" (%d)", val->v.val_file->filename,
5931 val->v.val_file->emitted_number);
5932 break;
5933 case dw_val_class_data8:
5935 int i;
5937 for (i = 0; i < 8; i++)
5938 fprintf (outfile, "%02x", val->v.val_data8[i]);
5939 break;
5941 case dw_val_class_discr_value:
5942 print_discr_value (outfile, &val->v.val_discr_value);
5943 break;
5944 case dw_val_class_discr_list:
5945 for (dw_discr_list_ref node = val->v.val_discr_list;
5946 node != NULL;
5947 node = node->dw_discr_next)
5949 if (node->dw_discr_range)
5951 fprintf (outfile, " .. ");
5952 print_discr_value (outfile, &node->dw_discr_lower_bound);
5953 print_discr_value (outfile, &node->dw_discr_upper_bound);
5955 else
5956 print_discr_value (outfile, &node->dw_discr_lower_bound);
5958 if (node->dw_discr_next != NULL)
5959 fprintf (outfile, " | ");
5961 default:
5962 break;
5966 /* Likewise, for a DIE attribute. */
5968 static void
5969 print_attribute (dw_attr_node *a, bool recurse, FILE *outfile)
5971 print_dw_val (&a->dw_attr_val, recurse, outfile);
5975 /* Print the list of operands in the LOC location description to OUTFILE. This
5976 routine is a debugging aid only. */
5978 static void
5979 print_loc_descr (dw_loc_descr_ref loc, FILE *outfile)
5981 dw_loc_descr_ref l = loc;
5983 if (loc == NULL)
5985 print_spaces (outfile);
5986 fprintf (outfile, "<null>\n");
5987 return;
5990 for (l = loc; l != NULL; l = l->dw_loc_next)
5992 print_spaces (outfile);
5993 fprintf (outfile, "(%p) %s",
5994 (void *) l,
5995 dwarf_stack_op_name (l->dw_loc_opc));
5996 if (l->dw_loc_oprnd1.val_class != dw_val_class_none)
5998 fprintf (outfile, " ");
5999 print_dw_val (&l->dw_loc_oprnd1, false, outfile);
6001 if (l->dw_loc_oprnd2.val_class != dw_val_class_none)
6003 fprintf (outfile, ", ");
6004 print_dw_val (&l->dw_loc_oprnd2, false, outfile);
6006 fprintf (outfile, "\n");
6010 /* Print the information associated with a given DIE, and its children.
6011 This routine is a debugging aid only. */
6013 static void
6014 print_die (dw_die_ref die, FILE *outfile)
6016 dw_attr_node *a;
6017 dw_die_ref c;
6018 unsigned ix;
6020 print_spaces (outfile);
6021 fprintf (outfile, "DIE %4ld: %s (%p)\n",
6022 die->die_offset, dwarf_tag_name (die->die_tag),
6023 (void*) die);
6024 print_spaces (outfile);
6025 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
6026 fprintf (outfile, " offset: %ld", die->die_offset);
6027 fprintf (outfile, " mark: %d\n", die->die_mark);
6029 if (die->comdat_type_p)
6031 print_spaces (outfile);
6032 fprintf (outfile, " signature: ");
6033 print_signature (outfile, die->die_id.die_type_node->signature);
6034 fprintf (outfile, "\n");
6037 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6039 print_spaces (outfile);
6040 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
6042 print_attribute (a, true, outfile);
6043 fprintf (outfile, "\n");
6046 if (die->die_child != NULL)
6048 print_indent += 4;
6049 FOR_EACH_CHILD (die, c, print_die (c, outfile));
6050 print_indent -= 4;
6052 if (print_indent == 0)
6053 fprintf (outfile, "\n");
6056 /* Print the list of operations in the LOC location description. */
6058 DEBUG_FUNCTION void
6059 debug_dwarf_loc_descr (dw_loc_descr_ref loc)
6061 print_loc_descr (loc, stderr);
6064 /* Print the information collected for a given DIE. */
6066 DEBUG_FUNCTION void
6067 debug_dwarf_die (dw_die_ref die)
6069 print_die (die, stderr);
6072 DEBUG_FUNCTION void
6073 debug (die_struct &ref)
6075 print_die (&ref, stderr);
6078 DEBUG_FUNCTION void
6079 debug (die_struct *ptr)
6081 if (ptr)
6082 debug (*ptr);
6083 else
6084 fprintf (stderr, "<nil>\n");
6088 /* Print all DWARF information collected for the compilation unit.
6089 This routine is a debugging aid only. */
6091 DEBUG_FUNCTION void
6092 debug_dwarf (void)
6094 print_indent = 0;
6095 print_die (comp_unit_die (), stderr);
6098 /* Verify the DIE tree structure. */
6100 DEBUG_FUNCTION void
6101 verify_die (dw_die_ref die)
6103 gcc_assert (!die->die_mark);
6104 if (die->die_parent == NULL
6105 && die->die_sib == NULL)
6106 return;
6107 /* Verify the die_sib list is cyclic. */
6108 dw_die_ref x = die;
6111 x->die_mark = 1;
6112 x = x->die_sib;
6114 while (x && !x->die_mark);
6115 gcc_assert (x == die);
6116 x = die;
6119 /* Verify all dies have the same parent. */
6120 gcc_assert (x->die_parent == die->die_parent);
6121 if (x->die_child)
6123 /* Verify the child has the proper parent and recurse. */
6124 gcc_assert (x->die_child->die_parent == x);
6125 verify_die (x->die_child);
6127 x->die_mark = 0;
6128 x = x->die_sib;
6130 while (x && x->die_mark);
6133 /* Sanity checks on DIEs. */
6135 static void
6136 check_die (dw_die_ref die)
6138 unsigned ix;
6139 dw_attr_node *a;
6140 bool inline_found = false;
6141 int n_location = 0, n_low_pc = 0, n_high_pc = 0, n_artificial = 0;
6142 int n_decl_line = 0, n_decl_column = 0, n_decl_file = 0;
6143 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6145 switch (a->dw_attr)
6147 case DW_AT_inline:
6148 if (a->dw_attr_val.v.val_unsigned)
6149 inline_found = true;
6150 break;
6151 case DW_AT_location:
6152 ++n_location;
6153 break;
6154 case DW_AT_low_pc:
6155 ++n_low_pc;
6156 break;
6157 case DW_AT_high_pc:
6158 ++n_high_pc;
6159 break;
6160 case DW_AT_artificial:
6161 ++n_artificial;
6162 break;
6163 case DW_AT_decl_column:
6164 ++n_decl_column;
6165 break;
6166 case DW_AT_decl_line:
6167 ++n_decl_line;
6168 break;
6169 case DW_AT_decl_file:
6170 ++n_decl_file;
6171 break;
6172 default:
6173 break;
6176 if (n_location > 1 || n_low_pc > 1 || n_high_pc > 1 || n_artificial > 1
6177 || n_decl_column > 1 || n_decl_line > 1 || n_decl_file > 1)
6179 fprintf (stderr, "Duplicate attributes in DIE:\n");
6180 debug_dwarf_die (die);
6181 gcc_unreachable ();
6183 if (inline_found)
6185 /* A debugging information entry that is a member of an abstract
6186 instance tree [that has DW_AT_inline] should not contain any
6187 attributes which describe aspects of the subroutine which vary
6188 between distinct inlined expansions or distinct out-of-line
6189 expansions. */
6190 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6191 gcc_assert (a->dw_attr != DW_AT_low_pc
6192 && a->dw_attr != DW_AT_high_pc
6193 && a->dw_attr != DW_AT_location
6194 && a->dw_attr != DW_AT_frame_base
6195 && a->dw_attr != DW_AT_call_all_calls
6196 && a->dw_attr != DW_AT_GNU_all_call_sites);
6200 /* Start a new compilation unit DIE for an include file. OLD_UNIT is the CU
6201 for the enclosing include file, if any. BINCL_DIE is the DW_TAG_GNU_BINCL
6202 DIE that marks the start of the DIEs for this include file. */
6204 static dw_die_ref
6205 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
6207 const char *filename = get_AT_string (bincl_die, DW_AT_name);
6208 dw_die_ref new_unit = gen_compile_unit_die (filename);
6210 new_unit->die_sib = old_unit;
6211 return new_unit;
6214 /* Close an include-file CU and reopen the enclosing one. */
6216 static dw_die_ref
6217 pop_compile_unit (dw_die_ref old_unit)
6219 dw_die_ref new_unit = old_unit->die_sib;
6221 old_unit->die_sib = NULL;
6222 return new_unit;
6225 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
6226 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
6227 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
6229 /* Calculate the checksum of a location expression. */
6231 static inline void
6232 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
6234 int tem;
6235 inchash::hash hstate;
6236 hashval_t hash;
6238 tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
6239 CHECKSUM (tem);
6240 hash_loc_operands (loc, hstate);
6241 hash = hstate.end();
6242 CHECKSUM (hash);
6245 /* Calculate the checksum of an attribute. */
6247 static void
6248 attr_checksum (dw_attr_node *at, struct md5_ctx *ctx, int *mark)
6250 dw_loc_descr_ref loc;
6251 rtx r;
6253 CHECKSUM (at->dw_attr);
6255 /* We don't care that this was compiled with a different compiler
6256 snapshot; if the output is the same, that's what matters. */
6257 if (at->dw_attr == DW_AT_producer)
6258 return;
6260 switch (AT_class (at))
6262 case dw_val_class_const:
6263 case dw_val_class_const_implicit:
6264 CHECKSUM (at->dw_attr_val.v.val_int);
6265 break;
6266 case dw_val_class_unsigned_const:
6267 case dw_val_class_unsigned_const_implicit:
6268 CHECKSUM (at->dw_attr_val.v.val_unsigned);
6269 break;
6270 case dw_val_class_const_double:
6271 CHECKSUM (at->dw_attr_val.v.val_double);
6272 break;
6273 case dw_val_class_wide_int:
6274 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
6275 get_full_len (*at->dw_attr_val.v.val_wide)
6276 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
6277 break;
6278 case dw_val_class_vec:
6279 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
6280 (at->dw_attr_val.v.val_vec.length
6281 * at->dw_attr_val.v.val_vec.elt_size));
6282 break;
6283 case dw_val_class_flag:
6284 CHECKSUM (at->dw_attr_val.v.val_flag);
6285 break;
6286 case dw_val_class_str:
6287 CHECKSUM_STRING (AT_string (at));
6288 break;
6290 case dw_val_class_addr:
6291 r = AT_addr (at);
6292 gcc_assert (GET_CODE (r) == SYMBOL_REF);
6293 CHECKSUM_STRING (XSTR (r, 0));
6294 break;
6296 case dw_val_class_offset:
6297 CHECKSUM (at->dw_attr_val.v.val_offset);
6298 break;
6300 case dw_val_class_loc:
6301 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6302 loc_checksum (loc, ctx);
6303 break;
6305 case dw_val_class_die_ref:
6306 die_checksum (AT_ref (at), ctx, mark);
6307 break;
6309 case dw_val_class_fde_ref:
6310 case dw_val_class_vms_delta:
6311 case dw_val_class_lbl_id:
6312 case dw_val_class_lineptr:
6313 case dw_val_class_macptr:
6314 case dw_val_class_loclistsptr:
6315 case dw_val_class_high_pc:
6316 break;
6318 case dw_val_class_file:
6319 case dw_val_class_file_implicit:
6320 CHECKSUM_STRING (AT_file (at)->filename);
6321 break;
6323 case dw_val_class_data8:
6324 CHECKSUM (at->dw_attr_val.v.val_data8);
6325 break;
6327 default:
6328 break;
6332 /* Calculate the checksum of a DIE. */
6334 static void
6335 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6337 dw_die_ref c;
6338 dw_attr_node *a;
6339 unsigned ix;
6341 /* To avoid infinite recursion. */
6342 if (die->die_mark)
6344 CHECKSUM (die->die_mark);
6345 return;
6347 die->die_mark = ++(*mark);
6349 CHECKSUM (die->die_tag);
6351 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6352 attr_checksum (a, ctx, mark);
6354 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
6357 #undef CHECKSUM
6358 #undef CHECKSUM_BLOCK
6359 #undef CHECKSUM_STRING
6361 /* For DWARF-4 types, include the trailing NULL when checksumming strings. */
6362 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
6363 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
6364 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
6365 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
6366 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
6367 #define CHECKSUM_ATTR(FOO) \
6368 if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
6370 /* Calculate the checksum of a number in signed LEB128 format. */
6372 static void
6373 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
6375 unsigned char byte;
6376 bool more;
6378 while (1)
6380 byte = (value & 0x7f);
6381 value >>= 7;
6382 more = !((value == 0 && (byte & 0x40) == 0)
6383 || (value == -1 && (byte & 0x40) != 0));
6384 if (more)
6385 byte |= 0x80;
6386 CHECKSUM (byte);
6387 if (!more)
6388 break;
6392 /* Calculate the checksum of a number in unsigned LEB128 format. */
6394 static void
6395 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
6397 while (1)
6399 unsigned char byte = (value & 0x7f);
6400 value >>= 7;
6401 if (value != 0)
6402 /* More bytes to follow. */
6403 byte |= 0x80;
6404 CHECKSUM (byte);
6405 if (value == 0)
6406 break;
6410 /* Checksum the context of the DIE. This adds the names of any
6411 surrounding namespaces or structures to the checksum. */
6413 static void
6414 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
6416 const char *name;
6417 dw_die_ref spec;
6418 int tag = die->die_tag;
6420 if (tag != DW_TAG_namespace
6421 && tag != DW_TAG_structure_type
6422 && tag != DW_TAG_class_type)
6423 return;
6425 name = get_AT_string (die, DW_AT_name);
6427 spec = get_AT_ref (die, DW_AT_specification);
6428 if (spec != NULL)
6429 die = spec;
6431 if (die->die_parent != NULL)
6432 checksum_die_context (die->die_parent, ctx);
6434 CHECKSUM_ULEB128 ('C');
6435 CHECKSUM_ULEB128 (tag);
6436 if (name != NULL)
6437 CHECKSUM_STRING (name);
6440 /* Calculate the checksum of a location expression. */
6442 static inline void
6443 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
6445 /* Special case for lone DW_OP_plus_uconst: checksum as if the location
6446 were emitted as a DW_FORM_sdata instead of a location expression. */
6447 if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
6449 CHECKSUM_ULEB128 (DW_FORM_sdata);
6450 CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
6451 return;
6454 /* Otherwise, just checksum the raw location expression. */
6455 while (loc != NULL)
6457 inchash::hash hstate;
6458 hashval_t hash;
6460 CHECKSUM_ULEB128 (loc->dtprel);
6461 CHECKSUM_ULEB128 (loc->dw_loc_opc);
6462 hash_loc_operands (loc, hstate);
6463 hash = hstate.end ();
6464 CHECKSUM (hash);
6465 loc = loc->dw_loc_next;
6469 /* Calculate the checksum of an attribute. */
6471 static void
6472 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_node *at,
6473 struct md5_ctx *ctx, int *mark)
6475 dw_loc_descr_ref loc;
6476 rtx r;
6478 if (AT_class (at) == dw_val_class_die_ref)
6480 dw_die_ref target_die = AT_ref (at);
6482 /* For pointer and reference types, we checksum only the (qualified)
6483 name of the target type (if there is a name). For friend entries,
6484 we checksum only the (qualified) name of the target type or function.
6485 This allows the checksum to remain the same whether the target type
6486 is complete or not. */
6487 if ((at->dw_attr == DW_AT_type
6488 && (tag == DW_TAG_pointer_type
6489 || tag == DW_TAG_reference_type
6490 || tag == DW_TAG_rvalue_reference_type
6491 || tag == DW_TAG_ptr_to_member_type))
6492 || (at->dw_attr == DW_AT_friend
6493 && tag == DW_TAG_friend))
6495 dw_attr_node *name_attr = get_AT (target_die, DW_AT_name);
6497 if (name_attr != NULL)
6499 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
6501 if (decl == NULL)
6502 decl = target_die;
6503 CHECKSUM_ULEB128 ('N');
6504 CHECKSUM_ULEB128 (at->dw_attr);
6505 if (decl->die_parent != NULL)
6506 checksum_die_context (decl->die_parent, ctx);
6507 CHECKSUM_ULEB128 ('E');
6508 CHECKSUM_STRING (AT_string (name_attr));
6509 return;
6513 /* For all other references to another DIE, we check to see if the
6514 target DIE has already been visited. If it has, we emit a
6515 backward reference; if not, we descend recursively. */
6516 if (target_die->die_mark > 0)
6518 CHECKSUM_ULEB128 ('R');
6519 CHECKSUM_ULEB128 (at->dw_attr);
6520 CHECKSUM_ULEB128 (target_die->die_mark);
6522 else
6524 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
6526 if (decl == NULL)
6527 decl = target_die;
6528 target_die->die_mark = ++(*mark);
6529 CHECKSUM_ULEB128 ('T');
6530 CHECKSUM_ULEB128 (at->dw_attr);
6531 if (decl->die_parent != NULL)
6532 checksum_die_context (decl->die_parent, ctx);
6533 die_checksum_ordered (target_die, ctx, mark);
6535 return;
6538 CHECKSUM_ULEB128 ('A');
6539 CHECKSUM_ULEB128 (at->dw_attr);
6541 switch (AT_class (at))
6543 case dw_val_class_const:
6544 case dw_val_class_const_implicit:
6545 CHECKSUM_ULEB128 (DW_FORM_sdata);
6546 CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
6547 break;
6549 case dw_val_class_unsigned_const:
6550 case dw_val_class_unsigned_const_implicit:
6551 CHECKSUM_ULEB128 (DW_FORM_sdata);
6552 CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
6553 break;
6555 case dw_val_class_const_double:
6556 CHECKSUM_ULEB128 (DW_FORM_block);
6557 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
6558 CHECKSUM (at->dw_attr_val.v.val_double);
6559 break;
6561 case dw_val_class_wide_int:
6562 CHECKSUM_ULEB128 (DW_FORM_block);
6563 CHECKSUM_ULEB128 (get_full_len (*at->dw_attr_val.v.val_wide)
6564 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
6565 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
6566 get_full_len (*at->dw_attr_val.v.val_wide)
6567 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
6568 break;
6570 case dw_val_class_vec:
6571 CHECKSUM_ULEB128 (DW_FORM_block);
6572 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_vec.length
6573 * at->dw_attr_val.v.val_vec.elt_size);
6574 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
6575 (at->dw_attr_val.v.val_vec.length
6576 * at->dw_attr_val.v.val_vec.elt_size));
6577 break;
6579 case dw_val_class_flag:
6580 CHECKSUM_ULEB128 (DW_FORM_flag);
6581 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
6582 break;
6584 case dw_val_class_str:
6585 CHECKSUM_ULEB128 (DW_FORM_string);
6586 CHECKSUM_STRING (AT_string (at));
6587 break;
6589 case dw_val_class_addr:
6590 r = AT_addr (at);
6591 gcc_assert (GET_CODE (r) == SYMBOL_REF);
6592 CHECKSUM_ULEB128 (DW_FORM_string);
6593 CHECKSUM_STRING (XSTR (r, 0));
6594 break;
6596 case dw_val_class_offset:
6597 CHECKSUM_ULEB128 (DW_FORM_sdata);
6598 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
6599 break;
6601 case dw_val_class_loc:
6602 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6603 loc_checksum_ordered (loc, ctx);
6604 break;
6606 case dw_val_class_fde_ref:
6607 case dw_val_class_lbl_id:
6608 case dw_val_class_lineptr:
6609 case dw_val_class_macptr:
6610 case dw_val_class_loclistsptr:
6611 case dw_val_class_high_pc:
6612 break;
6614 case dw_val_class_file:
6615 case dw_val_class_file_implicit:
6616 CHECKSUM_ULEB128 (DW_FORM_string);
6617 CHECKSUM_STRING (AT_file (at)->filename);
6618 break;
6620 case dw_val_class_data8:
6621 CHECKSUM (at->dw_attr_val.v.val_data8);
6622 break;
6624 default:
6625 break;
6629 struct checksum_attributes
6631 dw_attr_node *at_name;
6632 dw_attr_node *at_type;
6633 dw_attr_node *at_friend;
6634 dw_attr_node *at_accessibility;
6635 dw_attr_node *at_address_class;
6636 dw_attr_node *at_alignment;
6637 dw_attr_node *at_allocated;
6638 dw_attr_node *at_artificial;
6639 dw_attr_node *at_associated;
6640 dw_attr_node *at_binary_scale;
6641 dw_attr_node *at_bit_offset;
6642 dw_attr_node *at_bit_size;
6643 dw_attr_node *at_bit_stride;
6644 dw_attr_node *at_byte_size;
6645 dw_attr_node *at_byte_stride;
6646 dw_attr_node *at_const_value;
6647 dw_attr_node *at_containing_type;
6648 dw_attr_node *at_count;
6649 dw_attr_node *at_data_location;
6650 dw_attr_node *at_data_member_location;
6651 dw_attr_node *at_decimal_scale;
6652 dw_attr_node *at_decimal_sign;
6653 dw_attr_node *at_default_value;
6654 dw_attr_node *at_digit_count;
6655 dw_attr_node *at_discr;
6656 dw_attr_node *at_discr_list;
6657 dw_attr_node *at_discr_value;
6658 dw_attr_node *at_encoding;
6659 dw_attr_node *at_endianity;
6660 dw_attr_node *at_explicit;
6661 dw_attr_node *at_is_optional;
6662 dw_attr_node *at_location;
6663 dw_attr_node *at_lower_bound;
6664 dw_attr_node *at_mutable;
6665 dw_attr_node *at_ordering;
6666 dw_attr_node *at_picture_string;
6667 dw_attr_node *at_prototyped;
6668 dw_attr_node *at_small;
6669 dw_attr_node *at_segment;
6670 dw_attr_node *at_string_length;
6671 dw_attr_node *at_string_length_bit_size;
6672 dw_attr_node *at_string_length_byte_size;
6673 dw_attr_node *at_threads_scaled;
6674 dw_attr_node *at_upper_bound;
6675 dw_attr_node *at_use_location;
6676 dw_attr_node *at_use_UTF8;
6677 dw_attr_node *at_variable_parameter;
6678 dw_attr_node *at_virtuality;
6679 dw_attr_node *at_visibility;
6680 dw_attr_node *at_vtable_elem_location;
6683 /* Collect the attributes that we will want to use for the checksum. */
6685 static void
6686 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
6688 dw_attr_node *a;
6689 unsigned ix;
6691 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6693 switch (a->dw_attr)
6695 case DW_AT_name:
6696 attrs->at_name = a;
6697 break;
6698 case DW_AT_type:
6699 attrs->at_type = a;
6700 break;
6701 case DW_AT_friend:
6702 attrs->at_friend = a;
6703 break;
6704 case DW_AT_accessibility:
6705 attrs->at_accessibility = a;
6706 break;
6707 case DW_AT_address_class:
6708 attrs->at_address_class = a;
6709 break;
6710 case DW_AT_alignment:
6711 attrs->at_alignment = a;
6712 break;
6713 case DW_AT_allocated:
6714 attrs->at_allocated = a;
6715 break;
6716 case DW_AT_artificial:
6717 attrs->at_artificial = a;
6718 break;
6719 case DW_AT_associated:
6720 attrs->at_associated = a;
6721 break;
6722 case DW_AT_binary_scale:
6723 attrs->at_binary_scale = a;
6724 break;
6725 case DW_AT_bit_offset:
6726 attrs->at_bit_offset = a;
6727 break;
6728 case DW_AT_bit_size:
6729 attrs->at_bit_size = a;
6730 break;
6731 case DW_AT_bit_stride:
6732 attrs->at_bit_stride = a;
6733 break;
6734 case DW_AT_byte_size:
6735 attrs->at_byte_size = a;
6736 break;
6737 case DW_AT_byte_stride:
6738 attrs->at_byte_stride = a;
6739 break;
6740 case DW_AT_const_value:
6741 attrs->at_const_value = a;
6742 break;
6743 case DW_AT_containing_type:
6744 attrs->at_containing_type = a;
6745 break;
6746 case DW_AT_count:
6747 attrs->at_count = a;
6748 break;
6749 case DW_AT_data_location:
6750 attrs->at_data_location = a;
6751 break;
6752 case DW_AT_data_member_location:
6753 attrs->at_data_member_location = a;
6754 break;
6755 case DW_AT_decimal_scale:
6756 attrs->at_decimal_scale = a;
6757 break;
6758 case DW_AT_decimal_sign:
6759 attrs->at_decimal_sign = a;
6760 break;
6761 case DW_AT_default_value:
6762 attrs->at_default_value = a;
6763 break;
6764 case DW_AT_digit_count:
6765 attrs->at_digit_count = a;
6766 break;
6767 case DW_AT_discr:
6768 attrs->at_discr = a;
6769 break;
6770 case DW_AT_discr_list:
6771 attrs->at_discr_list = a;
6772 break;
6773 case DW_AT_discr_value:
6774 attrs->at_discr_value = a;
6775 break;
6776 case DW_AT_encoding:
6777 attrs->at_encoding = a;
6778 break;
6779 case DW_AT_endianity:
6780 attrs->at_endianity = a;
6781 break;
6782 case DW_AT_explicit:
6783 attrs->at_explicit = a;
6784 break;
6785 case DW_AT_is_optional:
6786 attrs->at_is_optional = a;
6787 break;
6788 case DW_AT_location:
6789 attrs->at_location = a;
6790 break;
6791 case DW_AT_lower_bound:
6792 attrs->at_lower_bound = a;
6793 break;
6794 case DW_AT_mutable:
6795 attrs->at_mutable = a;
6796 break;
6797 case DW_AT_ordering:
6798 attrs->at_ordering = a;
6799 break;
6800 case DW_AT_picture_string:
6801 attrs->at_picture_string = a;
6802 break;
6803 case DW_AT_prototyped:
6804 attrs->at_prototyped = a;
6805 break;
6806 case DW_AT_small:
6807 attrs->at_small = a;
6808 break;
6809 case DW_AT_segment:
6810 attrs->at_segment = a;
6811 break;
6812 case DW_AT_string_length:
6813 attrs->at_string_length = a;
6814 break;
6815 case DW_AT_string_length_bit_size:
6816 attrs->at_string_length_bit_size = a;
6817 break;
6818 case DW_AT_string_length_byte_size:
6819 attrs->at_string_length_byte_size = a;
6820 break;
6821 case DW_AT_threads_scaled:
6822 attrs->at_threads_scaled = a;
6823 break;
6824 case DW_AT_upper_bound:
6825 attrs->at_upper_bound = a;
6826 break;
6827 case DW_AT_use_location:
6828 attrs->at_use_location = a;
6829 break;
6830 case DW_AT_use_UTF8:
6831 attrs->at_use_UTF8 = a;
6832 break;
6833 case DW_AT_variable_parameter:
6834 attrs->at_variable_parameter = a;
6835 break;
6836 case DW_AT_virtuality:
6837 attrs->at_virtuality = a;
6838 break;
6839 case DW_AT_visibility:
6840 attrs->at_visibility = a;
6841 break;
6842 case DW_AT_vtable_elem_location:
6843 attrs->at_vtable_elem_location = a;
6844 break;
6845 default:
6846 break;
6851 /* Calculate the checksum of a DIE, using an ordered subset of attributes. */
6853 static void
6854 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6856 dw_die_ref c;
6857 dw_die_ref decl;
6858 struct checksum_attributes attrs;
6860 CHECKSUM_ULEB128 ('D');
6861 CHECKSUM_ULEB128 (die->die_tag);
6863 memset (&attrs, 0, sizeof (attrs));
6865 decl = get_AT_ref (die, DW_AT_specification);
6866 if (decl != NULL)
6867 collect_checksum_attributes (&attrs, decl);
6868 collect_checksum_attributes (&attrs, die);
6870 CHECKSUM_ATTR (attrs.at_name);
6871 CHECKSUM_ATTR (attrs.at_accessibility);
6872 CHECKSUM_ATTR (attrs.at_address_class);
6873 CHECKSUM_ATTR (attrs.at_allocated);
6874 CHECKSUM_ATTR (attrs.at_artificial);
6875 CHECKSUM_ATTR (attrs.at_associated);
6876 CHECKSUM_ATTR (attrs.at_binary_scale);
6877 CHECKSUM_ATTR (attrs.at_bit_offset);
6878 CHECKSUM_ATTR (attrs.at_bit_size);
6879 CHECKSUM_ATTR (attrs.at_bit_stride);
6880 CHECKSUM_ATTR (attrs.at_byte_size);
6881 CHECKSUM_ATTR (attrs.at_byte_stride);
6882 CHECKSUM_ATTR (attrs.at_const_value);
6883 CHECKSUM_ATTR (attrs.at_containing_type);
6884 CHECKSUM_ATTR (attrs.at_count);
6885 CHECKSUM_ATTR (attrs.at_data_location);
6886 CHECKSUM_ATTR (attrs.at_data_member_location);
6887 CHECKSUM_ATTR (attrs.at_decimal_scale);
6888 CHECKSUM_ATTR (attrs.at_decimal_sign);
6889 CHECKSUM_ATTR (attrs.at_default_value);
6890 CHECKSUM_ATTR (attrs.at_digit_count);
6891 CHECKSUM_ATTR (attrs.at_discr);
6892 CHECKSUM_ATTR (attrs.at_discr_list);
6893 CHECKSUM_ATTR (attrs.at_discr_value);
6894 CHECKSUM_ATTR (attrs.at_encoding);
6895 CHECKSUM_ATTR (attrs.at_endianity);
6896 CHECKSUM_ATTR (attrs.at_explicit);
6897 CHECKSUM_ATTR (attrs.at_is_optional);
6898 CHECKSUM_ATTR (attrs.at_location);
6899 CHECKSUM_ATTR (attrs.at_lower_bound);
6900 CHECKSUM_ATTR (attrs.at_mutable);
6901 CHECKSUM_ATTR (attrs.at_ordering);
6902 CHECKSUM_ATTR (attrs.at_picture_string);
6903 CHECKSUM_ATTR (attrs.at_prototyped);
6904 CHECKSUM_ATTR (attrs.at_small);
6905 CHECKSUM_ATTR (attrs.at_segment);
6906 CHECKSUM_ATTR (attrs.at_string_length);
6907 CHECKSUM_ATTR (attrs.at_string_length_bit_size);
6908 CHECKSUM_ATTR (attrs.at_string_length_byte_size);
6909 CHECKSUM_ATTR (attrs.at_threads_scaled);
6910 CHECKSUM_ATTR (attrs.at_upper_bound);
6911 CHECKSUM_ATTR (attrs.at_use_location);
6912 CHECKSUM_ATTR (attrs.at_use_UTF8);
6913 CHECKSUM_ATTR (attrs.at_variable_parameter);
6914 CHECKSUM_ATTR (attrs.at_virtuality);
6915 CHECKSUM_ATTR (attrs.at_visibility);
6916 CHECKSUM_ATTR (attrs.at_vtable_elem_location);
6917 CHECKSUM_ATTR (attrs.at_type);
6918 CHECKSUM_ATTR (attrs.at_friend);
6919 CHECKSUM_ATTR (attrs.at_alignment);
6921 /* Checksum the child DIEs. */
6922 c = die->die_child;
6923 if (c) do {
6924 dw_attr_node *name_attr;
6926 c = c->die_sib;
6927 name_attr = get_AT (c, DW_AT_name);
6928 if (is_template_instantiation (c))
6930 /* Ignore instantiations of member type and function templates. */
6932 else if (name_attr != NULL
6933 && (is_type_die (c) || c->die_tag == DW_TAG_subprogram))
6935 /* Use a shallow checksum for named nested types and member
6936 functions. */
6937 CHECKSUM_ULEB128 ('S');
6938 CHECKSUM_ULEB128 (c->die_tag);
6939 CHECKSUM_STRING (AT_string (name_attr));
6941 else
6943 /* Use a deep checksum for other children. */
6944 /* Mark this DIE so it gets processed when unmarking. */
6945 if (c->die_mark == 0)
6946 c->die_mark = -1;
6947 die_checksum_ordered (c, ctx, mark);
6949 } while (c != die->die_child);
6951 CHECKSUM_ULEB128 (0);
6954 /* Add a type name and tag to a hash. */
6955 static void
6956 die_odr_checksum (int tag, const char *name, md5_ctx *ctx)
6958 CHECKSUM_ULEB128 (tag);
6959 CHECKSUM_STRING (name);
6962 #undef CHECKSUM
6963 #undef CHECKSUM_STRING
6964 #undef CHECKSUM_ATTR
6965 #undef CHECKSUM_LEB128
6966 #undef CHECKSUM_ULEB128
6968 /* Generate the type signature for DIE. This is computed by generating an
6969 MD5 checksum over the DIE's tag, its relevant attributes, and its
6970 children. Attributes that are references to other DIEs are processed
6971 by recursion, using the MARK field to prevent infinite recursion.
6972 If the DIE is nested inside a namespace or another type, we also
6973 need to include that context in the signature. The lower 64 bits
6974 of the resulting MD5 checksum comprise the signature. */
6976 static void
6977 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
6979 int mark;
6980 const char *name;
6981 unsigned char checksum[16];
6982 struct md5_ctx ctx;
6983 dw_die_ref decl;
6984 dw_die_ref parent;
6986 name = get_AT_string (die, DW_AT_name);
6987 decl = get_AT_ref (die, DW_AT_specification);
6988 parent = get_die_parent (die);
6990 /* First, compute a signature for just the type name (and its surrounding
6991 context, if any. This is stored in the type unit DIE for link-time
6992 ODR (one-definition rule) checking. */
6994 if (is_cxx () && name != NULL)
6996 md5_init_ctx (&ctx);
6998 /* Checksum the names of surrounding namespaces and structures. */
6999 if (parent != NULL)
7000 checksum_die_context (parent, &ctx);
7002 /* Checksum the current DIE. */
7003 die_odr_checksum (die->die_tag, name, &ctx);
7004 md5_finish_ctx (&ctx, checksum);
7006 add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
7009 /* Next, compute the complete type signature. */
7011 md5_init_ctx (&ctx);
7012 mark = 1;
7013 die->die_mark = mark;
7015 /* Checksum the names of surrounding namespaces and structures. */
7016 if (parent != NULL)
7017 checksum_die_context (parent, &ctx);
7019 /* Checksum the DIE and its children. */
7020 die_checksum_ordered (die, &ctx, &mark);
7021 unmark_all_dies (die);
7022 md5_finish_ctx (&ctx, checksum);
7024 /* Store the signature in the type node and link the type DIE and the
7025 type node together. */
7026 memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
7027 DWARF_TYPE_SIGNATURE_SIZE);
7028 die->comdat_type_p = true;
7029 die->die_id.die_type_node = type_node;
7030 type_node->type_die = die;
7032 /* If the DIE is a specification, link its declaration to the type node
7033 as well. */
7034 if (decl != NULL)
7036 decl->comdat_type_p = true;
7037 decl->die_id.die_type_node = type_node;
7041 /* Do the location expressions look same? */
7042 static inline int
7043 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
7045 return loc1->dw_loc_opc == loc2->dw_loc_opc
7046 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
7047 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
7050 /* Do the values look the same? */
7051 static int
7052 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
7054 dw_loc_descr_ref loc1, loc2;
7055 rtx r1, r2;
7057 if (v1->val_class != v2->val_class)
7058 return 0;
7060 switch (v1->val_class)
7062 case dw_val_class_const:
7063 case dw_val_class_const_implicit:
7064 return v1->v.val_int == v2->v.val_int;
7065 case dw_val_class_unsigned_const:
7066 case dw_val_class_unsigned_const_implicit:
7067 return v1->v.val_unsigned == v2->v.val_unsigned;
7068 case dw_val_class_const_double:
7069 return v1->v.val_double.high == v2->v.val_double.high
7070 && v1->v.val_double.low == v2->v.val_double.low;
7071 case dw_val_class_wide_int:
7072 return *v1->v.val_wide == *v2->v.val_wide;
7073 case dw_val_class_vec:
7074 if (v1->v.val_vec.length != v2->v.val_vec.length
7075 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
7076 return 0;
7077 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
7078 v1->v.val_vec.length * v1->v.val_vec.elt_size))
7079 return 0;
7080 return 1;
7081 case dw_val_class_flag:
7082 return v1->v.val_flag == v2->v.val_flag;
7083 case dw_val_class_str:
7084 return !strcmp (v1->v.val_str->str, v2->v.val_str->str);
7086 case dw_val_class_addr:
7087 r1 = v1->v.val_addr;
7088 r2 = v2->v.val_addr;
7089 if (GET_CODE (r1) != GET_CODE (r2))
7090 return 0;
7091 return !rtx_equal_p (r1, r2);
7093 case dw_val_class_offset:
7094 return v1->v.val_offset == v2->v.val_offset;
7096 case dw_val_class_loc:
7097 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
7098 loc1 && loc2;
7099 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
7100 if (!same_loc_p (loc1, loc2, mark))
7101 return 0;
7102 return !loc1 && !loc2;
7104 case dw_val_class_die_ref:
7105 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
7107 case dw_val_class_fde_ref:
7108 case dw_val_class_vms_delta:
7109 case dw_val_class_lbl_id:
7110 case dw_val_class_lineptr:
7111 case dw_val_class_macptr:
7112 case dw_val_class_loclistsptr:
7113 case dw_val_class_high_pc:
7114 return 1;
7116 case dw_val_class_file:
7117 case dw_val_class_file_implicit:
7118 return v1->v.val_file == v2->v.val_file;
7120 case dw_val_class_data8:
7121 return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
7123 default:
7124 return 1;
7128 /* Do the attributes look the same? */
7130 static int
7131 same_attr_p (dw_attr_node *at1, dw_attr_node *at2, int *mark)
7133 if (at1->dw_attr != at2->dw_attr)
7134 return 0;
7136 /* We don't care that this was compiled with a different compiler
7137 snapshot; if the output is the same, that's what matters. */
7138 if (at1->dw_attr == DW_AT_producer)
7139 return 1;
7141 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
7144 /* Do the dies look the same? */
7146 static int
7147 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
7149 dw_die_ref c1, c2;
7150 dw_attr_node *a1;
7151 unsigned ix;
7153 /* To avoid infinite recursion. */
7154 if (die1->die_mark)
7155 return die1->die_mark == die2->die_mark;
7156 die1->die_mark = die2->die_mark = ++(*mark);
7158 if (die1->die_tag != die2->die_tag)
7159 return 0;
7161 if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
7162 return 0;
7164 FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
7165 if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
7166 return 0;
7168 c1 = die1->die_child;
7169 c2 = die2->die_child;
7170 if (! c1)
7172 if (c2)
7173 return 0;
7175 else
7176 for (;;)
7178 if (!same_die_p (c1, c2, mark))
7179 return 0;
7180 c1 = c1->die_sib;
7181 c2 = c2->die_sib;
7182 if (c1 == die1->die_child)
7184 if (c2 == die2->die_child)
7185 break;
7186 else
7187 return 0;
7191 return 1;
7194 /* Do the dies look the same? Wrapper around same_die_p. */
7196 static int
7197 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
7199 int mark = 0;
7200 int ret = same_die_p (die1, die2, &mark);
7202 unmark_all_dies (die1);
7203 unmark_all_dies (die2);
7205 return ret;
7208 /* The prefix to attach to symbols on DIEs in the current comdat debug
7209 info section. */
7210 static const char *comdat_symbol_id;
7212 /* The index of the current symbol within the current comdat CU. */
7213 static unsigned int comdat_symbol_number;
7215 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
7216 children, and set comdat_symbol_id accordingly. */
7218 static void
7219 compute_section_prefix (dw_die_ref unit_die)
7221 const char *die_name = get_AT_string (unit_die, DW_AT_name);
7222 const char *base = die_name ? lbasename (die_name) : "anonymous";
7223 char *name = XALLOCAVEC (char, strlen (base) + 64);
7224 char *p;
7225 int i, mark;
7226 unsigned char checksum[16];
7227 struct md5_ctx ctx;
7229 /* Compute the checksum of the DIE, then append part of it as hex digits to
7230 the name filename of the unit. */
7232 md5_init_ctx (&ctx);
7233 mark = 0;
7234 die_checksum (unit_die, &ctx, &mark);
7235 unmark_all_dies (unit_die);
7236 md5_finish_ctx (&ctx, checksum);
7238 sprintf (name, "%s.", base);
7239 clean_symbol_name (name);
7241 p = name + strlen (name);
7242 for (i = 0; i < 4; i++)
7244 sprintf (p, "%.2x", checksum[i]);
7245 p += 2;
7248 comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
7249 comdat_symbol_number = 0;
7252 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
7254 static int
7255 is_type_die (dw_die_ref die)
7257 switch (die->die_tag)
7259 case DW_TAG_array_type:
7260 case DW_TAG_class_type:
7261 case DW_TAG_interface_type:
7262 case DW_TAG_enumeration_type:
7263 case DW_TAG_pointer_type:
7264 case DW_TAG_reference_type:
7265 case DW_TAG_rvalue_reference_type:
7266 case DW_TAG_string_type:
7267 case DW_TAG_structure_type:
7268 case DW_TAG_subroutine_type:
7269 case DW_TAG_union_type:
7270 case DW_TAG_ptr_to_member_type:
7271 case DW_TAG_set_type:
7272 case DW_TAG_subrange_type:
7273 case DW_TAG_base_type:
7274 case DW_TAG_const_type:
7275 case DW_TAG_file_type:
7276 case DW_TAG_packed_type:
7277 case DW_TAG_volatile_type:
7278 case DW_TAG_typedef:
7279 return 1;
7280 default:
7281 return 0;
7285 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
7286 Basically, we want to choose the bits that are likely to be shared between
7287 compilations (types) and leave out the bits that are specific to individual
7288 compilations (functions). */
7290 static int
7291 is_comdat_die (dw_die_ref c)
7293 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
7294 we do for stabs. The advantage is a greater likelihood of sharing between
7295 objects that don't include headers in the same order (and therefore would
7296 put the base types in a different comdat). jason 8/28/00 */
7298 if (c->die_tag == DW_TAG_base_type)
7299 return 0;
7301 if (c->die_tag == DW_TAG_pointer_type
7302 || c->die_tag == DW_TAG_reference_type
7303 || c->die_tag == DW_TAG_rvalue_reference_type
7304 || c->die_tag == DW_TAG_const_type
7305 || c->die_tag == DW_TAG_volatile_type)
7307 dw_die_ref t = get_AT_ref (c, DW_AT_type);
7309 return t ? is_comdat_die (t) : 0;
7312 return is_type_die (c);
7315 /* Returns 1 iff C is the sort of DIE that might be referred to from another
7316 compilation unit. */
7318 static int
7319 is_symbol_die (dw_die_ref c)
7321 return (is_type_die (c)
7322 || is_declaration_die (c)
7323 || c->die_tag == DW_TAG_namespace
7324 || c->die_tag == DW_TAG_module);
7327 /* Returns true iff C is a compile-unit DIE. */
7329 static inline bool
7330 is_cu_die (dw_die_ref c)
7332 return c && (c->die_tag == DW_TAG_compile_unit
7333 || c->die_tag == DW_TAG_skeleton_unit);
7336 /* Returns true iff C is a unit DIE of some sort. */
7338 static inline bool
7339 is_unit_die (dw_die_ref c)
7341 return c && (c->die_tag == DW_TAG_compile_unit
7342 || c->die_tag == DW_TAG_partial_unit
7343 || c->die_tag == DW_TAG_type_unit
7344 || c->die_tag == DW_TAG_skeleton_unit);
7347 /* Returns true iff C is a namespace DIE. */
7349 static inline bool
7350 is_namespace_die (dw_die_ref c)
7352 return c && c->die_tag == DW_TAG_namespace;
7355 /* Returns true iff C is a class or structure DIE. */
7357 static inline bool
7358 is_class_die (dw_die_ref c)
7360 return c && (c->die_tag == DW_TAG_class_type
7361 || c->die_tag == DW_TAG_structure_type);
7364 /* Return non-zero if this DIE is a template parameter. */
7366 static inline bool
7367 is_template_parameter (dw_die_ref die)
7369 switch (die->die_tag)
7371 case DW_TAG_template_type_param:
7372 case DW_TAG_template_value_param:
7373 case DW_TAG_GNU_template_template_param:
7374 case DW_TAG_GNU_template_parameter_pack:
7375 return true;
7376 default:
7377 return false;
7381 /* Return non-zero if this DIE represents a template instantiation. */
7383 static inline bool
7384 is_template_instantiation (dw_die_ref die)
7386 dw_die_ref c;
7388 if (!is_type_die (die) && die->die_tag != DW_TAG_subprogram)
7389 return false;
7390 FOR_EACH_CHILD (die, c, if (is_template_parameter (c)) return true);
7391 return false;
7394 static char *
7395 gen_internal_sym (const char *prefix)
7397 char buf[MAX_ARTIFICIAL_LABEL_BYTES];
7399 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
7400 return xstrdup (buf);
7403 /* Assign symbols to all worthy DIEs under DIE. */
7405 static void
7406 assign_symbol_names (dw_die_ref die)
7408 dw_die_ref c;
7410 if (is_symbol_die (die) && !die->comdat_type_p)
7412 if (comdat_symbol_id)
7414 char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
7416 sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
7417 comdat_symbol_id, comdat_symbol_number++);
7418 die->die_id.die_symbol = xstrdup (p);
7420 else
7421 die->die_id.die_symbol = gen_internal_sym ("LDIE");
7424 FOR_EACH_CHILD (die, c, assign_symbol_names (c));
7427 struct cu_hash_table_entry
7429 dw_die_ref cu;
7430 unsigned min_comdat_num, max_comdat_num;
7431 struct cu_hash_table_entry *next;
7434 /* Helpers to manipulate hash table of CUs. */
7436 struct cu_hash_table_entry_hasher : pointer_hash <cu_hash_table_entry>
7438 typedef die_struct *compare_type;
7439 static inline hashval_t hash (const cu_hash_table_entry *);
7440 static inline bool equal (const cu_hash_table_entry *, const die_struct *);
7441 static inline void remove (cu_hash_table_entry *);
7444 inline hashval_t
7445 cu_hash_table_entry_hasher::hash (const cu_hash_table_entry *entry)
7447 return htab_hash_string (entry->cu->die_id.die_symbol);
7450 inline bool
7451 cu_hash_table_entry_hasher::equal (const cu_hash_table_entry *entry1,
7452 const die_struct *entry2)
7454 return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
7457 inline void
7458 cu_hash_table_entry_hasher::remove (cu_hash_table_entry *entry)
7460 struct cu_hash_table_entry *next;
7462 while (entry)
7464 next = entry->next;
7465 free (entry);
7466 entry = next;
7470 typedef hash_table<cu_hash_table_entry_hasher> cu_hash_type;
7472 /* Check whether we have already seen this CU and set up SYM_NUM
7473 accordingly. */
7474 static int
7475 check_duplicate_cu (dw_die_ref cu, cu_hash_type *htable, unsigned int *sym_num)
7477 struct cu_hash_table_entry dummy;
7478 struct cu_hash_table_entry **slot, *entry, *last = &dummy;
7480 dummy.max_comdat_num = 0;
7482 slot = htable->find_slot_with_hash (cu,
7483 htab_hash_string (cu->die_id.die_symbol),
7484 INSERT);
7485 entry = *slot;
7487 for (; entry; last = entry, entry = entry->next)
7489 if (same_die_p_wrap (cu, entry->cu))
7490 break;
7493 if (entry)
7495 *sym_num = entry->min_comdat_num;
7496 return 1;
7499 entry = XCNEW (struct cu_hash_table_entry);
7500 entry->cu = cu;
7501 entry->min_comdat_num = *sym_num = last->max_comdat_num;
7502 entry->next = *slot;
7503 *slot = entry;
7505 return 0;
7508 /* Record SYM_NUM to record of CU in HTABLE. */
7509 static void
7510 record_comdat_symbol_number (dw_die_ref cu, cu_hash_type *htable,
7511 unsigned int sym_num)
7513 struct cu_hash_table_entry **slot, *entry;
7515 slot = htable->find_slot_with_hash (cu,
7516 htab_hash_string (cu->die_id.die_symbol),
7517 NO_INSERT);
7518 entry = *slot;
7520 entry->max_comdat_num = sym_num;
7523 /* Traverse the DIE (which is always comp_unit_die), and set up
7524 additional compilation units for each of the include files we see
7525 bracketed by BINCL/EINCL. */
7527 static void
7528 break_out_includes (dw_die_ref die)
7530 dw_die_ref c;
7531 dw_die_ref unit = NULL;
7532 limbo_die_node *node, **pnode;
7534 c = die->die_child;
7535 if (c) do {
7536 dw_die_ref prev = c;
7537 c = c->die_sib;
7538 while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
7539 || (unit && is_comdat_die (c)))
7541 dw_die_ref next = c->die_sib;
7543 /* This DIE is for a secondary CU; remove it from the main one. */
7544 remove_child_with_prev (c, prev);
7546 if (c->die_tag == DW_TAG_GNU_BINCL)
7547 unit = push_new_compile_unit (unit, c);
7548 else if (c->die_tag == DW_TAG_GNU_EINCL)
7549 unit = pop_compile_unit (unit);
7550 else
7551 add_child_die (unit, c);
7552 c = next;
7553 if (c == die->die_child)
7554 break;
7556 } while (c != die->die_child);
7558 #if 0
7559 /* We can only use this in debugging, since the frontend doesn't check
7560 to make sure that we leave every include file we enter. */
7561 gcc_assert (!unit);
7562 #endif
7564 assign_symbol_names (die);
7565 cu_hash_type cu_hash_table (10);
7566 for (node = limbo_die_list, pnode = &limbo_die_list;
7567 node;
7568 node = node->next)
7570 int is_dupl;
7572 compute_section_prefix (node->die);
7573 is_dupl = check_duplicate_cu (node->die, &cu_hash_table,
7574 &comdat_symbol_number);
7575 assign_symbol_names (node->die);
7576 if (is_dupl)
7577 *pnode = node->next;
7578 else
7580 pnode = &node->next;
7581 record_comdat_symbol_number (node->die, &cu_hash_table,
7582 comdat_symbol_number);
7587 /* Return non-zero if this DIE is a declaration. */
7589 static int
7590 is_declaration_die (dw_die_ref die)
7592 dw_attr_node *a;
7593 unsigned ix;
7595 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7596 if (a->dw_attr == DW_AT_declaration)
7597 return 1;
7599 return 0;
7602 /* Return non-zero if this DIE is nested inside a subprogram. */
7604 static int
7605 is_nested_in_subprogram (dw_die_ref die)
7607 dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
7609 if (decl == NULL)
7610 decl = die;
7611 return local_scope_p (decl);
7614 /* Return non-zero if this DIE contains a defining declaration of a
7615 subprogram. */
7617 static int
7618 contains_subprogram_definition (dw_die_ref die)
7620 dw_die_ref c;
7622 if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
7623 return 1;
7624 FOR_EACH_CHILD (die, c, if (contains_subprogram_definition (c)) return 1);
7625 return 0;
7628 /* Return non-zero if this is a type DIE that should be moved to a
7629 COMDAT .debug_types section or .debug_info section with DW_UT_*type
7630 unit type. */
7632 static int
7633 should_move_die_to_comdat (dw_die_ref die)
7635 switch (die->die_tag)
7637 case DW_TAG_class_type:
7638 case DW_TAG_structure_type:
7639 case DW_TAG_enumeration_type:
7640 case DW_TAG_union_type:
7641 /* Don't move declarations, inlined instances, types nested in a
7642 subprogram, or types that contain subprogram definitions. */
7643 if (is_declaration_die (die)
7644 || get_AT (die, DW_AT_abstract_origin)
7645 || is_nested_in_subprogram (die)
7646 || contains_subprogram_definition (die))
7647 return 0;
7648 return 1;
7649 case DW_TAG_array_type:
7650 case DW_TAG_interface_type:
7651 case DW_TAG_pointer_type:
7652 case DW_TAG_reference_type:
7653 case DW_TAG_rvalue_reference_type:
7654 case DW_TAG_string_type:
7655 case DW_TAG_subroutine_type:
7656 case DW_TAG_ptr_to_member_type:
7657 case DW_TAG_set_type:
7658 case DW_TAG_subrange_type:
7659 case DW_TAG_base_type:
7660 case DW_TAG_const_type:
7661 case DW_TAG_file_type:
7662 case DW_TAG_packed_type:
7663 case DW_TAG_volatile_type:
7664 case DW_TAG_typedef:
7665 default:
7666 return 0;
7670 /* Make a clone of DIE. */
7672 static dw_die_ref
7673 clone_die (dw_die_ref die)
7675 dw_die_ref clone;
7676 dw_attr_node *a;
7677 unsigned ix;
7679 clone = ggc_cleared_alloc<die_node> ();
7680 clone->die_tag = die->die_tag;
7682 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7683 add_dwarf_attr (clone, a);
7685 return clone;
7688 /* Make a clone of the tree rooted at DIE. */
7690 static dw_die_ref
7691 clone_tree (dw_die_ref die)
7693 dw_die_ref c;
7694 dw_die_ref clone = clone_die (die);
7696 FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree (c)));
7698 return clone;
7701 /* Make a clone of DIE as a declaration. */
7703 static dw_die_ref
7704 clone_as_declaration (dw_die_ref die)
7706 dw_die_ref clone;
7707 dw_die_ref decl;
7708 dw_attr_node *a;
7709 unsigned ix;
7711 /* If the DIE is already a declaration, just clone it. */
7712 if (is_declaration_die (die))
7713 return clone_die (die);
7715 /* If the DIE is a specification, just clone its declaration DIE. */
7716 decl = get_AT_ref (die, DW_AT_specification);
7717 if (decl != NULL)
7719 clone = clone_die (decl);
7720 if (die->comdat_type_p)
7721 add_AT_die_ref (clone, DW_AT_signature, die);
7722 return clone;
7725 clone = ggc_cleared_alloc<die_node> ();
7726 clone->die_tag = die->die_tag;
7728 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7730 /* We don't want to copy over all attributes.
7731 For example we don't want DW_AT_byte_size because otherwise we will no
7732 longer have a declaration and GDB will treat it as a definition. */
7734 switch (a->dw_attr)
7736 case DW_AT_abstract_origin:
7737 case DW_AT_artificial:
7738 case DW_AT_containing_type:
7739 case DW_AT_external:
7740 case DW_AT_name:
7741 case DW_AT_type:
7742 case DW_AT_virtuality:
7743 case DW_AT_linkage_name:
7744 case DW_AT_MIPS_linkage_name:
7745 add_dwarf_attr (clone, a);
7746 break;
7747 case DW_AT_byte_size:
7748 case DW_AT_alignment:
7749 default:
7750 break;
7754 if (die->comdat_type_p)
7755 add_AT_die_ref (clone, DW_AT_signature, die);
7757 add_AT_flag (clone, DW_AT_declaration, 1);
7758 return clone;
7762 /* Structure to map a DIE in one CU to its copy in a comdat type unit. */
7764 struct decl_table_entry
7766 dw_die_ref orig;
7767 dw_die_ref copy;
7770 /* Helpers to manipulate hash table of copied declarations. */
7772 /* Hashtable helpers. */
7774 struct decl_table_entry_hasher : free_ptr_hash <decl_table_entry>
7776 typedef die_struct *compare_type;
7777 static inline hashval_t hash (const decl_table_entry *);
7778 static inline bool equal (const decl_table_entry *, const die_struct *);
7781 inline hashval_t
7782 decl_table_entry_hasher::hash (const decl_table_entry *entry)
7784 return htab_hash_pointer (entry->orig);
7787 inline bool
7788 decl_table_entry_hasher::equal (const decl_table_entry *entry1,
7789 const die_struct *entry2)
7791 return entry1->orig == entry2;
7794 typedef hash_table<decl_table_entry_hasher> decl_hash_type;
7796 /* Copy DIE and its ancestors, up to, but not including, the compile unit
7797 or type unit entry, to a new tree. Adds the new tree to UNIT and returns
7798 a pointer to the copy of DIE. If DECL_TABLE is provided, it is used
7799 to check if the ancestor has already been copied into UNIT. */
7801 static dw_die_ref
7802 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die,
7803 decl_hash_type *decl_table)
7805 dw_die_ref parent = die->die_parent;
7806 dw_die_ref new_parent = unit;
7807 dw_die_ref copy;
7808 decl_table_entry **slot = NULL;
7809 struct decl_table_entry *entry = NULL;
7811 if (decl_table)
7813 /* Check if the entry has already been copied to UNIT. */
7814 slot = decl_table->find_slot_with_hash (die, htab_hash_pointer (die),
7815 INSERT);
7816 if (*slot != HTAB_EMPTY_ENTRY)
7818 entry = *slot;
7819 return entry->copy;
7822 /* Record in DECL_TABLE that DIE has been copied to UNIT. */
7823 entry = XCNEW (struct decl_table_entry);
7824 entry->orig = die;
7825 entry->copy = NULL;
7826 *slot = entry;
7829 if (parent != NULL)
7831 dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
7832 if (spec != NULL)
7833 parent = spec;
7834 if (!is_unit_die (parent))
7835 new_parent = copy_ancestor_tree (unit, parent, decl_table);
7838 copy = clone_as_declaration (die);
7839 add_child_die (new_parent, copy);
7841 if (decl_table)
7843 /* Record the pointer to the copy. */
7844 entry->copy = copy;
7847 return copy;
7849 /* Copy the declaration context to the new type unit DIE. This includes
7850 any surrounding namespace or type declarations. If the DIE has an
7851 AT_specification attribute, it also includes attributes and children
7852 attached to the specification, and returns a pointer to the original
7853 parent of the declaration DIE. Returns NULL otherwise. */
7855 static dw_die_ref
7856 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
7858 dw_die_ref decl;
7859 dw_die_ref new_decl;
7860 dw_die_ref orig_parent = NULL;
7862 decl = get_AT_ref (die, DW_AT_specification);
7863 if (decl == NULL)
7864 decl = die;
7865 else
7867 unsigned ix;
7868 dw_die_ref c;
7869 dw_attr_node *a;
7871 /* The original DIE will be changed to a declaration, and must
7872 be moved to be a child of the original declaration DIE. */
7873 orig_parent = decl->die_parent;
7875 /* Copy the type node pointer from the new DIE to the original
7876 declaration DIE so we can forward references later. */
7877 decl->comdat_type_p = true;
7878 decl->die_id.die_type_node = die->die_id.die_type_node;
7880 remove_AT (die, DW_AT_specification);
7882 FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
7884 if (a->dw_attr != DW_AT_name
7885 && a->dw_attr != DW_AT_declaration
7886 && a->dw_attr != DW_AT_external)
7887 add_dwarf_attr (die, a);
7890 FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree (c)));
7893 if (decl->die_parent != NULL
7894 && !is_unit_die (decl->die_parent))
7896 new_decl = copy_ancestor_tree (unit, decl, NULL);
7897 if (new_decl != NULL)
7899 remove_AT (new_decl, DW_AT_signature);
7900 add_AT_specification (die, new_decl);
7904 return orig_parent;
7907 /* Generate the skeleton ancestor tree for the given NODE, then clone
7908 the DIE and add the clone into the tree. */
7910 static void
7911 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
7913 if (node->new_die != NULL)
7914 return;
7916 node->new_die = clone_as_declaration (node->old_die);
7918 if (node->parent != NULL)
7920 generate_skeleton_ancestor_tree (node->parent);
7921 add_child_die (node->parent->new_die, node->new_die);
7925 /* Generate a skeleton tree of DIEs containing any declarations that are
7926 found in the original tree. We traverse the tree looking for declaration
7927 DIEs, and construct the skeleton from the bottom up whenever we find one. */
7929 static void
7930 generate_skeleton_bottom_up (skeleton_chain_node *parent)
7932 skeleton_chain_node node;
7933 dw_die_ref c;
7934 dw_die_ref first;
7935 dw_die_ref prev = NULL;
7936 dw_die_ref next = NULL;
7938 node.parent = parent;
7940 first = c = parent->old_die->die_child;
7941 if (c)
7942 next = c->die_sib;
7943 if (c) do {
7944 if (prev == NULL || prev->die_sib == c)
7945 prev = c;
7946 c = next;
7947 next = (c == first ? NULL : c->die_sib);
7948 node.old_die = c;
7949 node.new_die = NULL;
7950 if (is_declaration_die (c))
7952 if (is_template_instantiation (c))
7954 /* Instantiated templates do not need to be cloned into the
7955 type unit. Just move the DIE and its children back to
7956 the skeleton tree (in the main CU). */
7957 remove_child_with_prev (c, prev);
7958 add_child_die (parent->new_die, c);
7959 c = prev;
7961 else if (c->comdat_type_p)
7963 /* This is the skeleton of earlier break_out_comdat_types
7964 type. Clone the existing DIE, but keep the children
7965 under the original (which is in the main CU). */
7966 dw_die_ref clone = clone_die (c);
7968 replace_child (c, clone, prev);
7969 generate_skeleton_ancestor_tree (parent);
7970 add_child_die (parent->new_die, c);
7971 c = clone;
7972 continue;
7974 else
7976 /* Clone the existing DIE, move the original to the skeleton
7977 tree (which is in the main CU), and put the clone, with
7978 all the original's children, where the original came from
7979 (which is about to be moved to the type unit). */
7980 dw_die_ref clone = clone_die (c);
7981 move_all_children (c, clone);
7983 /* If the original has a DW_AT_object_pointer attribute,
7984 it would now point to a child DIE just moved to the
7985 cloned tree, so we need to remove that attribute from
7986 the original. */
7987 remove_AT (c, DW_AT_object_pointer);
7989 replace_child (c, clone, prev);
7990 generate_skeleton_ancestor_tree (parent);
7991 add_child_die (parent->new_die, c);
7992 node.old_die = clone;
7993 node.new_die = c;
7994 c = clone;
7997 generate_skeleton_bottom_up (&node);
7998 } while (next != NULL);
8001 /* Wrapper function for generate_skeleton_bottom_up. */
8003 static dw_die_ref
8004 generate_skeleton (dw_die_ref die)
8006 skeleton_chain_node node;
8008 node.old_die = die;
8009 node.new_die = NULL;
8010 node.parent = NULL;
8012 /* If this type definition is nested inside another type,
8013 and is not an instantiation of a template, always leave
8014 at least a declaration in its place. */
8015 if (die->die_parent != NULL
8016 && is_type_die (die->die_parent)
8017 && !is_template_instantiation (die))
8018 node.new_die = clone_as_declaration (die);
8020 generate_skeleton_bottom_up (&node);
8021 return node.new_die;
8024 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
8025 declaration. The original DIE is moved to a new compile unit so that
8026 existing references to it follow it to the new location. If any of the
8027 original DIE's descendants is a declaration, we need to replace the
8028 original DIE with a skeleton tree and move the declarations back into the
8029 skeleton tree. */
8031 static dw_die_ref
8032 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
8033 dw_die_ref prev)
8035 dw_die_ref skeleton, orig_parent;
8037 /* Copy the declaration context to the type unit DIE. If the returned
8038 ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
8039 that DIE. */
8040 orig_parent = copy_declaration_context (unit, child);
8042 skeleton = generate_skeleton (child);
8043 if (skeleton == NULL)
8044 remove_child_with_prev (child, prev);
8045 else
8047 skeleton->comdat_type_p = true;
8048 skeleton->die_id.die_type_node = child->die_id.die_type_node;
8050 /* If the original DIE was a specification, we need to put
8051 the skeleton under the parent DIE of the declaration.
8052 This leaves the original declaration in the tree, but
8053 it will be pruned later since there are no longer any
8054 references to it. */
8055 if (orig_parent != NULL)
8057 remove_child_with_prev (child, prev);
8058 add_child_die (orig_parent, skeleton);
8060 else
8061 replace_child (child, skeleton, prev);
8064 return skeleton;
8067 static void
8068 copy_dwarf_procs_ref_in_attrs (dw_die_ref die,
8069 comdat_type_node *type_node,
8070 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs);
8072 /* Helper for copy_dwarf_procs_ref_in_dies. Make a copy of the DIE DWARF
8073 procedure, put it under TYPE_NODE and return the copy. Continue looking for
8074 DWARF procedure references in the DW_AT_location attribute. */
8076 static dw_die_ref
8077 copy_dwarf_procedure (dw_die_ref die,
8078 comdat_type_node *type_node,
8079 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8081 gcc_assert (die->die_tag == DW_TAG_dwarf_procedure);
8083 /* DWARF procedures are not supposed to have children... */
8084 gcc_assert (die->die_child == NULL);
8086 /* ... and they are supposed to have only one attribute: DW_AT_location. */
8087 gcc_assert (vec_safe_length (die->die_attr) == 1
8088 && ((*die->die_attr)[0].dw_attr == DW_AT_location));
8090 /* Do not copy more than once DWARF procedures. */
8091 bool existed;
8092 dw_die_ref &die_copy = copied_dwarf_procs.get_or_insert (die, &existed);
8093 if (existed)
8094 return die_copy;
8096 die_copy = clone_die (die);
8097 add_child_die (type_node->root_die, die_copy);
8098 copy_dwarf_procs_ref_in_attrs (die_copy, type_node, copied_dwarf_procs);
8099 return die_copy;
8102 /* Helper for copy_dwarf_procs_ref_in_dies. Look for references to DWARF
8103 procedures in DIE's attributes. */
8105 static void
8106 copy_dwarf_procs_ref_in_attrs (dw_die_ref die,
8107 comdat_type_node *type_node,
8108 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8110 dw_attr_node *a;
8111 unsigned i;
8113 FOR_EACH_VEC_SAFE_ELT (die->die_attr, i, a)
8115 dw_loc_descr_ref loc;
8117 if (a->dw_attr_val.val_class != dw_val_class_loc)
8118 continue;
8120 for (loc = a->dw_attr_val.v.val_loc; loc != NULL; loc = loc->dw_loc_next)
8122 switch (loc->dw_loc_opc)
8124 case DW_OP_call2:
8125 case DW_OP_call4:
8126 case DW_OP_call_ref:
8127 gcc_assert (loc->dw_loc_oprnd1.val_class
8128 == dw_val_class_die_ref);
8129 loc->dw_loc_oprnd1.v.val_die_ref.die
8130 = copy_dwarf_procedure (loc->dw_loc_oprnd1.v.val_die_ref.die,
8131 type_node,
8132 copied_dwarf_procs);
8134 default:
8135 break;
8141 /* Copy DWARF procedures that are referenced by the DIE tree to TREE_NODE and
8142 rewrite references to point to the copies.
8144 References are looked for in DIE's attributes and recursively in all its
8145 children attributes that are location descriptions. COPIED_DWARF_PROCS is a
8146 mapping from old DWARF procedures to their copy. It is used not to copy
8147 twice the same DWARF procedure under TYPE_NODE. */
8149 static void
8150 copy_dwarf_procs_ref_in_dies (dw_die_ref die,
8151 comdat_type_node *type_node,
8152 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8154 dw_die_ref c;
8156 copy_dwarf_procs_ref_in_attrs (die, type_node, copied_dwarf_procs);
8157 FOR_EACH_CHILD (die, c, copy_dwarf_procs_ref_in_dies (c,
8158 type_node,
8159 copied_dwarf_procs));
8162 /* Traverse the DIE and set up additional .debug_types or .debug_info
8163 DW_UT_*type sections for each type worthy of being placed in a COMDAT
8164 section. */
8166 static void
8167 break_out_comdat_types (dw_die_ref die)
8169 dw_die_ref c;
8170 dw_die_ref first;
8171 dw_die_ref prev = NULL;
8172 dw_die_ref next = NULL;
8173 dw_die_ref unit = NULL;
8175 first = c = die->die_child;
8176 if (c)
8177 next = c->die_sib;
8178 if (c) do {
8179 if (prev == NULL || prev->die_sib == c)
8180 prev = c;
8181 c = next;
8182 next = (c == first ? NULL : c->die_sib);
8183 if (should_move_die_to_comdat (c))
8185 dw_die_ref replacement;
8186 comdat_type_node *type_node;
8188 /* Break out nested types into their own type units. */
8189 break_out_comdat_types (c);
8191 /* Create a new type unit DIE as the root for the new tree, and
8192 add it to the list of comdat types. */
8193 unit = new_die (DW_TAG_type_unit, NULL, NULL);
8194 add_AT_unsigned (unit, DW_AT_language,
8195 get_AT_unsigned (comp_unit_die (), DW_AT_language));
8196 type_node = ggc_cleared_alloc<comdat_type_node> ();
8197 type_node->root_die = unit;
8198 type_node->next = comdat_type_list;
8199 comdat_type_list = type_node;
8201 /* Generate the type signature. */
8202 generate_type_signature (c, type_node);
8204 /* Copy the declaration context, attributes, and children of the
8205 declaration into the new type unit DIE, then remove this DIE
8206 from the main CU (or replace it with a skeleton if necessary). */
8207 replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
8208 type_node->skeleton_die = replacement;
8210 /* Add the DIE to the new compunit. */
8211 add_child_die (unit, c);
8213 /* Types can reference DWARF procedures for type size or data location
8214 expressions. Calls in DWARF expressions cannot target procedures
8215 that are not in the same section. So we must copy DWARF procedures
8216 along with this type and then rewrite references to them. */
8217 hash_map<dw_die_ref, dw_die_ref> copied_dwarf_procs;
8218 copy_dwarf_procs_ref_in_dies (c, type_node, copied_dwarf_procs);
8220 if (replacement != NULL)
8221 c = replacement;
8223 else if (c->die_tag == DW_TAG_namespace
8224 || c->die_tag == DW_TAG_class_type
8225 || c->die_tag == DW_TAG_structure_type
8226 || c->die_tag == DW_TAG_union_type)
8228 /* Look for nested types that can be broken out. */
8229 break_out_comdat_types (c);
8231 } while (next != NULL);
8234 /* Like clone_tree, but copy DW_TAG_subprogram DIEs as declarations.
8235 Enter all the cloned children into the hash table decl_table. */
8237 static dw_die_ref
8238 clone_tree_partial (dw_die_ref die, decl_hash_type *decl_table)
8240 dw_die_ref c;
8241 dw_die_ref clone;
8242 struct decl_table_entry *entry;
8243 decl_table_entry **slot;
8245 if (die->die_tag == DW_TAG_subprogram)
8246 clone = clone_as_declaration (die);
8247 else
8248 clone = clone_die (die);
8250 slot = decl_table->find_slot_with_hash (die,
8251 htab_hash_pointer (die), INSERT);
8253 /* Assert that DIE isn't in the hash table yet. If it would be there
8254 before, the ancestors would be necessarily there as well, therefore
8255 clone_tree_partial wouldn't be called. */
8256 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
8258 entry = XCNEW (struct decl_table_entry);
8259 entry->orig = die;
8260 entry->copy = clone;
8261 *slot = entry;
8263 if (die->die_tag != DW_TAG_subprogram)
8264 FOR_EACH_CHILD (die, c,
8265 add_child_die (clone, clone_tree_partial (c, decl_table)));
8267 return clone;
8270 /* Walk the DIE and its children, looking for references to incomplete
8271 or trivial types that are unmarked (i.e., that are not in the current
8272 type_unit). */
8274 static void
8275 copy_decls_walk (dw_die_ref unit, dw_die_ref die, decl_hash_type *decl_table)
8277 dw_die_ref c;
8278 dw_attr_node *a;
8279 unsigned ix;
8281 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8283 if (AT_class (a) == dw_val_class_die_ref)
8285 dw_die_ref targ = AT_ref (a);
8286 decl_table_entry **slot;
8287 struct decl_table_entry *entry;
8289 if (targ->die_mark != 0 || targ->comdat_type_p)
8290 continue;
8292 slot = decl_table->find_slot_with_hash (targ,
8293 htab_hash_pointer (targ),
8294 INSERT);
8296 if (*slot != HTAB_EMPTY_ENTRY)
8298 /* TARG has already been copied, so we just need to
8299 modify the reference to point to the copy. */
8300 entry = *slot;
8301 a->dw_attr_val.v.val_die_ref.die = entry->copy;
8303 else
8305 dw_die_ref parent = unit;
8306 dw_die_ref copy = clone_die (targ);
8308 /* Record in DECL_TABLE that TARG has been copied.
8309 Need to do this now, before the recursive call,
8310 because DECL_TABLE may be expanded and SLOT
8311 would no longer be a valid pointer. */
8312 entry = XCNEW (struct decl_table_entry);
8313 entry->orig = targ;
8314 entry->copy = copy;
8315 *slot = entry;
8317 /* If TARG is not a declaration DIE, we need to copy its
8318 children. */
8319 if (!is_declaration_die (targ))
8321 FOR_EACH_CHILD (
8322 targ, c,
8323 add_child_die (copy,
8324 clone_tree_partial (c, decl_table)));
8327 /* Make sure the cloned tree is marked as part of the
8328 type unit. */
8329 mark_dies (copy);
8331 /* If TARG has surrounding context, copy its ancestor tree
8332 into the new type unit. */
8333 if (targ->die_parent != NULL
8334 && !is_unit_die (targ->die_parent))
8335 parent = copy_ancestor_tree (unit, targ->die_parent,
8336 decl_table);
8338 add_child_die (parent, copy);
8339 a->dw_attr_val.v.val_die_ref.die = copy;
8341 /* Make sure the newly-copied DIE is walked. If it was
8342 installed in a previously-added context, it won't
8343 get visited otherwise. */
8344 if (parent != unit)
8346 /* Find the highest point of the newly-added tree,
8347 mark each node along the way, and walk from there. */
8348 parent->die_mark = 1;
8349 while (parent->die_parent
8350 && parent->die_parent->die_mark == 0)
8352 parent = parent->die_parent;
8353 parent->die_mark = 1;
8355 copy_decls_walk (unit, parent, decl_table);
8361 FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
8364 /* Copy declarations for "unworthy" types into the new comdat section.
8365 Incomplete types, modified types, and certain other types aren't broken
8366 out into comdat sections of their own, so they don't have a signature,
8367 and we need to copy the declaration into the same section so that we
8368 don't have an external reference. */
8370 static void
8371 copy_decls_for_unworthy_types (dw_die_ref unit)
8373 mark_dies (unit);
8374 decl_hash_type decl_table (10);
8375 copy_decls_walk (unit, unit, &decl_table);
8376 unmark_dies (unit);
8379 /* Traverse the DIE and add a sibling attribute if it may have the
8380 effect of speeding up access to siblings. To save some space,
8381 avoid generating sibling attributes for DIE's without children. */
8383 static void
8384 add_sibling_attributes (dw_die_ref die)
8386 dw_die_ref c;
8388 if (! die->die_child)
8389 return;
8391 if (die->die_parent && die != die->die_parent->die_child)
8392 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
8394 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
8397 /* Output all location lists for the DIE and its children. */
8399 static void
8400 output_location_lists (dw_die_ref die)
8402 dw_die_ref c;
8403 dw_attr_node *a;
8404 unsigned ix;
8406 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8407 if (AT_class (a) == dw_val_class_loc_list)
8408 output_loc_list (AT_loc_list (a));
8410 FOR_EACH_CHILD (die, c, output_location_lists (c));
8413 /* During assign_location_list_indexes and output_loclists_offset the
8414 current index, after it the number of assigned indexes (i.e. how
8415 large the .debug_loclists* offset table should be). */
8416 static unsigned int loc_list_idx;
8418 /* Output all location list offsets for the DIE and its children. */
8420 static void
8421 output_loclists_offsets (dw_die_ref die)
8423 dw_die_ref c;
8424 dw_attr_node *a;
8425 unsigned ix;
8427 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8428 if (AT_class (a) == dw_val_class_loc_list)
8430 dw_loc_list_ref l = AT_loc_list (a);
8431 if (l->offset_emitted)
8432 continue;
8433 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l->ll_symbol,
8434 loc_section_label, NULL);
8435 gcc_assert (l->hash == loc_list_idx);
8436 loc_list_idx++;
8437 l->offset_emitted = true;
8440 FOR_EACH_CHILD (die, c, output_loclists_offsets (c));
8443 /* Recursively set indexes of location lists. */
8445 static void
8446 assign_location_list_indexes (dw_die_ref die)
8448 dw_die_ref c;
8449 dw_attr_node *a;
8450 unsigned ix;
8452 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8453 if (AT_class (a) == dw_val_class_loc_list)
8455 dw_loc_list_ref list = AT_loc_list (a);
8456 if (!list->num_assigned)
8458 list->num_assigned = true;
8459 list->hash = loc_list_idx++;
8463 FOR_EACH_CHILD (die, c, assign_location_list_indexes (c));
8466 /* We want to limit the number of external references, because they are
8467 larger than local references: a relocation takes multiple words, and
8468 even a sig8 reference is always eight bytes, whereas a local reference
8469 can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
8470 So if we encounter multiple external references to the same type DIE, we
8471 make a local typedef stub for it and redirect all references there.
8473 This is the element of the hash table for keeping track of these
8474 references. */
8476 struct external_ref
8478 dw_die_ref type;
8479 dw_die_ref stub;
8480 unsigned n_refs;
8483 /* Hashtable helpers. */
8485 struct external_ref_hasher : free_ptr_hash <external_ref>
8487 static inline hashval_t hash (const external_ref *);
8488 static inline bool equal (const external_ref *, const external_ref *);
8491 inline hashval_t
8492 external_ref_hasher::hash (const external_ref *r)
8494 dw_die_ref die = r->type;
8495 hashval_t h = 0;
8497 /* We can't use the address of the DIE for hashing, because
8498 that will make the order of the stub DIEs non-deterministic. */
8499 if (! die->comdat_type_p)
8500 /* We have a symbol; use it to compute a hash. */
8501 h = htab_hash_string (die->die_id.die_symbol);
8502 else
8504 /* We have a type signature; use a subset of the bits as the hash.
8505 The 8-byte signature is at least as large as hashval_t. */
8506 comdat_type_node *type_node = die->die_id.die_type_node;
8507 memcpy (&h, type_node->signature, sizeof (h));
8509 return h;
8512 inline bool
8513 external_ref_hasher::equal (const external_ref *r1, const external_ref *r2)
8515 return r1->type == r2->type;
8518 typedef hash_table<external_ref_hasher> external_ref_hash_type;
8520 /* Return a pointer to the external_ref for references to DIE. */
8522 static struct external_ref *
8523 lookup_external_ref (external_ref_hash_type *map, dw_die_ref die)
8525 struct external_ref ref, *ref_p;
8526 external_ref **slot;
8528 ref.type = die;
8529 slot = map->find_slot (&ref, INSERT);
8530 if (*slot != HTAB_EMPTY_ENTRY)
8531 return *slot;
8533 ref_p = XCNEW (struct external_ref);
8534 ref_p->type = die;
8535 *slot = ref_p;
8536 return ref_p;
8539 /* Subroutine of optimize_external_refs, below.
8541 If we see a type skeleton, record it as our stub. If we see external
8542 references, remember how many we've seen. */
8544 static void
8545 optimize_external_refs_1 (dw_die_ref die, external_ref_hash_type *map)
8547 dw_die_ref c;
8548 dw_attr_node *a;
8549 unsigned ix;
8550 struct external_ref *ref_p;
8552 if (is_type_die (die)
8553 && (c = get_AT_ref (die, DW_AT_signature)))
8555 /* This is a local skeleton; use it for local references. */
8556 ref_p = lookup_external_ref (map, c);
8557 ref_p->stub = die;
8560 /* Scan the DIE references, and remember any that refer to DIEs from
8561 other CUs (i.e. those which are not marked). */
8562 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8563 if (AT_class (a) == dw_val_class_die_ref
8564 && (c = AT_ref (a))->die_mark == 0
8565 && is_type_die (c))
8567 ref_p = lookup_external_ref (map, c);
8568 ref_p->n_refs++;
8571 FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
8574 /* htab_traverse callback function for optimize_external_refs, below. SLOT
8575 points to an external_ref, DATA is the CU we're processing. If we don't
8576 already have a local stub, and we have multiple refs, build a stub. */
8579 dwarf2_build_local_stub (external_ref **slot, dw_die_ref data)
8581 struct external_ref *ref_p = *slot;
8583 if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
8585 /* We have multiple references to this type, so build a small stub.
8586 Both of these forms are a bit dodgy from the perspective of the
8587 DWARF standard, since technically they should have names. */
8588 dw_die_ref cu = data;
8589 dw_die_ref type = ref_p->type;
8590 dw_die_ref stub = NULL;
8592 if (type->comdat_type_p)
8594 /* If we refer to this type via sig8, use AT_signature. */
8595 stub = new_die (type->die_tag, cu, NULL_TREE);
8596 add_AT_die_ref (stub, DW_AT_signature, type);
8598 else
8600 /* Otherwise, use a typedef with no name. */
8601 stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
8602 add_AT_die_ref (stub, DW_AT_type, type);
8605 stub->die_mark++;
8606 ref_p->stub = stub;
8608 return 1;
8611 /* DIE is a unit; look through all the DIE references to see if there are
8612 any external references to types, and if so, create local stubs for
8613 them which will be applied in build_abbrev_table. This is useful because
8614 references to local DIEs are smaller. */
8616 static external_ref_hash_type *
8617 optimize_external_refs (dw_die_ref die)
8619 external_ref_hash_type *map = new external_ref_hash_type (10);
8620 optimize_external_refs_1 (die, map);
8621 map->traverse <dw_die_ref, dwarf2_build_local_stub> (die);
8622 return map;
8625 /* The following 3 variables are temporaries that are computed only during the
8626 build_abbrev_table call and used and released during the following
8627 optimize_abbrev_table call. */
8629 /* First abbrev_id that can be optimized based on usage. */
8630 static unsigned int abbrev_opt_start;
8632 /* Maximum abbrev_id of a base type plus one (we can't optimize DIEs with
8633 abbrev_id smaller than this, because they must be already sized
8634 during build_abbrev_table). */
8635 static unsigned int abbrev_opt_base_type_end;
8637 /* Vector of usage counts during build_abbrev_table. Indexed by
8638 abbrev_id - abbrev_opt_start. */
8639 static vec<unsigned int> abbrev_usage_count;
8641 /* Vector of all DIEs added with die_abbrev >= abbrev_opt_start. */
8642 static vec<dw_die_ref> sorted_abbrev_dies;
8644 /* The format of each DIE (and its attribute value pairs) is encoded in an
8645 abbreviation table. This routine builds the abbreviation table and assigns
8646 a unique abbreviation id for each abbreviation entry. The children of each
8647 die are visited recursively. */
8649 static void
8650 build_abbrev_table (dw_die_ref die, external_ref_hash_type *extern_map)
8652 unsigned int abbrev_id = 0;
8653 dw_die_ref c;
8654 dw_attr_node *a;
8655 unsigned ix;
8656 dw_die_ref abbrev;
8658 /* Scan the DIE references, and replace any that refer to
8659 DIEs from other CUs (i.e. those which are not marked) with
8660 the local stubs we built in optimize_external_refs. */
8661 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8662 if (AT_class (a) == dw_val_class_die_ref
8663 && (c = AT_ref (a))->die_mark == 0)
8665 struct external_ref *ref_p;
8666 gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
8668 ref_p = lookup_external_ref (extern_map, c);
8669 if (ref_p->stub && ref_p->stub != die)
8670 change_AT_die_ref (a, ref_p->stub);
8671 else
8672 /* We aren't changing this reference, so mark it external. */
8673 set_AT_ref_external (a, 1);
8676 FOR_EACH_VEC_SAFE_ELT (abbrev_die_table, abbrev_id, abbrev)
8678 dw_attr_node *die_a, *abbrev_a;
8679 unsigned ix;
8680 bool ok = true;
8682 if (abbrev_id == 0)
8683 continue;
8684 if (abbrev->die_tag != die->die_tag)
8685 continue;
8686 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
8687 continue;
8689 if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
8690 continue;
8692 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
8694 abbrev_a = &(*abbrev->die_attr)[ix];
8695 if ((abbrev_a->dw_attr != die_a->dw_attr)
8696 || (value_format (abbrev_a) != value_format (die_a)))
8698 ok = false;
8699 break;
8702 if (ok)
8703 break;
8706 if (abbrev_id >= vec_safe_length (abbrev_die_table))
8708 vec_safe_push (abbrev_die_table, die);
8709 if (abbrev_opt_start)
8710 abbrev_usage_count.safe_push (0);
8712 if (abbrev_opt_start && abbrev_id >= abbrev_opt_start)
8714 abbrev_usage_count[abbrev_id - abbrev_opt_start]++;
8715 sorted_abbrev_dies.safe_push (die);
8718 die->die_abbrev = abbrev_id;
8719 FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
8722 /* Callback function for sorted_abbrev_dies vector sorting. We sort
8723 by die_abbrev's usage count, from the most commonly used
8724 abbreviation to the least. */
8726 static int
8727 die_abbrev_cmp (const void *p1, const void *p2)
8729 dw_die_ref die1 = *(const dw_die_ref *) p1;
8730 dw_die_ref die2 = *(const dw_die_ref *) p2;
8732 gcc_checking_assert (die1->die_abbrev >= abbrev_opt_start);
8733 gcc_checking_assert (die2->die_abbrev >= abbrev_opt_start);
8735 if (die1->die_abbrev >= abbrev_opt_base_type_end
8736 && die2->die_abbrev >= abbrev_opt_base_type_end)
8738 if (abbrev_usage_count[die1->die_abbrev - abbrev_opt_start]
8739 > abbrev_usage_count[die2->die_abbrev - abbrev_opt_start])
8740 return -1;
8741 if (abbrev_usage_count[die1->die_abbrev - abbrev_opt_start]
8742 < abbrev_usage_count[die2->die_abbrev - abbrev_opt_start])
8743 return 1;
8746 /* Stabilize the sort. */
8747 if (die1->die_abbrev < die2->die_abbrev)
8748 return -1;
8749 if (die1->die_abbrev > die2->die_abbrev)
8750 return 1;
8752 return 0;
8755 /* Convert dw_val_class_const and dw_val_class_unsigned_const class attributes
8756 of DIEs in between sorted_abbrev_dies[first_id] and abbrev_dies[end_id - 1]
8757 into dw_val_class_const_implicit or
8758 dw_val_class_unsigned_const_implicit. */
8760 static void
8761 optimize_implicit_const (unsigned int first_id, unsigned int end,
8762 vec<bool> &implicit_consts)
8764 /* It never makes sense if there is just one DIE using the abbreviation. */
8765 if (end < first_id + 2)
8766 return;
8768 dw_attr_node *a;
8769 unsigned ix, i;
8770 dw_die_ref die = sorted_abbrev_dies[first_id];
8771 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8772 if (implicit_consts[ix])
8774 enum dw_val_class new_class = dw_val_class_none;
8775 switch (AT_class (a))
8777 case dw_val_class_unsigned_const:
8778 if ((HOST_WIDE_INT) AT_unsigned (a) < 0)
8779 continue;
8781 /* The .debug_abbrev section will grow by
8782 size_of_sleb128 (AT_unsigned (a)) and we avoid the constants
8783 in all the DIEs using that abbreviation. */
8784 if (constant_size (AT_unsigned (a)) * (end - first_id)
8785 <= (unsigned) size_of_sleb128 (AT_unsigned (a)))
8786 continue;
8788 new_class = dw_val_class_unsigned_const_implicit;
8789 break;
8791 case dw_val_class_const:
8792 new_class = dw_val_class_const_implicit;
8793 break;
8795 case dw_val_class_file:
8796 new_class = dw_val_class_file_implicit;
8797 break;
8799 default:
8800 continue;
8802 for (i = first_id; i < end; i++)
8803 (*sorted_abbrev_dies[i]->die_attr)[ix].dw_attr_val.val_class
8804 = new_class;
8808 /* Attempt to optimize abbreviation table from abbrev_opt_start
8809 abbreviation above. */
8811 static void
8812 optimize_abbrev_table (void)
8814 if (abbrev_opt_start
8815 && vec_safe_length (abbrev_die_table) > abbrev_opt_start
8816 && (dwarf_version >= 5 || vec_safe_length (abbrev_die_table) > 127))
8818 auto_vec<bool, 32> implicit_consts;
8819 sorted_abbrev_dies.qsort (die_abbrev_cmp);
8821 unsigned int abbrev_id = abbrev_opt_start - 1;
8822 unsigned int first_id = ~0U;
8823 unsigned int last_abbrev_id = 0;
8824 unsigned int i;
8825 dw_die_ref die;
8826 if (abbrev_opt_base_type_end > abbrev_opt_start)
8827 abbrev_id = abbrev_opt_base_type_end - 1;
8828 /* Reassign abbreviation ids from abbrev_opt_start above, so that
8829 most commonly used abbreviations come first. */
8830 FOR_EACH_VEC_ELT (sorted_abbrev_dies, i, die)
8832 dw_attr_node *a;
8833 unsigned ix;
8835 /* If calc_base_type_die_sizes has been called, the CU and
8836 base types after it can't be optimized, because we've already
8837 calculated their DIE offsets. We've sorted them first. */
8838 if (die->die_abbrev < abbrev_opt_base_type_end)
8839 continue;
8840 if (die->die_abbrev != last_abbrev_id)
8842 last_abbrev_id = die->die_abbrev;
8843 if (dwarf_version >= 5 && first_id != ~0U)
8844 optimize_implicit_const (first_id, i, implicit_consts);
8845 abbrev_id++;
8846 (*abbrev_die_table)[abbrev_id] = die;
8847 if (dwarf_version >= 5)
8849 first_id = i;
8850 implicit_consts.truncate (0);
8852 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8853 switch (AT_class (a))
8855 case dw_val_class_const:
8856 case dw_val_class_unsigned_const:
8857 case dw_val_class_file:
8858 implicit_consts.safe_push (true);
8859 break;
8860 default:
8861 implicit_consts.safe_push (false);
8862 break;
8866 else if (dwarf_version >= 5)
8868 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8869 if (!implicit_consts[ix])
8870 continue;
8871 else
8873 dw_attr_node *other_a
8874 = &(*(*abbrev_die_table)[abbrev_id]->die_attr)[ix];
8875 if (!dw_val_equal_p (&a->dw_attr_val,
8876 &other_a->dw_attr_val))
8877 implicit_consts[ix] = false;
8880 die->die_abbrev = abbrev_id;
8882 gcc_assert (abbrev_id == vec_safe_length (abbrev_die_table) - 1);
8883 if (dwarf_version >= 5 && first_id != ~0U)
8884 optimize_implicit_const (first_id, i, implicit_consts);
8887 abbrev_opt_start = 0;
8888 abbrev_opt_base_type_end = 0;
8889 abbrev_usage_count.release ();
8890 sorted_abbrev_dies.release ();
8893 /* Return the power-of-two number of bytes necessary to represent VALUE. */
8895 static int
8896 constant_size (unsigned HOST_WIDE_INT value)
8898 int log;
8900 if (value == 0)
8901 log = 0;
8902 else
8903 log = floor_log2 (value);
8905 log = log / 8;
8906 log = 1 << (floor_log2 (log) + 1);
8908 return log;
8911 /* Return the size of a DIE as it is represented in the
8912 .debug_info section. */
8914 static unsigned long
8915 size_of_die (dw_die_ref die)
8917 unsigned long size = 0;
8918 dw_attr_node *a;
8919 unsigned ix;
8920 enum dwarf_form form;
8922 size += size_of_uleb128 (die->die_abbrev);
8923 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8925 switch (AT_class (a))
8927 case dw_val_class_addr:
8928 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8930 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8931 size += size_of_uleb128 (AT_index (a));
8933 else
8934 size += DWARF2_ADDR_SIZE;
8935 break;
8936 case dw_val_class_offset:
8937 size += DWARF_OFFSET_SIZE;
8938 break;
8939 case dw_val_class_loc:
8941 unsigned long lsize = size_of_locs (AT_loc (a));
8943 /* Block length. */
8944 if (dwarf_version >= 4)
8945 size += size_of_uleb128 (lsize);
8946 else
8947 size += constant_size (lsize);
8948 size += lsize;
8950 break;
8951 case dw_val_class_loc_list:
8952 if (dwarf_split_debug_info && dwarf_version >= 5)
8954 gcc_assert (AT_loc_list (a)->num_assigned);
8955 size += size_of_uleb128 (AT_loc_list (a)->hash);
8957 else
8958 size += DWARF_OFFSET_SIZE;
8959 break;
8960 case dw_val_class_range_list:
8961 if (value_format (a) == DW_FORM_rnglistx)
8963 gcc_assert (rnglist_idx);
8964 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
8965 size += size_of_uleb128 (r->idx);
8967 else
8968 size += DWARF_OFFSET_SIZE;
8969 break;
8970 case dw_val_class_const:
8971 size += size_of_sleb128 (AT_int (a));
8972 break;
8973 case dw_val_class_unsigned_const:
8975 int csize = constant_size (AT_unsigned (a));
8976 if (dwarf_version == 3
8977 && a->dw_attr == DW_AT_data_member_location
8978 && csize >= 4)
8979 size += size_of_uleb128 (AT_unsigned (a));
8980 else
8981 size += csize;
8983 break;
8984 case dw_val_class_const_implicit:
8985 case dw_val_class_unsigned_const_implicit:
8986 case dw_val_class_file_implicit:
8987 /* These occupy no size in the DIE, just an extra sleb128 in
8988 .debug_abbrev. */
8989 break;
8990 case dw_val_class_const_double:
8991 size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
8992 if (HOST_BITS_PER_WIDE_INT >= DWARF_LARGEST_DATA_FORM_BITS)
8993 size++; /* block */
8994 break;
8995 case dw_val_class_wide_int:
8996 size += (get_full_len (*a->dw_attr_val.v.val_wide)
8997 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
8998 if (get_full_len (*a->dw_attr_val.v.val_wide)
8999 * HOST_BITS_PER_WIDE_INT > DWARF_LARGEST_DATA_FORM_BITS)
9000 size++; /* block */
9001 break;
9002 case dw_val_class_vec:
9003 size += constant_size (a->dw_attr_val.v.val_vec.length
9004 * a->dw_attr_val.v.val_vec.elt_size)
9005 + a->dw_attr_val.v.val_vec.length
9006 * a->dw_attr_val.v.val_vec.elt_size; /* block */
9007 break;
9008 case dw_val_class_flag:
9009 if (dwarf_version >= 4)
9010 /* Currently all add_AT_flag calls pass in 1 as last argument,
9011 so DW_FORM_flag_present can be used. If that ever changes,
9012 we'll need to use DW_FORM_flag and have some optimization
9013 in build_abbrev_table that will change those to
9014 DW_FORM_flag_present if it is set to 1 in all DIEs using
9015 the same abbrev entry. */
9016 gcc_assert (a->dw_attr_val.v.val_flag == 1);
9017 else
9018 size += 1;
9019 break;
9020 case dw_val_class_die_ref:
9021 if (AT_ref_external (a))
9023 /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
9024 we use DW_FORM_ref_addr. In DWARF2, DW_FORM_ref_addr
9025 is sized by target address length, whereas in DWARF3
9026 it's always sized as an offset. */
9027 if (use_debug_types)
9028 size += DWARF_TYPE_SIGNATURE_SIZE;
9029 else if (dwarf_version == 2)
9030 size += DWARF2_ADDR_SIZE;
9031 else
9032 size += DWARF_OFFSET_SIZE;
9034 else
9035 size += DWARF_OFFSET_SIZE;
9036 break;
9037 case dw_val_class_fde_ref:
9038 size += DWARF_OFFSET_SIZE;
9039 break;
9040 case dw_val_class_lbl_id:
9041 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
9043 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
9044 size += size_of_uleb128 (AT_index (a));
9046 else
9047 size += DWARF2_ADDR_SIZE;
9048 break;
9049 case dw_val_class_lineptr:
9050 case dw_val_class_macptr:
9051 case dw_val_class_loclistsptr:
9052 size += DWARF_OFFSET_SIZE;
9053 break;
9054 case dw_val_class_str:
9055 form = AT_string_form (a);
9056 if (form == DW_FORM_strp || form == DW_FORM_line_strp)
9057 size += DWARF_OFFSET_SIZE;
9058 else if (form == DW_FORM_GNU_str_index)
9059 size += size_of_uleb128 (AT_index (a));
9060 else
9061 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
9062 break;
9063 case dw_val_class_file:
9064 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
9065 break;
9066 case dw_val_class_data8:
9067 size += 8;
9068 break;
9069 case dw_val_class_vms_delta:
9070 size += DWARF_OFFSET_SIZE;
9071 break;
9072 case dw_val_class_high_pc:
9073 size += DWARF2_ADDR_SIZE;
9074 break;
9075 case dw_val_class_discr_value:
9076 size += size_of_discr_value (&a->dw_attr_val.v.val_discr_value);
9077 break;
9078 case dw_val_class_discr_list:
9080 unsigned block_size = size_of_discr_list (AT_discr_list (a));
9082 /* This is a block, so we have the block length and then its
9083 data. */
9084 size += constant_size (block_size) + block_size;
9086 break;
9087 default:
9088 gcc_unreachable ();
9092 return size;
9095 /* Size the debugging information associated with a given DIE. Visits the
9096 DIE's children recursively. Updates the global variable next_die_offset, on
9097 each time through. Uses the current value of next_die_offset to update the
9098 die_offset field in each DIE. */
9100 static void
9101 calc_die_sizes (dw_die_ref die)
9103 dw_die_ref c;
9105 gcc_assert (die->die_offset == 0
9106 || (unsigned long int) die->die_offset == next_die_offset);
9107 die->die_offset = next_die_offset;
9108 next_die_offset += size_of_die (die);
9110 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
9112 if (die->die_child != NULL)
9113 /* Count the null byte used to terminate sibling lists. */
9114 next_die_offset += 1;
9117 /* Size just the base type children at the start of the CU.
9118 This is needed because build_abbrev needs to size locs
9119 and sizing of type based stack ops needs to know die_offset
9120 values for the base types. */
9122 static void
9123 calc_base_type_die_sizes (void)
9125 unsigned long die_offset = (dwarf_split_debug_info
9126 ? DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
9127 : DWARF_COMPILE_UNIT_HEADER_SIZE);
9128 unsigned int i;
9129 dw_die_ref base_type;
9130 #if ENABLE_ASSERT_CHECKING
9131 dw_die_ref prev = comp_unit_die ()->die_child;
9132 #endif
9134 die_offset += size_of_die (comp_unit_die ());
9135 for (i = 0; base_types.iterate (i, &base_type); i++)
9137 #if ENABLE_ASSERT_CHECKING
9138 gcc_assert (base_type->die_offset == 0
9139 && prev->die_sib == base_type
9140 && base_type->die_child == NULL
9141 && base_type->die_abbrev);
9142 prev = base_type;
9143 #endif
9144 if (abbrev_opt_start
9145 && base_type->die_abbrev >= abbrev_opt_base_type_end)
9146 abbrev_opt_base_type_end = base_type->die_abbrev + 1;
9147 base_type->die_offset = die_offset;
9148 die_offset += size_of_die (base_type);
9152 /* Set the marks for a die and its children. We do this so
9153 that we know whether or not a reference needs to use FORM_ref_addr; only
9154 DIEs in the same CU will be marked. We used to clear out the offset
9155 and use that as the flag, but ran into ordering problems. */
9157 static void
9158 mark_dies (dw_die_ref die)
9160 dw_die_ref c;
9162 gcc_assert (!die->die_mark);
9164 die->die_mark = 1;
9165 FOR_EACH_CHILD (die, c, mark_dies (c));
9168 /* Clear the marks for a die and its children. */
9170 static void
9171 unmark_dies (dw_die_ref die)
9173 dw_die_ref c;
9175 if (! use_debug_types)
9176 gcc_assert (die->die_mark);
9178 die->die_mark = 0;
9179 FOR_EACH_CHILD (die, c, unmark_dies (c));
9182 /* Clear the marks for a die, its children and referred dies. */
9184 static void
9185 unmark_all_dies (dw_die_ref die)
9187 dw_die_ref c;
9188 dw_attr_node *a;
9189 unsigned ix;
9191 if (!die->die_mark)
9192 return;
9193 die->die_mark = 0;
9195 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
9197 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9198 if (AT_class (a) == dw_val_class_die_ref)
9199 unmark_all_dies (AT_ref (a));
9202 /* Calculate if the entry should appear in the final output file. It may be
9203 from a pruned a type. */
9205 static bool
9206 include_pubname_in_output (vec<pubname_entry, va_gc> *table, pubname_entry *p)
9208 /* By limiting gnu pubnames to definitions only, gold can generate a
9209 gdb index without entries for declarations, which don't include
9210 enough information to be useful. */
9211 if (debug_generate_pub_sections == 2 && is_declaration_die (p->die))
9212 return false;
9214 if (table == pubname_table)
9216 /* Enumerator names are part of the pubname table, but the
9217 parent DW_TAG_enumeration_type die may have been pruned.
9218 Don't output them if that is the case. */
9219 if (p->die->die_tag == DW_TAG_enumerator &&
9220 (p->die->die_parent == NULL
9221 || !p->die->die_parent->die_perennial_p))
9222 return false;
9224 /* Everything else in the pubname table is included. */
9225 return true;
9228 /* The pubtypes table shouldn't include types that have been
9229 pruned. */
9230 return (p->die->die_offset != 0
9231 || !flag_eliminate_unused_debug_types);
9234 /* Return the size of the .debug_pubnames or .debug_pubtypes table
9235 generated for the compilation unit. */
9237 static unsigned long
9238 size_of_pubnames (vec<pubname_entry, va_gc> *names)
9240 unsigned long size;
9241 unsigned i;
9242 pubname_entry *p;
9243 int space_for_flags = (debug_generate_pub_sections == 2) ? 1 : 0;
9245 size = DWARF_PUBNAMES_HEADER_SIZE;
9246 FOR_EACH_VEC_ELT (*names, i, p)
9247 if (include_pubname_in_output (names, p))
9248 size += strlen (p->name) + DWARF_OFFSET_SIZE + 1 + space_for_flags;
9250 size += DWARF_OFFSET_SIZE;
9251 return size;
9254 /* Return the size of the information in the .debug_aranges section. */
9256 static unsigned long
9257 size_of_aranges (void)
9259 unsigned long size;
9261 size = DWARF_ARANGES_HEADER_SIZE;
9263 /* Count the address/length pair for this compilation unit. */
9264 if (text_section_used)
9265 size += 2 * DWARF2_ADDR_SIZE;
9266 if (cold_text_section_used)
9267 size += 2 * DWARF2_ADDR_SIZE;
9268 if (have_multiple_function_sections)
9270 unsigned fde_idx;
9271 dw_fde_ref fde;
9273 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
9275 if (DECL_IGNORED_P (fde->decl))
9276 continue;
9277 if (!fde->in_std_section)
9278 size += 2 * DWARF2_ADDR_SIZE;
9279 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
9280 size += 2 * DWARF2_ADDR_SIZE;
9284 /* Count the two zero words used to terminated the address range table. */
9285 size += 2 * DWARF2_ADDR_SIZE;
9286 return size;
9289 /* Select the encoding of an attribute value. */
9291 static enum dwarf_form
9292 value_format (dw_attr_node *a)
9294 switch (AT_class (a))
9296 case dw_val_class_addr:
9297 /* Only very few attributes allow DW_FORM_addr. */
9298 switch (a->dw_attr)
9300 case DW_AT_low_pc:
9301 case DW_AT_high_pc:
9302 case DW_AT_entry_pc:
9303 case DW_AT_trampoline:
9304 return (AT_index (a) == NOT_INDEXED
9305 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
9306 default:
9307 break;
9309 switch (DWARF2_ADDR_SIZE)
9311 case 1:
9312 return DW_FORM_data1;
9313 case 2:
9314 return DW_FORM_data2;
9315 case 4:
9316 return DW_FORM_data4;
9317 case 8:
9318 return DW_FORM_data8;
9319 default:
9320 gcc_unreachable ();
9322 case dw_val_class_loc_list:
9323 if (dwarf_split_debug_info
9324 && dwarf_version >= 5
9325 && AT_loc_list (a)->num_assigned)
9326 return DW_FORM_loclistx;
9327 /* FALLTHRU */
9328 case dw_val_class_range_list:
9329 /* For range lists in DWARF 5, use DW_FORM_rnglistx from .debug_info.dwo
9330 but in .debug_info use DW_FORM_sec_offset, which is shorter if we
9331 care about sizes of .debug* sections in shared libraries and
9332 executables and don't take into account relocations that affect just
9333 relocatable objects - for DW_FORM_rnglistx we'd have to emit offset
9334 table in the .debug_rnglists section. */
9335 if (dwarf_split_debug_info
9336 && dwarf_version >= 5
9337 && AT_class (a) == dw_val_class_range_list
9338 && rnglist_idx
9339 && a->dw_attr_val.val_entry != RELOCATED_OFFSET)
9340 return DW_FORM_rnglistx;
9341 if (dwarf_version >= 4)
9342 return DW_FORM_sec_offset;
9343 /* FALLTHRU */
9344 case dw_val_class_vms_delta:
9345 case dw_val_class_offset:
9346 switch (DWARF_OFFSET_SIZE)
9348 case 4:
9349 return DW_FORM_data4;
9350 case 8:
9351 return DW_FORM_data8;
9352 default:
9353 gcc_unreachable ();
9355 case dw_val_class_loc:
9356 if (dwarf_version >= 4)
9357 return DW_FORM_exprloc;
9358 switch (constant_size (size_of_locs (AT_loc (a))))
9360 case 1:
9361 return DW_FORM_block1;
9362 case 2:
9363 return DW_FORM_block2;
9364 case 4:
9365 return DW_FORM_block4;
9366 default:
9367 gcc_unreachable ();
9369 case dw_val_class_const:
9370 return DW_FORM_sdata;
9371 case dw_val_class_unsigned_const:
9372 switch (constant_size (AT_unsigned (a)))
9374 case 1:
9375 return DW_FORM_data1;
9376 case 2:
9377 return DW_FORM_data2;
9378 case 4:
9379 /* In DWARF3 DW_AT_data_member_location with
9380 DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
9381 constant, so we need to use DW_FORM_udata if we need
9382 a large constant. */
9383 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
9384 return DW_FORM_udata;
9385 return DW_FORM_data4;
9386 case 8:
9387 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
9388 return DW_FORM_udata;
9389 return DW_FORM_data8;
9390 default:
9391 gcc_unreachable ();
9393 case dw_val_class_const_implicit:
9394 case dw_val_class_unsigned_const_implicit:
9395 case dw_val_class_file_implicit:
9396 return DW_FORM_implicit_const;
9397 case dw_val_class_const_double:
9398 switch (HOST_BITS_PER_WIDE_INT)
9400 case 8:
9401 return DW_FORM_data2;
9402 case 16:
9403 return DW_FORM_data4;
9404 case 32:
9405 return DW_FORM_data8;
9406 case 64:
9407 if (dwarf_version >= 5)
9408 return DW_FORM_data16;
9409 /* FALLTHRU */
9410 default:
9411 return DW_FORM_block1;
9413 case dw_val_class_wide_int:
9414 switch (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT)
9416 case 8:
9417 return DW_FORM_data1;
9418 case 16:
9419 return DW_FORM_data2;
9420 case 32:
9421 return DW_FORM_data4;
9422 case 64:
9423 return DW_FORM_data8;
9424 case 128:
9425 if (dwarf_version >= 5)
9426 return DW_FORM_data16;
9427 /* FALLTHRU */
9428 default:
9429 return DW_FORM_block1;
9431 case dw_val_class_vec:
9432 switch (constant_size (a->dw_attr_val.v.val_vec.length
9433 * a->dw_attr_val.v.val_vec.elt_size))
9435 case 1:
9436 return DW_FORM_block1;
9437 case 2:
9438 return DW_FORM_block2;
9439 case 4:
9440 return DW_FORM_block4;
9441 default:
9442 gcc_unreachable ();
9444 case dw_val_class_flag:
9445 if (dwarf_version >= 4)
9447 /* Currently all add_AT_flag calls pass in 1 as last argument,
9448 so DW_FORM_flag_present can be used. If that ever changes,
9449 we'll need to use DW_FORM_flag and have some optimization
9450 in build_abbrev_table that will change those to
9451 DW_FORM_flag_present if it is set to 1 in all DIEs using
9452 the same abbrev entry. */
9453 gcc_assert (a->dw_attr_val.v.val_flag == 1);
9454 return DW_FORM_flag_present;
9456 return DW_FORM_flag;
9457 case dw_val_class_die_ref:
9458 if (AT_ref_external (a))
9459 return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
9460 else
9461 return DW_FORM_ref;
9462 case dw_val_class_fde_ref:
9463 return DW_FORM_data;
9464 case dw_val_class_lbl_id:
9465 return (AT_index (a) == NOT_INDEXED
9466 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
9467 case dw_val_class_lineptr:
9468 case dw_val_class_macptr:
9469 case dw_val_class_loclistsptr:
9470 return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
9471 case dw_val_class_str:
9472 return AT_string_form (a);
9473 case dw_val_class_file:
9474 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
9476 case 1:
9477 return DW_FORM_data1;
9478 case 2:
9479 return DW_FORM_data2;
9480 case 4:
9481 return DW_FORM_data4;
9482 default:
9483 gcc_unreachable ();
9486 case dw_val_class_data8:
9487 return DW_FORM_data8;
9489 case dw_val_class_high_pc:
9490 switch (DWARF2_ADDR_SIZE)
9492 case 1:
9493 return DW_FORM_data1;
9494 case 2:
9495 return DW_FORM_data2;
9496 case 4:
9497 return DW_FORM_data4;
9498 case 8:
9499 return DW_FORM_data8;
9500 default:
9501 gcc_unreachable ();
9504 case dw_val_class_discr_value:
9505 return (a->dw_attr_val.v.val_discr_value.pos
9506 ? DW_FORM_udata
9507 : DW_FORM_sdata);
9508 case dw_val_class_discr_list:
9509 switch (constant_size (size_of_discr_list (AT_discr_list (a))))
9511 case 1:
9512 return DW_FORM_block1;
9513 case 2:
9514 return DW_FORM_block2;
9515 case 4:
9516 return DW_FORM_block4;
9517 default:
9518 gcc_unreachable ();
9521 default:
9522 gcc_unreachable ();
9526 /* Output the encoding of an attribute value. */
9528 static void
9529 output_value_format (dw_attr_node *a)
9531 enum dwarf_form form = value_format (a);
9533 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
9536 /* Given a die and id, produce the appropriate abbreviations. */
9538 static void
9539 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
9541 unsigned ix;
9542 dw_attr_node *a_attr;
9544 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
9545 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
9546 dwarf_tag_name (abbrev->die_tag));
9548 if (abbrev->die_child != NULL)
9549 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
9550 else
9551 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
9553 for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
9555 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
9556 dwarf_attr_name (a_attr->dw_attr));
9557 output_value_format (a_attr);
9558 if (value_format (a_attr) == DW_FORM_implicit_const)
9560 if (AT_class (a_attr) == dw_val_class_file_implicit)
9562 int f = maybe_emit_file (a_attr->dw_attr_val.v.val_file);
9563 const char *filename = a_attr->dw_attr_val.v.val_file->filename;
9564 dw2_asm_output_data_sleb128 (f, "(%s)", filename);
9566 else
9567 dw2_asm_output_data_sleb128 (a_attr->dw_attr_val.v.val_int, NULL);
9571 dw2_asm_output_data (1, 0, NULL);
9572 dw2_asm_output_data (1, 0, NULL);
9576 /* Output the .debug_abbrev section which defines the DIE abbreviation
9577 table. */
9579 static void
9580 output_abbrev_section (void)
9582 unsigned int abbrev_id;
9583 dw_die_ref abbrev;
9585 FOR_EACH_VEC_SAFE_ELT (abbrev_die_table, abbrev_id, abbrev)
9586 if (abbrev_id != 0)
9587 output_die_abbrevs (abbrev_id, abbrev);
9589 /* Terminate the table. */
9590 dw2_asm_output_data (1, 0, NULL);
9593 /* Output a symbol we can use to refer to this DIE from another CU. */
9595 static inline void
9596 output_die_symbol (dw_die_ref die)
9598 const char *sym = die->die_id.die_symbol;
9600 gcc_assert (!die->comdat_type_p);
9602 if (sym == 0)
9603 return;
9605 if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
9606 /* We make these global, not weak; if the target doesn't support
9607 .linkonce, it doesn't support combining the sections, so debugging
9608 will break. */
9609 targetm.asm_out.globalize_label (asm_out_file, sym);
9611 ASM_OUTPUT_LABEL (asm_out_file, sym);
9614 /* Return a new location list, given the begin and end range, and the
9615 expression. */
9617 static inline dw_loc_list_ref
9618 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
9619 const char *section)
9621 dw_loc_list_ref retlist = ggc_cleared_alloc<dw_loc_list_node> ();
9623 retlist->begin = begin;
9624 retlist->begin_entry = NULL;
9625 retlist->end = end;
9626 retlist->expr = expr;
9627 retlist->section = section;
9629 return retlist;
9632 /* Generate a new internal symbol for this location list node, if it
9633 hasn't got one yet. */
9635 static inline void
9636 gen_llsym (dw_loc_list_ref list)
9638 gcc_assert (!list->ll_symbol);
9639 list->ll_symbol = gen_internal_sym ("LLST");
9642 /* Output the location list given to us. */
9644 static void
9645 output_loc_list (dw_loc_list_ref list_head)
9647 if (list_head->emitted)
9648 return;
9649 list_head->emitted = true;
9651 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
9653 dw_loc_list_ref curr = list_head;
9654 const char *last_section = NULL;
9655 const char *base_label = NULL;
9657 /* Walk the location list, and output each range + expression. */
9658 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
9660 unsigned long size;
9661 /* Don't output an entry that starts and ends at the same address. */
9662 if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
9663 continue;
9664 size = size_of_locs (curr->expr);
9665 /* If the expression is too large, drop it on the floor. We could
9666 perhaps put it into DW_TAG_dwarf_procedure and refer to that
9667 in the expression, but >= 64KB expressions for a single value
9668 in a single range are unlikely very useful. */
9669 if (dwarf_version < 5 && size > 0xffff)
9670 continue;
9671 if (dwarf_version >= 5)
9673 if (dwarf_split_debug_info)
9675 /* For -gsplit-dwarf, emit DW_LLE_starx_length, which has
9676 uleb128 index into .debug_addr and uleb128 length. */
9677 dw2_asm_output_data (1, DW_LLE_startx_length,
9678 "DW_LLE_startx_length (%s)",
9679 list_head->ll_symbol);
9680 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
9681 "Location list range start index "
9682 "(%s)", curr->begin);
9683 /* FIXME: This will ICE ifndef HAVE_AS_LEB128.
9684 For that case we probably need to emit DW_LLE_startx_endx,
9685 but we'd need 2 .debug_addr entries rather than just one. */
9686 dw2_asm_output_delta_uleb128 (curr->end, curr->begin,
9687 "Location list length (%s)",
9688 list_head->ll_symbol);
9690 else if (!have_multiple_function_sections && HAVE_AS_LEB128)
9692 /* If all code is in .text section, the base address is
9693 already provided by the CU attributes. Use
9694 DW_LLE_offset_pair where both addresses are uleb128 encoded
9695 offsets against that base. */
9696 dw2_asm_output_data (1, DW_LLE_offset_pair,
9697 "DW_LLE_offset_pair (%s)",
9698 list_head->ll_symbol);
9699 dw2_asm_output_delta_uleb128 (curr->begin, curr->section,
9700 "Location list begin address (%s)",
9701 list_head->ll_symbol);
9702 dw2_asm_output_delta_uleb128 (curr->end, curr->section,
9703 "Location list end address (%s)",
9704 list_head->ll_symbol);
9706 else if (HAVE_AS_LEB128)
9708 /* Otherwise, find out how many consecutive entries could share
9709 the same base entry. If just one, emit DW_LLE_start_length,
9710 otherwise emit DW_LLE_base_address for the base address
9711 followed by a series of DW_LLE_offset_pair. */
9712 if (last_section == NULL || curr->section != last_section)
9714 dw_loc_list_ref curr2;
9715 for (curr2 = curr->dw_loc_next; curr2 != NULL;
9716 curr2 = curr2->dw_loc_next)
9718 if (strcmp (curr2->begin, curr2->end) == 0
9719 && !curr2->force)
9720 continue;
9721 break;
9723 if (curr2 == NULL || curr->section != curr2->section)
9724 last_section = NULL;
9725 else
9727 last_section = curr->section;
9728 base_label = curr->begin;
9729 dw2_asm_output_data (1, DW_LLE_base_address,
9730 "DW_LLE_base_address (%s)",
9731 list_head->ll_symbol);
9732 dw2_asm_output_addr (DWARF2_ADDR_SIZE, base_label,
9733 "Base address (%s)",
9734 list_head->ll_symbol);
9737 /* Only one entry with the same base address. Use
9738 DW_LLE_start_length with absolute address and uleb128
9739 length. */
9740 if (last_section == NULL)
9742 dw2_asm_output_data (1, DW_LLE_start_length,
9743 "DW_LLE_start_length (%s)",
9744 list_head->ll_symbol);
9745 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
9746 "Location list begin address (%s)",
9747 list_head->ll_symbol);
9748 dw2_asm_output_delta_uleb128 (curr->end, curr->begin,
9749 "Location list length "
9750 "(%s)", list_head->ll_symbol);
9752 /* Otherwise emit DW_LLE_offset_pair, relative to above emitted
9753 DW_LLE_base_address. */
9754 else
9756 dw2_asm_output_data (1, DW_LLE_offset_pair,
9757 "DW_LLE_offset_pair (%s)",
9758 list_head->ll_symbol);
9759 dw2_asm_output_delta_uleb128 (curr->begin, base_label,
9760 "Location list begin address "
9761 "(%s)", list_head->ll_symbol);
9762 dw2_asm_output_delta_uleb128 (curr->end, base_label,
9763 "Location list end address "
9764 "(%s)", list_head->ll_symbol);
9767 /* The assembler does not support .uleb128 directive. Emit
9768 DW_LLE_start_end with a pair of absolute addresses. */
9769 else
9771 dw2_asm_output_data (1, DW_LLE_start_end,
9772 "DW_LLE_start_end (%s)",
9773 list_head->ll_symbol);
9774 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
9775 "Location list begin address (%s)",
9776 list_head->ll_symbol);
9777 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
9778 "Location list end address (%s)",
9779 list_head->ll_symbol);
9782 else if (dwarf_split_debug_info)
9784 /* For -gsplit-dwarf -gdwarf-{2,3,4} emit index into .debug_addr
9785 and 4 byte length. */
9786 dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
9787 "Location list start/length entry (%s)",
9788 list_head->ll_symbol);
9789 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
9790 "Location list range start index (%s)",
9791 curr->begin);
9792 /* The length field is 4 bytes. If we ever need to support
9793 an 8-byte length, we can add a new DW_LLE code or fall back
9794 to DW_LLE_GNU_start_end_entry. */
9795 dw2_asm_output_delta (4, curr->end, curr->begin,
9796 "Location list range length (%s)",
9797 list_head->ll_symbol);
9799 else if (!have_multiple_function_sections)
9801 /* Pair of relative addresses against start of text section. */
9802 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
9803 "Location list begin address (%s)",
9804 list_head->ll_symbol);
9805 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
9806 "Location list end address (%s)",
9807 list_head->ll_symbol);
9809 else
9811 /* Pair of absolute addresses. */
9812 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
9813 "Location list begin address (%s)",
9814 list_head->ll_symbol);
9815 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
9816 "Location list end address (%s)",
9817 list_head->ll_symbol);
9820 /* Output the block length for this list of location operations. */
9821 if (dwarf_version >= 5)
9822 dw2_asm_output_data_uleb128 (size, "Location expression size");
9823 else
9825 gcc_assert (size <= 0xffff);
9826 dw2_asm_output_data (2, size, "Location expression size");
9829 output_loc_sequence (curr->expr, -1);
9832 /* And finally list termination. */
9833 if (dwarf_version >= 5)
9834 dw2_asm_output_data (1, DW_LLE_end_of_list,
9835 "DW_LLE_end_of_list (%s)", list_head->ll_symbol);
9836 else if (dwarf_split_debug_info)
9837 dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
9838 "Location list terminator (%s)",
9839 list_head->ll_symbol);
9840 else
9842 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
9843 "Location list terminator begin (%s)",
9844 list_head->ll_symbol);
9845 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
9846 "Location list terminator end (%s)",
9847 list_head->ll_symbol);
9851 /* Output a range_list offset into the .debug_ranges or .debug_rnglists
9852 section. Emit a relocated reference if val_entry is NULL, otherwise,
9853 emit an indirect reference. */
9855 static void
9856 output_range_list_offset (dw_attr_node *a)
9858 const char *name = dwarf_attr_name (a->dw_attr);
9860 if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
9862 if (dwarf_version >= 5)
9864 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
9865 dw2_asm_output_offset (DWARF_OFFSET_SIZE, r->label,
9866 debug_ranges_section, "%s", name);
9868 else
9870 char *p = strchr (ranges_section_label, '\0');
9871 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
9872 a->dw_attr_val.v.val_offset * 2 * DWARF2_ADDR_SIZE);
9873 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
9874 debug_ranges_section, "%s", name);
9875 *p = '\0';
9878 else if (dwarf_version >= 5)
9880 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
9881 gcc_assert (rnglist_idx);
9882 dw2_asm_output_data_uleb128 (r->idx, "%s", name);
9884 else
9885 dw2_asm_output_data (DWARF_OFFSET_SIZE,
9886 a->dw_attr_val.v.val_offset * 2 * DWARF2_ADDR_SIZE,
9887 "%s (offset from %s)", name, ranges_section_label);
9890 /* Output the offset into the debug_loc section. */
9892 static void
9893 output_loc_list_offset (dw_attr_node *a)
9895 char *sym = AT_loc_list (a)->ll_symbol;
9897 gcc_assert (sym);
9898 if (!dwarf_split_debug_info)
9899 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
9900 "%s", dwarf_attr_name (a->dw_attr));
9901 else if (dwarf_version >= 5)
9903 gcc_assert (AT_loc_list (a)->num_assigned);
9904 dw2_asm_output_data_uleb128 (AT_loc_list (a)->hash, "%s (%s)",
9905 dwarf_attr_name (a->dw_attr),
9906 sym);
9908 else
9909 dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
9910 "%s", dwarf_attr_name (a->dw_attr));
9913 /* Output an attribute's index or value appropriately. */
9915 static void
9916 output_attr_index_or_value (dw_attr_node *a)
9918 const char *name = dwarf_attr_name (a->dw_attr);
9920 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
9922 dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
9923 return;
9925 switch (AT_class (a))
9927 case dw_val_class_addr:
9928 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
9929 break;
9930 case dw_val_class_high_pc:
9931 case dw_val_class_lbl_id:
9932 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
9933 break;
9934 default:
9935 gcc_unreachable ();
9939 /* Output a type signature. */
9941 static inline void
9942 output_signature (const char *sig, const char *name)
9944 int i;
9946 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9947 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
9950 /* Output a discriminant value. */
9952 static inline void
9953 output_discr_value (dw_discr_value *discr_value, const char *name)
9955 if (discr_value->pos)
9956 dw2_asm_output_data_uleb128 (discr_value->v.uval, "%s", name);
9957 else
9958 dw2_asm_output_data_sleb128 (discr_value->v.sval, "%s", name);
9961 /* Output the DIE and its attributes. Called recursively to generate
9962 the definitions of each child DIE. */
9964 static void
9965 output_die (dw_die_ref die)
9967 dw_attr_node *a;
9968 dw_die_ref c;
9969 unsigned long size;
9970 unsigned ix;
9972 /* If someone in another CU might refer to us, set up a symbol for
9973 them to point to. */
9974 if (! die->comdat_type_p && die->die_id.die_symbol)
9975 output_die_symbol (die);
9977 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
9978 (unsigned long)die->die_offset,
9979 dwarf_tag_name (die->die_tag));
9981 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9983 const char *name = dwarf_attr_name (a->dw_attr);
9985 switch (AT_class (a))
9987 case dw_val_class_addr:
9988 output_attr_index_or_value (a);
9989 break;
9991 case dw_val_class_offset:
9992 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
9993 "%s", name);
9994 break;
9996 case dw_val_class_range_list:
9997 output_range_list_offset (a);
9998 break;
10000 case dw_val_class_loc:
10001 size = size_of_locs (AT_loc (a));
10003 /* Output the block length for this list of location operations. */
10004 if (dwarf_version >= 4)
10005 dw2_asm_output_data_uleb128 (size, "%s", name);
10006 else
10007 dw2_asm_output_data (constant_size (size), size, "%s", name);
10009 output_loc_sequence (AT_loc (a), -1);
10010 break;
10012 case dw_val_class_const:
10013 /* ??? It would be slightly more efficient to use a scheme like is
10014 used for unsigned constants below, but gdb 4.x does not sign
10015 extend. Gdb 5.x does sign extend. */
10016 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
10017 break;
10019 case dw_val_class_unsigned_const:
10021 int csize = constant_size (AT_unsigned (a));
10022 if (dwarf_version == 3
10023 && a->dw_attr == DW_AT_data_member_location
10024 && csize >= 4)
10025 dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
10026 else
10027 dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
10029 break;
10031 case dw_val_class_const_implicit:
10032 if (flag_debug_asm)
10033 fprintf (asm_out_file, "\t\t\t%s %s ("
10034 HOST_WIDE_INT_PRINT_DEC ")\n",
10035 ASM_COMMENT_START, name, AT_int (a));
10036 break;
10038 case dw_val_class_unsigned_const_implicit:
10039 if (flag_debug_asm)
10040 fprintf (asm_out_file, "\t\t\t%s %s ("
10041 HOST_WIDE_INT_PRINT_HEX ")\n",
10042 ASM_COMMENT_START, name, AT_unsigned (a));
10043 break;
10045 case dw_val_class_const_double:
10047 unsigned HOST_WIDE_INT first, second;
10049 if (HOST_BITS_PER_WIDE_INT >= DWARF_LARGEST_DATA_FORM_BITS)
10050 dw2_asm_output_data (1,
10051 HOST_BITS_PER_DOUBLE_INT
10052 / HOST_BITS_PER_CHAR,
10053 NULL);
10055 if (WORDS_BIG_ENDIAN)
10057 first = a->dw_attr_val.v.val_double.high;
10058 second = a->dw_attr_val.v.val_double.low;
10060 else
10062 first = a->dw_attr_val.v.val_double.low;
10063 second = a->dw_attr_val.v.val_double.high;
10066 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
10067 first, "%s", name);
10068 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
10069 second, NULL);
10071 break;
10073 case dw_val_class_wide_int:
10075 int i;
10076 int len = get_full_len (*a->dw_attr_val.v.val_wide);
10077 int l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
10078 if (len * HOST_BITS_PER_WIDE_INT > DWARF_LARGEST_DATA_FORM_BITS)
10079 dw2_asm_output_data (1, get_full_len (*a->dw_attr_val.v.val_wide)
10080 * l, NULL);
10082 if (WORDS_BIG_ENDIAN)
10083 for (i = len - 1; i >= 0; --i)
10085 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
10086 "%s", name);
10087 name = "";
10089 else
10090 for (i = 0; i < len; ++i)
10092 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
10093 "%s", name);
10094 name = "";
10097 break;
10099 case dw_val_class_vec:
10101 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
10102 unsigned int len = a->dw_attr_val.v.val_vec.length;
10103 unsigned int i;
10104 unsigned char *p;
10106 dw2_asm_output_data (constant_size (len * elt_size),
10107 len * elt_size, "%s", name);
10108 if (elt_size > sizeof (HOST_WIDE_INT))
10110 elt_size /= 2;
10111 len *= 2;
10113 for (i = 0, p = (unsigned char *) a->dw_attr_val.v.val_vec.array;
10114 i < len;
10115 i++, p += elt_size)
10116 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
10117 "fp or vector constant word %u", i);
10118 break;
10121 case dw_val_class_flag:
10122 if (dwarf_version >= 4)
10124 /* Currently all add_AT_flag calls pass in 1 as last argument,
10125 so DW_FORM_flag_present can be used. If that ever changes,
10126 we'll need to use DW_FORM_flag and have some optimization
10127 in build_abbrev_table that will change those to
10128 DW_FORM_flag_present if it is set to 1 in all DIEs using
10129 the same abbrev entry. */
10130 gcc_assert (AT_flag (a) == 1);
10131 if (flag_debug_asm)
10132 fprintf (asm_out_file, "\t\t\t%s %s\n",
10133 ASM_COMMENT_START, name);
10134 break;
10136 dw2_asm_output_data (1, AT_flag (a), "%s", name);
10137 break;
10139 case dw_val_class_loc_list:
10140 output_loc_list_offset (a);
10141 break;
10143 case dw_val_class_die_ref:
10144 if (AT_ref_external (a))
10146 if (AT_ref (a)->comdat_type_p)
10148 comdat_type_node *type_node
10149 = AT_ref (a)->die_id.die_type_node;
10151 gcc_assert (type_node);
10152 output_signature (type_node->signature, name);
10154 else
10156 const char *sym = AT_ref (a)->die_id.die_symbol;
10157 int size;
10159 gcc_assert (sym);
10160 /* In DWARF2, DW_FORM_ref_addr is sized by target address
10161 length, whereas in DWARF3 it's always sized as an
10162 offset. */
10163 if (dwarf_version == 2)
10164 size = DWARF2_ADDR_SIZE;
10165 else
10166 size = DWARF_OFFSET_SIZE;
10167 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
10168 name);
10171 else
10173 gcc_assert (AT_ref (a)->die_offset);
10174 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
10175 "%s", name);
10177 break;
10179 case dw_val_class_fde_ref:
10181 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
10183 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
10184 a->dw_attr_val.v.val_fde_index * 2);
10185 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
10186 "%s", name);
10188 break;
10190 case dw_val_class_vms_delta:
10191 #ifdef ASM_OUTPUT_DWARF_VMS_DELTA
10192 dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
10193 AT_vms_delta2 (a), AT_vms_delta1 (a),
10194 "%s", name);
10195 #else
10196 dw2_asm_output_delta (DWARF_OFFSET_SIZE,
10197 AT_vms_delta2 (a), AT_vms_delta1 (a),
10198 "%s", name);
10199 #endif
10200 break;
10202 case dw_val_class_lbl_id:
10203 output_attr_index_or_value (a);
10204 break;
10206 case dw_val_class_lineptr:
10207 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
10208 debug_line_section, "%s", name);
10209 break;
10211 case dw_val_class_macptr:
10212 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
10213 debug_macinfo_section, "%s", name);
10214 break;
10216 case dw_val_class_loclistsptr:
10217 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
10218 debug_loc_section, "%s", name);
10219 break;
10221 case dw_val_class_str:
10222 if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
10223 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
10224 a->dw_attr_val.v.val_str->label,
10225 debug_str_section,
10226 "%s: \"%s\"", name, AT_string (a));
10227 else if (a->dw_attr_val.v.val_str->form == DW_FORM_line_strp)
10228 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
10229 a->dw_attr_val.v.val_str->label,
10230 debug_line_str_section,
10231 "%s: \"%s\"", name, AT_string (a));
10232 else if (a->dw_attr_val.v.val_str->form == DW_FORM_GNU_str_index)
10233 dw2_asm_output_data_uleb128 (AT_index (a),
10234 "%s: \"%s\"", name, AT_string (a));
10235 else
10236 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
10237 break;
10239 case dw_val_class_file:
10241 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
10243 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
10244 a->dw_attr_val.v.val_file->filename);
10245 break;
10248 case dw_val_class_file_implicit:
10249 if (flag_debug_asm)
10250 fprintf (asm_out_file, "\t\t\t%s %s (%d, %s)\n",
10251 ASM_COMMENT_START, name,
10252 maybe_emit_file (a->dw_attr_val.v.val_file),
10253 a->dw_attr_val.v.val_file->filename);
10254 break;
10256 case dw_val_class_data8:
10258 int i;
10260 for (i = 0; i < 8; i++)
10261 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
10262 i == 0 ? "%s" : NULL, name);
10263 break;
10266 case dw_val_class_high_pc:
10267 dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
10268 get_AT_low_pc (die), "DW_AT_high_pc");
10269 break;
10271 case dw_val_class_discr_value:
10272 output_discr_value (&a->dw_attr_val.v.val_discr_value, name);
10273 break;
10275 case dw_val_class_discr_list:
10277 dw_discr_list_ref list = AT_discr_list (a);
10278 const int size = size_of_discr_list (list);
10280 /* This is a block, so output its length first. */
10281 dw2_asm_output_data (constant_size (size), size,
10282 "%s: block size", name);
10284 for (; list != NULL; list = list->dw_discr_next)
10286 /* One byte for the discriminant value descriptor, and then as
10287 many LEB128 numbers as required. */
10288 if (list->dw_discr_range)
10289 dw2_asm_output_data (1, DW_DSC_range,
10290 "%s: DW_DSC_range", name);
10291 else
10292 dw2_asm_output_data (1, DW_DSC_label,
10293 "%s: DW_DSC_label", name);
10295 output_discr_value (&list->dw_discr_lower_bound, name);
10296 if (list->dw_discr_range)
10297 output_discr_value (&list->dw_discr_upper_bound, name);
10299 break;
10302 default:
10303 gcc_unreachable ();
10307 FOR_EACH_CHILD (die, c, output_die (c));
10309 /* Add null byte to terminate sibling list. */
10310 if (die->die_child != NULL)
10311 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
10312 (unsigned long) die->die_offset);
10315 /* Output the compilation unit that appears at the beginning of the
10316 .debug_info section, and precedes the DIE descriptions. */
10318 static void
10319 output_compilation_unit_header (enum dwarf_unit_type ut)
10321 if (!XCOFF_DEBUGGING_INFO)
10323 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10324 dw2_asm_output_data (4, 0xffffffff,
10325 "Initial length escape value indicating 64-bit DWARF extension");
10326 dw2_asm_output_data (DWARF_OFFSET_SIZE,
10327 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
10328 "Length of Compilation Unit Info");
10331 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
10332 if (dwarf_version >= 5)
10334 const char *name;
10335 switch (ut)
10337 case DW_UT_compile: name = "DW_UT_compile"; break;
10338 case DW_UT_type: name = "DW_UT_type"; break;
10339 case DW_UT_split_compile: name = "DW_UT_split_compile"; break;
10340 case DW_UT_split_type: name = "DW_UT_split_type"; break;
10341 default: gcc_unreachable ();
10343 dw2_asm_output_data (1, ut, "%s", name);
10344 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10346 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
10347 debug_abbrev_section,
10348 "Offset Into Abbrev. Section");
10349 if (dwarf_version < 5)
10350 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10353 /* Output the compilation unit DIE and its children. */
10355 static void
10356 output_comp_unit (dw_die_ref die, int output_if_empty,
10357 const unsigned char *dwo_id)
10359 const char *secname, *oldsym;
10360 char *tmp;
10362 /* Unless we are outputting main CU, we may throw away empty ones. */
10363 if (!output_if_empty && die->die_child == NULL)
10364 return;
10366 /* Even if there are no children of this DIE, we must output the information
10367 about the compilation unit. Otherwise, on an empty translation unit, we
10368 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
10369 will then complain when examining the file. First mark all the DIEs in
10370 this CU so we know which get local refs. */
10371 mark_dies (die);
10373 external_ref_hash_type *extern_map = optimize_external_refs (die);
10375 /* For now, optimize only the main CU, in order to optimize the rest
10376 we'd need to see all of them earlier. Leave the rest for post-linking
10377 tools like DWZ. */
10378 if (die == comp_unit_die ())
10379 abbrev_opt_start = vec_safe_length (abbrev_die_table);
10381 build_abbrev_table (die, extern_map);
10383 optimize_abbrev_table ();
10385 delete extern_map;
10387 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
10388 next_die_offset = (dwo_id
10389 ? DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
10390 : DWARF_COMPILE_UNIT_HEADER_SIZE);
10391 calc_die_sizes (die);
10393 oldsym = die->die_id.die_symbol;
10394 if (oldsym)
10396 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
10398 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
10399 secname = tmp;
10400 die->die_id.die_symbol = NULL;
10401 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
10403 else
10405 switch_to_section (debug_info_section);
10406 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
10407 info_section_emitted = true;
10410 /* Output debugging information. */
10411 output_compilation_unit_header (dwo_id
10412 ? DW_UT_split_compile : DW_UT_compile);
10413 if (dwarf_version >= 5)
10415 if (dwo_id != NULL)
10416 for (int i = 0; i < 8; i++)
10417 dw2_asm_output_data (1, dwo_id[i], i == 0 ? "DWO id" : NULL);
10419 output_die (die);
10421 /* Leave the marks on the main CU, so we can check them in
10422 output_pubnames. */
10423 if (oldsym)
10425 unmark_dies (die);
10426 die->die_id.die_symbol = oldsym;
10430 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
10431 and .debug_pubtypes. This is configured per-target, but can be
10432 overridden by the -gpubnames or -gno-pubnames options. */
10434 static inline bool
10435 want_pubnames (void)
10437 if (debug_info_level <= DINFO_LEVEL_TERSE)
10438 return false;
10439 if (debug_generate_pub_sections != -1)
10440 return debug_generate_pub_sections;
10441 return targetm.want_debug_pub_sections;
10444 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes. */
10446 static void
10447 add_AT_pubnames (dw_die_ref die)
10449 if (want_pubnames ())
10450 add_AT_flag (die, DW_AT_GNU_pubnames, 1);
10453 /* Add a string attribute value to a skeleton DIE. */
10455 static inline void
10456 add_skeleton_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
10457 const char *str)
10459 dw_attr_node attr;
10460 struct indirect_string_node *node;
10462 if (! skeleton_debug_str_hash)
10463 skeleton_debug_str_hash
10464 = hash_table<indirect_string_hasher>::create_ggc (10);
10466 node = find_AT_string_in_table (str, skeleton_debug_str_hash);
10467 find_string_form (node);
10468 if (node->form == DW_FORM_GNU_str_index)
10469 node->form = DW_FORM_strp;
10471 attr.dw_attr = attr_kind;
10472 attr.dw_attr_val.val_class = dw_val_class_str;
10473 attr.dw_attr_val.val_entry = NULL;
10474 attr.dw_attr_val.v.val_str = node;
10475 add_dwarf_attr (die, &attr);
10478 /* Helper function to generate top-level dies for skeleton debug_info and
10479 debug_types. */
10481 static void
10482 add_top_level_skeleton_die_attrs (dw_die_ref die)
10484 const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
10485 const char *comp_dir = comp_dir_string ();
10487 add_skeleton_AT_string (die, dwarf_AT (DW_AT_dwo_name), dwo_file_name);
10488 if (comp_dir != NULL)
10489 add_skeleton_AT_string (die, DW_AT_comp_dir, comp_dir);
10490 add_AT_pubnames (die);
10491 add_AT_lineptr (die, DW_AT_GNU_addr_base, debug_addr_section_label);
10494 /* Output skeleton debug sections that point to the dwo file. */
10496 static void
10497 output_skeleton_debug_sections (dw_die_ref comp_unit,
10498 const unsigned char *dwo_id)
10500 /* These attributes will be found in the full debug_info section. */
10501 remove_AT (comp_unit, DW_AT_producer);
10502 remove_AT (comp_unit, DW_AT_language);
10504 switch_to_section (debug_skeleton_info_section);
10505 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
10507 /* Produce the skeleton compilation-unit header. This one differs enough from
10508 a normal CU header that it's better not to call output_compilation_unit
10509 header. */
10510 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10511 dw2_asm_output_data (4, 0xffffffff,
10512 "Initial length escape value indicating 64-bit "
10513 "DWARF extension");
10515 dw2_asm_output_data (DWARF_OFFSET_SIZE,
10516 DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
10517 - DWARF_INITIAL_LENGTH_SIZE
10518 + size_of_die (comp_unit),
10519 "Length of Compilation Unit Info");
10520 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
10521 if (dwarf_version >= 5)
10523 dw2_asm_output_data (1, DW_UT_skeleton, "DW_UT_skeleton");
10524 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10526 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_abbrev_section_label,
10527 debug_skeleton_abbrev_section,
10528 "Offset Into Abbrev. Section");
10529 if (dwarf_version < 5)
10530 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10531 else
10532 for (int i = 0; i < 8; i++)
10533 dw2_asm_output_data (1, dwo_id[i], i == 0 ? "DWO id" : NULL);
10535 comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
10536 output_die (comp_unit);
10538 /* Build the skeleton debug_abbrev section. */
10539 switch_to_section (debug_skeleton_abbrev_section);
10540 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
10542 output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
10544 dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
10547 /* Output a comdat type unit DIE and its children. */
10549 static void
10550 output_comdat_type_unit (comdat_type_node *node)
10552 const char *secname;
10553 char *tmp;
10554 int i;
10555 #if defined (OBJECT_FORMAT_ELF)
10556 tree comdat_key;
10557 #endif
10559 /* First mark all the DIEs in this CU so we know which get local refs. */
10560 mark_dies (node->root_die);
10562 external_ref_hash_type *extern_map = optimize_external_refs (node->root_die);
10564 build_abbrev_table (node->root_die, extern_map);
10566 delete extern_map;
10567 extern_map = NULL;
10569 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
10570 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
10571 calc_die_sizes (node->root_die);
10573 #if defined (OBJECT_FORMAT_ELF)
10574 if (dwarf_version >= 5)
10576 if (!dwarf_split_debug_info)
10577 secname = ".debug_info";
10578 else
10579 secname = ".debug_info.dwo";
10581 else if (!dwarf_split_debug_info)
10582 secname = ".debug_types";
10583 else
10584 secname = ".debug_types.dwo";
10586 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
10587 sprintf (tmp, dwarf_version >= 5 ? "wi." : "wt.");
10588 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
10589 sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
10590 comdat_key = get_identifier (tmp);
10591 targetm.asm_out.named_section (secname,
10592 SECTION_DEBUG | SECTION_LINKONCE,
10593 comdat_key);
10594 #else
10595 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
10596 sprintf (tmp, (dwarf_version >= 5
10597 ? ".gnu.linkonce.wi." : ".gnu.linkonce.wt."));
10598 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
10599 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
10600 secname = tmp;
10601 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
10602 #endif
10604 /* Output debugging information. */
10605 output_compilation_unit_header (dwarf_split_debug_info
10606 ? DW_UT_split_type : DW_UT_type);
10607 output_signature (node->signature, "Type Signature");
10608 dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
10609 "Offset to Type DIE");
10610 output_die (node->root_die);
10612 unmark_dies (node->root_die);
10615 /* Return the DWARF2/3 pubname associated with a decl. */
10617 static const char *
10618 dwarf2_name (tree decl, int scope)
10620 if (DECL_NAMELESS (decl))
10621 return NULL;
10622 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
10625 /* Add a new entry to .debug_pubnames if appropriate. */
10627 static void
10628 add_pubname_string (const char *str, dw_die_ref die)
10630 pubname_entry e;
10632 e.die = die;
10633 e.name = xstrdup (str);
10634 vec_safe_push (pubname_table, e);
10637 static void
10638 add_pubname (tree decl, dw_die_ref die)
10640 if (!want_pubnames ())
10641 return;
10643 /* Don't add items to the table when we expect that the consumer will have
10644 just read the enclosing die. For example, if the consumer is looking at a
10645 class_member, it will either be inside the class already, or will have just
10646 looked up the class to find the member. Either way, searching the class is
10647 faster than searching the index. */
10648 if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
10649 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
10651 const char *name = dwarf2_name (decl, 1);
10653 if (name)
10654 add_pubname_string (name, die);
10658 /* Add an enumerator to the pubnames section. */
10660 static void
10661 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
10663 pubname_entry e;
10665 gcc_assert (scope_name);
10666 e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
10667 e.die = die;
10668 vec_safe_push (pubname_table, e);
10671 /* Add a new entry to .debug_pubtypes if appropriate. */
10673 static void
10674 add_pubtype (tree decl, dw_die_ref die)
10676 pubname_entry e;
10678 if (!want_pubnames ())
10679 return;
10681 if ((TREE_PUBLIC (decl)
10682 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
10683 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
10685 tree scope = NULL;
10686 const char *scope_name = "";
10687 const char *sep = is_cxx () ? "::" : ".";
10688 const char *name;
10690 scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
10691 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
10693 scope_name = lang_hooks.dwarf_name (scope, 1);
10694 if (scope_name != NULL && scope_name[0] != '\0')
10695 scope_name = concat (scope_name, sep, NULL);
10696 else
10697 scope_name = "";
10700 if (TYPE_P (decl))
10701 name = type_tag (decl);
10702 else
10703 name = lang_hooks.dwarf_name (decl, 1);
10705 /* If we don't have a name for the type, there's no point in adding
10706 it to the table. */
10707 if (name != NULL && name[0] != '\0')
10709 e.die = die;
10710 e.name = concat (scope_name, name, NULL);
10711 vec_safe_push (pubtype_table, e);
10714 /* Although it might be more consistent to add the pubinfo for the
10715 enumerators as their dies are created, they should only be added if the
10716 enum type meets the criteria above. So rather than re-check the parent
10717 enum type whenever an enumerator die is created, just output them all
10718 here. This isn't protected by the name conditional because anonymous
10719 enums don't have names. */
10720 if (die->die_tag == DW_TAG_enumeration_type)
10722 dw_die_ref c;
10724 FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
10729 /* Output a single entry in the pubnames table. */
10731 static void
10732 output_pubname (dw_offset die_offset, pubname_entry *entry)
10734 dw_die_ref die = entry->die;
10735 int is_static = get_AT_flag (die, DW_AT_external) ? 0 : 1;
10737 dw2_asm_output_data (DWARF_OFFSET_SIZE, die_offset, "DIE offset");
10739 if (debug_generate_pub_sections == 2)
10741 /* This logic follows gdb's method for determining the value of the flag
10742 byte. */
10743 uint32_t flags = GDB_INDEX_SYMBOL_KIND_NONE;
10744 switch (die->die_tag)
10746 case DW_TAG_typedef:
10747 case DW_TAG_base_type:
10748 case DW_TAG_subrange_type:
10749 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
10750 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
10751 break;
10752 case DW_TAG_enumerator:
10753 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
10754 GDB_INDEX_SYMBOL_KIND_VARIABLE);
10755 if (!is_cxx () && !is_java ())
10756 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
10757 break;
10758 case DW_TAG_subprogram:
10759 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
10760 GDB_INDEX_SYMBOL_KIND_FUNCTION);
10761 if (!is_ada ())
10762 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
10763 break;
10764 case DW_TAG_constant:
10765 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
10766 GDB_INDEX_SYMBOL_KIND_VARIABLE);
10767 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
10768 break;
10769 case DW_TAG_variable:
10770 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
10771 GDB_INDEX_SYMBOL_KIND_VARIABLE);
10772 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
10773 break;
10774 case DW_TAG_namespace:
10775 case DW_TAG_imported_declaration:
10776 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
10777 break;
10778 case DW_TAG_class_type:
10779 case DW_TAG_interface_type:
10780 case DW_TAG_structure_type:
10781 case DW_TAG_union_type:
10782 case DW_TAG_enumeration_type:
10783 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
10784 if (!is_cxx () && !is_java ())
10785 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
10786 break;
10787 default:
10788 /* An unusual tag. Leave the flag-byte empty. */
10789 break;
10791 dw2_asm_output_data (1, flags >> GDB_INDEX_CU_BITSIZE,
10792 "GDB-index flags");
10795 dw2_asm_output_nstring (entry->name, -1, "external name");
10799 /* Output the public names table used to speed up access to externally
10800 visible names; or the public types table used to find type definitions. */
10802 static void
10803 output_pubnames (vec<pubname_entry, va_gc> *names)
10805 unsigned i;
10806 unsigned long pubnames_length = size_of_pubnames (names);
10807 pubname_entry *pub;
10809 if (!XCOFF_DEBUGGING_INFO)
10811 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10812 dw2_asm_output_data (4, 0xffffffff,
10813 "Initial length escape value indicating 64-bit DWARF extension");
10814 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
10815 "Pub Info Length");
10818 /* Version number for pubnames/pubtypes is independent of dwarf version. */
10819 dw2_asm_output_data (2, 2, "DWARF Version");
10821 if (dwarf_split_debug_info)
10822 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
10823 debug_skeleton_info_section,
10824 "Offset of Compilation Unit Info");
10825 else
10826 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
10827 debug_info_section,
10828 "Offset of Compilation Unit Info");
10829 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
10830 "Compilation Unit Length");
10832 FOR_EACH_VEC_ELT (*names, i, pub)
10834 if (include_pubname_in_output (names, pub))
10836 dw_offset die_offset = pub->die->die_offset;
10838 /* We shouldn't see pubnames for DIEs outside of the main CU. */
10839 if (names == pubname_table && pub->die->die_tag != DW_TAG_enumerator)
10840 gcc_assert (pub->die->die_mark);
10842 /* If we're putting types in their own .debug_types sections,
10843 the .debug_pubtypes table will still point to the compile
10844 unit (not the type unit), so we want to use the offset of
10845 the skeleton DIE (if there is one). */
10846 if (pub->die->comdat_type_p && names == pubtype_table)
10848 comdat_type_node *type_node = pub->die->die_id.die_type_node;
10850 if (type_node != NULL)
10851 die_offset = (type_node->skeleton_die != NULL
10852 ? type_node->skeleton_die->die_offset
10853 : comp_unit_die ()->die_offset);
10856 output_pubname (die_offset, pub);
10860 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
10863 /* Output public names and types tables if necessary. */
10865 static void
10866 output_pubtables (void)
10868 if (!want_pubnames () || !info_section_emitted)
10869 return;
10871 switch_to_section (debug_pubnames_section);
10872 output_pubnames (pubname_table);
10873 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
10874 It shouldn't hurt to emit it always, since pure DWARF2 consumers
10875 simply won't look for the section. */
10876 switch_to_section (debug_pubtypes_section);
10877 output_pubnames (pubtype_table);
10881 /* Output the information that goes into the .debug_aranges table.
10882 Namely, define the beginning and ending address range of the
10883 text section generated for this compilation unit. */
10885 static void
10886 output_aranges (void)
10888 unsigned i;
10889 unsigned long aranges_length = size_of_aranges ();
10891 if (!XCOFF_DEBUGGING_INFO)
10893 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10894 dw2_asm_output_data (4, 0xffffffff,
10895 "Initial length escape value indicating 64-bit DWARF extension");
10896 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
10897 "Length of Address Ranges Info");
10900 /* Version number for aranges is still 2, even up to DWARF5. */
10901 dw2_asm_output_data (2, 2, "DWARF Version");
10902 if (dwarf_split_debug_info)
10903 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
10904 debug_skeleton_info_section,
10905 "Offset of Compilation Unit Info");
10906 else
10907 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
10908 debug_info_section,
10909 "Offset of Compilation Unit Info");
10910 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
10911 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
10913 /* We need to align to twice the pointer size here. */
10914 if (DWARF_ARANGES_PAD_SIZE)
10916 /* Pad using a 2 byte words so that padding is correct for any
10917 pointer size. */
10918 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
10919 2 * DWARF2_ADDR_SIZE);
10920 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
10921 dw2_asm_output_data (2, 0, NULL);
10924 /* It is necessary not to output these entries if the sections were
10925 not used; if the sections were not used, the length will be 0 and
10926 the address may end up as 0 if the section is discarded by ld
10927 --gc-sections, leaving an invalid (0, 0) entry that can be
10928 confused with the terminator. */
10929 if (text_section_used)
10931 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
10932 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
10933 text_section_label, "Length");
10935 if (cold_text_section_used)
10937 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
10938 "Address");
10939 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
10940 cold_text_section_label, "Length");
10943 if (have_multiple_function_sections)
10945 unsigned fde_idx;
10946 dw_fde_ref fde;
10948 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
10950 if (DECL_IGNORED_P (fde->decl))
10951 continue;
10952 if (!fde->in_std_section)
10954 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
10955 "Address");
10956 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
10957 fde->dw_fde_begin, "Length");
10959 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
10961 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
10962 "Address");
10963 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
10964 fde->dw_fde_second_begin, "Length");
10969 /* Output the terminator words. */
10970 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
10971 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
10974 /* Add a new entry to .debug_ranges. Return its index into
10975 ranges_table vector. */
10977 static unsigned int
10978 add_ranges_num (int num, bool maybe_new_sec)
10980 dw_ranges r = { NULL, num, 0, maybe_new_sec };
10981 vec_safe_push (ranges_table, r);
10982 return vec_safe_length (ranges_table) - 1;
10985 /* Add a new entry to .debug_ranges corresponding to a block, or a
10986 range terminator if BLOCK is NULL. MAYBE_NEW_SEC is true if
10987 this entry might be in a different section from previous range. */
10989 static unsigned int
10990 add_ranges (const_tree block, bool maybe_new_sec)
10992 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0, maybe_new_sec);
10995 /* Note that (*rnglist_table)[offset] is either a head of a rnglist
10996 chain, or middle entry of a chain that will be directly referred to. */
10998 static void
10999 note_rnglist_head (unsigned int offset)
11001 if (dwarf_version < 5 || (*ranges_table)[offset].label)
11002 return;
11003 (*ranges_table)[offset].label = gen_internal_sym ("LLRL");
11006 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
11007 When using dwarf_split_debug_info, address attributes in dies destined
11008 for the final executable should be direct references--setting the
11009 parameter force_direct ensures this behavior. */
11011 static void
11012 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
11013 bool *added, bool force_direct)
11015 unsigned int in_use = vec_safe_length (ranges_by_label);
11016 unsigned int offset;
11017 dw_ranges_by_label rbl = { begin, end };
11018 vec_safe_push (ranges_by_label, rbl);
11019 offset = add_ranges_num (-(int)in_use - 1, true);
11020 if (!*added)
11022 add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
11023 *added = true;
11024 note_rnglist_head (offset);
11028 /* Emit .debug_ranges section. */
11030 static void
11031 output_ranges (void)
11033 unsigned i;
11034 static const char *const start_fmt = "Offset %#x";
11035 const char *fmt = start_fmt;
11036 dw_ranges *r;
11038 switch_to_section (debug_ranges_section);
11039 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
11040 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11042 int block_num = r->num;
11044 if (block_num > 0)
11046 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
11047 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
11049 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
11050 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
11052 /* If all code is in the text section, then the compilation
11053 unit base address defaults to DW_AT_low_pc, which is the
11054 base of the text section. */
11055 if (!have_multiple_function_sections)
11057 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
11058 text_section_label,
11059 fmt, i * 2 * DWARF2_ADDR_SIZE);
11060 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
11061 text_section_label, NULL);
11064 /* Otherwise, the compilation unit base address is zero,
11065 which allows us to use absolute addresses, and not worry
11066 about whether the target supports cross-section
11067 arithmetic. */
11068 else
11070 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11071 fmt, i * 2 * DWARF2_ADDR_SIZE);
11072 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
11075 fmt = NULL;
11078 /* Negative block_num stands for an index into ranges_by_label. */
11079 else if (block_num < 0)
11081 int lab_idx = - block_num - 1;
11083 if (!have_multiple_function_sections)
11085 gcc_unreachable ();
11086 #if 0
11087 /* If we ever use add_ranges_by_labels () for a single
11088 function section, all we have to do is to take out
11089 the #if 0 above. */
11090 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11091 (*ranges_by_label)[lab_idx].begin,
11092 text_section_label,
11093 fmt, i * 2 * DWARF2_ADDR_SIZE);
11094 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11095 (*ranges_by_label)[lab_idx].end,
11096 text_section_label, NULL);
11097 #endif
11099 else
11101 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11102 (*ranges_by_label)[lab_idx].begin,
11103 fmt, i * 2 * DWARF2_ADDR_SIZE);
11104 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11105 (*ranges_by_label)[lab_idx].end,
11106 NULL);
11109 else
11111 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11112 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11113 fmt = start_fmt;
11118 /* Non-zero if .debug_line_str should be used for .debug_line section
11119 strings or strings that are likely shareable with those. */
11120 #define DWARF5_USE_DEBUG_LINE_STR \
11121 (!DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET \
11122 && (DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) != 0 \
11123 /* FIXME: there is no .debug_line_str.dwo section, \
11124 for -gsplit-dwarf we should use DW_FORM_strx instead. */ \
11125 && !dwarf_split_debug_info)
11127 /* Assign .debug_rnglists indexes. */
11129 static void
11130 index_rnglists (void)
11132 unsigned i;
11133 dw_ranges *r;
11135 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11136 if (r->label)
11137 r->idx = rnglist_idx++;
11140 /* Emit .debug_rnglists section. */
11142 static void
11143 output_rnglists (void)
11145 unsigned i;
11146 dw_ranges *r;
11147 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
11148 char l2[MAX_ARTIFICIAL_LABEL_BYTES];
11149 char basebuf[MAX_ARTIFICIAL_LABEL_BYTES];
11151 switch_to_section (debug_ranges_section);
11152 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
11153 ASM_GENERATE_INTERNAL_LABEL (l1, DEBUG_RANGES_SECTION_LABEL, 2);
11154 ASM_GENERATE_INTERNAL_LABEL (l2, DEBUG_RANGES_SECTION_LABEL, 3);
11155 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11156 dw2_asm_output_data (4, 0xffffffff,
11157 "Initial length escape value indicating "
11158 "64-bit DWARF extension");
11159 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
11160 "Length of Range Lists");
11161 ASM_OUTPUT_LABEL (asm_out_file, l1);
11162 dw2_asm_output_data (2, dwarf_version, "DWARF Version");
11163 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
11164 dw2_asm_output_data (1, 0, "Segment Size");
11165 /* Emit the offset table only for -gsplit-dwarf. If we don't care
11166 about relocation sizes and primarily care about the size of .debug*
11167 sections in linked shared libraries and executables, then
11168 the offset table plus corresponding DW_FORM_rnglistx uleb128 indexes
11169 into it are usually larger than just DW_FORM_sec_offset offsets
11170 into the .debug_rnglists section. */
11171 dw2_asm_output_data (4, dwarf_split_debug_info ? rnglist_idx : 0,
11172 "Offset Entry Count");
11173 if (dwarf_split_debug_info)
11175 ASM_OUTPUT_LABEL (asm_out_file, ranges_base_label);
11176 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11177 if (r->label)
11178 dw2_asm_output_delta (DWARF_OFFSET_SIZE, r->label,
11179 ranges_base_label, NULL);
11182 const char *lab = "";
11183 unsigned int len = vec_safe_length (ranges_table);
11184 const char *base = NULL;
11185 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11187 int block_num = r->num;
11189 if (r->label)
11191 ASM_OUTPUT_LABEL (asm_out_file, r->label);
11192 lab = r->label;
11194 if (HAVE_AS_LEB128 && (r->label || r->maybe_new_sec))
11195 base = NULL;
11196 if (block_num > 0)
11198 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
11199 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
11201 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
11202 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
11204 if (HAVE_AS_LEB128)
11206 /* If all code is in the text section, then the compilation
11207 unit base address defaults to DW_AT_low_pc, which is the
11208 base of the text section. */
11209 if (!have_multiple_function_sections)
11211 dw2_asm_output_data (1, DW_RLE_offset_pair,
11212 "DW_RLE_offset_pair (%s)", lab);
11213 dw2_asm_output_delta_uleb128 (blabel, text_section_label,
11214 "Range begin address (%s)", lab);
11215 dw2_asm_output_delta_uleb128 (elabel, text_section_label,
11216 "Range end address (%s)", lab);
11217 continue;
11219 if (base == NULL)
11221 dw_ranges *r2 = NULL;
11222 if (i < len - 1)
11223 r2 = &(*ranges_table)[i + 1];
11224 if (r2
11225 && r2->num != 0
11226 && r2->label == NULL
11227 && !r2->maybe_new_sec)
11229 dw2_asm_output_data (1, DW_RLE_base_address,
11230 "DW_RLE_base_address (%s)", lab);
11231 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11232 "Base address (%s)", lab);
11233 strcpy (basebuf, blabel);
11234 base = basebuf;
11237 if (base)
11239 dw2_asm_output_data (1, DW_RLE_offset_pair,
11240 "DW_RLE_offset_pair (%s)", lab);
11241 dw2_asm_output_delta_uleb128 (blabel, base,
11242 "Range begin address (%s)", lab);
11243 dw2_asm_output_delta_uleb128 (elabel, base,
11244 "Range end address (%s)", lab);
11245 continue;
11247 dw2_asm_output_data (1, DW_RLE_start_length,
11248 "DW_RLE_start_length (%s)", lab);
11249 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11250 "Range begin address (%s)", lab);
11251 dw2_asm_output_delta_uleb128 (elabel, blabel,
11252 "Range length (%s)", lab);
11254 else
11256 dw2_asm_output_data (1, DW_RLE_start_end,
11257 "DW_RLE_start_end (%s)", lab);
11258 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11259 "Range begin address (%s)", lab);
11260 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel,
11261 "Range end address (%s)", lab);
11265 /* Negative block_num stands for an index into ranges_by_label. */
11266 else if (block_num < 0)
11268 int lab_idx = - block_num - 1;
11269 const char *blabel = (*ranges_by_label)[lab_idx].begin;
11270 const char *elabel = (*ranges_by_label)[lab_idx].end;
11272 if (!have_multiple_function_sections)
11273 gcc_unreachable ();
11274 if (HAVE_AS_LEB128)
11276 dw2_asm_output_data (1, DW_RLE_start_length,
11277 "DW_RLE_start_length (%s)", lab);
11278 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11279 "Range begin address (%s)", lab);
11280 dw2_asm_output_delta_uleb128 (elabel, blabel,
11281 "Range length (%s)", lab);
11283 else
11285 dw2_asm_output_data (1, DW_RLE_start_end,
11286 "DW_RLE_start_end (%s)", lab);
11287 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11288 "Range begin address (%s)", lab);
11289 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel,
11290 "Range end address (%s)", lab);
11293 else
11294 dw2_asm_output_data (1, DW_RLE_end_of_list,
11295 "DW_RLE_end_of_list (%s)", lab);
11297 ASM_OUTPUT_LABEL (asm_out_file, l2);
11300 /* Data structure containing information about input files. */
11301 struct file_info
11303 const char *path; /* Complete file name. */
11304 const char *fname; /* File name part. */
11305 int length; /* Length of entire string. */
11306 struct dwarf_file_data * file_idx; /* Index in input file table. */
11307 int dir_idx; /* Index in directory table. */
11310 /* Data structure containing information about directories with source
11311 files. */
11312 struct dir_info
11314 const char *path; /* Path including directory name. */
11315 int length; /* Path length. */
11316 int prefix; /* Index of directory entry which is a prefix. */
11317 int count; /* Number of files in this directory. */
11318 int dir_idx; /* Index of directory used as base. */
11321 /* Callback function for file_info comparison. We sort by looking at
11322 the directories in the path. */
11324 static int
11325 file_info_cmp (const void *p1, const void *p2)
11327 const struct file_info *const s1 = (const struct file_info *) p1;
11328 const struct file_info *const s2 = (const struct file_info *) p2;
11329 const unsigned char *cp1;
11330 const unsigned char *cp2;
11332 /* Take care of file names without directories. We need to make sure that
11333 we return consistent values to qsort since some will get confused if
11334 we return the same value when identical operands are passed in opposite
11335 orders. So if neither has a directory, return 0 and otherwise return
11336 1 or -1 depending on which one has the directory. */
11337 if ((s1->path == s1->fname || s2->path == s2->fname))
11338 return (s2->path == s2->fname) - (s1->path == s1->fname);
11340 cp1 = (const unsigned char *) s1->path;
11341 cp2 = (const unsigned char *) s2->path;
11343 while (1)
11345 ++cp1;
11346 ++cp2;
11347 /* Reached the end of the first path? If so, handle like above. */
11348 if ((cp1 == (const unsigned char *) s1->fname)
11349 || (cp2 == (const unsigned char *) s2->fname))
11350 return ((cp2 == (const unsigned char *) s2->fname)
11351 - (cp1 == (const unsigned char *) s1->fname));
11353 /* Character of current path component the same? */
11354 else if (*cp1 != *cp2)
11355 return *cp1 - *cp2;
11359 struct file_name_acquire_data
11361 struct file_info *files;
11362 int used_files;
11363 int max_files;
11366 /* Traversal function for the hash table. */
11369 file_name_acquire (dwarf_file_data **slot, file_name_acquire_data *fnad)
11371 struct dwarf_file_data *d = *slot;
11372 struct file_info *fi;
11373 const char *f;
11375 gcc_assert (fnad->max_files >= d->emitted_number);
11377 if (! d->emitted_number)
11378 return 1;
11380 gcc_assert (fnad->max_files != fnad->used_files);
11382 fi = fnad->files + fnad->used_files++;
11384 /* Skip all leading "./". */
11385 f = d->filename;
11386 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
11387 f += 2;
11389 /* Create a new array entry. */
11390 fi->path = f;
11391 fi->length = strlen (f);
11392 fi->file_idx = d;
11394 /* Search for the file name part. */
11395 f = strrchr (f, DIR_SEPARATOR);
11396 #if defined (DIR_SEPARATOR_2)
11398 char *g = strrchr (fi->path, DIR_SEPARATOR_2);
11400 if (g != NULL)
11402 if (f == NULL || f < g)
11403 f = g;
11406 #endif
11408 fi->fname = f == NULL ? fi->path : f + 1;
11409 return 1;
11412 /* Helper function for output_file_names. Emit a FORM encoded
11413 string STR, with assembly comment start ENTRY_KIND and
11414 index IDX */
11416 static void
11417 output_line_string (enum dwarf_form form, const char *str,
11418 const char *entry_kind, unsigned int idx)
11420 switch (form)
11422 case DW_FORM_string:
11423 dw2_asm_output_nstring (str, -1, "%s: %#x", entry_kind, idx);
11424 break;
11425 case DW_FORM_line_strp:
11426 if (!debug_line_str_hash)
11427 debug_line_str_hash
11428 = hash_table<indirect_string_hasher>::create_ggc (10);
11430 struct indirect_string_node *node;
11431 node = find_AT_string_in_table (str, debug_line_str_hash);
11432 set_indirect_string (node);
11433 node->form = form;
11434 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
11435 debug_line_str_section, "%s: %#x: \"%s\"",
11436 entry_kind, 0, node->str);
11437 break;
11438 default:
11439 gcc_unreachable ();
11443 /* Output the directory table and the file name table. We try to minimize
11444 the total amount of memory needed. A heuristic is used to avoid large
11445 slowdowns with many input files. */
11447 static void
11448 output_file_names (void)
11450 struct file_name_acquire_data fnad;
11451 int numfiles;
11452 struct file_info *files;
11453 struct dir_info *dirs;
11454 int *saved;
11455 int *savehere;
11456 int *backmap;
11457 int ndirs;
11458 int idx_offset;
11459 int i;
11461 if (!last_emitted_file)
11463 if (dwarf_version >= 5)
11465 dw2_asm_output_data (1, 0, "Directory entry format count");
11466 dw2_asm_output_data_uleb128 (0, "Directories count");
11467 dw2_asm_output_data (1, 0, "File name entry format count");
11468 dw2_asm_output_data_uleb128 (0, "File names count");
11470 else
11472 dw2_asm_output_data (1, 0, "End directory table");
11473 dw2_asm_output_data (1, 0, "End file name table");
11475 return;
11478 numfiles = last_emitted_file->emitted_number;
11480 /* Allocate the various arrays we need. */
11481 files = XALLOCAVEC (struct file_info, numfiles);
11482 dirs = XALLOCAVEC (struct dir_info, numfiles);
11484 fnad.files = files;
11485 fnad.used_files = 0;
11486 fnad.max_files = numfiles;
11487 file_table->traverse<file_name_acquire_data *, file_name_acquire> (&fnad);
11488 gcc_assert (fnad.used_files == fnad.max_files);
11490 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
11492 /* Find all the different directories used. */
11493 dirs[0].path = files[0].path;
11494 dirs[0].length = files[0].fname - files[0].path;
11495 dirs[0].prefix = -1;
11496 dirs[0].count = 1;
11497 dirs[0].dir_idx = 0;
11498 files[0].dir_idx = 0;
11499 ndirs = 1;
11501 for (i = 1; i < numfiles; i++)
11502 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
11503 && memcmp (dirs[ndirs - 1].path, files[i].path,
11504 dirs[ndirs - 1].length) == 0)
11506 /* Same directory as last entry. */
11507 files[i].dir_idx = ndirs - 1;
11508 ++dirs[ndirs - 1].count;
11510 else
11512 int j;
11514 /* This is a new directory. */
11515 dirs[ndirs].path = files[i].path;
11516 dirs[ndirs].length = files[i].fname - files[i].path;
11517 dirs[ndirs].count = 1;
11518 dirs[ndirs].dir_idx = ndirs;
11519 files[i].dir_idx = ndirs;
11521 /* Search for a prefix. */
11522 dirs[ndirs].prefix = -1;
11523 for (j = 0; j < ndirs; j++)
11524 if (dirs[j].length < dirs[ndirs].length
11525 && dirs[j].length > 1
11526 && (dirs[ndirs].prefix == -1
11527 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
11528 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
11529 dirs[ndirs].prefix = j;
11531 ++ndirs;
11534 /* Now to the actual work. We have to find a subset of the directories which
11535 allow expressing the file name using references to the directory table
11536 with the least amount of characters. We do not do an exhaustive search
11537 where we would have to check out every combination of every single
11538 possible prefix. Instead we use a heuristic which provides nearly optimal
11539 results in most cases and never is much off. */
11540 saved = XALLOCAVEC (int, ndirs);
11541 savehere = XALLOCAVEC (int, ndirs);
11543 memset (saved, '\0', ndirs * sizeof (saved[0]));
11544 for (i = 0; i < ndirs; i++)
11546 int j;
11547 int total;
11549 /* We can always save some space for the current directory. But this
11550 does not mean it will be enough to justify adding the directory. */
11551 savehere[i] = dirs[i].length;
11552 total = (savehere[i] - saved[i]) * dirs[i].count;
11554 for (j = i + 1; j < ndirs; j++)
11556 savehere[j] = 0;
11557 if (saved[j] < dirs[i].length)
11559 /* Determine whether the dirs[i] path is a prefix of the
11560 dirs[j] path. */
11561 int k;
11563 k = dirs[j].prefix;
11564 while (k != -1 && k != (int) i)
11565 k = dirs[k].prefix;
11567 if (k == (int) i)
11569 /* Yes it is. We can possibly save some memory by
11570 writing the filenames in dirs[j] relative to
11571 dirs[i]. */
11572 savehere[j] = dirs[i].length;
11573 total += (savehere[j] - saved[j]) * dirs[j].count;
11578 /* Check whether we can save enough to justify adding the dirs[i]
11579 directory. */
11580 if (total > dirs[i].length + 1)
11582 /* It's worthwhile adding. */
11583 for (j = i; j < ndirs; j++)
11584 if (savehere[j] > 0)
11586 /* Remember how much we saved for this directory so far. */
11587 saved[j] = savehere[j];
11589 /* Remember the prefix directory. */
11590 dirs[j].dir_idx = i;
11595 /* Emit the directory name table. */
11596 idx_offset = dirs[0].length > 0 ? 1 : 0;
11597 enum dwarf_form str_form = DW_FORM_string;
11598 enum dwarf_form idx_form = DW_FORM_udata;
11599 if (dwarf_version >= 5)
11601 const char *comp_dir = comp_dir_string ();
11602 if (comp_dir == NULL)
11603 comp_dir = "";
11604 dw2_asm_output_data (1, 1, "Directory entry format count");
11605 if (DWARF5_USE_DEBUG_LINE_STR)
11606 str_form = DW_FORM_line_strp;
11607 dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
11608 dw2_asm_output_data_uleb128 (str_form, "%s",
11609 get_DW_FORM_name (str_form));
11610 dw2_asm_output_data_uleb128 (ndirs + idx_offset, "Directories count");
11611 if (str_form == DW_FORM_string)
11613 dw2_asm_output_nstring (comp_dir, -1, "Directory Entry: %#x", 0);
11614 for (i = 1 - idx_offset; i < ndirs; i++)
11615 dw2_asm_output_nstring (dirs[i].path,
11616 dirs[i].length
11617 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
11618 "Directory Entry: %#x", i + idx_offset);
11620 else
11622 output_line_string (str_form, comp_dir, "Directory Entry", 0);
11623 for (i = 1 - idx_offset; i < ndirs; i++)
11625 const char *str
11626 = ggc_alloc_string (dirs[i].path,
11627 dirs[i].length
11628 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR);
11629 output_line_string (str_form, str, "Directory Entry",
11630 (unsigned) i + idx_offset);
11634 else
11636 for (i = 1 - idx_offset; i < ndirs; i++)
11637 dw2_asm_output_nstring (dirs[i].path,
11638 dirs[i].length
11639 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
11640 "Directory Entry: %#x", i + idx_offset);
11642 dw2_asm_output_data (1, 0, "End directory table");
11645 /* We have to emit them in the order of emitted_number since that's
11646 used in the debug info generation. To do this efficiently we
11647 generate a back-mapping of the indices first. */
11648 backmap = XALLOCAVEC (int, numfiles);
11649 for (i = 0; i < numfiles; i++)
11650 backmap[files[i].file_idx->emitted_number - 1] = i;
11652 if (dwarf_version >= 5)
11654 const char *filename0 = get_AT_string (comp_unit_die (), DW_AT_name);
11655 if (filename0 == NULL)
11656 filename0 = "";
11657 /* DW_LNCT_directory_index can use DW_FORM_udata, DW_FORM_data1 and
11658 DW_FORM_data2. Choose one based on the number of directories
11659 and how much space would they occupy in each encoding.
11660 If we have at most 256 directories, all indexes fit into
11661 a single byte, so DW_FORM_data1 is most compact (if there
11662 are at most 128 directories, DW_FORM_udata would be as
11663 compact as that, but not shorter and slower to decode). */
11664 if (ndirs + idx_offset <= 256)
11665 idx_form = DW_FORM_data1;
11666 /* If there are more than 65536 directories, we have to use
11667 DW_FORM_udata, DW_FORM_data2 can't refer to them.
11668 Otherwise, compute what space would occupy if all the indexes
11669 used DW_FORM_udata - sum - and compare that to how large would
11670 be DW_FORM_data2 encoding, and pick the more efficient one. */
11671 else if (ndirs + idx_offset <= 65536)
11673 unsigned HOST_WIDE_INT sum = 1;
11674 for (i = 0; i < numfiles; i++)
11676 int file_idx = backmap[i];
11677 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
11678 sum += size_of_uleb128 (dir_idx);
11680 if (sum >= HOST_WIDE_INT_UC (2) * (numfiles + 1))
11681 idx_form = DW_FORM_data2;
11683 #ifdef VMS_DEBUGGING_INFO
11684 dw2_asm_output_data (1, 4, "File name entry format count");
11685 #else
11686 dw2_asm_output_data (1, 2, "File name entry format count");
11687 #endif
11688 dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
11689 dw2_asm_output_data_uleb128 (str_form, "%s",
11690 get_DW_FORM_name (str_form));
11691 dw2_asm_output_data_uleb128 (DW_LNCT_directory_index,
11692 "DW_LNCT_directory_index");
11693 dw2_asm_output_data_uleb128 (idx_form, "%s",
11694 get_DW_FORM_name (idx_form));
11695 #ifdef VMS_DEBUGGING_INFO
11696 dw2_asm_output_data_uleb128 (DW_LNCT_timestamp, "DW_LNCT_timestamp");
11697 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
11698 dw2_asm_output_data_uleb128 (DW_LNCT_size, "DW_LNCT_size");
11699 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
11700 #endif
11701 dw2_asm_output_data_uleb128 (numfiles + 1, "File names count");
11703 output_line_string (str_form, filename0, "File Entry", 0);
11705 /* Include directory index. */
11706 if (dwarf_version >= 5 && idx_form != DW_FORM_udata)
11707 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
11708 0, NULL);
11709 else
11710 dw2_asm_output_data_uleb128 (0, NULL);
11712 #ifdef VMS_DEBUGGING_INFO
11713 dw2_asm_output_data_uleb128 (0, NULL);
11714 dw2_asm_output_data_uleb128 (0, NULL);
11715 #endif
11718 /* Now write all the file names. */
11719 for (i = 0; i < numfiles; i++)
11721 int file_idx = backmap[i];
11722 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
11724 #ifdef VMS_DEBUGGING_INFO
11725 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
11727 /* Setting these fields can lead to debugger miscomparisons,
11728 but VMS Debug requires them to be set correctly. */
11730 int ver;
11731 long long cdt;
11732 long siz;
11733 int maxfilelen = (strlen (files[file_idx].path)
11734 + dirs[dir_idx].length
11735 + MAX_VMS_VERSION_LEN + 1);
11736 char *filebuf = XALLOCAVEC (char, maxfilelen);
11738 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
11739 snprintf (filebuf, maxfilelen, "%s;%d",
11740 files[file_idx].path + dirs[dir_idx].length, ver);
11742 output_line_string (str_form, filebuf, "File Entry", (unsigned) i + 1);
11744 /* Include directory index. */
11745 if (dwarf_version >= 5 && idx_form != DW_FORM_udata)
11746 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
11747 dir_idx + idx_offset, NULL);
11748 else
11749 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
11751 /* Modification time. */
11752 dw2_asm_output_data_uleb128 ((vms_file_stats_name (files[file_idx].path,
11753 &cdt, 0, 0, 0) == 0)
11754 ? cdt : 0, NULL);
11756 /* File length in bytes. */
11757 dw2_asm_output_data_uleb128 ((vms_file_stats_name (files[file_idx].path,
11758 0, &siz, 0, 0) == 0)
11759 ? siz : 0, NULL);
11760 #else
11761 output_line_string (str_form,
11762 files[file_idx].path + dirs[dir_idx].length,
11763 "File Entry", (unsigned) i + 1);
11765 /* Include directory index. */
11766 if (dwarf_version >= 5 && idx_form != DW_FORM_udata)
11767 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
11768 dir_idx + idx_offset, NULL);
11769 else
11770 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
11772 if (dwarf_version >= 5)
11773 continue;
11775 /* Modification time. */
11776 dw2_asm_output_data_uleb128 (0, NULL);
11778 /* File length in bytes. */
11779 dw2_asm_output_data_uleb128 (0, NULL);
11780 #endif /* VMS_DEBUGGING_INFO */
11783 if (dwarf_version < 5)
11784 dw2_asm_output_data (1, 0, "End file name table");
11788 /* Output one line number table into the .debug_line section. */
11790 static void
11791 output_one_line_info_table (dw_line_info_table *table)
11793 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
11794 unsigned int current_line = 1;
11795 bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
11796 dw_line_info_entry *ent;
11797 size_t i;
11799 FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
11801 switch (ent->opcode)
11803 case LI_set_address:
11804 /* ??? Unfortunately, we have little choice here currently, and
11805 must always use the most general form. GCC does not know the
11806 address delta itself, so we can't use DW_LNS_advance_pc. Many
11807 ports do have length attributes which will give an upper bound
11808 on the address range. We could perhaps use length attributes
11809 to determine when it is safe to use DW_LNS_fixed_advance_pc. */
11810 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
11812 /* This can handle any delta. This takes
11813 4+DWARF2_ADDR_SIZE bytes. */
11814 dw2_asm_output_data (1, 0, "set address %s", line_label);
11815 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
11816 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
11817 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
11818 break;
11820 case LI_set_line:
11821 if (ent->val == current_line)
11823 /* We still need to start a new row, so output a copy insn. */
11824 dw2_asm_output_data (1, DW_LNS_copy,
11825 "copy line %u", current_line);
11827 else
11829 int line_offset = ent->val - current_line;
11830 int line_delta = line_offset - DWARF_LINE_BASE;
11832 current_line = ent->val;
11833 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
11835 /* This can handle deltas from -10 to 234, using the current
11836 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
11837 This takes 1 byte. */
11838 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
11839 "line %u", current_line);
11841 else
11843 /* This can handle any delta. This takes at least 4 bytes,
11844 depending on the value being encoded. */
11845 dw2_asm_output_data (1, DW_LNS_advance_line,
11846 "advance to line %u", current_line);
11847 dw2_asm_output_data_sleb128 (line_offset, NULL);
11848 dw2_asm_output_data (1, DW_LNS_copy, NULL);
11851 break;
11853 case LI_set_file:
11854 dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
11855 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
11856 break;
11858 case LI_set_column:
11859 dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
11860 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
11861 break;
11863 case LI_negate_stmt:
11864 current_is_stmt = !current_is_stmt;
11865 dw2_asm_output_data (1, DW_LNS_negate_stmt,
11866 "is_stmt %d", current_is_stmt);
11867 break;
11869 case LI_set_prologue_end:
11870 dw2_asm_output_data (1, DW_LNS_set_prologue_end,
11871 "set prologue end");
11872 break;
11874 case LI_set_epilogue_begin:
11875 dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
11876 "set epilogue begin");
11877 break;
11879 case LI_set_discriminator:
11880 dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
11881 dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
11882 dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
11883 dw2_asm_output_data_uleb128 (ent->val, NULL);
11884 break;
11888 /* Emit debug info for the address of the end of the table. */
11889 dw2_asm_output_data (1, 0, "set address %s", table->end_label);
11890 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
11891 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
11892 dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
11894 dw2_asm_output_data (1, 0, "end sequence");
11895 dw2_asm_output_data_uleb128 (1, NULL);
11896 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
11899 /* Output the source line number correspondence information. This
11900 information goes into the .debug_line section. */
11902 static void
11903 output_line_info (bool prologue_only)
11905 static unsigned int generation;
11906 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
11907 char p1[MAX_ARTIFICIAL_LABEL_BYTES], p2[MAX_ARTIFICIAL_LABEL_BYTES];
11908 bool saw_one = false;
11909 int opc;
11911 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, generation);
11912 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, generation);
11913 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, generation);
11914 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, generation++);
11916 if (!XCOFF_DEBUGGING_INFO)
11918 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11919 dw2_asm_output_data (4, 0xffffffff,
11920 "Initial length escape value indicating 64-bit DWARF extension");
11921 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
11922 "Length of Source Line Info");
11925 ASM_OUTPUT_LABEL (asm_out_file, l1);
11927 dw2_asm_output_data (2, dwarf_version, "DWARF Version");
11928 if (dwarf_version >= 5)
11930 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
11931 dw2_asm_output_data (1, 0, "Segment Size");
11933 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
11934 ASM_OUTPUT_LABEL (asm_out_file, p1);
11936 /* Define the architecture-dependent minimum instruction length (in bytes).
11937 In this implementation of DWARF, this field is used for information
11938 purposes only. Since GCC generates assembly language, we have no
11939 a priori knowledge of how many instruction bytes are generated for each
11940 source line, and therefore can use only the DW_LNE_set_address and
11941 DW_LNS_fixed_advance_pc line information commands. Accordingly, we fix
11942 this as '1', which is "correct enough" for all architectures,
11943 and don't let the target override. */
11944 dw2_asm_output_data (1, 1, "Minimum Instruction Length");
11946 if (dwarf_version >= 4)
11947 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
11948 "Maximum Operations Per Instruction");
11949 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
11950 "Default is_stmt_start flag");
11951 dw2_asm_output_data (1, DWARF_LINE_BASE,
11952 "Line Base Value (Special Opcodes)");
11953 dw2_asm_output_data (1, DWARF_LINE_RANGE,
11954 "Line Range Value (Special Opcodes)");
11955 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
11956 "Special Opcode Base");
11958 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
11960 int n_op_args;
11961 switch (opc)
11963 case DW_LNS_advance_pc:
11964 case DW_LNS_advance_line:
11965 case DW_LNS_set_file:
11966 case DW_LNS_set_column:
11967 case DW_LNS_fixed_advance_pc:
11968 case DW_LNS_set_isa:
11969 n_op_args = 1;
11970 break;
11971 default:
11972 n_op_args = 0;
11973 break;
11976 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
11977 opc, n_op_args);
11980 /* Write out the information about the files we use. */
11981 output_file_names ();
11982 ASM_OUTPUT_LABEL (asm_out_file, p2);
11983 if (prologue_only)
11985 /* Output the marker for the end of the line number info. */
11986 ASM_OUTPUT_LABEL (asm_out_file, l2);
11987 return;
11990 if (separate_line_info)
11992 dw_line_info_table *table;
11993 size_t i;
11995 FOR_EACH_VEC_ELT (*separate_line_info, i, table)
11996 if (table->in_use)
11998 output_one_line_info_table (table);
11999 saw_one = true;
12002 if (cold_text_section_line_info && cold_text_section_line_info->in_use)
12004 output_one_line_info_table (cold_text_section_line_info);
12005 saw_one = true;
12008 /* ??? Some Darwin linkers crash on a .debug_line section with no
12009 sequences. Further, merely a DW_LNE_end_sequence entry is not
12010 sufficient -- the address column must also be initialized.
12011 Make sure to output at least one set_address/end_sequence pair,
12012 choosing .text since that section is always present. */
12013 if (text_section_line_info->in_use || !saw_one)
12014 output_one_line_info_table (text_section_line_info);
12016 /* Output the marker for the end of the line number info. */
12017 ASM_OUTPUT_LABEL (asm_out_file, l2);
12020 /* Return true if DW_AT_endianity should be emitted according to REVERSE. */
12022 static inline bool
12023 need_endianity_attribute_p (bool reverse)
12025 return reverse && (dwarf_version >= 3 || !dwarf_strict);
12028 /* Given a pointer to a tree node for some base type, return a pointer to
12029 a DIE that describes the given type. REVERSE is true if the type is
12030 to be interpreted in the reverse storage order wrt the target order.
12032 This routine must only be called for GCC type nodes that correspond to
12033 Dwarf base (fundamental) types. */
12035 static dw_die_ref
12036 base_type_die (tree type, bool reverse)
12038 dw_die_ref base_type_result;
12039 enum dwarf_type encoding;
12040 bool fpt_used = false;
12041 struct fixed_point_type_info fpt_info;
12042 tree type_bias = NULL_TREE;
12044 if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
12045 return 0;
12047 /* If this is a subtype that should not be emitted as a subrange type,
12048 use the base type. See subrange_type_for_debug_p. */
12049 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
12050 type = TREE_TYPE (type);
12052 switch (TREE_CODE (type))
12054 case INTEGER_TYPE:
12055 if ((dwarf_version >= 4 || !dwarf_strict)
12056 && TYPE_NAME (type)
12057 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12058 && DECL_IS_BUILTIN (TYPE_NAME (type))
12059 && DECL_NAME (TYPE_NAME (type)))
12061 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
12062 if (strcmp (name, "char16_t") == 0
12063 || strcmp (name, "char32_t") == 0)
12065 encoding = DW_ATE_UTF;
12066 break;
12069 if ((dwarf_version >= 3 || !dwarf_strict)
12070 && lang_hooks.types.get_fixed_point_type_info)
12072 memset (&fpt_info, 0, sizeof (fpt_info));
12073 if (lang_hooks.types.get_fixed_point_type_info (type, &fpt_info))
12075 fpt_used = true;
12076 encoding = ((TYPE_UNSIGNED (type))
12077 ? DW_ATE_unsigned_fixed
12078 : DW_ATE_signed_fixed);
12079 break;
12082 if (TYPE_STRING_FLAG (type))
12084 if (TYPE_UNSIGNED (type))
12085 encoding = DW_ATE_unsigned_char;
12086 else
12087 encoding = DW_ATE_signed_char;
12089 else if (TYPE_UNSIGNED (type))
12090 encoding = DW_ATE_unsigned;
12091 else
12092 encoding = DW_ATE_signed;
12094 if (!dwarf_strict
12095 && lang_hooks.types.get_type_bias)
12096 type_bias = lang_hooks.types.get_type_bias (type);
12097 break;
12099 case REAL_TYPE:
12100 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
12102 if (dwarf_version >= 3 || !dwarf_strict)
12103 encoding = DW_ATE_decimal_float;
12104 else
12105 encoding = DW_ATE_lo_user;
12107 else
12108 encoding = DW_ATE_float;
12109 break;
12111 case FIXED_POINT_TYPE:
12112 if (!(dwarf_version >= 3 || !dwarf_strict))
12113 encoding = DW_ATE_lo_user;
12114 else if (TYPE_UNSIGNED (type))
12115 encoding = DW_ATE_unsigned_fixed;
12116 else
12117 encoding = DW_ATE_signed_fixed;
12118 break;
12120 /* Dwarf2 doesn't know anything about complex ints, so use
12121 a user defined type for it. */
12122 case COMPLEX_TYPE:
12123 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
12124 encoding = DW_ATE_complex_float;
12125 else
12126 encoding = DW_ATE_lo_user;
12127 break;
12129 case BOOLEAN_TYPE:
12130 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
12131 encoding = DW_ATE_boolean;
12132 break;
12134 default:
12135 /* No other TREE_CODEs are Dwarf fundamental types. */
12136 gcc_unreachable ();
12139 base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
12141 add_AT_unsigned (base_type_result, DW_AT_byte_size,
12142 int_size_in_bytes (type));
12143 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
12145 if (need_endianity_attribute_p (reverse))
12146 add_AT_unsigned (base_type_result, DW_AT_endianity,
12147 BYTES_BIG_ENDIAN ? DW_END_little : DW_END_big);
12149 add_alignment_attribute (base_type_result, type);
12151 if (fpt_used)
12153 switch (fpt_info.scale_factor_kind)
12155 case fixed_point_scale_factor_binary:
12156 add_AT_int (base_type_result, DW_AT_binary_scale,
12157 fpt_info.scale_factor.binary);
12158 break;
12160 case fixed_point_scale_factor_decimal:
12161 add_AT_int (base_type_result, DW_AT_decimal_scale,
12162 fpt_info.scale_factor.decimal);
12163 break;
12165 case fixed_point_scale_factor_arbitrary:
12166 /* Arbitrary scale factors cannot be described in standard DWARF,
12167 yet. */
12168 if (!dwarf_strict)
12170 /* Describe the scale factor as a rational constant. */
12171 const dw_die_ref scale_factor
12172 = new_die (DW_TAG_constant, comp_unit_die (), type);
12174 add_AT_unsigned (scale_factor, DW_AT_GNU_numerator,
12175 fpt_info.scale_factor.arbitrary.numerator);
12176 add_AT_int (scale_factor, DW_AT_GNU_denominator,
12177 fpt_info.scale_factor.arbitrary.denominator);
12179 add_AT_die_ref (base_type_result, DW_AT_small, scale_factor);
12181 break;
12183 default:
12184 gcc_unreachable ();
12188 if (type_bias)
12189 add_scalar_info (base_type_result, DW_AT_GNU_bias, type_bias,
12190 dw_scalar_form_constant
12191 | dw_scalar_form_exprloc
12192 | dw_scalar_form_reference,
12193 NULL);
12195 add_pubtype (type, base_type_result);
12197 return base_type_result;
12200 /* A C++ function with deduced return type can have a TEMPLATE_TYPE_PARM
12201 named 'auto' in its type: return true for it, false otherwise. */
12203 static inline bool
12204 is_cxx_auto (tree type)
12206 if (is_cxx ())
12208 tree name = TYPE_IDENTIFIER (type);
12209 if (name == get_identifier ("auto")
12210 || name == get_identifier ("decltype(auto)"))
12211 return true;
12213 return false;
12216 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
12217 given input type is a Dwarf "fundamental" type. Otherwise return null. */
12219 static inline int
12220 is_base_type (tree type)
12222 switch (TREE_CODE (type))
12224 case ERROR_MARK:
12225 case VOID_TYPE:
12226 case INTEGER_TYPE:
12227 case REAL_TYPE:
12228 case FIXED_POINT_TYPE:
12229 case COMPLEX_TYPE:
12230 case BOOLEAN_TYPE:
12231 case POINTER_BOUNDS_TYPE:
12232 return 1;
12234 case ARRAY_TYPE:
12235 case RECORD_TYPE:
12236 case UNION_TYPE:
12237 case QUAL_UNION_TYPE:
12238 case ENUMERAL_TYPE:
12239 case FUNCTION_TYPE:
12240 case METHOD_TYPE:
12241 case POINTER_TYPE:
12242 case REFERENCE_TYPE:
12243 case NULLPTR_TYPE:
12244 case OFFSET_TYPE:
12245 case LANG_TYPE:
12246 case VECTOR_TYPE:
12247 return 0;
12249 default:
12250 if (is_cxx_auto (type))
12251 return 0;
12252 gcc_unreachable ();
12255 return 0;
12258 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
12259 node, return the size in bits for the type if it is a constant, or else
12260 return the alignment for the type if the type's size is not constant, or
12261 else return BITS_PER_WORD if the type actually turns out to be an
12262 ERROR_MARK node. */
12264 static inline unsigned HOST_WIDE_INT
12265 simple_type_size_in_bits (const_tree type)
12267 if (TREE_CODE (type) == ERROR_MARK)
12268 return BITS_PER_WORD;
12269 else if (TYPE_SIZE (type) == NULL_TREE)
12270 return 0;
12271 else if (tree_fits_uhwi_p (TYPE_SIZE (type)))
12272 return tree_to_uhwi (TYPE_SIZE (type));
12273 else
12274 return TYPE_ALIGN (type);
12277 /* Similarly, but return an offset_int instead of UHWI. */
12279 static inline offset_int
12280 offset_int_type_size_in_bits (const_tree type)
12282 if (TREE_CODE (type) == ERROR_MARK)
12283 return BITS_PER_WORD;
12284 else if (TYPE_SIZE (type) == NULL_TREE)
12285 return 0;
12286 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
12287 return wi::to_offset (TYPE_SIZE (type));
12288 else
12289 return TYPE_ALIGN (type);
12292 /* Given a pointer to a tree node for a subrange type, return a pointer
12293 to a DIE that describes the given type. */
12295 static dw_die_ref
12296 subrange_type_die (tree type, tree low, tree high, tree bias,
12297 dw_die_ref context_die)
12299 dw_die_ref subrange_die;
12300 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
12302 if (context_die == NULL)
12303 context_die = comp_unit_die ();
12305 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
12307 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
12309 /* The size of the subrange type and its base type do not match,
12310 so we need to generate a size attribute for the subrange type. */
12311 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
12314 add_alignment_attribute (subrange_die, type);
12316 if (low)
12317 add_bound_info (subrange_die, DW_AT_lower_bound, low, NULL);
12318 if (high)
12319 add_bound_info (subrange_die, DW_AT_upper_bound, high, NULL);
12320 if (bias && !dwarf_strict)
12321 add_scalar_info (subrange_die, DW_AT_GNU_bias, bias,
12322 dw_scalar_form_constant
12323 | dw_scalar_form_exprloc
12324 | dw_scalar_form_reference,
12325 NULL);
12327 return subrange_die;
12330 /* Returns the (const and/or volatile) cv_qualifiers associated with
12331 the decl node. This will normally be augmented with the
12332 cv_qualifiers of the underlying type in add_type_attribute. */
12334 static int
12335 decl_quals (const_tree decl)
12337 return ((TREE_READONLY (decl)
12338 /* The C++ front-end correctly marks reference-typed
12339 variables as readonly, but from a language (and debug
12340 info) standpoint they are not const-qualified. */
12341 && TREE_CODE (TREE_TYPE (decl)) != REFERENCE_TYPE
12342 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED)
12343 | (TREE_THIS_VOLATILE (decl)
12344 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED));
12347 /* Determine the TYPE whose qualifiers match the largest strict subset
12348 of the given TYPE_QUALS, and return its qualifiers. Ignore all
12349 qualifiers outside QUAL_MASK. */
12351 static int
12352 get_nearest_type_subqualifiers (tree type, int type_quals, int qual_mask)
12354 tree t;
12355 int best_rank = 0, best_qual = 0, max_rank;
12357 type_quals &= qual_mask;
12358 max_rank = popcount_hwi (type_quals) - 1;
12360 for (t = TYPE_MAIN_VARIANT (type); t && best_rank < max_rank;
12361 t = TYPE_NEXT_VARIANT (t))
12363 int q = TYPE_QUALS (t) & qual_mask;
12365 if ((q & type_quals) == q && q != type_quals
12366 && check_base_type (t, type))
12368 int rank = popcount_hwi (q);
12370 if (rank > best_rank)
12372 best_rank = rank;
12373 best_qual = q;
12378 return best_qual;
12381 struct dwarf_qual_info_t { int q; enum dwarf_tag t; };
12382 static const dwarf_qual_info_t dwarf_qual_info[] =
12384 { TYPE_QUAL_CONST, DW_TAG_const_type },
12385 { TYPE_QUAL_VOLATILE, DW_TAG_volatile_type },
12386 { TYPE_QUAL_RESTRICT, DW_TAG_restrict_type },
12387 { TYPE_QUAL_ATOMIC, DW_TAG_atomic_type }
12389 static const unsigned int dwarf_qual_info_size
12390 = sizeof (dwarf_qual_info) / sizeof (dwarf_qual_info[0]);
12392 /* If DIE is a qualified DIE of some base DIE with the same parent,
12393 return the base DIE, otherwise return NULL. Set MASK to the
12394 qualifiers added compared to the returned DIE. */
12396 static dw_die_ref
12397 qualified_die_p (dw_die_ref die, int *mask, unsigned int depth)
12399 unsigned int i;
12400 for (i = 0; i < dwarf_qual_info_size; i++)
12401 if (die->die_tag == dwarf_qual_info[i].t)
12402 break;
12403 if (i == dwarf_qual_info_size)
12404 return NULL;
12405 if (vec_safe_length (die->die_attr) != 1)
12406 return NULL;
12407 dw_die_ref type = get_AT_ref (die, DW_AT_type);
12408 if (type == NULL || type->die_parent != die->die_parent)
12409 return NULL;
12410 *mask |= dwarf_qual_info[i].q;
12411 if (depth)
12413 dw_die_ref ret = qualified_die_p (type, mask, depth - 1);
12414 if (ret)
12415 return ret;
12417 return type;
12420 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
12421 entry that chains the modifiers specified by CV_QUALS in front of the
12422 given type. REVERSE is true if the type is to be interpreted in the
12423 reverse storage order wrt the target order. */
12425 static dw_die_ref
12426 modified_type_die (tree type, int cv_quals, bool reverse,
12427 dw_die_ref context_die)
12429 enum tree_code code = TREE_CODE (type);
12430 dw_die_ref mod_type_die;
12431 dw_die_ref sub_die = NULL;
12432 tree item_type = NULL;
12433 tree qualified_type;
12434 tree name, low, high;
12435 dw_die_ref mod_scope;
12436 /* Only these cv-qualifiers are currently handled. */
12437 const int cv_qual_mask = (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE
12438 | TYPE_QUAL_RESTRICT | TYPE_QUAL_ATOMIC);
12440 if (code == ERROR_MARK)
12441 return NULL;
12443 if (lang_hooks.types.get_debug_type)
12445 tree debug_type = lang_hooks.types.get_debug_type (type);
12447 if (debug_type != NULL_TREE && debug_type != type)
12448 return modified_type_die (debug_type, cv_quals, reverse, context_die);
12451 cv_quals &= cv_qual_mask;
12453 /* Don't emit DW_TAG_restrict_type for DWARFv2, since it is a type
12454 tag modifier (and not an attribute) old consumers won't be able
12455 to handle it. */
12456 if (dwarf_version < 3)
12457 cv_quals &= ~TYPE_QUAL_RESTRICT;
12459 /* Likewise for DW_TAG_atomic_type for DWARFv5. */
12460 if (dwarf_version < 5)
12461 cv_quals &= ~TYPE_QUAL_ATOMIC;
12463 /* See if we already have the appropriately qualified variant of
12464 this type. */
12465 qualified_type = get_qualified_type (type, cv_quals);
12467 if (qualified_type == sizetype)
12469 /* Try not to expose the internal sizetype type's name. */
12470 if (TYPE_NAME (qualified_type)
12471 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
12473 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
12475 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
12476 && (TYPE_PRECISION (t)
12477 == TYPE_PRECISION (qualified_type))
12478 && (TYPE_UNSIGNED (t)
12479 == TYPE_UNSIGNED (qualified_type)));
12480 qualified_type = t;
12482 else if (qualified_type == sizetype
12483 && TREE_CODE (sizetype) == TREE_CODE (size_type_node)
12484 && TYPE_PRECISION (sizetype) == TYPE_PRECISION (size_type_node)
12485 && TYPE_UNSIGNED (sizetype) == TYPE_UNSIGNED (size_type_node))
12486 qualified_type = size_type_node;
12490 /* If we do, then we can just use its DIE, if it exists. */
12491 if (qualified_type)
12493 mod_type_die = lookup_type_die (qualified_type);
12495 /* DW_AT_endianity doesn't come from a qualifier on the type. */
12496 if (mod_type_die
12497 && (!need_endianity_attribute_p (reverse)
12498 || !is_base_type (type)
12499 || get_AT_unsigned (mod_type_die, DW_AT_endianity)))
12500 return mod_type_die;
12503 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
12505 /* Handle C typedef types. */
12506 if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
12507 && !DECL_ARTIFICIAL (name))
12509 tree dtype = TREE_TYPE (name);
12511 if (qualified_type == dtype)
12513 /* For a named type, use the typedef. */
12514 gen_type_die (qualified_type, context_die);
12515 return lookup_type_die (qualified_type);
12517 else
12519 int dquals = TYPE_QUALS_NO_ADDR_SPACE (dtype);
12520 dquals &= cv_qual_mask;
12521 if ((dquals & ~cv_quals) != TYPE_UNQUALIFIED
12522 || (cv_quals == dquals && DECL_ORIGINAL_TYPE (name) != type))
12523 /* cv-unqualified version of named type. Just use
12524 the unnamed type to which it refers. */
12525 return modified_type_die (DECL_ORIGINAL_TYPE (name), cv_quals,
12526 reverse, context_die);
12527 /* Else cv-qualified version of named type; fall through. */
12531 mod_scope = scope_die_for (type, context_die);
12533 if (cv_quals)
12535 int sub_quals = 0, first_quals = 0;
12536 unsigned i;
12537 dw_die_ref first = NULL, last = NULL;
12539 /* Determine a lesser qualified type that most closely matches
12540 this one. Then generate DW_TAG_* entries for the remaining
12541 qualifiers. */
12542 sub_quals = get_nearest_type_subqualifiers (type, cv_quals,
12543 cv_qual_mask);
12544 if (sub_quals && use_debug_types)
12546 bool needed = false;
12547 /* If emitting type units, make sure the order of qualifiers
12548 is canonical. Thus, start from unqualified type if
12549 an earlier qualifier is missing in sub_quals, but some later
12550 one is present there. */
12551 for (i = 0; i < dwarf_qual_info_size; i++)
12552 if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
12553 needed = true;
12554 else if (needed && (dwarf_qual_info[i].q & cv_quals))
12556 sub_quals = 0;
12557 break;
12560 mod_type_die = modified_type_die (type, sub_quals, reverse, context_die);
12561 if (mod_scope && mod_type_die && mod_type_die->die_parent == mod_scope)
12563 /* As not all intermediate qualified DIEs have corresponding
12564 tree types, ensure that qualified DIEs in the same scope
12565 as their DW_AT_type are emitted after their DW_AT_type,
12566 only with other qualified DIEs for the same type possibly
12567 in between them. Determine the range of such qualified
12568 DIEs now (first being the base type, last being corresponding
12569 last qualified DIE for it). */
12570 unsigned int count = 0;
12571 first = qualified_die_p (mod_type_die, &first_quals,
12572 dwarf_qual_info_size);
12573 if (first == NULL)
12574 first = mod_type_die;
12575 gcc_assert ((first_quals & ~sub_quals) == 0);
12576 for (count = 0, last = first;
12577 count < (1U << dwarf_qual_info_size);
12578 count++, last = last->die_sib)
12580 int quals = 0;
12581 if (last == mod_scope->die_child)
12582 break;
12583 if (qualified_die_p (last->die_sib, &quals, dwarf_qual_info_size)
12584 != first)
12585 break;
12589 for (i = 0; i < dwarf_qual_info_size; i++)
12590 if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
12592 dw_die_ref d;
12593 if (first && first != last)
12595 for (d = first->die_sib; ; d = d->die_sib)
12597 int quals = 0;
12598 qualified_die_p (d, &quals, dwarf_qual_info_size);
12599 if (quals == (first_quals | dwarf_qual_info[i].q))
12600 break;
12601 if (d == last)
12603 d = NULL;
12604 break;
12607 if (d)
12609 mod_type_die = d;
12610 continue;
12613 if (first)
12615 d = ggc_cleared_alloc<die_node> ();
12616 d->die_tag = dwarf_qual_info[i].t;
12617 add_child_die_after (mod_scope, d, last);
12618 last = d;
12620 else
12621 d = new_die (dwarf_qual_info[i].t, mod_scope, type);
12622 if (mod_type_die)
12623 add_AT_die_ref (d, DW_AT_type, mod_type_die);
12624 mod_type_die = d;
12625 first_quals |= dwarf_qual_info[i].q;
12628 else if (code == POINTER_TYPE || code == REFERENCE_TYPE)
12630 dwarf_tag tag = DW_TAG_pointer_type;
12631 if (code == REFERENCE_TYPE)
12633 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
12634 tag = DW_TAG_rvalue_reference_type;
12635 else
12636 tag = DW_TAG_reference_type;
12638 mod_type_die = new_die (tag, mod_scope, type);
12640 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
12641 simple_type_size_in_bits (type) / BITS_PER_UNIT);
12642 add_alignment_attribute (mod_type_die, type);
12643 item_type = TREE_TYPE (type);
12645 addr_space_t as = TYPE_ADDR_SPACE (item_type);
12646 if (!ADDR_SPACE_GENERIC_P (as))
12648 int action = targetm.addr_space.debug (as);
12649 if (action >= 0)
12651 /* Positive values indicate an address_class. */
12652 add_AT_unsigned (mod_type_die, DW_AT_address_class, action);
12654 else
12656 /* Negative values indicate an (inverted) segment base reg. */
12657 dw_loc_descr_ref d
12658 = one_reg_loc_descriptor (~action, VAR_INIT_STATUS_INITIALIZED);
12659 add_AT_loc (mod_type_die, DW_AT_segment, d);
12663 else if (code == INTEGER_TYPE
12664 && TREE_TYPE (type) != NULL_TREE
12665 && subrange_type_for_debug_p (type, &low, &high))
12667 tree bias = NULL_TREE;
12668 if (lang_hooks.types.get_type_bias)
12669 bias = lang_hooks.types.get_type_bias (type);
12670 mod_type_die = subrange_type_die (type, low, high, bias, context_die);
12671 item_type = TREE_TYPE (type);
12673 else if (is_base_type (type))
12674 mod_type_die = base_type_die (type, reverse);
12675 else
12677 gen_type_die (type, context_die);
12679 /* We have to get the type_main_variant here (and pass that to the
12680 `lookup_type_die' routine) because the ..._TYPE node we have
12681 might simply be a *copy* of some original type node (where the
12682 copy was created to help us keep track of typedef names) and
12683 that copy might have a different TYPE_UID from the original
12684 ..._TYPE node. */
12685 if (TREE_CODE (type) == FUNCTION_TYPE
12686 || TREE_CODE (type) == METHOD_TYPE)
12688 /* For function/method types, can't just use type_main_variant here,
12689 because that can have different ref-qualifiers for C++,
12690 but try to canonicalize. */
12691 tree main = TYPE_MAIN_VARIANT (type);
12692 for (tree t = main; t; t = TYPE_NEXT_VARIANT (t))
12693 if (TYPE_QUALS_NO_ADDR_SPACE (t) == 0
12694 && check_base_type (t, main)
12695 && check_lang_type (t, type))
12696 return lookup_type_die (t);
12697 return lookup_type_die (type);
12699 else if (TREE_CODE (type) != VECTOR_TYPE
12700 && TREE_CODE (type) != ARRAY_TYPE)
12701 return lookup_type_die (type_main_variant (type));
12702 else
12703 /* Vectors have the debugging information in the type,
12704 not the main variant. */
12705 return lookup_type_die (type);
12708 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
12709 don't output a DW_TAG_typedef, since there isn't one in the
12710 user's program; just attach a DW_AT_name to the type.
12711 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
12712 if the base type already has the same name. */
12713 if (name
12714 && ((TREE_CODE (name) != TYPE_DECL
12715 && (qualified_type == TYPE_MAIN_VARIANT (type)
12716 || (cv_quals == TYPE_UNQUALIFIED)))
12717 || (TREE_CODE (name) == TYPE_DECL
12718 && TREE_TYPE (name) == qualified_type
12719 && DECL_NAME (name))))
12721 if (TREE_CODE (name) == TYPE_DECL)
12722 /* Could just call add_name_and_src_coords_attributes here,
12723 but since this is a builtin type it doesn't have any
12724 useful source coordinates anyway. */
12725 name = DECL_NAME (name);
12726 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
12728 /* This probably indicates a bug. */
12729 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
12731 name = TYPE_IDENTIFIER (type);
12732 add_name_attribute (mod_type_die,
12733 name ? IDENTIFIER_POINTER (name) : "__unknown__");
12736 if (qualified_type)
12737 equate_type_number_to_die (qualified_type, mod_type_die);
12739 if (item_type)
12740 /* We must do this after the equate_type_number_to_die call, in case
12741 this is a recursive type. This ensures that the modified_type_die
12742 recursion will terminate even if the type is recursive. Recursive
12743 types are possible in Ada. */
12744 sub_die = modified_type_die (item_type,
12745 TYPE_QUALS_NO_ADDR_SPACE (item_type),
12746 reverse,
12747 context_die);
12749 if (sub_die != NULL)
12750 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
12752 add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
12753 if (TYPE_ARTIFICIAL (type))
12754 add_AT_flag (mod_type_die, DW_AT_artificial, 1);
12756 return mod_type_die;
12759 /* Generate DIEs for the generic parameters of T.
12760 T must be either a generic type or a generic function.
12761 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
12763 static void
12764 gen_generic_params_dies (tree t)
12766 tree parms, args;
12767 int parms_num, i;
12768 dw_die_ref die = NULL;
12769 int non_default;
12771 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
12772 return;
12774 if (TYPE_P (t))
12775 die = lookup_type_die (t);
12776 else if (DECL_P (t))
12777 die = lookup_decl_die (t);
12779 gcc_assert (die);
12781 parms = lang_hooks.get_innermost_generic_parms (t);
12782 if (!parms)
12783 /* T has no generic parameter. It means T is neither a generic type
12784 or function. End of story. */
12785 return;
12787 parms_num = TREE_VEC_LENGTH (parms);
12788 args = lang_hooks.get_innermost_generic_args (t);
12789 if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
12790 non_default = int_cst_value (TREE_CHAIN (args));
12791 else
12792 non_default = TREE_VEC_LENGTH (args);
12793 for (i = 0; i < parms_num; i++)
12795 tree parm, arg, arg_pack_elems;
12796 dw_die_ref parm_die;
12798 parm = TREE_VEC_ELT (parms, i);
12799 arg = TREE_VEC_ELT (args, i);
12800 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
12801 gcc_assert (parm && TREE_VALUE (parm) && arg);
12803 if (parm && TREE_VALUE (parm) && arg)
12805 /* If PARM represents a template parameter pack,
12806 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
12807 by DW_TAG_template_*_parameter DIEs for the argument
12808 pack elements of ARG. Note that ARG would then be
12809 an argument pack. */
12810 if (arg_pack_elems)
12811 parm_die = template_parameter_pack_die (TREE_VALUE (parm),
12812 arg_pack_elems,
12813 die);
12814 else
12815 parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
12816 true /* emit name */, die);
12817 if (i >= non_default)
12818 add_AT_flag (parm_die, DW_AT_default_value, 1);
12823 /* Create and return a DIE for PARM which should be
12824 the representation of a generic type parameter.
12825 For instance, in the C++ front end, PARM would be a template parameter.
12826 ARG is the argument to PARM.
12827 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
12828 name of the PARM.
12829 PARENT_DIE is the parent DIE which the new created DIE should be added to,
12830 as a child node. */
12832 static dw_die_ref
12833 generic_parameter_die (tree parm, tree arg,
12834 bool emit_name_p,
12835 dw_die_ref parent_die)
12837 dw_die_ref tmpl_die = NULL;
12838 const char *name = NULL;
12840 if (!parm || !DECL_NAME (parm) || !arg)
12841 return NULL;
12843 /* We support non-type generic parameters and arguments,
12844 type generic parameters and arguments, as well as
12845 generic generic parameters (a.k.a. template template parameters in C++)
12846 and arguments. */
12847 if (TREE_CODE (parm) == PARM_DECL)
12848 /* PARM is a nontype generic parameter */
12849 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
12850 else if (TREE_CODE (parm) == TYPE_DECL)
12851 /* PARM is a type generic parameter. */
12852 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
12853 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
12854 /* PARM is a generic generic parameter.
12855 Its DIE is a GNU extension. It shall have a
12856 DW_AT_name attribute to represent the name of the template template
12857 parameter, and a DW_AT_GNU_template_name attribute to represent the
12858 name of the template template argument. */
12859 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
12860 parent_die, parm);
12861 else
12862 gcc_unreachable ();
12864 if (tmpl_die)
12866 tree tmpl_type;
12868 /* If PARM is a generic parameter pack, it means we are
12869 emitting debug info for a template argument pack element.
12870 In other terms, ARG is a template argument pack element.
12871 In that case, we don't emit any DW_AT_name attribute for
12872 the die. */
12873 if (emit_name_p)
12875 name = IDENTIFIER_POINTER (DECL_NAME (parm));
12876 gcc_assert (name);
12877 add_AT_string (tmpl_die, DW_AT_name, name);
12880 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
12882 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
12883 TMPL_DIE should have a child DW_AT_type attribute that is set
12884 to the type of the argument to PARM, which is ARG.
12885 If PARM is a type generic parameter, TMPL_DIE should have a
12886 child DW_AT_type that is set to ARG. */
12887 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
12888 add_type_attribute (tmpl_die, tmpl_type,
12889 (TREE_THIS_VOLATILE (tmpl_type)
12890 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED),
12891 false, parent_die);
12893 else
12895 /* So TMPL_DIE is a DIE representing a
12896 a generic generic template parameter, a.k.a template template
12897 parameter in C++ and arg is a template. */
12899 /* The DW_AT_GNU_template_name attribute of the DIE must be set
12900 to the name of the argument. */
12901 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
12902 if (name)
12903 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
12906 if (TREE_CODE (parm) == PARM_DECL)
12907 /* So PARM is a non-type generic parameter.
12908 DWARF3 5.6.8 says we must set a DW_AT_const_value child
12909 attribute of TMPL_DIE which value represents the value
12910 of ARG.
12911 We must be careful here:
12912 The value of ARG might reference some function decls.
12913 We might currently be emitting debug info for a generic
12914 type and types are emitted before function decls, we don't
12915 know if the function decls referenced by ARG will actually be
12916 emitted after cgraph computations.
12917 So must defer the generation of the DW_AT_const_value to
12918 after cgraph is ready. */
12919 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
12922 return tmpl_die;
12925 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
12926 PARM_PACK must be a template parameter pack. The returned DIE
12927 will be child DIE of PARENT_DIE. */
12929 static dw_die_ref
12930 template_parameter_pack_die (tree parm_pack,
12931 tree parm_pack_args,
12932 dw_die_ref parent_die)
12934 dw_die_ref die;
12935 int j;
12937 gcc_assert (parent_die && parm_pack);
12939 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
12940 add_name_and_src_coords_attributes (die, parm_pack);
12941 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
12942 generic_parameter_die (parm_pack,
12943 TREE_VEC_ELT (parm_pack_args, j),
12944 false /* Don't emit DW_AT_name */,
12945 die);
12946 return die;
12949 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
12950 an enumerated type. */
12952 static inline int
12953 type_is_enum (const_tree type)
12955 return TREE_CODE (type) == ENUMERAL_TYPE;
12958 /* Return the DBX register number described by a given RTL node. */
12960 static unsigned int
12961 dbx_reg_number (const_rtx rtl)
12963 unsigned regno = REGNO (rtl);
12965 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
12967 #ifdef LEAF_REG_REMAP
12968 if (crtl->uses_only_leaf_regs)
12970 int leaf_reg = LEAF_REG_REMAP (regno);
12971 if (leaf_reg != -1)
12972 regno = (unsigned) leaf_reg;
12974 #endif
12976 regno = DBX_REGISTER_NUMBER (regno);
12977 gcc_assert (regno != INVALID_REGNUM);
12978 return regno;
12981 /* Optionally add a DW_OP_piece term to a location description expression.
12982 DW_OP_piece is only added if the location description expression already
12983 doesn't end with DW_OP_piece. */
12985 static void
12986 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
12988 dw_loc_descr_ref loc;
12990 if (*list_head != NULL)
12992 /* Find the end of the chain. */
12993 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
12996 if (loc->dw_loc_opc != DW_OP_piece)
12997 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
13001 /* Return a location descriptor that designates a machine register or
13002 zero if there is none. */
13004 static dw_loc_descr_ref
13005 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
13007 rtx regs;
13009 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
13010 return 0;
13012 /* We only use "frame base" when we're sure we're talking about the
13013 post-prologue local stack frame. We do this by *not* running
13014 register elimination until this point, and recognizing the special
13015 argument pointer and soft frame pointer rtx's.
13016 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
13017 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
13018 && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
13020 dw_loc_descr_ref result = NULL;
13022 if (dwarf_version >= 4 || !dwarf_strict)
13024 result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
13025 initialized);
13026 if (result)
13027 add_loc_descr (&result,
13028 new_loc_descr (DW_OP_stack_value, 0, 0));
13030 return result;
13033 regs = targetm.dwarf_register_span (rtl);
13035 if (REG_NREGS (rtl) > 1 || regs)
13036 return multiple_reg_loc_descriptor (rtl, regs, initialized);
13037 else
13039 unsigned int dbx_regnum = dbx_reg_number (rtl);
13040 if (dbx_regnum == IGNORED_DWARF_REGNUM)
13041 return 0;
13042 return one_reg_loc_descriptor (dbx_regnum, initialized);
13046 /* Return a location descriptor that designates a machine register for
13047 a given hard register number. */
13049 static dw_loc_descr_ref
13050 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
13052 dw_loc_descr_ref reg_loc_descr;
13054 if (regno <= 31)
13055 reg_loc_descr
13056 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
13057 else
13058 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
13060 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13061 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13063 return reg_loc_descr;
13066 /* Given an RTL of a register, return a location descriptor that
13067 designates a value that spans more than one register. */
13069 static dw_loc_descr_ref
13070 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
13071 enum var_init_status initialized)
13073 int size, i;
13074 dw_loc_descr_ref loc_result = NULL;
13076 /* Simple, contiguous registers. */
13077 if (regs == NULL_RTX)
13079 unsigned reg = REGNO (rtl);
13080 int nregs;
13082 #ifdef LEAF_REG_REMAP
13083 if (crtl->uses_only_leaf_regs)
13085 int leaf_reg = LEAF_REG_REMAP (reg);
13086 if (leaf_reg != -1)
13087 reg = (unsigned) leaf_reg;
13089 #endif
13091 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
13092 nregs = REG_NREGS (rtl);
13094 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
13096 loc_result = NULL;
13097 while (nregs--)
13099 dw_loc_descr_ref t;
13101 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
13102 VAR_INIT_STATUS_INITIALIZED);
13103 add_loc_descr (&loc_result, t);
13104 add_loc_descr_op_piece (&loc_result, size);
13105 ++reg;
13107 return loc_result;
13110 /* Now onto stupid register sets in non contiguous locations. */
13112 gcc_assert (GET_CODE (regs) == PARALLEL);
13114 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
13115 loc_result = NULL;
13117 for (i = 0; i < XVECLEN (regs, 0); ++i)
13119 dw_loc_descr_ref t;
13121 t = one_reg_loc_descriptor (dbx_reg_number (XVECEXP (regs, 0, i)),
13122 VAR_INIT_STATUS_INITIALIZED);
13123 add_loc_descr (&loc_result, t);
13124 add_loc_descr_op_piece (&loc_result, size);
13127 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13128 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13129 return loc_result;
13132 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
13134 /* Return a location descriptor that designates a constant i,
13135 as a compound operation from constant (i >> shift), constant shift
13136 and DW_OP_shl. */
13138 static dw_loc_descr_ref
13139 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
13141 dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
13142 add_loc_descr (&ret, int_loc_descriptor (shift));
13143 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
13144 return ret;
13147 /* Return a location descriptor that designates a constant. */
13149 static dw_loc_descr_ref
13150 int_loc_descriptor (HOST_WIDE_INT i)
13152 enum dwarf_location_atom op;
13154 /* Pick the smallest representation of a constant, rather than just
13155 defaulting to the LEB encoding. */
13156 if (i >= 0)
13158 int clz = clz_hwi (i);
13159 int ctz = ctz_hwi (i);
13160 if (i <= 31)
13161 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
13162 else if (i <= 0xff)
13163 op = DW_OP_const1u;
13164 else if (i <= 0xffff)
13165 op = DW_OP_const2u;
13166 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
13167 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
13168 /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
13169 DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
13170 while DW_OP_const4u is 5 bytes. */
13171 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
13172 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13173 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
13174 /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
13175 while DW_OP_const4u is 5 bytes. */
13176 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
13178 else if (DWARF2_ADDR_SIZE == 4 && i > 0x7fffffff
13179 && size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i)
13180 <= 4)
13182 /* As i >= 2**31, the double cast above will yield a negative number.
13183 Since wrapping is defined in DWARF expressions we can output big
13184 positive integers as small negative ones, regardless of the size
13185 of host wide ints.
13187 Here, since the evaluator will handle 32-bit values and since i >=
13188 2**31, we know it's going to be interpreted as a negative literal:
13189 store it this way if we can do better than 5 bytes this way. */
13190 return int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i);
13192 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
13193 op = DW_OP_const4u;
13195 /* Past this point, i >= 0x100000000 and thus DW_OP_constu will take at
13196 least 6 bytes: see if we can do better before falling back to it. */
13197 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13198 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
13199 /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes. */
13200 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
13201 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
13202 && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
13203 >= HOST_BITS_PER_WIDE_INT)
13204 /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
13205 DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes. */
13206 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
13207 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
13208 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
13209 && size_of_uleb128 (i) > 6)
13210 /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes. */
13211 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
13212 else
13213 op = DW_OP_constu;
13215 else
13217 if (i >= -0x80)
13218 op = DW_OP_const1s;
13219 else if (i >= -0x8000)
13220 op = DW_OP_const2s;
13221 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
13223 if (size_of_int_loc_descriptor (i) < 5)
13225 dw_loc_descr_ref ret = int_loc_descriptor (-i);
13226 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
13227 return ret;
13229 op = DW_OP_const4s;
13231 else
13233 if (size_of_int_loc_descriptor (i)
13234 < (unsigned long) 1 + size_of_sleb128 (i))
13236 dw_loc_descr_ref ret = int_loc_descriptor (-i);
13237 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
13238 return ret;
13240 op = DW_OP_consts;
13244 return new_loc_descr (op, i, 0);
13247 /* Likewise, for unsigned constants. */
13249 static dw_loc_descr_ref
13250 uint_loc_descriptor (unsigned HOST_WIDE_INT i)
13252 const unsigned HOST_WIDE_INT max_int = INTTYPE_MAXIMUM (HOST_WIDE_INT);
13253 const unsigned HOST_WIDE_INT max_uint
13254 = INTTYPE_MAXIMUM (unsigned HOST_WIDE_INT);
13256 /* If possible, use the clever signed constants handling. */
13257 if (i <= max_int)
13258 return int_loc_descriptor ((HOST_WIDE_INT) i);
13260 /* Here, we are left with positive numbers that cannot be represented as
13261 HOST_WIDE_INT, i.e.:
13262 max (HOST_WIDE_INT) < i <= max (unsigned HOST_WIDE_INT)
13264 Using DW_OP_const4/8/./u operation to encode them consumes a lot of bytes
13265 whereas may be better to output a negative integer: thanks to integer
13266 wrapping, we know that:
13267 x = x - 2 ** DWARF2_ADDR_SIZE
13268 = x - 2 * (max (HOST_WIDE_INT) + 1)
13269 So numbers close to max (unsigned HOST_WIDE_INT) could be represented as
13270 small negative integers. Let's try that in cases it will clearly improve
13271 the encoding: there is no gain turning DW_OP_const4u into
13272 DW_OP_const4s. */
13273 if (DWARF2_ADDR_SIZE * 8 == HOST_BITS_PER_WIDE_INT
13274 && ((DWARF2_ADDR_SIZE == 4 && i > max_uint - 0x8000)
13275 || (DWARF2_ADDR_SIZE == 8 && i > max_uint - 0x80000000)))
13277 const unsigned HOST_WIDE_INT first_shift = i - max_int - 1;
13279 /* Now, -1 < first_shift <= max (HOST_WIDE_INT)
13280 i.e. 0 <= first_shift <= max (HOST_WIDE_INT). */
13281 const HOST_WIDE_INT second_shift
13282 = (HOST_WIDE_INT) first_shift - (HOST_WIDE_INT) max_int - 1;
13284 /* So we finally have:
13285 -max (HOST_WIDE_INT) - 1 <= second_shift <= -1.
13286 i.e. min (HOST_WIDE_INT) <= second_shift < 0. */
13287 return int_loc_descriptor (second_shift);
13290 /* Last chance: fallback to a simple constant operation. */
13291 return new_loc_descr
13292 ((HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
13293 ? DW_OP_const4u
13294 : DW_OP_const8u,
13295 i, 0);
13298 /* Generate and return a location description that computes the unsigned
13299 comparison of the two stack top entries (a OP b where b is the top-most
13300 entry and a is the second one). The KIND of comparison can be LT_EXPR,
13301 LE_EXPR, GT_EXPR or GE_EXPR. */
13303 static dw_loc_descr_ref
13304 uint_comparison_loc_list (enum tree_code kind)
13306 enum dwarf_location_atom op, flip_op;
13307 dw_loc_descr_ref ret, bra_node, jmp_node, tmp;
13309 switch (kind)
13311 case LT_EXPR:
13312 op = DW_OP_lt;
13313 break;
13314 case LE_EXPR:
13315 op = DW_OP_le;
13316 break;
13317 case GT_EXPR:
13318 op = DW_OP_gt;
13319 break;
13320 case GE_EXPR:
13321 op = DW_OP_ge;
13322 break;
13323 default:
13324 gcc_unreachable ();
13327 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
13328 jmp_node = new_loc_descr (DW_OP_skip, 0, 0);
13330 /* Until DWARFv4, operations all work on signed integers. It is nevertheless
13331 possible to perform unsigned comparisons: we just have to distinguish
13332 three cases:
13334 1. when a and b have the same sign (as signed integers); then we should
13335 return: a OP(signed) b;
13337 2. when a is a negative signed integer while b is a positive one, then a
13338 is a greater unsigned integer than b; likewise when a and b's roles
13339 are flipped.
13341 So first, compare the sign of the two operands. */
13342 ret = new_loc_descr (DW_OP_over, 0, 0);
13343 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
13344 add_loc_descr (&ret, new_loc_descr (DW_OP_xor, 0, 0));
13345 /* If they have different signs (i.e. they have different sign bits), then
13346 the stack top value has now the sign bit set and thus it's smaller than
13347 zero. */
13348 add_loc_descr (&ret, new_loc_descr (DW_OP_lit0, 0, 0));
13349 add_loc_descr (&ret, new_loc_descr (DW_OP_lt, 0, 0));
13350 add_loc_descr (&ret, bra_node);
13352 /* We are in case 1. At this point, we know both operands have the same
13353 sign, to it's safe to use the built-in signed comparison. */
13354 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
13355 add_loc_descr (&ret, jmp_node);
13357 /* We are in case 2. Here, we know both operands do not have the same sign,
13358 so we have to flip the signed comparison. */
13359 flip_op = (kind == LT_EXPR || kind == LE_EXPR) ? DW_OP_gt : DW_OP_lt;
13360 tmp = new_loc_descr (flip_op, 0, 0);
13361 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13362 bra_node->dw_loc_oprnd1.v.val_loc = tmp;
13363 add_loc_descr (&ret, tmp);
13365 /* This dummy operation is necessary to make the two branches join. */
13366 tmp = new_loc_descr (DW_OP_nop, 0, 0);
13367 jmp_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13368 jmp_node->dw_loc_oprnd1.v.val_loc = tmp;
13369 add_loc_descr (&ret, tmp);
13371 return ret;
13374 /* Likewise, but takes the location description lists (might be destructive on
13375 them). Return NULL if either is NULL or if concatenation fails. */
13377 static dw_loc_list_ref
13378 loc_list_from_uint_comparison (dw_loc_list_ref left, dw_loc_list_ref right,
13379 enum tree_code kind)
13381 if (left == NULL || right == NULL)
13382 return NULL;
13384 add_loc_list (&left, right);
13385 if (left == NULL)
13386 return NULL;
13388 add_loc_descr_to_each (left, uint_comparison_loc_list (kind));
13389 return left;
13392 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
13393 without actually allocating it. */
13395 static unsigned long
13396 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
13398 return size_of_int_loc_descriptor (i >> shift)
13399 + size_of_int_loc_descriptor (shift)
13400 + 1;
13403 /* Return size_of_locs (int_loc_descriptor (i)) without
13404 actually allocating it. */
13406 static unsigned long
13407 size_of_int_loc_descriptor (HOST_WIDE_INT i)
13409 unsigned long s;
13411 if (i >= 0)
13413 int clz, ctz;
13414 if (i <= 31)
13415 return 1;
13416 else if (i <= 0xff)
13417 return 2;
13418 else if (i <= 0xffff)
13419 return 3;
13420 clz = clz_hwi (i);
13421 ctz = ctz_hwi (i);
13422 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
13423 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
13424 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13425 - clz - 5);
13426 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13427 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
13428 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13429 - clz - 8);
13430 else if (DWARF2_ADDR_SIZE == 4 && i > 0x7fffffff
13431 && size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i)
13432 <= 4)
13433 return size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i);
13434 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
13435 return 5;
13436 s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
13437 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13438 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
13439 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13440 - clz - 8);
13441 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
13442 && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
13443 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13444 - clz - 16);
13445 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
13446 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
13447 && s > 6)
13448 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13449 - clz - 32);
13450 else
13451 return 1 + s;
13453 else
13455 if (i >= -0x80)
13456 return 2;
13457 else if (i >= -0x8000)
13458 return 3;
13459 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
13461 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
13463 s = size_of_int_loc_descriptor (-i) + 1;
13464 if (s < 5)
13465 return s;
13467 return 5;
13469 else
13471 unsigned long r = 1 + size_of_sleb128 (i);
13472 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
13474 s = size_of_int_loc_descriptor (-i) + 1;
13475 if (s < r)
13476 return s;
13478 return r;
13483 /* Return loc description representing "address" of integer value.
13484 This can appear only as toplevel expression. */
13486 static dw_loc_descr_ref
13487 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
13489 int litsize;
13490 dw_loc_descr_ref loc_result = NULL;
13492 if (!(dwarf_version >= 4 || !dwarf_strict))
13493 return NULL;
13495 litsize = size_of_int_loc_descriptor (i);
13496 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
13497 is more compact. For DW_OP_stack_value we need:
13498 litsize + 1 (DW_OP_stack_value)
13499 and for DW_OP_implicit_value:
13500 1 (DW_OP_implicit_value) + 1 (length) + size. */
13501 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
13503 loc_result = int_loc_descriptor (i);
13504 add_loc_descr (&loc_result,
13505 new_loc_descr (DW_OP_stack_value, 0, 0));
13506 return loc_result;
13509 loc_result = new_loc_descr (DW_OP_implicit_value,
13510 size, 0);
13511 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
13512 loc_result->dw_loc_oprnd2.v.val_int = i;
13513 return loc_result;
13516 /* Return a location descriptor that designates a base+offset location. */
13518 static dw_loc_descr_ref
13519 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
13520 enum var_init_status initialized)
13522 unsigned int regno;
13523 dw_loc_descr_ref result;
13524 dw_fde_ref fde = cfun->fde;
13526 /* We only use "frame base" when we're sure we're talking about the
13527 post-prologue local stack frame. We do this by *not* running
13528 register elimination until this point, and recognizing the special
13529 argument pointer and soft frame pointer rtx's. */
13530 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
13532 rtx elim = (ira_use_lra_p
13533 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
13534 : eliminate_regs (reg, VOIDmode, NULL_RTX));
13536 if (elim != reg)
13538 if (GET_CODE (elim) == PLUS)
13540 offset += INTVAL (XEXP (elim, 1));
13541 elim = XEXP (elim, 0);
13543 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
13544 && (elim == hard_frame_pointer_rtx
13545 || elim == stack_pointer_rtx))
13546 || elim == (frame_pointer_needed
13547 ? hard_frame_pointer_rtx
13548 : stack_pointer_rtx));
13550 /* If drap register is used to align stack, use frame
13551 pointer + offset to access stack variables. If stack
13552 is aligned without drap, use stack pointer + offset to
13553 access stack variables. */
13554 if (crtl->stack_realign_tried
13555 && reg == frame_pointer_rtx)
13557 int base_reg
13558 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
13559 ? HARD_FRAME_POINTER_REGNUM
13560 : REGNO (elim));
13561 return new_reg_loc_descr (base_reg, offset);
13564 gcc_assert (frame_pointer_fb_offset_valid);
13565 offset += frame_pointer_fb_offset;
13566 return new_loc_descr (DW_OP_fbreg, offset, 0);
13570 regno = REGNO (reg);
13571 #ifdef LEAF_REG_REMAP
13572 if (crtl->uses_only_leaf_regs)
13574 int leaf_reg = LEAF_REG_REMAP (regno);
13575 if (leaf_reg != -1)
13576 regno = (unsigned) leaf_reg;
13578 #endif
13579 regno = DWARF_FRAME_REGNUM (regno);
13581 if (!optimize && fde
13582 && (fde->drap_reg == regno || fde->vdrap_reg == regno))
13584 /* Use cfa+offset to represent the location of arguments passed
13585 on the stack when drap is used to align stack.
13586 Only do this when not optimizing, for optimized code var-tracking
13587 is supposed to track where the arguments live and the register
13588 used as vdrap or drap in some spot might be used for something
13589 else in other part of the routine. */
13590 return new_loc_descr (DW_OP_fbreg, offset, 0);
13593 if (regno <= 31)
13594 result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
13595 offset, 0);
13596 else
13597 result = new_loc_descr (DW_OP_bregx, regno, offset);
13599 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13600 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13602 return result;
13605 /* Return true if this RTL expression describes a base+offset calculation. */
13607 static inline int
13608 is_based_loc (const_rtx rtl)
13610 return (GET_CODE (rtl) == PLUS
13611 && ((REG_P (XEXP (rtl, 0))
13612 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
13613 && CONST_INT_P (XEXP (rtl, 1)))));
13616 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
13617 failed. */
13619 static dw_loc_descr_ref
13620 tls_mem_loc_descriptor (rtx mem)
13622 tree base;
13623 dw_loc_descr_ref loc_result;
13625 if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
13626 return NULL;
13628 base = get_base_address (MEM_EXPR (mem));
13629 if (base == NULL
13630 || !VAR_P (base)
13631 || !DECL_THREAD_LOCAL_P (base))
13632 return NULL;
13634 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1, NULL);
13635 if (loc_result == NULL)
13636 return NULL;
13638 if (MEM_OFFSET (mem))
13639 loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
13641 return loc_result;
13644 /* Output debug info about reason why we failed to expand expression as dwarf
13645 expression. */
13647 static void
13648 expansion_failed (tree expr, rtx rtl, char const *reason)
13650 if (dump_file && (dump_flags & TDF_DETAILS))
13652 fprintf (dump_file, "Failed to expand as dwarf: ");
13653 if (expr)
13654 print_generic_expr (dump_file, expr, dump_flags);
13655 if (rtl)
13657 fprintf (dump_file, "\n");
13658 print_rtl (dump_file, rtl);
13660 fprintf (dump_file, "\nReason: %s\n", reason);
13664 /* Helper function for const_ok_for_output. */
13666 static bool
13667 const_ok_for_output_1 (rtx rtl)
13669 if (GET_CODE (rtl) == UNSPEC)
13671 /* If delegitimize_address couldn't do anything with the UNSPEC, assume
13672 we can't express it in the debug info. */
13673 /* Don't complain about TLS UNSPECs, those are just too hard to
13674 delegitimize. Note this could be a non-decl SYMBOL_REF such as
13675 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
13676 rather than DECL_THREAD_LOCAL_P is not just an optimization. */
13677 if (flag_checking
13678 && (XVECLEN (rtl, 0) == 0
13679 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
13680 || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE))
13681 inform (current_function_decl
13682 ? DECL_SOURCE_LOCATION (current_function_decl)
13683 : UNKNOWN_LOCATION,
13684 #if NUM_UNSPEC_VALUES > 0
13685 "non-delegitimized UNSPEC %s (%d) found in variable location",
13686 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
13687 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
13688 XINT (rtl, 1));
13689 #else
13690 "non-delegitimized UNSPEC %d found in variable location",
13691 XINT (rtl, 1));
13692 #endif
13693 expansion_failed (NULL_TREE, rtl,
13694 "UNSPEC hasn't been delegitimized.\n");
13695 return false;
13698 if (targetm.const_not_ok_for_debug_p (rtl))
13700 expansion_failed (NULL_TREE, rtl,
13701 "Expression rejected for debug by the backend.\n");
13702 return false;
13705 /* FIXME: Refer to PR60655. It is possible for simplification
13706 of rtl expressions in var tracking to produce such expressions.
13707 We should really identify / validate expressions
13708 enclosed in CONST that can be handled by assemblers on various
13709 targets and only handle legitimate cases here. */
13710 if (GET_CODE (rtl) != SYMBOL_REF)
13712 if (GET_CODE (rtl) == NOT)
13713 return false;
13714 return true;
13717 if (CONSTANT_POOL_ADDRESS_P (rtl))
13719 bool marked;
13720 get_pool_constant_mark (rtl, &marked);
13721 /* If all references to this pool constant were optimized away,
13722 it was not output and thus we can't represent it. */
13723 if (!marked)
13725 expansion_failed (NULL_TREE, rtl,
13726 "Constant was removed from constant pool.\n");
13727 return false;
13731 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
13732 return false;
13734 /* Avoid references to external symbols in debug info, on several targets
13735 the linker might even refuse to link when linking a shared library,
13736 and in many other cases the relocations for .debug_info/.debug_loc are
13737 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
13738 to be defined within the same shared library or executable are fine. */
13739 if (SYMBOL_REF_EXTERNAL_P (rtl))
13741 tree decl = SYMBOL_REF_DECL (rtl);
13743 if (decl == NULL || !targetm.binds_local_p (decl))
13745 expansion_failed (NULL_TREE, rtl,
13746 "Symbol not defined in current TU.\n");
13747 return false;
13751 return true;
13754 /* Return true if constant RTL can be emitted in DW_OP_addr or
13755 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
13756 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
13758 static bool
13759 const_ok_for_output (rtx rtl)
13761 if (GET_CODE (rtl) == SYMBOL_REF)
13762 return const_ok_for_output_1 (rtl);
13764 if (GET_CODE (rtl) == CONST)
13766 subrtx_var_iterator::array_type array;
13767 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 0), ALL)
13768 if (!const_ok_for_output_1 (*iter))
13769 return false;
13770 return true;
13773 return true;
13776 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
13777 if possible, NULL otherwise. */
13779 static dw_die_ref
13780 base_type_for_mode (machine_mode mode, bool unsignedp)
13782 dw_die_ref type_die;
13783 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
13785 if (type == NULL)
13786 return NULL;
13787 switch (TREE_CODE (type))
13789 case INTEGER_TYPE:
13790 case REAL_TYPE:
13791 break;
13792 default:
13793 return NULL;
13795 type_die = lookup_type_die (type);
13796 if (!type_die)
13797 type_die = modified_type_die (type, TYPE_UNQUALIFIED, false,
13798 comp_unit_die ());
13799 if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
13800 return NULL;
13801 return type_die;
13804 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
13805 type matching MODE, or, if MODE is narrower than or as wide as
13806 DWARF2_ADDR_SIZE, untyped. Return NULL if the conversion is not
13807 possible. */
13809 static dw_loc_descr_ref
13810 convert_descriptor_to_mode (machine_mode mode, dw_loc_descr_ref op)
13812 machine_mode outer_mode = mode;
13813 dw_die_ref type_die;
13814 dw_loc_descr_ref cvt;
13816 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
13818 add_loc_descr (&op, new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0));
13819 return op;
13821 type_die = base_type_for_mode (outer_mode, 1);
13822 if (type_die == NULL)
13823 return NULL;
13824 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
13825 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13826 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13827 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13828 add_loc_descr (&op, cvt);
13829 return op;
13832 /* Return location descriptor for comparison OP with operands OP0 and OP1. */
13834 static dw_loc_descr_ref
13835 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
13836 dw_loc_descr_ref op1)
13838 dw_loc_descr_ref ret = op0;
13839 add_loc_descr (&ret, op1);
13840 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
13841 if (STORE_FLAG_VALUE != 1)
13843 add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
13844 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
13846 return ret;
13849 /* Return location descriptor for signed comparison OP RTL. */
13851 static dw_loc_descr_ref
13852 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
13853 machine_mode mem_mode)
13855 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
13856 dw_loc_descr_ref op0, op1;
13857 int shift;
13859 if (op_mode == VOIDmode)
13860 op_mode = GET_MODE (XEXP (rtl, 1));
13861 if (op_mode == VOIDmode)
13862 return NULL;
13864 if (dwarf_strict
13865 && dwarf_version < 5
13866 && (!SCALAR_INT_MODE_P (op_mode)
13867 || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
13868 return NULL;
13870 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
13871 VAR_INIT_STATUS_INITIALIZED);
13872 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
13873 VAR_INIT_STATUS_INITIALIZED);
13875 if (op0 == NULL || op1 == NULL)
13876 return NULL;
13878 if (!SCALAR_INT_MODE_P (op_mode)
13879 || GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
13880 return compare_loc_descriptor (op, op0, op1);
13882 if (GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
13884 dw_die_ref type_die = base_type_for_mode (op_mode, 0);
13885 dw_loc_descr_ref cvt;
13887 if (type_die == NULL)
13888 return NULL;
13889 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
13890 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13891 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13892 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13893 add_loc_descr (&op0, cvt);
13894 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
13895 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13896 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13897 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13898 add_loc_descr (&op1, cvt);
13899 return compare_loc_descriptor (op, op0, op1);
13902 shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
13903 /* For eq/ne, if the operands are known to be zero-extended,
13904 there is no need to do the fancy shifting up. */
13905 if (op == DW_OP_eq || op == DW_OP_ne)
13907 dw_loc_descr_ref last0, last1;
13908 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
13910 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
13912 /* deref_size zero extends, and for constants we can check
13913 whether they are zero extended or not. */
13914 if (((last0->dw_loc_opc == DW_OP_deref_size
13915 && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
13916 || (CONST_INT_P (XEXP (rtl, 0))
13917 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
13918 == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
13919 && ((last1->dw_loc_opc == DW_OP_deref_size
13920 && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
13921 || (CONST_INT_P (XEXP (rtl, 1))
13922 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
13923 == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
13924 return compare_loc_descriptor (op, op0, op1);
13926 /* EQ/NE comparison against constant in narrower type than
13927 DWARF2_ADDR_SIZE can be performed either as
13928 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
13929 DW_OP_{eq,ne}
13931 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
13932 DW_OP_{eq,ne}. Pick whatever is shorter. */
13933 if (CONST_INT_P (XEXP (rtl, 1))
13934 && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
13935 && (size_of_int_loc_descriptor (shift) + 1
13936 + size_of_int_loc_descriptor (UINTVAL (XEXP (rtl, 1)) << shift)
13937 >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
13938 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
13939 & GET_MODE_MASK (op_mode))))
13941 add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
13942 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
13943 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
13944 & GET_MODE_MASK (op_mode));
13945 return compare_loc_descriptor (op, op0, op1);
13948 add_loc_descr (&op0, int_loc_descriptor (shift));
13949 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
13950 if (CONST_INT_P (XEXP (rtl, 1)))
13951 op1 = int_loc_descriptor (UINTVAL (XEXP (rtl, 1)) << shift);
13952 else
13954 add_loc_descr (&op1, int_loc_descriptor (shift));
13955 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
13957 return compare_loc_descriptor (op, op0, op1);
13960 /* Return location descriptor for unsigned comparison OP RTL. */
13962 static dw_loc_descr_ref
13963 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
13964 machine_mode mem_mode)
13966 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
13967 dw_loc_descr_ref op0, op1;
13969 if (op_mode == VOIDmode)
13970 op_mode = GET_MODE (XEXP (rtl, 1));
13971 if (op_mode == VOIDmode)
13972 return NULL;
13973 if (!SCALAR_INT_MODE_P (op_mode))
13974 return NULL;
13976 if (dwarf_strict
13977 && dwarf_version < 5
13978 && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
13979 return NULL;
13981 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
13982 VAR_INIT_STATUS_INITIALIZED);
13983 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
13984 VAR_INIT_STATUS_INITIALIZED);
13986 if (op0 == NULL || op1 == NULL)
13987 return NULL;
13989 if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
13991 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
13992 dw_loc_descr_ref last0, last1;
13993 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
13995 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
13997 if (CONST_INT_P (XEXP (rtl, 0)))
13998 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
13999 /* deref_size zero extends, so no need to mask it again. */
14000 else if (last0->dw_loc_opc != DW_OP_deref_size
14001 || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
14003 add_loc_descr (&op0, int_loc_descriptor (mask));
14004 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14006 if (CONST_INT_P (XEXP (rtl, 1)))
14007 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
14008 /* deref_size zero extends, so no need to mask it again. */
14009 else if (last1->dw_loc_opc != DW_OP_deref_size
14010 || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
14012 add_loc_descr (&op1, int_loc_descriptor (mask));
14013 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
14016 else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
14018 HOST_WIDE_INT bias = 1;
14019 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
14020 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14021 if (CONST_INT_P (XEXP (rtl, 1)))
14022 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
14023 + INTVAL (XEXP (rtl, 1)));
14024 else
14025 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
14026 bias, 0));
14028 return compare_loc_descriptor (op, op0, op1);
14031 /* Return location descriptor for {U,S}{MIN,MAX}. */
14033 static dw_loc_descr_ref
14034 minmax_loc_descriptor (rtx rtl, machine_mode mode,
14035 machine_mode mem_mode)
14037 enum dwarf_location_atom op;
14038 dw_loc_descr_ref op0, op1, ret;
14039 dw_loc_descr_ref bra_node, drop_node;
14041 if (dwarf_strict
14042 && dwarf_version < 5
14043 && (!SCALAR_INT_MODE_P (mode)
14044 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
14045 return NULL;
14047 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14048 VAR_INIT_STATUS_INITIALIZED);
14049 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14050 VAR_INIT_STATUS_INITIALIZED);
14052 if (op0 == NULL || op1 == NULL)
14053 return NULL;
14055 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
14056 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
14057 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
14058 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
14060 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
14062 HOST_WIDE_INT mask = GET_MODE_MASK (mode);
14063 add_loc_descr (&op0, int_loc_descriptor (mask));
14064 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14065 add_loc_descr (&op1, int_loc_descriptor (mask));
14066 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
14068 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
14070 HOST_WIDE_INT bias = 1;
14071 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
14072 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14073 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14076 else if (!SCALAR_INT_MODE_P (mode)
14077 && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
14079 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode)) * BITS_PER_UNIT;
14080 add_loc_descr (&op0, int_loc_descriptor (shift));
14081 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
14082 add_loc_descr (&op1, int_loc_descriptor (shift));
14083 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
14085 else if (SCALAR_INT_MODE_P (mode)
14086 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
14088 dw_die_ref type_die = base_type_for_mode (mode, 0);
14089 dw_loc_descr_ref cvt;
14090 if (type_die == NULL)
14091 return NULL;
14092 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14093 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14094 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14095 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14096 add_loc_descr (&op0, cvt);
14097 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14098 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14099 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14100 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14101 add_loc_descr (&op1, cvt);
14104 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
14105 op = DW_OP_lt;
14106 else
14107 op = DW_OP_gt;
14108 ret = op0;
14109 add_loc_descr (&ret, op1);
14110 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
14111 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14112 add_loc_descr (&ret, bra_node);
14113 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14114 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
14115 add_loc_descr (&ret, drop_node);
14116 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14117 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
14118 if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
14119 && SCALAR_INT_MODE_P (mode)
14120 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
14121 ret = convert_descriptor_to_mode (mode, ret);
14122 return ret;
14125 /* Helper function for mem_loc_descriptor. Perform OP binary op,
14126 but after converting arguments to type_die, afterwards
14127 convert back to unsigned. */
14129 static dw_loc_descr_ref
14130 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
14131 machine_mode mode, machine_mode mem_mode)
14133 dw_loc_descr_ref cvt, op0, op1;
14135 if (type_die == NULL)
14136 return NULL;
14137 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14138 VAR_INIT_STATUS_INITIALIZED);
14139 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14140 VAR_INIT_STATUS_INITIALIZED);
14141 if (op0 == NULL || op1 == NULL)
14142 return NULL;
14143 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14144 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14145 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14146 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14147 add_loc_descr (&op0, cvt);
14148 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14149 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14150 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14151 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14152 add_loc_descr (&op1, cvt);
14153 add_loc_descr (&op0, op1);
14154 add_loc_descr (&op0, new_loc_descr (op, 0, 0));
14155 return convert_descriptor_to_mode (mode, op0);
14158 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
14159 const0 is DW_OP_lit0 or corresponding typed constant,
14160 const1 is DW_OP_lit1 or corresponding typed constant
14161 and constMSB is constant with just the MSB bit set
14162 for the mode):
14163 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
14164 L1: const0 DW_OP_swap
14165 L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
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 CTZ is similar:
14171 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
14172 L1: const0 DW_OP_swap
14173 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
14174 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
14175 L3: DW_OP_drop
14176 L4: DW_OP_nop
14178 FFS is similar:
14179 DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
14180 L1: const1 DW_OP_swap
14181 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
14182 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
14183 L3: DW_OP_drop
14184 L4: DW_OP_nop */
14186 static dw_loc_descr_ref
14187 clz_loc_descriptor (rtx rtl, machine_mode mode,
14188 machine_mode mem_mode)
14190 dw_loc_descr_ref op0, ret, tmp;
14191 HOST_WIDE_INT valv;
14192 dw_loc_descr_ref l1jump, l1label;
14193 dw_loc_descr_ref l2jump, l2label;
14194 dw_loc_descr_ref l3jump, l3label;
14195 dw_loc_descr_ref l4jump, l4label;
14196 rtx msb;
14198 if (!SCALAR_INT_MODE_P (mode)
14199 || GET_MODE (XEXP (rtl, 0)) != mode)
14200 return NULL;
14202 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14203 VAR_INIT_STATUS_INITIALIZED);
14204 if (op0 == NULL)
14205 return NULL;
14206 ret = op0;
14207 if (GET_CODE (rtl) == CLZ)
14209 if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
14210 valv = GET_MODE_BITSIZE (mode);
14212 else if (GET_CODE (rtl) == FFS)
14213 valv = 0;
14214 else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
14215 valv = GET_MODE_BITSIZE (mode);
14216 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
14217 l1jump = new_loc_descr (DW_OP_bra, 0, 0);
14218 add_loc_descr (&ret, l1jump);
14219 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
14220 tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
14221 VAR_INIT_STATUS_INITIALIZED);
14222 if (tmp == NULL)
14223 return NULL;
14224 add_loc_descr (&ret, tmp);
14225 l4jump = new_loc_descr (DW_OP_skip, 0, 0);
14226 add_loc_descr (&ret, l4jump);
14227 l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
14228 ? const1_rtx : const0_rtx,
14229 mode, mem_mode,
14230 VAR_INIT_STATUS_INITIALIZED);
14231 if (l1label == NULL)
14232 return NULL;
14233 add_loc_descr (&ret, l1label);
14234 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14235 l2label = new_loc_descr (DW_OP_dup, 0, 0);
14236 add_loc_descr (&ret, l2label);
14237 if (GET_CODE (rtl) != CLZ)
14238 msb = const1_rtx;
14239 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
14240 msb = GEN_INT (HOST_WIDE_INT_1U
14241 << (GET_MODE_BITSIZE (mode) - 1));
14242 else
14243 msb = immed_wide_int_const
14244 (wi::set_bit_in_zero (GET_MODE_PRECISION (mode) - 1,
14245 GET_MODE_PRECISION (mode)), mode);
14246 if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
14247 tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
14248 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
14249 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
14250 else
14251 tmp = mem_loc_descriptor (msb, mode, mem_mode,
14252 VAR_INIT_STATUS_INITIALIZED);
14253 if (tmp == NULL)
14254 return NULL;
14255 add_loc_descr (&ret, tmp);
14256 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
14257 l3jump = new_loc_descr (DW_OP_bra, 0, 0);
14258 add_loc_descr (&ret, l3jump);
14259 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
14260 VAR_INIT_STATUS_INITIALIZED);
14261 if (tmp == NULL)
14262 return NULL;
14263 add_loc_descr (&ret, tmp);
14264 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
14265 ? DW_OP_shl : DW_OP_shr, 0, 0));
14266 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14267 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
14268 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14269 l2jump = new_loc_descr (DW_OP_skip, 0, 0);
14270 add_loc_descr (&ret, l2jump);
14271 l3label = new_loc_descr (DW_OP_drop, 0, 0);
14272 add_loc_descr (&ret, l3label);
14273 l4label = new_loc_descr (DW_OP_nop, 0, 0);
14274 add_loc_descr (&ret, l4label);
14275 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14276 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
14277 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14278 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
14279 l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14280 l3jump->dw_loc_oprnd1.v.val_loc = l3label;
14281 l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14282 l4jump->dw_loc_oprnd1.v.val_loc = l4label;
14283 return ret;
14286 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
14287 const1 is DW_OP_lit1 or corresponding typed constant):
14288 const0 DW_OP_swap
14289 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
14290 DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
14291 L2: DW_OP_drop
14293 PARITY is similar:
14294 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
14295 DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
14296 L2: DW_OP_drop */
14298 static dw_loc_descr_ref
14299 popcount_loc_descriptor (rtx rtl, machine_mode mode,
14300 machine_mode mem_mode)
14302 dw_loc_descr_ref op0, ret, tmp;
14303 dw_loc_descr_ref l1jump, l1label;
14304 dw_loc_descr_ref l2jump, l2label;
14306 if (!SCALAR_INT_MODE_P (mode)
14307 || GET_MODE (XEXP (rtl, 0)) != mode)
14308 return NULL;
14310 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14311 VAR_INIT_STATUS_INITIALIZED);
14312 if (op0 == NULL)
14313 return NULL;
14314 ret = op0;
14315 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
14316 VAR_INIT_STATUS_INITIALIZED);
14317 if (tmp == NULL)
14318 return NULL;
14319 add_loc_descr (&ret, tmp);
14320 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14321 l1label = new_loc_descr (DW_OP_dup, 0, 0);
14322 add_loc_descr (&ret, l1label);
14323 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
14324 add_loc_descr (&ret, l2jump);
14325 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
14326 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
14327 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
14328 VAR_INIT_STATUS_INITIALIZED);
14329 if (tmp == NULL)
14330 return NULL;
14331 add_loc_descr (&ret, tmp);
14332 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
14333 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
14334 ? DW_OP_plus : DW_OP_xor, 0, 0));
14335 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14336 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
14337 VAR_INIT_STATUS_INITIALIZED);
14338 add_loc_descr (&ret, tmp);
14339 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
14340 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
14341 add_loc_descr (&ret, l1jump);
14342 l2label = new_loc_descr (DW_OP_drop, 0, 0);
14343 add_loc_descr (&ret, l2label);
14344 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14345 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
14346 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14347 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
14348 return ret;
14351 /* BSWAP (constS is initial shift count, either 56 or 24):
14352 constS const0
14353 L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
14354 const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
14355 DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
14356 DW_OP_minus DW_OP_swap DW_OP_skip <L1>
14357 L2: DW_OP_drop DW_OP_swap DW_OP_drop */
14359 static dw_loc_descr_ref
14360 bswap_loc_descriptor (rtx rtl, machine_mode mode,
14361 machine_mode mem_mode)
14363 dw_loc_descr_ref op0, ret, tmp;
14364 dw_loc_descr_ref l1jump, l1label;
14365 dw_loc_descr_ref l2jump, l2label;
14367 if (!SCALAR_INT_MODE_P (mode)
14368 || BITS_PER_UNIT != 8
14369 || (GET_MODE_BITSIZE (mode) != 32
14370 && GET_MODE_BITSIZE (mode) != 64))
14371 return NULL;
14373 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14374 VAR_INIT_STATUS_INITIALIZED);
14375 if (op0 == NULL)
14376 return NULL;
14378 ret = op0;
14379 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
14380 mode, mem_mode,
14381 VAR_INIT_STATUS_INITIALIZED);
14382 if (tmp == NULL)
14383 return NULL;
14384 add_loc_descr (&ret, tmp);
14385 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
14386 VAR_INIT_STATUS_INITIALIZED);
14387 if (tmp == NULL)
14388 return NULL;
14389 add_loc_descr (&ret, tmp);
14390 l1label = new_loc_descr (DW_OP_pick, 2, 0);
14391 add_loc_descr (&ret, l1label);
14392 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
14393 mode, mem_mode,
14394 VAR_INIT_STATUS_INITIALIZED);
14395 add_loc_descr (&ret, tmp);
14396 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
14397 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
14398 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
14399 tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
14400 VAR_INIT_STATUS_INITIALIZED);
14401 if (tmp == NULL)
14402 return NULL;
14403 add_loc_descr (&ret, tmp);
14404 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
14405 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
14406 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
14407 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
14408 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14409 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
14410 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
14411 VAR_INIT_STATUS_INITIALIZED);
14412 add_loc_descr (&ret, tmp);
14413 add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
14414 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
14415 add_loc_descr (&ret, l2jump);
14416 tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
14417 VAR_INIT_STATUS_INITIALIZED);
14418 add_loc_descr (&ret, tmp);
14419 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
14420 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14421 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
14422 add_loc_descr (&ret, l1jump);
14423 l2label = new_loc_descr (DW_OP_drop, 0, 0);
14424 add_loc_descr (&ret, l2label);
14425 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14426 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
14427 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14428 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
14429 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14430 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
14431 return ret;
14434 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
14435 DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
14436 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
14437 DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
14439 ROTATERT is similar:
14440 DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
14441 DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
14442 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or */
14444 static dw_loc_descr_ref
14445 rotate_loc_descriptor (rtx rtl, machine_mode mode,
14446 machine_mode mem_mode)
14448 rtx rtlop1 = XEXP (rtl, 1);
14449 dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
14450 int i;
14452 if (!SCALAR_INT_MODE_P (mode))
14453 return NULL;
14455 if (GET_MODE (rtlop1) != VOIDmode
14456 && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
14457 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
14458 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14459 VAR_INIT_STATUS_INITIALIZED);
14460 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
14461 VAR_INIT_STATUS_INITIALIZED);
14462 if (op0 == NULL || op1 == NULL)
14463 return NULL;
14464 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
14465 for (i = 0; i < 2; i++)
14467 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
14468 mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
14469 mode, mem_mode,
14470 VAR_INIT_STATUS_INITIALIZED);
14471 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
14472 mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
14473 ? DW_OP_const4u
14474 : HOST_BITS_PER_WIDE_INT == 64
14475 ? DW_OP_const8u : DW_OP_constu,
14476 GET_MODE_MASK (mode), 0);
14477 else
14478 mask[i] = NULL;
14479 if (mask[i] == NULL)
14480 return NULL;
14481 add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
14483 ret = op0;
14484 add_loc_descr (&ret, op1);
14485 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
14486 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
14487 if (GET_CODE (rtl) == ROTATERT)
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_shl, 0, 0));
14494 if (mask[0] != NULL)
14495 add_loc_descr (&ret, mask[0]);
14496 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
14497 if (mask[1] != NULL)
14499 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14500 add_loc_descr (&ret, mask[1]);
14501 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14503 if (GET_CODE (rtl) == ROTATE)
14505 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
14506 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
14507 GET_MODE_BITSIZE (mode), 0));
14509 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
14510 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
14511 return ret;
14514 /* Helper function for mem_loc_descriptor. Return DW_OP_GNU_parameter_ref
14515 for DEBUG_PARAMETER_REF RTL. */
14517 static dw_loc_descr_ref
14518 parameter_ref_descriptor (rtx rtl)
14520 dw_loc_descr_ref ret;
14521 dw_die_ref ref;
14523 if (dwarf_strict)
14524 return NULL;
14525 gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
14526 ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
14527 ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
14528 if (ref)
14530 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14531 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
14532 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
14534 else
14536 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
14537 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
14539 return ret;
14542 /* The following routine converts the RTL for a variable or parameter
14543 (resident in memory) into an equivalent Dwarf representation of a
14544 mechanism for getting the address of that same variable onto the top of a
14545 hypothetical "address evaluation" stack.
14547 When creating memory location descriptors, we are effectively transforming
14548 the RTL for a memory-resident object into its Dwarf postfix expression
14549 equivalent. This routine recursively descends an RTL tree, turning
14550 it into Dwarf postfix code as it goes.
14552 MODE is the mode that should be assumed for the rtl if it is VOIDmode.
14554 MEM_MODE is the mode of the memory reference, needed to handle some
14555 autoincrement addressing modes.
14557 Return 0 if we can't represent the location. */
14559 dw_loc_descr_ref
14560 mem_loc_descriptor (rtx rtl, machine_mode mode,
14561 machine_mode mem_mode,
14562 enum var_init_status initialized)
14564 dw_loc_descr_ref mem_loc_result = NULL;
14565 enum dwarf_location_atom op;
14566 dw_loc_descr_ref op0, op1;
14567 rtx inner = NULL_RTX;
14569 if (mode == VOIDmode)
14570 mode = GET_MODE (rtl);
14572 /* Note that for a dynamically sized array, the location we will generate a
14573 description of here will be the lowest numbered location which is
14574 actually within the array. That's *not* necessarily the same as the
14575 zeroth element of the array. */
14577 rtl = targetm.delegitimize_address (rtl);
14579 if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
14580 return NULL;
14582 switch (GET_CODE (rtl))
14584 case POST_INC:
14585 case POST_DEC:
14586 case POST_MODIFY:
14587 return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
14589 case SUBREG:
14590 /* The case of a subreg may arise when we have a local (register)
14591 variable or a formal (register) parameter which doesn't quite fill
14592 up an entire register. For now, just assume that it is
14593 legitimate to make the Dwarf info refer to the whole register which
14594 contains the given subreg. */
14595 if (!subreg_lowpart_p (rtl))
14596 break;
14597 inner = SUBREG_REG (rtl);
14598 /* FALLTHRU */
14599 case TRUNCATE:
14600 if (inner == NULL_RTX)
14601 inner = XEXP (rtl, 0);
14602 if (SCALAR_INT_MODE_P (mode)
14603 && SCALAR_INT_MODE_P (GET_MODE (inner))
14604 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
14605 #ifdef POINTERS_EXTEND_UNSIGNED
14606 || (mode == Pmode && mem_mode != VOIDmode)
14607 #endif
14609 && GET_MODE_SIZE (GET_MODE (inner)) <= DWARF2_ADDR_SIZE)
14611 mem_loc_result = mem_loc_descriptor (inner,
14612 GET_MODE (inner),
14613 mem_mode, initialized);
14614 break;
14616 if (dwarf_strict && dwarf_version < 5)
14617 break;
14618 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (inner)))
14619 break;
14620 if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (inner))
14621 && (!SCALAR_INT_MODE_P (mode)
14622 || !SCALAR_INT_MODE_P (GET_MODE (inner))))
14623 break;
14624 else
14626 dw_die_ref type_die;
14627 dw_loc_descr_ref cvt;
14629 mem_loc_result = mem_loc_descriptor (inner,
14630 GET_MODE (inner),
14631 mem_mode, initialized);
14632 if (mem_loc_result == NULL)
14633 break;
14634 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
14635 if (type_die == NULL)
14637 mem_loc_result = NULL;
14638 break;
14640 if (GET_MODE_SIZE (mode)
14641 != GET_MODE_SIZE (GET_MODE (inner)))
14642 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14643 else
14644 cvt = new_loc_descr (dwarf_OP (DW_OP_reinterpret), 0, 0);
14645 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14646 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14647 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14648 add_loc_descr (&mem_loc_result, cvt);
14649 if (SCALAR_INT_MODE_P (mode)
14650 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
14652 /* Convert it to untyped afterwards. */
14653 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14654 add_loc_descr (&mem_loc_result, cvt);
14657 break;
14659 case REG:
14660 if (! SCALAR_INT_MODE_P (mode)
14661 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
14662 && rtl != arg_pointer_rtx
14663 && rtl != frame_pointer_rtx
14664 #ifdef POINTERS_EXTEND_UNSIGNED
14665 && (mode != Pmode || mem_mode == VOIDmode)
14666 #endif
14669 dw_die_ref type_die;
14670 unsigned int dbx_regnum;
14672 if (dwarf_strict && dwarf_version < 5)
14673 break;
14674 if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
14675 break;
14676 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
14677 if (type_die == NULL)
14678 break;
14680 dbx_regnum = dbx_reg_number (rtl);
14681 if (dbx_regnum == IGNORED_DWARF_REGNUM)
14682 break;
14683 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_regval_type),
14684 dbx_regnum, 0);
14685 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
14686 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
14687 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
14688 break;
14690 /* Whenever a register number forms a part of the description of the
14691 method for calculating the (dynamic) address of a memory resident
14692 object, DWARF rules require the register number be referred to as
14693 a "base register". This distinction is not based in any way upon
14694 what category of register the hardware believes the given register
14695 belongs to. This is strictly DWARF terminology we're dealing with
14696 here. Note that in cases where the location of a memory-resident
14697 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
14698 OP_CONST (0)) the actual DWARF location descriptor that we generate
14699 may just be OP_BASEREG (basereg). This may look deceptively like
14700 the object in question was allocated to a register (rather than in
14701 memory) so DWARF consumers need to be aware of the subtle
14702 distinction between OP_REG and OP_BASEREG. */
14703 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
14704 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
14705 else if (stack_realign_drap
14706 && crtl->drap_reg
14707 && crtl->args.internal_arg_pointer == rtl
14708 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
14710 /* If RTL is internal_arg_pointer, which has been optimized
14711 out, use DRAP instead. */
14712 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
14713 VAR_INIT_STATUS_INITIALIZED);
14715 break;
14717 case SIGN_EXTEND:
14718 case ZERO_EXTEND:
14719 if (!SCALAR_INT_MODE_P (mode))
14720 break;
14721 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
14722 mem_mode, VAR_INIT_STATUS_INITIALIZED);
14723 if (op0 == 0)
14724 break;
14725 else if (GET_CODE (rtl) == ZERO_EXTEND
14726 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
14727 && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
14728 < HOST_BITS_PER_WIDE_INT
14729 /* If DW_OP_const{1,2,4}u won't be used, it is shorter
14730 to expand zero extend as two shifts instead of
14731 masking. */
14732 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= 4)
14734 machine_mode imode = GET_MODE (XEXP (rtl, 0));
14735 mem_loc_result = op0;
14736 add_loc_descr (&mem_loc_result,
14737 int_loc_descriptor (GET_MODE_MASK (imode)));
14738 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
14740 else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
14742 int shift = DWARF2_ADDR_SIZE
14743 - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
14744 shift *= BITS_PER_UNIT;
14745 if (GET_CODE (rtl) == SIGN_EXTEND)
14746 op = DW_OP_shra;
14747 else
14748 op = DW_OP_shr;
14749 mem_loc_result = op0;
14750 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
14751 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
14752 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
14753 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14755 else if (!dwarf_strict || dwarf_version >= 5)
14757 dw_die_ref type_die1, type_die2;
14758 dw_loc_descr_ref cvt;
14760 type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
14761 GET_CODE (rtl) == ZERO_EXTEND);
14762 if (type_die1 == NULL)
14763 break;
14764 type_die2 = base_type_for_mode (mode, 1);
14765 if (type_die2 == NULL)
14766 break;
14767 mem_loc_result = op0;
14768 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14769 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14770 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
14771 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14772 add_loc_descr (&mem_loc_result, cvt);
14773 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14774 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14775 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
14776 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14777 add_loc_descr (&mem_loc_result, cvt);
14779 break;
14781 case MEM:
14783 rtx new_rtl = avoid_constant_pool_reference (rtl);
14784 if (new_rtl != rtl)
14786 mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
14787 initialized);
14788 if (mem_loc_result != NULL)
14789 return mem_loc_result;
14792 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
14793 get_address_mode (rtl), mode,
14794 VAR_INIT_STATUS_INITIALIZED);
14795 if (mem_loc_result == NULL)
14796 mem_loc_result = tls_mem_loc_descriptor (rtl);
14797 if (mem_loc_result != NULL)
14799 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
14800 || !SCALAR_INT_MODE_P(mode))
14802 dw_die_ref type_die;
14803 dw_loc_descr_ref deref;
14805 if (dwarf_strict && dwarf_version < 5)
14806 return NULL;
14807 type_die
14808 = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
14809 if (type_die == NULL)
14810 return NULL;
14811 deref = new_loc_descr (dwarf_OP (DW_OP_deref_type),
14812 GET_MODE_SIZE (mode), 0);
14813 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
14814 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
14815 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
14816 add_loc_descr (&mem_loc_result, deref);
14818 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
14819 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
14820 else
14821 add_loc_descr (&mem_loc_result,
14822 new_loc_descr (DW_OP_deref_size,
14823 GET_MODE_SIZE (mode), 0));
14825 break;
14827 case LO_SUM:
14828 return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
14830 case LABEL_REF:
14831 /* Some ports can transform a symbol ref into a label ref, because
14832 the symbol ref is too far away and has to be dumped into a constant
14833 pool. */
14834 case CONST:
14835 case SYMBOL_REF:
14836 if (!SCALAR_INT_MODE_P (mode)
14837 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
14838 #ifdef POINTERS_EXTEND_UNSIGNED
14839 && (mode != Pmode || mem_mode == VOIDmode)
14840 #endif
14842 break;
14843 if (GET_CODE (rtl) == SYMBOL_REF
14844 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
14846 dw_loc_descr_ref temp;
14848 /* If this is not defined, we have no way to emit the data. */
14849 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
14850 break;
14852 temp = new_addr_loc_descr (rtl, dtprel_true);
14854 /* We check for DWARF 5 here because gdb did not implement
14855 DW_OP_form_tls_address until after 7.12. */
14856 mem_loc_result = new_loc_descr ((dwarf_version >= 5
14857 ? DW_OP_form_tls_address
14858 : DW_OP_GNU_push_tls_address),
14859 0, 0);
14860 add_loc_descr (&mem_loc_result, temp);
14862 break;
14865 if (!const_ok_for_output (rtl))
14867 if (GET_CODE (rtl) == CONST)
14868 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14869 initialized);
14870 break;
14873 symref:
14874 mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
14875 vec_safe_push (used_rtx_array, rtl);
14876 break;
14878 case CONCAT:
14879 case CONCATN:
14880 case VAR_LOCATION:
14881 case DEBUG_IMPLICIT_PTR:
14882 expansion_failed (NULL_TREE, rtl,
14883 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
14884 return 0;
14886 case ENTRY_VALUE:
14887 if (dwarf_strict && dwarf_version < 5)
14888 return NULL;
14889 if (REG_P (ENTRY_VALUE_EXP (rtl)))
14891 if (!SCALAR_INT_MODE_P (mode)
14892 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
14893 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
14894 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14895 else
14897 unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
14898 if (dbx_regnum == IGNORED_DWARF_REGNUM)
14899 return NULL;
14900 op0 = one_reg_loc_descriptor (dbx_regnum,
14901 VAR_INIT_STATUS_INITIALIZED);
14904 else if (MEM_P (ENTRY_VALUE_EXP (rtl))
14905 && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
14907 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
14908 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14909 if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
14910 return NULL;
14912 else
14913 gcc_unreachable ();
14914 if (op0 == NULL)
14915 return NULL;
14916 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_entry_value), 0, 0);
14917 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
14918 mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
14919 break;
14921 case DEBUG_PARAMETER_REF:
14922 mem_loc_result = parameter_ref_descriptor (rtl);
14923 break;
14925 case PRE_MODIFY:
14926 /* Extract the PLUS expression nested inside and fall into
14927 PLUS code below. */
14928 rtl = XEXP (rtl, 1);
14929 goto plus;
14931 case PRE_INC:
14932 case PRE_DEC:
14933 /* Turn these into a PLUS expression and fall into the PLUS code
14934 below. */
14935 rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
14936 gen_int_mode (GET_CODE (rtl) == PRE_INC
14937 ? GET_MODE_UNIT_SIZE (mem_mode)
14938 : -GET_MODE_UNIT_SIZE (mem_mode),
14939 mode));
14941 /* fall through */
14943 case PLUS:
14944 plus:
14945 if (is_based_loc (rtl)
14946 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
14947 || XEXP (rtl, 0) == arg_pointer_rtx
14948 || XEXP (rtl, 0) == frame_pointer_rtx)
14949 && SCALAR_INT_MODE_P (mode))
14950 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
14951 INTVAL (XEXP (rtl, 1)),
14952 VAR_INIT_STATUS_INITIALIZED);
14953 else
14955 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14956 VAR_INIT_STATUS_INITIALIZED);
14957 if (mem_loc_result == 0)
14958 break;
14960 if (CONST_INT_P (XEXP (rtl, 1))
14961 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
14962 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
14963 else
14965 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14966 VAR_INIT_STATUS_INITIALIZED);
14967 if (op1 == 0)
14968 return NULL;
14969 add_loc_descr (&mem_loc_result, op1);
14970 add_loc_descr (&mem_loc_result,
14971 new_loc_descr (DW_OP_plus, 0, 0));
14974 break;
14976 /* If a pseudo-reg is optimized away, it is possible for it to
14977 be replaced with a MEM containing a multiply or shift. */
14978 case MINUS:
14979 op = DW_OP_minus;
14980 goto do_binop;
14982 case MULT:
14983 op = DW_OP_mul;
14984 goto do_binop;
14986 case DIV:
14987 if ((!dwarf_strict || dwarf_version >= 5)
14988 && SCALAR_INT_MODE_P (mode)
14989 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
14991 mem_loc_result = typed_binop (DW_OP_div, rtl,
14992 base_type_for_mode (mode, 0),
14993 mode, mem_mode);
14994 break;
14996 op = DW_OP_div;
14997 goto do_binop;
14999 case UMOD:
15000 op = DW_OP_mod;
15001 goto do_binop;
15003 case ASHIFT:
15004 op = DW_OP_shl;
15005 goto do_shift;
15007 case ASHIFTRT:
15008 op = DW_OP_shra;
15009 goto do_shift;
15011 case LSHIFTRT:
15012 op = DW_OP_shr;
15013 goto do_shift;
15015 do_shift:
15016 if (!SCALAR_INT_MODE_P (mode))
15017 break;
15018 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15019 VAR_INIT_STATUS_INITIALIZED);
15021 rtx rtlop1 = XEXP (rtl, 1);
15022 if (GET_MODE (rtlop1) != VOIDmode
15023 && GET_MODE_BITSIZE (GET_MODE (rtlop1))
15024 < GET_MODE_BITSIZE (mode))
15025 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
15026 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
15027 VAR_INIT_STATUS_INITIALIZED);
15030 if (op0 == 0 || op1 == 0)
15031 break;
15033 mem_loc_result = op0;
15034 add_loc_descr (&mem_loc_result, op1);
15035 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15036 break;
15038 case AND:
15039 op = DW_OP_and;
15040 goto do_binop;
15042 case IOR:
15043 op = DW_OP_or;
15044 goto do_binop;
15046 case XOR:
15047 op = DW_OP_xor;
15048 goto do_binop;
15050 do_binop:
15051 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15052 VAR_INIT_STATUS_INITIALIZED);
15053 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15054 VAR_INIT_STATUS_INITIALIZED);
15056 if (op0 == 0 || op1 == 0)
15057 break;
15059 mem_loc_result = op0;
15060 add_loc_descr (&mem_loc_result, op1);
15061 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15062 break;
15064 case MOD:
15065 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
15066 && (!dwarf_strict || dwarf_version >= 5))
15068 mem_loc_result = typed_binop (DW_OP_mod, rtl,
15069 base_type_for_mode (mode, 0),
15070 mode, mem_mode);
15071 break;
15074 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15075 VAR_INIT_STATUS_INITIALIZED);
15076 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15077 VAR_INIT_STATUS_INITIALIZED);
15079 if (op0 == 0 || op1 == 0)
15080 break;
15082 mem_loc_result = op0;
15083 add_loc_descr (&mem_loc_result, op1);
15084 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
15085 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
15086 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
15087 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
15088 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
15089 break;
15091 case UDIV:
15092 if ((!dwarf_strict || dwarf_version >= 5)
15093 && SCALAR_INT_MODE_P (mode))
15095 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
15097 op = DW_OP_div;
15098 goto do_binop;
15100 mem_loc_result = typed_binop (DW_OP_div, rtl,
15101 base_type_for_mode (mode, 1),
15102 mode, mem_mode);
15104 break;
15106 case NOT:
15107 op = DW_OP_not;
15108 goto do_unop;
15110 case ABS:
15111 op = DW_OP_abs;
15112 goto do_unop;
15114 case NEG:
15115 op = DW_OP_neg;
15116 goto do_unop;
15118 do_unop:
15119 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15120 VAR_INIT_STATUS_INITIALIZED);
15122 if (op0 == 0)
15123 break;
15125 mem_loc_result = op0;
15126 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15127 break;
15129 case CONST_INT:
15130 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
15131 #ifdef POINTERS_EXTEND_UNSIGNED
15132 || (mode == Pmode
15133 && mem_mode != VOIDmode
15134 && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
15135 #endif
15138 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
15139 break;
15141 if ((!dwarf_strict || dwarf_version >= 5)
15142 && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
15143 || GET_MODE_BITSIZE (mode) == HOST_BITS_PER_DOUBLE_INT))
15145 dw_die_ref type_die = base_type_for_mode (mode, 1);
15146 machine_mode amode;
15147 if (type_die == NULL)
15148 return NULL;
15149 amode = mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT,
15150 MODE_INT, 0);
15151 if (INTVAL (rtl) >= 0
15152 && amode != BLKmode
15153 && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
15154 /* const DW_OP_convert <XXX> vs.
15155 DW_OP_const_type <XXX, 1, const>. */
15156 && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
15157 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (mode))
15159 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
15160 op0 = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15161 op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15162 op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15163 op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
15164 add_loc_descr (&mem_loc_result, op0);
15165 return mem_loc_result;
15167 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0,
15168 INTVAL (rtl));
15169 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15170 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15171 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
15172 if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
15173 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
15174 else
15176 mem_loc_result->dw_loc_oprnd2.val_class
15177 = dw_val_class_const_double;
15178 mem_loc_result->dw_loc_oprnd2.v.val_double
15179 = double_int::from_shwi (INTVAL (rtl));
15182 break;
15184 case CONST_DOUBLE:
15185 if (!dwarf_strict || dwarf_version >= 5)
15187 dw_die_ref type_die;
15189 /* Note that if TARGET_SUPPORTS_WIDE_INT == 0, a
15190 CONST_DOUBLE rtx could represent either a large integer
15191 or a floating-point constant. If TARGET_SUPPORTS_WIDE_INT != 0,
15192 the value is always a floating point constant.
15194 When it is an integer, a CONST_DOUBLE is used whenever
15195 the constant requires 2 HWIs to be adequately represented.
15196 We output CONST_DOUBLEs as blocks. */
15197 if (mode == VOIDmode
15198 || (GET_MODE (rtl) == VOIDmode
15199 && GET_MODE_BITSIZE (mode) != HOST_BITS_PER_DOUBLE_INT))
15200 break;
15201 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
15202 if (type_die == NULL)
15203 return NULL;
15204 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0, 0);
15205 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15206 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15207 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
15208 #if TARGET_SUPPORTS_WIDE_INT == 0
15209 if (!SCALAR_FLOAT_MODE_P (mode))
15211 mem_loc_result->dw_loc_oprnd2.val_class
15212 = dw_val_class_const_double;
15213 mem_loc_result->dw_loc_oprnd2.v.val_double
15214 = rtx_to_double_int (rtl);
15216 else
15217 #endif
15219 unsigned int length = GET_MODE_SIZE (mode);
15220 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
15222 insert_float (rtl, array);
15223 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
15224 mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
15225 mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
15226 mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
15229 break;
15231 case CONST_WIDE_INT:
15232 if (!dwarf_strict || dwarf_version >= 5)
15234 dw_die_ref type_die;
15236 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
15237 if (type_die == NULL)
15238 return NULL;
15239 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0, 0);
15240 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15241 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15242 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
15243 mem_loc_result->dw_loc_oprnd2.val_class
15244 = dw_val_class_wide_int;
15245 mem_loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
15246 *mem_loc_result->dw_loc_oprnd2.v.val_wide = rtx_mode_t (rtl, mode);
15248 break;
15250 case EQ:
15251 mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
15252 break;
15254 case GE:
15255 mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
15256 break;
15258 case GT:
15259 mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
15260 break;
15262 case LE:
15263 mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
15264 break;
15266 case LT:
15267 mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
15268 break;
15270 case NE:
15271 mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
15272 break;
15274 case GEU:
15275 mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
15276 break;
15278 case GTU:
15279 mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
15280 break;
15282 case LEU:
15283 mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
15284 break;
15286 case LTU:
15287 mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
15288 break;
15290 case UMIN:
15291 case UMAX:
15292 if (!SCALAR_INT_MODE_P (mode))
15293 break;
15294 /* FALLTHRU */
15295 case SMIN:
15296 case SMAX:
15297 mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
15298 break;
15300 case ZERO_EXTRACT:
15301 case SIGN_EXTRACT:
15302 if (CONST_INT_P (XEXP (rtl, 1))
15303 && CONST_INT_P (XEXP (rtl, 2))
15304 && ((unsigned) INTVAL (XEXP (rtl, 1))
15305 + (unsigned) INTVAL (XEXP (rtl, 2))
15306 <= GET_MODE_BITSIZE (mode))
15307 && SCALAR_INT_MODE_P (mode)
15308 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
15309 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
15311 int shift, size;
15312 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
15313 mem_mode, VAR_INIT_STATUS_INITIALIZED);
15314 if (op0 == 0)
15315 break;
15316 if (GET_CODE (rtl) == SIGN_EXTRACT)
15317 op = DW_OP_shra;
15318 else
15319 op = DW_OP_shr;
15320 mem_loc_result = op0;
15321 size = INTVAL (XEXP (rtl, 1));
15322 shift = INTVAL (XEXP (rtl, 2));
15323 if (BITS_BIG_ENDIAN)
15324 shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
15325 - shift - size;
15326 if (shift + size != (int) DWARF2_ADDR_SIZE)
15328 add_loc_descr (&mem_loc_result,
15329 int_loc_descriptor (DWARF2_ADDR_SIZE
15330 - shift - size));
15331 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
15333 if (size != (int) DWARF2_ADDR_SIZE)
15335 add_loc_descr (&mem_loc_result,
15336 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
15337 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15340 break;
15342 case IF_THEN_ELSE:
15344 dw_loc_descr_ref op2, bra_node, drop_node;
15345 op0 = mem_loc_descriptor (XEXP (rtl, 0),
15346 GET_MODE (XEXP (rtl, 0)) == VOIDmode
15347 ? word_mode : GET_MODE (XEXP (rtl, 0)),
15348 mem_mode, VAR_INIT_STATUS_INITIALIZED);
15349 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15350 VAR_INIT_STATUS_INITIALIZED);
15351 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
15352 VAR_INIT_STATUS_INITIALIZED);
15353 if (op0 == NULL || op1 == NULL || op2 == NULL)
15354 break;
15356 mem_loc_result = op1;
15357 add_loc_descr (&mem_loc_result, op2);
15358 add_loc_descr (&mem_loc_result, op0);
15359 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
15360 add_loc_descr (&mem_loc_result, bra_node);
15361 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
15362 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
15363 add_loc_descr (&mem_loc_result, drop_node);
15364 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15365 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
15367 break;
15369 case FLOAT_EXTEND:
15370 case FLOAT_TRUNCATE:
15371 case FLOAT:
15372 case UNSIGNED_FLOAT:
15373 case FIX:
15374 case UNSIGNED_FIX:
15375 if (!dwarf_strict || dwarf_version >= 5)
15377 dw_die_ref type_die;
15378 dw_loc_descr_ref cvt;
15380 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
15381 mem_mode, VAR_INIT_STATUS_INITIALIZED);
15382 if (op0 == NULL)
15383 break;
15384 if (SCALAR_INT_MODE_P (GET_MODE (XEXP (rtl, 0)))
15385 && (GET_CODE (rtl) == FLOAT
15386 || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
15387 <= DWARF2_ADDR_SIZE))
15389 type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
15390 GET_CODE (rtl) == UNSIGNED_FLOAT);
15391 if (type_die == NULL)
15392 break;
15393 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15394 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15395 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15396 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15397 add_loc_descr (&op0, cvt);
15399 type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
15400 if (type_die == NULL)
15401 break;
15402 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15403 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15404 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15405 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15406 add_loc_descr (&op0, cvt);
15407 if (SCALAR_INT_MODE_P (mode)
15408 && (GET_CODE (rtl) == FIX
15409 || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
15411 op0 = convert_descriptor_to_mode (mode, op0);
15412 if (op0 == NULL)
15413 break;
15415 mem_loc_result = op0;
15417 break;
15419 case CLZ:
15420 case CTZ:
15421 case FFS:
15422 mem_loc_result = clz_loc_descriptor (rtl, mode, mem_mode);
15423 break;
15425 case POPCOUNT:
15426 case PARITY:
15427 mem_loc_result = popcount_loc_descriptor (rtl, mode, mem_mode);
15428 break;
15430 case BSWAP:
15431 mem_loc_result = bswap_loc_descriptor (rtl, mode, mem_mode);
15432 break;
15434 case ROTATE:
15435 case ROTATERT:
15436 mem_loc_result = rotate_loc_descriptor (rtl, mode, mem_mode);
15437 break;
15439 case COMPARE:
15440 /* In theory, we could implement the above. */
15441 /* DWARF cannot represent the unsigned compare operations
15442 natively. */
15443 case SS_MULT:
15444 case US_MULT:
15445 case SS_DIV:
15446 case US_DIV:
15447 case SS_PLUS:
15448 case US_PLUS:
15449 case SS_MINUS:
15450 case US_MINUS:
15451 case SS_NEG:
15452 case US_NEG:
15453 case SS_ABS:
15454 case SS_ASHIFT:
15455 case US_ASHIFT:
15456 case SS_TRUNCATE:
15457 case US_TRUNCATE:
15458 case UNORDERED:
15459 case ORDERED:
15460 case UNEQ:
15461 case UNGE:
15462 case UNGT:
15463 case UNLE:
15464 case UNLT:
15465 case LTGT:
15466 case FRACT_CONVERT:
15467 case UNSIGNED_FRACT_CONVERT:
15468 case SAT_FRACT:
15469 case UNSIGNED_SAT_FRACT:
15470 case SQRT:
15471 case ASM_OPERANDS:
15472 case VEC_MERGE:
15473 case VEC_SELECT:
15474 case VEC_CONCAT:
15475 case VEC_DUPLICATE:
15476 case UNSPEC:
15477 case HIGH:
15478 case FMA:
15479 case STRICT_LOW_PART:
15480 case CONST_VECTOR:
15481 case CONST_FIXED:
15482 case CLRSB:
15483 case CLOBBER:
15484 /* If delegitimize_address couldn't do anything with the UNSPEC, we
15485 can't express it in the debug info. This can happen e.g. with some
15486 TLS UNSPECs. */
15487 break;
15489 case CONST_STRING:
15490 resolve_one_addr (&rtl);
15491 goto symref;
15493 /* RTL sequences inside PARALLEL record a series of DWARF operations for
15494 the expression. An UNSPEC rtx represents a raw DWARF operation,
15495 new_loc_descr is called for it to build the operation directly.
15496 Otherwise mem_loc_descriptor is called recursively. */
15497 case PARALLEL:
15499 int index = 0;
15500 dw_loc_descr_ref exp_result = NULL;
15502 for (; index < XVECLEN (rtl, 0); index++)
15504 rtx elem = XVECEXP (rtl, 0, index);
15505 if (GET_CODE (elem) == UNSPEC)
15507 /* Each DWARF operation UNSPEC contain two operands, if
15508 one operand is not used for the operation, const0_rtx is
15509 passed. */
15510 gcc_assert (XVECLEN (elem, 0) == 2);
15512 HOST_WIDE_INT dw_op = XINT (elem, 1);
15513 HOST_WIDE_INT oprnd1 = INTVAL (XVECEXP (elem, 0, 0));
15514 HOST_WIDE_INT oprnd2 = INTVAL (XVECEXP (elem, 0, 1));
15515 exp_result
15516 = new_loc_descr ((enum dwarf_location_atom) dw_op, oprnd1,
15517 oprnd2);
15519 else
15520 exp_result
15521 = mem_loc_descriptor (elem, mode, mem_mode,
15522 VAR_INIT_STATUS_INITIALIZED);
15524 if (!mem_loc_result)
15525 mem_loc_result = exp_result;
15526 else
15527 add_loc_descr (&mem_loc_result, exp_result);
15530 break;
15533 default:
15534 if (flag_checking)
15536 print_rtl (stderr, rtl);
15537 gcc_unreachable ();
15539 break;
15542 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
15543 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
15545 return mem_loc_result;
15548 /* Return a descriptor that describes the concatenation of two locations.
15549 This is typically a complex variable. */
15551 static dw_loc_descr_ref
15552 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
15554 dw_loc_descr_ref cc_loc_result = NULL;
15555 dw_loc_descr_ref x0_ref
15556 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15557 dw_loc_descr_ref x1_ref
15558 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15560 if (x0_ref == 0 || x1_ref == 0)
15561 return 0;
15563 cc_loc_result = x0_ref;
15564 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
15566 add_loc_descr (&cc_loc_result, x1_ref);
15567 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
15569 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
15570 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
15572 return cc_loc_result;
15575 /* Return a descriptor that describes the concatenation of N
15576 locations. */
15578 static dw_loc_descr_ref
15579 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
15581 unsigned int i;
15582 dw_loc_descr_ref cc_loc_result = NULL;
15583 unsigned int n = XVECLEN (concatn, 0);
15585 for (i = 0; i < n; ++i)
15587 dw_loc_descr_ref ref;
15588 rtx x = XVECEXP (concatn, 0, i);
15590 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15591 if (ref == NULL)
15592 return NULL;
15594 add_loc_descr (&cc_loc_result, ref);
15595 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
15598 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
15599 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
15601 return cc_loc_result;
15604 /* Helper function for loc_descriptor. Return DW_OP_implicit_pointer
15605 for DEBUG_IMPLICIT_PTR RTL. */
15607 static dw_loc_descr_ref
15608 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
15610 dw_loc_descr_ref ret;
15611 dw_die_ref ref;
15613 if (dwarf_strict && dwarf_version < 5)
15614 return NULL;
15615 gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
15616 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
15617 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
15618 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
15619 ret = new_loc_descr (dwarf_OP (DW_OP_implicit_pointer), 0, offset);
15620 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
15621 if (ref)
15623 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15624 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
15625 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
15627 else
15629 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
15630 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
15632 return ret;
15635 /* Output a proper Dwarf location descriptor for a variable or parameter
15636 which is either allocated in a register or in a memory location. For a
15637 register, we just generate an OP_REG and the register number. For a
15638 memory location we provide a Dwarf postfix expression describing how to
15639 generate the (dynamic) address of the object onto the address stack.
15641 MODE is mode of the decl if this loc_descriptor is going to be used in
15642 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
15643 allowed, VOIDmode otherwise.
15645 If we don't know how to describe it, return 0. */
15647 static dw_loc_descr_ref
15648 loc_descriptor (rtx rtl, machine_mode mode,
15649 enum var_init_status initialized)
15651 dw_loc_descr_ref loc_result = NULL;
15653 switch (GET_CODE (rtl))
15655 case SUBREG:
15656 /* The case of a subreg may arise when we have a local (register)
15657 variable or a formal (register) parameter which doesn't quite fill
15658 up an entire register. For now, just assume that it is
15659 legitimate to make the Dwarf info refer to the whole register which
15660 contains the given subreg. */
15661 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
15662 loc_result = loc_descriptor (SUBREG_REG (rtl),
15663 GET_MODE (SUBREG_REG (rtl)), initialized);
15664 else
15665 goto do_default;
15666 break;
15668 case REG:
15669 loc_result = reg_loc_descriptor (rtl, initialized);
15670 break;
15672 case MEM:
15673 loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
15674 GET_MODE (rtl), initialized);
15675 if (loc_result == NULL)
15676 loc_result = tls_mem_loc_descriptor (rtl);
15677 if (loc_result == NULL)
15679 rtx new_rtl = avoid_constant_pool_reference (rtl);
15680 if (new_rtl != rtl)
15681 loc_result = loc_descriptor (new_rtl, mode, initialized);
15683 break;
15685 case CONCAT:
15686 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
15687 initialized);
15688 break;
15690 case CONCATN:
15691 loc_result = concatn_loc_descriptor (rtl, initialized);
15692 break;
15694 case VAR_LOCATION:
15695 /* Single part. */
15696 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
15698 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
15699 if (GET_CODE (loc) == EXPR_LIST)
15700 loc = XEXP (loc, 0);
15701 loc_result = loc_descriptor (loc, mode, initialized);
15702 break;
15705 rtl = XEXP (rtl, 1);
15706 /* FALLTHRU */
15708 case PARALLEL:
15710 rtvec par_elems = XVEC (rtl, 0);
15711 int num_elem = GET_NUM_ELEM (par_elems);
15712 machine_mode mode;
15713 int i;
15715 /* Create the first one, so we have something to add to. */
15716 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
15717 VOIDmode, initialized);
15718 if (loc_result == NULL)
15719 return NULL;
15720 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
15721 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
15722 for (i = 1; i < num_elem; i++)
15724 dw_loc_descr_ref temp;
15726 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
15727 VOIDmode, initialized);
15728 if (temp == NULL)
15729 return NULL;
15730 add_loc_descr (&loc_result, temp);
15731 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
15732 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
15735 break;
15737 case CONST_INT:
15738 if (mode != VOIDmode && mode != BLKmode)
15739 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
15740 INTVAL (rtl));
15741 break;
15743 case CONST_DOUBLE:
15744 if (mode == VOIDmode)
15745 mode = GET_MODE (rtl);
15747 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
15749 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
15751 /* Note that a CONST_DOUBLE rtx could represent either an integer
15752 or a floating-point constant. A CONST_DOUBLE is used whenever
15753 the constant requires more than one word in order to be
15754 adequately represented. We output CONST_DOUBLEs as blocks. */
15755 loc_result = new_loc_descr (DW_OP_implicit_value,
15756 GET_MODE_SIZE (mode), 0);
15757 #if TARGET_SUPPORTS_WIDE_INT == 0
15758 if (!SCALAR_FLOAT_MODE_P (mode))
15760 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
15761 loc_result->dw_loc_oprnd2.v.val_double
15762 = rtx_to_double_int (rtl);
15764 else
15765 #endif
15767 unsigned int length = GET_MODE_SIZE (mode);
15768 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
15770 insert_float (rtl, array);
15771 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
15772 loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
15773 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
15774 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
15777 break;
15779 case CONST_WIDE_INT:
15780 if (mode == VOIDmode)
15781 mode = GET_MODE (rtl);
15783 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
15785 loc_result = new_loc_descr (DW_OP_implicit_value,
15786 GET_MODE_SIZE (mode), 0);
15787 loc_result->dw_loc_oprnd2.val_class = dw_val_class_wide_int;
15788 loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
15789 *loc_result->dw_loc_oprnd2.v.val_wide = rtx_mode_t (rtl, mode);
15791 break;
15793 case CONST_VECTOR:
15794 if (mode == VOIDmode)
15795 mode = GET_MODE (rtl);
15797 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
15799 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
15800 unsigned int length = CONST_VECTOR_NUNITS (rtl);
15801 unsigned char *array
15802 = ggc_vec_alloc<unsigned char> (length * elt_size);
15803 unsigned int i;
15804 unsigned char *p;
15805 machine_mode imode = GET_MODE_INNER (mode);
15807 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
15808 switch (GET_MODE_CLASS (mode))
15810 case MODE_VECTOR_INT:
15811 for (i = 0, p = array; i < length; i++, p += elt_size)
15813 rtx elt = CONST_VECTOR_ELT (rtl, i);
15814 insert_wide_int (rtx_mode_t (elt, imode), p, elt_size);
15816 break;
15818 case MODE_VECTOR_FLOAT:
15819 for (i = 0, p = array; i < length; i++, p += elt_size)
15821 rtx elt = CONST_VECTOR_ELT (rtl, i);
15822 insert_float (elt, p);
15824 break;
15826 default:
15827 gcc_unreachable ();
15830 loc_result = new_loc_descr (DW_OP_implicit_value,
15831 length * elt_size, 0);
15832 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
15833 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
15834 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
15835 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
15837 break;
15839 case CONST:
15840 if (mode == VOIDmode
15841 || CONST_SCALAR_INT_P (XEXP (rtl, 0))
15842 || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
15843 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
15845 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
15846 break;
15848 /* FALLTHROUGH */
15849 case SYMBOL_REF:
15850 if (!const_ok_for_output (rtl))
15851 break;
15852 /* FALLTHROUGH */
15853 case LABEL_REF:
15854 if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
15855 && (dwarf_version >= 4 || !dwarf_strict))
15857 loc_result = new_addr_loc_descr (rtl, dtprel_false);
15858 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
15859 vec_safe_push (used_rtx_array, rtl);
15861 break;
15863 case DEBUG_IMPLICIT_PTR:
15864 loc_result = implicit_ptr_descriptor (rtl, 0);
15865 break;
15867 case PLUS:
15868 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
15869 && CONST_INT_P (XEXP (rtl, 1)))
15871 loc_result
15872 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
15873 break;
15875 /* FALLTHRU */
15876 do_default:
15877 default:
15878 if ((SCALAR_INT_MODE_P (mode)
15879 && GET_MODE (rtl) == mode
15880 && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
15881 && dwarf_version >= 4)
15882 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
15884 /* Value expression. */
15885 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
15886 if (loc_result)
15887 add_loc_descr (&loc_result,
15888 new_loc_descr (DW_OP_stack_value, 0, 0));
15890 break;
15893 return loc_result;
15896 /* We need to figure out what section we should use as the base for the
15897 address ranges where a given location is valid.
15898 1. If this particular DECL has a section associated with it, use that.
15899 2. If this function has a section associated with it, use that.
15900 3. Otherwise, use the text section.
15901 XXX: If you split a variable across multiple sections, we won't notice. */
15903 static const char *
15904 secname_for_decl (const_tree decl)
15906 const char *secname;
15908 if (VAR_OR_FUNCTION_DECL_P (decl)
15909 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl) || TREE_STATIC (decl))
15910 && DECL_SECTION_NAME (decl))
15911 secname = DECL_SECTION_NAME (decl);
15912 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
15913 secname = DECL_SECTION_NAME (current_function_decl);
15914 else if (cfun && in_cold_section_p)
15915 secname = crtl->subsections.cold_section_label;
15916 else
15917 secname = text_section_label;
15919 return secname;
15922 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
15924 static bool
15925 decl_by_reference_p (tree decl)
15927 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
15928 || VAR_P (decl))
15929 && DECL_BY_REFERENCE (decl));
15932 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
15933 for VARLOC. */
15935 static dw_loc_descr_ref
15936 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
15937 enum var_init_status initialized)
15939 int have_address = 0;
15940 dw_loc_descr_ref descr;
15941 machine_mode mode;
15943 if (want_address != 2)
15945 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
15946 /* Single part. */
15947 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
15949 varloc = PAT_VAR_LOCATION_LOC (varloc);
15950 if (GET_CODE (varloc) == EXPR_LIST)
15951 varloc = XEXP (varloc, 0);
15952 mode = GET_MODE (varloc);
15953 if (MEM_P (varloc))
15955 rtx addr = XEXP (varloc, 0);
15956 descr = mem_loc_descriptor (addr, get_address_mode (varloc),
15957 mode, initialized);
15958 if (descr)
15959 have_address = 1;
15960 else
15962 rtx x = avoid_constant_pool_reference (varloc);
15963 if (x != varloc)
15964 descr = mem_loc_descriptor (x, mode, VOIDmode,
15965 initialized);
15968 else
15969 descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
15971 else
15972 return 0;
15974 else
15976 if (GET_CODE (varloc) == VAR_LOCATION)
15977 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
15978 else
15979 mode = DECL_MODE (loc);
15980 descr = loc_descriptor (varloc, mode, initialized);
15981 have_address = 1;
15984 if (!descr)
15985 return 0;
15987 if (want_address == 2 && !have_address
15988 && (dwarf_version >= 4 || !dwarf_strict))
15990 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
15992 expansion_failed (loc, NULL_RTX,
15993 "DWARF address size mismatch");
15994 return 0;
15996 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
15997 have_address = 1;
15999 /* Show if we can't fill the request for an address. */
16000 if (want_address && !have_address)
16002 expansion_failed (loc, NULL_RTX,
16003 "Want address and only have value");
16004 return 0;
16007 /* If we've got an address and don't want one, dereference. */
16008 if (!want_address && have_address)
16010 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
16011 enum dwarf_location_atom op;
16013 if (size > DWARF2_ADDR_SIZE || size == -1)
16015 expansion_failed (loc, NULL_RTX,
16016 "DWARF address size mismatch");
16017 return 0;
16019 else if (size == DWARF2_ADDR_SIZE)
16020 op = DW_OP_deref;
16021 else
16022 op = DW_OP_deref_size;
16024 add_loc_descr (&descr, new_loc_descr (op, size, 0));
16027 return descr;
16030 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
16031 if it is not possible. */
16033 static dw_loc_descr_ref
16034 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
16036 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
16037 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
16038 else if (dwarf_version >= 3 || !dwarf_strict)
16039 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
16040 else
16041 return NULL;
16044 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
16045 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
16047 static dw_loc_descr_ref
16048 dw_sra_loc_expr (tree decl, rtx loc)
16050 rtx p;
16051 unsigned HOST_WIDE_INT padsize = 0;
16052 dw_loc_descr_ref descr, *descr_tail;
16053 unsigned HOST_WIDE_INT decl_size;
16054 rtx varloc;
16055 enum var_init_status initialized;
16057 if (DECL_SIZE (decl) == NULL
16058 || !tree_fits_uhwi_p (DECL_SIZE (decl)))
16059 return NULL;
16061 decl_size = tree_to_uhwi (DECL_SIZE (decl));
16062 descr = NULL;
16063 descr_tail = &descr;
16065 for (p = loc; p; p = XEXP (p, 1))
16067 unsigned HOST_WIDE_INT bitsize = decl_piece_bitsize (p);
16068 rtx loc_note = *decl_piece_varloc_ptr (p);
16069 dw_loc_descr_ref cur_descr;
16070 dw_loc_descr_ref *tail, last = NULL;
16071 unsigned HOST_WIDE_INT opsize = 0;
16073 if (loc_note == NULL_RTX
16074 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
16076 padsize += bitsize;
16077 continue;
16079 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
16080 varloc = NOTE_VAR_LOCATION (loc_note);
16081 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
16082 if (cur_descr == NULL)
16084 padsize += bitsize;
16085 continue;
16088 /* Check that cur_descr either doesn't use
16089 DW_OP_*piece operations, or their sum is equal
16090 to bitsize. Otherwise we can't embed it. */
16091 for (tail = &cur_descr; *tail != NULL;
16092 tail = &(*tail)->dw_loc_next)
16093 if ((*tail)->dw_loc_opc == DW_OP_piece)
16095 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
16096 * BITS_PER_UNIT;
16097 last = *tail;
16099 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
16101 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
16102 last = *tail;
16105 if (last != NULL && opsize != bitsize)
16107 padsize += bitsize;
16108 /* Discard the current piece of the descriptor and release any
16109 addr_table entries it uses. */
16110 remove_loc_list_addr_table_entries (cur_descr);
16111 continue;
16114 /* If there is a hole, add DW_OP_*piece after empty DWARF
16115 expression, which means that those bits are optimized out. */
16116 if (padsize)
16118 if (padsize > decl_size)
16120 remove_loc_list_addr_table_entries (cur_descr);
16121 goto discard_descr;
16123 decl_size -= padsize;
16124 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
16125 if (*descr_tail == NULL)
16127 remove_loc_list_addr_table_entries (cur_descr);
16128 goto discard_descr;
16130 descr_tail = &(*descr_tail)->dw_loc_next;
16131 padsize = 0;
16133 *descr_tail = cur_descr;
16134 descr_tail = tail;
16135 if (bitsize > decl_size)
16136 goto discard_descr;
16137 decl_size -= bitsize;
16138 if (last == NULL)
16140 HOST_WIDE_INT offset = 0;
16141 if (GET_CODE (varloc) == VAR_LOCATION
16142 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
16144 varloc = PAT_VAR_LOCATION_LOC (varloc);
16145 if (GET_CODE (varloc) == EXPR_LIST)
16146 varloc = XEXP (varloc, 0);
16150 if (GET_CODE (varloc) == CONST
16151 || GET_CODE (varloc) == SIGN_EXTEND
16152 || GET_CODE (varloc) == ZERO_EXTEND)
16153 varloc = XEXP (varloc, 0);
16154 else if (GET_CODE (varloc) == SUBREG)
16155 varloc = SUBREG_REG (varloc);
16156 else
16157 break;
16159 while (1);
16160 /* DW_OP_bit_size offset should be zero for register
16161 or implicit location descriptions and empty location
16162 descriptions, but for memory addresses needs big endian
16163 adjustment. */
16164 if (MEM_P (varloc))
16166 unsigned HOST_WIDE_INT memsize
16167 = MEM_SIZE (varloc) * BITS_PER_UNIT;
16168 if (memsize != bitsize)
16170 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
16171 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
16172 goto discard_descr;
16173 if (memsize < bitsize)
16174 goto discard_descr;
16175 if (BITS_BIG_ENDIAN)
16176 offset = memsize - bitsize;
16180 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
16181 if (*descr_tail == NULL)
16182 goto discard_descr;
16183 descr_tail = &(*descr_tail)->dw_loc_next;
16187 /* If there were any non-empty expressions, add padding till the end of
16188 the decl. */
16189 if (descr != NULL && decl_size != 0)
16191 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
16192 if (*descr_tail == NULL)
16193 goto discard_descr;
16195 return descr;
16197 discard_descr:
16198 /* Discard the descriptor and release any addr_table entries it uses. */
16199 remove_loc_list_addr_table_entries (descr);
16200 return NULL;
16203 /* Return the dwarf representation of the location list LOC_LIST of
16204 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
16205 function. */
16207 static dw_loc_list_ref
16208 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
16210 const char *endname, *secname;
16211 rtx varloc;
16212 enum var_init_status initialized;
16213 struct var_loc_node *node;
16214 dw_loc_descr_ref descr;
16215 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
16216 dw_loc_list_ref list = NULL;
16217 dw_loc_list_ref *listp = &list;
16219 /* Now that we know what section we are using for a base,
16220 actually construct the list of locations.
16221 The first location information is what is passed to the
16222 function that creates the location list, and the remaining
16223 locations just get added on to that list.
16224 Note that we only know the start address for a location
16225 (IE location changes), so to build the range, we use
16226 the range [current location start, next location start].
16227 This means we have to special case the last node, and generate
16228 a range of [last location start, end of function label]. */
16230 secname = secname_for_decl (decl);
16232 for (node = loc_list->first; node; node = node->next)
16233 if (GET_CODE (node->loc) == EXPR_LIST
16234 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
16236 if (GET_CODE (node->loc) == EXPR_LIST)
16238 /* This requires DW_OP_{,bit_}piece, which is not usable
16239 inside DWARF expressions. */
16240 if (want_address != 2)
16241 continue;
16242 descr = dw_sra_loc_expr (decl, node->loc);
16243 if (descr == NULL)
16244 continue;
16246 else
16248 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
16249 varloc = NOTE_VAR_LOCATION (node->loc);
16250 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
16252 if (descr)
16254 bool range_across_switch = false;
16255 /* If section switch happens in between node->label
16256 and node->next->label (or end of function) and
16257 we can't emit it as a single entry list,
16258 emit two ranges, first one ending at the end
16259 of first partition and second one starting at the
16260 beginning of second partition. */
16261 if (node == loc_list->last_before_switch
16262 && (node != loc_list->first || loc_list->first->next)
16263 && current_function_decl)
16265 endname = cfun->fde->dw_fde_end;
16266 range_across_switch = true;
16268 /* The variable has a location between NODE->LABEL and
16269 NODE->NEXT->LABEL. */
16270 else if (node->next)
16271 endname = node->next->label;
16272 /* If the variable has a location at the last label
16273 it keeps its location until the end of function. */
16274 else if (!current_function_decl)
16275 endname = text_end_label;
16276 else
16278 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
16279 current_function_funcdef_no);
16280 endname = ggc_strdup (label_id);
16283 *listp = new_loc_list (descr, node->label, endname, secname);
16284 if (TREE_CODE (decl) == PARM_DECL
16285 && node == loc_list->first
16286 && NOTE_P (node->loc)
16287 && strcmp (node->label, endname) == 0)
16288 (*listp)->force = true;
16289 listp = &(*listp)->dw_loc_next;
16291 if (range_across_switch)
16293 if (GET_CODE (node->loc) == EXPR_LIST)
16294 descr = dw_sra_loc_expr (decl, node->loc);
16295 else
16297 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
16298 varloc = NOTE_VAR_LOCATION (node->loc);
16299 descr = dw_loc_list_1 (decl, varloc, want_address,
16300 initialized);
16302 gcc_assert (descr);
16303 /* The variable has a location between NODE->LABEL and
16304 NODE->NEXT->LABEL. */
16305 if (node->next)
16306 endname = node->next->label;
16307 else
16308 endname = cfun->fde->dw_fde_second_end;
16309 *listp = new_loc_list (descr,
16310 cfun->fde->dw_fde_second_begin,
16311 endname, secname);
16312 listp = &(*listp)->dw_loc_next;
16317 /* Try to avoid the overhead of a location list emitting a location
16318 expression instead, but only if we didn't have more than one
16319 location entry in the first place. If some entries were not
16320 representable, we don't want to pretend a single entry that was
16321 applies to the entire scope in which the variable is
16322 available. */
16323 if (list && loc_list->first->next)
16324 gen_llsym (list);
16326 return list;
16329 /* Return if the loc_list has only single element and thus can be represented
16330 as location description. */
16332 static bool
16333 single_element_loc_list_p (dw_loc_list_ref list)
16335 gcc_assert (!list->dw_loc_next || list->ll_symbol);
16336 return !list->ll_symbol;
16339 /* Duplicate a single element of location list. */
16341 static inline dw_loc_descr_ref
16342 copy_loc_descr (dw_loc_descr_ref ref)
16344 dw_loc_descr_ref copy = ggc_alloc<dw_loc_descr_node> ();
16345 memcpy (copy, ref, sizeof (dw_loc_descr_node));
16346 return copy;
16349 /* To each location in list LIST append loc descr REF. */
16351 static void
16352 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
16354 dw_loc_descr_ref copy;
16355 add_loc_descr (&list->expr, ref);
16356 list = list->dw_loc_next;
16357 while (list)
16359 copy = copy_loc_descr (ref);
16360 add_loc_descr (&list->expr, copy);
16361 while (copy->dw_loc_next)
16362 copy = copy->dw_loc_next = copy_loc_descr (copy->dw_loc_next);
16363 list = list->dw_loc_next;
16367 /* To each location in list LIST prepend loc descr REF. */
16369 static void
16370 prepend_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
16372 dw_loc_descr_ref copy;
16373 dw_loc_descr_ref ref_end = list->expr;
16374 add_loc_descr (&ref, list->expr);
16375 list->expr = ref;
16376 list = list->dw_loc_next;
16377 while (list)
16379 dw_loc_descr_ref end = list->expr;
16380 list->expr = copy = copy_loc_descr (ref);
16381 while (copy->dw_loc_next != ref_end)
16382 copy = copy->dw_loc_next = copy_loc_descr (copy->dw_loc_next);
16383 copy->dw_loc_next = end;
16384 list = list->dw_loc_next;
16388 /* Given two lists RET and LIST
16389 produce location list that is result of adding expression in LIST
16390 to expression in RET on each position in program.
16391 Might be destructive on both RET and LIST.
16393 TODO: We handle only simple cases of RET or LIST having at most one
16394 element. General case would involve sorting the lists in program order
16395 and merging them that will need some additional work.
16396 Adding that will improve quality of debug info especially for SRA-ed
16397 structures. */
16399 static void
16400 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
16402 if (!list)
16403 return;
16404 if (!*ret)
16406 *ret = list;
16407 return;
16409 if (!list->dw_loc_next)
16411 add_loc_descr_to_each (*ret, list->expr);
16412 return;
16414 if (!(*ret)->dw_loc_next)
16416 prepend_loc_descr_to_each (list, (*ret)->expr);
16417 *ret = list;
16418 return;
16420 expansion_failed (NULL_TREE, NULL_RTX,
16421 "Don't know how to merge two non-trivial"
16422 " location lists.\n");
16423 *ret = NULL;
16424 return;
16427 /* LOC is constant expression. Try a luck, look it up in constant
16428 pool and return its loc_descr of its address. */
16430 static dw_loc_descr_ref
16431 cst_pool_loc_descr (tree loc)
16433 /* Get an RTL for this, if something has been emitted. */
16434 rtx rtl = lookup_constant_def (loc);
16436 if (!rtl || !MEM_P (rtl))
16438 gcc_assert (!rtl);
16439 return 0;
16441 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
16443 /* TODO: We might get more coverage if we was actually delaying expansion
16444 of all expressions till end of compilation when constant pools are fully
16445 populated. */
16446 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
16448 expansion_failed (loc, NULL_RTX,
16449 "CST value in contant pool but not marked.");
16450 return 0;
16452 return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
16453 GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
16456 /* Return dw_loc_list representing address of addr_expr LOC
16457 by looking for inner INDIRECT_REF expression and turning
16458 it into simple arithmetics.
16460 See loc_list_from_tree for the meaning of CONTEXT. */
16462 static dw_loc_list_ref
16463 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev,
16464 loc_descr_context *context)
16466 tree obj, offset;
16467 HOST_WIDE_INT bitsize, bitpos, bytepos;
16468 machine_mode mode;
16469 int unsignedp, reversep, volatilep = 0;
16470 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
16472 obj = get_inner_reference (TREE_OPERAND (loc, 0),
16473 &bitsize, &bitpos, &offset, &mode,
16474 &unsignedp, &reversep, &volatilep);
16475 STRIP_NOPS (obj);
16476 if (bitpos % BITS_PER_UNIT)
16478 expansion_failed (loc, NULL_RTX, "bitfield access");
16479 return 0;
16481 if (!INDIRECT_REF_P (obj))
16483 expansion_failed (obj,
16484 NULL_RTX, "no indirect ref in inner refrence");
16485 return 0;
16487 if (!offset && !bitpos)
16488 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1,
16489 context);
16490 else if (toplev
16491 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
16492 && (dwarf_version >= 4 || !dwarf_strict))
16494 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0, context);
16495 if (!list_ret)
16496 return 0;
16497 if (offset)
16499 /* Variable offset. */
16500 list_ret1 = loc_list_from_tree (offset, 0, context);
16501 if (list_ret1 == 0)
16502 return 0;
16503 add_loc_list (&list_ret, list_ret1);
16504 if (!list_ret)
16505 return 0;
16506 add_loc_descr_to_each (list_ret,
16507 new_loc_descr (DW_OP_plus, 0, 0));
16509 bytepos = bitpos / BITS_PER_UNIT;
16510 if (bytepos > 0)
16511 add_loc_descr_to_each (list_ret,
16512 new_loc_descr (DW_OP_plus_uconst,
16513 bytepos, 0));
16514 else if (bytepos < 0)
16515 loc_list_plus_const (list_ret, bytepos);
16516 add_loc_descr_to_each (list_ret,
16517 new_loc_descr (DW_OP_stack_value, 0, 0));
16519 return list_ret;
16522 /* Set LOC to the next operation that is not a DW_OP_nop operation. In the case
16523 all operations from LOC are nops, move to the last one. Insert in NOPS all
16524 operations that are skipped. */
16526 static void
16527 loc_descr_to_next_no_nop (dw_loc_descr_ref &loc,
16528 hash_set<dw_loc_descr_ref> &nops)
16530 while (loc->dw_loc_next != NULL && loc->dw_loc_opc == DW_OP_nop)
16532 nops.add (loc);
16533 loc = loc->dw_loc_next;
16537 /* Helper for loc_descr_without_nops: free the location description operation
16538 P. */
16540 bool
16541 free_loc_descr (const dw_loc_descr_ref &loc, void *data ATTRIBUTE_UNUSED)
16543 ggc_free (loc);
16544 return true;
16547 /* Remove all DW_OP_nop operations from LOC except, if it exists, the one that
16548 finishes LOC. */
16550 static void
16551 loc_descr_without_nops (dw_loc_descr_ref &loc)
16553 if (loc->dw_loc_opc == DW_OP_nop && loc->dw_loc_next == NULL)
16554 return;
16556 /* Set of all DW_OP_nop operations we remove. */
16557 hash_set<dw_loc_descr_ref> nops;
16559 /* First, strip all prefix NOP operations in order to keep the head of the
16560 operations list. */
16561 loc_descr_to_next_no_nop (loc, nops);
16563 for (dw_loc_descr_ref cur = loc; cur != NULL;)
16565 /* For control flow operations: strip "prefix" nops in destination
16566 labels. */
16567 if (cur->dw_loc_oprnd1.val_class == dw_val_class_loc)
16568 loc_descr_to_next_no_nop (cur->dw_loc_oprnd1.v.val_loc, nops);
16569 if (cur->dw_loc_oprnd2.val_class == dw_val_class_loc)
16570 loc_descr_to_next_no_nop (cur->dw_loc_oprnd2.v.val_loc, nops);
16572 /* Do the same for the operations that follow, then move to the next
16573 iteration. */
16574 if (cur->dw_loc_next != NULL)
16575 loc_descr_to_next_no_nop (cur->dw_loc_next, nops);
16576 cur = cur->dw_loc_next;
16579 nops.traverse<void *, free_loc_descr> (NULL);
16583 struct dwarf_procedure_info;
16585 /* Helper structure for location descriptions generation. */
16586 struct loc_descr_context
16588 /* The type that is implicitly referenced by DW_OP_push_object_address, or
16589 NULL_TREE if DW_OP_push_object_address in invalid for this location
16590 description. This is used when processing PLACEHOLDER_EXPR nodes. */
16591 tree context_type;
16592 /* The ..._DECL node that should be translated as a
16593 DW_OP_push_object_address operation. */
16594 tree base_decl;
16595 /* Information about the DWARF procedure we are currently generating. NULL if
16596 we are not generating a DWARF procedure. */
16597 struct dwarf_procedure_info *dpi;
16598 /* True if integral PLACEHOLDER_EXPR stands for the first argument passed
16599 by consumer. Used for DW_TAG_generic_subrange attributes. */
16600 bool placeholder_arg;
16601 /* True if PLACEHOLDER_EXPR has been seen. */
16602 bool placeholder_seen;
16605 /* DWARF procedures generation
16607 DWARF expressions (aka. location descriptions) are used to encode variable
16608 things such as sizes or offsets. Such computations can have redundant parts
16609 that can be factorized in order to reduce the size of the output debug
16610 information. This is the whole point of DWARF procedures.
16612 Thanks to stor-layout.c, size and offset expressions in GENERIC trees are
16613 already factorized into functions ("size functions") in order to handle very
16614 big and complex types. Such functions are quite simple: they have integral
16615 arguments, they return an integral result and their body contains only a
16616 return statement with arithmetic expressions. This is the only kind of
16617 function we are interested in translating into DWARF procedures, here.
16619 DWARF expressions and DWARF procedure are executed using a stack, so we have
16620 to define some calling convention for them to interact. Let's say that:
16622 - Before calling a DWARF procedure, DWARF expressions must push on the stack
16623 all arguments in reverse order (right-to-left) so that when the DWARF
16624 procedure execution starts, the first argument is the top of the stack.
16626 - Then, when returning, the DWARF procedure must have consumed all arguments
16627 on the stack, must have pushed the result and touched nothing else.
16629 - Each integral argument and the result are integral types can be hold in a
16630 single stack slot.
16632 - We call "frame offset" the number of stack slots that are "under DWARF
16633 procedure control": it includes the arguments slots, the temporaries and
16634 the result slot. Thus, it is equal to the number of arguments when the
16635 procedure execution starts and must be equal to one (the result) when it
16636 returns. */
16638 /* Helper structure used when generating operations for a DWARF procedure. */
16639 struct dwarf_procedure_info
16641 /* The FUNCTION_DECL node corresponding to the DWARF procedure that is
16642 currently translated. */
16643 tree fndecl;
16644 /* The number of arguments FNDECL takes. */
16645 unsigned args_count;
16648 /* Return a pointer to a newly created DIE node for a DWARF procedure. Add
16649 LOCATION as its DW_AT_location attribute. If FNDECL is not NULL_TREE,
16650 equate it to this DIE. */
16652 static dw_die_ref
16653 new_dwarf_proc_die (dw_loc_descr_ref location, tree fndecl,
16654 dw_die_ref parent_die)
16656 dw_die_ref dwarf_proc_die;
16658 if ((dwarf_version < 3 && dwarf_strict)
16659 || location == NULL)
16660 return NULL;
16662 dwarf_proc_die = new_die (DW_TAG_dwarf_procedure, parent_die, fndecl);
16663 if (fndecl)
16664 equate_decl_number_to_die (fndecl, dwarf_proc_die);
16665 add_AT_loc (dwarf_proc_die, DW_AT_location, location);
16666 return dwarf_proc_die;
16669 /* Return whether TYPE is a supported type as a DWARF procedure argument
16670 type or return type (we handle only scalar types and pointer types that
16671 aren't wider than the DWARF expression evaluation stack. */
16673 static bool
16674 is_handled_procedure_type (tree type)
16676 return ((INTEGRAL_TYPE_P (type)
16677 || TREE_CODE (type) == OFFSET_TYPE
16678 || TREE_CODE (type) == POINTER_TYPE)
16679 && int_size_in_bytes (type) <= DWARF2_ADDR_SIZE);
16682 /* Helper for resolve_args_picking: do the same but stop when coming across
16683 visited nodes. For each node we visit, register in FRAME_OFFSETS the frame
16684 offset *before* evaluating the corresponding operation. */
16686 static bool
16687 resolve_args_picking_1 (dw_loc_descr_ref loc, unsigned initial_frame_offset,
16688 struct dwarf_procedure_info *dpi,
16689 hash_map<dw_loc_descr_ref, unsigned> &frame_offsets)
16691 /* The "frame_offset" identifier is already used to name a macro... */
16692 unsigned frame_offset_ = initial_frame_offset;
16693 dw_loc_descr_ref l;
16695 for (l = loc; l != NULL;)
16697 bool existed;
16698 unsigned &l_frame_offset = frame_offsets.get_or_insert (l, &existed);
16700 /* If we already met this node, there is nothing to compute anymore. */
16701 if (existed)
16703 /* Make sure that the stack size is consistent wherever the execution
16704 flow comes from. */
16705 gcc_assert ((unsigned) l_frame_offset == frame_offset_);
16706 break;
16708 l_frame_offset = frame_offset_;
16710 /* If needed, relocate the picking offset with respect to the frame
16711 offset. */
16712 if (l->frame_offset_rel)
16714 unsigned HOST_WIDE_INT off;
16715 switch (l->dw_loc_opc)
16717 case DW_OP_pick:
16718 off = l->dw_loc_oprnd1.v.val_unsigned;
16719 break;
16720 case DW_OP_dup:
16721 off = 0;
16722 break;
16723 case DW_OP_over:
16724 off = 1;
16725 break;
16726 default:
16727 gcc_unreachable ();
16729 /* frame_offset_ is the size of the current stack frame, including
16730 incoming arguments. Besides, the arguments are pushed
16731 right-to-left. Thus, in order to access the Nth argument from
16732 this operation node, the picking has to skip temporaries *plus*
16733 one stack slot per argument (0 for the first one, 1 for the second
16734 one, etc.).
16736 The targetted argument number (N) is already set as the operand,
16737 and the number of temporaries can be computed with:
16738 frame_offsets_ - dpi->args_count */
16739 off += frame_offset_ - dpi->args_count;
16741 /* DW_OP_pick handles only offsets from 0 to 255 (inclusive)... */
16742 if (off > 255)
16743 return false;
16745 if (off == 0)
16747 l->dw_loc_opc = DW_OP_dup;
16748 l->dw_loc_oprnd1.v.val_unsigned = 0;
16750 else if (off == 1)
16752 l->dw_loc_opc = DW_OP_over;
16753 l->dw_loc_oprnd1.v.val_unsigned = 0;
16755 else
16757 l->dw_loc_opc = DW_OP_pick;
16758 l->dw_loc_oprnd1.v.val_unsigned = off;
16762 /* Update frame_offset according to the effect the current operation has
16763 on the stack. */
16764 switch (l->dw_loc_opc)
16766 case DW_OP_deref:
16767 case DW_OP_swap:
16768 case DW_OP_rot:
16769 case DW_OP_abs:
16770 case DW_OP_neg:
16771 case DW_OP_not:
16772 case DW_OP_plus_uconst:
16773 case DW_OP_skip:
16774 case DW_OP_reg0:
16775 case DW_OP_reg1:
16776 case DW_OP_reg2:
16777 case DW_OP_reg3:
16778 case DW_OP_reg4:
16779 case DW_OP_reg5:
16780 case DW_OP_reg6:
16781 case DW_OP_reg7:
16782 case DW_OP_reg8:
16783 case DW_OP_reg9:
16784 case DW_OP_reg10:
16785 case DW_OP_reg11:
16786 case DW_OP_reg12:
16787 case DW_OP_reg13:
16788 case DW_OP_reg14:
16789 case DW_OP_reg15:
16790 case DW_OP_reg16:
16791 case DW_OP_reg17:
16792 case DW_OP_reg18:
16793 case DW_OP_reg19:
16794 case DW_OP_reg20:
16795 case DW_OP_reg21:
16796 case DW_OP_reg22:
16797 case DW_OP_reg23:
16798 case DW_OP_reg24:
16799 case DW_OP_reg25:
16800 case DW_OP_reg26:
16801 case DW_OP_reg27:
16802 case DW_OP_reg28:
16803 case DW_OP_reg29:
16804 case DW_OP_reg30:
16805 case DW_OP_reg31:
16806 case DW_OP_bregx:
16807 case DW_OP_piece:
16808 case DW_OP_deref_size:
16809 case DW_OP_nop:
16810 case DW_OP_bit_piece:
16811 case DW_OP_implicit_value:
16812 case DW_OP_stack_value:
16813 break;
16815 case DW_OP_addr:
16816 case DW_OP_const1u:
16817 case DW_OP_const1s:
16818 case DW_OP_const2u:
16819 case DW_OP_const2s:
16820 case DW_OP_const4u:
16821 case DW_OP_const4s:
16822 case DW_OP_const8u:
16823 case DW_OP_const8s:
16824 case DW_OP_constu:
16825 case DW_OP_consts:
16826 case DW_OP_dup:
16827 case DW_OP_over:
16828 case DW_OP_pick:
16829 case DW_OP_lit0:
16830 case DW_OP_lit1:
16831 case DW_OP_lit2:
16832 case DW_OP_lit3:
16833 case DW_OP_lit4:
16834 case DW_OP_lit5:
16835 case DW_OP_lit6:
16836 case DW_OP_lit7:
16837 case DW_OP_lit8:
16838 case DW_OP_lit9:
16839 case DW_OP_lit10:
16840 case DW_OP_lit11:
16841 case DW_OP_lit12:
16842 case DW_OP_lit13:
16843 case DW_OP_lit14:
16844 case DW_OP_lit15:
16845 case DW_OP_lit16:
16846 case DW_OP_lit17:
16847 case DW_OP_lit18:
16848 case DW_OP_lit19:
16849 case DW_OP_lit20:
16850 case DW_OP_lit21:
16851 case DW_OP_lit22:
16852 case DW_OP_lit23:
16853 case DW_OP_lit24:
16854 case DW_OP_lit25:
16855 case DW_OP_lit26:
16856 case DW_OP_lit27:
16857 case DW_OP_lit28:
16858 case DW_OP_lit29:
16859 case DW_OP_lit30:
16860 case DW_OP_lit31:
16861 case DW_OP_breg0:
16862 case DW_OP_breg1:
16863 case DW_OP_breg2:
16864 case DW_OP_breg3:
16865 case DW_OP_breg4:
16866 case DW_OP_breg5:
16867 case DW_OP_breg6:
16868 case DW_OP_breg7:
16869 case DW_OP_breg8:
16870 case DW_OP_breg9:
16871 case DW_OP_breg10:
16872 case DW_OP_breg11:
16873 case DW_OP_breg12:
16874 case DW_OP_breg13:
16875 case DW_OP_breg14:
16876 case DW_OP_breg15:
16877 case DW_OP_breg16:
16878 case DW_OP_breg17:
16879 case DW_OP_breg18:
16880 case DW_OP_breg19:
16881 case DW_OP_breg20:
16882 case DW_OP_breg21:
16883 case DW_OP_breg22:
16884 case DW_OP_breg23:
16885 case DW_OP_breg24:
16886 case DW_OP_breg25:
16887 case DW_OP_breg26:
16888 case DW_OP_breg27:
16889 case DW_OP_breg28:
16890 case DW_OP_breg29:
16891 case DW_OP_breg30:
16892 case DW_OP_breg31:
16893 case DW_OP_fbreg:
16894 case DW_OP_push_object_address:
16895 case DW_OP_call_frame_cfa:
16896 case DW_OP_GNU_variable_value:
16897 ++frame_offset_;
16898 break;
16900 case DW_OP_drop:
16901 case DW_OP_xderef:
16902 case DW_OP_and:
16903 case DW_OP_div:
16904 case DW_OP_minus:
16905 case DW_OP_mod:
16906 case DW_OP_mul:
16907 case DW_OP_or:
16908 case DW_OP_plus:
16909 case DW_OP_shl:
16910 case DW_OP_shr:
16911 case DW_OP_shra:
16912 case DW_OP_xor:
16913 case DW_OP_bra:
16914 case DW_OP_eq:
16915 case DW_OP_ge:
16916 case DW_OP_gt:
16917 case DW_OP_le:
16918 case DW_OP_lt:
16919 case DW_OP_ne:
16920 case DW_OP_regx:
16921 case DW_OP_xderef_size:
16922 --frame_offset_;
16923 break;
16925 case DW_OP_call2:
16926 case DW_OP_call4:
16927 case DW_OP_call_ref:
16929 dw_die_ref dwarf_proc = l->dw_loc_oprnd1.v.val_die_ref.die;
16930 int *stack_usage = dwarf_proc_stack_usage_map->get (dwarf_proc);
16932 if (stack_usage == NULL)
16933 return false;
16934 frame_offset_ += *stack_usage;
16935 break;
16938 case DW_OP_implicit_pointer:
16939 case DW_OP_entry_value:
16940 case DW_OP_const_type:
16941 case DW_OP_regval_type:
16942 case DW_OP_deref_type:
16943 case DW_OP_convert:
16944 case DW_OP_reinterpret:
16945 case DW_OP_form_tls_address:
16946 case DW_OP_GNU_push_tls_address:
16947 case DW_OP_GNU_uninit:
16948 case DW_OP_GNU_encoded_addr:
16949 case DW_OP_GNU_implicit_pointer:
16950 case DW_OP_GNU_entry_value:
16951 case DW_OP_GNU_const_type:
16952 case DW_OP_GNU_regval_type:
16953 case DW_OP_GNU_deref_type:
16954 case DW_OP_GNU_convert:
16955 case DW_OP_GNU_reinterpret:
16956 case DW_OP_GNU_parameter_ref:
16957 /* loc_list_from_tree will probably not output these operations for
16958 size functions, so assume they will not appear here. */
16959 /* Fall through... */
16961 default:
16962 gcc_unreachable ();
16965 /* Now, follow the control flow (except subroutine calls). */
16966 switch (l->dw_loc_opc)
16968 case DW_OP_bra:
16969 if (!resolve_args_picking_1 (l->dw_loc_next, frame_offset_, dpi,
16970 frame_offsets))
16971 return false;
16972 /* Fall through. */
16974 case DW_OP_skip:
16975 l = l->dw_loc_oprnd1.v.val_loc;
16976 break;
16978 case DW_OP_stack_value:
16979 return true;
16981 default:
16982 l = l->dw_loc_next;
16983 break;
16987 return true;
16990 /* Make a DFS over operations reachable through LOC (i.e. follow branch
16991 operations) in order to resolve the operand of DW_OP_pick operations that
16992 target DWARF procedure arguments (DPI). INITIAL_FRAME_OFFSET is the frame
16993 offset *before* LOC is executed. Return if all relocations were
16994 successful. */
16996 static bool
16997 resolve_args_picking (dw_loc_descr_ref loc, unsigned initial_frame_offset,
16998 struct dwarf_procedure_info *dpi)
17000 /* Associate to all visited operations the frame offset *before* evaluating
17001 this operation. */
17002 hash_map<dw_loc_descr_ref, unsigned> frame_offsets;
17004 return resolve_args_picking_1 (loc, initial_frame_offset, dpi,
17005 frame_offsets);
17008 /* Try to generate a DWARF procedure that computes the same result as FNDECL.
17009 Return NULL if it is not possible. */
17011 static dw_die_ref
17012 function_to_dwarf_procedure (tree fndecl)
17014 struct loc_descr_context ctx;
17015 struct dwarf_procedure_info dpi;
17016 dw_die_ref dwarf_proc_die;
17017 tree tree_body = DECL_SAVED_TREE (fndecl);
17018 dw_loc_descr_ref loc_body, epilogue;
17020 tree cursor;
17021 unsigned i;
17023 /* Do not generate multiple DWARF procedures for the same function
17024 declaration. */
17025 dwarf_proc_die = lookup_decl_die (fndecl);
17026 if (dwarf_proc_die != NULL)
17027 return dwarf_proc_die;
17029 /* DWARF procedures are available starting with the DWARFv3 standard. */
17030 if (dwarf_version < 3 && dwarf_strict)
17031 return NULL;
17033 /* We handle only functions for which we still have a body, that return a
17034 supported type and that takes arguments with supported types. Note that
17035 there is no point translating functions that return nothing. */
17036 if (tree_body == NULL_TREE
17037 || DECL_RESULT (fndecl) == NULL_TREE
17038 || !is_handled_procedure_type (TREE_TYPE (DECL_RESULT (fndecl))))
17039 return NULL;
17041 for (cursor = DECL_ARGUMENTS (fndecl);
17042 cursor != NULL_TREE;
17043 cursor = TREE_CHAIN (cursor))
17044 if (!is_handled_procedure_type (TREE_TYPE (cursor)))
17045 return NULL;
17047 /* Match only "expr" in: RETURN_EXPR (MODIFY_EXPR (RESULT_DECL, expr)). */
17048 if (TREE_CODE (tree_body) != RETURN_EXPR)
17049 return NULL;
17050 tree_body = TREE_OPERAND (tree_body, 0);
17051 if (TREE_CODE (tree_body) != MODIFY_EXPR
17052 || TREE_OPERAND (tree_body, 0) != DECL_RESULT (fndecl))
17053 return NULL;
17054 tree_body = TREE_OPERAND (tree_body, 1);
17056 /* Try to translate the body expression itself. Note that this will probably
17057 cause an infinite recursion if its call graph has a cycle. This is very
17058 unlikely for size functions, however, so don't bother with such things at
17059 the moment. */
17060 ctx.context_type = NULL_TREE;
17061 ctx.base_decl = NULL_TREE;
17062 ctx.dpi = &dpi;
17063 ctx.placeholder_arg = false;
17064 ctx.placeholder_seen = false;
17065 dpi.fndecl = fndecl;
17066 dpi.args_count = list_length (DECL_ARGUMENTS (fndecl));
17067 loc_body = loc_descriptor_from_tree (tree_body, 0, &ctx);
17068 if (!loc_body)
17069 return NULL;
17071 /* After evaluating all operands in "loc_body", we should still have on the
17072 stack all arguments plus the desired function result (top of the stack).
17073 Generate code in order to keep only the result in our stack frame. */
17074 epilogue = NULL;
17075 for (i = 0; i < dpi.args_count; ++i)
17077 dw_loc_descr_ref op_couple = new_loc_descr (DW_OP_swap, 0, 0);
17078 op_couple->dw_loc_next = new_loc_descr (DW_OP_drop, 0, 0);
17079 op_couple->dw_loc_next->dw_loc_next = epilogue;
17080 epilogue = op_couple;
17082 add_loc_descr (&loc_body, epilogue);
17083 if (!resolve_args_picking (loc_body, dpi.args_count, &dpi))
17084 return NULL;
17086 /* Trailing nops from loc_descriptor_from_tree (if any) cannot be removed
17087 because they are considered useful. Now there is an epilogue, they are
17088 not anymore, so give it another try. */
17089 loc_descr_without_nops (loc_body);
17091 /* fndecl may be used both as a regular DW_TAG_subprogram DIE and as
17092 a DW_TAG_dwarf_procedure, so we may have a conflict, here. It's unlikely,
17093 though, given that size functions do not come from source, so they should
17094 not have a dedicated DW_TAG_subprogram DIE. */
17095 dwarf_proc_die
17096 = new_dwarf_proc_die (loc_body, fndecl,
17097 get_context_die (DECL_CONTEXT (fndecl)));
17099 /* The called DWARF procedure consumes one stack slot per argument and
17100 returns one stack slot. */
17101 dwarf_proc_stack_usage_map->put (dwarf_proc_die, 1 - dpi.args_count);
17103 return dwarf_proc_die;
17107 /* Generate Dwarf location list representing LOC.
17108 If WANT_ADDRESS is false, expression computing LOC will be computed
17109 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
17110 if WANT_ADDRESS is 2, expression computing address useable in location
17111 will be returned (i.e. DW_OP_reg can be used
17112 to refer to register values).
17114 CONTEXT provides information to customize the location descriptions
17115 generation. Its context_type field specifies what type is implicitly
17116 referenced by DW_OP_push_object_address. If it is NULL_TREE, this operation
17117 will not be generated.
17119 Its DPI field determines whether we are generating a DWARF expression for a
17120 DWARF procedure, so PARM_DECL references are processed specifically.
17122 If CONTEXT is NULL, the behavior is the same as if context_type, base_decl
17123 and dpi fields were null. */
17125 static dw_loc_list_ref
17126 loc_list_from_tree_1 (tree loc, int want_address,
17127 struct loc_descr_context *context)
17129 dw_loc_descr_ref ret = NULL, ret1 = NULL;
17130 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
17131 int have_address = 0;
17132 enum dwarf_location_atom op;
17134 /* ??? Most of the time we do not take proper care for sign/zero
17135 extending the values properly. Hopefully this won't be a real
17136 problem... */
17138 if (context != NULL
17139 && context->base_decl == loc
17140 && want_address == 0)
17142 if (dwarf_version >= 3 || !dwarf_strict)
17143 return new_loc_list (new_loc_descr (DW_OP_push_object_address, 0, 0),
17144 NULL, NULL, NULL);
17145 else
17146 return NULL;
17149 switch (TREE_CODE (loc))
17151 case ERROR_MARK:
17152 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
17153 return 0;
17155 case PLACEHOLDER_EXPR:
17156 /* This case involves extracting fields from an object to determine the
17157 position of other fields. It is supposed to appear only as the first
17158 operand of COMPONENT_REF nodes and to reference precisely the type
17159 that the context allows. */
17160 if (context != NULL
17161 && TREE_TYPE (loc) == context->context_type
17162 && want_address >= 1)
17164 if (dwarf_version >= 3 || !dwarf_strict)
17166 ret = new_loc_descr (DW_OP_push_object_address, 0, 0);
17167 have_address = 1;
17168 break;
17170 else
17171 return NULL;
17173 /* For DW_TAG_generic_subrange attributes, PLACEHOLDER_EXPR stands for
17174 the single argument passed by consumer. */
17175 else if (context != NULL
17176 && context->placeholder_arg
17177 && INTEGRAL_TYPE_P (TREE_TYPE (loc))
17178 && want_address == 0)
17180 ret = new_loc_descr (DW_OP_pick, 0, 0);
17181 ret->frame_offset_rel = 1;
17182 context->placeholder_seen = true;
17183 break;
17185 else
17186 expansion_failed (loc, NULL_RTX,
17187 "PLACEHOLDER_EXPR for an unexpected type");
17188 break;
17190 case CALL_EXPR:
17192 const int nargs = call_expr_nargs (loc);
17193 tree callee = get_callee_fndecl (loc);
17194 int i;
17195 dw_die_ref dwarf_proc;
17197 if (callee == NULL_TREE)
17198 goto call_expansion_failed;
17200 /* We handle only functions that return an integer. */
17201 if (!is_handled_procedure_type (TREE_TYPE (TREE_TYPE (callee))))
17202 goto call_expansion_failed;
17204 dwarf_proc = function_to_dwarf_procedure (callee);
17205 if (dwarf_proc == NULL)
17206 goto call_expansion_failed;
17208 /* Evaluate arguments right-to-left so that the first argument will
17209 be the top-most one on the stack. */
17210 for (i = nargs - 1; i >= 0; --i)
17212 dw_loc_descr_ref loc_descr
17213 = loc_descriptor_from_tree (CALL_EXPR_ARG (loc, i), 0,
17214 context);
17216 if (loc_descr == NULL)
17217 goto call_expansion_failed;
17219 add_loc_descr (&ret, loc_descr);
17222 ret1 = new_loc_descr (DW_OP_call4, 0, 0);
17223 ret1->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
17224 ret1->dw_loc_oprnd1.v.val_die_ref.die = dwarf_proc;
17225 ret1->dw_loc_oprnd1.v.val_die_ref.external = 0;
17226 add_loc_descr (&ret, ret1);
17227 break;
17229 call_expansion_failed:
17230 expansion_failed (loc, NULL_RTX, "CALL_EXPR");
17231 /* There are no opcodes for these operations. */
17232 return 0;
17235 case PREINCREMENT_EXPR:
17236 case PREDECREMENT_EXPR:
17237 case POSTINCREMENT_EXPR:
17238 case POSTDECREMENT_EXPR:
17239 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
17240 /* There are no opcodes for these operations. */
17241 return 0;
17243 case ADDR_EXPR:
17244 /* If we already want an address, see if there is INDIRECT_REF inside
17245 e.g. for &this->field. */
17246 if (want_address)
17248 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
17249 (loc, want_address == 2, context);
17250 if (list_ret)
17251 have_address = 1;
17252 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
17253 && (ret = cst_pool_loc_descr (loc)))
17254 have_address = 1;
17256 /* Otherwise, process the argument and look for the address. */
17257 if (!list_ret && !ret)
17258 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 1, context);
17259 else
17261 if (want_address)
17262 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
17263 return NULL;
17265 break;
17267 case VAR_DECL:
17268 if (DECL_THREAD_LOCAL_P (loc))
17270 rtx rtl;
17271 enum dwarf_location_atom tls_op;
17272 enum dtprel_bool dtprel = dtprel_false;
17274 if (targetm.have_tls)
17276 /* If this is not defined, we have no way to emit the
17277 data. */
17278 if (!targetm.asm_out.output_dwarf_dtprel)
17279 return 0;
17281 /* The way DW_OP_GNU_push_tls_address is specified, we
17282 can only look up addresses of objects in the current
17283 module. We used DW_OP_addr as first op, but that's
17284 wrong, because DW_OP_addr is relocated by the debug
17285 info consumer, while DW_OP_GNU_push_tls_address
17286 operand shouldn't be. */
17287 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
17288 return 0;
17289 dtprel = dtprel_true;
17290 /* We check for DWARF 5 here because gdb did not implement
17291 DW_OP_form_tls_address until after 7.12. */
17292 tls_op = (dwarf_version >= 5 ? DW_OP_form_tls_address
17293 : DW_OP_GNU_push_tls_address);
17295 else
17297 if (!targetm.emutls.debug_form_tls_address
17298 || !(dwarf_version >= 3 || !dwarf_strict))
17299 return 0;
17300 /* We stuffed the control variable into the DECL_VALUE_EXPR
17301 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
17302 no longer appear in gimple code. We used the control
17303 variable in specific so that we could pick it up here. */
17304 loc = DECL_VALUE_EXPR (loc);
17305 tls_op = DW_OP_form_tls_address;
17308 rtl = rtl_for_decl_location (loc);
17309 if (rtl == NULL_RTX)
17310 return 0;
17312 if (!MEM_P (rtl))
17313 return 0;
17314 rtl = XEXP (rtl, 0);
17315 if (! CONSTANT_P (rtl))
17316 return 0;
17318 ret = new_addr_loc_descr (rtl, dtprel);
17319 ret1 = new_loc_descr (tls_op, 0, 0);
17320 add_loc_descr (&ret, ret1);
17322 have_address = 1;
17323 break;
17325 /* FALLTHRU */
17327 case PARM_DECL:
17328 if (context != NULL && context->dpi != NULL
17329 && DECL_CONTEXT (loc) == context->dpi->fndecl)
17331 /* We are generating code for a DWARF procedure and we want to access
17332 one of its arguments: find the appropriate argument offset and let
17333 the resolve_args_picking pass compute the offset that complies
17334 with the stack frame size. */
17335 unsigned i = 0;
17336 tree cursor;
17338 for (cursor = DECL_ARGUMENTS (context->dpi->fndecl);
17339 cursor != NULL_TREE && cursor != loc;
17340 cursor = TREE_CHAIN (cursor), ++i)
17342 /* If we are translating a DWARF procedure, all referenced parameters
17343 must belong to the current function. */
17344 gcc_assert (cursor != NULL_TREE);
17346 ret = new_loc_descr (DW_OP_pick, i, 0);
17347 ret->frame_offset_rel = 1;
17348 break;
17350 /* FALLTHRU */
17352 case RESULT_DECL:
17353 if (DECL_HAS_VALUE_EXPR_P (loc))
17354 return loc_list_from_tree_1 (DECL_VALUE_EXPR (loc),
17355 want_address, context);
17356 /* FALLTHRU */
17358 case FUNCTION_DECL:
17360 rtx rtl;
17361 var_loc_list *loc_list = lookup_decl_loc (loc);
17363 if (loc_list && loc_list->first)
17365 list_ret = dw_loc_list (loc_list, loc, want_address);
17366 have_address = want_address != 0;
17367 break;
17369 rtl = rtl_for_decl_location (loc);
17370 if (rtl == NULL_RTX)
17372 if (TREE_CODE (loc) != FUNCTION_DECL
17373 && early_dwarf
17374 && current_function_decl
17375 && want_address != 1
17376 && ! DECL_IGNORED_P (loc)
17377 && (INTEGRAL_TYPE_P (TREE_TYPE (loc))
17378 || POINTER_TYPE_P (TREE_TYPE (loc)))
17379 && DECL_CONTEXT (loc) == current_function_decl
17380 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (loc)))
17381 <= DWARF2_ADDR_SIZE))
17383 dw_die_ref ref = lookup_decl_die (loc);
17384 ret = new_loc_descr (DW_OP_GNU_variable_value, 0, 0);
17385 if (ref)
17387 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
17388 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
17389 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
17391 else
17393 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
17394 ret->dw_loc_oprnd1.v.val_decl_ref = loc;
17396 break;
17398 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
17399 return 0;
17401 else if (CONST_INT_P (rtl))
17403 HOST_WIDE_INT val = INTVAL (rtl);
17404 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
17405 val &= GET_MODE_MASK (DECL_MODE (loc));
17406 ret = int_loc_descriptor (val);
17408 else if (GET_CODE (rtl) == CONST_STRING)
17410 expansion_failed (loc, NULL_RTX, "CONST_STRING");
17411 return 0;
17413 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
17414 ret = new_addr_loc_descr (rtl, dtprel_false);
17415 else
17417 machine_mode mode, mem_mode;
17419 /* Certain constructs can only be represented at top-level. */
17420 if (want_address == 2)
17422 ret = loc_descriptor (rtl, VOIDmode,
17423 VAR_INIT_STATUS_INITIALIZED);
17424 have_address = 1;
17426 else
17428 mode = GET_MODE (rtl);
17429 mem_mode = VOIDmode;
17430 if (MEM_P (rtl))
17432 mem_mode = mode;
17433 mode = get_address_mode (rtl);
17434 rtl = XEXP (rtl, 0);
17435 have_address = 1;
17437 ret = mem_loc_descriptor (rtl, mode, mem_mode,
17438 VAR_INIT_STATUS_INITIALIZED);
17440 if (!ret)
17441 expansion_failed (loc, rtl,
17442 "failed to produce loc descriptor for rtl");
17445 break;
17447 case MEM_REF:
17448 if (!integer_zerop (TREE_OPERAND (loc, 1)))
17450 have_address = 1;
17451 goto do_plus;
17453 /* Fallthru. */
17454 case INDIRECT_REF:
17455 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17456 have_address = 1;
17457 break;
17459 case TARGET_MEM_REF:
17460 case SSA_NAME:
17461 case DEBUG_EXPR_DECL:
17462 return NULL;
17464 case COMPOUND_EXPR:
17465 return loc_list_from_tree_1 (TREE_OPERAND (loc, 1), want_address,
17466 context);
17468 CASE_CONVERT:
17469 case VIEW_CONVERT_EXPR:
17470 case SAVE_EXPR:
17471 case MODIFY_EXPR:
17472 case NON_LVALUE_EXPR:
17473 return loc_list_from_tree_1 (TREE_OPERAND (loc, 0), want_address,
17474 context);
17476 case COMPONENT_REF:
17477 case BIT_FIELD_REF:
17478 case ARRAY_REF:
17479 case ARRAY_RANGE_REF:
17480 case REALPART_EXPR:
17481 case IMAGPART_EXPR:
17483 tree obj, offset;
17484 HOST_WIDE_INT bitsize, bitpos, bytepos;
17485 machine_mode mode;
17486 int unsignedp, reversep, volatilep = 0;
17488 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
17489 &unsignedp, &reversep, &volatilep);
17491 gcc_assert (obj != loc);
17493 list_ret = loc_list_from_tree_1 (obj,
17494 want_address == 2
17495 && !bitpos && !offset ? 2 : 1,
17496 context);
17497 /* TODO: We can extract value of the small expression via shifting even
17498 for nonzero bitpos. */
17499 if (list_ret == 0)
17500 return 0;
17501 if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
17503 expansion_failed (loc, NULL_RTX,
17504 "bitfield access");
17505 return 0;
17508 if (offset != NULL_TREE)
17510 /* Variable offset. */
17511 list_ret1 = loc_list_from_tree_1 (offset, 0, context);
17512 if (list_ret1 == 0)
17513 return 0;
17514 add_loc_list (&list_ret, list_ret1);
17515 if (!list_ret)
17516 return 0;
17517 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
17520 bytepos = bitpos / BITS_PER_UNIT;
17521 if (bytepos > 0)
17522 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
17523 else if (bytepos < 0)
17524 loc_list_plus_const (list_ret, bytepos);
17526 have_address = 1;
17527 break;
17530 case INTEGER_CST:
17531 if ((want_address || !tree_fits_shwi_p (loc))
17532 && (ret = cst_pool_loc_descr (loc)))
17533 have_address = 1;
17534 else if (want_address == 2
17535 && tree_fits_shwi_p (loc)
17536 && (ret = address_of_int_loc_descriptor
17537 (int_size_in_bytes (TREE_TYPE (loc)),
17538 tree_to_shwi (loc))))
17539 have_address = 1;
17540 else if (tree_fits_shwi_p (loc))
17541 ret = int_loc_descriptor (tree_to_shwi (loc));
17542 else if (tree_fits_uhwi_p (loc))
17543 ret = uint_loc_descriptor (tree_to_uhwi (loc));
17544 else
17546 expansion_failed (loc, NULL_RTX,
17547 "Integer operand is not host integer");
17548 return 0;
17550 break;
17552 case CONSTRUCTOR:
17553 case REAL_CST:
17554 case STRING_CST:
17555 case COMPLEX_CST:
17556 if ((ret = cst_pool_loc_descr (loc)))
17557 have_address = 1;
17558 else if (TREE_CODE (loc) == CONSTRUCTOR)
17560 tree type = TREE_TYPE (loc);
17561 unsigned HOST_WIDE_INT size = int_size_in_bytes (type);
17562 unsigned HOST_WIDE_INT offset = 0;
17563 unsigned HOST_WIDE_INT cnt;
17564 constructor_elt *ce;
17566 if (TREE_CODE (type) == RECORD_TYPE)
17568 /* This is very limited, but it's enough to output
17569 pointers to member functions, as long as the
17570 referenced function is defined in the current
17571 translation unit. */
17572 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (loc), cnt, ce)
17574 tree val = ce->value;
17576 tree field = ce->index;
17578 if (val)
17579 STRIP_NOPS (val);
17581 if (!field || DECL_BIT_FIELD (field))
17583 expansion_failed (loc, NULL_RTX,
17584 "bitfield in record type constructor");
17585 size = offset = (unsigned HOST_WIDE_INT)-1;
17586 ret = NULL;
17587 break;
17590 HOST_WIDE_INT fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
17591 unsigned HOST_WIDE_INT pos = int_byte_position (field);
17592 gcc_assert (pos + fieldsize <= size);
17593 if (pos < offset)
17595 expansion_failed (loc, NULL_RTX,
17596 "out-of-order fields in record constructor");
17597 size = offset = (unsigned HOST_WIDE_INT)-1;
17598 ret = NULL;
17599 break;
17601 if (pos > offset)
17603 ret1 = new_loc_descr (DW_OP_piece, pos - offset, 0);
17604 add_loc_descr (&ret, ret1);
17605 offset = pos;
17607 if (val && fieldsize != 0)
17609 ret1 = loc_descriptor_from_tree (val, want_address, context);
17610 if (!ret1)
17612 expansion_failed (loc, NULL_RTX,
17613 "unsupported expression in field");
17614 size = offset = (unsigned HOST_WIDE_INT)-1;
17615 ret = NULL;
17616 break;
17618 add_loc_descr (&ret, ret1);
17620 if (fieldsize)
17622 ret1 = new_loc_descr (DW_OP_piece, fieldsize, 0);
17623 add_loc_descr (&ret, ret1);
17624 offset = pos + fieldsize;
17628 if (offset != size)
17630 ret1 = new_loc_descr (DW_OP_piece, size - offset, 0);
17631 add_loc_descr (&ret, ret1);
17632 offset = size;
17635 have_address = !!want_address;
17637 else
17638 expansion_failed (loc, NULL_RTX,
17639 "constructor of non-record type");
17641 else
17642 /* We can construct small constants here using int_loc_descriptor. */
17643 expansion_failed (loc, NULL_RTX,
17644 "constructor or constant not in constant pool");
17645 break;
17647 case TRUTH_AND_EXPR:
17648 case TRUTH_ANDIF_EXPR:
17649 case BIT_AND_EXPR:
17650 op = DW_OP_and;
17651 goto do_binop;
17653 case TRUTH_XOR_EXPR:
17654 case BIT_XOR_EXPR:
17655 op = DW_OP_xor;
17656 goto do_binop;
17658 case TRUTH_OR_EXPR:
17659 case TRUTH_ORIF_EXPR:
17660 case BIT_IOR_EXPR:
17661 op = DW_OP_or;
17662 goto do_binop;
17664 case FLOOR_DIV_EXPR:
17665 case CEIL_DIV_EXPR:
17666 case ROUND_DIV_EXPR:
17667 case TRUNC_DIV_EXPR:
17668 case EXACT_DIV_EXPR:
17669 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
17670 return 0;
17671 op = DW_OP_div;
17672 goto do_binop;
17674 case MINUS_EXPR:
17675 op = DW_OP_minus;
17676 goto do_binop;
17678 case FLOOR_MOD_EXPR:
17679 case CEIL_MOD_EXPR:
17680 case ROUND_MOD_EXPR:
17681 case TRUNC_MOD_EXPR:
17682 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
17684 op = DW_OP_mod;
17685 goto do_binop;
17687 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17688 list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
17689 if (list_ret == 0 || list_ret1 == 0)
17690 return 0;
17692 add_loc_list (&list_ret, list_ret1);
17693 if (list_ret == 0)
17694 return 0;
17695 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
17696 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
17697 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
17698 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
17699 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
17700 break;
17702 case MULT_EXPR:
17703 op = DW_OP_mul;
17704 goto do_binop;
17706 case LSHIFT_EXPR:
17707 op = DW_OP_shl;
17708 goto do_binop;
17710 case RSHIFT_EXPR:
17711 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
17712 goto do_binop;
17714 case POINTER_PLUS_EXPR:
17715 case PLUS_EXPR:
17716 do_plus:
17717 if (tree_fits_shwi_p (TREE_OPERAND (loc, 1)))
17719 /* Big unsigned numbers can fit in HOST_WIDE_INT but it may be
17720 smarter to encode their opposite. The DW_OP_plus_uconst operation
17721 takes 1 + X bytes, X being the size of the ULEB128 addend. On the
17722 other hand, a "<push literal>; DW_OP_minus" pattern takes 1 + Y
17723 bytes, Y being the size of the operation that pushes the opposite
17724 of the addend. So let's choose the smallest representation. */
17725 const tree tree_addend = TREE_OPERAND (loc, 1);
17726 offset_int wi_addend;
17727 HOST_WIDE_INT shwi_addend;
17728 dw_loc_descr_ref loc_naddend;
17730 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17731 if (list_ret == 0)
17732 return 0;
17734 /* Try to get the literal to push. It is the opposite of the addend,
17735 so as we rely on wrapping during DWARF evaluation, first decode
17736 the literal as a "DWARF-sized" signed number. */
17737 wi_addend = wi::to_offset (tree_addend);
17738 wi_addend = wi::sext (wi_addend, DWARF2_ADDR_SIZE * 8);
17739 shwi_addend = wi_addend.to_shwi ();
17740 loc_naddend = (shwi_addend != INTTYPE_MINIMUM (HOST_WIDE_INT))
17741 ? int_loc_descriptor (-shwi_addend)
17742 : NULL;
17744 if (loc_naddend != NULL
17745 && ((unsigned) size_of_uleb128 (shwi_addend)
17746 > size_of_loc_descr (loc_naddend)))
17748 add_loc_descr_to_each (list_ret, loc_naddend);
17749 add_loc_descr_to_each (list_ret,
17750 new_loc_descr (DW_OP_minus, 0, 0));
17752 else
17754 for (dw_loc_descr_ref loc_cur = loc_naddend; loc_cur != NULL; )
17756 loc_naddend = loc_cur;
17757 loc_cur = loc_cur->dw_loc_next;
17758 ggc_free (loc_naddend);
17760 loc_list_plus_const (list_ret, wi_addend.to_shwi ());
17762 break;
17765 op = DW_OP_plus;
17766 goto do_binop;
17768 case LE_EXPR:
17769 op = DW_OP_le;
17770 goto do_comp_binop;
17772 case GE_EXPR:
17773 op = DW_OP_ge;
17774 goto do_comp_binop;
17776 case LT_EXPR:
17777 op = DW_OP_lt;
17778 goto do_comp_binop;
17780 case GT_EXPR:
17781 op = DW_OP_gt;
17782 goto do_comp_binop;
17784 do_comp_binop:
17785 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
17787 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
17788 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
17789 list_ret = loc_list_from_uint_comparison (list_ret, list_ret1,
17790 TREE_CODE (loc));
17791 break;
17793 else
17794 goto do_binop;
17796 case EQ_EXPR:
17797 op = DW_OP_eq;
17798 goto do_binop;
17800 case NE_EXPR:
17801 op = DW_OP_ne;
17802 goto do_binop;
17804 do_binop:
17805 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17806 list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
17807 if (list_ret == 0 || list_ret1 == 0)
17808 return 0;
17810 add_loc_list (&list_ret, list_ret1);
17811 if (list_ret == 0)
17812 return 0;
17813 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
17814 break;
17816 case TRUTH_NOT_EXPR:
17817 case BIT_NOT_EXPR:
17818 op = DW_OP_not;
17819 goto do_unop;
17821 case ABS_EXPR:
17822 op = DW_OP_abs;
17823 goto do_unop;
17825 case NEGATE_EXPR:
17826 op = DW_OP_neg;
17827 goto do_unop;
17829 do_unop:
17830 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17831 if (list_ret == 0)
17832 return 0;
17834 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
17835 break;
17837 case MIN_EXPR:
17838 case MAX_EXPR:
17840 const enum tree_code code =
17841 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
17843 loc = build3 (COND_EXPR, TREE_TYPE (loc),
17844 build2 (code, integer_type_node,
17845 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
17846 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
17849 /* fall through */
17851 case COND_EXPR:
17853 dw_loc_descr_ref lhs
17854 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0, context);
17855 dw_loc_list_ref rhs
17856 = loc_list_from_tree_1 (TREE_OPERAND (loc, 2), 0, context);
17857 dw_loc_descr_ref bra_node, jump_node, tmp;
17859 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17860 if (list_ret == 0 || lhs == 0 || rhs == 0)
17861 return 0;
17863 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
17864 add_loc_descr_to_each (list_ret, bra_node);
17866 add_loc_list (&list_ret, rhs);
17867 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
17868 add_loc_descr_to_each (list_ret, jump_node);
17870 add_loc_descr_to_each (list_ret, lhs);
17871 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
17872 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
17874 /* ??? Need a node to point the skip at. Use a nop. */
17875 tmp = new_loc_descr (DW_OP_nop, 0, 0);
17876 add_loc_descr_to_each (list_ret, tmp);
17877 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
17878 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
17880 break;
17882 case FIX_TRUNC_EXPR:
17883 return 0;
17885 default:
17886 /* Leave front-end specific codes as simply unknown. This comes
17887 up, for instance, with the C STMT_EXPR. */
17888 if ((unsigned int) TREE_CODE (loc)
17889 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
17891 expansion_failed (loc, NULL_RTX,
17892 "language specific tree node");
17893 return 0;
17896 /* Otherwise this is a generic code; we should just lists all of
17897 these explicitly. We forgot one. */
17898 if (flag_checking)
17899 gcc_unreachable ();
17901 /* In a release build, we want to degrade gracefully: better to
17902 generate incomplete debugging information than to crash. */
17903 return NULL;
17906 if (!ret && !list_ret)
17907 return 0;
17909 if (want_address == 2 && !have_address
17910 && (dwarf_version >= 4 || !dwarf_strict))
17912 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
17914 expansion_failed (loc, NULL_RTX,
17915 "DWARF address size mismatch");
17916 return 0;
17918 if (ret)
17919 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
17920 else
17921 add_loc_descr_to_each (list_ret,
17922 new_loc_descr (DW_OP_stack_value, 0, 0));
17923 have_address = 1;
17925 /* Show if we can't fill the request for an address. */
17926 if (want_address && !have_address)
17928 expansion_failed (loc, NULL_RTX,
17929 "Want address and only have value");
17930 return 0;
17933 gcc_assert (!ret || !list_ret);
17935 /* If we've got an address and don't want one, dereference. */
17936 if (!want_address && have_address)
17938 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
17940 if (size > DWARF2_ADDR_SIZE || size == -1)
17942 expansion_failed (loc, NULL_RTX,
17943 "DWARF address size mismatch");
17944 return 0;
17946 else if (size == DWARF2_ADDR_SIZE)
17947 op = DW_OP_deref;
17948 else
17949 op = DW_OP_deref_size;
17951 if (ret)
17952 add_loc_descr (&ret, new_loc_descr (op, size, 0));
17953 else
17954 add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
17956 if (ret)
17957 list_ret = new_loc_list (ret, NULL, NULL, NULL);
17959 return list_ret;
17962 /* Likewise, but strip useless DW_OP_nop operations in the resulting
17963 expressions. */
17965 static dw_loc_list_ref
17966 loc_list_from_tree (tree loc, int want_address,
17967 struct loc_descr_context *context)
17969 dw_loc_list_ref result = loc_list_from_tree_1 (loc, want_address, context);
17971 for (dw_loc_list_ref loc_cur = result;
17972 loc_cur != NULL; loc_cur = loc_cur->dw_loc_next)
17973 loc_descr_without_nops (loc_cur->expr);
17974 return result;
17977 /* Same as above but return only single location expression. */
17978 static dw_loc_descr_ref
17979 loc_descriptor_from_tree (tree loc, int want_address,
17980 struct loc_descr_context *context)
17982 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address, context);
17983 if (!ret)
17984 return NULL;
17985 if (ret->dw_loc_next)
17987 expansion_failed (loc, NULL_RTX,
17988 "Location list where only loc descriptor needed");
17989 return NULL;
17991 return ret->expr;
17994 /* Given a value, round it up to the lowest multiple of `boundary'
17995 which is not less than the value itself. */
17997 static inline HOST_WIDE_INT
17998 ceiling (HOST_WIDE_INT value, unsigned int boundary)
18000 return (((value + boundary - 1) / boundary) * boundary);
18003 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
18004 pointer to the declared type for the relevant field variable, or return
18005 `integer_type_node' if the given node turns out to be an
18006 ERROR_MARK node. */
18008 static inline tree
18009 field_type (const_tree decl)
18011 tree type;
18013 if (TREE_CODE (decl) == ERROR_MARK)
18014 return integer_type_node;
18016 type = DECL_BIT_FIELD_TYPE (decl);
18017 if (type == NULL_TREE)
18018 type = TREE_TYPE (decl);
18020 return type;
18023 /* Given a pointer to a tree node, return the alignment in bits for
18024 it, or else return BITS_PER_WORD if the node actually turns out to
18025 be an ERROR_MARK node. */
18027 static inline unsigned
18028 simple_type_align_in_bits (const_tree type)
18030 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
18033 static inline unsigned
18034 simple_decl_align_in_bits (const_tree decl)
18036 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
18039 /* Return the result of rounding T up to ALIGN. */
18041 static inline offset_int
18042 round_up_to_align (const offset_int &t, unsigned int align)
18044 return wi::udiv_trunc (t + align - 1, align) * align;
18047 /* Compute the size of TYPE in bytes. If possible, return NULL and store the
18048 size as an integer constant in CST_SIZE. Otherwise, if possible, return a
18049 DWARF expression that computes the size. Return NULL and set CST_SIZE to -1
18050 if we fail to return the size in one of these two forms. */
18052 static dw_loc_descr_ref
18053 type_byte_size (const_tree type, HOST_WIDE_INT *cst_size)
18055 tree tree_size;
18056 struct loc_descr_context ctx;
18058 /* Return a constant integer in priority, if possible. */
18059 *cst_size = int_size_in_bytes (type);
18060 if (*cst_size != -1)
18061 return NULL;
18063 ctx.context_type = const_cast<tree> (type);
18064 ctx.base_decl = NULL_TREE;
18065 ctx.dpi = NULL;
18066 ctx.placeholder_arg = false;
18067 ctx.placeholder_seen = false;
18069 type = TYPE_MAIN_VARIANT (type);
18070 tree_size = TYPE_SIZE_UNIT (type);
18071 return ((tree_size != NULL_TREE)
18072 ? loc_descriptor_from_tree (tree_size, 0, &ctx)
18073 : NULL);
18076 /* Helper structure for RECORD_TYPE processing. */
18077 struct vlr_context
18079 /* Root RECORD_TYPE. It is needed to generate data member location
18080 descriptions in variable-length records (VLR), but also to cope with
18081 variants, which are composed of nested structures multiplexed with
18082 QUAL_UNION_TYPE nodes. Each time such a structure is passed to a
18083 function processing a FIELD_DECL, it is required to be non null. */
18084 tree struct_type;
18085 /* When generating a variant part in a RECORD_TYPE (i.e. a nested
18086 QUAL_UNION_TYPE), this holds an expression that computes the offset for
18087 this variant part as part of the root record (in storage units). For
18088 regular records, it must be NULL_TREE. */
18089 tree variant_part_offset;
18092 /* Given a pointer to a FIELD_DECL, compute the byte offset of the lowest
18093 addressed byte of the "containing object" for the given FIELD_DECL. If
18094 possible, return a native constant through CST_OFFSET (in which case NULL is
18095 returned); otherwise return a DWARF expression that computes the offset.
18097 Set *CST_OFFSET to 0 and return NULL if we are unable to determine what
18098 that offset is, either because the argument turns out to be a pointer to an
18099 ERROR_MARK node, or because the offset expression is too complex for us.
18101 CTX is required: see the comment for VLR_CONTEXT. */
18103 static dw_loc_descr_ref
18104 field_byte_offset (const_tree decl, struct vlr_context *ctx,
18105 HOST_WIDE_INT *cst_offset)
18107 tree tree_result;
18108 dw_loc_list_ref loc_result;
18110 *cst_offset = 0;
18112 if (TREE_CODE (decl) == ERROR_MARK)
18113 return NULL;
18114 else
18115 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
18117 /* We cannot handle variable bit offsets at the moment, so abort if it's the
18118 case. */
18119 if (TREE_CODE (DECL_FIELD_BIT_OFFSET (decl)) != INTEGER_CST)
18120 return NULL;
18122 #ifdef PCC_BITFIELD_TYPE_MATTERS
18123 /* We used to handle only constant offsets in all cases. Now, we handle
18124 properly dynamic byte offsets only when PCC bitfield type doesn't
18125 matter. */
18126 if (PCC_BITFIELD_TYPE_MATTERS
18127 && TREE_CODE (DECL_FIELD_OFFSET (decl)) == INTEGER_CST)
18129 offset_int object_offset_in_bits;
18130 offset_int object_offset_in_bytes;
18131 offset_int bitpos_int;
18132 tree type;
18133 tree field_size_tree;
18134 offset_int deepest_bitpos;
18135 offset_int field_size_in_bits;
18136 unsigned int type_align_in_bits;
18137 unsigned int decl_align_in_bits;
18138 offset_int type_size_in_bits;
18140 bitpos_int = wi::to_offset (bit_position (decl));
18141 type = field_type (decl);
18142 type_size_in_bits = offset_int_type_size_in_bits (type);
18143 type_align_in_bits = simple_type_align_in_bits (type);
18145 field_size_tree = DECL_SIZE (decl);
18147 /* The size could be unspecified if there was an error, or for
18148 a flexible array member. */
18149 if (!field_size_tree)
18150 field_size_tree = bitsize_zero_node;
18152 /* If the size of the field is not constant, use the type size. */
18153 if (TREE_CODE (field_size_tree) == INTEGER_CST)
18154 field_size_in_bits = wi::to_offset (field_size_tree);
18155 else
18156 field_size_in_bits = type_size_in_bits;
18158 decl_align_in_bits = simple_decl_align_in_bits (decl);
18160 /* The GCC front-end doesn't make any attempt to keep track of the
18161 starting bit offset (relative to the start of the containing
18162 structure type) of the hypothetical "containing object" for a
18163 bit-field. Thus, when computing the byte offset value for the
18164 start of the "containing object" of a bit-field, we must deduce
18165 this information on our own. This can be rather tricky to do in
18166 some cases. For example, handling the following structure type
18167 definition when compiling for an i386/i486 target (which only
18168 aligns long long's to 32-bit boundaries) can be very tricky:
18170 struct S { int field1; long long field2:31; };
18172 Fortunately, there is a simple rule-of-thumb which can be used
18173 in such cases. When compiling for an i386/i486, GCC will
18174 allocate 8 bytes for the structure shown above. It decides to
18175 do this based upon one simple rule for bit-field allocation.
18176 GCC allocates each "containing object" for each bit-field at
18177 the first (i.e. lowest addressed) legitimate alignment boundary
18178 (based upon the required minimum alignment for the declared
18179 type of the field) which it can possibly use, subject to the
18180 condition that there is still enough available space remaining
18181 in the containing object (when allocated at the selected point)
18182 to fully accommodate all of the bits of the bit-field itself.
18184 This simple rule makes it obvious why GCC allocates 8 bytes for
18185 each object of the structure type shown above. When looking
18186 for a place to allocate the "containing object" for `field2',
18187 the compiler simply tries to allocate a 64-bit "containing
18188 object" at each successive 32-bit boundary (starting at zero)
18189 until it finds a place to allocate that 64- bit field such that
18190 at least 31 contiguous (and previously unallocated) bits remain
18191 within that selected 64 bit field. (As it turns out, for the
18192 example above, the compiler finds it is OK to allocate the
18193 "containing object" 64-bit field at bit-offset zero within the
18194 structure type.)
18196 Here we attempt to work backwards from the limited set of facts
18197 we're given, and we try to deduce from those facts, where GCC
18198 must have believed that the containing object started (within
18199 the structure type). The value we deduce is then used (by the
18200 callers of this routine) to generate DW_AT_location and
18201 DW_AT_bit_offset attributes for fields (both bit-fields and, in
18202 the case of DW_AT_location, regular fields as well). */
18204 /* Figure out the bit-distance from the start of the structure to
18205 the "deepest" bit of the bit-field. */
18206 deepest_bitpos = bitpos_int + field_size_in_bits;
18208 /* This is the tricky part. Use some fancy footwork to deduce
18209 where the lowest addressed bit of the containing object must
18210 be. */
18211 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
18213 /* Round up to type_align by default. This works best for
18214 bitfields. */
18215 object_offset_in_bits
18216 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
18218 if (wi::gtu_p (object_offset_in_bits, bitpos_int))
18220 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
18222 /* Round up to decl_align instead. */
18223 object_offset_in_bits
18224 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
18227 object_offset_in_bytes
18228 = wi::lrshift (object_offset_in_bits, LOG2_BITS_PER_UNIT);
18229 if (ctx->variant_part_offset == NULL_TREE)
18231 *cst_offset = object_offset_in_bytes.to_shwi ();
18232 return NULL;
18234 tree_result = wide_int_to_tree (sizetype, object_offset_in_bytes);
18236 else
18237 #endif /* PCC_BITFIELD_TYPE_MATTERS */
18238 tree_result = byte_position (decl);
18240 if (ctx->variant_part_offset != NULL_TREE)
18241 tree_result = fold_build2 (PLUS_EXPR, TREE_TYPE (tree_result),
18242 ctx->variant_part_offset, tree_result);
18244 /* If the byte offset is a constant, it's simplier to handle a native
18245 constant rather than a DWARF expression. */
18246 if (TREE_CODE (tree_result) == INTEGER_CST)
18248 *cst_offset = wi::to_offset (tree_result).to_shwi ();
18249 return NULL;
18251 struct loc_descr_context loc_ctx = {
18252 ctx->struct_type, /* context_type */
18253 NULL_TREE, /* base_decl */
18254 NULL, /* dpi */
18255 false, /* placeholder_arg */
18256 false /* placeholder_seen */
18258 loc_result = loc_list_from_tree (tree_result, 0, &loc_ctx);
18260 /* We want a DWARF expression: abort if we only have a location list with
18261 multiple elements. */
18262 if (!loc_result || !single_element_loc_list_p (loc_result))
18263 return NULL;
18264 else
18265 return loc_result->expr;
18268 /* The following routines define various Dwarf attributes and any data
18269 associated with them. */
18271 /* Add a location description attribute value to a DIE.
18273 This emits location attributes suitable for whole variables and
18274 whole parameters. Note that the location attributes for struct fields are
18275 generated by the routine `data_member_location_attribute' below. */
18277 static inline void
18278 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
18279 dw_loc_list_ref descr)
18281 if (descr == 0)
18282 return;
18283 if (single_element_loc_list_p (descr))
18284 add_AT_loc (die, attr_kind, descr->expr);
18285 else
18286 add_AT_loc_list (die, attr_kind, descr);
18289 /* Add DW_AT_accessibility attribute to DIE if needed. */
18291 static void
18292 add_accessibility_attribute (dw_die_ref die, tree decl)
18294 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
18295 children, otherwise the default is DW_ACCESS_public. In DWARF2
18296 the default has always been DW_ACCESS_public. */
18297 if (TREE_PROTECTED (decl))
18298 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
18299 else if (TREE_PRIVATE (decl))
18301 if (dwarf_version == 2
18302 || die->die_parent == NULL
18303 || die->die_parent->die_tag != DW_TAG_class_type)
18304 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
18306 else if (dwarf_version > 2
18307 && die->die_parent
18308 && die->die_parent->die_tag == DW_TAG_class_type)
18309 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
18312 /* Attach the specialized form of location attribute used for data members of
18313 struct and union types. In the special case of a FIELD_DECL node which
18314 represents a bit-field, the "offset" part of this special location
18315 descriptor must indicate the distance in bytes from the lowest-addressed
18316 byte of the containing struct or union type to the lowest-addressed byte of
18317 the "containing object" for the bit-field. (See the `field_byte_offset'
18318 function above).
18320 For any given bit-field, the "containing object" is a hypothetical object
18321 (of some integral or enum type) within which the given bit-field lives. The
18322 type of this hypothetical "containing object" is always the same as the
18323 declared type of the individual bit-field itself (for GCC anyway... the
18324 DWARF spec doesn't actually mandate this). Note that it is the size (in
18325 bytes) of the hypothetical "containing object" which will be given in the
18326 DW_AT_byte_size attribute for this bit-field. (See the
18327 `byte_size_attribute' function below.) It is also used when calculating the
18328 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
18329 function below.)
18331 CTX is required: see the comment for VLR_CONTEXT. */
18333 static void
18334 add_data_member_location_attribute (dw_die_ref die,
18335 tree decl,
18336 struct vlr_context *ctx)
18338 HOST_WIDE_INT offset;
18339 dw_loc_descr_ref loc_descr = 0;
18341 if (TREE_CODE (decl) == TREE_BINFO)
18343 /* We're working on the TAG_inheritance for a base class. */
18344 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
18346 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
18347 aren't at a fixed offset from all (sub)objects of the same
18348 type. We need to extract the appropriate offset from our
18349 vtable. The following dwarf expression means
18351 BaseAddr = ObAddr + *((*ObAddr) - Offset)
18353 This is specific to the V3 ABI, of course. */
18355 dw_loc_descr_ref tmp;
18357 /* Make a copy of the object address. */
18358 tmp = new_loc_descr (DW_OP_dup, 0, 0);
18359 add_loc_descr (&loc_descr, tmp);
18361 /* Extract the vtable address. */
18362 tmp = new_loc_descr (DW_OP_deref, 0, 0);
18363 add_loc_descr (&loc_descr, tmp);
18365 /* Calculate the address of the offset. */
18366 offset = tree_to_shwi (BINFO_VPTR_FIELD (decl));
18367 gcc_assert (offset < 0);
18369 tmp = int_loc_descriptor (-offset);
18370 add_loc_descr (&loc_descr, tmp);
18371 tmp = new_loc_descr (DW_OP_minus, 0, 0);
18372 add_loc_descr (&loc_descr, tmp);
18374 /* Extract the offset. */
18375 tmp = new_loc_descr (DW_OP_deref, 0, 0);
18376 add_loc_descr (&loc_descr, tmp);
18378 /* Add it to the object address. */
18379 tmp = new_loc_descr (DW_OP_plus, 0, 0);
18380 add_loc_descr (&loc_descr, tmp);
18382 else
18383 offset = tree_to_shwi (BINFO_OFFSET (decl));
18385 else
18387 loc_descr = field_byte_offset (decl, ctx, &offset);
18389 /* If loc_descr is available then we know the field offset is dynamic.
18390 However, GDB does not handle dynamic field offsets very well at the
18391 moment. */
18392 if (loc_descr != NULL && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL)
18394 loc_descr = NULL;
18395 offset = 0;
18398 /* Data member location evalutation starts with the base address on the
18399 stack. Compute the field offset and add it to this base address. */
18400 else if (loc_descr != NULL)
18401 add_loc_descr (&loc_descr, new_loc_descr (DW_OP_plus, 0, 0));
18404 if (! loc_descr)
18406 /* While DW_AT_data_bit_offset has been added already in DWARF4,
18407 e.g. GDB only added support to it in November 2016. For DWARF5
18408 we need newer debug info consumers anyway. We might change this
18409 to dwarf_version >= 4 once most consumers catched up. */
18410 if (dwarf_version >= 5
18411 && TREE_CODE (decl) == FIELD_DECL
18412 && DECL_BIT_FIELD_TYPE (decl))
18414 tree off = bit_position (decl);
18415 if (tree_fits_uhwi_p (off) && get_AT (die, DW_AT_bit_size))
18417 remove_AT (die, DW_AT_byte_size);
18418 remove_AT (die, DW_AT_bit_offset);
18419 add_AT_unsigned (die, DW_AT_data_bit_offset, tree_to_uhwi (off));
18420 return;
18423 if (dwarf_version > 2)
18425 /* Don't need to output a location expression, just the constant. */
18426 if (offset < 0)
18427 add_AT_int (die, DW_AT_data_member_location, offset);
18428 else
18429 add_AT_unsigned (die, DW_AT_data_member_location, offset);
18430 return;
18432 else
18434 enum dwarf_location_atom op;
18436 /* The DWARF2 standard says that we should assume that the structure
18437 address is already on the stack, so we can specify a structure
18438 field address by using DW_OP_plus_uconst. */
18439 op = DW_OP_plus_uconst;
18440 loc_descr = new_loc_descr (op, offset, 0);
18444 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
18447 /* Writes integer values to dw_vec_const array. */
18449 static void
18450 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
18452 while (size != 0)
18454 *dest++ = val & 0xff;
18455 val >>= 8;
18456 --size;
18460 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
18462 static HOST_WIDE_INT
18463 extract_int (const unsigned char *src, unsigned int size)
18465 HOST_WIDE_INT val = 0;
18467 src += size;
18468 while (size != 0)
18470 val <<= 8;
18471 val |= *--src & 0xff;
18472 --size;
18474 return val;
18477 /* Writes wide_int values to dw_vec_const array. */
18479 static void
18480 insert_wide_int (const wide_int &val, unsigned char *dest, int elt_size)
18482 int i;
18484 if (elt_size <= HOST_BITS_PER_WIDE_INT/BITS_PER_UNIT)
18486 insert_int ((HOST_WIDE_INT) val.elt (0), elt_size, dest);
18487 return;
18490 /* We'd have to extend this code to support odd sizes. */
18491 gcc_assert (elt_size % (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT) == 0);
18493 int n = elt_size / (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
18495 if (WORDS_BIG_ENDIAN)
18496 for (i = n - 1; i >= 0; i--)
18498 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
18499 dest += sizeof (HOST_WIDE_INT);
18501 else
18502 for (i = 0; i < n; i++)
18504 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
18505 dest += sizeof (HOST_WIDE_INT);
18509 /* Writes floating point values to dw_vec_const array. */
18511 static void
18512 insert_float (const_rtx rtl, unsigned char *array)
18514 long val[4];
18515 int i;
18517 real_to_target (val, CONST_DOUBLE_REAL_VALUE (rtl), GET_MODE (rtl));
18519 /* real_to_target puts 32-bit pieces in each long. Pack them. */
18520 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
18522 insert_int (val[i], 4, array);
18523 array += 4;
18527 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
18528 does not have a "location" either in memory or in a register. These
18529 things can arise in GNU C when a constant is passed as an actual parameter
18530 to an inlined function. They can also arise in C++ where declared
18531 constants do not necessarily get memory "homes". */
18533 static bool
18534 add_const_value_attribute (dw_die_ref die, rtx rtl)
18536 switch (GET_CODE (rtl))
18538 case CONST_INT:
18540 HOST_WIDE_INT val = INTVAL (rtl);
18542 if (val < 0)
18543 add_AT_int (die, DW_AT_const_value, val);
18544 else
18545 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
18547 return true;
18549 case CONST_WIDE_INT:
18551 wide_int w1 = rtx_mode_t (rtl, MAX_MODE_INT);
18552 unsigned int prec = MIN (wi::min_precision (w1, UNSIGNED),
18553 (unsigned int)CONST_WIDE_INT_NUNITS (rtl) * HOST_BITS_PER_WIDE_INT);
18554 wide_int w = wi::zext (w1, prec);
18555 add_AT_wide (die, DW_AT_const_value, w);
18557 return true;
18559 case CONST_DOUBLE:
18560 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
18561 floating-point constant. A CONST_DOUBLE is used whenever the
18562 constant requires more than one word in order to be adequately
18563 represented. */
18565 machine_mode mode = GET_MODE (rtl);
18567 if (TARGET_SUPPORTS_WIDE_INT == 0 && !SCALAR_FLOAT_MODE_P (mode))
18568 add_AT_double (die, DW_AT_const_value,
18569 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
18570 else
18572 unsigned int length = GET_MODE_SIZE (mode);
18573 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
18575 insert_float (rtl, array);
18576 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
18579 return true;
18581 case CONST_VECTOR:
18583 machine_mode mode = GET_MODE (rtl);
18584 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
18585 unsigned int length = CONST_VECTOR_NUNITS (rtl);
18586 unsigned char *array
18587 = ggc_vec_alloc<unsigned char> (length * elt_size);
18588 unsigned int i;
18589 unsigned char *p;
18590 machine_mode imode = GET_MODE_INNER (mode);
18592 switch (GET_MODE_CLASS (mode))
18594 case MODE_VECTOR_INT:
18595 for (i = 0, p = array; i < length; i++, p += elt_size)
18597 rtx elt = CONST_VECTOR_ELT (rtl, i);
18598 insert_wide_int (rtx_mode_t (elt, imode), p, elt_size);
18600 break;
18602 case MODE_VECTOR_FLOAT:
18603 for (i = 0, p = array; i < length; i++, p += elt_size)
18605 rtx elt = CONST_VECTOR_ELT (rtl, i);
18606 insert_float (elt, p);
18608 break;
18610 default:
18611 gcc_unreachable ();
18614 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
18616 return true;
18618 case CONST_STRING:
18619 if (dwarf_version >= 4 || !dwarf_strict)
18621 dw_loc_descr_ref loc_result;
18622 resolve_one_addr (&rtl);
18623 rtl_addr:
18624 loc_result = new_addr_loc_descr (rtl, dtprel_false);
18625 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
18626 add_AT_loc (die, DW_AT_location, loc_result);
18627 vec_safe_push (used_rtx_array, rtl);
18628 return true;
18630 return false;
18632 case CONST:
18633 if (CONSTANT_P (XEXP (rtl, 0)))
18634 return add_const_value_attribute (die, XEXP (rtl, 0));
18635 /* FALLTHROUGH */
18636 case SYMBOL_REF:
18637 if (!const_ok_for_output (rtl))
18638 return false;
18639 /* FALLTHROUGH */
18640 case LABEL_REF:
18641 if (dwarf_version >= 4 || !dwarf_strict)
18642 goto rtl_addr;
18643 return false;
18645 case PLUS:
18646 /* In cases where an inlined instance of an inline function is passed
18647 the address of an `auto' variable (which is local to the caller) we
18648 can get a situation where the DECL_RTL of the artificial local
18649 variable (for the inlining) which acts as a stand-in for the
18650 corresponding formal parameter (of the inline function) will look
18651 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
18652 exactly a compile-time constant expression, but it isn't the address
18653 of the (artificial) local variable either. Rather, it represents the
18654 *value* which the artificial local variable always has during its
18655 lifetime. We currently have no way to represent such quasi-constant
18656 values in Dwarf, so for now we just punt and generate nothing. */
18657 return false;
18659 case HIGH:
18660 case CONST_FIXED:
18661 return false;
18663 case MEM:
18664 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
18665 && MEM_READONLY_P (rtl)
18666 && GET_MODE (rtl) == BLKmode)
18668 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
18669 return true;
18671 return false;
18673 default:
18674 /* No other kinds of rtx should be possible here. */
18675 gcc_unreachable ();
18677 return false;
18680 /* Determine whether the evaluation of EXPR references any variables
18681 or functions which aren't otherwise used (and therefore may not be
18682 output). */
18683 static tree
18684 reference_to_unused (tree * tp, int * walk_subtrees,
18685 void * data ATTRIBUTE_UNUSED)
18687 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
18688 *walk_subtrees = 0;
18690 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
18691 && ! TREE_ASM_WRITTEN (*tp))
18692 return *tp;
18693 /* ??? The C++ FE emits debug information for using decls, so
18694 putting gcc_unreachable here falls over. See PR31899. For now
18695 be conservative. */
18696 else if (!symtab->global_info_ready && VAR_OR_FUNCTION_DECL_P (*tp))
18697 return *tp;
18698 else if (VAR_P (*tp))
18700 varpool_node *node = varpool_node::get (*tp);
18701 if (!node || !node->definition)
18702 return *tp;
18704 else if (TREE_CODE (*tp) == FUNCTION_DECL
18705 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
18707 /* The call graph machinery must have finished analyzing,
18708 optimizing and gimplifying the CU by now.
18709 So if *TP has no call graph node associated
18710 to it, it means *TP will not be emitted. */
18711 if (!cgraph_node::get (*tp))
18712 return *tp;
18714 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
18715 return *tp;
18717 return NULL_TREE;
18720 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
18721 for use in a later add_const_value_attribute call. */
18723 static rtx
18724 rtl_for_decl_init (tree init, tree type)
18726 rtx rtl = NULL_RTX;
18728 STRIP_NOPS (init);
18730 /* If a variable is initialized with a string constant without embedded
18731 zeros, build CONST_STRING. */
18732 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
18734 tree enttype = TREE_TYPE (type);
18735 tree domain = TYPE_DOMAIN (type);
18736 machine_mode mode = TYPE_MODE (enttype);
18738 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
18739 && domain
18740 && integer_zerop (TYPE_MIN_VALUE (domain))
18741 && compare_tree_int (TYPE_MAX_VALUE (domain),
18742 TREE_STRING_LENGTH (init) - 1) == 0
18743 && ((size_t) TREE_STRING_LENGTH (init)
18744 == strlen (TREE_STRING_POINTER (init)) + 1))
18746 rtl = gen_rtx_CONST_STRING (VOIDmode,
18747 ggc_strdup (TREE_STRING_POINTER (init)));
18748 rtl = gen_rtx_MEM (BLKmode, rtl);
18749 MEM_READONLY_P (rtl) = 1;
18752 /* Other aggregates, and complex values, could be represented using
18753 CONCAT: FIXME! */
18754 else if (AGGREGATE_TYPE_P (type)
18755 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
18756 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
18757 || TREE_CODE (type) == COMPLEX_TYPE)
18759 /* Vectors only work if their mode is supported by the target.
18760 FIXME: generic vectors ought to work too. */
18761 else if (TREE_CODE (type) == VECTOR_TYPE
18762 && !VECTOR_MODE_P (TYPE_MODE (type)))
18764 /* If the initializer is something that we know will expand into an
18765 immediate RTL constant, expand it now. We must be careful not to
18766 reference variables which won't be output. */
18767 else if (initializer_constant_valid_p (init, type)
18768 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
18770 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
18771 possible. */
18772 if (TREE_CODE (type) == VECTOR_TYPE)
18773 switch (TREE_CODE (init))
18775 case VECTOR_CST:
18776 break;
18777 case CONSTRUCTOR:
18778 if (TREE_CONSTANT (init))
18780 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
18781 bool constant_p = true;
18782 tree value;
18783 unsigned HOST_WIDE_INT ix;
18785 /* Even when ctor is constant, it might contain non-*_CST
18786 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
18787 belong into VECTOR_CST nodes. */
18788 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
18789 if (!CONSTANT_CLASS_P (value))
18791 constant_p = false;
18792 break;
18795 if (constant_p)
18797 init = build_vector_from_ctor (type, elts);
18798 break;
18801 /* FALLTHRU */
18803 default:
18804 return NULL;
18807 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
18809 /* If expand_expr returns a MEM, it wasn't immediate. */
18810 gcc_assert (!rtl || !MEM_P (rtl));
18813 return rtl;
18816 /* Generate RTL for the variable DECL to represent its location. */
18818 static rtx
18819 rtl_for_decl_location (tree decl)
18821 rtx rtl;
18823 /* Here we have to decide where we are going to say the parameter "lives"
18824 (as far as the debugger is concerned). We only have a couple of
18825 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
18827 DECL_RTL normally indicates where the parameter lives during most of the
18828 activation of the function. If optimization is enabled however, this
18829 could be either NULL or else a pseudo-reg. Both of those cases indicate
18830 that the parameter doesn't really live anywhere (as far as the code
18831 generation parts of GCC are concerned) during most of the function's
18832 activation. That will happen (for example) if the parameter is never
18833 referenced within the function.
18835 We could just generate a location descriptor here for all non-NULL
18836 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
18837 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
18838 where DECL_RTL is NULL or is a pseudo-reg.
18840 Note however that we can only get away with using DECL_INCOMING_RTL as
18841 a backup substitute for DECL_RTL in certain limited cases. In cases
18842 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
18843 we can be sure that the parameter was passed using the same type as it is
18844 declared to have within the function, and that its DECL_INCOMING_RTL
18845 points us to a place where a value of that type is passed.
18847 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
18848 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
18849 because in these cases DECL_INCOMING_RTL points us to a value of some
18850 type which is *different* from the type of the parameter itself. Thus,
18851 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
18852 such cases, the debugger would end up (for example) trying to fetch a
18853 `float' from a place which actually contains the first part of a
18854 `double'. That would lead to really incorrect and confusing
18855 output at debug-time.
18857 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
18858 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
18859 are a couple of exceptions however. On little-endian machines we can
18860 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
18861 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
18862 an integral type that is smaller than TREE_TYPE (decl). These cases arise
18863 when (on a little-endian machine) a non-prototyped function has a
18864 parameter declared to be of type `short' or `char'. In such cases,
18865 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
18866 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
18867 passed `int' value. If the debugger then uses that address to fetch
18868 a `short' or a `char' (on a little-endian machine) the result will be
18869 the correct data, so we allow for such exceptional cases below.
18871 Note that our goal here is to describe the place where the given formal
18872 parameter lives during most of the function's activation (i.e. between the
18873 end of the prologue and the start of the epilogue). We'll do that as best
18874 as we can. Note however that if the given formal parameter is modified
18875 sometime during the execution of the function, then a stack backtrace (at
18876 debug-time) will show the function as having been called with the *new*
18877 value rather than the value which was originally passed in. This happens
18878 rarely enough that it is not a major problem, but it *is* a problem, and
18879 I'd like to fix it.
18881 A future version of dwarf2out.c may generate two additional attributes for
18882 any given DW_TAG_formal_parameter DIE which will describe the "passed
18883 type" and the "passed location" for the given formal parameter in addition
18884 to the attributes we now generate to indicate the "declared type" and the
18885 "active location" for each parameter. This additional set of attributes
18886 could be used by debuggers for stack backtraces. Separately, note that
18887 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
18888 This happens (for example) for inlined-instances of inline function formal
18889 parameters which are never referenced. This really shouldn't be
18890 happening. All PARM_DECL nodes should get valid non-NULL
18891 DECL_INCOMING_RTL values. FIXME. */
18893 /* Use DECL_RTL as the "location" unless we find something better. */
18894 rtl = DECL_RTL_IF_SET (decl);
18896 /* When generating abstract instances, ignore everything except
18897 constants, symbols living in memory, and symbols living in
18898 fixed registers. */
18899 if (! reload_completed)
18901 if (rtl
18902 && (CONSTANT_P (rtl)
18903 || (MEM_P (rtl)
18904 && CONSTANT_P (XEXP (rtl, 0)))
18905 || (REG_P (rtl)
18906 && VAR_P (decl)
18907 && TREE_STATIC (decl))))
18909 rtl = targetm.delegitimize_address (rtl);
18910 return rtl;
18912 rtl = NULL_RTX;
18914 else if (TREE_CODE (decl) == PARM_DECL)
18916 if (rtl == NULL_RTX
18917 || is_pseudo_reg (rtl)
18918 || (MEM_P (rtl)
18919 && is_pseudo_reg (XEXP (rtl, 0))
18920 && DECL_INCOMING_RTL (decl)
18921 && MEM_P (DECL_INCOMING_RTL (decl))
18922 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
18924 tree declared_type = TREE_TYPE (decl);
18925 tree passed_type = DECL_ARG_TYPE (decl);
18926 machine_mode dmode = TYPE_MODE (declared_type);
18927 machine_mode pmode = TYPE_MODE (passed_type);
18929 /* This decl represents a formal parameter which was optimized out.
18930 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
18931 all cases where (rtl == NULL_RTX) just below. */
18932 if (dmode == pmode)
18933 rtl = DECL_INCOMING_RTL (decl);
18934 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
18935 && SCALAR_INT_MODE_P (dmode)
18936 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
18937 && DECL_INCOMING_RTL (decl))
18939 rtx inc = DECL_INCOMING_RTL (decl);
18940 if (REG_P (inc))
18941 rtl = inc;
18942 else if (MEM_P (inc))
18944 if (BYTES_BIG_ENDIAN)
18945 rtl = adjust_address_nv (inc, dmode,
18946 GET_MODE_SIZE (pmode)
18947 - GET_MODE_SIZE (dmode));
18948 else
18949 rtl = inc;
18954 /* If the parm was passed in registers, but lives on the stack, then
18955 make a big endian correction if the mode of the type of the
18956 parameter is not the same as the mode of the rtl. */
18957 /* ??? This is the same series of checks that are made in dbxout.c before
18958 we reach the big endian correction code there. It isn't clear if all
18959 of these checks are necessary here, but keeping them all is the safe
18960 thing to do. */
18961 else if (MEM_P (rtl)
18962 && XEXP (rtl, 0) != const0_rtx
18963 && ! CONSTANT_P (XEXP (rtl, 0))
18964 /* Not passed in memory. */
18965 && !MEM_P (DECL_INCOMING_RTL (decl))
18966 /* Not passed by invisible reference. */
18967 && (!REG_P (XEXP (rtl, 0))
18968 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
18969 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
18970 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
18971 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
18972 #endif
18974 /* Big endian correction check. */
18975 && BYTES_BIG_ENDIAN
18976 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
18977 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
18978 < UNITS_PER_WORD))
18980 machine_mode addr_mode = get_address_mode (rtl);
18981 int offset = (UNITS_PER_WORD
18982 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
18984 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
18985 plus_constant (addr_mode, XEXP (rtl, 0), offset));
18988 else if (VAR_P (decl)
18989 && rtl
18990 && MEM_P (rtl)
18991 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
18992 && BYTES_BIG_ENDIAN)
18994 machine_mode addr_mode = get_address_mode (rtl);
18995 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
18996 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
18998 /* If a variable is declared "register" yet is smaller than
18999 a register, then if we store the variable to memory, it
19000 looks like we're storing a register-sized value, when in
19001 fact we are not. We need to adjust the offset of the
19002 storage location to reflect the actual value's bytes,
19003 else gdb will not be able to display it. */
19004 if (rsize > dsize)
19005 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
19006 plus_constant (addr_mode, XEXP (rtl, 0),
19007 rsize - dsize));
19010 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
19011 and will have been substituted directly into all expressions that use it.
19012 C does not have such a concept, but C++ and other languages do. */
19013 if (!rtl && VAR_P (decl) && DECL_INITIAL (decl))
19014 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
19016 if (rtl)
19017 rtl = targetm.delegitimize_address (rtl);
19019 /* If we don't look past the constant pool, we risk emitting a
19020 reference to a constant pool entry that isn't referenced from
19021 code, and thus is not emitted. */
19022 if (rtl)
19023 rtl = avoid_constant_pool_reference (rtl);
19025 /* Try harder to get a rtl. If this symbol ends up not being emitted
19026 in the current CU, resolve_addr will remove the expression referencing
19027 it. */
19028 if (rtl == NULL_RTX
19029 && VAR_P (decl)
19030 && !DECL_EXTERNAL (decl)
19031 && TREE_STATIC (decl)
19032 && DECL_NAME (decl)
19033 && !DECL_HARD_REGISTER (decl)
19034 && DECL_MODE (decl) != VOIDmode)
19036 rtl = make_decl_rtl_for_debug (decl);
19037 if (!MEM_P (rtl)
19038 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
19039 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
19040 rtl = NULL_RTX;
19043 return rtl;
19046 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
19047 returned. If so, the decl for the COMMON block is returned, and the
19048 value is the offset into the common block for the symbol. */
19050 static tree
19051 fortran_common (tree decl, HOST_WIDE_INT *value)
19053 tree val_expr, cvar;
19054 machine_mode mode;
19055 HOST_WIDE_INT bitsize, bitpos;
19056 tree offset;
19057 int unsignedp, reversep, volatilep = 0;
19059 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
19060 it does not have a value (the offset into the common area), or if it
19061 is thread local (as opposed to global) then it isn't common, and shouldn't
19062 be handled as such. */
19063 if (!VAR_P (decl)
19064 || !TREE_STATIC (decl)
19065 || !DECL_HAS_VALUE_EXPR_P (decl)
19066 || !is_fortran ())
19067 return NULL_TREE;
19069 val_expr = DECL_VALUE_EXPR (decl);
19070 if (TREE_CODE (val_expr) != COMPONENT_REF)
19071 return NULL_TREE;
19073 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset, &mode,
19074 &unsignedp, &reversep, &volatilep);
19076 if (cvar == NULL_TREE
19077 || !VAR_P (cvar)
19078 || DECL_ARTIFICIAL (cvar)
19079 || !TREE_PUBLIC (cvar))
19080 return NULL_TREE;
19082 *value = 0;
19083 if (offset != NULL)
19085 if (!tree_fits_shwi_p (offset))
19086 return NULL_TREE;
19087 *value = tree_to_shwi (offset);
19089 if (bitpos != 0)
19090 *value += bitpos / BITS_PER_UNIT;
19092 return cvar;
19095 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
19096 data attribute for a variable or a parameter. We generate the
19097 DW_AT_const_value attribute only in those cases where the given variable
19098 or parameter does not have a true "location" either in memory or in a
19099 register. This can happen (for example) when a constant is passed as an
19100 actual argument in a call to an inline function. (It's possible that
19101 these things can crop up in other ways also.) Note that one type of
19102 constant value which can be passed into an inlined function is a constant
19103 pointer. This can happen for example if an actual argument in an inlined
19104 function call evaluates to a compile-time constant address.
19106 CACHE_P is true if it is worth caching the location list for DECL,
19107 so that future calls can reuse it rather than regenerate it from scratch.
19108 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
19109 since we will need to refer to them each time the function is inlined. */
19111 static bool
19112 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p)
19114 rtx rtl;
19115 dw_loc_list_ref list;
19116 var_loc_list *loc_list;
19117 cached_dw_loc_list *cache;
19119 if (early_dwarf)
19120 return false;
19122 if (TREE_CODE (decl) == ERROR_MARK)
19123 return false;
19125 if (get_AT (die, DW_AT_location)
19126 || get_AT (die, DW_AT_const_value))
19127 return true;
19129 gcc_assert (VAR_P (decl) || TREE_CODE (decl) == PARM_DECL
19130 || TREE_CODE (decl) == RESULT_DECL);
19132 /* Try to get some constant RTL for this decl, and use that as the value of
19133 the location. */
19135 rtl = rtl_for_decl_location (decl);
19136 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
19137 && add_const_value_attribute (die, rtl))
19138 return true;
19140 /* See if we have single element location list that is equivalent to
19141 a constant value. That way we are better to use add_const_value_attribute
19142 rather than expanding constant value equivalent. */
19143 loc_list = lookup_decl_loc (decl);
19144 if (loc_list
19145 && loc_list->first
19146 && loc_list->first->next == NULL
19147 && NOTE_P (loc_list->first->loc)
19148 && NOTE_VAR_LOCATION (loc_list->first->loc)
19149 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
19151 struct var_loc_node *node;
19153 node = loc_list->first;
19154 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
19155 if (GET_CODE (rtl) == EXPR_LIST)
19156 rtl = XEXP (rtl, 0);
19157 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
19158 && add_const_value_attribute (die, rtl))
19159 return true;
19161 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
19162 list several times. See if we've already cached the contents. */
19163 list = NULL;
19164 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
19165 cache_p = false;
19166 if (cache_p)
19168 cache = cached_dw_loc_list_table->find_with_hash (decl, DECL_UID (decl));
19169 if (cache)
19170 list = cache->loc_list;
19172 if (list == NULL)
19174 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2,
19175 NULL);
19176 /* It is usually worth caching this result if the decl is from
19177 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
19178 if (cache_p && list && list->dw_loc_next)
19180 cached_dw_loc_list **slot
19181 = cached_dw_loc_list_table->find_slot_with_hash (decl,
19182 DECL_UID (decl),
19183 INSERT);
19184 cache = ggc_cleared_alloc<cached_dw_loc_list> ();
19185 cache->decl_id = DECL_UID (decl);
19186 cache->loc_list = list;
19187 *slot = cache;
19190 if (list)
19192 add_AT_location_description (die, DW_AT_location, list);
19193 return true;
19195 /* None of that worked, so it must not really have a location;
19196 try adding a constant value attribute from the DECL_INITIAL. */
19197 return tree_add_const_value_attribute_for_decl (die, decl);
19200 /* Helper function for tree_add_const_value_attribute. Natively encode
19201 initializer INIT into an array. Return true if successful. */
19203 static bool
19204 native_encode_initializer (tree init, unsigned char *array, int size)
19206 tree type;
19208 if (init == NULL_TREE)
19209 return false;
19211 STRIP_NOPS (init);
19212 switch (TREE_CODE (init))
19214 case STRING_CST:
19215 type = TREE_TYPE (init);
19216 if (TREE_CODE (type) == ARRAY_TYPE)
19218 tree enttype = TREE_TYPE (type);
19219 machine_mode mode = TYPE_MODE (enttype);
19221 if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
19222 return false;
19223 if (int_size_in_bytes (type) != size)
19224 return false;
19225 if (size > TREE_STRING_LENGTH (init))
19227 memcpy (array, TREE_STRING_POINTER (init),
19228 TREE_STRING_LENGTH (init));
19229 memset (array + TREE_STRING_LENGTH (init),
19230 '\0', size - TREE_STRING_LENGTH (init));
19232 else
19233 memcpy (array, TREE_STRING_POINTER (init), size);
19234 return true;
19236 return false;
19237 case CONSTRUCTOR:
19238 type = TREE_TYPE (init);
19239 if (int_size_in_bytes (type) != size)
19240 return false;
19241 if (TREE_CODE (type) == ARRAY_TYPE)
19243 HOST_WIDE_INT min_index;
19244 unsigned HOST_WIDE_INT cnt;
19245 int curpos = 0, fieldsize;
19246 constructor_elt *ce;
19248 if (TYPE_DOMAIN (type) == NULL_TREE
19249 || !tree_fits_shwi_p (TYPE_MIN_VALUE (TYPE_DOMAIN (type))))
19250 return false;
19252 fieldsize = int_size_in_bytes (TREE_TYPE (type));
19253 if (fieldsize <= 0)
19254 return false;
19256 min_index = tree_to_shwi (TYPE_MIN_VALUE (TYPE_DOMAIN (type)));
19257 memset (array, '\0', size);
19258 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
19260 tree val = ce->value;
19261 tree index = ce->index;
19262 int pos = curpos;
19263 if (index && TREE_CODE (index) == RANGE_EXPR)
19264 pos = (tree_to_shwi (TREE_OPERAND (index, 0)) - min_index)
19265 * fieldsize;
19266 else if (index)
19267 pos = (tree_to_shwi (index) - min_index) * fieldsize;
19269 if (val)
19271 STRIP_NOPS (val);
19272 if (!native_encode_initializer (val, array + pos, fieldsize))
19273 return false;
19275 curpos = pos + fieldsize;
19276 if (index && TREE_CODE (index) == RANGE_EXPR)
19278 int count = tree_to_shwi (TREE_OPERAND (index, 1))
19279 - tree_to_shwi (TREE_OPERAND (index, 0));
19280 while (count-- > 0)
19282 if (val)
19283 memcpy (array + curpos, array + pos, fieldsize);
19284 curpos += fieldsize;
19287 gcc_assert (curpos <= size);
19289 return true;
19291 else if (TREE_CODE (type) == RECORD_TYPE
19292 || TREE_CODE (type) == UNION_TYPE)
19294 tree field = NULL_TREE;
19295 unsigned HOST_WIDE_INT cnt;
19296 constructor_elt *ce;
19298 if (int_size_in_bytes (type) != size)
19299 return false;
19301 if (TREE_CODE (type) == RECORD_TYPE)
19302 field = TYPE_FIELDS (type);
19304 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
19306 tree val = ce->value;
19307 int pos, fieldsize;
19309 if (ce->index != 0)
19310 field = ce->index;
19312 if (val)
19313 STRIP_NOPS (val);
19315 if (field == NULL_TREE || DECL_BIT_FIELD (field))
19316 return false;
19318 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
19319 && TYPE_DOMAIN (TREE_TYPE (field))
19320 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
19321 return false;
19322 else if (DECL_SIZE_UNIT (field) == NULL_TREE
19323 || !tree_fits_shwi_p (DECL_SIZE_UNIT (field)))
19324 return false;
19325 fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
19326 pos = int_byte_position (field);
19327 gcc_assert (pos + fieldsize <= size);
19328 if (val && fieldsize != 0
19329 && !native_encode_initializer (val, array + pos, fieldsize))
19330 return false;
19332 return true;
19334 return false;
19335 case VIEW_CONVERT_EXPR:
19336 case NON_LVALUE_EXPR:
19337 return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
19338 default:
19339 return native_encode_expr (init, array, size) == size;
19343 /* Attach a DW_AT_const_value attribute to DIE. The value of the
19344 attribute is the const value T. */
19346 static bool
19347 tree_add_const_value_attribute (dw_die_ref die, tree t)
19349 tree init;
19350 tree type = TREE_TYPE (t);
19351 rtx rtl;
19353 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
19354 return false;
19356 init = t;
19357 gcc_assert (!DECL_P (init));
19359 if (! early_dwarf)
19361 rtl = rtl_for_decl_init (init, type);
19362 if (rtl)
19363 return add_const_value_attribute (die, rtl);
19365 /* If the host and target are sane, try harder. */
19366 if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
19367 && initializer_constant_valid_p (init, type))
19369 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
19370 if (size > 0 && (int) size == size)
19372 unsigned char *array = ggc_cleared_vec_alloc<unsigned char> (size);
19374 if (native_encode_initializer (init, array, size))
19376 add_AT_vec (die, DW_AT_const_value, size, 1, array);
19377 return true;
19379 ggc_free (array);
19382 return false;
19385 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
19386 attribute is the const value of T, where T is an integral constant
19387 variable with static storage duration
19388 (so it can't be a PARM_DECL or a RESULT_DECL). */
19390 static bool
19391 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
19394 if (!decl
19395 || (!VAR_P (decl) && TREE_CODE (decl) != CONST_DECL)
19396 || (VAR_P (decl) && !TREE_STATIC (decl)))
19397 return false;
19399 if (TREE_READONLY (decl)
19400 && ! TREE_THIS_VOLATILE (decl)
19401 && DECL_INITIAL (decl))
19402 /* OK */;
19403 else
19404 return false;
19406 /* Don't add DW_AT_const_value if abstract origin already has one. */
19407 if (get_AT (var_die, DW_AT_const_value))
19408 return false;
19410 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
19413 /* Convert the CFI instructions for the current function into a
19414 location list. This is used for DW_AT_frame_base when we targeting
19415 a dwarf2 consumer that does not support the dwarf3
19416 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
19417 expressions. */
19419 static dw_loc_list_ref
19420 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
19422 int ix;
19423 dw_fde_ref fde;
19424 dw_loc_list_ref list, *list_tail;
19425 dw_cfi_ref cfi;
19426 dw_cfa_location last_cfa, next_cfa;
19427 const char *start_label, *last_label, *section;
19428 dw_cfa_location remember;
19430 fde = cfun->fde;
19431 gcc_assert (fde != NULL);
19433 section = secname_for_decl (current_function_decl);
19434 list_tail = &list;
19435 list = NULL;
19437 memset (&next_cfa, 0, sizeof (next_cfa));
19438 next_cfa.reg = INVALID_REGNUM;
19439 remember = next_cfa;
19441 start_label = fde->dw_fde_begin;
19443 /* ??? Bald assumption that the CIE opcode list does not contain
19444 advance opcodes. */
19445 FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
19446 lookup_cfa_1 (cfi, &next_cfa, &remember);
19448 last_cfa = next_cfa;
19449 last_label = start_label;
19451 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
19453 /* If the first partition contained no CFI adjustments, the
19454 CIE opcodes apply to the whole first partition. */
19455 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19456 fde->dw_fde_begin, fde->dw_fde_end, section);
19457 list_tail =&(*list_tail)->dw_loc_next;
19458 start_label = last_label = fde->dw_fde_second_begin;
19461 FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
19463 switch (cfi->dw_cfi_opc)
19465 case DW_CFA_set_loc:
19466 case DW_CFA_advance_loc1:
19467 case DW_CFA_advance_loc2:
19468 case DW_CFA_advance_loc4:
19469 if (!cfa_equal_p (&last_cfa, &next_cfa))
19471 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19472 start_label, last_label, section);
19474 list_tail = &(*list_tail)->dw_loc_next;
19475 last_cfa = next_cfa;
19476 start_label = last_label;
19478 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
19479 break;
19481 case DW_CFA_advance_loc:
19482 /* The encoding is complex enough that we should never emit this. */
19483 gcc_unreachable ();
19485 default:
19486 lookup_cfa_1 (cfi, &next_cfa, &remember);
19487 break;
19489 if (ix + 1 == fde->dw_fde_switch_cfi_index)
19491 if (!cfa_equal_p (&last_cfa, &next_cfa))
19493 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19494 start_label, last_label, section);
19496 list_tail = &(*list_tail)->dw_loc_next;
19497 last_cfa = next_cfa;
19498 start_label = last_label;
19500 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19501 start_label, fde->dw_fde_end, section);
19502 list_tail = &(*list_tail)->dw_loc_next;
19503 start_label = last_label = fde->dw_fde_second_begin;
19507 if (!cfa_equal_p (&last_cfa, &next_cfa))
19509 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19510 start_label, last_label, section);
19511 list_tail = &(*list_tail)->dw_loc_next;
19512 start_label = last_label;
19515 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
19516 start_label,
19517 fde->dw_fde_second_begin
19518 ? fde->dw_fde_second_end : fde->dw_fde_end,
19519 section);
19521 if (list && list->dw_loc_next)
19522 gen_llsym (list);
19524 return list;
19527 /* Compute a displacement from the "steady-state frame pointer" to the
19528 frame base (often the same as the CFA), and store it in
19529 frame_pointer_fb_offset. OFFSET is added to the displacement
19530 before the latter is negated. */
19532 static void
19533 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
19535 rtx reg, elim;
19537 #ifdef FRAME_POINTER_CFA_OFFSET
19538 reg = frame_pointer_rtx;
19539 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
19540 #else
19541 reg = arg_pointer_rtx;
19542 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
19543 #endif
19545 elim = (ira_use_lra_p
19546 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
19547 : eliminate_regs (reg, VOIDmode, NULL_RTX));
19548 if (GET_CODE (elim) == PLUS)
19550 offset += INTVAL (XEXP (elim, 1));
19551 elim = XEXP (elim, 0);
19554 frame_pointer_fb_offset = -offset;
19556 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
19557 in which to eliminate. This is because it's stack pointer isn't
19558 directly accessible as a register within the ISA. To work around
19559 this, assume that while we cannot provide a proper value for
19560 frame_pointer_fb_offset, we won't need one either. */
19561 frame_pointer_fb_offset_valid
19562 = ((SUPPORTS_STACK_ALIGNMENT
19563 && (elim == hard_frame_pointer_rtx
19564 || elim == stack_pointer_rtx))
19565 || elim == (frame_pointer_needed
19566 ? hard_frame_pointer_rtx
19567 : stack_pointer_rtx));
19570 /* Generate a DW_AT_name attribute given some string value to be included as
19571 the value of the attribute. */
19573 static void
19574 add_name_attribute (dw_die_ref die, const char *name_string)
19576 if (name_string != NULL && *name_string != 0)
19578 if (demangle_name_func)
19579 name_string = (*demangle_name_func) (name_string);
19581 add_AT_string (die, DW_AT_name, name_string);
19585 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
19586 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
19587 of TYPE accordingly.
19589 ??? This is a temporary measure until after we're able to generate
19590 regular DWARF for the complex Ada type system. */
19592 static void
19593 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
19594 dw_die_ref context_die)
19596 tree dtype;
19597 dw_die_ref dtype_die;
19599 if (!lang_hooks.types.descriptive_type)
19600 return;
19602 dtype = lang_hooks.types.descriptive_type (type);
19603 if (!dtype)
19604 return;
19606 dtype_die = lookup_type_die (dtype);
19607 if (!dtype_die)
19609 gen_type_die (dtype, context_die);
19610 dtype_die = lookup_type_die (dtype);
19611 gcc_assert (dtype_die);
19614 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
19617 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir. */
19619 static const char *
19620 comp_dir_string (void)
19622 const char *wd;
19623 char *wd1;
19624 static const char *cached_wd = NULL;
19626 if (cached_wd != NULL)
19627 return cached_wd;
19629 wd = get_src_pwd ();
19630 if (wd == NULL)
19631 return NULL;
19633 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
19635 int wdlen;
19637 wdlen = strlen (wd);
19638 wd1 = ggc_vec_alloc<char> (wdlen + 2);
19639 strcpy (wd1, wd);
19640 wd1 [wdlen] = DIR_SEPARATOR;
19641 wd1 [wdlen + 1] = 0;
19642 wd = wd1;
19645 cached_wd = remap_debug_filename (wd);
19646 return cached_wd;
19649 /* Generate a DW_AT_comp_dir attribute for DIE. */
19651 static void
19652 add_comp_dir_attribute (dw_die_ref die)
19654 const char * wd = comp_dir_string ();
19655 if (wd != NULL)
19656 add_AT_string (die, DW_AT_comp_dir, wd);
19659 /* Given a tree node VALUE describing a scalar attribute ATTR (i.e. a bound, a
19660 pointer computation, ...), output a representation for that bound according
19661 to the accepted FORMS (see enum dw_scalar_form) and add it to DIE. See
19662 loc_list_from_tree for the meaning of CONTEXT. */
19664 static void
19665 add_scalar_info (dw_die_ref die, enum dwarf_attribute attr, tree value,
19666 int forms, struct loc_descr_context *context)
19668 dw_die_ref context_die, decl_die;
19669 dw_loc_list_ref list;
19670 bool strip_conversions = true;
19671 bool placeholder_seen = false;
19673 while (strip_conversions)
19674 switch (TREE_CODE (value))
19676 case ERROR_MARK:
19677 case SAVE_EXPR:
19678 return;
19680 CASE_CONVERT:
19681 case VIEW_CONVERT_EXPR:
19682 value = TREE_OPERAND (value, 0);
19683 break;
19685 default:
19686 strip_conversions = false;
19687 break;
19690 /* If possible and permitted, output the attribute as a constant. */
19691 if ((forms & dw_scalar_form_constant) != 0
19692 && TREE_CODE (value) == INTEGER_CST)
19694 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (value));
19696 /* If HOST_WIDE_INT is big enough then represent the bound as
19697 a constant value. We need to choose a form based on
19698 whether the type is signed or unsigned. We cannot just
19699 call add_AT_unsigned if the value itself is positive
19700 (add_AT_unsigned might add the unsigned value encoded as
19701 DW_FORM_data[1248]). Some DWARF consumers will lookup the
19702 bounds type and then sign extend any unsigned values found
19703 for signed types. This is needed only for
19704 DW_AT_{lower,upper}_bound, since for most other attributes,
19705 consumers will treat DW_FORM_data[1248] as unsigned values,
19706 regardless of the underlying type. */
19707 if (prec <= HOST_BITS_PER_WIDE_INT
19708 || tree_fits_uhwi_p (value))
19710 if (TYPE_UNSIGNED (TREE_TYPE (value)))
19711 add_AT_unsigned (die, attr, TREE_INT_CST_LOW (value));
19712 else
19713 add_AT_int (die, attr, TREE_INT_CST_LOW (value));
19715 else
19716 /* Otherwise represent the bound as an unsigned value with
19717 the precision of its type. The precision and signedness
19718 of the type will be necessary to re-interpret it
19719 unambiguously. */
19720 add_AT_wide (die, attr, value);
19721 return;
19724 /* Otherwise, if it's possible and permitted too, output a reference to
19725 another DIE. */
19726 if ((forms & dw_scalar_form_reference) != 0)
19728 tree decl = NULL_TREE;
19730 /* Some type attributes reference an outer type. For instance, the upper
19731 bound of an array may reference an embedding record (this happens in
19732 Ada). */
19733 if (TREE_CODE (value) == COMPONENT_REF
19734 && TREE_CODE (TREE_OPERAND (value, 0)) == PLACEHOLDER_EXPR
19735 && TREE_CODE (TREE_OPERAND (value, 1)) == FIELD_DECL)
19736 decl = TREE_OPERAND (value, 1);
19738 else if (VAR_P (value)
19739 || TREE_CODE (value) == PARM_DECL
19740 || TREE_CODE (value) == RESULT_DECL)
19741 decl = value;
19743 if (decl != NULL_TREE)
19745 dw_die_ref decl_die = lookup_decl_die (decl);
19747 /* ??? Can this happen, or should the variable have been bound
19748 first? Probably it can, since I imagine that we try to create
19749 the types of parameters in the order in which they exist in
19750 the list, and won't have created a forward reference to a
19751 later parameter. */
19752 if (decl_die != NULL)
19754 add_AT_die_ref (die, attr, decl_die);
19755 return;
19760 /* Last chance: try to create a stack operation procedure to evaluate the
19761 value. Do nothing if even that is not possible or permitted. */
19762 if ((forms & dw_scalar_form_exprloc) == 0)
19763 return;
19765 list = loc_list_from_tree (value, 2, context);
19766 if (context && context->placeholder_arg)
19768 placeholder_seen = context->placeholder_seen;
19769 context->placeholder_seen = false;
19771 if (list == NULL || single_element_loc_list_p (list))
19773 /* If this attribute is not a reference nor constant, it is
19774 a DWARF expression rather than location description. For that
19775 loc_list_from_tree (value, 0, &context) is needed. */
19776 dw_loc_list_ref list2 = loc_list_from_tree (value, 0, context);
19777 if (list2 && single_element_loc_list_p (list2))
19779 if (placeholder_seen)
19781 struct dwarf_procedure_info dpi;
19782 dpi.fndecl = NULL_TREE;
19783 dpi.args_count = 1;
19784 if (!resolve_args_picking (list2->expr, 1, &dpi))
19785 return;
19787 add_AT_loc (die, attr, list2->expr);
19788 return;
19792 /* If that failed to give a single element location list, fall back to
19793 outputting this as a reference... still if permitted. */
19794 if (list == NULL
19795 || (forms & dw_scalar_form_reference) == 0
19796 || placeholder_seen)
19797 return;
19799 if (current_function_decl == 0)
19800 context_die = comp_unit_die ();
19801 else
19802 context_die = lookup_decl_die (current_function_decl);
19804 decl_die = new_die (DW_TAG_variable, context_die, value);
19805 add_AT_flag (decl_die, DW_AT_artificial, 1);
19806 add_type_attribute (decl_die, TREE_TYPE (value), TYPE_QUAL_CONST, false,
19807 context_die);
19808 add_AT_location_description (decl_die, DW_AT_location, list);
19809 add_AT_die_ref (die, attr, decl_die);
19812 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
19813 default. */
19815 static int
19816 lower_bound_default (void)
19818 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
19820 case DW_LANG_C:
19821 case DW_LANG_C89:
19822 case DW_LANG_C99:
19823 case DW_LANG_C11:
19824 case DW_LANG_C_plus_plus:
19825 case DW_LANG_C_plus_plus_11:
19826 case DW_LANG_C_plus_plus_14:
19827 case DW_LANG_ObjC:
19828 case DW_LANG_ObjC_plus_plus:
19829 case DW_LANG_Java:
19830 return 0;
19831 case DW_LANG_Fortran77:
19832 case DW_LANG_Fortran90:
19833 case DW_LANG_Fortran95:
19834 case DW_LANG_Fortran03:
19835 case DW_LANG_Fortran08:
19836 return 1;
19837 case DW_LANG_UPC:
19838 case DW_LANG_D:
19839 case DW_LANG_Python:
19840 return dwarf_version >= 4 ? 0 : -1;
19841 case DW_LANG_Ada95:
19842 case DW_LANG_Ada83:
19843 case DW_LANG_Cobol74:
19844 case DW_LANG_Cobol85:
19845 case DW_LANG_Pascal83:
19846 case DW_LANG_Modula2:
19847 case DW_LANG_PLI:
19848 return dwarf_version >= 4 ? 1 : -1;
19849 default:
19850 return -1;
19854 /* Given a tree node describing an array bound (either lower or upper) output
19855 a representation for that bound. */
19857 static void
19858 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr,
19859 tree bound, struct loc_descr_context *context)
19861 int dflt;
19863 while (1)
19864 switch (TREE_CODE (bound))
19866 /* Strip all conversions. */
19867 CASE_CONVERT:
19868 case VIEW_CONVERT_EXPR:
19869 bound = TREE_OPERAND (bound, 0);
19870 break;
19872 /* All fixed-bounds are represented by INTEGER_CST nodes. Lower bounds
19873 are even omitted when they are the default. */
19874 case INTEGER_CST:
19875 /* If the value for this bound is the default one, we can even omit the
19876 attribute. */
19877 if (bound_attr == DW_AT_lower_bound
19878 && tree_fits_shwi_p (bound)
19879 && (dflt = lower_bound_default ()) != -1
19880 && tree_to_shwi (bound) == dflt)
19881 return;
19883 /* FALLTHRU */
19885 default:
19886 /* Because of the complex interaction there can be with other GNAT
19887 encodings, GDB isn't ready yet to handle proper DWARF description
19888 for self-referencial subrange bounds: let GNAT encodings do the
19889 magic in such a case. */
19890 if (is_ada ()
19891 && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL
19892 && contains_placeholder_p (bound))
19893 return;
19895 add_scalar_info (subrange_die, bound_attr, bound,
19896 dw_scalar_form_constant
19897 | dw_scalar_form_exprloc
19898 | dw_scalar_form_reference,
19899 context);
19900 return;
19904 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
19905 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
19906 Note that the block of subscript information for an array type also
19907 includes information about the element type of the given array type.
19909 This function reuses previously set type and bound information if
19910 available. */
19912 static void
19913 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
19915 unsigned dimension_number;
19916 tree lower, upper;
19917 dw_die_ref child = type_die->die_child;
19919 for (dimension_number = 0;
19920 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
19921 type = TREE_TYPE (type), dimension_number++)
19923 tree domain = TYPE_DOMAIN (type);
19925 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
19926 break;
19928 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
19929 and (in GNU C only) variable bounds. Handle all three forms
19930 here. */
19932 /* Find and reuse a previously generated DW_TAG_subrange_type if
19933 available.
19935 For multi-dimensional arrays, as we iterate through the
19936 various dimensions in the enclosing for loop above, we also
19937 iterate through the DIE children and pick at each
19938 DW_TAG_subrange_type previously generated (if available).
19939 Each child DW_TAG_subrange_type DIE describes the range of
19940 the current dimension. At this point we should have as many
19941 DW_TAG_subrange_type's as we have dimensions in the
19942 array. */
19943 dw_die_ref subrange_die = NULL;
19944 if (child)
19945 while (1)
19947 child = child->die_sib;
19948 if (child->die_tag == DW_TAG_subrange_type)
19949 subrange_die = child;
19950 if (child == type_die->die_child)
19952 /* If we wrapped around, stop looking next time. */
19953 child = NULL;
19954 break;
19956 if (child->die_tag == DW_TAG_subrange_type)
19957 break;
19959 if (!subrange_die)
19960 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
19962 if (domain)
19964 /* We have an array type with specified bounds. */
19965 lower = TYPE_MIN_VALUE (domain);
19966 upper = TYPE_MAX_VALUE (domain);
19968 /* Define the index type. */
19969 if (TREE_TYPE (domain)
19970 && !get_AT (subrange_die, DW_AT_type))
19972 /* ??? This is probably an Ada unnamed subrange type. Ignore the
19973 TREE_TYPE field. We can't emit debug info for this
19974 because it is an unnamed integral type. */
19975 if (TREE_CODE (domain) == INTEGER_TYPE
19976 && TYPE_NAME (domain) == NULL_TREE
19977 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
19978 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
19980 else
19981 add_type_attribute (subrange_die, TREE_TYPE (domain),
19982 TYPE_UNQUALIFIED, false, type_die);
19985 /* ??? If upper is NULL, the array has unspecified length,
19986 but it does have a lower bound. This happens with Fortran
19987 dimension arr(N:*)
19988 Since the debugger is definitely going to need to know N
19989 to produce useful results, go ahead and output the lower
19990 bound solo, and hope the debugger can cope. */
19992 if (!get_AT (subrange_die, DW_AT_lower_bound))
19993 add_bound_info (subrange_die, DW_AT_lower_bound, lower, NULL);
19994 if (upper && !get_AT (subrange_die, DW_AT_upper_bound))
19995 add_bound_info (subrange_die, DW_AT_upper_bound, upper, NULL);
19998 /* Otherwise we have an array type with an unspecified length. The
19999 DWARF-2 spec does not say how to handle this; let's just leave out the
20000 bounds. */
20004 /* Add a DW_AT_byte_size attribute to DIE with TREE_NODE's size. */
20006 static void
20007 add_byte_size_attribute (dw_die_ref die, tree tree_node)
20009 dw_die_ref decl_die;
20010 HOST_WIDE_INT size;
20011 dw_loc_descr_ref size_expr = NULL;
20013 switch (TREE_CODE (tree_node))
20015 case ERROR_MARK:
20016 size = 0;
20017 break;
20018 case ENUMERAL_TYPE:
20019 case RECORD_TYPE:
20020 case UNION_TYPE:
20021 case QUAL_UNION_TYPE:
20022 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
20023 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
20025 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
20026 return;
20028 size_expr = type_byte_size (tree_node, &size);
20029 break;
20030 case FIELD_DECL:
20031 /* For a data member of a struct or union, the DW_AT_byte_size is
20032 generally given as the number of bytes normally allocated for an
20033 object of the *declared* type of the member itself. This is true
20034 even for bit-fields. */
20035 size = int_size_in_bytes (field_type (tree_node));
20036 break;
20037 default:
20038 gcc_unreachable ();
20041 /* Support for dynamically-sized objects was introduced by DWARFv3.
20042 At the moment, GDB does not handle variable byte sizes very well,
20043 though. */
20044 if ((dwarf_version >= 3 || !dwarf_strict)
20045 && gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL
20046 && size_expr != NULL)
20047 add_AT_loc (die, DW_AT_byte_size, size_expr);
20049 /* Note that `size' might be -1 when we get to this point. If it is, that
20050 indicates that the byte size of the entity in question is variable and
20051 that we could not generate a DWARF expression that computes it. */
20052 if (size >= 0)
20053 add_AT_unsigned (die, DW_AT_byte_size, size);
20056 /* Add a DW_AT_alignment attribute to DIE with TREE_NODE's non-default
20057 alignment. */
20059 static void
20060 add_alignment_attribute (dw_die_ref die, tree tree_node)
20062 if (dwarf_version < 5 && dwarf_strict)
20063 return;
20065 unsigned align;
20067 if (DECL_P (tree_node))
20069 if (!DECL_USER_ALIGN (tree_node))
20070 return;
20072 align = DECL_ALIGN_UNIT (tree_node);
20074 else if (TYPE_P (tree_node))
20076 if (!TYPE_USER_ALIGN (tree_node))
20077 return;
20079 align = TYPE_ALIGN_UNIT (tree_node);
20081 else
20082 gcc_unreachable ();
20084 add_AT_unsigned (die, DW_AT_alignment, align);
20087 /* For a FIELD_DECL node which represents a bit-field, output an attribute
20088 which specifies the distance in bits from the highest order bit of the
20089 "containing object" for the bit-field to the highest order bit of the
20090 bit-field itself.
20092 For any given bit-field, the "containing object" is a hypothetical object
20093 (of some integral or enum type) within which the given bit-field lives. The
20094 type of this hypothetical "containing object" is always the same as the
20095 declared type of the individual bit-field itself. The determination of the
20096 exact location of the "containing object" for a bit-field is rather
20097 complicated. It's handled by the `field_byte_offset' function (above).
20099 CTX is required: see the comment for VLR_CONTEXT.
20101 Note that it is the size (in bytes) of the hypothetical "containing object"
20102 which will be given in the DW_AT_byte_size attribute for this bit-field.
20103 (See `byte_size_attribute' above). */
20105 static inline void
20106 add_bit_offset_attribute (dw_die_ref die, tree decl, struct vlr_context *ctx)
20108 HOST_WIDE_INT object_offset_in_bytes;
20109 tree original_type = DECL_BIT_FIELD_TYPE (decl);
20110 HOST_WIDE_INT bitpos_int;
20111 HOST_WIDE_INT highest_order_object_bit_offset;
20112 HOST_WIDE_INT highest_order_field_bit_offset;
20113 HOST_WIDE_INT bit_offset;
20115 field_byte_offset (decl, ctx, &object_offset_in_bytes);
20117 /* Must be a field and a bit field. */
20118 gcc_assert (original_type && TREE_CODE (decl) == FIELD_DECL);
20120 /* We can't yet handle bit-fields whose offsets are variable, so if we
20121 encounter such things, just return without generating any attribute
20122 whatsoever. Likewise for variable or too large size. */
20123 if (! tree_fits_shwi_p (bit_position (decl))
20124 || ! tree_fits_uhwi_p (DECL_SIZE (decl)))
20125 return;
20127 bitpos_int = int_bit_position (decl);
20129 /* Note that the bit offset is always the distance (in bits) from the
20130 highest-order bit of the "containing object" to the highest-order bit of
20131 the bit-field itself. Since the "high-order end" of any object or field
20132 is different on big-endian and little-endian machines, the computation
20133 below must take account of these differences. */
20134 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
20135 highest_order_field_bit_offset = bitpos_int;
20137 if (! BYTES_BIG_ENDIAN)
20139 highest_order_field_bit_offset += tree_to_shwi (DECL_SIZE (decl));
20140 highest_order_object_bit_offset +=
20141 simple_type_size_in_bits (original_type);
20144 bit_offset
20145 = (! BYTES_BIG_ENDIAN
20146 ? highest_order_object_bit_offset - highest_order_field_bit_offset
20147 : highest_order_field_bit_offset - highest_order_object_bit_offset);
20149 if (bit_offset < 0)
20150 add_AT_int (die, DW_AT_bit_offset, bit_offset);
20151 else
20152 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
20155 /* For a FIELD_DECL node which represents a bit field, output an attribute
20156 which specifies the length in bits of the given field. */
20158 static inline void
20159 add_bit_size_attribute (dw_die_ref die, tree decl)
20161 /* Must be a field and a bit field. */
20162 gcc_assert (TREE_CODE (decl) == FIELD_DECL
20163 && DECL_BIT_FIELD_TYPE (decl));
20165 if (tree_fits_uhwi_p (DECL_SIZE (decl)))
20166 add_AT_unsigned (die, DW_AT_bit_size, tree_to_uhwi (DECL_SIZE (decl)));
20169 /* If the compiled language is ANSI C, then add a 'prototyped'
20170 attribute, if arg types are given for the parameters of a function. */
20172 static inline void
20173 add_prototyped_attribute (dw_die_ref die, tree func_type)
20175 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
20177 case DW_LANG_C:
20178 case DW_LANG_C89:
20179 case DW_LANG_C99:
20180 case DW_LANG_C11:
20181 case DW_LANG_ObjC:
20182 if (prototype_p (func_type))
20183 add_AT_flag (die, DW_AT_prototyped, 1);
20184 break;
20185 default:
20186 break;
20190 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
20191 by looking in the type declaration, the object declaration equate table or
20192 the block mapping. */
20194 static inline dw_die_ref
20195 add_abstract_origin_attribute (dw_die_ref die, tree origin)
20197 dw_die_ref origin_die = NULL;
20199 if (TREE_CODE (origin) != FUNCTION_DECL
20200 && TREE_CODE (origin) != BLOCK)
20202 /* We may have gotten separated from the block for the inlined
20203 function, if we're in an exception handler or some such; make
20204 sure that the abstract function has been written out.
20206 Doing this for nested functions is wrong, however; functions are
20207 distinct units, and our context might not even be inline. */
20208 tree fn = origin;
20210 if (TYPE_P (fn))
20211 fn = TYPE_STUB_DECL (fn);
20213 fn = decl_function_context (fn);
20214 if (fn)
20215 dwarf2out_abstract_function (fn);
20218 if (DECL_P (origin))
20219 origin_die = lookup_decl_die (origin);
20220 else if (TYPE_P (origin))
20221 origin_die = lookup_type_die (origin);
20222 else if (TREE_CODE (origin) == BLOCK)
20223 origin_die = BLOCK_DIE (origin);
20225 /* XXX: Functions that are never lowered don't always have correct block
20226 trees (in the case of java, they simply have no block tree, in some other
20227 languages). For these functions, there is nothing we can really do to
20228 output correct debug info for inlined functions in all cases. Rather
20229 than die, we'll just produce deficient debug info now, in that we will
20230 have variables without a proper abstract origin. In the future, when all
20231 functions are lowered, we should re-add a gcc_assert (origin_die)
20232 here. */
20234 if (origin_die)
20235 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
20236 return origin_die;
20239 /* We do not currently support the pure_virtual attribute. */
20241 static inline void
20242 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
20244 if (DECL_VINDEX (func_decl))
20246 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
20248 if (tree_fits_shwi_p (DECL_VINDEX (func_decl)))
20249 add_AT_loc (die, DW_AT_vtable_elem_location,
20250 new_loc_descr (DW_OP_constu,
20251 tree_to_shwi (DECL_VINDEX (func_decl)),
20252 0));
20254 /* GNU extension: Record what type this method came from originally. */
20255 if (debug_info_level > DINFO_LEVEL_TERSE
20256 && DECL_CONTEXT (func_decl))
20257 add_AT_die_ref (die, DW_AT_containing_type,
20258 lookup_type_die (DECL_CONTEXT (func_decl)));
20262 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
20263 given decl. This used to be a vendor extension until after DWARF 4
20264 standardized it. */
20266 static void
20267 add_linkage_attr (dw_die_ref die, tree decl)
20269 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
20271 /* Mimic what assemble_name_raw does with a leading '*'. */
20272 if (name[0] == '*')
20273 name = &name[1];
20275 if (dwarf_version >= 4)
20276 add_AT_string (die, DW_AT_linkage_name, name);
20277 else
20278 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
20281 /* Add source coordinate attributes for the given decl. */
20283 static void
20284 add_src_coords_attributes (dw_die_ref die, tree decl)
20286 expanded_location s;
20288 if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
20289 return;
20290 s = expand_location (DECL_SOURCE_LOCATION (decl));
20291 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
20292 add_AT_unsigned (die, DW_AT_decl_line, s.line);
20293 if (debug_column_info && s.column)
20294 add_AT_unsigned (die, DW_AT_decl_column, s.column);
20297 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
20299 static void
20300 add_linkage_name_raw (dw_die_ref die, tree decl)
20302 /* Defer until we have an assembler name set. */
20303 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
20305 limbo_die_node *asm_name;
20307 asm_name = ggc_cleared_alloc<limbo_die_node> ();
20308 asm_name->die = die;
20309 asm_name->created_for = decl;
20310 asm_name->next = deferred_asm_name;
20311 deferred_asm_name = asm_name;
20313 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
20314 add_linkage_attr (die, decl);
20317 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl if desired. */
20319 static void
20320 add_linkage_name (dw_die_ref die, tree decl)
20322 if (debug_info_level > DINFO_LEVEL_NONE
20323 && VAR_OR_FUNCTION_DECL_P (decl)
20324 && TREE_PUBLIC (decl)
20325 && !(VAR_P (decl) && DECL_REGISTER (decl))
20326 && die->die_tag != DW_TAG_member)
20327 add_linkage_name_raw (die, decl);
20330 /* Add a DW_AT_name attribute and source coordinate attribute for the
20331 given decl, but only if it actually has a name. */
20333 static void
20334 add_name_and_src_coords_attributes (dw_die_ref die, tree decl,
20335 bool no_linkage_name)
20337 tree decl_name;
20339 decl_name = DECL_NAME (decl);
20340 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
20342 const char *name = dwarf2_name (decl, 0);
20343 if (name)
20344 add_name_attribute (die, name);
20345 if (! DECL_ARTIFICIAL (decl))
20346 add_src_coords_attributes (die, decl);
20348 if (!no_linkage_name)
20349 add_linkage_name (die, decl);
20352 #ifdef VMS_DEBUGGING_INFO
20353 /* Get the function's name, as described by its RTL. This may be different
20354 from the DECL_NAME name used in the source file. */
20355 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
20357 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
20358 XEXP (DECL_RTL (decl), 0), false);
20359 vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
20361 #endif /* VMS_DEBUGGING_INFO */
20364 /* Add VALUE as a DW_AT_discr_value attribute to DIE. */
20366 static void
20367 add_discr_value (dw_die_ref die, dw_discr_value *value)
20369 dw_attr_node attr;
20371 attr.dw_attr = DW_AT_discr_value;
20372 attr.dw_attr_val.val_class = dw_val_class_discr_value;
20373 attr.dw_attr_val.val_entry = NULL;
20374 attr.dw_attr_val.v.val_discr_value.pos = value->pos;
20375 if (value->pos)
20376 attr.dw_attr_val.v.val_discr_value.v.uval = value->v.uval;
20377 else
20378 attr.dw_attr_val.v.val_discr_value.v.sval = value->v.sval;
20379 add_dwarf_attr (die, &attr);
20382 /* Add DISCR_LIST as a DW_AT_discr_list to DIE. */
20384 static void
20385 add_discr_list (dw_die_ref die, dw_discr_list_ref discr_list)
20387 dw_attr_node attr;
20389 attr.dw_attr = DW_AT_discr_list;
20390 attr.dw_attr_val.val_class = dw_val_class_discr_list;
20391 attr.dw_attr_val.val_entry = NULL;
20392 attr.dw_attr_val.v.val_discr_list = discr_list;
20393 add_dwarf_attr (die, &attr);
20396 static inline dw_discr_list_ref
20397 AT_discr_list (dw_attr_node *attr)
20399 return attr->dw_attr_val.v.val_discr_list;
20402 #ifdef VMS_DEBUGGING_INFO
20403 /* Output the debug main pointer die for VMS */
20405 void
20406 dwarf2out_vms_debug_main_pointer (void)
20408 char label[MAX_ARTIFICIAL_LABEL_BYTES];
20409 dw_die_ref die;
20411 /* Allocate the VMS debug main subprogram die. */
20412 die = ggc_cleared_alloc<die_node> ();
20413 die->die_tag = DW_TAG_subprogram;
20414 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
20415 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
20416 current_function_funcdef_no);
20417 add_AT_lbl_id (die, DW_AT_entry_pc, label);
20419 /* Make it the first child of comp_unit_die (). */
20420 die->die_parent = comp_unit_die ();
20421 if (comp_unit_die ()->die_child)
20423 die->die_sib = comp_unit_die ()->die_child->die_sib;
20424 comp_unit_die ()->die_child->die_sib = die;
20426 else
20428 die->die_sib = die;
20429 comp_unit_die ()->die_child = die;
20432 #endif /* VMS_DEBUGGING_INFO */
20434 /* Push a new declaration scope. */
20436 static void
20437 push_decl_scope (tree scope)
20439 vec_safe_push (decl_scope_table, scope);
20442 /* Pop a declaration scope. */
20444 static inline void
20445 pop_decl_scope (void)
20447 decl_scope_table->pop ();
20450 /* walk_tree helper function for uses_local_type, below. */
20452 static tree
20453 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
20455 if (!TYPE_P (*tp))
20456 *walk_subtrees = 0;
20457 else
20459 tree name = TYPE_NAME (*tp);
20460 if (name && DECL_P (name) && decl_function_context (name))
20461 return *tp;
20463 return NULL_TREE;
20466 /* If TYPE involves a function-local type (including a local typedef to a
20467 non-local type), returns that type; otherwise returns NULL_TREE. */
20469 static tree
20470 uses_local_type (tree type)
20472 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
20473 return used;
20476 /* Return the DIE for the scope that immediately contains this type.
20477 Non-named types that do not involve a function-local type get global
20478 scope. Named types nested in namespaces or other types get their
20479 containing scope. All other types (i.e. function-local named types) get
20480 the current active scope. */
20482 static dw_die_ref
20483 scope_die_for (tree t, dw_die_ref context_die)
20485 dw_die_ref scope_die = NULL;
20486 tree containing_scope;
20488 /* Non-types always go in the current scope. */
20489 gcc_assert (TYPE_P (t));
20491 /* Use the scope of the typedef, rather than the scope of the type
20492 it refers to. */
20493 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
20494 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
20495 else
20496 containing_scope = TYPE_CONTEXT (t);
20498 /* Use the containing namespace if there is one. */
20499 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
20501 if (context_die == lookup_decl_die (containing_scope))
20502 /* OK */;
20503 else if (debug_info_level > DINFO_LEVEL_TERSE)
20504 context_die = get_context_die (containing_scope);
20505 else
20506 containing_scope = NULL_TREE;
20509 /* Ignore function type "scopes" from the C frontend. They mean that
20510 a tagged type is local to a parmlist of a function declarator, but
20511 that isn't useful to DWARF. */
20512 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
20513 containing_scope = NULL_TREE;
20515 if (SCOPE_FILE_SCOPE_P (containing_scope))
20517 /* If T uses a local type keep it local as well, to avoid references
20518 to function-local DIEs from outside the function. */
20519 if (current_function_decl && uses_local_type (t))
20520 scope_die = context_die;
20521 else
20522 scope_die = comp_unit_die ();
20524 else if (TYPE_P (containing_scope))
20526 /* For types, we can just look up the appropriate DIE. */
20527 if (debug_info_level > DINFO_LEVEL_TERSE)
20528 scope_die = get_context_die (containing_scope);
20529 else
20531 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
20532 if (scope_die == NULL)
20533 scope_die = comp_unit_die ();
20536 else
20537 scope_die = context_die;
20539 return scope_die;
20542 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
20544 static inline int
20545 local_scope_p (dw_die_ref context_die)
20547 for (; context_die; context_die = context_die->die_parent)
20548 if (context_die->die_tag == DW_TAG_inlined_subroutine
20549 || context_die->die_tag == DW_TAG_subprogram)
20550 return 1;
20552 return 0;
20555 /* Returns nonzero if CONTEXT_DIE is a class. */
20557 static inline int
20558 class_scope_p (dw_die_ref context_die)
20560 return (context_die
20561 && (context_die->die_tag == DW_TAG_structure_type
20562 || context_die->die_tag == DW_TAG_class_type
20563 || context_die->die_tag == DW_TAG_interface_type
20564 || context_die->die_tag == DW_TAG_union_type));
20567 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
20568 whether or not to treat a DIE in this context as a declaration. */
20570 static inline int
20571 class_or_namespace_scope_p (dw_die_ref context_die)
20573 return (class_scope_p (context_die)
20574 || (context_die && context_die->die_tag == DW_TAG_namespace));
20577 /* Many forms of DIEs require a "type description" attribute. This
20578 routine locates the proper "type descriptor" die for the type given
20579 by 'type' plus any additional qualifiers given by 'cv_quals', and
20580 adds a DW_AT_type attribute below the given die. */
20582 static void
20583 add_type_attribute (dw_die_ref object_die, tree type, int cv_quals,
20584 bool reverse, dw_die_ref context_die)
20586 enum tree_code code = TREE_CODE (type);
20587 dw_die_ref type_die = NULL;
20589 /* ??? If this type is an unnamed subrange type of an integral, floating-point
20590 or fixed-point type, use the inner type. This is because we have no
20591 support for unnamed types in base_type_die. This can happen if this is
20592 an Ada subrange type. Correct solution is emit a subrange type die. */
20593 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
20594 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
20595 type = TREE_TYPE (type), code = TREE_CODE (type);
20597 if (code == ERROR_MARK
20598 /* Handle a special case. For functions whose return type is void, we
20599 generate *no* type attribute. (Note that no object may have type
20600 `void', so this only applies to function return types). */
20601 || code == VOID_TYPE)
20602 return;
20604 type_die = modified_type_die (type,
20605 cv_quals | TYPE_QUALS_NO_ADDR_SPACE (type),
20606 reverse,
20607 context_die);
20609 if (type_die != NULL)
20610 add_AT_die_ref (object_die, DW_AT_type, type_die);
20613 /* Given an object die, add the calling convention attribute for the
20614 function call type. */
20615 static void
20616 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
20618 enum dwarf_calling_convention value = DW_CC_normal;
20620 value = ((enum dwarf_calling_convention)
20621 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
20623 if (is_fortran ()
20624 && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
20626 /* DWARF 2 doesn't provide a way to identify a program's source-level
20627 entry point. DW_AT_calling_convention attributes are only meant
20628 to describe functions' calling conventions. However, lacking a
20629 better way to signal the Fortran main program, we used this for
20630 a long time, following existing custom. Now, DWARF 4 has
20631 DW_AT_main_subprogram, which we add below, but some tools still
20632 rely on the old way, which we thus keep. */
20633 value = DW_CC_program;
20635 if (dwarf_version >= 4 || !dwarf_strict)
20636 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
20639 /* Only add the attribute if the backend requests it, and
20640 is not DW_CC_normal. */
20641 if (value && (value != DW_CC_normal))
20642 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
20645 /* Given a tree pointer to a struct, class, union, or enum type node, return
20646 a pointer to the (string) tag name for the given type, or zero if the type
20647 was declared without a tag. */
20649 static const char *
20650 type_tag (const_tree type)
20652 const char *name = 0;
20654 if (TYPE_NAME (type) != 0)
20656 tree t = 0;
20658 /* Find the IDENTIFIER_NODE for the type name. */
20659 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
20660 && !TYPE_NAMELESS (type))
20661 t = TYPE_NAME (type);
20663 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
20664 a TYPE_DECL node, regardless of whether or not a `typedef' was
20665 involved. */
20666 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
20667 && ! DECL_IGNORED_P (TYPE_NAME (type)))
20669 /* We want to be extra verbose. Don't call dwarf_name if
20670 DECL_NAME isn't set. The default hook for decl_printable_name
20671 doesn't like that, and in this context it's correct to return
20672 0, instead of "<anonymous>" or the like. */
20673 if (DECL_NAME (TYPE_NAME (type))
20674 && !DECL_NAMELESS (TYPE_NAME (type)))
20675 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
20678 /* Now get the name as a string, or invent one. */
20679 if (!name && t != 0)
20680 name = IDENTIFIER_POINTER (t);
20683 return (name == 0 || *name == '\0') ? 0 : name;
20686 /* Return the type associated with a data member, make a special check
20687 for bit field types. */
20689 static inline tree
20690 member_declared_type (const_tree member)
20692 return (DECL_BIT_FIELD_TYPE (member)
20693 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
20696 /* Get the decl's label, as described by its RTL. This may be different
20697 from the DECL_NAME name used in the source file. */
20699 #if 0
20700 static const char *
20701 decl_start_label (tree decl)
20703 rtx x;
20704 const char *fnname;
20706 x = DECL_RTL (decl);
20707 gcc_assert (MEM_P (x));
20709 x = XEXP (x, 0);
20710 gcc_assert (GET_CODE (x) == SYMBOL_REF);
20712 fnname = XSTR (x, 0);
20713 return fnname;
20715 #endif
20717 /* For variable-length arrays that have been previously generated, but
20718 may be incomplete due to missing subscript info, fill the subscript
20719 info. Return TRUE if this is one of those cases. */
20720 static bool
20721 fill_variable_array_bounds (tree type)
20723 if (TREE_ASM_WRITTEN (type)
20724 && TREE_CODE (type) == ARRAY_TYPE
20725 && variably_modified_type_p (type, NULL))
20727 dw_die_ref array_die = lookup_type_die (type);
20728 if (!array_die)
20729 return false;
20730 add_subscript_info (array_die, type, !is_ada ());
20731 return true;
20733 return false;
20736 /* These routines generate the internal representation of the DIE's for
20737 the compilation unit. Debugging information is collected by walking
20738 the declaration trees passed in from dwarf2out_decl(). */
20740 static void
20741 gen_array_type_die (tree type, dw_die_ref context_die)
20743 dw_die_ref array_die;
20745 /* GNU compilers represent multidimensional array types as sequences of one
20746 dimensional array types whose element types are themselves array types.
20747 We sometimes squish that down to a single array_type DIE with multiple
20748 subscripts in the Dwarf debugging info. The draft Dwarf specification
20749 say that we are allowed to do this kind of compression in C, because
20750 there is no difference between an array of arrays and a multidimensional
20751 array. We don't do this for Ada to remain as close as possible to the
20752 actual representation, which is especially important against the language
20753 flexibilty wrt arrays of variable size. */
20755 bool collapse_nested_arrays = !is_ada ();
20757 if (fill_variable_array_bounds (type))
20758 return;
20760 dw_die_ref scope_die = scope_die_for (type, context_die);
20761 tree element_type;
20763 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
20764 DW_TAG_string_type doesn't have DW_AT_type attribute). */
20765 if (TYPE_STRING_FLAG (type)
20766 && TREE_CODE (type) == ARRAY_TYPE
20767 && is_fortran ()
20768 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
20770 HOST_WIDE_INT size;
20772 array_die = new_die (DW_TAG_string_type, scope_die, type);
20773 add_name_attribute (array_die, type_tag (type));
20774 equate_type_number_to_die (type, array_die);
20775 size = int_size_in_bytes (type);
20776 if (size >= 0)
20777 add_AT_unsigned (array_die, DW_AT_byte_size, size);
20778 else if (TYPE_DOMAIN (type) != NULL_TREE
20779 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE)
20781 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
20782 tree rszdecl = szdecl;
20784 size = int_size_in_bytes (TREE_TYPE (szdecl));
20785 if (!DECL_P (szdecl))
20787 if (TREE_CODE (szdecl) == INDIRECT_REF
20788 && DECL_P (TREE_OPERAND (szdecl, 0)))
20790 rszdecl = TREE_OPERAND (szdecl, 0);
20791 if (int_size_in_bytes (TREE_TYPE (rszdecl))
20792 != DWARF2_ADDR_SIZE)
20793 size = 0;
20795 else
20796 size = 0;
20798 if (size > 0)
20800 dw_loc_list_ref loc
20801 = loc_list_from_tree (rszdecl, szdecl == rszdecl ? 2 : 0,
20802 NULL);
20803 if (loc)
20805 add_AT_location_description (array_die, DW_AT_string_length,
20806 loc);
20807 if (size != DWARF2_ADDR_SIZE)
20808 add_AT_unsigned (array_die, dwarf_version >= 5
20809 ? DW_AT_string_length_byte_size
20810 : DW_AT_byte_size, size);
20814 return;
20817 array_die = new_die (DW_TAG_array_type, scope_die, type);
20818 add_name_attribute (array_die, type_tag (type));
20819 equate_type_number_to_die (type, array_die);
20821 if (TREE_CODE (type) == VECTOR_TYPE)
20822 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
20824 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
20825 if (is_fortran ()
20826 && TREE_CODE (type) == ARRAY_TYPE
20827 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
20828 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
20829 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
20831 #if 0
20832 /* We default the array ordering. SDB will probably do
20833 the right things even if DW_AT_ordering is not present. It's not even
20834 an issue until we start to get into multidimensional arrays anyway. If
20835 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
20836 then we'll have to put the DW_AT_ordering attribute back in. (But if
20837 and when we find out that we need to put these in, we will only do so
20838 for multidimensional arrays. */
20839 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
20840 #endif
20842 if (TREE_CODE (type) == VECTOR_TYPE)
20844 /* For VECTOR_TYPEs we use an array die with appropriate bounds. */
20845 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
20846 add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node, NULL);
20847 add_bound_info (subrange_die, DW_AT_upper_bound,
20848 size_int (TYPE_VECTOR_SUBPARTS (type) - 1), NULL);
20850 else
20851 add_subscript_info (array_die, type, collapse_nested_arrays);
20853 /* Add representation of the type of the elements of this array type and
20854 emit the corresponding DIE if we haven't done it already. */
20855 element_type = TREE_TYPE (type);
20856 if (collapse_nested_arrays)
20857 while (TREE_CODE (element_type) == ARRAY_TYPE)
20859 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
20860 break;
20861 element_type = TREE_TYPE (element_type);
20864 add_type_attribute (array_die, element_type, TYPE_UNQUALIFIED,
20865 TREE_CODE (type) == ARRAY_TYPE
20866 && TYPE_REVERSE_STORAGE_ORDER (type),
20867 context_die);
20869 add_gnat_descriptive_type_attribute (array_die, type, context_die);
20870 if (TYPE_ARTIFICIAL (type))
20871 add_AT_flag (array_die, DW_AT_artificial, 1);
20873 if (get_AT (array_die, DW_AT_name))
20874 add_pubtype (type, array_die);
20876 add_alignment_attribute (array_die, type);
20879 /* This routine generates DIE for array with hidden descriptor, details
20880 are filled into *info by a langhook. */
20882 static void
20883 gen_descr_array_type_die (tree type, struct array_descr_info *info,
20884 dw_die_ref context_die)
20886 const dw_die_ref scope_die = scope_die_for (type, context_die);
20887 const dw_die_ref array_die = new_die (DW_TAG_array_type, scope_die, type);
20888 struct loc_descr_context context = { type, info->base_decl, NULL,
20889 false, false };
20890 enum dwarf_tag subrange_tag = DW_TAG_subrange_type;
20891 int dim;
20893 add_name_attribute (array_die, type_tag (type));
20894 equate_type_number_to_die (type, array_die);
20896 if (info->ndimensions > 1)
20897 switch (info->ordering)
20899 case array_descr_ordering_row_major:
20900 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
20901 break;
20902 case array_descr_ordering_column_major:
20903 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
20904 break;
20905 default:
20906 break;
20909 if (dwarf_version >= 3 || !dwarf_strict)
20911 if (info->data_location)
20912 add_scalar_info (array_die, DW_AT_data_location, info->data_location,
20913 dw_scalar_form_exprloc, &context);
20914 if (info->associated)
20915 add_scalar_info (array_die, DW_AT_associated, info->associated,
20916 dw_scalar_form_constant
20917 | dw_scalar_form_exprloc
20918 | dw_scalar_form_reference, &context);
20919 if (info->allocated)
20920 add_scalar_info (array_die, DW_AT_allocated, info->allocated,
20921 dw_scalar_form_constant
20922 | dw_scalar_form_exprloc
20923 | dw_scalar_form_reference, &context);
20924 if (info->stride)
20926 const enum dwarf_attribute attr
20927 = (info->stride_in_bits) ? DW_AT_bit_stride : DW_AT_byte_stride;
20928 const int forms
20929 = (info->stride_in_bits)
20930 ? dw_scalar_form_constant
20931 : (dw_scalar_form_constant
20932 | dw_scalar_form_exprloc
20933 | dw_scalar_form_reference);
20935 add_scalar_info (array_die, attr, info->stride, forms, &context);
20938 if (dwarf_version >= 5)
20940 if (info->rank)
20942 add_scalar_info (array_die, DW_AT_rank, info->rank,
20943 dw_scalar_form_constant
20944 | dw_scalar_form_exprloc, &context);
20945 subrange_tag = DW_TAG_generic_subrange;
20946 context.placeholder_arg = true;
20950 add_gnat_descriptive_type_attribute (array_die, type, context_die);
20952 for (dim = 0; dim < info->ndimensions; dim++)
20954 dw_die_ref subrange_die = new_die (subrange_tag, array_die, NULL);
20956 if (info->dimen[dim].bounds_type)
20957 add_type_attribute (subrange_die,
20958 info->dimen[dim].bounds_type, TYPE_UNQUALIFIED,
20959 false, context_die);
20960 if (info->dimen[dim].lower_bound)
20961 add_bound_info (subrange_die, DW_AT_lower_bound,
20962 info->dimen[dim].lower_bound, &context);
20963 if (info->dimen[dim].upper_bound)
20964 add_bound_info (subrange_die, DW_AT_upper_bound,
20965 info->dimen[dim].upper_bound, &context);
20966 if ((dwarf_version >= 3 || !dwarf_strict) && info->dimen[dim].stride)
20967 add_scalar_info (subrange_die, DW_AT_byte_stride,
20968 info->dimen[dim].stride,
20969 dw_scalar_form_constant
20970 | dw_scalar_form_exprloc
20971 | dw_scalar_form_reference,
20972 &context);
20975 gen_type_die (info->element_type, context_die);
20976 add_type_attribute (array_die, info->element_type, TYPE_UNQUALIFIED,
20977 TREE_CODE (type) == ARRAY_TYPE
20978 && TYPE_REVERSE_STORAGE_ORDER (type),
20979 context_die);
20981 if (get_AT (array_die, DW_AT_name))
20982 add_pubtype (type, array_die);
20984 add_alignment_attribute (array_die, type);
20987 #if 0
20988 static void
20989 gen_entry_point_die (tree decl, dw_die_ref context_die)
20991 tree origin = decl_ultimate_origin (decl);
20992 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
20994 if (origin != NULL)
20995 add_abstract_origin_attribute (decl_die, origin);
20996 else
20998 add_name_and_src_coords_attributes (decl_die, decl);
20999 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
21000 TYPE_UNQUALIFIED, false, context_die);
21003 if (DECL_ABSTRACT_P (decl))
21004 equate_decl_number_to_die (decl, decl_die);
21005 else
21006 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
21008 #endif
21010 /* Walk through the list of incomplete types again, trying once more to
21011 emit full debugging info for them. */
21013 static void
21014 retry_incomplete_types (void)
21016 set_early_dwarf s;
21017 int i;
21019 for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
21020 if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
21021 gen_type_die ((*incomplete_types)[i], comp_unit_die ());
21022 vec_safe_truncate (incomplete_types, 0);
21025 /* Determine what tag to use for a record type. */
21027 static enum dwarf_tag
21028 record_type_tag (tree type)
21030 if (! lang_hooks.types.classify_record)
21031 return DW_TAG_structure_type;
21033 switch (lang_hooks.types.classify_record (type))
21035 case RECORD_IS_STRUCT:
21036 return DW_TAG_structure_type;
21038 case RECORD_IS_CLASS:
21039 return DW_TAG_class_type;
21041 case RECORD_IS_INTERFACE:
21042 if (dwarf_version >= 3 || !dwarf_strict)
21043 return DW_TAG_interface_type;
21044 return DW_TAG_structure_type;
21046 default:
21047 gcc_unreachable ();
21051 /* Generate a DIE to represent an enumeration type. Note that these DIEs
21052 include all of the information about the enumeration values also. Each
21053 enumerated type name/value is listed as a child of the enumerated type
21054 DIE. */
21056 static dw_die_ref
21057 gen_enumeration_type_die (tree type, dw_die_ref context_die)
21059 dw_die_ref type_die = lookup_type_die (type);
21061 if (type_die == NULL)
21063 type_die = new_die (DW_TAG_enumeration_type,
21064 scope_die_for (type, context_die), type);
21065 equate_type_number_to_die (type, type_die);
21066 add_name_attribute (type_die, type_tag (type));
21067 if (dwarf_version >= 4 || !dwarf_strict)
21069 if (ENUM_IS_SCOPED (type))
21070 add_AT_flag (type_die, DW_AT_enum_class, 1);
21071 if (ENUM_IS_OPAQUE (type))
21072 add_AT_flag (type_die, DW_AT_declaration, 1);
21074 if (!dwarf_strict)
21075 add_AT_unsigned (type_die, DW_AT_encoding,
21076 TYPE_UNSIGNED (type)
21077 ? DW_ATE_unsigned
21078 : DW_ATE_signed);
21080 else if (! TYPE_SIZE (type))
21081 return type_die;
21082 else
21083 remove_AT (type_die, DW_AT_declaration);
21085 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
21086 given enum type is incomplete, do not generate the DW_AT_byte_size
21087 attribute or the DW_AT_element_list attribute. */
21088 if (TYPE_SIZE (type))
21090 tree link;
21092 TREE_ASM_WRITTEN (type) = 1;
21093 add_byte_size_attribute (type_die, type);
21094 add_alignment_attribute (type_die, type);
21095 if (dwarf_version >= 3 || !dwarf_strict)
21097 tree underlying = lang_hooks.types.enum_underlying_base_type (type);
21098 add_type_attribute (type_die, underlying, TYPE_UNQUALIFIED, false,
21099 context_die);
21101 if (TYPE_STUB_DECL (type) != NULL_TREE)
21103 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
21104 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
21107 /* If the first reference to this type was as the return type of an
21108 inline function, then it may not have a parent. Fix this now. */
21109 if (type_die->die_parent == NULL)
21110 add_child_die (scope_die_for (type, context_die), type_die);
21112 for (link = TYPE_VALUES (type);
21113 link != NULL; link = TREE_CHAIN (link))
21115 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
21116 tree value = TREE_VALUE (link);
21118 add_name_attribute (enum_die,
21119 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
21121 if (TREE_CODE (value) == CONST_DECL)
21122 value = DECL_INITIAL (value);
21124 if (simple_type_size_in_bits (TREE_TYPE (value))
21125 <= HOST_BITS_PER_WIDE_INT || tree_fits_shwi_p (value))
21127 /* For constant forms created by add_AT_unsigned DWARF
21128 consumers (GDB, elfutils, etc.) always zero extend
21129 the value. Only when the actual value is negative
21130 do we need to use add_AT_int to generate a constant
21131 form that can represent negative values. */
21132 HOST_WIDE_INT val = TREE_INT_CST_LOW (value);
21133 if (TYPE_UNSIGNED (TREE_TYPE (value)) || val >= 0)
21134 add_AT_unsigned (enum_die, DW_AT_const_value,
21135 (unsigned HOST_WIDE_INT) val);
21136 else
21137 add_AT_int (enum_die, DW_AT_const_value, val);
21139 else
21140 /* Enumeration constants may be wider than HOST_WIDE_INT. Handle
21141 that here. TODO: This should be re-worked to use correct
21142 signed/unsigned double tags for all cases. */
21143 add_AT_wide (enum_die, DW_AT_const_value, value);
21146 add_gnat_descriptive_type_attribute (type_die, type, context_die);
21147 if (TYPE_ARTIFICIAL (type))
21148 add_AT_flag (type_die, DW_AT_artificial, 1);
21150 else
21151 add_AT_flag (type_die, DW_AT_declaration, 1);
21153 add_alignment_attribute (type_die, type);
21155 add_pubtype (type, type_die);
21157 return type_die;
21160 /* Generate a DIE to represent either a real live formal parameter decl or to
21161 represent just the type of some formal parameter position in some function
21162 type.
21164 Note that this routine is a bit unusual because its argument may be a
21165 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
21166 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
21167 node. If it's the former then this function is being called to output a
21168 DIE to represent a formal parameter object (or some inlining thereof). If
21169 it's the latter, then this function is only being called to output a
21170 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
21171 argument type of some subprogram type.
21172 If EMIT_NAME_P is true, name and source coordinate attributes
21173 are emitted. */
21175 static dw_die_ref
21176 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
21177 dw_die_ref context_die)
21179 tree node_or_origin = node ? node : origin;
21180 tree ultimate_origin;
21181 dw_die_ref parm_die = NULL;
21183 if (TREE_CODE_CLASS (TREE_CODE (node_or_origin)) == tcc_declaration)
21185 parm_die = lookup_decl_die (node);
21187 /* If the contexts differ, we may not be talking about the same
21188 thing. */
21189 if (parm_die && parm_die->die_parent != context_die)
21191 if (!DECL_ABSTRACT_P (node))
21193 /* This can happen when creating an inlined instance, in
21194 which case we need to create a new DIE that will get
21195 annotated with DW_AT_abstract_origin. */
21196 parm_die = NULL;
21198 else
21200 /* FIXME: Reuse DIE even with a differing context.
21202 This can happen when calling
21203 dwarf2out_abstract_function to build debug info for
21204 the abstract instance of a function for which we have
21205 already generated a DIE in
21206 dwarf2out_early_global_decl.
21208 Once we remove dwarf2out_abstract_function, we should
21209 have a call to gcc_unreachable here. */
21213 if (parm_die && parm_die->die_parent == NULL)
21215 /* Check that parm_die already has the right attributes that
21216 we would have added below. If any attributes are
21217 missing, fall through to add them. */
21218 if (! DECL_ABSTRACT_P (node_or_origin)
21219 && !get_AT (parm_die, DW_AT_location)
21220 && !get_AT (parm_die, DW_AT_const_value))
21221 /* We are missing location info, and are about to add it. */
21223 else
21225 add_child_die (context_die, parm_die);
21226 return parm_die;
21231 /* If we have a previously generated DIE, use it, unless this is an
21232 concrete instance (origin != NULL), in which case we need a new
21233 DIE with a corresponding DW_AT_abstract_origin. */
21234 bool reusing_die;
21235 if (parm_die && origin == NULL)
21236 reusing_die = true;
21237 else
21239 parm_die = new_die (DW_TAG_formal_parameter, context_die, node);
21240 reusing_die = false;
21243 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
21245 case tcc_declaration:
21246 ultimate_origin = decl_ultimate_origin (node_or_origin);
21247 if (node || ultimate_origin)
21248 origin = ultimate_origin;
21250 if (reusing_die)
21251 goto add_location;
21253 if (origin != NULL)
21254 add_abstract_origin_attribute (parm_die, origin);
21255 else if (emit_name_p)
21256 add_name_and_src_coords_attributes (parm_die, node);
21257 if (origin == NULL
21258 || (! DECL_ABSTRACT_P (node_or_origin)
21259 && variably_modified_type_p (TREE_TYPE (node_or_origin),
21260 decl_function_context
21261 (node_or_origin))))
21263 tree type = TREE_TYPE (node_or_origin);
21264 if (decl_by_reference_p (node_or_origin))
21265 add_type_attribute (parm_die, TREE_TYPE (type),
21266 TYPE_UNQUALIFIED,
21267 false, context_die);
21268 else
21269 add_type_attribute (parm_die, type,
21270 decl_quals (node_or_origin),
21271 false, context_die);
21273 if (origin == NULL && DECL_ARTIFICIAL (node))
21274 add_AT_flag (parm_die, DW_AT_artificial, 1);
21275 add_location:
21276 if (node && node != origin)
21277 equate_decl_number_to_die (node, parm_die);
21278 if (! DECL_ABSTRACT_P (node_or_origin))
21279 add_location_or_const_value_attribute (parm_die, node_or_origin,
21280 node == NULL);
21282 break;
21284 case tcc_type:
21285 /* We were called with some kind of a ..._TYPE node. */
21286 add_type_attribute (parm_die, node_or_origin, TYPE_UNQUALIFIED, false,
21287 context_die);
21288 break;
21290 default:
21291 gcc_unreachable ();
21294 return parm_die;
21297 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
21298 children DW_TAG_formal_parameter DIEs representing the arguments of the
21299 parameter pack.
21301 PARM_PACK must be a function parameter pack.
21302 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
21303 must point to the subsequent arguments of the function PACK_ARG belongs to.
21304 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
21305 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
21306 following the last one for which a DIE was generated. */
21308 static dw_die_ref
21309 gen_formal_parameter_pack_die (tree parm_pack,
21310 tree pack_arg,
21311 dw_die_ref subr_die,
21312 tree *next_arg)
21314 tree arg;
21315 dw_die_ref parm_pack_die;
21317 gcc_assert (parm_pack
21318 && lang_hooks.function_parameter_pack_p (parm_pack)
21319 && subr_die);
21321 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
21322 add_src_coords_attributes (parm_pack_die, parm_pack);
21324 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
21326 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
21327 parm_pack))
21328 break;
21329 gen_formal_parameter_die (arg, NULL,
21330 false /* Don't emit name attribute. */,
21331 parm_pack_die);
21333 if (next_arg)
21334 *next_arg = arg;
21335 return parm_pack_die;
21338 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
21339 at the end of an (ANSI prototyped) formal parameters list. */
21341 static void
21342 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
21344 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
21347 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
21348 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
21349 parameters as specified in some function type specification (except for
21350 those which appear as part of a function *definition*). */
21352 static void
21353 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
21355 tree link;
21356 tree formal_type = NULL;
21357 tree first_parm_type;
21358 tree arg;
21360 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
21362 arg = DECL_ARGUMENTS (function_or_method_type);
21363 function_or_method_type = TREE_TYPE (function_or_method_type);
21365 else
21366 arg = NULL_TREE;
21368 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
21370 /* Make our first pass over the list of formal parameter types and output a
21371 DW_TAG_formal_parameter DIE for each one. */
21372 for (link = first_parm_type; link; )
21374 dw_die_ref parm_die;
21376 formal_type = TREE_VALUE (link);
21377 if (formal_type == void_type_node)
21378 break;
21380 /* Output a (nameless) DIE to represent the formal parameter itself. */
21381 if (!POINTER_BOUNDS_TYPE_P (formal_type))
21383 parm_die = gen_formal_parameter_die (formal_type, NULL,
21384 true /* Emit name attribute. */,
21385 context_die);
21386 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
21387 && link == first_parm_type)
21389 add_AT_flag (parm_die, DW_AT_artificial, 1);
21390 if (dwarf_version >= 3 || !dwarf_strict)
21391 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
21393 else if (arg && DECL_ARTIFICIAL (arg))
21394 add_AT_flag (parm_die, DW_AT_artificial, 1);
21397 link = TREE_CHAIN (link);
21398 if (arg)
21399 arg = DECL_CHAIN (arg);
21402 /* If this function type has an ellipsis, add a
21403 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
21404 if (formal_type != void_type_node)
21405 gen_unspecified_parameters_die (function_or_method_type, context_die);
21407 /* Make our second (and final) pass over the list of formal parameter types
21408 and output DIEs to represent those types (as necessary). */
21409 for (link = TYPE_ARG_TYPES (function_or_method_type);
21410 link && TREE_VALUE (link);
21411 link = TREE_CHAIN (link))
21412 gen_type_die (TREE_VALUE (link), context_die);
21415 /* We want to generate the DIE for TYPE so that we can generate the
21416 die for MEMBER, which has been defined; we will need to refer back
21417 to the member declaration nested within TYPE. If we're trying to
21418 generate minimal debug info for TYPE, processing TYPE won't do the
21419 trick; we need to attach the member declaration by hand. */
21421 static void
21422 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
21424 gen_type_die (type, context_die);
21426 /* If we're trying to avoid duplicate debug info, we may not have
21427 emitted the member decl for this function. Emit it now. */
21428 if (TYPE_STUB_DECL (type)
21429 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
21430 && ! lookup_decl_die (member))
21432 dw_die_ref type_die;
21433 gcc_assert (!decl_ultimate_origin (member));
21435 push_decl_scope (type);
21436 type_die = lookup_type_die_strip_naming_typedef (type);
21437 if (TREE_CODE (member) == FUNCTION_DECL)
21438 gen_subprogram_die (member, type_die);
21439 else if (TREE_CODE (member) == FIELD_DECL)
21441 /* Ignore the nameless fields that are used to skip bits but handle
21442 C++ anonymous unions and structs. */
21443 if (DECL_NAME (member) != NULL_TREE
21444 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
21445 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
21447 struct vlr_context vlr_ctx = {
21448 DECL_CONTEXT (member), /* struct_type */
21449 NULL_TREE /* variant_part_offset */
21451 gen_type_die (member_declared_type (member), type_die);
21452 gen_field_die (member, &vlr_ctx, type_die);
21455 else
21456 gen_variable_die (member, NULL_TREE, type_die);
21458 pop_decl_scope ();
21462 /* Forward declare these functions, because they are mutually recursive
21463 with their set_block_* pairing functions. */
21464 static void set_decl_origin_self (tree);
21465 static void set_decl_abstract_flags (tree, vec<tree> &);
21467 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
21468 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
21469 that it points to the node itself, thus indicating that the node is its
21470 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
21471 the given node is NULL, recursively descend the decl/block tree which
21472 it is the root of, and for each other ..._DECL or BLOCK node contained
21473 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
21474 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
21475 values to point to themselves. */
21477 static void
21478 set_block_origin_self (tree stmt)
21480 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
21482 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
21485 tree local_decl;
21487 for (local_decl = BLOCK_VARS (stmt);
21488 local_decl != NULL_TREE;
21489 local_decl = DECL_CHAIN (local_decl))
21490 /* Do not recurse on nested functions since the inlining status
21491 of parent and child can be different as per the DWARF spec. */
21492 if (TREE_CODE (local_decl) != FUNCTION_DECL
21493 && !DECL_EXTERNAL (local_decl))
21494 set_decl_origin_self (local_decl);
21498 tree subblock;
21500 for (subblock = BLOCK_SUBBLOCKS (stmt);
21501 subblock != NULL_TREE;
21502 subblock = BLOCK_CHAIN (subblock))
21503 set_block_origin_self (subblock); /* Recurse. */
21508 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
21509 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
21510 node to so that it points to the node itself, thus indicating that the
21511 node represents its own (abstract) origin. Additionally, if the
21512 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
21513 the decl/block tree of which the given node is the root of, and for
21514 each other ..._DECL or BLOCK node contained therein whose
21515 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
21516 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
21517 point to themselves. */
21519 static void
21520 set_decl_origin_self (tree decl)
21522 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
21524 DECL_ABSTRACT_ORIGIN (decl) = decl;
21525 if (TREE_CODE (decl) == FUNCTION_DECL)
21527 tree arg;
21529 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
21530 DECL_ABSTRACT_ORIGIN (arg) = arg;
21531 if (DECL_INITIAL (decl) != NULL_TREE
21532 && DECL_INITIAL (decl) != error_mark_node)
21533 set_block_origin_self (DECL_INITIAL (decl));
21538 /* Given a pointer to some BLOCK node, set the BLOCK_ABSTRACT flag to 1
21539 and if it wasn't 1 before, push it to abstract_vec vector.
21540 For all local decls and all local sub-blocks (recursively) do it
21541 too. */
21543 static void
21544 set_block_abstract_flags (tree stmt, vec<tree> &abstract_vec)
21546 tree local_decl;
21547 tree subblock;
21548 unsigned int i;
21550 if (!BLOCK_ABSTRACT (stmt))
21552 abstract_vec.safe_push (stmt);
21553 BLOCK_ABSTRACT (stmt) = 1;
21556 for (local_decl = BLOCK_VARS (stmt);
21557 local_decl != NULL_TREE;
21558 local_decl = DECL_CHAIN (local_decl))
21559 if (! DECL_EXTERNAL (local_decl))
21560 set_decl_abstract_flags (local_decl, abstract_vec);
21562 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
21564 local_decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
21565 if ((VAR_P (local_decl) && !TREE_STATIC (local_decl))
21566 || TREE_CODE (local_decl) == PARM_DECL)
21567 set_decl_abstract_flags (local_decl, abstract_vec);
21570 for (subblock = BLOCK_SUBBLOCKS (stmt);
21571 subblock != NULL_TREE;
21572 subblock = BLOCK_CHAIN (subblock))
21573 set_block_abstract_flags (subblock, abstract_vec);
21576 /* Given a pointer to some ..._DECL node, set DECL_ABSTRACT_P flag on it
21577 to 1 and if it wasn't 1 before, push to abstract_vec vector.
21578 In the case where the decl is a FUNCTION_DECL also set the abstract
21579 flags for all of the parameters, local vars, local
21580 blocks and sub-blocks (recursively). */
21582 static void
21583 set_decl_abstract_flags (tree decl, vec<tree> &abstract_vec)
21585 if (!DECL_ABSTRACT_P (decl))
21587 abstract_vec.safe_push (decl);
21588 DECL_ABSTRACT_P (decl) = 1;
21591 if (TREE_CODE (decl) == FUNCTION_DECL)
21593 tree arg;
21595 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
21596 if (!DECL_ABSTRACT_P (arg))
21598 abstract_vec.safe_push (arg);
21599 DECL_ABSTRACT_P (arg) = 1;
21601 if (DECL_INITIAL (decl) != NULL_TREE
21602 && DECL_INITIAL (decl) != error_mark_node)
21603 set_block_abstract_flags (DECL_INITIAL (decl), abstract_vec);
21607 /* Generate the DWARF2 info for the "abstract" instance of a function which we
21608 may later generate inlined and/or out-of-line instances of.
21610 FIXME: In the early-dwarf world, this function, and most of the
21611 DECL_ABSTRACT code should be obsoleted. The early DIE _is_
21612 the abstract instance. All we would need to do is annotate
21613 the early DIE with the appropriate DW_AT_inline in late
21614 dwarf (perhaps in gen_inlined_subroutine_die).
21616 However, we can't do this yet, because LTO streaming of DIEs
21617 has not been implemented yet. */
21619 static void
21620 dwarf2out_abstract_function (tree decl)
21622 dw_die_ref old_die;
21623 tree save_fn;
21624 tree context;
21625 hash_table<decl_loc_hasher> *old_decl_loc_table;
21626 hash_table<dw_loc_list_hasher> *old_cached_dw_loc_list_table;
21627 int old_call_site_count, old_tail_call_site_count;
21628 struct call_arg_loc_node *old_call_arg_locations;
21630 /* Make sure we have the actual abstract inline, not a clone. */
21631 decl = DECL_ORIGIN (decl);
21633 old_die = lookup_decl_die (decl);
21634 if (old_die && get_AT (old_die, DW_AT_inline))
21635 /* We've already generated the abstract instance. */
21636 return;
21638 /* We can be called while recursively when seeing block defining inlined subroutine
21639 DIE. Be sure to not clobber the outer location table nor use it or we would
21640 get locations in abstract instantces. */
21641 old_decl_loc_table = decl_loc_table;
21642 decl_loc_table = NULL;
21643 old_cached_dw_loc_list_table = cached_dw_loc_list_table;
21644 cached_dw_loc_list_table = NULL;
21645 old_call_arg_locations = call_arg_locations;
21646 call_arg_locations = NULL;
21647 old_call_site_count = call_site_count;
21648 call_site_count = -1;
21649 old_tail_call_site_count = tail_call_site_count;
21650 tail_call_site_count = -1;
21652 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
21653 we don't get confused by DECL_ABSTRACT_P. */
21654 if (debug_info_level > DINFO_LEVEL_TERSE)
21656 context = decl_class_context (decl);
21657 if (context)
21658 gen_type_die_for_member
21659 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
21662 /* Pretend we've just finished compiling this function. */
21663 save_fn = current_function_decl;
21664 current_function_decl = decl;
21666 auto_vec<tree, 64> abstract_vec;
21667 set_decl_abstract_flags (decl, abstract_vec);
21668 dwarf2out_decl (decl);
21669 unsigned int i;
21670 tree t;
21671 FOR_EACH_VEC_ELT (abstract_vec, i, t)
21672 if (TREE_CODE (t) == BLOCK)
21673 BLOCK_ABSTRACT (t) = 0;
21674 else
21675 DECL_ABSTRACT_P (t) = 0;
21677 current_function_decl = save_fn;
21678 decl_loc_table = old_decl_loc_table;
21679 cached_dw_loc_list_table = old_cached_dw_loc_list_table;
21680 call_arg_locations = old_call_arg_locations;
21681 call_site_count = old_call_site_count;
21682 tail_call_site_count = old_tail_call_site_count;
21685 /* Helper function of premark_used_types() which gets called through
21686 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. */
21691 bool
21692 premark_used_types_helper (tree const &type, void *)
21694 dw_die_ref die;
21696 die = lookup_type_die (type);
21697 if (die != NULL)
21698 die->die_perennial_p = 1;
21699 return true;
21702 /* Helper function of premark_types_used_by_global_vars which gets called
21703 through htab_traverse.
21705 Marks the DIE of a given type in *SLOT as perennial, so it never gets
21706 marked as unused by prune_unused_types. The DIE of the type is marked
21707 only if the global variable using the type will actually be emitted. */
21710 premark_types_used_by_global_vars_helper (types_used_by_vars_entry **slot,
21711 void *)
21713 struct types_used_by_vars_entry *entry;
21714 dw_die_ref die;
21716 entry = (struct types_used_by_vars_entry *) *slot;
21717 gcc_assert (entry->type != NULL
21718 && entry->var_decl != NULL);
21719 die = lookup_type_die (entry->type);
21720 if (die)
21722 /* Ask cgraph if the global variable really is to be emitted.
21723 If yes, then we'll keep the DIE of ENTRY->TYPE. */
21724 varpool_node *node = varpool_node::get (entry->var_decl);
21725 if (node && node->definition)
21727 die->die_perennial_p = 1;
21728 /* Keep the parent DIEs as well. */
21729 while ((die = die->die_parent) && die->die_perennial_p == 0)
21730 die->die_perennial_p = 1;
21733 return 1;
21736 /* Mark all members of used_types_hash as perennial. */
21738 static void
21739 premark_used_types (struct function *fun)
21741 if (fun && fun->used_types_hash)
21742 fun->used_types_hash->traverse<void *, premark_used_types_helper> (NULL);
21745 /* Mark all members of types_used_by_vars_entry as perennial. */
21747 static void
21748 premark_types_used_by_global_vars (void)
21750 if (types_used_by_vars_hash)
21751 types_used_by_vars_hash
21752 ->traverse<void *, premark_types_used_by_global_vars_helper> (NULL);
21755 /* Generate a DW_TAG_call_site DIE in function DECL under SUBR_DIE
21756 for CA_LOC call arg loc node. */
21758 static dw_die_ref
21759 gen_call_site_die (tree decl, dw_die_ref subr_die,
21760 struct call_arg_loc_node *ca_loc)
21762 dw_die_ref stmt_die = NULL, die;
21763 tree block = ca_loc->block;
21765 while (block
21766 && block != DECL_INITIAL (decl)
21767 && TREE_CODE (block) == BLOCK)
21769 stmt_die = BLOCK_DIE (block);
21770 if (stmt_die)
21771 break;
21772 block = BLOCK_SUPERCONTEXT (block);
21774 if (stmt_die == NULL)
21775 stmt_die = subr_die;
21776 die = new_die (dwarf_TAG (DW_TAG_call_site), stmt_die, NULL_TREE);
21777 add_AT_lbl_id (die, dwarf_AT (DW_AT_call_return_pc), ca_loc->label);
21778 if (ca_loc->tail_call_p)
21779 add_AT_flag (die, dwarf_AT (DW_AT_call_tail_call), 1);
21780 if (ca_loc->symbol_ref)
21782 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
21783 if (tdie)
21784 add_AT_die_ref (die, dwarf_AT (DW_AT_call_origin), tdie);
21785 else
21786 add_AT_addr (die, dwarf_AT (DW_AT_call_origin), ca_loc->symbol_ref,
21787 false);
21789 return die;
21792 /* Generate a DIE to represent a declared function (either file-scope or
21793 block-local). */
21795 static void
21796 gen_subprogram_die (tree decl, dw_die_ref context_die)
21798 tree origin = decl_ultimate_origin (decl);
21799 dw_die_ref subr_die;
21800 dw_die_ref old_die = lookup_decl_die (decl);
21802 /* This function gets called multiple times for different stages of
21803 the debug process. For example, for func() in this code:
21805 namespace S
21807 void func() { ... }
21810 ...we get called 4 times. Twice in early debug and twice in
21811 late debug:
21813 Early debug
21814 -----------
21816 1. Once while generating func() within the namespace. This is
21817 the declaration. The declaration bit below is set, as the
21818 context is the namespace.
21820 A new DIE will be generated with DW_AT_declaration set.
21822 2. Once for func() itself. This is the specification. The
21823 declaration bit below is clear as the context is the CU.
21825 We will use the cached DIE from (1) to create a new DIE with
21826 DW_AT_specification pointing to the declaration in (1).
21828 Late debug via rest_of_handle_final()
21829 -------------------------------------
21831 3. Once generating func() within the namespace. This is also the
21832 declaration, as in (1), but this time we will early exit below
21833 as we have a cached DIE and a declaration needs no additional
21834 annotations (no locations), as the source declaration line
21835 info is enough.
21837 4. Once for func() itself. As in (2), this is the specification,
21838 but this time we will re-use the cached DIE, and just annotate
21839 it with the location information that should now be available.
21841 For something without namespaces, but with abstract instances, we
21842 are also called a multiple times:
21844 class Base
21846 public:
21847 Base (); // constructor declaration (1)
21850 Base::Base () { } // constructor specification (2)
21852 Early debug
21853 -----------
21855 1. Once for the Base() constructor by virtue of it being a
21856 member of the Base class. This is done via
21857 rest_of_type_compilation.
21859 This is a declaration, so a new DIE will be created with
21860 DW_AT_declaration.
21862 2. Once for the Base() constructor definition, but this time
21863 while generating the abstract instance of the base
21864 constructor (__base_ctor) which is being generated via early
21865 debug of reachable functions.
21867 Even though we have a cached version of the declaration (1),
21868 we will create a DW_AT_specification of the declaration DIE
21869 in (1).
21871 3. Once for the __base_ctor itself, but this time, we generate
21872 an DW_AT_abstract_origin version of the DW_AT_specification in
21873 (2).
21875 Late debug via rest_of_handle_final
21876 -----------------------------------
21878 4. One final time for the __base_ctor (which will have a cached
21879 DIE with DW_AT_abstract_origin created in (3). This time,
21880 we will just annotate the location information now
21881 available.
21883 int declaration = (current_function_decl != decl
21884 || class_or_namespace_scope_p (context_die));
21886 /* Now that the C++ front end lazily declares artificial member fns, we
21887 might need to retrofit the declaration into its class. */
21888 if (!declaration && !origin && !old_die
21889 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
21890 && !class_or_namespace_scope_p (context_die)
21891 && debug_info_level > DINFO_LEVEL_TERSE)
21892 old_die = force_decl_die (decl);
21894 /* An inlined instance, tag a new DIE with DW_AT_abstract_origin. */
21895 if (origin != NULL)
21897 gcc_assert (!declaration || local_scope_p (context_die));
21899 /* Fixup die_parent for the abstract instance of a nested
21900 inline function. */
21901 if (old_die && old_die->die_parent == NULL)
21902 add_child_die (context_die, old_die);
21904 if (old_die && get_AT_ref (old_die, DW_AT_abstract_origin))
21906 /* If we have a DW_AT_abstract_origin we have a working
21907 cached version. */
21908 subr_die = old_die;
21910 else
21912 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
21913 add_abstract_origin_attribute (subr_die, origin);
21914 /* This is where the actual code for a cloned function is.
21915 Let's emit linkage name attribute for it. This helps
21916 debuggers to e.g, set breakpoints into
21917 constructors/destructors when the user asks "break
21918 K::K". */
21919 add_linkage_name (subr_die, decl);
21922 /* A cached copy, possibly from early dwarf generation. Reuse as
21923 much as possible. */
21924 else if (old_die)
21926 /* A declaration that has been previously dumped needs no
21927 additional information. */
21928 if (declaration)
21929 return;
21931 if (!get_AT_flag (old_die, DW_AT_declaration)
21932 /* We can have a normal definition following an inline one in the
21933 case of redefinition of GNU C extern inlines.
21934 It seems reasonable to use AT_specification in this case. */
21935 && !get_AT (old_die, DW_AT_inline))
21937 /* Detect and ignore this case, where we are trying to output
21938 something we have already output. */
21939 if (get_AT (old_die, DW_AT_low_pc)
21940 || get_AT (old_die, DW_AT_ranges))
21941 return;
21943 /* If we have no location information, this must be a
21944 partially generated DIE from early dwarf generation.
21945 Fall through and generate it. */
21948 /* If the definition comes from the same place as the declaration,
21949 maybe use the old DIE. We always want the DIE for this function
21950 that has the *_pc attributes to be under comp_unit_die so the
21951 debugger can find it. We also need to do this for abstract
21952 instances of inlines, since the spec requires the out-of-line copy
21953 to have the same parent. For local class methods, this doesn't
21954 apply; we just use the old DIE. */
21955 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
21956 struct dwarf_file_data * file_index = lookup_filename (s.file);
21957 if ((is_cu_die (old_die->die_parent)
21958 /* This condition fixes the inconsistency/ICE with the
21959 following Fortran test (or some derivative thereof) while
21960 building libgfortran:
21962 module some_m
21963 contains
21964 logical function funky (FLAG)
21965 funky = .true.
21966 end function
21967 end module
21969 || (old_die->die_parent
21970 && old_die->die_parent->die_tag == DW_TAG_module)
21971 || context_die == NULL)
21972 && (DECL_ARTIFICIAL (decl)
21973 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
21974 && (get_AT_unsigned (old_die, DW_AT_decl_line)
21975 == (unsigned) s.line)
21976 && (!debug_column_info
21977 || s.column == 0
21978 || (get_AT_unsigned (old_die, DW_AT_decl_column)
21979 == (unsigned) s.column)))))
21981 subr_die = old_die;
21983 /* Clear out the declaration attribute, but leave the
21984 parameters so they can be augmented with location
21985 information later. Unless this was a declaration, in
21986 which case, wipe out the nameless parameters and recreate
21987 them further down. */
21988 if (remove_AT (subr_die, DW_AT_declaration))
21991 remove_AT (subr_die, DW_AT_object_pointer);
21992 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
21995 /* Make a specification pointing to the previously built
21996 declaration. */
21997 else
21999 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
22000 add_AT_specification (subr_die, old_die);
22001 add_pubname (decl, subr_die);
22002 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
22003 add_AT_file (subr_die, DW_AT_decl_file, file_index);
22004 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
22005 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
22006 if (debug_column_info
22007 && s.column
22008 && (get_AT_unsigned (old_die, DW_AT_decl_column)
22009 != (unsigned) s.column))
22010 add_AT_unsigned (subr_die, DW_AT_decl_column, s.column);
22012 /* If the prototype had an 'auto' or 'decltype(auto)' return type,
22013 emit the real type on the definition die. */
22014 if (is_cxx () && debug_info_level > DINFO_LEVEL_TERSE)
22016 dw_die_ref die = get_AT_ref (old_die, DW_AT_type);
22017 if (die == auto_die || die == decltype_auto_die)
22018 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
22019 TYPE_UNQUALIFIED, false, context_die);
22022 /* When we process the method declaration, we haven't seen
22023 the out-of-class defaulted definition yet, so we have to
22024 recheck now. */
22025 if ((dwarf_version >= 5 || ! dwarf_strict)
22026 && !get_AT (subr_die, DW_AT_defaulted))
22028 int defaulted
22029 = lang_hooks.decls.decl_dwarf_attribute (decl,
22030 DW_AT_defaulted);
22031 if (defaulted != -1)
22033 /* Other values must have been handled before. */
22034 gcc_assert (defaulted == DW_DEFAULTED_out_of_class);
22035 add_AT_unsigned (subr_die, DW_AT_defaulted, defaulted);
22040 /* Create a fresh DIE for anything else. */
22041 else
22043 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
22045 if (TREE_PUBLIC (decl))
22046 add_AT_flag (subr_die, DW_AT_external, 1);
22048 add_name_and_src_coords_attributes (subr_die, decl);
22049 add_pubname (decl, subr_die);
22050 if (debug_info_level > DINFO_LEVEL_TERSE)
22052 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
22053 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
22054 TYPE_UNQUALIFIED, false, context_die);
22057 add_pure_or_virtual_attribute (subr_die, decl);
22058 if (DECL_ARTIFICIAL (decl))
22059 add_AT_flag (subr_die, DW_AT_artificial, 1);
22061 if (TREE_THIS_VOLATILE (decl) && (dwarf_version >= 5 || !dwarf_strict))
22062 add_AT_flag (subr_die, DW_AT_noreturn, 1);
22064 add_alignment_attribute (subr_die, decl);
22066 add_accessibility_attribute (subr_die, decl);
22069 /* Unless we have an existing non-declaration DIE, equate the new
22070 DIE. */
22071 if (!old_die || is_declaration_die (old_die))
22072 equate_decl_number_to_die (decl, subr_die);
22074 if (declaration)
22076 if (!old_die || !get_AT (old_die, DW_AT_inline))
22078 add_AT_flag (subr_die, DW_AT_declaration, 1);
22080 /* If this is an explicit function declaration then generate
22081 a DW_AT_explicit attribute. */
22082 if ((dwarf_version >= 3 || !dwarf_strict)
22083 && lang_hooks.decls.decl_dwarf_attribute (decl,
22084 DW_AT_explicit) == 1)
22085 add_AT_flag (subr_die, DW_AT_explicit, 1);
22087 /* If this is a C++11 deleted special function member then generate
22088 a DW_AT_deleted attribute. */
22089 if ((dwarf_version >= 5 || !dwarf_strict)
22090 && lang_hooks.decls.decl_dwarf_attribute (decl,
22091 DW_AT_deleted) == 1)
22092 add_AT_flag (subr_die, DW_AT_deleted, 1);
22094 /* If this is a C++11 defaulted special function member then
22095 generate a DW_AT_defaulted attribute. */
22096 if (dwarf_version >= 5 || !dwarf_strict)
22098 int defaulted
22099 = lang_hooks.decls.decl_dwarf_attribute (decl,
22100 DW_AT_defaulted);
22101 if (defaulted != -1)
22102 add_AT_unsigned (subr_die, DW_AT_defaulted, defaulted);
22105 /* If this is a C++11 non-static member function with & ref-qualifier
22106 then generate a DW_AT_reference attribute. */
22107 if ((dwarf_version >= 5 || !dwarf_strict)
22108 && lang_hooks.decls.decl_dwarf_attribute (decl,
22109 DW_AT_reference) == 1)
22110 add_AT_flag (subr_die, DW_AT_reference, 1);
22112 /* If this is a C++11 non-static member function with &&
22113 ref-qualifier then generate a DW_AT_reference attribute. */
22114 if ((dwarf_version >= 5 || !dwarf_strict)
22115 && lang_hooks.decls.decl_dwarf_attribute (decl,
22116 DW_AT_rvalue_reference)
22117 == 1)
22118 add_AT_flag (subr_die, DW_AT_rvalue_reference, 1);
22121 /* Tag abstract instances with DW_AT_inline. */
22122 else if (DECL_ABSTRACT_P (decl))
22124 if (DECL_DECLARED_INLINE_P (decl))
22126 if (cgraph_function_possibly_inlined_p (decl))
22127 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
22128 else
22129 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
22131 else
22133 if (cgraph_function_possibly_inlined_p (decl))
22134 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
22135 else
22136 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
22139 if (DECL_DECLARED_INLINE_P (decl)
22140 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
22141 add_AT_flag (subr_die, DW_AT_artificial, 1);
22143 /* For non DECL_EXTERNALs, if range information is available, fill
22144 the DIE with it. */
22145 else if (!DECL_EXTERNAL (decl) && !early_dwarf)
22147 HOST_WIDE_INT cfa_fb_offset;
22149 struct function *fun = DECL_STRUCT_FUNCTION (decl);
22151 if (!flag_reorder_blocks_and_partition)
22153 dw_fde_ref fde = fun->fde;
22154 if (fde->dw_fde_begin)
22156 /* We have already generated the labels. */
22157 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
22158 fde->dw_fde_end, false);
22160 else
22162 /* Create start/end labels and add the range. */
22163 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
22164 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
22165 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
22166 current_function_funcdef_no);
22167 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
22168 current_function_funcdef_no);
22169 add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
22170 false);
22173 #if VMS_DEBUGGING_INFO
22174 /* HP OpenVMS Industry Standard 64: DWARF Extensions
22175 Section 2.3 Prologue and Epilogue Attributes:
22176 When a breakpoint is set on entry to a function, it is generally
22177 desirable for execution to be suspended, not on the very first
22178 instruction of the function, but rather at a point after the
22179 function's frame has been set up, after any language defined local
22180 declaration processing has been completed, and before execution of
22181 the first statement of the function begins. Debuggers generally
22182 cannot properly determine where this point is. Similarly for a
22183 breakpoint set on exit from a function. The prologue and epilogue
22184 attributes allow a compiler to communicate the location(s) to use. */
22187 if (fde->dw_fde_vms_end_prologue)
22188 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
22189 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
22191 if (fde->dw_fde_vms_begin_epilogue)
22192 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
22193 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
22195 #endif
22198 else
22200 /* Generate pubnames entries for the split function code ranges. */
22201 dw_fde_ref fde = fun->fde;
22203 if (fde->dw_fde_second_begin)
22205 if (dwarf_version >= 3 || !dwarf_strict)
22207 /* We should use ranges for non-contiguous code section
22208 addresses. Use the actual code range for the initial
22209 section, since the HOT/COLD labels might precede an
22210 alignment offset. */
22211 bool range_list_added = false;
22212 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
22213 fde->dw_fde_end, &range_list_added,
22214 false);
22215 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
22216 fde->dw_fde_second_end,
22217 &range_list_added, false);
22218 if (range_list_added)
22219 add_ranges (NULL);
22221 else
22223 /* There is no real support in DW2 for this .. so we make
22224 a work-around. First, emit the pub name for the segment
22225 containing the function label. Then make and emit a
22226 simplified subprogram DIE for the second segment with the
22227 name pre-fixed by __hot/cold_sect_of_. We use the same
22228 linkage name for the second die so that gdb will find both
22229 sections when given "b foo". */
22230 const char *name = NULL;
22231 tree decl_name = DECL_NAME (decl);
22232 dw_die_ref seg_die;
22234 /* Do the 'primary' section. */
22235 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
22236 fde->dw_fde_end, false);
22238 /* Build a minimal DIE for the secondary section. */
22239 seg_die = new_die (DW_TAG_subprogram,
22240 subr_die->die_parent, decl);
22242 if (TREE_PUBLIC (decl))
22243 add_AT_flag (seg_die, DW_AT_external, 1);
22245 if (decl_name != NULL
22246 && IDENTIFIER_POINTER (decl_name) != NULL)
22248 name = dwarf2_name (decl, 1);
22249 if (! DECL_ARTIFICIAL (decl))
22250 add_src_coords_attributes (seg_die, decl);
22252 add_linkage_name (seg_die, decl);
22254 gcc_assert (name != NULL);
22255 add_pure_or_virtual_attribute (seg_die, decl);
22256 if (DECL_ARTIFICIAL (decl))
22257 add_AT_flag (seg_die, DW_AT_artificial, 1);
22259 name = concat ("__second_sect_of_", name, NULL);
22260 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
22261 fde->dw_fde_second_end, false);
22262 add_name_attribute (seg_die, name);
22263 if (want_pubnames ())
22264 add_pubname_string (name, seg_die);
22267 else
22268 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
22269 false);
22272 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
22274 /* We define the "frame base" as the function's CFA. This is more
22275 convenient for several reasons: (1) It's stable across the prologue
22276 and epilogue, which makes it better than just a frame pointer,
22277 (2) With dwarf3, there exists a one-byte encoding that allows us
22278 to reference the .debug_frame data by proxy, but failing that,
22279 (3) We can at least reuse the code inspection and interpretation
22280 code that determines the CFA position at various points in the
22281 function. */
22282 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
22284 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
22285 add_AT_loc (subr_die, DW_AT_frame_base, op);
22287 else
22289 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
22290 if (list->dw_loc_next)
22291 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
22292 else
22293 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
22296 /* Compute a displacement from the "steady-state frame pointer" to
22297 the CFA. The former is what all stack slots and argument slots
22298 will reference in the rtl; the latter is what we've told the
22299 debugger about. We'll need to adjust all frame_base references
22300 by this displacement. */
22301 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
22303 if (fun->static_chain_decl)
22305 /* DWARF requires here a location expression that computes the
22306 address of the enclosing subprogram's frame base. The machinery
22307 in tree-nested.c is supposed to store this specific address in the
22308 last field of the FRAME record. */
22309 const tree frame_type
22310 = TREE_TYPE (TREE_TYPE (fun->static_chain_decl));
22311 const tree fb_decl = tree_last (TYPE_FIELDS (frame_type));
22313 tree fb_expr
22314 = build1 (INDIRECT_REF, frame_type, fun->static_chain_decl);
22315 fb_expr = build3 (COMPONENT_REF, TREE_TYPE (fb_decl),
22316 fb_expr, fb_decl, NULL_TREE);
22318 add_AT_location_description (subr_die, DW_AT_static_link,
22319 loc_list_from_tree (fb_expr, 0, NULL));
22322 resolve_variable_values ();
22325 /* Generate child dies for template paramaters. */
22326 if (early_dwarf && debug_info_level > DINFO_LEVEL_TERSE)
22327 gen_generic_params_dies (decl);
22329 /* Now output descriptions of the arguments for this function. This gets
22330 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
22331 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
22332 `...' at the end of the formal parameter list. In order to find out if
22333 there was a trailing ellipsis or not, we must instead look at the type
22334 associated with the FUNCTION_DECL. This will be a node of type
22335 FUNCTION_TYPE. If the chain of type nodes hanging off of this
22336 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
22337 an ellipsis at the end. */
22339 /* In the case where we are describing a mere function declaration, all we
22340 need to do here (and all we *can* do here) is to describe the *types* of
22341 its formal parameters. */
22342 if (debug_info_level <= DINFO_LEVEL_TERSE)
22344 else if (declaration)
22345 gen_formal_types_die (decl, subr_die);
22346 else
22348 /* Generate DIEs to represent all known formal parameters. */
22349 tree parm = DECL_ARGUMENTS (decl);
22350 tree generic_decl = early_dwarf
22351 ? lang_hooks.decls.get_generic_function_decl (decl) : NULL;
22352 tree generic_decl_parm = generic_decl
22353 ? DECL_ARGUMENTS (generic_decl)
22354 : NULL;
22356 /* Now we want to walk the list of parameters of the function and
22357 emit their relevant DIEs.
22359 We consider the case of DECL being an instance of a generic function
22360 as well as it being a normal function.
22362 If DECL is an instance of a generic function we walk the
22363 parameters of the generic function declaration _and_ the parameters of
22364 DECL itself. This is useful because we want to emit specific DIEs for
22365 function parameter packs and those are declared as part of the
22366 generic function declaration. In that particular case,
22367 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
22368 That DIE has children DIEs representing the set of arguments
22369 of the pack. Note that the set of pack arguments can be empty.
22370 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
22371 children DIE.
22373 Otherwise, we just consider the parameters of DECL. */
22374 while (generic_decl_parm || parm)
22376 if (generic_decl_parm
22377 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
22378 gen_formal_parameter_pack_die (generic_decl_parm,
22379 parm, subr_die,
22380 &parm);
22381 else if (parm && !POINTER_BOUNDS_P (parm))
22383 dw_die_ref parm_die = gen_decl_die (parm, NULL, NULL, subr_die);
22385 if (parm == DECL_ARGUMENTS (decl)
22386 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
22387 && parm_die
22388 && (dwarf_version >= 3 || !dwarf_strict))
22389 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
22391 parm = DECL_CHAIN (parm);
22393 else if (parm)
22394 parm = DECL_CHAIN (parm);
22396 if (generic_decl_parm)
22397 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
22400 /* Decide whether we need an unspecified_parameters DIE at the end.
22401 There are 2 more cases to do this for: 1) the ansi ... declaration -
22402 this is detectable when the end of the arg list is not a
22403 void_type_node 2) an unprototyped function declaration (not a
22404 definition). This just means that we have no info about the
22405 parameters at all. */
22406 if (early_dwarf)
22408 if (prototype_p (TREE_TYPE (decl)))
22410 /* This is the prototyped case, check for.... */
22411 if (stdarg_p (TREE_TYPE (decl)))
22412 gen_unspecified_parameters_die (decl, subr_die);
22414 else if (DECL_INITIAL (decl) == NULL_TREE)
22415 gen_unspecified_parameters_die (decl, subr_die);
22419 if (subr_die != old_die)
22420 /* Add the calling convention attribute if requested. */
22421 add_calling_convention_attribute (subr_die, decl);
22423 /* Output Dwarf info for all of the stuff within the body of the function
22424 (if it has one - it may be just a declaration).
22426 OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
22427 a function. This BLOCK actually represents the outermost binding contour
22428 for the function, i.e. the contour in which the function's formal
22429 parameters and labels get declared. Curiously, it appears that the front
22430 end doesn't actually put the PARM_DECL nodes for the current function onto
22431 the BLOCK_VARS list for this outer scope, but are strung off of the
22432 DECL_ARGUMENTS list for the function instead.
22434 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
22435 the LABEL_DECL nodes for the function however, and we output DWARF info
22436 for those in decls_for_scope. Just within the `outer_scope' there will be
22437 a BLOCK node representing the function's outermost pair of curly braces,
22438 and any blocks used for the base and member initializers of a C++
22439 constructor function. */
22440 tree outer_scope = DECL_INITIAL (decl);
22441 if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
22443 int call_site_note_count = 0;
22444 int tail_call_site_note_count = 0;
22446 /* Emit a DW_TAG_variable DIE for a named return value. */
22447 if (DECL_NAME (DECL_RESULT (decl)))
22448 gen_decl_die (DECL_RESULT (decl), NULL, NULL, subr_die);
22450 /* The first time through decls_for_scope we will generate the
22451 DIEs for the locals. The second time, we fill in the
22452 location info. */
22453 decls_for_scope (outer_scope, subr_die);
22455 if (call_arg_locations && (!dwarf_strict || dwarf_version >= 5))
22457 struct call_arg_loc_node *ca_loc;
22458 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
22460 dw_die_ref die = NULL;
22461 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
22462 rtx arg, next_arg;
22464 for (arg = (ca_loc->call_arg_loc_note != NULL_RTX
22465 ? NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note)
22466 : NULL_RTX);
22467 arg; arg = next_arg)
22469 dw_loc_descr_ref reg, val;
22470 machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
22471 dw_die_ref cdie, tdie = NULL;
22473 next_arg = XEXP (arg, 1);
22474 if (REG_P (XEXP (XEXP (arg, 0), 0))
22475 && next_arg
22476 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
22477 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
22478 && REGNO (XEXP (XEXP (arg, 0), 0))
22479 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
22480 next_arg = XEXP (next_arg, 1);
22481 if (mode == VOIDmode)
22483 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
22484 if (mode == VOIDmode)
22485 mode = GET_MODE (XEXP (arg, 0));
22487 if (mode == VOIDmode || mode == BLKmode)
22488 continue;
22489 /* Get dynamic information about call target only if we
22490 have no static information: we cannot generate both
22491 DW_AT_call_origin and DW_AT_call_target
22492 attributes. */
22493 if (ca_loc->symbol_ref == NULL_RTX)
22495 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
22497 tloc = XEXP (XEXP (arg, 0), 1);
22498 continue;
22500 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
22501 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
22503 tlocc = XEXP (XEXP (arg, 0), 1);
22504 continue;
22507 reg = NULL;
22508 if (REG_P (XEXP (XEXP (arg, 0), 0)))
22509 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
22510 VAR_INIT_STATUS_INITIALIZED);
22511 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
22513 rtx mem = XEXP (XEXP (arg, 0), 0);
22514 reg = mem_loc_descriptor (XEXP (mem, 0),
22515 get_address_mode (mem),
22516 GET_MODE (mem),
22517 VAR_INIT_STATUS_INITIALIZED);
22519 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
22520 == DEBUG_PARAMETER_REF)
22522 tree tdecl
22523 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
22524 tdie = lookup_decl_die (tdecl);
22525 if (tdie == NULL)
22526 continue;
22528 else
22529 continue;
22530 if (reg == NULL
22531 && GET_CODE (XEXP (XEXP (arg, 0), 0))
22532 != DEBUG_PARAMETER_REF)
22533 continue;
22534 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
22535 VOIDmode,
22536 VAR_INIT_STATUS_INITIALIZED);
22537 if (val == NULL)
22538 continue;
22539 if (die == NULL)
22540 die = gen_call_site_die (decl, subr_die, ca_loc);
22541 cdie = new_die (dwarf_TAG (DW_TAG_call_site_parameter), die,
22542 NULL_TREE);
22543 if (reg != NULL)
22544 add_AT_loc (cdie, DW_AT_location, reg);
22545 else if (tdie != NULL)
22546 add_AT_die_ref (cdie, dwarf_AT (DW_AT_call_parameter),
22547 tdie);
22548 add_AT_loc (cdie, dwarf_AT (DW_AT_call_value), val);
22549 if (next_arg != XEXP (arg, 1))
22551 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
22552 if (mode == VOIDmode)
22553 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
22554 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
22555 0), 1),
22556 mode, VOIDmode,
22557 VAR_INIT_STATUS_INITIALIZED);
22558 if (val != NULL)
22559 add_AT_loc (cdie, dwarf_AT (DW_AT_call_data_value),
22560 val);
22563 if (die == NULL
22564 && (ca_loc->symbol_ref || tloc))
22565 die = gen_call_site_die (decl, subr_die, ca_loc);
22566 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
22568 dw_loc_descr_ref tval = NULL;
22570 if (tloc != NULL_RTX)
22571 tval = mem_loc_descriptor (tloc,
22572 GET_MODE (tloc) == VOIDmode
22573 ? Pmode : GET_MODE (tloc),
22574 VOIDmode,
22575 VAR_INIT_STATUS_INITIALIZED);
22576 if (tval)
22577 add_AT_loc (die, dwarf_AT (DW_AT_call_target), tval);
22578 else if (tlocc != NULL_RTX)
22580 tval = mem_loc_descriptor (tlocc,
22581 GET_MODE (tlocc) == VOIDmode
22582 ? Pmode : GET_MODE (tlocc),
22583 VOIDmode,
22584 VAR_INIT_STATUS_INITIALIZED);
22585 if (tval)
22586 add_AT_loc (die,
22587 dwarf_AT (DW_AT_call_target_clobbered),
22588 tval);
22591 if (die != NULL)
22593 call_site_note_count++;
22594 if (ca_loc->tail_call_p)
22595 tail_call_site_note_count++;
22599 call_arg_locations = NULL;
22600 call_arg_loc_last = NULL;
22601 if (tail_call_site_count >= 0
22602 && tail_call_site_count == tail_call_site_note_count
22603 && (!dwarf_strict || dwarf_version >= 5))
22605 if (call_site_count >= 0
22606 && call_site_count == call_site_note_count)
22607 add_AT_flag (subr_die, dwarf_AT (DW_AT_call_all_calls), 1);
22608 else
22609 add_AT_flag (subr_die, dwarf_AT (DW_AT_call_all_tail_calls), 1);
22611 call_site_count = -1;
22612 tail_call_site_count = -1;
22615 /* Mark used types after we have created DIEs for the functions scopes. */
22616 premark_used_types (DECL_STRUCT_FUNCTION (decl));
22619 /* Returns a hash value for X (which really is a die_struct). */
22621 hashval_t
22622 block_die_hasher::hash (die_struct *d)
22624 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
22627 /* Return nonzero if decl_id and die_parent of die_struct X is the same
22628 as decl_id and die_parent of die_struct Y. */
22630 bool
22631 block_die_hasher::equal (die_struct *x, die_struct *y)
22633 return x->decl_id == y->decl_id && x->die_parent == y->die_parent;
22636 /* Return TRUE if DECL, which may have been previously generated as
22637 OLD_DIE, is a candidate for a DW_AT_specification. DECLARATION is
22638 true if decl (or its origin) is either an extern declaration or a
22639 class/namespace scoped declaration.
22641 The declare_in_namespace support causes us to get two DIEs for one
22642 variable, both of which are declarations. We want to avoid
22643 considering one to be a specification, so we must test for
22644 DECLARATION and DW_AT_declaration. */
22645 static inline bool
22646 decl_will_get_specification_p (dw_die_ref old_die, tree decl, bool declaration)
22648 return (old_die && TREE_STATIC (decl) && !declaration
22649 && get_AT_flag (old_die, DW_AT_declaration) == 1);
22652 /* Return true if DECL is a local static. */
22654 static inline bool
22655 local_function_static (tree decl)
22657 gcc_assert (VAR_P (decl));
22658 return TREE_STATIC (decl)
22659 && DECL_CONTEXT (decl)
22660 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL;
22663 /* Generate a DIE to represent a declared data object.
22664 Either DECL or ORIGIN must be non-null. */
22666 static void
22667 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
22669 HOST_WIDE_INT off = 0;
22670 tree com_decl;
22671 tree decl_or_origin = decl ? decl : origin;
22672 tree ultimate_origin;
22673 dw_die_ref var_die;
22674 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
22675 bool declaration = (DECL_EXTERNAL (decl_or_origin)
22676 || class_or_namespace_scope_p (context_die));
22677 bool specialization_p = false;
22678 bool no_linkage_name = false;
22680 /* While C++ inline static data members have definitions inside of the
22681 class, force the first DIE to be a declaration, then let gen_member_die
22682 reparent it to the class context and call gen_variable_die again
22683 to create the outside of the class DIE for the definition. */
22684 if (!declaration
22685 && old_die == NULL
22686 && decl
22687 && DECL_CONTEXT (decl)
22688 && TYPE_P (DECL_CONTEXT (decl))
22689 && lang_hooks.decls.decl_dwarf_attribute (decl, DW_AT_inline) != -1)
22691 declaration = true;
22692 if (dwarf_version < 5)
22693 no_linkage_name = true;
22696 ultimate_origin = decl_ultimate_origin (decl_or_origin);
22697 if (decl || ultimate_origin)
22698 origin = ultimate_origin;
22699 com_decl = fortran_common (decl_or_origin, &off);
22701 /* Symbol in common gets emitted as a child of the common block, in the form
22702 of a data member. */
22703 if (com_decl)
22705 dw_die_ref com_die;
22706 dw_loc_list_ref loc = NULL;
22707 die_node com_die_arg;
22709 var_die = lookup_decl_die (decl_or_origin);
22710 if (var_die)
22712 if (! early_dwarf && get_AT (var_die, DW_AT_location) == NULL)
22714 loc = loc_list_from_tree (com_decl, off ? 1 : 2, NULL);
22715 if (loc)
22717 if (off)
22719 /* Optimize the common case. */
22720 if (single_element_loc_list_p (loc)
22721 && loc->expr->dw_loc_opc == DW_OP_addr
22722 && loc->expr->dw_loc_next == NULL
22723 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
22724 == SYMBOL_REF)
22726 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
22727 loc->expr->dw_loc_oprnd1.v.val_addr
22728 = plus_constant (GET_MODE (x), x , off);
22730 else
22731 loc_list_plus_const (loc, off);
22733 add_AT_location_description (var_die, DW_AT_location, loc);
22734 remove_AT (var_die, DW_AT_declaration);
22737 return;
22740 if (common_block_die_table == NULL)
22741 common_block_die_table = hash_table<block_die_hasher>::create_ggc (10);
22743 com_die_arg.decl_id = DECL_UID (com_decl);
22744 com_die_arg.die_parent = context_die;
22745 com_die = common_block_die_table->find (&com_die_arg);
22746 if (! early_dwarf)
22747 loc = loc_list_from_tree (com_decl, 2, NULL);
22748 if (com_die == NULL)
22750 const char *cnam
22751 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
22752 die_node **slot;
22754 com_die = new_die (DW_TAG_common_block, context_die, decl);
22755 add_name_and_src_coords_attributes (com_die, com_decl);
22756 if (loc)
22758 add_AT_location_description (com_die, DW_AT_location, loc);
22759 /* Avoid sharing the same loc descriptor between
22760 DW_TAG_common_block and DW_TAG_variable. */
22761 loc = loc_list_from_tree (com_decl, 2, NULL);
22763 else if (DECL_EXTERNAL (decl_or_origin))
22764 add_AT_flag (com_die, DW_AT_declaration, 1);
22765 if (want_pubnames ())
22766 add_pubname_string (cnam, com_die); /* ??? needed? */
22767 com_die->decl_id = DECL_UID (com_decl);
22768 slot = common_block_die_table->find_slot (com_die, INSERT);
22769 *slot = com_die;
22771 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
22773 add_AT_location_description (com_die, DW_AT_location, loc);
22774 loc = loc_list_from_tree (com_decl, 2, NULL);
22775 remove_AT (com_die, DW_AT_declaration);
22777 var_die = new_die (DW_TAG_variable, com_die, decl);
22778 add_name_and_src_coords_attributes (var_die, decl_or_origin);
22779 add_type_attribute (var_die, TREE_TYPE (decl_or_origin),
22780 decl_quals (decl_or_origin), false,
22781 context_die);
22782 add_alignment_attribute (var_die, decl);
22783 add_AT_flag (var_die, DW_AT_external, 1);
22784 if (loc)
22786 if (off)
22788 /* Optimize the common case. */
22789 if (single_element_loc_list_p (loc)
22790 && loc->expr->dw_loc_opc == DW_OP_addr
22791 && loc->expr->dw_loc_next == NULL
22792 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
22794 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
22795 loc->expr->dw_loc_oprnd1.v.val_addr
22796 = plus_constant (GET_MODE (x), x, off);
22798 else
22799 loc_list_plus_const (loc, off);
22801 add_AT_location_description (var_die, DW_AT_location, loc);
22803 else if (DECL_EXTERNAL (decl_or_origin))
22804 add_AT_flag (var_die, DW_AT_declaration, 1);
22805 if (decl)
22806 equate_decl_number_to_die (decl, var_die);
22807 return;
22810 if (old_die)
22812 if (declaration)
22814 /* A declaration that has been previously dumped, needs no
22815 further annotations, since it doesn't need location on
22816 the second pass. */
22817 return;
22819 else if (decl_will_get_specification_p (old_die, decl, declaration)
22820 && !get_AT (old_die, DW_AT_specification))
22822 /* Fall-thru so we can make a new variable die along with a
22823 DW_AT_specification. */
22825 else if (origin && old_die->die_parent != context_die)
22827 /* If we will be creating an inlined instance, we need a
22828 new DIE that will get annotated with
22829 DW_AT_abstract_origin. Clear things so we can get a
22830 new DIE. */
22831 gcc_assert (!DECL_ABSTRACT_P (decl));
22832 old_die = NULL;
22834 else
22836 /* If a DIE was dumped early, it still needs location info.
22837 Skip to where we fill the location bits. */
22838 var_die = old_die;
22839 goto gen_variable_die_location;
22843 /* For static data members, the declaration in the class is supposed
22844 to have DW_TAG_member tag in DWARF{3,4} and we emit it for compatibility
22845 also in DWARF2; the specification should still be DW_TAG_variable
22846 referencing the DW_TAG_member DIE. */
22847 if (declaration && class_scope_p (context_die) && dwarf_version < 5)
22848 var_die = new_die (DW_TAG_member, context_die, decl);
22849 else
22850 var_die = new_die (DW_TAG_variable, context_die, decl);
22852 if (origin != NULL)
22853 add_abstract_origin_attribute (var_die, origin);
22855 /* Loop unrolling can create multiple blocks that refer to the same
22856 static variable, so we must test for the DW_AT_declaration flag.
22858 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
22859 copy decls and set the DECL_ABSTRACT_P flag on them instead of
22860 sharing them.
22862 ??? Duplicated blocks have been rewritten to use .debug_ranges. */
22863 else if (decl_will_get_specification_p (old_die, decl, declaration))
22865 /* This is a definition of a C++ class level static. */
22866 add_AT_specification (var_die, old_die);
22867 specialization_p = true;
22868 if (DECL_NAME (decl))
22870 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
22871 struct dwarf_file_data * file_index = lookup_filename (s.file);
22873 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
22874 add_AT_file (var_die, DW_AT_decl_file, file_index);
22876 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
22877 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
22879 if (debug_column_info
22880 && s.column
22881 && (get_AT_unsigned (old_die, DW_AT_decl_column)
22882 != (unsigned) s.column))
22883 add_AT_unsigned (var_die, DW_AT_decl_column, s.column);
22885 if (old_die->die_tag == DW_TAG_member)
22886 add_linkage_name (var_die, decl);
22889 else
22890 add_name_and_src_coords_attributes (var_die, decl, no_linkage_name);
22892 if ((origin == NULL && !specialization_p)
22893 || (origin != NULL
22894 && !DECL_ABSTRACT_P (decl_or_origin)
22895 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
22896 decl_function_context
22897 (decl_or_origin))))
22899 tree type = TREE_TYPE (decl_or_origin);
22901 if (decl_by_reference_p (decl_or_origin))
22902 add_type_attribute (var_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
22903 context_die);
22904 else
22905 add_type_attribute (var_die, type, decl_quals (decl_or_origin), false,
22906 context_die);
22909 if (origin == NULL && !specialization_p)
22911 if (TREE_PUBLIC (decl))
22912 add_AT_flag (var_die, DW_AT_external, 1);
22914 if (DECL_ARTIFICIAL (decl))
22915 add_AT_flag (var_die, DW_AT_artificial, 1);
22917 add_alignment_attribute (var_die, decl);
22919 add_accessibility_attribute (var_die, decl);
22922 if (declaration)
22923 add_AT_flag (var_die, DW_AT_declaration, 1);
22925 if (decl && (DECL_ABSTRACT_P (decl)
22926 || !old_die || is_declaration_die (old_die)))
22927 equate_decl_number_to_die (decl, var_die);
22929 gen_variable_die_location:
22930 if (! declaration
22931 && (! DECL_ABSTRACT_P (decl_or_origin)
22932 /* Local static vars are shared between all clones/inlines,
22933 so emit DW_AT_location on the abstract DIE if DECL_RTL is
22934 already set. */
22935 || (VAR_P (decl_or_origin)
22936 && TREE_STATIC (decl_or_origin)
22937 && DECL_RTL_SET_P (decl_or_origin))))
22939 if (early_dwarf)
22940 add_pubname (decl_or_origin, var_die);
22941 else
22942 add_location_or_const_value_attribute (var_die, decl_or_origin,
22943 decl == NULL);
22945 else
22946 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
22948 if ((dwarf_version >= 4 || !dwarf_strict)
22949 && lang_hooks.decls.decl_dwarf_attribute (decl_or_origin,
22950 DW_AT_const_expr) == 1
22951 && !get_AT (var_die, DW_AT_const_expr)
22952 && !specialization_p)
22953 add_AT_flag (var_die, DW_AT_const_expr, 1);
22955 if (!dwarf_strict)
22957 int inl = lang_hooks.decls.decl_dwarf_attribute (decl_or_origin,
22958 DW_AT_inline);
22959 if (inl != -1
22960 && !get_AT (var_die, DW_AT_inline)
22961 && !specialization_p)
22962 add_AT_unsigned (var_die, DW_AT_inline, inl);
22966 /* Generate a DIE to represent a named constant. */
22968 static void
22969 gen_const_die (tree decl, dw_die_ref context_die)
22971 dw_die_ref const_die;
22972 tree type = TREE_TYPE (decl);
22974 const_die = lookup_decl_die (decl);
22975 if (const_die)
22976 return;
22978 const_die = new_die (DW_TAG_constant, context_die, decl);
22979 equate_decl_number_to_die (decl, const_die);
22980 add_name_and_src_coords_attributes (const_die, decl);
22981 add_type_attribute (const_die, type, TYPE_QUAL_CONST, false, context_die);
22982 if (TREE_PUBLIC (decl))
22983 add_AT_flag (const_die, DW_AT_external, 1);
22984 if (DECL_ARTIFICIAL (decl))
22985 add_AT_flag (const_die, DW_AT_artificial, 1);
22986 tree_add_const_value_attribute_for_decl (const_die, decl);
22989 /* Generate a DIE to represent a label identifier. */
22991 static void
22992 gen_label_die (tree decl, dw_die_ref context_die)
22994 tree origin = decl_ultimate_origin (decl);
22995 dw_die_ref lbl_die = lookup_decl_die (decl);
22996 rtx insn;
22997 char label[MAX_ARTIFICIAL_LABEL_BYTES];
22999 if (!lbl_die)
23001 lbl_die = new_die (DW_TAG_label, context_die, decl);
23002 equate_decl_number_to_die (decl, lbl_die);
23004 if (origin != NULL)
23005 add_abstract_origin_attribute (lbl_die, origin);
23006 else
23007 add_name_and_src_coords_attributes (lbl_die, decl);
23010 if (DECL_ABSTRACT_P (decl))
23011 equate_decl_number_to_die (decl, lbl_die);
23012 else if (! early_dwarf)
23014 insn = DECL_RTL_IF_SET (decl);
23016 /* Deleted labels are programmer specified labels which have been
23017 eliminated because of various optimizations. We still emit them
23018 here so that it is possible to put breakpoints on them. */
23019 if (insn
23020 && (LABEL_P (insn)
23021 || ((NOTE_P (insn)
23022 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
23024 /* When optimization is enabled (via -O) some parts of the compiler
23025 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
23026 represent source-level labels which were explicitly declared by
23027 the user. This really shouldn't be happening though, so catch
23028 it if it ever does happen. */
23029 gcc_assert (!as_a<rtx_insn *> (insn)->deleted ());
23031 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
23032 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
23034 else if (insn
23035 && NOTE_P (insn)
23036 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
23037 && CODE_LABEL_NUMBER (insn) != -1)
23039 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
23040 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
23045 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
23046 attributes to the DIE for a block STMT, to describe where the inlined
23047 function was called from. This is similar to add_src_coords_attributes. */
23049 static inline void
23050 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
23052 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
23054 if (dwarf_version >= 3 || !dwarf_strict)
23056 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
23057 add_AT_unsigned (die, DW_AT_call_line, s.line);
23058 if (debug_column_info && s.column)
23059 add_AT_unsigned (die, DW_AT_call_column, s.column);
23064 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
23065 Add low_pc and high_pc attributes to the DIE for a block STMT. */
23067 static inline void
23068 add_high_low_attributes (tree stmt, dw_die_ref die)
23070 char label[MAX_ARTIFICIAL_LABEL_BYTES];
23072 if (BLOCK_FRAGMENT_CHAIN (stmt)
23073 && (dwarf_version >= 3 || !dwarf_strict))
23075 tree chain, superblock = NULL_TREE;
23076 dw_die_ref pdie;
23077 dw_attr_node *attr = NULL;
23079 if (inlined_function_outer_scope_p (stmt))
23081 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
23082 BLOCK_NUMBER (stmt));
23083 add_AT_lbl_id (die, DW_AT_entry_pc, label);
23086 /* Optimize duplicate .debug_ranges lists or even tails of
23087 lists. If this BLOCK has same ranges as its supercontext,
23088 lookup DW_AT_ranges attribute in the supercontext (and
23089 recursively so), verify that the ranges_table contains the
23090 right values and use it instead of adding a new .debug_range. */
23091 for (chain = stmt, pdie = die;
23092 BLOCK_SAME_RANGE (chain);
23093 chain = BLOCK_SUPERCONTEXT (chain))
23095 dw_attr_node *new_attr;
23097 pdie = pdie->die_parent;
23098 if (pdie == NULL)
23099 break;
23100 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
23101 break;
23102 new_attr = get_AT (pdie, DW_AT_ranges);
23103 if (new_attr == NULL
23104 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
23105 break;
23106 attr = new_attr;
23107 superblock = BLOCK_SUPERCONTEXT (chain);
23109 if (attr != NULL
23110 && ((*ranges_table)[attr->dw_attr_val.v.val_offset].num
23111 == BLOCK_NUMBER (superblock))
23112 && BLOCK_FRAGMENT_CHAIN (superblock))
23114 unsigned long off = attr->dw_attr_val.v.val_offset;
23115 unsigned long supercnt = 0, thiscnt = 0;
23116 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
23117 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
23119 ++supercnt;
23120 gcc_checking_assert ((*ranges_table)[off + supercnt].num
23121 == BLOCK_NUMBER (chain));
23123 gcc_checking_assert ((*ranges_table)[off + supercnt + 1].num == 0);
23124 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
23125 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
23126 ++thiscnt;
23127 gcc_assert (supercnt >= thiscnt);
23128 add_AT_range_list (die, DW_AT_ranges, off + supercnt - thiscnt,
23129 false);
23130 note_rnglist_head (off + supercnt - thiscnt);
23131 return;
23134 unsigned int offset = add_ranges (stmt, true);
23135 add_AT_range_list (die, DW_AT_ranges, offset, false);
23136 note_rnglist_head (offset);
23138 bool prev_in_cold = BLOCK_IN_COLD_SECTION_P (stmt);
23139 chain = BLOCK_FRAGMENT_CHAIN (stmt);
23142 add_ranges (chain, prev_in_cold != BLOCK_IN_COLD_SECTION_P (chain));
23143 prev_in_cold = BLOCK_IN_COLD_SECTION_P (chain);
23144 chain = BLOCK_FRAGMENT_CHAIN (chain);
23146 while (chain);
23147 add_ranges (NULL);
23149 else
23151 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
23152 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
23153 BLOCK_NUMBER (stmt));
23154 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
23155 BLOCK_NUMBER (stmt));
23156 add_AT_low_high_pc (die, label, label_high, false);
23160 /* Generate a DIE for a lexical block. */
23162 static void
23163 gen_lexical_block_die (tree stmt, dw_die_ref context_die)
23165 dw_die_ref old_die = BLOCK_DIE (stmt);
23166 dw_die_ref stmt_die = NULL;
23167 if (!old_die)
23169 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
23170 BLOCK_DIE (stmt) = stmt_die;
23173 if (BLOCK_ABSTRACT (stmt))
23175 if (old_die)
23177 /* This must have been generated early and it won't even
23178 need location information since it's a DW_AT_inline
23179 function. */
23180 if (flag_checking)
23181 for (dw_die_ref c = context_die; c; c = c->die_parent)
23182 if (c->die_tag == DW_TAG_inlined_subroutine
23183 || c->die_tag == DW_TAG_subprogram)
23185 gcc_assert (get_AT (c, DW_AT_inline));
23186 break;
23188 return;
23191 else if (BLOCK_ABSTRACT_ORIGIN (stmt))
23193 /* If this is an inlined instance, create a new lexical die for
23194 anything below to attach DW_AT_abstract_origin to. */
23195 if (old_die)
23197 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
23198 BLOCK_DIE (stmt) = stmt_die;
23199 old_die = NULL;
23202 tree origin = block_ultimate_origin (stmt);
23203 if (origin != NULL_TREE && origin != stmt)
23204 add_abstract_origin_attribute (stmt_die, origin);
23207 if (old_die)
23208 stmt_die = old_die;
23210 /* A non abstract block whose blocks have already been reordered
23211 should have the instruction range for this block. If so, set the
23212 high/low attributes. */
23213 if (!early_dwarf && !BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
23215 gcc_assert (stmt_die);
23216 add_high_low_attributes (stmt, stmt_die);
23219 decls_for_scope (stmt, stmt_die);
23222 /* Generate a DIE for an inlined subprogram. */
23224 static void
23225 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die)
23227 tree decl;
23229 /* The instance of function that is effectively being inlined shall not
23230 be abstract. */
23231 gcc_assert (! BLOCK_ABSTRACT (stmt));
23233 decl = block_ultimate_origin (stmt);
23235 /* Make sure any inlined functions are known to be inlineable. */
23236 gcc_checking_assert (DECL_ABSTRACT_P (decl)
23237 || cgraph_function_possibly_inlined_p (decl));
23239 /* Emit info for the abstract instance first, if we haven't yet. We
23240 must emit this even if the block is abstract, otherwise when we
23241 emit the block below (or elsewhere), we may end up trying to emit
23242 a die whose origin die hasn't been emitted, and crashing. */
23243 dwarf2out_abstract_function (decl);
23245 if (! BLOCK_ABSTRACT (stmt))
23247 dw_die_ref subr_die
23248 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
23250 if (call_arg_locations)
23251 BLOCK_DIE (stmt) = subr_die;
23252 add_abstract_origin_attribute (subr_die, decl);
23253 if (TREE_ASM_WRITTEN (stmt))
23254 add_high_low_attributes (stmt, subr_die);
23255 add_call_src_coords_attributes (stmt, subr_die);
23257 decls_for_scope (stmt, subr_die);
23261 /* Generate a DIE for a field in a record, or structure. CTX is required: see
23262 the comment for VLR_CONTEXT. */
23264 static void
23265 gen_field_die (tree decl, struct vlr_context *ctx, dw_die_ref context_die)
23267 dw_die_ref decl_die;
23269 if (TREE_TYPE (decl) == error_mark_node)
23270 return;
23272 decl_die = new_die (DW_TAG_member, context_die, decl);
23273 add_name_and_src_coords_attributes (decl_die, decl);
23274 add_type_attribute (decl_die, member_declared_type (decl), decl_quals (decl),
23275 TYPE_REVERSE_STORAGE_ORDER (DECL_FIELD_CONTEXT (decl)),
23276 context_die);
23278 if (DECL_BIT_FIELD_TYPE (decl))
23280 add_byte_size_attribute (decl_die, decl);
23281 add_bit_size_attribute (decl_die, decl);
23282 add_bit_offset_attribute (decl_die, decl, ctx);
23285 add_alignment_attribute (decl_die, decl);
23287 /* If we have a variant part offset, then we are supposed to process a member
23288 of a QUAL_UNION_TYPE, which is how we represent variant parts in
23289 trees. */
23290 gcc_assert (ctx->variant_part_offset == NULL_TREE
23291 || TREE_CODE (DECL_FIELD_CONTEXT (decl)) != QUAL_UNION_TYPE);
23292 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
23293 add_data_member_location_attribute (decl_die, decl, ctx);
23295 if (DECL_ARTIFICIAL (decl))
23296 add_AT_flag (decl_die, DW_AT_artificial, 1);
23298 add_accessibility_attribute (decl_die, decl);
23300 /* Equate decl number to die, so that we can look up this decl later on. */
23301 equate_decl_number_to_die (decl, decl_die);
23304 #if 0
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_pointer_type_die (tree type, dw_die_ref context_die)
23313 dw_die_ref ptr_die
23314 = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
23316 equate_type_number_to_die (type, ptr_die);
23317 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
23318 context_die);
23319 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
23322 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
23323 Use modified_type_die instead.
23324 We keep this code here just in case these types of DIEs may be needed to
23325 represent certain things in other languages (e.g. Pascal) someday. */
23327 static void
23328 gen_reference_type_die (tree type, dw_die_ref context_die)
23330 dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
23332 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
23333 ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
23334 else
23335 ref_die = new_die (DW_TAG_reference_type, scope_die, type);
23337 equate_type_number_to_die (type, ref_die);
23338 add_type_attribute (ref_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
23339 context_die);
23340 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
23342 #endif
23344 /* Generate a DIE for a pointer to a member type. TYPE can be an
23345 OFFSET_TYPE, for a pointer to data member, or a RECORD_TYPE, for a
23346 pointer to member function. */
23348 static void
23349 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
23351 if (lookup_type_die (type))
23352 return;
23354 dw_die_ref ptr_die = new_die (DW_TAG_ptr_to_member_type,
23355 scope_die_for (type, context_die), type);
23357 equate_type_number_to_die (type, ptr_die);
23358 add_AT_die_ref (ptr_die, DW_AT_containing_type,
23359 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
23360 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
23361 context_die);
23362 add_alignment_attribute (ptr_die, type);
23364 if (TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE
23365 && TREE_CODE (TREE_TYPE (type)) != METHOD_TYPE)
23367 dw_loc_descr_ref op = new_loc_descr (DW_OP_plus, 0, 0);
23368 add_AT_loc (ptr_die, DW_AT_use_location, op);
23372 static char *producer_string;
23374 /* Return a heap allocated producer string including command line options
23375 if -grecord-gcc-switches. */
23377 static char *
23378 gen_producer_string (void)
23380 size_t j;
23381 auto_vec<const char *> switches;
23382 const char *language_string = lang_hooks.name;
23383 char *producer, *tail;
23384 const char *p;
23385 size_t len = dwarf_record_gcc_switches ? 0 : 3;
23386 size_t plen = strlen (language_string) + 1 + strlen (version_string);
23388 for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
23389 switch (save_decoded_options[j].opt_index)
23391 case OPT_o:
23392 case OPT_d:
23393 case OPT_dumpbase:
23394 case OPT_dumpdir:
23395 case OPT_auxbase:
23396 case OPT_auxbase_strip:
23397 case OPT_quiet:
23398 case OPT_version:
23399 case OPT_v:
23400 case OPT_w:
23401 case OPT_L:
23402 case OPT_D:
23403 case OPT_I:
23404 case OPT_U:
23405 case OPT_SPECIAL_unknown:
23406 case OPT_SPECIAL_ignore:
23407 case OPT_SPECIAL_program_name:
23408 case OPT_SPECIAL_input_file:
23409 case OPT_grecord_gcc_switches:
23410 case OPT_gno_record_gcc_switches:
23411 case OPT__output_pch_:
23412 case OPT_fdiagnostics_show_location_:
23413 case OPT_fdiagnostics_show_option:
23414 case OPT_fdiagnostics_show_caret:
23415 case OPT_fdiagnostics_color_:
23416 case OPT_fverbose_asm:
23417 case OPT____:
23418 case OPT__sysroot_:
23419 case OPT_nostdinc:
23420 case OPT_nostdinc__:
23421 case OPT_fpreprocessed:
23422 case OPT_fltrans_output_list_:
23423 case OPT_fresolution_:
23424 case OPT_fdebug_prefix_map_:
23425 /* Ignore these. */
23426 continue;
23427 default:
23428 if (cl_options[save_decoded_options[j].opt_index].flags
23429 & CL_NO_DWARF_RECORD)
23430 continue;
23431 gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
23432 == '-');
23433 switch (save_decoded_options[j].canonical_option[0][1])
23435 case 'M':
23436 case 'i':
23437 case 'W':
23438 continue;
23439 case 'f':
23440 if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
23441 "dump", 4) == 0)
23442 continue;
23443 break;
23444 default:
23445 break;
23447 switches.safe_push (save_decoded_options[j].orig_option_with_args_text);
23448 len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
23449 break;
23452 producer = XNEWVEC (char, plen + 1 + len + 1);
23453 tail = producer;
23454 sprintf (tail, "%s %s", language_string, version_string);
23455 tail += plen;
23457 FOR_EACH_VEC_ELT (switches, j, p)
23459 len = strlen (p);
23460 *tail = ' ';
23461 memcpy (tail + 1, p, len);
23462 tail += len + 1;
23465 *tail = '\0';
23466 return producer;
23469 /* Given a C and/or C++ language/version string return the "highest".
23470 C++ is assumed to be "higher" than C in this case. Used for merging
23471 LTO translation unit languages. */
23472 static const char *
23473 highest_c_language (const char *lang1, const char *lang2)
23475 if (strcmp ("GNU C++14", lang1) == 0 || strcmp ("GNU C++14", lang2) == 0)
23476 return "GNU C++14";
23477 if (strcmp ("GNU C++11", lang1) == 0 || strcmp ("GNU C++11", lang2) == 0)
23478 return "GNU C++11";
23479 if (strcmp ("GNU C++98", lang1) == 0 || strcmp ("GNU C++98", lang2) == 0)
23480 return "GNU C++98";
23482 if (strcmp ("GNU C11", lang1) == 0 || strcmp ("GNU C11", lang2) == 0)
23483 return "GNU C11";
23484 if (strcmp ("GNU C99", lang1) == 0 || strcmp ("GNU C99", lang2) == 0)
23485 return "GNU C99";
23486 if (strcmp ("GNU C89", lang1) == 0 || strcmp ("GNU C89", lang2) == 0)
23487 return "GNU C89";
23489 gcc_unreachable ();
23493 /* Generate the DIE for the compilation unit. */
23495 static dw_die_ref
23496 gen_compile_unit_die (const char *filename)
23498 dw_die_ref die;
23499 const char *language_string = lang_hooks.name;
23500 int language;
23502 die = new_die (DW_TAG_compile_unit, NULL, NULL);
23504 if (filename)
23506 add_name_attribute (die, filename);
23507 /* Don't add cwd for <built-in>. */
23508 if (filename[0] != '<')
23509 add_comp_dir_attribute (die);
23512 add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
23514 /* If our producer is LTO try to figure out a common language to use
23515 from the global list of translation units. */
23516 if (strcmp (language_string, "GNU GIMPLE") == 0)
23518 unsigned i;
23519 tree t;
23520 const char *common_lang = NULL;
23522 FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
23524 if (!TRANSLATION_UNIT_LANGUAGE (t))
23525 continue;
23526 if (!common_lang)
23527 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
23528 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
23530 else if (strncmp (common_lang, "GNU C", 5) == 0
23531 && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
23532 /* Mixing C and C++ is ok, use C++ in that case. */
23533 common_lang = highest_c_language (common_lang,
23534 TRANSLATION_UNIT_LANGUAGE (t));
23535 else
23537 /* Fall back to C. */
23538 common_lang = NULL;
23539 break;
23543 if (common_lang)
23544 language_string = common_lang;
23547 language = DW_LANG_C;
23548 if (strncmp (language_string, "GNU C", 5) == 0
23549 && ISDIGIT (language_string[5]))
23551 language = DW_LANG_C89;
23552 if (dwarf_version >= 3 || !dwarf_strict)
23554 if (strcmp (language_string, "GNU C89") != 0)
23555 language = DW_LANG_C99;
23557 if (dwarf_version >= 5 /* || !dwarf_strict */)
23558 if (strcmp (language_string, "GNU C11") == 0)
23559 language = DW_LANG_C11;
23562 else if (strncmp (language_string, "GNU C++", 7) == 0)
23564 language = DW_LANG_C_plus_plus;
23565 if (dwarf_version >= 5 /* || !dwarf_strict */)
23567 if (strcmp (language_string, "GNU C++11") == 0)
23568 language = DW_LANG_C_plus_plus_11;
23569 else if (strcmp (language_string, "GNU C++14") == 0)
23570 language = DW_LANG_C_plus_plus_14;
23573 else if (strcmp (language_string, "GNU F77") == 0)
23574 language = DW_LANG_Fortran77;
23575 else if (strcmp (language_string, "GNU Pascal") == 0)
23576 language = DW_LANG_Pascal83;
23577 else if (dwarf_version >= 3 || !dwarf_strict)
23579 if (strcmp (language_string, "GNU Ada") == 0)
23580 language = DW_LANG_Ada95;
23581 else if (strncmp (language_string, "GNU Fortran", 11) == 0)
23583 language = DW_LANG_Fortran95;
23584 if (dwarf_version >= 5 /* || !dwarf_strict */)
23586 if (strcmp (language_string, "GNU Fortran2003") == 0)
23587 language = DW_LANG_Fortran03;
23588 else if (strcmp (language_string, "GNU Fortran2008") == 0)
23589 language = DW_LANG_Fortran08;
23592 else if (strcmp (language_string, "GNU Java") == 0)
23593 language = DW_LANG_Java;
23594 else if (strcmp (language_string, "GNU Objective-C") == 0)
23595 language = DW_LANG_ObjC;
23596 else if (strcmp (language_string, "GNU Objective-C++") == 0)
23597 language = DW_LANG_ObjC_plus_plus;
23598 else if (dwarf_version >= 5 || !dwarf_strict)
23600 if (strcmp (language_string, "GNU Go") == 0)
23601 language = DW_LANG_Go;
23604 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
23605 else if (strncmp (language_string, "GNU Fortran", 11) == 0)
23606 language = DW_LANG_Fortran90;
23608 add_AT_unsigned (die, DW_AT_language, language);
23610 switch (language)
23612 case DW_LANG_Fortran77:
23613 case DW_LANG_Fortran90:
23614 case DW_LANG_Fortran95:
23615 case DW_LANG_Fortran03:
23616 case DW_LANG_Fortran08:
23617 /* Fortran has case insensitive identifiers and the front-end
23618 lowercases everything. */
23619 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
23620 break;
23621 default:
23622 /* The default DW_ID_case_sensitive doesn't need to be specified. */
23623 break;
23625 return die;
23628 /* Generate the DIE for a base class. */
23630 static void
23631 gen_inheritance_die (tree binfo, tree access, tree type,
23632 dw_die_ref context_die)
23634 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
23635 struct vlr_context ctx = { type, NULL };
23637 add_type_attribute (die, BINFO_TYPE (binfo), TYPE_UNQUALIFIED, false,
23638 context_die);
23639 add_data_member_location_attribute (die, binfo, &ctx);
23641 if (BINFO_VIRTUAL_P (binfo))
23642 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
23644 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
23645 children, otherwise the default is DW_ACCESS_public. In DWARF2
23646 the default has always been DW_ACCESS_private. */
23647 if (access == access_public_node)
23649 if (dwarf_version == 2
23650 || context_die->die_tag == DW_TAG_class_type)
23651 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
23653 else if (access == access_protected_node)
23654 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
23655 else if (dwarf_version > 2
23656 && context_die->die_tag != DW_TAG_class_type)
23657 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
23660 /* Return whether DECL is a FIELD_DECL that represents the variant part of a
23661 structure. */
23662 static bool
23663 is_variant_part (tree decl)
23665 return (TREE_CODE (decl) == FIELD_DECL
23666 && TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE);
23669 /* Check that OPERAND is a reference to a field in STRUCT_TYPE. If it is,
23670 return the FIELD_DECL. Return NULL_TREE otherwise. */
23672 static tree
23673 analyze_discr_in_predicate (tree operand, tree struct_type)
23675 bool continue_stripping = true;
23676 while (continue_stripping)
23677 switch (TREE_CODE (operand))
23679 CASE_CONVERT:
23680 operand = TREE_OPERAND (operand, 0);
23681 break;
23682 default:
23683 continue_stripping = false;
23684 break;
23687 /* Match field access to members of struct_type only. */
23688 if (TREE_CODE (operand) == COMPONENT_REF
23689 && TREE_CODE (TREE_OPERAND (operand, 0)) == PLACEHOLDER_EXPR
23690 && TREE_TYPE (TREE_OPERAND (operand, 0)) == struct_type
23691 && TREE_CODE (TREE_OPERAND (operand, 1)) == FIELD_DECL)
23692 return TREE_OPERAND (operand, 1);
23693 else
23694 return NULL_TREE;
23697 /* Check that SRC is a constant integer that can be represented as a native
23698 integer constant (either signed or unsigned). If so, store it into DEST and
23699 return true. Return false otherwise. */
23701 static bool
23702 get_discr_value (tree src, dw_discr_value *dest)
23704 bool is_unsigned = TYPE_UNSIGNED (TREE_TYPE (src));
23706 if (TREE_CODE (src) != INTEGER_CST
23707 || !(is_unsigned ? tree_fits_uhwi_p (src) : tree_fits_shwi_p (src)))
23708 return false;
23710 dest->pos = is_unsigned;
23711 if (is_unsigned)
23712 dest->v.uval = tree_to_uhwi (src);
23713 else
23714 dest->v.sval = tree_to_shwi (src);
23716 return true;
23719 /* Try to extract synthetic properties out of VARIANT_PART_DECL, which is a
23720 FIELD_DECL in STRUCT_TYPE that represents a variant part. If unsuccessful,
23721 store NULL_TREE in DISCR_DECL. Otherwise:
23723 - store the discriminant field in STRUCT_TYPE that controls the variant
23724 part to *DISCR_DECL
23726 - put in *DISCR_LISTS_P an array where for each variant, the item
23727 represents the corresponding matching list of discriminant values.
23729 - put in *DISCR_LISTS_LENGTH the number of variants, which is the size of
23730 the above array.
23732 Note that when the array is allocated (i.e. when the analysis is
23733 successful), it is up to the caller to free the array. */
23735 static void
23736 analyze_variants_discr (tree variant_part_decl,
23737 tree struct_type,
23738 tree *discr_decl,
23739 dw_discr_list_ref **discr_lists_p,
23740 unsigned *discr_lists_length)
23742 tree variant_part_type = TREE_TYPE (variant_part_decl);
23743 tree variant;
23744 dw_discr_list_ref *discr_lists;
23745 unsigned i;
23747 /* Compute how many variants there are in this variant part. */
23748 *discr_lists_length = 0;
23749 for (variant = TYPE_FIELDS (variant_part_type);
23750 variant != NULL_TREE;
23751 variant = DECL_CHAIN (variant))
23752 ++*discr_lists_length;
23754 *discr_decl = NULL_TREE;
23755 *discr_lists_p
23756 = (dw_discr_list_ref *) xcalloc (*discr_lists_length,
23757 sizeof (**discr_lists_p));
23758 discr_lists = *discr_lists_p;
23760 /* And then analyze all variants to extract discriminant information for all
23761 of them. This analysis is conservative: as soon as we detect something we
23762 do not support, abort everything and pretend we found nothing. */
23763 for (variant = TYPE_FIELDS (variant_part_type), i = 0;
23764 variant != NULL_TREE;
23765 variant = DECL_CHAIN (variant), ++i)
23767 tree match_expr = DECL_QUALIFIER (variant);
23769 /* Now, try to analyze the predicate and deduce a discriminant for
23770 it. */
23771 if (match_expr == boolean_true_node)
23772 /* Typically happens for the default variant: it matches all cases that
23773 previous variants rejected. Don't output any matching value for
23774 this one. */
23775 continue;
23777 /* The following loop tries to iterate over each discriminant
23778 possibility: single values or ranges. */
23779 while (match_expr != NULL_TREE)
23781 tree next_round_match_expr;
23782 tree candidate_discr = NULL_TREE;
23783 dw_discr_list_ref new_node = NULL;
23785 /* Possibilities are matched one after the other by nested
23786 TRUTH_ORIF_EXPR expressions. Process the current possibility and
23787 continue with the rest at next iteration. */
23788 if (TREE_CODE (match_expr) == TRUTH_ORIF_EXPR)
23790 next_round_match_expr = TREE_OPERAND (match_expr, 0);
23791 match_expr = TREE_OPERAND (match_expr, 1);
23793 else
23794 next_round_match_expr = NULL_TREE;
23796 if (match_expr == boolean_false_node)
23797 /* This sub-expression matches nothing: just wait for the next
23798 one. */
23801 else if (TREE_CODE (match_expr) == EQ_EXPR)
23803 /* We are matching: <discr_field> == <integer_cst>
23804 This sub-expression matches a single value. */
23805 tree integer_cst = TREE_OPERAND (match_expr, 1);
23807 candidate_discr
23808 = analyze_discr_in_predicate (TREE_OPERAND (match_expr, 0),
23809 struct_type);
23811 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
23812 if (!get_discr_value (integer_cst,
23813 &new_node->dw_discr_lower_bound))
23814 goto abort;
23815 new_node->dw_discr_range = false;
23818 else if (TREE_CODE (match_expr) == TRUTH_ANDIF_EXPR)
23820 /* We are matching:
23821 <discr_field> > <integer_cst>
23822 && <discr_field> < <integer_cst>.
23823 This sub-expression matches the range of values between the
23824 two matched integer constants. Note that comparisons can be
23825 inclusive or exclusive. */
23826 tree candidate_discr_1, candidate_discr_2;
23827 tree lower_cst, upper_cst;
23828 bool lower_cst_included, upper_cst_included;
23829 tree lower_op = TREE_OPERAND (match_expr, 0);
23830 tree upper_op = TREE_OPERAND (match_expr, 1);
23832 /* When the comparison is exclusive, the integer constant is not
23833 the discriminant range bound we are looking for: we will have
23834 to increment or decrement it. */
23835 if (TREE_CODE (lower_op) == GE_EXPR)
23836 lower_cst_included = true;
23837 else if (TREE_CODE (lower_op) == GT_EXPR)
23838 lower_cst_included = false;
23839 else
23840 goto abort;
23842 if (TREE_CODE (upper_op) == LE_EXPR)
23843 upper_cst_included = true;
23844 else if (TREE_CODE (upper_op) == LT_EXPR)
23845 upper_cst_included = false;
23846 else
23847 goto abort;
23849 /* Extract the discriminant from the first operand and check it
23850 is consistant with the same analysis in the second
23851 operand. */
23852 candidate_discr_1
23853 = analyze_discr_in_predicate (TREE_OPERAND (lower_op, 0),
23854 struct_type);
23855 candidate_discr_2
23856 = analyze_discr_in_predicate (TREE_OPERAND (upper_op, 0),
23857 struct_type);
23858 if (candidate_discr_1 == candidate_discr_2)
23859 candidate_discr = candidate_discr_1;
23860 else
23861 goto abort;
23863 /* Extract bounds from both. */
23864 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
23865 lower_cst = TREE_OPERAND (lower_op, 1);
23866 upper_cst = TREE_OPERAND (upper_op, 1);
23868 if (!lower_cst_included)
23869 lower_cst
23870 = fold_build2 (PLUS_EXPR, TREE_TYPE (lower_cst), lower_cst,
23871 build_int_cst (TREE_TYPE (lower_cst), 1));
23872 if (!upper_cst_included)
23873 upper_cst
23874 = fold_build2 (MINUS_EXPR, TREE_TYPE (upper_cst), upper_cst,
23875 build_int_cst (TREE_TYPE (upper_cst), 1));
23877 if (!get_discr_value (lower_cst,
23878 &new_node->dw_discr_lower_bound)
23879 || !get_discr_value (upper_cst,
23880 &new_node->dw_discr_upper_bound))
23881 goto abort;
23883 new_node->dw_discr_range = true;
23886 else
23887 /* Unsupported sub-expression: we cannot determine the set of
23888 matching discriminant values. Abort everything. */
23889 goto abort;
23891 /* If the discriminant info is not consistant with what we saw so
23892 far, consider the analysis failed and abort everything. */
23893 if (candidate_discr == NULL_TREE
23894 || (*discr_decl != NULL_TREE && candidate_discr != *discr_decl))
23895 goto abort;
23896 else
23897 *discr_decl = candidate_discr;
23899 if (new_node != NULL)
23901 new_node->dw_discr_next = discr_lists[i];
23902 discr_lists[i] = new_node;
23904 match_expr = next_round_match_expr;
23908 /* If we reach this point, we could match everything we were interested
23909 in. */
23910 return;
23912 abort:
23913 /* Clean all data structure and return no result. */
23914 free (*discr_lists_p);
23915 *discr_lists_p = NULL;
23916 *discr_decl = NULL_TREE;
23919 /* Generate a DIE to represent VARIANT_PART_DECL, a variant part that is part
23920 of STRUCT_TYPE, a record type. This new DIE is emitted as the next child
23921 under CONTEXT_DIE.
23923 Variant parts are supposed to be implemented as a FIELD_DECL whose type is a
23924 QUAL_UNION_TYPE: this is the VARIANT_PART_DECL parameter. The members for
23925 this type, which are record types, represent the available variants and each
23926 has a DECL_QUALIFIER attribute. The discriminant and the discriminant
23927 values are inferred from these attributes.
23929 In trees, the offsets for the fields inside these sub-records are relative
23930 to the variant part itself, whereas the corresponding DIEs should have
23931 offset attributes that are relative to the embedding record base address.
23932 This is why the caller must provide a VARIANT_PART_OFFSET expression: it
23933 must be an expression that computes the offset of the variant part to
23934 describe in DWARF. */
23936 static void
23937 gen_variant_part (tree variant_part_decl, struct vlr_context *vlr_ctx,
23938 dw_die_ref context_die)
23940 const tree variant_part_type = TREE_TYPE (variant_part_decl);
23941 tree variant_part_offset = vlr_ctx->variant_part_offset;
23942 struct loc_descr_context ctx = {
23943 vlr_ctx->struct_type, /* context_type */
23944 NULL_TREE, /* base_decl */
23945 NULL, /* dpi */
23946 false, /* placeholder_arg */
23947 false /* placeholder_seen */
23950 /* The FIELD_DECL node in STRUCT_TYPE that acts as the discriminant, or
23951 NULL_TREE if there is no such field. */
23952 tree discr_decl = NULL_TREE;
23953 dw_discr_list_ref *discr_lists;
23954 unsigned discr_lists_length = 0;
23955 unsigned i;
23957 dw_die_ref dwarf_proc_die = NULL;
23958 dw_die_ref variant_part_die
23959 = new_die (DW_TAG_variant_part, context_die, variant_part_type);
23961 equate_decl_number_to_die (variant_part_decl, variant_part_die);
23963 analyze_variants_discr (variant_part_decl, vlr_ctx->struct_type,
23964 &discr_decl, &discr_lists, &discr_lists_length);
23966 if (discr_decl != NULL_TREE)
23968 dw_die_ref discr_die = lookup_decl_die (discr_decl);
23970 if (discr_die)
23971 add_AT_die_ref (variant_part_die, DW_AT_discr, discr_die);
23972 else
23973 /* We have no DIE for the discriminant, so just discard all
23974 discrimimant information in the output. */
23975 discr_decl = NULL_TREE;
23978 /* If the offset for this variant part is more complex than a constant,
23979 create a DWARF procedure for it so that we will not have to generate DWARF
23980 expressions for it for each member. */
23981 if (TREE_CODE (variant_part_offset) != INTEGER_CST
23982 && (dwarf_version >= 3 || !dwarf_strict))
23984 const tree dwarf_proc_fndecl
23985 = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL, NULL_TREE,
23986 build_function_type (TREE_TYPE (variant_part_offset),
23987 NULL_TREE));
23988 const tree dwarf_proc_call = build_call_expr (dwarf_proc_fndecl, 0);
23989 const dw_loc_descr_ref dwarf_proc_body
23990 = loc_descriptor_from_tree (variant_part_offset, 0, &ctx);
23992 dwarf_proc_die = new_dwarf_proc_die (dwarf_proc_body,
23993 dwarf_proc_fndecl, context_die);
23994 if (dwarf_proc_die != NULL)
23995 variant_part_offset = dwarf_proc_call;
23998 /* Output DIEs for all variants. */
23999 i = 0;
24000 for (tree variant = TYPE_FIELDS (variant_part_type);
24001 variant != NULL_TREE;
24002 variant = DECL_CHAIN (variant), ++i)
24004 tree variant_type = TREE_TYPE (variant);
24005 dw_die_ref variant_die;
24007 /* All variants (i.e. members of a variant part) are supposed to be
24008 encoded as structures. Sub-variant parts are QUAL_UNION_TYPE fields
24009 under these records. */
24010 gcc_assert (TREE_CODE (variant_type) == RECORD_TYPE);
24012 variant_die = new_die (DW_TAG_variant, variant_part_die, variant_type);
24013 equate_decl_number_to_die (variant, variant_die);
24015 /* Output discriminant values this variant matches, if any. */
24016 if (discr_decl == NULL || discr_lists[i] == NULL)
24017 /* In the case we have discriminant information at all, this is
24018 probably the default variant: as the standard says, don't
24019 output any discriminant value/list attribute. */
24021 else if (discr_lists[i]->dw_discr_next == NULL
24022 && !discr_lists[i]->dw_discr_range)
24023 /* If there is only one accepted value, don't bother outputting a
24024 list. */
24025 add_discr_value (variant_die, &discr_lists[i]->dw_discr_lower_bound);
24026 else
24027 add_discr_list (variant_die, discr_lists[i]);
24029 for (tree member = TYPE_FIELDS (variant_type);
24030 member != NULL_TREE;
24031 member = DECL_CHAIN (member))
24033 struct vlr_context vlr_sub_ctx = {
24034 vlr_ctx->struct_type, /* struct_type */
24035 NULL /* variant_part_offset */
24037 if (is_variant_part (member))
24039 /* All offsets for fields inside variant parts are relative to
24040 the top-level embedding RECORD_TYPE's base address. On the
24041 other hand, offsets in GCC's types are relative to the
24042 nested-most variant part. So we have to sum offsets each time
24043 we recurse. */
24045 vlr_sub_ctx.variant_part_offset
24046 = fold_build2 (PLUS_EXPR, TREE_TYPE (variant_part_offset),
24047 variant_part_offset, byte_position (member));
24048 gen_variant_part (member, &vlr_sub_ctx, variant_die);
24050 else
24052 vlr_sub_ctx.variant_part_offset = variant_part_offset;
24053 gen_decl_die (member, NULL, &vlr_sub_ctx, variant_die);
24058 free (discr_lists);
24061 /* Generate a DIE for a class member. */
24063 static void
24064 gen_member_die (tree type, dw_die_ref context_die)
24066 tree member;
24067 tree binfo = TYPE_BINFO (type);
24068 dw_die_ref child;
24070 /* If this is not an incomplete type, output descriptions of each of its
24071 members. Note that as we output the DIEs necessary to represent the
24072 members of this record or union type, we will also be trying to output
24073 DIEs to represent the *types* of those members. However the `type'
24074 function (above) will specifically avoid generating type DIEs for member
24075 types *within* the list of member DIEs for this (containing) type except
24076 for those types (of members) which are explicitly marked as also being
24077 members of this (containing) type themselves. The g++ front- end can
24078 force any given type to be treated as a member of some other (containing)
24079 type by setting the TYPE_CONTEXT of the given (member) type to point to
24080 the TREE node representing the appropriate (containing) type. */
24082 /* First output info about the base classes. */
24083 if (binfo)
24085 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
24086 int i;
24087 tree base;
24089 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
24090 gen_inheritance_die (base,
24091 (accesses ? (*accesses)[i] : access_public_node),
24092 type,
24093 context_die);
24096 /* Now output info about the data members and type members. */
24097 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
24099 struct vlr_context vlr_ctx = { type, NULL_TREE };
24100 bool static_inline_p
24101 = (TREE_STATIC (member)
24102 && (lang_hooks.decls.decl_dwarf_attribute (member, DW_AT_inline)
24103 != -1));
24105 /* If we thought we were generating minimal debug info for TYPE
24106 and then changed our minds, some of the member declarations
24107 may have already been defined. Don't define them again, but
24108 do put them in the right order. */
24110 child = lookup_decl_die (member);
24111 if (child)
24113 /* Handle inline static data members, which only have in-class
24114 declarations. */
24115 dw_die_ref ref = NULL;
24116 if (child->die_tag == DW_TAG_variable
24117 && child->die_parent == comp_unit_die ())
24119 ref = get_AT_ref (child, DW_AT_specification);
24120 /* For C++17 inline static data members followed by redundant
24121 out of class redeclaration, we might get here with
24122 child being the DIE created for the out of class
24123 redeclaration and with its DW_AT_specification being
24124 the DIE created for in-class definition. We want to
24125 reparent the latter, and don't want to create another
24126 DIE with DW_AT_specification in that case, because
24127 we already have one. */
24128 if (ref
24129 && static_inline_p
24130 && ref->die_tag == DW_TAG_variable
24131 && ref->die_parent == comp_unit_die ()
24132 && get_AT (ref, DW_AT_specification) == NULL)
24134 child = ref;
24135 ref = NULL;
24136 static_inline_p = false;
24139 if (child->die_tag == DW_TAG_variable
24140 && child->die_parent == comp_unit_die ()
24141 && ref == NULL)
24143 reparent_child (child, context_die);
24144 if (dwarf_version < 5)
24145 child->die_tag = DW_TAG_member;
24147 else
24148 splice_child_die (context_die, child);
24151 /* Do not generate standard DWARF for variant parts if we are generating
24152 the corresponding GNAT encodings: DIEs generated for both would
24153 conflict in our mappings. */
24154 else if (is_variant_part (member)
24155 && gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL)
24157 vlr_ctx.variant_part_offset = byte_position (member);
24158 gen_variant_part (member, &vlr_ctx, context_die);
24160 else
24162 vlr_ctx.variant_part_offset = NULL_TREE;
24163 gen_decl_die (member, NULL, &vlr_ctx, context_die);
24166 /* For C++ inline static data members emit immediately a DW_TAG_variable
24167 DIE that will refer to that DW_TAG_member/DW_TAG_variable through
24168 DW_AT_specification. */
24169 if (static_inline_p)
24171 int old_extern = DECL_EXTERNAL (member);
24172 DECL_EXTERNAL (member) = 0;
24173 gen_decl_die (member, NULL, NULL, comp_unit_die ());
24174 DECL_EXTERNAL (member) = old_extern;
24178 /* We do not keep type methods in type variants. */
24179 gcc_assert (TYPE_MAIN_VARIANT (type) == type);
24180 /* Now output info about the function members (if any). */
24181 if (TYPE_METHODS (type) != error_mark_node)
24182 for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
24184 /* Don't include clones in the member list. */
24185 if (DECL_ABSTRACT_ORIGIN (member))
24186 continue;
24187 /* Nor constructors for anonymous classes. */
24188 if (DECL_ARTIFICIAL (member)
24189 && dwarf2_name (member, 0) == NULL)
24190 continue;
24192 child = lookup_decl_die (member);
24193 if (child)
24194 splice_child_die (context_die, child);
24195 else
24196 gen_decl_die (member, NULL, NULL, context_die);
24200 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
24201 is set, we pretend that the type was never defined, so we only get the
24202 member DIEs needed by later specification DIEs. */
24204 static void
24205 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
24206 enum debug_info_usage usage)
24208 if (TREE_ASM_WRITTEN (type))
24210 /* Fill in the bound of variable-length fields in late dwarf if
24211 still incomplete. */
24212 if (!early_dwarf && variably_modified_type_p (type, NULL))
24213 for (tree member = TYPE_FIELDS (type);
24214 member;
24215 member = DECL_CHAIN (member))
24216 fill_variable_array_bounds (TREE_TYPE (member));
24217 return;
24220 dw_die_ref type_die = lookup_type_die (type);
24221 dw_die_ref scope_die = 0;
24222 int nested = 0;
24223 int complete = (TYPE_SIZE (type)
24224 && (! TYPE_STUB_DECL (type)
24225 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
24226 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
24227 complete = complete && should_emit_struct_debug (type, usage);
24229 if (type_die && ! complete)
24230 return;
24232 if (TYPE_CONTEXT (type) != NULL_TREE
24233 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
24234 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
24235 nested = 1;
24237 scope_die = scope_die_for (type, context_die);
24239 /* Generate child dies for template paramaters. */
24240 if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
24241 schedule_generic_params_dies_gen (type);
24243 if (! type_die || (nested && is_cu_die (scope_die)))
24244 /* First occurrence of type or toplevel definition of nested class. */
24246 dw_die_ref old_die = type_die;
24248 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
24249 ? record_type_tag (type) : DW_TAG_union_type,
24250 scope_die, type);
24251 equate_type_number_to_die (type, type_die);
24252 if (old_die)
24253 add_AT_specification (type_die, old_die);
24254 else
24255 add_name_attribute (type_die, type_tag (type));
24257 else
24258 remove_AT (type_die, DW_AT_declaration);
24260 /* If this type has been completed, then give it a byte_size attribute and
24261 then give a list of members. */
24262 if (complete && !ns_decl)
24264 /* Prevent infinite recursion in cases where the type of some member of
24265 this type is expressed in terms of this type itself. */
24266 TREE_ASM_WRITTEN (type) = 1;
24267 add_byte_size_attribute (type_die, type);
24268 add_alignment_attribute (type_die, type);
24269 if (TYPE_STUB_DECL (type) != NULL_TREE)
24271 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
24272 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
24275 /* If the first reference to this type was as the return type of an
24276 inline function, then it may not have a parent. Fix this now. */
24277 if (type_die->die_parent == NULL)
24278 add_child_die (scope_die, type_die);
24280 push_decl_scope (type);
24281 gen_member_die (type, type_die);
24282 pop_decl_scope ();
24284 add_gnat_descriptive_type_attribute (type_die, type, context_die);
24285 if (TYPE_ARTIFICIAL (type))
24286 add_AT_flag (type_die, DW_AT_artificial, 1);
24288 /* GNU extension: Record what type our vtable lives in. */
24289 if (TYPE_VFIELD (type))
24291 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
24293 gen_type_die (vtype, context_die);
24294 add_AT_die_ref (type_die, DW_AT_containing_type,
24295 lookup_type_die (vtype));
24298 else
24300 add_AT_flag (type_die, DW_AT_declaration, 1);
24302 /* We don't need to do this for function-local types. */
24303 if (TYPE_STUB_DECL (type)
24304 && ! decl_function_context (TYPE_STUB_DECL (type)))
24305 vec_safe_push (incomplete_types, type);
24308 if (get_AT (type_die, DW_AT_name))
24309 add_pubtype (type, type_die);
24312 /* Generate a DIE for a subroutine _type_. */
24314 static void
24315 gen_subroutine_type_die (tree type, dw_die_ref context_die)
24317 tree return_type = TREE_TYPE (type);
24318 dw_die_ref subr_die
24319 = new_die (DW_TAG_subroutine_type,
24320 scope_die_for (type, context_die), type);
24322 equate_type_number_to_die (type, subr_die);
24323 add_prototyped_attribute (subr_die, type);
24324 add_type_attribute (subr_die, return_type, TYPE_UNQUALIFIED, false,
24325 context_die);
24326 add_alignment_attribute (subr_die, type);
24327 gen_formal_types_die (type, subr_die);
24329 if (get_AT (subr_die, DW_AT_name))
24330 add_pubtype (type, subr_die);
24331 if ((dwarf_version >= 5 || !dwarf_strict)
24332 && lang_hooks.types.type_dwarf_attribute (type, DW_AT_reference) != -1)
24333 add_AT_flag (subr_die, DW_AT_reference, 1);
24334 if ((dwarf_version >= 5 || !dwarf_strict)
24335 && lang_hooks.types.type_dwarf_attribute (type,
24336 DW_AT_rvalue_reference) != -1)
24337 add_AT_flag (subr_die, DW_AT_rvalue_reference, 1);
24340 /* Generate a DIE for a type definition. */
24342 static void
24343 gen_typedef_die (tree decl, dw_die_ref context_die)
24345 dw_die_ref type_die;
24346 tree origin;
24348 if (TREE_ASM_WRITTEN (decl))
24350 if (DECL_ORIGINAL_TYPE (decl))
24351 fill_variable_array_bounds (DECL_ORIGINAL_TYPE (decl));
24352 return;
24355 TREE_ASM_WRITTEN (decl) = 1;
24356 type_die = new_die (DW_TAG_typedef, context_die, decl);
24357 origin = decl_ultimate_origin (decl);
24358 if (origin != NULL)
24359 add_abstract_origin_attribute (type_die, origin);
24360 else
24362 tree type = TREE_TYPE (decl);
24364 if (type == error_mark_node)
24365 return;
24367 add_name_and_src_coords_attributes (type_die, decl);
24368 if (DECL_ORIGINAL_TYPE (decl))
24370 type = DECL_ORIGINAL_TYPE (decl);
24372 if (type == error_mark_node)
24373 return;
24375 gcc_assert (type != TREE_TYPE (decl));
24376 equate_type_number_to_die (TREE_TYPE (decl), type_die);
24378 else
24380 if (is_naming_typedef_decl (TYPE_NAME (type)))
24382 /* Here, we are in the case of decl being a typedef naming
24383 an anonymous type, e.g:
24384 typedef struct {...} foo;
24385 In that case TREE_TYPE (decl) is not a typedef variant
24386 type and TYPE_NAME of the anonymous type is set to the
24387 TYPE_DECL of the typedef. This construct is emitted by
24388 the C++ FE.
24390 TYPE is the anonymous struct named by the typedef
24391 DECL. As we need the DW_AT_type attribute of the
24392 DW_TAG_typedef to point to the DIE of TYPE, let's
24393 generate that DIE right away. add_type_attribute
24394 called below will then pick (via lookup_type_die) that
24395 anonymous struct DIE. */
24396 if (!TREE_ASM_WRITTEN (type))
24397 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
24399 /* This is a GNU Extension. We are adding a
24400 DW_AT_linkage_name attribute to the DIE of the
24401 anonymous struct TYPE. The value of that attribute
24402 is the name of the typedef decl naming the anonymous
24403 struct. This greatly eases the work of consumers of
24404 this debug info. */
24405 add_linkage_name_raw (lookup_type_die (type), decl);
24409 add_type_attribute (type_die, type, decl_quals (decl), false,
24410 context_die);
24412 if (is_naming_typedef_decl (decl))
24413 /* We want that all subsequent calls to lookup_type_die with
24414 TYPE in argument yield the DW_TAG_typedef we have just
24415 created. */
24416 equate_type_number_to_die (type, type_die);
24418 type = TREE_TYPE (decl);
24420 add_alignment_attribute (type_die, type);
24422 add_accessibility_attribute (type_die, decl);
24425 if (DECL_ABSTRACT_P (decl))
24426 equate_decl_number_to_die (decl, type_die);
24428 if (get_AT (type_die, DW_AT_name))
24429 add_pubtype (decl, type_die);
24432 /* Generate a DIE for a struct, class, enum or union type. */
24434 static void
24435 gen_tagged_type_die (tree type,
24436 dw_die_ref context_die,
24437 enum debug_info_usage usage)
24439 int need_pop;
24441 if (type == NULL_TREE
24442 || !is_tagged_type (type))
24443 return;
24445 if (TREE_ASM_WRITTEN (type))
24446 need_pop = 0;
24447 /* If this is a nested type whose containing class hasn't been written
24448 out yet, writing it out will cover this one, too. This does not apply
24449 to instantiations of member class templates; they need to be added to
24450 the containing class as they are generated. FIXME: This hurts the
24451 idea of combining type decls from multiple TUs, since we can't predict
24452 what set of template instantiations we'll get. */
24453 else if (TYPE_CONTEXT (type)
24454 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
24455 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
24457 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
24459 if (TREE_ASM_WRITTEN (type))
24460 return;
24462 /* If that failed, attach ourselves to the stub. */
24463 push_decl_scope (TYPE_CONTEXT (type));
24464 context_die = lookup_type_die (TYPE_CONTEXT (type));
24465 need_pop = 1;
24467 else if (TYPE_CONTEXT (type) != NULL_TREE
24468 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
24470 /* If this type is local to a function that hasn't been written
24471 out yet, use a NULL context for now; it will be fixed up in
24472 decls_for_scope. */
24473 context_die = lookup_decl_die (TYPE_CONTEXT (type));
24474 /* A declaration DIE doesn't count; nested types need to go in the
24475 specification. */
24476 if (context_die && is_declaration_die (context_die))
24477 context_die = NULL;
24478 need_pop = 0;
24480 else
24482 context_die = declare_in_namespace (type, context_die);
24483 need_pop = 0;
24486 if (TREE_CODE (type) == ENUMERAL_TYPE)
24488 /* This might have been written out by the call to
24489 declare_in_namespace. */
24490 if (!TREE_ASM_WRITTEN (type))
24491 gen_enumeration_type_die (type, context_die);
24493 else
24494 gen_struct_or_union_type_die (type, context_die, usage);
24496 if (need_pop)
24497 pop_decl_scope ();
24499 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
24500 it up if it is ever completed. gen_*_type_die will set it for us
24501 when appropriate. */
24504 /* Generate a type description DIE. */
24506 static void
24507 gen_type_die_with_usage (tree type, dw_die_ref context_die,
24508 enum debug_info_usage usage)
24510 struct array_descr_info info;
24512 if (type == NULL_TREE || type == error_mark_node)
24513 return;
24515 if (flag_checking && type)
24516 verify_type (type);
24518 if (TYPE_NAME (type) != NULL_TREE
24519 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
24520 && is_redundant_typedef (TYPE_NAME (type))
24521 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
24522 /* The DECL of this type is a typedef we don't want to emit debug
24523 info for but we want debug info for its underlying typedef.
24524 This can happen for e.g, the injected-class-name of a C++
24525 type. */
24526 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
24528 /* If TYPE is a typedef type variant, let's generate debug info
24529 for the parent typedef which TYPE is a type of. */
24530 if (typedef_variant_p (type))
24532 if (TREE_ASM_WRITTEN (type))
24533 return;
24535 /* Prevent broken recursion; we can't hand off to the same type. */
24536 gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
24538 /* Give typedefs the right scope. */
24539 context_die = scope_die_for (type, context_die);
24541 TREE_ASM_WRITTEN (type) = 1;
24543 gen_decl_die (TYPE_NAME (type), NULL, NULL, context_die);
24544 return;
24547 /* If type is an anonymous tagged type named by a typedef, let's
24548 generate debug info for the typedef. */
24549 if (is_naming_typedef_decl (TYPE_NAME (type)))
24551 /* Use the DIE of the containing namespace as the parent DIE of
24552 the type description DIE we want to generate. */
24553 if (DECL_CONTEXT (TYPE_NAME (type))
24554 && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
24555 context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
24557 gen_decl_die (TYPE_NAME (type), NULL, NULL, context_die);
24558 return;
24561 if (lang_hooks.types.get_debug_type)
24563 tree debug_type = lang_hooks.types.get_debug_type (type);
24565 if (debug_type != NULL_TREE && debug_type != type)
24567 gen_type_die_with_usage (debug_type, context_die, usage);
24568 return;
24572 /* We are going to output a DIE to represent the unqualified version
24573 of this type (i.e. without any const or volatile qualifiers) so
24574 get the main variant (i.e. the unqualified version) of this type
24575 now. (Vectors and arrays are special because the debugging info is in the
24576 cloned type itself. Similarly function/method types can contain extra
24577 ref-qualification). */
24578 if (TREE_CODE (type) == FUNCTION_TYPE
24579 || TREE_CODE (type) == METHOD_TYPE)
24581 /* For function/method types, can't use type_main_variant here,
24582 because that can have different ref-qualifiers for C++,
24583 but try to canonicalize. */
24584 tree main = TYPE_MAIN_VARIANT (type);
24585 for (tree t = main; t; t = TYPE_NEXT_VARIANT (t))
24586 if (TYPE_QUALS_NO_ADDR_SPACE (t) == 0
24587 && check_base_type (t, main)
24588 && check_lang_type (t, type))
24590 type = t;
24591 break;
24594 else if (TREE_CODE (type) != VECTOR_TYPE
24595 && TREE_CODE (type) != ARRAY_TYPE)
24596 type = type_main_variant (type);
24598 /* If this is an array type with hidden descriptor, handle it first. */
24599 if (!TREE_ASM_WRITTEN (type)
24600 && lang_hooks.types.get_array_descr_info)
24602 memset (&info, 0, sizeof (info));
24603 if (lang_hooks.types.get_array_descr_info (type, &info))
24605 /* Fortran sometimes emits array types with no dimension. */
24606 gcc_assert (info.ndimensions >= 0
24607 && (info.ndimensions
24608 <= DWARF2OUT_ARRAY_DESCR_INFO_MAX_DIMEN));
24609 gen_descr_array_type_die (type, &info, context_die);
24610 TREE_ASM_WRITTEN (type) = 1;
24611 return;
24615 if (TREE_ASM_WRITTEN (type))
24617 /* Variable-length types may be incomplete even if
24618 TREE_ASM_WRITTEN. For such types, fall through to
24619 gen_array_type_die() and possibly fill in
24620 DW_AT_{upper,lower}_bound attributes. */
24621 if ((TREE_CODE (type) != ARRAY_TYPE
24622 && TREE_CODE (type) != RECORD_TYPE
24623 && TREE_CODE (type) != UNION_TYPE
24624 && TREE_CODE (type) != QUAL_UNION_TYPE)
24625 || !variably_modified_type_p (type, NULL))
24626 return;
24629 switch (TREE_CODE (type))
24631 case ERROR_MARK:
24632 break;
24634 case POINTER_TYPE:
24635 case REFERENCE_TYPE:
24636 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
24637 ensures that the gen_type_die recursion will terminate even if the
24638 type is recursive. Recursive types are possible in Ada. */
24639 /* ??? We could perhaps do this for all types before the switch
24640 statement. */
24641 TREE_ASM_WRITTEN (type) = 1;
24643 /* For these types, all that is required is that we output a DIE (or a
24644 set of DIEs) to represent the "basis" type. */
24645 gen_type_die_with_usage (TREE_TYPE (type), context_die,
24646 DINFO_USAGE_IND_USE);
24647 break;
24649 case OFFSET_TYPE:
24650 /* This code is used for C++ pointer-to-data-member types.
24651 Output a description of the relevant class type. */
24652 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
24653 DINFO_USAGE_IND_USE);
24655 /* Output a description of the type of the object pointed to. */
24656 gen_type_die_with_usage (TREE_TYPE (type), context_die,
24657 DINFO_USAGE_IND_USE);
24659 /* Now output a DIE to represent this pointer-to-data-member type
24660 itself. */
24661 gen_ptr_to_mbr_type_die (type, context_die);
24662 break;
24664 case FUNCTION_TYPE:
24665 /* Force out return type (in case it wasn't forced out already). */
24666 gen_type_die_with_usage (TREE_TYPE (type), context_die,
24667 DINFO_USAGE_DIR_USE);
24668 gen_subroutine_type_die (type, context_die);
24669 break;
24671 case METHOD_TYPE:
24672 /* Force out return type (in case it wasn't forced out already). */
24673 gen_type_die_with_usage (TREE_TYPE (type), context_die,
24674 DINFO_USAGE_DIR_USE);
24675 gen_subroutine_type_die (type, context_die);
24676 break;
24678 case ARRAY_TYPE:
24679 case VECTOR_TYPE:
24680 gen_array_type_die (type, context_die);
24681 break;
24683 case ENUMERAL_TYPE:
24684 case RECORD_TYPE:
24685 case UNION_TYPE:
24686 case QUAL_UNION_TYPE:
24687 gen_tagged_type_die (type, context_die, usage);
24688 return;
24690 case VOID_TYPE:
24691 case INTEGER_TYPE:
24692 case REAL_TYPE:
24693 case FIXED_POINT_TYPE:
24694 case COMPLEX_TYPE:
24695 case BOOLEAN_TYPE:
24696 case POINTER_BOUNDS_TYPE:
24697 /* No DIEs needed for fundamental types. */
24698 break;
24700 case NULLPTR_TYPE:
24701 case LANG_TYPE:
24702 /* Just use DW_TAG_unspecified_type. */
24704 dw_die_ref type_die = lookup_type_die (type);
24705 if (type_die == NULL)
24707 tree name = TYPE_IDENTIFIER (type);
24708 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (),
24709 type);
24710 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
24711 equate_type_number_to_die (type, type_die);
24714 break;
24716 default:
24717 if (is_cxx_auto (type))
24719 tree name = TYPE_IDENTIFIER (type);
24720 dw_die_ref *die = (name == get_identifier ("auto")
24721 ? &auto_die : &decltype_auto_die);
24722 if (!*die)
24724 *die = new_die (DW_TAG_unspecified_type,
24725 comp_unit_die (), NULL_TREE);
24726 add_name_attribute (*die, IDENTIFIER_POINTER (name));
24728 equate_type_number_to_die (type, *die);
24729 break;
24731 gcc_unreachable ();
24734 TREE_ASM_WRITTEN (type) = 1;
24737 static void
24738 gen_type_die (tree type, dw_die_ref context_die)
24740 if (type != error_mark_node)
24742 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
24743 if (flag_checking)
24745 dw_die_ref die = lookup_type_die (type);
24746 if (die)
24747 check_die (die);
24752 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
24753 things which are local to the given block. */
24755 static void
24756 gen_block_die (tree stmt, dw_die_ref context_die)
24758 int must_output_die = 0;
24759 bool inlined_func;
24761 /* Ignore blocks that are NULL. */
24762 if (stmt == NULL_TREE)
24763 return;
24765 inlined_func = inlined_function_outer_scope_p (stmt);
24767 /* If the block is one fragment of a non-contiguous block, do not
24768 process the variables, since they will have been done by the
24769 origin block. Do process subblocks. */
24770 if (BLOCK_FRAGMENT_ORIGIN (stmt))
24772 tree sub;
24774 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
24775 gen_block_die (sub, context_die);
24777 return;
24780 /* Determine if we need to output any Dwarf DIEs at all to represent this
24781 block. */
24782 if (inlined_func)
24783 /* The outer scopes for inlinings *must* always be represented. We
24784 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
24785 must_output_die = 1;
24786 else
24788 /* Determine if this block directly contains any "significant"
24789 local declarations which we will need to output DIEs for. */
24790 if (debug_info_level > DINFO_LEVEL_TERSE)
24791 /* We are not in terse mode so *any* local declaration counts
24792 as being a "significant" one. */
24793 must_output_die = ((BLOCK_VARS (stmt) != NULL
24794 || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
24795 && (TREE_USED (stmt)
24796 || TREE_ASM_WRITTEN (stmt)
24797 || BLOCK_ABSTRACT (stmt)));
24798 else if ((TREE_USED (stmt)
24799 || TREE_ASM_WRITTEN (stmt)
24800 || BLOCK_ABSTRACT (stmt))
24801 && !dwarf2out_ignore_block (stmt))
24802 must_output_die = 1;
24805 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
24806 DIE for any block which contains no significant local declarations at
24807 all. Rather, in such cases we just call `decls_for_scope' so that any
24808 needed Dwarf info for any sub-blocks will get properly generated. Note
24809 that in terse mode, our definition of what constitutes a "significant"
24810 local declaration gets restricted to include only inlined function
24811 instances and local (nested) function definitions. */
24812 if (must_output_die)
24814 if (inlined_func)
24816 /* If STMT block is abstract, that means we have been called
24817 indirectly from dwarf2out_abstract_function.
24818 That function rightfully marks the descendent blocks (of
24819 the abstract function it is dealing with) as being abstract,
24820 precisely to prevent us from emitting any
24821 DW_TAG_inlined_subroutine DIE as a descendent
24822 of an abstract function instance. So in that case, we should
24823 not call gen_inlined_subroutine_die.
24825 Later though, when cgraph asks dwarf2out to emit info
24826 for the concrete instance of the function decl into which
24827 the concrete instance of STMT got inlined, the later will lead
24828 to the generation of a DW_TAG_inlined_subroutine DIE. */
24829 if (! BLOCK_ABSTRACT (stmt))
24830 gen_inlined_subroutine_die (stmt, context_die);
24832 else
24833 gen_lexical_block_die (stmt, context_die);
24835 else
24836 decls_for_scope (stmt, context_die);
24839 /* Process variable DECL (or variable with origin ORIGIN) within
24840 block STMT and add it to CONTEXT_DIE. */
24841 static void
24842 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
24844 dw_die_ref die;
24845 tree decl_or_origin = decl ? decl : origin;
24847 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
24848 die = lookup_decl_die (decl_or_origin);
24849 else if (TREE_CODE (decl_or_origin) == TYPE_DECL)
24851 if (TYPE_DECL_IS_STUB (decl_or_origin))
24852 die = lookup_type_die (TREE_TYPE (decl_or_origin));
24853 else
24854 die = lookup_decl_die (decl_or_origin);
24855 /* Avoid re-creating the DIE late if it was optimized as unused early. */
24856 if (! die && ! early_dwarf)
24857 return;
24859 else
24860 die = NULL;
24862 if (die != NULL && die->die_parent == NULL)
24863 add_child_die (context_die, die);
24864 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
24866 if (early_dwarf)
24867 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
24868 stmt, context_die);
24870 else
24871 gen_decl_die (decl, origin, NULL, context_die);
24874 /* Generate all of the decls declared within a given scope and (recursively)
24875 all of its sub-blocks. */
24877 static void
24878 decls_for_scope (tree stmt, dw_die_ref context_die)
24880 tree decl;
24881 unsigned int i;
24882 tree subblocks;
24884 /* Ignore NULL blocks. */
24885 if (stmt == NULL_TREE)
24886 return;
24888 /* Output the DIEs to represent all of the data objects and typedefs
24889 declared directly within this block but not within any nested
24890 sub-blocks. Also, nested function and tag DIEs have been
24891 generated with a parent of NULL; fix that up now. We don't
24892 have to do this if we're at -g1. */
24893 if (debug_info_level > DINFO_LEVEL_TERSE)
24895 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
24896 process_scope_var (stmt, decl, NULL_TREE, context_die);
24897 /* BLOCK_NONLOCALIZED_VARs simply generate DIE stubs with abstract
24898 origin - avoid doing this twice as we have no good way to see
24899 if we've done it once already. */
24900 if (! early_dwarf)
24901 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
24903 decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
24904 if (decl == current_function_decl)
24905 /* Ignore declarations of the current function, while they
24906 are declarations, gen_subprogram_die would treat them
24907 as definitions again, because they are equal to
24908 current_function_decl and endlessly recurse. */;
24909 else if (TREE_CODE (decl) == FUNCTION_DECL)
24910 process_scope_var (stmt, decl, NULL_TREE, context_die);
24911 else
24912 process_scope_var (stmt, NULL_TREE, decl, context_die);
24916 /* Even if we're at -g1, we need to process the subblocks in order to get
24917 inlined call information. */
24919 /* Output the DIEs to represent all sub-blocks (and the items declared
24920 therein) of this block. */
24921 for (subblocks = BLOCK_SUBBLOCKS (stmt);
24922 subblocks != NULL;
24923 subblocks = BLOCK_CHAIN (subblocks))
24924 gen_block_die (subblocks, context_die);
24927 /* Is this a typedef we can avoid emitting? */
24929 bool
24930 is_redundant_typedef (const_tree decl)
24932 if (TYPE_DECL_IS_STUB (decl))
24933 return true;
24935 if (DECL_ARTIFICIAL (decl)
24936 && DECL_CONTEXT (decl)
24937 && is_tagged_type (DECL_CONTEXT (decl))
24938 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
24939 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
24940 /* Also ignore the artificial member typedef for the class name. */
24941 return true;
24943 return false;
24946 /* Return TRUE if TYPE is a typedef that names a type for linkage
24947 purposes. This kind of typedefs is produced by the C++ FE for
24948 constructs like:
24950 typedef struct {...} foo;
24952 In that case, there is no typedef variant type produced for foo.
24953 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
24954 struct type. */
24956 static bool
24957 is_naming_typedef_decl (const_tree decl)
24959 if (decl == NULL_TREE
24960 || TREE_CODE (decl) != TYPE_DECL
24961 || DECL_NAMELESS (decl)
24962 || !is_tagged_type (TREE_TYPE (decl))
24963 || DECL_IS_BUILTIN (decl)
24964 || is_redundant_typedef (decl)
24965 /* It looks like Ada produces TYPE_DECLs that are very similar
24966 to C++ naming typedefs but that have different
24967 semantics. Let's be specific to c++ for now. */
24968 || !is_cxx (decl))
24969 return FALSE;
24971 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
24972 && TYPE_NAME (TREE_TYPE (decl)) == decl
24973 && (TYPE_STUB_DECL (TREE_TYPE (decl))
24974 != TYPE_NAME (TREE_TYPE (decl))));
24977 /* Looks up the DIE for a context. */
24979 static inline dw_die_ref
24980 lookup_context_die (tree context)
24982 if (context)
24984 /* Find die that represents this context. */
24985 if (TYPE_P (context))
24987 context = TYPE_MAIN_VARIANT (context);
24988 dw_die_ref ctx = lookup_type_die (context);
24989 if (!ctx)
24990 return NULL;
24991 return strip_naming_typedef (context, ctx);
24993 else
24994 return lookup_decl_die (context);
24996 return comp_unit_die ();
24999 /* Returns the DIE for a context. */
25001 static inline dw_die_ref
25002 get_context_die (tree context)
25004 if (context)
25006 /* Find die that represents this context. */
25007 if (TYPE_P (context))
25009 context = TYPE_MAIN_VARIANT (context);
25010 return strip_naming_typedef (context, force_type_die (context));
25012 else
25013 return force_decl_die (context);
25015 return comp_unit_die ();
25018 /* Returns the DIE for decl. A DIE will always be returned. */
25020 static dw_die_ref
25021 force_decl_die (tree decl)
25023 dw_die_ref decl_die;
25024 unsigned saved_external_flag;
25025 tree save_fn = NULL_TREE;
25026 decl_die = lookup_decl_die (decl);
25027 if (!decl_die)
25029 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
25031 decl_die = lookup_decl_die (decl);
25032 if (decl_die)
25033 return decl_die;
25035 switch (TREE_CODE (decl))
25037 case FUNCTION_DECL:
25038 /* Clear current_function_decl, so that gen_subprogram_die thinks
25039 that this is a declaration. At this point, we just want to force
25040 declaration die. */
25041 save_fn = current_function_decl;
25042 current_function_decl = NULL_TREE;
25043 gen_subprogram_die (decl, context_die);
25044 current_function_decl = save_fn;
25045 break;
25047 case VAR_DECL:
25048 /* Set external flag to force declaration die. Restore it after
25049 gen_decl_die() call. */
25050 saved_external_flag = DECL_EXTERNAL (decl);
25051 DECL_EXTERNAL (decl) = 1;
25052 gen_decl_die (decl, NULL, NULL, context_die);
25053 DECL_EXTERNAL (decl) = saved_external_flag;
25054 break;
25056 case NAMESPACE_DECL:
25057 if (dwarf_version >= 3 || !dwarf_strict)
25058 dwarf2out_decl (decl);
25059 else
25060 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
25061 decl_die = comp_unit_die ();
25062 break;
25064 case TRANSLATION_UNIT_DECL:
25065 decl_die = comp_unit_die ();
25066 break;
25068 default:
25069 gcc_unreachable ();
25072 /* We should be able to find the DIE now. */
25073 if (!decl_die)
25074 decl_die = lookup_decl_die (decl);
25075 gcc_assert (decl_die);
25078 return decl_die;
25081 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
25082 always returned. */
25084 static dw_die_ref
25085 force_type_die (tree type)
25087 dw_die_ref type_die;
25089 type_die = lookup_type_die (type);
25090 if (!type_die)
25092 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
25094 type_die = modified_type_die (type, TYPE_QUALS_NO_ADDR_SPACE (type),
25095 false, context_die);
25096 gcc_assert (type_die);
25098 return type_die;
25101 /* Force out any required namespaces to be able to output DECL,
25102 and return the new context_die for it, if it's changed. */
25104 static dw_die_ref
25105 setup_namespace_context (tree thing, dw_die_ref context_die)
25107 tree context = (DECL_P (thing)
25108 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
25109 if (context && TREE_CODE (context) == NAMESPACE_DECL)
25110 /* Force out the namespace. */
25111 context_die = force_decl_die (context);
25113 return context_die;
25116 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
25117 type) within its namespace, if appropriate.
25119 For compatibility with older debuggers, namespace DIEs only contain
25120 declarations; all definitions are emitted at CU scope, with
25121 DW_AT_specification pointing to the declaration (like with class
25122 members). */
25124 static dw_die_ref
25125 declare_in_namespace (tree thing, dw_die_ref context_die)
25127 dw_die_ref ns_context;
25129 if (debug_info_level <= DINFO_LEVEL_TERSE)
25130 return context_die;
25132 /* External declarations in the local scope only need to be emitted
25133 once, not once in the namespace and once in the scope.
25135 This avoids declaring the `extern' below in the
25136 namespace DIE as well as in the innermost scope:
25138 namespace S
25140 int i=5;
25141 int foo()
25143 int i=8;
25144 extern int i;
25145 return i;
25149 if (DECL_P (thing) && DECL_EXTERNAL (thing) && local_scope_p (context_die))
25150 return context_die;
25152 /* If this decl is from an inlined function, then don't try to emit it in its
25153 namespace, as we will get confused. It would have already been emitted
25154 when the abstract instance of the inline function was emitted anyways. */
25155 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
25156 return context_die;
25158 ns_context = setup_namespace_context (thing, context_die);
25160 if (ns_context != context_die)
25162 if (is_fortran ())
25163 return ns_context;
25164 if (DECL_P (thing))
25165 gen_decl_die (thing, NULL, NULL, ns_context);
25166 else
25167 gen_type_die (thing, ns_context);
25169 return context_die;
25172 /* Generate a DIE for a namespace or namespace alias. */
25174 static void
25175 gen_namespace_die (tree decl, dw_die_ref context_die)
25177 dw_die_ref namespace_die;
25179 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
25180 they are an alias of. */
25181 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
25183 /* Output a real namespace or module. */
25184 context_die = setup_namespace_context (decl, comp_unit_die ());
25185 namespace_die = new_die (is_fortran ()
25186 ? DW_TAG_module : DW_TAG_namespace,
25187 context_die, decl);
25188 /* For Fortran modules defined in different CU don't add src coords. */
25189 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
25191 const char *name = dwarf2_name (decl, 0);
25192 if (name)
25193 add_name_attribute (namespace_die, name);
25195 else
25196 add_name_and_src_coords_attributes (namespace_die, decl);
25197 if (DECL_EXTERNAL (decl))
25198 add_AT_flag (namespace_die, DW_AT_declaration, 1);
25199 equate_decl_number_to_die (decl, namespace_die);
25201 else
25203 /* Output a namespace alias. */
25205 /* Force out the namespace we are an alias of, if necessary. */
25206 dw_die_ref origin_die
25207 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
25209 if (DECL_FILE_SCOPE_P (decl)
25210 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
25211 context_die = setup_namespace_context (decl, comp_unit_die ());
25212 /* Now create the namespace alias DIE. */
25213 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
25214 add_name_and_src_coords_attributes (namespace_die, decl);
25215 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
25216 equate_decl_number_to_die (decl, namespace_die);
25218 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
25219 if (want_pubnames ())
25220 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
25223 /* Generate Dwarf debug information for a decl described by DECL.
25224 The return value is currently only meaningful for PARM_DECLs,
25225 for all other decls it returns NULL.
25227 If DECL is a FIELD_DECL, CTX is required: see the comment for VLR_CONTEXT.
25228 It can be NULL otherwise. */
25230 static dw_die_ref
25231 gen_decl_die (tree decl, tree origin, struct vlr_context *ctx,
25232 dw_die_ref context_die)
25234 tree decl_or_origin = decl ? decl : origin;
25235 tree class_origin = NULL, ultimate_origin;
25237 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
25238 return NULL;
25240 /* Ignore pointer bounds decls. */
25241 if (DECL_P (decl_or_origin)
25242 && TREE_TYPE (decl_or_origin)
25243 && POINTER_BOUNDS_P (decl_or_origin))
25244 return NULL;
25246 switch (TREE_CODE (decl_or_origin))
25248 case ERROR_MARK:
25249 break;
25251 case CONST_DECL:
25252 if (!is_fortran () && !is_ada ())
25254 /* The individual enumerators of an enum type get output when we output
25255 the Dwarf representation of the relevant enum type itself. */
25256 break;
25259 /* Emit its type. */
25260 gen_type_die (TREE_TYPE (decl), context_die);
25262 /* And its containing namespace. */
25263 context_die = declare_in_namespace (decl, context_die);
25265 gen_const_die (decl, context_die);
25266 break;
25268 case FUNCTION_DECL:
25269 /* Don't output any DIEs to represent mere function declarations,
25270 unless they are class members or explicit block externs. */
25271 if (DECL_INITIAL (decl_or_origin) == NULL_TREE
25272 && DECL_FILE_SCOPE_P (decl_or_origin)
25273 && (current_function_decl == NULL_TREE
25274 || DECL_ARTIFICIAL (decl_or_origin)))
25275 break;
25277 #if 0
25278 /* FIXME */
25279 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
25280 on local redeclarations of global functions. That seems broken. */
25281 if (current_function_decl != decl)
25282 /* This is only a declaration. */;
25283 #endif
25285 /* If we're emitting a clone, emit info for the abstract instance. */
25286 if (origin || DECL_ORIGIN (decl) != decl)
25287 dwarf2out_abstract_function (origin
25288 ? DECL_ORIGIN (origin)
25289 : DECL_ABSTRACT_ORIGIN (decl));
25291 /* If we're emitting an out-of-line copy of an inline function,
25292 emit info for the abstract instance and set up to refer to it. */
25293 else if (cgraph_function_possibly_inlined_p (decl)
25294 && ! DECL_ABSTRACT_P (decl)
25295 && ! class_or_namespace_scope_p (context_die)
25296 /* dwarf2out_abstract_function won't emit a die if this is just
25297 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
25298 that case, because that works only if we have a die. */
25299 && DECL_INITIAL (decl) != NULL_TREE)
25301 dwarf2out_abstract_function (decl);
25302 set_decl_origin_self (decl);
25305 /* Otherwise we're emitting the primary DIE for this decl. */
25306 else if (debug_info_level > DINFO_LEVEL_TERSE)
25308 /* Before we describe the FUNCTION_DECL itself, make sure that we
25309 have its containing type. */
25310 if (!origin)
25311 origin = decl_class_context (decl);
25312 if (origin != NULL_TREE)
25313 gen_type_die (origin, context_die);
25315 /* And its return type. */
25316 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
25318 /* And its virtual context. */
25319 if (DECL_VINDEX (decl) != NULL_TREE)
25320 gen_type_die (DECL_CONTEXT (decl), context_die);
25322 /* Make sure we have a member DIE for decl. */
25323 if (origin != NULL_TREE)
25324 gen_type_die_for_member (origin, decl, context_die);
25326 /* And its containing namespace. */
25327 context_die = declare_in_namespace (decl, context_die);
25330 /* Now output a DIE to represent the function itself. */
25331 if (decl)
25332 gen_subprogram_die (decl, context_die);
25333 break;
25335 case TYPE_DECL:
25336 /* If we are in terse mode, don't generate any DIEs to represent any
25337 actual typedefs. */
25338 if (debug_info_level <= DINFO_LEVEL_TERSE)
25339 break;
25341 /* In the special case of a TYPE_DECL node representing the declaration
25342 of some type tag, if the given TYPE_DECL is marked as having been
25343 instantiated from some other (original) TYPE_DECL node (e.g. one which
25344 was generated within the original definition of an inline function) we
25345 used to generate a special (abbreviated) DW_TAG_structure_type,
25346 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
25347 should be actually referencing those DIEs, as variable DIEs with that
25348 type would be emitted already in the abstract origin, so it was always
25349 removed during unused type prunning. Don't add anything in this
25350 case. */
25351 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
25352 break;
25354 if (is_redundant_typedef (decl))
25355 gen_type_die (TREE_TYPE (decl), context_die);
25356 else
25357 /* Output a DIE to represent the typedef itself. */
25358 gen_typedef_die (decl, context_die);
25359 break;
25361 case LABEL_DECL:
25362 if (debug_info_level >= DINFO_LEVEL_NORMAL)
25363 gen_label_die (decl, context_die);
25364 break;
25366 case VAR_DECL:
25367 case RESULT_DECL:
25368 /* If we are in terse mode, don't generate any DIEs to represent any
25369 variable declarations or definitions. */
25370 if (debug_info_level <= DINFO_LEVEL_TERSE)
25371 break;
25373 /* Output any DIEs that are needed to specify the type of this data
25374 object. */
25375 if (decl_by_reference_p (decl_or_origin))
25376 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
25377 else
25378 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
25380 /* And its containing type. */
25381 class_origin = decl_class_context (decl_or_origin);
25382 if (class_origin != NULL_TREE)
25383 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
25385 /* And its containing namespace. */
25386 context_die = declare_in_namespace (decl_or_origin, context_die);
25388 /* Now output the DIE to represent the data object itself. This gets
25389 complicated because of the possibility that the VAR_DECL really
25390 represents an inlined instance of a formal parameter for an inline
25391 function. */
25392 ultimate_origin = decl_ultimate_origin (decl_or_origin);
25393 if (ultimate_origin != NULL_TREE
25394 && TREE_CODE (ultimate_origin) == PARM_DECL)
25395 gen_formal_parameter_die (decl, origin,
25396 true /* Emit name attribute. */,
25397 context_die);
25398 else
25399 gen_variable_die (decl, origin, context_die);
25400 break;
25402 case FIELD_DECL:
25403 gcc_assert (ctx != NULL && ctx->struct_type != NULL);
25404 /* Ignore the nameless fields that are used to skip bits but handle C++
25405 anonymous unions and structs. */
25406 if (DECL_NAME (decl) != NULL_TREE
25407 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
25408 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
25410 gen_type_die (member_declared_type (decl), context_die);
25411 gen_field_die (decl, ctx, context_die);
25413 break;
25415 case PARM_DECL:
25416 if (DECL_BY_REFERENCE (decl_or_origin))
25417 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
25418 else
25419 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
25420 return gen_formal_parameter_die (decl, origin,
25421 true /* Emit name attribute. */,
25422 context_die);
25424 case NAMESPACE_DECL:
25425 if (dwarf_version >= 3 || !dwarf_strict)
25426 gen_namespace_die (decl, context_die);
25427 break;
25429 case IMPORTED_DECL:
25430 dwarf2out_imported_module_or_decl_1 (decl, DECL_NAME (decl),
25431 DECL_CONTEXT (decl), context_die);
25432 break;
25434 case NAMELIST_DECL:
25435 gen_namelist_decl (DECL_NAME (decl), context_die,
25436 NAMELIST_DECL_ASSOCIATED_DECL (decl));
25437 break;
25439 default:
25440 /* Probably some frontend-internal decl. Assume we don't care. */
25441 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
25442 break;
25445 return NULL;
25448 /* Output initial debug information for global DECL. Called at the
25449 end of the parsing process.
25451 This is the initial debug generation process. As such, the DIEs
25452 generated may be incomplete. A later debug generation pass
25453 (dwarf2out_late_global_decl) will augment the information generated
25454 in this pass (e.g., with complete location info). */
25456 static void
25457 dwarf2out_early_global_decl (tree decl)
25459 set_early_dwarf s;
25461 /* gen_decl_die() will set DECL_ABSTRACT because
25462 cgraph_function_possibly_inlined_p() returns true. This is in
25463 turn will cause DW_AT_inline attributes to be set.
25465 This happens because at early dwarf generation, there is no
25466 cgraph information, causing cgraph_function_possibly_inlined_p()
25467 to return true. Trick cgraph_function_possibly_inlined_p()
25468 while we generate dwarf early. */
25469 bool save = symtab->global_info_ready;
25470 symtab->global_info_ready = true;
25472 /* We don't handle TYPE_DECLs. If required, they'll be reached via
25473 other DECLs and they can point to template types or other things
25474 that dwarf2out can't handle when done via dwarf2out_decl. */
25475 if (TREE_CODE (decl) != TYPE_DECL
25476 && TREE_CODE (decl) != PARM_DECL)
25478 tree save_fndecl = current_function_decl;
25479 if (TREE_CODE (decl) == FUNCTION_DECL)
25481 /* No cfun means the symbol has no body, so there's nothing
25482 to emit. */
25483 if (!DECL_STRUCT_FUNCTION (decl))
25484 goto early_decl_exit;
25486 /* For nested functions, make sure we have DIEs for the parents first
25487 so that all nested DIEs are generated at the proper scope in the
25488 first shot. */
25489 tree context = decl_function_context (decl);
25490 if (context != NULL && lookup_decl_die (context) == NULL)
25492 current_function_decl = context;
25493 dwarf2out_decl (context);
25496 current_function_decl = decl;
25498 dwarf2out_decl (decl);
25499 if (TREE_CODE (decl) == FUNCTION_DECL)
25500 current_function_decl = save_fndecl;
25502 early_decl_exit:
25503 symtab->global_info_ready = save;
25506 /* Output debug information for global decl DECL. Called from
25507 toplev.c after compilation proper has finished. */
25509 static void
25510 dwarf2out_late_global_decl (tree decl)
25512 /* Fill-in any location information we were unable to determine
25513 on the first pass. */
25514 if (VAR_P (decl) && !POINTER_BOUNDS_P (decl))
25516 dw_die_ref die = lookup_decl_die (decl);
25518 /* We have to generate early debug late for LTO. */
25519 if (! die && in_lto_p)
25521 dwarf2out_decl (decl);
25522 die = lookup_decl_die (decl);
25525 if (die)
25527 /* We get called via the symtab code invoking late_global_decl
25528 for symbols that are optimized out. Do not add locations
25529 for those. */
25530 varpool_node *node = varpool_node::get (decl);
25531 if (! node || ! node->definition)
25532 tree_add_const_value_attribute_for_decl (die, decl);
25533 else
25534 add_location_or_const_value_attribute (die, decl, false);
25539 /* Output debug information for type decl DECL. Called from toplev.c
25540 and from language front ends (to record built-in types). */
25541 static void
25542 dwarf2out_type_decl (tree decl, int local)
25544 if (!local)
25546 set_early_dwarf s;
25547 dwarf2out_decl (decl);
25551 /* Output debug information for imported module or decl DECL.
25552 NAME is non-NULL name in the lexical block if the decl has been renamed.
25553 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
25554 that DECL belongs to.
25555 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
25556 static void
25557 dwarf2out_imported_module_or_decl_1 (tree decl,
25558 tree name,
25559 tree lexical_block,
25560 dw_die_ref lexical_block_die)
25562 expanded_location xloc;
25563 dw_die_ref imported_die = NULL;
25564 dw_die_ref at_import_die;
25566 if (TREE_CODE (decl) == IMPORTED_DECL)
25568 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
25569 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
25570 gcc_assert (decl);
25572 else
25573 xloc = expand_location (input_location);
25575 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
25577 at_import_die = force_type_die (TREE_TYPE (decl));
25578 /* For namespace N { typedef void T; } using N::T; base_type_die
25579 returns NULL, but DW_TAG_imported_declaration requires
25580 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
25581 if (!at_import_die)
25583 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
25584 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
25585 at_import_die = lookup_type_die (TREE_TYPE (decl));
25586 gcc_assert (at_import_die);
25589 else
25591 at_import_die = lookup_decl_die (decl);
25592 if (!at_import_die)
25594 /* If we're trying to avoid duplicate debug info, we may not have
25595 emitted the member decl for this field. Emit it now. */
25596 if (TREE_CODE (decl) == FIELD_DECL)
25598 tree type = DECL_CONTEXT (decl);
25600 if (TYPE_CONTEXT (type)
25601 && TYPE_P (TYPE_CONTEXT (type))
25602 && !should_emit_struct_debug (TYPE_CONTEXT (type),
25603 DINFO_USAGE_DIR_USE))
25604 return;
25605 gen_type_die_for_member (type, decl,
25606 get_context_die (TYPE_CONTEXT (type)));
25608 if (TREE_CODE (decl) == NAMELIST_DECL)
25609 at_import_die = gen_namelist_decl (DECL_NAME (decl),
25610 get_context_die (DECL_CONTEXT (decl)),
25611 NULL_TREE);
25612 else
25613 at_import_die = force_decl_die (decl);
25617 if (TREE_CODE (decl) == NAMESPACE_DECL)
25619 if (dwarf_version >= 3 || !dwarf_strict)
25620 imported_die = new_die (DW_TAG_imported_module,
25621 lexical_block_die,
25622 lexical_block);
25623 else
25624 return;
25626 else
25627 imported_die = new_die (DW_TAG_imported_declaration,
25628 lexical_block_die,
25629 lexical_block);
25631 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
25632 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
25633 if (debug_column_info && xloc.column)
25634 add_AT_unsigned (imported_die, DW_AT_decl_column, xloc.column);
25635 if (name)
25636 add_AT_string (imported_die, DW_AT_name,
25637 IDENTIFIER_POINTER (name));
25638 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
25641 /* Output debug information for imported module or decl DECL.
25642 NAME is non-NULL name in context if the decl has been renamed.
25643 CHILD is true if decl is one of the renamed decls as part of
25644 importing whole module. */
25646 static void
25647 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
25648 bool child)
25650 /* dw_die_ref at_import_die; */
25651 dw_die_ref scope_die;
25653 if (debug_info_level <= DINFO_LEVEL_TERSE)
25654 return;
25656 gcc_assert (decl);
25658 set_early_dwarf s;
25660 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
25661 We need decl DIE for reference and scope die. First, get DIE for the decl
25662 itself. */
25664 /* Get the scope die for decl context. Use comp_unit_die for global module
25665 or decl. If die is not found for non globals, force new die. */
25666 if (context
25667 && TYPE_P (context)
25668 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
25669 return;
25671 scope_die = get_context_die (context);
25673 if (child)
25675 /* DW_TAG_imported_module was introduced in the DWARFv3 specification, so
25676 there is nothing we can do, here. */
25677 if (dwarf_version < 3 && dwarf_strict)
25678 return;
25680 gcc_assert (scope_die->die_child);
25681 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
25682 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
25683 scope_die = scope_die->die_child;
25686 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
25687 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
25690 /* Output debug information for namelists. */
25692 static dw_die_ref
25693 gen_namelist_decl (tree name, dw_die_ref scope_die, tree item_decls)
25695 dw_die_ref nml_die, nml_item_die, nml_item_ref_die;
25696 tree value;
25697 unsigned i;
25699 if (debug_info_level <= DINFO_LEVEL_TERSE)
25700 return NULL;
25702 gcc_assert (scope_die != NULL);
25703 nml_die = new_die (DW_TAG_namelist, scope_die, NULL);
25704 add_AT_string (nml_die, DW_AT_name, IDENTIFIER_POINTER (name));
25706 /* If there are no item_decls, we have a nondefining namelist, e.g.
25707 with USE association; hence, set DW_AT_declaration. */
25708 if (item_decls == NULL_TREE)
25710 add_AT_flag (nml_die, DW_AT_declaration, 1);
25711 return nml_die;
25714 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (item_decls), i, value)
25716 nml_item_ref_die = lookup_decl_die (value);
25717 if (!nml_item_ref_die)
25718 nml_item_ref_die = force_decl_die (value);
25720 nml_item_die = new_die (DW_TAG_namelist_item, nml_die, NULL);
25721 add_AT_die_ref (nml_item_die, DW_AT_namelist_items, nml_item_ref_die);
25723 return nml_die;
25727 /* Write the debugging output for DECL and return the DIE. */
25729 static void
25730 dwarf2out_decl (tree decl)
25732 dw_die_ref context_die = comp_unit_die ();
25734 switch (TREE_CODE (decl))
25736 case ERROR_MARK:
25737 return;
25739 case FUNCTION_DECL:
25740 /* What we would really like to do here is to filter out all mere
25741 file-scope declarations of file-scope functions which are never
25742 referenced later within this translation unit (and keep all of ones
25743 that *are* referenced later on) but we aren't clairvoyant, so we have
25744 no idea which functions will be referenced in the future (i.e. later
25745 on within the current translation unit). So here we just ignore all
25746 file-scope function declarations which are not also definitions. If
25747 and when the debugger needs to know something about these functions,
25748 it will have to hunt around and find the DWARF information associated
25749 with the definition of the function.
25751 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
25752 nodes represent definitions and which ones represent mere
25753 declarations. We have to check DECL_INITIAL instead. That's because
25754 the C front-end supports some weird semantics for "extern inline"
25755 function definitions. These can get inlined within the current
25756 translation unit (and thus, we need to generate Dwarf info for their
25757 abstract instances so that the Dwarf info for the concrete inlined
25758 instances can have something to refer to) but the compiler never
25759 generates any out-of-lines instances of such things (despite the fact
25760 that they *are* definitions).
25762 The important point is that the C front-end marks these "extern
25763 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
25764 them anyway. Note that the C++ front-end also plays some similar games
25765 for inline function definitions appearing within include files which
25766 also contain `#pragma interface' pragmas.
25768 If we are called from dwarf2out_abstract_function output a DIE
25769 anyway. We can end up here this way with early inlining and LTO
25770 where the inlined function is output in a different LTRANS unit
25771 or not at all. */
25772 if (DECL_INITIAL (decl) == NULL_TREE
25773 && ! DECL_ABSTRACT_P (decl))
25774 return;
25776 /* If we're a nested function, initially use a parent of NULL; if we're
25777 a plain function, this will be fixed up in decls_for_scope. If
25778 we're a method, it will be ignored, since we already have a DIE. */
25779 if (decl_function_context (decl)
25780 /* But if we're in terse mode, we don't care about scope. */
25781 && debug_info_level > DINFO_LEVEL_TERSE)
25782 context_die = NULL;
25783 break;
25785 case VAR_DECL:
25786 /* For local statics lookup proper context die. */
25787 if (local_function_static (decl))
25788 context_die = lookup_decl_die (DECL_CONTEXT (decl));
25790 /* If we are in terse mode, don't generate any DIEs to represent any
25791 variable declarations or definitions. */
25792 if (debug_info_level <= DINFO_LEVEL_TERSE)
25793 return;
25794 break;
25796 case CONST_DECL:
25797 if (debug_info_level <= DINFO_LEVEL_TERSE)
25798 return;
25799 if (!is_fortran () && !is_ada ())
25800 return;
25801 if (TREE_STATIC (decl) && decl_function_context (decl))
25802 context_die = lookup_decl_die (DECL_CONTEXT (decl));
25803 break;
25805 case NAMESPACE_DECL:
25806 case IMPORTED_DECL:
25807 if (debug_info_level <= DINFO_LEVEL_TERSE)
25808 return;
25809 if (lookup_decl_die (decl) != NULL)
25810 return;
25811 break;
25813 case TYPE_DECL:
25814 /* Don't emit stubs for types unless they are needed by other DIEs. */
25815 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
25816 return;
25818 /* Don't bother trying to generate any DIEs to represent any of the
25819 normal built-in types for the language we are compiling. */
25820 if (DECL_IS_BUILTIN (decl))
25821 return;
25823 /* If we are in terse mode, don't generate any DIEs for types. */
25824 if (debug_info_level <= DINFO_LEVEL_TERSE)
25825 return;
25827 /* If we're a function-scope tag, initially use a parent of NULL;
25828 this will be fixed up in decls_for_scope. */
25829 if (decl_function_context (decl))
25830 context_die = NULL;
25832 break;
25834 case NAMELIST_DECL:
25835 break;
25837 default:
25838 return;
25841 gen_decl_die (decl, NULL, NULL, context_die);
25843 if (flag_checking)
25845 dw_die_ref die = lookup_decl_die (decl);
25846 if (die)
25847 check_die (die);
25851 /* Write the debugging output for DECL. */
25853 static void
25854 dwarf2out_function_decl (tree decl)
25856 dwarf2out_decl (decl);
25857 call_arg_locations = NULL;
25858 call_arg_loc_last = NULL;
25859 call_site_count = -1;
25860 tail_call_site_count = -1;
25861 decl_loc_table->empty ();
25862 cached_dw_loc_list_table->empty ();
25865 /* Output a marker (i.e. a label) for the beginning of the generated code for
25866 a lexical block. */
25868 static void
25869 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
25870 unsigned int blocknum)
25872 switch_to_section (current_function_section ());
25873 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
25876 /* Output a marker (i.e. a label) for the end of the generated code for a
25877 lexical block. */
25879 static void
25880 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
25882 switch_to_section (current_function_section ());
25883 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
25886 /* Returns nonzero if it is appropriate not to emit any debugging
25887 information for BLOCK, because it doesn't contain any instructions.
25889 Don't allow this for blocks with nested functions or local classes
25890 as we would end up with orphans, and in the presence of scheduling
25891 we may end up calling them anyway. */
25893 static bool
25894 dwarf2out_ignore_block (const_tree block)
25896 tree decl;
25897 unsigned int i;
25899 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
25900 if (TREE_CODE (decl) == FUNCTION_DECL
25901 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
25902 return 0;
25903 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
25905 decl = BLOCK_NONLOCALIZED_VAR (block, i);
25906 if (TREE_CODE (decl) == FUNCTION_DECL
25907 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
25908 return 0;
25911 return 1;
25914 /* Hash table routines for file_hash. */
25916 bool
25917 dwarf_file_hasher::equal (dwarf_file_data *p1, const char *p2)
25919 return filename_cmp (p1->filename, p2) == 0;
25922 hashval_t
25923 dwarf_file_hasher::hash (dwarf_file_data *p)
25925 return htab_hash_string (p->filename);
25928 /* Lookup FILE_NAME (in the list of filenames that we know about here in
25929 dwarf2out.c) and return its "index". The index of each (known) filename is
25930 just a unique number which is associated with only that one filename. We
25931 need such numbers for the sake of generating labels (in the .debug_sfnames
25932 section) and references to those files numbers (in the .debug_srcinfo
25933 and .debug_macinfo sections). If the filename given as an argument is not
25934 found in our current list, add it to the list and assign it the next
25935 available unique index number. */
25937 static struct dwarf_file_data *
25938 lookup_filename (const char *file_name)
25940 struct dwarf_file_data * created;
25942 if (!file_name)
25943 return NULL;
25945 dwarf_file_data **slot
25946 = file_table->find_slot_with_hash (file_name, htab_hash_string (file_name),
25947 INSERT);
25948 if (*slot)
25949 return *slot;
25951 created = ggc_alloc<dwarf_file_data> ();
25952 created->filename = file_name;
25953 created->emitted_number = 0;
25954 *slot = created;
25955 return created;
25958 /* If the assembler will construct the file table, then translate the compiler
25959 internal file table number into the assembler file table number, and emit
25960 a .file directive if we haven't already emitted one yet. The file table
25961 numbers are different because we prune debug info for unused variables and
25962 types, which may include filenames. */
25964 static int
25965 maybe_emit_file (struct dwarf_file_data * fd)
25967 if (! fd->emitted_number)
25969 if (last_emitted_file)
25970 fd->emitted_number = last_emitted_file->emitted_number + 1;
25971 else
25972 fd->emitted_number = 1;
25973 last_emitted_file = fd;
25975 if (DWARF2_ASM_LINE_DEBUG_INFO)
25977 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
25978 output_quoted_string (asm_out_file,
25979 remap_debug_filename (fd->filename));
25980 fputc ('\n', asm_out_file);
25984 return fd->emitted_number;
25987 /* Schedule generation of a DW_AT_const_value attribute to DIE.
25988 That generation should happen after function debug info has been
25989 generated. The value of the attribute is the constant value of ARG. */
25991 static void
25992 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
25994 die_arg_entry entry;
25996 if (!die || !arg)
25997 return;
25999 gcc_assert (early_dwarf);
26001 if (!tmpl_value_parm_die_table)
26002 vec_alloc (tmpl_value_parm_die_table, 32);
26004 entry.die = die;
26005 entry.arg = arg;
26006 vec_safe_push (tmpl_value_parm_die_table, entry);
26009 /* Return TRUE if T is an instance of generic type, FALSE
26010 otherwise. */
26012 static bool
26013 generic_type_p (tree t)
26015 if (t == NULL_TREE || !TYPE_P (t))
26016 return false;
26017 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
26020 /* Schedule the generation of the generic parameter dies for the
26021 instance of generic type T. The proper generation itself is later
26022 done by gen_scheduled_generic_parms_dies. */
26024 static void
26025 schedule_generic_params_dies_gen (tree t)
26027 if (!generic_type_p (t))
26028 return;
26030 gcc_assert (early_dwarf);
26032 if (!generic_type_instances)
26033 vec_alloc (generic_type_instances, 256);
26035 vec_safe_push (generic_type_instances, t);
26038 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
26039 by append_entry_to_tmpl_value_parm_die_table. This function must
26040 be called after function DIEs have been generated. */
26042 static void
26043 gen_remaining_tmpl_value_param_die_attribute (void)
26045 if (tmpl_value_parm_die_table)
26047 unsigned i, j;
26048 die_arg_entry *e;
26050 /* We do this in two phases - first get the cases we can
26051 handle during early-finish, preserving those we cannot
26052 (containing symbolic constants where we don't yet know
26053 whether we are going to output the referenced symbols).
26054 For those we try again at late-finish. */
26055 j = 0;
26056 FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
26058 if (!tree_add_const_value_attribute (e->die, e->arg))
26060 dw_loc_descr_ref loc = NULL;
26061 if (! early_dwarf
26062 && (dwarf_version >= 5 || !dwarf_strict))
26063 loc = loc_descriptor_from_tree (e->arg, 2, NULL);
26064 if (loc)
26065 add_AT_loc (e->die, DW_AT_location, loc);
26066 else
26067 (*tmpl_value_parm_die_table)[j++] = *e;
26070 tmpl_value_parm_die_table->truncate (j);
26074 /* Generate generic parameters DIEs for instances of generic types
26075 that have been previously scheduled by
26076 schedule_generic_params_dies_gen. This function must be called
26077 after all the types of the CU have been laid out. */
26079 static void
26080 gen_scheduled_generic_parms_dies (void)
26082 unsigned i;
26083 tree t;
26085 if (!generic_type_instances)
26086 return;
26088 FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
26089 if (COMPLETE_TYPE_P (t))
26090 gen_generic_params_dies (t);
26092 generic_type_instances = NULL;
26096 /* Replace DW_AT_name for the decl with name. */
26098 static void
26099 dwarf2out_set_name (tree decl, tree name)
26101 dw_die_ref die;
26102 dw_attr_node *attr;
26103 const char *dname;
26105 die = TYPE_SYMTAB_DIE (decl);
26106 if (!die)
26107 return;
26109 dname = dwarf2_name (name, 0);
26110 if (!dname)
26111 return;
26113 attr = get_AT (die, DW_AT_name);
26114 if (attr)
26116 struct indirect_string_node *node;
26118 node = find_AT_string (dname);
26119 /* replace the string. */
26120 attr->dw_attr_val.v.val_str = node;
26123 else
26124 add_name_attribute (die, dname);
26127 /* True if before or during processing of the first function being emitted. */
26128 static bool in_first_function_p = true;
26129 /* True if loc_note during dwarf2out_var_location call might still be
26130 before first real instruction at address equal to .Ltext0. */
26131 static bool maybe_at_text_label_p = true;
26132 /* One above highest N where .LVLN label might be equal to .Ltext0 label. */
26133 static unsigned int first_loclabel_num_not_at_text_label;
26135 /* Called by the final INSN scan whenever we see a var location. We
26136 use it to drop labels in the right places, and throw the location in
26137 our lookup table. */
26139 static void
26140 dwarf2out_var_location (rtx_insn *loc_note)
26142 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
26143 struct var_loc_node *newloc;
26144 rtx_insn *next_real, *next_note;
26145 rtx_insn *call_insn = NULL;
26146 static const char *last_label;
26147 static const char *last_postcall_label;
26148 static bool last_in_cold_section_p;
26149 static rtx_insn *expected_next_loc_note;
26150 tree decl;
26151 bool var_loc_p;
26153 if (!NOTE_P (loc_note))
26155 if (CALL_P (loc_note))
26157 call_site_count++;
26158 if (SIBLING_CALL_P (loc_note))
26159 tail_call_site_count++;
26160 if (optimize == 0 && !flag_var_tracking)
26162 /* When the var-tracking pass is not running, there is no note
26163 for indirect calls whose target is compile-time known. In this
26164 case, process such calls specifically so that we generate call
26165 sites for them anyway. */
26166 rtx x = PATTERN (loc_note);
26167 if (GET_CODE (x) == PARALLEL)
26168 x = XVECEXP (x, 0, 0);
26169 if (GET_CODE (x) == SET)
26170 x = SET_SRC (x);
26171 if (GET_CODE (x) == CALL)
26172 x = XEXP (x, 0);
26173 if (!MEM_P (x)
26174 || GET_CODE (XEXP (x, 0)) != SYMBOL_REF
26175 || !SYMBOL_REF_DECL (XEXP (x, 0))
26176 || (TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0)))
26177 != FUNCTION_DECL))
26179 call_insn = loc_note;
26180 loc_note = NULL;
26181 var_loc_p = false;
26183 next_real = next_real_insn (call_insn);
26184 next_note = NULL;
26185 cached_next_real_insn = NULL;
26186 goto create_label;
26190 return;
26193 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
26194 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
26195 return;
26197 /* Optimize processing a large consecutive sequence of location
26198 notes so we don't spend too much time in next_real_insn. If the
26199 next insn is another location note, remember the next_real_insn
26200 calculation for next time. */
26201 next_real = cached_next_real_insn;
26202 if (next_real)
26204 if (expected_next_loc_note != loc_note)
26205 next_real = NULL;
26208 next_note = NEXT_INSN (loc_note);
26209 if (! next_note
26210 || next_note->deleted ()
26211 || ! NOTE_P (next_note)
26212 || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
26213 && NOTE_KIND (next_note) != NOTE_INSN_CALL_ARG_LOCATION))
26214 next_note = NULL;
26216 if (! next_real)
26217 next_real = next_real_insn (loc_note);
26219 if (next_note)
26221 expected_next_loc_note = next_note;
26222 cached_next_real_insn = next_real;
26224 else
26225 cached_next_real_insn = NULL;
26227 /* If there are no instructions which would be affected by this note,
26228 don't do anything. */
26229 if (var_loc_p
26230 && next_real == NULL_RTX
26231 && !NOTE_DURING_CALL_P (loc_note))
26232 return;
26234 create_label:
26236 if (next_real == NULL_RTX)
26237 next_real = get_last_insn ();
26239 /* If there were any real insns between note we processed last time
26240 and this note (or if it is the first note), clear
26241 last_{,postcall_}label so that they are not reused this time. */
26242 if (last_var_location_insn == NULL_RTX
26243 || last_var_location_insn != next_real
26244 || last_in_cold_section_p != in_cold_section_p)
26246 last_label = NULL;
26247 last_postcall_label = NULL;
26250 if (var_loc_p)
26252 decl = NOTE_VAR_LOCATION_DECL (loc_note);
26253 newloc = add_var_loc_to_decl (decl, loc_note,
26254 NOTE_DURING_CALL_P (loc_note)
26255 ? last_postcall_label : last_label);
26256 if (newloc == NULL)
26257 return;
26259 else
26261 decl = NULL_TREE;
26262 newloc = NULL;
26265 /* If there were no real insns between note we processed last time
26266 and this note, use the label we emitted last time. Otherwise
26267 create a new label and emit it. */
26268 if (last_label == NULL)
26270 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
26271 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
26272 loclabel_num++;
26273 last_label = ggc_strdup (loclabel);
26274 /* See if loclabel might be equal to .Ltext0. If yes,
26275 bump first_loclabel_num_not_at_text_label. */
26276 if (!have_multiple_function_sections
26277 && in_first_function_p
26278 && maybe_at_text_label_p)
26280 static rtx_insn *last_start;
26281 rtx_insn *insn;
26282 for (insn = loc_note; insn; insn = previous_insn (insn))
26283 if (insn == last_start)
26284 break;
26285 else if (!NONDEBUG_INSN_P (insn))
26286 continue;
26287 else
26289 rtx body = PATTERN (insn);
26290 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
26291 continue;
26292 /* Inline asm could occupy zero bytes. */
26293 else if (GET_CODE (body) == ASM_INPUT
26294 || asm_noperands (body) >= 0)
26295 continue;
26296 #ifdef HAVE_attr_length
26297 else if (get_attr_min_length (insn) == 0)
26298 continue;
26299 #endif
26300 else
26302 /* Assume insn has non-zero length. */
26303 maybe_at_text_label_p = false;
26304 break;
26307 if (maybe_at_text_label_p)
26309 last_start = loc_note;
26310 first_loclabel_num_not_at_text_label = loclabel_num;
26315 gcc_assert ((loc_note == NULL_RTX && call_insn != NULL_RTX)
26316 || (loc_note != NULL_RTX && call_insn == NULL_RTX));
26318 if (!var_loc_p)
26320 struct call_arg_loc_node *ca_loc
26321 = ggc_cleared_alloc<call_arg_loc_node> ();
26322 rtx_insn *prev
26323 = loc_note != NULL_RTX ? prev_real_insn (loc_note) : call_insn;
26325 ca_loc->call_arg_loc_note = loc_note;
26326 ca_loc->next = NULL;
26327 ca_loc->label = last_label;
26328 gcc_assert (prev
26329 && (CALL_P (prev)
26330 || (NONJUMP_INSN_P (prev)
26331 && GET_CODE (PATTERN (prev)) == SEQUENCE
26332 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
26333 if (!CALL_P (prev))
26334 prev = as_a <rtx_sequence *> (PATTERN (prev))->insn (0);
26335 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
26337 /* Look for a SYMBOL_REF in the "prev" instruction. */
26338 rtx x = get_call_rtx_from (PATTERN (prev));
26339 if (x)
26341 /* Try to get the call symbol, if any. */
26342 if (MEM_P (XEXP (x, 0)))
26343 x = XEXP (x, 0);
26344 /* First, look for a memory access to a symbol_ref. */
26345 if (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
26346 && SYMBOL_REF_DECL (XEXP (x, 0))
26347 && TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0))) == FUNCTION_DECL)
26348 ca_loc->symbol_ref = XEXP (x, 0);
26349 /* Otherwise, look at a compile-time known user-level function
26350 declaration. */
26351 else if (MEM_P (x)
26352 && MEM_EXPR (x)
26353 && TREE_CODE (MEM_EXPR (x)) == FUNCTION_DECL)
26354 ca_loc->symbol_ref = XEXP (DECL_RTL (MEM_EXPR (x)), 0);
26357 ca_loc->block = insn_scope (prev);
26358 if (call_arg_locations)
26359 call_arg_loc_last->next = ca_loc;
26360 else
26361 call_arg_locations = ca_loc;
26362 call_arg_loc_last = ca_loc;
26364 else if (loc_note != NULL_RTX && !NOTE_DURING_CALL_P (loc_note))
26365 newloc->label = last_label;
26366 else
26368 if (!last_postcall_label)
26370 sprintf (loclabel, "%s-1", last_label);
26371 last_postcall_label = ggc_strdup (loclabel);
26373 newloc->label = last_postcall_label;
26376 last_var_location_insn = next_real;
26377 last_in_cold_section_p = in_cold_section_p;
26380 /* Called from finalize_size_functions for size functions so that their body
26381 can be encoded in the debug info to describe the layout of variable-length
26382 structures. */
26384 static void
26385 dwarf2out_size_function (tree decl)
26387 function_to_dwarf_procedure (decl);
26390 /* Note in one location list that text section has changed. */
26393 var_location_switch_text_section_1 (var_loc_list **slot, void *)
26395 var_loc_list *list = *slot;
26396 if (list->first)
26397 list->last_before_switch
26398 = list->last->next ? list->last->next : list->last;
26399 return 1;
26402 /* Note in all location lists that text section has changed. */
26404 static void
26405 var_location_switch_text_section (void)
26407 if (decl_loc_table == NULL)
26408 return;
26410 decl_loc_table->traverse<void *, var_location_switch_text_section_1> (NULL);
26413 /* Create a new line number table. */
26415 static dw_line_info_table *
26416 new_line_info_table (void)
26418 dw_line_info_table *table;
26420 table = ggc_cleared_alloc<dw_line_info_table> ();
26421 table->file_num = 1;
26422 table->line_num = 1;
26423 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
26425 return table;
26428 /* Lookup the "current" table into which we emit line info, so
26429 that we don't have to do it for every source line. */
26431 static void
26432 set_cur_line_info_table (section *sec)
26434 dw_line_info_table *table;
26436 if (sec == text_section)
26437 table = text_section_line_info;
26438 else if (sec == cold_text_section)
26440 table = cold_text_section_line_info;
26441 if (!table)
26443 cold_text_section_line_info = table = new_line_info_table ();
26444 table->end_label = cold_end_label;
26447 else
26449 const char *end_label;
26451 if (flag_reorder_blocks_and_partition)
26453 if (in_cold_section_p)
26454 end_label = crtl->subsections.cold_section_end_label;
26455 else
26456 end_label = crtl->subsections.hot_section_end_label;
26458 else
26460 char label[MAX_ARTIFICIAL_LABEL_BYTES];
26461 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
26462 current_function_funcdef_no);
26463 end_label = ggc_strdup (label);
26466 table = new_line_info_table ();
26467 table->end_label = end_label;
26469 vec_safe_push (separate_line_info, table);
26472 if (DWARF2_ASM_LINE_DEBUG_INFO)
26473 table->is_stmt = (cur_line_info_table
26474 ? cur_line_info_table->is_stmt
26475 : DWARF_LINE_DEFAULT_IS_STMT_START);
26476 cur_line_info_table = table;
26480 /* We need to reset the locations at the beginning of each
26481 function. We can't do this in the end_function hook, because the
26482 declarations that use the locations won't have been output when
26483 that hook is called. Also compute have_multiple_function_sections here. */
26485 static void
26486 dwarf2out_begin_function (tree fun)
26488 section *sec = function_section (fun);
26490 if (sec != text_section)
26491 have_multiple_function_sections = true;
26493 if (flag_reorder_blocks_and_partition && !cold_text_section)
26495 gcc_assert (current_function_decl == fun);
26496 cold_text_section = unlikely_text_section ();
26497 switch_to_section (cold_text_section);
26498 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
26499 switch_to_section (sec);
26502 dwarf2out_note_section_used ();
26503 call_site_count = 0;
26504 tail_call_site_count = 0;
26506 set_cur_line_info_table (sec);
26509 /* Helper function of dwarf2out_end_function, called only after emitting
26510 the very first function into assembly. Check if some .debug_loc range
26511 might end with a .LVL* label that could be equal to .Ltext0.
26512 In that case we must force using absolute addresses in .debug_loc ranges,
26513 because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
26514 .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
26515 list terminator.
26516 Set have_multiple_function_sections to true in that case and
26517 terminate htab traversal. */
26520 find_empty_loc_ranges_at_text_label (var_loc_list **slot, int)
26522 var_loc_list *entry = *slot;
26523 struct var_loc_node *node;
26525 node = entry->first;
26526 if (node && node->next && node->next->label)
26528 unsigned int i;
26529 const char *label = node->next->label;
26530 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
26532 for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
26534 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
26535 if (strcmp (label, loclabel) == 0)
26537 have_multiple_function_sections = true;
26538 return 0;
26542 return 1;
26545 /* Hook called after emitting a function into assembly.
26546 This does something only for the very first function emitted. */
26548 static void
26549 dwarf2out_end_function (unsigned int)
26551 if (in_first_function_p
26552 && !have_multiple_function_sections
26553 && first_loclabel_num_not_at_text_label
26554 && decl_loc_table)
26555 decl_loc_table->traverse<int, find_empty_loc_ranges_at_text_label> (0);
26556 in_first_function_p = false;
26557 maybe_at_text_label_p = false;
26560 /* Temporary holder for dwarf2out_register_main_translation_unit. Used to let
26561 front-ends register a translation unit even before dwarf2out_init is
26562 called. */
26563 static tree main_translation_unit = NULL_TREE;
26565 /* Hook called by front-ends after they built their main translation unit.
26566 Associate comp_unit_die to UNIT. */
26568 static void
26569 dwarf2out_register_main_translation_unit (tree unit)
26571 gcc_assert (TREE_CODE (unit) == TRANSLATION_UNIT_DECL
26572 && main_translation_unit == NULL_TREE);
26573 main_translation_unit = unit;
26574 /* If dwarf2out_init has not been called yet, it will perform the association
26575 itself looking at main_translation_unit. */
26576 if (decl_die_table != NULL)
26577 equate_decl_number_to_die (unit, comp_unit_die ());
26580 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
26582 static void
26583 push_dw_line_info_entry (dw_line_info_table *table,
26584 enum dw_line_info_opcode opcode, unsigned int val)
26586 dw_line_info_entry e;
26587 e.opcode = opcode;
26588 e.val = val;
26589 vec_safe_push (table->entries, e);
26592 /* Output a label to mark the beginning of a source code line entry
26593 and record information relating to this source line, in
26594 'line_info_table' for later output of the .debug_line section. */
26595 /* ??? The discriminator parameter ought to be unsigned. */
26597 static void
26598 dwarf2out_source_line (unsigned int line, unsigned int column,
26599 const char *filename,
26600 int discriminator, bool is_stmt)
26602 unsigned int file_num;
26603 dw_line_info_table *table;
26605 if (debug_info_level < DINFO_LEVEL_TERSE || line == 0)
26606 return;
26608 /* The discriminator column was added in dwarf4. Simplify the below
26609 by simply removing it if we're not supposed to output it. */
26610 if (dwarf_version < 4 && dwarf_strict)
26611 discriminator = 0;
26613 if (!debug_column_info)
26614 column = 0;
26616 table = cur_line_info_table;
26617 file_num = maybe_emit_file (lookup_filename (filename));
26619 /* ??? TODO: Elide duplicate line number entries. Traditionally,
26620 the debugger has used the second (possibly duplicate) line number
26621 at the beginning of the function to mark the end of the prologue.
26622 We could eliminate any other duplicates within the function. For
26623 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
26624 that second line number entry. */
26625 /* Recall that this end-of-prologue indication is *not* the same thing
26626 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
26627 to which the hook corresponds, follows the last insn that was
26628 emitted by gen_prologue. What we need is to precede the first insn
26629 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
26630 insn that corresponds to something the user wrote. These may be
26631 very different locations once scheduling is enabled. */
26633 if (0 && file_num == table->file_num
26634 && line == table->line_num
26635 && column == table->column_num
26636 && discriminator == table->discrim_num
26637 && is_stmt == table->is_stmt)
26638 return;
26640 switch_to_section (current_function_section ());
26642 /* If requested, emit something human-readable. */
26643 if (flag_debug_asm)
26645 if (debug_column_info)
26646 fprintf (asm_out_file, "\t%s %s:%d:%d\n", ASM_COMMENT_START,
26647 filename, line, column);
26648 else
26649 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
26650 filename, line);
26653 if (DWARF2_ASM_LINE_DEBUG_INFO)
26655 /* Emit the .loc directive understood by GNU as. */
26656 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
26657 file_num, line, is_stmt, discriminator */
26658 fputs ("\t.loc ", asm_out_file);
26659 fprint_ul (asm_out_file, file_num);
26660 putc (' ', asm_out_file);
26661 fprint_ul (asm_out_file, line);
26662 putc (' ', asm_out_file);
26663 if (debug_column_info)
26664 fprint_ul (asm_out_file, column);
26665 else
26666 putc ('0', asm_out_file);
26668 if (is_stmt != table->is_stmt)
26670 fputs (" is_stmt ", asm_out_file);
26671 putc (is_stmt ? '1' : '0', asm_out_file);
26673 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
26675 gcc_assert (discriminator > 0);
26676 fputs (" discriminator ", asm_out_file);
26677 fprint_ul (asm_out_file, (unsigned long) discriminator);
26679 putc ('\n', asm_out_file);
26681 else
26683 unsigned int label_num = ++line_info_label_num;
26685 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
26687 push_dw_line_info_entry (table, LI_set_address, label_num);
26688 if (file_num != table->file_num)
26689 push_dw_line_info_entry (table, LI_set_file, file_num);
26690 if (discriminator != table->discrim_num)
26691 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
26692 if (is_stmt != table->is_stmt)
26693 push_dw_line_info_entry (table, LI_negate_stmt, 0);
26694 push_dw_line_info_entry (table, LI_set_line, line);
26695 if (debug_column_info)
26696 push_dw_line_info_entry (table, LI_set_column, column);
26699 table->file_num = file_num;
26700 table->line_num = line;
26701 table->column_num = column;
26702 table->discrim_num = discriminator;
26703 table->is_stmt = is_stmt;
26704 table->in_use = true;
26707 /* Record the beginning of a new source file. */
26709 static void
26710 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
26712 if (flag_eliminate_dwarf2_dups)
26714 /* Record the beginning of the file for break_out_includes. */
26715 dw_die_ref bincl_die;
26717 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
26718 add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
26721 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
26723 macinfo_entry e;
26724 e.code = DW_MACINFO_start_file;
26725 e.lineno = lineno;
26726 e.info = ggc_strdup (filename);
26727 vec_safe_push (macinfo_table, e);
26731 /* Record the end of a source file. */
26733 static void
26734 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
26736 if (flag_eliminate_dwarf2_dups)
26737 /* Record the end of the file for break_out_includes. */
26738 new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
26740 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
26742 macinfo_entry e;
26743 e.code = DW_MACINFO_end_file;
26744 e.lineno = lineno;
26745 e.info = NULL;
26746 vec_safe_push (macinfo_table, e);
26750 /* Called from debug_define in toplev.c. The `buffer' parameter contains
26751 the tail part of the directive line, i.e. the part which is past the
26752 initial whitespace, #, whitespace, directive-name, whitespace part. */
26754 static void
26755 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
26756 const char *buffer ATTRIBUTE_UNUSED)
26758 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
26760 macinfo_entry e;
26761 /* Insert a dummy first entry to be able to optimize the whole
26762 predefined macro block using DW_MACRO_import. */
26763 if (macinfo_table->is_empty () && lineno <= 1)
26765 e.code = 0;
26766 e.lineno = 0;
26767 e.info = NULL;
26768 vec_safe_push (macinfo_table, e);
26770 e.code = DW_MACINFO_define;
26771 e.lineno = lineno;
26772 e.info = ggc_strdup (buffer);
26773 vec_safe_push (macinfo_table, e);
26777 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
26778 the tail part of the directive line, i.e. the part which is past the
26779 initial whitespace, #, whitespace, directive-name, whitespace part. */
26781 static void
26782 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
26783 const char *buffer ATTRIBUTE_UNUSED)
26785 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
26787 macinfo_entry e;
26788 /* Insert a dummy first entry to be able to optimize the whole
26789 predefined macro block using DW_MACRO_import. */
26790 if (macinfo_table->is_empty () && lineno <= 1)
26792 e.code = 0;
26793 e.lineno = 0;
26794 e.info = NULL;
26795 vec_safe_push (macinfo_table, e);
26797 e.code = DW_MACINFO_undef;
26798 e.lineno = lineno;
26799 e.info = ggc_strdup (buffer);
26800 vec_safe_push (macinfo_table, e);
26804 /* Helpers to manipulate hash table of CUs. */
26806 struct macinfo_entry_hasher : nofree_ptr_hash <macinfo_entry>
26808 static inline hashval_t hash (const macinfo_entry *);
26809 static inline bool equal (const macinfo_entry *, const macinfo_entry *);
26812 inline hashval_t
26813 macinfo_entry_hasher::hash (const macinfo_entry *entry)
26815 return htab_hash_string (entry->info);
26818 inline bool
26819 macinfo_entry_hasher::equal (const macinfo_entry *entry1,
26820 const macinfo_entry *entry2)
26822 return !strcmp (entry1->info, entry2->info);
26825 typedef hash_table<macinfo_entry_hasher> macinfo_hash_type;
26827 /* Output a single .debug_macinfo entry. */
26829 static void
26830 output_macinfo_op (macinfo_entry *ref)
26832 int file_num;
26833 size_t len;
26834 struct indirect_string_node *node;
26835 char label[MAX_ARTIFICIAL_LABEL_BYTES];
26836 struct dwarf_file_data *fd;
26838 switch (ref->code)
26840 case DW_MACINFO_start_file:
26841 fd = lookup_filename (ref->info);
26842 file_num = maybe_emit_file (fd);
26843 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
26844 dw2_asm_output_data_uleb128 (ref->lineno,
26845 "Included from line number %lu",
26846 (unsigned long) ref->lineno);
26847 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
26848 break;
26849 case DW_MACINFO_end_file:
26850 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
26851 break;
26852 case DW_MACINFO_define:
26853 case DW_MACINFO_undef:
26854 len = strlen (ref->info) + 1;
26855 if (!dwarf_strict
26856 && len > DWARF_OFFSET_SIZE
26857 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
26858 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
26860 ref->code = ref->code == DW_MACINFO_define
26861 ? DW_MACRO_define_strp : DW_MACRO_undef_strp;
26862 output_macinfo_op (ref);
26863 return;
26865 dw2_asm_output_data (1, ref->code,
26866 ref->code == DW_MACINFO_define
26867 ? "Define macro" : "Undefine macro");
26868 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
26869 (unsigned long) ref->lineno);
26870 dw2_asm_output_nstring (ref->info, -1, "The macro");
26871 break;
26872 case DW_MACRO_define_strp:
26873 case DW_MACRO_undef_strp:
26874 node = find_AT_string (ref->info);
26875 gcc_assert (node
26876 && (node->form == DW_FORM_strp
26877 || node->form == DW_FORM_GNU_str_index));
26878 dw2_asm_output_data (1, ref->code,
26879 ref->code == DW_MACRO_define_strp
26880 ? "Define macro strp"
26881 : "Undefine macro strp");
26882 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
26883 (unsigned long) ref->lineno);
26884 if (node->form == DW_FORM_strp)
26885 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
26886 debug_str_section, "The macro: \"%s\"",
26887 ref->info);
26888 else
26889 dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
26890 ref->info);
26891 break;
26892 case DW_MACRO_import:
26893 dw2_asm_output_data (1, ref->code, "Import");
26894 ASM_GENERATE_INTERNAL_LABEL (label,
26895 DEBUG_MACRO_SECTION_LABEL, ref->lineno);
26896 dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
26897 break;
26898 default:
26899 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
26900 ASM_COMMENT_START, (unsigned long) ref->code);
26901 break;
26905 /* Attempt to make a sequence of define/undef macinfo ops shareable with
26906 other compilation unit .debug_macinfo sections. IDX is the first
26907 index of a define/undef, return the number of ops that should be
26908 emitted in a comdat .debug_macinfo section and emit
26909 a DW_MACRO_import entry referencing it.
26910 If the define/undef entry should be emitted normally, return 0. */
26912 static unsigned
26913 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
26914 macinfo_hash_type **macinfo_htab)
26916 macinfo_entry *first, *second, *cur, *inc;
26917 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
26918 unsigned char checksum[16];
26919 struct md5_ctx ctx;
26920 char *grp_name, *tail;
26921 const char *base;
26922 unsigned int i, count, encoded_filename_len, linebuf_len;
26923 macinfo_entry **slot;
26925 first = &(*macinfo_table)[idx];
26926 second = &(*macinfo_table)[idx + 1];
26928 /* Optimize only if there are at least two consecutive define/undef ops,
26929 and either all of them are before first DW_MACINFO_start_file
26930 with lineno {0,1} (i.e. predefined macro block), or all of them are
26931 in some included header file. */
26932 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
26933 return 0;
26934 if (vec_safe_is_empty (files))
26936 if (first->lineno > 1 || second->lineno > 1)
26937 return 0;
26939 else if (first->lineno == 0)
26940 return 0;
26942 /* Find the last define/undef entry that can be grouped together
26943 with first and at the same time compute md5 checksum of their
26944 codes, linenumbers and strings. */
26945 md5_init_ctx (&ctx);
26946 for (i = idx; macinfo_table->iterate (i, &cur); i++)
26947 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
26948 break;
26949 else if (vec_safe_is_empty (files) && cur->lineno > 1)
26950 break;
26951 else
26953 unsigned char code = cur->code;
26954 md5_process_bytes (&code, 1, &ctx);
26955 checksum_uleb128 (cur->lineno, &ctx);
26956 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
26958 md5_finish_ctx (&ctx, checksum);
26959 count = i - idx;
26961 /* From the containing include filename (if any) pick up just
26962 usable characters from its basename. */
26963 if (vec_safe_is_empty (files))
26964 base = "";
26965 else
26966 base = lbasename (files->last ().info);
26967 for (encoded_filename_len = 0, i = 0; base[i]; i++)
26968 if (ISIDNUM (base[i]) || base[i] == '.')
26969 encoded_filename_len++;
26970 /* Count . at the end. */
26971 if (encoded_filename_len)
26972 encoded_filename_len++;
26974 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
26975 linebuf_len = strlen (linebuf);
26977 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
26978 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
26979 + 16 * 2 + 1);
26980 memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
26981 tail = grp_name + 4;
26982 if (encoded_filename_len)
26984 for (i = 0; base[i]; i++)
26985 if (ISIDNUM (base[i]) || base[i] == '.')
26986 *tail++ = base[i];
26987 *tail++ = '.';
26989 memcpy (tail, linebuf, linebuf_len);
26990 tail += linebuf_len;
26991 *tail++ = '.';
26992 for (i = 0; i < 16; i++)
26993 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
26995 /* Construct a macinfo_entry for DW_MACRO_import
26996 in the empty vector entry before the first define/undef. */
26997 inc = &(*macinfo_table)[idx - 1];
26998 inc->code = DW_MACRO_import;
26999 inc->lineno = 0;
27000 inc->info = ggc_strdup (grp_name);
27001 if (!*macinfo_htab)
27002 *macinfo_htab = new macinfo_hash_type (10);
27003 /* Avoid emitting duplicates. */
27004 slot = (*macinfo_htab)->find_slot (inc, INSERT);
27005 if (*slot != NULL)
27007 inc->code = 0;
27008 inc->info = NULL;
27009 /* If such an entry has been used before, just emit
27010 a DW_MACRO_import op. */
27011 inc = *slot;
27012 output_macinfo_op (inc);
27013 /* And clear all macinfo_entry in the range to avoid emitting them
27014 in the second pass. */
27015 for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
27017 cur->code = 0;
27018 cur->info = NULL;
27021 else
27023 *slot = inc;
27024 inc->lineno = (*macinfo_htab)->elements ();
27025 output_macinfo_op (inc);
27027 return count;
27030 /* Save any strings needed by the macinfo table in the debug str
27031 table. All strings must be collected into the table by the time
27032 index_string is called. */
27034 static void
27035 save_macinfo_strings (void)
27037 unsigned len;
27038 unsigned i;
27039 macinfo_entry *ref;
27041 for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
27043 switch (ref->code)
27045 /* Match the logic in output_macinfo_op to decide on
27046 indirect strings. */
27047 case DW_MACINFO_define:
27048 case DW_MACINFO_undef:
27049 len = strlen (ref->info) + 1;
27050 if (!dwarf_strict
27051 && len > DWARF_OFFSET_SIZE
27052 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
27053 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
27054 set_indirect_string (find_AT_string (ref->info));
27055 break;
27056 case DW_MACRO_define_strp:
27057 case DW_MACRO_undef_strp:
27058 set_indirect_string (find_AT_string (ref->info));
27059 break;
27060 default:
27061 break;
27066 /* Output macinfo section(s). */
27068 static void
27069 output_macinfo (void)
27071 unsigned i;
27072 unsigned long length = vec_safe_length (macinfo_table);
27073 macinfo_entry *ref;
27074 vec<macinfo_entry, va_gc> *files = NULL;
27075 macinfo_hash_type *macinfo_htab = NULL;
27077 if (! length)
27078 return;
27080 /* output_macinfo* uses these interchangeably. */
27081 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_define
27082 && (int) DW_MACINFO_undef == (int) DW_MACRO_undef
27083 && (int) DW_MACINFO_start_file == (int) DW_MACRO_start_file
27084 && (int) DW_MACINFO_end_file == (int) DW_MACRO_end_file);
27086 /* For .debug_macro emit the section header. */
27087 if (!dwarf_strict || dwarf_version >= 5)
27089 dw2_asm_output_data (2, dwarf_version >= 5 ? 5 : 4,
27090 "DWARF macro version number");
27091 if (DWARF_OFFSET_SIZE == 8)
27092 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
27093 else
27094 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
27095 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
27096 (!dwarf_split_debug_info ? debug_line_section_label
27097 : debug_skeleton_line_section_label),
27098 debug_line_section, NULL);
27101 /* In the first loop, it emits the primary .debug_macinfo section
27102 and after each emitted op the macinfo_entry is cleared.
27103 If a longer range of define/undef ops can be optimized using
27104 DW_MACRO_import, the DW_MACRO_import op is emitted and kept in
27105 the vector before the first define/undef in the range and the
27106 whole range of define/undef ops is not emitted and kept. */
27107 for (i = 0; macinfo_table->iterate (i, &ref); i++)
27109 switch (ref->code)
27111 case DW_MACINFO_start_file:
27112 vec_safe_push (files, *ref);
27113 break;
27114 case DW_MACINFO_end_file:
27115 if (!vec_safe_is_empty (files))
27116 files->pop ();
27117 break;
27118 case DW_MACINFO_define:
27119 case DW_MACINFO_undef:
27120 if ((!dwarf_strict || dwarf_version >= 5)
27121 && HAVE_COMDAT_GROUP
27122 && vec_safe_length (files) != 1
27123 && i > 0
27124 && i + 1 < length
27125 && (*macinfo_table)[i - 1].code == 0)
27127 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
27128 if (count)
27130 i += count - 1;
27131 continue;
27134 break;
27135 case 0:
27136 /* A dummy entry may be inserted at the beginning to be able
27137 to optimize the whole block of predefined macros. */
27138 if (i == 0)
27139 continue;
27140 default:
27141 break;
27143 output_macinfo_op (ref);
27144 ref->info = NULL;
27145 ref->code = 0;
27148 if (!macinfo_htab)
27149 return;
27151 delete macinfo_htab;
27152 macinfo_htab = NULL;
27154 /* If any DW_MACRO_import were used, on those DW_MACRO_import entries
27155 terminate the current chain and switch to a new comdat .debug_macinfo
27156 section and emit the define/undef entries within it. */
27157 for (i = 0; macinfo_table->iterate (i, &ref); i++)
27158 switch (ref->code)
27160 case 0:
27161 continue;
27162 case DW_MACRO_import:
27164 char label[MAX_ARTIFICIAL_LABEL_BYTES];
27165 tree comdat_key = get_identifier (ref->info);
27166 /* Terminate the previous .debug_macinfo section. */
27167 dw2_asm_output_data (1, 0, "End compilation unit");
27168 targetm.asm_out.named_section (debug_macinfo_section_name,
27169 SECTION_DEBUG
27170 | SECTION_LINKONCE,
27171 comdat_key);
27172 ASM_GENERATE_INTERNAL_LABEL (label,
27173 DEBUG_MACRO_SECTION_LABEL,
27174 ref->lineno);
27175 ASM_OUTPUT_LABEL (asm_out_file, label);
27176 ref->code = 0;
27177 ref->info = NULL;
27178 dw2_asm_output_data (2, dwarf_version >= 5 ? 5 : 4,
27179 "DWARF macro version number");
27180 if (DWARF_OFFSET_SIZE == 8)
27181 dw2_asm_output_data (1, 1, "Flags: 64-bit");
27182 else
27183 dw2_asm_output_data (1, 0, "Flags: 32-bit");
27185 break;
27186 case DW_MACINFO_define:
27187 case DW_MACINFO_undef:
27188 output_macinfo_op (ref);
27189 ref->code = 0;
27190 ref->info = NULL;
27191 break;
27192 default:
27193 gcc_unreachable ();
27197 /* Initialize the various sections and labels for dwarf output. */
27199 static void
27200 init_sections_and_labels (void)
27202 if (!dwarf_split_debug_info)
27204 debug_info_section = get_section (DEBUG_INFO_SECTION,
27205 SECTION_DEBUG, NULL);
27206 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
27207 SECTION_DEBUG, NULL);
27208 debug_loc_section = get_section (dwarf_version >= 5
27209 ? DEBUG_LOCLISTS_SECTION
27210 : DEBUG_LOC_SECTION,
27211 SECTION_DEBUG, NULL);
27212 debug_macinfo_section_name
27213 = (dwarf_strict && dwarf_version < 5)
27214 ? DEBUG_MACINFO_SECTION : DEBUG_MACRO_SECTION;
27215 debug_macinfo_section = get_section (debug_macinfo_section_name,
27216 SECTION_DEBUG, NULL);
27218 else
27220 debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
27221 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
27222 debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
27223 SECTION_DEBUG | SECTION_EXCLUDE,
27224 NULL);
27225 debug_addr_section = get_section (DEBUG_ADDR_SECTION,
27226 SECTION_DEBUG, NULL);
27227 debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
27228 SECTION_DEBUG, NULL);
27229 debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
27230 SECTION_DEBUG, NULL);
27231 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
27232 DEBUG_SKELETON_ABBREV_SECTION_LABEL, 0);
27234 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections stay in
27235 the main .o, but the skeleton_line goes into the split off dwo. */
27236 debug_skeleton_line_section
27237 = get_section (DEBUG_DWO_LINE_SECTION,
27238 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
27239 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
27240 DEBUG_SKELETON_LINE_SECTION_LABEL, 0);
27241 debug_str_offsets_section = get_section (DEBUG_DWO_STR_OFFSETS_SECTION,
27242 SECTION_DEBUG | SECTION_EXCLUDE,
27243 NULL);
27244 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
27245 DEBUG_SKELETON_INFO_SECTION_LABEL, 0);
27246 debug_loc_section = get_section (dwarf_version >= 5
27247 ? DEBUG_DWO_LOCLISTS_SECTION
27248 : DEBUG_DWO_LOC_SECTION,
27249 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
27250 debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
27251 DEBUG_STR_DWO_SECTION_FLAGS, NULL);
27252 debug_macinfo_section_name
27253 = (dwarf_strict && dwarf_version < 5)
27254 ? DEBUG_DWO_MACINFO_SECTION : DEBUG_DWO_MACRO_SECTION;
27255 debug_macinfo_section = get_section (debug_macinfo_section_name,
27256 SECTION_DEBUG | SECTION_EXCLUDE,
27257 NULL);
27259 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
27260 SECTION_DEBUG, NULL);
27261 debug_line_section = get_section (DEBUG_LINE_SECTION,
27262 SECTION_DEBUG, NULL);
27263 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
27264 SECTION_DEBUG, NULL);
27265 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
27266 SECTION_DEBUG, NULL);
27267 debug_str_section = get_section (DEBUG_STR_SECTION,
27268 DEBUG_STR_SECTION_FLAGS, NULL);
27269 if (!dwarf_split_debug_info && !DWARF2_ASM_LINE_DEBUG_INFO)
27270 debug_line_str_section = get_section (DEBUG_LINE_STR_SECTION,
27271 DEBUG_STR_SECTION_FLAGS, NULL);
27273 debug_ranges_section = get_section (dwarf_version >= 5
27274 ? DEBUG_RNGLISTS_SECTION
27275 : DEBUG_RANGES_SECTION,
27276 SECTION_DEBUG, NULL);
27277 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
27278 SECTION_DEBUG, NULL);
27280 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
27281 DEBUG_ABBREV_SECTION_LABEL, 0);
27282 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
27283 DEBUG_INFO_SECTION_LABEL, 0);
27284 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
27285 DEBUG_LINE_SECTION_LABEL, 0);
27286 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
27287 DEBUG_RANGES_SECTION_LABEL, 0);
27288 if (dwarf_version >= 5 && dwarf_split_debug_info)
27289 ASM_GENERATE_INTERNAL_LABEL (ranges_base_label,
27290 DEBUG_RANGES_SECTION_LABEL, 1);
27291 ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
27292 DEBUG_ADDR_SECTION_LABEL, 0);
27293 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
27294 (dwarf_strict && dwarf_version < 5)
27295 ? DEBUG_MACINFO_SECTION_LABEL
27296 : DEBUG_MACRO_SECTION_LABEL, 0);
27297 ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL, 0);
27300 /* Set up for Dwarf output at the start of compilation. */
27302 static void
27303 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
27305 /* This option is currently broken, see (PR53118 and PR46102). */
27306 if (flag_eliminate_dwarf2_dups
27307 && strstr (lang_hooks.name, "C++"))
27309 warning (0, "-feliminate-dwarf2-dups is broken for C++, ignoring");
27310 flag_eliminate_dwarf2_dups = 0;
27313 /* Allocate the file_table. */
27314 file_table = hash_table<dwarf_file_hasher>::create_ggc (50);
27316 #ifndef DWARF2_LINENO_DEBUGGING_INFO
27317 /* Allocate the decl_die_table. */
27318 decl_die_table = hash_table<decl_die_hasher>::create_ggc (10);
27320 /* Allocate the decl_loc_table. */
27321 decl_loc_table = hash_table<decl_loc_hasher>::create_ggc (10);
27323 /* Allocate the cached_dw_loc_list_table. */
27324 cached_dw_loc_list_table = hash_table<dw_loc_list_hasher>::create_ggc (10);
27326 /* Allocate the initial hunk of the decl_scope_table. */
27327 vec_alloc (decl_scope_table, 256);
27329 /* Allocate the initial hunk of the abbrev_die_table. */
27330 vec_alloc (abbrev_die_table, 256);
27331 /* Zero-th entry is allocated, but unused. */
27332 abbrev_die_table->quick_push (NULL);
27334 /* Allocate the dwarf_proc_stack_usage_map. */
27335 dwarf_proc_stack_usage_map = new hash_map<dw_die_ref, int>;
27337 /* Allocate the pubtypes and pubnames vectors. */
27338 vec_alloc (pubname_table, 32);
27339 vec_alloc (pubtype_table, 32);
27341 vec_alloc (incomplete_types, 64);
27343 vec_alloc (used_rtx_array, 32);
27345 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
27346 vec_alloc (macinfo_table, 64);
27347 #endif
27349 /* If front-ends already registered a main translation unit but we were not
27350 ready to perform the association, do this now. */
27351 if (main_translation_unit != NULL_TREE)
27352 equate_decl_number_to_die (main_translation_unit, comp_unit_die ());
27355 /* Called before compile () starts outputtting functions, variables
27356 and toplevel asms into assembly. */
27358 static void
27359 dwarf2out_assembly_start (void)
27361 #ifndef DWARF2_LINENO_DEBUGGING_INFO
27362 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
27363 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
27364 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
27365 COLD_TEXT_SECTION_LABEL, 0);
27366 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
27368 switch_to_section (text_section);
27369 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
27370 #endif
27372 /* Make sure the line number table for .text always exists. */
27373 text_section_line_info = new_line_info_table ();
27374 text_section_line_info->end_label = text_end_label;
27376 #ifdef DWARF2_LINENO_DEBUGGING_INFO
27377 cur_line_info_table = text_section_line_info;
27378 #endif
27380 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
27381 && dwarf2out_do_cfi_asm ()
27382 && (!(flag_unwind_tables || flag_exceptions)
27383 || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
27384 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
27387 /* A helper function for dwarf2out_finish called through
27388 htab_traverse. Assign a string its index. All strings must be
27389 collected into the table by the time index_string is called,
27390 because the indexing code relies on htab_traverse to traverse nodes
27391 in the same order for each run. */
27394 index_string (indirect_string_node **h, unsigned int *index)
27396 indirect_string_node *node = *h;
27398 find_string_form (node);
27399 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
27401 gcc_assert (node->index == NO_INDEX_ASSIGNED);
27402 node->index = *index;
27403 *index += 1;
27405 return 1;
27408 /* A helper function for output_indirect_strings called through
27409 htab_traverse. Output the offset to a string and update the
27410 current offset. */
27413 output_index_string_offset (indirect_string_node **h, unsigned int *offset)
27415 indirect_string_node *node = *h;
27417 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
27419 /* Assert that this node has been assigned an index. */
27420 gcc_assert (node->index != NO_INDEX_ASSIGNED
27421 && node->index != NOT_INDEXED);
27422 dw2_asm_output_data (DWARF_OFFSET_SIZE, *offset,
27423 "indexed string 0x%x: %s", node->index, node->str);
27424 *offset += strlen (node->str) + 1;
27426 return 1;
27429 /* A helper function for dwarf2out_finish called through
27430 htab_traverse. Output the indexed string. */
27433 output_index_string (indirect_string_node **h, unsigned int *cur_idx)
27435 struct indirect_string_node *node = *h;
27437 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
27439 /* Assert that the strings are output in the same order as their
27440 indexes were assigned. */
27441 gcc_assert (*cur_idx == node->index);
27442 assemble_string (node->str, strlen (node->str) + 1);
27443 *cur_idx += 1;
27445 return 1;
27448 /* A helper function for dwarf2out_finish called through
27449 htab_traverse. Emit one queued .debug_str string. */
27452 output_indirect_string (indirect_string_node **h, enum dwarf_form form)
27454 struct indirect_string_node *node = *h;
27456 node->form = find_string_form (node);
27457 if (node->form == form && node->refcount > 0)
27459 ASM_OUTPUT_LABEL (asm_out_file, node->label);
27460 assemble_string (node->str, strlen (node->str) + 1);
27463 return 1;
27466 /* Output the indexed string table. */
27468 static void
27469 output_indirect_strings (void)
27471 switch_to_section (debug_str_section);
27472 if (!dwarf_split_debug_info)
27473 debug_str_hash->traverse<enum dwarf_form,
27474 output_indirect_string> (DW_FORM_strp);
27475 else
27477 unsigned int offset = 0;
27478 unsigned int cur_idx = 0;
27480 skeleton_debug_str_hash->traverse<enum dwarf_form,
27481 output_indirect_string> (DW_FORM_strp);
27483 switch_to_section (debug_str_offsets_section);
27484 debug_str_hash->traverse_noresize
27485 <unsigned int *, output_index_string_offset> (&offset);
27486 switch_to_section (debug_str_dwo_section);
27487 debug_str_hash->traverse_noresize<unsigned int *, output_index_string>
27488 (&cur_idx);
27492 /* Callback for htab_traverse to assign an index to an entry in the
27493 table, and to write that entry to the .debug_addr section. */
27496 output_addr_table_entry (addr_table_entry **slot, unsigned int *cur_index)
27498 addr_table_entry *entry = *slot;
27500 if (entry->refcount == 0)
27502 gcc_assert (entry->index == NO_INDEX_ASSIGNED
27503 || entry->index == NOT_INDEXED);
27504 return 1;
27507 gcc_assert (entry->index == *cur_index);
27508 (*cur_index)++;
27510 switch (entry->kind)
27512 case ate_kind_rtx:
27513 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
27514 "0x%x", entry->index);
27515 break;
27516 case ate_kind_rtx_dtprel:
27517 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
27518 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
27519 DWARF2_ADDR_SIZE,
27520 entry->addr.rtl);
27521 fputc ('\n', asm_out_file);
27522 break;
27523 case ate_kind_label:
27524 dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
27525 "0x%x", entry->index);
27526 break;
27527 default:
27528 gcc_unreachable ();
27530 return 1;
27533 /* Produce the .debug_addr section. */
27535 static void
27536 output_addr_table (void)
27538 unsigned int index = 0;
27539 if (addr_index_table == NULL || addr_index_table->size () == 0)
27540 return;
27542 switch_to_section (debug_addr_section);
27543 addr_index_table
27544 ->traverse_noresize<unsigned int *, output_addr_table_entry> (&index);
27547 #if ENABLE_ASSERT_CHECKING
27548 /* Verify that all marks are clear. */
27550 static void
27551 verify_marks_clear (dw_die_ref die)
27553 dw_die_ref c;
27555 gcc_assert (! die->die_mark);
27556 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
27558 #endif /* ENABLE_ASSERT_CHECKING */
27560 /* Clear the marks for a die and its children.
27561 Be cool if the mark isn't set. */
27563 static void
27564 prune_unmark_dies (dw_die_ref die)
27566 dw_die_ref c;
27568 if (die->die_mark)
27569 die->die_mark = 0;
27570 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
27573 /* Given LOC that is referenced by a DIE we're marking as used, find all
27574 referenced DWARF procedures it references and mark them as used. */
27576 static void
27577 prune_unused_types_walk_loc_descr (dw_loc_descr_ref loc)
27579 for (; loc != NULL; loc = loc->dw_loc_next)
27580 switch (loc->dw_loc_opc)
27582 case DW_OP_implicit_pointer:
27583 case DW_OP_convert:
27584 case DW_OP_reinterpret:
27585 case DW_OP_GNU_implicit_pointer:
27586 case DW_OP_GNU_convert:
27587 case DW_OP_GNU_reinterpret:
27588 if (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref)
27589 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
27590 break;
27591 case DW_OP_GNU_variable_value:
27592 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
27594 dw_die_ref ref
27595 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
27596 if (ref == NULL)
27597 break;
27598 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
27599 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
27600 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
27602 /* FALLTHRU */
27603 case DW_OP_call2:
27604 case DW_OP_call4:
27605 case DW_OP_call_ref:
27606 case DW_OP_const_type:
27607 case DW_OP_GNU_const_type:
27608 case DW_OP_GNU_parameter_ref:
27609 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref);
27610 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
27611 break;
27612 case DW_OP_regval_type:
27613 case DW_OP_deref_type:
27614 case DW_OP_GNU_regval_type:
27615 case DW_OP_GNU_deref_type:
27616 gcc_assert (loc->dw_loc_oprnd2.val_class == dw_val_class_die_ref);
27617 prune_unused_types_mark (loc->dw_loc_oprnd2.v.val_die_ref.die, 1);
27618 break;
27619 case DW_OP_entry_value:
27620 case DW_OP_GNU_entry_value:
27621 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_loc);
27622 prune_unused_types_walk_loc_descr (loc->dw_loc_oprnd1.v.val_loc);
27623 break;
27624 default:
27625 break;
27629 /* Given DIE that we're marking as used, find any other dies
27630 it references as attributes and mark them as used. */
27632 static void
27633 prune_unused_types_walk_attribs (dw_die_ref die)
27635 dw_attr_node *a;
27636 unsigned ix;
27638 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
27640 switch (AT_class (a))
27642 /* Make sure DWARF procedures referenced by location descriptions will
27643 get emitted. */
27644 case dw_val_class_loc:
27645 prune_unused_types_walk_loc_descr (AT_loc (a));
27646 break;
27647 case dw_val_class_loc_list:
27648 for (dw_loc_list_ref list = AT_loc_list (a);
27649 list != NULL;
27650 list = list->dw_loc_next)
27651 prune_unused_types_walk_loc_descr (list->expr);
27652 break;
27654 case dw_val_class_die_ref:
27655 /* A reference to another DIE.
27656 Make sure that it will get emitted.
27657 If it was broken out into a comdat group, don't follow it. */
27658 if (! AT_ref (a)->comdat_type_p
27659 || a->dw_attr == DW_AT_specification)
27660 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
27661 break;
27663 case dw_val_class_str:
27664 /* Set the string's refcount to 0 so that prune_unused_types_mark
27665 accounts properly for it. */
27666 a->dw_attr_val.v.val_str->refcount = 0;
27667 break;
27669 default:
27670 break;
27675 /* Mark the generic parameters and arguments children DIEs of DIE. */
27677 static void
27678 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
27680 dw_die_ref c;
27682 if (die == NULL || die->die_child == NULL)
27683 return;
27684 c = die->die_child;
27687 if (is_template_parameter (c))
27688 prune_unused_types_mark (c, 1);
27689 c = c->die_sib;
27690 } while (c && c != die->die_child);
27693 /* Mark DIE as being used. If DOKIDS is true, then walk down
27694 to DIE's children. */
27696 static void
27697 prune_unused_types_mark (dw_die_ref die, int dokids)
27699 dw_die_ref c;
27701 if (die->die_mark == 0)
27703 /* We haven't done this node yet. Mark it as used. */
27704 die->die_mark = 1;
27705 /* If this is the DIE of a generic type instantiation,
27706 mark the children DIEs that describe its generic parms and
27707 args. */
27708 prune_unused_types_mark_generic_parms_dies (die);
27710 /* We also have to mark its parents as used.
27711 (But we don't want to mark our parent's kids due to this,
27712 unless it is a class.) */
27713 if (die->die_parent)
27714 prune_unused_types_mark (die->die_parent,
27715 class_scope_p (die->die_parent));
27717 /* Mark any referenced nodes. */
27718 prune_unused_types_walk_attribs (die);
27720 /* If this node is a specification,
27721 also mark the definition, if it exists. */
27722 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
27723 prune_unused_types_mark (die->die_definition, 1);
27726 if (dokids && die->die_mark != 2)
27728 /* We need to walk the children, but haven't done so yet.
27729 Remember that we've walked the kids. */
27730 die->die_mark = 2;
27732 /* If this is an array type, we need to make sure our
27733 kids get marked, even if they're types. If we're
27734 breaking out types into comdat sections, do this
27735 for all type definitions. */
27736 if (die->die_tag == DW_TAG_array_type
27737 || (use_debug_types
27738 && is_type_die (die) && ! is_declaration_die (die)))
27739 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
27740 else
27741 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
27745 /* For local classes, look if any static member functions were emitted
27746 and if so, mark them. */
27748 static void
27749 prune_unused_types_walk_local_classes (dw_die_ref die)
27751 dw_die_ref c;
27753 if (die->die_mark == 2)
27754 return;
27756 switch (die->die_tag)
27758 case DW_TAG_structure_type:
27759 case DW_TAG_union_type:
27760 case DW_TAG_class_type:
27761 break;
27763 case DW_TAG_subprogram:
27764 if (!get_AT_flag (die, DW_AT_declaration)
27765 || die->die_definition != NULL)
27766 prune_unused_types_mark (die, 1);
27767 return;
27769 default:
27770 return;
27773 /* Mark children. */
27774 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
27777 /* Walk the tree DIE and mark types that we actually use. */
27779 static void
27780 prune_unused_types_walk (dw_die_ref die)
27782 dw_die_ref c;
27784 /* Don't do anything if this node is already marked and
27785 children have been marked as well. */
27786 if (die->die_mark == 2)
27787 return;
27789 switch (die->die_tag)
27791 case DW_TAG_structure_type:
27792 case DW_TAG_union_type:
27793 case DW_TAG_class_type:
27794 if (die->die_perennial_p)
27795 break;
27797 for (c = die->die_parent; c; c = c->die_parent)
27798 if (c->die_tag == DW_TAG_subprogram)
27799 break;
27801 /* Finding used static member functions inside of classes
27802 is needed just for local classes, because for other classes
27803 static member function DIEs with DW_AT_specification
27804 are emitted outside of the DW_TAG_*_type. If we ever change
27805 it, we'd need to call this even for non-local classes. */
27806 if (c)
27807 prune_unused_types_walk_local_classes (die);
27809 /* It's a type node --- don't mark it. */
27810 return;
27812 case DW_TAG_const_type:
27813 case DW_TAG_packed_type:
27814 case DW_TAG_pointer_type:
27815 case DW_TAG_reference_type:
27816 case DW_TAG_rvalue_reference_type:
27817 case DW_TAG_volatile_type:
27818 case DW_TAG_typedef:
27819 case DW_TAG_array_type:
27820 case DW_TAG_interface_type:
27821 case DW_TAG_friend:
27822 case DW_TAG_enumeration_type:
27823 case DW_TAG_subroutine_type:
27824 case DW_TAG_string_type:
27825 case DW_TAG_set_type:
27826 case DW_TAG_subrange_type:
27827 case DW_TAG_ptr_to_member_type:
27828 case DW_TAG_file_type:
27829 /* Type nodes are useful only when other DIEs reference them --- don't
27830 mark them. */
27831 /* FALLTHROUGH */
27833 case DW_TAG_dwarf_procedure:
27834 /* Likewise for DWARF procedures. */
27836 if (die->die_perennial_p)
27837 break;
27839 return;
27841 default:
27842 /* Mark everything else. */
27843 break;
27846 if (die->die_mark == 0)
27848 die->die_mark = 1;
27850 /* Now, mark any dies referenced from here. */
27851 prune_unused_types_walk_attribs (die);
27854 die->die_mark = 2;
27856 /* Mark children. */
27857 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
27860 /* Increment the string counts on strings referred to from DIE's
27861 attributes. */
27863 static void
27864 prune_unused_types_update_strings (dw_die_ref die)
27866 dw_attr_node *a;
27867 unsigned ix;
27869 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
27870 if (AT_class (a) == dw_val_class_str)
27872 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
27873 s->refcount++;
27874 /* Avoid unnecessarily putting strings that are used less than
27875 twice in the hash table. */
27876 if (s->refcount
27877 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
27879 indirect_string_node **slot
27880 = debug_str_hash->find_slot_with_hash (s->str,
27881 htab_hash_string (s->str),
27882 INSERT);
27883 gcc_assert (*slot == NULL);
27884 *slot = s;
27889 /* Mark DIE and its children as removed. */
27891 static void
27892 mark_removed (dw_die_ref die)
27894 dw_die_ref c;
27895 die->removed = true;
27896 FOR_EACH_CHILD (die, c, mark_removed (c));
27899 /* Remove from the tree DIE any dies that aren't marked. */
27901 static void
27902 prune_unused_types_prune (dw_die_ref die)
27904 dw_die_ref c;
27906 gcc_assert (die->die_mark);
27907 prune_unused_types_update_strings (die);
27909 if (! die->die_child)
27910 return;
27912 c = die->die_child;
27913 do {
27914 dw_die_ref prev = c, next;
27915 for (c = c->die_sib; ! c->die_mark; c = next)
27916 if (c == die->die_child)
27918 /* No marked children between 'prev' and the end of the list. */
27919 if (prev == c)
27920 /* No marked children at all. */
27921 die->die_child = NULL;
27922 else
27924 prev->die_sib = c->die_sib;
27925 die->die_child = prev;
27927 c->die_sib = NULL;
27928 mark_removed (c);
27929 return;
27931 else
27933 next = c->die_sib;
27934 c->die_sib = NULL;
27935 mark_removed (c);
27938 if (c != prev->die_sib)
27939 prev->die_sib = c;
27940 prune_unused_types_prune (c);
27941 } while (c != die->die_child);
27944 /* Remove dies representing declarations that we never use. */
27946 static void
27947 prune_unused_types (void)
27949 unsigned int i;
27950 limbo_die_node *node;
27951 comdat_type_node *ctnode;
27952 pubname_entry *pub;
27953 dw_die_ref base_type;
27955 #if ENABLE_ASSERT_CHECKING
27956 /* All the marks should already be clear. */
27957 verify_marks_clear (comp_unit_die ());
27958 for (node = limbo_die_list; node; node = node->next)
27959 verify_marks_clear (node->die);
27960 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
27961 verify_marks_clear (ctnode->root_die);
27962 #endif /* ENABLE_ASSERT_CHECKING */
27964 /* Mark types that are used in global variables. */
27965 premark_types_used_by_global_vars ();
27967 /* Set the mark on nodes that are actually used. */
27968 prune_unused_types_walk (comp_unit_die ());
27969 for (node = limbo_die_list; node; node = node->next)
27970 prune_unused_types_walk (node->die);
27971 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
27973 prune_unused_types_walk (ctnode->root_die);
27974 prune_unused_types_mark (ctnode->type_die, 1);
27977 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
27978 are unusual in that they are pubnames that are the children of pubtypes.
27979 They should only be marked via their parent DW_TAG_enumeration_type die,
27980 not as roots in themselves. */
27981 FOR_EACH_VEC_ELT (*pubname_table, i, pub)
27982 if (pub->die->die_tag != DW_TAG_enumerator)
27983 prune_unused_types_mark (pub->die, 1);
27984 for (i = 0; base_types.iterate (i, &base_type); i++)
27985 prune_unused_types_mark (base_type, 1);
27987 /* For -fvar-tracking-assignments, also set the mark on nodes that could be
27988 referenced by DW_TAG_call_site DW_AT_call_origin (i.e. direct call
27989 callees). */
27990 cgraph_node *cnode;
27991 FOR_EACH_FUNCTION (cnode)
27992 if (cnode->referred_to_p (false))
27994 dw_die_ref die = lookup_decl_die (cnode->decl);
27995 if (die == NULL || die->die_mark)
27996 continue;
27997 for (cgraph_edge *e = cnode->callers; e; e = e->next_caller)
27998 if (e->caller != cnode
27999 && opt_for_fn (e->caller->decl, flag_var_tracking_assignments))
28001 prune_unused_types_mark (die, 1);
28002 break;
28006 if (debug_str_hash)
28007 debug_str_hash->empty ();
28008 if (skeleton_debug_str_hash)
28009 skeleton_debug_str_hash->empty ();
28010 prune_unused_types_prune (comp_unit_die ());
28011 for (limbo_die_node **pnode = &limbo_die_list; *pnode; )
28013 node = *pnode;
28014 if (!node->die->die_mark)
28015 *pnode = node->next;
28016 else
28018 prune_unused_types_prune (node->die);
28019 pnode = &node->next;
28022 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
28023 prune_unused_types_prune (ctnode->root_die);
28025 /* Leave the marks clear. */
28026 prune_unmark_dies (comp_unit_die ());
28027 for (node = limbo_die_list; node; node = node->next)
28028 prune_unmark_dies (node->die);
28029 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
28030 prune_unmark_dies (ctnode->root_die);
28033 /* Helpers to manipulate hash table of comdat type units. */
28035 struct comdat_type_hasher : nofree_ptr_hash <comdat_type_node>
28037 static inline hashval_t hash (const comdat_type_node *);
28038 static inline bool equal (const comdat_type_node *, const comdat_type_node *);
28041 inline hashval_t
28042 comdat_type_hasher::hash (const comdat_type_node *type_node)
28044 hashval_t h;
28045 memcpy (&h, type_node->signature, sizeof (h));
28046 return h;
28049 inline bool
28050 comdat_type_hasher::equal (const comdat_type_node *type_node_1,
28051 const comdat_type_node *type_node_2)
28053 return (! memcmp (type_node_1->signature, type_node_2->signature,
28054 DWARF_TYPE_SIGNATURE_SIZE));
28057 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
28058 to the location it would have been added, should we know its
28059 DECL_ASSEMBLER_NAME when we added other attributes. This will
28060 probably improve compactness of debug info, removing equivalent
28061 abbrevs, and hide any differences caused by deferring the
28062 computation of the assembler name, triggered by e.g. PCH. */
28064 static inline void
28065 move_linkage_attr (dw_die_ref die)
28067 unsigned ix = vec_safe_length (die->die_attr);
28068 dw_attr_node linkage = (*die->die_attr)[ix - 1];
28070 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
28071 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
28073 while (--ix > 0)
28075 dw_attr_node *prev = &(*die->die_attr)[ix - 1];
28077 if (prev->dw_attr == DW_AT_decl_line
28078 || prev->dw_attr == DW_AT_decl_column
28079 || prev->dw_attr == DW_AT_name)
28080 break;
28083 if (ix != vec_safe_length (die->die_attr) - 1)
28085 die->die_attr->pop ();
28086 die->die_attr->quick_insert (ix, linkage);
28090 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
28091 referenced from typed stack ops and count how often they are used. */
28093 static void
28094 mark_base_types (dw_loc_descr_ref loc)
28096 dw_die_ref base_type = NULL;
28098 for (; loc; loc = loc->dw_loc_next)
28100 switch (loc->dw_loc_opc)
28102 case DW_OP_regval_type:
28103 case DW_OP_deref_type:
28104 case DW_OP_GNU_regval_type:
28105 case DW_OP_GNU_deref_type:
28106 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
28107 break;
28108 case DW_OP_convert:
28109 case DW_OP_reinterpret:
28110 case DW_OP_GNU_convert:
28111 case DW_OP_GNU_reinterpret:
28112 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
28113 continue;
28114 /* FALLTHRU */
28115 case DW_OP_const_type:
28116 case DW_OP_GNU_const_type:
28117 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
28118 break;
28119 case DW_OP_entry_value:
28120 case DW_OP_GNU_entry_value:
28121 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
28122 continue;
28123 default:
28124 continue;
28126 gcc_assert (base_type->die_parent == comp_unit_die ());
28127 if (base_type->die_mark)
28128 base_type->die_mark++;
28129 else
28131 base_types.safe_push (base_type);
28132 base_type->die_mark = 1;
28137 /* Comparison function for sorting marked base types. */
28139 static int
28140 base_type_cmp (const void *x, const void *y)
28142 dw_die_ref dx = *(const dw_die_ref *) x;
28143 dw_die_ref dy = *(const dw_die_ref *) y;
28144 unsigned int byte_size1, byte_size2;
28145 unsigned int encoding1, encoding2;
28146 unsigned int align1, align2;
28147 if (dx->die_mark > dy->die_mark)
28148 return -1;
28149 if (dx->die_mark < dy->die_mark)
28150 return 1;
28151 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
28152 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
28153 if (byte_size1 < byte_size2)
28154 return 1;
28155 if (byte_size1 > byte_size2)
28156 return -1;
28157 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
28158 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
28159 if (encoding1 < encoding2)
28160 return 1;
28161 if (encoding1 > encoding2)
28162 return -1;
28163 align1 = get_AT_unsigned (dx, DW_AT_alignment);
28164 align2 = get_AT_unsigned (dy, DW_AT_alignment);
28165 if (align1 < align2)
28166 return 1;
28167 if (align1 > align2)
28168 return -1;
28169 return 0;
28172 /* Move base types marked by mark_base_types as early as possible
28173 in the CU, sorted by decreasing usage count both to make the
28174 uleb128 references as small as possible and to make sure they
28175 will have die_offset already computed by calc_die_sizes when
28176 sizes of typed stack loc ops is computed. */
28178 static void
28179 move_marked_base_types (void)
28181 unsigned int i;
28182 dw_die_ref base_type, die, c;
28184 if (base_types.is_empty ())
28185 return;
28187 /* Sort by decreasing usage count, they will be added again in that
28188 order later on. */
28189 base_types.qsort (base_type_cmp);
28190 die = comp_unit_die ();
28191 c = die->die_child;
28194 dw_die_ref prev = c;
28195 c = c->die_sib;
28196 while (c->die_mark)
28198 remove_child_with_prev (c, prev);
28199 /* As base types got marked, there must be at least
28200 one node other than DW_TAG_base_type. */
28201 gcc_assert (die->die_child != NULL);
28202 c = prev->die_sib;
28205 while (c != die->die_child);
28206 gcc_assert (die->die_child);
28207 c = die->die_child;
28208 for (i = 0; base_types.iterate (i, &base_type); i++)
28210 base_type->die_mark = 0;
28211 base_type->die_sib = c->die_sib;
28212 c->die_sib = base_type;
28213 c = base_type;
28217 /* Helper function for resolve_addr, attempt to resolve
28218 one CONST_STRING, return true if successful. Similarly verify that
28219 SYMBOL_REFs refer to variables emitted in the current CU. */
28221 static bool
28222 resolve_one_addr (rtx *addr)
28224 rtx rtl = *addr;
28226 if (GET_CODE (rtl) == CONST_STRING)
28228 size_t len = strlen (XSTR (rtl, 0)) + 1;
28229 tree t = build_string (len, XSTR (rtl, 0));
28230 tree tlen = size_int (len - 1);
28231 TREE_TYPE (t)
28232 = build_array_type (char_type_node, build_index_type (tlen));
28233 rtl = lookup_constant_def (t);
28234 if (!rtl || !MEM_P (rtl))
28235 return false;
28236 rtl = XEXP (rtl, 0);
28237 if (GET_CODE (rtl) == SYMBOL_REF
28238 && SYMBOL_REF_DECL (rtl)
28239 && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
28240 return false;
28241 vec_safe_push (used_rtx_array, rtl);
28242 *addr = rtl;
28243 return true;
28246 if (GET_CODE (rtl) == SYMBOL_REF
28247 && SYMBOL_REF_DECL (rtl))
28249 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
28251 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
28252 return false;
28254 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
28255 return false;
28258 if (GET_CODE (rtl) == CONST)
28260 subrtx_ptr_iterator::array_type array;
28261 FOR_EACH_SUBRTX_PTR (iter, array, &XEXP (rtl, 0), ALL)
28262 if (!resolve_one_addr (*iter))
28263 return false;
28266 return true;
28269 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
28270 if possible, and create DW_TAG_dwarf_procedure that can be referenced
28271 from DW_OP_implicit_pointer if the string hasn't been seen yet. */
28273 static rtx
28274 string_cst_pool_decl (tree t)
28276 rtx rtl = output_constant_def (t, 1);
28277 unsigned char *array;
28278 dw_loc_descr_ref l;
28279 tree decl;
28280 size_t len;
28281 dw_die_ref ref;
28283 if (!rtl || !MEM_P (rtl))
28284 return NULL_RTX;
28285 rtl = XEXP (rtl, 0);
28286 if (GET_CODE (rtl) != SYMBOL_REF
28287 || SYMBOL_REF_DECL (rtl) == NULL_TREE)
28288 return NULL_RTX;
28290 decl = SYMBOL_REF_DECL (rtl);
28291 if (!lookup_decl_die (decl))
28293 len = TREE_STRING_LENGTH (t);
28294 vec_safe_push (used_rtx_array, rtl);
28295 ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
28296 array = ggc_vec_alloc<unsigned char> (len);
28297 memcpy (array, TREE_STRING_POINTER (t), len);
28298 l = new_loc_descr (DW_OP_implicit_value, len, 0);
28299 l->dw_loc_oprnd2.val_class = dw_val_class_vec;
28300 l->dw_loc_oprnd2.v.val_vec.length = len;
28301 l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
28302 l->dw_loc_oprnd2.v.val_vec.array = array;
28303 add_AT_loc (ref, DW_AT_location, l);
28304 equate_decl_number_to_die (decl, ref);
28306 return rtl;
28309 /* Helper function of resolve_addr_in_expr. LOC is
28310 a DW_OP_addr followed by DW_OP_stack_value, either at the start
28311 of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
28312 resolved. Replace it (both DW_OP_addr and DW_OP_stack_value)
28313 with DW_OP_implicit_pointer if possible
28314 and return true, if unsuccessful, return false. */
28316 static bool
28317 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
28319 rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
28320 HOST_WIDE_INT offset = 0;
28321 dw_die_ref ref = NULL;
28322 tree decl;
28324 if (GET_CODE (rtl) == CONST
28325 && GET_CODE (XEXP (rtl, 0)) == PLUS
28326 && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
28328 offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
28329 rtl = XEXP (XEXP (rtl, 0), 0);
28331 if (GET_CODE (rtl) == CONST_STRING)
28333 size_t len = strlen (XSTR (rtl, 0)) + 1;
28334 tree t = build_string (len, XSTR (rtl, 0));
28335 tree tlen = size_int (len - 1);
28337 TREE_TYPE (t)
28338 = build_array_type (char_type_node, build_index_type (tlen));
28339 rtl = string_cst_pool_decl (t);
28340 if (!rtl)
28341 return false;
28343 if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
28345 decl = SYMBOL_REF_DECL (rtl);
28346 if (VAR_P (decl) && !DECL_EXTERNAL (decl))
28348 ref = lookup_decl_die (decl);
28349 if (ref && (get_AT (ref, DW_AT_location)
28350 || get_AT (ref, DW_AT_const_value)))
28352 loc->dw_loc_opc = dwarf_OP (DW_OP_implicit_pointer);
28353 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
28354 loc->dw_loc_oprnd1.val_entry = NULL;
28355 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
28356 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
28357 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
28358 loc->dw_loc_oprnd2.v.val_int = offset;
28359 return true;
28363 return false;
28366 /* Helper function for resolve_addr, handle one location
28367 expression, return false if at least one CONST_STRING or SYMBOL_REF in
28368 the location list couldn't be resolved. */
28370 static bool
28371 resolve_addr_in_expr (dw_attr_node *a, dw_loc_descr_ref loc)
28373 dw_loc_descr_ref keep = NULL;
28374 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
28375 switch (loc->dw_loc_opc)
28377 case DW_OP_addr:
28378 if (!resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
28380 if ((prev == NULL
28381 || prev->dw_loc_opc == DW_OP_piece
28382 || prev->dw_loc_opc == DW_OP_bit_piece)
28383 && loc->dw_loc_next
28384 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
28385 && (!dwarf_strict || dwarf_version >= 5)
28386 && optimize_one_addr_into_implicit_ptr (loc))
28387 break;
28388 return false;
28390 break;
28391 case DW_OP_GNU_addr_index:
28392 case DW_OP_GNU_const_index:
28393 if (loc->dw_loc_opc == DW_OP_GNU_addr_index
28394 || (loc->dw_loc_opc == DW_OP_GNU_const_index && loc->dtprel))
28396 rtx rtl = loc->dw_loc_oprnd1.val_entry->addr.rtl;
28397 if (!resolve_one_addr (&rtl))
28398 return false;
28399 remove_addr_table_entry (loc->dw_loc_oprnd1.val_entry);
28400 loc->dw_loc_oprnd1.val_entry
28401 = add_addr_table_entry (rtl, ate_kind_rtx);
28403 break;
28404 case DW_OP_const4u:
28405 case DW_OP_const8u:
28406 if (loc->dtprel
28407 && !resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
28408 return false;
28409 break;
28410 case DW_OP_plus_uconst:
28411 if (size_of_loc_descr (loc)
28412 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
28414 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
28416 dw_loc_descr_ref repl
28417 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
28418 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
28419 add_loc_descr (&repl, loc->dw_loc_next);
28420 *loc = *repl;
28422 break;
28423 case DW_OP_implicit_value:
28424 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
28425 && !resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr))
28426 return false;
28427 break;
28428 case DW_OP_implicit_pointer:
28429 case DW_OP_GNU_implicit_pointer:
28430 case DW_OP_GNU_parameter_ref:
28431 case DW_OP_GNU_variable_value:
28432 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
28434 dw_die_ref ref
28435 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
28436 if (ref == NULL)
28437 return false;
28438 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
28439 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
28440 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
28442 if (loc->dw_loc_opc == DW_OP_GNU_variable_value)
28444 if (prev == NULL
28445 && loc->dw_loc_next == NULL
28446 && AT_class (a) == dw_val_class_loc)
28447 switch (a->dw_attr)
28449 /* Following attributes allow both exprloc and reference,
28450 so if the whole expression is DW_OP_GNU_variable_value
28451 alone we could transform it into reference. */
28452 case DW_AT_byte_size:
28453 case DW_AT_bit_size:
28454 case DW_AT_lower_bound:
28455 case DW_AT_upper_bound:
28456 case DW_AT_bit_stride:
28457 case DW_AT_count:
28458 case DW_AT_allocated:
28459 case DW_AT_associated:
28460 case DW_AT_byte_stride:
28461 a->dw_attr_val.val_class = dw_val_class_die_ref;
28462 a->dw_attr_val.val_entry = NULL;
28463 a->dw_attr_val.v.val_die_ref.die
28464 = loc->dw_loc_oprnd1.v.val_die_ref.die;
28465 a->dw_attr_val.v.val_die_ref.external = 0;
28466 return true;
28467 default:
28468 break;
28470 if (dwarf_strict)
28471 return false;
28473 break;
28474 case DW_OP_const_type:
28475 case DW_OP_regval_type:
28476 case DW_OP_deref_type:
28477 case DW_OP_convert:
28478 case DW_OP_reinterpret:
28479 case DW_OP_GNU_const_type:
28480 case DW_OP_GNU_regval_type:
28481 case DW_OP_GNU_deref_type:
28482 case DW_OP_GNU_convert:
28483 case DW_OP_GNU_reinterpret:
28484 while (loc->dw_loc_next
28485 && (loc->dw_loc_next->dw_loc_opc == DW_OP_convert
28486 || loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert))
28488 dw_die_ref base1, base2;
28489 unsigned enc1, enc2, size1, size2;
28490 if (loc->dw_loc_opc == DW_OP_regval_type
28491 || loc->dw_loc_opc == DW_OP_deref_type
28492 || loc->dw_loc_opc == DW_OP_GNU_regval_type
28493 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
28494 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
28495 else if (loc->dw_loc_oprnd1.val_class
28496 == dw_val_class_unsigned_const)
28497 break;
28498 else
28499 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
28500 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
28501 == dw_val_class_unsigned_const)
28502 break;
28503 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
28504 gcc_assert (base1->die_tag == DW_TAG_base_type
28505 && base2->die_tag == DW_TAG_base_type);
28506 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
28507 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
28508 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
28509 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
28510 if (size1 == size2
28511 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
28512 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
28513 && loc != keep)
28514 || enc1 == enc2))
28516 /* Optimize away next DW_OP_convert after
28517 adjusting LOC's base type die reference. */
28518 if (loc->dw_loc_opc == DW_OP_regval_type
28519 || loc->dw_loc_opc == DW_OP_deref_type
28520 || loc->dw_loc_opc == DW_OP_GNU_regval_type
28521 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
28522 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
28523 else
28524 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
28525 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
28526 continue;
28528 /* Don't change integer DW_OP_convert after e.g. floating
28529 point typed stack entry. */
28530 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
28531 keep = loc->dw_loc_next;
28532 break;
28534 break;
28535 default:
28536 break;
28538 return true;
28541 /* Helper function of resolve_addr. DIE had DW_AT_location of
28542 DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
28543 and DW_OP_addr couldn't be resolved. resolve_addr has already
28544 removed the DW_AT_location attribute. This function attempts to
28545 add a new DW_AT_location attribute with DW_OP_implicit_pointer
28546 to it or DW_AT_const_value attribute, if possible. */
28548 static void
28549 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
28551 if (!VAR_P (decl)
28552 || lookup_decl_die (decl) != die
28553 || DECL_EXTERNAL (decl)
28554 || !TREE_STATIC (decl)
28555 || DECL_INITIAL (decl) == NULL_TREE
28556 || DECL_P (DECL_INITIAL (decl))
28557 || get_AT (die, DW_AT_const_value))
28558 return;
28560 tree init = DECL_INITIAL (decl);
28561 HOST_WIDE_INT offset = 0;
28562 /* For variables that have been optimized away and thus
28563 don't have a memory location, see if we can emit
28564 DW_AT_const_value instead. */
28565 if (tree_add_const_value_attribute (die, init))
28566 return;
28567 if (dwarf_strict && dwarf_version < 5)
28568 return;
28569 /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
28570 and ADDR_EXPR refers to a decl that has DW_AT_location or
28571 DW_AT_const_value (but isn't addressable, otherwise
28572 resolving the original DW_OP_addr wouldn't fail), see if
28573 we can add DW_OP_implicit_pointer. */
28574 STRIP_NOPS (init);
28575 if (TREE_CODE (init) == POINTER_PLUS_EXPR
28576 && tree_fits_shwi_p (TREE_OPERAND (init, 1)))
28578 offset = tree_to_shwi (TREE_OPERAND (init, 1));
28579 init = TREE_OPERAND (init, 0);
28580 STRIP_NOPS (init);
28582 if (TREE_CODE (init) != ADDR_EXPR)
28583 return;
28584 if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
28585 && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
28586 || (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL
28587 && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
28588 && TREE_OPERAND (init, 0) != decl))
28590 dw_die_ref ref;
28591 dw_loc_descr_ref l;
28593 if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
28595 rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
28596 if (!rtl)
28597 return;
28598 decl = SYMBOL_REF_DECL (rtl);
28600 else
28601 decl = TREE_OPERAND (init, 0);
28602 ref = lookup_decl_die (decl);
28603 if (ref == NULL
28604 || (!get_AT (ref, DW_AT_location)
28605 && !get_AT (ref, DW_AT_const_value)))
28606 return;
28607 l = new_loc_descr (dwarf_OP (DW_OP_implicit_pointer), 0, offset);
28608 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
28609 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
28610 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
28611 add_AT_loc (die, DW_AT_location, l);
28615 /* Return NULL if l is a DWARF expression, or first op that is not
28616 valid DWARF expression. */
28618 static dw_loc_descr_ref
28619 non_dwarf_expression (dw_loc_descr_ref l)
28621 while (l)
28623 if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
28624 return l;
28625 switch (l->dw_loc_opc)
28627 case DW_OP_regx:
28628 case DW_OP_implicit_value:
28629 case DW_OP_stack_value:
28630 case DW_OP_implicit_pointer:
28631 case DW_OP_GNU_implicit_pointer:
28632 case DW_OP_GNU_parameter_ref:
28633 case DW_OP_piece:
28634 case DW_OP_bit_piece:
28635 return l;
28636 default:
28637 break;
28639 l = l->dw_loc_next;
28641 return NULL;
28644 /* Return adjusted copy of EXPR:
28645 If it is empty DWARF expression, return it.
28646 If it is valid non-empty DWARF expression,
28647 return copy of EXPR with DW_OP_deref appended to it.
28648 If it is DWARF expression followed by DW_OP_reg{N,x}, return
28649 copy of the DWARF expression with DW_OP_breg{N,x} <0> appended.
28650 If it is DWARF expression followed by DW_OP_stack_value, return
28651 copy of the DWARF expression without anything appended.
28652 Otherwise, return NULL. */
28654 static dw_loc_descr_ref
28655 copy_deref_exprloc (dw_loc_descr_ref expr)
28657 dw_loc_descr_ref tail = NULL;
28659 if (expr == NULL)
28660 return NULL;
28662 dw_loc_descr_ref l = non_dwarf_expression (expr);
28663 if (l && l->dw_loc_next)
28664 return NULL;
28666 if (l)
28668 if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
28669 tail = new_loc_descr ((enum dwarf_location_atom)
28670 (DW_OP_breg0 + (l->dw_loc_opc - DW_OP_reg0)),
28671 0, 0);
28672 else
28673 switch (l->dw_loc_opc)
28675 case DW_OP_regx:
28676 tail = new_loc_descr (DW_OP_bregx,
28677 l->dw_loc_oprnd1.v.val_unsigned, 0);
28678 break;
28679 case DW_OP_stack_value:
28680 break;
28681 default:
28682 return NULL;
28685 else
28686 tail = new_loc_descr (DW_OP_deref, 0, 0);
28688 dw_loc_descr_ref ret = NULL, *p = &ret;
28689 while (expr != l)
28691 *p = new_loc_descr (expr->dw_loc_opc, 0, 0);
28692 (*p)->dw_loc_oprnd1 = expr->dw_loc_oprnd1;
28693 (*p)->dw_loc_oprnd2 = expr->dw_loc_oprnd2;
28694 p = &(*p)->dw_loc_next;
28695 expr = expr->dw_loc_next;
28697 *p = tail;
28698 return ret;
28701 /* For DW_AT_string_length attribute with DW_OP_GNU_variable_value
28702 reference to a variable or argument, adjust it if needed and return:
28703 -1 if the DW_AT_string_length attribute and DW_AT_{string_length_,}byte_size
28704 attribute if present should be removed
28705 0 keep the attribute perhaps with minor modifications, no need to rescan
28706 1 if the attribute has been successfully adjusted. */
28708 static int
28709 optimize_string_length (dw_attr_node *a)
28711 dw_loc_descr_ref l = AT_loc (a), lv;
28712 dw_die_ref die;
28713 if (l->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
28715 tree decl = l->dw_loc_oprnd1.v.val_decl_ref;
28716 die = lookup_decl_die (decl);
28717 if (die)
28719 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
28720 l->dw_loc_oprnd1.v.val_die_ref.die = die;
28721 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
28723 else
28724 return -1;
28726 else
28727 die = l->dw_loc_oprnd1.v.val_die_ref.die;
28729 /* DWARF5 allows reference class, so we can then reference the DIE.
28730 Only do this for DW_OP_GNU_variable_value DW_OP_stack_value. */
28731 if (l->dw_loc_next != NULL && dwarf_version >= 5)
28733 a->dw_attr_val.val_class = dw_val_class_die_ref;
28734 a->dw_attr_val.val_entry = NULL;
28735 a->dw_attr_val.v.val_die_ref.die = die;
28736 a->dw_attr_val.v.val_die_ref.external = 0;
28737 return 0;
28740 dw_attr_node *av = get_AT (die, DW_AT_location);
28741 dw_loc_list_ref d;
28742 bool non_dwarf_expr = false;
28744 if (av == NULL)
28745 return dwarf_strict ? -1 : 0;
28746 switch (AT_class (av))
28748 case dw_val_class_loc_list:
28749 for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
28750 if (d->expr && non_dwarf_expression (d->expr))
28751 non_dwarf_expr = true;
28752 break;
28753 case dw_val_class_loc:
28754 lv = AT_loc (av);
28755 if (lv == NULL)
28756 return dwarf_strict ? -1 : 0;
28757 if (non_dwarf_expression (lv))
28758 non_dwarf_expr = true;
28759 break;
28760 default:
28761 return dwarf_strict ? -1 : 0;
28764 /* If it is safe to transform DW_OP_GNU_variable_value DW_OP_stack_value
28765 into DW_OP_call4 or DW_OP_GNU_variable_value into
28766 DW_OP_call4 DW_OP_deref, do so. */
28767 if (!non_dwarf_expr
28768 && (l->dw_loc_next != NULL || AT_class (av) == dw_val_class_loc))
28770 l->dw_loc_opc = DW_OP_call4;
28771 if (l->dw_loc_next)
28772 l->dw_loc_next = NULL;
28773 else
28774 l->dw_loc_next = new_loc_descr (DW_OP_deref, 0, 0);
28775 return 0;
28778 /* For DW_OP_GNU_variable_value DW_OP_stack_value, we can just
28779 copy over the DW_AT_location attribute from die to a. */
28780 if (l->dw_loc_next != NULL)
28782 a->dw_attr_val = av->dw_attr_val;
28783 return 1;
28786 dw_loc_list_ref list, *p;
28787 switch (AT_class (av))
28789 case dw_val_class_loc_list:
28790 p = &list;
28791 list = NULL;
28792 for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
28794 lv = copy_deref_exprloc (d->expr);
28795 if (lv)
28797 *p = new_loc_list (lv, d->begin, d->end, d->section);
28798 p = &(*p)->dw_loc_next;
28800 else if (!dwarf_strict && d->expr)
28801 return 0;
28803 if (list == NULL)
28804 return dwarf_strict ? -1 : 0;
28805 a->dw_attr_val.val_class = dw_val_class_loc_list;
28806 gen_llsym (list);
28807 *AT_loc_list_ptr (a) = list;
28808 return 1;
28809 case dw_val_class_loc:
28810 lv = copy_deref_exprloc (AT_loc (av));
28811 if (lv == NULL)
28812 return dwarf_strict ? -1 : 0;
28813 a->dw_attr_val.v.val_loc = lv;
28814 return 1;
28815 default:
28816 gcc_unreachable ();
28820 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
28821 an address in .rodata section if the string literal is emitted there,
28822 or remove the containing location list or replace DW_AT_const_value
28823 with DW_AT_location and empty location expression, if it isn't found
28824 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
28825 to something that has been emitted in the current CU. */
28827 static void
28828 resolve_addr (dw_die_ref die)
28830 dw_die_ref c;
28831 dw_attr_node *a;
28832 dw_loc_list_ref *curr, *start, loc;
28833 unsigned ix;
28834 bool remove_AT_byte_size = false;
28836 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
28837 switch (AT_class (a))
28839 case dw_val_class_loc_list:
28840 start = curr = AT_loc_list_ptr (a);
28841 loc = *curr;
28842 gcc_assert (loc);
28843 /* The same list can be referenced more than once. See if we have
28844 already recorded the result from a previous pass. */
28845 if (loc->replaced)
28846 *curr = loc->dw_loc_next;
28847 else if (!loc->resolved_addr)
28849 /* As things stand, we do not expect or allow one die to
28850 reference a suffix of another die's location list chain.
28851 References must be identical or completely separate.
28852 There is therefore no need to cache the result of this
28853 pass on any list other than the first; doing so
28854 would lead to unnecessary writes. */
28855 while (*curr)
28857 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
28858 if (!resolve_addr_in_expr (a, (*curr)->expr))
28860 dw_loc_list_ref next = (*curr)->dw_loc_next;
28861 dw_loc_descr_ref l = (*curr)->expr;
28863 if (next && (*curr)->ll_symbol)
28865 gcc_assert (!next->ll_symbol);
28866 next->ll_symbol = (*curr)->ll_symbol;
28868 if (dwarf_split_debug_info)
28869 remove_loc_list_addr_table_entries (l);
28870 *curr = next;
28872 else
28874 mark_base_types ((*curr)->expr);
28875 curr = &(*curr)->dw_loc_next;
28878 if (loc == *start)
28879 loc->resolved_addr = 1;
28880 else
28882 loc->replaced = 1;
28883 loc->dw_loc_next = *start;
28886 if (!*start)
28888 remove_AT (die, a->dw_attr);
28889 ix--;
28891 break;
28892 case dw_val_class_loc:
28894 dw_loc_descr_ref l = AT_loc (a);
28895 /* DW_OP_GNU_variable_value DW_OP_stack_value or
28896 DW_OP_GNU_variable_value in DW_AT_string_length can be converted
28897 into DW_OP_call4 or DW_OP_call4 DW_OP_deref, which is standard
28898 DWARF4 unlike DW_OP_GNU_variable_value. Or for DWARF5
28899 DW_OP_GNU_variable_value DW_OP_stack_value can be replaced
28900 with DW_FORM_ref referencing the same DIE as
28901 DW_OP_GNU_variable_value used to reference. */
28902 if (a->dw_attr == DW_AT_string_length
28903 && l
28904 && l->dw_loc_opc == DW_OP_GNU_variable_value
28905 && (l->dw_loc_next == NULL
28906 || (l->dw_loc_next->dw_loc_next == NULL
28907 && l->dw_loc_next->dw_loc_opc == DW_OP_stack_value)))
28909 switch (optimize_string_length (a))
28911 case -1:
28912 remove_AT (die, a->dw_attr);
28913 ix--;
28914 /* If we drop DW_AT_string_length, we need to drop also
28915 DW_AT_{string_length_,}byte_size. */
28916 remove_AT_byte_size = true;
28917 continue;
28918 default:
28919 break;
28920 case 1:
28921 /* Even if we keep the optimized DW_AT_string_length,
28922 it might have changed AT_class, so process it again. */
28923 ix--;
28924 continue;
28927 /* For -gdwarf-2 don't attempt to optimize
28928 DW_AT_data_member_location containing
28929 DW_OP_plus_uconst - older consumers might
28930 rely on it being that op instead of a more complex,
28931 but shorter, location description. */
28932 if ((dwarf_version > 2
28933 || a->dw_attr != DW_AT_data_member_location
28934 || l == NULL
28935 || l->dw_loc_opc != DW_OP_plus_uconst
28936 || l->dw_loc_next != NULL)
28937 && !resolve_addr_in_expr (a, l))
28939 if (dwarf_split_debug_info)
28940 remove_loc_list_addr_table_entries (l);
28941 if (l != NULL
28942 && l->dw_loc_next == NULL
28943 && l->dw_loc_opc == DW_OP_addr
28944 && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
28945 && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
28946 && a->dw_attr == DW_AT_location)
28948 tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
28949 remove_AT (die, a->dw_attr);
28950 ix--;
28951 optimize_location_into_implicit_ptr (die, decl);
28952 break;
28954 if (a->dw_attr == DW_AT_string_length)
28955 /* If we drop DW_AT_string_length, we need to drop also
28956 DW_AT_{string_length_,}byte_size. */
28957 remove_AT_byte_size = true;
28958 remove_AT (die, a->dw_attr);
28959 ix--;
28961 else
28962 mark_base_types (l);
28964 break;
28965 case dw_val_class_addr:
28966 if (a->dw_attr == DW_AT_const_value
28967 && !resolve_one_addr (&a->dw_attr_val.v.val_addr))
28969 if (AT_index (a) != NOT_INDEXED)
28970 remove_addr_table_entry (a->dw_attr_val.val_entry);
28971 remove_AT (die, a->dw_attr);
28972 ix--;
28974 if ((die->die_tag == DW_TAG_call_site
28975 && a->dw_attr == DW_AT_call_origin)
28976 || (die->die_tag == DW_TAG_GNU_call_site
28977 && a->dw_attr == DW_AT_abstract_origin))
28979 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
28980 dw_die_ref tdie = lookup_decl_die (tdecl);
28981 dw_die_ref cdie;
28982 if (tdie == NULL
28983 && DECL_EXTERNAL (tdecl)
28984 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE
28985 && (cdie = lookup_context_die (DECL_CONTEXT (tdecl))))
28987 dw_die_ref pdie = cdie;
28988 /* Make sure we don't add these DIEs into type units.
28989 We could emit skeleton DIEs for context (namespaces,
28990 outer structs/classes) and a skeleton DIE for the
28991 innermost context with DW_AT_signature pointing to the
28992 type unit. See PR78835. */
28993 while (pdie && pdie->die_tag != DW_TAG_type_unit)
28994 pdie = pdie->die_parent;
28995 if (pdie == NULL)
28997 /* Creating a full DIE for tdecl is overly expensive and
28998 at this point even wrong when in the LTO phase
28999 as it can end up generating new type DIEs we didn't
29000 output and thus optimize_external_refs will crash. */
29001 tdie = new_die (DW_TAG_subprogram, cdie, NULL_TREE);
29002 add_AT_flag (tdie, DW_AT_external, 1);
29003 add_AT_flag (tdie, DW_AT_declaration, 1);
29004 add_linkage_attr (tdie, tdecl);
29005 add_name_and_src_coords_attributes (tdie, tdecl);
29006 equate_decl_number_to_die (tdecl, tdie);
29009 if (tdie)
29011 a->dw_attr_val.val_class = dw_val_class_die_ref;
29012 a->dw_attr_val.v.val_die_ref.die = tdie;
29013 a->dw_attr_val.v.val_die_ref.external = 0;
29015 else
29017 if (AT_index (a) != NOT_INDEXED)
29018 remove_addr_table_entry (a->dw_attr_val.val_entry);
29019 remove_AT (die, a->dw_attr);
29020 ix--;
29023 break;
29024 default:
29025 break;
29028 if (remove_AT_byte_size)
29029 remove_AT (die, dwarf_version >= 5
29030 ? DW_AT_string_length_byte_size
29031 : DW_AT_byte_size);
29033 FOR_EACH_CHILD (die, c, resolve_addr (c));
29036 /* Helper routines for optimize_location_lists.
29037 This pass tries to share identical local lists in .debug_loc
29038 section. */
29040 /* Iteratively hash operands of LOC opcode into HSTATE. */
29042 static void
29043 hash_loc_operands (dw_loc_descr_ref loc, inchash::hash &hstate)
29045 dw_val_ref val1 = &loc->dw_loc_oprnd1;
29046 dw_val_ref val2 = &loc->dw_loc_oprnd2;
29048 switch (loc->dw_loc_opc)
29050 case DW_OP_const4u:
29051 case DW_OP_const8u:
29052 if (loc->dtprel)
29053 goto hash_addr;
29054 /* FALLTHRU */
29055 case DW_OP_const1u:
29056 case DW_OP_const1s:
29057 case DW_OP_const2u:
29058 case DW_OP_const2s:
29059 case DW_OP_const4s:
29060 case DW_OP_const8s:
29061 case DW_OP_constu:
29062 case DW_OP_consts:
29063 case DW_OP_pick:
29064 case DW_OP_plus_uconst:
29065 case DW_OP_breg0:
29066 case DW_OP_breg1:
29067 case DW_OP_breg2:
29068 case DW_OP_breg3:
29069 case DW_OP_breg4:
29070 case DW_OP_breg5:
29071 case DW_OP_breg6:
29072 case DW_OP_breg7:
29073 case DW_OP_breg8:
29074 case DW_OP_breg9:
29075 case DW_OP_breg10:
29076 case DW_OP_breg11:
29077 case DW_OP_breg12:
29078 case DW_OP_breg13:
29079 case DW_OP_breg14:
29080 case DW_OP_breg15:
29081 case DW_OP_breg16:
29082 case DW_OP_breg17:
29083 case DW_OP_breg18:
29084 case DW_OP_breg19:
29085 case DW_OP_breg20:
29086 case DW_OP_breg21:
29087 case DW_OP_breg22:
29088 case DW_OP_breg23:
29089 case DW_OP_breg24:
29090 case DW_OP_breg25:
29091 case DW_OP_breg26:
29092 case DW_OP_breg27:
29093 case DW_OP_breg28:
29094 case DW_OP_breg29:
29095 case DW_OP_breg30:
29096 case DW_OP_breg31:
29097 case DW_OP_regx:
29098 case DW_OP_fbreg:
29099 case DW_OP_piece:
29100 case DW_OP_deref_size:
29101 case DW_OP_xderef_size:
29102 hstate.add_object (val1->v.val_int);
29103 break;
29104 case DW_OP_skip:
29105 case DW_OP_bra:
29107 int offset;
29109 gcc_assert (val1->val_class == dw_val_class_loc);
29110 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
29111 hstate.add_object (offset);
29113 break;
29114 case DW_OP_implicit_value:
29115 hstate.add_object (val1->v.val_unsigned);
29116 switch (val2->val_class)
29118 case dw_val_class_const:
29119 hstate.add_object (val2->v.val_int);
29120 break;
29121 case dw_val_class_vec:
29123 unsigned int elt_size = val2->v.val_vec.elt_size;
29124 unsigned int len = val2->v.val_vec.length;
29126 hstate.add_int (elt_size);
29127 hstate.add_int (len);
29128 hstate.add (val2->v.val_vec.array, len * elt_size);
29130 break;
29131 case dw_val_class_const_double:
29132 hstate.add_object (val2->v.val_double.low);
29133 hstate.add_object (val2->v.val_double.high);
29134 break;
29135 case dw_val_class_wide_int:
29136 hstate.add (val2->v.val_wide->get_val (),
29137 get_full_len (*val2->v.val_wide)
29138 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
29139 break;
29140 case dw_val_class_addr:
29141 inchash::add_rtx (val2->v.val_addr, hstate);
29142 break;
29143 default:
29144 gcc_unreachable ();
29146 break;
29147 case DW_OP_bregx:
29148 case DW_OP_bit_piece:
29149 hstate.add_object (val1->v.val_int);
29150 hstate.add_object (val2->v.val_int);
29151 break;
29152 case DW_OP_addr:
29153 hash_addr:
29154 if (loc->dtprel)
29156 unsigned char dtprel = 0xd1;
29157 hstate.add_object (dtprel);
29159 inchash::add_rtx (val1->v.val_addr, hstate);
29160 break;
29161 case DW_OP_GNU_addr_index:
29162 case DW_OP_GNU_const_index:
29164 if (loc->dtprel)
29166 unsigned char dtprel = 0xd1;
29167 hstate.add_object (dtprel);
29169 inchash::add_rtx (val1->val_entry->addr.rtl, hstate);
29171 break;
29172 case DW_OP_implicit_pointer:
29173 case DW_OP_GNU_implicit_pointer:
29174 hstate.add_int (val2->v.val_int);
29175 break;
29176 case DW_OP_entry_value:
29177 case DW_OP_GNU_entry_value:
29178 hstate.add_object (val1->v.val_loc);
29179 break;
29180 case DW_OP_regval_type:
29181 case DW_OP_deref_type:
29182 case DW_OP_GNU_regval_type:
29183 case DW_OP_GNU_deref_type:
29185 unsigned int byte_size
29186 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
29187 unsigned int encoding
29188 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
29189 hstate.add_object (val1->v.val_int);
29190 hstate.add_object (byte_size);
29191 hstate.add_object (encoding);
29193 break;
29194 case DW_OP_convert:
29195 case DW_OP_reinterpret:
29196 case DW_OP_GNU_convert:
29197 case DW_OP_GNU_reinterpret:
29198 if (val1->val_class == dw_val_class_unsigned_const)
29200 hstate.add_object (val1->v.val_unsigned);
29201 break;
29203 /* FALLTHRU */
29204 case DW_OP_const_type:
29205 case DW_OP_GNU_const_type:
29207 unsigned int byte_size
29208 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
29209 unsigned int encoding
29210 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
29211 hstate.add_object (byte_size);
29212 hstate.add_object (encoding);
29213 if (loc->dw_loc_opc != DW_OP_const_type
29214 && loc->dw_loc_opc != DW_OP_GNU_const_type)
29215 break;
29216 hstate.add_object (val2->val_class);
29217 switch (val2->val_class)
29219 case dw_val_class_const:
29220 hstate.add_object (val2->v.val_int);
29221 break;
29222 case dw_val_class_vec:
29224 unsigned int elt_size = val2->v.val_vec.elt_size;
29225 unsigned int len = val2->v.val_vec.length;
29227 hstate.add_object (elt_size);
29228 hstate.add_object (len);
29229 hstate.add (val2->v.val_vec.array, len * elt_size);
29231 break;
29232 case dw_val_class_const_double:
29233 hstate.add_object (val2->v.val_double.low);
29234 hstate.add_object (val2->v.val_double.high);
29235 break;
29236 case dw_val_class_wide_int:
29237 hstate.add (val2->v.val_wide->get_val (),
29238 get_full_len (*val2->v.val_wide)
29239 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
29240 break;
29241 default:
29242 gcc_unreachable ();
29245 break;
29247 default:
29248 /* Other codes have no operands. */
29249 break;
29253 /* Iteratively hash the whole DWARF location expression LOC into HSTATE. */
29255 static inline void
29256 hash_locs (dw_loc_descr_ref loc, inchash::hash &hstate)
29258 dw_loc_descr_ref l;
29259 bool sizes_computed = false;
29260 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
29261 size_of_locs (loc);
29263 for (l = loc; l != NULL; l = l->dw_loc_next)
29265 enum dwarf_location_atom opc = l->dw_loc_opc;
29266 hstate.add_object (opc);
29267 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
29269 size_of_locs (loc);
29270 sizes_computed = true;
29272 hash_loc_operands (l, hstate);
29276 /* Compute hash of the whole location list LIST_HEAD. */
29278 static inline void
29279 hash_loc_list (dw_loc_list_ref list_head)
29281 dw_loc_list_ref curr = list_head;
29282 inchash::hash hstate;
29284 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
29286 hstate.add (curr->begin, strlen (curr->begin) + 1);
29287 hstate.add (curr->end, strlen (curr->end) + 1);
29288 if (curr->section)
29289 hstate.add (curr->section, strlen (curr->section) + 1);
29290 hash_locs (curr->expr, hstate);
29292 list_head->hash = hstate.end ();
29295 /* Return true if X and Y opcodes have the same operands. */
29297 static inline bool
29298 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
29300 dw_val_ref valx1 = &x->dw_loc_oprnd1;
29301 dw_val_ref valx2 = &x->dw_loc_oprnd2;
29302 dw_val_ref valy1 = &y->dw_loc_oprnd1;
29303 dw_val_ref valy2 = &y->dw_loc_oprnd2;
29305 switch (x->dw_loc_opc)
29307 case DW_OP_const4u:
29308 case DW_OP_const8u:
29309 if (x->dtprel)
29310 goto hash_addr;
29311 /* FALLTHRU */
29312 case DW_OP_const1u:
29313 case DW_OP_const1s:
29314 case DW_OP_const2u:
29315 case DW_OP_const2s:
29316 case DW_OP_const4s:
29317 case DW_OP_const8s:
29318 case DW_OP_constu:
29319 case DW_OP_consts:
29320 case DW_OP_pick:
29321 case DW_OP_plus_uconst:
29322 case DW_OP_breg0:
29323 case DW_OP_breg1:
29324 case DW_OP_breg2:
29325 case DW_OP_breg3:
29326 case DW_OP_breg4:
29327 case DW_OP_breg5:
29328 case DW_OP_breg6:
29329 case DW_OP_breg7:
29330 case DW_OP_breg8:
29331 case DW_OP_breg9:
29332 case DW_OP_breg10:
29333 case DW_OP_breg11:
29334 case DW_OP_breg12:
29335 case DW_OP_breg13:
29336 case DW_OP_breg14:
29337 case DW_OP_breg15:
29338 case DW_OP_breg16:
29339 case DW_OP_breg17:
29340 case DW_OP_breg18:
29341 case DW_OP_breg19:
29342 case DW_OP_breg20:
29343 case DW_OP_breg21:
29344 case DW_OP_breg22:
29345 case DW_OP_breg23:
29346 case DW_OP_breg24:
29347 case DW_OP_breg25:
29348 case DW_OP_breg26:
29349 case DW_OP_breg27:
29350 case DW_OP_breg28:
29351 case DW_OP_breg29:
29352 case DW_OP_breg30:
29353 case DW_OP_breg31:
29354 case DW_OP_regx:
29355 case DW_OP_fbreg:
29356 case DW_OP_piece:
29357 case DW_OP_deref_size:
29358 case DW_OP_xderef_size:
29359 return valx1->v.val_int == valy1->v.val_int;
29360 case DW_OP_skip:
29361 case DW_OP_bra:
29362 /* If splitting debug info, the use of DW_OP_GNU_addr_index
29363 can cause irrelevant differences in dw_loc_addr. */
29364 gcc_assert (valx1->val_class == dw_val_class_loc
29365 && valy1->val_class == dw_val_class_loc
29366 && (dwarf_split_debug_info
29367 || x->dw_loc_addr == y->dw_loc_addr));
29368 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
29369 case DW_OP_implicit_value:
29370 if (valx1->v.val_unsigned != valy1->v.val_unsigned
29371 || valx2->val_class != valy2->val_class)
29372 return false;
29373 switch (valx2->val_class)
29375 case dw_val_class_const:
29376 return valx2->v.val_int == valy2->v.val_int;
29377 case dw_val_class_vec:
29378 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
29379 && valx2->v.val_vec.length == valy2->v.val_vec.length
29380 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
29381 valx2->v.val_vec.elt_size
29382 * valx2->v.val_vec.length) == 0;
29383 case dw_val_class_const_double:
29384 return valx2->v.val_double.low == valy2->v.val_double.low
29385 && valx2->v.val_double.high == valy2->v.val_double.high;
29386 case dw_val_class_wide_int:
29387 return *valx2->v.val_wide == *valy2->v.val_wide;
29388 case dw_val_class_addr:
29389 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
29390 default:
29391 gcc_unreachable ();
29393 case DW_OP_bregx:
29394 case DW_OP_bit_piece:
29395 return valx1->v.val_int == valy1->v.val_int
29396 && valx2->v.val_int == valy2->v.val_int;
29397 case DW_OP_addr:
29398 hash_addr:
29399 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
29400 case DW_OP_GNU_addr_index:
29401 case DW_OP_GNU_const_index:
29403 rtx ax1 = valx1->val_entry->addr.rtl;
29404 rtx ay1 = valy1->val_entry->addr.rtl;
29405 return rtx_equal_p (ax1, ay1);
29407 case DW_OP_implicit_pointer:
29408 case DW_OP_GNU_implicit_pointer:
29409 return valx1->val_class == dw_val_class_die_ref
29410 && valx1->val_class == valy1->val_class
29411 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
29412 && valx2->v.val_int == valy2->v.val_int;
29413 case DW_OP_entry_value:
29414 case DW_OP_GNU_entry_value:
29415 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
29416 case DW_OP_const_type:
29417 case DW_OP_GNU_const_type:
29418 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
29419 || valx2->val_class != valy2->val_class)
29420 return false;
29421 switch (valx2->val_class)
29423 case dw_val_class_const:
29424 return valx2->v.val_int == valy2->v.val_int;
29425 case dw_val_class_vec:
29426 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
29427 && valx2->v.val_vec.length == valy2->v.val_vec.length
29428 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
29429 valx2->v.val_vec.elt_size
29430 * valx2->v.val_vec.length) == 0;
29431 case dw_val_class_const_double:
29432 return valx2->v.val_double.low == valy2->v.val_double.low
29433 && valx2->v.val_double.high == valy2->v.val_double.high;
29434 case dw_val_class_wide_int:
29435 return *valx2->v.val_wide == *valy2->v.val_wide;
29436 default:
29437 gcc_unreachable ();
29439 case DW_OP_regval_type:
29440 case DW_OP_deref_type:
29441 case DW_OP_GNU_regval_type:
29442 case DW_OP_GNU_deref_type:
29443 return valx1->v.val_int == valy1->v.val_int
29444 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
29445 case DW_OP_convert:
29446 case DW_OP_reinterpret:
29447 case DW_OP_GNU_convert:
29448 case DW_OP_GNU_reinterpret:
29449 if (valx1->val_class != valy1->val_class)
29450 return false;
29451 if (valx1->val_class == dw_val_class_unsigned_const)
29452 return valx1->v.val_unsigned == valy1->v.val_unsigned;
29453 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
29454 case DW_OP_GNU_parameter_ref:
29455 return valx1->val_class == dw_val_class_die_ref
29456 && valx1->val_class == valy1->val_class
29457 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
29458 default:
29459 /* Other codes have no operands. */
29460 return true;
29464 /* Return true if DWARF location expressions X and Y are the same. */
29466 static inline bool
29467 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
29469 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
29470 if (x->dw_loc_opc != y->dw_loc_opc
29471 || x->dtprel != y->dtprel
29472 || !compare_loc_operands (x, y))
29473 break;
29474 return x == NULL && y == NULL;
29477 /* Hashtable helpers. */
29479 struct loc_list_hasher : nofree_ptr_hash <dw_loc_list_struct>
29481 static inline hashval_t hash (const dw_loc_list_struct *);
29482 static inline bool equal (const dw_loc_list_struct *,
29483 const dw_loc_list_struct *);
29486 /* Return precomputed hash of location list X. */
29488 inline hashval_t
29489 loc_list_hasher::hash (const dw_loc_list_struct *x)
29491 return x->hash;
29494 /* Return true if location lists A and B are the same. */
29496 inline bool
29497 loc_list_hasher::equal (const dw_loc_list_struct *a,
29498 const dw_loc_list_struct *b)
29500 if (a == b)
29501 return 1;
29502 if (a->hash != b->hash)
29503 return 0;
29504 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
29505 if (strcmp (a->begin, b->begin) != 0
29506 || strcmp (a->end, b->end) != 0
29507 || (a->section == NULL) != (b->section == NULL)
29508 || (a->section && strcmp (a->section, b->section) != 0)
29509 || !compare_locs (a->expr, b->expr))
29510 break;
29511 return a == NULL && b == NULL;
29514 typedef hash_table<loc_list_hasher> loc_list_hash_type;
29517 /* Recursively optimize location lists referenced from DIE
29518 children and share them whenever possible. */
29520 static void
29521 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type *htab)
29523 dw_die_ref c;
29524 dw_attr_node *a;
29525 unsigned ix;
29526 dw_loc_list_struct **slot;
29528 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
29529 if (AT_class (a) == dw_val_class_loc_list)
29531 dw_loc_list_ref list = AT_loc_list (a);
29532 /* TODO: perform some optimizations here, before hashing
29533 it and storing into the hash table. */
29534 hash_loc_list (list);
29535 slot = htab->find_slot_with_hash (list, list->hash, INSERT);
29536 if (*slot == NULL)
29537 *slot = list;
29538 else
29539 a->dw_attr_val.v.val_loc_list = *slot;
29542 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
29546 /* Recursively assign each location list a unique index into the debug_addr
29547 section. */
29549 static void
29550 index_location_lists (dw_die_ref die)
29552 dw_die_ref c;
29553 dw_attr_node *a;
29554 unsigned ix;
29556 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
29557 if (AT_class (a) == dw_val_class_loc_list)
29559 dw_loc_list_ref list = AT_loc_list (a);
29560 dw_loc_list_ref curr;
29561 for (curr = list; curr != NULL; curr = curr->dw_loc_next)
29563 /* Don't index an entry that has already been indexed
29564 or won't be output. */
29565 if (curr->begin_entry != NULL
29566 || (strcmp (curr->begin, curr->end) == 0 && !curr->force))
29567 continue;
29569 curr->begin_entry
29570 = add_addr_table_entry (xstrdup (curr->begin), ate_kind_label);
29574 FOR_EACH_CHILD (die, c, index_location_lists (c));
29577 /* Optimize location lists referenced from DIE
29578 children and share them whenever possible. */
29580 static void
29581 optimize_location_lists (dw_die_ref die)
29583 loc_list_hash_type htab (500);
29584 optimize_location_lists_1 (die, &htab);
29587 /* Traverse the limbo die list, and add parent/child links. The only
29588 dies without parents that should be here are concrete instances of
29589 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
29590 For concrete instances, we can get the parent die from the abstract
29591 instance. */
29593 static void
29594 flush_limbo_die_list (void)
29596 limbo_die_node *node;
29598 /* get_context_die calls force_decl_die, which can put new DIEs on the
29599 limbo list in LTO mode when nested functions are put in a different
29600 partition than that of their parent function. */
29601 while ((node = limbo_die_list))
29603 dw_die_ref die = node->die;
29604 limbo_die_list = node->next;
29606 if (die->die_parent == NULL)
29608 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
29610 if (origin && origin->die_parent)
29611 add_child_die (origin->die_parent, die);
29612 else if (is_cu_die (die))
29614 else if (seen_error ())
29615 /* It's OK to be confused by errors in the input. */
29616 add_child_die (comp_unit_die (), die);
29617 else
29619 /* In certain situations, the lexical block containing a
29620 nested function can be optimized away, which results
29621 in the nested function die being orphaned. Likewise
29622 with the return type of that nested function. Force
29623 this to be a child of the containing function.
29625 It may happen that even the containing function got fully
29626 inlined and optimized out. In that case we are lost and
29627 assign the empty child. This should not be big issue as
29628 the function is likely unreachable too. */
29629 gcc_assert (node->created_for);
29631 if (DECL_P (node->created_for))
29632 origin = get_context_die (DECL_CONTEXT (node->created_for));
29633 else if (TYPE_P (node->created_for))
29634 origin = scope_die_for (node->created_for, comp_unit_die ());
29635 else
29636 origin = comp_unit_die ();
29638 add_child_die (origin, die);
29644 /* Output stuff that dwarf requires at the end of every file,
29645 and generate the DWARF-2 debugging info. */
29647 static void
29648 dwarf2out_finish (const char *)
29650 comdat_type_node *ctnode;
29651 dw_die_ref main_comp_unit_die;
29652 unsigned char checksum[16];
29654 /* Flush out any latecomers to the limbo party. */
29655 flush_limbo_die_list ();
29657 if (flag_checking)
29659 verify_die (comp_unit_die ());
29660 for (limbo_die_node *node = cu_die_list; node; node = node->next)
29661 verify_die (node->die);
29664 /* We shouldn't have any symbols with delayed asm names for
29665 DIEs generated after early finish. */
29666 gcc_assert (deferred_asm_name == NULL);
29668 gen_remaining_tmpl_value_param_die_attribute ();
29670 #if ENABLE_ASSERT_CHECKING
29672 dw_die_ref die = comp_unit_die (), c;
29673 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
29675 #endif
29676 resolve_addr (comp_unit_die ());
29677 move_marked_base_types ();
29679 /* Initialize sections and labels used for actual assembler output. */
29680 init_sections_and_labels ();
29682 /* Traverse the DIE's and add sibling attributes to those DIE's that
29683 have children. */
29684 add_sibling_attributes (comp_unit_die ());
29685 limbo_die_node *node;
29686 for (node = cu_die_list; node; node = node->next)
29687 add_sibling_attributes (node->die);
29688 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
29689 add_sibling_attributes (ctnode->root_die);
29691 /* When splitting DWARF info, we put some attributes in the
29692 skeleton compile_unit DIE that remains in the .o, while
29693 most attributes go in the DWO compile_unit_die. */
29694 if (dwarf_split_debug_info)
29696 limbo_die_node *cu;
29697 main_comp_unit_die = gen_compile_unit_die (NULL);
29698 if (dwarf_version >= 5)
29699 main_comp_unit_die->die_tag = DW_TAG_skeleton_unit;
29700 cu = limbo_die_list;
29701 gcc_assert (cu->die == main_comp_unit_die);
29702 limbo_die_list = limbo_die_list->next;
29703 cu->next = cu_die_list;
29704 cu_die_list = cu;
29706 else
29707 main_comp_unit_die = comp_unit_die ();
29709 /* Output a terminator label for the .text section. */
29710 switch_to_section (text_section);
29711 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
29712 if (cold_text_section)
29714 switch_to_section (cold_text_section);
29715 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
29718 /* We can only use the low/high_pc attributes if all of the code was
29719 in .text. */
29720 if (!have_multiple_function_sections
29721 || (dwarf_version < 3 && dwarf_strict))
29723 /* Don't add if the CU has no associated code. */
29724 if (text_section_used)
29725 add_AT_low_high_pc (main_comp_unit_die, text_section_label,
29726 text_end_label, true);
29728 else
29730 unsigned fde_idx;
29731 dw_fde_ref fde;
29732 bool range_list_added = false;
29734 if (text_section_used)
29735 add_ranges_by_labels (main_comp_unit_die, text_section_label,
29736 text_end_label, &range_list_added, true);
29737 if (cold_text_section_used)
29738 add_ranges_by_labels (main_comp_unit_die, cold_text_section_label,
29739 cold_end_label, &range_list_added, true);
29741 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
29743 if (DECL_IGNORED_P (fde->decl))
29744 continue;
29745 if (!fde->in_std_section)
29746 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
29747 fde->dw_fde_end, &range_list_added,
29748 true);
29749 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
29750 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
29751 fde->dw_fde_second_end, &range_list_added,
29752 true);
29755 if (range_list_added)
29757 /* We need to give .debug_loc and .debug_ranges an appropriate
29758 "base address". Use zero so that these addresses become
29759 absolute. Historically, we've emitted the unexpected
29760 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
29761 Emit both to give time for other tools to adapt. */
29762 add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
29763 if (! dwarf_strict && dwarf_version < 4)
29764 add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
29766 add_ranges (NULL);
29770 if (debug_info_level >= DINFO_LEVEL_TERSE)
29771 add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
29772 debug_line_section_label);
29774 if (have_macinfo)
29775 add_AT_macptr (comp_unit_die (),
29776 dwarf_version >= 5 ? DW_AT_macros
29777 : dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros,
29778 macinfo_section_label);
29780 if (dwarf_split_debug_info)
29782 if (have_location_lists)
29784 if (dwarf_version >= 5)
29785 add_AT_loclistsptr (comp_unit_die (), DW_AT_loclists_base,
29786 loc_section_label);
29787 /* optimize_location_lists calculates the size of the lists,
29788 so index them first, and assign indices to the entries.
29789 Although optimize_location_lists will remove entries from
29790 the table, it only does so for duplicates, and therefore
29791 only reduces ref_counts to 1. */
29792 index_location_lists (comp_unit_die ());
29795 if (addr_index_table != NULL)
29797 unsigned int index = 0;
29798 addr_index_table
29799 ->traverse_noresize<unsigned int *, index_addr_table_entry>
29800 (&index);
29804 loc_list_idx = 0;
29805 if (have_location_lists)
29807 optimize_location_lists (comp_unit_die ());
29808 /* And finally assign indexes to the entries for -gsplit-dwarf. */
29809 if (dwarf_version >= 5 && dwarf_split_debug_info)
29810 assign_location_list_indexes (comp_unit_die ());
29813 save_macinfo_strings ();
29815 if (dwarf_split_debug_info)
29817 unsigned int index = 0;
29819 /* Add attributes common to skeleton compile_units and
29820 type_units. Because these attributes include strings, it
29821 must be done before freezing the string table. Top-level
29822 skeleton die attrs are added when the skeleton type unit is
29823 created, so ensure it is created by this point. */
29824 add_top_level_skeleton_die_attrs (main_comp_unit_die);
29825 debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
29828 /* Output all of the compilation units. We put the main one last so that
29829 the offsets are available to output_pubnames. */
29830 for (node = cu_die_list; node; node = node->next)
29831 output_comp_unit (node->die, 0, NULL);
29833 hash_table<comdat_type_hasher> comdat_type_table (100);
29834 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
29836 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
29838 /* Don't output duplicate types. */
29839 if (*slot != HTAB_EMPTY_ENTRY)
29840 continue;
29842 /* Add a pointer to the line table for the main compilation unit
29843 so that the debugger can make sense of DW_AT_decl_file
29844 attributes. */
29845 if (debug_info_level >= DINFO_LEVEL_TERSE)
29846 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
29847 (!dwarf_split_debug_info
29848 ? debug_line_section_label
29849 : debug_skeleton_line_section_label));
29851 output_comdat_type_unit (ctnode);
29852 *slot = ctnode;
29855 /* The AT_pubnames attribute needs to go in all skeleton dies, including
29856 both the main_cu and all skeleton TUs. Making this call unconditional
29857 would end up either adding a second copy of the AT_pubnames attribute, or
29858 requiring a special case in add_top_level_skeleton_die_attrs. */
29859 if (!dwarf_split_debug_info)
29860 add_AT_pubnames (comp_unit_die ());
29862 if (dwarf_split_debug_info)
29864 int mark;
29865 struct md5_ctx ctx;
29867 if (dwarf_version >= 5 && !vec_safe_is_empty (ranges_table))
29868 index_rnglists ();
29870 /* Compute a checksum of the comp_unit to use as the dwo_id. */
29871 md5_init_ctx (&ctx);
29872 mark = 0;
29873 die_checksum (comp_unit_die (), &ctx, &mark);
29874 unmark_all_dies (comp_unit_die ());
29875 md5_finish_ctx (&ctx, checksum);
29877 if (dwarf_version < 5)
29879 /* Use the first 8 bytes of the checksum as the dwo_id,
29880 and add it to both comp-unit DIEs. */
29881 add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
29882 add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
29885 /* Add the base offset of the ranges table to the skeleton
29886 comp-unit DIE. */
29887 if (!vec_safe_is_empty (ranges_table))
29889 if (dwarf_version >= 5)
29890 add_AT_lineptr (main_comp_unit_die, DW_AT_rnglists_base,
29891 ranges_base_label);
29892 else
29893 add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
29894 ranges_section_label);
29897 switch_to_section (debug_addr_section);
29898 ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
29899 output_addr_table ();
29902 /* Output the main compilation unit if non-empty or if .debug_macinfo
29903 or .debug_macro will be emitted. */
29904 output_comp_unit (comp_unit_die (), have_macinfo,
29905 dwarf_split_debug_info ? checksum : NULL);
29907 if (dwarf_split_debug_info && info_section_emitted)
29908 output_skeleton_debug_sections (main_comp_unit_die, checksum);
29910 /* Output the abbreviation table. */
29911 if (vec_safe_length (abbrev_die_table) != 1)
29913 switch_to_section (debug_abbrev_section);
29914 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
29915 output_abbrev_section ();
29918 /* Output location list section if necessary. */
29919 if (have_location_lists)
29921 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
29922 char l2[MAX_ARTIFICIAL_LABEL_BYTES];
29923 /* Output the location lists info. */
29924 switch_to_section (debug_loc_section);
29925 if (dwarf_version >= 5)
29927 ASM_GENERATE_INTERNAL_LABEL (l1, DEBUG_LOC_SECTION_LABEL, 1);
29928 ASM_GENERATE_INTERNAL_LABEL (l2, DEBUG_LOC_SECTION_LABEL, 2);
29929 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
29930 dw2_asm_output_data (4, 0xffffffff,
29931 "Initial length escape value indicating "
29932 "64-bit DWARF extension");
29933 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
29934 "Length of Location Lists");
29935 ASM_OUTPUT_LABEL (asm_out_file, l1);
29936 dw2_asm_output_data (2, dwarf_version, "DWARF Version");
29937 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
29938 dw2_asm_output_data (1, 0, "Segment Size");
29939 dw2_asm_output_data (4, dwarf_split_debug_info ? loc_list_idx : 0,
29940 "Offset Entry Count");
29942 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
29943 if (dwarf_version >= 5 && dwarf_split_debug_info)
29945 unsigned int save_loc_list_idx = loc_list_idx;
29946 loc_list_idx = 0;
29947 output_loclists_offsets (comp_unit_die ());
29948 gcc_assert (save_loc_list_idx == loc_list_idx);
29950 output_location_lists (comp_unit_die ());
29951 if (dwarf_version >= 5)
29952 ASM_OUTPUT_LABEL (asm_out_file, l2);
29955 output_pubtables ();
29957 /* Output the address range information if a CU (.debug_info section)
29958 was emitted. We output an empty table even if we had no functions
29959 to put in it. This because the consumer has no way to tell the
29960 difference between an empty table that we omitted and failure to
29961 generate a table that would have contained data. */
29962 if (info_section_emitted)
29964 switch_to_section (debug_aranges_section);
29965 output_aranges ();
29968 /* Output ranges section if necessary. */
29969 if (!vec_safe_is_empty (ranges_table))
29971 if (dwarf_version >= 5)
29972 output_rnglists ();
29973 else
29974 output_ranges ();
29977 /* Have to end the macro section. */
29978 if (have_macinfo)
29980 switch_to_section (debug_macinfo_section);
29981 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
29982 output_macinfo ();
29983 dw2_asm_output_data (1, 0, "End compilation unit");
29986 /* Output the source line correspondence table. We must do this
29987 even if there is no line information. Otherwise, on an empty
29988 translation unit, we will generate a present, but empty,
29989 .debug_info section. IRIX 6.5 `nm' will then complain when
29990 examining the file. This is done late so that any filenames
29991 used by the debug_info section are marked as 'used'. */
29992 switch_to_section (debug_line_section);
29993 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
29994 if (! DWARF2_ASM_LINE_DEBUG_INFO)
29995 output_line_info (false);
29997 if (dwarf_split_debug_info && info_section_emitted)
29999 switch_to_section (debug_skeleton_line_section);
30000 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
30001 output_line_info (true);
30004 /* If we emitted any indirect strings, output the string table too. */
30005 if (debug_str_hash || skeleton_debug_str_hash)
30006 output_indirect_strings ();
30007 if (debug_line_str_hash)
30009 switch_to_section (debug_line_str_section);
30010 const enum dwarf_form form = DW_FORM_line_strp;
30011 debug_line_str_hash->traverse<enum dwarf_form,
30012 output_indirect_string> (form);
30016 /* Returns a hash value for X (which really is a variable_value_struct). */
30018 inline hashval_t
30019 variable_value_hasher::hash (variable_value_struct *x)
30021 return (hashval_t) x->decl_id;
30024 /* Return nonzero if decl_id of variable_value_struct X is the same as
30025 UID of decl Y. */
30027 inline bool
30028 variable_value_hasher::equal (variable_value_struct *x, tree y)
30030 return x->decl_id == DECL_UID (y);
30033 /* Helper function for resolve_variable_value, handle
30034 DW_OP_GNU_variable_value in one location expression.
30035 Return true if exprloc has been changed into loclist. */
30037 static bool
30038 resolve_variable_value_in_expr (dw_attr_node *a, dw_loc_descr_ref loc)
30040 dw_loc_descr_ref next;
30041 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = next)
30043 next = loc->dw_loc_next;
30044 if (loc->dw_loc_opc != DW_OP_GNU_variable_value
30045 || loc->dw_loc_oprnd1.val_class != dw_val_class_decl_ref)
30046 continue;
30048 tree decl = loc->dw_loc_oprnd1.v.val_decl_ref;
30049 if (DECL_CONTEXT (decl) != current_function_decl)
30050 continue;
30052 dw_die_ref ref = lookup_decl_die (decl);
30053 if (ref)
30055 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30056 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
30057 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
30058 continue;
30060 dw_loc_list_ref l = loc_list_from_tree (decl, 0, NULL);
30061 if (l == NULL)
30062 continue;
30063 if (l->dw_loc_next)
30065 if (AT_class (a) != dw_val_class_loc)
30066 continue;
30067 switch (a->dw_attr)
30069 /* Following attributes allow both exprloc and loclist
30070 classes, so we can change them into a loclist. */
30071 case DW_AT_location:
30072 case DW_AT_string_length:
30073 case DW_AT_return_addr:
30074 case DW_AT_data_member_location:
30075 case DW_AT_frame_base:
30076 case DW_AT_segment:
30077 case DW_AT_static_link:
30078 case DW_AT_use_location:
30079 case DW_AT_vtable_elem_location:
30080 if (prev)
30082 prev->dw_loc_next = NULL;
30083 prepend_loc_descr_to_each (l, AT_loc (a));
30085 if (next)
30086 add_loc_descr_to_each (l, next);
30087 a->dw_attr_val.val_class = dw_val_class_loc_list;
30088 a->dw_attr_val.val_entry = NULL;
30089 a->dw_attr_val.v.val_loc_list = l;
30090 have_location_lists = true;
30091 return true;
30092 /* Following attributes allow both exprloc and reference,
30093 so if the whole expression is DW_OP_GNU_variable_value alone
30094 we could transform it into reference. */
30095 case DW_AT_byte_size:
30096 case DW_AT_bit_size:
30097 case DW_AT_lower_bound:
30098 case DW_AT_upper_bound:
30099 case DW_AT_bit_stride:
30100 case DW_AT_count:
30101 case DW_AT_allocated:
30102 case DW_AT_associated:
30103 case DW_AT_byte_stride:
30104 if (prev == NULL && next == NULL)
30105 break;
30106 /* FALLTHRU */
30107 default:
30108 if (dwarf_strict)
30109 continue;
30110 break;
30112 /* Create DW_TAG_variable that we can refer to. */
30113 gen_decl_die (decl, NULL_TREE, NULL,
30114 lookup_decl_die (current_function_decl));
30115 ref = lookup_decl_die (decl);
30116 if (ref)
30118 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30119 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
30120 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
30122 continue;
30124 if (prev)
30126 prev->dw_loc_next = l->expr;
30127 add_loc_descr (&prev->dw_loc_next, next);
30128 free_loc_descr (loc, NULL);
30129 next = prev->dw_loc_next;
30131 else
30133 memcpy (loc, l->expr, sizeof (dw_loc_descr_node));
30134 add_loc_descr (&loc, next);
30135 next = loc;
30137 loc = prev;
30139 return false;
30142 /* Attempt to resolve DW_OP_GNU_variable_value using loc_list_from_tree. */
30144 static void
30145 resolve_variable_value (dw_die_ref die)
30147 dw_attr_node *a;
30148 dw_loc_list_ref loc;
30149 unsigned ix;
30151 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
30152 switch (AT_class (a))
30154 case dw_val_class_loc:
30155 if (!resolve_variable_value_in_expr (a, AT_loc (a)))
30156 break;
30157 /* FALLTHRU */
30158 case dw_val_class_loc_list:
30159 loc = AT_loc_list (a);
30160 gcc_assert (loc);
30161 for (; loc; loc = loc->dw_loc_next)
30162 resolve_variable_value_in_expr (a, loc->expr);
30163 break;
30164 default:
30165 break;
30169 /* Attempt to optimize DW_OP_GNU_variable_value refering to
30170 temporaries in the current function. */
30172 static void
30173 resolve_variable_values (void)
30175 if (!variable_value_hash || !current_function_decl)
30176 return;
30178 struct variable_value_struct *node
30179 = variable_value_hash->find_with_hash (current_function_decl,
30180 DECL_UID (current_function_decl));
30182 if (node == NULL)
30183 return;
30185 unsigned int i;
30186 dw_die_ref die;
30187 FOR_EACH_VEC_SAFE_ELT (node->dies, i, die)
30188 resolve_variable_value (die);
30191 /* Helper function for note_variable_value, handle one location
30192 expression. */
30194 static void
30195 note_variable_value_in_expr (dw_die_ref die, dw_loc_descr_ref loc)
30197 for (; loc; loc = loc->dw_loc_next)
30198 if (loc->dw_loc_opc == DW_OP_GNU_variable_value
30199 && loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
30201 tree decl = loc->dw_loc_oprnd1.v.val_decl_ref;
30202 dw_die_ref ref = lookup_decl_die (decl);
30203 if (ref)
30205 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30206 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
30207 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
30208 continue;
30210 if (VAR_P (decl)
30211 && DECL_CONTEXT (decl)
30212 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL
30213 && lookup_decl_die (DECL_CONTEXT (decl)))
30215 if (!variable_value_hash)
30216 variable_value_hash
30217 = hash_table<variable_value_hasher>::create_ggc (10);
30219 tree fndecl = DECL_CONTEXT (decl);
30220 struct variable_value_struct *node;
30221 struct variable_value_struct **slot
30222 = variable_value_hash->find_slot_with_hash (fndecl,
30223 DECL_UID (fndecl),
30224 INSERT);
30225 if (*slot == NULL)
30227 node = ggc_cleared_alloc<variable_value_struct> ();
30228 node->decl_id = DECL_UID (fndecl);
30229 *slot = node;
30231 else
30232 node = *slot;
30234 vec_safe_push (node->dies, die);
30239 /* Walk the tree DIE and note DIEs with DW_OP_GNU_variable_value still
30240 with dw_val_class_decl_ref operand. */
30242 static void
30243 note_variable_value (dw_die_ref die)
30245 dw_die_ref c;
30246 dw_attr_node *a;
30247 dw_loc_list_ref loc;
30248 unsigned ix;
30250 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
30251 switch (AT_class (a))
30253 case dw_val_class_loc_list:
30254 loc = AT_loc_list (a);
30255 gcc_assert (loc);
30256 if (!loc->noted_variable_value)
30258 loc->noted_variable_value = 1;
30259 for (; loc; loc = loc->dw_loc_next)
30260 note_variable_value_in_expr (die, loc->expr);
30262 break;
30263 case dw_val_class_loc:
30264 note_variable_value_in_expr (die, AT_loc (a));
30265 break;
30266 default:
30267 break;
30270 /* Mark children. */
30271 FOR_EACH_CHILD (die, c, note_variable_value (c));
30274 /* Perform any cleanups needed after the early debug generation pass
30275 has run. */
30277 static void
30278 dwarf2out_early_finish (const char *filename)
30280 set_early_dwarf s;
30282 /* PCH might result in DW_AT_producer string being restored from the
30283 header compilation, so always fill it with empty string initially
30284 and overwrite only here. */
30285 dw_attr_node *producer = get_AT (comp_unit_die (), DW_AT_producer);
30286 producer_string = gen_producer_string ();
30287 producer->dw_attr_val.v.val_str->refcount--;
30288 producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
30290 /* Add the name for the main input file now. We delayed this from
30291 dwarf2out_init to avoid complications with PCH. */
30292 add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
30293 add_comp_dir_attribute (comp_unit_die ());
30295 /* When emitting DWARF5 .debug_line_str, move DW_AT_name and
30296 DW_AT_comp_dir into .debug_line_str section. */
30297 if (!DWARF2_ASM_LINE_DEBUG_INFO
30298 && dwarf_version >= 5
30299 && DWARF5_USE_DEBUG_LINE_STR)
30301 for (int i = 0; i < 2; i++)
30303 dw_attr_node *a = get_AT (comp_unit_die (),
30304 i ? DW_AT_comp_dir : DW_AT_name);
30305 if (a == NULL
30306 || AT_class (a) != dw_val_class_str
30307 || strlen (AT_string (a)) + 1 <= DWARF_OFFSET_SIZE)
30308 continue;
30310 if (! debug_line_str_hash)
30311 debug_line_str_hash
30312 = hash_table<indirect_string_hasher>::create_ggc (10);
30314 struct indirect_string_node *node
30315 = find_AT_string_in_table (AT_string (a), debug_line_str_hash);
30316 set_indirect_string (node);
30317 node->form = DW_FORM_line_strp;
30318 a->dw_attr_val.v.val_str->refcount--;
30319 a->dw_attr_val.v.val_str = node;
30323 /* With LTO early dwarf was really finished at compile-time, so make
30324 sure to adjust the phase after annotating the LTRANS CU DIE. */
30325 if (in_lto_p)
30327 early_dwarf_finished = true;
30328 return;
30331 /* Walk through the list of incomplete types again, trying once more to
30332 emit full debugging info for them. */
30333 retry_incomplete_types ();
30335 /* The point here is to flush out the limbo list so that it is empty
30336 and we don't need to stream it for LTO. */
30337 flush_limbo_die_list ();
30339 gen_scheduled_generic_parms_dies ();
30340 gen_remaining_tmpl_value_param_die_attribute ();
30342 /* Add DW_AT_linkage_name for all deferred DIEs. */
30343 for (limbo_die_node *node = deferred_asm_name; node; node = node->next)
30345 tree decl = node->created_for;
30346 if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
30347 /* A missing DECL_ASSEMBLER_NAME can be a constant DIE that
30348 ended up in deferred_asm_name before we knew it was
30349 constant and never written to disk. */
30350 && DECL_ASSEMBLER_NAME (decl))
30352 add_linkage_attr (node->die, decl);
30353 move_linkage_attr (node->die);
30356 deferred_asm_name = NULL;
30358 if (flag_eliminate_unused_debug_types)
30359 prune_unused_types ();
30361 /* Generate separate COMDAT sections for type DIEs. */
30362 if (use_debug_types)
30364 break_out_comdat_types (comp_unit_die ());
30366 /* Each new type_unit DIE was added to the limbo die list when created.
30367 Since these have all been added to comdat_type_list, clear the
30368 limbo die list. */
30369 limbo_die_list = NULL;
30371 /* For each new comdat type unit, copy declarations for incomplete
30372 types to make the new unit self-contained (i.e., no direct
30373 references to the main compile unit). */
30374 for (comdat_type_node *ctnode = comdat_type_list;
30375 ctnode != NULL; ctnode = ctnode->next)
30376 copy_decls_for_unworthy_types (ctnode->root_die);
30377 copy_decls_for_unworthy_types (comp_unit_die ());
30379 /* In the process of copying declarations from one unit to another,
30380 we may have left some declarations behind that are no longer
30381 referenced. Prune them. */
30382 prune_unused_types ();
30385 /* Generate separate CUs for each of the include files we've seen.
30386 They will go into limbo_die_list and from there to cu_die_list. */
30387 if (flag_eliminate_dwarf2_dups)
30389 gcc_assert (limbo_die_list == NULL);
30390 break_out_includes (comp_unit_die ());
30391 limbo_die_node *cu;
30392 while ((cu = limbo_die_list))
30394 limbo_die_list = cu->next;
30395 cu->next = cu_die_list;
30396 cu_die_list = cu;
30400 /* Traverse the DIE's and note DIEs with DW_OP_GNU_variable_value still
30401 with dw_val_class_decl_ref operand. */
30402 note_variable_value (comp_unit_die ());
30403 for (limbo_die_node *node = cu_die_list; node; node = node->next)
30404 note_variable_value (node->die);
30405 for (comdat_type_node *ctnode = comdat_type_list; ctnode != NULL;
30406 ctnode = ctnode->next)
30407 note_variable_value (ctnode->root_die);
30408 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
30409 note_variable_value (node->die);
30411 /* The early debug phase is now finished. */
30412 early_dwarf_finished = true;
30415 /* Reset all state within dwarf2out.c so that we can rerun the compiler
30416 within the same process. For use by toplev::finalize. */
30418 void
30419 dwarf2out_c_finalize (void)
30421 last_var_location_insn = NULL;
30422 cached_next_real_insn = NULL;
30423 used_rtx_array = NULL;
30424 incomplete_types = NULL;
30425 decl_scope_table = NULL;
30426 debug_info_section = NULL;
30427 debug_skeleton_info_section = NULL;
30428 debug_abbrev_section = NULL;
30429 debug_skeleton_abbrev_section = NULL;
30430 debug_aranges_section = NULL;
30431 debug_addr_section = NULL;
30432 debug_macinfo_section = NULL;
30433 debug_line_section = NULL;
30434 debug_skeleton_line_section = NULL;
30435 debug_loc_section = NULL;
30436 debug_pubnames_section = NULL;
30437 debug_pubtypes_section = NULL;
30438 debug_str_section = NULL;
30439 debug_line_str_section = NULL;
30440 debug_str_dwo_section = NULL;
30441 debug_str_offsets_section = NULL;
30442 debug_ranges_section = NULL;
30443 debug_frame_section = NULL;
30444 fde_vec = NULL;
30445 debug_str_hash = NULL;
30446 debug_line_str_hash = NULL;
30447 skeleton_debug_str_hash = NULL;
30448 dw2_string_counter = 0;
30449 have_multiple_function_sections = false;
30450 text_section_used = false;
30451 cold_text_section_used = false;
30452 cold_text_section = NULL;
30453 current_unit_personality = NULL;
30455 early_dwarf = false;
30456 early_dwarf_finished = false;
30458 next_die_offset = 0;
30459 single_comp_unit_die = NULL;
30460 comdat_type_list = NULL;
30461 limbo_die_list = NULL;
30462 file_table = NULL;
30463 decl_die_table = NULL;
30464 common_block_die_table = NULL;
30465 decl_loc_table = NULL;
30466 call_arg_locations = NULL;
30467 call_arg_loc_last = NULL;
30468 call_site_count = -1;
30469 tail_call_site_count = -1;
30470 cached_dw_loc_list_table = NULL;
30471 abbrev_die_table = NULL;
30472 delete dwarf_proc_stack_usage_map;
30473 dwarf_proc_stack_usage_map = NULL;
30474 line_info_label_num = 0;
30475 cur_line_info_table = NULL;
30476 text_section_line_info = NULL;
30477 cold_text_section_line_info = NULL;
30478 separate_line_info = NULL;
30479 info_section_emitted = false;
30480 pubname_table = NULL;
30481 pubtype_table = NULL;
30482 macinfo_table = NULL;
30483 ranges_table = NULL;
30484 ranges_by_label = NULL;
30485 rnglist_idx = 0;
30486 have_location_lists = false;
30487 loclabel_num = 0;
30488 poc_label_num = 0;
30489 last_emitted_file = NULL;
30490 label_num = 0;
30491 tmpl_value_parm_die_table = NULL;
30492 generic_type_instances = NULL;
30493 frame_pointer_fb_offset = 0;
30494 frame_pointer_fb_offset_valid = false;
30495 base_types.release ();
30496 XDELETEVEC (producer_string);
30497 producer_string = NULL;
30500 #include "gt-dwarf2out.h"