libgo: add misc/cgo files
[official-gcc.git] / gcc / dwarf2out.c
blobc277d27e8e81d4dd1ff86a31df55a1c604d5fde6
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 #ifndef DWARF_INITIAL_LENGTH_SIZE_STR
193 #define DWARF_INITIAL_LENGTH_SIZE_STR (DWARF_OFFSET_SIZE == 4 ? "-4" : "-12")
194 #endif
196 /* Round SIZE up to the nearest BOUNDARY. */
197 #define DWARF_ROUND(SIZE,BOUNDARY) \
198 ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
200 /* CIE identifier. */
201 #if HOST_BITS_PER_WIDE_INT >= 64
202 #define DWARF_CIE_ID \
203 (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
204 #else
205 #define DWARF_CIE_ID DW_CIE_ID
206 #endif
209 /* A vector for a table that contains frame description
210 information for each routine. */
211 #define NOT_INDEXED (-1U)
212 #define NO_INDEX_ASSIGNED (-2U)
214 static GTY(()) vec<dw_fde_ref, va_gc> *fde_vec;
216 struct GTY((for_user)) indirect_string_node {
217 const char *str;
218 unsigned int refcount;
219 enum dwarf_form form;
220 char *label;
221 unsigned int index;
224 struct indirect_string_hasher : ggc_ptr_hash<indirect_string_node>
226 typedef const char *compare_type;
228 static hashval_t hash (indirect_string_node *);
229 static bool equal (indirect_string_node *, const char *);
232 static GTY (()) hash_table<indirect_string_hasher> *debug_str_hash;
234 static GTY (()) hash_table<indirect_string_hasher> *debug_line_str_hash;
236 /* With split_debug_info, both the comp_dir and dwo_name go in the
237 main object file, rather than the dwo, similar to the force_direct
238 parameter elsewhere but with additional complications:
240 1) The string is needed in both the main object file and the dwo.
241 That is, the comp_dir and dwo_name will appear in both places.
243 2) Strings can use four forms: DW_FORM_string, DW_FORM_strp,
244 DW_FORM_line_strp or DW_FORM_GNU_str_index.
246 3) GCC chooses the form to use late, depending on the size and
247 reference count.
249 Rather than forcing the all debug string handling functions and
250 callers to deal with these complications, simply use a separate,
251 special-cased string table for any attribute that should go in the
252 main object file. This limits the complexity to just the places
253 that need it. */
255 static GTY (()) hash_table<indirect_string_hasher> *skeleton_debug_str_hash;
257 static GTY(()) int dw2_string_counter;
259 /* True if the compilation unit places functions in more than one section. */
260 static GTY(()) bool have_multiple_function_sections = false;
262 /* Whether the default text and cold text sections have been used at all. */
264 static GTY(()) bool text_section_used = false;
265 static GTY(()) bool cold_text_section_used = false;
267 /* The default cold text section. */
268 static GTY(()) section *cold_text_section;
270 /* The DIE for C++14 'auto' in a function return type. */
271 static GTY(()) dw_die_ref auto_die;
273 /* The DIE for C++14 'decltype(auto)' in a function return type. */
274 static GTY(()) dw_die_ref decltype_auto_die;
276 /* Forward declarations for functions defined in this file. */
278 static void output_call_frame_info (int);
279 static void dwarf2out_note_section_used (void);
281 /* Personality decl of current unit. Used only when assembler does not support
282 personality CFI. */
283 static GTY(()) rtx current_unit_personality;
285 /* .debug_rnglists next index. */
286 static unsigned int rnglist_idx;
288 /* Data and reference forms for relocatable data. */
289 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
290 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
292 #ifndef DEBUG_FRAME_SECTION
293 #define DEBUG_FRAME_SECTION ".debug_frame"
294 #endif
296 #ifndef FUNC_BEGIN_LABEL
297 #define FUNC_BEGIN_LABEL "LFB"
298 #endif
300 #ifndef FUNC_END_LABEL
301 #define FUNC_END_LABEL "LFE"
302 #endif
304 #ifndef PROLOGUE_END_LABEL
305 #define PROLOGUE_END_LABEL "LPE"
306 #endif
308 #ifndef EPILOGUE_BEGIN_LABEL
309 #define EPILOGUE_BEGIN_LABEL "LEB"
310 #endif
312 #ifndef FRAME_BEGIN_LABEL
313 #define FRAME_BEGIN_LABEL "Lframe"
314 #endif
315 #define CIE_AFTER_SIZE_LABEL "LSCIE"
316 #define CIE_END_LABEL "LECIE"
317 #define FDE_LABEL "LSFDE"
318 #define FDE_AFTER_SIZE_LABEL "LASFDE"
319 #define FDE_END_LABEL "LEFDE"
320 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
321 #define LINE_NUMBER_END_LABEL "LELT"
322 #define LN_PROLOG_AS_LABEL "LASLTP"
323 #define LN_PROLOG_END_LABEL "LELTP"
324 #define DIE_LABEL_PREFIX "DW"
326 /* Match the base name of a file to the base name of a compilation unit. */
328 static int
329 matches_main_base (const char *path)
331 /* Cache the last query. */
332 static const char *last_path = NULL;
333 static int last_match = 0;
334 if (path != last_path)
336 const char *base;
337 int length = base_of_path (path, &base);
338 last_path = path;
339 last_match = (length == main_input_baselength
340 && memcmp (base, main_input_basename, length) == 0);
342 return last_match;
345 #ifdef DEBUG_DEBUG_STRUCT
347 static int
348 dump_struct_debug (tree type, enum debug_info_usage usage,
349 enum debug_struct_file criterion, int generic,
350 int matches, int result)
352 /* Find the type name. */
353 tree type_decl = TYPE_STUB_DECL (type);
354 tree t = type_decl;
355 const char *name = 0;
356 if (TREE_CODE (t) == TYPE_DECL)
357 t = DECL_NAME (t);
358 if (t)
359 name = IDENTIFIER_POINTER (t);
361 fprintf (stderr, " struct %d %s %s %s %s %d %p %s\n",
362 criterion,
363 DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
364 matches ? "bas" : "hdr",
365 generic ? "gen" : "ord",
366 usage == DINFO_USAGE_DFN ? ";" :
367 usage == DINFO_USAGE_DIR_USE ? "." : "*",
368 result,
369 (void*) type_decl, name);
370 return result;
372 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
373 dump_struct_debug (type, usage, criterion, generic, matches, result)
375 #else
377 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
378 (result)
380 #endif
382 /* Get the number of HOST_WIDE_INTs needed to represent the precision
383 of the number. Some constants have a large uniform precision, so
384 we get the precision needed for the actual value of the number. */
386 static unsigned int
387 get_full_len (const wide_int &op)
389 int prec = wi::min_precision (op, UNSIGNED);
390 return ((prec + HOST_BITS_PER_WIDE_INT - 1)
391 / HOST_BITS_PER_WIDE_INT);
394 static bool
395 should_emit_struct_debug (tree type, enum debug_info_usage usage)
397 enum debug_struct_file criterion;
398 tree type_decl;
399 bool generic = lang_hooks.types.generic_p (type);
401 if (generic)
402 criterion = debug_struct_generic[usage];
403 else
404 criterion = debug_struct_ordinary[usage];
406 if (criterion == DINFO_STRUCT_FILE_NONE)
407 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
408 if (criterion == DINFO_STRUCT_FILE_ANY)
409 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
411 type_decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type));
413 if (type_decl != NULL)
415 if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
416 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
418 if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
419 return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
422 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
425 /* Switch [BACK] to eh_frame_section. If we don't have an eh_frame_section,
426 switch to the data section instead, and write out a synthetic start label
427 for collect2 the first time around. */
429 static void
430 switch_to_eh_frame_section (bool back ATTRIBUTE_UNUSED)
432 if (eh_frame_section == 0)
434 int flags;
436 if (EH_TABLES_CAN_BE_READ_ONLY)
438 int fde_encoding;
439 int per_encoding;
440 int lsda_encoding;
442 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
443 /*global=*/0);
444 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
445 /*global=*/1);
446 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
447 /*global=*/0);
448 flags = ((! flag_pic
449 || ((fde_encoding & 0x70) != DW_EH_PE_absptr
450 && (fde_encoding & 0x70) != DW_EH_PE_aligned
451 && (per_encoding & 0x70) != DW_EH_PE_absptr
452 && (per_encoding & 0x70) != DW_EH_PE_aligned
453 && (lsda_encoding & 0x70) != DW_EH_PE_absptr
454 && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
455 ? 0 : SECTION_WRITE);
457 else
458 flags = SECTION_WRITE;
460 #ifdef EH_FRAME_SECTION_NAME
461 eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
462 #else
463 eh_frame_section = ((flags == SECTION_WRITE)
464 ? data_section : readonly_data_section);
465 #endif /* EH_FRAME_SECTION_NAME */
468 switch_to_section (eh_frame_section);
470 #ifdef EH_FRAME_THROUGH_COLLECT2
471 /* We have no special eh_frame section. Emit special labels to guide
472 collect2. */
473 if (!back)
475 tree label = get_file_function_name ("F");
476 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
477 targetm.asm_out.globalize_label (asm_out_file,
478 IDENTIFIER_POINTER (label));
479 ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
481 #endif
484 /* Switch [BACK] to the eh or debug frame table section, depending on
485 FOR_EH. */
487 static void
488 switch_to_frame_table_section (int for_eh, bool back)
490 if (for_eh)
491 switch_to_eh_frame_section (back);
492 else
494 if (!debug_frame_section)
495 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
496 SECTION_DEBUG, NULL);
497 switch_to_section (debug_frame_section);
501 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used. */
503 enum dw_cfi_oprnd_type
504 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
506 switch (cfi)
508 case DW_CFA_nop:
509 case DW_CFA_GNU_window_save:
510 case DW_CFA_remember_state:
511 case DW_CFA_restore_state:
512 return dw_cfi_oprnd_unused;
514 case DW_CFA_set_loc:
515 case DW_CFA_advance_loc1:
516 case DW_CFA_advance_loc2:
517 case DW_CFA_advance_loc4:
518 case DW_CFA_MIPS_advance_loc8:
519 return dw_cfi_oprnd_addr;
521 case DW_CFA_offset:
522 case DW_CFA_offset_extended:
523 case DW_CFA_def_cfa:
524 case DW_CFA_offset_extended_sf:
525 case DW_CFA_def_cfa_sf:
526 case DW_CFA_restore:
527 case DW_CFA_restore_extended:
528 case DW_CFA_undefined:
529 case DW_CFA_same_value:
530 case DW_CFA_def_cfa_register:
531 case DW_CFA_register:
532 case DW_CFA_expression:
533 case DW_CFA_val_expression:
534 return dw_cfi_oprnd_reg_num;
536 case DW_CFA_def_cfa_offset:
537 case DW_CFA_GNU_args_size:
538 case DW_CFA_def_cfa_offset_sf:
539 return dw_cfi_oprnd_offset;
541 case DW_CFA_def_cfa_expression:
542 return dw_cfi_oprnd_loc;
544 default:
545 gcc_unreachable ();
549 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used. */
551 enum dw_cfi_oprnd_type
552 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
554 switch (cfi)
556 case DW_CFA_def_cfa:
557 case DW_CFA_def_cfa_sf:
558 case DW_CFA_offset:
559 case DW_CFA_offset_extended_sf:
560 case DW_CFA_offset_extended:
561 return dw_cfi_oprnd_offset;
563 case DW_CFA_register:
564 return dw_cfi_oprnd_reg_num;
566 case DW_CFA_expression:
567 case DW_CFA_val_expression:
568 return dw_cfi_oprnd_loc;
570 default:
571 return dw_cfi_oprnd_unused;
575 /* Output one FDE. */
577 static void
578 output_fde (dw_fde_ref fde, bool for_eh, bool second,
579 char *section_start_label, int fde_encoding, char *augmentation,
580 bool any_lsda_needed, int lsda_encoding)
582 const char *begin, *end;
583 static unsigned int j;
584 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
586 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
587 /* empty */ 0);
588 targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
589 for_eh + j);
590 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
591 ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
592 if (!XCOFF_DEBUGGING_INFO || for_eh)
594 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
595 dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
596 " indicating 64-bit DWARF extension");
597 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
598 "FDE Length");
600 ASM_OUTPUT_LABEL (asm_out_file, l1);
602 if (for_eh)
603 dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
604 else
605 dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
606 debug_frame_section, "FDE CIE offset");
608 begin = second ? fde->dw_fde_second_begin : fde->dw_fde_begin;
609 end = second ? fde->dw_fde_second_end : fde->dw_fde_end;
611 if (for_eh)
613 rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
614 SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
615 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
616 "FDE initial location");
617 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
618 end, begin, "FDE address range");
620 else
622 dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
623 dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
626 if (augmentation[0])
628 if (any_lsda_needed)
630 int size = size_of_encoded_value (lsda_encoding);
632 if (lsda_encoding == DW_EH_PE_aligned)
634 int offset = ( 4 /* Length */
635 + 4 /* CIE offset */
636 + 2 * size_of_encoded_value (fde_encoding)
637 + 1 /* Augmentation size */ );
638 int pad = -offset & (PTR_SIZE - 1);
640 size += pad;
641 gcc_assert (size_of_uleb128 (size) == 1);
644 dw2_asm_output_data_uleb128 (size, "Augmentation size");
646 if (fde->uses_eh_lsda)
648 ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
649 fde->funcdef_number);
650 dw2_asm_output_encoded_addr_rtx (lsda_encoding,
651 gen_rtx_SYMBOL_REF (Pmode, l1),
652 false,
653 "Language Specific Data Area");
655 else
657 if (lsda_encoding == DW_EH_PE_aligned)
658 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
659 dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
660 "Language Specific Data Area (none)");
663 else
664 dw2_asm_output_data_uleb128 (0, "Augmentation size");
667 /* Loop through the Call Frame Instructions associated with this FDE. */
668 fde->dw_fde_current_label = begin;
670 size_t from, until, i;
672 from = 0;
673 until = vec_safe_length (fde->dw_fde_cfi);
675 if (fde->dw_fde_second_begin == NULL)
677 else if (!second)
678 until = fde->dw_fde_switch_cfi_index;
679 else
680 from = fde->dw_fde_switch_cfi_index;
682 for (i = from; i < until; i++)
683 output_cfi ((*fde->dw_fde_cfi)[i], fde, for_eh);
686 /* If we are to emit a ref/link from function bodies to their frame tables,
687 do it now. This is typically performed to make sure that tables
688 associated with functions are dragged with them and not discarded in
689 garbage collecting links. We need to do this on a per function basis to
690 cope with -ffunction-sections. */
692 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
693 /* Switch to the function section, emit the ref to the tables, and
694 switch *back* into the table section. */
695 switch_to_section (function_section (fde->decl));
696 ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
697 switch_to_frame_table_section (for_eh, true);
698 #endif
700 /* Pad the FDE out to an address sized boundary. */
701 ASM_OUTPUT_ALIGN (asm_out_file,
702 floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
703 ASM_OUTPUT_LABEL (asm_out_file, l2);
705 j += 2;
708 /* Return true if frame description entry FDE is needed for EH. */
710 static bool
711 fde_needed_for_eh_p (dw_fde_ref fde)
713 if (flag_asynchronous_unwind_tables)
714 return true;
716 if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
717 return true;
719 if (fde->uses_eh_lsda)
720 return true;
722 /* If exceptions are enabled, we have collected nothrow info. */
723 if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
724 return false;
726 return true;
729 /* Output the call frame information used to record information
730 that relates to calculating the frame pointer, and records the
731 location of saved registers. */
733 static void
734 output_call_frame_info (int for_eh)
736 unsigned int i;
737 dw_fde_ref fde;
738 dw_cfi_ref cfi;
739 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
740 char section_start_label[MAX_ARTIFICIAL_LABEL_BYTES];
741 bool any_lsda_needed = false;
742 char augmentation[6];
743 int augmentation_size;
744 int fde_encoding = DW_EH_PE_absptr;
745 int per_encoding = DW_EH_PE_absptr;
746 int lsda_encoding = DW_EH_PE_absptr;
747 int return_reg;
748 rtx personality = NULL;
749 int dw_cie_version;
751 /* Don't emit a CIE if there won't be any FDEs. */
752 if (!fde_vec)
753 return;
755 /* Nothing to do if the assembler's doing it all. */
756 if (dwarf2out_do_cfi_asm ())
757 return;
759 /* If we don't have any functions we'll want to unwind out of, don't emit
760 any EH unwind information. If we make FDEs linkonce, we may have to
761 emit an empty label for an FDE that wouldn't otherwise be emitted. We
762 want to avoid having an FDE kept around when the function it refers to
763 is discarded. Example where this matters: a primary function template
764 in C++ requires EH information, an explicit specialization doesn't. */
765 if (for_eh)
767 bool any_eh_needed = false;
769 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
771 if (fde->uses_eh_lsda)
772 any_eh_needed = any_lsda_needed = true;
773 else if (fde_needed_for_eh_p (fde))
774 any_eh_needed = true;
775 else if (TARGET_USES_WEAK_UNWIND_INFO)
776 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, 1, 1);
779 if (!any_eh_needed)
780 return;
783 /* We're going to be generating comments, so turn on app. */
784 if (flag_debug_asm)
785 app_enable ();
787 /* Switch to the proper frame section, first time. */
788 switch_to_frame_table_section (for_eh, false);
790 ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
791 ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
793 /* Output the CIE. */
794 ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
795 ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
796 if (!XCOFF_DEBUGGING_INFO || for_eh)
798 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
799 dw2_asm_output_data (4, 0xffffffff,
800 "Initial length escape value indicating 64-bit DWARF extension");
801 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
802 "Length of Common Information Entry");
804 ASM_OUTPUT_LABEL (asm_out_file, l1);
806 /* Now that the CIE pointer is PC-relative for EH,
807 use 0 to identify the CIE. */
808 dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
809 (for_eh ? 0 : DWARF_CIE_ID),
810 "CIE Identifier Tag");
812 /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
813 use CIE version 1, unless that would produce incorrect results
814 due to overflowing the return register column. */
815 return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
816 dw_cie_version = 1;
817 if (return_reg >= 256 || dwarf_version > 2)
818 dw_cie_version = 3;
819 dw2_asm_output_data (1, dw_cie_version, "CIE Version");
821 augmentation[0] = 0;
822 augmentation_size = 0;
824 personality = current_unit_personality;
825 if (for_eh)
827 char *p;
829 /* Augmentation:
830 z Indicates that a uleb128 is present to size the
831 augmentation section.
832 L Indicates the encoding (and thus presence) of
833 an LSDA pointer in the FDE augmentation.
834 R Indicates a non-default pointer encoding for
835 FDE code pointers.
836 P Indicates the presence of an encoding + language
837 personality routine in the CIE augmentation. */
839 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
840 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
841 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
843 p = augmentation + 1;
844 if (personality)
846 *p++ = 'P';
847 augmentation_size += 1 + size_of_encoded_value (per_encoding);
848 assemble_external_libcall (personality);
850 if (any_lsda_needed)
852 *p++ = 'L';
853 augmentation_size += 1;
855 if (fde_encoding != DW_EH_PE_absptr)
857 *p++ = 'R';
858 augmentation_size += 1;
860 if (p > augmentation + 1)
862 augmentation[0] = 'z';
863 *p = '\0';
866 /* Ug. Some platforms can't do unaligned dynamic relocations at all. */
867 if (personality && per_encoding == DW_EH_PE_aligned)
869 int offset = ( 4 /* Length */
870 + 4 /* CIE Id */
871 + 1 /* CIE version */
872 + strlen (augmentation) + 1 /* Augmentation */
873 + size_of_uleb128 (1) /* Code alignment */
874 + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
875 + 1 /* RA column */
876 + 1 /* Augmentation size */
877 + 1 /* Personality encoding */ );
878 int pad = -offset & (PTR_SIZE - 1);
880 augmentation_size += pad;
882 /* Augmentations should be small, so there's scarce need to
883 iterate for a solution. Die if we exceed one uleb128 byte. */
884 gcc_assert (size_of_uleb128 (augmentation_size) == 1);
888 dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
889 if (dw_cie_version >= 4)
891 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
892 dw2_asm_output_data (1, 0, "CIE Segment Size");
894 dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
895 dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
896 "CIE Data Alignment Factor");
898 if (dw_cie_version == 1)
899 dw2_asm_output_data (1, return_reg, "CIE RA Column");
900 else
901 dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
903 if (augmentation[0])
905 dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
906 if (personality)
908 dw2_asm_output_data (1, per_encoding, "Personality (%s)",
909 eh_data_format_name (per_encoding));
910 dw2_asm_output_encoded_addr_rtx (per_encoding,
911 personality,
912 true, NULL);
915 if (any_lsda_needed)
916 dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
917 eh_data_format_name (lsda_encoding));
919 if (fde_encoding != DW_EH_PE_absptr)
920 dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
921 eh_data_format_name (fde_encoding));
924 FOR_EACH_VEC_ELT (*cie_cfi_vec, i, cfi)
925 output_cfi (cfi, NULL, for_eh);
927 /* Pad the CIE out to an address sized boundary. */
928 ASM_OUTPUT_ALIGN (asm_out_file,
929 floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
930 ASM_OUTPUT_LABEL (asm_out_file, l2);
932 /* Loop through all of the FDE's. */
933 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
935 unsigned int k;
937 /* Don't emit EH unwind info for leaf functions that don't need it. */
938 if (for_eh && !fde_needed_for_eh_p (fde))
939 continue;
941 for (k = 0; k < (fde->dw_fde_second_begin ? 2 : 1); k++)
942 output_fde (fde, for_eh, k, section_start_label, fde_encoding,
943 augmentation, any_lsda_needed, lsda_encoding);
946 if (for_eh && targetm.terminate_dw2_eh_frame_info)
947 dw2_asm_output_data (4, 0, "End of Table");
949 /* Turn off app to make assembly quicker. */
950 if (flag_debug_asm)
951 app_disable ();
954 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed. */
956 static void
957 dwarf2out_do_cfi_startproc (bool second)
959 int enc;
960 rtx ref;
961 rtx personality = get_personality_function (current_function_decl);
963 fprintf (asm_out_file, "\t.cfi_startproc\n");
965 if (personality)
967 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
968 ref = personality;
970 /* ??? The GAS support isn't entirely consistent. We have to
971 handle indirect support ourselves, but PC-relative is done
972 in the assembler. Further, the assembler can't handle any
973 of the weirder relocation types. */
974 if (enc & DW_EH_PE_indirect)
975 ref = dw2_force_const_mem (ref, true);
977 fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
978 output_addr_const (asm_out_file, ref);
979 fputc ('\n', asm_out_file);
982 if (crtl->uses_eh_lsda)
984 char lab[MAX_ARTIFICIAL_LABEL_BYTES];
986 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
987 ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
988 current_function_funcdef_no);
989 ref = gen_rtx_SYMBOL_REF (Pmode, lab);
990 SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
992 if (enc & DW_EH_PE_indirect)
993 ref = dw2_force_const_mem (ref, true);
995 fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
996 output_addr_const (asm_out_file, ref);
997 fputc ('\n', asm_out_file);
1001 /* Allocate CURRENT_FDE. Immediately initialize all we can, noting that
1002 this allocation may be done before pass_final. */
1004 dw_fde_ref
1005 dwarf2out_alloc_current_fde (void)
1007 dw_fde_ref fde;
1009 fde = ggc_cleared_alloc<dw_fde_node> ();
1010 fde->decl = current_function_decl;
1011 fde->funcdef_number = current_function_funcdef_no;
1012 fde->fde_index = vec_safe_length (fde_vec);
1013 fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
1014 fde->uses_eh_lsda = crtl->uses_eh_lsda;
1015 fde->nothrow = crtl->nothrow;
1016 fde->drap_reg = INVALID_REGNUM;
1017 fde->vdrap_reg = INVALID_REGNUM;
1019 /* Record the FDE associated with this function. */
1020 cfun->fde = fde;
1021 vec_safe_push (fde_vec, fde);
1023 return fde;
1026 /* Output a marker (i.e. a label) for the beginning of a function, before
1027 the prologue. */
1029 void
1030 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
1031 unsigned int column ATTRIBUTE_UNUSED,
1032 const char *file ATTRIBUTE_UNUSED)
1034 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1035 char * dup_label;
1036 dw_fde_ref fde;
1037 section *fnsec;
1038 bool do_frame;
1040 current_function_func_begin_label = NULL;
1042 do_frame = dwarf2out_do_frame ();
1044 /* ??? current_function_func_begin_label is also used by except.c for
1045 call-site information. We must emit this label if it might be used. */
1046 if (!do_frame
1047 && (!flag_exceptions
1048 || targetm_common.except_unwind_info (&global_options) == UI_SJLJ))
1049 return;
1051 fnsec = function_section (current_function_decl);
1052 switch_to_section (fnsec);
1053 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
1054 current_function_funcdef_no);
1055 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
1056 current_function_funcdef_no);
1057 dup_label = xstrdup (label);
1058 current_function_func_begin_label = dup_label;
1060 /* We can elide the fde allocation if we're not emitting debug info. */
1061 if (!do_frame)
1062 return;
1064 /* Cater to the various TARGET_ASM_OUTPUT_MI_THUNK implementations that
1065 emit insns as rtx but bypass the bulk of rest_of_compilation, which
1066 would include pass_dwarf2_frame. If we've not created the FDE yet,
1067 do so now. */
1068 fde = cfun->fde;
1069 if (fde == NULL)
1070 fde = dwarf2out_alloc_current_fde ();
1072 /* Initialize the bits of CURRENT_FDE that were not available earlier. */
1073 fde->dw_fde_begin = dup_label;
1074 fde->dw_fde_current_label = dup_label;
1075 fde->in_std_section = (fnsec == text_section
1076 || (cold_text_section && fnsec == cold_text_section));
1078 /* We only want to output line number information for the genuine dwarf2
1079 prologue case, not the eh frame case. */
1080 #ifdef DWARF2_DEBUGGING_INFO
1081 if (file)
1082 dwarf2out_source_line (line, column, file, 0, true);
1083 #endif
1085 if (dwarf2out_do_cfi_asm ())
1086 dwarf2out_do_cfi_startproc (false);
1087 else
1089 rtx personality = get_personality_function (current_function_decl);
1090 if (!current_unit_personality)
1091 current_unit_personality = personality;
1093 /* We cannot keep a current personality per function as without CFI
1094 asm, at the point where we emit the CFI data, there is no current
1095 function anymore. */
1096 if (personality && current_unit_personality != personality)
1097 sorry ("multiple EH personalities are supported only with assemblers "
1098 "supporting .cfi_personality directive");
1102 /* Output a marker (i.e. a label) for the end of the generated code
1103 for a function prologue. This gets called *after* the prologue code has
1104 been generated. */
1106 void
1107 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
1108 const char *file ATTRIBUTE_UNUSED)
1110 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1112 /* Output a label to mark the endpoint of the code generated for this
1113 function. */
1114 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
1115 current_function_funcdef_no);
1116 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
1117 current_function_funcdef_no);
1118 cfun->fde->dw_fde_vms_end_prologue = xstrdup (label);
1121 /* Output a marker (i.e. a label) for the beginning of the generated code
1122 for a function epilogue. This gets called *before* the prologue code has
1123 been generated. */
1125 void
1126 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1127 const char *file ATTRIBUTE_UNUSED)
1129 dw_fde_ref fde = cfun->fde;
1130 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1132 if (fde->dw_fde_vms_begin_epilogue)
1133 return;
1135 /* Output a label to mark the endpoint of the code generated for this
1136 function. */
1137 ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
1138 current_function_funcdef_no);
1139 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
1140 current_function_funcdef_no);
1141 fde->dw_fde_vms_begin_epilogue = xstrdup (label);
1144 /* Output a marker (i.e. a label) for the absolute end of the generated code
1145 for a function definition. This gets called *after* the epilogue code has
1146 been generated. */
1148 void
1149 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1150 const char *file ATTRIBUTE_UNUSED)
1152 dw_fde_ref fde;
1153 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1155 last_var_location_insn = NULL;
1156 cached_next_real_insn = NULL;
1158 if (dwarf2out_do_cfi_asm ())
1159 fprintf (asm_out_file, "\t.cfi_endproc\n");
1161 /* Output a label to mark the endpoint of the code generated for this
1162 function. */
1163 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
1164 current_function_funcdef_no);
1165 ASM_OUTPUT_LABEL (asm_out_file, label);
1166 fde = cfun->fde;
1167 gcc_assert (fde != NULL);
1168 if (fde->dw_fde_second_begin == NULL)
1169 fde->dw_fde_end = xstrdup (label);
1172 void
1173 dwarf2out_frame_finish (void)
1175 /* Output call frame information. */
1176 if (targetm.debug_unwind_info () == UI_DWARF2)
1177 output_call_frame_info (0);
1179 /* Output another copy for the unwinder. */
1180 if ((flag_unwind_tables || flag_exceptions)
1181 && targetm_common.except_unwind_info (&global_options) == UI_DWARF2)
1182 output_call_frame_info (1);
1185 /* Note that the current function section is being used for code. */
1187 static void
1188 dwarf2out_note_section_used (void)
1190 section *sec = current_function_section ();
1191 if (sec == text_section)
1192 text_section_used = true;
1193 else if (sec == cold_text_section)
1194 cold_text_section_used = true;
1197 static void var_location_switch_text_section (void);
1198 static void set_cur_line_info_table (section *);
1200 void
1201 dwarf2out_switch_text_section (void)
1203 section *sect;
1204 dw_fde_ref fde = cfun->fde;
1206 gcc_assert (cfun && fde && fde->dw_fde_second_begin == NULL);
1208 if (!in_cold_section_p)
1210 fde->dw_fde_end = crtl->subsections.cold_section_end_label;
1211 fde->dw_fde_second_begin = crtl->subsections.hot_section_label;
1212 fde->dw_fde_second_end = crtl->subsections.hot_section_end_label;
1214 else
1216 fde->dw_fde_end = crtl->subsections.hot_section_end_label;
1217 fde->dw_fde_second_begin = crtl->subsections.cold_section_label;
1218 fde->dw_fde_second_end = crtl->subsections.cold_section_end_label;
1220 have_multiple_function_sections = true;
1222 /* There is no need to mark used sections when not debugging. */
1223 if (cold_text_section != NULL)
1224 dwarf2out_note_section_used ();
1226 if (dwarf2out_do_cfi_asm ())
1227 fprintf (asm_out_file, "\t.cfi_endproc\n");
1229 /* Now do the real section switch. */
1230 sect = current_function_section ();
1231 switch_to_section (sect);
1233 fde->second_in_std_section
1234 = (sect == text_section
1235 || (cold_text_section && sect == cold_text_section));
1237 if (dwarf2out_do_cfi_asm ())
1238 dwarf2out_do_cfi_startproc (true);
1240 var_location_switch_text_section ();
1242 if (cold_text_section != NULL)
1243 set_cur_line_info_table (sect);
1246 /* And now, the subset of the debugging information support code necessary
1247 for emitting location expressions. */
1249 /* Data about a single source file. */
1250 struct GTY((for_user)) dwarf_file_data {
1251 const char * filename;
1252 int emitted_number;
1255 /* Describe an entry into the .debug_addr section. */
1257 enum ate_kind {
1258 ate_kind_rtx,
1259 ate_kind_rtx_dtprel,
1260 ate_kind_label
1263 struct GTY((for_user)) addr_table_entry {
1264 enum ate_kind kind;
1265 unsigned int refcount;
1266 unsigned int index;
1267 union addr_table_entry_struct_union
1269 rtx GTY ((tag ("0"))) rtl;
1270 char * GTY ((tag ("1"))) label;
1272 GTY ((desc ("%1.kind"))) addr;
1275 /* Location lists are ranges + location descriptions for that range,
1276 so you can track variables that are in different places over
1277 their entire life. */
1278 typedef struct GTY(()) dw_loc_list_struct {
1279 dw_loc_list_ref dw_loc_next;
1280 const char *begin; /* Label and addr_entry for start of range */
1281 addr_table_entry *begin_entry;
1282 const char *end; /* Label for end of range */
1283 char *ll_symbol; /* Label for beginning of location list.
1284 Only on head of list */
1285 const char *section; /* Section this loclist is relative to */
1286 dw_loc_descr_ref expr;
1287 hashval_t hash;
1288 /* True if all addresses in this and subsequent lists are known to be
1289 resolved. */
1290 bool resolved_addr;
1291 /* True if this list has been replaced by dw_loc_next. */
1292 bool replaced;
1293 /* True if it has been emitted into .debug_loc* / .debug_loclists*
1294 section. */
1295 unsigned char emitted : 1;
1296 /* True if hash field is index rather than hash value. */
1297 unsigned char num_assigned : 1;
1298 /* True if .debug_loclists.dwo offset has been emitted for it already. */
1299 unsigned char offset_emitted : 1;
1300 /* True if note_variable_value_in_expr has been called on it. */
1301 unsigned char noted_variable_value : 1;
1302 /* True if the range should be emitted even if begin and end
1303 are the same. */
1304 bool force;
1305 } dw_loc_list_node;
1307 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
1308 static dw_loc_descr_ref uint_loc_descriptor (unsigned HOST_WIDE_INT);
1310 /* Convert a DWARF stack opcode into its string name. */
1312 static const char *
1313 dwarf_stack_op_name (unsigned int op)
1315 const char *name = get_DW_OP_name (op);
1317 if (name != NULL)
1318 return name;
1320 return "OP_<unknown>";
1323 /* Return a pointer to a newly allocated location description. Location
1324 descriptions are simple expression terms that can be strung
1325 together to form more complicated location (address) descriptions. */
1327 static inline dw_loc_descr_ref
1328 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
1329 unsigned HOST_WIDE_INT oprnd2)
1331 dw_loc_descr_ref descr = ggc_cleared_alloc<dw_loc_descr_node> ();
1333 descr->dw_loc_opc = op;
1334 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
1335 descr->dw_loc_oprnd1.val_entry = NULL;
1336 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
1337 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
1338 descr->dw_loc_oprnd2.val_entry = NULL;
1339 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
1341 return descr;
1344 /* Return a pointer to a newly allocated location description for
1345 REG and OFFSET. */
1347 static inline dw_loc_descr_ref
1348 new_reg_loc_descr (unsigned int reg, unsigned HOST_WIDE_INT offset)
1350 if (reg <= 31)
1351 return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
1352 offset, 0);
1353 else
1354 return new_loc_descr (DW_OP_bregx, reg, offset);
1357 /* Add a location description term to a location description expression. */
1359 static inline void
1360 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
1362 dw_loc_descr_ref *d;
1364 /* Find the end of the chain. */
1365 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
1368 *d = descr;
1371 /* Compare two location operands for exact equality. */
1373 static bool
1374 dw_val_equal_p (dw_val_node *a, dw_val_node *b)
1376 if (a->val_class != b->val_class)
1377 return false;
1378 switch (a->val_class)
1380 case dw_val_class_none:
1381 return true;
1382 case dw_val_class_addr:
1383 return rtx_equal_p (a->v.val_addr, b->v.val_addr);
1385 case dw_val_class_offset:
1386 case dw_val_class_unsigned_const:
1387 case dw_val_class_const:
1388 case dw_val_class_unsigned_const_implicit:
1389 case dw_val_class_const_implicit:
1390 case dw_val_class_range_list:
1391 /* These are all HOST_WIDE_INT, signed or unsigned. */
1392 return a->v.val_unsigned == b->v.val_unsigned;
1394 case dw_val_class_loc:
1395 return a->v.val_loc == b->v.val_loc;
1396 case dw_val_class_loc_list:
1397 return a->v.val_loc_list == b->v.val_loc_list;
1398 case dw_val_class_die_ref:
1399 return a->v.val_die_ref.die == b->v.val_die_ref.die;
1400 case dw_val_class_fde_ref:
1401 return a->v.val_fde_index == b->v.val_fde_index;
1402 case dw_val_class_lbl_id:
1403 case dw_val_class_lineptr:
1404 case dw_val_class_macptr:
1405 case dw_val_class_loclistsptr:
1406 case dw_val_class_high_pc:
1407 return strcmp (a->v.val_lbl_id, b->v.val_lbl_id) == 0;
1408 case dw_val_class_str:
1409 return a->v.val_str == b->v.val_str;
1410 case dw_val_class_flag:
1411 return a->v.val_flag == b->v.val_flag;
1412 case dw_val_class_file:
1413 case dw_val_class_file_implicit:
1414 return a->v.val_file == b->v.val_file;
1415 case dw_val_class_decl_ref:
1416 return a->v.val_decl_ref == b->v.val_decl_ref;
1418 case dw_val_class_const_double:
1419 return (a->v.val_double.high == b->v.val_double.high
1420 && a->v.val_double.low == b->v.val_double.low);
1422 case dw_val_class_wide_int:
1423 return *a->v.val_wide == *b->v.val_wide;
1425 case dw_val_class_vec:
1427 size_t a_len = a->v.val_vec.elt_size * a->v.val_vec.length;
1428 size_t b_len = b->v.val_vec.elt_size * b->v.val_vec.length;
1430 return (a_len == b_len
1431 && !memcmp (a->v.val_vec.array, b->v.val_vec.array, a_len));
1434 case dw_val_class_data8:
1435 return memcmp (a->v.val_data8, b->v.val_data8, 8) == 0;
1437 case dw_val_class_vms_delta:
1438 return (!strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1)
1439 && !strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1));
1441 case dw_val_class_discr_value:
1442 return (a->v.val_discr_value.pos == b->v.val_discr_value.pos
1443 && a->v.val_discr_value.v.uval == b->v.val_discr_value.v.uval);
1444 case dw_val_class_discr_list:
1445 /* It makes no sense comparing two discriminant value lists. */
1446 return false;
1448 gcc_unreachable ();
1451 /* Compare two location atoms for exact equality. */
1453 static bool
1454 loc_descr_equal_p_1 (dw_loc_descr_ref a, dw_loc_descr_ref b)
1456 if (a->dw_loc_opc != b->dw_loc_opc)
1457 return false;
1459 /* ??? This is only ever set for DW_OP_constNu, for N equal to the
1460 address size, but since we always allocate cleared storage it
1461 should be zero for other types of locations. */
1462 if (a->dtprel != b->dtprel)
1463 return false;
1465 return (dw_val_equal_p (&a->dw_loc_oprnd1, &b->dw_loc_oprnd1)
1466 && dw_val_equal_p (&a->dw_loc_oprnd2, &b->dw_loc_oprnd2));
1469 /* Compare two complete location expressions for exact equality. */
1471 bool
1472 loc_descr_equal_p (dw_loc_descr_ref a, dw_loc_descr_ref b)
1474 while (1)
1476 if (a == b)
1477 return true;
1478 if (a == NULL || b == NULL)
1479 return false;
1480 if (!loc_descr_equal_p_1 (a, b))
1481 return false;
1483 a = a->dw_loc_next;
1484 b = b->dw_loc_next;
1489 /* Add a constant OFFSET to a location expression. */
1491 static void
1492 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
1494 dw_loc_descr_ref loc;
1495 HOST_WIDE_INT *p;
1497 gcc_assert (*list_head != NULL);
1499 if (!offset)
1500 return;
1502 /* Find the end of the chain. */
1503 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
1506 p = NULL;
1507 if (loc->dw_loc_opc == DW_OP_fbreg
1508 || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
1509 p = &loc->dw_loc_oprnd1.v.val_int;
1510 else if (loc->dw_loc_opc == DW_OP_bregx)
1511 p = &loc->dw_loc_oprnd2.v.val_int;
1513 /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
1514 offset. Don't optimize if an signed integer overflow would happen. */
1515 if (p != NULL
1516 && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
1517 || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
1518 *p += offset;
1520 else if (offset > 0)
1521 loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
1523 else
1525 loc->dw_loc_next
1526 = uint_loc_descriptor (-(unsigned HOST_WIDE_INT) offset);
1527 add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
1531 /* Add a constant OFFSET to a location list. */
1533 static void
1534 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
1536 dw_loc_list_ref d;
1537 for (d = list_head; d != NULL; d = d->dw_loc_next)
1538 loc_descr_plus_const (&d->expr, offset);
1541 #define DWARF_REF_SIZE \
1542 (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
1544 /* The number of bits that can be encoded by largest DW_FORM_dataN.
1545 In DWARF4 and earlier it is DW_FORM_data8 with 64 bits, in DWARF5
1546 DW_FORM_data16 with 128 bits. */
1547 #define DWARF_LARGEST_DATA_FORM_BITS \
1548 (dwarf_version >= 5 ? 128 : 64)
1550 /* Utility inline function for construction of ops that were GNU extension
1551 before DWARF 5. */
1552 static inline enum dwarf_location_atom
1553 dwarf_OP (enum dwarf_location_atom op)
1555 switch (op)
1557 case DW_OP_implicit_pointer:
1558 if (dwarf_version < 5)
1559 return DW_OP_GNU_implicit_pointer;
1560 break;
1562 case DW_OP_entry_value:
1563 if (dwarf_version < 5)
1564 return DW_OP_GNU_entry_value;
1565 break;
1567 case DW_OP_const_type:
1568 if (dwarf_version < 5)
1569 return DW_OP_GNU_const_type;
1570 break;
1572 case DW_OP_regval_type:
1573 if (dwarf_version < 5)
1574 return DW_OP_GNU_regval_type;
1575 break;
1577 case DW_OP_deref_type:
1578 if (dwarf_version < 5)
1579 return DW_OP_GNU_deref_type;
1580 break;
1582 case DW_OP_convert:
1583 if (dwarf_version < 5)
1584 return DW_OP_GNU_convert;
1585 break;
1587 case DW_OP_reinterpret:
1588 if (dwarf_version < 5)
1589 return DW_OP_GNU_reinterpret;
1590 break;
1592 default:
1593 break;
1595 return op;
1598 /* Similarly for attributes. */
1599 static inline enum dwarf_attribute
1600 dwarf_AT (enum dwarf_attribute at)
1602 switch (at)
1604 case DW_AT_call_return_pc:
1605 if (dwarf_version < 5)
1606 return DW_AT_low_pc;
1607 break;
1609 case DW_AT_call_tail_call:
1610 if (dwarf_version < 5)
1611 return DW_AT_GNU_tail_call;
1612 break;
1614 case DW_AT_call_origin:
1615 if (dwarf_version < 5)
1616 return DW_AT_abstract_origin;
1617 break;
1619 case DW_AT_call_target:
1620 if (dwarf_version < 5)
1621 return DW_AT_GNU_call_site_target;
1622 break;
1624 case DW_AT_call_target_clobbered:
1625 if (dwarf_version < 5)
1626 return DW_AT_GNU_call_site_target_clobbered;
1627 break;
1629 case DW_AT_call_parameter:
1630 if (dwarf_version < 5)
1631 return DW_AT_abstract_origin;
1632 break;
1634 case DW_AT_call_value:
1635 if (dwarf_version < 5)
1636 return DW_AT_GNU_call_site_value;
1637 break;
1639 case DW_AT_call_data_value:
1640 if (dwarf_version < 5)
1641 return DW_AT_GNU_call_site_data_value;
1642 break;
1644 case DW_AT_call_all_calls:
1645 if (dwarf_version < 5)
1646 return DW_AT_GNU_all_call_sites;
1647 break;
1649 case DW_AT_call_all_tail_calls:
1650 if (dwarf_version < 5)
1651 return DW_AT_GNU_all_tail_call_sites;
1652 break;
1654 case DW_AT_dwo_name:
1655 if (dwarf_version < 5)
1656 return DW_AT_GNU_dwo_name;
1657 break;
1659 default:
1660 break;
1662 return at;
1665 /* And similarly for tags. */
1666 static inline enum dwarf_tag
1667 dwarf_TAG (enum dwarf_tag tag)
1669 switch (tag)
1671 case DW_TAG_call_site:
1672 if (dwarf_version < 5)
1673 return DW_TAG_GNU_call_site;
1674 break;
1676 case DW_TAG_call_site_parameter:
1677 if (dwarf_version < 5)
1678 return DW_TAG_GNU_call_site_parameter;
1679 break;
1681 default:
1682 break;
1684 return tag;
1687 static unsigned long int get_base_type_offset (dw_die_ref);
1689 /* Return the size of a location descriptor. */
1691 static unsigned long
1692 size_of_loc_descr (dw_loc_descr_ref loc)
1694 unsigned long size = 1;
1696 switch (loc->dw_loc_opc)
1698 case DW_OP_addr:
1699 size += DWARF2_ADDR_SIZE;
1700 break;
1701 case DW_OP_GNU_addr_index:
1702 case DW_OP_GNU_const_index:
1703 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1704 size += size_of_uleb128 (loc->dw_loc_oprnd1.val_entry->index);
1705 break;
1706 case DW_OP_const1u:
1707 case DW_OP_const1s:
1708 size += 1;
1709 break;
1710 case DW_OP_const2u:
1711 case DW_OP_const2s:
1712 size += 2;
1713 break;
1714 case DW_OP_const4u:
1715 case DW_OP_const4s:
1716 size += 4;
1717 break;
1718 case DW_OP_const8u:
1719 case DW_OP_const8s:
1720 size += 8;
1721 break;
1722 case DW_OP_constu:
1723 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1724 break;
1725 case DW_OP_consts:
1726 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1727 break;
1728 case DW_OP_pick:
1729 size += 1;
1730 break;
1731 case DW_OP_plus_uconst:
1732 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1733 break;
1734 case DW_OP_skip:
1735 case DW_OP_bra:
1736 size += 2;
1737 break;
1738 case DW_OP_breg0:
1739 case DW_OP_breg1:
1740 case DW_OP_breg2:
1741 case DW_OP_breg3:
1742 case DW_OP_breg4:
1743 case DW_OP_breg5:
1744 case DW_OP_breg6:
1745 case DW_OP_breg7:
1746 case DW_OP_breg8:
1747 case DW_OP_breg9:
1748 case DW_OP_breg10:
1749 case DW_OP_breg11:
1750 case DW_OP_breg12:
1751 case DW_OP_breg13:
1752 case DW_OP_breg14:
1753 case DW_OP_breg15:
1754 case DW_OP_breg16:
1755 case DW_OP_breg17:
1756 case DW_OP_breg18:
1757 case DW_OP_breg19:
1758 case DW_OP_breg20:
1759 case DW_OP_breg21:
1760 case DW_OP_breg22:
1761 case DW_OP_breg23:
1762 case DW_OP_breg24:
1763 case DW_OP_breg25:
1764 case DW_OP_breg26:
1765 case DW_OP_breg27:
1766 case DW_OP_breg28:
1767 case DW_OP_breg29:
1768 case DW_OP_breg30:
1769 case DW_OP_breg31:
1770 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1771 break;
1772 case DW_OP_regx:
1773 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1774 break;
1775 case DW_OP_fbreg:
1776 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1777 break;
1778 case DW_OP_bregx:
1779 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1780 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1781 break;
1782 case DW_OP_piece:
1783 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1784 break;
1785 case DW_OP_bit_piece:
1786 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1787 size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
1788 break;
1789 case DW_OP_deref_size:
1790 case DW_OP_xderef_size:
1791 size += 1;
1792 break;
1793 case DW_OP_call2:
1794 size += 2;
1795 break;
1796 case DW_OP_call4:
1797 size += 4;
1798 break;
1799 case DW_OP_call_ref:
1800 case DW_OP_GNU_variable_value:
1801 size += DWARF_REF_SIZE;
1802 break;
1803 case DW_OP_implicit_value:
1804 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1805 + loc->dw_loc_oprnd1.v.val_unsigned;
1806 break;
1807 case DW_OP_implicit_pointer:
1808 case DW_OP_GNU_implicit_pointer:
1809 size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1810 break;
1811 case DW_OP_entry_value:
1812 case DW_OP_GNU_entry_value:
1814 unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
1815 size += size_of_uleb128 (op_size) + op_size;
1816 break;
1818 case DW_OP_const_type:
1819 case DW_OP_GNU_const_type:
1821 unsigned long o
1822 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1823 size += size_of_uleb128 (o) + 1;
1824 switch (loc->dw_loc_oprnd2.val_class)
1826 case dw_val_class_vec:
1827 size += loc->dw_loc_oprnd2.v.val_vec.length
1828 * loc->dw_loc_oprnd2.v.val_vec.elt_size;
1829 break;
1830 case dw_val_class_const:
1831 size += HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
1832 break;
1833 case dw_val_class_const_double:
1834 size += HOST_BITS_PER_DOUBLE_INT / BITS_PER_UNIT;
1835 break;
1836 case dw_val_class_wide_int:
1837 size += (get_full_len (*loc->dw_loc_oprnd2.v.val_wide)
1838 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
1839 break;
1840 default:
1841 gcc_unreachable ();
1843 break;
1845 case DW_OP_regval_type:
1846 case DW_OP_GNU_regval_type:
1848 unsigned long o
1849 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1850 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1851 + size_of_uleb128 (o);
1853 break;
1854 case DW_OP_deref_type:
1855 case DW_OP_GNU_deref_type:
1857 unsigned long o
1858 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1859 size += 1 + size_of_uleb128 (o);
1861 break;
1862 case DW_OP_convert:
1863 case DW_OP_reinterpret:
1864 case DW_OP_GNU_convert:
1865 case DW_OP_GNU_reinterpret:
1866 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
1867 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1868 else
1870 unsigned long o
1871 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1872 size += size_of_uleb128 (o);
1874 break;
1875 case DW_OP_GNU_parameter_ref:
1876 size += 4;
1877 break;
1878 default:
1879 break;
1882 return size;
1885 /* Return the size of a series of location descriptors. */
1887 unsigned long
1888 size_of_locs (dw_loc_descr_ref loc)
1890 dw_loc_descr_ref l;
1891 unsigned long size;
1893 /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
1894 field, to avoid writing to a PCH file. */
1895 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1897 if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
1898 break;
1899 size += size_of_loc_descr (l);
1901 if (! l)
1902 return size;
1904 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1906 l->dw_loc_addr = size;
1907 size += size_of_loc_descr (l);
1910 return size;
1913 /* Return the size of the value in a DW_AT_discr_value attribute. */
1915 static int
1916 size_of_discr_value (dw_discr_value *discr_value)
1918 if (discr_value->pos)
1919 return size_of_uleb128 (discr_value->v.uval);
1920 else
1921 return size_of_sleb128 (discr_value->v.sval);
1924 /* Return the size of the value in a DW_AT_discr_list attribute. */
1926 static int
1927 size_of_discr_list (dw_discr_list_ref discr_list)
1929 int size = 0;
1931 for (dw_discr_list_ref list = discr_list;
1932 list != NULL;
1933 list = list->dw_discr_next)
1935 /* One byte for the discriminant value descriptor, and then one or two
1936 LEB128 numbers, depending on whether it's a single case label or a
1937 range label. */
1938 size += 1;
1939 size += size_of_discr_value (&list->dw_discr_lower_bound);
1940 if (list->dw_discr_range != 0)
1941 size += size_of_discr_value (&list->dw_discr_upper_bound);
1943 return size;
1946 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
1947 static void get_ref_die_offset_label (char *, dw_die_ref);
1948 static unsigned long int get_ref_die_offset (dw_die_ref);
1950 /* Output location description stack opcode's operands (if any).
1951 The for_eh_or_skip parameter controls whether register numbers are
1952 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
1953 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
1954 info). This should be suppressed for the cases that have not been converted
1955 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
1957 static void
1958 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
1960 dw_val_ref val1 = &loc->dw_loc_oprnd1;
1961 dw_val_ref val2 = &loc->dw_loc_oprnd2;
1963 switch (loc->dw_loc_opc)
1965 #ifdef DWARF2_DEBUGGING_INFO
1966 case DW_OP_const2u:
1967 case DW_OP_const2s:
1968 dw2_asm_output_data (2, val1->v.val_int, NULL);
1969 break;
1970 case DW_OP_const4u:
1971 if (loc->dtprel)
1973 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1974 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
1975 val1->v.val_addr);
1976 fputc ('\n', asm_out_file);
1977 break;
1979 /* FALLTHRU */
1980 case DW_OP_const4s:
1981 dw2_asm_output_data (4, val1->v.val_int, NULL);
1982 break;
1983 case DW_OP_const8u:
1984 if (loc->dtprel)
1986 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1987 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
1988 val1->v.val_addr);
1989 fputc ('\n', asm_out_file);
1990 break;
1992 /* FALLTHRU */
1993 case DW_OP_const8s:
1994 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
1995 dw2_asm_output_data (8, val1->v.val_int, NULL);
1996 break;
1997 case DW_OP_skip:
1998 case DW_OP_bra:
2000 int offset;
2002 gcc_assert (val1->val_class == dw_val_class_loc);
2003 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2005 dw2_asm_output_data (2, offset, NULL);
2007 break;
2008 case DW_OP_implicit_value:
2009 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2010 switch (val2->val_class)
2012 case dw_val_class_const:
2013 dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
2014 break;
2015 case dw_val_class_vec:
2017 unsigned int elt_size = val2->v.val_vec.elt_size;
2018 unsigned int len = val2->v.val_vec.length;
2019 unsigned int i;
2020 unsigned char *p;
2022 if (elt_size > sizeof (HOST_WIDE_INT))
2024 elt_size /= 2;
2025 len *= 2;
2027 for (i = 0, p = (unsigned char *) val2->v.val_vec.array;
2028 i < len;
2029 i++, p += elt_size)
2030 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2031 "fp or vector constant word %u", i);
2033 break;
2034 case dw_val_class_const_double:
2036 unsigned HOST_WIDE_INT first, second;
2038 if (WORDS_BIG_ENDIAN)
2040 first = val2->v.val_double.high;
2041 second = val2->v.val_double.low;
2043 else
2045 first = val2->v.val_double.low;
2046 second = val2->v.val_double.high;
2048 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2049 first, NULL);
2050 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2051 second, NULL);
2053 break;
2054 case dw_val_class_wide_int:
2056 int i;
2057 int len = get_full_len (*val2->v.val_wide);
2058 if (WORDS_BIG_ENDIAN)
2059 for (i = len - 1; i >= 0; --i)
2060 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2061 val2->v.val_wide->elt (i), NULL);
2062 else
2063 for (i = 0; i < len; ++i)
2064 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2065 val2->v.val_wide->elt (i), NULL);
2067 break;
2068 case dw_val_class_addr:
2069 gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
2070 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
2071 break;
2072 default:
2073 gcc_unreachable ();
2075 break;
2076 #else
2077 case DW_OP_const2u:
2078 case DW_OP_const2s:
2079 case DW_OP_const4u:
2080 case DW_OP_const4s:
2081 case DW_OP_const8u:
2082 case DW_OP_const8s:
2083 case DW_OP_skip:
2084 case DW_OP_bra:
2085 case DW_OP_implicit_value:
2086 /* We currently don't make any attempt to make sure these are
2087 aligned properly like we do for the main unwind info, so
2088 don't support emitting things larger than a byte if we're
2089 only doing unwinding. */
2090 gcc_unreachable ();
2091 #endif
2092 case DW_OP_const1u:
2093 case DW_OP_const1s:
2094 dw2_asm_output_data (1, val1->v.val_int, NULL);
2095 break;
2096 case DW_OP_constu:
2097 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2098 break;
2099 case DW_OP_consts:
2100 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2101 break;
2102 case DW_OP_pick:
2103 dw2_asm_output_data (1, val1->v.val_int, NULL);
2104 break;
2105 case DW_OP_plus_uconst:
2106 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2107 break;
2108 case DW_OP_breg0:
2109 case DW_OP_breg1:
2110 case DW_OP_breg2:
2111 case DW_OP_breg3:
2112 case DW_OP_breg4:
2113 case DW_OP_breg5:
2114 case DW_OP_breg6:
2115 case DW_OP_breg7:
2116 case DW_OP_breg8:
2117 case DW_OP_breg9:
2118 case DW_OP_breg10:
2119 case DW_OP_breg11:
2120 case DW_OP_breg12:
2121 case DW_OP_breg13:
2122 case DW_OP_breg14:
2123 case DW_OP_breg15:
2124 case DW_OP_breg16:
2125 case DW_OP_breg17:
2126 case DW_OP_breg18:
2127 case DW_OP_breg19:
2128 case DW_OP_breg20:
2129 case DW_OP_breg21:
2130 case DW_OP_breg22:
2131 case DW_OP_breg23:
2132 case DW_OP_breg24:
2133 case DW_OP_breg25:
2134 case DW_OP_breg26:
2135 case DW_OP_breg27:
2136 case DW_OP_breg28:
2137 case DW_OP_breg29:
2138 case DW_OP_breg30:
2139 case DW_OP_breg31:
2140 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2141 break;
2142 case DW_OP_regx:
2144 unsigned r = val1->v.val_unsigned;
2145 if (for_eh_or_skip >= 0)
2146 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2147 gcc_assert (size_of_uleb128 (r)
2148 == size_of_uleb128 (val1->v.val_unsigned));
2149 dw2_asm_output_data_uleb128 (r, NULL);
2151 break;
2152 case DW_OP_fbreg:
2153 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2154 break;
2155 case DW_OP_bregx:
2157 unsigned r = val1->v.val_unsigned;
2158 if (for_eh_or_skip >= 0)
2159 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2160 gcc_assert (size_of_uleb128 (r)
2161 == size_of_uleb128 (val1->v.val_unsigned));
2162 dw2_asm_output_data_uleb128 (r, NULL);
2163 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2165 break;
2166 case DW_OP_piece:
2167 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2168 break;
2169 case DW_OP_bit_piece:
2170 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2171 dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
2172 break;
2173 case DW_OP_deref_size:
2174 case DW_OP_xderef_size:
2175 dw2_asm_output_data (1, val1->v.val_int, NULL);
2176 break;
2178 case DW_OP_addr:
2179 if (loc->dtprel)
2181 if (targetm.asm_out.output_dwarf_dtprel)
2183 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
2184 DWARF2_ADDR_SIZE,
2185 val1->v.val_addr);
2186 fputc ('\n', asm_out_file);
2188 else
2189 gcc_unreachable ();
2191 else
2193 #ifdef DWARF2_DEBUGGING_INFO
2194 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
2195 #else
2196 gcc_unreachable ();
2197 #endif
2199 break;
2201 case DW_OP_GNU_addr_index:
2202 case DW_OP_GNU_const_index:
2203 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
2204 dw2_asm_output_data_uleb128 (loc->dw_loc_oprnd1.val_entry->index,
2205 "(index into .debug_addr)");
2206 break;
2208 case DW_OP_call2:
2209 case DW_OP_call4:
2211 unsigned long die_offset
2212 = get_ref_die_offset (val1->v.val_die_ref.die);
2213 /* Make sure the offset has been computed and that we can encode it as
2214 an operand. */
2215 gcc_assert (die_offset > 0
2216 && die_offset <= (loc->dw_loc_opc == DW_OP_call2
2217 ? 0xffff
2218 : 0xffffffff));
2219 dw2_asm_output_data ((loc->dw_loc_opc == DW_OP_call2) ? 2 : 4,
2220 die_offset, NULL);
2222 break;
2224 case DW_OP_call_ref:
2225 case DW_OP_GNU_variable_value:
2227 char label[MAX_ARTIFICIAL_LABEL_BYTES
2228 + HOST_BITS_PER_WIDE_INT / 2 + 2];
2229 gcc_assert (val1->val_class == dw_val_class_die_ref);
2230 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2231 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2233 break;
2235 case DW_OP_implicit_pointer:
2236 case DW_OP_GNU_implicit_pointer:
2238 char label[MAX_ARTIFICIAL_LABEL_BYTES
2239 + HOST_BITS_PER_WIDE_INT / 2 + 2];
2240 gcc_assert (val1->val_class == dw_val_class_die_ref);
2241 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2242 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2243 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2245 break;
2247 case DW_OP_entry_value:
2248 case DW_OP_GNU_entry_value:
2249 dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
2250 output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
2251 break;
2253 case DW_OP_const_type:
2254 case DW_OP_GNU_const_type:
2256 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
2257 gcc_assert (o);
2258 dw2_asm_output_data_uleb128 (o, NULL);
2259 switch (val2->val_class)
2261 case dw_val_class_const:
2262 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2263 dw2_asm_output_data (1, l, NULL);
2264 dw2_asm_output_data (l, val2->v.val_int, NULL);
2265 break;
2266 case dw_val_class_vec:
2268 unsigned int elt_size = val2->v.val_vec.elt_size;
2269 unsigned int len = val2->v.val_vec.length;
2270 unsigned int i;
2271 unsigned char *p;
2273 l = len * elt_size;
2274 dw2_asm_output_data (1, l, NULL);
2275 if (elt_size > sizeof (HOST_WIDE_INT))
2277 elt_size /= 2;
2278 len *= 2;
2280 for (i = 0, p = (unsigned char *) val2->v.val_vec.array;
2281 i < len;
2282 i++, p += elt_size)
2283 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2284 "fp or vector constant word %u", i);
2286 break;
2287 case dw_val_class_const_double:
2289 unsigned HOST_WIDE_INT first, second;
2290 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2292 dw2_asm_output_data (1, 2 * l, NULL);
2293 if (WORDS_BIG_ENDIAN)
2295 first = val2->v.val_double.high;
2296 second = val2->v.val_double.low;
2298 else
2300 first = val2->v.val_double.low;
2301 second = val2->v.val_double.high;
2303 dw2_asm_output_data (l, first, NULL);
2304 dw2_asm_output_data (l, second, NULL);
2306 break;
2307 case dw_val_class_wide_int:
2309 int i;
2310 int len = get_full_len (*val2->v.val_wide);
2311 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2313 dw2_asm_output_data (1, len * l, NULL);
2314 if (WORDS_BIG_ENDIAN)
2315 for (i = len - 1; i >= 0; --i)
2316 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2317 else
2318 for (i = 0; i < len; ++i)
2319 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2321 break;
2322 default:
2323 gcc_unreachable ();
2326 break;
2327 case DW_OP_regval_type:
2328 case DW_OP_GNU_regval_type:
2330 unsigned r = val1->v.val_unsigned;
2331 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2332 gcc_assert (o);
2333 if (for_eh_or_skip >= 0)
2335 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2336 gcc_assert (size_of_uleb128 (r)
2337 == size_of_uleb128 (val1->v.val_unsigned));
2339 dw2_asm_output_data_uleb128 (r, NULL);
2340 dw2_asm_output_data_uleb128 (o, NULL);
2342 break;
2343 case DW_OP_deref_type:
2344 case DW_OP_GNU_deref_type:
2346 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2347 gcc_assert (o);
2348 dw2_asm_output_data (1, val1->v.val_int, NULL);
2349 dw2_asm_output_data_uleb128 (o, NULL);
2351 break;
2352 case DW_OP_convert:
2353 case DW_OP_reinterpret:
2354 case DW_OP_GNU_convert:
2355 case DW_OP_GNU_reinterpret:
2356 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2357 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2358 else
2360 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
2361 gcc_assert (o);
2362 dw2_asm_output_data_uleb128 (o, NULL);
2364 break;
2366 case DW_OP_GNU_parameter_ref:
2368 unsigned long o;
2369 gcc_assert (val1->val_class == dw_val_class_die_ref);
2370 o = get_ref_die_offset (val1->v.val_die_ref.die);
2371 dw2_asm_output_data (4, o, NULL);
2373 break;
2375 default:
2376 /* Other codes have no operands. */
2377 break;
2381 /* Output a sequence of location operations.
2382 The for_eh_or_skip parameter controls whether register numbers are
2383 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2384 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2385 info). This should be suppressed for the cases that have not been converted
2386 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
2388 void
2389 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
2391 for (; loc != NULL; loc = loc->dw_loc_next)
2393 enum dwarf_location_atom opc = loc->dw_loc_opc;
2394 /* Output the opcode. */
2395 if (for_eh_or_skip >= 0
2396 && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2398 unsigned r = (opc - DW_OP_breg0);
2399 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2400 gcc_assert (r <= 31);
2401 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2403 else if (for_eh_or_skip >= 0
2404 && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2406 unsigned r = (opc - DW_OP_reg0);
2407 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2408 gcc_assert (r <= 31);
2409 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2412 dw2_asm_output_data (1, opc,
2413 "%s", dwarf_stack_op_name (opc));
2415 /* Output the operand(s) (if any). */
2416 output_loc_operands (loc, for_eh_or_skip);
2420 /* Output location description stack opcode's operands (if any).
2421 The output is single bytes on a line, suitable for .cfi_escape. */
2423 static void
2424 output_loc_operands_raw (dw_loc_descr_ref loc)
2426 dw_val_ref val1 = &loc->dw_loc_oprnd1;
2427 dw_val_ref val2 = &loc->dw_loc_oprnd2;
2429 switch (loc->dw_loc_opc)
2431 case DW_OP_addr:
2432 case DW_OP_GNU_addr_index:
2433 case DW_OP_GNU_const_index:
2434 case DW_OP_implicit_value:
2435 /* We cannot output addresses in .cfi_escape, only bytes. */
2436 gcc_unreachable ();
2438 case DW_OP_const1u:
2439 case DW_OP_const1s:
2440 case DW_OP_pick:
2441 case DW_OP_deref_size:
2442 case DW_OP_xderef_size:
2443 fputc (',', asm_out_file);
2444 dw2_asm_output_data_raw (1, val1->v.val_int);
2445 break;
2447 case DW_OP_const2u:
2448 case DW_OP_const2s:
2449 fputc (',', asm_out_file);
2450 dw2_asm_output_data_raw (2, val1->v.val_int);
2451 break;
2453 case DW_OP_const4u:
2454 case DW_OP_const4s:
2455 fputc (',', asm_out_file);
2456 dw2_asm_output_data_raw (4, val1->v.val_int);
2457 break;
2459 case DW_OP_const8u:
2460 case DW_OP_const8s:
2461 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2462 fputc (',', asm_out_file);
2463 dw2_asm_output_data_raw (8, val1->v.val_int);
2464 break;
2466 case DW_OP_skip:
2467 case DW_OP_bra:
2469 int offset;
2471 gcc_assert (val1->val_class == dw_val_class_loc);
2472 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2474 fputc (',', asm_out_file);
2475 dw2_asm_output_data_raw (2, offset);
2477 break;
2479 case DW_OP_regx:
2481 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2482 gcc_assert (size_of_uleb128 (r)
2483 == size_of_uleb128 (val1->v.val_unsigned));
2484 fputc (',', asm_out_file);
2485 dw2_asm_output_data_uleb128_raw (r);
2487 break;
2489 case DW_OP_constu:
2490 case DW_OP_plus_uconst:
2491 case DW_OP_piece:
2492 fputc (',', asm_out_file);
2493 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2494 break;
2496 case DW_OP_bit_piece:
2497 fputc (',', asm_out_file);
2498 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2499 dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
2500 break;
2502 case DW_OP_consts:
2503 case DW_OP_breg0:
2504 case DW_OP_breg1:
2505 case DW_OP_breg2:
2506 case DW_OP_breg3:
2507 case DW_OP_breg4:
2508 case DW_OP_breg5:
2509 case DW_OP_breg6:
2510 case DW_OP_breg7:
2511 case DW_OP_breg8:
2512 case DW_OP_breg9:
2513 case DW_OP_breg10:
2514 case DW_OP_breg11:
2515 case DW_OP_breg12:
2516 case DW_OP_breg13:
2517 case DW_OP_breg14:
2518 case DW_OP_breg15:
2519 case DW_OP_breg16:
2520 case DW_OP_breg17:
2521 case DW_OP_breg18:
2522 case DW_OP_breg19:
2523 case DW_OP_breg20:
2524 case DW_OP_breg21:
2525 case DW_OP_breg22:
2526 case DW_OP_breg23:
2527 case DW_OP_breg24:
2528 case DW_OP_breg25:
2529 case DW_OP_breg26:
2530 case DW_OP_breg27:
2531 case DW_OP_breg28:
2532 case DW_OP_breg29:
2533 case DW_OP_breg30:
2534 case DW_OP_breg31:
2535 case DW_OP_fbreg:
2536 fputc (',', asm_out_file);
2537 dw2_asm_output_data_sleb128_raw (val1->v.val_int);
2538 break;
2540 case DW_OP_bregx:
2542 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2543 gcc_assert (size_of_uleb128 (r)
2544 == size_of_uleb128 (val1->v.val_unsigned));
2545 fputc (',', asm_out_file);
2546 dw2_asm_output_data_uleb128_raw (r);
2547 fputc (',', asm_out_file);
2548 dw2_asm_output_data_sleb128_raw (val2->v.val_int);
2550 break;
2552 case DW_OP_implicit_pointer:
2553 case DW_OP_entry_value:
2554 case DW_OP_const_type:
2555 case DW_OP_regval_type:
2556 case DW_OP_deref_type:
2557 case DW_OP_convert:
2558 case DW_OP_reinterpret:
2559 case DW_OP_GNU_implicit_pointer:
2560 case DW_OP_GNU_entry_value:
2561 case DW_OP_GNU_const_type:
2562 case DW_OP_GNU_regval_type:
2563 case DW_OP_GNU_deref_type:
2564 case DW_OP_GNU_convert:
2565 case DW_OP_GNU_reinterpret:
2566 case DW_OP_GNU_parameter_ref:
2567 gcc_unreachable ();
2568 break;
2570 default:
2571 /* Other codes have no operands. */
2572 break;
2576 void
2577 output_loc_sequence_raw (dw_loc_descr_ref loc)
2579 while (1)
2581 enum dwarf_location_atom opc = loc->dw_loc_opc;
2582 /* Output the opcode. */
2583 if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2585 unsigned r = (opc - DW_OP_breg0);
2586 r = DWARF2_FRAME_REG_OUT (r, 1);
2587 gcc_assert (r <= 31);
2588 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2590 else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2592 unsigned r = (opc - DW_OP_reg0);
2593 r = DWARF2_FRAME_REG_OUT (r, 1);
2594 gcc_assert (r <= 31);
2595 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2597 /* Output the opcode. */
2598 fprintf (asm_out_file, "%#x", opc);
2599 output_loc_operands_raw (loc);
2601 if (!loc->dw_loc_next)
2602 break;
2603 loc = loc->dw_loc_next;
2605 fputc (',', asm_out_file);
2609 /* This function builds a dwarf location descriptor sequence from a
2610 dw_cfa_location, adding the given OFFSET to the result of the
2611 expression. */
2613 struct dw_loc_descr_node *
2614 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
2616 struct dw_loc_descr_node *head, *tmp;
2618 offset += cfa->offset;
2620 if (cfa->indirect)
2622 head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
2623 head->dw_loc_oprnd1.val_class = dw_val_class_const;
2624 head->dw_loc_oprnd1.val_entry = NULL;
2625 tmp = new_loc_descr (DW_OP_deref, 0, 0);
2626 add_loc_descr (&head, tmp);
2627 if (offset != 0)
2629 tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
2630 add_loc_descr (&head, tmp);
2633 else
2634 head = new_reg_loc_descr (cfa->reg, offset);
2636 return head;
2639 /* This function builds a dwarf location descriptor sequence for
2640 the address at OFFSET from the CFA when stack is aligned to
2641 ALIGNMENT byte. */
2643 struct dw_loc_descr_node *
2644 build_cfa_aligned_loc (dw_cfa_location *cfa,
2645 HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
2647 struct dw_loc_descr_node *head;
2648 unsigned int dwarf_fp
2649 = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
2651 /* When CFA is defined as FP+OFFSET, emulate stack alignment. */
2652 if (cfa->reg == HARD_FRAME_POINTER_REGNUM && cfa->indirect == 0)
2654 head = new_reg_loc_descr (dwarf_fp, 0);
2655 add_loc_descr (&head, int_loc_descriptor (alignment));
2656 add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
2657 loc_descr_plus_const (&head, offset);
2659 else
2660 head = new_reg_loc_descr (dwarf_fp, offset);
2661 return head;
2664 /* And now, the support for symbolic debugging information. */
2666 /* .debug_str support. */
2668 static void dwarf2out_init (const char *);
2669 static void dwarf2out_finish (const char *);
2670 static void dwarf2out_early_finish (const char *);
2671 static void dwarf2out_assembly_start (void);
2672 static void dwarf2out_define (unsigned int, const char *);
2673 static void dwarf2out_undef (unsigned int, const char *);
2674 static void dwarf2out_start_source_file (unsigned, const char *);
2675 static void dwarf2out_end_source_file (unsigned);
2676 static void dwarf2out_function_decl (tree);
2677 static void dwarf2out_begin_block (unsigned, unsigned);
2678 static void dwarf2out_end_block (unsigned, unsigned);
2679 static bool dwarf2out_ignore_block (const_tree);
2680 static void dwarf2out_early_global_decl (tree);
2681 static void dwarf2out_late_global_decl (tree);
2682 static void dwarf2out_type_decl (tree, int);
2683 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
2684 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
2685 dw_die_ref);
2686 static void dwarf2out_abstract_function (tree);
2687 static void dwarf2out_var_location (rtx_insn *);
2688 static void dwarf2out_size_function (tree);
2689 static void dwarf2out_begin_function (tree);
2690 static void dwarf2out_end_function (unsigned int);
2691 static void dwarf2out_register_main_translation_unit (tree unit);
2692 static void dwarf2out_set_name (tree, tree);
2694 /* The debug hooks structure. */
2696 const struct gcc_debug_hooks dwarf2_debug_hooks =
2698 dwarf2out_init,
2699 dwarf2out_finish,
2700 dwarf2out_early_finish,
2701 dwarf2out_assembly_start,
2702 dwarf2out_define,
2703 dwarf2out_undef,
2704 dwarf2out_start_source_file,
2705 dwarf2out_end_source_file,
2706 dwarf2out_begin_block,
2707 dwarf2out_end_block,
2708 dwarf2out_ignore_block,
2709 dwarf2out_source_line,
2710 dwarf2out_begin_prologue,
2711 #if VMS_DEBUGGING_INFO
2712 dwarf2out_vms_end_prologue,
2713 dwarf2out_vms_begin_epilogue,
2714 #else
2715 debug_nothing_int_charstar,
2716 debug_nothing_int_charstar,
2717 #endif
2718 dwarf2out_end_epilogue,
2719 dwarf2out_begin_function,
2720 dwarf2out_end_function, /* end_function */
2721 dwarf2out_register_main_translation_unit,
2722 dwarf2out_function_decl, /* function_decl */
2723 dwarf2out_early_global_decl,
2724 dwarf2out_late_global_decl,
2725 dwarf2out_type_decl, /* type_decl */
2726 dwarf2out_imported_module_or_decl,
2727 debug_nothing_tree, /* deferred_inline_function */
2728 /* The DWARF 2 backend tries to reduce debugging bloat by not
2729 emitting the abstract description of inline functions until
2730 something tries to reference them. */
2731 dwarf2out_abstract_function, /* outlining_inline_function */
2732 debug_nothing_rtx_code_label, /* label */
2733 debug_nothing_int, /* handle_pch */
2734 dwarf2out_var_location,
2735 dwarf2out_size_function, /* size_function */
2736 dwarf2out_switch_text_section,
2737 dwarf2out_set_name,
2738 1, /* start_end_main_source_file */
2739 TYPE_SYMTAB_IS_DIE /* tree_type_symtab_field */
2742 const struct gcc_debug_hooks dwarf2_lineno_debug_hooks =
2744 dwarf2out_init,
2745 debug_nothing_charstar,
2746 debug_nothing_charstar,
2747 dwarf2out_assembly_start,
2748 debug_nothing_int_charstar,
2749 debug_nothing_int_charstar,
2750 debug_nothing_int_charstar,
2751 debug_nothing_int,
2752 debug_nothing_int_int, /* begin_block */
2753 debug_nothing_int_int, /* end_block */
2754 debug_true_const_tree, /* ignore_block */
2755 dwarf2out_source_line, /* source_line */
2756 debug_nothing_int_int_charstar, /* begin_prologue */
2757 debug_nothing_int_charstar, /* end_prologue */
2758 debug_nothing_int_charstar, /* begin_epilogue */
2759 debug_nothing_int_charstar, /* end_epilogue */
2760 debug_nothing_tree, /* begin_function */
2761 debug_nothing_int, /* end_function */
2762 debug_nothing_tree, /* register_main_translation_unit */
2763 debug_nothing_tree, /* function_decl */
2764 debug_nothing_tree, /* early_global_decl */
2765 debug_nothing_tree, /* late_global_decl */
2766 debug_nothing_tree_int, /* type_decl */
2767 debug_nothing_tree_tree_tree_bool, /* imported_module_or_decl */
2768 debug_nothing_tree, /* deferred_inline_function */
2769 debug_nothing_tree, /* outlining_inline_function */
2770 debug_nothing_rtx_code_label, /* label */
2771 debug_nothing_int, /* handle_pch */
2772 debug_nothing_rtx_insn, /* var_location */
2773 debug_nothing_tree, /* size_function */
2774 debug_nothing_void, /* switch_text_section */
2775 debug_nothing_tree_tree, /* set_name */
2776 0, /* start_end_main_source_file */
2777 TYPE_SYMTAB_IS_ADDRESS /* tree_type_symtab_field */
2780 /* NOTE: In the comments in this file, many references are made to
2781 "Debugging Information Entries". This term is abbreviated as `DIE'
2782 throughout the remainder of this file. */
2784 /* An internal representation of the DWARF output is built, and then
2785 walked to generate the DWARF debugging info. The walk of the internal
2786 representation is done after the entire program has been compiled.
2787 The types below are used to describe the internal representation. */
2789 /* Whether to put type DIEs into their own section .debug_types instead
2790 of making them part of the .debug_info section. Only supported for
2791 Dwarf V4 or higher and the user didn't disable them through
2792 -fno-debug-types-section. It is more efficient to put them in a
2793 separate comdat sections since the linker will then be able to
2794 remove duplicates. But not all tools support .debug_types sections
2795 yet. For Dwarf V5 or higher .debug_types doesn't exist any more,
2796 it is DW_UT_type unit type in .debug_info section. */
2798 #define use_debug_types (dwarf_version >= 4 && flag_debug_types_section)
2800 /* Various DIE's use offsets relative to the beginning of the
2801 .debug_info section to refer to each other. */
2803 typedef long int dw_offset;
2805 struct comdat_type_node;
2807 /* The entries in the line_info table more-or-less mirror the opcodes
2808 that are used in the real dwarf line table. Arrays of these entries
2809 are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
2810 supported. */
2812 enum dw_line_info_opcode {
2813 /* Emit DW_LNE_set_address; the operand is the label index. */
2814 LI_set_address,
2816 /* Emit a row to the matrix with the given line. This may be done
2817 via any combination of DW_LNS_copy, DW_LNS_advance_line, and
2818 special opcodes. */
2819 LI_set_line,
2821 /* Emit a DW_LNS_set_file. */
2822 LI_set_file,
2824 /* Emit a DW_LNS_set_column. */
2825 LI_set_column,
2827 /* Emit a DW_LNS_negate_stmt; the operand is ignored. */
2828 LI_negate_stmt,
2830 /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored. */
2831 LI_set_prologue_end,
2832 LI_set_epilogue_begin,
2834 /* Emit a DW_LNE_set_discriminator. */
2835 LI_set_discriminator
2838 typedef struct GTY(()) dw_line_info_struct {
2839 enum dw_line_info_opcode opcode;
2840 unsigned int val;
2841 } dw_line_info_entry;
2844 struct GTY(()) dw_line_info_table {
2845 /* The label that marks the end of this section. */
2846 const char *end_label;
2848 /* The values for the last row of the matrix, as collected in the table.
2849 These are used to minimize the changes to the next row. */
2850 unsigned int file_num;
2851 unsigned int line_num;
2852 unsigned int column_num;
2853 int discrim_num;
2854 bool is_stmt;
2855 bool in_use;
2857 vec<dw_line_info_entry, va_gc> *entries;
2861 /* Each DIE attribute has a field specifying the attribute kind,
2862 a link to the next attribute in the chain, and an attribute value.
2863 Attributes are typically linked below the DIE they modify. */
2865 typedef struct GTY(()) dw_attr_struct {
2866 enum dwarf_attribute dw_attr;
2867 dw_val_node dw_attr_val;
2869 dw_attr_node;
2872 /* The Debugging Information Entry (DIE) structure. DIEs form a tree.
2873 The children of each node form a circular list linked by
2874 die_sib. die_child points to the node *before* the "first" child node. */
2876 typedef struct GTY((chain_circular ("%h.die_sib"), for_user)) die_struct {
2877 union die_symbol_or_type_node
2879 const char * GTY ((tag ("0"))) die_symbol;
2880 comdat_type_node *GTY ((tag ("1"))) die_type_node;
2882 GTY ((desc ("%0.comdat_type_p"))) die_id;
2883 vec<dw_attr_node, va_gc> *die_attr;
2884 dw_die_ref die_parent;
2885 dw_die_ref die_child;
2886 dw_die_ref die_sib;
2887 dw_die_ref die_definition; /* ref from a specification to its definition */
2888 dw_offset die_offset;
2889 unsigned long die_abbrev;
2890 int die_mark;
2891 unsigned int decl_id;
2892 enum dwarf_tag die_tag;
2893 /* Die is used and must not be pruned as unused. */
2894 BOOL_BITFIELD die_perennial_p : 1;
2895 BOOL_BITFIELD comdat_type_p : 1; /* DIE has a type signature */
2896 /* Whether this DIE was removed from the DIE tree, for example via
2897 prune_unused_types. We don't consider those present from the
2898 DIE lookup routines. */
2899 BOOL_BITFIELD removed : 1;
2900 /* Lots of spare bits. */
2902 die_node;
2904 /* Set to TRUE while dwarf2out_early_global_decl is running. */
2905 static bool early_dwarf;
2906 static bool early_dwarf_finished;
2907 struct set_early_dwarf {
2908 bool saved;
2909 set_early_dwarf () : saved(early_dwarf)
2911 gcc_assert (! early_dwarf_finished);
2912 early_dwarf = true;
2914 ~set_early_dwarf () { early_dwarf = saved; }
2917 /* Evaluate 'expr' while 'c' is set to each child of DIE in order. */
2918 #define FOR_EACH_CHILD(die, c, expr) do { \
2919 c = die->die_child; \
2920 if (c) do { \
2921 c = c->die_sib; \
2922 expr; \
2923 } while (c != die->die_child); \
2924 } while (0)
2926 /* The pubname structure */
2928 typedef struct GTY(()) pubname_struct {
2929 dw_die_ref die;
2930 const char *name;
2932 pubname_entry;
2935 struct GTY(()) dw_ranges {
2936 const char *label;
2937 /* If this is positive, it's a block number, otherwise it's a
2938 bitwise-negated index into dw_ranges_by_label. */
2939 int num;
2940 /* Index for the range list for DW_FORM_rnglistx. */
2941 unsigned int idx : 31;
2942 /* True if this range might be possibly in a different section
2943 from previous entry. */
2944 unsigned int maybe_new_sec : 1;
2947 /* A structure to hold a macinfo entry. */
2949 typedef struct GTY(()) macinfo_struct {
2950 unsigned char code;
2951 unsigned HOST_WIDE_INT lineno;
2952 const char *info;
2954 macinfo_entry;
2957 struct GTY(()) dw_ranges_by_label {
2958 const char *begin;
2959 const char *end;
2962 /* The comdat type node structure. */
2963 struct GTY(()) comdat_type_node
2965 dw_die_ref root_die;
2966 dw_die_ref type_die;
2967 dw_die_ref skeleton_die;
2968 char signature[DWARF_TYPE_SIGNATURE_SIZE];
2969 comdat_type_node *next;
2972 /* A list of DIEs for which we can't determine ancestry (parent_die
2973 field) just yet. Later in dwarf2out_finish we will fill in the
2974 missing bits. */
2975 typedef struct GTY(()) limbo_die_struct {
2976 dw_die_ref die;
2977 /* The tree for which this DIE was created. We use this to
2978 determine ancestry later. */
2979 tree created_for;
2980 struct limbo_die_struct *next;
2982 limbo_die_node;
2984 typedef struct skeleton_chain_struct
2986 dw_die_ref old_die;
2987 dw_die_ref new_die;
2988 struct skeleton_chain_struct *parent;
2990 skeleton_chain_node;
2992 /* Define a macro which returns nonzero for a TYPE_DECL which was
2993 implicitly generated for a type.
2995 Note that, unlike the C front-end (which generates a NULL named
2996 TYPE_DECL node for each complete tagged type, each array type,
2997 and each function type node created) the C++ front-end generates
2998 a _named_ TYPE_DECL node for each tagged type node created.
2999 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
3000 generate a DW_TAG_typedef DIE for them. Likewise with the Ada
3001 front-end, but for each type, tagged or not. */
3003 #define TYPE_DECL_IS_STUB(decl) \
3004 (DECL_NAME (decl) == NULL_TREE \
3005 || (DECL_ARTIFICIAL (decl) \
3006 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
3007 /* This is necessary for stub decls that \
3008 appear in nested inline functions. */ \
3009 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
3010 && (decl_ultimate_origin (decl) \
3011 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
3013 /* Information concerning the compilation unit's programming
3014 language, and compiler version. */
3016 /* Fixed size portion of the DWARF compilation unit header. */
3017 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
3018 (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE \
3019 + (dwarf_version >= 5 ? 4 : 3))
3021 /* Fixed size portion of the DWARF comdat type unit header. */
3022 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
3023 (DWARF_COMPILE_UNIT_HEADER_SIZE \
3024 + DWARF_TYPE_SIGNATURE_SIZE + DWARF_OFFSET_SIZE)
3026 /* Fixed size portion of the DWARF skeleton compilation unit header. */
3027 #define DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE \
3028 (DWARF_COMPILE_UNIT_HEADER_SIZE + (dwarf_version >= 5 ? 8 : 0))
3030 /* Fixed size portion of public names info. */
3031 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
3033 /* Fixed size portion of the address range info. */
3034 #define DWARF_ARANGES_HEADER_SIZE \
3035 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
3036 DWARF2_ADDR_SIZE * 2) \
3037 - DWARF_INITIAL_LENGTH_SIZE)
3039 /* Size of padding portion in the address range info. It must be
3040 aligned to twice the pointer size. */
3041 #define DWARF_ARANGES_PAD_SIZE \
3042 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
3043 DWARF2_ADDR_SIZE * 2) \
3044 - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
3046 /* Use assembler line directives if available. */
3047 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
3048 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
3049 #define DWARF2_ASM_LINE_DEBUG_INFO 1
3050 #else
3051 #define DWARF2_ASM_LINE_DEBUG_INFO 0
3052 #endif
3053 #endif
3055 /* Minimum line offset in a special line info. opcode.
3056 This value was chosen to give a reasonable range of values. */
3057 #define DWARF_LINE_BASE -10
3059 /* First special line opcode - leave room for the standard opcodes. */
3060 #define DWARF_LINE_OPCODE_BASE ((int)DW_LNS_set_isa + 1)
3062 /* Range of line offsets in a special line info. opcode. */
3063 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
3065 /* Flag that indicates the initial value of the is_stmt_start flag.
3066 In the present implementation, we do not mark any lines as
3067 the beginning of a source statement, because that information
3068 is not made available by the GCC front-end. */
3069 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
3071 /* Maximum number of operations per instruction bundle. */
3072 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
3073 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
3074 #endif
3076 /* This location is used by calc_die_sizes() to keep track
3077 the offset of each DIE within the .debug_info section. */
3078 static unsigned long next_die_offset;
3080 /* Record the root of the DIE's built for the current compilation unit. */
3081 static GTY(()) dw_die_ref single_comp_unit_die;
3083 /* A list of type DIEs that have been separated into comdat sections. */
3084 static GTY(()) comdat_type_node *comdat_type_list;
3086 /* A list of CU DIEs that have been separated. */
3087 static GTY(()) limbo_die_node *cu_die_list;
3089 /* A list of DIEs with a NULL parent waiting to be relocated. */
3090 static GTY(()) limbo_die_node *limbo_die_list;
3092 /* A list of DIEs for which we may have to generate
3093 DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set. */
3094 static GTY(()) limbo_die_node *deferred_asm_name;
3096 struct dwarf_file_hasher : ggc_ptr_hash<dwarf_file_data>
3098 typedef const char *compare_type;
3100 static hashval_t hash (dwarf_file_data *);
3101 static bool equal (dwarf_file_data *, const char *);
3104 /* Filenames referenced by this compilation unit. */
3105 static GTY(()) hash_table<dwarf_file_hasher> *file_table;
3107 struct decl_die_hasher : ggc_ptr_hash<die_node>
3109 typedef tree compare_type;
3111 static hashval_t hash (die_node *);
3112 static bool equal (die_node *, tree);
3114 /* A hash table of references to DIE's that describe declarations.
3115 The key is a DECL_UID() which is a unique number identifying each decl. */
3116 static GTY (()) hash_table<decl_die_hasher> *decl_die_table;
3118 struct GTY ((for_user)) variable_value_struct {
3119 unsigned int decl_id;
3120 vec<dw_die_ref, va_gc> *dies;
3123 struct variable_value_hasher : ggc_ptr_hash<variable_value_struct>
3125 typedef tree compare_type;
3127 static hashval_t hash (variable_value_struct *);
3128 static bool equal (variable_value_struct *, tree);
3130 /* A hash table of DIEs that contain DW_OP_GNU_variable_value with
3131 dw_val_class_decl_ref class, indexed by FUNCTION_DECLs which is
3132 DECL_CONTEXT of the referenced VAR_DECLs. */
3133 static GTY (()) hash_table<variable_value_hasher> *variable_value_hash;
3135 struct block_die_hasher : ggc_ptr_hash<die_struct>
3137 static hashval_t hash (die_struct *);
3138 static bool equal (die_struct *, die_struct *);
3141 /* A hash table of references to DIE's that describe COMMON blocks.
3142 The key is DECL_UID() ^ die_parent. */
3143 static GTY (()) hash_table<block_die_hasher> *common_block_die_table;
3145 typedef struct GTY(()) die_arg_entry_struct {
3146 dw_die_ref die;
3147 tree arg;
3148 } die_arg_entry;
3151 /* Node of the variable location list. */
3152 struct GTY ((chain_next ("%h.next"))) var_loc_node {
3153 /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
3154 EXPR_LIST chain. For small bitsizes, bitsize is encoded
3155 in mode of the EXPR_LIST node and first EXPR_LIST operand
3156 is either NOTE_INSN_VAR_LOCATION for a piece with a known
3157 location or NULL for padding. For larger bitsizes,
3158 mode is 0 and first operand is a CONCAT with bitsize
3159 as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
3160 NULL as second operand. */
3161 rtx GTY (()) loc;
3162 const char * GTY (()) label;
3163 struct var_loc_node * GTY (()) next;
3166 /* Variable location list. */
3167 struct GTY ((for_user)) var_loc_list_def {
3168 struct var_loc_node * GTY (()) first;
3170 /* Pointer to the last but one or last element of the
3171 chained list. If the list is empty, both first and
3172 last are NULL, if the list contains just one node
3173 or the last node certainly is not redundant, it points
3174 to the last node, otherwise points to the last but one.
3175 Do not mark it for GC because it is marked through the chain. */
3176 struct var_loc_node * GTY ((skip ("%h"))) last;
3178 /* Pointer to the last element before section switch,
3179 if NULL, either sections weren't switched or first
3180 is after section switch. */
3181 struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
3183 /* DECL_UID of the variable decl. */
3184 unsigned int decl_id;
3186 typedef struct var_loc_list_def var_loc_list;
3188 /* Call argument location list. */
3189 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
3190 rtx GTY (()) call_arg_loc_note;
3191 const char * GTY (()) label;
3192 tree GTY (()) block;
3193 bool tail_call_p;
3194 rtx GTY (()) symbol_ref;
3195 struct call_arg_loc_node * GTY (()) next;
3199 struct decl_loc_hasher : ggc_ptr_hash<var_loc_list>
3201 typedef const_tree compare_type;
3203 static hashval_t hash (var_loc_list *);
3204 static bool equal (var_loc_list *, const_tree);
3207 /* Table of decl location linked lists. */
3208 static GTY (()) hash_table<decl_loc_hasher> *decl_loc_table;
3210 /* Head and tail of call_arg_loc chain. */
3211 static GTY (()) struct call_arg_loc_node *call_arg_locations;
3212 static struct call_arg_loc_node *call_arg_loc_last;
3214 /* Number of call sites in the current function. */
3215 static int call_site_count = -1;
3216 /* Number of tail call sites in the current function. */
3217 static int tail_call_site_count = -1;
3219 /* A cached location list. */
3220 struct GTY ((for_user)) cached_dw_loc_list_def {
3221 /* The DECL_UID of the decl that this entry describes. */
3222 unsigned int decl_id;
3224 /* The cached location list. */
3225 dw_loc_list_ref loc_list;
3227 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
3229 struct dw_loc_list_hasher : ggc_ptr_hash<cached_dw_loc_list>
3232 typedef const_tree compare_type;
3234 static hashval_t hash (cached_dw_loc_list *);
3235 static bool equal (cached_dw_loc_list *, const_tree);
3238 /* Table of cached location lists. */
3239 static GTY (()) hash_table<dw_loc_list_hasher> *cached_dw_loc_list_table;
3241 /* A vector of references to DIE's that are uniquely identified by their tag,
3242 presence/absence of children DIE's, and list of attribute/value pairs. */
3243 static GTY(()) vec<dw_die_ref, va_gc> *abbrev_die_table;
3245 /* A hash map to remember the stack usage for DWARF procedures. The value
3246 stored is the stack size difference between before the DWARF procedure
3247 invokation and after it returned. In other words, for a DWARF procedure
3248 that consumes N stack slots and that pushes M ones, this stores M - N. */
3249 static hash_map<dw_die_ref, int> *dwarf_proc_stack_usage_map;
3251 /* A global counter for generating labels for line number data. */
3252 static unsigned int line_info_label_num;
3254 /* The current table to which we should emit line number information
3255 for the current function. This will be set up at the beginning of
3256 assembly for the function. */
3257 static GTY(()) dw_line_info_table *cur_line_info_table;
3259 /* The two default tables of line number info. */
3260 static GTY(()) dw_line_info_table *text_section_line_info;
3261 static GTY(()) dw_line_info_table *cold_text_section_line_info;
3263 /* The set of all non-default tables of line number info. */
3264 static GTY(()) vec<dw_line_info_table *, va_gc> *separate_line_info;
3266 /* A flag to tell pubnames/types export if there is an info section to
3267 refer to. */
3268 static bool info_section_emitted;
3270 /* A pointer to the base of a table that contains a list of publicly
3271 accessible names. */
3272 static GTY (()) vec<pubname_entry, va_gc> *pubname_table;
3274 /* A pointer to the base of a table that contains a list of publicly
3275 accessible types. */
3276 static GTY (()) vec<pubname_entry, va_gc> *pubtype_table;
3278 /* A pointer to the base of a table that contains a list of macro
3279 defines/undefines (and file start/end markers). */
3280 static GTY (()) vec<macinfo_entry, va_gc> *macinfo_table;
3282 /* True if .debug_macinfo or .debug_macros section is going to be
3283 emitted. */
3284 #define have_macinfo \
3285 ((!XCOFF_DEBUGGING_INFO || HAVE_XCOFF_DWARF_EXTRAS) \
3286 && debug_info_level >= DINFO_LEVEL_VERBOSE \
3287 && !macinfo_table->is_empty ())
3289 /* Vector of dies for which we should generate .debug_ranges info. */
3290 static GTY (()) vec<dw_ranges, va_gc> *ranges_table;
3292 /* Vector of pairs of labels referenced in ranges_table. */
3293 static GTY (()) vec<dw_ranges_by_label, va_gc> *ranges_by_label;
3295 /* Whether we have location lists that need outputting */
3296 static GTY(()) bool have_location_lists;
3298 /* Unique label counter. */
3299 static GTY(()) unsigned int loclabel_num;
3301 /* Unique label counter for point-of-call tables. */
3302 static GTY(()) unsigned int poc_label_num;
3304 /* The last file entry emitted by maybe_emit_file(). */
3305 static GTY(()) struct dwarf_file_data * last_emitted_file;
3307 /* Number of internal labels generated by gen_internal_sym(). */
3308 static GTY(()) int label_num;
3310 static GTY(()) vec<die_arg_entry, va_gc> *tmpl_value_parm_die_table;
3312 /* Instances of generic types for which we need to generate debug
3313 info that describe their generic parameters and arguments. That
3314 generation needs to happen once all types are properly laid out so
3315 we do it at the end of compilation. */
3316 static GTY(()) vec<tree, va_gc> *generic_type_instances;
3318 /* Offset from the "steady-state frame pointer" to the frame base,
3319 within the current function. */
3320 static HOST_WIDE_INT frame_pointer_fb_offset;
3321 static bool frame_pointer_fb_offset_valid;
3323 static vec<dw_die_ref> base_types;
3325 /* Flags to represent a set of attribute classes for attributes that represent
3326 a scalar value (bounds, pointers, ...). */
3327 enum dw_scalar_form
3329 dw_scalar_form_constant = 0x01,
3330 dw_scalar_form_exprloc = 0x02,
3331 dw_scalar_form_reference = 0x04
3334 /* Forward declarations for functions defined in this file. */
3336 static int is_pseudo_reg (const_rtx);
3337 static tree type_main_variant (tree);
3338 static int is_tagged_type (const_tree);
3339 static const char *dwarf_tag_name (unsigned);
3340 static const char *dwarf_attr_name (unsigned);
3341 static const char *dwarf_form_name (unsigned);
3342 static tree decl_ultimate_origin (const_tree);
3343 static tree decl_class_context (tree);
3344 static void add_dwarf_attr (dw_die_ref, dw_attr_node *);
3345 static inline enum dw_val_class AT_class (dw_attr_node *);
3346 static inline unsigned int AT_index (dw_attr_node *);
3347 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
3348 static inline unsigned AT_flag (dw_attr_node *);
3349 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
3350 static inline HOST_WIDE_INT AT_int (dw_attr_node *);
3351 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
3352 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_node *);
3353 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
3354 HOST_WIDE_INT, unsigned HOST_WIDE_INT);
3355 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
3356 unsigned int, unsigned char *);
3357 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
3358 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
3359 static inline const char *AT_string (dw_attr_node *);
3360 static enum dwarf_form AT_string_form (dw_attr_node *);
3361 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
3362 static void add_AT_specification (dw_die_ref, dw_die_ref);
3363 static inline dw_die_ref AT_ref (dw_attr_node *);
3364 static inline int AT_ref_external (dw_attr_node *);
3365 static inline void set_AT_ref_external (dw_attr_node *, int);
3366 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
3367 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
3368 static inline dw_loc_descr_ref AT_loc (dw_attr_node *);
3369 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
3370 dw_loc_list_ref);
3371 static inline dw_loc_list_ref AT_loc_list (dw_attr_node *);
3372 static addr_table_entry *add_addr_table_entry (void *, enum ate_kind);
3373 static void remove_addr_table_entry (addr_table_entry *);
3374 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx, bool);
3375 static inline rtx AT_addr (dw_attr_node *);
3376 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
3377 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
3378 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
3379 static void add_AT_loclistsptr (dw_die_ref, enum dwarf_attribute,
3380 const char *);
3381 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
3382 unsigned HOST_WIDE_INT);
3383 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
3384 unsigned long, bool);
3385 static inline const char *AT_lbl (dw_attr_node *);
3386 static dw_attr_node *get_AT (dw_die_ref, enum dwarf_attribute);
3387 static const char *get_AT_low_pc (dw_die_ref);
3388 static const char *get_AT_hi_pc (dw_die_ref);
3389 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
3390 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
3391 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
3392 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
3393 static bool is_cxx (void);
3394 static bool is_cxx (const_tree);
3395 static bool is_fortran (void);
3396 static bool is_ada (void);
3397 static bool remove_AT (dw_die_ref, enum dwarf_attribute);
3398 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
3399 static void add_child_die (dw_die_ref, dw_die_ref);
3400 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
3401 static dw_die_ref lookup_type_die (tree);
3402 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
3403 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
3404 static void equate_type_number_to_die (tree, dw_die_ref);
3405 static dw_die_ref lookup_decl_die (tree);
3406 static var_loc_list *lookup_decl_loc (const_tree);
3407 static void equate_decl_number_to_die (tree, dw_die_ref);
3408 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
3409 static void print_spaces (FILE *);
3410 static void print_die (dw_die_ref, FILE *);
3411 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
3412 static dw_die_ref pop_compile_unit (dw_die_ref);
3413 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3414 static void attr_checksum (dw_attr_node *, struct md5_ctx *, int *);
3415 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3416 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
3417 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
3418 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
3419 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_node *,
3420 struct md5_ctx *, int *);
3421 struct checksum_attributes;
3422 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
3423 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
3424 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
3425 static void generate_type_signature (dw_die_ref, comdat_type_node *);
3426 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3427 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
3428 static int same_attr_p (dw_attr_node *, dw_attr_node *, int *);
3429 static int same_die_p (dw_die_ref, dw_die_ref, int *);
3430 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
3431 static void compute_section_prefix (dw_die_ref);
3432 static int is_type_die (dw_die_ref);
3433 static int is_comdat_die (dw_die_ref);
3434 static int is_symbol_die (dw_die_ref);
3435 static inline bool is_template_instantiation (dw_die_ref);
3436 static void assign_symbol_names (dw_die_ref);
3437 static void break_out_includes (dw_die_ref);
3438 static int is_declaration_die (dw_die_ref);
3439 static int should_move_die_to_comdat (dw_die_ref);
3440 static dw_die_ref clone_as_declaration (dw_die_ref);
3441 static dw_die_ref clone_die (dw_die_ref);
3442 static dw_die_ref clone_tree (dw_die_ref);
3443 static dw_die_ref copy_declaration_context (dw_die_ref, dw_die_ref);
3444 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
3445 static void generate_skeleton_bottom_up (skeleton_chain_node *);
3446 static dw_die_ref generate_skeleton (dw_die_ref);
3447 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
3448 dw_die_ref,
3449 dw_die_ref);
3450 static void break_out_comdat_types (dw_die_ref);
3451 static void copy_decls_for_unworthy_types (dw_die_ref);
3453 static void add_sibling_attributes (dw_die_ref);
3454 static void output_location_lists (dw_die_ref);
3455 static int constant_size (unsigned HOST_WIDE_INT);
3456 static unsigned long size_of_die (dw_die_ref);
3457 static void calc_die_sizes (dw_die_ref);
3458 static void calc_base_type_die_sizes (void);
3459 static void mark_dies (dw_die_ref);
3460 static void unmark_dies (dw_die_ref);
3461 static void unmark_all_dies (dw_die_ref);
3462 static unsigned long size_of_pubnames (vec<pubname_entry, va_gc> *);
3463 static unsigned long size_of_aranges (void);
3464 static enum dwarf_form value_format (dw_attr_node *);
3465 static void output_value_format (dw_attr_node *);
3466 static void output_abbrev_section (void);
3467 static void output_die_abbrevs (unsigned long, dw_die_ref);
3468 static void output_die_symbol (dw_die_ref);
3469 static void output_die (dw_die_ref);
3470 static void output_compilation_unit_header (enum dwarf_unit_type);
3471 static void output_comp_unit (dw_die_ref, int, const unsigned char *);
3472 static void output_comdat_type_unit (comdat_type_node *);
3473 static const char *dwarf2_name (tree, int);
3474 static void add_pubname (tree, dw_die_ref);
3475 static void add_enumerator_pubname (const char *, dw_die_ref);
3476 static void add_pubname_string (const char *, dw_die_ref);
3477 static void add_pubtype (tree, dw_die_ref);
3478 static void output_pubnames (vec<pubname_entry, va_gc> *);
3479 static void output_aranges (void);
3480 static unsigned int add_ranges (const_tree, bool = false);
3481 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3482 bool *, bool);
3483 static void output_ranges (void);
3484 static dw_line_info_table *new_line_info_table (void);
3485 static void output_line_info (bool);
3486 static void output_file_names (void);
3487 static dw_die_ref base_type_die (tree, bool);
3488 static int is_base_type (tree);
3489 static dw_die_ref subrange_type_die (tree, tree, tree, tree, dw_die_ref);
3490 static int decl_quals (const_tree);
3491 static dw_die_ref modified_type_die (tree, int, bool, dw_die_ref);
3492 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3493 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3494 static int type_is_enum (const_tree);
3495 static unsigned int dbx_reg_number (const_rtx);
3496 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3497 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3498 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3499 enum var_init_status);
3500 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3501 enum var_init_status);
3502 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
3503 enum var_init_status);
3504 static int is_based_loc (const_rtx);
3505 static bool resolve_one_addr (rtx *);
3506 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3507 enum var_init_status);
3508 static dw_loc_descr_ref loc_descriptor (rtx, machine_mode mode,
3509 enum var_init_status);
3510 struct loc_descr_context;
3511 static void add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref);
3512 static void add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list);
3513 static dw_loc_list_ref loc_list_from_tree (tree, int,
3514 struct loc_descr_context *);
3515 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int,
3516 struct loc_descr_context *);
3517 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
3518 static tree field_type (const_tree);
3519 static unsigned int simple_type_align_in_bits (const_tree);
3520 static unsigned int simple_decl_align_in_bits (const_tree);
3521 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3522 struct vlr_context;
3523 static dw_loc_descr_ref field_byte_offset (const_tree, struct vlr_context *,
3524 HOST_WIDE_INT *);
3525 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3526 dw_loc_list_ref);
3527 static void add_data_member_location_attribute (dw_die_ref, tree,
3528 struct vlr_context *);
3529 static bool add_const_value_attribute (dw_die_ref, rtx);
3530 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3531 static void insert_wide_int (const wide_int &, unsigned char *, int);
3532 static void insert_float (const_rtx, unsigned char *);
3533 static rtx rtl_for_decl_location (tree);
3534 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool);
3535 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3536 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3537 static void add_name_attribute (dw_die_ref, const char *);
3538 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3539 static void add_comp_dir_attribute (dw_die_ref);
3540 static void add_scalar_info (dw_die_ref, enum dwarf_attribute, tree, int,
3541 struct loc_descr_context *);
3542 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree,
3543 struct loc_descr_context *);
3544 static void add_subscript_info (dw_die_ref, tree, bool);
3545 static void add_byte_size_attribute (dw_die_ref, tree);
3546 static void add_alignment_attribute (dw_die_ref, tree);
3547 static inline void add_bit_offset_attribute (dw_die_ref, tree,
3548 struct vlr_context *);
3549 static void add_bit_size_attribute (dw_die_ref, tree);
3550 static void add_prototyped_attribute (dw_die_ref, tree);
3551 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
3552 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3553 static void add_src_coords_attributes (dw_die_ref, tree);
3554 static void add_name_and_src_coords_attributes (dw_die_ref, tree, bool = false);
3555 static void add_discr_value (dw_die_ref, dw_discr_value *);
3556 static void add_discr_list (dw_die_ref, dw_discr_list_ref);
3557 static inline dw_discr_list_ref AT_discr_list (dw_attr_node *);
3558 static void push_decl_scope (tree);
3559 static void pop_decl_scope (void);
3560 static dw_die_ref scope_die_for (tree, dw_die_ref);
3561 static inline int local_scope_p (dw_die_ref);
3562 static inline int class_scope_p (dw_die_ref);
3563 static inline int class_or_namespace_scope_p (dw_die_ref);
3564 static void add_type_attribute (dw_die_ref, tree, int, bool, dw_die_ref);
3565 static void add_calling_convention_attribute (dw_die_ref, tree);
3566 static const char *type_tag (const_tree);
3567 static tree member_declared_type (const_tree);
3568 #if 0
3569 static const char *decl_start_label (tree);
3570 #endif
3571 static void gen_array_type_die (tree, dw_die_ref);
3572 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3573 #if 0
3574 static void gen_entry_point_die (tree, dw_die_ref);
3575 #endif
3576 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3577 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3578 static dw_die_ref gen_formal_parameter_pack_die (tree, tree, dw_die_ref, tree*);
3579 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3580 static void gen_formal_types_die (tree, dw_die_ref);
3581 static void gen_subprogram_die (tree, dw_die_ref);
3582 static void gen_variable_die (tree, tree, dw_die_ref);
3583 static void gen_const_die (tree, dw_die_ref);
3584 static void gen_label_die (tree, dw_die_ref);
3585 static void gen_lexical_block_die (tree, dw_die_ref);
3586 static void gen_inlined_subroutine_die (tree, dw_die_ref);
3587 static void gen_field_die (tree, struct vlr_context *, dw_die_ref);
3588 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3589 static dw_die_ref gen_compile_unit_die (const char *);
3590 static void gen_inheritance_die (tree, tree, tree, dw_die_ref);
3591 static void gen_member_die (tree, dw_die_ref);
3592 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3593 enum debug_info_usage);
3594 static void gen_subroutine_type_die (tree, dw_die_ref);
3595 static void gen_typedef_die (tree, dw_die_ref);
3596 static void gen_type_die (tree, dw_die_ref);
3597 static void gen_block_die (tree, dw_die_ref);
3598 static void decls_for_scope (tree, dw_die_ref);
3599 static bool is_naming_typedef_decl (const_tree);
3600 static inline dw_die_ref get_context_die (tree);
3601 static void gen_namespace_die (tree, dw_die_ref);
3602 static dw_die_ref gen_namelist_decl (tree, dw_die_ref, tree);
3603 static dw_die_ref gen_decl_die (tree, tree, struct vlr_context *, dw_die_ref);
3604 static dw_die_ref force_decl_die (tree);
3605 static dw_die_ref force_type_die (tree);
3606 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3607 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3608 static struct dwarf_file_data * lookup_filename (const char *);
3609 static void retry_incomplete_types (void);
3610 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3611 static void gen_generic_params_dies (tree);
3612 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
3613 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
3614 static void splice_child_die (dw_die_ref, dw_die_ref);
3615 static int file_info_cmp (const void *, const void *);
3616 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
3617 const char *, const char *);
3618 static void output_loc_list (dw_loc_list_ref);
3619 static char *gen_internal_sym (const char *);
3620 static bool want_pubnames (void);
3622 static void prune_unmark_dies (dw_die_ref);
3623 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3624 static void prune_unused_types_mark (dw_die_ref, int);
3625 static void prune_unused_types_walk (dw_die_ref);
3626 static void prune_unused_types_walk_attribs (dw_die_ref);
3627 static void prune_unused_types_prune (dw_die_ref);
3628 static void prune_unused_types (void);
3629 static int maybe_emit_file (struct dwarf_file_data *fd);
3630 static inline const char *AT_vms_delta1 (dw_attr_node *);
3631 static inline const char *AT_vms_delta2 (dw_attr_node *);
3632 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
3633 const char *, const char *);
3634 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
3635 static void gen_remaining_tmpl_value_param_die_attribute (void);
3636 static bool generic_type_p (tree);
3637 static void schedule_generic_params_dies_gen (tree t);
3638 static void gen_scheduled_generic_parms_dies (void);
3639 static void resolve_variable_values (void);
3641 static const char *comp_dir_string (void);
3643 static void hash_loc_operands (dw_loc_descr_ref, inchash::hash &);
3645 /* enum for tracking thread-local variables whose address is really an offset
3646 relative to the TLS pointer, which will need link-time relocation, but will
3647 not need relocation by the DWARF consumer. */
3649 enum dtprel_bool
3651 dtprel_false = 0,
3652 dtprel_true = 1
3655 /* Return the operator to use for an address of a variable. For dtprel_true, we
3656 use DW_OP_const*. For regular variables, which need both link-time
3657 relocation and consumer-level relocation (e.g., to account for shared objects
3658 loaded at a random address), we use DW_OP_addr*. */
3660 static inline enum dwarf_location_atom
3661 dw_addr_op (enum dtprel_bool dtprel)
3663 if (dtprel == dtprel_true)
3664 return (dwarf_split_debug_info ? DW_OP_GNU_const_index
3665 : (DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u));
3666 else
3667 return dwarf_split_debug_info ? DW_OP_GNU_addr_index : DW_OP_addr;
3670 /* Return a pointer to a newly allocated address location description. If
3671 dwarf_split_debug_info is true, then record the address with the appropriate
3672 relocation. */
3673 static inline dw_loc_descr_ref
3674 new_addr_loc_descr (rtx addr, enum dtprel_bool dtprel)
3676 dw_loc_descr_ref ref = new_loc_descr (dw_addr_op (dtprel), 0, 0);
3678 ref->dw_loc_oprnd1.val_class = dw_val_class_addr;
3679 ref->dw_loc_oprnd1.v.val_addr = addr;
3680 ref->dtprel = dtprel;
3681 if (dwarf_split_debug_info)
3682 ref->dw_loc_oprnd1.val_entry
3683 = add_addr_table_entry (addr,
3684 dtprel ? ate_kind_rtx_dtprel : ate_kind_rtx);
3685 else
3686 ref->dw_loc_oprnd1.val_entry = NULL;
3688 return ref;
3691 /* Section names used to hold DWARF debugging information. */
3693 #ifndef DEBUG_INFO_SECTION
3694 #define DEBUG_INFO_SECTION ".debug_info"
3695 #endif
3696 #ifndef DEBUG_DWO_INFO_SECTION
3697 #define DEBUG_DWO_INFO_SECTION ".debug_info.dwo"
3698 #endif
3699 #ifndef DEBUG_ABBREV_SECTION
3700 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
3701 #endif
3702 #ifndef DEBUG_DWO_ABBREV_SECTION
3703 #define DEBUG_DWO_ABBREV_SECTION ".debug_abbrev.dwo"
3704 #endif
3705 #ifndef DEBUG_ARANGES_SECTION
3706 #define DEBUG_ARANGES_SECTION ".debug_aranges"
3707 #endif
3708 #ifndef DEBUG_ADDR_SECTION
3709 #define DEBUG_ADDR_SECTION ".debug_addr"
3710 #endif
3711 #ifndef DEBUG_MACINFO_SECTION
3712 #define DEBUG_MACINFO_SECTION ".debug_macinfo"
3713 #endif
3714 #ifndef DEBUG_DWO_MACINFO_SECTION
3715 #define DEBUG_DWO_MACINFO_SECTION ".debug_macinfo.dwo"
3716 #endif
3717 #ifndef DEBUG_DWO_MACRO_SECTION
3718 #define DEBUG_DWO_MACRO_SECTION ".debug_macro.dwo"
3719 #endif
3720 #ifndef DEBUG_MACRO_SECTION
3721 #define DEBUG_MACRO_SECTION ".debug_macro"
3722 #endif
3723 #ifndef DEBUG_LINE_SECTION
3724 #define DEBUG_LINE_SECTION ".debug_line"
3725 #endif
3726 #ifndef DEBUG_DWO_LINE_SECTION
3727 #define DEBUG_DWO_LINE_SECTION ".debug_line.dwo"
3728 #endif
3729 #ifndef DEBUG_LOC_SECTION
3730 #define DEBUG_LOC_SECTION ".debug_loc"
3731 #endif
3732 #ifndef DEBUG_DWO_LOC_SECTION
3733 #define DEBUG_DWO_LOC_SECTION ".debug_loc.dwo"
3734 #endif
3735 #ifndef DEBUG_LOCLISTS_SECTION
3736 #define DEBUG_LOCLISTS_SECTION ".debug_loclists"
3737 #endif
3738 #ifndef DEBUG_DWO_LOCLISTS_SECTION
3739 #define DEBUG_DWO_LOCLISTS_SECTION ".debug_loclists.dwo"
3740 #endif
3741 #ifndef DEBUG_PUBNAMES_SECTION
3742 #define DEBUG_PUBNAMES_SECTION \
3743 ((debug_generate_pub_sections == 2) \
3744 ? ".debug_gnu_pubnames" : ".debug_pubnames")
3745 #endif
3746 #ifndef DEBUG_PUBTYPES_SECTION
3747 #define DEBUG_PUBTYPES_SECTION \
3748 ((debug_generate_pub_sections == 2) \
3749 ? ".debug_gnu_pubtypes" : ".debug_pubtypes")
3750 #endif
3751 #ifndef DEBUG_STR_OFFSETS_SECTION
3752 #define DEBUG_STR_OFFSETS_SECTION ".debug_str_offsets"
3753 #endif
3754 #ifndef DEBUG_DWO_STR_OFFSETS_SECTION
3755 #define DEBUG_DWO_STR_OFFSETS_SECTION ".debug_str_offsets.dwo"
3756 #endif
3757 #ifndef DEBUG_STR_DWO_SECTION
3758 #define DEBUG_STR_DWO_SECTION ".debug_str.dwo"
3759 #endif
3760 #ifndef DEBUG_STR_SECTION
3761 #define DEBUG_STR_SECTION ".debug_str"
3762 #endif
3763 #ifndef DEBUG_RANGES_SECTION
3764 #define DEBUG_RANGES_SECTION ".debug_ranges"
3765 #endif
3766 #ifndef DEBUG_RNGLISTS_SECTION
3767 #define DEBUG_RNGLISTS_SECTION ".debug_rnglists"
3768 #endif
3769 #ifndef DEBUG_LINE_STR_SECTION
3770 #define DEBUG_LINE_STR_SECTION ".debug_line_str"
3771 #endif
3773 /* Standard ELF section names for compiled code and data. */
3774 #ifndef TEXT_SECTION_NAME
3775 #define TEXT_SECTION_NAME ".text"
3776 #endif
3778 /* Section flags for .debug_str section. */
3779 #define DEBUG_STR_SECTION_FLAGS \
3780 (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings \
3781 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
3782 : SECTION_DEBUG)
3784 /* Section flags for .debug_str.dwo section. */
3785 #define DEBUG_STR_DWO_SECTION_FLAGS (SECTION_DEBUG | SECTION_EXCLUDE)
3787 /* Labels we insert at beginning sections we can reference instead of
3788 the section names themselves. */
3790 #ifndef TEXT_SECTION_LABEL
3791 #define TEXT_SECTION_LABEL "Ltext"
3792 #endif
3793 #ifndef COLD_TEXT_SECTION_LABEL
3794 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
3795 #endif
3796 #ifndef DEBUG_LINE_SECTION_LABEL
3797 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
3798 #endif
3799 #ifndef DEBUG_SKELETON_LINE_SECTION_LABEL
3800 #define DEBUG_SKELETON_LINE_SECTION_LABEL "Lskeleton_debug_line"
3801 #endif
3802 #ifndef DEBUG_INFO_SECTION_LABEL
3803 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
3804 #endif
3805 #ifndef DEBUG_SKELETON_INFO_SECTION_LABEL
3806 #define DEBUG_SKELETON_INFO_SECTION_LABEL "Lskeleton_debug_info"
3807 #endif
3808 #ifndef DEBUG_ABBREV_SECTION_LABEL
3809 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
3810 #endif
3811 #ifndef DEBUG_SKELETON_ABBREV_SECTION_LABEL
3812 #define DEBUG_SKELETON_ABBREV_SECTION_LABEL "Lskeleton_debug_abbrev"
3813 #endif
3814 #ifndef DEBUG_ADDR_SECTION_LABEL
3815 #define DEBUG_ADDR_SECTION_LABEL "Ldebug_addr"
3816 #endif
3817 #ifndef DEBUG_LOC_SECTION_LABEL
3818 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
3819 #endif
3820 #ifndef DEBUG_RANGES_SECTION_LABEL
3821 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
3822 #endif
3823 #ifndef DEBUG_MACINFO_SECTION_LABEL
3824 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
3825 #endif
3826 #ifndef DEBUG_MACRO_SECTION_LABEL
3827 #define DEBUG_MACRO_SECTION_LABEL "Ldebug_macro"
3828 #endif
3829 #define SKELETON_COMP_DIE_ABBREV 1
3830 #define SKELETON_TYPE_DIE_ABBREV 2
3832 /* Definitions of defaults for formats and names of various special
3833 (artificial) labels which may be generated within this file (when the -g
3834 options is used and DWARF2_DEBUGGING_INFO is in effect.
3835 If necessary, these may be overridden from within the tm.h file, but
3836 typically, overriding these defaults is unnecessary. */
3838 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3839 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3840 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3841 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3842 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3843 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3844 static char debug_skeleton_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3845 static char debug_skeleton_abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3846 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3847 static char debug_addr_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3848 static char debug_skeleton_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3849 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3850 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3851 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3852 static char ranges_base_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3854 #ifndef TEXT_END_LABEL
3855 #define TEXT_END_LABEL "Letext"
3856 #endif
3857 #ifndef COLD_END_LABEL
3858 #define COLD_END_LABEL "Letext_cold"
3859 #endif
3860 #ifndef BLOCK_BEGIN_LABEL
3861 #define BLOCK_BEGIN_LABEL "LBB"
3862 #endif
3863 #ifndef BLOCK_END_LABEL
3864 #define BLOCK_END_LABEL "LBE"
3865 #endif
3866 #ifndef LINE_CODE_LABEL
3867 #define LINE_CODE_LABEL "LM"
3868 #endif
3871 /* Return the root of the DIE's built for the current compilation unit. */
3872 static dw_die_ref
3873 comp_unit_die (void)
3875 if (!single_comp_unit_die)
3876 single_comp_unit_die = gen_compile_unit_die (NULL);
3877 return single_comp_unit_die;
3880 /* We allow a language front-end to designate a function that is to be
3881 called to "demangle" any name before it is put into a DIE. */
3883 static const char *(*demangle_name_func) (const char *);
3885 void
3886 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
3888 demangle_name_func = func;
3891 /* Test if rtl node points to a pseudo register. */
3893 static inline int
3894 is_pseudo_reg (const_rtx rtl)
3896 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
3897 || (GET_CODE (rtl) == SUBREG
3898 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
3901 /* Return a reference to a type, with its const and volatile qualifiers
3902 removed. */
3904 static inline tree
3905 type_main_variant (tree type)
3907 type = TYPE_MAIN_VARIANT (type);
3909 /* ??? There really should be only one main variant among any group of
3910 variants of a given type (and all of the MAIN_VARIANT values for all
3911 members of the group should point to that one type) but sometimes the C
3912 front-end messes this up for array types, so we work around that bug
3913 here. */
3914 if (TREE_CODE (type) == ARRAY_TYPE)
3915 while (type != TYPE_MAIN_VARIANT (type))
3916 type = TYPE_MAIN_VARIANT (type);
3918 return type;
3921 /* Return nonzero if the given type node represents a tagged type. */
3923 static inline int
3924 is_tagged_type (const_tree type)
3926 enum tree_code code = TREE_CODE (type);
3928 return (code == RECORD_TYPE || code == UNION_TYPE
3929 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
3932 /* Set label to debug_info_section_label + die_offset of a DIE reference. */
3934 static void
3935 get_ref_die_offset_label (char *label, dw_die_ref ref)
3937 sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
3940 /* Return die_offset of a DIE reference to a base type. */
3942 static unsigned long int
3943 get_base_type_offset (dw_die_ref ref)
3945 if (ref->die_offset)
3946 return ref->die_offset;
3947 if (comp_unit_die ()->die_abbrev)
3949 calc_base_type_die_sizes ();
3950 gcc_assert (ref->die_offset);
3952 return ref->die_offset;
3955 /* Return die_offset of a DIE reference other than base type. */
3957 static unsigned long int
3958 get_ref_die_offset (dw_die_ref ref)
3960 gcc_assert (ref->die_offset);
3961 return ref->die_offset;
3964 /* Convert a DIE tag into its string name. */
3966 static const char *
3967 dwarf_tag_name (unsigned int tag)
3969 const char *name = get_DW_TAG_name (tag);
3971 if (name != NULL)
3972 return name;
3974 return "DW_TAG_<unknown>";
3977 /* Convert a DWARF attribute code into its string name. */
3979 static const char *
3980 dwarf_attr_name (unsigned int attr)
3982 const char *name;
3984 switch (attr)
3986 #if VMS_DEBUGGING_INFO
3987 case DW_AT_HP_prologue:
3988 return "DW_AT_HP_prologue";
3989 #else
3990 case DW_AT_MIPS_loop_unroll_factor:
3991 return "DW_AT_MIPS_loop_unroll_factor";
3992 #endif
3994 #if VMS_DEBUGGING_INFO
3995 case DW_AT_HP_epilogue:
3996 return "DW_AT_HP_epilogue";
3997 #else
3998 case DW_AT_MIPS_stride:
3999 return "DW_AT_MIPS_stride";
4000 #endif
4003 name = get_DW_AT_name (attr);
4005 if (name != NULL)
4006 return name;
4008 return "DW_AT_<unknown>";
4011 /* Convert a DWARF value form code into its string name. */
4013 static const char *
4014 dwarf_form_name (unsigned int form)
4016 const char *name = get_DW_FORM_name (form);
4018 if (name != NULL)
4019 return name;
4021 return "DW_FORM_<unknown>";
4024 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
4025 instance of an inlined instance of a decl which is local to an inline
4026 function, so we have to trace all of the way back through the origin chain
4027 to find out what sort of node actually served as the original seed for the
4028 given block. */
4030 static tree
4031 decl_ultimate_origin (const_tree decl)
4033 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
4034 return NULL_TREE;
4036 /* DECL_ABSTRACT_ORIGIN can point to itself; ignore that if
4037 we're trying to output the abstract instance of this function. */
4038 if (DECL_ABSTRACT_P (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
4039 return NULL_TREE;
4041 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
4042 most distant ancestor, this should never happen. */
4043 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
4045 return DECL_ABSTRACT_ORIGIN (decl);
4048 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
4049 of a virtual function may refer to a base class, so we check the 'this'
4050 parameter. */
4052 static tree
4053 decl_class_context (tree decl)
4055 tree context = NULL_TREE;
4057 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
4058 context = DECL_CONTEXT (decl);
4059 else
4060 context = TYPE_MAIN_VARIANT
4061 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
4063 if (context && !TYPE_P (context))
4064 context = NULL_TREE;
4066 return context;
4069 /* Add an attribute/value pair to a DIE. */
4071 static inline void
4072 add_dwarf_attr (dw_die_ref die, dw_attr_node *attr)
4074 /* Maybe this should be an assert? */
4075 if (die == NULL)
4076 return;
4078 vec_safe_reserve (die->die_attr, 1);
4079 vec_safe_push (die->die_attr, *attr);
4082 static inline enum dw_val_class
4083 AT_class (dw_attr_node *a)
4085 return a->dw_attr_val.val_class;
4088 /* Return the index for any attribute that will be referenced with a
4089 DW_FORM_GNU_addr_index or DW_FORM_GNU_str_index. String indices
4090 are stored in dw_attr_val.v.val_str for reference counting
4091 pruning. */
4093 static inline unsigned int
4094 AT_index (dw_attr_node *a)
4096 if (AT_class (a) == dw_val_class_str)
4097 return a->dw_attr_val.v.val_str->index;
4098 else if (a->dw_attr_val.val_entry != NULL)
4099 return a->dw_attr_val.val_entry->index;
4100 return NOT_INDEXED;
4103 /* Add a flag value attribute to a DIE. */
4105 static inline void
4106 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
4108 dw_attr_node attr;
4110 attr.dw_attr = attr_kind;
4111 attr.dw_attr_val.val_class = dw_val_class_flag;
4112 attr.dw_attr_val.val_entry = NULL;
4113 attr.dw_attr_val.v.val_flag = flag;
4114 add_dwarf_attr (die, &attr);
4117 static inline unsigned
4118 AT_flag (dw_attr_node *a)
4120 gcc_assert (a && AT_class (a) == dw_val_class_flag);
4121 return a->dw_attr_val.v.val_flag;
4124 /* Add a signed integer attribute value to a DIE. */
4126 static inline void
4127 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
4129 dw_attr_node attr;
4131 attr.dw_attr = attr_kind;
4132 attr.dw_attr_val.val_class = dw_val_class_const;
4133 attr.dw_attr_val.val_entry = NULL;
4134 attr.dw_attr_val.v.val_int = int_val;
4135 add_dwarf_attr (die, &attr);
4138 static inline HOST_WIDE_INT
4139 AT_int (dw_attr_node *a)
4141 gcc_assert (a && (AT_class (a) == dw_val_class_const
4142 || AT_class (a) == dw_val_class_const_implicit));
4143 return a->dw_attr_val.v.val_int;
4146 /* Add an unsigned integer attribute value to a DIE. */
4148 static inline void
4149 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
4150 unsigned HOST_WIDE_INT unsigned_val)
4152 dw_attr_node attr;
4154 attr.dw_attr = attr_kind;
4155 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
4156 attr.dw_attr_val.val_entry = NULL;
4157 attr.dw_attr_val.v.val_unsigned = unsigned_val;
4158 add_dwarf_attr (die, &attr);
4161 static inline unsigned HOST_WIDE_INT
4162 AT_unsigned (dw_attr_node *a)
4164 gcc_assert (a && (AT_class (a) == dw_val_class_unsigned_const
4165 || AT_class (a) == dw_val_class_unsigned_const_implicit));
4166 return a->dw_attr_val.v.val_unsigned;
4169 /* Add an unsigned wide integer attribute value to a DIE. */
4171 static inline void
4172 add_AT_wide (dw_die_ref die, enum dwarf_attribute attr_kind,
4173 const wide_int& w)
4175 dw_attr_node attr;
4177 attr.dw_attr = attr_kind;
4178 attr.dw_attr_val.val_class = dw_val_class_wide_int;
4179 attr.dw_attr_val.val_entry = NULL;
4180 attr.dw_attr_val.v.val_wide = ggc_alloc<wide_int> ();
4181 *attr.dw_attr_val.v.val_wide = w;
4182 add_dwarf_attr (die, &attr);
4185 /* Add an unsigned double integer attribute value to a DIE. */
4187 static inline void
4188 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
4189 HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
4191 dw_attr_node attr;
4193 attr.dw_attr = attr_kind;
4194 attr.dw_attr_val.val_class = dw_val_class_const_double;
4195 attr.dw_attr_val.val_entry = NULL;
4196 attr.dw_attr_val.v.val_double.high = high;
4197 attr.dw_attr_val.v.val_double.low = low;
4198 add_dwarf_attr (die, &attr);
4201 /* Add a floating point attribute value to a DIE and return it. */
4203 static inline void
4204 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
4205 unsigned int length, unsigned int elt_size, unsigned char *array)
4207 dw_attr_node attr;
4209 attr.dw_attr = attr_kind;
4210 attr.dw_attr_val.val_class = dw_val_class_vec;
4211 attr.dw_attr_val.val_entry = NULL;
4212 attr.dw_attr_val.v.val_vec.length = length;
4213 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
4214 attr.dw_attr_val.v.val_vec.array = array;
4215 add_dwarf_attr (die, &attr);
4218 /* Add an 8-byte data attribute value to a DIE. */
4220 static inline void
4221 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
4222 unsigned char data8[8])
4224 dw_attr_node attr;
4226 attr.dw_attr = attr_kind;
4227 attr.dw_attr_val.val_class = dw_val_class_data8;
4228 attr.dw_attr_val.val_entry = NULL;
4229 memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
4230 add_dwarf_attr (die, &attr);
4233 /* Add DW_AT_low_pc and DW_AT_high_pc to a DIE. When using
4234 dwarf_split_debug_info, address attributes in dies destined for the
4235 final executable have force_direct set to avoid using indexed
4236 references. */
4238 static inline void
4239 add_AT_low_high_pc (dw_die_ref die, const char *lbl_low, const char *lbl_high,
4240 bool force_direct)
4242 dw_attr_node attr;
4243 char * lbl_id;
4245 lbl_id = xstrdup (lbl_low);
4246 attr.dw_attr = DW_AT_low_pc;
4247 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4248 attr.dw_attr_val.v.val_lbl_id = lbl_id;
4249 if (dwarf_split_debug_info && !force_direct)
4250 attr.dw_attr_val.val_entry
4251 = add_addr_table_entry (lbl_id, ate_kind_label);
4252 else
4253 attr.dw_attr_val.val_entry = NULL;
4254 add_dwarf_attr (die, &attr);
4256 attr.dw_attr = DW_AT_high_pc;
4257 if (dwarf_version < 4)
4258 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4259 else
4260 attr.dw_attr_val.val_class = dw_val_class_high_pc;
4261 lbl_id = xstrdup (lbl_high);
4262 attr.dw_attr_val.v.val_lbl_id = lbl_id;
4263 if (attr.dw_attr_val.val_class == dw_val_class_lbl_id
4264 && dwarf_split_debug_info && !force_direct)
4265 attr.dw_attr_val.val_entry
4266 = add_addr_table_entry (lbl_id, ate_kind_label);
4267 else
4268 attr.dw_attr_val.val_entry = NULL;
4269 add_dwarf_attr (die, &attr);
4272 /* Hash and equality functions for debug_str_hash. */
4274 hashval_t
4275 indirect_string_hasher::hash (indirect_string_node *x)
4277 return htab_hash_string (x->str);
4280 bool
4281 indirect_string_hasher::equal (indirect_string_node *x1, const char *x2)
4283 return strcmp (x1->str, x2) == 0;
4286 /* Add STR to the given string hash table. */
4288 static struct indirect_string_node *
4289 find_AT_string_in_table (const char *str,
4290 hash_table<indirect_string_hasher> *table)
4292 struct indirect_string_node *node;
4294 indirect_string_node **slot
4295 = table->find_slot_with_hash (str, htab_hash_string (str), INSERT);
4296 if (*slot == NULL)
4298 node = ggc_cleared_alloc<indirect_string_node> ();
4299 node->str = ggc_strdup (str);
4300 *slot = node;
4302 else
4303 node = *slot;
4305 node->refcount++;
4306 return node;
4309 /* Add STR to the indirect string hash table. */
4311 static struct indirect_string_node *
4312 find_AT_string (const char *str)
4314 if (! debug_str_hash)
4315 debug_str_hash = hash_table<indirect_string_hasher>::create_ggc (10);
4317 return find_AT_string_in_table (str, debug_str_hash);
4320 /* Add a string attribute value to a DIE. */
4322 static inline void
4323 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
4325 dw_attr_node attr;
4326 struct indirect_string_node *node;
4328 node = find_AT_string (str);
4330 attr.dw_attr = attr_kind;
4331 attr.dw_attr_val.val_class = dw_val_class_str;
4332 attr.dw_attr_val.val_entry = NULL;
4333 attr.dw_attr_val.v.val_str = node;
4334 add_dwarf_attr (die, &attr);
4337 static inline const char *
4338 AT_string (dw_attr_node *a)
4340 gcc_assert (a && AT_class (a) == dw_val_class_str);
4341 return a->dw_attr_val.v.val_str->str;
4344 /* Call this function directly to bypass AT_string_form's logic to put
4345 the string inline in the die. */
4347 static void
4348 set_indirect_string (struct indirect_string_node *node)
4350 char label[MAX_ARTIFICIAL_LABEL_BYTES];
4351 /* Already indirect is a no op. */
4352 if (node->form == DW_FORM_strp
4353 || node->form == DW_FORM_line_strp
4354 || node->form == DW_FORM_GNU_str_index)
4356 gcc_assert (node->label);
4357 return;
4359 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
4360 ++dw2_string_counter;
4361 node->label = xstrdup (label);
4363 if (!dwarf_split_debug_info)
4365 node->form = DW_FORM_strp;
4366 node->index = NOT_INDEXED;
4368 else
4370 node->form = DW_FORM_GNU_str_index;
4371 node->index = NO_INDEX_ASSIGNED;
4375 /* Find out whether a string should be output inline in DIE
4376 or out-of-line in .debug_str section. */
4378 static enum dwarf_form
4379 find_string_form (struct indirect_string_node *node)
4381 unsigned int len;
4383 if (node->form)
4384 return node->form;
4386 len = strlen (node->str) + 1;
4388 /* If the string is shorter or equal to the size of the reference, it is
4389 always better to put it inline. */
4390 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
4391 return node->form = DW_FORM_string;
4393 /* If we cannot expect the linker to merge strings in .debug_str
4394 section, only put it into .debug_str if it is worth even in this
4395 single module. */
4396 if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
4397 || ((debug_str_section->common.flags & SECTION_MERGE) == 0
4398 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
4399 return node->form = DW_FORM_string;
4401 set_indirect_string (node);
4403 return node->form;
4406 /* Find out whether the string referenced from the attribute should be
4407 output inline in DIE or out-of-line in .debug_str section. */
4409 static enum dwarf_form
4410 AT_string_form (dw_attr_node *a)
4412 gcc_assert (a && AT_class (a) == dw_val_class_str);
4413 return find_string_form (a->dw_attr_val.v.val_str);
4416 /* Add a DIE reference attribute value to a DIE. */
4418 static inline void
4419 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
4421 dw_attr_node attr;
4422 gcc_checking_assert (targ_die != NULL);
4424 /* With LTO we can end up trying to reference something we didn't create
4425 a DIE for. Avoid crashing later on a NULL referenced DIE. */
4426 if (targ_die == NULL)
4427 return;
4429 attr.dw_attr = attr_kind;
4430 attr.dw_attr_val.val_class = dw_val_class_die_ref;
4431 attr.dw_attr_val.val_entry = NULL;
4432 attr.dw_attr_val.v.val_die_ref.die = targ_die;
4433 attr.dw_attr_val.v.val_die_ref.external = 0;
4434 add_dwarf_attr (die, &attr);
4437 /* Change DIE reference REF to point to NEW_DIE instead. */
4439 static inline void
4440 change_AT_die_ref (dw_attr_node *ref, dw_die_ref new_die)
4442 gcc_assert (ref->dw_attr_val.val_class == dw_val_class_die_ref);
4443 ref->dw_attr_val.v.val_die_ref.die = new_die;
4444 ref->dw_attr_val.v.val_die_ref.external = 0;
4447 /* Add an AT_specification attribute to a DIE, and also make the back
4448 pointer from the specification to the definition. */
4450 static inline void
4451 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4453 add_AT_die_ref (die, DW_AT_specification, targ_die);
4454 gcc_assert (!targ_die->die_definition);
4455 targ_die->die_definition = die;
4458 static inline dw_die_ref
4459 AT_ref (dw_attr_node *a)
4461 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4462 return a->dw_attr_val.v.val_die_ref.die;
4465 static inline int
4466 AT_ref_external (dw_attr_node *a)
4468 if (a && AT_class (a) == dw_val_class_die_ref)
4469 return a->dw_attr_val.v.val_die_ref.external;
4471 return 0;
4474 static inline void
4475 set_AT_ref_external (dw_attr_node *a, int i)
4477 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4478 a->dw_attr_val.v.val_die_ref.external = i;
4481 /* Add an FDE reference attribute value to a DIE. */
4483 static inline void
4484 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
4486 dw_attr_node attr;
4488 attr.dw_attr = attr_kind;
4489 attr.dw_attr_val.val_class = dw_val_class_fde_ref;
4490 attr.dw_attr_val.val_entry = NULL;
4491 attr.dw_attr_val.v.val_fde_index = targ_fde;
4492 add_dwarf_attr (die, &attr);
4495 /* Add a location description attribute value to a DIE. */
4497 static inline void
4498 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4500 dw_attr_node attr;
4502 attr.dw_attr = attr_kind;
4503 attr.dw_attr_val.val_class = dw_val_class_loc;
4504 attr.dw_attr_val.val_entry = NULL;
4505 attr.dw_attr_val.v.val_loc = loc;
4506 add_dwarf_attr (die, &attr);
4509 static inline dw_loc_descr_ref
4510 AT_loc (dw_attr_node *a)
4512 gcc_assert (a && AT_class (a) == dw_val_class_loc);
4513 return a->dw_attr_val.v.val_loc;
4516 static inline void
4517 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4519 dw_attr_node attr;
4521 if (XCOFF_DEBUGGING_INFO && !HAVE_XCOFF_DWARF_EXTRAS)
4522 return;
4524 attr.dw_attr = attr_kind;
4525 attr.dw_attr_val.val_class = dw_val_class_loc_list;
4526 attr.dw_attr_val.val_entry = NULL;
4527 attr.dw_attr_val.v.val_loc_list = loc_list;
4528 add_dwarf_attr (die, &attr);
4529 have_location_lists = true;
4532 static inline dw_loc_list_ref
4533 AT_loc_list (dw_attr_node *a)
4535 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4536 return a->dw_attr_val.v.val_loc_list;
4539 static inline dw_loc_list_ref *
4540 AT_loc_list_ptr (dw_attr_node *a)
4542 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4543 return &a->dw_attr_val.v.val_loc_list;
4546 struct addr_hasher : ggc_ptr_hash<addr_table_entry>
4548 static hashval_t hash (addr_table_entry *);
4549 static bool equal (addr_table_entry *, addr_table_entry *);
4552 /* Table of entries into the .debug_addr section. */
4554 static GTY (()) hash_table<addr_hasher> *addr_index_table;
4556 /* Hash an address_table_entry. */
4558 hashval_t
4559 addr_hasher::hash (addr_table_entry *a)
4561 inchash::hash hstate;
4562 switch (a->kind)
4564 case ate_kind_rtx:
4565 hstate.add_int (0);
4566 break;
4567 case ate_kind_rtx_dtprel:
4568 hstate.add_int (1);
4569 break;
4570 case ate_kind_label:
4571 return htab_hash_string (a->addr.label);
4572 default:
4573 gcc_unreachable ();
4575 inchash::add_rtx (a->addr.rtl, hstate);
4576 return hstate.end ();
4579 /* Determine equality for two address_table_entries. */
4581 bool
4582 addr_hasher::equal (addr_table_entry *a1, addr_table_entry *a2)
4584 if (a1->kind != a2->kind)
4585 return 0;
4586 switch (a1->kind)
4588 case ate_kind_rtx:
4589 case ate_kind_rtx_dtprel:
4590 return rtx_equal_p (a1->addr.rtl, a2->addr.rtl);
4591 case ate_kind_label:
4592 return strcmp (a1->addr.label, a2->addr.label) == 0;
4593 default:
4594 gcc_unreachable ();
4598 /* Initialize an addr_table_entry. */
4600 void
4601 init_addr_table_entry (addr_table_entry *e, enum ate_kind kind, void *addr)
4603 e->kind = kind;
4604 switch (kind)
4606 case ate_kind_rtx:
4607 case ate_kind_rtx_dtprel:
4608 e->addr.rtl = (rtx) addr;
4609 break;
4610 case ate_kind_label:
4611 e->addr.label = (char *) addr;
4612 break;
4614 e->refcount = 0;
4615 e->index = NO_INDEX_ASSIGNED;
4618 /* Add attr to the address table entry to the table. Defer setting an
4619 index until output time. */
4621 static addr_table_entry *
4622 add_addr_table_entry (void *addr, enum ate_kind kind)
4624 addr_table_entry *node;
4625 addr_table_entry finder;
4627 gcc_assert (dwarf_split_debug_info);
4628 if (! addr_index_table)
4629 addr_index_table = hash_table<addr_hasher>::create_ggc (10);
4630 init_addr_table_entry (&finder, kind, addr);
4631 addr_table_entry **slot = addr_index_table->find_slot (&finder, INSERT);
4633 if (*slot == HTAB_EMPTY_ENTRY)
4635 node = ggc_cleared_alloc<addr_table_entry> ();
4636 init_addr_table_entry (node, kind, addr);
4637 *slot = node;
4639 else
4640 node = *slot;
4642 node->refcount++;
4643 return node;
4646 /* Remove an entry from the addr table by decrementing its refcount.
4647 Strictly, decrementing the refcount would be enough, but the
4648 assertion that the entry is actually in the table has found
4649 bugs. */
4651 static void
4652 remove_addr_table_entry (addr_table_entry *entry)
4654 gcc_assert (dwarf_split_debug_info && addr_index_table);
4655 /* After an index is assigned, the table is frozen. */
4656 gcc_assert (entry->refcount > 0 && entry->index == NO_INDEX_ASSIGNED);
4657 entry->refcount--;
4660 /* Given a location list, remove all addresses it refers to from the
4661 address_table. */
4663 static void
4664 remove_loc_list_addr_table_entries (dw_loc_descr_ref descr)
4666 for (; descr; descr = descr->dw_loc_next)
4667 if (descr->dw_loc_oprnd1.val_entry != NULL)
4669 gcc_assert (descr->dw_loc_oprnd1.val_entry->index == NO_INDEX_ASSIGNED);
4670 remove_addr_table_entry (descr->dw_loc_oprnd1.val_entry);
4674 /* A helper function for dwarf2out_finish called through
4675 htab_traverse. Assign an addr_table_entry its index. All entries
4676 must be collected into the table when this function is called,
4677 because the indexing code relies on htab_traverse to traverse nodes
4678 in the same order for each run. */
4681 index_addr_table_entry (addr_table_entry **h, unsigned int *index)
4683 addr_table_entry *node = *h;
4685 /* Don't index unreferenced nodes. */
4686 if (node->refcount == 0)
4687 return 1;
4689 gcc_assert (node->index == NO_INDEX_ASSIGNED);
4690 node->index = *index;
4691 *index += 1;
4693 return 1;
4696 /* Add an address constant attribute value to a DIE. When using
4697 dwarf_split_debug_info, address attributes in dies destined for the
4698 final executable should be direct references--setting the parameter
4699 force_direct ensures this behavior. */
4701 static inline void
4702 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr,
4703 bool force_direct)
4705 dw_attr_node attr;
4707 attr.dw_attr = attr_kind;
4708 attr.dw_attr_val.val_class = dw_val_class_addr;
4709 attr.dw_attr_val.v.val_addr = addr;
4710 if (dwarf_split_debug_info && !force_direct)
4711 attr.dw_attr_val.val_entry = add_addr_table_entry (addr, ate_kind_rtx);
4712 else
4713 attr.dw_attr_val.val_entry = NULL;
4714 add_dwarf_attr (die, &attr);
4717 /* Get the RTX from to an address DIE attribute. */
4719 static inline rtx
4720 AT_addr (dw_attr_node *a)
4722 gcc_assert (a && AT_class (a) == dw_val_class_addr);
4723 return a->dw_attr_val.v.val_addr;
4726 /* Add a file attribute value to a DIE. */
4728 static inline void
4729 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
4730 struct dwarf_file_data *fd)
4732 dw_attr_node attr;
4734 attr.dw_attr = attr_kind;
4735 attr.dw_attr_val.val_class = dw_val_class_file;
4736 attr.dw_attr_val.val_entry = NULL;
4737 attr.dw_attr_val.v.val_file = fd;
4738 add_dwarf_attr (die, &attr);
4741 /* Get the dwarf_file_data from a file DIE attribute. */
4743 static inline struct dwarf_file_data *
4744 AT_file (dw_attr_node *a)
4746 gcc_assert (a && (AT_class (a) == dw_val_class_file
4747 || AT_class (a) == dw_val_class_file_implicit));
4748 return a->dw_attr_val.v.val_file;
4751 /* Add a vms delta attribute value to a DIE. */
4753 static inline void
4754 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
4755 const char *lbl1, const char *lbl2)
4757 dw_attr_node attr;
4759 attr.dw_attr = attr_kind;
4760 attr.dw_attr_val.val_class = dw_val_class_vms_delta;
4761 attr.dw_attr_val.val_entry = NULL;
4762 attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
4763 attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
4764 add_dwarf_attr (die, &attr);
4767 /* Add a label identifier attribute value to a DIE. */
4769 static inline void
4770 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind,
4771 const char *lbl_id)
4773 dw_attr_node attr;
4775 attr.dw_attr = attr_kind;
4776 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4777 attr.dw_attr_val.val_entry = NULL;
4778 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
4779 if (dwarf_split_debug_info)
4780 attr.dw_attr_val.val_entry
4781 = add_addr_table_entry (attr.dw_attr_val.v.val_lbl_id,
4782 ate_kind_label);
4783 add_dwarf_attr (die, &attr);
4786 /* Add a section offset attribute value to a DIE, an offset into the
4787 debug_line section. */
4789 static inline void
4790 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4791 const char *label)
4793 dw_attr_node attr;
4795 attr.dw_attr = attr_kind;
4796 attr.dw_attr_val.val_class = dw_val_class_lineptr;
4797 attr.dw_attr_val.val_entry = NULL;
4798 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4799 add_dwarf_attr (die, &attr);
4802 /* Add a section offset attribute value to a DIE, an offset into the
4803 debug_loclists section. */
4805 static inline void
4806 add_AT_loclistsptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4807 const char *label)
4809 dw_attr_node attr;
4811 attr.dw_attr = attr_kind;
4812 attr.dw_attr_val.val_class = dw_val_class_loclistsptr;
4813 attr.dw_attr_val.val_entry = NULL;
4814 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4815 add_dwarf_attr (die, &attr);
4818 /* Add a section offset attribute value to a DIE, an offset into the
4819 debug_macinfo section. */
4821 static inline void
4822 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4823 const char *label)
4825 dw_attr_node attr;
4827 attr.dw_attr = attr_kind;
4828 attr.dw_attr_val.val_class = dw_val_class_macptr;
4829 attr.dw_attr_val.val_entry = NULL;
4830 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4831 add_dwarf_attr (die, &attr);
4834 /* Add an offset attribute value to a DIE. */
4836 static inline void
4837 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
4838 unsigned HOST_WIDE_INT offset)
4840 dw_attr_node attr;
4842 attr.dw_attr = attr_kind;
4843 attr.dw_attr_val.val_class = dw_val_class_offset;
4844 attr.dw_attr_val.val_entry = NULL;
4845 attr.dw_attr_val.v.val_offset = offset;
4846 add_dwarf_attr (die, &attr);
4849 /* Add a range_list attribute value to a DIE. When using
4850 dwarf_split_debug_info, address attributes in dies destined for the
4851 final executable should be direct references--setting the parameter
4852 force_direct ensures this behavior. */
4854 #define UNRELOCATED_OFFSET ((addr_table_entry *) 1)
4855 #define RELOCATED_OFFSET (NULL)
4857 static void
4858 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
4859 long unsigned int offset, bool force_direct)
4861 dw_attr_node attr;
4863 attr.dw_attr = attr_kind;
4864 attr.dw_attr_val.val_class = dw_val_class_range_list;
4865 /* For the range_list attribute, use val_entry to store whether the
4866 offset should follow split-debug-info or normal semantics. This
4867 value is read in output_range_list_offset. */
4868 if (dwarf_split_debug_info && !force_direct)
4869 attr.dw_attr_val.val_entry = UNRELOCATED_OFFSET;
4870 else
4871 attr.dw_attr_val.val_entry = RELOCATED_OFFSET;
4872 attr.dw_attr_val.v.val_offset = offset;
4873 add_dwarf_attr (die, &attr);
4876 /* Return the start label of a delta attribute. */
4878 static inline const char *
4879 AT_vms_delta1 (dw_attr_node *a)
4881 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4882 return a->dw_attr_val.v.val_vms_delta.lbl1;
4885 /* Return the end label of a delta attribute. */
4887 static inline const char *
4888 AT_vms_delta2 (dw_attr_node *a)
4890 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4891 return a->dw_attr_val.v.val_vms_delta.lbl2;
4894 static inline const char *
4895 AT_lbl (dw_attr_node *a)
4897 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
4898 || AT_class (a) == dw_val_class_lineptr
4899 || AT_class (a) == dw_val_class_macptr
4900 || AT_class (a) == dw_val_class_loclistsptr
4901 || AT_class (a) == dw_val_class_high_pc));
4902 return a->dw_attr_val.v.val_lbl_id;
4905 /* Get the attribute of type attr_kind. */
4907 static dw_attr_node *
4908 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4910 dw_attr_node *a;
4911 unsigned ix;
4912 dw_die_ref spec = NULL;
4914 if (! die)
4915 return NULL;
4917 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4918 if (a->dw_attr == attr_kind)
4919 return a;
4920 else if (a->dw_attr == DW_AT_specification
4921 || a->dw_attr == DW_AT_abstract_origin)
4922 spec = AT_ref (a);
4924 if (spec)
4925 return get_AT (spec, attr_kind);
4927 return NULL;
4930 /* Returns the parent of the declaration of DIE. */
4932 static dw_die_ref
4933 get_die_parent (dw_die_ref die)
4935 dw_die_ref t;
4937 if (!die)
4938 return NULL;
4940 if ((t = get_AT_ref (die, DW_AT_abstract_origin))
4941 || (t = get_AT_ref (die, DW_AT_specification)))
4942 die = t;
4944 return die->die_parent;
4947 /* Return the "low pc" attribute value, typically associated with a subprogram
4948 DIE. Return null if the "low pc" attribute is either not present, or if it
4949 cannot be represented as an assembler label identifier. */
4951 static inline const char *
4952 get_AT_low_pc (dw_die_ref die)
4954 dw_attr_node *a = get_AT (die, DW_AT_low_pc);
4956 return a ? AT_lbl (a) : NULL;
4959 /* Return the "high pc" attribute value, typically associated with a subprogram
4960 DIE. Return null if the "high pc" attribute is either not present, or if it
4961 cannot be represented as an assembler label identifier. */
4963 static inline const char *
4964 get_AT_hi_pc (dw_die_ref die)
4966 dw_attr_node *a = get_AT (die, DW_AT_high_pc);
4968 return a ? AT_lbl (a) : NULL;
4971 /* Return the value of the string attribute designated by ATTR_KIND, or
4972 NULL if it is not present. */
4974 static inline const char *
4975 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
4977 dw_attr_node *a = get_AT (die, attr_kind);
4979 return a ? AT_string (a) : NULL;
4982 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
4983 if it is not present. */
4985 static inline int
4986 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
4988 dw_attr_node *a = get_AT (die, attr_kind);
4990 return a ? AT_flag (a) : 0;
4993 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
4994 if it is not present. */
4996 static inline unsigned
4997 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
4999 dw_attr_node *a = get_AT (die, attr_kind);
5001 return a ? AT_unsigned (a) : 0;
5004 static inline dw_die_ref
5005 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
5007 dw_attr_node *a = get_AT (die, attr_kind);
5009 return a ? AT_ref (a) : NULL;
5012 static inline struct dwarf_file_data *
5013 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
5015 dw_attr_node *a = get_AT (die, attr_kind);
5017 return a ? AT_file (a) : NULL;
5020 /* Return TRUE if the language is C++. */
5022 static inline bool
5023 is_cxx (void)
5025 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5027 return (lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus
5028 || lang == DW_LANG_C_plus_plus_11 || lang == DW_LANG_C_plus_plus_14);
5031 /* Return TRUE if DECL was created by the C++ frontend. */
5033 static bool
5034 is_cxx (const_tree decl)
5036 if (in_lto_p)
5038 const_tree context = decl;
5039 while (context && TREE_CODE (context) != TRANSLATION_UNIT_DECL)
5041 if (TREE_CODE (context) == BLOCK)
5042 context = BLOCK_SUPERCONTEXT (context);
5043 else
5044 context = get_containing_scope (context);
5046 if (context && TRANSLATION_UNIT_LANGUAGE (context))
5047 return strncmp (TRANSLATION_UNIT_LANGUAGE (context), "GNU C++", 7) == 0;
5049 return is_cxx ();
5052 /* Return TRUE if the language is Java. */
5054 static inline bool
5055 is_java (void)
5057 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5059 return lang == DW_LANG_Java;
5062 /* Return TRUE if the language is Fortran. */
5064 static inline bool
5065 is_fortran (void)
5067 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5069 return (lang == DW_LANG_Fortran77
5070 || lang == DW_LANG_Fortran90
5071 || lang == DW_LANG_Fortran95
5072 || lang == DW_LANG_Fortran03
5073 || lang == DW_LANG_Fortran08);
5076 /* Return TRUE if the language is Ada. */
5078 static inline bool
5079 is_ada (void)
5081 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5083 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
5086 /* Remove the specified attribute if present. Return TRUE if removal
5087 was successful. */
5089 static bool
5090 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5092 dw_attr_node *a;
5093 unsigned ix;
5095 if (! die)
5096 return false;
5098 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5099 if (a->dw_attr == attr_kind)
5101 if (AT_class (a) == dw_val_class_str)
5102 if (a->dw_attr_val.v.val_str->refcount)
5103 a->dw_attr_val.v.val_str->refcount--;
5105 /* vec::ordered_remove should help reduce the number of abbrevs
5106 that are needed. */
5107 die->die_attr->ordered_remove (ix);
5108 return true;
5110 return false;
5113 /* Remove CHILD from its parent. PREV must have the property that
5114 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
5116 static void
5117 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
5119 gcc_assert (child->die_parent == prev->die_parent);
5120 gcc_assert (prev->die_sib == child);
5121 if (prev == child)
5123 gcc_assert (child->die_parent->die_child == child);
5124 prev = NULL;
5126 else
5127 prev->die_sib = child->die_sib;
5128 if (child->die_parent->die_child == child)
5129 child->die_parent->die_child = prev;
5130 child->die_sib = NULL;
5133 /* Replace OLD_CHILD with NEW_CHILD. PREV must have the property that
5134 PREV->DIE_SIB == OLD_CHILD. Does not alter OLD_CHILD. */
5136 static void
5137 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
5139 dw_die_ref parent = old_child->die_parent;
5141 gcc_assert (parent == prev->die_parent);
5142 gcc_assert (prev->die_sib == old_child);
5144 new_child->die_parent = parent;
5145 if (prev == old_child)
5147 gcc_assert (parent->die_child == old_child);
5148 new_child->die_sib = new_child;
5150 else
5152 prev->die_sib = new_child;
5153 new_child->die_sib = old_child->die_sib;
5155 if (old_child->die_parent->die_child == old_child)
5156 old_child->die_parent->die_child = new_child;
5157 old_child->die_sib = NULL;
5160 /* Move all children from OLD_PARENT to NEW_PARENT. */
5162 static void
5163 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
5165 dw_die_ref c;
5166 new_parent->die_child = old_parent->die_child;
5167 old_parent->die_child = NULL;
5168 FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
5171 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
5172 matches TAG. */
5174 static void
5175 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
5177 dw_die_ref c;
5179 c = die->die_child;
5180 if (c) do {
5181 dw_die_ref prev = c;
5182 c = c->die_sib;
5183 while (c->die_tag == tag)
5185 remove_child_with_prev (c, prev);
5186 c->die_parent = NULL;
5187 /* Might have removed every child. */
5188 if (die->die_child == NULL)
5189 return;
5190 c = prev->die_sib;
5192 } while (c != die->die_child);
5195 /* Add a CHILD_DIE as the last child of DIE. */
5197 static void
5198 add_child_die (dw_die_ref die, dw_die_ref child_die)
5200 /* FIXME this should probably be an assert. */
5201 if (! die || ! child_die)
5202 return;
5203 gcc_assert (die != child_die);
5205 child_die->die_parent = die;
5206 if (die->die_child)
5208 child_die->die_sib = die->die_child->die_sib;
5209 die->die_child->die_sib = child_die;
5211 else
5212 child_die->die_sib = child_die;
5213 die->die_child = child_die;
5216 /* Like add_child_die, but put CHILD_DIE after AFTER_DIE. */
5218 static void
5219 add_child_die_after (dw_die_ref die, dw_die_ref child_die,
5220 dw_die_ref after_die)
5222 gcc_assert (die
5223 && child_die
5224 && after_die
5225 && die->die_child
5226 && die != child_die);
5228 child_die->die_parent = die;
5229 child_die->die_sib = after_die->die_sib;
5230 after_die->die_sib = child_die;
5231 if (die->die_child == after_die)
5232 die->die_child = child_die;
5235 /* Unassociate CHILD from its parent, and make its parent be
5236 NEW_PARENT. */
5238 static void
5239 reparent_child (dw_die_ref child, dw_die_ref new_parent)
5241 for (dw_die_ref p = child->die_parent->die_child; ; p = p->die_sib)
5242 if (p->die_sib == child)
5244 remove_child_with_prev (child, p);
5245 break;
5247 add_child_die (new_parent, child);
5250 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
5251 is the specification, to the end of PARENT's list of children.
5252 This is done by removing and re-adding it. */
5254 static void
5255 splice_child_die (dw_die_ref parent, dw_die_ref child)
5257 /* We want the declaration DIE from inside the class, not the
5258 specification DIE at toplevel. */
5259 if (child->die_parent != parent)
5261 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
5263 if (tmp)
5264 child = tmp;
5267 gcc_assert (child->die_parent == parent
5268 || (child->die_parent
5269 == get_AT_ref (parent, DW_AT_specification)));
5271 reparent_child (child, parent);
5274 /* Create and return a new die with a parent of PARENT_DIE. If
5275 PARENT_DIE is NULL, the new DIE is placed in limbo and an
5276 associated tree T must be supplied to determine parenthood
5277 later. */
5279 static inline dw_die_ref
5280 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
5282 dw_die_ref die = ggc_cleared_alloc<die_node> ();
5284 die->die_tag = tag_value;
5286 if (parent_die != NULL)
5287 add_child_die (parent_die, die);
5288 else
5290 limbo_die_node *limbo_node;
5292 /* No DIEs created after early dwarf should end up in limbo,
5293 because the limbo list should not persist past LTO
5294 streaming. */
5295 if (tag_value != DW_TAG_compile_unit
5296 /* These are allowed because they're generated while
5297 breaking out COMDAT units late. */
5298 && tag_value != DW_TAG_type_unit
5299 && tag_value != DW_TAG_skeleton_unit
5300 && !early_dwarf
5301 /* Allow nested functions to live in limbo because they will
5302 only temporarily live there, as decls_for_scope will fix
5303 them up. */
5304 && (TREE_CODE (t) != FUNCTION_DECL
5305 || !decl_function_context (t))
5306 /* Same as nested functions above but for types. Types that
5307 are local to a function will be fixed in
5308 decls_for_scope. */
5309 && (!RECORD_OR_UNION_TYPE_P (t)
5310 || !TYPE_CONTEXT (t)
5311 || TREE_CODE (TYPE_CONTEXT (t)) != FUNCTION_DECL)
5312 /* FIXME debug-early: Allow late limbo DIE creation for LTO,
5313 especially in the ltrans stage, but once we implement LTO
5314 dwarf streaming, we should remove this exception. */
5315 && !in_lto_p)
5317 fprintf (stderr, "symbol ended up in limbo too late:");
5318 debug_generic_stmt (t);
5319 gcc_unreachable ();
5322 limbo_node = ggc_cleared_alloc<limbo_die_node> ();
5323 limbo_node->die = die;
5324 limbo_node->created_for = t;
5325 limbo_node->next = limbo_die_list;
5326 limbo_die_list = limbo_node;
5329 return die;
5332 /* Return the DIE associated with the given type specifier. */
5334 static inline dw_die_ref
5335 lookup_type_die (tree type)
5337 dw_die_ref die = TYPE_SYMTAB_DIE (type);
5338 if (die && die->removed)
5340 TYPE_SYMTAB_DIE (type) = NULL;
5341 return NULL;
5343 return die;
5346 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
5347 anonymous type named by the typedef TYPE_DIE, return the DIE of the
5348 anonymous type instead the one of the naming typedef. */
5350 static inline dw_die_ref
5351 strip_naming_typedef (tree type, dw_die_ref type_die)
5353 if (type
5354 && TREE_CODE (type) == RECORD_TYPE
5355 && type_die
5356 && type_die->die_tag == DW_TAG_typedef
5357 && is_naming_typedef_decl (TYPE_NAME (type)))
5358 type_die = get_AT_ref (type_die, DW_AT_type);
5359 return type_die;
5362 /* Like lookup_type_die, but if type is an anonymous type named by a
5363 typedef[1], return the DIE of the anonymous type instead the one of
5364 the naming typedef. This is because in gen_typedef_die, we did
5365 equate the anonymous struct named by the typedef with the DIE of
5366 the naming typedef. So by default, lookup_type_die on an anonymous
5367 struct yields the DIE of the naming typedef.
5369 [1]: Read the comment of is_naming_typedef_decl to learn about what
5370 a naming typedef is. */
5372 static inline dw_die_ref
5373 lookup_type_die_strip_naming_typedef (tree type)
5375 dw_die_ref die = lookup_type_die (type);
5376 return strip_naming_typedef (type, die);
5379 /* Equate a DIE to a given type specifier. */
5381 static inline void
5382 equate_type_number_to_die (tree type, dw_die_ref type_die)
5384 TYPE_SYMTAB_DIE (type) = type_die;
5387 /* Returns a hash value for X (which really is a die_struct). */
5389 inline hashval_t
5390 decl_die_hasher::hash (die_node *x)
5392 return (hashval_t) x->decl_id;
5395 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
5397 inline bool
5398 decl_die_hasher::equal (die_node *x, tree y)
5400 return (x->decl_id == DECL_UID (y));
5403 /* Return the DIE associated with a given declaration. */
5405 static inline dw_die_ref
5406 lookup_decl_die (tree decl)
5408 dw_die_ref *die = decl_die_table->find_slot_with_hash (decl, DECL_UID (decl),
5409 NO_INSERT);
5410 if (!die)
5411 return NULL;
5412 if ((*die)->removed)
5414 decl_die_table->clear_slot (die);
5415 return NULL;
5417 return *die;
5420 /* Returns a hash value for X (which really is a var_loc_list). */
5422 inline hashval_t
5423 decl_loc_hasher::hash (var_loc_list *x)
5425 return (hashval_t) x->decl_id;
5428 /* Return nonzero if decl_id of var_loc_list X is the same as
5429 UID of decl *Y. */
5431 inline bool
5432 decl_loc_hasher::equal (var_loc_list *x, const_tree y)
5434 return (x->decl_id == DECL_UID (y));
5437 /* Return the var_loc list associated with a given declaration. */
5439 static inline var_loc_list *
5440 lookup_decl_loc (const_tree decl)
5442 if (!decl_loc_table)
5443 return NULL;
5444 return decl_loc_table->find_with_hash (decl, DECL_UID (decl));
5447 /* Returns a hash value for X (which really is a cached_dw_loc_list_list). */
5449 inline hashval_t
5450 dw_loc_list_hasher::hash (cached_dw_loc_list *x)
5452 return (hashval_t) x->decl_id;
5455 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
5456 UID of decl *Y. */
5458 inline bool
5459 dw_loc_list_hasher::equal (cached_dw_loc_list *x, const_tree y)
5461 return (x->decl_id == DECL_UID (y));
5464 /* Equate a DIE to a particular declaration. */
5466 static void
5467 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
5469 unsigned int decl_id = DECL_UID (decl);
5471 *decl_die_table->find_slot_with_hash (decl, decl_id, INSERT) = decl_die;
5472 decl_die->decl_id = decl_id;
5475 /* Return how many bits covers PIECE EXPR_LIST. */
5477 static HOST_WIDE_INT
5478 decl_piece_bitsize (rtx piece)
5480 int ret = (int) GET_MODE (piece);
5481 if (ret)
5482 return ret;
5483 gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
5484 && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
5485 return INTVAL (XEXP (XEXP (piece, 0), 0));
5488 /* Return pointer to the location of location note in PIECE EXPR_LIST. */
5490 static rtx *
5491 decl_piece_varloc_ptr (rtx piece)
5493 if ((int) GET_MODE (piece))
5494 return &XEXP (piece, 0);
5495 else
5496 return &XEXP (XEXP (piece, 0), 1);
5499 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
5500 Next is the chain of following piece nodes. */
5502 static rtx_expr_list *
5503 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
5505 if (bitsize > 0 && bitsize <= (int) MAX_MACHINE_MODE)
5506 return alloc_EXPR_LIST (bitsize, loc_note, next);
5507 else
5508 return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
5509 GEN_INT (bitsize),
5510 loc_note), next);
5513 /* Return rtx that should be stored into loc field for
5514 LOC_NOTE and BITPOS/BITSIZE. */
5516 static rtx
5517 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
5518 HOST_WIDE_INT bitsize)
5520 if (bitsize != -1)
5522 loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
5523 if (bitpos != 0)
5524 loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
5526 return loc_note;
5529 /* This function either modifies location piece list *DEST in
5530 place (if SRC and INNER is NULL), or copies location piece list
5531 *SRC to *DEST while modifying it. Location BITPOS is modified
5532 to contain LOC_NOTE, any pieces overlapping it are removed resp.
5533 not copied and if needed some padding around it is added.
5534 When modifying in place, DEST should point to EXPR_LIST where
5535 earlier pieces cover PIECE_BITPOS bits, when copying SRC points
5536 to the start of the whole list and INNER points to the EXPR_LIST
5537 where earlier pieces cover PIECE_BITPOS bits. */
5539 static void
5540 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
5541 HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
5542 HOST_WIDE_INT bitsize, rtx loc_note)
5544 HOST_WIDE_INT diff;
5545 bool copy = inner != NULL;
5547 if (copy)
5549 /* First copy all nodes preceding the current bitpos. */
5550 while (src != inner)
5552 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5553 decl_piece_bitsize (*src), NULL_RTX);
5554 dest = &XEXP (*dest, 1);
5555 src = &XEXP (*src, 1);
5558 /* Add padding if needed. */
5559 if (bitpos != piece_bitpos)
5561 *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
5562 copy ? NULL_RTX : *dest);
5563 dest = &XEXP (*dest, 1);
5565 else if (*dest && decl_piece_bitsize (*dest) == bitsize)
5567 gcc_assert (!copy);
5568 /* A piece with correct bitpos and bitsize already exist,
5569 just update the location for it and return. */
5570 *decl_piece_varloc_ptr (*dest) = loc_note;
5571 return;
5573 /* Add the piece that changed. */
5574 *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
5575 dest = &XEXP (*dest, 1);
5576 /* Skip over pieces that overlap it. */
5577 diff = bitpos - piece_bitpos + bitsize;
5578 if (!copy)
5579 src = dest;
5580 while (diff > 0 && *src)
5582 rtx piece = *src;
5583 diff -= decl_piece_bitsize (piece);
5584 if (copy)
5585 src = &XEXP (piece, 1);
5586 else
5588 *src = XEXP (piece, 1);
5589 free_EXPR_LIST_node (piece);
5592 /* Add padding if needed. */
5593 if (diff < 0 && *src)
5595 if (!copy)
5596 dest = src;
5597 *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
5598 dest = &XEXP (*dest, 1);
5600 if (!copy)
5601 return;
5602 /* Finally copy all nodes following it. */
5603 while (*src)
5605 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5606 decl_piece_bitsize (*src), NULL_RTX);
5607 dest = &XEXP (*dest, 1);
5608 src = &XEXP (*src, 1);
5612 /* Add a variable location node to the linked list for DECL. */
5614 static struct var_loc_node *
5615 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
5617 unsigned int decl_id;
5618 var_loc_list *temp;
5619 struct var_loc_node *loc = NULL;
5620 HOST_WIDE_INT bitsize = -1, bitpos = -1;
5622 if (VAR_P (decl) && DECL_HAS_DEBUG_EXPR_P (decl))
5624 tree realdecl = DECL_DEBUG_EXPR (decl);
5625 if (handled_component_p (realdecl)
5626 || (TREE_CODE (realdecl) == MEM_REF
5627 && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR))
5629 HOST_WIDE_INT maxsize;
5630 bool reverse;
5631 tree innerdecl
5632 = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize,
5633 &reverse);
5634 if (!DECL_P (innerdecl)
5635 || DECL_IGNORED_P (innerdecl)
5636 || TREE_STATIC (innerdecl)
5637 || bitsize <= 0
5638 || bitpos + bitsize > 256
5639 || bitsize != maxsize)
5640 return NULL;
5641 decl = innerdecl;
5645 decl_id = DECL_UID (decl);
5646 var_loc_list **slot
5647 = decl_loc_table->find_slot_with_hash (decl, decl_id, INSERT);
5648 if (*slot == NULL)
5650 temp = ggc_cleared_alloc<var_loc_list> ();
5651 temp->decl_id = decl_id;
5652 *slot = temp;
5654 else
5655 temp = *slot;
5657 /* For PARM_DECLs try to keep around the original incoming value,
5658 even if that means we'll emit a zero-range .debug_loc entry. */
5659 if (temp->last
5660 && temp->first == temp->last
5661 && TREE_CODE (decl) == PARM_DECL
5662 && NOTE_P (temp->first->loc)
5663 && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
5664 && DECL_INCOMING_RTL (decl)
5665 && NOTE_VAR_LOCATION_LOC (temp->first->loc)
5666 && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
5667 == GET_CODE (DECL_INCOMING_RTL (decl))
5668 && prev_real_insn (as_a<rtx_insn *> (temp->first->loc)) == NULL_RTX
5669 && (bitsize != -1
5670 || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
5671 NOTE_VAR_LOCATION_LOC (loc_note))
5672 || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
5673 != NOTE_VAR_LOCATION_STATUS (loc_note))))
5675 loc = ggc_cleared_alloc<var_loc_node> ();
5676 temp->first->next = loc;
5677 temp->last = loc;
5678 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5680 else if (temp->last)
5682 struct var_loc_node *last = temp->last, *unused = NULL;
5683 rtx *piece_loc = NULL, last_loc_note;
5684 HOST_WIDE_INT piece_bitpos = 0;
5685 if (last->next)
5687 last = last->next;
5688 gcc_assert (last->next == NULL);
5690 if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
5692 piece_loc = &last->loc;
5695 HOST_WIDE_INT cur_bitsize = decl_piece_bitsize (*piece_loc);
5696 if (piece_bitpos + cur_bitsize > bitpos)
5697 break;
5698 piece_bitpos += cur_bitsize;
5699 piece_loc = &XEXP (*piece_loc, 1);
5701 while (*piece_loc);
5703 /* TEMP->LAST here is either pointer to the last but one or
5704 last element in the chained list, LAST is pointer to the
5705 last element. */
5706 if (label && strcmp (last->label, label) == 0)
5708 /* For SRA optimized variables if there weren't any real
5709 insns since last note, just modify the last node. */
5710 if (piece_loc != NULL)
5712 adjust_piece_list (piece_loc, NULL, NULL,
5713 bitpos, piece_bitpos, bitsize, loc_note);
5714 return NULL;
5716 /* If the last note doesn't cover any instructions, remove it. */
5717 if (temp->last != last)
5719 temp->last->next = NULL;
5720 unused = last;
5721 last = temp->last;
5722 gcc_assert (strcmp (last->label, label) != 0);
5724 else
5726 gcc_assert (temp->first == temp->last
5727 || (temp->first->next == temp->last
5728 && TREE_CODE (decl) == PARM_DECL));
5729 memset (temp->last, '\0', sizeof (*temp->last));
5730 temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
5731 return temp->last;
5734 if (bitsize == -1 && NOTE_P (last->loc))
5735 last_loc_note = last->loc;
5736 else if (piece_loc != NULL
5737 && *piece_loc != NULL_RTX
5738 && piece_bitpos == bitpos
5739 && decl_piece_bitsize (*piece_loc) == bitsize)
5740 last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
5741 else
5742 last_loc_note = NULL_RTX;
5743 /* If the current location is the same as the end of the list,
5744 and either both or neither of the locations is uninitialized,
5745 we have nothing to do. */
5746 if (last_loc_note == NULL_RTX
5747 || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
5748 NOTE_VAR_LOCATION_LOC (loc_note)))
5749 || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5750 != NOTE_VAR_LOCATION_STATUS (loc_note))
5751 && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5752 == VAR_INIT_STATUS_UNINITIALIZED)
5753 || (NOTE_VAR_LOCATION_STATUS (loc_note)
5754 == VAR_INIT_STATUS_UNINITIALIZED))))
5756 /* Add LOC to the end of list and update LAST. If the last
5757 element of the list has been removed above, reuse its
5758 memory for the new node, otherwise allocate a new one. */
5759 if (unused)
5761 loc = unused;
5762 memset (loc, '\0', sizeof (*loc));
5764 else
5765 loc = ggc_cleared_alloc<var_loc_node> ();
5766 if (bitsize == -1 || piece_loc == NULL)
5767 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5768 else
5769 adjust_piece_list (&loc->loc, &last->loc, piece_loc,
5770 bitpos, piece_bitpos, bitsize, loc_note);
5771 last->next = loc;
5772 /* Ensure TEMP->LAST will point either to the new last but one
5773 element of the chain, or to the last element in it. */
5774 if (last != temp->last)
5775 temp->last = last;
5777 else if (unused)
5778 ggc_free (unused);
5780 else
5782 loc = ggc_cleared_alloc<var_loc_node> ();
5783 temp->first = loc;
5784 temp->last = loc;
5785 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5787 return loc;
5790 /* Keep track of the number of spaces used to indent the
5791 output of the debugging routines that print the structure of
5792 the DIE internal representation. */
5793 static int print_indent;
5795 /* Indent the line the number of spaces given by print_indent. */
5797 static inline void
5798 print_spaces (FILE *outfile)
5800 fprintf (outfile, "%*s", print_indent, "");
5803 /* Print a type signature in hex. */
5805 static inline void
5806 print_signature (FILE *outfile, char *sig)
5808 int i;
5810 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
5811 fprintf (outfile, "%02x", sig[i] & 0xff);
5814 static inline void
5815 print_discr_value (FILE *outfile, dw_discr_value *discr_value)
5817 if (discr_value->pos)
5818 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, discr_value->v.sval);
5819 else
5820 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, discr_value->v.uval);
5823 static void print_loc_descr (dw_loc_descr_ref, FILE *);
5825 /* Print the value associated to the VAL DWARF value node to OUTFILE. If
5826 RECURSE, output location descriptor operations. */
5828 static void
5829 print_dw_val (dw_val_node *val, bool recurse, FILE *outfile)
5831 switch (val->val_class)
5833 case dw_val_class_addr:
5834 fprintf (outfile, "address");
5835 break;
5836 case dw_val_class_offset:
5837 fprintf (outfile, "offset");
5838 break;
5839 case dw_val_class_loc:
5840 fprintf (outfile, "location descriptor");
5841 if (val->v.val_loc == NULL)
5842 fprintf (outfile, " -> <null>\n");
5843 else if (recurse)
5845 fprintf (outfile, ":\n");
5846 print_indent += 4;
5847 print_loc_descr (val->v.val_loc, outfile);
5848 print_indent -= 4;
5850 else
5851 fprintf (outfile, " (%p)\n", (void *) val->v.val_loc);
5852 break;
5853 case dw_val_class_loc_list:
5854 fprintf (outfile, "location list -> label:%s",
5855 val->v.val_loc_list->ll_symbol);
5856 break;
5857 case dw_val_class_range_list:
5858 fprintf (outfile, "range list");
5859 break;
5860 case dw_val_class_const:
5861 case dw_val_class_const_implicit:
5862 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, val->v.val_int);
5863 break;
5864 case dw_val_class_unsigned_const:
5865 case dw_val_class_unsigned_const_implicit:
5866 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, val->v.val_unsigned);
5867 break;
5868 case dw_val_class_const_double:
5869 fprintf (outfile, "constant (" HOST_WIDE_INT_PRINT_DEC","\
5870 HOST_WIDE_INT_PRINT_UNSIGNED")",
5871 val->v.val_double.high,
5872 val->v.val_double.low);
5873 break;
5874 case dw_val_class_wide_int:
5876 int i = val->v.val_wide->get_len ();
5877 fprintf (outfile, "constant (");
5878 gcc_assert (i > 0);
5879 if (val->v.val_wide->elt (i - 1) == 0)
5880 fprintf (outfile, "0x");
5881 fprintf (outfile, HOST_WIDE_INT_PRINT_HEX,
5882 val->v.val_wide->elt (--i));
5883 while (--i >= 0)
5884 fprintf (outfile, HOST_WIDE_INT_PRINT_PADDED_HEX,
5885 val->v.val_wide->elt (i));
5886 fprintf (outfile, ")");
5887 break;
5889 case dw_val_class_vec:
5890 fprintf (outfile, "floating-point or vector constant");
5891 break;
5892 case dw_val_class_flag:
5893 fprintf (outfile, "%u", val->v.val_flag);
5894 break;
5895 case dw_val_class_die_ref:
5896 if (val->v.val_die_ref.die != NULL)
5898 dw_die_ref die = val->v.val_die_ref.die;
5900 if (die->comdat_type_p)
5902 fprintf (outfile, "die -> signature: ");
5903 print_signature (outfile,
5904 die->die_id.die_type_node->signature);
5906 else if (die->die_id.die_symbol)
5907 fprintf (outfile, "die -> label: %s", die->die_id.die_symbol);
5908 else
5909 fprintf (outfile, "die -> %ld", die->die_offset);
5910 fprintf (outfile, " (%p)", (void *) die);
5912 else
5913 fprintf (outfile, "die -> <null>");
5914 break;
5915 case dw_val_class_vms_delta:
5916 fprintf (outfile, "delta: @slotcount(%s-%s)",
5917 val->v.val_vms_delta.lbl2, val->v.val_vms_delta.lbl1);
5918 break;
5919 case dw_val_class_lbl_id:
5920 case dw_val_class_lineptr:
5921 case dw_val_class_macptr:
5922 case dw_val_class_loclistsptr:
5923 case dw_val_class_high_pc:
5924 fprintf (outfile, "label: %s", val->v.val_lbl_id);
5925 break;
5926 case dw_val_class_str:
5927 if (val->v.val_str->str != NULL)
5928 fprintf (outfile, "\"%s\"", val->v.val_str->str);
5929 else
5930 fprintf (outfile, "<null>");
5931 break;
5932 case dw_val_class_file:
5933 case dw_val_class_file_implicit:
5934 fprintf (outfile, "\"%s\" (%d)", val->v.val_file->filename,
5935 val->v.val_file->emitted_number);
5936 break;
5937 case dw_val_class_data8:
5939 int i;
5941 for (i = 0; i < 8; i++)
5942 fprintf (outfile, "%02x", val->v.val_data8[i]);
5943 break;
5945 case dw_val_class_discr_value:
5946 print_discr_value (outfile, &val->v.val_discr_value);
5947 break;
5948 case dw_val_class_discr_list:
5949 for (dw_discr_list_ref node = val->v.val_discr_list;
5950 node != NULL;
5951 node = node->dw_discr_next)
5953 if (node->dw_discr_range)
5955 fprintf (outfile, " .. ");
5956 print_discr_value (outfile, &node->dw_discr_lower_bound);
5957 print_discr_value (outfile, &node->dw_discr_upper_bound);
5959 else
5960 print_discr_value (outfile, &node->dw_discr_lower_bound);
5962 if (node->dw_discr_next != NULL)
5963 fprintf (outfile, " | ");
5965 default:
5966 break;
5970 /* Likewise, for a DIE attribute. */
5972 static void
5973 print_attribute (dw_attr_node *a, bool recurse, FILE *outfile)
5975 print_dw_val (&a->dw_attr_val, recurse, outfile);
5979 /* Print the list of operands in the LOC location description to OUTFILE. This
5980 routine is a debugging aid only. */
5982 static void
5983 print_loc_descr (dw_loc_descr_ref loc, FILE *outfile)
5985 dw_loc_descr_ref l = loc;
5987 if (loc == NULL)
5989 print_spaces (outfile);
5990 fprintf (outfile, "<null>\n");
5991 return;
5994 for (l = loc; l != NULL; l = l->dw_loc_next)
5996 print_spaces (outfile);
5997 fprintf (outfile, "(%p) %s",
5998 (void *) l,
5999 dwarf_stack_op_name (l->dw_loc_opc));
6000 if (l->dw_loc_oprnd1.val_class != dw_val_class_none)
6002 fprintf (outfile, " ");
6003 print_dw_val (&l->dw_loc_oprnd1, false, outfile);
6005 if (l->dw_loc_oprnd2.val_class != dw_val_class_none)
6007 fprintf (outfile, ", ");
6008 print_dw_val (&l->dw_loc_oprnd2, false, outfile);
6010 fprintf (outfile, "\n");
6014 /* Print the information associated with a given DIE, and its children.
6015 This routine is a debugging aid only. */
6017 static void
6018 print_die (dw_die_ref die, FILE *outfile)
6020 dw_attr_node *a;
6021 dw_die_ref c;
6022 unsigned ix;
6024 print_spaces (outfile);
6025 fprintf (outfile, "DIE %4ld: %s (%p)\n",
6026 die->die_offset, dwarf_tag_name (die->die_tag),
6027 (void*) die);
6028 print_spaces (outfile);
6029 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
6030 fprintf (outfile, " offset: %ld", die->die_offset);
6031 fprintf (outfile, " mark: %d\n", die->die_mark);
6033 if (die->comdat_type_p)
6035 print_spaces (outfile);
6036 fprintf (outfile, " signature: ");
6037 print_signature (outfile, die->die_id.die_type_node->signature);
6038 fprintf (outfile, "\n");
6041 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6043 print_spaces (outfile);
6044 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
6046 print_attribute (a, true, outfile);
6047 fprintf (outfile, "\n");
6050 if (die->die_child != NULL)
6052 print_indent += 4;
6053 FOR_EACH_CHILD (die, c, print_die (c, outfile));
6054 print_indent -= 4;
6056 if (print_indent == 0)
6057 fprintf (outfile, "\n");
6060 /* Print the list of operations in the LOC location description. */
6062 DEBUG_FUNCTION void
6063 debug_dwarf_loc_descr (dw_loc_descr_ref loc)
6065 print_loc_descr (loc, stderr);
6068 /* Print the information collected for a given DIE. */
6070 DEBUG_FUNCTION void
6071 debug_dwarf_die (dw_die_ref die)
6073 print_die (die, stderr);
6076 DEBUG_FUNCTION void
6077 debug (die_struct &ref)
6079 print_die (&ref, stderr);
6082 DEBUG_FUNCTION void
6083 debug (die_struct *ptr)
6085 if (ptr)
6086 debug (*ptr);
6087 else
6088 fprintf (stderr, "<nil>\n");
6092 /* Print all DWARF information collected for the compilation unit.
6093 This routine is a debugging aid only. */
6095 DEBUG_FUNCTION void
6096 debug_dwarf (void)
6098 print_indent = 0;
6099 print_die (comp_unit_die (), stderr);
6102 /* Verify the DIE tree structure. */
6104 DEBUG_FUNCTION void
6105 verify_die (dw_die_ref die)
6107 gcc_assert (!die->die_mark);
6108 if (die->die_parent == NULL
6109 && die->die_sib == NULL)
6110 return;
6111 /* Verify the die_sib list is cyclic. */
6112 dw_die_ref x = die;
6115 x->die_mark = 1;
6116 x = x->die_sib;
6118 while (x && !x->die_mark);
6119 gcc_assert (x == die);
6120 x = die;
6123 /* Verify all dies have the same parent. */
6124 gcc_assert (x->die_parent == die->die_parent);
6125 if (x->die_child)
6127 /* Verify the child has the proper parent and recurse. */
6128 gcc_assert (x->die_child->die_parent == x);
6129 verify_die (x->die_child);
6131 x->die_mark = 0;
6132 x = x->die_sib;
6134 while (x && x->die_mark);
6137 /* Sanity checks on DIEs. */
6139 static void
6140 check_die (dw_die_ref die)
6142 unsigned ix;
6143 dw_attr_node *a;
6144 bool inline_found = false;
6145 int n_location = 0, n_low_pc = 0, n_high_pc = 0, n_artificial = 0;
6146 int n_decl_line = 0, n_decl_column = 0, n_decl_file = 0;
6147 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6149 switch (a->dw_attr)
6151 case DW_AT_inline:
6152 if (a->dw_attr_val.v.val_unsigned)
6153 inline_found = true;
6154 break;
6155 case DW_AT_location:
6156 ++n_location;
6157 break;
6158 case DW_AT_low_pc:
6159 ++n_low_pc;
6160 break;
6161 case DW_AT_high_pc:
6162 ++n_high_pc;
6163 break;
6164 case DW_AT_artificial:
6165 ++n_artificial;
6166 break;
6167 case DW_AT_decl_column:
6168 ++n_decl_column;
6169 break;
6170 case DW_AT_decl_line:
6171 ++n_decl_line;
6172 break;
6173 case DW_AT_decl_file:
6174 ++n_decl_file;
6175 break;
6176 default:
6177 break;
6180 if (n_location > 1 || n_low_pc > 1 || n_high_pc > 1 || n_artificial > 1
6181 || n_decl_column > 1 || n_decl_line > 1 || n_decl_file > 1)
6183 fprintf (stderr, "Duplicate attributes in DIE:\n");
6184 debug_dwarf_die (die);
6185 gcc_unreachable ();
6187 if (inline_found)
6189 /* A debugging information entry that is a member of an abstract
6190 instance tree [that has DW_AT_inline] should not contain any
6191 attributes which describe aspects of the subroutine which vary
6192 between distinct inlined expansions or distinct out-of-line
6193 expansions. */
6194 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6195 gcc_assert (a->dw_attr != DW_AT_low_pc
6196 && a->dw_attr != DW_AT_high_pc
6197 && a->dw_attr != DW_AT_location
6198 && a->dw_attr != DW_AT_frame_base
6199 && a->dw_attr != DW_AT_call_all_calls
6200 && a->dw_attr != DW_AT_GNU_all_call_sites);
6204 /* Start a new compilation unit DIE for an include file. OLD_UNIT is the CU
6205 for the enclosing include file, if any. BINCL_DIE is the DW_TAG_GNU_BINCL
6206 DIE that marks the start of the DIEs for this include file. */
6208 static dw_die_ref
6209 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
6211 const char *filename = get_AT_string (bincl_die, DW_AT_name);
6212 dw_die_ref new_unit = gen_compile_unit_die (filename);
6214 new_unit->die_sib = old_unit;
6215 return new_unit;
6218 /* Close an include-file CU and reopen the enclosing one. */
6220 static dw_die_ref
6221 pop_compile_unit (dw_die_ref old_unit)
6223 dw_die_ref new_unit = old_unit->die_sib;
6225 old_unit->die_sib = NULL;
6226 return new_unit;
6229 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
6230 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
6231 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
6233 /* Calculate the checksum of a location expression. */
6235 static inline void
6236 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
6238 int tem;
6239 inchash::hash hstate;
6240 hashval_t hash;
6242 tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
6243 CHECKSUM (tem);
6244 hash_loc_operands (loc, hstate);
6245 hash = hstate.end();
6246 CHECKSUM (hash);
6249 /* Calculate the checksum of an attribute. */
6251 static void
6252 attr_checksum (dw_attr_node *at, struct md5_ctx *ctx, int *mark)
6254 dw_loc_descr_ref loc;
6255 rtx r;
6257 CHECKSUM (at->dw_attr);
6259 /* We don't care that this was compiled with a different compiler
6260 snapshot; if the output is the same, that's what matters. */
6261 if (at->dw_attr == DW_AT_producer)
6262 return;
6264 switch (AT_class (at))
6266 case dw_val_class_const:
6267 case dw_val_class_const_implicit:
6268 CHECKSUM (at->dw_attr_val.v.val_int);
6269 break;
6270 case dw_val_class_unsigned_const:
6271 case dw_val_class_unsigned_const_implicit:
6272 CHECKSUM (at->dw_attr_val.v.val_unsigned);
6273 break;
6274 case dw_val_class_const_double:
6275 CHECKSUM (at->dw_attr_val.v.val_double);
6276 break;
6277 case dw_val_class_wide_int:
6278 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
6279 get_full_len (*at->dw_attr_val.v.val_wide)
6280 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
6281 break;
6282 case dw_val_class_vec:
6283 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
6284 (at->dw_attr_val.v.val_vec.length
6285 * at->dw_attr_val.v.val_vec.elt_size));
6286 break;
6287 case dw_val_class_flag:
6288 CHECKSUM (at->dw_attr_val.v.val_flag);
6289 break;
6290 case dw_val_class_str:
6291 CHECKSUM_STRING (AT_string (at));
6292 break;
6294 case dw_val_class_addr:
6295 r = AT_addr (at);
6296 gcc_assert (GET_CODE (r) == SYMBOL_REF);
6297 CHECKSUM_STRING (XSTR (r, 0));
6298 break;
6300 case dw_val_class_offset:
6301 CHECKSUM (at->dw_attr_val.v.val_offset);
6302 break;
6304 case dw_val_class_loc:
6305 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6306 loc_checksum (loc, ctx);
6307 break;
6309 case dw_val_class_die_ref:
6310 die_checksum (AT_ref (at), ctx, mark);
6311 break;
6313 case dw_val_class_fde_ref:
6314 case dw_val_class_vms_delta:
6315 case dw_val_class_lbl_id:
6316 case dw_val_class_lineptr:
6317 case dw_val_class_macptr:
6318 case dw_val_class_loclistsptr:
6319 case dw_val_class_high_pc:
6320 break;
6322 case dw_val_class_file:
6323 case dw_val_class_file_implicit:
6324 CHECKSUM_STRING (AT_file (at)->filename);
6325 break;
6327 case dw_val_class_data8:
6328 CHECKSUM (at->dw_attr_val.v.val_data8);
6329 break;
6331 default:
6332 break;
6336 /* Calculate the checksum of a DIE. */
6338 static void
6339 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6341 dw_die_ref c;
6342 dw_attr_node *a;
6343 unsigned ix;
6345 /* To avoid infinite recursion. */
6346 if (die->die_mark)
6348 CHECKSUM (die->die_mark);
6349 return;
6351 die->die_mark = ++(*mark);
6353 CHECKSUM (die->die_tag);
6355 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6356 attr_checksum (a, ctx, mark);
6358 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
6361 #undef CHECKSUM
6362 #undef CHECKSUM_BLOCK
6363 #undef CHECKSUM_STRING
6365 /* For DWARF-4 types, include the trailing NULL when checksumming strings. */
6366 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
6367 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
6368 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
6369 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
6370 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
6371 #define CHECKSUM_ATTR(FOO) \
6372 if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
6374 /* Calculate the checksum of a number in signed LEB128 format. */
6376 static void
6377 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
6379 unsigned char byte;
6380 bool more;
6382 while (1)
6384 byte = (value & 0x7f);
6385 value >>= 7;
6386 more = !((value == 0 && (byte & 0x40) == 0)
6387 || (value == -1 && (byte & 0x40) != 0));
6388 if (more)
6389 byte |= 0x80;
6390 CHECKSUM (byte);
6391 if (!more)
6392 break;
6396 /* Calculate the checksum of a number in unsigned LEB128 format. */
6398 static void
6399 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
6401 while (1)
6403 unsigned char byte = (value & 0x7f);
6404 value >>= 7;
6405 if (value != 0)
6406 /* More bytes to follow. */
6407 byte |= 0x80;
6408 CHECKSUM (byte);
6409 if (value == 0)
6410 break;
6414 /* Checksum the context of the DIE. This adds the names of any
6415 surrounding namespaces or structures to the checksum. */
6417 static void
6418 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
6420 const char *name;
6421 dw_die_ref spec;
6422 int tag = die->die_tag;
6424 if (tag != DW_TAG_namespace
6425 && tag != DW_TAG_structure_type
6426 && tag != DW_TAG_class_type)
6427 return;
6429 name = get_AT_string (die, DW_AT_name);
6431 spec = get_AT_ref (die, DW_AT_specification);
6432 if (spec != NULL)
6433 die = spec;
6435 if (die->die_parent != NULL)
6436 checksum_die_context (die->die_parent, ctx);
6438 CHECKSUM_ULEB128 ('C');
6439 CHECKSUM_ULEB128 (tag);
6440 if (name != NULL)
6441 CHECKSUM_STRING (name);
6444 /* Calculate the checksum of a location expression. */
6446 static inline void
6447 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
6449 /* Special case for lone DW_OP_plus_uconst: checksum as if the location
6450 were emitted as a DW_FORM_sdata instead of a location expression. */
6451 if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
6453 CHECKSUM_ULEB128 (DW_FORM_sdata);
6454 CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
6455 return;
6458 /* Otherwise, just checksum the raw location expression. */
6459 while (loc != NULL)
6461 inchash::hash hstate;
6462 hashval_t hash;
6464 CHECKSUM_ULEB128 (loc->dtprel);
6465 CHECKSUM_ULEB128 (loc->dw_loc_opc);
6466 hash_loc_operands (loc, hstate);
6467 hash = hstate.end ();
6468 CHECKSUM (hash);
6469 loc = loc->dw_loc_next;
6473 /* Calculate the checksum of an attribute. */
6475 static void
6476 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_node *at,
6477 struct md5_ctx *ctx, int *mark)
6479 dw_loc_descr_ref loc;
6480 rtx r;
6482 if (AT_class (at) == dw_val_class_die_ref)
6484 dw_die_ref target_die = AT_ref (at);
6486 /* For pointer and reference types, we checksum only the (qualified)
6487 name of the target type (if there is a name). For friend entries,
6488 we checksum only the (qualified) name of the target type or function.
6489 This allows the checksum to remain the same whether the target type
6490 is complete or not. */
6491 if ((at->dw_attr == DW_AT_type
6492 && (tag == DW_TAG_pointer_type
6493 || tag == DW_TAG_reference_type
6494 || tag == DW_TAG_rvalue_reference_type
6495 || tag == DW_TAG_ptr_to_member_type))
6496 || (at->dw_attr == DW_AT_friend
6497 && tag == DW_TAG_friend))
6499 dw_attr_node *name_attr = get_AT (target_die, DW_AT_name);
6501 if (name_attr != NULL)
6503 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
6505 if (decl == NULL)
6506 decl = target_die;
6507 CHECKSUM_ULEB128 ('N');
6508 CHECKSUM_ULEB128 (at->dw_attr);
6509 if (decl->die_parent != NULL)
6510 checksum_die_context (decl->die_parent, ctx);
6511 CHECKSUM_ULEB128 ('E');
6512 CHECKSUM_STRING (AT_string (name_attr));
6513 return;
6517 /* For all other references to another DIE, we check to see if the
6518 target DIE has already been visited. If it has, we emit a
6519 backward reference; if not, we descend recursively. */
6520 if (target_die->die_mark > 0)
6522 CHECKSUM_ULEB128 ('R');
6523 CHECKSUM_ULEB128 (at->dw_attr);
6524 CHECKSUM_ULEB128 (target_die->die_mark);
6526 else
6528 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
6530 if (decl == NULL)
6531 decl = target_die;
6532 target_die->die_mark = ++(*mark);
6533 CHECKSUM_ULEB128 ('T');
6534 CHECKSUM_ULEB128 (at->dw_attr);
6535 if (decl->die_parent != NULL)
6536 checksum_die_context (decl->die_parent, ctx);
6537 die_checksum_ordered (target_die, ctx, mark);
6539 return;
6542 CHECKSUM_ULEB128 ('A');
6543 CHECKSUM_ULEB128 (at->dw_attr);
6545 switch (AT_class (at))
6547 case dw_val_class_const:
6548 case dw_val_class_const_implicit:
6549 CHECKSUM_ULEB128 (DW_FORM_sdata);
6550 CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
6551 break;
6553 case dw_val_class_unsigned_const:
6554 case dw_val_class_unsigned_const_implicit:
6555 CHECKSUM_ULEB128 (DW_FORM_sdata);
6556 CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
6557 break;
6559 case dw_val_class_const_double:
6560 CHECKSUM_ULEB128 (DW_FORM_block);
6561 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
6562 CHECKSUM (at->dw_attr_val.v.val_double);
6563 break;
6565 case dw_val_class_wide_int:
6566 CHECKSUM_ULEB128 (DW_FORM_block);
6567 CHECKSUM_ULEB128 (get_full_len (*at->dw_attr_val.v.val_wide)
6568 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
6569 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
6570 get_full_len (*at->dw_attr_val.v.val_wide)
6571 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
6572 break;
6574 case dw_val_class_vec:
6575 CHECKSUM_ULEB128 (DW_FORM_block);
6576 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_vec.length
6577 * at->dw_attr_val.v.val_vec.elt_size);
6578 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
6579 (at->dw_attr_val.v.val_vec.length
6580 * at->dw_attr_val.v.val_vec.elt_size));
6581 break;
6583 case dw_val_class_flag:
6584 CHECKSUM_ULEB128 (DW_FORM_flag);
6585 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
6586 break;
6588 case dw_val_class_str:
6589 CHECKSUM_ULEB128 (DW_FORM_string);
6590 CHECKSUM_STRING (AT_string (at));
6591 break;
6593 case dw_val_class_addr:
6594 r = AT_addr (at);
6595 gcc_assert (GET_CODE (r) == SYMBOL_REF);
6596 CHECKSUM_ULEB128 (DW_FORM_string);
6597 CHECKSUM_STRING (XSTR (r, 0));
6598 break;
6600 case dw_val_class_offset:
6601 CHECKSUM_ULEB128 (DW_FORM_sdata);
6602 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
6603 break;
6605 case dw_val_class_loc:
6606 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6607 loc_checksum_ordered (loc, ctx);
6608 break;
6610 case dw_val_class_fde_ref:
6611 case dw_val_class_lbl_id:
6612 case dw_val_class_lineptr:
6613 case dw_val_class_macptr:
6614 case dw_val_class_loclistsptr:
6615 case dw_val_class_high_pc:
6616 break;
6618 case dw_val_class_file:
6619 case dw_val_class_file_implicit:
6620 CHECKSUM_ULEB128 (DW_FORM_string);
6621 CHECKSUM_STRING (AT_file (at)->filename);
6622 break;
6624 case dw_val_class_data8:
6625 CHECKSUM (at->dw_attr_val.v.val_data8);
6626 break;
6628 default:
6629 break;
6633 struct checksum_attributes
6635 dw_attr_node *at_name;
6636 dw_attr_node *at_type;
6637 dw_attr_node *at_friend;
6638 dw_attr_node *at_accessibility;
6639 dw_attr_node *at_address_class;
6640 dw_attr_node *at_alignment;
6641 dw_attr_node *at_allocated;
6642 dw_attr_node *at_artificial;
6643 dw_attr_node *at_associated;
6644 dw_attr_node *at_binary_scale;
6645 dw_attr_node *at_bit_offset;
6646 dw_attr_node *at_bit_size;
6647 dw_attr_node *at_bit_stride;
6648 dw_attr_node *at_byte_size;
6649 dw_attr_node *at_byte_stride;
6650 dw_attr_node *at_const_value;
6651 dw_attr_node *at_containing_type;
6652 dw_attr_node *at_count;
6653 dw_attr_node *at_data_location;
6654 dw_attr_node *at_data_member_location;
6655 dw_attr_node *at_decimal_scale;
6656 dw_attr_node *at_decimal_sign;
6657 dw_attr_node *at_default_value;
6658 dw_attr_node *at_digit_count;
6659 dw_attr_node *at_discr;
6660 dw_attr_node *at_discr_list;
6661 dw_attr_node *at_discr_value;
6662 dw_attr_node *at_encoding;
6663 dw_attr_node *at_endianity;
6664 dw_attr_node *at_explicit;
6665 dw_attr_node *at_is_optional;
6666 dw_attr_node *at_location;
6667 dw_attr_node *at_lower_bound;
6668 dw_attr_node *at_mutable;
6669 dw_attr_node *at_ordering;
6670 dw_attr_node *at_picture_string;
6671 dw_attr_node *at_prototyped;
6672 dw_attr_node *at_small;
6673 dw_attr_node *at_segment;
6674 dw_attr_node *at_string_length;
6675 dw_attr_node *at_string_length_bit_size;
6676 dw_attr_node *at_string_length_byte_size;
6677 dw_attr_node *at_threads_scaled;
6678 dw_attr_node *at_upper_bound;
6679 dw_attr_node *at_use_location;
6680 dw_attr_node *at_use_UTF8;
6681 dw_attr_node *at_variable_parameter;
6682 dw_attr_node *at_virtuality;
6683 dw_attr_node *at_visibility;
6684 dw_attr_node *at_vtable_elem_location;
6687 /* Collect the attributes that we will want to use for the checksum. */
6689 static void
6690 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
6692 dw_attr_node *a;
6693 unsigned ix;
6695 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6697 switch (a->dw_attr)
6699 case DW_AT_name:
6700 attrs->at_name = a;
6701 break;
6702 case DW_AT_type:
6703 attrs->at_type = a;
6704 break;
6705 case DW_AT_friend:
6706 attrs->at_friend = a;
6707 break;
6708 case DW_AT_accessibility:
6709 attrs->at_accessibility = a;
6710 break;
6711 case DW_AT_address_class:
6712 attrs->at_address_class = a;
6713 break;
6714 case DW_AT_alignment:
6715 attrs->at_alignment = a;
6716 break;
6717 case DW_AT_allocated:
6718 attrs->at_allocated = a;
6719 break;
6720 case DW_AT_artificial:
6721 attrs->at_artificial = a;
6722 break;
6723 case DW_AT_associated:
6724 attrs->at_associated = a;
6725 break;
6726 case DW_AT_binary_scale:
6727 attrs->at_binary_scale = a;
6728 break;
6729 case DW_AT_bit_offset:
6730 attrs->at_bit_offset = a;
6731 break;
6732 case DW_AT_bit_size:
6733 attrs->at_bit_size = a;
6734 break;
6735 case DW_AT_bit_stride:
6736 attrs->at_bit_stride = a;
6737 break;
6738 case DW_AT_byte_size:
6739 attrs->at_byte_size = a;
6740 break;
6741 case DW_AT_byte_stride:
6742 attrs->at_byte_stride = a;
6743 break;
6744 case DW_AT_const_value:
6745 attrs->at_const_value = a;
6746 break;
6747 case DW_AT_containing_type:
6748 attrs->at_containing_type = a;
6749 break;
6750 case DW_AT_count:
6751 attrs->at_count = a;
6752 break;
6753 case DW_AT_data_location:
6754 attrs->at_data_location = a;
6755 break;
6756 case DW_AT_data_member_location:
6757 attrs->at_data_member_location = a;
6758 break;
6759 case DW_AT_decimal_scale:
6760 attrs->at_decimal_scale = a;
6761 break;
6762 case DW_AT_decimal_sign:
6763 attrs->at_decimal_sign = a;
6764 break;
6765 case DW_AT_default_value:
6766 attrs->at_default_value = a;
6767 break;
6768 case DW_AT_digit_count:
6769 attrs->at_digit_count = a;
6770 break;
6771 case DW_AT_discr:
6772 attrs->at_discr = a;
6773 break;
6774 case DW_AT_discr_list:
6775 attrs->at_discr_list = a;
6776 break;
6777 case DW_AT_discr_value:
6778 attrs->at_discr_value = a;
6779 break;
6780 case DW_AT_encoding:
6781 attrs->at_encoding = a;
6782 break;
6783 case DW_AT_endianity:
6784 attrs->at_endianity = a;
6785 break;
6786 case DW_AT_explicit:
6787 attrs->at_explicit = a;
6788 break;
6789 case DW_AT_is_optional:
6790 attrs->at_is_optional = a;
6791 break;
6792 case DW_AT_location:
6793 attrs->at_location = a;
6794 break;
6795 case DW_AT_lower_bound:
6796 attrs->at_lower_bound = a;
6797 break;
6798 case DW_AT_mutable:
6799 attrs->at_mutable = a;
6800 break;
6801 case DW_AT_ordering:
6802 attrs->at_ordering = a;
6803 break;
6804 case DW_AT_picture_string:
6805 attrs->at_picture_string = a;
6806 break;
6807 case DW_AT_prototyped:
6808 attrs->at_prototyped = a;
6809 break;
6810 case DW_AT_small:
6811 attrs->at_small = a;
6812 break;
6813 case DW_AT_segment:
6814 attrs->at_segment = a;
6815 break;
6816 case DW_AT_string_length:
6817 attrs->at_string_length = a;
6818 break;
6819 case DW_AT_string_length_bit_size:
6820 attrs->at_string_length_bit_size = a;
6821 break;
6822 case DW_AT_string_length_byte_size:
6823 attrs->at_string_length_byte_size = a;
6824 break;
6825 case DW_AT_threads_scaled:
6826 attrs->at_threads_scaled = a;
6827 break;
6828 case DW_AT_upper_bound:
6829 attrs->at_upper_bound = a;
6830 break;
6831 case DW_AT_use_location:
6832 attrs->at_use_location = a;
6833 break;
6834 case DW_AT_use_UTF8:
6835 attrs->at_use_UTF8 = a;
6836 break;
6837 case DW_AT_variable_parameter:
6838 attrs->at_variable_parameter = a;
6839 break;
6840 case DW_AT_virtuality:
6841 attrs->at_virtuality = a;
6842 break;
6843 case DW_AT_visibility:
6844 attrs->at_visibility = a;
6845 break;
6846 case DW_AT_vtable_elem_location:
6847 attrs->at_vtable_elem_location = a;
6848 break;
6849 default:
6850 break;
6855 /* Calculate the checksum of a DIE, using an ordered subset of attributes. */
6857 static void
6858 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6860 dw_die_ref c;
6861 dw_die_ref decl;
6862 struct checksum_attributes attrs;
6864 CHECKSUM_ULEB128 ('D');
6865 CHECKSUM_ULEB128 (die->die_tag);
6867 memset (&attrs, 0, sizeof (attrs));
6869 decl = get_AT_ref (die, DW_AT_specification);
6870 if (decl != NULL)
6871 collect_checksum_attributes (&attrs, decl);
6872 collect_checksum_attributes (&attrs, die);
6874 CHECKSUM_ATTR (attrs.at_name);
6875 CHECKSUM_ATTR (attrs.at_accessibility);
6876 CHECKSUM_ATTR (attrs.at_address_class);
6877 CHECKSUM_ATTR (attrs.at_allocated);
6878 CHECKSUM_ATTR (attrs.at_artificial);
6879 CHECKSUM_ATTR (attrs.at_associated);
6880 CHECKSUM_ATTR (attrs.at_binary_scale);
6881 CHECKSUM_ATTR (attrs.at_bit_offset);
6882 CHECKSUM_ATTR (attrs.at_bit_size);
6883 CHECKSUM_ATTR (attrs.at_bit_stride);
6884 CHECKSUM_ATTR (attrs.at_byte_size);
6885 CHECKSUM_ATTR (attrs.at_byte_stride);
6886 CHECKSUM_ATTR (attrs.at_const_value);
6887 CHECKSUM_ATTR (attrs.at_containing_type);
6888 CHECKSUM_ATTR (attrs.at_count);
6889 CHECKSUM_ATTR (attrs.at_data_location);
6890 CHECKSUM_ATTR (attrs.at_data_member_location);
6891 CHECKSUM_ATTR (attrs.at_decimal_scale);
6892 CHECKSUM_ATTR (attrs.at_decimal_sign);
6893 CHECKSUM_ATTR (attrs.at_default_value);
6894 CHECKSUM_ATTR (attrs.at_digit_count);
6895 CHECKSUM_ATTR (attrs.at_discr);
6896 CHECKSUM_ATTR (attrs.at_discr_list);
6897 CHECKSUM_ATTR (attrs.at_discr_value);
6898 CHECKSUM_ATTR (attrs.at_encoding);
6899 CHECKSUM_ATTR (attrs.at_endianity);
6900 CHECKSUM_ATTR (attrs.at_explicit);
6901 CHECKSUM_ATTR (attrs.at_is_optional);
6902 CHECKSUM_ATTR (attrs.at_location);
6903 CHECKSUM_ATTR (attrs.at_lower_bound);
6904 CHECKSUM_ATTR (attrs.at_mutable);
6905 CHECKSUM_ATTR (attrs.at_ordering);
6906 CHECKSUM_ATTR (attrs.at_picture_string);
6907 CHECKSUM_ATTR (attrs.at_prototyped);
6908 CHECKSUM_ATTR (attrs.at_small);
6909 CHECKSUM_ATTR (attrs.at_segment);
6910 CHECKSUM_ATTR (attrs.at_string_length);
6911 CHECKSUM_ATTR (attrs.at_string_length_bit_size);
6912 CHECKSUM_ATTR (attrs.at_string_length_byte_size);
6913 CHECKSUM_ATTR (attrs.at_threads_scaled);
6914 CHECKSUM_ATTR (attrs.at_upper_bound);
6915 CHECKSUM_ATTR (attrs.at_use_location);
6916 CHECKSUM_ATTR (attrs.at_use_UTF8);
6917 CHECKSUM_ATTR (attrs.at_variable_parameter);
6918 CHECKSUM_ATTR (attrs.at_virtuality);
6919 CHECKSUM_ATTR (attrs.at_visibility);
6920 CHECKSUM_ATTR (attrs.at_vtable_elem_location);
6921 CHECKSUM_ATTR (attrs.at_type);
6922 CHECKSUM_ATTR (attrs.at_friend);
6923 CHECKSUM_ATTR (attrs.at_alignment);
6925 /* Checksum the child DIEs. */
6926 c = die->die_child;
6927 if (c) do {
6928 dw_attr_node *name_attr;
6930 c = c->die_sib;
6931 name_attr = get_AT (c, DW_AT_name);
6932 if (is_template_instantiation (c))
6934 /* Ignore instantiations of member type and function templates. */
6936 else if (name_attr != NULL
6937 && (is_type_die (c) || c->die_tag == DW_TAG_subprogram))
6939 /* Use a shallow checksum for named nested types and member
6940 functions. */
6941 CHECKSUM_ULEB128 ('S');
6942 CHECKSUM_ULEB128 (c->die_tag);
6943 CHECKSUM_STRING (AT_string (name_attr));
6945 else
6947 /* Use a deep checksum for other children. */
6948 /* Mark this DIE so it gets processed when unmarking. */
6949 if (c->die_mark == 0)
6950 c->die_mark = -1;
6951 die_checksum_ordered (c, ctx, mark);
6953 } while (c != die->die_child);
6955 CHECKSUM_ULEB128 (0);
6958 /* Add a type name and tag to a hash. */
6959 static void
6960 die_odr_checksum (int tag, const char *name, md5_ctx *ctx)
6962 CHECKSUM_ULEB128 (tag);
6963 CHECKSUM_STRING (name);
6966 #undef CHECKSUM
6967 #undef CHECKSUM_STRING
6968 #undef CHECKSUM_ATTR
6969 #undef CHECKSUM_LEB128
6970 #undef CHECKSUM_ULEB128
6972 /* Generate the type signature for DIE. This is computed by generating an
6973 MD5 checksum over the DIE's tag, its relevant attributes, and its
6974 children. Attributes that are references to other DIEs are processed
6975 by recursion, using the MARK field to prevent infinite recursion.
6976 If the DIE is nested inside a namespace or another type, we also
6977 need to include that context in the signature. The lower 64 bits
6978 of the resulting MD5 checksum comprise the signature. */
6980 static void
6981 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
6983 int mark;
6984 const char *name;
6985 unsigned char checksum[16];
6986 struct md5_ctx ctx;
6987 dw_die_ref decl;
6988 dw_die_ref parent;
6990 name = get_AT_string (die, DW_AT_name);
6991 decl = get_AT_ref (die, DW_AT_specification);
6992 parent = get_die_parent (die);
6994 /* First, compute a signature for just the type name (and its surrounding
6995 context, if any. This is stored in the type unit DIE for link-time
6996 ODR (one-definition rule) checking. */
6998 if (is_cxx () && name != NULL)
7000 md5_init_ctx (&ctx);
7002 /* Checksum the names of surrounding namespaces and structures. */
7003 if (parent != NULL)
7004 checksum_die_context (parent, &ctx);
7006 /* Checksum the current DIE. */
7007 die_odr_checksum (die->die_tag, name, &ctx);
7008 md5_finish_ctx (&ctx, checksum);
7010 add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
7013 /* Next, compute the complete type signature. */
7015 md5_init_ctx (&ctx);
7016 mark = 1;
7017 die->die_mark = mark;
7019 /* Checksum the names of surrounding namespaces and structures. */
7020 if (parent != NULL)
7021 checksum_die_context (parent, &ctx);
7023 /* Checksum the DIE and its children. */
7024 die_checksum_ordered (die, &ctx, &mark);
7025 unmark_all_dies (die);
7026 md5_finish_ctx (&ctx, checksum);
7028 /* Store the signature in the type node and link the type DIE and the
7029 type node together. */
7030 memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
7031 DWARF_TYPE_SIGNATURE_SIZE);
7032 die->comdat_type_p = true;
7033 die->die_id.die_type_node = type_node;
7034 type_node->type_die = die;
7036 /* If the DIE is a specification, link its declaration to the type node
7037 as well. */
7038 if (decl != NULL)
7040 decl->comdat_type_p = true;
7041 decl->die_id.die_type_node = type_node;
7045 /* Do the location expressions look same? */
7046 static inline int
7047 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
7049 return loc1->dw_loc_opc == loc2->dw_loc_opc
7050 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
7051 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
7054 /* Do the values look the same? */
7055 static int
7056 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
7058 dw_loc_descr_ref loc1, loc2;
7059 rtx r1, r2;
7061 if (v1->val_class != v2->val_class)
7062 return 0;
7064 switch (v1->val_class)
7066 case dw_val_class_const:
7067 case dw_val_class_const_implicit:
7068 return v1->v.val_int == v2->v.val_int;
7069 case dw_val_class_unsigned_const:
7070 case dw_val_class_unsigned_const_implicit:
7071 return v1->v.val_unsigned == v2->v.val_unsigned;
7072 case dw_val_class_const_double:
7073 return v1->v.val_double.high == v2->v.val_double.high
7074 && v1->v.val_double.low == v2->v.val_double.low;
7075 case dw_val_class_wide_int:
7076 return *v1->v.val_wide == *v2->v.val_wide;
7077 case dw_val_class_vec:
7078 if (v1->v.val_vec.length != v2->v.val_vec.length
7079 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
7080 return 0;
7081 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
7082 v1->v.val_vec.length * v1->v.val_vec.elt_size))
7083 return 0;
7084 return 1;
7085 case dw_val_class_flag:
7086 return v1->v.val_flag == v2->v.val_flag;
7087 case dw_val_class_str:
7088 return !strcmp (v1->v.val_str->str, v2->v.val_str->str);
7090 case dw_val_class_addr:
7091 r1 = v1->v.val_addr;
7092 r2 = v2->v.val_addr;
7093 if (GET_CODE (r1) != GET_CODE (r2))
7094 return 0;
7095 return !rtx_equal_p (r1, r2);
7097 case dw_val_class_offset:
7098 return v1->v.val_offset == v2->v.val_offset;
7100 case dw_val_class_loc:
7101 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
7102 loc1 && loc2;
7103 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
7104 if (!same_loc_p (loc1, loc2, mark))
7105 return 0;
7106 return !loc1 && !loc2;
7108 case dw_val_class_die_ref:
7109 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
7111 case dw_val_class_fde_ref:
7112 case dw_val_class_vms_delta:
7113 case dw_val_class_lbl_id:
7114 case dw_val_class_lineptr:
7115 case dw_val_class_macptr:
7116 case dw_val_class_loclistsptr:
7117 case dw_val_class_high_pc:
7118 return 1;
7120 case dw_val_class_file:
7121 case dw_val_class_file_implicit:
7122 return v1->v.val_file == v2->v.val_file;
7124 case dw_val_class_data8:
7125 return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
7127 default:
7128 return 1;
7132 /* Do the attributes look the same? */
7134 static int
7135 same_attr_p (dw_attr_node *at1, dw_attr_node *at2, int *mark)
7137 if (at1->dw_attr != at2->dw_attr)
7138 return 0;
7140 /* We don't care that this was compiled with a different compiler
7141 snapshot; if the output is the same, that's what matters. */
7142 if (at1->dw_attr == DW_AT_producer)
7143 return 1;
7145 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
7148 /* Do the dies look the same? */
7150 static int
7151 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
7153 dw_die_ref c1, c2;
7154 dw_attr_node *a1;
7155 unsigned ix;
7157 /* To avoid infinite recursion. */
7158 if (die1->die_mark)
7159 return die1->die_mark == die2->die_mark;
7160 die1->die_mark = die2->die_mark = ++(*mark);
7162 if (die1->die_tag != die2->die_tag)
7163 return 0;
7165 if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
7166 return 0;
7168 FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
7169 if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
7170 return 0;
7172 c1 = die1->die_child;
7173 c2 = die2->die_child;
7174 if (! c1)
7176 if (c2)
7177 return 0;
7179 else
7180 for (;;)
7182 if (!same_die_p (c1, c2, mark))
7183 return 0;
7184 c1 = c1->die_sib;
7185 c2 = c2->die_sib;
7186 if (c1 == die1->die_child)
7188 if (c2 == die2->die_child)
7189 break;
7190 else
7191 return 0;
7195 return 1;
7198 /* Do the dies look the same? Wrapper around same_die_p. */
7200 static int
7201 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
7203 int mark = 0;
7204 int ret = same_die_p (die1, die2, &mark);
7206 unmark_all_dies (die1);
7207 unmark_all_dies (die2);
7209 return ret;
7212 /* The prefix to attach to symbols on DIEs in the current comdat debug
7213 info section. */
7214 static const char *comdat_symbol_id;
7216 /* The index of the current symbol within the current comdat CU. */
7217 static unsigned int comdat_symbol_number;
7219 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
7220 children, and set comdat_symbol_id accordingly. */
7222 static void
7223 compute_section_prefix (dw_die_ref unit_die)
7225 const char *die_name = get_AT_string (unit_die, DW_AT_name);
7226 const char *base = die_name ? lbasename (die_name) : "anonymous";
7227 char *name = XALLOCAVEC (char, strlen (base) + 64);
7228 char *p;
7229 int i, mark;
7230 unsigned char checksum[16];
7231 struct md5_ctx ctx;
7233 /* Compute the checksum of the DIE, then append part of it as hex digits to
7234 the name filename of the unit. */
7236 md5_init_ctx (&ctx);
7237 mark = 0;
7238 die_checksum (unit_die, &ctx, &mark);
7239 unmark_all_dies (unit_die);
7240 md5_finish_ctx (&ctx, checksum);
7242 sprintf (name, "%s.", base);
7243 clean_symbol_name (name);
7245 p = name + strlen (name);
7246 for (i = 0; i < 4; i++)
7248 sprintf (p, "%.2x", checksum[i]);
7249 p += 2;
7252 comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
7253 comdat_symbol_number = 0;
7256 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
7258 static int
7259 is_type_die (dw_die_ref die)
7261 switch (die->die_tag)
7263 case DW_TAG_array_type:
7264 case DW_TAG_class_type:
7265 case DW_TAG_interface_type:
7266 case DW_TAG_enumeration_type:
7267 case DW_TAG_pointer_type:
7268 case DW_TAG_reference_type:
7269 case DW_TAG_rvalue_reference_type:
7270 case DW_TAG_string_type:
7271 case DW_TAG_structure_type:
7272 case DW_TAG_subroutine_type:
7273 case DW_TAG_union_type:
7274 case DW_TAG_ptr_to_member_type:
7275 case DW_TAG_set_type:
7276 case DW_TAG_subrange_type:
7277 case DW_TAG_base_type:
7278 case DW_TAG_const_type:
7279 case DW_TAG_file_type:
7280 case DW_TAG_packed_type:
7281 case DW_TAG_volatile_type:
7282 case DW_TAG_typedef:
7283 return 1;
7284 default:
7285 return 0;
7289 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
7290 Basically, we want to choose the bits that are likely to be shared between
7291 compilations (types) and leave out the bits that are specific to individual
7292 compilations (functions). */
7294 static int
7295 is_comdat_die (dw_die_ref c)
7297 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
7298 we do for stabs. The advantage is a greater likelihood of sharing between
7299 objects that don't include headers in the same order (and therefore would
7300 put the base types in a different comdat). jason 8/28/00 */
7302 if (c->die_tag == DW_TAG_base_type)
7303 return 0;
7305 if (c->die_tag == DW_TAG_pointer_type
7306 || c->die_tag == DW_TAG_reference_type
7307 || c->die_tag == DW_TAG_rvalue_reference_type
7308 || c->die_tag == DW_TAG_const_type
7309 || c->die_tag == DW_TAG_volatile_type)
7311 dw_die_ref t = get_AT_ref (c, DW_AT_type);
7313 return t ? is_comdat_die (t) : 0;
7316 return is_type_die (c);
7319 /* Returns 1 iff C is the sort of DIE that might be referred to from another
7320 compilation unit. */
7322 static int
7323 is_symbol_die (dw_die_ref c)
7325 return (is_type_die (c)
7326 || is_declaration_die (c)
7327 || c->die_tag == DW_TAG_namespace
7328 || c->die_tag == DW_TAG_module);
7331 /* Returns true iff C is a compile-unit DIE. */
7333 static inline bool
7334 is_cu_die (dw_die_ref c)
7336 return c && (c->die_tag == DW_TAG_compile_unit
7337 || c->die_tag == DW_TAG_skeleton_unit);
7340 /* Returns true iff C is a unit DIE of some sort. */
7342 static inline bool
7343 is_unit_die (dw_die_ref c)
7345 return c && (c->die_tag == DW_TAG_compile_unit
7346 || c->die_tag == DW_TAG_partial_unit
7347 || c->die_tag == DW_TAG_type_unit
7348 || c->die_tag == DW_TAG_skeleton_unit);
7351 /* Returns true iff C is a namespace DIE. */
7353 static inline bool
7354 is_namespace_die (dw_die_ref c)
7356 return c && c->die_tag == DW_TAG_namespace;
7359 /* Returns true iff C is a class or structure DIE. */
7361 static inline bool
7362 is_class_die (dw_die_ref c)
7364 return c && (c->die_tag == DW_TAG_class_type
7365 || c->die_tag == DW_TAG_structure_type);
7368 /* Return non-zero if this DIE is a template parameter. */
7370 static inline bool
7371 is_template_parameter (dw_die_ref die)
7373 switch (die->die_tag)
7375 case DW_TAG_template_type_param:
7376 case DW_TAG_template_value_param:
7377 case DW_TAG_GNU_template_template_param:
7378 case DW_TAG_GNU_template_parameter_pack:
7379 return true;
7380 default:
7381 return false;
7385 /* Return non-zero if this DIE represents a template instantiation. */
7387 static inline bool
7388 is_template_instantiation (dw_die_ref die)
7390 dw_die_ref c;
7392 if (!is_type_die (die) && die->die_tag != DW_TAG_subprogram)
7393 return false;
7394 FOR_EACH_CHILD (die, c, if (is_template_parameter (c)) return true);
7395 return false;
7398 static char *
7399 gen_internal_sym (const char *prefix)
7401 char buf[MAX_ARTIFICIAL_LABEL_BYTES];
7403 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
7404 return xstrdup (buf);
7407 /* Assign symbols to all worthy DIEs under DIE. */
7409 static void
7410 assign_symbol_names (dw_die_ref die)
7412 dw_die_ref c;
7414 if (is_symbol_die (die) && !die->comdat_type_p)
7416 if (comdat_symbol_id)
7418 char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
7420 sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
7421 comdat_symbol_id, comdat_symbol_number++);
7422 die->die_id.die_symbol = xstrdup (p);
7424 else
7425 die->die_id.die_symbol = gen_internal_sym ("LDIE");
7428 FOR_EACH_CHILD (die, c, assign_symbol_names (c));
7431 struct cu_hash_table_entry
7433 dw_die_ref cu;
7434 unsigned min_comdat_num, max_comdat_num;
7435 struct cu_hash_table_entry *next;
7438 /* Helpers to manipulate hash table of CUs. */
7440 struct cu_hash_table_entry_hasher : pointer_hash <cu_hash_table_entry>
7442 typedef die_struct *compare_type;
7443 static inline hashval_t hash (const cu_hash_table_entry *);
7444 static inline bool equal (const cu_hash_table_entry *, const die_struct *);
7445 static inline void remove (cu_hash_table_entry *);
7448 inline hashval_t
7449 cu_hash_table_entry_hasher::hash (const cu_hash_table_entry *entry)
7451 return htab_hash_string (entry->cu->die_id.die_symbol);
7454 inline bool
7455 cu_hash_table_entry_hasher::equal (const cu_hash_table_entry *entry1,
7456 const die_struct *entry2)
7458 return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
7461 inline void
7462 cu_hash_table_entry_hasher::remove (cu_hash_table_entry *entry)
7464 struct cu_hash_table_entry *next;
7466 while (entry)
7468 next = entry->next;
7469 free (entry);
7470 entry = next;
7474 typedef hash_table<cu_hash_table_entry_hasher> cu_hash_type;
7476 /* Check whether we have already seen this CU and set up SYM_NUM
7477 accordingly. */
7478 static int
7479 check_duplicate_cu (dw_die_ref cu, cu_hash_type *htable, unsigned int *sym_num)
7481 struct cu_hash_table_entry dummy;
7482 struct cu_hash_table_entry **slot, *entry, *last = &dummy;
7484 dummy.max_comdat_num = 0;
7486 slot = htable->find_slot_with_hash (cu,
7487 htab_hash_string (cu->die_id.die_symbol),
7488 INSERT);
7489 entry = *slot;
7491 for (; entry; last = entry, entry = entry->next)
7493 if (same_die_p_wrap (cu, entry->cu))
7494 break;
7497 if (entry)
7499 *sym_num = entry->min_comdat_num;
7500 return 1;
7503 entry = XCNEW (struct cu_hash_table_entry);
7504 entry->cu = cu;
7505 entry->min_comdat_num = *sym_num = last->max_comdat_num;
7506 entry->next = *slot;
7507 *slot = entry;
7509 return 0;
7512 /* Record SYM_NUM to record of CU in HTABLE. */
7513 static void
7514 record_comdat_symbol_number (dw_die_ref cu, cu_hash_type *htable,
7515 unsigned int sym_num)
7517 struct cu_hash_table_entry **slot, *entry;
7519 slot = htable->find_slot_with_hash (cu,
7520 htab_hash_string (cu->die_id.die_symbol),
7521 NO_INSERT);
7522 entry = *slot;
7524 entry->max_comdat_num = sym_num;
7527 /* Traverse the DIE (which is always comp_unit_die), and set up
7528 additional compilation units for each of the include files we see
7529 bracketed by BINCL/EINCL. */
7531 static void
7532 break_out_includes (dw_die_ref die)
7534 dw_die_ref c;
7535 dw_die_ref unit = NULL;
7536 limbo_die_node *node, **pnode;
7538 c = die->die_child;
7539 if (c) do {
7540 dw_die_ref prev = c;
7541 c = c->die_sib;
7542 while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
7543 || (unit && is_comdat_die (c)))
7545 dw_die_ref next = c->die_sib;
7547 /* This DIE is for a secondary CU; remove it from the main one. */
7548 remove_child_with_prev (c, prev);
7550 if (c->die_tag == DW_TAG_GNU_BINCL)
7551 unit = push_new_compile_unit (unit, c);
7552 else if (c->die_tag == DW_TAG_GNU_EINCL)
7553 unit = pop_compile_unit (unit);
7554 else
7555 add_child_die (unit, c);
7556 c = next;
7557 if (c == die->die_child)
7558 break;
7560 } while (c != die->die_child);
7562 #if 0
7563 /* We can only use this in debugging, since the frontend doesn't check
7564 to make sure that we leave every include file we enter. */
7565 gcc_assert (!unit);
7566 #endif
7568 assign_symbol_names (die);
7569 cu_hash_type cu_hash_table (10);
7570 for (node = limbo_die_list, pnode = &limbo_die_list;
7571 node;
7572 node = node->next)
7574 int is_dupl;
7576 compute_section_prefix (node->die);
7577 is_dupl = check_duplicate_cu (node->die, &cu_hash_table,
7578 &comdat_symbol_number);
7579 assign_symbol_names (node->die);
7580 if (is_dupl)
7581 *pnode = node->next;
7582 else
7584 pnode = &node->next;
7585 record_comdat_symbol_number (node->die, &cu_hash_table,
7586 comdat_symbol_number);
7591 /* Return non-zero if this DIE is a declaration. */
7593 static int
7594 is_declaration_die (dw_die_ref die)
7596 dw_attr_node *a;
7597 unsigned ix;
7599 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7600 if (a->dw_attr == DW_AT_declaration)
7601 return 1;
7603 return 0;
7606 /* Return non-zero if this DIE is nested inside a subprogram. */
7608 static int
7609 is_nested_in_subprogram (dw_die_ref die)
7611 dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
7613 if (decl == NULL)
7614 decl = die;
7615 return local_scope_p (decl);
7618 /* Return non-zero if this DIE contains a defining declaration of a
7619 subprogram. */
7621 static int
7622 contains_subprogram_definition (dw_die_ref die)
7624 dw_die_ref c;
7626 if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
7627 return 1;
7628 FOR_EACH_CHILD (die, c, if (contains_subprogram_definition (c)) return 1);
7629 return 0;
7632 /* Return non-zero if this is a type DIE that should be moved to a
7633 COMDAT .debug_types section or .debug_info section with DW_UT_*type
7634 unit type. */
7636 static int
7637 should_move_die_to_comdat (dw_die_ref die)
7639 switch (die->die_tag)
7641 case DW_TAG_class_type:
7642 case DW_TAG_structure_type:
7643 case DW_TAG_enumeration_type:
7644 case DW_TAG_union_type:
7645 /* Don't move declarations, inlined instances, types nested in a
7646 subprogram, or types that contain subprogram definitions. */
7647 if (is_declaration_die (die)
7648 || get_AT (die, DW_AT_abstract_origin)
7649 || is_nested_in_subprogram (die)
7650 || contains_subprogram_definition (die))
7651 return 0;
7652 return 1;
7653 case DW_TAG_array_type:
7654 case DW_TAG_interface_type:
7655 case DW_TAG_pointer_type:
7656 case DW_TAG_reference_type:
7657 case DW_TAG_rvalue_reference_type:
7658 case DW_TAG_string_type:
7659 case DW_TAG_subroutine_type:
7660 case DW_TAG_ptr_to_member_type:
7661 case DW_TAG_set_type:
7662 case DW_TAG_subrange_type:
7663 case DW_TAG_base_type:
7664 case DW_TAG_const_type:
7665 case DW_TAG_file_type:
7666 case DW_TAG_packed_type:
7667 case DW_TAG_volatile_type:
7668 case DW_TAG_typedef:
7669 default:
7670 return 0;
7674 /* Make a clone of DIE. */
7676 static dw_die_ref
7677 clone_die (dw_die_ref die)
7679 dw_die_ref clone;
7680 dw_attr_node *a;
7681 unsigned ix;
7683 clone = ggc_cleared_alloc<die_node> ();
7684 clone->die_tag = die->die_tag;
7686 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7687 add_dwarf_attr (clone, a);
7689 return clone;
7692 /* Make a clone of the tree rooted at DIE. */
7694 static dw_die_ref
7695 clone_tree (dw_die_ref die)
7697 dw_die_ref c;
7698 dw_die_ref clone = clone_die (die);
7700 FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree (c)));
7702 return clone;
7705 /* Make a clone of DIE as a declaration. */
7707 static dw_die_ref
7708 clone_as_declaration (dw_die_ref die)
7710 dw_die_ref clone;
7711 dw_die_ref decl;
7712 dw_attr_node *a;
7713 unsigned ix;
7715 /* If the DIE is already a declaration, just clone it. */
7716 if (is_declaration_die (die))
7717 return clone_die (die);
7719 /* If the DIE is a specification, just clone its declaration DIE. */
7720 decl = get_AT_ref (die, DW_AT_specification);
7721 if (decl != NULL)
7723 clone = clone_die (decl);
7724 if (die->comdat_type_p)
7725 add_AT_die_ref (clone, DW_AT_signature, die);
7726 return clone;
7729 clone = ggc_cleared_alloc<die_node> ();
7730 clone->die_tag = die->die_tag;
7732 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7734 /* We don't want to copy over all attributes.
7735 For example we don't want DW_AT_byte_size because otherwise we will no
7736 longer have a declaration and GDB will treat it as a definition. */
7738 switch (a->dw_attr)
7740 case DW_AT_abstract_origin:
7741 case DW_AT_artificial:
7742 case DW_AT_containing_type:
7743 case DW_AT_external:
7744 case DW_AT_name:
7745 case DW_AT_type:
7746 case DW_AT_virtuality:
7747 case DW_AT_linkage_name:
7748 case DW_AT_MIPS_linkage_name:
7749 add_dwarf_attr (clone, a);
7750 break;
7751 case DW_AT_byte_size:
7752 case DW_AT_alignment:
7753 default:
7754 break;
7758 if (die->comdat_type_p)
7759 add_AT_die_ref (clone, DW_AT_signature, die);
7761 add_AT_flag (clone, DW_AT_declaration, 1);
7762 return clone;
7766 /* Structure to map a DIE in one CU to its copy in a comdat type unit. */
7768 struct decl_table_entry
7770 dw_die_ref orig;
7771 dw_die_ref copy;
7774 /* Helpers to manipulate hash table of copied declarations. */
7776 /* Hashtable helpers. */
7778 struct decl_table_entry_hasher : free_ptr_hash <decl_table_entry>
7780 typedef die_struct *compare_type;
7781 static inline hashval_t hash (const decl_table_entry *);
7782 static inline bool equal (const decl_table_entry *, const die_struct *);
7785 inline hashval_t
7786 decl_table_entry_hasher::hash (const decl_table_entry *entry)
7788 return htab_hash_pointer (entry->orig);
7791 inline bool
7792 decl_table_entry_hasher::equal (const decl_table_entry *entry1,
7793 const die_struct *entry2)
7795 return entry1->orig == entry2;
7798 typedef hash_table<decl_table_entry_hasher> decl_hash_type;
7800 /* Copy DIE and its ancestors, up to, but not including, the compile unit
7801 or type unit entry, to a new tree. Adds the new tree to UNIT and returns
7802 a pointer to the copy of DIE. If DECL_TABLE is provided, it is used
7803 to check if the ancestor has already been copied into UNIT. */
7805 static dw_die_ref
7806 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die,
7807 decl_hash_type *decl_table)
7809 dw_die_ref parent = die->die_parent;
7810 dw_die_ref new_parent = unit;
7811 dw_die_ref copy;
7812 decl_table_entry **slot = NULL;
7813 struct decl_table_entry *entry = NULL;
7815 if (decl_table)
7817 /* Check if the entry has already been copied to UNIT. */
7818 slot = decl_table->find_slot_with_hash (die, htab_hash_pointer (die),
7819 INSERT);
7820 if (*slot != HTAB_EMPTY_ENTRY)
7822 entry = *slot;
7823 return entry->copy;
7826 /* Record in DECL_TABLE that DIE has been copied to UNIT. */
7827 entry = XCNEW (struct decl_table_entry);
7828 entry->orig = die;
7829 entry->copy = NULL;
7830 *slot = entry;
7833 if (parent != NULL)
7835 dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
7836 if (spec != NULL)
7837 parent = spec;
7838 if (!is_unit_die (parent))
7839 new_parent = copy_ancestor_tree (unit, parent, decl_table);
7842 copy = clone_as_declaration (die);
7843 add_child_die (new_parent, copy);
7845 if (decl_table)
7847 /* Record the pointer to the copy. */
7848 entry->copy = copy;
7851 return copy;
7853 /* Copy the declaration context to the new type unit DIE. This includes
7854 any surrounding namespace or type declarations. If the DIE has an
7855 AT_specification attribute, it also includes attributes and children
7856 attached to the specification, and returns a pointer to the original
7857 parent of the declaration DIE. Returns NULL otherwise. */
7859 static dw_die_ref
7860 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
7862 dw_die_ref decl;
7863 dw_die_ref new_decl;
7864 dw_die_ref orig_parent = NULL;
7866 decl = get_AT_ref (die, DW_AT_specification);
7867 if (decl == NULL)
7868 decl = die;
7869 else
7871 unsigned ix;
7872 dw_die_ref c;
7873 dw_attr_node *a;
7875 /* The original DIE will be changed to a declaration, and must
7876 be moved to be a child of the original declaration DIE. */
7877 orig_parent = decl->die_parent;
7879 /* Copy the type node pointer from the new DIE to the original
7880 declaration DIE so we can forward references later. */
7881 decl->comdat_type_p = true;
7882 decl->die_id.die_type_node = die->die_id.die_type_node;
7884 remove_AT (die, DW_AT_specification);
7886 FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
7888 if (a->dw_attr != DW_AT_name
7889 && a->dw_attr != DW_AT_declaration
7890 && a->dw_attr != DW_AT_external)
7891 add_dwarf_attr (die, a);
7894 FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree (c)));
7897 if (decl->die_parent != NULL
7898 && !is_unit_die (decl->die_parent))
7900 new_decl = copy_ancestor_tree (unit, decl, NULL);
7901 if (new_decl != NULL)
7903 remove_AT (new_decl, DW_AT_signature);
7904 add_AT_specification (die, new_decl);
7908 return orig_parent;
7911 /* Generate the skeleton ancestor tree for the given NODE, then clone
7912 the DIE and add the clone into the tree. */
7914 static void
7915 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
7917 if (node->new_die != NULL)
7918 return;
7920 node->new_die = clone_as_declaration (node->old_die);
7922 if (node->parent != NULL)
7924 generate_skeleton_ancestor_tree (node->parent);
7925 add_child_die (node->parent->new_die, node->new_die);
7929 /* Generate a skeleton tree of DIEs containing any declarations that are
7930 found in the original tree. We traverse the tree looking for declaration
7931 DIEs, and construct the skeleton from the bottom up whenever we find one. */
7933 static void
7934 generate_skeleton_bottom_up (skeleton_chain_node *parent)
7936 skeleton_chain_node node;
7937 dw_die_ref c;
7938 dw_die_ref first;
7939 dw_die_ref prev = NULL;
7940 dw_die_ref next = NULL;
7942 node.parent = parent;
7944 first = c = parent->old_die->die_child;
7945 if (c)
7946 next = c->die_sib;
7947 if (c) do {
7948 if (prev == NULL || prev->die_sib == c)
7949 prev = c;
7950 c = next;
7951 next = (c == first ? NULL : c->die_sib);
7952 node.old_die = c;
7953 node.new_die = NULL;
7954 if (is_declaration_die (c))
7956 if (is_template_instantiation (c))
7958 /* Instantiated templates do not need to be cloned into the
7959 type unit. Just move the DIE and its children back to
7960 the skeleton tree (in the main CU). */
7961 remove_child_with_prev (c, prev);
7962 add_child_die (parent->new_die, c);
7963 c = prev;
7965 else if (c->comdat_type_p)
7967 /* This is the skeleton of earlier break_out_comdat_types
7968 type. Clone the existing DIE, but keep the children
7969 under the original (which is in the main CU). */
7970 dw_die_ref clone = clone_die (c);
7972 replace_child (c, clone, prev);
7973 generate_skeleton_ancestor_tree (parent);
7974 add_child_die (parent->new_die, c);
7975 c = clone;
7976 continue;
7978 else
7980 /* Clone the existing DIE, move the original to the skeleton
7981 tree (which is in the main CU), and put the clone, with
7982 all the original's children, where the original came from
7983 (which is about to be moved to the type unit). */
7984 dw_die_ref clone = clone_die (c);
7985 move_all_children (c, clone);
7987 /* If the original has a DW_AT_object_pointer attribute,
7988 it would now point to a child DIE just moved to the
7989 cloned tree, so we need to remove that attribute from
7990 the original. */
7991 remove_AT (c, DW_AT_object_pointer);
7993 replace_child (c, clone, prev);
7994 generate_skeleton_ancestor_tree (parent);
7995 add_child_die (parent->new_die, c);
7996 node.old_die = clone;
7997 node.new_die = c;
7998 c = clone;
8001 generate_skeleton_bottom_up (&node);
8002 } while (next != NULL);
8005 /* Wrapper function for generate_skeleton_bottom_up. */
8007 static dw_die_ref
8008 generate_skeleton (dw_die_ref die)
8010 skeleton_chain_node node;
8012 node.old_die = die;
8013 node.new_die = NULL;
8014 node.parent = NULL;
8016 /* If this type definition is nested inside another type,
8017 and is not an instantiation of a template, always leave
8018 at least a declaration in its place. */
8019 if (die->die_parent != NULL
8020 && is_type_die (die->die_parent)
8021 && !is_template_instantiation (die))
8022 node.new_die = clone_as_declaration (die);
8024 generate_skeleton_bottom_up (&node);
8025 return node.new_die;
8028 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
8029 declaration. The original DIE is moved to a new compile unit so that
8030 existing references to it follow it to the new location. If any of the
8031 original DIE's descendants is a declaration, we need to replace the
8032 original DIE with a skeleton tree and move the declarations back into the
8033 skeleton tree. */
8035 static dw_die_ref
8036 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
8037 dw_die_ref prev)
8039 dw_die_ref skeleton, orig_parent;
8041 /* Copy the declaration context to the type unit DIE. If the returned
8042 ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
8043 that DIE. */
8044 orig_parent = copy_declaration_context (unit, child);
8046 skeleton = generate_skeleton (child);
8047 if (skeleton == NULL)
8048 remove_child_with_prev (child, prev);
8049 else
8051 skeleton->comdat_type_p = true;
8052 skeleton->die_id.die_type_node = child->die_id.die_type_node;
8054 /* If the original DIE was a specification, we need to put
8055 the skeleton under the parent DIE of the declaration.
8056 This leaves the original declaration in the tree, but
8057 it will be pruned later since there are no longer any
8058 references to it. */
8059 if (orig_parent != NULL)
8061 remove_child_with_prev (child, prev);
8062 add_child_die (orig_parent, skeleton);
8064 else
8065 replace_child (child, skeleton, prev);
8068 return skeleton;
8071 static void
8072 copy_dwarf_procs_ref_in_attrs (dw_die_ref die,
8073 comdat_type_node *type_node,
8074 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs);
8076 /* Helper for copy_dwarf_procs_ref_in_dies. Make a copy of the DIE DWARF
8077 procedure, put it under TYPE_NODE and return the copy. Continue looking for
8078 DWARF procedure references in the DW_AT_location attribute. */
8080 static dw_die_ref
8081 copy_dwarf_procedure (dw_die_ref die,
8082 comdat_type_node *type_node,
8083 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8085 gcc_assert (die->die_tag == DW_TAG_dwarf_procedure);
8087 /* DWARF procedures are not supposed to have children... */
8088 gcc_assert (die->die_child == NULL);
8090 /* ... and they are supposed to have only one attribute: DW_AT_location. */
8091 gcc_assert (vec_safe_length (die->die_attr) == 1
8092 && ((*die->die_attr)[0].dw_attr == DW_AT_location));
8094 /* Do not copy more than once DWARF procedures. */
8095 bool existed;
8096 dw_die_ref &die_copy = copied_dwarf_procs.get_or_insert (die, &existed);
8097 if (existed)
8098 return die_copy;
8100 die_copy = clone_die (die);
8101 add_child_die (type_node->root_die, die_copy);
8102 copy_dwarf_procs_ref_in_attrs (die_copy, type_node, copied_dwarf_procs);
8103 return die_copy;
8106 /* Helper for copy_dwarf_procs_ref_in_dies. Look for references to DWARF
8107 procedures in DIE's attributes. */
8109 static void
8110 copy_dwarf_procs_ref_in_attrs (dw_die_ref die,
8111 comdat_type_node *type_node,
8112 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8114 dw_attr_node *a;
8115 unsigned i;
8117 FOR_EACH_VEC_SAFE_ELT (die->die_attr, i, a)
8119 dw_loc_descr_ref loc;
8121 if (a->dw_attr_val.val_class != dw_val_class_loc)
8122 continue;
8124 for (loc = a->dw_attr_val.v.val_loc; loc != NULL; loc = loc->dw_loc_next)
8126 switch (loc->dw_loc_opc)
8128 case DW_OP_call2:
8129 case DW_OP_call4:
8130 case DW_OP_call_ref:
8131 gcc_assert (loc->dw_loc_oprnd1.val_class
8132 == dw_val_class_die_ref);
8133 loc->dw_loc_oprnd1.v.val_die_ref.die
8134 = copy_dwarf_procedure (loc->dw_loc_oprnd1.v.val_die_ref.die,
8135 type_node,
8136 copied_dwarf_procs);
8138 default:
8139 break;
8145 /* Copy DWARF procedures that are referenced by the DIE tree to TREE_NODE and
8146 rewrite references to point to the copies.
8148 References are looked for in DIE's attributes and recursively in all its
8149 children attributes that are location descriptions. COPIED_DWARF_PROCS is a
8150 mapping from old DWARF procedures to their copy. It is used not to copy
8151 twice the same DWARF procedure under TYPE_NODE. */
8153 static void
8154 copy_dwarf_procs_ref_in_dies (dw_die_ref die,
8155 comdat_type_node *type_node,
8156 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8158 dw_die_ref c;
8160 copy_dwarf_procs_ref_in_attrs (die, type_node, copied_dwarf_procs);
8161 FOR_EACH_CHILD (die, c, copy_dwarf_procs_ref_in_dies (c,
8162 type_node,
8163 copied_dwarf_procs));
8166 /* Traverse the DIE and set up additional .debug_types or .debug_info
8167 DW_UT_*type sections for each type worthy of being placed in a COMDAT
8168 section. */
8170 static void
8171 break_out_comdat_types (dw_die_ref die)
8173 dw_die_ref c;
8174 dw_die_ref first;
8175 dw_die_ref prev = NULL;
8176 dw_die_ref next = NULL;
8177 dw_die_ref unit = NULL;
8179 first = c = die->die_child;
8180 if (c)
8181 next = c->die_sib;
8182 if (c) do {
8183 if (prev == NULL || prev->die_sib == c)
8184 prev = c;
8185 c = next;
8186 next = (c == first ? NULL : c->die_sib);
8187 if (should_move_die_to_comdat (c))
8189 dw_die_ref replacement;
8190 comdat_type_node *type_node;
8192 /* Break out nested types into their own type units. */
8193 break_out_comdat_types (c);
8195 /* Create a new type unit DIE as the root for the new tree, and
8196 add it to the list of comdat types. */
8197 unit = new_die (DW_TAG_type_unit, NULL, NULL);
8198 add_AT_unsigned (unit, DW_AT_language,
8199 get_AT_unsigned (comp_unit_die (), DW_AT_language));
8200 type_node = ggc_cleared_alloc<comdat_type_node> ();
8201 type_node->root_die = unit;
8202 type_node->next = comdat_type_list;
8203 comdat_type_list = type_node;
8205 /* Generate the type signature. */
8206 generate_type_signature (c, type_node);
8208 /* Copy the declaration context, attributes, and children of the
8209 declaration into the new type unit DIE, then remove this DIE
8210 from the main CU (or replace it with a skeleton if necessary). */
8211 replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
8212 type_node->skeleton_die = replacement;
8214 /* Add the DIE to the new compunit. */
8215 add_child_die (unit, c);
8217 /* Types can reference DWARF procedures for type size or data location
8218 expressions. Calls in DWARF expressions cannot target procedures
8219 that are not in the same section. So we must copy DWARF procedures
8220 along with this type and then rewrite references to them. */
8221 hash_map<dw_die_ref, dw_die_ref> copied_dwarf_procs;
8222 copy_dwarf_procs_ref_in_dies (c, type_node, copied_dwarf_procs);
8224 if (replacement != NULL)
8225 c = replacement;
8227 else if (c->die_tag == DW_TAG_namespace
8228 || c->die_tag == DW_TAG_class_type
8229 || c->die_tag == DW_TAG_structure_type
8230 || c->die_tag == DW_TAG_union_type)
8232 /* Look for nested types that can be broken out. */
8233 break_out_comdat_types (c);
8235 } while (next != NULL);
8238 /* Like clone_tree, but copy DW_TAG_subprogram DIEs as declarations.
8239 Enter all the cloned children into the hash table decl_table. */
8241 static dw_die_ref
8242 clone_tree_partial (dw_die_ref die, decl_hash_type *decl_table)
8244 dw_die_ref c;
8245 dw_die_ref clone;
8246 struct decl_table_entry *entry;
8247 decl_table_entry **slot;
8249 if (die->die_tag == DW_TAG_subprogram)
8250 clone = clone_as_declaration (die);
8251 else
8252 clone = clone_die (die);
8254 slot = decl_table->find_slot_with_hash (die,
8255 htab_hash_pointer (die), INSERT);
8257 /* Assert that DIE isn't in the hash table yet. If it would be there
8258 before, the ancestors would be necessarily there as well, therefore
8259 clone_tree_partial wouldn't be called. */
8260 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
8262 entry = XCNEW (struct decl_table_entry);
8263 entry->orig = die;
8264 entry->copy = clone;
8265 *slot = entry;
8267 if (die->die_tag != DW_TAG_subprogram)
8268 FOR_EACH_CHILD (die, c,
8269 add_child_die (clone, clone_tree_partial (c, decl_table)));
8271 return clone;
8274 /* Walk the DIE and its children, looking for references to incomplete
8275 or trivial types that are unmarked (i.e., that are not in the current
8276 type_unit). */
8278 static void
8279 copy_decls_walk (dw_die_ref unit, dw_die_ref die, decl_hash_type *decl_table)
8281 dw_die_ref c;
8282 dw_attr_node *a;
8283 unsigned ix;
8285 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8287 if (AT_class (a) == dw_val_class_die_ref)
8289 dw_die_ref targ = AT_ref (a);
8290 decl_table_entry **slot;
8291 struct decl_table_entry *entry;
8293 if (targ->die_mark != 0 || targ->comdat_type_p)
8294 continue;
8296 slot = decl_table->find_slot_with_hash (targ,
8297 htab_hash_pointer (targ),
8298 INSERT);
8300 if (*slot != HTAB_EMPTY_ENTRY)
8302 /* TARG has already been copied, so we just need to
8303 modify the reference to point to the copy. */
8304 entry = *slot;
8305 a->dw_attr_val.v.val_die_ref.die = entry->copy;
8307 else
8309 dw_die_ref parent = unit;
8310 dw_die_ref copy = clone_die (targ);
8312 /* Record in DECL_TABLE that TARG has been copied.
8313 Need to do this now, before the recursive call,
8314 because DECL_TABLE may be expanded and SLOT
8315 would no longer be a valid pointer. */
8316 entry = XCNEW (struct decl_table_entry);
8317 entry->orig = targ;
8318 entry->copy = copy;
8319 *slot = entry;
8321 /* If TARG is not a declaration DIE, we need to copy its
8322 children. */
8323 if (!is_declaration_die (targ))
8325 FOR_EACH_CHILD (
8326 targ, c,
8327 add_child_die (copy,
8328 clone_tree_partial (c, decl_table)));
8331 /* Make sure the cloned tree is marked as part of the
8332 type unit. */
8333 mark_dies (copy);
8335 /* If TARG has surrounding context, copy its ancestor tree
8336 into the new type unit. */
8337 if (targ->die_parent != NULL
8338 && !is_unit_die (targ->die_parent))
8339 parent = copy_ancestor_tree (unit, targ->die_parent,
8340 decl_table);
8342 add_child_die (parent, copy);
8343 a->dw_attr_val.v.val_die_ref.die = copy;
8345 /* Make sure the newly-copied DIE is walked. If it was
8346 installed in a previously-added context, it won't
8347 get visited otherwise. */
8348 if (parent != unit)
8350 /* Find the highest point of the newly-added tree,
8351 mark each node along the way, and walk from there. */
8352 parent->die_mark = 1;
8353 while (parent->die_parent
8354 && parent->die_parent->die_mark == 0)
8356 parent = parent->die_parent;
8357 parent->die_mark = 1;
8359 copy_decls_walk (unit, parent, decl_table);
8365 FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
8368 /* Copy declarations for "unworthy" types into the new comdat section.
8369 Incomplete types, modified types, and certain other types aren't broken
8370 out into comdat sections of their own, so they don't have a signature,
8371 and we need to copy the declaration into the same section so that we
8372 don't have an external reference. */
8374 static void
8375 copy_decls_for_unworthy_types (dw_die_ref unit)
8377 mark_dies (unit);
8378 decl_hash_type decl_table (10);
8379 copy_decls_walk (unit, unit, &decl_table);
8380 unmark_dies (unit);
8383 /* Traverse the DIE and add a sibling attribute if it may have the
8384 effect of speeding up access to siblings. To save some space,
8385 avoid generating sibling attributes for DIE's without children. */
8387 static void
8388 add_sibling_attributes (dw_die_ref die)
8390 dw_die_ref c;
8392 if (! die->die_child)
8393 return;
8395 if (die->die_parent && die != die->die_parent->die_child)
8396 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
8398 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
8401 /* Output all location lists for the DIE and its children. */
8403 static void
8404 output_location_lists (dw_die_ref die)
8406 dw_die_ref c;
8407 dw_attr_node *a;
8408 unsigned ix;
8410 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8411 if (AT_class (a) == dw_val_class_loc_list)
8412 output_loc_list (AT_loc_list (a));
8414 FOR_EACH_CHILD (die, c, output_location_lists (c));
8417 /* During assign_location_list_indexes and output_loclists_offset the
8418 current index, after it the number of assigned indexes (i.e. how
8419 large the .debug_loclists* offset table should be). */
8420 static unsigned int loc_list_idx;
8422 /* Output all location list offsets for the DIE and its children. */
8424 static void
8425 output_loclists_offsets (dw_die_ref die)
8427 dw_die_ref c;
8428 dw_attr_node *a;
8429 unsigned ix;
8431 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8432 if (AT_class (a) == dw_val_class_loc_list)
8434 dw_loc_list_ref l = AT_loc_list (a);
8435 if (l->offset_emitted)
8436 continue;
8437 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l->ll_symbol,
8438 loc_section_label, NULL);
8439 gcc_assert (l->hash == loc_list_idx);
8440 loc_list_idx++;
8441 l->offset_emitted = true;
8444 FOR_EACH_CHILD (die, c, output_loclists_offsets (c));
8447 /* Recursively set indexes of location lists. */
8449 static void
8450 assign_location_list_indexes (dw_die_ref die)
8452 dw_die_ref c;
8453 dw_attr_node *a;
8454 unsigned ix;
8456 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8457 if (AT_class (a) == dw_val_class_loc_list)
8459 dw_loc_list_ref list = AT_loc_list (a);
8460 if (!list->num_assigned)
8462 list->num_assigned = true;
8463 list->hash = loc_list_idx++;
8467 FOR_EACH_CHILD (die, c, assign_location_list_indexes (c));
8470 /* We want to limit the number of external references, because they are
8471 larger than local references: a relocation takes multiple words, and
8472 even a sig8 reference is always eight bytes, whereas a local reference
8473 can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
8474 So if we encounter multiple external references to the same type DIE, we
8475 make a local typedef stub for it and redirect all references there.
8477 This is the element of the hash table for keeping track of these
8478 references. */
8480 struct external_ref
8482 dw_die_ref type;
8483 dw_die_ref stub;
8484 unsigned n_refs;
8487 /* Hashtable helpers. */
8489 struct external_ref_hasher : free_ptr_hash <external_ref>
8491 static inline hashval_t hash (const external_ref *);
8492 static inline bool equal (const external_ref *, const external_ref *);
8495 inline hashval_t
8496 external_ref_hasher::hash (const external_ref *r)
8498 dw_die_ref die = r->type;
8499 hashval_t h = 0;
8501 /* We can't use the address of the DIE for hashing, because
8502 that will make the order of the stub DIEs non-deterministic. */
8503 if (! die->comdat_type_p)
8504 /* We have a symbol; use it to compute a hash. */
8505 h = htab_hash_string (die->die_id.die_symbol);
8506 else
8508 /* We have a type signature; use a subset of the bits as the hash.
8509 The 8-byte signature is at least as large as hashval_t. */
8510 comdat_type_node *type_node = die->die_id.die_type_node;
8511 memcpy (&h, type_node->signature, sizeof (h));
8513 return h;
8516 inline bool
8517 external_ref_hasher::equal (const external_ref *r1, const external_ref *r2)
8519 return r1->type == r2->type;
8522 typedef hash_table<external_ref_hasher> external_ref_hash_type;
8524 /* Return a pointer to the external_ref for references to DIE. */
8526 static struct external_ref *
8527 lookup_external_ref (external_ref_hash_type *map, dw_die_ref die)
8529 struct external_ref ref, *ref_p;
8530 external_ref **slot;
8532 ref.type = die;
8533 slot = map->find_slot (&ref, INSERT);
8534 if (*slot != HTAB_EMPTY_ENTRY)
8535 return *slot;
8537 ref_p = XCNEW (struct external_ref);
8538 ref_p->type = die;
8539 *slot = ref_p;
8540 return ref_p;
8543 /* Subroutine of optimize_external_refs, below.
8545 If we see a type skeleton, record it as our stub. If we see external
8546 references, remember how many we've seen. */
8548 static void
8549 optimize_external_refs_1 (dw_die_ref die, external_ref_hash_type *map)
8551 dw_die_ref c;
8552 dw_attr_node *a;
8553 unsigned ix;
8554 struct external_ref *ref_p;
8556 if (is_type_die (die)
8557 && (c = get_AT_ref (die, DW_AT_signature)))
8559 /* This is a local skeleton; use it for local references. */
8560 ref_p = lookup_external_ref (map, c);
8561 ref_p->stub = die;
8564 /* Scan the DIE references, and remember any that refer to DIEs from
8565 other CUs (i.e. those which are not marked). */
8566 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8567 if (AT_class (a) == dw_val_class_die_ref
8568 && (c = AT_ref (a))->die_mark == 0
8569 && is_type_die (c))
8571 ref_p = lookup_external_ref (map, c);
8572 ref_p->n_refs++;
8575 FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
8578 /* htab_traverse callback function for optimize_external_refs, below. SLOT
8579 points to an external_ref, DATA is the CU we're processing. If we don't
8580 already have a local stub, and we have multiple refs, build a stub. */
8583 dwarf2_build_local_stub (external_ref **slot, dw_die_ref data)
8585 struct external_ref *ref_p = *slot;
8587 if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
8589 /* We have multiple references to this type, so build a small stub.
8590 Both of these forms are a bit dodgy from the perspective of the
8591 DWARF standard, since technically they should have names. */
8592 dw_die_ref cu = data;
8593 dw_die_ref type = ref_p->type;
8594 dw_die_ref stub = NULL;
8596 if (type->comdat_type_p)
8598 /* If we refer to this type via sig8, use AT_signature. */
8599 stub = new_die (type->die_tag, cu, NULL_TREE);
8600 add_AT_die_ref (stub, DW_AT_signature, type);
8602 else
8604 /* Otherwise, use a typedef with no name. */
8605 stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
8606 add_AT_die_ref (stub, DW_AT_type, type);
8609 stub->die_mark++;
8610 ref_p->stub = stub;
8612 return 1;
8615 /* DIE is a unit; look through all the DIE references to see if there are
8616 any external references to types, and if so, create local stubs for
8617 them which will be applied in build_abbrev_table. This is useful because
8618 references to local DIEs are smaller. */
8620 static external_ref_hash_type *
8621 optimize_external_refs (dw_die_ref die)
8623 external_ref_hash_type *map = new external_ref_hash_type (10);
8624 optimize_external_refs_1 (die, map);
8625 map->traverse <dw_die_ref, dwarf2_build_local_stub> (die);
8626 return map;
8629 /* The following 3 variables are temporaries that are computed only during the
8630 build_abbrev_table call and used and released during the following
8631 optimize_abbrev_table call. */
8633 /* First abbrev_id that can be optimized based on usage. */
8634 static unsigned int abbrev_opt_start;
8636 /* Maximum abbrev_id of a base type plus one (we can't optimize DIEs with
8637 abbrev_id smaller than this, because they must be already sized
8638 during build_abbrev_table). */
8639 static unsigned int abbrev_opt_base_type_end;
8641 /* Vector of usage counts during build_abbrev_table. Indexed by
8642 abbrev_id - abbrev_opt_start. */
8643 static vec<unsigned int> abbrev_usage_count;
8645 /* Vector of all DIEs added with die_abbrev >= abbrev_opt_start. */
8646 static vec<dw_die_ref> sorted_abbrev_dies;
8648 /* The format of each DIE (and its attribute value pairs) is encoded in an
8649 abbreviation table. This routine builds the abbreviation table and assigns
8650 a unique abbreviation id for each abbreviation entry. The children of each
8651 die are visited recursively. */
8653 static void
8654 build_abbrev_table (dw_die_ref die, external_ref_hash_type *extern_map)
8656 unsigned int abbrev_id = 0;
8657 dw_die_ref c;
8658 dw_attr_node *a;
8659 unsigned ix;
8660 dw_die_ref abbrev;
8662 /* Scan the DIE references, and replace any that refer to
8663 DIEs from other CUs (i.e. those which are not marked) with
8664 the local stubs we built in optimize_external_refs. */
8665 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8666 if (AT_class (a) == dw_val_class_die_ref
8667 && (c = AT_ref (a))->die_mark == 0)
8669 struct external_ref *ref_p;
8670 gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
8672 ref_p = lookup_external_ref (extern_map, c);
8673 if (ref_p->stub && ref_p->stub != die)
8674 change_AT_die_ref (a, ref_p->stub);
8675 else
8676 /* We aren't changing this reference, so mark it external. */
8677 set_AT_ref_external (a, 1);
8680 FOR_EACH_VEC_SAFE_ELT (abbrev_die_table, abbrev_id, abbrev)
8682 dw_attr_node *die_a, *abbrev_a;
8683 unsigned ix;
8684 bool ok = true;
8686 if (abbrev_id == 0)
8687 continue;
8688 if (abbrev->die_tag != die->die_tag)
8689 continue;
8690 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
8691 continue;
8693 if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
8694 continue;
8696 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
8698 abbrev_a = &(*abbrev->die_attr)[ix];
8699 if ((abbrev_a->dw_attr != die_a->dw_attr)
8700 || (value_format (abbrev_a) != value_format (die_a)))
8702 ok = false;
8703 break;
8706 if (ok)
8707 break;
8710 if (abbrev_id >= vec_safe_length (abbrev_die_table))
8712 vec_safe_push (abbrev_die_table, die);
8713 if (abbrev_opt_start)
8714 abbrev_usage_count.safe_push (0);
8716 if (abbrev_opt_start && abbrev_id >= abbrev_opt_start)
8718 abbrev_usage_count[abbrev_id - abbrev_opt_start]++;
8719 sorted_abbrev_dies.safe_push (die);
8722 die->die_abbrev = abbrev_id;
8723 FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
8726 /* Callback function for sorted_abbrev_dies vector sorting. We sort
8727 by die_abbrev's usage count, from the most commonly used
8728 abbreviation to the least. */
8730 static int
8731 die_abbrev_cmp (const void *p1, const void *p2)
8733 dw_die_ref die1 = *(const dw_die_ref *) p1;
8734 dw_die_ref die2 = *(const dw_die_ref *) p2;
8736 gcc_checking_assert (die1->die_abbrev >= abbrev_opt_start);
8737 gcc_checking_assert (die2->die_abbrev >= abbrev_opt_start);
8739 if (die1->die_abbrev >= abbrev_opt_base_type_end
8740 && die2->die_abbrev >= abbrev_opt_base_type_end)
8742 if (abbrev_usage_count[die1->die_abbrev - abbrev_opt_start]
8743 > abbrev_usage_count[die2->die_abbrev - abbrev_opt_start])
8744 return -1;
8745 if (abbrev_usage_count[die1->die_abbrev - abbrev_opt_start]
8746 < abbrev_usage_count[die2->die_abbrev - abbrev_opt_start])
8747 return 1;
8750 /* Stabilize the sort. */
8751 if (die1->die_abbrev < die2->die_abbrev)
8752 return -1;
8753 if (die1->die_abbrev > die2->die_abbrev)
8754 return 1;
8756 return 0;
8759 /* Convert dw_val_class_const and dw_val_class_unsigned_const class attributes
8760 of DIEs in between sorted_abbrev_dies[first_id] and abbrev_dies[end_id - 1]
8761 into dw_val_class_const_implicit or
8762 dw_val_class_unsigned_const_implicit. */
8764 static void
8765 optimize_implicit_const (unsigned int first_id, unsigned int end,
8766 vec<bool> &implicit_consts)
8768 /* It never makes sense if there is just one DIE using the abbreviation. */
8769 if (end < first_id + 2)
8770 return;
8772 dw_attr_node *a;
8773 unsigned ix, i;
8774 dw_die_ref die = sorted_abbrev_dies[first_id];
8775 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8776 if (implicit_consts[ix])
8778 enum dw_val_class new_class = dw_val_class_none;
8779 switch (AT_class (a))
8781 case dw_val_class_unsigned_const:
8782 if ((HOST_WIDE_INT) AT_unsigned (a) < 0)
8783 continue;
8785 /* The .debug_abbrev section will grow by
8786 size_of_sleb128 (AT_unsigned (a)) and we avoid the constants
8787 in all the DIEs using that abbreviation. */
8788 if (constant_size (AT_unsigned (a)) * (end - first_id)
8789 <= (unsigned) size_of_sleb128 (AT_unsigned (a)))
8790 continue;
8792 new_class = dw_val_class_unsigned_const_implicit;
8793 break;
8795 case dw_val_class_const:
8796 new_class = dw_val_class_const_implicit;
8797 break;
8799 case dw_val_class_file:
8800 new_class = dw_val_class_file_implicit;
8801 break;
8803 default:
8804 continue;
8806 for (i = first_id; i < end; i++)
8807 (*sorted_abbrev_dies[i]->die_attr)[ix].dw_attr_val.val_class
8808 = new_class;
8812 /* Attempt to optimize abbreviation table from abbrev_opt_start
8813 abbreviation above. */
8815 static void
8816 optimize_abbrev_table (void)
8818 if (abbrev_opt_start
8819 && vec_safe_length (abbrev_die_table) > abbrev_opt_start
8820 && (dwarf_version >= 5 || vec_safe_length (abbrev_die_table) > 127))
8822 auto_vec<bool, 32> implicit_consts;
8823 sorted_abbrev_dies.qsort (die_abbrev_cmp);
8825 unsigned int abbrev_id = abbrev_opt_start - 1;
8826 unsigned int first_id = ~0U;
8827 unsigned int last_abbrev_id = 0;
8828 unsigned int i;
8829 dw_die_ref die;
8830 if (abbrev_opt_base_type_end > abbrev_opt_start)
8831 abbrev_id = abbrev_opt_base_type_end - 1;
8832 /* Reassign abbreviation ids from abbrev_opt_start above, so that
8833 most commonly used abbreviations come first. */
8834 FOR_EACH_VEC_ELT (sorted_abbrev_dies, i, die)
8836 dw_attr_node *a;
8837 unsigned ix;
8839 /* If calc_base_type_die_sizes has been called, the CU and
8840 base types after it can't be optimized, because we've already
8841 calculated their DIE offsets. We've sorted them first. */
8842 if (die->die_abbrev < abbrev_opt_base_type_end)
8843 continue;
8844 if (die->die_abbrev != last_abbrev_id)
8846 last_abbrev_id = die->die_abbrev;
8847 if (dwarf_version >= 5 && first_id != ~0U)
8848 optimize_implicit_const (first_id, i, implicit_consts);
8849 abbrev_id++;
8850 (*abbrev_die_table)[abbrev_id] = die;
8851 if (dwarf_version >= 5)
8853 first_id = i;
8854 implicit_consts.truncate (0);
8856 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8857 switch (AT_class (a))
8859 case dw_val_class_const:
8860 case dw_val_class_unsigned_const:
8861 case dw_val_class_file:
8862 implicit_consts.safe_push (true);
8863 break;
8864 default:
8865 implicit_consts.safe_push (false);
8866 break;
8870 else if (dwarf_version >= 5)
8872 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8873 if (!implicit_consts[ix])
8874 continue;
8875 else
8877 dw_attr_node *other_a
8878 = &(*(*abbrev_die_table)[abbrev_id]->die_attr)[ix];
8879 if (!dw_val_equal_p (&a->dw_attr_val,
8880 &other_a->dw_attr_val))
8881 implicit_consts[ix] = false;
8884 die->die_abbrev = abbrev_id;
8886 gcc_assert (abbrev_id == vec_safe_length (abbrev_die_table) - 1);
8887 if (dwarf_version >= 5 && first_id != ~0U)
8888 optimize_implicit_const (first_id, i, implicit_consts);
8891 abbrev_opt_start = 0;
8892 abbrev_opt_base_type_end = 0;
8893 abbrev_usage_count.release ();
8894 sorted_abbrev_dies.release ();
8897 /* Return the power-of-two number of bytes necessary to represent VALUE. */
8899 static int
8900 constant_size (unsigned HOST_WIDE_INT value)
8902 int log;
8904 if (value == 0)
8905 log = 0;
8906 else
8907 log = floor_log2 (value);
8909 log = log / 8;
8910 log = 1 << (floor_log2 (log) + 1);
8912 return log;
8915 /* Return the size of a DIE as it is represented in the
8916 .debug_info section. */
8918 static unsigned long
8919 size_of_die (dw_die_ref die)
8921 unsigned long size = 0;
8922 dw_attr_node *a;
8923 unsigned ix;
8924 enum dwarf_form form;
8926 size += size_of_uleb128 (die->die_abbrev);
8927 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8929 switch (AT_class (a))
8931 case dw_val_class_addr:
8932 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8934 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8935 size += size_of_uleb128 (AT_index (a));
8937 else
8938 size += DWARF2_ADDR_SIZE;
8939 break;
8940 case dw_val_class_offset:
8941 size += DWARF_OFFSET_SIZE;
8942 break;
8943 case dw_val_class_loc:
8945 unsigned long lsize = size_of_locs (AT_loc (a));
8947 /* Block length. */
8948 if (dwarf_version >= 4)
8949 size += size_of_uleb128 (lsize);
8950 else
8951 size += constant_size (lsize);
8952 size += lsize;
8954 break;
8955 case dw_val_class_loc_list:
8956 if (dwarf_split_debug_info && dwarf_version >= 5)
8958 gcc_assert (AT_loc_list (a)->num_assigned);
8959 size += size_of_uleb128 (AT_loc_list (a)->hash);
8961 else
8962 size += DWARF_OFFSET_SIZE;
8963 break;
8964 case dw_val_class_range_list:
8965 if (value_format (a) == DW_FORM_rnglistx)
8967 gcc_assert (rnglist_idx);
8968 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
8969 size += size_of_uleb128 (r->idx);
8971 else
8972 size += DWARF_OFFSET_SIZE;
8973 break;
8974 case dw_val_class_const:
8975 size += size_of_sleb128 (AT_int (a));
8976 break;
8977 case dw_val_class_unsigned_const:
8979 int csize = constant_size (AT_unsigned (a));
8980 if (dwarf_version == 3
8981 && a->dw_attr == DW_AT_data_member_location
8982 && csize >= 4)
8983 size += size_of_uleb128 (AT_unsigned (a));
8984 else
8985 size += csize;
8987 break;
8988 case dw_val_class_const_implicit:
8989 case dw_val_class_unsigned_const_implicit:
8990 case dw_val_class_file_implicit:
8991 /* These occupy no size in the DIE, just an extra sleb128 in
8992 .debug_abbrev. */
8993 break;
8994 case dw_val_class_const_double:
8995 size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
8996 if (HOST_BITS_PER_WIDE_INT >= DWARF_LARGEST_DATA_FORM_BITS)
8997 size++; /* block */
8998 break;
8999 case dw_val_class_wide_int:
9000 size += (get_full_len (*a->dw_attr_val.v.val_wide)
9001 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
9002 if (get_full_len (*a->dw_attr_val.v.val_wide)
9003 * HOST_BITS_PER_WIDE_INT > DWARF_LARGEST_DATA_FORM_BITS)
9004 size++; /* block */
9005 break;
9006 case dw_val_class_vec:
9007 size += constant_size (a->dw_attr_val.v.val_vec.length
9008 * a->dw_attr_val.v.val_vec.elt_size)
9009 + a->dw_attr_val.v.val_vec.length
9010 * a->dw_attr_val.v.val_vec.elt_size; /* block */
9011 break;
9012 case dw_val_class_flag:
9013 if (dwarf_version >= 4)
9014 /* Currently all add_AT_flag calls pass in 1 as last argument,
9015 so DW_FORM_flag_present can be used. If that ever changes,
9016 we'll need to use DW_FORM_flag and have some optimization
9017 in build_abbrev_table that will change those to
9018 DW_FORM_flag_present if it is set to 1 in all DIEs using
9019 the same abbrev entry. */
9020 gcc_assert (a->dw_attr_val.v.val_flag == 1);
9021 else
9022 size += 1;
9023 break;
9024 case dw_val_class_die_ref:
9025 if (AT_ref_external (a))
9027 /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
9028 we use DW_FORM_ref_addr. In DWARF2, DW_FORM_ref_addr
9029 is sized by target address length, whereas in DWARF3
9030 it's always sized as an offset. */
9031 if (use_debug_types)
9032 size += DWARF_TYPE_SIGNATURE_SIZE;
9033 else if (dwarf_version == 2)
9034 size += DWARF2_ADDR_SIZE;
9035 else
9036 size += DWARF_OFFSET_SIZE;
9038 else
9039 size += DWARF_OFFSET_SIZE;
9040 break;
9041 case dw_val_class_fde_ref:
9042 size += DWARF_OFFSET_SIZE;
9043 break;
9044 case dw_val_class_lbl_id:
9045 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
9047 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
9048 size += size_of_uleb128 (AT_index (a));
9050 else
9051 size += DWARF2_ADDR_SIZE;
9052 break;
9053 case dw_val_class_lineptr:
9054 case dw_val_class_macptr:
9055 case dw_val_class_loclistsptr:
9056 size += DWARF_OFFSET_SIZE;
9057 break;
9058 case dw_val_class_str:
9059 form = AT_string_form (a);
9060 if (form == DW_FORM_strp || form == DW_FORM_line_strp)
9061 size += DWARF_OFFSET_SIZE;
9062 else if (form == DW_FORM_GNU_str_index)
9063 size += size_of_uleb128 (AT_index (a));
9064 else
9065 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
9066 break;
9067 case dw_val_class_file:
9068 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
9069 break;
9070 case dw_val_class_data8:
9071 size += 8;
9072 break;
9073 case dw_val_class_vms_delta:
9074 size += DWARF_OFFSET_SIZE;
9075 break;
9076 case dw_val_class_high_pc:
9077 size += DWARF2_ADDR_SIZE;
9078 break;
9079 case dw_val_class_discr_value:
9080 size += size_of_discr_value (&a->dw_attr_val.v.val_discr_value);
9081 break;
9082 case dw_val_class_discr_list:
9084 unsigned block_size = size_of_discr_list (AT_discr_list (a));
9086 /* This is a block, so we have the block length and then its
9087 data. */
9088 size += constant_size (block_size) + block_size;
9090 break;
9091 default:
9092 gcc_unreachable ();
9096 return size;
9099 /* Size the debugging information associated with a given DIE. Visits the
9100 DIE's children recursively. Updates the global variable next_die_offset, on
9101 each time through. Uses the current value of next_die_offset to update the
9102 die_offset field in each DIE. */
9104 static void
9105 calc_die_sizes (dw_die_ref die)
9107 dw_die_ref c;
9109 gcc_assert (die->die_offset == 0
9110 || (unsigned long int) die->die_offset == next_die_offset);
9111 die->die_offset = next_die_offset;
9112 next_die_offset += size_of_die (die);
9114 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
9116 if (die->die_child != NULL)
9117 /* Count the null byte used to terminate sibling lists. */
9118 next_die_offset += 1;
9121 /* Size just the base type children at the start of the CU.
9122 This is needed because build_abbrev needs to size locs
9123 and sizing of type based stack ops needs to know die_offset
9124 values for the base types. */
9126 static void
9127 calc_base_type_die_sizes (void)
9129 unsigned long die_offset = (dwarf_split_debug_info
9130 ? DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
9131 : DWARF_COMPILE_UNIT_HEADER_SIZE);
9132 unsigned int i;
9133 dw_die_ref base_type;
9134 #if ENABLE_ASSERT_CHECKING
9135 dw_die_ref prev = comp_unit_die ()->die_child;
9136 #endif
9138 die_offset += size_of_die (comp_unit_die ());
9139 for (i = 0; base_types.iterate (i, &base_type); i++)
9141 #if ENABLE_ASSERT_CHECKING
9142 gcc_assert (base_type->die_offset == 0
9143 && prev->die_sib == base_type
9144 && base_type->die_child == NULL
9145 && base_type->die_abbrev);
9146 prev = base_type;
9147 #endif
9148 if (abbrev_opt_start
9149 && base_type->die_abbrev >= abbrev_opt_base_type_end)
9150 abbrev_opt_base_type_end = base_type->die_abbrev + 1;
9151 base_type->die_offset = die_offset;
9152 die_offset += size_of_die (base_type);
9156 /* Set the marks for a die and its children. We do this so
9157 that we know whether or not a reference needs to use FORM_ref_addr; only
9158 DIEs in the same CU will be marked. We used to clear out the offset
9159 and use that as the flag, but ran into ordering problems. */
9161 static void
9162 mark_dies (dw_die_ref die)
9164 dw_die_ref c;
9166 gcc_assert (!die->die_mark);
9168 die->die_mark = 1;
9169 FOR_EACH_CHILD (die, c, mark_dies (c));
9172 /* Clear the marks for a die and its children. */
9174 static void
9175 unmark_dies (dw_die_ref die)
9177 dw_die_ref c;
9179 if (! use_debug_types)
9180 gcc_assert (die->die_mark);
9182 die->die_mark = 0;
9183 FOR_EACH_CHILD (die, c, unmark_dies (c));
9186 /* Clear the marks for a die, its children and referred dies. */
9188 static void
9189 unmark_all_dies (dw_die_ref die)
9191 dw_die_ref c;
9192 dw_attr_node *a;
9193 unsigned ix;
9195 if (!die->die_mark)
9196 return;
9197 die->die_mark = 0;
9199 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
9201 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9202 if (AT_class (a) == dw_val_class_die_ref)
9203 unmark_all_dies (AT_ref (a));
9206 /* Calculate if the entry should appear in the final output file. It may be
9207 from a pruned a type. */
9209 static bool
9210 include_pubname_in_output (vec<pubname_entry, va_gc> *table, pubname_entry *p)
9212 /* By limiting gnu pubnames to definitions only, gold can generate a
9213 gdb index without entries for declarations, which don't include
9214 enough information to be useful. */
9215 if (debug_generate_pub_sections == 2 && is_declaration_die (p->die))
9216 return false;
9218 if (table == pubname_table)
9220 /* Enumerator names are part of the pubname table, but the
9221 parent DW_TAG_enumeration_type die may have been pruned.
9222 Don't output them if that is the case. */
9223 if (p->die->die_tag == DW_TAG_enumerator &&
9224 (p->die->die_parent == NULL
9225 || !p->die->die_parent->die_perennial_p))
9226 return false;
9228 /* Everything else in the pubname table is included. */
9229 return true;
9232 /* The pubtypes table shouldn't include types that have been
9233 pruned. */
9234 return (p->die->die_offset != 0
9235 || !flag_eliminate_unused_debug_types);
9238 /* Return the size of the .debug_pubnames or .debug_pubtypes table
9239 generated for the compilation unit. */
9241 static unsigned long
9242 size_of_pubnames (vec<pubname_entry, va_gc> *names)
9244 unsigned long size;
9245 unsigned i;
9246 pubname_entry *p;
9247 int space_for_flags = (debug_generate_pub_sections == 2) ? 1 : 0;
9249 size = DWARF_PUBNAMES_HEADER_SIZE;
9250 FOR_EACH_VEC_ELT (*names, i, p)
9251 if (include_pubname_in_output (names, p))
9252 size += strlen (p->name) + DWARF_OFFSET_SIZE + 1 + space_for_flags;
9254 size += DWARF_OFFSET_SIZE;
9255 return size;
9258 /* Return the size of the information in the .debug_aranges section. */
9260 static unsigned long
9261 size_of_aranges (void)
9263 unsigned long size;
9265 size = DWARF_ARANGES_HEADER_SIZE;
9267 /* Count the address/length pair for this compilation unit. */
9268 if (text_section_used)
9269 size += 2 * DWARF2_ADDR_SIZE;
9270 if (cold_text_section_used)
9271 size += 2 * DWARF2_ADDR_SIZE;
9272 if (have_multiple_function_sections)
9274 unsigned fde_idx;
9275 dw_fde_ref fde;
9277 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
9279 if (DECL_IGNORED_P (fde->decl))
9280 continue;
9281 if (!fde->in_std_section)
9282 size += 2 * DWARF2_ADDR_SIZE;
9283 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
9284 size += 2 * DWARF2_ADDR_SIZE;
9288 /* Count the two zero words used to terminated the address range table. */
9289 size += 2 * DWARF2_ADDR_SIZE;
9290 return size;
9293 /* Select the encoding of an attribute value. */
9295 static enum dwarf_form
9296 value_format (dw_attr_node *a)
9298 switch (AT_class (a))
9300 case dw_val_class_addr:
9301 /* Only very few attributes allow DW_FORM_addr. */
9302 switch (a->dw_attr)
9304 case DW_AT_low_pc:
9305 case DW_AT_high_pc:
9306 case DW_AT_entry_pc:
9307 case DW_AT_trampoline:
9308 return (AT_index (a) == NOT_INDEXED
9309 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
9310 default:
9311 break;
9313 switch (DWARF2_ADDR_SIZE)
9315 case 1:
9316 return DW_FORM_data1;
9317 case 2:
9318 return DW_FORM_data2;
9319 case 4:
9320 return DW_FORM_data4;
9321 case 8:
9322 return DW_FORM_data8;
9323 default:
9324 gcc_unreachable ();
9326 case dw_val_class_loc_list:
9327 if (dwarf_split_debug_info
9328 && dwarf_version >= 5
9329 && AT_loc_list (a)->num_assigned)
9330 return DW_FORM_loclistx;
9331 /* FALLTHRU */
9332 case dw_val_class_range_list:
9333 /* For range lists in DWARF 5, use DW_FORM_rnglistx from .debug_info.dwo
9334 but in .debug_info use DW_FORM_sec_offset, which is shorter if we
9335 care about sizes of .debug* sections in shared libraries and
9336 executables and don't take into account relocations that affect just
9337 relocatable objects - for DW_FORM_rnglistx we'd have to emit offset
9338 table in the .debug_rnglists section. */
9339 if (dwarf_split_debug_info
9340 && dwarf_version >= 5
9341 && AT_class (a) == dw_val_class_range_list
9342 && rnglist_idx
9343 && a->dw_attr_val.val_entry != RELOCATED_OFFSET)
9344 return DW_FORM_rnglistx;
9345 if (dwarf_version >= 4)
9346 return DW_FORM_sec_offset;
9347 /* FALLTHRU */
9348 case dw_val_class_vms_delta:
9349 case dw_val_class_offset:
9350 switch (DWARF_OFFSET_SIZE)
9352 case 4:
9353 return DW_FORM_data4;
9354 case 8:
9355 return DW_FORM_data8;
9356 default:
9357 gcc_unreachable ();
9359 case dw_val_class_loc:
9360 if (dwarf_version >= 4)
9361 return DW_FORM_exprloc;
9362 switch (constant_size (size_of_locs (AT_loc (a))))
9364 case 1:
9365 return DW_FORM_block1;
9366 case 2:
9367 return DW_FORM_block2;
9368 case 4:
9369 return DW_FORM_block4;
9370 default:
9371 gcc_unreachable ();
9373 case dw_val_class_const:
9374 return DW_FORM_sdata;
9375 case dw_val_class_unsigned_const:
9376 switch (constant_size (AT_unsigned (a)))
9378 case 1:
9379 return DW_FORM_data1;
9380 case 2:
9381 return DW_FORM_data2;
9382 case 4:
9383 /* In DWARF3 DW_AT_data_member_location with
9384 DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
9385 constant, so we need to use DW_FORM_udata if we need
9386 a large constant. */
9387 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
9388 return DW_FORM_udata;
9389 return DW_FORM_data4;
9390 case 8:
9391 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
9392 return DW_FORM_udata;
9393 return DW_FORM_data8;
9394 default:
9395 gcc_unreachable ();
9397 case dw_val_class_const_implicit:
9398 case dw_val_class_unsigned_const_implicit:
9399 case dw_val_class_file_implicit:
9400 return DW_FORM_implicit_const;
9401 case dw_val_class_const_double:
9402 switch (HOST_BITS_PER_WIDE_INT)
9404 case 8:
9405 return DW_FORM_data2;
9406 case 16:
9407 return DW_FORM_data4;
9408 case 32:
9409 return DW_FORM_data8;
9410 case 64:
9411 if (dwarf_version >= 5)
9412 return DW_FORM_data16;
9413 /* FALLTHRU */
9414 default:
9415 return DW_FORM_block1;
9417 case dw_val_class_wide_int:
9418 switch (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT)
9420 case 8:
9421 return DW_FORM_data1;
9422 case 16:
9423 return DW_FORM_data2;
9424 case 32:
9425 return DW_FORM_data4;
9426 case 64:
9427 return DW_FORM_data8;
9428 case 128:
9429 if (dwarf_version >= 5)
9430 return DW_FORM_data16;
9431 /* FALLTHRU */
9432 default:
9433 return DW_FORM_block1;
9435 case dw_val_class_vec:
9436 switch (constant_size (a->dw_attr_val.v.val_vec.length
9437 * a->dw_attr_val.v.val_vec.elt_size))
9439 case 1:
9440 return DW_FORM_block1;
9441 case 2:
9442 return DW_FORM_block2;
9443 case 4:
9444 return DW_FORM_block4;
9445 default:
9446 gcc_unreachable ();
9448 case dw_val_class_flag:
9449 if (dwarf_version >= 4)
9451 /* Currently all add_AT_flag calls pass in 1 as last argument,
9452 so DW_FORM_flag_present can be used. If that ever changes,
9453 we'll need to use DW_FORM_flag and have some optimization
9454 in build_abbrev_table that will change those to
9455 DW_FORM_flag_present if it is set to 1 in all DIEs using
9456 the same abbrev entry. */
9457 gcc_assert (a->dw_attr_val.v.val_flag == 1);
9458 return DW_FORM_flag_present;
9460 return DW_FORM_flag;
9461 case dw_val_class_die_ref:
9462 if (AT_ref_external (a))
9463 return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
9464 else
9465 return DW_FORM_ref;
9466 case dw_val_class_fde_ref:
9467 return DW_FORM_data;
9468 case dw_val_class_lbl_id:
9469 return (AT_index (a) == NOT_INDEXED
9470 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
9471 case dw_val_class_lineptr:
9472 case dw_val_class_macptr:
9473 case dw_val_class_loclistsptr:
9474 return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
9475 case dw_val_class_str:
9476 return AT_string_form (a);
9477 case dw_val_class_file:
9478 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
9480 case 1:
9481 return DW_FORM_data1;
9482 case 2:
9483 return DW_FORM_data2;
9484 case 4:
9485 return DW_FORM_data4;
9486 default:
9487 gcc_unreachable ();
9490 case dw_val_class_data8:
9491 return DW_FORM_data8;
9493 case dw_val_class_high_pc:
9494 switch (DWARF2_ADDR_SIZE)
9496 case 1:
9497 return DW_FORM_data1;
9498 case 2:
9499 return DW_FORM_data2;
9500 case 4:
9501 return DW_FORM_data4;
9502 case 8:
9503 return DW_FORM_data8;
9504 default:
9505 gcc_unreachable ();
9508 case dw_val_class_discr_value:
9509 return (a->dw_attr_val.v.val_discr_value.pos
9510 ? DW_FORM_udata
9511 : DW_FORM_sdata);
9512 case dw_val_class_discr_list:
9513 switch (constant_size (size_of_discr_list (AT_discr_list (a))))
9515 case 1:
9516 return DW_FORM_block1;
9517 case 2:
9518 return DW_FORM_block2;
9519 case 4:
9520 return DW_FORM_block4;
9521 default:
9522 gcc_unreachable ();
9525 default:
9526 gcc_unreachable ();
9530 /* Output the encoding of an attribute value. */
9532 static void
9533 output_value_format (dw_attr_node *a)
9535 enum dwarf_form form = value_format (a);
9537 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
9540 /* Given a die and id, produce the appropriate abbreviations. */
9542 static void
9543 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
9545 unsigned ix;
9546 dw_attr_node *a_attr;
9548 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
9549 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
9550 dwarf_tag_name (abbrev->die_tag));
9552 if (abbrev->die_child != NULL)
9553 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
9554 else
9555 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
9557 for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
9559 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
9560 dwarf_attr_name (a_attr->dw_attr));
9561 output_value_format (a_attr);
9562 if (value_format (a_attr) == DW_FORM_implicit_const)
9564 if (AT_class (a_attr) == dw_val_class_file_implicit)
9566 int f = maybe_emit_file (a_attr->dw_attr_val.v.val_file);
9567 const char *filename = a_attr->dw_attr_val.v.val_file->filename;
9568 dw2_asm_output_data_sleb128 (f, "(%s)", filename);
9570 else
9571 dw2_asm_output_data_sleb128 (a_attr->dw_attr_val.v.val_int, NULL);
9575 dw2_asm_output_data (1, 0, NULL);
9576 dw2_asm_output_data (1, 0, NULL);
9580 /* Output the .debug_abbrev section which defines the DIE abbreviation
9581 table. */
9583 static void
9584 output_abbrev_section (void)
9586 unsigned int abbrev_id;
9587 dw_die_ref abbrev;
9589 FOR_EACH_VEC_SAFE_ELT (abbrev_die_table, abbrev_id, abbrev)
9590 if (abbrev_id != 0)
9591 output_die_abbrevs (abbrev_id, abbrev);
9593 /* Terminate the table. */
9594 dw2_asm_output_data (1, 0, NULL);
9597 /* Output a symbol we can use to refer to this DIE from another CU. */
9599 static inline void
9600 output_die_symbol (dw_die_ref die)
9602 const char *sym = die->die_id.die_symbol;
9604 gcc_assert (!die->comdat_type_p);
9606 if (sym == 0)
9607 return;
9609 if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
9610 /* We make these global, not weak; if the target doesn't support
9611 .linkonce, it doesn't support combining the sections, so debugging
9612 will break. */
9613 targetm.asm_out.globalize_label (asm_out_file, sym);
9615 ASM_OUTPUT_LABEL (asm_out_file, sym);
9618 /* Return a new location list, given the begin and end range, and the
9619 expression. */
9621 static inline dw_loc_list_ref
9622 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
9623 const char *section)
9625 dw_loc_list_ref retlist = ggc_cleared_alloc<dw_loc_list_node> ();
9627 retlist->begin = begin;
9628 retlist->begin_entry = NULL;
9629 retlist->end = end;
9630 retlist->expr = expr;
9631 retlist->section = section;
9633 return retlist;
9636 /* Generate a new internal symbol for this location list node, if it
9637 hasn't got one yet. */
9639 static inline void
9640 gen_llsym (dw_loc_list_ref list)
9642 gcc_assert (!list->ll_symbol);
9643 list->ll_symbol = gen_internal_sym ("LLST");
9646 /* Output the location list given to us. */
9648 static void
9649 output_loc_list (dw_loc_list_ref list_head)
9651 if (list_head->emitted)
9652 return;
9653 list_head->emitted = true;
9655 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
9657 dw_loc_list_ref curr = list_head;
9658 const char *last_section = NULL;
9659 const char *base_label = NULL;
9661 /* Walk the location list, and output each range + expression. */
9662 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
9664 unsigned long size;
9665 /* Don't output an entry that starts and ends at the same address. */
9666 if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
9667 continue;
9668 size = size_of_locs (curr->expr);
9669 /* If the expression is too large, drop it on the floor. We could
9670 perhaps put it into DW_TAG_dwarf_procedure and refer to that
9671 in the expression, but >= 64KB expressions for a single value
9672 in a single range are unlikely very useful. */
9673 if (dwarf_version < 5 && size > 0xffff)
9674 continue;
9675 if (dwarf_version >= 5)
9677 if (dwarf_split_debug_info)
9679 /* For -gsplit-dwarf, emit DW_LLE_starx_length, which has
9680 uleb128 index into .debug_addr and uleb128 length. */
9681 dw2_asm_output_data (1, DW_LLE_startx_length,
9682 "DW_LLE_startx_length (%s)",
9683 list_head->ll_symbol);
9684 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
9685 "Location list range start index "
9686 "(%s)", curr->begin);
9687 /* FIXME: This will ICE ifndef HAVE_AS_LEB128.
9688 For that case we probably need to emit DW_LLE_startx_endx,
9689 but we'd need 2 .debug_addr entries rather than just one. */
9690 dw2_asm_output_delta_uleb128 (curr->end, curr->begin,
9691 "Location list length (%s)",
9692 list_head->ll_symbol);
9694 else if (!have_multiple_function_sections && HAVE_AS_LEB128)
9696 /* If all code is in .text section, the base address is
9697 already provided by the CU attributes. Use
9698 DW_LLE_offset_pair where both addresses are uleb128 encoded
9699 offsets against that base. */
9700 dw2_asm_output_data (1, DW_LLE_offset_pair,
9701 "DW_LLE_offset_pair (%s)",
9702 list_head->ll_symbol);
9703 dw2_asm_output_delta_uleb128 (curr->begin, curr->section,
9704 "Location list begin address (%s)",
9705 list_head->ll_symbol);
9706 dw2_asm_output_delta_uleb128 (curr->end, curr->section,
9707 "Location list end address (%s)",
9708 list_head->ll_symbol);
9710 else if (HAVE_AS_LEB128)
9712 /* Otherwise, find out how many consecutive entries could share
9713 the same base entry. If just one, emit DW_LLE_start_length,
9714 otherwise emit DW_LLE_base_address for the base address
9715 followed by a series of DW_LLE_offset_pair. */
9716 if (last_section == NULL || curr->section != last_section)
9718 dw_loc_list_ref curr2;
9719 for (curr2 = curr->dw_loc_next; curr2 != NULL;
9720 curr2 = curr2->dw_loc_next)
9722 if (strcmp (curr2->begin, curr2->end) == 0
9723 && !curr2->force)
9724 continue;
9725 break;
9727 if (curr2 == NULL || curr->section != curr2->section)
9728 last_section = NULL;
9729 else
9731 last_section = curr->section;
9732 base_label = curr->begin;
9733 dw2_asm_output_data (1, DW_LLE_base_address,
9734 "DW_LLE_base_address (%s)",
9735 list_head->ll_symbol);
9736 dw2_asm_output_addr (DWARF2_ADDR_SIZE, base_label,
9737 "Base address (%s)",
9738 list_head->ll_symbol);
9741 /* Only one entry with the same base address. Use
9742 DW_LLE_start_length with absolute address and uleb128
9743 length. */
9744 if (last_section == NULL)
9746 dw2_asm_output_data (1, DW_LLE_start_length,
9747 "DW_LLE_start_length (%s)",
9748 list_head->ll_symbol);
9749 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
9750 "Location list begin address (%s)",
9751 list_head->ll_symbol);
9752 dw2_asm_output_delta_uleb128 (curr->end, curr->begin,
9753 "Location list length "
9754 "(%s)", list_head->ll_symbol);
9756 /* Otherwise emit DW_LLE_offset_pair, relative to above emitted
9757 DW_LLE_base_address. */
9758 else
9760 dw2_asm_output_data (1, DW_LLE_offset_pair,
9761 "DW_LLE_offset_pair (%s)",
9762 list_head->ll_symbol);
9763 dw2_asm_output_delta_uleb128 (curr->begin, base_label,
9764 "Location list begin address "
9765 "(%s)", list_head->ll_symbol);
9766 dw2_asm_output_delta_uleb128 (curr->end, base_label,
9767 "Location list end address "
9768 "(%s)", list_head->ll_symbol);
9771 /* The assembler does not support .uleb128 directive. Emit
9772 DW_LLE_start_end with a pair of absolute addresses. */
9773 else
9775 dw2_asm_output_data (1, DW_LLE_start_end,
9776 "DW_LLE_start_end (%s)",
9777 list_head->ll_symbol);
9778 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
9779 "Location list begin address (%s)",
9780 list_head->ll_symbol);
9781 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
9782 "Location list end address (%s)",
9783 list_head->ll_symbol);
9786 else if (dwarf_split_debug_info)
9788 /* For -gsplit-dwarf -gdwarf-{2,3,4} emit index into .debug_addr
9789 and 4 byte length. */
9790 dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
9791 "Location list start/length entry (%s)",
9792 list_head->ll_symbol);
9793 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
9794 "Location list range start index (%s)",
9795 curr->begin);
9796 /* The length field is 4 bytes. If we ever need to support
9797 an 8-byte length, we can add a new DW_LLE code or fall back
9798 to DW_LLE_GNU_start_end_entry. */
9799 dw2_asm_output_delta (4, curr->end, curr->begin,
9800 "Location list range length (%s)",
9801 list_head->ll_symbol);
9803 else if (!have_multiple_function_sections)
9805 /* Pair of relative addresses against start of text section. */
9806 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
9807 "Location list begin address (%s)",
9808 list_head->ll_symbol);
9809 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
9810 "Location list end address (%s)",
9811 list_head->ll_symbol);
9813 else
9815 /* Pair of absolute addresses. */
9816 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
9817 "Location list begin address (%s)",
9818 list_head->ll_symbol);
9819 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
9820 "Location list end address (%s)",
9821 list_head->ll_symbol);
9824 /* Output the block length for this list of location operations. */
9825 if (dwarf_version >= 5)
9826 dw2_asm_output_data_uleb128 (size, "Location expression size");
9827 else
9829 gcc_assert (size <= 0xffff);
9830 dw2_asm_output_data (2, size, "Location expression size");
9833 output_loc_sequence (curr->expr, -1);
9836 /* And finally list termination. */
9837 if (dwarf_version >= 5)
9838 dw2_asm_output_data (1, DW_LLE_end_of_list,
9839 "DW_LLE_end_of_list (%s)", list_head->ll_symbol);
9840 else if (dwarf_split_debug_info)
9841 dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
9842 "Location list terminator (%s)",
9843 list_head->ll_symbol);
9844 else
9846 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
9847 "Location list terminator begin (%s)",
9848 list_head->ll_symbol);
9849 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
9850 "Location list terminator end (%s)",
9851 list_head->ll_symbol);
9855 /* Output a range_list offset into the .debug_ranges or .debug_rnglists
9856 section. Emit a relocated reference if val_entry is NULL, otherwise,
9857 emit an indirect reference. */
9859 static void
9860 output_range_list_offset (dw_attr_node *a)
9862 const char *name = dwarf_attr_name (a->dw_attr);
9864 if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
9866 if (dwarf_version >= 5)
9868 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
9869 dw2_asm_output_offset (DWARF_OFFSET_SIZE, r->label,
9870 debug_ranges_section, "%s", name);
9872 else
9874 char *p = strchr (ranges_section_label, '\0');
9875 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
9876 a->dw_attr_val.v.val_offset * 2 * DWARF2_ADDR_SIZE);
9877 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
9878 debug_ranges_section, "%s", name);
9879 *p = '\0';
9882 else if (dwarf_version >= 5)
9884 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
9885 gcc_assert (rnglist_idx);
9886 dw2_asm_output_data_uleb128 (r->idx, "%s", name);
9888 else
9889 dw2_asm_output_data (DWARF_OFFSET_SIZE,
9890 a->dw_attr_val.v.val_offset * 2 * DWARF2_ADDR_SIZE,
9891 "%s (offset from %s)", name, ranges_section_label);
9894 /* Output the offset into the debug_loc section. */
9896 static void
9897 output_loc_list_offset (dw_attr_node *a)
9899 char *sym = AT_loc_list (a)->ll_symbol;
9901 gcc_assert (sym);
9902 if (!dwarf_split_debug_info)
9903 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
9904 "%s", dwarf_attr_name (a->dw_attr));
9905 else if (dwarf_version >= 5)
9907 gcc_assert (AT_loc_list (a)->num_assigned);
9908 dw2_asm_output_data_uleb128 (AT_loc_list (a)->hash, "%s (%s)",
9909 dwarf_attr_name (a->dw_attr),
9910 sym);
9912 else
9913 dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
9914 "%s", dwarf_attr_name (a->dw_attr));
9917 /* Output an attribute's index or value appropriately. */
9919 static void
9920 output_attr_index_or_value (dw_attr_node *a)
9922 const char *name = dwarf_attr_name (a->dw_attr);
9924 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
9926 dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
9927 return;
9929 switch (AT_class (a))
9931 case dw_val_class_addr:
9932 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
9933 break;
9934 case dw_val_class_high_pc:
9935 case dw_val_class_lbl_id:
9936 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
9937 break;
9938 default:
9939 gcc_unreachable ();
9943 /* Output a type signature. */
9945 static inline void
9946 output_signature (const char *sig, const char *name)
9948 int i;
9950 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9951 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
9954 /* Output a discriminant value. */
9956 static inline void
9957 output_discr_value (dw_discr_value *discr_value, const char *name)
9959 if (discr_value->pos)
9960 dw2_asm_output_data_uleb128 (discr_value->v.uval, "%s", name);
9961 else
9962 dw2_asm_output_data_sleb128 (discr_value->v.sval, "%s", name);
9965 /* Output the DIE and its attributes. Called recursively to generate
9966 the definitions of each child DIE. */
9968 static void
9969 output_die (dw_die_ref die)
9971 dw_attr_node *a;
9972 dw_die_ref c;
9973 unsigned long size;
9974 unsigned ix;
9976 /* If someone in another CU might refer to us, set up a symbol for
9977 them to point to. */
9978 if (! die->comdat_type_p && die->die_id.die_symbol)
9979 output_die_symbol (die);
9981 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
9982 (unsigned long)die->die_offset,
9983 dwarf_tag_name (die->die_tag));
9985 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9987 const char *name = dwarf_attr_name (a->dw_attr);
9989 switch (AT_class (a))
9991 case dw_val_class_addr:
9992 output_attr_index_or_value (a);
9993 break;
9995 case dw_val_class_offset:
9996 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
9997 "%s", name);
9998 break;
10000 case dw_val_class_range_list:
10001 output_range_list_offset (a);
10002 break;
10004 case dw_val_class_loc:
10005 size = size_of_locs (AT_loc (a));
10007 /* Output the block length for this list of location operations. */
10008 if (dwarf_version >= 4)
10009 dw2_asm_output_data_uleb128 (size, "%s", name);
10010 else
10011 dw2_asm_output_data (constant_size (size), size, "%s", name);
10013 output_loc_sequence (AT_loc (a), -1);
10014 break;
10016 case dw_val_class_const:
10017 /* ??? It would be slightly more efficient to use a scheme like is
10018 used for unsigned constants below, but gdb 4.x does not sign
10019 extend. Gdb 5.x does sign extend. */
10020 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
10021 break;
10023 case dw_val_class_unsigned_const:
10025 int csize = constant_size (AT_unsigned (a));
10026 if (dwarf_version == 3
10027 && a->dw_attr == DW_AT_data_member_location
10028 && csize >= 4)
10029 dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
10030 else
10031 dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
10033 break;
10035 case dw_val_class_const_implicit:
10036 if (flag_debug_asm)
10037 fprintf (asm_out_file, "\t\t\t%s %s ("
10038 HOST_WIDE_INT_PRINT_DEC ")\n",
10039 ASM_COMMENT_START, name, AT_int (a));
10040 break;
10042 case dw_val_class_unsigned_const_implicit:
10043 if (flag_debug_asm)
10044 fprintf (asm_out_file, "\t\t\t%s %s ("
10045 HOST_WIDE_INT_PRINT_HEX ")\n",
10046 ASM_COMMENT_START, name, AT_unsigned (a));
10047 break;
10049 case dw_val_class_const_double:
10051 unsigned HOST_WIDE_INT first, second;
10053 if (HOST_BITS_PER_WIDE_INT >= DWARF_LARGEST_DATA_FORM_BITS)
10054 dw2_asm_output_data (1,
10055 HOST_BITS_PER_DOUBLE_INT
10056 / HOST_BITS_PER_CHAR,
10057 NULL);
10059 if (WORDS_BIG_ENDIAN)
10061 first = a->dw_attr_val.v.val_double.high;
10062 second = a->dw_attr_val.v.val_double.low;
10064 else
10066 first = a->dw_attr_val.v.val_double.low;
10067 second = a->dw_attr_val.v.val_double.high;
10070 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
10071 first, "%s", name);
10072 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
10073 second, NULL);
10075 break;
10077 case dw_val_class_wide_int:
10079 int i;
10080 int len = get_full_len (*a->dw_attr_val.v.val_wide);
10081 int l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
10082 if (len * HOST_BITS_PER_WIDE_INT > DWARF_LARGEST_DATA_FORM_BITS)
10083 dw2_asm_output_data (1, get_full_len (*a->dw_attr_val.v.val_wide)
10084 * l, NULL);
10086 if (WORDS_BIG_ENDIAN)
10087 for (i = len - 1; i >= 0; --i)
10089 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
10090 "%s", name);
10091 name = "";
10093 else
10094 for (i = 0; i < len; ++i)
10096 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
10097 "%s", name);
10098 name = "";
10101 break;
10103 case dw_val_class_vec:
10105 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
10106 unsigned int len = a->dw_attr_val.v.val_vec.length;
10107 unsigned int i;
10108 unsigned char *p;
10110 dw2_asm_output_data (constant_size (len * elt_size),
10111 len * elt_size, "%s", name);
10112 if (elt_size > sizeof (HOST_WIDE_INT))
10114 elt_size /= 2;
10115 len *= 2;
10117 for (i = 0, p = (unsigned char *) a->dw_attr_val.v.val_vec.array;
10118 i < len;
10119 i++, p += elt_size)
10120 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
10121 "fp or vector constant word %u", i);
10122 break;
10125 case dw_val_class_flag:
10126 if (dwarf_version >= 4)
10128 /* Currently all add_AT_flag calls pass in 1 as last argument,
10129 so DW_FORM_flag_present can be used. If that ever changes,
10130 we'll need to use DW_FORM_flag and have some optimization
10131 in build_abbrev_table that will change those to
10132 DW_FORM_flag_present if it is set to 1 in all DIEs using
10133 the same abbrev entry. */
10134 gcc_assert (AT_flag (a) == 1);
10135 if (flag_debug_asm)
10136 fprintf (asm_out_file, "\t\t\t%s %s\n",
10137 ASM_COMMENT_START, name);
10138 break;
10140 dw2_asm_output_data (1, AT_flag (a), "%s", name);
10141 break;
10143 case dw_val_class_loc_list:
10144 output_loc_list_offset (a);
10145 break;
10147 case dw_val_class_die_ref:
10148 if (AT_ref_external (a))
10150 if (AT_ref (a)->comdat_type_p)
10152 comdat_type_node *type_node
10153 = AT_ref (a)->die_id.die_type_node;
10155 gcc_assert (type_node);
10156 output_signature (type_node->signature, name);
10158 else
10160 const char *sym = AT_ref (a)->die_id.die_symbol;
10161 int size;
10163 gcc_assert (sym);
10164 /* In DWARF2, DW_FORM_ref_addr is sized by target address
10165 length, whereas in DWARF3 it's always sized as an
10166 offset. */
10167 if (dwarf_version == 2)
10168 size = DWARF2_ADDR_SIZE;
10169 else
10170 size = DWARF_OFFSET_SIZE;
10171 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
10172 name);
10175 else
10177 gcc_assert (AT_ref (a)->die_offset);
10178 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
10179 "%s", name);
10181 break;
10183 case dw_val_class_fde_ref:
10185 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
10187 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
10188 a->dw_attr_val.v.val_fde_index * 2);
10189 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
10190 "%s", name);
10192 break;
10194 case dw_val_class_vms_delta:
10195 #ifdef ASM_OUTPUT_DWARF_VMS_DELTA
10196 dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
10197 AT_vms_delta2 (a), AT_vms_delta1 (a),
10198 "%s", name);
10199 #else
10200 dw2_asm_output_delta (DWARF_OFFSET_SIZE,
10201 AT_vms_delta2 (a), AT_vms_delta1 (a),
10202 "%s", name);
10203 #endif
10204 break;
10206 case dw_val_class_lbl_id:
10207 output_attr_index_or_value (a);
10208 break;
10210 case dw_val_class_lineptr:
10211 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
10212 debug_line_section, "%s", name);
10213 break;
10215 case dw_val_class_macptr:
10216 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
10217 debug_macinfo_section, "%s", name);
10218 break;
10220 case dw_val_class_loclistsptr:
10221 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
10222 debug_loc_section, "%s", name);
10223 break;
10225 case dw_val_class_str:
10226 if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
10227 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
10228 a->dw_attr_val.v.val_str->label,
10229 debug_str_section,
10230 "%s: \"%s\"", name, AT_string (a));
10231 else if (a->dw_attr_val.v.val_str->form == DW_FORM_line_strp)
10232 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
10233 a->dw_attr_val.v.val_str->label,
10234 debug_line_str_section,
10235 "%s: \"%s\"", name, AT_string (a));
10236 else if (a->dw_attr_val.v.val_str->form == DW_FORM_GNU_str_index)
10237 dw2_asm_output_data_uleb128 (AT_index (a),
10238 "%s: \"%s\"", name, AT_string (a));
10239 else
10240 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
10241 break;
10243 case dw_val_class_file:
10245 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
10247 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
10248 a->dw_attr_val.v.val_file->filename);
10249 break;
10252 case dw_val_class_file_implicit:
10253 if (flag_debug_asm)
10254 fprintf (asm_out_file, "\t\t\t%s %s (%d, %s)\n",
10255 ASM_COMMENT_START, name,
10256 maybe_emit_file (a->dw_attr_val.v.val_file),
10257 a->dw_attr_val.v.val_file->filename);
10258 break;
10260 case dw_val_class_data8:
10262 int i;
10264 for (i = 0; i < 8; i++)
10265 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
10266 i == 0 ? "%s" : NULL, name);
10267 break;
10270 case dw_val_class_high_pc:
10271 dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
10272 get_AT_low_pc (die), "DW_AT_high_pc");
10273 break;
10275 case dw_val_class_discr_value:
10276 output_discr_value (&a->dw_attr_val.v.val_discr_value, name);
10277 break;
10279 case dw_val_class_discr_list:
10281 dw_discr_list_ref list = AT_discr_list (a);
10282 const int size = size_of_discr_list (list);
10284 /* This is a block, so output its length first. */
10285 dw2_asm_output_data (constant_size (size), size,
10286 "%s: block size", name);
10288 for (; list != NULL; list = list->dw_discr_next)
10290 /* One byte for the discriminant value descriptor, and then as
10291 many LEB128 numbers as required. */
10292 if (list->dw_discr_range)
10293 dw2_asm_output_data (1, DW_DSC_range,
10294 "%s: DW_DSC_range", name);
10295 else
10296 dw2_asm_output_data (1, DW_DSC_label,
10297 "%s: DW_DSC_label", name);
10299 output_discr_value (&list->dw_discr_lower_bound, name);
10300 if (list->dw_discr_range)
10301 output_discr_value (&list->dw_discr_upper_bound, name);
10303 break;
10306 default:
10307 gcc_unreachable ();
10311 FOR_EACH_CHILD (die, c, output_die (c));
10313 /* Add null byte to terminate sibling list. */
10314 if (die->die_child != NULL)
10315 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
10316 (unsigned long) die->die_offset);
10319 /* Output the compilation unit that appears at the beginning of the
10320 .debug_info section, and precedes the DIE descriptions. */
10322 static void
10323 output_compilation_unit_header (enum dwarf_unit_type ut)
10325 if (!XCOFF_DEBUGGING_INFO)
10327 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10328 dw2_asm_output_data (4, 0xffffffff,
10329 "Initial length escape value indicating 64-bit DWARF extension");
10330 dw2_asm_output_data (DWARF_OFFSET_SIZE,
10331 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
10332 "Length of Compilation Unit Info");
10335 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
10336 if (dwarf_version >= 5)
10338 const char *name;
10339 switch (ut)
10341 case DW_UT_compile: name = "DW_UT_compile"; break;
10342 case DW_UT_type: name = "DW_UT_type"; break;
10343 case DW_UT_split_compile: name = "DW_UT_split_compile"; break;
10344 case DW_UT_split_type: name = "DW_UT_split_type"; break;
10345 default: gcc_unreachable ();
10347 dw2_asm_output_data (1, ut, "%s", name);
10348 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10350 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
10351 debug_abbrev_section,
10352 "Offset Into Abbrev. Section");
10353 if (dwarf_version < 5)
10354 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10357 /* Output the compilation unit DIE and its children. */
10359 static void
10360 output_comp_unit (dw_die_ref die, int output_if_empty,
10361 const unsigned char *dwo_id)
10363 const char *secname, *oldsym;
10364 char *tmp;
10366 /* Unless we are outputting main CU, we may throw away empty ones. */
10367 if (!output_if_empty && die->die_child == NULL)
10368 return;
10370 /* Even if there are no children of this DIE, we must output the information
10371 about the compilation unit. Otherwise, on an empty translation unit, we
10372 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
10373 will then complain when examining the file. First mark all the DIEs in
10374 this CU so we know which get local refs. */
10375 mark_dies (die);
10377 external_ref_hash_type *extern_map = optimize_external_refs (die);
10379 /* For now, optimize only the main CU, in order to optimize the rest
10380 we'd need to see all of them earlier. Leave the rest for post-linking
10381 tools like DWZ. */
10382 if (die == comp_unit_die ())
10383 abbrev_opt_start = vec_safe_length (abbrev_die_table);
10385 build_abbrev_table (die, extern_map);
10387 optimize_abbrev_table ();
10389 delete extern_map;
10391 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
10392 next_die_offset = (dwo_id
10393 ? DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
10394 : DWARF_COMPILE_UNIT_HEADER_SIZE);
10395 calc_die_sizes (die);
10397 oldsym = die->die_id.die_symbol;
10398 if (oldsym)
10400 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
10402 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
10403 secname = tmp;
10404 die->die_id.die_symbol = NULL;
10405 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
10407 else
10409 switch_to_section (debug_info_section);
10410 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
10411 info_section_emitted = true;
10414 /* Output debugging information. */
10415 output_compilation_unit_header (dwo_id
10416 ? DW_UT_split_compile : DW_UT_compile);
10417 if (dwarf_version >= 5)
10419 if (dwo_id != NULL)
10420 for (int i = 0; i < 8; i++)
10421 dw2_asm_output_data (1, dwo_id[i], i == 0 ? "DWO id" : NULL);
10423 output_die (die);
10425 /* Leave the marks on the main CU, so we can check them in
10426 output_pubnames. */
10427 if (oldsym)
10429 unmark_dies (die);
10430 die->die_id.die_symbol = oldsym;
10434 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
10435 and .debug_pubtypes. This is configured per-target, but can be
10436 overridden by the -gpubnames or -gno-pubnames options. */
10438 static inline bool
10439 want_pubnames (void)
10441 if (debug_info_level <= DINFO_LEVEL_TERSE)
10442 return false;
10443 if (debug_generate_pub_sections != -1)
10444 return debug_generate_pub_sections;
10445 return targetm.want_debug_pub_sections;
10448 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes. */
10450 static void
10451 add_AT_pubnames (dw_die_ref die)
10453 if (want_pubnames ())
10454 add_AT_flag (die, DW_AT_GNU_pubnames, 1);
10457 /* Add a string attribute value to a skeleton DIE. */
10459 static inline void
10460 add_skeleton_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
10461 const char *str)
10463 dw_attr_node attr;
10464 struct indirect_string_node *node;
10466 if (! skeleton_debug_str_hash)
10467 skeleton_debug_str_hash
10468 = hash_table<indirect_string_hasher>::create_ggc (10);
10470 node = find_AT_string_in_table (str, skeleton_debug_str_hash);
10471 find_string_form (node);
10472 if (node->form == DW_FORM_GNU_str_index)
10473 node->form = DW_FORM_strp;
10475 attr.dw_attr = attr_kind;
10476 attr.dw_attr_val.val_class = dw_val_class_str;
10477 attr.dw_attr_val.val_entry = NULL;
10478 attr.dw_attr_val.v.val_str = node;
10479 add_dwarf_attr (die, &attr);
10482 /* Helper function to generate top-level dies for skeleton debug_info and
10483 debug_types. */
10485 static void
10486 add_top_level_skeleton_die_attrs (dw_die_ref die)
10488 const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
10489 const char *comp_dir = comp_dir_string ();
10491 add_skeleton_AT_string (die, dwarf_AT (DW_AT_dwo_name), dwo_file_name);
10492 if (comp_dir != NULL)
10493 add_skeleton_AT_string (die, DW_AT_comp_dir, comp_dir);
10494 add_AT_pubnames (die);
10495 add_AT_lineptr (die, DW_AT_GNU_addr_base, debug_addr_section_label);
10498 /* Output skeleton debug sections that point to the dwo file. */
10500 static void
10501 output_skeleton_debug_sections (dw_die_ref comp_unit,
10502 const unsigned char *dwo_id)
10504 /* These attributes will be found in the full debug_info section. */
10505 remove_AT (comp_unit, DW_AT_producer);
10506 remove_AT (comp_unit, DW_AT_language);
10508 switch_to_section (debug_skeleton_info_section);
10509 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
10511 /* Produce the skeleton compilation-unit header. This one differs enough from
10512 a normal CU header that it's better not to call output_compilation_unit
10513 header. */
10514 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10515 dw2_asm_output_data (4, 0xffffffff,
10516 "Initial length escape value indicating 64-bit "
10517 "DWARF extension");
10519 dw2_asm_output_data (DWARF_OFFSET_SIZE,
10520 DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
10521 - DWARF_INITIAL_LENGTH_SIZE
10522 + size_of_die (comp_unit),
10523 "Length of Compilation Unit Info");
10524 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
10525 if (dwarf_version >= 5)
10527 dw2_asm_output_data (1, DW_UT_skeleton, "DW_UT_skeleton");
10528 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10530 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_abbrev_section_label,
10531 debug_skeleton_abbrev_section,
10532 "Offset Into Abbrev. Section");
10533 if (dwarf_version < 5)
10534 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10535 else
10536 for (int i = 0; i < 8; i++)
10537 dw2_asm_output_data (1, dwo_id[i], i == 0 ? "DWO id" : NULL);
10539 comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
10540 output_die (comp_unit);
10542 /* Build the skeleton debug_abbrev section. */
10543 switch_to_section (debug_skeleton_abbrev_section);
10544 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
10546 output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
10548 dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
10551 /* Output a comdat type unit DIE and its children. */
10553 static void
10554 output_comdat_type_unit (comdat_type_node *node)
10556 const char *secname;
10557 char *tmp;
10558 int i;
10559 #if defined (OBJECT_FORMAT_ELF)
10560 tree comdat_key;
10561 #endif
10563 /* First mark all the DIEs in this CU so we know which get local refs. */
10564 mark_dies (node->root_die);
10566 external_ref_hash_type *extern_map = optimize_external_refs (node->root_die);
10568 build_abbrev_table (node->root_die, extern_map);
10570 delete extern_map;
10571 extern_map = NULL;
10573 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
10574 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
10575 calc_die_sizes (node->root_die);
10577 #if defined (OBJECT_FORMAT_ELF)
10578 if (dwarf_version >= 5)
10580 if (!dwarf_split_debug_info)
10581 secname = ".debug_info";
10582 else
10583 secname = ".debug_info.dwo";
10585 else if (!dwarf_split_debug_info)
10586 secname = ".debug_types";
10587 else
10588 secname = ".debug_types.dwo";
10590 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
10591 sprintf (tmp, dwarf_version >= 5 ? "wi." : "wt.");
10592 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
10593 sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
10594 comdat_key = get_identifier (tmp);
10595 targetm.asm_out.named_section (secname,
10596 SECTION_DEBUG | SECTION_LINKONCE,
10597 comdat_key);
10598 #else
10599 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
10600 sprintf (tmp, (dwarf_version >= 5
10601 ? ".gnu.linkonce.wi." : ".gnu.linkonce.wt."));
10602 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
10603 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
10604 secname = tmp;
10605 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
10606 #endif
10608 /* Output debugging information. */
10609 output_compilation_unit_header (dwarf_split_debug_info
10610 ? DW_UT_split_type : DW_UT_type);
10611 output_signature (node->signature, "Type Signature");
10612 dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
10613 "Offset to Type DIE");
10614 output_die (node->root_die);
10616 unmark_dies (node->root_die);
10619 /* Return the DWARF2/3 pubname associated with a decl. */
10621 static const char *
10622 dwarf2_name (tree decl, int scope)
10624 if (DECL_NAMELESS (decl))
10625 return NULL;
10626 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
10629 /* Add a new entry to .debug_pubnames if appropriate. */
10631 static void
10632 add_pubname_string (const char *str, dw_die_ref die)
10634 pubname_entry e;
10636 e.die = die;
10637 e.name = xstrdup (str);
10638 vec_safe_push (pubname_table, e);
10641 static void
10642 add_pubname (tree decl, dw_die_ref die)
10644 if (!want_pubnames ())
10645 return;
10647 /* Don't add items to the table when we expect that the consumer will have
10648 just read the enclosing die. For example, if the consumer is looking at a
10649 class_member, it will either be inside the class already, or will have just
10650 looked up the class to find the member. Either way, searching the class is
10651 faster than searching the index. */
10652 if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
10653 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
10655 const char *name = dwarf2_name (decl, 1);
10657 if (name)
10658 add_pubname_string (name, die);
10662 /* Add an enumerator to the pubnames section. */
10664 static void
10665 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
10667 pubname_entry e;
10669 gcc_assert (scope_name);
10670 e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
10671 e.die = die;
10672 vec_safe_push (pubname_table, e);
10675 /* Add a new entry to .debug_pubtypes if appropriate. */
10677 static void
10678 add_pubtype (tree decl, dw_die_ref die)
10680 pubname_entry e;
10682 if (!want_pubnames ())
10683 return;
10685 if ((TREE_PUBLIC (decl)
10686 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
10687 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
10689 tree scope = NULL;
10690 const char *scope_name = "";
10691 const char *sep = is_cxx () ? "::" : ".";
10692 const char *name;
10694 scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
10695 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
10697 scope_name = lang_hooks.dwarf_name (scope, 1);
10698 if (scope_name != NULL && scope_name[0] != '\0')
10699 scope_name = concat (scope_name, sep, NULL);
10700 else
10701 scope_name = "";
10704 if (TYPE_P (decl))
10705 name = type_tag (decl);
10706 else
10707 name = lang_hooks.dwarf_name (decl, 1);
10709 /* If we don't have a name for the type, there's no point in adding
10710 it to the table. */
10711 if (name != NULL && name[0] != '\0')
10713 e.die = die;
10714 e.name = concat (scope_name, name, NULL);
10715 vec_safe_push (pubtype_table, e);
10718 /* Although it might be more consistent to add the pubinfo for the
10719 enumerators as their dies are created, they should only be added if the
10720 enum type meets the criteria above. So rather than re-check the parent
10721 enum type whenever an enumerator die is created, just output them all
10722 here. This isn't protected by the name conditional because anonymous
10723 enums don't have names. */
10724 if (die->die_tag == DW_TAG_enumeration_type)
10726 dw_die_ref c;
10728 FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
10733 /* Output a single entry in the pubnames table. */
10735 static void
10736 output_pubname (dw_offset die_offset, pubname_entry *entry)
10738 dw_die_ref die = entry->die;
10739 int is_static = get_AT_flag (die, DW_AT_external) ? 0 : 1;
10741 dw2_asm_output_data (DWARF_OFFSET_SIZE, die_offset, "DIE offset");
10743 if (debug_generate_pub_sections == 2)
10745 /* This logic follows gdb's method for determining the value of the flag
10746 byte. */
10747 uint32_t flags = GDB_INDEX_SYMBOL_KIND_NONE;
10748 switch (die->die_tag)
10750 case DW_TAG_typedef:
10751 case DW_TAG_base_type:
10752 case DW_TAG_subrange_type:
10753 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
10754 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
10755 break;
10756 case DW_TAG_enumerator:
10757 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
10758 GDB_INDEX_SYMBOL_KIND_VARIABLE);
10759 if (!is_cxx () && !is_java ())
10760 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
10761 break;
10762 case DW_TAG_subprogram:
10763 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
10764 GDB_INDEX_SYMBOL_KIND_FUNCTION);
10765 if (!is_ada ())
10766 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
10767 break;
10768 case DW_TAG_constant:
10769 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
10770 GDB_INDEX_SYMBOL_KIND_VARIABLE);
10771 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
10772 break;
10773 case DW_TAG_variable:
10774 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
10775 GDB_INDEX_SYMBOL_KIND_VARIABLE);
10776 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
10777 break;
10778 case DW_TAG_namespace:
10779 case DW_TAG_imported_declaration:
10780 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
10781 break;
10782 case DW_TAG_class_type:
10783 case DW_TAG_interface_type:
10784 case DW_TAG_structure_type:
10785 case DW_TAG_union_type:
10786 case DW_TAG_enumeration_type:
10787 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
10788 if (!is_cxx () && !is_java ())
10789 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
10790 break;
10791 default:
10792 /* An unusual tag. Leave the flag-byte empty. */
10793 break;
10795 dw2_asm_output_data (1, flags >> GDB_INDEX_CU_BITSIZE,
10796 "GDB-index flags");
10799 dw2_asm_output_nstring (entry->name, -1, "external name");
10803 /* Output the public names table used to speed up access to externally
10804 visible names; or the public types table used to find type definitions. */
10806 static void
10807 output_pubnames (vec<pubname_entry, va_gc> *names)
10809 unsigned i;
10810 unsigned long pubnames_length = size_of_pubnames (names);
10811 pubname_entry *pub;
10813 if (!XCOFF_DEBUGGING_INFO)
10815 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10816 dw2_asm_output_data (4, 0xffffffff,
10817 "Initial length escape value indicating 64-bit DWARF extension");
10818 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
10819 "Pub Info Length");
10822 /* Version number for pubnames/pubtypes is independent of dwarf version. */
10823 dw2_asm_output_data (2, 2, "DWARF Version");
10825 if (dwarf_split_debug_info)
10826 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
10827 debug_skeleton_info_section,
10828 "Offset of Compilation Unit Info");
10829 else
10830 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
10831 debug_info_section,
10832 "Offset of Compilation Unit Info");
10833 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
10834 "Compilation Unit Length");
10836 FOR_EACH_VEC_ELT (*names, i, pub)
10838 if (include_pubname_in_output (names, pub))
10840 dw_offset die_offset = pub->die->die_offset;
10842 /* We shouldn't see pubnames for DIEs outside of the main CU. */
10843 if (names == pubname_table && pub->die->die_tag != DW_TAG_enumerator)
10844 gcc_assert (pub->die->die_mark);
10846 /* If we're putting types in their own .debug_types sections,
10847 the .debug_pubtypes table will still point to the compile
10848 unit (not the type unit), so we want to use the offset of
10849 the skeleton DIE (if there is one). */
10850 if (pub->die->comdat_type_p && names == pubtype_table)
10852 comdat_type_node *type_node = pub->die->die_id.die_type_node;
10854 if (type_node != NULL)
10855 die_offset = (type_node->skeleton_die != NULL
10856 ? type_node->skeleton_die->die_offset
10857 : comp_unit_die ()->die_offset);
10860 output_pubname (die_offset, pub);
10864 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
10867 /* Output public names and types tables if necessary. */
10869 static void
10870 output_pubtables (void)
10872 if (!want_pubnames () || !info_section_emitted)
10873 return;
10875 switch_to_section (debug_pubnames_section);
10876 output_pubnames (pubname_table);
10877 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
10878 It shouldn't hurt to emit it always, since pure DWARF2 consumers
10879 simply won't look for the section. */
10880 switch_to_section (debug_pubtypes_section);
10881 output_pubnames (pubtype_table);
10885 /* Output the information that goes into the .debug_aranges table.
10886 Namely, define the beginning and ending address range of the
10887 text section generated for this compilation unit. */
10889 static void
10890 output_aranges (void)
10892 unsigned i;
10893 unsigned long aranges_length = size_of_aranges ();
10895 if (!XCOFF_DEBUGGING_INFO)
10897 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10898 dw2_asm_output_data (4, 0xffffffff,
10899 "Initial length escape value indicating 64-bit DWARF extension");
10900 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
10901 "Length of Address Ranges Info");
10904 /* Version number for aranges is still 2, even up to DWARF5. */
10905 dw2_asm_output_data (2, 2, "DWARF Version");
10906 if (dwarf_split_debug_info)
10907 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
10908 debug_skeleton_info_section,
10909 "Offset of Compilation Unit Info");
10910 else
10911 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
10912 debug_info_section,
10913 "Offset of Compilation Unit Info");
10914 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
10915 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
10917 /* We need to align to twice the pointer size here. */
10918 if (DWARF_ARANGES_PAD_SIZE)
10920 /* Pad using a 2 byte words so that padding is correct for any
10921 pointer size. */
10922 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
10923 2 * DWARF2_ADDR_SIZE);
10924 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
10925 dw2_asm_output_data (2, 0, NULL);
10928 /* It is necessary not to output these entries if the sections were
10929 not used; if the sections were not used, the length will be 0 and
10930 the address may end up as 0 if the section is discarded by ld
10931 --gc-sections, leaving an invalid (0, 0) entry that can be
10932 confused with the terminator. */
10933 if (text_section_used)
10935 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
10936 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
10937 text_section_label, "Length");
10939 if (cold_text_section_used)
10941 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
10942 "Address");
10943 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
10944 cold_text_section_label, "Length");
10947 if (have_multiple_function_sections)
10949 unsigned fde_idx;
10950 dw_fde_ref fde;
10952 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
10954 if (DECL_IGNORED_P (fde->decl))
10955 continue;
10956 if (!fde->in_std_section)
10958 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
10959 "Address");
10960 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
10961 fde->dw_fde_begin, "Length");
10963 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
10965 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
10966 "Address");
10967 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
10968 fde->dw_fde_second_begin, "Length");
10973 /* Output the terminator words. */
10974 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
10975 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
10978 /* Add a new entry to .debug_ranges. Return its index into
10979 ranges_table vector. */
10981 static unsigned int
10982 add_ranges_num (int num, bool maybe_new_sec)
10984 dw_ranges r = { NULL, num, 0, maybe_new_sec };
10985 vec_safe_push (ranges_table, r);
10986 return vec_safe_length (ranges_table) - 1;
10989 /* Add a new entry to .debug_ranges corresponding to a block, or a
10990 range terminator if BLOCK is NULL. MAYBE_NEW_SEC is true if
10991 this entry might be in a different section from previous range. */
10993 static unsigned int
10994 add_ranges (const_tree block, bool maybe_new_sec)
10996 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0, maybe_new_sec);
10999 /* Note that (*rnglist_table)[offset] is either a head of a rnglist
11000 chain, or middle entry of a chain that will be directly referred to. */
11002 static void
11003 note_rnglist_head (unsigned int offset)
11005 if (dwarf_version < 5 || (*ranges_table)[offset].label)
11006 return;
11007 (*ranges_table)[offset].label = gen_internal_sym ("LLRL");
11010 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
11011 When using dwarf_split_debug_info, address attributes in dies destined
11012 for the final executable should be direct references--setting the
11013 parameter force_direct ensures this behavior. */
11015 static void
11016 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
11017 bool *added, bool force_direct)
11019 unsigned int in_use = vec_safe_length (ranges_by_label);
11020 unsigned int offset;
11021 dw_ranges_by_label rbl = { begin, end };
11022 vec_safe_push (ranges_by_label, rbl);
11023 offset = add_ranges_num (-(int)in_use - 1, true);
11024 if (!*added)
11026 add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
11027 *added = true;
11028 note_rnglist_head (offset);
11032 /* Emit .debug_ranges section. */
11034 static void
11035 output_ranges (void)
11037 unsigned i;
11038 static const char *const start_fmt = "Offset %#x";
11039 const char *fmt = start_fmt;
11040 dw_ranges *r;
11042 switch_to_section (debug_ranges_section);
11043 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
11044 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11046 int block_num = r->num;
11048 if (block_num > 0)
11050 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
11051 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
11053 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
11054 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
11056 /* If all code is in the text section, then the compilation
11057 unit base address defaults to DW_AT_low_pc, which is the
11058 base of the text section. */
11059 if (!have_multiple_function_sections)
11061 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
11062 text_section_label,
11063 fmt, i * 2 * DWARF2_ADDR_SIZE);
11064 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
11065 text_section_label, NULL);
11068 /* Otherwise, the compilation unit base address is zero,
11069 which allows us to use absolute addresses, and not worry
11070 about whether the target supports cross-section
11071 arithmetic. */
11072 else
11074 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11075 fmt, i * 2 * DWARF2_ADDR_SIZE);
11076 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
11079 fmt = NULL;
11082 /* Negative block_num stands for an index into ranges_by_label. */
11083 else if (block_num < 0)
11085 int lab_idx = - block_num - 1;
11087 if (!have_multiple_function_sections)
11089 gcc_unreachable ();
11090 #if 0
11091 /* If we ever use add_ranges_by_labels () for a single
11092 function section, all we have to do is to take out
11093 the #if 0 above. */
11094 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11095 (*ranges_by_label)[lab_idx].begin,
11096 text_section_label,
11097 fmt, i * 2 * DWARF2_ADDR_SIZE);
11098 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11099 (*ranges_by_label)[lab_idx].end,
11100 text_section_label, NULL);
11101 #endif
11103 else
11105 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11106 (*ranges_by_label)[lab_idx].begin,
11107 fmt, i * 2 * DWARF2_ADDR_SIZE);
11108 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11109 (*ranges_by_label)[lab_idx].end,
11110 NULL);
11113 else
11115 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11116 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11117 fmt = start_fmt;
11122 /* Non-zero if .debug_line_str should be used for .debug_line section
11123 strings or strings that are likely shareable with those. */
11124 #define DWARF5_USE_DEBUG_LINE_STR \
11125 (!DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET \
11126 && (DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) != 0 \
11127 /* FIXME: there is no .debug_line_str.dwo section, \
11128 for -gsplit-dwarf we should use DW_FORM_strx instead. */ \
11129 && !dwarf_split_debug_info)
11131 /* Assign .debug_rnglists indexes. */
11133 static void
11134 index_rnglists (void)
11136 unsigned i;
11137 dw_ranges *r;
11139 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11140 if (r->label)
11141 r->idx = rnglist_idx++;
11144 /* Emit .debug_rnglists section. */
11146 static void
11147 output_rnglists (void)
11149 unsigned i;
11150 dw_ranges *r;
11151 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
11152 char l2[MAX_ARTIFICIAL_LABEL_BYTES];
11153 char basebuf[MAX_ARTIFICIAL_LABEL_BYTES];
11155 switch_to_section (debug_ranges_section);
11156 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
11157 ASM_GENERATE_INTERNAL_LABEL (l1, DEBUG_RANGES_SECTION_LABEL, 2);
11158 ASM_GENERATE_INTERNAL_LABEL (l2, DEBUG_RANGES_SECTION_LABEL, 3);
11159 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11160 dw2_asm_output_data (4, 0xffffffff,
11161 "Initial length escape value indicating "
11162 "64-bit DWARF extension");
11163 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
11164 "Length of Range Lists");
11165 ASM_OUTPUT_LABEL (asm_out_file, l1);
11166 dw2_asm_output_data (2, dwarf_version, "DWARF Version");
11167 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
11168 dw2_asm_output_data (1, 0, "Segment Size");
11169 /* Emit the offset table only for -gsplit-dwarf. If we don't care
11170 about relocation sizes and primarily care about the size of .debug*
11171 sections in linked shared libraries and executables, then
11172 the offset table plus corresponding DW_FORM_rnglistx uleb128 indexes
11173 into it are usually larger than just DW_FORM_sec_offset offsets
11174 into the .debug_rnglists section. */
11175 dw2_asm_output_data (4, dwarf_split_debug_info ? rnglist_idx : 0,
11176 "Offset Entry Count");
11177 if (dwarf_split_debug_info)
11179 ASM_OUTPUT_LABEL (asm_out_file, ranges_base_label);
11180 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11181 if (r->label)
11182 dw2_asm_output_delta (DWARF_OFFSET_SIZE, r->label,
11183 ranges_base_label, NULL);
11186 const char *lab = "";
11187 unsigned int len = vec_safe_length (ranges_table);
11188 const char *base = NULL;
11189 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11191 int block_num = r->num;
11193 if (r->label)
11195 ASM_OUTPUT_LABEL (asm_out_file, r->label);
11196 lab = r->label;
11198 if (HAVE_AS_LEB128 && (r->label || r->maybe_new_sec))
11199 base = NULL;
11200 if (block_num > 0)
11202 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
11203 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
11205 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
11206 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
11208 if (HAVE_AS_LEB128)
11210 /* If all code is in the text section, then the compilation
11211 unit base address defaults to DW_AT_low_pc, which is the
11212 base of the text section. */
11213 if (!have_multiple_function_sections)
11215 dw2_asm_output_data (1, DW_RLE_offset_pair,
11216 "DW_RLE_offset_pair (%s)", lab);
11217 dw2_asm_output_delta_uleb128 (blabel, text_section_label,
11218 "Range begin address (%s)", lab);
11219 dw2_asm_output_delta_uleb128 (elabel, text_section_label,
11220 "Range end address (%s)", lab);
11221 continue;
11223 if (base == NULL)
11225 dw_ranges *r2 = NULL;
11226 if (i < len - 1)
11227 r2 = &(*ranges_table)[i + 1];
11228 if (r2
11229 && r2->num != 0
11230 && r2->label == NULL
11231 && !r2->maybe_new_sec)
11233 dw2_asm_output_data (1, DW_RLE_base_address,
11234 "DW_RLE_base_address (%s)", lab);
11235 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11236 "Base address (%s)", lab);
11237 strcpy (basebuf, blabel);
11238 base = basebuf;
11241 if (base)
11243 dw2_asm_output_data (1, DW_RLE_offset_pair,
11244 "DW_RLE_offset_pair (%s)", lab);
11245 dw2_asm_output_delta_uleb128 (blabel, base,
11246 "Range begin address (%s)", lab);
11247 dw2_asm_output_delta_uleb128 (elabel, base,
11248 "Range end address (%s)", lab);
11249 continue;
11251 dw2_asm_output_data (1, DW_RLE_start_length,
11252 "DW_RLE_start_length (%s)", lab);
11253 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11254 "Range begin address (%s)", lab);
11255 dw2_asm_output_delta_uleb128 (elabel, blabel,
11256 "Range length (%s)", lab);
11258 else
11260 dw2_asm_output_data (1, DW_RLE_start_end,
11261 "DW_RLE_start_end (%s)", lab);
11262 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11263 "Range begin address (%s)", lab);
11264 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel,
11265 "Range end address (%s)", lab);
11269 /* Negative block_num stands for an index into ranges_by_label. */
11270 else if (block_num < 0)
11272 int lab_idx = - block_num - 1;
11273 const char *blabel = (*ranges_by_label)[lab_idx].begin;
11274 const char *elabel = (*ranges_by_label)[lab_idx].end;
11276 if (!have_multiple_function_sections)
11277 gcc_unreachable ();
11278 if (HAVE_AS_LEB128)
11280 dw2_asm_output_data (1, DW_RLE_start_length,
11281 "DW_RLE_start_length (%s)", lab);
11282 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11283 "Range begin address (%s)", lab);
11284 dw2_asm_output_delta_uleb128 (elabel, blabel,
11285 "Range length (%s)", lab);
11287 else
11289 dw2_asm_output_data (1, DW_RLE_start_end,
11290 "DW_RLE_start_end (%s)", lab);
11291 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11292 "Range begin address (%s)", lab);
11293 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel,
11294 "Range end address (%s)", lab);
11297 else
11298 dw2_asm_output_data (1, DW_RLE_end_of_list,
11299 "DW_RLE_end_of_list (%s)", lab);
11301 ASM_OUTPUT_LABEL (asm_out_file, l2);
11304 /* Data structure containing information about input files. */
11305 struct file_info
11307 const char *path; /* Complete file name. */
11308 const char *fname; /* File name part. */
11309 int length; /* Length of entire string. */
11310 struct dwarf_file_data * file_idx; /* Index in input file table. */
11311 int dir_idx; /* Index in directory table. */
11314 /* Data structure containing information about directories with source
11315 files. */
11316 struct dir_info
11318 const char *path; /* Path including directory name. */
11319 int length; /* Path length. */
11320 int prefix; /* Index of directory entry which is a prefix. */
11321 int count; /* Number of files in this directory. */
11322 int dir_idx; /* Index of directory used as base. */
11325 /* Callback function for file_info comparison. We sort by looking at
11326 the directories in the path. */
11328 static int
11329 file_info_cmp (const void *p1, const void *p2)
11331 const struct file_info *const s1 = (const struct file_info *) p1;
11332 const struct file_info *const s2 = (const struct file_info *) p2;
11333 const unsigned char *cp1;
11334 const unsigned char *cp2;
11336 /* Take care of file names without directories. We need to make sure that
11337 we return consistent values to qsort since some will get confused if
11338 we return the same value when identical operands are passed in opposite
11339 orders. So if neither has a directory, return 0 and otherwise return
11340 1 or -1 depending on which one has the directory. */
11341 if ((s1->path == s1->fname || s2->path == s2->fname))
11342 return (s2->path == s2->fname) - (s1->path == s1->fname);
11344 cp1 = (const unsigned char *) s1->path;
11345 cp2 = (const unsigned char *) s2->path;
11347 while (1)
11349 ++cp1;
11350 ++cp2;
11351 /* Reached the end of the first path? If so, handle like above. */
11352 if ((cp1 == (const unsigned char *) s1->fname)
11353 || (cp2 == (const unsigned char *) s2->fname))
11354 return ((cp2 == (const unsigned char *) s2->fname)
11355 - (cp1 == (const unsigned char *) s1->fname));
11357 /* Character of current path component the same? */
11358 else if (*cp1 != *cp2)
11359 return *cp1 - *cp2;
11363 struct file_name_acquire_data
11365 struct file_info *files;
11366 int used_files;
11367 int max_files;
11370 /* Traversal function for the hash table. */
11373 file_name_acquire (dwarf_file_data **slot, file_name_acquire_data *fnad)
11375 struct dwarf_file_data *d = *slot;
11376 struct file_info *fi;
11377 const char *f;
11379 gcc_assert (fnad->max_files >= d->emitted_number);
11381 if (! d->emitted_number)
11382 return 1;
11384 gcc_assert (fnad->max_files != fnad->used_files);
11386 fi = fnad->files + fnad->used_files++;
11388 /* Skip all leading "./". */
11389 f = d->filename;
11390 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
11391 f += 2;
11393 /* Create a new array entry. */
11394 fi->path = f;
11395 fi->length = strlen (f);
11396 fi->file_idx = d;
11398 /* Search for the file name part. */
11399 f = strrchr (f, DIR_SEPARATOR);
11400 #if defined (DIR_SEPARATOR_2)
11402 char *g = strrchr (fi->path, DIR_SEPARATOR_2);
11404 if (g != NULL)
11406 if (f == NULL || f < g)
11407 f = g;
11410 #endif
11412 fi->fname = f == NULL ? fi->path : f + 1;
11413 return 1;
11416 /* Helper function for output_file_names. Emit a FORM encoded
11417 string STR, with assembly comment start ENTRY_KIND and
11418 index IDX */
11420 static void
11421 output_line_string (enum dwarf_form form, const char *str,
11422 const char *entry_kind, unsigned int idx)
11424 switch (form)
11426 case DW_FORM_string:
11427 dw2_asm_output_nstring (str, -1, "%s: %#x", entry_kind, idx);
11428 break;
11429 case DW_FORM_line_strp:
11430 if (!debug_line_str_hash)
11431 debug_line_str_hash
11432 = hash_table<indirect_string_hasher>::create_ggc (10);
11434 struct indirect_string_node *node;
11435 node = find_AT_string_in_table (str, debug_line_str_hash);
11436 set_indirect_string (node);
11437 node->form = form;
11438 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
11439 debug_line_str_section, "%s: %#x: \"%s\"",
11440 entry_kind, 0, node->str);
11441 break;
11442 default:
11443 gcc_unreachable ();
11447 /* Output the directory table and the file name table. We try to minimize
11448 the total amount of memory needed. A heuristic is used to avoid large
11449 slowdowns with many input files. */
11451 static void
11452 output_file_names (void)
11454 struct file_name_acquire_data fnad;
11455 int numfiles;
11456 struct file_info *files;
11457 struct dir_info *dirs;
11458 int *saved;
11459 int *savehere;
11460 int *backmap;
11461 int ndirs;
11462 int idx_offset;
11463 int i;
11465 if (!last_emitted_file)
11467 if (dwarf_version >= 5)
11469 dw2_asm_output_data (1, 0, "Directory entry format count");
11470 dw2_asm_output_data_uleb128 (0, "Directories count");
11471 dw2_asm_output_data (1, 0, "File name entry format count");
11472 dw2_asm_output_data_uleb128 (0, "File names count");
11474 else
11476 dw2_asm_output_data (1, 0, "End directory table");
11477 dw2_asm_output_data (1, 0, "End file name table");
11479 return;
11482 numfiles = last_emitted_file->emitted_number;
11484 /* Allocate the various arrays we need. */
11485 files = XALLOCAVEC (struct file_info, numfiles);
11486 dirs = XALLOCAVEC (struct dir_info, numfiles);
11488 fnad.files = files;
11489 fnad.used_files = 0;
11490 fnad.max_files = numfiles;
11491 file_table->traverse<file_name_acquire_data *, file_name_acquire> (&fnad);
11492 gcc_assert (fnad.used_files == fnad.max_files);
11494 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
11496 /* Find all the different directories used. */
11497 dirs[0].path = files[0].path;
11498 dirs[0].length = files[0].fname - files[0].path;
11499 dirs[0].prefix = -1;
11500 dirs[0].count = 1;
11501 dirs[0].dir_idx = 0;
11502 files[0].dir_idx = 0;
11503 ndirs = 1;
11505 for (i = 1; i < numfiles; i++)
11506 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
11507 && memcmp (dirs[ndirs - 1].path, files[i].path,
11508 dirs[ndirs - 1].length) == 0)
11510 /* Same directory as last entry. */
11511 files[i].dir_idx = ndirs - 1;
11512 ++dirs[ndirs - 1].count;
11514 else
11516 int j;
11518 /* This is a new directory. */
11519 dirs[ndirs].path = files[i].path;
11520 dirs[ndirs].length = files[i].fname - files[i].path;
11521 dirs[ndirs].count = 1;
11522 dirs[ndirs].dir_idx = ndirs;
11523 files[i].dir_idx = ndirs;
11525 /* Search for a prefix. */
11526 dirs[ndirs].prefix = -1;
11527 for (j = 0; j < ndirs; j++)
11528 if (dirs[j].length < dirs[ndirs].length
11529 && dirs[j].length > 1
11530 && (dirs[ndirs].prefix == -1
11531 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
11532 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
11533 dirs[ndirs].prefix = j;
11535 ++ndirs;
11538 /* Now to the actual work. We have to find a subset of the directories which
11539 allow expressing the file name using references to the directory table
11540 with the least amount of characters. We do not do an exhaustive search
11541 where we would have to check out every combination of every single
11542 possible prefix. Instead we use a heuristic which provides nearly optimal
11543 results in most cases and never is much off. */
11544 saved = XALLOCAVEC (int, ndirs);
11545 savehere = XALLOCAVEC (int, ndirs);
11547 memset (saved, '\0', ndirs * sizeof (saved[0]));
11548 for (i = 0; i < ndirs; i++)
11550 int j;
11551 int total;
11553 /* We can always save some space for the current directory. But this
11554 does not mean it will be enough to justify adding the directory. */
11555 savehere[i] = dirs[i].length;
11556 total = (savehere[i] - saved[i]) * dirs[i].count;
11558 for (j = i + 1; j < ndirs; j++)
11560 savehere[j] = 0;
11561 if (saved[j] < dirs[i].length)
11563 /* Determine whether the dirs[i] path is a prefix of the
11564 dirs[j] path. */
11565 int k;
11567 k = dirs[j].prefix;
11568 while (k != -1 && k != (int) i)
11569 k = dirs[k].prefix;
11571 if (k == (int) i)
11573 /* Yes it is. We can possibly save some memory by
11574 writing the filenames in dirs[j] relative to
11575 dirs[i]. */
11576 savehere[j] = dirs[i].length;
11577 total += (savehere[j] - saved[j]) * dirs[j].count;
11582 /* Check whether we can save enough to justify adding the dirs[i]
11583 directory. */
11584 if (total > dirs[i].length + 1)
11586 /* It's worthwhile adding. */
11587 for (j = i; j < ndirs; j++)
11588 if (savehere[j] > 0)
11590 /* Remember how much we saved for this directory so far. */
11591 saved[j] = savehere[j];
11593 /* Remember the prefix directory. */
11594 dirs[j].dir_idx = i;
11599 /* Emit the directory name table. */
11600 idx_offset = dirs[0].length > 0 ? 1 : 0;
11601 enum dwarf_form str_form = DW_FORM_string;
11602 enum dwarf_form idx_form = DW_FORM_udata;
11603 if (dwarf_version >= 5)
11605 const char *comp_dir = comp_dir_string ();
11606 if (comp_dir == NULL)
11607 comp_dir = "";
11608 dw2_asm_output_data (1, 1, "Directory entry format count");
11609 if (DWARF5_USE_DEBUG_LINE_STR)
11610 str_form = DW_FORM_line_strp;
11611 dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
11612 dw2_asm_output_data_uleb128 (str_form, "%s",
11613 get_DW_FORM_name (str_form));
11614 dw2_asm_output_data_uleb128 (ndirs + idx_offset, "Directories count");
11615 if (str_form == DW_FORM_string)
11617 dw2_asm_output_nstring (comp_dir, -1, "Directory Entry: %#x", 0);
11618 for (i = 1 - idx_offset; i < ndirs; i++)
11619 dw2_asm_output_nstring (dirs[i].path,
11620 dirs[i].length
11621 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
11622 "Directory Entry: %#x", i + idx_offset);
11624 else
11626 output_line_string (str_form, comp_dir, "Directory Entry", 0);
11627 for (i = 1 - idx_offset; i < ndirs; i++)
11629 const char *str
11630 = ggc_alloc_string (dirs[i].path,
11631 dirs[i].length
11632 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR);
11633 output_line_string (str_form, str, "Directory Entry",
11634 (unsigned) i + idx_offset);
11638 else
11640 for (i = 1 - idx_offset; i < ndirs; i++)
11641 dw2_asm_output_nstring (dirs[i].path,
11642 dirs[i].length
11643 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
11644 "Directory Entry: %#x", i + idx_offset);
11646 dw2_asm_output_data (1, 0, "End directory table");
11649 /* We have to emit them in the order of emitted_number since that's
11650 used in the debug info generation. To do this efficiently we
11651 generate a back-mapping of the indices first. */
11652 backmap = XALLOCAVEC (int, numfiles);
11653 for (i = 0; i < numfiles; i++)
11654 backmap[files[i].file_idx->emitted_number - 1] = i;
11656 if (dwarf_version >= 5)
11658 const char *filename0 = get_AT_string (comp_unit_die (), DW_AT_name);
11659 if (filename0 == NULL)
11660 filename0 = "";
11661 /* DW_LNCT_directory_index can use DW_FORM_udata, DW_FORM_data1 and
11662 DW_FORM_data2. Choose one based on the number of directories
11663 and how much space would they occupy in each encoding.
11664 If we have at most 256 directories, all indexes fit into
11665 a single byte, so DW_FORM_data1 is most compact (if there
11666 are at most 128 directories, DW_FORM_udata would be as
11667 compact as that, but not shorter and slower to decode). */
11668 if (ndirs + idx_offset <= 256)
11669 idx_form = DW_FORM_data1;
11670 /* If there are more than 65536 directories, we have to use
11671 DW_FORM_udata, DW_FORM_data2 can't refer to them.
11672 Otherwise, compute what space would occupy if all the indexes
11673 used DW_FORM_udata - sum - and compare that to how large would
11674 be DW_FORM_data2 encoding, and pick the more efficient one. */
11675 else if (ndirs + idx_offset <= 65536)
11677 unsigned HOST_WIDE_INT sum = 1;
11678 for (i = 0; i < numfiles; i++)
11680 int file_idx = backmap[i];
11681 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
11682 sum += size_of_uleb128 (dir_idx);
11684 if (sum >= HOST_WIDE_INT_UC (2) * (numfiles + 1))
11685 idx_form = DW_FORM_data2;
11687 #ifdef VMS_DEBUGGING_INFO
11688 dw2_asm_output_data (1, 4, "File name entry format count");
11689 #else
11690 dw2_asm_output_data (1, 2, "File name entry format count");
11691 #endif
11692 dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
11693 dw2_asm_output_data_uleb128 (str_form, "%s",
11694 get_DW_FORM_name (str_form));
11695 dw2_asm_output_data_uleb128 (DW_LNCT_directory_index,
11696 "DW_LNCT_directory_index");
11697 dw2_asm_output_data_uleb128 (idx_form, "%s",
11698 get_DW_FORM_name (idx_form));
11699 #ifdef VMS_DEBUGGING_INFO
11700 dw2_asm_output_data_uleb128 (DW_LNCT_timestamp, "DW_LNCT_timestamp");
11701 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
11702 dw2_asm_output_data_uleb128 (DW_LNCT_size, "DW_LNCT_size");
11703 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
11704 #endif
11705 dw2_asm_output_data_uleb128 (numfiles + 1, "File names count");
11707 output_line_string (str_form, filename0, "File Entry", 0);
11709 /* Include directory index. */
11710 if (dwarf_version >= 5 && idx_form != DW_FORM_udata)
11711 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
11712 0, NULL);
11713 else
11714 dw2_asm_output_data_uleb128 (0, NULL);
11716 #ifdef VMS_DEBUGGING_INFO
11717 dw2_asm_output_data_uleb128 (0, NULL);
11718 dw2_asm_output_data_uleb128 (0, NULL);
11719 #endif
11722 /* Now write all the file names. */
11723 for (i = 0; i < numfiles; i++)
11725 int file_idx = backmap[i];
11726 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
11728 #ifdef VMS_DEBUGGING_INFO
11729 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
11731 /* Setting these fields can lead to debugger miscomparisons,
11732 but VMS Debug requires them to be set correctly. */
11734 int ver;
11735 long long cdt;
11736 long siz;
11737 int maxfilelen = (strlen (files[file_idx].path)
11738 + dirs[dir_idx].length
11739 + MAX_VMS_VERSION_LEN + 1);
11740 char *filebuf = XALLOCAVEC (char, maxfilelen);
11742 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
11743 snprintf (filebuf, maxfilelen, "%s;%d",
11744 files[file_idx].path + dirs[dir_idx].length, ver);
11746 output_line_string (str_form, filebuf, "File Entry", (unsigned) i + 1);
11748 /* Include directory index. */
11749 if (dwarf_version >= 5 && idx_form != DW_FORM_udata)
11750 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
11751 dir_idx + idx_offset, NULL);
11752 else
11753 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
11755 /* Modification time. */
11756 dw2_asm_output_data_uleb128 ((vms_file_stats_name (files[file_idx].path,
11757 &cdt, 0, 0, 0) == 0)
11758 ? cdt : 0, NULL);
11760 /* File length in bytes. */
11761 dw2_asm_output_data_uleb128 ((vms_file_stats_name (files[file_idx].path,
11762 0, &siz, 0, 0) == 0)
11763 ? siz : 0, NULL);
11764 #else
11765 output_line_string (str_form,
11766 files[file_idx].path + dirs[dir_idx].length,
11767 "File Entry", (unsigned) i + 1);
11769 /* Include directory index. */
11770 if (dwarf_version >= 5 && idx_form != DW_FORM_udata)
11771 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
11772 dir_idx + idx_offset, NULL);
11773 else
11774 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
11776 if (dwarf_version >= 5)
11777 continue;
11779 /* Modification time. */
11780 dw2_asm_output_data_uleb128 (0, NULL);
11782 /* File length in bytes. */
11783 dw2_asm_output_data_uleb128 (0, NULL);
11784 #endif /* VMS_DEBUGGING_INFO */
11787 if (dwarf_version < 5)
11788 dw2_asm_output_data (1, 0, "End file name table");
11792 /* Output one line number table into the .debug_line section. */
11794 static void
11795 output_one_line_info_table (dw_line_info_table *table)
11797 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
11798 unsigned int current_line = 1;
11799 bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
11800 dw_line_info_entry *ent;
11801 size_t i;
11803 FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
11805 switch (ent->opcode)
11807 case LI_set_address:
11808 /* ??? Unfortunately, we have little choice here currently, and
11809 must always use the most general form. GCC does not know the
11810 address delta itself, so we can't use DW_LNS_advance_pc. Many
11811 ports do have length attributes which will give an upper bound
11812 on the address range. We could perhaps use length attributes
11813 to determine when it is safe to use DW_LNS_fixed_advance_pc. */
11814 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
11816 /* This can handle any delta. This takes
11817 4+DWARF2_ADDR_SIZE bytes. */
11818 dw2_asm_output_data (1, 0, "set address %s", line_label);
11819 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
11820 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
11821 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
11822 break;
11824 case LI_set_line:
11825 if (ent->val == current_line)
11827 /* We still need to start a new row, so output a copy insn. */
11828 dw2_asm_output_data (1, DW_LNS_copy,
11829 "copy line %u", current_line);
11831 else
11833 int line_offset = ent->val - current_line;
11834 int line_delta = line_offset - DWARF_LINE_BASE;
11836 current_line = ent->val;
11837 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
11839 /* This can handle deltas from -10 to 234, using the current
11840 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
11841 This takes 1 byte. */
11842 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
11843 "line %u", current_line);
11845 else
11847 /* This can handle any delta. This takes at least 4 bytes,
11848 depending on the value being encoded. */
11849 dw2_asm_output_data (1, DW_LNS_advance_line,
11850 "advance to line %u", current_line);
11851 dw2_asm_output_data_sleb128 (line_offset, NULL);
11852 dw2_asm_output_data (1, DW_LNS_copy, NULL);
11855 break;
11857 case LI_set_file:
11858 dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
11859 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
11860 break;
11862 case LI_set_column:
11863 dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
11864 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
11865 break;
11867 case LI_negate_stmt:
11868 current_is_stmt = !current_is_stmt;
11869 dw2_asm_output_data (1, DW_LNS_negate_stmt,
11870 "is_stmt %d", current_is_stmt);
11871 break;
11873 case LI_set_prologue_end:
11874 dw2_asm_output_data (1, DW_LNS_set_prologue_end,
11875 "set prologue end");
11876 break;
11878 case LI_set_epilogue_begin:
11879 dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
11880 "set epilogue begin");
11881 break;
11883 case LI_set_discriminator:
11884 dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
11885 dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
11886 dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
11887 dw2_asm_output_data_uleb128 (ent->val, NULL);
11888 break;
11892 /* Emit debug info for the address of the end of the table. */
11893 dw2_asm_output_data (1, 0, "set address %s", table->end_label);
11894 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
11895 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
11896 dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
11898 dw2_asm_output_data (1, 0, "end sequence");
11899 dw2_asm_output_data_uleb128 (1, NULL);
11900 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
11903 /* Output the source line number correspondence information. This
11904 information goes into the .debug_line section. */
11906 static void
11907 output_line_info (bool prologue_only)
11909 static unsigned int generation;
11910 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
11911 char p1[MAX_ARTIFICIAL_LABEL_BYTES], p2[MAX_ARTIFICIAL_LABEL_BYTES];
11912 bool saw_one = false;
11913 int opc;
11915 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, generation);
11916 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, generation);
11917 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, generation);
11918 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, generation++);
11920 if (!XCOFF_DEBUGGING_INFO)
11922 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11923 dw2_asm_output_data (4, 0xffffffff,
11924 "Initial length escape value indicating 64-bit DWARF extension");
11925 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
11926 "Length of Source Line Info");
11929 ASM_OUTPUT_LABEL (asm_out_file, l1);
11931 dw2_asm_output_data (2, dwarf_version, "DWARF Version");
11932 if (dwarf_version >= 5)
11934 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
11935 dw2_asm_output_data (1, 0, "Segment Size");
11937 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
11938 ASM_OUTPUT_LABEL (asm_out_file, p1);
11940 /* Define the architecture-dependent minimum instruction length (in bytes).
11941 In this implementation of DWARF, this field is used for information
11942 purposes only. Since GCC generates assembly language, we have no
11943 a priori knowledge of how many instruction bytes are generated for each
11944 source line, and therefore can use only the DW_LNE_set_address and
11945 DW_LNS_fixed_advance_pc line information commands. Accordingly, we fix
11946 this as '1', which is "correct enough" for all architectures,
11947 and don't let the target override. */
11948 dw2_asm_output_data (1, 1, "Minimum Instruction Length");
11950 if (dwarf_version >= 4)
11951 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
11952 "Maximum Operations Per Instruction");
11953 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
11954 "Default is_stmt_start flag");
11955 dw2_asm_output_data (1, DWARF_LINE_BASE,
11956 "Line Base Value (Special Opcodes)");
11957 dw2_asm_output_data (1, DWARF_LINE_RANGE,
11958 "Line Range Value (Special Opcodes)");
11959 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
11960 "Special Opcode Base");
11962 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
11964 int n_op_args;
11965 switch (opc)
11967 case DW_LNS_advance_pc:
11968 case DW_LNS_advance_line:
11969 case DW_LNS_set_file:
11970 case DW_LNS_set_column:
11971 case DW_LNS_fixed_advance_pc:
11972 case DW_LNS_set_isa:
11973 n_op_args = 1;
11974 break;
11975 default:
11976 n_op_args = 0;
11977 break;
11980 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
11981 opc, n_op_args);
11984 /* Write out the information about the files we use. */
11985 output_file_names ();
11986 ASM_OUTPUT_LABEL (asm_out_file, p2);
11987 if (prologue_only)
11989 /* Output the marker for the end of the line number info. */
11990 ASM_OUTPUT_LABEL (asm_out_file, l2);
11991 return;
11994 if (separate_line_info)
11996 dw_line_info_table *table;
11997 size_t i;
11999 FOR_EACH_VEC_ELT (*separate_line_info, i, table)
12000 if (table->in_use)
12002 output_one_line_info_table (table);
12003 saw_one = true;
12006 if (cold_text_section_line_info && cold_text_section_line_info->in_use)
12008 output_one_line_info_table (cold_text_section_line_info);
12009 saw_one = true;
12012 /* ??? Some Darwin linkers crash on a .debug_line section with no
12013 sequences. Further, merely a DW_LNE_end_sequence entry is not
12014 sufficient -- the address column must also be initialized.
12015 Make sure to output at least one set_address/end_sequence pair,
12016 choosing .text since that section is always present. */
12017 if (text_section_line_info->in_use || !saw_one)
12018 output_one_line_info_table (text_section_line_info);
12020 /* Output the marker for the end of the line number info. */
12021 ASM_OUTPUT_LABEL (asm_out_file, l2);
12024 /* Return true if DW_AT_endianity should be emitted according to REVERSE. */
12026 static inline bool
12027 need_endianity_attribute_p (bool reverse)
12029 return reverse && (dwarf_version >= 3 || !dwarf_strict);
12032 /* Given a pointer to a tree node for some base type, return a pointer to
12033 a DIE that describes the given type. REVERSE is true if the type is
12034 to be interpreted in the reverse storage order wrt the target order.
12036 This routine must only be called for GCC type nodes that correspond to
12037 Dwarf base (fundamental) types. */
12039 static dw_die_ref
12040 base_type_die (tree type, bool reverse)
12042 dw_die_ref base_type_result;
12043 enum dwarf_type encoding;
12044 bool fpt_used = false;
12045 struct fixed_point_type_info fpt_info;
12046 tree type_bias = NULL_TREE;
12048 if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
12049 return 0;
12051 /* If this is a subtype that should not be emitted as a subrange type,
12052 use the base type. See subrange_type_for_debug_p. */
12053 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
12054 type = TREE_TYPE (type);
12056 switch (TREE_CODE (type))
12058 case INTEGER_TYPE:
12059 if ((dwarf_version >= 4 || !dwarf_strict)
12060 && TYPE_NAME (type)
12061 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12062 && DECL_IS_BUILTIN (TYPE_NAME (type))
12063 && DECL_NAME (TYPE_NAME (type)))
12065 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
12066 if (strcmp (name, "char16_t") == 0
12067 || strcmp (name, "char32_t") == 0)
12069 encoding = DW_ATE_UTF;
12070 break;
12073 if ((dwarf_version >= 3 || !dwarf_strict)
12074 && lang_hooks.types.get_fixed_point_type_info)
12076 memset (&fpt_info, 0, sizeof (fpt_info));
12077 if (lang_hooks.types.get_fixed_point_type_info (type, &fpt_info))
12079 fpt_used = true;
12080 encoding = ((TYPE_UNSIGNED (type))
12081 ? DW_ATE_unsigned_fixed
12082 : DW_ATE_signed_fixed);
12083 break;
12086 if (TYPE_STRING_FLAG (type))
12088 if (TYPE_UNSIGNED (type))
12089 encoding = DW_ATE_unsigned_char;
12090 else
12091 encoding = DW_ATE_signed_char;
12093 else if (TYPE_UNSIGNED (type))
12094 encoding = DW_ATE_unsigned;
12095 else
12096 encoding = DW_ATE_signed;
12098 if (!dwarf_strict
12099 && lang_hooks.types.get_type_bias)
12100 type_bias = lang_hooks.types.get_type_bias (type);
12101 break;
12103 case REAL_TYPE:
12104 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
12106 if (dwarf_version >= 3 || !dwarf_strict)
12107 encoding = DW_ATE_decimal_float;
12108 else
12109 encoding = DW_ATE_lo_user;
12111 else
12112 encoding = DW_ATE_float;
12113 break;
12115 case FIXED_POINT_TYPE:
12116 if (!(dwarf_version >= 3 || !dwarf_strict))
12117 encoding = DW_ATE_lo_user;
12118 else if (TYPE_UNSIGNED (type))
12119 encoding = DW_ATE_unsigned_fixed;
12120 else
12121 encoding = DW_ATE_signed_fixed;
12122 break;
12124 /* Dwarf2 doesn't know anything about complex ints, so use
12125 a user defined type for it. */
12126 case COMPLEX_TYPE:
12127 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
12128 encoding = DW_ATE_complex_float;
12129 else
12130 encoding = DW_ATE_lo_user;
12131 break;
12133 case BOOLEAN_TYPE:
12134 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
12135 encoding = DW_ATE_boolean;
12136 break;
12138 default:
12139 /* No other TREE_CODEs are Dwarf fundamental types. */
12140 gcc_unreachable ();
12143 base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
12145 add_AT_unsigned (base_type_result, DW_AT_byte_size,
12146 int_size_in_bytes (type));
12147 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
12149 if (need_endianity_attribute_p (reverse))
12150 add_AT_unsigned (base_type_result, DW_AT_endianity,
12151 BYTES_BIG_ENDIAN ? DW_END_little : DW_END_big);
12153 add_alignment_attribute (base_type_result, type);
12155 if (fpt_used)
12157 switch (fpt_info.scale_factor_kind)
12159 case fixed_point_scale_factor_binary:
12160 add_AT_int (base_type_result, DW_AT_binary_scale,
12161 fpt_info.scale_factor.binary);
12162 break;
12164 case fixed_point_scale_factor_decimal:
12165 add_AT_int (base_type_result, DW_AT_decimal_scale,
12166 fpt_info.scale_factor.decimal);
12167 break;
12169 case fixed_point_scale_factor_arbitrary:
12170 /* Arbitrary scale factors cannot be described in standard DWARF,
12171 yet. */
12172 if (!dwarf_strict)
12174 /* Describe the scale factor as a rational constant. */
12175 const dw_die_ref scale_factor
12176 = new_die (DW_TAG_constant, comp_unit_die (), type);
12178 add_AT_unsigned (scale_factor, DW_AT_GNU_numerator,
12179 fpt_info.scale_factor.arbitrary.numerator);
12180 add_AT_int (scale_factor, DW_AT_GNU_denominator,
12181 fpt_info.scale_factor.arbitrary.denominator);
12183 add_AT_die_ref (base_type_result, DW_AT_small, scale_factor);
12185 break;
12187 default:
12188 gcc_unreachable ();
12192 if (type_bias)
12193 add_scalar_info (base_type_result, DW_AT_GNU_bias, type_bias,
12194 dw_scalar_form_constant
12195 | dw_scalar_form_exprloc
12196 | dw_scalar_form_reference,
12197 NULL);
12199 add_pubtype (type, base_type_result);
12201 return base_type_result;
12204 /* A C++ function with deduced return type can have a TEMPLATE_TYPE_PARM
12205 named 'auto' in its type: return true for it, false otherwise. */
12207 static inline bool
12208 is_cxx_auto (tree type)
12210 if (is_cxx ())
12212 tree name = TYPE_IDENTIFIER (type);
12213 if (name == get_identifier ("auto")
12214 || name == get_identifier ("decltype(auto)"))
12215 return true;
12217 return false;
12220 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
12221 given input type is a Dwarf "fundamental" type. Otherwise return null. */
12223 static inline int
12224 is_base_type (tree type)
12226 switch (TREE_CODE (type))
12228 case ERROR_MARK:
12229 case VOID_TYPE:
12230 case INTEGER_TYPE:
12231 case REAL_TYPE:
12232 case FIXED_POINT_TYPE:
12233 case COMPLEX_TYPE:
12234 case BOOLEAN_TYPE:
12235 case POINTER_BOUNDS_TYPE:
12236 return 1;
12238 case ARRAY_TYPE:
12239 case RECORD_TYPE:
12240 case UNION_TYPE:
12241 case QUAL_UNION_TYPE:
12242 case ENUMERAL_TYPE:
12243 case FUNCTION_TYPE:
12244 case METHOD_TYPE:
12245 case POINTER_TYPE:
12246 case REFERENCE_TYPE:
12247 case NULLPTR_TYPE:
12248 case OFFSET_TYPE:
12249 case LANG_TYPE:
12250 case VECTOR_TYPE:
12251 return 0;
12253 default:
12254 if (is_cxx_auto (type))
12255 return 0;
12256 gcc_unreachable ();
12259 return 0;
12262 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
12263 node, return the size in bits for the type if it is a constant, or else
12264 return the alignment for the type if the type's size is not constant, or
12265 else return BITS_PER_WORD if the type actually turns out to be an
12266 ERROR_MARK node. */
12268 static inline unsigned HOST_WIDE_INT
12269 simple_type_size_in_bits (const_tree type)
12271 if (TREE_CODE (type) == ERROR_MARK)
12272 return BITS_PER_WORD;
12273 else if (TYPE_SIZE (type) == NULL_TREE)
12274 return 0;
12275 else if (tree_fits_uhwi_p (TYPE_SIZE (type)))
12276 return tree_to_uhwi (TYPE_SIZE (type));
12277 else
12278 return TYPE_ALIGN (type);
12281 /* Similarly, but return an offset_int instead of UHWI. */
12283 static inline offset_int
12284 offset_int_type_size_in_bits (const_tree type)
12286 if (TREE_CODE (type) == ERROR_MARK)
12287 return BITS_PER_WORD;
12288 else if (TYPE_SIZE (type) == NULL_TREE)
12289 return 0;
12290 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
12291 return wi::to_offset (TYPE_SIZE (type));
12292 else
12293 return TYPE_ALIGN (type);
12296 /* Given a pointer to a tree node for a subrange type, return a pointer
12297 to a DIE that describes the given type. */
12299 static dw_die_ref
12300 subrange_type_die (tree type, tree low, tree high, tree bias,
12301 dw_die_ref context_die)
12303 dw_die_ref subrange_die;
12304 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
12306 if (context_die == NULL)
12307 context_die = comp_unit_die ();
12309 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
12311 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
12313 /* The size of the subrange type and its base type do not match,
12314 so we need to generate a size attribute for the subrange type. */
12315 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
12318 add_alignment_attribute (subrange_die, type);
12320 if (low)
12321 add_bound_info (subrange_die, DW_AT_lower_bound, low, NULL);
12322 if (high)
12323 add_bound_info (subrange_die, DW_AT_upper_bound, high, NULL);
12324 if (bias && !dwarf_strict)
12325 add_scalar_info (subrange_die, DW_AT_GNU_bias, bias,
12326 dw_scalar_form_constant
12327 | dw_scalar_form_exprloc
12328 | dw_scalar_form_reference,
12329 NULL);
12331 return subrange_die;
12334 /* Returns the (const and/or volatile) cv_qualifiers associated with
12335 the decl node. This will normally be augmented with the
12336 cv_qualifiers of the underlying type in add_type_attribute. */
12338 static int
12339 decl_quals (const_tree decl)
12341 return ((TREE_READONLY (decl)
12342 /* The C++ front-end correctly marks reference-typed
12343 variables as readonly, but from a language (and debug
12344 info) standpoint they are not const-qualified. */
12345 && TREE_CODE (TREE_TYPE (decl)) != REFERENCE_TYPE
12346 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED)
12347 | (TREE_THIS_VOLATILE (decl)
12348 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED));
12351 /* Determine the TYPE whose qualifiers match the largest strict subset
12352 of the given TYPE_QUALS, and return its qualifiers. Ignore all
12353 qualifiers outside QUAL_MASK. */
12355 static int
12356 get_nearest_type_subqualifiers (tree type, int type_quals, int qual_mask)
12358 tree t;
12359 int best_rank = 0, best_qual = 0, max_rank;
12361 type_quals &= qual_mask;
12362 max_rank = popcount_hwi (type_quals) - 1;
12364 for (t = TYPE_MAIN_VARIANT (type); t && best_rank < max_rank;
12365 t = TYPE_NEXT_VARIANT (t))
12367 int q = TYPE_QUALS (t) & qual_mask;
12369 if ((q & type_quals) == q && q != type_quals
12370 && check_base_type (t, type))
12372 int rank = popcount_hwi (q);
12374 if (rank > best_rank)
12376 best_rank = rank;
12377 best_qual = q;
12382 return best_qual;
12385 struct dwarf_qual_info_t { int q; enum dwarf_tag t; };
12386 static const dwarf_qual_info_t dwarf_qual_info[] =
12388 { TYPE_QUAL_CONST, DW_TAG_const_type },
12389 { TYPE_QUAL_VOLATILE, DW_TAG_volatile_type },
12390 { TYPE_QUAL_RESTRICT, DW_TAG_restrict_type },
12391 { TYPE_QUAL_ATOMIC, DW_TAG_atomic_type }
12393 static const unsigned int dwarf_qual_info_size
12394 = sizeof (dwarf_qual_info) / sizeof (dwarf_qual_info[0]);
12396 /* If DIE is a qualified DIE of some base DIE with the same parent,
12397 return the base DIE, otherwise return NULL. Set MASK to the
12398 qualifiers added compared to the returned DIE. */
12400 static dw_die_ref
12401 qualified_die_p (dw_die_ref die, int *mask, unsigned int depth)
12403 unsigned int i;
12404 for (i = 0; i < dwarf_qual_info_size; i++)
12405 if (die->die_tag == dwarf_qual_info[i].t)
12406 break;
12407 if (i == dwarf_qual_info_size)
12408 return NULL;
12409 if (vec_safe_length (die->die_attr) != 1)
12410 return NULL;
12411 dw_die_ref type = get_AT_ref (die, DW_AT_type);
12412 if (type == NULL || type->die_parent != die->die_parent)
12413 return NULL;
12414 *mask |= dwarf_qual_info[i].q;
12415 if (depth)
12417 dw_die_ref ret = qualified_die_p (type, mask, depth - 1);
12418 if (ret)
12419 return ret;
12421 return type;
12424 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
12425 entry that chains the modifiers specified by CV_QUALS in front of the
12426 given type. REVERSE is true if the type is to be interpreted in the
12427 reverse storage order wrt the target order. */
12429 static dw_die_ref
12430 modified_type_die (tree type, int cv_quals, bool reverse,
12431 dw_die_ref context_die)
12433 enum tree_code code = TREE_CODE (type);
12434 dw_die_ref mod_type_die;
12435 dw_die_ref sub_die = NULL;
12436 tree item_type = NULL;
12437 tree qualified_type;
12438 tree name, low, high;
12439 dw_die_ref mod_scope;
12440 /* Only these cv-qualifiers are currently handled. */
12441 const int cv_qual_mask = (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE
12442 | TYPE_QUAL_RESTRICT | TYPE_QUAL_ATOMIC);
12444 if (code == ERROR_MARK)
12445 return NULL;
12447 if (lang_hooks.types.get_debug_type)
12449 tree debug_type = lang_hooks.types.get_debug_type (type);
12451 if (debug_type != NULL_TREE && debug_type != type)
12452 return modified_type_die (debug_type, cv_quals, reverse, context_die);
12455 cv_quals &= cv_qual_mask;
12457 /* Don't emit DW_TAG_restrict_type for DWARFv2, since it is a type
12458 tag modifier (and not an attribute) old consumers won't be able
12459 to handle it. */
12460 if (dwarf_version < 3)
12461 cv_quals &= ~TYPE_QUAL_RESTRICT;
12463 /* Likewise for DW_TAG_atomic_type for DWARFv5. */
12464 if (dwarf_version < 5)
12465 cv_quals &= ~TYPE_QUAL_ATOMIC;
12467 /* See if we already have the appropriately qualified variant of
12468 this type. */
12469 qualified_type = get_qualified_type (type, cv_quals);
12471 if (qualified_type == sizetype)
12473 /* Try not to expose the internal sizetype type's name. */
12474 if (TYPE_NAME (qualified_type)
12475 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
12477 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
12479 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
12480 && (TYPE_PRECISION (t)
12481 == TYPE_PRECISION (qualified_type))
12482 && (TYPE_UNSIGNED (t)
12483 == TYPE_UNSIGNED (qualified_type)));
12484 qualified_type = t;
12486 else if (qualified_type == sizetype
12487 && TREE_CODE (sizetype) == TREE_CODE (size_type_node)
12488 && TYPE_PRECISION (sizetype) == TYPE_PRECISION (size_type_node)
12489 && TYPE_UNSIGNED (sizetype) == TYPE_UNSIGNED (size_type_node))
12490 qualified_type = size_type_node;
12494 /* If we do, then we can just use its DIE, if it exists. */
12495 if (qualified_type)
12497 mod_type_die = lookup_type_die (qualified_type);
12499 /* DW_AT_endianity doesn't come from a qualifier on the type. */
12500 if (mod_type_die
12501 && (!need_endianity_attribute_p (reverse)
12502 || !is_base_type (type)
12503 || get_AT_unsigned (mod_type_die, DW_AT_endianity)))
12504 return mod_type_die;
12507 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
12509 /* Handle C typedef types. */
12510 if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
12511 && !DECL_ARTIFICIAL (name))
12513 tree dtype = TREE_TYPE (name);
12515 if (qualified_type == dtype)
12517 /* For a named type, use the typedef. */
12518 gen_type_die (qualified_type, context_die);
12519 return lookup_type_die (qualified_type);
12521 else
12523 int dquals = TYPE_QUALS_NO_ADDR_SPACE (dtype);
12524 dquals &= cv_qual_mask;
12525 if ((dquals & ~cv_quals) != TYPE_UNQUALIFIED
12526 || (cv_quals == dquals && DECL_ORIGINAL_TYPE (name) != type))
12527 /* cv-unqualified version of named type. Just use
12528 the unnamed type to which it refers. */
12529 return modified_type_die (DECL_ORIGINAL_TYPE (name), cv_quals,
12530 reverse, context_die);
12531 /* Else cv-qualified version of named type; fall through. */
12535 mod_scope = scope_die_for (type, context_die);
12537 if (cv_quals)
12539 int sub_quals = 0, first_quals = 0;
12540 unsigned i;
12541 dw_die_ref first = NULL, last = NULL;
12543 /* Determine a lesser qualified type that most closely matches
12544 this one. Then generate DW_TAG_* entries for the remaining
12545 qualifiers. */
12546 sub_quals = get_nearest_type_subqualifiers (type, cv_quals,
12547 cv_qual_mask);
12548 if (sub_quals && use_debug_types)
12550 bool needed = false;
12551 /* If emitting type units, make sure the order of qualifiers
12552 is canonical. Thus, start from unqualified type if
12553 an earlier qualifier is missing in sub_quals, but some later
12554 one is present there. */
12555 for (i = 0; i < dwarf_qual_info_size; i++)
12556 if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
12557 needed = true;
12558 else if (needed && (dwarf_qual_info[i].q & cv_quals))
12560 sub_quals = 0;
12561 break;
12564 mod_type_die = modified_type_die (type, sub_quals, reverse, context_die);
12565 if (mod_scope && mod_type_die && mod_type_die->die_parent == mod_scope)
12567 /* As not all intermediate qualified DIEs have corresponding
12568 tree types, ensure that qualified DIEs in the same scope
12569 as their DW_AT_type are emitted after their DW_AT_type,
12570 only with other qualified DIEs for the same type possibly
12571 in between them. Determine the range of such qualified
12572 DIEs now (first being the base type, last being corresponding
12573 last qualified DIE for it). */
12574 unsigned int count = 0;
12575 first = qualified_die_p (mod_type_die, &first_quals,
12576 dwarf_qual_info_size);
12577 if (first == NULL)
12578 first = mod_type_die;
12579 gcc_assert ((first_quals & ~sub_quals) == 0);
12580 for (count = 0, last = first;
12581 count < (1U << dwarf_qual_info_size);
12582 count++, last = last->die_sib)
12584 int quals = 0;
12585 if (last == mod_scope->die_child)
12586 break;
12587 if (qualified_die_p (last->die_sib, &quals, dwarf_qual_info_size)
12588 != first)
12589 break;
12593 for (i = 0; i < dwarf_qual_info_size; i++)
12594 if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
12596 dw_die_ref d;
12597 if (first && first != last)
12599 for (d = first->die_sib; ; d = d->die_sib)
12601 int quals = 0;
12602 qualified_die_p (d, &quals, dwarf_qual_info_size);
12603 if (quals == (first_quals | dwarf_qual_info[i].q))
12604 break;
12605 if (d == last)
12607 d = NULL;
12608 break;
12611 if (d)
12613 mod_type_die = d;
12614 continue;
12617 if (first)
12619 d = ggc_cleared_alloc<die_node> ();
12620 d->die_tag = dwarf_qual_info[i].t;
12621 add_child_die_after (mod_scope, d, last);
12622 last = d;
12624 else
12625 d = new_die (dwarf_qual_info[i].t, mod_scope, type);
12626 if (mod_type_die)
12627 add_AT_die_ref (d, DW_AT_type, mod_type_die);
12628 mod_type_die = d;
12629 first_quals |= dwarf_qual_info[i].q;
12632 else if (code == POINTER_TYPE || code == REFERENCE_TYPE)
12634 dwarf_tag tag = DW_TAG_pointer_type;
12635 if (code == REFERENCE_TYPE)
12637 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
12638 tag = DW_TAG_rvalue_reference_type;
12639 else
12640 tag = DW_TAG_reference_type;
12642 mod_type_die = new_die (tag, mod_scope, type);
12644 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
12645 simple_type_size_in_bits (type) / BITS_PER_UNIT);
12646 add_alignment_attribute (mod_type_die, type);
12647 item_type = TREE_TYPE (type);
12649 addr_space_t as = TYPE_ADDR_SPACE (item_type);
12650 if (!ADDR_SPACE_GENERIC_P (as))
12652 int action = targetm.addr_space.debug (as);
12653 if (action >= 0)
12655 /* Positive values indicate an address_class. */
12656 add_AT_unsigned (mod_type_die, DW_AT_address_class, action);
12658 else
12660 /* Negative values indicate an (inverted) segment base reg. */
12661 dw_loc_descr_ref d
12662 = one_reg_loc_descriptor (~action, VAR_INIT_STATUS_INITIALIZED);
12663 add_AT_loc (mod_type_die, DW_AT_segment, d);
12667 else if (code == INTEGER_TYPE
12668 && TREE_TYPE (type) != NULL_TREE
12669 && subrange_type_for_debug_p (type, &low, &high))
12671 tree bias = NULL_TREE;
12672 if (lang_hooks.types.get_type_bias)
12673 bias = lang_hooks.types.get_type_bias (type);
12674 mod_type_die = subrange_type_die (type, low, high, bias, context_die);
12675 item_type = TREE_TYPE (type);
12677 else if (is_base_type (type))
12678 mod_type_die = base_type_die (type, reverse);
12679 else
12681 gen_type_die (type, context_die);
12683 /* We have to get the type_main_variant here (and pass that to the
12684 `lookup_type_die' routine) because the ..._TYPE node we have
12685 might simply be a *copy* of some original type node (where the
12686 copy was created to help us keep track of typedef names) and
12687 that copy might have a different TYPE_UID from the original
12688 ..._TYPE node. */
12689 if (TREE_CODE (type) == FUNCTION_TYPE
12690 || TREE_CODE (type) == METHOD_TYPE)
12692 /* For function/method types, can't just use type_main_variant here,
12693 because that can have different ref-qualifiers for C++,
12694 but try to canonicalize. */
12695 tree main = TYPE_MAIN_VARIANT (type);
12696 for (tree t = main; t; t = TYPE_NEXT_VARIANT (t))
12697 if (TYPE_QUALS_NO_ADDR_SPACE (t) == 0
12698 && check_base_type (t, main)
12699 && check_lang_type (t, type))
12700 return lookup_type_die (t);
12701 return lookup_type_die (type);
12703 else if (TREE_CODE (type) != VECTOR_TYPE
12704 && TREE_CODE (type) != ARRAY_TYPE)
12705 return lookup_type_die (type_main_variant (type));
12706 else
12707 /* Vectors have the debugging information in the type,
12708 not the main variant. */
12709 return lookup_type_die (type);
12712 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
12713 don't output a DW_TAG_typedef, since there isn't one in the
12714 user's program; just attach a DW_AT_name to the type.
12715 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
12716 if the base type already has the same name. */
12717 if (name
12718 && ((TREE_CODE (name) != TYPE_DECL
12719 && (qualified_type == TYPE_MAIN_VARIANT (type)
12720 || (cv_quals == TYPE_UNQUALIFIED)))
12721 || (TREE_CODE (name) == TYPE_DECL
12722 && TREE_TYPE (name) == qualified_type
12723 && DECL_NAME (name))))
12725 if (TREE_CODE (name) == TYPE_DECL)
12726 /* Could just call add_name_and_src_coords_attributes here,
12727 but since this is a builtin type it doesn't have any
12728 useful source coordinates anyway. */
12729 name = DECL_NAME (name);
12730 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
12732 /* This probably indicates a bug. */
12733 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
12735 name = TYPE_IDENTIFIER (type);
12736 add_name_attribute (mod_type_die,
12737 name ? IDENTIFIER_POINTER (name) : "__unknown__");
12740 if (qualified_type)
12741 equate_type_number_to_die (qualified_type, mod_type_die);
12743 if (item_type)
12744 /* We must do this after the equate_type_number_to_die call, in case
12745 this is a recursive type. This ensures that the modified_type_die
12746 recursion will terminate even if the type is recursive. Recursive
12747 types are possible in Ada. */
12748 sub_die = modified_type_die (item_type,
12749 TYPE_QUALS_NO_ADDR_SPACE (item_type),
12750 reverse,
12751 context_die);
12753 if (sub_die != NULL)
12754 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
12756 add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
12757 if (TYPE_ARTIFICIAL (type))
12758 add_AT_flag (mod_type_die, DW_AT_artificial, 1);
12760 return mod_type_die;
12763 /* Generate DIEs for the generic parameters of T.
12764 T must be either a generic type or a generic function.
12765 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
12767 static void
12768 gen_generic_params_dies (tree t)
12770 tree parms, args;
12771 int parms_num, i;
12772 dw_die_ref die = NULL;
12773 int non_default;
12775 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
12776 return;
12778 if (TYPE_P (t))
12779 die = lookup_type_die (t);
12780 else if (DECL_P (t))
12781 die = lookup_decl_die (t);
12783 gcc_assert (die);
12785 parms = lang_hooks.get_innermost_generic_parms (t);
12786 if (!parms)
12787 /* T has no generic parameter. It means T is neither a generic type
12788 or function. End of story. */
12789 return;
12791 parms_num = TREE_VEC_LENGTH (parms);
12792 args = lang_hooks.get_innermost_generic_args (t);
12793 if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
12794 non_default = int_cst_value (TREE_CHAIN (args));
12795 else
12796 non_default = TREE_VEC_LENGTH (args);
12797 for (i = 0; i < parms_num; i++)
12799 tree parm, arg, arg_pack_elems;
12800 dw_die_ref parm_die;
12802 parm = TREE_VEC_ELT (parms, i);
12803 arg = TREE_VEC_ELT (args, i);
12804 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
12805 gcc_assert (parm && TREE_VALUE (parm) && arg);
12807 if (parm && TREE_VALUE (parm) && arg)
12809 /* If PARM represents a template parameter pack,
12810 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
12811 by DW_TAG_template_*_parameter DIEs for the argument
12812 pack elements of ARG. Note that ARG would then be
12813 an argument pack. */
12814 if (arg_pack_elems)
12815 parm_die = template_parameter_pack_die (TREE_VALUE (parm),
12816 arg_pack_elems,
12817 die);
12818 else
12819 parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
12820 true /* emit name */, die);
12821 if (i >= non_default)
12822 add_AT_flag (parm_die, DW_AT_default_value, 1);
12827 /* Create and return a DIE for PARM which should be
12828 the representation of a generic type parameter.
12829 For instance, in the C++ front end, PARM would be a template parameter.
12830 ARG is the argument to PARM.
12831 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
12832 name of the PARM.
12833 PARENT_DIE is the parent DIE which the new created DIE should be added to,
12834 as a child node. */
12836 static dw_die_ref
12837 generic_parameter_die (tree parm, tree arg,
12838 bool emit_name_p,
12839 dw_die_ref parent_die)
12841 dw_die_ref tmpl_die = NULL;
12842 const char *name = NULL;
12844 if (!parm || !DECL_NAME (parm) || !arg)
12845 return NULL;
12847 /* We support non-type generic parameters and arguments,
12848 type generic parameters and arguments, as well as
12849 generic generic parameters (a.k.a. template template parameters in C++)
12850 and arguments. */
12851 if (TREE_CODE (parm) == PARM_DECL)
12852 /* PARM is a nontype generic parameter */
12853 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
12854 else if (TREE_CODE (parm) == TYPE_DECL)
12855 /* PARM is a type generic parameter. */
12856 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
12857 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
12858 /* PARM is a generic generic parameter.
12859 Its DIE is a GNU extension. It shall have a
12860 DW_AT_name attribute to represent the name of the template template
12861 parameter, and a DW_AT_GNU_template_name attribute to represent the
12862 name of the template template argument. */
12863 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
12864 parent_die, parm);
12865 else
12866 gcc_unreachable ();
12868 if (tmpl_die)
12870 tree tmpl_type;
12872 /* If PARM is a generic parameter pack, it means we are
12873 emitting debug info for a template argument pack element.
12874 In other terms, ARG is a template argument pack element.
12875 In that case, we don't emit any DW_AT_name attribute for
12876 the die. */
12877 if (emit_name_p)
12879 name = IDENTIFIER_POINTER (DECL_NAME (parm));
12880 gcc_assert (name);
12881 add_AT_string (tmpl_die, DW_AT_name, name);
12884 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
12886 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
12887 TMPL_DIE should have a child DW_AT_type attribute that is set
12888 to the type of the argument to PARM, which is ARG.
12889 If PARM is a type generic parameter, TMPL_DIE should have a
12890 child DW_AT_type that is set to ARG. */
12891 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
12892 add_type_attribute (tmpl_die, tmpl_type,
12893 (TREE_THIS_VOLATILE (tmpl_type)
12894 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED),
12895 false, parent_die);
12897 else
12899 /* So TMPL_DIE is a DIE representing a
12900 a generic generic template parameter, a.k.a template template
12901 parameter in C++ and arg is a template. */
12903 /* The DW_AT_GNU_template_name attribute of the DIE must be set
12904 to the name of the argument. */
12905 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
12906 if (name)
12907 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
12910 if (TREE_CODE (parm) == PARM_DECL)
12911 /* So PARM is a non-type generic parameter.
12912 DWARF3 5.6.8 says we must set a DW_AT_const_value child
12913 attribute of TMPL_DIE which value represents the value
12914 of ARG.
12915 We must be careful here:
12916 The value of ARG might reference some function decls.
12917 We might currently be emitting debug info for a generic
12918 type and types are emitted before function decls, we don't
12919 know if the function decls referenced by ARG will actually be
12920 emitted after cgraph computations.
12921 So must defer the generation of the DW_AT_const_value to
12922 after cgraph is ready. */
12923 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
12926 return tmpl_die;
12929 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
12930 PARM_PACK must be a template parameter pack. The returned DIE
12931 will be child DIE of PARENT_DIE. */
12933 static dw_die_ref
12934 template_parameter_pack_die (tree parm_pack,
12935 tree parm_pack_args,
12936 dw_die_ref parent_die)
12938 dw_die_ref die;
12939 int j;
12941 gcc_assert (parent_die && parm_pack);
12943 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
12944 add_name_and_src_coords_attributes (die, parm_pack);
12945 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
12946 generic_parameter_die (parm_pack,
12947 TREE_VEC_ELT (parm_pack_args, j),
12948 false /* Don't emit DW_AT_name */,
12949 die);
12950 return die;
12953 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
12954 an enumerated type. */
12956 static inline int
12957 type_is_enum (const_tree type)
12959 return TREE_CODE (type) == ENUMERAL_TYPE;
12962 /* Return the DBX register number described by a given RTL node. */
12964 static unsigned int
12965 dbx_reg_number (const_rtx rtl)
12967 unsigned regno = REGNO (rtl);
12969 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
12971 #ifdef LEAF_REG_REMAP
12972 if (crtl->uses_only_leaf_regs)
12974 int leaf_reg = LEAF_REG_REMAP (regno);
12975 if (leaf_reg != -1)
12976 regno = (unsigned) leaf_reg;
12978 #endif
12980 regno = DBX_REGISTER_NUMBER (regno);
12981 gcc_assert (regno != INVALID_REGNUM);
12982 return regno;
12985 /* Optionally add a DW_OP_piece term to a location description expression.
12986 DW_OP_piece is only added if the location description expression already
12987 doesn't end with DW_OP_piece. */
12989 static void
12990 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
12992 dw_loc_descr_ref loc;
12994 if (*list_head != NULL)
12996 /* Find the end of the chain. */
12997 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
13000 if (loc->dw_loc_opc != DW_OP_piece)
13001 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
13005 /* Return a location descriptor that designates a machine register or
13006 zero if there is none. */
13008 static dw_loc_descr_ref
13009 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
13011 rtx regs;
13013 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
13014 return 0;
13016 /* We only use "frame base" when we're sure we're talking about the
13017 post-prologue local stack frame. We do this by *not* running
13018 register elimination until this point, and recognizing the special
13019 argument pointer and soft frame pointer rtx's.
13020 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
13021 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
13022 && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
13024 dw_loc_descr_ref result = NULL;
13026 if (dwarf_version >= 4 || !dwarf_strict)
13028 result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
13029 initialized);
13030 if (result)
13031 add_loc_descr (&result,
13032 new_loc_descr (DW_OP_stack_value, 0, 0));
13034 return result;
13037 regs = targetm.dwarf_register_span (rtl);
13039 if (REG_NREGS (rtl) > 1 || regs)
13040 return multiple_reg_loc_descriptor (rtl, regs, initialized);
13041 else
13043 unsigned int dbx_regnum = dbx_reg_number (rtl);
13044 if (dbx_regnum == IGNORED_DWARF_REGNUM)
13045 return 0;
13046 return one_reg_loc_descriptor (dbx_regnum, initialized);
13050 /* Return a location descriptor that designates a machine register for
13051 a given hard register number. */
13053 static dw_loc_descr_ref
13054 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
13056 dw_loc_descr_ref reg_loc_descr;
13058 if (regno <= 31)
13059 reg_loc_descr
13060 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
13061 else
13062 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
13064 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13065 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13067 return reg_loc_descr;
13070 /* Given an RTL of a register, return a location descriptor that
13071 designates a value that spans more than one register. */
13073 static dw_loc_descr_ref
13074 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
13075 enum var_init_status initialized)
13077 int size, i;
13078 dw_loc_descr_ref loc_result = NULL;
13080 /* Simple, contiguous registers. */
13081 if (regs == NULL_RTX)
13083 unsigned reg = REGNO (rtl);
13084 int nregs;
13086 #ifdef LEAF_REG_REMAP
13087 if (crtl->uses_only_leaf_regs)
13089 int leaf_reg = LEAF_REG_REMAP (reg);
13090 if (leaf_reg != -1)
13091 reg = (unsigned) leaf_reg;
13093 #endif
13095 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
13096 nregs = REG_NREGS (rtl);
13098 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
13100 loc_result = NULL;
13101 while (nregs--)
13103 dw_loc_descr_ref t;
13105 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
13106 VAR_INIT_STATUS_INITIALIZED);
13107 add_loc_descr (&loc_result, t);
13108 add_loc_descr_op_piece (&loc_result, size);
13109 ++reg;
13111 return loc_result;
13114 /* Now onto stupid register sets in non contiguous locations. */
13116 gcc_assert (GET_CODE (regs) == PARALLEL);
13118 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
13119 loc_result = NULL;
13121 for (i = 0; i < XVECLEN (regs, 0); ++i)
13123 dw_loc_descr_ref t;
13125 t = one_reg_loc_descriptor (dbx_reg_number (XVECEXP (regs, 0, i)),
13126 VAR_INIT_STATUS_INITIALIZED);
13127 add_loc_descr (&loc_result, t);
13128 add_loc_descr_op_piece (&loc_result, size);
13131 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13132 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13133 return loc_result;
13136 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
13138 /* Return a location descriptor that designates a constant i,
13139 as a compound operation from constant (i >> shift), constant shift
13140 and DW_OP_shl. */
13142 static dw_loc_descr_ref
13143 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
13145 dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
13146 add_loc_descr (&ret, int_loc_descriptor (shift));
13147 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
13148 return ret;
13151 /* Return a location descriptor that designates a constant. */
13153 static dw_loc_descr_ref
13154 int_loc_descriptor (HOST_WIDE_INT i)
13156 enum dwarf_location_atom op;
13158 /* Pick the smallest representation of a constant, rather than just
13159 defaulting to the LEB encoding. */
13160 if (i >= 0)
13162 int clz = clz_hwi (i);
13163 int ctz = ctz_hwi (i);
13164 if (i <= 31)
13165 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
13166 else if (i <= 0xff)
13167 op = DW_OP_const1u;
13168 else if (i <= 0xffff)
13169 op = DW_OP_const2u;
13170 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
13171 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
13172 /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
13173 DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
13174 while DW_OP_const4u is 5 bytes. */
13175 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
13176 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13177 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
13178 /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
13179 while DW_OP_const4u is 5 bytes. */
13180 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
13182 else if (DWARF2_ADDR_SIZE == 4 && i > 0x7fffffff
13183 && size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i)
13184 <= 4)
13186 /* As i >= 2**31, the double cast above will yield a negative number.
13187 Since wrapping is defined in DWARF expressions we can output big
13188 positive integers as small negative ones, regardless of the size
13189 of host wide ints.
13191 Here, since the evaluator will handle 32-bit values and since i >=
13192 2**31, we know it's going to be interpreted as a negative literal:
13193 store it this way if we can do better than 5 bytes this way. */
13194 return int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i);
13196 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
13197 op = DW_OP_const4u;
13199 /* Past this point, i >= 0x100000000 and thus DW_OP_constu will take at
13200 least 6 bytes: see if we can do better before falling back to it. */
13201 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13202 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
13203 /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes. */
13204 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
13205 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
13206 && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
13207 >= HOST_BITS_PER_WIDE_INT)
13208 /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
13209 DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes. */
13210 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
13211 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
13212 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
13213 && size_of_uleb128 (i) > 6)
13214 /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes. */
13215 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
13216 else
13217 op = DW_OP_constu;
13219 else
13221 if (i >= -0x80)
13222 op = DW_OP_const1s;
13223 else if (i >= -0x8000)
13224 op = DW_OP_const2s;
13225 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
13227 if (size_of_int_loc_descriptor (i) < 5)
13229 dw_loc_descr_ref ret = int_loc_descriptor (-i);
13230 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
13231 return ret;
13233 op = DW_OP_const4s;
13235 else
13237 if (size_of_int_loc_descriptor (i)
13238 < (unsigned long) 1 + size_of_sleb128 (i))
13240 dw_loc_descr_ref ret = int_loc_descriptor (-i);
13241 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
13242 return ret;
13244 op = DW_OP_consts;
13248 return new_loc_descr (op, i, 0);
13251 /* Likewise, for unsigned constants. */
13253 static dw_loc_descr_ref
13254 uint_loc_descriptor (unsigned HOST_WIDE_INT i)
13256 const unsigned HOST_WIDE_INT max_int = INTTYPE_MAXIMUM (HOST_WIDE_INT);
13257 const unsigned HOST_WIDE_INT max_uint
13258 = INTTYPE_MAXIMUM (unsigned HOST_WIDE_INT);
13260 /* If possible, use the clever signed constants handling. */
13261 if (i <= max_int)
13262 return int_loc_descriptor ((HOST_WIDE_INT) i);
13264 /* Here, we are left with positive numbers that cannot be represented as
13265 HOST_WIDE_INT, i.e.:
13266 max (HOST_WIDE_INT) < i <= max (unsigned HOST_WIDE_INT)
13268 Using DW_OP_const4/8/./u operation to encode them consumes a lot of bytes
13269 whereas may be better to output a negative integer: thanks to integer
13270 wrapping, we know that:
13271 x = x - 2 ** DWARF2_ADDR_SIZE
13272 = x - 2 * (max (HOST_WIDE_INT) + 1)
13273 So numbers close to max (unsigned HOST_WIDE_INT) could be represented as
13274 small negative integers. Let's try that in cases it will clearly improve
13275 the encoding: there is no gain turning DW_OP_const4u into
13276 DW_OP_const4s. */
13277 if (DWARF2_ADDR_SIZE * 8 == HOST_BITS_PER_WIDE_INT
13278 && ((DWARF2_ADDR_SIZE == 4 && i > max_uint - 0x8000)
13279 || (DWARF2_ADDR_SIZE == 8 && i > max_uint - 0x80000000)))
13281 const unsigned HOST_WIDE_INT first_shift = i - max_int - 1;
13283 /* Now, -1 < first_shift <= max (HOST_WIDE_INT)
13284 i.e. 0 <= first_shift <= max (HOST_WIDE_INT). */
13285 const HOST_WIDE_INT second_shift
13286 = (HOST_WIDE_INT) first_shift - (HOST_WIDE_INT) max_int - 1;
13288 /* So we finally have:
13289 -max (HOST_WIDE_INT) - 1 <= second_shift <= -1.
13290 i.e. min (HOST_WIDE_INT) <= second_shift < 0. */
13291 return int_loc_descriptor (second_shift);
13294 /* Last chance: fallback to a simple constant operation. */
13295 return new_loc_descr
13296 ((HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
13297 ? DW_OP_const4u
13298 : DW_OP_const8u,
13299 i, 0);
13302 /* Generate and return a location description that computes the unsigned
13303 comparison of the two stack top entries (a OP b where b is the top-most
13304 entry and a is the second one). The KIND of comparison can be LT_EXPR,
13305 LE_EXPR, GT_EXPR or GE_EXPR. */
13307 static dw_loc_descr_ref
13308 uint_comparison_loc_list (enum tree_code kind)
13310 enum dwarf_location_atom op, flip_op;
13311 dw_loc_descr_ref ret, bra_node, jmp_node, tmp;
13313 switch (kind)
13315 case LT_EXPR:
13316 op = DW_OP_lt;
13317 break;
13318 case LE_EXPR:
13319 op = DW_OP_le;
13320 break;
13321 case GT_EXPR:
13322 op = DW_OP_gt;
13323 break;
13324 case GE_EXPR:
13325 op = DW_OP_ge;
13326 break;
13327 default:
13328 gcc_unreachable ();
13331 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
13332 jmp_node = new_loc_descr (DW_OP_skip, 0, 0);
13334 /* Until DWARFv4, operations all work on signed integers. It is nevertheless
13335 possible to perform unsigned comparisons: we just have to distinguish
13336 three cases:
13338 1. when a and b have the same sign (as signed integers); then we should
13339 return: a OP(signed) b;
13341 2. when a is a negative signed integer while b is a positive one, then a
13342 is a greater unsigned integer than b; likewise when a and b's roles
13343 are flipped.
13345 So first, compare the sign of the two operands. */
13346 ret = new_loc_descr (DW_OP_over, 0, 0);
13347 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
13348 add_loc_descr (&ret, new_loc_descr (DW_OP_xor, 0, 0));
13349 /* If they have different signs (i.e. they have different sign bits), then
13350 the stack top value has now the sign bit set and thus it's smaller than
13351 zero. */
13352 add_loc_descr (&ret, new_loc_descr (DW_OP_lit0, 0, 0));
13353 add_loc_descr (&ret, new_loc_descr (DW_OP_lt, 0, 0));
13354 add_loc_descr (&ret, bra_node);
13356 /* We are in case 1. At this point, we know both operands have the same
13357 sign, to it's safe to use the built-in signed comparison. */
13358 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
13359 add_loc_descr (&ret, jmp_node);
13361 /* We are in case 2. Here, we know both operands do not have the same sign,
13362 so we have to flip the signed comparison. */
13363 flip_op = (kind == LT_EXPR || kind == LE_EXPR) ? DW_OP_gt : DW_OP_lt;
13364 tmp = new_loc_descr (flip_op, 0, 0);
13365 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13366 bra_node->dw_loc_oprnd1.v.val_loc = tmp;
13367 add_loc_descr (&ret, tmp);
13369 /* This dummy operation is necessary to make the two branches join. */
13370 tmp = new_loc_descr (DW_OP_nop, 0, 0);
13371 jmp_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13372 jmp_node->dw_loc_oprnd1.v.val_loc = tmp;
13373 add_loc_descr (&ret, tmp);
13375 return ret;
13378 /* Likewise, but takes the location description lists (might be destructive on
13379 them). Return NULL if either is NULL or if concatenation fails. */
13381 static dw_loc_list_ref
13382 loc_list_from_uint_comparison (dw_loc_list_ref left, dw_loc_list_ref right,
13383 enum tree_code kind)
13385 if (left == NULL || right == NULL)
13386 return NULL;
13388 add_loc_list (&left, right);
13389 if (left == NULL)
13390 return NULL;
13392 add_loc_descr_to_each (left, uint_comparison_loc_list (kind));
13393 return left;
13396 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
13397 without actually allocating it. */
13399 static unsigned long
13400 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
13402 return size_of_int_loc_descriptor (i >> shift)
13403 + size_of_int_loc_descriptor (shift)
13404 + 1;
13407 /* Return size_of_locs (int_loc_descriptor (i)) without
13408 actually allocating it. */
13410 static unsigned long
13411 size_of_int_loc_descriptor (HOST_WIDE_INT i)
13413 unsigned long s;
13415 if (i >= 0)
13417 int clz, ctz;
13418 if (i <= 31)
13419 return 1;
13420 else if (i <= 0xff)
13421 return 2;
13422 else if (i <= 0xffff)
13423 return 3;
13424 clz = clz_hwi (i);
13425 ctz = ctz_hwi (i);
13426 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
13427 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
13428 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13429 - clz - 5);
13430 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13431 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
13432 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13433 - clz - 8);
13434 else if (DWARF2_ADDR_SIZE == 4 && i > 0x7fffffff
13435 && size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i)
13436 <= 4)
13437 return size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i);
13438 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
13439 return 5;
13440 s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
13441 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13442 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
13443 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13444 - clz - 8);
13445 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
13446 && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
13447 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13448 - clz - 16);
13449 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
13450 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
13451 && s > 6)
13452 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13453 - clz - 32);
13454 else
13455 return 1 + s;
13457 else
13459 if (i >= -0x80)
13460 return 2;
13461 else if (i >= -0x8000)
13462 return 3;
13463 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
13465 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
13467 s = size_of_int_loc_descriptor (-i) + 1;
13468 if (s < 5)
13469 return s;
13471 return 5;
13473 else
13475 unsigned long r = 1 + size_of_sleb128 (i);
13476 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
13478 s = size_of_int_loc_descriptor (-i) + 1;
13479 if (s < r)
13480 return s;
13482 return r;
13487 /* Return loc description representing "address" of integer value.
13488 This can appear only as toplevel expression. */
13490 static dw_loc_descr_ref
13491 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
13493 int litsize;
13494 dw_loc_descr_ref loc_result = NULL;
13496 if (!(dwarf_version >= 4 || !dwarf_strict))
13497 return NULL;
13499 litsize = size_of_int_loc_descriptor (i);
13500 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
13501 is more compact. For DW_OP_stack_value we need:
13502 litsize + 1 (DW_OP_stack_value)
13503 and for DW_OP_implicit_value:
13504 1 (DW_OP_implicit_value) + 1 (length) + size. */
13505 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
13507 loc_result = int_loc_descriptor (i);
13508 add_loc_descr (&loc_result,
13509 new_loc_descr (DW_OP_stack_value, 0, 0));
13510 return loc_result;
13513 loc_result = new_loc_descr (DW_OP_implicit_value,
13514 size, 0);
13515 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
13516 loc_result->dw_loc_oprnd2.v.val_int = i;
13517 return loc_result;
13520 /* Return a location descriptor that designates a base+offset location. */
13522 static dw_loc_descr_ref
13523 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
13524 enum var_init_status initialized)
13526 unsigned int regno;
13527 dw_loc_descr_ref result;
13528 dw_fde_ref fde = cfun->fde;
13530 /* We only use "frame base" when we're sure we're talking about the
13531 post-prologue local stack frame. We do this by *not* running
13532 register elimination until this point, and recognizing the special
13533 argument pointer and soft frame pointer rtx's. */
13534 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
13536 rtx elim = (ira_use_lra_p
13537 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
13538 : eliminate_regs (reg, VOIDmode, NULL_RTX));
13540 if (elim != reg)
13542 if (GET_CODE (elim) == PLUS)
13544 offset += INTVAL (XEXP (elim, 1));
13545 elim = XEXP (elim, 0);
13547 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
13548 && (elim == hard_frame_pointer_rtx
13549 || elim == stack_pointer_rtx))
13550 || elim == (frame_pointer_needed
13551 ? hard_frame_pointer_rtx
13552 : stack_pointer_rtx));
13554 /* If drap register is used to align stack, use frame
13555 pointer + offset to access stack variables. If stack
13556 is aligned without drap, use stack pointer + offset to
13557 access stack variables. */
13558 if (crtl->stack_realign_tried
13559 && reg == frame_pointer_rtx)
13561 int base_reg
13562 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
13563 ? HARD_FRAME_POINTER_REGNUM
13564 : REGNO (elim));
13565 return new_reg_loc_descr (base_reg, offset);
13568 gcc_assert (frame_pointer_fb_offset_valid);
13569 offset += frame_pointer_fb_offset;
13570 return new_loc_descr (DW_OP_fbreg, offset, 0);
13574 regno = REGNO (reg);
13575 #ifdef LEAF_REG_REMAP
13576 if (crtl->uses_only_leaf_regs)
13578 int leaf_reg = LEAF_REG_REMAP (regno);
13579 if (leaf_reg != -1)
13580 regno = (unsigned) leaf_reg;
13582 #endif
13583 regno = DWARF_FRAME_REGNUM (regno);
13585 if (!optimize && fde
13586 && (fde->drap_reg == regno || fde->vdrap_reg == regno))
13588 /* Use cfa+offset to represent the location of arguments passed
13589 on the stack when drap is used to align stack.
13590 Only do this when not optimizing, for optimized code var-tracking
13591 is supposed to track where the arguments live and the register
13592 used as vdrap or drap in some spot might be used for something
13593 else in other part of the routine. */
13594 return new_loc_descr (DW_OP_fbreg, offset, 0);
13597 if (regno <= 31)
13598 result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
13599 offset, 0);
13600 else
13601 result = new_loc_descr (DW_OP_bregx, regno, offset);
13603 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13604 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13606 return result;
13609 /* Return true if this RTL expression describes a base+offset calculation. */
13611 static inline int
13612 is_based_loc (const_rtx rtl)
13614 return (GET_CODE (rtl) == PLUS
13615 && ((REG_P (XEXP (rtl, 0))
13616 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
13617 && CONST_INT_P (XEXP (rtl, 1)))));
13620 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
13621 failed. */
13623 static dw_loc_descr_ref
13624 tls_mem_loc_descriptor (rtx mem)
13626 tree base;
13627 dw_loc_descr_ref loc_result;
13629 if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
13630 return NULL;
13632 base = get_base_address (MEM_EXPR (mem));
13633 if (base == NULL
13634 || !VAR_P (base)
13635 || !DECL_THREAD_LOCAL_P (base))
13636 return NULL;
13638 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1, NULL);
13639 if (loc_result == NULL)
13640 return NULL;
13642 if (MEM_OFFSET (mem))
13643 loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
13645 return loc_result;
13648 /* Output debug info about reason why we failed to expand expression as dwarf
13649 expression. */
13651 static void
13652 expansion_failed (tree expr, rtx rtl, char const *reason)
13654 if (dump_file && (dump_flags & TDF_DETAILS))
13656 fprintf (dump_file, "Failed to expand as dwarf: ");
13657 if (expr)
13658 print_generic_expr (dump_file, expr, dump_flags);
13659 if (rtl)
13661 fprintf (dump_file, "\n");
13662 print_rtl (dump_file, rtl);
13664 fprintf (dump_file, "\nReason: %s\n", reason);
13668 /* Helper function for const_ok_for_output. */
13670 static bool
13671 const_ok_for_output_1 (rtx rtl)
13673 if (GET_CODE (rtl) == UNSPEC)
13675 /* If delegitimize_address couldn't do anything with the UNSPEC, assume
13676 we can't express it in the debug info. */
13677 /* Don't complain about TLS UNSPECs, those are just too hard to
13678 delegitimize. Note this could be a non-decl SYMBOL_REF such as
13679 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
13680 rather than DECL_THREAD_LOCAL_P is not just an optimization. */
13681 if (flag_checking
13682 && (XVECLEN (rtl, 0) == 0
13683 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
13684 || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE))
13685 inform (current_function_decl
13686 ? DECL_SOURCE_LOCATION (current_function_decl)
13687 : UNKNOWN_LOCATION,
13688 #if NUM_UNSPEC_VALUES > 0
13689 "non-delegitimized UNSPEC %s (%d) found in variable location",
13690 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
13691 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
13692 XINT (rtl, 1));
13693 #else
13694 "non-delegitimized UNSPEC %d found in variable location",
13695 XINT (rtl, 1));
13696 #endif
13697 expansion_failed (NULL_TREE, rtl,
13698 "UNSPEC hasn't been delegitimized.\n");
13699 return false;
13702 if (targetm.const_not_ok_for_debug_p (rtl))
13704 expansion_failed (NULL_TREE, rtl,
13705 "Expression rejected for debug by the backend.\n");
13706 return false;
13709 /* FIXME: Refer to PR60655. It is possible for simplification
13710 of rtl expressions in var tracking to produce such expressions.
13711 We should really identify / validate expressions
13712 enclosed in CONST that can be handled by assemblers on various
13713 targets and only handle legitimate cases here. */
13714 if (GET_CODE (rtl) != SYMBOL_REF)
13716 if (GET_CODE (rtl) == NOT)
13717 return false;
13718 return true;
13721 if (CONSTANT_POOL_ADDRESS_P (rtl))
13723 bool marked;
13724 get_pool_constant_mark (rtl, &marked);
13725 /* If all references to this pool constant were optimized away,
13726 it was not output and thus we can't represent it. */
13727 if (!marked)
13729 expansion_failed (NULL_TREE, rtl,
13730 "Constant was removed from constant pool.\n");
13731 return false;
13735 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
13736 return false;
13738 /* Avoid references to external symbols in debug info, on several targets
13739 the linker might even refuse to link when linking a shared library,
13740 and in many other cases the relocations for .debug_info/.debug_loc are
13741 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
13742 to be defined within the same shared library or executable are fine. */
13743 if (SYMBOL_REF_EXTERNAL_P (rtl))
13745 tree decl = SYMBOL_REF_DECL (rtl);
13747 if (decl == NULL || !targetm.binds_local_p (decl))
13749 expansion_failed (NULL_TREE, rtl,
13750 "Symbol not defined in current TU.\n");
13751 return false;
13755 return true;
13758 /* Return true if constant RTL can be emitted in DW_OP_addr or
13759 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
13760 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
13762 static bool
13763 const_ok_for_output (rtx rtl)
13765 if (GET_CODE (rtl) == SYMBOL_REF)
13766 return const_ok_for_output_1 (rtl);
13768 if (GET_CODE (rtl) == CONST)
13770 subrtx_var_iterator::array_type array;
13771 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 0), ALL)
13772 if (!const_ok_for_output_1 (*iter))
13773 return false;
13774 return true;
13777 return true;
13780 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
13781 if possible, NULL otherwise. */
13783 static dw_die_ref
13784 base_type_for_mode (machine_mode mode, bool unsignedp)
13786 dw_die_ref type_die;
13787 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
13789 if (type == NULL)
13790 return NULL;
13791 switch (TREE_CODE (type))
13793 case INTEGER_TYPE:
13794 case REAL_TYPE:
13795 break;
13796 default:
13797 return NULL;
13799 type_die = lookup_type_die (type);
13800 if (!type_die)
13801 type_die = modified_type_die (type, TYPE_UNQUALIFIED, false,
13802 comp_unit_die ());
13803 if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
13804 return NULL;
13805 return type_die;
13808 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
13809 type matching MODE, or, if MODE is narrower than or as wide as
13810 DWARF2_ADDR_SIZE, untyped. Return NULL if the conversion is not
13811 possible. */
13813 static dw_loc_descr_ref
13814 convert_descriptor_to_mode (machine_mode mode, dw_loc_descr_ref op)
13816 machine_mode outer_mode = mode;
13817 dw_die_ref type_die;
13818 dw_loc_descr_ref cvt;
13820 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
13822 add_loc_descr (&op, new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0));
13823 return op;
13825 type_die = base_type_for_mode (outer_mode, 1);
13826 if (type_die == NULL)
13827 return NULL;
13828 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
13829 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13830 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13831 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13832 add_loc_descr (&op, cvt);
13833 return op;
13836 /* Return location descriptor for comparison OP with operands OP0 and OP1. */
13838 static dw_loc_descr_ref
13839 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
13840 dw_loc_descr_ref op1)
13842 dw_loc_descr_ref ret = op0;
13843 add_loc_descr (&ret, op1);
13844 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
13845 if (STORE_FLAG_VALUE != 1)
13847 add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
13848 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
13850 return ret;
13853 /* Return location descriptor for signed comparison OP RTL. */
13855 static dw_loc_descr_ref
13856 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
13857 machine_mode mem_mode)
13859 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
13860 dw_loc_descr_ref op0, op1;
13861 int shift;
13863 if (op_mode == VOIDmode)
13864 op_mode = GET_MODE (XEXP (rtl, 1));
13865 if (op_mode == VOIDmode)
13866 return NULL;
13868 if (dwarf_strict
13869 && dwarf_version < 5
13870 && (!SCALAR_INT_MODE_P (op_mode)
13871 || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
13872 return NULL;
13874 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
13875 VAR_INIT_STATUS_INITIALIZED);
13876 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
13877 VAR_INIT_STATUS_INITIALIZED);
13879 if (op0 == NULL || op1 == NULL)
13880 return NULL;
13882 if (!SCALAR_INT_MODE_P (op_mode)
13883 || GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
13884 return compare_loc_descriptor (op, op0, op1);
13886 if (GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
13888 dw_die_ref type_die = base_type_for_mode (op_mode, 0);
13889 dw_loc_descr_ref cvt;
13891 if (type_die == NULL)
13892 return NULL;
13893 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
13894 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13895 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13896 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13897 add_loc_descr (&op0, cvt);
13898 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
13899 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13900 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13901 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13902 add_loc_descr (&op1, cvt);
13903 return compare_loc_descriptor (op, op0, op1);
13906 shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
13907 /* For eq/ne, if the operands are known to be zero-extended,
13908 there is no need to do the fancy shifting up. */
13909 if (op == DW_OP_eq || op == DW_OP_ne)
13911 dw_loc_descr_ref last0, last1;
13912 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
13914 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
13916 /* deref_size zero extends, and for constants we can check
13917 whether they are zero extended or not. */
13918 if (((last0->dw_loc_opc == DW_OP_deref_size
13919 && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
13920 || (CONST_INT_P (XEXP (rtl, 0))
13921 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
13922 == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
13923 && ((last1->dw_loc_opc == DW_OP_deref_size
13924 && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
13925 || (CONST_INT_P (XEXP (rtl, 1))
13926 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
13927 == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
13928 return compare_loc_descriptor (op, op0, op1);
13930 /* EQ/NE comparison against constant in narrower type than
13931 DWARF2_ADDR_SIZE can be performed either as
13932 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
13933 DW_OP_{eq,ne}
13935 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
13936 DW_OP_{eq,ne}. Pick whatever is shorter. */
13937 if (CONST_INT_P (XEXP (rtl, 1))
13938 && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
13939 && (size_of_int_loc_descriptor (shift) + 1
13940 + size_of_int_loc_descriptor (UINTVAL (XEXP (rtl, 1)) << shift)
13941 >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
13942 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
13943 & GET_MODE_MASK (op_mode))))
13945 add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
13946 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
13947 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
13948 & GET_MODE_MASK (op_mode));
13949 return compare_loc_descriptor (op, op0, op1);
13952 add_loc_descr (&op0, int_loc_descriptor (shift));
13953 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
13954 if (CONST_INT_P (XEXP (rtl, 1)))
13955 op1 = int_loc_descriptor (UINTVAL (XEXP (rtl, 1)) << shift);
13956 else
13958 add_loc_descr (&op1, int_loc_descriptor (shift));
13959 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
13961 return compare_loc_descriptor (op, op0, op1);
13964 /* Return location descriptor for unsigned comparison OP RTL. */
13966 static dw_loc_descr_ref
13967 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
13968 machine_mode mem_mode)
13970 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
13971 dw_loc_descr_ref op0, op1;
13973 if (op_mode == VOIDmode)
13974 op_mode = GET_MODE (XEXP (rtl, 1));
13975 if (op_mode == VOIDmode)
13976 return NULL;
13977 if (!SCALAR_INT_MODE_P (op_mode))
13978 return NULL;
13980 if (dwarf_strict
13981 && dwarf_version < 5
13982 && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
13983 return NULL;
13985 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
13986 VAR_INIT_STATUS_INITIALIZED);
13987 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
13988 VAR_INIT_STATUS_INITIALIZED);
13990 if (op0 == NULL || op1 == NULL)
13991 return NULL;
13993 if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
13995 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
13996 dw_loc_descr_ref last0, last1;
13997 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
13999 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
14001 if (CONST_INT_P (XEXP (rtl, 0)))
14002 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
14003 /* deref_size zero extends, so no need to mask it again. */
14004 else if (last0->dw_loc_opc != DW_OP_deref_size
14005 || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
14007 add_loc_descr (&op0, int_loc_descriptor (mask));
14008 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14010 if (CONST_INT_P (XEXP (rtl, 1)))
14011 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
14012 /* deref_size zero extends, so no need to mask it again. */
14013 else if (last1->dw_loc_opc != DW_OP_deref_size
14014 || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
14016 add_loc_descr (&op1, int_loc_descriptor (mask));
14017 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
14020 else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
14022 HOST_WIDE_INT bias = 1;
14023 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
14024 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14025 if (CONST_INT_P (XEXP (rtl, 1)))
14026 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
14027 + INTVAL (XEXP (rtl, 1)));
14028 else
14029 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
14030 bias, 0));
14032 return compare_loc_descriptor (op, op0, op1);
14035 /* Return location descriptor for {U,S}{MIN,MAX}. */
14037 static dw_loc_descr_ref
14038 minmax_loc_descriptor (rtx rtl, machine_mode mode,
14039 machine_mode mem_mode)
14041 enum dwarf_location_atom op;
14042 dw_loc_descr_ref op0, op1, ret;
14043 dw_loc_descr_ref bra_node, drop_node;
14045 if (dwarf_strict
14046 && dwarf_version < 5
14047 && (!SCALAR_INT_MODE_P (mode)
14048 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
14049 return NULL;
14051 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14052 VAR_INIT_STATUS_INITIALIZED);
14053 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14054 VAR_INIT_STATUS_INITIALIZED);
14056 if (op0 == NULL || op1 == NULL)
14057 return NULL;
14059 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
14060 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
14061 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
14062 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
14064 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
14066 HOST_WIDE_INT mask = GET_MODE_MASK (mode);
14067 add_loc_descr (&op0, int_loc_descriptor (mask));
14068 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14069 add_loc_descr (&op1, int_loc_descriptor (mask));
14070 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
14072 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
14074 HOST_WIDE_INT bias = 1;
14075 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
14076 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14077 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14080 else if (!SCALAR_INT_MODE_P (mode)
14081 && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
14083 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode)) * BITS_PER_UNIT;
14084 add_loc_descr (&op0, int_loc_descriptor (shift));
14085 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
14086 add_loc_descr (&op1, int_loc_descriptor (shift));
14087 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
14089 else if (SCALAR_INT_MODE_P (mode)
14090 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
14092 dw_die_ref type_die = base_type_for_mode (mode, 0);
14093 dw_loc_descr_ref cvt;
14094 if (type_die == NULL)
14095 return NULL;
14096 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14097 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14098 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14099 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14100 add_loc_descr (&op0, cvt);
14101 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14102 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14103 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14104 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14105 add_loc_descr (&op1, cvt);
14108 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
14109 op = DW_OP_lt;
14110 else
14111 op = DW_OP_gt;
14112 ret = op0;
14113 add_loc_descr (&ret, op1);
14114 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
14115 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14116 add_loc_descr (&ret, bra_node);
14117 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14118 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
14119 add_loc_descr (&ret, drop_node);
14120 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14121 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
14122 if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
14123 && SCALAR_INT_MODE_P (mode)
14124 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
14125 ret = convert_descriptor_to_mode (mode, ret);
14126 return ret;
14129 /* Helper function for mem_loc_descriptor. Perform OP binary op,
14130 but after converting arguments to type_die, afterwards
14131 convert back to unsigned. */
14133 static dw_loc_descr_ref
14134 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
14135 machine_mode mode, machine_mode mem_mode)
14137 dw_loc_descr_ref cvt, op0, op1;
14139 if (type_die == NULL)
14140 return NULL;
14141 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14142 VAR_INIT_STATUS_INITIALIZED);
14143 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14144 VAR_INIT_STATUS_INITIALIZED);
14145 if (op0 == NULL || op1 == NULL)
14146 return NULL;
14147 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14148 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14149 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14150 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14151 add_loc_descr (&op0, cvt);
14152 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14153 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14154 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14155 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14156 add_loc_descr (&op1, cvt);
14157 add_loc_descr (&op0, op1);
14158 add_loc_descr (&op0, new_loc_descr (op, 0, 0));
14159 return convert_descriptor_to_mode (mode, op0);
14162 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
14163 const0 is DW_OP_lit0 or corresponding typed constant,
14164 const1 is DW_OP_lit1 or corresponding typed constant
14165 and constMSB is constant with just the MSB bit set
14166 for the mode):
14167 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
14168 L1: const0 DW_OP_swap
14169 L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
14170 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
14171 L3: DW_OP_drop
14172 L4: DW_OP_nop
14174 CTZ is similar:
14175 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
14176 L1: const0 DW_OP_swap
14177 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
14178 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
14179 L3: DW_OP_drop
14180 L4: DW_OP_nop
14182 FFS is similar:
14183 DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
14184 L1: const1 DW_OP_swap
14185 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
14186 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
14187 L3: DW_OP_drop
14188 L4: DW_OP_nop */
14190 static dw_loc_descr_ref
14191 clz_loc_descriptor (rtx rtl, machine_mode mode,
14192 machine_mode mem_mode)
14194 dw_loc_descr_ref op0, ret, tmp;
14195 HOST_WIDE_INT valv;
14196 dw_loc_descr_ref l1jump, l1label;
14197 dw_loc_descr_ref l2jump, l2label;
14198 dw_loc_descr_ref l3jump, l3label;
14199 dw_loc_descr_ref l4jump, l4label;
14200 rtx msb;
14202 if (!SCALAR_INT_MODE_P (mode)
14203 || GET_MODE (XEXP (rtl, 0)) != mode)
14204 return NULL;
14206 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14207 VAR_INIT_STATUS_INITIALIZED);
14208 if (op0 == NULL)
14209 return NULL;
14210 ret = op0;
14211 if (GET_CODE (rtl) == CLZ)
14213 if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
14214 valv = GET_MODE_BITSIZE (mode);
14216 else if (GET_CODE (rtl) == FFS)
14217 valv = 0;
14218 else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
14219 valv = GET_MODE_BITSIZE (mode);
14220 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
14221 l1jump = new_loc_descr (DW_OP_bra, 0, 0);
14222 add_loc_descr (&ret, l1jump);
14223 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
14224 tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
14225 VAR_INIT_STATUS_INITIALIZED);
14226 if (tmp == NULL)
14227 return NULL;
14228 add_loc_descr (&ret, tmp);
14229 l4jump = new_loc_descr (DW_OP_skip, 0, 0);
14230 add_loc_descr (&ret, l4jump);
14231 l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
14232 ? const1_rtx : const0_rtx,
14233 mode, mem_mode,
14234 VAR_INIT_STATUS_INITIALIZED);
14235 if (l1label == NULL)
14236 return NULL;
14237 add_loc_descr (&ret, l1label);
14238 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14239 l2label = new_loc_descr (DW_OP_dup, 0, 0);
14240 add_loc_descr (&ret, l2label);
14241 if (GET_CODE (rtl) != CLZ)
14242 msb = const1_rtx;
14243 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
14244 msb = GEN_INT (HOST_WIDE_INT_1U
14245 << (GET_MODE_BITSIZE (mode) - 1));
14246 else
14247 msb = immed_wide_int_const
14248 (wi::set_bit_in_zero (GET_MODE_PRECISION (mode) - 1,
14249 GET_MODE_PRECISION (mode)), mode);
14250 if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
14251 tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
14252 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
14253 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
14254 else
14255 tmp = mem_loc_descriptor (msb, mode, mem_mode,
14256 VAR_INIT_STATUS_INITIALIZED);
14257 if (tmp == NULL)
14258 return NULL;
14259 add_loc_descr (&ret, tmp);
14260 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
14261 l3jump = new_loc_descr (DW_OP_bra, 0, 0);
14262 add_loc_descr (&ret, l3jump);
14263 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
14264 VAR_INIT_STATUS_INITIALIZED);
14265 if (tmp == NULL)
14266 return NULL;
14267 add_loc_descr (&ret, tmp);
14268 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
14269 ? DW_OP_shl : DW_OP_shr, 0, 0));
14270 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14271 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
14272 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14273 l2jump = new_loc_descr (DW_OP_skip, 0, 0);
14274 add_loc_descr (&ret, l2jump);
14275 l3label = new_loc_descr (DW_OP_drop, 0, 0);
14276 add_loc_descr (&ret, l3label);
14277 l4label = new_loc_descr (DW_OP_nop, 0, 0);
14278 add_loc_descr (&ret, l4label);
14279 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14280 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
14281 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14282 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
14283 l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14284 l3jump->dw_loc_oprnd1.v.val_loc = l3label;
14285 l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14286 l4jump->dw_loc_oprnd1.v.val_loc = l4label;
14287 return ret;
14290 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
14291 const1 is DW_OP_lit1 or corresponding typed constant):
14292 const0 DW_OP_swap
14293 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
14294 DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
14295 L2: DW_OP_drop
14297 PARITY is similar:
14298 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
14299 DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
14300 L2: DW_OP_drop */
14302 static dw_loc_descr_ref
14303 popcount_loc_descriptor (rtx rtl, machine_mode mode,
14304 machine_mode mem_mode)
14306 dw_loc_descr_ref op0, ret, tmp;
14307 dw_loc_descr_ref l1jump, l1label;
14308 dw_loc_descr_ref l2jump, l2label;
14310 if (!SCALAR_INT_MODE_P (mode)
14311 || GET_MODE (XEXP (rtl, 0)) != mode)
14312 return NULL;
14314 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14315 VAR_INIT_STATUS_INITIALIZED);
14316 if (op0 == NULL)
14317 return NULL;
14318 ret = op0;
14319 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
14320 VAR_INIT_STATUS_INITIALIZED);
14321 if (tmp == NULL)
14322 return NULL;
14323 add_loc_descr (&ret, tmp);
14324 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14325 l1label = new_loc_descr (DW_OP_dup, 0, 0);
14326 add_loc_descr (&ret, l1label);
14327 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
14328 add_loc_descr (&ret, l2jump);
14329 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
14330 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
14331 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
14332 VAR_INIT_STATUS_INITIALIZED);
14333 if (tmp == NULL)
14334 return NULL;
14335 add_loc_descr (&ret, tmp);
14336 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
14337 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
14338 ? DW_OP_plus : DW_OP_xor, 0, 0));
14339 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14340 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
14341 VAR_INIT_STATUS_INITIALIZED);
14342 add_loc_descr (&ret, tmp);
14343 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
14344 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
14345 add_loc_descr (&ret, l1jump);
14346 l2label = new_loc_descr (DW_OP_drop, 0, 0);
14347 add_loc_descr (&ret, l2label);
14348 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14349 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
14350 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14351 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
14352 return ret;
14355 /* BSWAP (constS is initial shift count, either 56 or 24):
14356 constS const0
14357 L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
14358 const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
14359 DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
14360 DW_OP_minus DW_OP_swap DW_OP_skip <L1>
14361 L2: DW_OP_drop DW_OP_swap DW_OP_drop */
14363 static dw_loc_descr_ref
14364 bswap_loc_descriptor (rtx rtl, machine_mode mode,
14365 machine_mode mem_mode)
14367 dw_loc_descr_ref op0, ret, tmp;
14368 dw_loc_descr_ref l1jump, l1label;
14369 dw_loc_descr_ref l2jump, l2label;
14371 if (!SCALAR_INT_MODE_P (mode)
14372 || BITS_PER_UNIT != 8
14373 || (GET_MODE_BITSIZE (mode) != 32
14374 && GET_MODE_BITSIZE (mode) != 64))
14375 return NULL;
14377 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14378 VAR_INIT_STATUS_INITIALIZED);
14379 if (op0 == NULL)
14380 return NULL;
14382 ret = op0;
14383 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
14384 mode, mem_mode,
14385 VAR_INIT_STATUS_INITIALIZED);
14386 if (tmp == NULL)
14387 return NULL;
14388 add_loc_descr (&ret, tmp);
14389 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
14390 VAR_INIT_STATUS_INITIALIZED);
14391 if (tmp == NULL)
14392 return NULL;
14393 add_loc_descr (&ret, tmp);
14394 l1label = new_loc_descr (DW_OP_pick, 2, 0);
14395 add_loc_descr (&ret, l1label);
14396 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
14397 mode, mem_mode,
14398 VAR_INIT_STATUS_INITIALIZED);
14399 add_loc_descr (&ret, tmp);
14400 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
14401 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
14402 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
14403 tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
14404 VAR_INIT_STATUS_INITIALIZED);
14405 if (tmp == NULL)
14406 return NULL;
14407 add_loc_descr (&ret, tmp);
14408 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
14409 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
14410 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
14411 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
14412 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14413 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
14414 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
14415 VAR_INIT_STATUS_INITIALIZED);
14416 add_loc_descr (&ret, tmp);
14417 add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
14418 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
14419 add_loc_descr (&ret, l2jump);
14420 tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
14421 VAR_INIT_STATUS_INITIALIZED);
14422 add_loc_descr (&ret, tmp);
14423 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
14424 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14425 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
14426 add_loc_descr (&ret, l1jump);
14427 l2label = new_loc_descr (DW_OP_drop, 0, 0);
14428 add_loc_descr (&ret, l2label);
14429 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14430 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
14431 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14432 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
14433 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14434 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
14435 return ret;
14438 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
14439 DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
14440 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
14441 DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
14443 ROTATERT is similar:
14444 DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
14445 DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
14446 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or */
14448 static dw_loc_descr_ref
14449 rotate_loc_descriptor (rtx rtl, machine_mode mode,
14450 machine_mode mem_mode)
14452 rtx rtlop1 = XEXP (rtl, 1);
14453 dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
14454 int i;
14456 if (!SCALAR_INT_MODE_P (mode))
14457 return NULL;
14459 if (GET_MODE (rtlop1) != VOIDmode
14460 && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
14461 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
14462 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14463 VAR_INIT_STATUS_INITIALIZED);
14464 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
14465 VAR_INIT_STATUS_INITIALIZED);
14466 if (op0 == NULL || op1 == NULL)
14467 return NULL;
14468 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
14469 for (i = 0; i < 2; i++)
14471 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
14472 mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
14473 mode, mem_mode,
14474 VAR_INIT_STATUS_INITIALIZED);
14475 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
14476 mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
14477 ? DW_OP_const4u
14478 : HOST_BITS_PER_WIDE_INT == 64
14479 ? DW_OP_const8u : DW_OP_constu,
14480 GET_MODE_MASK (mode), 0);
14481 else
14482 mask[i] = NULL;
14483 if (mask[i] == NULL)
14484 return NULL;
14485 add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
14487 ret = op0;
14488 add_loc_descr (&ret, op1);
14489 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
14490 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
14491 if (GET_CODE (rtl) == ROTATERT)
14493 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
14494 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
14495 GET_MODE_BITSIZE (mode), 0));
14497 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
14498 if (mask[0] != NULL)
14499 add_loc_descr (&ret, mask[0]);
14500 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
14501 if (mask[1] != NULL)
14503 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14504 add_loc_descr (&ret, mask[1]);
14505 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14507 if (GET_CODE (rtl) == ROTATE)
14509 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
14510 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
14511 GET_MODE_BITSIZE (mode), 0));
14513 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
14514 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
14515 return ret;
14518 /* Helper function for mem_loc_descriptor. Return DW_OP_GNU_parameter_ref
14519 for DEBUG_PARAMETER_REF RTL. */
14521 static dw_loc_descr_ref
14522 parameter_ref_descriptor (rtx rtl)
14524 dw_loc_descr_ref ret;
14525 dw_die_ref ref;
14527 if (dwarf_strict)
14528 return NULL;
14529 gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
14530 ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
14531 ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
14532 if (ref)
14534 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14535 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
14536 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
14538 else
14540 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
14541 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
14543 return ret;
14546 /* The following routine converts the RTL for a variable or parameter
14547 (resident in memory) into an equivalent Dwarf representation of a
14548 mechanism for getting the address of that same variable onto the top of a
14549 hypothetical "address evaluation" stack.
14551 When creating memory location descriptors, we are effectively transforming
14552 the RTL for a memory-resident object into its Dwarf postfix expression
14553 equivalent. This routine recursively descends an RTL tree, turning
14554 it into Dwarf postfix code as it goes.
14556 MODE is the mode that should be assumed for the rtl if it is VOIDmode.
14558 MEM_MODE is the mode of the memory reference, needed to handle some
14559 autoincrement addressing modes.
14561 Return 0 if we can't represent the location. */
14563 dw_loc_descr_ref
14564 mem_loc_descriptor (rtx rtl, machine_mode mode,
14565 machine_mode mem_mode,
14566 enum var_init_status initialized)
14568 dw_loc_descr_ref mem_loc_result = NULL;
14569 enum dwarf_location_atom op;
14570 dw_loc_descr_ref op0, op1;
14571 rtx inner = NULL_RTX;
14573 if (mode == VOIDmode)
14574 mode = GET_MODE (rtl);
14576 /* Note that for a dynamically sized array, the location we will generate a
14577 description of here will be the lowest numbered location which is
14578 actually within the array. That's *not* necessarily the same as the
14579 zeroth element of the array. */
14581 rtl = targetm.delegitimize_address (rtl);
14583 if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
14584 return NULL;
14586 switch (GET_CODE (rtl))
14588 case POST_INC:
14589 case POST_DEC:
14590 case POST_MODIFY:
14591 return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
14593 case SUBREG:
14594 /* The case of a subreg may arise when we have a local (register)
14595 variable or a formal (register) parameter which doesn't quite fill
14596 up an entire register. For now, just assume that it is
14597 legitimate to make the Dwarf info refer to the whole register which
14598 contains the given subreg. */
14599 if (!subreg_lowpart_p (rtl))
14600 break;
14601 inner = SUBREG_REG (rtl);
14602 /* FALLTHRU */
14603 case TRUNCATE:
14604 if (inner == NULL_RTX)
14605 inner = XEXP (rtl, 0);
14606 if (SCALAR_INT_MODE_P (mode)
14607 && SCALAR_INT_MODE_P (GET_MODE (inner))
14608 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
14609 #ifdef POINTERS_EXTEND_UNSIGNED
14610 || (mode == Pmode && mem_mode != VOIDmode)
14611 #endif
14613 && GET_MODE_SIZE (GET_MODE (inner)) <= DWARF2_ADDR_SIZE)
14615 mem_loc_result = mem_loc_descriptor (inner,
14616 GET_MODE (inner),
14617 mem_mode, initialized);
14618 break;
14620 if (dwarf_strict && dwarf_version < 5)
14621 break;
14622 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (inner)))
14623 break;
14624 if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (inner))
14625 && (!SCALAR_INT_MODE_P (mode)
14626 || !SCALAR_INT_MODE_P (GET_MODE (inner))))
14627 break;
14628 else
14630 dw_die_ref type_die;
14631 dw_loc_descr_ref cvt;
14633 mem_loc_result = mem_loc_descriptor (inner,
14634 GET_MODE (inner),
14635 mem_mode, initialized);
14636 if (mem_loc_result == NULL)
14637 break;
14638 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
14639 if (type_die == NULL)
14641 mem_loc_result = NULL;
14642 break;
14644 if (GET_MODE_SIZE (mode)
14645 != GET_MODE_SIZE (GET_MODE (inner)))
14646 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14647 else
14648 cvt = new_loc_descr (dwarf_OP (DW_OP_reinterpret), 0, 0);
14649 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14650 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14651 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14652 add_loc_descr (&mem_loc_result, cvt);
14653 if (SCALAR_INT_MODE_P (mode)
14654 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
14656 /* Convert it to untyped afterwards. */
14657 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14658 add_loc_descr (&mem_loc_result, cvt);
14661 break;
14663 case REG:
14664 if (! SCALAR_INT_MODE_P (mode)
14665 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
14666 && rtl != arg_pointer_rtx
14667 && rtl != frame_pointer_rtx
14668 #ifdef POINTERS_EXTEND_UNSIGNED
14669 && (mode != Pmode || mem_mode == VOIDmode)
14670 #endif
14673 dw_die_ref type_die;
14674 unsigned int dbx_regnum;
14676 if (dwarf_strict && dwarf_version < 5)
14677 break;
14678 if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
14679 break;
14680 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
14681 if (type_die == NULL)
14682 break;
14684 dbx_regnum = dbx_reg_number (rtl);
14685 if (dbx_regnum == IGNORED_DWARF_REGNUM)
14686 break;
14687 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_regval_type),
14688 dbx_regnum, 0);
14689 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
14690 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
14691 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
14692 break;
14694 /* Whenever a register number forms a part of the description of the
14695 method for calculating the (dynamic) address of a memory resident
14696 object, DWARF rules require the register number be referred to as
14697 a "base register". This distinction is not based in any way upon
14698 what category of register the hardware believes the given register
14699 belongs to. This is strictly DWARF terminology we're dealing with
14700 here. Note that in cases where the location of a memory-resident
14701 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
14702 OP_CONST (0)) the actual DWARF location descriptor that we generate
14703 may just be OP_BASEREG (basereg). This may look deceptively like
14704 the object in question was allocated to a register (rather than in
14705 memory) so DWARF consumers need to be aware of the subtle
14706 distinction between OP_REG and OP_BASEREG. */
14707 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
14708 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
14709 else if (stack_realign_drap
14710 && crtl->drap_reg
14711 && crtl->args.internal_arg_pointer == rtl
14712 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
14714 /* If RTL is internal_arg_pointer, which has been optimized
14715 out, use DRAP instead. */
14716 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
14717 VAR_INIT_STATUS_INITIALIZED);
14719 break;
14721 case SIGN_EXTEND:
14722 case ZERO_EXTEND:
14723 if (!SCALAR_INT_MODE_P (mode))
14724 break;
14725 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
14726 mem_mode, VAR_INIT_STATUS_INITIALIZED);
14727 if (op0 == 0)
14728 break;
14729 else if (GET_CODE (rtl) == ZERO_EXTEND
14730 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
14731 && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
14732 < HOST_BITS_PER_WIDE_INT
14733 /* If DW_OP_const{1,2,4}u won't be used, it is shorter
14734 to expand zero extend as two shifts instead of
14735 masking. */
14736 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= 4)
14738 machine_mode imode = GET_MODE (XEXP (rtl, 0));
14739 mem_loc_result = op0;
14740 add_loc_descr (&mem_loc_result,
14741 int_loc_descriptor (GET_MODE_MASK (imode)));
14742 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
14744 else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
14746 int shift = DWARF2_ADDR_SIZE
14747 - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
14748 shift *= BITS_PER_UNIT;
14749 if (GET_CODE (rtl) == SIGN_EXTEND)
14750 op = DW_OP_shra;
14751 else
14752 op = DW_OP_shr;
14753 mem_loc_result = op0;
14754 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
14755 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
14756 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
14757 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14759 else if (!dwarf_strict || dwarf_version >= 5)
14761 dw_die_ref type_die1, type_die2;
14762 dw_loc_descr_ref cvt;
14764 type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
14765 GET_CODE (rtl) == ZERO_EXTEND);
14766 if (type_die1 == NULL)
14767 break;
14768 type_die2 = base_type_for_mode (mode, 1);
14769 if (type_die2 == NULL)
14770 break;
14771 mem_loc_result = op0;
14772 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14773 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14774 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
14775 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14776 add_loc_descr (&mem_loc_result, cvt);
14777 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14778 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14779 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
14780 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14781 add_loc_descr (&mem_loc_result, cvt);
14783 break;
14785 case MEM:
14787 rtx new_rtl = avoid_constant_pool_reference (rtl);
14788 if (new_rtl != rtl)
14790 mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
14791 initialized);
14792 if (mem_loc_result != NULL)
14793 return mem_loc_result;
14796 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
14797 get_address_mode (rtl), mode,
14798 VAR_INIT_STATUS_INITIALIZED);
14799 if (mem_loc_result == NULL)
14800 mem_loc_result = tls_mem_loc_descriptor (rtl);
14801 if (mem_loc_result != NULL)
14803 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
14804 || !SCALAR_INT_MODE_P(mode))
14806 dw_die_ref type_die;
14807 dw_loc_descr_ref deref;
14809 if (dwarf_strict && dwarf_version < 5)
14810 return NULL;
14811 type_die
14812 = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
14813 if (type_die == NULL)
14814 return NULL;
14815 deref = new_loc_descr (dwarf_OP (DW_OP_deref_type),
14816 GET_MODE_SIZE (mode), 0);
14817 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
14818 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
14819 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
14820 add_loc_descr (&mem_loc_result, deref);
14822 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
14823 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
14824 else
14825 add_loc_descr (&mem_loc_result,
14826 new_loc_descr (DW_OP_deref_size,
14827 GET_MODE_SIZE (mode), 0));
14829 break;
14831 case LO_SUM:
14832 return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
14834 case LABEL_REF:
14835 /* Some ports can transform a symbol ref into a label ref, because
14836 the symbol ref is too far away and has to be dumped into a constant
14837 pool. */
14838 case CONST:
14839 case SYMBOL_REF:
14840 if (!SCALAR_INT_MODE_P (mode)
14841 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
14842 #ifdef POINTERS_EXTEND_UNSIGNED
14843 && (mode != Pmode || mem_mode == VOIDmode)
14844 #endif
14846 break;
14847 if (GET_CODE (rtl) == SYMBOL_REF
14848 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
14850 dw_loc_descr_ref temp;
14852 /* If this is not defined, we have no way to emit the data. */
14853 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
14854 break;
14856 temp = new_addr_loc_descr (rtl, dtprel_true);
14858 /* We check for DWARF 5 here because gdb did not implement
14859 DW_OP_form_tls_address until after 7.12. */
14860 mem_loc_result = new_loc_descr ((dwarf_version >= 5
14861 ? DW_OP_form_tls_address
14862 : DW_OP_GNU_push_tls_address),
14863 0, 0);
14864 add_loc_descr (&mem_loc_result, temp);
14866 break;
14869 if (!const_ok_for_output (rtl))
14871 if (GET_CODE (rtl) == CONST)
14872 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14873 initialized);
14874 break;
14877 symref:
14878 mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
14879 vec_safe_push (used_rtx_array, rtl);
14880 break;
14882 case CONCAT:
14883 case CONCATN:
14884 case VAR_LOCATION:
14885 case DEBUG_IMPLICIT_PTR:
14886 expansion_failed (NULL_TREE, rtl,
14887 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
14888 return 0;
14890 case ENTRY_VALUE:
14891 if (dwarf_strict && dwarf_version < 5)
14892 return NULL;
14893 if (REG_P (ENTRY_VALUE_EXP (rtl)))
14895 if (!SCALAR_INT_MODE_P (mode)
14896 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
14897 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
14898 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14899 else
14901 unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
14902 if (dbx_regnum == IGNORED_DWARF_REGNUM)
14903 return NULL;
14904 op0 = one_reg_loc_descriptor (dbx_regnum,
14905 VAR_INIT_STATUS_INITIALIZED);
14908 else if (MEM_P (ENTRY_VALUE_EXP (rtl))
14909 && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
14911 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
14912 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14913 if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
14914 return NULL;
14916 else
14917 gcc_unreachable ();
14918 if (op0 == NULL)
14919 return NULL;
14920 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_entry_value), 0, 0);
14921 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
14922 mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
14923 break;
14925 case DEBUG_PARAMETER_REF:
14926 mem_loc_result = parameter_ref_descriptor (rtl);
14927 break;
14929 case PRE_MODIFY:
14930 /* Extract the PLUS expression nested inside and fall into
14931 PLUS code below. */
14932 rtl = XEXP (rtl, 1);
14933 goto plus;
14935 case PRE_INC:
14936 case PRE_DEC:
14937 /* Turn these into a PLUS expression and fall into the PLUS code
14938 below. */
14939 rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
14940 gen_int_mode (GET_CODE (rtl) == PRE_INC
14941 ? GET_MODE_UNIT_SIZE (mem_mode)
14942 : -GET_MODE_UNIT_SIZE (mem_mode),
14943 mode));
14945 /* fall through */
14947 case PLUS:
14948 plus:
14949 if (is_based_loc (rtl)
14950 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
14951 || XEXP (rtl, 0) == arg_pointer_rtx
14952 || XEXP (rtl, 0) == frame_pointer_rtx)
14953 && SCALAR_INT_MODE_P (mode))
14954 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
14955 INTVAL (XEXP (rtl, 1)),
14956 VAR_INIT_STATUS_INITIALIZED);
14957 else
14959 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14960 VAR_INIT_STATUS_INITIALIZED);
14961 if (mem_loc_result == 0)
14962 break;
14964 if (CONST_INT_P (XEXP (rtl, 1))
14965 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
14966 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
14967 else
14969 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14970 VAR_INIT_STATUS_INITIALIZED);
14971 if (op1 == 0)
14972 return NULL;
14973 add_loc_descr (&mem_loc_result, op1);
14974 add_loc_descr (&mem_loc_result,
14975 new_loc_descr (DW_OP_plus, 0, 0));
14978 break;
14980 /* If a pseudo-reg is optimized away, it is possible for it to
14981 be replaced with a MEM containing a multiply or shift. */
14982 case MINUS:
14983 op = DW_OP_minus;
14984 goto do_binop;
14986 case MULT:
14987 op = DW_OP_mul;
14988 goto do_binop;
14990 case DIV:
14991 if ((!dwarf_strict || dwarf_version >= 5)
14992 && SCALAR_INT_MODE_P (mode)
14993 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
14995 mem_loc_result = typed_binop (DW_OP_div, rtl,
14996 base_type_for_mode (mode, 0),
14997 mode, mem_mode);
14998 break;
15000 op = DW_OP_div;
15001 goto do_binop;
15003 case UMOD:
15004 op = DW_OP_mod;
15005 goto do_binop;
15007 case ASHIFT:
15008 op = DW_OP_shl;
15009 goto do_shift;
15011 case ASHIFTRT:
15012 op = DW_OP_shra;
15013 goto do_shift;
15015 case LSHIFTRT:
15016 op = DW_OP_shr;
15017 goto do_shift;
15019 do_shift:
15020 if (!SCALAR_INT_MODE_P (mode))
15021 break;
15022 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15023 VAR_INIT_STATUS_INITIALIZED);
15025 rtx rtlop1 = XEXP (rtl, 1);
15026 if (GET_MODE (rtlop1) != VOIDmode
15027 && GET_MODE_BITSIZE (GET_MODE (rtlop1))
15028 < GET_MODE_BITSIZE (mode))
15029 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
15030 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
15031 VAR_INIT_STATUS_INITIALIZED);
15034 if (op0 == 0 || op1 == 0)
15035 break;
15037 mem_loc_result = op0;
15038 add_loc_descr (&mem_loc_result, op1);
15039 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15040 break;
15042 case AND:
15043 op = DW_OP_and;
15044 goto do_binop;
15046 case IOR:
15047 op = DW_OP_or;
15048 goto do_binop;
15050 case XOR:
15051 op = DW_OP_xor;
15052 goto do_binop;
15054 do_binop:
15055 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15056 VAR_INIT_STATUS_INITIALIZED);
15057 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15058 VAR_INIT_STATUS_INITIALIZED);
15060 if (op0 == 0 || op1 == 0)
15061 break;
15063 mem_loc_result = op0;
15064 add_loc_descr (&mem_loc_result, op1);
15065 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15066 break;
15068 case MOD:
15069 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
15070 && (!dwarf_strict || dwarf_version >= 5))
15072 mem_loc_result = typed_binop (DW_OP_mod, rtl,
15073 base_type_for_mode (mode, 0),
15074 mode, mem_mode);
15075 break;
15078 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15079 VAR_INIT_STATUS_INITIALIZED);
15080 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15081 VAR_INIT_STATUS_INITIALIZED);
15083 if (op0 == 0 || op1 == 0)
15084 break;
15086 mem_loc_result = op0;
15087 add_loc_descr (&mem_loc_result, op1);
15088 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
15089 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
15090 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
15091 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
15092 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
15093 break;
15095 case UDIV:
15096 if ((!dwarf_strict || dwarf_version >= 5)
15097 && SCALAR_INT_MODE_P (mode))
15099 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
15101 op = DW_OP_div;
15102 goto do_binop;
15104 mem_loc_result = typed_binop (DW_OP_div, rtl,
15105 base_type_for_mode (mode, 1),
15106 mode, mem_mode);
15108 break;
15110 case NOT:
15111 op = DW_OP_not;
15112 goto do_unop;
15114 case ABS:
15115 op = DW_OP_abs;
15116 goto do_unop;
15118 case NEG:
15119 op = DW_OP_neg;
15120 goto do_unop;
15122 do_unop:
15123 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15124 VAR_INIT_STATUS_INITIALIZED);
15126 if (op0 == 0)
15127 break;
15129 mem_loc_result = op0;
15130 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15131 break;
15133 case CONST_INT:
15134 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
15135 #ifdef POINTERS_EXTEND_UNSIGNED
15136 || (mode == Pmode
15137 && mem_mode != VOIDmode
15138 && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
15139 #endif
15142 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
15143 break;
15145 if ((!dwarf_strict || dwarf_version >= 5)
15146 && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
15147 || GET_MODE_BITSIZE (mode) == HOST_BITS_PER_DOUBLE_INT))
15149 dw_die_ref type_die = base_type_for_mode (mode, 1);
15150 machine_mode amode;
15151 if (type_die == NULL)
15152 return NULL;
15153 amode = mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT,
15154 MODE_INT, 0);
15155 if (INTVAL (rtl) >= 0
15156 && amode != BLKmode
15157 && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
15158 /* const DW_OP_convert <XXX> vs.
15159 DW_OP_const_type <XXX, 1, const>. */
15160 && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
15161 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (mode))
15163 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
15164 op0 = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15165 op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15166 op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15167 op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
15168 add_loc_descr (&mem_loc_result, op0);
15169 return mem_loc_result;
15171 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0,
15172 INTVAL (rtl));
15173 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15174 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15175 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
15176 if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
15177 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
15178 else
15180 mem_loc_result->dw_loc_oprnd2.val_class
15181 = dw_val_class_const_double;
15182 mem_loc_result->dw_loc_oprnd2.v.val_double
15183 = double_int::from_shwi (INTVAL (rtl));
15186 break;
15188 case CONST_DOUBLE:
15189 if (!dwarf_strict || dwarf_version >= 5)
15191 dw_die_ref type_die;
15193 /* Note that if TARGET_SUPPORTS_WIDE_INT == 0, a
15194 CONST_DOUBLE rtx could represent either a large integer
15195 or a floating-point constant. If TARGET_SUPPORTS_WIDE_INT != 0,
15196 the value is always a floating point constant.
15198 When it is an integer, a CONST_DOUBLE is used whenever
15199 the constant requires 2 HWIs to be adequately represented.
15200 We output CONST_DOUBLEs as blocks. */
15201 if (mode == VOIDmode
15202 || (GET_MODE (rtl) == VOIDmode
15203 && GET_MODE_BITSIZE (mode) != HOST_BITS_PER_DOUBLE_INT))
15204 break;
15205 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
15206 if (type_die == NULL)
15207 return NULL;
15208 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0, 0);
15209 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15210 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15211 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
15212 #if TARGET_SUPPORTS_WIDE_INT == 0
15213 if (!SCALAR_FLOAT_MODE_P (mode))
15215 mem_loc_result->dw_loc_oprnd2.val_class
15216 = dw_val_class_const_double;
15217 mem_loc_result->dw_loc_oprnd2.v.val_double
15218 = rtx_to_double_int (rtl);
15220 else
15221 #endif
15223 unsigned int length = GET_MODE_SIZE (mode);
15224 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
15226 insert_float (rtl, array);
15227 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
15228 mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
15229 mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
15230 mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
15233 break;
15235 case CONST_WIDE_INT:
15236 if (!dwarf_strict || dwarf_version >= 5)
15238 dw_die_ref type_die;
15240 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
15241 if (type_die == NULL)
15242 return NULL;
15243 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0, 0);
15244 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15245 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15246 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
15247 mem_loc_result->dw_loc_oprnd2.val_class
15248 = dw_val_class_wide_int;
15249 mem_loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
15250 *mem_loc_result->dw_loc_oprnd2.v.val_wide = rtx_mode_t (rtl, mode);
15252 break;
15254 case EQ:
15255 mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
15256 break;
15258 case GE:
15259 mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
15260 break;
15262 case GT:
15263 mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
15264 break;
15266 case LE:
15267 mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
15268 break;
15270 case LT:
15271 mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
15272 break;
15274 case NE:
15275 mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
15276 break;
15278 case GEU:
15279 mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
15280 break;
15282 case GTU:
15283 mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
15284 break;
15286 case LEU:
15287 mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
15288 break;
15290 case LTU:
15291 mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
15292 break;
15294 case UMIN:
15295 case UMAX:
15296 if (!SCALAR_INT_MODE_P (mode))
15297 break;
15298 /* FALLTHRU */
15299 case SMIN:
15300 case SMAX:
15301 mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
15302 break;
15304 case ZERO_EXTRACT:
15305 case SIGN_EXTRACT:
15306 if (CONST_INT_P (XEXP (rtl, 1))
15307 && CONST_INT_P (XEXP (rtl, 2))
15308 && ((unsigned) INTVAL (XEXP (rtl, 1))
15309 + (unsigned) INTVAL (XEXP (rtl, 2))
15310 <= GET_MODE_BITSIZE (mode))
15311 && SCALAR_INT_MODE_P (mode)
15312 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
15313 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
15315 int shift, size;
15316 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
15317 mem_mode, VAR_INIT_STATUS_INITIALIZED);
15318 if (op0 == 0)
15319 break;
15320 if (GET_CODE (rtl) == SIGN_EXTRACT)
15321 op = DW_OP_shra;
15322 else
15323 op = DW_OP_shr;
15324 mem_loc_result = op0;
15325 size = INTVAL (XEXP (rtl, 1));
15326 shift = INTVAL (XEXP (rtl, 2));
15327 if (BITS_BIG_ENDIAN)
15328 shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
15329 - shift - size;
15330 if (shift + size != (int) DWARF2_ADDR_SIZE)
15332 add_loc_descr (&mem_loc_result,
15333 int_loc_descriptor (DWARF2_ADDR_SIZE
15334 - shift - size));
15335 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
15337 if (size != (int) DWARF2_ADDR_SIZE)
15339 add_loc_descr (&mem_loc_result,
15340 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
15341 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15344 break;
15346 case IF_THEN_ELSE:
15348 dw_loc_descr_ref op2, bra_node, drop_node;
15349 op0 = mem_loc_descriptor (XEXP (rtl, 0),
15350 GET_MODE (XEXP (rtl, 0)) == VOIDmode
15351 ? word_mode : GET_MODE (XEXP (rtl, 0)),
15352 mem_mode, VAR_INIT_STATUS_INITIALIZED);
15353 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15354 VAR_INIT_STATUS_INITIALIZED);
15355 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
15356 VAR_INIT_STATUS_INITIALIZED);
15357 if (op0 == NULL || op1 == NULL || op2 == NULL)
15358 break;
15360 mem_loc_result = op1;
15361 add_loc_descr (&mem_loc_result, op2);
15362 add_loc_descr (&mem_loc_result, op0);
15363 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
15364 add_loc_descr (&mem_loc_result, bra_node);
15365 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
15366 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
15367 add_loc_descr (&mem_loc_result, drop_node);
15368 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15369 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
15371 break;
15373 case FLOAT_EXTEND:
15374 case FLOAT_TRUNCATE:
15375 case FLOAT:
15376 case UNSIGNED_FLOAT:
15377 case FIX:
15378 case UNSIGNED_FIX:
15379 if (!dwarf_strict || dwarf_version >= 5)
15381 dw_die_ref type_die;
15382 dw_loc_descr_ref cvt;
15384 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
15385 mem_mode, VAR_INIT_STATUS_INITIALIZED);
15386 if (op0 == NULL)
15387 break;
15388 if (SCALAR_INT_MODE_P (GET_MODE (XEXP (rtl, 0)))
15389 && (GET_CODE (rtl) == FLOAT
15390 || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
15391 <= DWARF2_ADDR_SIZE))
15393 type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
15394 GET_CODE (rtl) == UNSIGNED_FLOAT);
15395 if (type_die == NULL)
15396 break;
15397 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15398 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15399 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15400 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15401 add_loc_descr (&op0, cvt);
15403 type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
15404 if (type_die == NULL)
15405 break;
15406 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15407 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15408 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15409 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15410 add_loc_descr (&op0, cvt);
15411 if (SCALAR_INT_MODE_P (mode)
15412 && (GET_CODE (rtl) == FIX
15413 || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
15415 op0 = convert_descriptor_to_mode (mode, op0);
15416 if (op0 == NULL)
15417 break;
15419 mem_loc_result = op0;
15421 break;
15423 case CLZ:
15424 case CTZ:
15425 case FFS:
15426 mem_loc_result = clz_loc_descriptor (rtl, mode, mem_mode);
15427 break;
15429 case POPCOUNT:
15430 case PARITY:
15431 mem_loc_result = popcount_loc_descriptor (rtl, mode, mem_mode);
15432 break;
15434 case BSWAP:
15435 mem_loc_result = bswap_loc_descriptor (rtl, mode, mem_mode);
15436 break;
15438 case ROTATE:
15439 case ROTATERT:
15440 mem_loc_result = rotate_loc_descriptor (rtl, mode, mem_mode);
15441 break;
15443 case COMPARE:
15444 /* In theory, we could implement the above. */
15445 /* DWARF cannot represent the unsigned compare operations
15446 natively. */
15447 case SS_MULT:
15448 case US_MULT:
15449 case SS_DIV:
15450 case US_DIV:
15451 case SS_PLUS:
15452 case US_PLUS:
15453 case SS_MINUS:
15454 case US_MINUS:
15455 case SS_NEG:
15456 case US_NEG:
15457 case SS_ABS:
15458 case SS_ASHIFT:
15459 case US_ASHIFT:
15460 case SS_TRUNCATE:
15461 case US_TRUNCATE:
15462 case UNORDERED:
15463 case ORDERED:
15464 case UNEQ:
15465 case UNGE:
15466 case UNGT:
15467 case UNLE:
15468 case UNLT:
15469 case LTGT:
15470 case FRACT_CONVERT:
15471 case UNSIGNED_FRACT_CONVERT:
15472 case SAT_FRACT:
15473 case UNSIGNED_SAT_FRACT:
15474 case SQRT:
15475 case ASM_OPERANDS:
15476 case VEC_MERGE:
15477 case VEC_SELECT:
15478 case VEC_CONCAT:
15479 case VEC_DUPLICATE:
15480 case UNSPEC:
15481 case HIGH:
15482 case FMA:
15483 case STRICT_LOW_PART:
15484 case CONST_VECTOR:
15485 case CONST_FIXED:
15486 case CLRSB:
15487 case CLOBBER:
15488 /* If delegitimize_address couldn't do anything with the UNSPEC, we
15489 can't express it in the debug info. This can happen e.g. with some
15490 TLS UNSPECs. */
15491 break;
15493 case CONST_STRING:
15494 resolve_one_addr (&rtl);
15495 goto symref;
15497 /* RTL sequences inside PARALLEL record a series of DWARF operations for
15498 the expression. An UNSPEC rtx represents a raw DWARF operation,
15499 new_loc_descr is called for it to build the operation directly.
15500 Otherwise mem_loc_descriptor is called recursively. */
15501 case PARALLEL:
15503 int index = 0;
15504 dw_loc_descr_ref exp_result = NULL;
15506 for (; index < XVECLEN (rtl, 0); index++)
15508 rtx elem = XVECEXP (rtl, 0, index);
15509 if (GET_CODE (elem) == UNSPEC)
15511 /* Each DWARF operation UNSPEC contain two operands, if
15512 one operand is not used for the operation, const0_rtx is
15513 passed. */
15514 gcc_assert (XVECLEN (elem, 0) == 2);
15516 HOST_WIDE_INT dw_op = XINT (elem, 1);
15517 HOST_WIDE_INT oprnd1 = INTVAL (XVECEXP (elem, 0, 0));
15518 HOST_WIDE_INT oprnd2 = INTVAL (XVECEXP (elem, 0, 1));
15519 exp_result
15520 = new_loc_descr ((enum dwarf_location_atom) dw_op, oprnd1,
15521 oprnd2);
15523 else
15524 exp_result
15525 = mem_loc_descriptor (elem, mode, mem_mode,
15526 VAR_INIT_STATUS_INITIALIZED);
15528 if (!mem_loc_result)
15529 mem_loc_result = exp_result;
15530 else
15531 add_loc_descr (&mem_loc_result, exp_result);
15534 break;
15537 default:
15538 if (flag_checking)
15540 print_rtl (stderr, rtl);
15541 gcc_unreachable ();
15543 break;
15546 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
15547 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
15549 return mem_loc_result;
15552 /* Return a descriptor that describes the concatenation of two locations.
15553 This is typically a complex variable. */
15555 static dw_loc_descr_ref
15556 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
15558 dw_loc_descr_ref cc_loc_result = NULL;
15559 dw_loc_descr_ref x0_ref
15560 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15561 dw_loc_descr_ref x1_ref
15562 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15564 if (x0_ref == 0 || x1_ref == 0)
15565 return 0;
15567 cc_loc_result = x0_ref;
15568 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
15570 add_loc_descr (&cc_loc_result, x1_ref);
15571 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
15573 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
15574 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
15576 return cc_loc_result;
15579 /* Return a descriptor that describes the concatenation of N
15580 locations. */
15582 static dw_loc_descr_ref
15583 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
15585 unsigned int i;
15586 dw_loc_descr_ref cc_loc_result = NULL;
15587 unsigned int n = XVECLEN (concatn, 0);
15589 for (i = 0; i < n; ++i)
15591 dw_loc_descr_ref ref;
15592 rtx x = XVECEXP (concatn, 0, i);
15594 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15595 if (ref == NULL)
15596 return NULL;
15598 add_loc_descr (&cc_loc_result, ref);
15599 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
15602 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
15603 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
15605 return cc_loc_result;
15608 /* Helper function for loc_descriptor. Return DW_OP_implicit_pointer
15609 for DEBUG_IMPLICIT_PTR RTL. */
15611 static dw_loc_descr_ref
15612 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
15614 dw_loc_descr_ref ret;
15615 dw_die_ref ref;
15617 if (dwarf_strict && dwarf_version < 5)
15618 return NULL;
15619 gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
15620 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
15621 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
15622 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
15623 ret = new_loc_descr (dwarf_OP (DW_OP_implicit_pointer), 0, offset);
15624 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
15625 if (ref)
15627 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15628 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
15629 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
15631 else
15633 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
15634 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
15636 return ret;
15639 /* Output a proper Dwarf location descriptor for a variable or parameter
15640 which is either allocated in a register or in a memory location. For a
15641 register, we just generate an OP_REG and the register number. For a
15642 memory location we provide a Dwarf postfix expression describing how to
15643 generate the (dynamic) address of the object onto the address stack.
15645 MODE is mode of the decl if this loc_descriptor is going to be used in
15646 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
15647 allowed, VOIDmode otherwise.
15649 If we don't know how to describe it, return 0. */
15651 static dw_loc_descr_ref
15652 loc_descriptor (rtx rtl, machine_mode mode,
15653 enum var_init_status initialized)
15655 dw_loc_descr_ref loc_result = NULL;
15657 switch (GET_CODE (rtl))
15659 case SUBREG:
15660 /* The case of a subreg may arise when we have a local (register)
15661 variable or a formal (register) parameter which doesn't quite fill
15662 up an entire register. For now, just assume that it is
15663 legitimate to make the Dwarf info refer to the whole register which
15664 contains the given subreg. */
15665 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
15666 loc_result = loc_descriptor (SUBREG_REG (rtl),
15667 GET_MODE (SUBREG_REG (rtl)), initialized);
15668 else
15669 goto do_default;
15670 break;
15672 case REG:
15673 loc_result = reg_loc_descriptor (rtl, initialized);
15674 break;
15676 case MEM:
15677 loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
15678 GET_MODE (rtl), initialized);
15679 if (loc_result == NULL)
15680 loc_result = tls_mem_loc_descriptor (rtl);
15681 if (loc_result == NULL)
15683 rtx new_rtl = avoid_constant_pool_reference (rtl);
15684 if (new_rtl != rtl)
15685 loc_result = loc_descriptor (new_rtl, mode, initialized);
15687 break;
15689 case CONCAT:
15690 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
15691 initialized);
15692 break;
15694 case CONCATN:
15695 loc_result = concatn_loc_descriptor (rtl, initialized);
15696 break;
15698 case VAR_LOCATION:
15699 /* Single part. */
15700 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
15702 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
15703 if (GET_CODE (loc) == EXPR_LIST)
15704 loc = XEXP (loc, 0);
15705 loc_result = loc_descriptor (loc, mode, initialized);
15706 break;
15709 rtl = XEXP (rtl, 1);
15710 /* FALLTHRU */
15712 case PARALLEL:
15714 rtvec par_elems = XVEC (rtl, 0);
15715 int num_elem = GET_NUM_ELEM (par_elems);
15716 machine_mode mode;
15717 int i;
15719 /* Create the first one, so we have something to add to. */
15720 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
15721 VOIDmode, initialized);
15722 if (loc_result == NULL)
15723 return NULL;
15724 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
15725 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
15726 for (i = 1; i < num_elem; i++)
15728 dw_loc_descr_ref temp;
15730 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
15731 VOIDmode, initialized);
15732 if (temp == NULL)
15733 return NULL;
15734 add_loc_descr (&loc_result, temp);
15735 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
15736 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
15739 break;
15741 case CONST_INT:
15742 if (mode != VOIDmode && mode != BLKmode)
15743 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
15744 INTVAL (rtl));
15745 break;
15747 case CONST_DOUBLE:
15748 if (mode == VOIDmode)
15749 mode = GET_MODE (rtl);
15751 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
15753 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
15755 /* Note that a CONST_DOUBLE rtx could represent either an integer
15756 or a floating-point constant. A CONST_DOUBLE is used whenever
15757 the constant requires more than one word in order to be
15758 adequately represented. We output CONST_DOUBLEs as blocks. */
15759 loc_result = new_loc_descr (DW_OP_implicit_value,
15760 GET_MODE_SIZE (mode), 0);
15761 #if TARGET_SUPPORTS_WIDE_INT == 0
15762 if (!SCALAR_FLOAT_MODE_P (mode))
15764 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
15765 loc_result->dw_loc_oprnd2.v.val_double
15766 = rtx_to_double_int (rtl);
15768 else
15769 #endif
15771 unsigned int length = GET_MODE_SIZE (mode);
15772 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
15774 insert_float (rtl, array);
15775 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
15776 loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
15777 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
15778 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
15781 break;
15783 case CONST_WIDE_INT:
15784 if (mode == VOIDmode)
15785 mode = GET_MODE (rtl);
15787 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
15789 loc_result = new_loc_descr (DW_OP_implicit_value,
15790 GET_MODE_SIZE (mode), 0);
15791 loc_result->dw_loc_oprnd2.val_class = dw_val_class_wide_int;
15792 loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
15793 *loc_result->dw_loc_oprnd2.v.val_wide = rtx_mode_t (rtl, mode);
15795 break;
15797 case CONST_VECTOR:
15798 if (mode == VOIDmode)
15799 mode = GET_MODE (rtl);
15801 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
15803 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
15804 unsigned int length = CONST_VECTOR_NUNITS (rtl);
15805 unsigned char *array
15806 = ggc_vec_alloc<unsigned char> (length * elt_size);
15807 unsigned int i;
15808 unsigned char *p;
15809 machine_mode imode = GET_MODE_INNER (mode);
15811 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
15812 switch (GET_MODE_CLASS (mode))
15814 case MODE_VECTOR_INT:
15815 for (i = 0, p = array; i < length; i++, p += elt_size)
15817 rtx elt = CONST_VECTOR_ELT (rtl, i);
15818 insert_wide_int (rtx_mode_t (elt, imode), p, elt_size);
15820 break;
15822 case MODE_VECTOR_FLOAT:
15823 for (i = 0, p = array; i < length; i++, p += elt_size)
15825 rtx elt = CONST_VECTOR_ELT (rtl, i);
15826 insert_float (elt, p);
15828 break;
15830 default:
15831 gcc_unreachable ();
15834 loc_result = new_loc_descr (DW_OP_implicit_value,
15835 length * elt_size, 0);
15836 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
15837 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
15838 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
15839 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
15841 break;
15843 case CONST:
15844 if (mode == VOIDmode
15845 || CONST_SCALAR_INT_P (XEXP (rtl, 0))
15846 || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
15847 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
15849 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
15850 break;
15852 /* FALLTHROUGH */
15853 case SYMBOL_REF:
15854 if (!const_ok_for_output (rtl))
15855 break;
15856 /* FALLTHROUGH */
15857 case LABEL_REF:
15858 if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
15859 && (dwarf_version >= 4 || !dwarf_strict))
15861 loc_result = new_addr_loc_descr (rtl, dtprel_false);
15862 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
15863 vec_safe_push (used_rtx_array, rtl);
15865 break;
15867 case DEBUG_IMPLICIT_PTR:
15868 loc_result = implicit_ptr_descriptor (rtl, 0);
15869 break;
15871 case PLUS:
15872 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
15873 && CONST_INT_P (XEXP (rtl, 1)))
15875 loc_result
15876 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
15877 break;
15879 /* FALLTHRU */
15880 do_default:
15881 default:
15882 if ((SCALAR_INT_MODE_P (mode)
15883 && GET_MODE (rtl) == mode
15884 && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
15885 && dwarf_version >= 4)
15886 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
15888 /* Value expression. */
15889 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
15890 if (loc_result)
15891 add_loc_descr (&loc_result,
15892 new_loc_descr (DW_OP_stack_value, 0, 0));
15894 break;
15897 return loc_result;
15900 /* We need to figure out what section we should use as the base for the
15901 address ranges where a given location is valid.
15902 1. If this particular DECL has a section associated with it, use that.
15903 2. If this function has a section associated with it, use that.
15904 3. Otherwise, use the text section.
15905 XXX: If you split a variable across multiple sections, we won't notice. */
15907 static const char *
15908 secname_for_decl (const_tree decl)
15910 const char *secname;
15912 if (VAR_OR_FUNCTION_DECL_P (decl)
15913 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl) || TREE_STATIC (decl))
15914 && DECL_SECTION_NAME (decl))
15915 secname = DECL_SECTION_NAME (decl);
15916 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
15917 secname = DECL_SECTION_NAME (current_function_decl);
15918 else if (cfun && in_cold_section_p)
15919 secname = crtl->subsections.cold_section_label;
15920 else
15921 secname = text_section_label;
15923 return secname;
15926 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
15928 static bool
15929 decl_by_reference_p (tree decl)
15931 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
15932 || VAR_P (decl))
15933 && DECL_BY_REFERENCE (decl));
15936 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
15937 for VARLOC. */
15939 static dw_loc_descr_ref
15940 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
15941 enum var_init_status initialized)
15943 int have_address = 0;
15944 dw_loc_descr_ref descr;
15945 machine_mode mode;
15947 if (want_address != 2)
15949 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
15950 /* Single part. */
15951 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
15953 varloc = PAT_VAR_LOCATION_LOC (varloc);
15954 if (GET_CODE (varloc) == EXPR_LIST)
15955 varloc = XEXP (varloc, 0);
15956 mode = GET_MODE (varloc);
15957 if (MEM_P (varloc))
15959 rtx addr = XEXP (varloc, 0);
15960 descr = mem_loc_descriptor (addr, get_address_mode (varloc),
15961 mode, initialized);
15962 if (descr)
15963 have_address = 1;
15964 else
15966 rtx x = avoid_constant_pool_reference (varloc);
15967 if (x != varloc)
15968 descr = mem_loc_descriptor (x, mode, VOIDmode,
15969 initialized);
15972 else
15973 descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
15975 else
15976 return 0;
15978 else
15980 if (GET_CODE (varloc) == VAR_LOCATION)
15981 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
15982 else
15983 mode = DECL_MODE (loc);
15984 descr = loc_descriptor (varloc, mode, initialized);
15985 have_address = 1;
15988 if (!descr)
15989 return 0;
15991 if (want_address == 2 && !have_address
15992 && (dwarf_version >= 4 || !dwarf_strict))
15994 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
15996 expansion_failed (loc, NULL_RTX,
15997 "DWARF address size mismatch");
15998 return 0;
16000 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
16001 have_address = 1;
16003 /* Show if we can't fill the request for an address. */
16004 if (want_address && !have_address)
16006 expansion_failed (loc, NULL_RTX,
16007 "Want address and only have value");
16008 return 0;
16011 /* If we've got an address and don't want one, dereference. */
16012 if (!want_address && have_address)
16014 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
16015 enum dwarf_location_atom op;
16017 if (size > DWARF2_ADDR_SIZE || size == -1)
16019 expansion_failed (loc, NULL_RTX,
16020 "DWARF address size mismatch");
16021 return 0;
16023 else if (size == DWARF2_ADDR_SIZE)
16024 op = DW_OP_deref;
16025 else
16026 op = DW_OP_deref_size;
16028 add_loc_descr (&descr, new_loc_descr (op, size, 0));
16031 return descr;
16034 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
16035 if it is not possible. */
16037 static dw_loc_descr_ref
16038 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
16040 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
16041 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
16042 else if (dwarf_version >= 3 || !dwarf_strict)
16043 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
16044 else
16045 return NULL;
16048 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
16049 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
16051 static dw_loc_descr_ref
16052 dw_sra_loc_expr (tree decl, rtx loc)
16054 rtx p;
16055 unsigned HOST_WIDE_INT padsize = 0;
16056 dw_loc_descr_ref descr, *descr_tail;
16057 unsigned HOST_WIDE_INT decl_size;
16058 rtx varloc;
16059 enum var_init_status initialized;
16061 if (DECL_SIZE (decl) == NULL
16062 || !tree_fits_uhwi_p (DECL_SIZE (decl)))
16063 return NULL;
16065 decl_size = tree_to_uhwi (DECL_SIZE (decl));
16066 descr = NULL;
16067 descr_tail = &descr;
16069 for (p = loc; p; p = XEXP (p, 1))
16071 unsigned HOST_WIDE_INT bitsize = decl_piece_bitsize (p);
16072 rtx loc_note = *decl_piece_varloc_ptr (p);
16073 dw_loc_descr_ref cur_descr;
16074 dw_loc_descr_ref *tail, last = NULL;
16075 unsigned HOST_WIDE_INT opsize = 0;
16077 if (loc_note == NULL_RTX
16078 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
16080 padsize += bitsize;
16081 continue;
16083 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
16084 varloc = NOTE_VAR_LOCATION (loc_note);
16085 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
16086 if (cur_descr == NULL)
16088 padsize += bitsize;
16089 continue;
16092 /* Check that cur_descr either doesn't use
16093 DW_OP_*piece operations, or their sum is equal
16094 to bitsize. Otherwise we can't embed it. */
16095 for (tail = &cur_descr; *tail != NULL;
16096 tail = &(*tail)->dw_loc_next)
16097 if ((*tail)->dw_loc_opc == DW_OP_piece)
16099 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
16100 * BITS_PER_UNIT;
16101 last = *tail;
16103 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
16105 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
16106 last = *tail;
16109 if (last != NULL && opsize != bitsize)
16111 padsize += bitsize;
16112 /* Discard the current piece of the descriptor and release any
16113 addr_table entries it uses. */
16114 remove_loc_list_addr_table_entries (cur_descr);
16115 continue;
16118 /* If there is a hole, add DW_OP_*piece after empty DWARF
16119 expression, which means that those bits are optimized out. */
16120 if (padsize)
16122 if (padsize > decl_size)
16124 remove_loc_list_addr_table_entries (cur_descr);
16125 goto discard_descr;
16127 decl_size -= padsize;
16128 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
16129 if (*descr_tail == NULL)
16131 remove_loc_list_addr_table_entries (cur_descr);
16132 goto discard_descr;
16134 descr_tail = &(*descr_tail)->dw_loc_next;
16135 padsize = 0;
16137 *descr_tail = cur_descr;
16138 descr_tail = tail;
16139 if (bitsize > decl_size)
16140 goto discard_descr;
16141 decl_size -= bitsize;
16142 if (last == NULL)
16144 HOST_WIDE_INT offset = 0;
16145 if (GET_CODE (varloc) == VAR_LOCATION
16146 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
16148 varloc = PAT_VAR_LOCATION_LOC (varloc);
16149 if (GET_CODE (varloc) == EXPR_LIST)
16150 varloc = XEXP (varloc, 0);
16154 if (GET_CODE (varloc) == CONST
16155 || GET_CODE (varloc) == SIGN_EXTEND
16156 || GET_CODE (varloc) == ZERO_EXTEND)
16157 varloc = XEXP (varloc, 0);
16158 else if (GET_CODE (varloc) == SUBREG)
16159 varloc = SUBREG_REG (varloc);
16160 else
16161 break;
16163 while (1);
16164 /* DW_OP_bit_size offset should be zero for register
16165 or implicit location descriptions and empty location
16166 descriptions, but for memory addresses needs big endian
16167 adjustment. */
16168 if (MEM_P (varloc))
16170 unsigned HOST_WIDE_INT memsize
16171 = MEM_SIZE (varloc) * BITS_PER_UNIT;
16172 if (memsize != bitsize)
16174 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
16175 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
16176 goto discard_descr;
16177 if (memsize < bitsize)
16178 goto discard_descr;
16179 if (BITS_BIG_ENDIAN)
16180 offset = memsize - bitsize;
16184 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
16185 if (*descr_tail == NULL)
16186 goto discard_descr;
16187 descr_tail = &(*descr_tail)->dw_loc_next;
16191 /* If there were any non-empty expressions, add padding till the end of
16192 the decl. */
16193 if (descr != NULL && decl_size != 0)
16195 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
16196 if (*descr_tail == NULL)
16197 goto discard_descr;
16199 return descr;
16201 discard_descr:
16202 /* Discard the descriptor and release any addr_table entries it uses. */
16203 remove_loc_list_addr_table_entries (descr);
16204 return NULL;
16207 /* Return the dwarf representation of the location list LOC_LIST of
16208 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
16209 function. */
16211 static dw_loc_list_ref
16212 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
16214 const char *endname, *secname;
16215 rtx varloc;
16216 enum var_init_status initialized;
16217 struct var_loc_node *node;
16218 dw_loc_descr_ref descr;
16219 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
16220 dw_loc_list_ref list = NULL;
16221 dw_loc_list_ref *listp = &list;
16223 /* Now that we know what section we are using for a base,
16224 actually construct the list of locations.
16225 The first location information is what is passed to the
16226 function that creates the location list, and the remaining
16227 locations just get added on to that list.
16228 Note that we only know the start address for a location
16229 (IE location changes), so to build the range, we use
16230 the range [current location start, next location start].
16231 This means we have to special case the last node, and generate
16232 a range of [last location start, end of function label]. */
16234 secname = secname_for_decl (decl);
16236 for (node = loc_list->first; node; node = node->next)
16237 if (GET_CODE (node->loc) == EXPR_LIST
16238 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
16240 if (GET_CODE (node->loc) == EXPR_LIST)
16242 /* This requires DW_OP_{,bit_}piece, which is not usable
16243 inside DWARF expressions. */
16244 if (want_address != 2)
16245 continue;
16246 descr = dw_sra_loc_expr (decl, node->loc);
16247 if (descr == NULL)
16248 continue;
16250 else
16252 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
16253 varloc = NOTE_VAR_LOCATION (node->loc);
16254 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
16256 if (descr)
16258 bool range_across_switch = false;
16259 /* If section switch happens in between node->label
16260 and node->next->label (or end of function) and
16261 we can't emit it as a single entry list,
16262 emit two ranges, first one ending at the end
16263 of first partition and second one starting at the
16264 beginning of second partition. */
16265 if (node == loc_list->last_before_switch
16266 && (node != loc_list->first || loc_list->first->next)
16267 && current_function_decl)
16269 endname = cfun->fde->dw_fde_end;
16270 range_across_switch = true;
16272 /* The variable has a location between NODE->LABEL and
16273 NODE->NEXT->LABEL. */
16274 else if (node->next)
16275 endname = node->next->label;
16276 /* If the variable has a location at the last label
16277 it keeps its location until the end of function. */
16278 else if (!current_function_decl)
16279 endname = text_end_label;
16280 else
16282 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
16283 current_function_funcdef_no);
16284 endname = ggc_strdup (label_id);
16287 *listp = new_loc_list (descr, node->label, endname, secname);
16288 if (TREE_CODE (decl) == PARM_DECL
16289 && node == loc_list->first
16290 && NOTE_P (node->loc)
16291 && strcmp (node->label, endname) == 0)
16292 (*listp)->force = true;
16293 listp = &(*listp)->dw_loc_next;
16295 if (range_across_switch)
16297 if (GET_CODE (node->loc) == EXPR_LIST)
16298 descr = dw_sra_loc_expr (decl, node->loc);
16299 else
16301 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
16302 varloc = NOTE_VAR_LOCATION (node->loc);
16303 descr = dw_loc_list_1 (decl, varloc, want_address,
16304 initialized);
16306 gcc_assert (descr);
16307 /* The variable has a location between NODE->LABEL and
16308 NODE->NEXT->LABEL. */
16309 if (node->next)
16310 endname = node->next->label;
16311 else
16312 endname = cfun->fde->dw_fde_second_end;
16313 *listp = new_loc_list (descr,
16314 cfun->fde->dw_fde_second_begin,
16315 endname, secname);
16316 listp = &(*listp)->dw_loc_next;
16321 /* Try to avoid the overhead of a location list emitting a location
16322 expression instead, but only if we didn't have more than one
16323 location entry in the first place. If some entries were not
16324 representable, we don't want to pretend a single entry that was
16325 applies to the entire scope in which the variable is
16326 available. */
16327 if (list && loc_list->first->next)
16328 gen_llsym (list);
16330 return list;
16333 /* Return if the loc_list has only single element and thus can be represented
16334 as location description. */
16336 static bool
16337 single_element_loc_list_p (dw_loc_list_ref list)
16339 gcc_assert (!list->dw_loc_next || list->ll_symbol);
16340 return !list->ll_symbol;
16343 /* Duplicate a single element of location list. */
16345 static inline dw_loc_descr_ref
16346 copy_loc_descr (dw_loc_descr_ref ref)
16348 dw_loc_descr_ref copy = ggc_alloc<dw_loc_descr_node> ();
16349 memcpy (copy, ref, sizeof (dw_loc_descr_node));
16350 return copy;
16353 /* To each location in list LIST append loc descr REF. */
16355 static void
16356 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
16358 dw_loc_descr_ref copy;
16359 add_loc_descr (&list->expr, ref);
16360 list = list->dw_loc_next;
16361 while (list)
16363 copy = copy_loc_descr (ref);
16364 add_loc_descr (&list->expr, copy);
16365 while (copy->dw_loc_next)
16366 copy = copy->dw_loc_next = copy_loc_descr (copy->dw_loc_next);
16367 list = list->dw_loc_next;
16371 /* To each location in list LIST prepend loc descr REF. */
16373 static void
16374 prepend_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
16376 dw_loc_descr_ref copy;
16377 dw_loc_descr_ref ref_end = list->expr;
16378 add_loc_descr (&ref, list->expr);
16379 list->expr = ref;
16380 list = list->dw_loc_next;
16381 while (list)
16383 dw_loc_descr_ref end = list->expr;
16384 list->expr = copy = copy_loc_descr (ref);
16385 while (copy->dw_loc_next != ref_end)
16386 copy = copy->dw_loc_next = copy_loc_descr (copy->dw_loc_next);
16387 copy->dw_loc_next = end;
16388 list = list->dw_loc_next;
16392 /* Given two lists RET and LIST
16393 produce location list that is result of adding expression in LIST
16394 to expression in RET on each position in program.
16395 Might be destructive on both RET and LIST.
16397 TODO: We handle only simple cases of RET or LIST having at most one
16398 element. General case would involve sorting the lists in program order
16399 and merging them that will need some additional work.
16400 Adding that will improve quality of debug info especially for SRA-ed
16401 structures. */
16403 static void
16404 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
16406 if (!list)
16407 return;
16408 if (!*ret)
16410 *ret = list;
16411 return;
16413 if (!list->dw_loc_next)
16415 add_loc_descr_to_each (*ret, list->expr);
16416 return;
16418 if (!(*ret)->dw_loc_next)
16420 prepend_loc_descr_to_each (list, (*ret)->expr);
16421 *ret = list;
16422 return;
16424 expansion_failed (NULL_TREE, NULL_RTX,
16425 "Don't know how to merge two non-trivial"
16426 " location lists.\n");
16427 *ret = NULL;
16428 return;
16431 /* LOC is constant expression. Try a luck, look it up in constant
16432 pool and return its loc_descr of its address. */
16434 static dw_loc_descr_ref
16435 cst_pool_loc_descr (tree loc)
16437 /* Get an RTL for this, if something has been emitted. */
16438 rtx rtl = lookup_constant_def (loc);
16440 if (!rtl || !MEM_P (rtl))
16442 gcc_assert (!rtl);
16443 return 0;
16445 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
16447 /* TODO: We might get more coverage if we was actually delaying expansion
16448 of all expressions till end of compilation when constant pools are fully
16449 populated. */
16450 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
16452 expansion_failed (loc, NULL_RTX,
16453 "CST value in contant pool but not marked.");
16454 return 0;
16456 return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
16457 GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
16460 /* Return dw_loc_list representing address of addr_expr LOC
16461 by looking for inner INDIRECT_REF expression and turning
16462 it into simple arithmetics.
16464 See loc_list_from_tree for the meaning of CONTEXT. */
16466 static dw_loc_list_ref
16467 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev,
16468 loc_descr_context *context)
16470 tree obj, offset;
16471 HOST_WIDE_INT bitsize, bitpos, bytepos;
16472 machine_mode mode;
16473 int unsignedp, reversep, volatilep = 0;
16474 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
16476 obj = get_inner_reference (TREE_OPERAND (loc, 0),
16477 &bitsize, &bitpos, &offset, &mode,
16478 &unsignedp, &reversep, &volatilep);
16479 STRIP_NOPS (obj);
16480 if (bitpos % BITS_PER_UNIT)
16482 expansion_failed (loc, NULL_RTX, "bitfield access");
16483 return 0;
16485 if (!INDIRECT_REF_P (obj))
16487 expansion_failed (obj,
16488 NULL_RTX, "no indirect ref in inner refrence");
16489 return 0;
16491 if (!offset && !bitpos)
16492 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1,
16493 context);
16494 else if (toplev
16495 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
16496 && (dwarf_version >= 4 || !dwarf_strict))
16498 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0, context);
16499 if (!list_ret)
16500 return 0;
16501 if (offset)
16503 /* Variable offset. */
16504 list_ret1 = loc_list_from_tree (offset, 0, context);
16505 if (list_ret1 == 0)
16506 return 0;
16507 add_loc_list (&list_ret, list_ret1);
16508 if (!list_ret)
16509 return 0;
16510 add_loc_descr_to_each (list_ret,
16511 new_loc_descr (DW_OP_plus, 0, 0));
16513 bytepos = bitpos / BITS_PER_UNIT;
16514 if (bytepos > 0)
16515 add_loc_descr_to_each (list_ret,
16516 new_loc_descr (DW_OP_plus_uconst,
16517 bytepos, 0));
16518 else if (bytepos < 0)
16519 loc_list_plus_const (list_ret, bytepos);
16520 add_loc_descr_to_each (list_ret,
16521 new_loc_descr (DW_OP_stack_value, 0, 0));
16523 return list_ret;
16526 /* Set LOC to the next operation that is not a DW_OP_nop operation. In the case
16527 all operations from LOC are nops, move to the last one. Insert in NOPS all
16528 operations that are skipped. */
16530 static void
16531 loc_descr_to_next_no_nop (dw_loc_descr_ref &loc,
16532 hash_set<dw_loc_descr_ref> &nops)
16534 while (loc->dw_loc_next != NULL && loc->dw_loc_opc == DW_OP_nop)
16536 nops.add (loc);
16537 loc = loc->dw_loc_next;
16541 /* Helper for loc_descr_without_nops: free the location description operation
16542 P. */
16544 bool
16545 free_loc_descr (const dw_loc_descr_ref &loc, void *data ATTRIBUTE_UNUSED)
16547 ggc_free (loc);
16548 return true;
16551 /* Remove all DW_OP_nop operations from LOC except, if it exists, the one that
16552 finishes LOC. */
16554 static void
16555 loc_descr_without_nops (dw_loc_descr_ref &loc)
16557 if (loc->dw_loc_opc == DW_OP_nop && loc->dw_loc_next == NULL)
16558 return;
16560 /* Set of all DW_OP_nop operations we remove. */
16561 hash_set<dw_loc_descr_ref> nops;
16563 /* First, strip all prefix NOP operations in order to keep the head of the
16564 operations list. */
16565 loc_descr_to_next_no_nop (loc, nops);
16567 for (dw_loc_descr_ref cur = loc; cur != NULL;)
16569 /* For control flow operations: strip "prefix" nops in destination
16570 labels. */
16571 if (cur->dw_loc_oprnd1.val_class == dw_val_class_loc)
16572 loc_descr_to_next_no_nop (cur->dw_loc_oprnd1.v.val_loc, nops);
16573 if (cur->dw_loc_oprnd2.val_class == dw_val_class_loc)
16574 loc_descr_to_next_no_nop (cur->dw_loc_oprnd2.v.val_loc, nops);
16576 /* Do the same for the operations that follow, then move to the next
16577 iteration. */
16578 if (cur->dw_loc_next != NULL)
16579 loc_descr_to_next_no_nop (cur->dw_loc_next, nops);
16580 cur = cur->dw_loc_next;
16583 nops.traverse<void *, free_loc_descr> (NULL);
16587 struct dwarf_procedure_info;
16589 /* Helper structure for location descriptions generation. */
16590 struct loc_descr_context
16592 /* The type that is implicitly referenced by DW_OP_push_object_address, or
16593 NULL_TREE if DW_OP_push_object_address in invalid for this location
16594 description. This is used when processing PLACEHOLDER_EXPR nodes. */
16595 tree context_type;
16596 /* The ..._DECL node that should be translated as a
16597 DW_OP_push_object_address operation. */
16598 tree base_decl;
16599 /* Information about the DWARF procedure we are currently generating. NULL if
16600 we are not generating a DWARF procedure. */
16601 struct dwarf_procedure_info *dpi;
16602 /* True if integral PLACEHOLDER_EXPR stands for the first argument passed
16603 by consumer. Used for DW_TAG_generic_subrange attributes. */
16604 bool placeholder_arg;
16605 /* True if PLACEHOLDER_EXPR has been seen. */
16606 bool placeholder_seen;
16609 /* DWARF procedures generation
16611 DWARF expressions (aka. location descriptions) are used to encode variable
16612 things such as sizes or offsets. Such computations can have redundant parts
16613 that can be factorized in order to reduce the size of the output debug
16614 information. This is the whole point of DWARF procedures.
16616 Thanks to stor-layout.c, size and offset expressions in GENERIC trees are
16617 already factorized into functions ("size functions") in order to handle very
16618 big and complex types. Such functions are quite simple: they have integral
16619 arguments, they return an integral result and their body contains only a
16620 return statement with arithmetic expressions. This is the only kind of
16621 function we are interested in translating into DWARF procedures, here.
16623 DWARF expressions and DWARF procedure are executed using a stack, so we have
16624 to define some calling convention for them to interact. Let's say that:
16626 - Before calling a DWARF procedure, DWARF expressions must push on the stack
16627 all arguments in reverse order (right-to-left) so that when the DWARF
16628 procedure execution starts, the first argument is the top of the stack.
16630 - Then, when returning, the DWARF procedure must have consumed all arguments
16631 on the stack, must have pushed the result and touched nothing else.
16633 - Each integral argument and the result are integral types can be hold in a
16634 single stack slot.
16636 - We call "frame offset" the number of stack slots that are "under DWARF
16637 procedure control": it includes the arguments slots, the temporaries and
16638 the result slot. Thus, it is equal to the number of arguments when the
16639 procedure execution starts and must be equal to one (the result) when it
16640 returns. */
16642 /* Helper structure used when generating operations for a DWARF procedure. */
16643 struct dwarf_procedure_info
16645 /* The FUNCTION_DECL node corresponding to the DWARF procedure that is
16646 currently translated. */
16647 tree fndecl;
16648 /* The number of arguments FNDECL takes. */
16649 unsigned args_count;
16652 /* Return a pointer to a newly created DIE node for a DWARF procedure. Add
16653 LOCATION as its DW_AT_location attribute. If FNDECL is not NULL_TREE,
16654 equate it to this DIE. */
16656 static dw_die_ref
16657 new_dwarf_proc_die (dw_loc_descr_ref location, tree fndecl,
16658 dw_die_ref parent_die)
16660 dw_die_ref dwarf_proc_die;
16662 if ((dwarf_version < 3 && dwarf_strict)
16663 || location == NULL)
16664 return NULL;
16666 dwarf_proc_die = new_die (DW_TAG_dwarf_procedure, parent_die, fndecl);
16667 if (fndecl)
16668 equate_decl_number_to_die (fndecl, dwarf_proc_die);
16669 add_AT_loc (dwarf_proc_die, DW_AT_location, location);
16670 return dwarf_proc_die;
16673 /* Return whether TYPE is a supported type as a DWARF procedure argument
16674 type or return type (we handle only scalar types and pointer types that
16675 aren't wider than the DWARF expression evaluation stack. */
16677 static bool
16678 is_handled_procedure_type (tree type)
16680 return ((INTEGRAL_TYPE_P (type)
16681 || TREE_CODE (type) == OFFSET_TYPE
16682 || TREE_CODE (type) == POINTER_TYPE)
16683 && int_size_in_bytes (type) <= DWARF2_ADDR_SIZE);
16686 /* Helper for resolve_args_picking: do the same but stop when coming across
16687 visited nodes. For each node we visit, register in FRAME_OFFSETS the frame
16688 offset *before* evaluating the corresponding operation. */
16690 static bool
16691 resolve_args_picking_1 (dw_loc_descr_ref loc, unsigned initial_frame_offset,
16692 struct dwarf_procedure_info *dpi,
16693 hash_map<dw_loc_descr_ref, unsigned> &frame_offsets)
16695 /* The "frame_offset" identifier is already used to name a macro... */
16696 unsigned frame_offset_ = initial_frame_offset;
16697 dw_loc_descr_ref l;
16699 for (l = loc; l != NULL;)
16701 bool existed;
16702 unsigned &l_frame_offset = frame_offsets.get_or_insert (l, &existed);
16704 /* If we already met this node, there is nothing to compute anymore. */
16705 if (existed)
16707 /* Make sure that the stack size is consistent wherever the execution
16708 flow comes from. */
16709 gcc_assert ((unsigned) l_frame_offset == frame_offset_);
16710 break;
16712 l_frame_offset = frame_offset_;
16714 /* If needed, relocate the picking offset with respect to the frame
16715 offset. */
16716 if (l->frame_offset_rel)
16718 unsigned HOST_WIDE_INT off;
16719 switch (l->dw_loc_opc)
16721 case DW_OP_pick:
16722 off = l->dw_loc_oprnd1.v.val_unsigned;
16723 break;
16724 case DW_OP_dup:
16725 off = 0;
16726 break;
16727 case DW_OP_over:
16728 off = 1;
16729 break;
16730 default:
16731 gcc_unreachable ();
16733 /* frame_offset_ is the size of the current stack frame, including
16734 incoming arguments. Besides, the arguments are pushed
16735 right-to-left. Thus, in order to access the Nth argument from
16736 this operation node, the picking has to skip temporaries *plus*
16737 one stack slot per argument (0 for the first one, 1 for the second
16738 one, etc.).
16740 The targetted argument number (N) is already set as the operand,
16741 and the number of temporaries can be computed with:
16742 frame_offsets_ - dpi->args_count */
16743 off += frame_offset_ - dpi->args_count;
16745 /* DW_OP_pick handles only offsets from 0 to 255 (inclusive)... */
16746 if (off > 255)
16747 return false;
16749 if (off == 0)
16751 l->dw_loc_opc = DW_OP_dup;
16752 l->dw_loc_oprnd1.v.val_unsigned = 0;
16754 else if (off == 1)
16756 l->dw_loc_opc = DW_OP_over;
16757 l->dw_loc_oprnd1.v.val_unsigned = 0;
16759 else
16761 l->dw_loc_opc = DW_OP_pick;
16762 l->dw_loc_oprnd1.v.val_unsigned = off;
16766 /* Update frame_offset according to the effect the current operation has
16767 on the stack. */
16768 switch (l->dw_loc_opc)
16770 case DW_OP_deref:
16771 case DW_OP_swap:
16772 case DW_OP_rot:
16773 case DW_OP_abs:
16774 case DW_OP_neg:
16775 case DW_OP_not:
16776 case DW_OP_plus_uconst:
16777 case DW_OP_skip:
16778 case DW_OP_reg0:
16779 case DW_OP_reg1:
16780 case DW_OP_reg2:
16781 case DW_OP_reg3:
16782 case DW_OP_reg4:
16783 case DW_OP_reg5:
16784 case DW_OP_reg6:
16785 case DW_OP_reg7:
16786 case DW_OP_reg8:
16787 case DW_OP_reg9:
16788 case DW_OP_reg10:
16789 case DW_OP_reg11:
16790 case DW_OP_reg12:
16791 case DW_OP_reg13:
16792 case DW_OP_reg14:
16793 case DW_OP_reg15:
16794 case DW_OP_reg16:
16795 case DW_OP_reg17:
16796 case DW_OP_reg18:
16797 case DW_OP_reg19:
16798 case DW_OP_reg20:
16799 case DW_OP_reg21:
16800 case DW_OP_reg22:
16801 case DW_OP_reg23:
16802 case DW_OP_reg24:
16803 case DW_OP_reg25:
16804 case DW_OP_reg26:
16805 case DW_OP_reg27:
16806 case DW_OP_reg28:
16807 case DW_OP_reg29:
16808 case DW_OP_reg30:
16809 case DW_OP_reg31:
16810 case DW_OP_bregx:
16811 case DW_OP_piece:
16812 case DW_OP_deref_size:
16813 case DW_OP_nop:
16814 case DW_OP_bit_piece:
16815 case DW_OP_implicit_value:
16816 case DW_OP_stack_value:
16817 break;
16819 case DW_OP_addr:
16820 case DW_OP_const1u:
16821 case DW_OP_const1s:
16822 case DW_OP_const2u:
16823 case DW_OP_const2s:
16824 case DW_OP_const4u:
16825 case DW_OP_const4s:
16826 case DW_OP_const8u:
16827 case DW_OP_const8s:
16828 case DW_OP_constu:
16829 case DW_OP_consts:
16830 case DW_OP_dup:
16831 case DW_OP_over:
16832 case DW_OP_pick:
16833 case DW_OP_lit0:
16834 case DW_OP_lit1:
16835 case DW_OP_lit2:
16836 case DW_OP_lit3:
16837 case DW_OP_lit4:
16838 case DW_OP_lit5:
16839 case DW_OP_lit6:
16840 case DW_OP_lit7:
16841 case DW_OP_lit8:
16842 case DW_OP_lit9:
16843 case DW_OP_lit10:
16844 case DW_OP_lit11:
16845 case DW_OP_lit12:
16846 case DW_OP_lit13:
16847 case DW_OP_lit14:
16848 case DW_OP_lit15:
16849 case DW_OP_lit16:
16850 case DW_OP_lit17:
16851 case DW_OP_lit18:
16852 case DW_OP_lit19:
16853 case DW_OP_lit20:
16854 case DW_OP_lit21:
16855 case DW_OP_lit22:
16856 case DW_OP_lit23:
16857 case DW_OP_lit24:
16858 case DW_OP_lit25:
16859 case DW_OP_lit26:
16860 case DW_OP_lit27:
16861 case DW_OP_lit28:
16862 case DW_OP_lit29:
16863 case DW_OP_lit30:
16864 case DW_OP_lit31:
16865 case DW_OP_breg0:
16866 case DW_OP_breg1:
16867 case DW_OP_breg2:
16868 case DW_OP_breg3:
16869 case DW_OP_breg4:
16870 case DW_OP_breg5:
16871 case DW_OP_breg6:
16872 case DW_OP_breg7:
16873 case DW_OP_breg8:
16874 case DW_OP_breg9:
16875 case DW_OP_breg10:
16876 case DW_OP_breg11:
16877 case DW_OP_breg12:
16878 case DW_OP_breg13:
16879 case DW_OP_breg14:
16880 case DW_OP_breg15:
16881 case DW_OP_breg16:
16882 case DW_OP_breg17:
16883 case DW_OP_breg18:
16884 case DW_OP_breg19:
16885 case DW_OP_breg20:
16886 case DW_OP_breg21:
16887 case DW_OP_breg22:
16888 case DW_OP_breg23:
16889 case DW_OP_breg24:
16890 case DW_OP_breg25:
16891 case DW_OP_breg26:
16892 case DW_OP_breg27:
16893 case DW_OP_breg28:
16894 case DW_OP_breg29:
16895 case DW_OP_breg30:
16896 case DW_OP_breg31:
16897 case DW_OP_fbreg:
16898 case DW_OP_push_object_address:
16899 case DW_OP_call_frame_cfa:
16900 case DW_OP_GNU_variable_value:
16901 ++frame_offset_;
16902 break;
16904 case DW_OP_drop:
16905 case DW_OP_xderef:
16906 case DW_OP_and:
16907 case DW_OP_div:
16908 case DW_OP_minus:
16909 case DW_OP_mod:
16910 case DW_OP_mul:
16911 case DW_OP_or:
16912 case DW_OP_plus:
16913 case DW_OP_shl:
16914 case DW_OP_shr:
16915 case DW_OP_shra:
16916 case DW_OP_xor:
16917 case DW_OP_bra:
16918 case DW_OP_eq:
16919 case DW_OP_ge:
16920 case DW_OP_gt:
16921 case DW_OP_le:
16922 case DW_OP_lt:
16923 case DW_OP_ne:
16924 case DW_OP_regx:
16925 case DW_OP_xderef_size:
16926 --frame_offset_;
16927 break;
16929 case DW_OP_call2:
16930 case DW_OP_call4:
16931 case DW_OP_call_ref:
16933 dw_die_ref dwarf_proc = l->dw_loc_oprnd1.v.val_die_ref.die;
16934 int *stack_usage = dwarf_proc_stack_usage_map->get (dwarf_proc);
16936 if (stack_usage == NULL)
16937 return false;
16938 frame_offset_ += *stack_usage;
16939 break;
16942 case DW_OP_implicit_pointer:
16943 case DW_OP_entry_value:
16944 case DW_OP_const_type:
16945 case DW_OP_regval_type:
16946 case DW_OP_deref_type:
16947 case DW_OP_convert:
16948 case DW_OP_reinterpret:
16949 case DW_OP_form_tls_address:
16950 case DW_OP_GNU_push_tls_address:
16951 case DW_OP_GNU_uninit:
16952 case DW_OP_GNU_encoded_addr:
16953 case DW_OP_GNU_implicit_pointer:
16954 case DW_OP_GNU_entry_value:
16955 case DW_OP_GNU_const_type:
16956 case DW_OP_GNU_regval_type:
16957 case DW_OP_GNU_deref_type:
16958 case DW_OP_GNU_convert:
16959 case DW_OP_GNU_reinterpret:
16960 case DW_OP_GNU_parameter_ref:
16961 /* loc_list_from_tree will probably not output these operations for
16962 size functions, so assume they will not appear here. */
16963 /* Fall through... */
16965 default:
16966 gcc_unreachable ();
16969 /* Now, follow the control flow (except subroutine calls). */
16970 switch (l->dw_loc_opc)
16972 case DW_OP_bra:
16973 if (!resolve_args_picking_1 (l->dw_loc_next, frame_offset_, dpi,
16974 frame_offsets))
16975 return false;
16976 /* Fall through. */
16978 case DW_OP_skip:
16979 l = l->dw_loc_oprnd1.v.val_loc;
16980 break;
16982 case DW_OP_stack_value:
16983 return true;
16985 default:
16986 l = l->dw_loc_next;
16987 break;
16991 return true;
16994 /* Make a DFS over operations reachable through LOC (i.e. follow branch
16995 operations) in order to resolve the operand of DW_OP_pick operations that
16996 target DWARF procedure arguments (DPI). INITIAL_FRAME_OFFSET is the frame
16997 offset *before* LOC is executed. Return if all relocations were
16998 successful. */
17000 static bool
17001 resolve_args_picking (dw_loc_descr_ref loc, unsigned initial_frame_offset,
17002 struct dwarf_procedure_info *dpi)
17004 /* Associate to all visited operations the frame offset *before* evaluating
17005 this operation. */
17006 hash_map<dw_loc_descr_ref, unsigned> frame_offsets;
17008 return resolve_args_picking_1 (loc, initial_frame_offset, dpi,
17009 frame_offsets);
17012 /* Try to generate a DWARF procedure that computes the same result as FNDECL.
17013 Return NULL if it is not possible. */
17015 static dw_die_ref
17016 function_to_dwarf_procedure (tree fndecl)
17018 struct loc_descr_context ctx;
17019 struct dwarf_procedure_info dpi;
17020 dw_die_ref dwarf_proc_die;
17021 tree tree_body = DECL_SAVED_TREE (fndecl);
17022 dw_loc_descr_ref loc_body, epilogue;
17024 tree cursor;
17025 unsigned i;
17027 /* Do not generate multiple DWARF procedures for the same function
17028 declaration. */
17029 dwarf_proc_die = lookup_decl_die (fndecl);
17030 if (dwarf_proc_die != NULL)
17031 return dwarf_proc_die;
17033 /* DWARF procedures are available starting with the DWARFv3 standard. */
17034 if (dwarf_version < 3 && dwarf_strict)
17035 return NULL;
17037 /* We handle only functions for which we still have a body, that return a
17038 supported type and that takes arguments with supported types. Note that
17039 there is no point translating functions that return nothing. */
17040 if (tree_body == NULL_TREE
17041 || DECL_RESULT (fndecl) == NULL_TREE
17042 || !is_handled_procedure_type (TREE_TYPE (DECL_RESULT (fndecl))))
17043 return NULL;
17045 for (cursor = DECL_ARGUMENTS (fndecl);
17046 cursor != NULL_TREE;
17047 cursor = TREE_CHAIN (cursor))
17048 if (!is_handled_procedure_type (TREE_TYPE (cursor)))
17049 return NULL;
17051 /* Match only "expr" in: RETURN_EXPR (MODIFY_EXPR (RESULT_DECL, expr)). */
17052 if (TREE_CODE (tree_body) != RETURN_EXPR)
17053 return NULL;
17054 tree_body = TREE_OPERAND (tree_body, 0);
17055 if (TREE_CODE (tree_body) != MODIFY_EXPR
17056 || TREE_OPERAND (tree_body, 0) != DECL_RESULT (fndecl))
17057 return NULL;
17058 tree_body = TREE_OPERAND (tree_body, 1);
17060 /* Try to translate the body expression itself. Note that this will probably
17061 cause an infinite recursion if its call graph has a cycle. This is very
17062 unlikely for size functions, however, so don't bother with such things at
17063 the moment. */
17064 ctx.context_type = NULL_TREE;
17065 ctx.base_decl = NULL_TREE;
17066 ctx.dpi = &dpi;
17067 ctx.placeholder_arg = false;
17068 ctx.placeholder_seen = false;
17069 dpi.fndecl = fndecl;
17070 dpi.args_count = list_length (DECL_ARGUMENTS (fndecl));
17071 loc_body = loc_descriptor_from_tree (tree_body, 0, &ctx);
17072 if (!loc_body)
17073 return NULL;
17075 /* After evaluating all operands in "loc_body", we should still have on the
17076 stack all arguments plus the desired function result (top of the stack).
17077 Generate code in order to keep only the result in our stack frame. */
17078 epilogue = NULL;
17079 for (i = 0; i < dpi.args_count; ++i)
17081 dw_loc_descr_ref op_couple = new_loc_descr (DW_OP_swap, 0, 0);
17082 op_couple->dw_loc_next = new_loc_descr (DW_OP_drop, 0, 0);
17083 op_couple->dw_loc_next->dw_loc_next = epilogue;
17084 epilogue = op_couple;
17086 add_loc_descr (&loc_body, epilogue);
17087 if (!resolve_args_picking (loc_body, dpi.args_count, &dpi))
17088 return NULL;
17090 /* Trailing nops from loc_descriptor_from_tree (if any) cannot be removed
17091 because they are considered useful. Now there is an epilogue, they are
17092 not anymore, so give it another try. */
17093 loc_descr_without_nops (loc_body);
17095 /* fndecl may be used both as a regular DW_TAG_subprogram DIE and as
17096 a DW_TAG_dwarf_procedure, so we may have a conflict, here. It's unlikely,
17097 though, given that size functions do not come from source, so they should
17098 not have a dedicated DW_TAG_subprogram DIE. */
17099 dwarf_proc_die
17100 = new_dwarf_proc_die (loc_body, fndecl,
17101 get_context_die (DECL_CONTEXT (fndecl)));
17103 /* The called DWARF procedure consumes one stack slot per argument and
17104 returns one stack slot. */
17105 dwarf_proc_stack_usage_map->put (dwarf_proc_die, 1 - dpi.args_count);
17107 return dwarf_proc_die;
17111 /* Generate Dwarf location list representing LOC.
17112 If WANT_ADDRESS is false, expression computing LOC will be computed
17113 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
17114 if WANT_ADDRESS is 2, expression computing address useable in location
17115 will be returned (i.e. DW_OP_reg can be used
17116 to refer to register values).
17118 CONTEXT provides information to customize the location descriptions
17119 generation. Its context_type field specifies what type is implicitly
17120 referenced by DW_OP_push_object_address. If it is NULL_TREE, this operation
17121 will not be generated.
17123 Its DPI field determines whether we are generating a DWARF expression for a
17124 DWARF procedure, so PARM_DECL references are processed specifically.
17126 If CONTEXT is NULL, the behavior is the same as if context_type, base_decl
17127 and dpi fields were null. */
17129 static dw_loc_list_ref
17130 loc_list_from_tree_1 (tree loc, int want_address,
17131 struct loc_descr_context *context)
17133 dw_loc_descr_ref ret = NULL, ret1 = NULL;
17134 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
17135 int have_address = 0;
17136 enum dwarf_location_atom op;
17138 /* ??? Most of the time we do not take proper care for sign/zero
17139 extending the values properly. Hopefully this won't be a real
17140 problem... */
17142 if (context != NULL
17143 && context->base_decl == loc
17144 && want_address == 0)
17146 if (dwarf_version >= 3 || !dwarf_strict)
17147 return new_loc_list (new_loc_descr (DW_OP_push_object_address, 0, 0),
17148 NULL, NULL, NULL);
17149 else
17150 return NULL;
17153 switch (TREE_CODE (loc))
17155 case ERROR_MARK:
17156 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
17157 return 0;
17159 case PLACEHOLDER_EXPR:
17160 /* This case involves extracting fields from an object to determine the
17161 position of other fields. It is supposed to appear only as the first
17162 operand of COMPONENT_REF nodes and to reference precisely the type
17163 that the context allows. */
17164 if (context != NULL
17165 && TREE_TYPE (loc) == context->context_type
17166 && want_address >= 1)
17168 if (dwarf_version >= 3 || !dwarf_strict)
17170 ret = new_loc_descr (DW_OP_push_object_address, 0, 0);
17171 have_address = 1;
17172 break;
17174 else
17175 return NULL;
17177 /* For DW_TAG_generic_subrange attributes, PLACEHOLDER_EXPR stands for
17178 the single argument passed by consumer. */
17179 else if (context != NULL
17180 && context->placeholder_arg
17181 && INTEGRAL_TYPE_P (TREE_TYPE (loc))
17182 && want_address == 0)
17184 ret = new_loc_descr (DW_OP_pick, 0, 0);
17185 ret->frame_offset_rel = 1;
17186 context->placeholder_seen = true;
17187 break;
17189 else
17190 expansion_failed (loc, NULL_RTX,
17191 "PLACEHOLDER_EXPR for an unexpected type");
17192 break;
17194 case CALL_EXPR:
17196 const int nargs = call_expr_nargs (loc);
17197 tree callee = get_callee_fndecl (loc);
17198 int i;
17199 dw_die_ref dwarf_proc;
17201 if (callee == NULL_TREE)
17202 goto call_expansion_failed;
17204 /* We handle only functions that return an integer. */
17205 if (!is_handled_procedure_type (TREE_TYPE (TREE_TYPE (callee))))
17206 goto call_expansion_failed;
17208 dwarf_proc = function_to_dwarf_procedure (callee);
17209 if (dwarf_proc == NULL)
17210 goto call_expansion_failed;
17212 /* Evaluate arguments right-to-left so that the first argument will
17213 be the top-most one on the stack. */
17214 for (i = nargs - 1; i >= 0; --i)
17216 dw_loc_descr_ref loc_descr
17217 = loc_descriptor_from_tree (CALL_EXPR_ARG (loc, i), 0,
17218 context);
17220 if (loc_descr == NULL)
17221 goto call_expansion_failed;
17223 add_loc_descr (&ret, loc_descr);
17226 ret1 = new_loc_descr (DW_OP_call4, 0, 0);
17227 ret1->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
17228 ret1->dw_loc_oprnd1.v.val_die_ref.die = dwarf_proc;
17229 ret1->dw_loc_oprnd1.v.val_die_ref.external = 0;
17230 add_loc_descr (&ret, ret1);
17231 break;
17233 call_expansion_failed:
17234 expansion_failed (loc, NULL_RTX, "CALL_EXPR");
17235 /* There are no opcodes for these operations. */
17236 return 0;
17239 case PREINCREMENT_EXPR:
17240 case PREDECREMENT_EXPR:
17241 case POSTINCREMENT_EXPR:
17242 case POSTDECREMENT_EXPR:
17243 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
17244 /* There are no opcodes for these operations. */
17245 return 0;
17247 case ADDR_EXPR:
17248 /* If we already want an address, see if there is INDIRECT_REF inside
17249 e.g. for &this->field. */
17250 if (want_address)
17252 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
17253 (loc, want_address == 2, context);
17254 if (list_ret)
17255 have_address = 1;
17256 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
17257 && (ret = cst_pool_loc_descr (loc)))
17258 have_address = 1;
17260 /* Otherwise, process the argument and look for the address. */
17261 if (!list_ret && !ret)
17262 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 1, context);
17263 else
17265 if (want_address)
17266 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
17267 return NULL;
17269 break;
17271 case VAR_DECL:
17272 if (DECL_THREAD_LOCAL_P (loc))
17274 rtx rtl;
17275 enum dwarf_location_atom tls_op;
17276 enum dtprel_bool dtprel = dtprel_false;
17278 if (targetm.have_tls)
17280 /* If this is not defined, we have no way to emit the
17281 data. */
17282 if (!targetm.asm_out.output_dwarf_dtprel)
17283 return 0;
17285 /* The way DW_OP_GNU_push_tls_address is specified, we
17286 can only look up addresses of objects in the current
17287 module. We used DW_OP_addr as first op, but that's
17288 wrong, because DW_OP_addr is relocated by the debug
17289 info consumer, while DW_OP_GNU_push_tls_address
17290 operand shouldn't be. */
17291 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
17292 return 0;
17293 dtprel = dtprel_true;
17294 /* We check for DWARF 5 here because gdb did not implement
17295 DW_OP_form_tls_address until after 7.12. */
17296 tls_op = (dwarf_version >= 5 ? DW_OP_form_tls_address
17297 : DW_OP_GNU_push_tls_address);
17299 else
17301 if (!targetm.emutls.debug_form_tls_address
17302 || !(dwarf_version >= 3 || !dwarf_strict))
17303 return 0;
17304 /* We stuffed the control variable into the DECL_VALUE_EXPR
17305 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
17306 no longer appear in gimple code. We used the control
17307 variable in specific so that we could pick it up here. */
17308 loc = DECL_VALUE_EXPR (loc);
17309 tls_op = DW_OP_form_tls_address;
17312 rtl = rtl_for_decl_location (loc);
17313 if (rtl == NULL_RTX)
17314 return 0;
17316 if (!MEM_P (rtl))
17317 return 0;
17318 rtl = XEXP (rtl, 0);
17319 if (! CONSTANT_P (rtl))
17320 return 0;
17322 ret = new_addr_loc_descr (rtl, dtprel);
17323 ret1 = new_loc_descr (tls_op, 0, 0);
17324 add_loc_descr (&ret, ret1);
17326 have_address = 1;
17327 break;
17329 /* FALLTHRU */
17331 case PARM_DECL:
17332 if (context != NULL && context->dpi != NULL
17333 && DECL_CONTEXT (loc) == context->dpi->fndecl)
17335 /* We are generating code for a DWARF procedure and we want to access
17336 one of its arguments: find the appropriate argument offset and let
17337 the resolve_args_picking pass compute the offset that complies
17338 with the stack frame size. */
17339 unsigned i = 0;
17340 tree cursor;
17342 for (cursor = DECL_ARGUMENTS (context->dpi->fndecl);
17343 cursor != NULL_TREE && cursor != loc;
17344 cursor = TREE_CHAIN (cursor), ++i)
17346 /* If we are translating a DWARF procedure, all referenced parameters
17347 must belong to the current function. */
17348 gcc_assert (cursor != NULL_TREE);
17350 ret = new_loc_descr (DW_OP_pick, i, 0);
17351 ret->frame_offset_rel = 1;
17352 break;
17354 /* FALLTHRU */
17356 case RESULT_DECL:
17357 if (DECL_HAS_VALUE_EXPR_P (loc))
17358 return loc_list_from_tree_1 (DECL_VALUE_EXPR (loc),
17359 want_address, context);
17360 /* FALLTHRU */
17362 case FUNCTION_DECL:
17364 rtx rtl;
17365 var_loc_list *loc_list = lookup_decl_loc (loc);
17367 if (loc_list && loc_list->first)
17369 list_ret = dw_loc_list (loc_list, loc, want_address);
17370 have_address = want_address != 0;
17371 break;
17373 rtl = rtl_for_decl_location (loc);
17374 if (rtl == NULL_RTX)
17376 if (TREE_CODE (loc) != FUNCTION_DECL
17377 && early_dwarf
17378 && current_function_decl
17379 && want_address != 1
17380 && ! DECL_IGNORED_P (loc)
17381 && (INTEGRAL_TYPE_P (TREE_TYPE (loc))
17382 || POINTER_TYPE_P (TREE_TYPE (loc)))
17383 && DECL_CONTEXT (loc) == current_function_decl
17384 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (loc)))
17385 <= DWARF2_ADDR_SIZE))
17387 dw_die_ref ref = lookup_decl_die (loc);
17388 ret = new_loc_descr (DW_OP_GNU_variable_value, 0, 0);
17389 if (ref)
17391 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
17392 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
17393 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
17395 else
17397 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
17398 ret->dw_loc_oprnd1.v.val_decl_ref = loc;
17400 break;
17402 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
17403 return 0;
17405 else if (CONST_INT_P (rtl))
17407 HOST_WIDE_INT val = INTVAL (rtl);
17408 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
17409 val &= GET_MODE_MASK (DECL_MODE (loc));
17410 ret = int_loc_descriptor (val);
17412 else if (GET_CODE (rtl) == CONST_STRING)
17414 expansion_failed (loc, NULL_RTX, "CONST_STRING");
17415 return 0;
17417 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
17418 ret = new_addr_loc_descr (rtl, dtprel_false);
17419 else
17421 machine_mode mode, mem_mode;
17423 /* Certain constructs can only be represented at top-level. */
17424 if (want_address == 2)
17426 ret = loc_descriptor (rtl, VOIDmode,
17427 VAR_INIT_STATUS_INITIALIZED);
17428 have_address = 1;
17430 else
17432 mode = GET_MODE (rtl);
17433 mem_mode = VOIDmode;
17434 if (MEM_P (rtl))
17436 mem_mode = mode;
17437 mode = get_address_mode (rtl);
17438 rtl = XEXP (rtl, 0);
17439 have_address = 1;
17441 ret = mem_loc_descriptor (rtl, mode, mem_mode,
17442 VAR_INIT_STATUS_INITIALIZED);
17444 if (!ret)
17445 expansion_failed (loc, rtl,
17446 "failed to produce loc descriptor for rtl");
17449 break;
17451 case MEM_REF:
17452 if (!integer_zerop (TREE_OPERAND (loc, 1)))
17454 have_address = 1;
17455 goto do_plus;
17457 /* Fallthru. */
17458 case INDIRECT_REF:
17459 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17460 have_address = 1;
17461 break;
17463 case TARGET_MEM_REF:
17464 case SSA_NAME:
17465 case DEBUG_EXPR_DECL:
17466 return NULL;
17468 case COMPOUND_EXPR:
17469 return loc_list_from_tree_1 (TREE_OPERAND (loc, 1), want_address,
17470 context);
17472 CASE_CONVERT:
17473 case VIEW_CONVERT_EXPR:
17474 case SAVE_EXPR:
17475 case MODIFY_EXPR:
17476 case NON_LVALUE_EXPR:
17477 return loc_list_from_tree_1 (TREE_OPERAND (loc, 0), want_address,
17478 context);
17480 case COMPONENT_REF:
17481 case BIT_FIELD_REF:
17482 case ARRAY_REF:
17483 case ARRAY_RANGE_REF:
17484 case REALPART_EXPR:
17485 case IMAGPART_EXPR:
17487 tree obj, offset;
17488 HOST_WIDE_INT bitsize, bitpos, bytepos;
17489 machine_mode mode;
17490 int unsignedp, reversep, volatilep = 0;
17492 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
17493 &unsignedp, &reversep, &volatilep);
17495 gcc_assert (obj != loc);
17497 list_ret = loc_list_from_tree_1 (obj,
17498 want_address == 2
17499 && !bitpos && !offset ? 2 : 1,
17500 context);
17501 /* TODO: We can extract value of the small expression via shifting even
17502 for nonzero bitpos. */
17503 if (list_ret == 0)
17504 return 0;
17505 if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
17507 expansion_failed (loc, NULL_RTX,
17508 "bitfield access");
17509 return 0;
17512 if (offset != NULL_TREE)
17514 /* Variable offset. */
17515 list_ret1 = loc_list_from_tree_1 (offset, 0, context);
17516 if (list_ret1 == 0)
17517 return 0;
17518 add_loc_list (&list_ret, list_ret1);
17519 if (!list_ret)
17520 return 0;
17521 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
17524 bytepos = bitpos / BITS_PER_UNIT;
17525 if (bytepos > 0)
17526 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
17527 else if (bytepos < 0)
17528 loc_list_plus_const (list_ret, bytepos);
17530 have_address = 1;
17531 break;
17534 case INTEGER_CST:
17535 if ((want_address || !tree_fits_shwi_p (loc))
17536 && (ret = cst_pool_loc_descr (loc)))
17537 have_address = 1;
17538 else if (want_address == 2
17539 && tree_fits_shwi_p (loc)
17540 && (ret = address_of_int_loc_descriptor
17541 (int_size_in_bytes (TREE_TYPE (loc)),
17542 tree_to_shwi (loc))))
17543 have_address = 1;
17544 else if (tree_fits_shwi_p (loc))
17545 ret = int_loc_descriptor (tree_to_shwi (loc));
17546 else if (tree_fits_uhwi_p (loc))
17547 ret = uint_loc_descriptor (tree_to_uhwi (loc));
17548 else
17550 expansion_failed (loc, NULL_RTX,
17551 "Integer operand is not host integer");
17552 return 0;
17554 break;
17556 case CONSTRUCTOR:
17557 case REAL_CST:
17558 case STRING_CST:
17559 case COMPLEX_CST:
17560 if ((ret = cst_pool_loc_descr (loc)))
17561 have_address = 1;
17562 else if (TREE_CODE (loc) == CONSTRUCTOR)
17564 tree type = TREE_TYPE (loc);
17565 unsigned HOST_WIDE_INT size = int_size_in_bytes (type);
17566 unsigned HOST_WIDE_INT offset = 0;
17567 unsigned HOST_WIDE_INT cnt;
17568 constructor_elt *ce;
17570 if (TREE_CODE (type) == RECORD_TYPE)
17572 /* This is very limited, but it's enough to output
17573 pointers to member functions, as long as the
17574 referenced function is defined in the current
17575 translation unit. */
17576 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (loc), cnt, ce)
17578 tree val = ce->value;
17580 tree field = ce->index;
17582 if (val)
17583 STRIP_NOPS (val);
17585 if (!field || DECL_BIT_FIELD (field))
17587 expansion_failed (loc, NULL_RTX,
17588 "bitfield in record type constructor");
17589 size = offset = (unsigned HOST_WIDE_INT)-1;
17590 ret = NULL;
17591 break;
17594 HOST_WIDE_INT fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
17595 unsigned HOST_WIDE_INT pos = int_byte_position (field);
17596 gcc_assert (pos + fieldsize <= size);
17597 if (pos < offset)
17599 expansion_failed (loc, NULL_RTX,
17600 "out-of-order fields in record constructor");
17601 size = offset = (unsigned HOST_WIDE_INT)-1;
17602 ret = NULL;
17603 break;
17605 if (pos > offset)
17607 ret1 = new_loc_descr (DW_OP_piece, pos - offset, 0);
17608 add_loc_descr (&ret, ret1);
17609 offset = pos;
17611 if (val && fieldsize != 0)
17613 ret1 = loc_descriptor_from_tree (val, want_address, context);
17614 if (!ret1)
17616 expansion_failed (loc, NULL_RTX,
17617 "unsupported expression in field");
17618 size = offset = (unsigned HOST_WIDE_INT)-1;
17619 ret = NULL;
17620 break;
17622 add_loc_descr (&ret, ret1);
17624 if (fieldsize)
17626 ret1 = new_loc_descr (DW_OP_piece, fieldsize, 0);
17627 add_loc_descr (&ret, ret1);
17628 offset = pos + fieldsize;
17632 if (offset != size)
17634 ret1 = new_loc_descr (DW_OP_piece, size - offset, 0);
17635 add_loc_descr (&ret, ret1);
17636 offset = size;
17639 have_address = !!want_address;
17641 else
17642 expansion_failed (loc, NULL_RTX,
17643 "constructor of non-record type");
17645 else
17646 /* We can construct small constants here using int_loc_descriptor. */
17647 expansion_failed (loc, NULL_RTX,
17648 "constructor or constant not in constant pool");
17649 break;
17651 case TRUTH_AND_EXPR:
17652 case TRUTH_ANDIF_EXPR:
17653 case BIT_AND_EXPR:
17654 op = DW_OP_and;
17655 goto do_binop;
17657 case TRUTH_XOR_EXPR:
17658 case BIT_XOR_EXPR:
17659 op = DW_OP_xor;
17660 goto do_binop;
17662 case TRUTH_OR_EXPR:
17663 case TRUTH_ORIF_EXPR:
17664 case BIT_IOR_EXPR:
17665 op = DW_OP_or;
17666 goto do_binop;
17668 case FLOOR_DIV_EXPR:
17669 case CEIL_DIV_EXPR:
17670 case ROUND_DIV_EXPR:
17671 case TRUNC_DIV_EXPR:
17672 case EXACT_DIV_EXPR:
17673 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
17674 return 0;
17675 op = DW_OP_div;
17676 goto do_binop;
17678 case MINUS_EXPR:
17679 op = DW_OP_minus;
17680 goto do_binop;
17682 case FLOOR_MOD_EXPR:
17683 case CEIL_MOD_EXPR:
17684 case ROUND_MOD_EXPR:
17685 case TRUNC_MOD_EXPR:
17686 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
17688 op = DW_OP_mod;
17689 goto do_binop;
17691 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17692 list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
17693 if (list_ret == 0 || list_ret1 == 0)
17694 return 0;
17696 add_loc_list (&list_ret, list_ret1);
17697 if (list_ret == 0)
17698 return 0;
17699 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
17700 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
17701 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
17702 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
17703 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
17704 break;
17706 case MULT_EXPR:
17707 op = DW_OP_mul;
17708 goto do_binop;
17710 case LSHIFT_EXPR:
17711 op = DW_OP_shl;
17712 goto do_binop;
17714 case RSHIFT_EXPR:
17715 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
17716 goto do_binop;
17718 case POINTER_PLUS_EXPR:
17719 case PLUS_EXPR:
17720 do_plus:
17721 if (tree_fits_shwi_p (TREE_OPERAND (loc, 1)))
17723 /* Big unsigned numbers can fit in HOST_WIDE_INT but it may be
17724 smarter to encode their opposite. The DW_OP_plus_uconst operation
17725 takes 1 + X bytes, X being the size of the ULEB128 addend. On the
17726 other hand, a "<push literal>; DW_OP_minus" pattern takes 1 + Y
17727 bytes, Y being the size of the operation that pushes the opposite
17728 of the addend. So let's choose the smallest representation. */
17729 const tree tree_addend = TREE_OPERAND (loc, 1);
17730 offset_int wi_addend;
17731 HOST_WIDE_INT shwi_addend;
17732 dw_loc_descr_ref loc_naddend;
17734 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17735 if (list_ret == 0)
17736 return 0;
17738 /* Try to get the literal to push. It is the opposite of the addend,
17739 so as we rely on wrapping during DWARF evaluation, first decode
17740 the literal as a "DWARF-sized" signed number. */
17741 wi_addend = wi::to_offset (tree_addend);
17742 wi_addend = wi::sext (wi_addend, DWARF2_ADDR_SIZE * 8);
17743 shwi_addend = wi_addend.to_shwi ();
17744 loc_naddend = (shwi_addend != INTTYPE_MINIMUM (HOST_WIDE_INT))
17745 ? int_loc_descriptor (-shwi_addend)
17746 : NULL;
17748 if (loc_naddend != NULL
17749 && ((unsigned) size_of_uleb128 (shwi_addend)
17750 > size_of_loc_descr (loc_naddend)))
17752 add_loc_descr_to_each (list_ret, loc_naddend);
17753 add_loc_descr_to_each (list_ret,
17754 new_loc_descr (DW_OP_minus, 0, 0));
17756 else
17758 for (dw_loc_descr_ref loc_cur = loc_naddend; loc_cur != NULL; )
17760 loc_naddend = loc_cur;
17761 loc_cur = loc_cur->dw_loc_next;
17762 ggc_free (loc_naddend);
17764 loc_list_plus_const (list_ret, wi_addend.to_shwi ());
17766 break;
17769 op = DW_OP_plus;
17770 goto do_binop;
17772 case LE_EXPR:
17773 op = DW_OP_le;
17774 goto do_comp_binop;
17776 case GE_EXPR:
17777 op = DW_OP_ge;
17778 goto do_comp_binop;
17780 case LT_EXPR:
17781 op = DW_OP_lt;
17782 goto do_comp_binop;
17784 case GT_EXPR:
17785 op = DW_OP_gt;
17786 goto do_comp_binop;
17788 do_comp_binop:
17789 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
17791 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
17792 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
17793 list_ret = loc_list_from_uint_comparison (list_ret, list_ret1,
17794 TREE_CODE (loc));
17795 break;
17797 else
17798 goto do_binop;
17800 case EQ_EXPR:
17801 op = DW_OP_eq;
17802 goto do_binop;
17804 case NE_EXPR:
17805 op = DW_OP_ne;
17806 goto do_binop;
17808 do_binop:
17809 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17810 list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
17811 if (list_ret == 0 || list_ret1 == 0)
17812 return 0;
17814 add_loc_list (&list_ret, list_ret1);
17815 if (list_ret == 0)
17816 return 0;
17817 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
17818 break;
17820 case TRUTH_NOT_EXPR:
17821 case BIT_NOT_EXPR:
17822 op = DW_OP_not;
17823 goto do_unop;
17825 case ABS_EXPR:
17826 op = DW_OP_abs;
17827 goto do_unop;
17829 case NEGATE_EXPR:
17830 op = DW_OP_neg;
17831 goto do_unop;
17833 do_unop:
17834 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17835 if (list_ret == 0)
17836 return 0;
17838 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
17839 break;
17841 case MIN_EXPR:
17842 case MAX_EXPR:
17844 const enum tree_code code =
17845 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
17847 loc = build3 (COND_EXPR, TREE_TYPE (loc),
17848 build2 (code, integer_type_node,
17849 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
17850 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
17853 /* fall through */
17855 case COND_EXPR:
17857 dw_loc_descr_ref lhs
17858 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0, context);
17859 dw_loc_list_ref rhs
17860 = loc_list_from_tree_1 (TREE_OPERAND (loc, 2), 0, context);
17861 dw_loc_descr_ref bra_node, jump_node, tmp;
17863 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17864 if (list_ret == 0 || lhs == 0 || rhs == 0)
17865 return 0;
17867 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
17868 add_loc_descr_to_each (list_ret, bra_node);
17870 add_loc_list (&list_ret, rhs);
17871 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
17872 add_loc_descr_to_each (list_ret, jump_node);
17874 add_loc_descr_to_each (list_ret, lhs);
17875 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
17876 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
17878 /* ??? Need a node to point the skip at. Use a nop. */
17879 tmp = new_loc_descr (DW_OP_nop, 0, 0);
17880 add_loc_descr_to_each (list_ret, tmp);
17881 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
17882 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
17884 break;
17886 case FIX_TRUNC_EXPR:
17887 return 0;
17889 default:
17890 /* Leave front-end specific codes as simply unknown. This comes
17891 up, for instance, with the C STMT_EXPR. */
17892 if ((unsigned int) TREE_CODE (loc)
17893 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
17895 expansion_failed (loc, NULL_RTX,
17896 "language specific tree node");
17897 return 0;
17900 /* Otherwise this is a generic code; we should just lists all of
17901 these explicitly. We forgot one. */
17902 if (flag_checking)
17903 gcc_unreachable ();
17905 /* In a release build, we want to degrade gracefully: better to
17906 generate incomplete debugging information than to crash. */
17907 return NULL;
17910 if (!ret && !list_ret)
17911 return 0;
17913 if (want_address == 2 && !have_address
17914 && (dwarf_version >= 4 || !dwarf_strict))
17916 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
17918 expansion_failed (loc, NULL_RTX,
17919 "DWARF address size mismatch");
17920 return 0;
17922 if (ret)
17923 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
17924 else
17925 add_loc_descr_to_each (list_ret,
17926 new_loc_descr (DW_OP_stack_value, 0, 0));
17927 have_address = 1;
17929 /* Show if we can't fill the request for an address. */
17930 if (want_address && !have_address)
17932 expansion_failed (loc, NULL_RTX,
17933 "Want address and only have value");
17934 return 0;
17937 gcc_assert (!ret || !list_ret);
17939 /* If we've got an address and don't want one, dereference. */
17940 if (!want_address && have_address)
17942 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
17944 if (size > DWARF2_ADDR_SIZE || size == -1)
17946 expansion_failed (loc, NULL_RTX,
17947 "DWARF address size mismatch");
17948 return 0;
17950 else if (size == DWARF2_ADDR_SIZE)
17951 op = DW_OP_deref;
17952 else
17953 op = DW_OP_deref_size;
17955 if (ret)
17956 add_loc_descr (&ret, new_loc_descr (op, size, 0));
17957 else
17958 add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
17960 if (ret)
17961 list_ret = new_loc_list (ret, NULL, NULL, NULL);
17963 return list_ret;
17966 /* Likewise, but strip useless DW_OP_nop operations in the resulting
17967 expressions. */
17969 static dw_loc_list_ref
17970 loc_list_from_tree (tree loc, int want_address,
17971 struct loc_descr_context *context)
17973 dw_loc_list_ref result = loc_list_from_tree_1 (loc, want_address, context);
17975 for (dw_loc_list_ref loc_cur = result;
17976 loc_cur != NULL; loc_cur = loc_cur->dw_loc_next)
17977 loc_descr_without_nops (loc_cur->expr);
17978 return result;
17981 /* Same as above but return only single location expression. */
17982 static dw_loc_descr_ref
17983 loc_descriptor_from_tree (tree loc, int want_address,
17984 struct loc_descr_context *context)
17986 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address, context);
17987 if (!ret)
17988 return NULL;
17989 if (ret->dw_loc_next)
17991 expansion_failed (loc, NULL_RTX,
17992 "Location list where only loc descriptor needed");
17993 return NULL;
17995 return ret->expr;
17998 /* Given a value, round it up to the lowest multiple of `boundary'
17999 which is not less than the value itself. */
18001 static inline HOST_WIDE_INT
18002 ceiling (HOST_WIDE_INT value, unsigned int boundary)
18004 return (((value + boundary - 1) / boundary) * boundary);
18007 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
18008 pointer to the declared type for the relevant field variable, or return
18009 `integer_type_node' if the given node turns out to be an
18010 ERROR_MARK node. */
18012 static inline tree
18013 field_type (const_tree decl)
18015 tree type;
18017 if (TREE_CODE (decl) == ERROR_MARK)
18018 return integer_type_node;
18020 type = DECL_BIT_FIELD_TYPE (decl);
18021 if (type == NULL_TREE)
18022 type = TREE_TYPE (decl);
18024 return type;
18027 /* Given a pointer to a tree node, return the alignment in bits for
18028 it, or else return BITS_PER_WORD if the node actually turns out to
18029 be an ERROR_MARK node. */
18031 static inline unsigned
18032 simple_type_align_in_bits (const_tree type)
18034 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
18037 static inline unsigned
18038 simple_decl_align_in_bits (const_tree decl)
18040 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
18043 /* Return the result of rounding T up to ALIGN. */
18045 static inline offset_int
18046 round_up_to_align (const offset_int &t, unsigned int align)
18048 return wi::udiv_trunc (t + align - 1, align) * align;
18051 /* Compute the size of TYPE in bytes. If possible, return NULL and store the
18052 size as an integer constant in CST_SIZE. Otherwise, if possible, return a
18053 DWARF expression that computes the size. Return NULL and set CST_SIZE to -1
18054 if we fail to return the size in one of these two forms. */
18056 static dw_loc_descr_ref
18057 type_byte_size (const_tree type, HOST_WIDE_INT *cst_size)
18059 tree tree_size;
18060 struct loc_descr_context ctx;
18062 /* Return a constant integer in priority, if possible. */
18063 *cst_size = int_size_in_bytes (type);
18064 if (*cst_size != -1)
18065 return NULL;
18067 ctx.context_type = const_cast<tree> (type);
18068 ctx.base_decl = NULL_TREE;
18069 ctx.dpi = NULL;
18070 ctx.placeholder_arg = false;
18071 ctx.placeholder_seen = false;
18073 type = TYPE_MAIN_VARIANT (type);
18074 tree_size = TYPE_SIZE_UNIT (type);
18075 return ((tree_size != NULL_TREE)
18076 ? loc_descriptor_from_tree (tree_size, 0, &ctx)
18077 : NULL);
18080 /* Helper structure for RECORD_TYPE processing. */
18081 struct vlr_context
18083 /* Root RECORD_TYPE. It is needed to generate data member location
18084 descriptions in variable-length records (VLR), but also to cope with
18085 variants, which are composed of nested structures multiplexed with
18086 QUAL_UNION_TYPE nodes. Each time such a structure is passed to a
18087 function processing a FIELD_DECL, it is required to be non null. */
18088 tree struct_type;
18089 /* When generating a variant part in a RECORD_TYPE (i.e. a nested
18090 QUAL_UNION_TYPE), this holds an expression that computes the offset for
18091 this variant part as part of the root record (in storage units). For
18092 regular records, it must be NULL_TREE. */
18093 tree variant_part_offset;
18096 /* Given a pointer to a FIELD_DECL, compute the byte offset of the lowest
18097 addressed byte of the "containing object" for the given FIELD_DECL. If
18098 possible, return a native constant through CST_OFFSET (in which case NULL is
18099 returned); otherwise return a DWARF expression that computes the offset.
18101 Set *CST_OFFSET to 0 and return NULL if we are unable to determine what
18102 that offset is, either because the argument turns out to be a pointer to an
18103 ERROR_MARK node, or because the offset expression is too complex for us.
18105 CTX is required: see the comment for VLR_CONTEXT. */
18107 static dw_loc_descr_ref
18108 field_byte_offset (const_tree decl, struct vlr_context *ctx,
18109 HOST_WIDE_INT *cst_offset)
18111 tree tree_result;
18112 dw_loc_list_ref loc_result;
18114 *cst_offset = 0;
18116 if (TREE_CODE (decl) == ERROR_MARK)
18117 return NULL;
18118 else
18119 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
18121 /* We cannot handle variable bit offsets at the moment, so abort if it's the
18122 case. */
18123 if (TREE_CODE (DECL_FIELD_BIT_OFFSET (decl)) != INTEGER_CST)
18124 return NULL;
18126 #ifdef PCC_BITFIELD_TYPE_MATTERS
18127 /* We used to handle only constant offsets in all cases. Now, we handle
18128 properly dynamic byte offsets only when PCC bitfield type doesn't
18129 matter. */
18130 if (PCC_BITFIELD_TYPE_MATTERS
18131 && TREE_CODE (DECL_FIELD_OFFSET (decl)) == INTEGER_CST)
18133 offset_int object_offset_in_bits;
18134 offset_int object_offset_in_bytes;
18135 offset_int bitpos_int;
18136 tree type;
18137 tree field_size_tree;
18138 offset_int deepest_bitpos;
18139 offset_int field_size_in_bits;
18140 unsigned int type_align_in_bits;
18141 unsigned int decl_align_in_bits;
18142 offset_int type_size_in_bits;
18144 bitpos_int = wi::to_offset (bit_position (decl));
18145 type = field_type (decl);
18146 type_size_in_bits = offset_int_type_size_in_bits (type);
18147 type_align_in_bits = simple_type_align_in_bits (type);
18149 field_size_tree = DECL_SIZE (decl);
18151 /* The size could be unspecified if there was an error, or for
18152 a flexible array member. */
18153 if (!field_size_tree)
18154 field_size_tree = bitsize_zero_node;
18156 /* If the size of the field is not constant, use the type size. */
18157 if (TREE_CODE (field_size_tree) == INTEGER_CST)
18158 field_size_in_bits = wi::to_offset (field_size_tree);
18159 else
18160 field_size_in_bits = type_size_in_bits;
18162 decl_align_in_bits = simple_decl_align_in_bits (decl);
18164 /* The GCC front-end doesn't make any attempt to keep track of the
18165 starting bit offset (relative to the start of the containing
18166 structure type) of the hypothetical "containing object" for a
18167 bit-field. Thus, when computing the byte offset value for the
18168 start of the "containing object" of a bit-field, we must deduce
18169 this information on our own. This can be rather tricky to do in
18170 some cases. For example, handling the following structure type
18171 definition when compiling for an i386/i486 target (which only
18172 aligns long long's to 32-bit boundaries) can be very tricky:
18174 struct S { int field1; long long field2:31; };
18176 Fortunately, there is a simple rule-of-thumb which can be used
18177 in such cases. When compiling for an i386/i486, GCC will
18178 allocate 8 bytes for the structure shown above. It decides to
18179 do this based upon one simple rule for bit-field allocation.
18180 GCC allocates each "containing object" for each bit-field at
18181 the first (i.e. lowest addressed) legitimate alignment boundary
18182 (based upon the required minimum alignment for the declared
18183 type of the field) which it can possibly use, subject to the
18184 condition that there is still enough available space remaining
18185 in the containing object (when allocated at the selected point)
18186 to fully accommodate all of the bits of the bit-field itself.
18188 This simple rule makes it obvious why GCC allocates 8 bytes for
18189 each object of the structure type shown above. When looking
18190 for a place to allocate the "containing object" for `field2',
18191 the compiler simply tries to allocate a 64-bit "containing
18192 object" at each successive 32-bit boundary (starting at zero)
18193 until it finds a place to allocate that 64- bit field such that
18194 at least 31 contiguous (and previously unallocated) bits remain
18195 within that selected 64 bit field. (As it turns out, for the
18196 example above, the compiler finds it is OK to allocate the
18197 "containing object" 64-bit field at bit-offset zero within the
18198 structure type.)
18200 Here we attempt to work backwards from the limited set of facts
18201 we're given, and we try to deduce from those facts, where GCC
18202 must have believed that the containing object started (within
18203 the structure type). The value we deduce is then used (by the
18204 callers of this routine) to generate DW_AT_location and
18205 DW_AT_bit_offset attributes for fields (both bit-fields and, in
18206 the case of DW_AT_location, regular fields as well). */
18208 /* Figure out the bit-distance from the start of the structure to
18209 the "deepest" bit of the bit-field. */
18210 deepest_bitpos = bitpos_int + field_size_in_bits;
18212 /* This is the tricky part. Use some fancy footwork to deduce
18213 where the lowest addressed bit of the containing object must
18214 be. */
18215 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
18217 /* Round up to type_align by default. This works best for
18218 bitfields. */
18219 object_offset_in_bits
18220 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
18222 if (wi::gtu_p (object_offset_in_bits, bitpos_int))
18224 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
18226 /* Round up to decl_align instead. */
18227 object_offset_in_bits
18228 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
18231 object_offset_in_bytes
18232 = wi::lrshift (object_offset_in_bits, LOG2_BITS_PER_UNIT);
18233 if (ctx->variant_part_offset == NULL_TREE)
18235 *cst_offset = object_offset_in_bytes.to_shwi ();
18236 return NULL;
18238 tree_result = wide_int_to_tree (sizetype, object_offset_in_bytes);
18240 else
18241 #endif /* PCC_BITFIELD_TYPE_MATTERS */
18242 tree_result = byte_position (decl);
18244 if (ctx->variant_part_offset != NULL_TREE)
18245 tree_result = fold_build2 (PLUS_EXPR, TREE_TYPE (tree_result),
18246 ctx->variant_part_offset, tree_result);
18248 /* If the byte offset is a constant, it's simplier to handle a native
18249 constant rather than a DWARF expression. */
18250 if (TREE_CODE (tree_result) == INTEGER_CST)
18252 *cst_offset = wi::to_offset (tree_result).to_shwi ();
18253 return NULL;
18255 struct loc_descr_context loc_ctx = {
18256 ctx->struct_type, /* context_type */
18257 NULL_TREE, /* base_decl */
18258 NULL, /* dpi */
18259 false, /* placeholder_arg */
18260 false /* placeholder_seen */
18262 loc_result = loc_list_from_tree (tree_result, 0, &loc_ctx);
18264 /* We want a DWARF expression: abort if we only have a location list with
18265 multiple elements. */
18266 if (!loc_result || !single_element_loc_list_p (loc_result))
18267 return NULL;
18268 else
18269 return loc_result->expr;
18272 /* The following routines define various Dwarf attributes and any data
18273 associated with them. */
18275 /* Add a location description attribute value to a DIE.
18277 This emits location attributes suitable for whole variables and
18278 whole parameters. Note that the location attributes for struct fields are
18279 generated by the routine `data_member_location_attribute' below. */
18281 static inline void
18282 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
18283 dw_loc_list_ref descr)
18285 if (descr == 0)
18286 return;
18287 if (single_element_loc_list_p (descr))
18288 add_AT_loc (die, attr_kind, descr->expr);
18289 else
18290 add_AT_loc_list (die, attr_kind, descr);
18293 /* Add DW_AT_accessibility attribute to DIE if needed. */
18295 static void
18296 add_accessibility_attribute (dw_die_ref die, tree decl)
18298 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
18299 children, otherwise the default is DW_ACCESS_public. In DWARF2
18300 the default has always been DW_ACCESS_public. */
18301 if (TREE_PROTECTED (decl))
18302 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
18303 else if (TREE_PRIVATE (decl))
18305 if (dwarf_version == 2
18306 || die->die_parent == NULL
18307 || die->die_parent->die_tag != DW_TAG_class_type)
18308 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
18310 else if (dwarf_version > 2
18311 && die->die_parent
18312 && die->die_parent->die_tag == DW_TAG_class_type)
18313 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
18316 /* Attach the specialized form of location attribute used for data members of
18317 struct and union types. In the special case of a FIELD_DECL node which
18318 represents a bit-field, the "offset" part of this special location
18319 descriptor must indicate the distance in bytes from the lowest-addressed
18320 byte of the containing struct or union type to the lowest-addressed byte of
18321 the "containing object" for the bit-field. (See the `field_byte_offset'
18322 function above).
18324 For any given bit-field, the "containing object" is a hypothetical object
18325 (of some integral or enum type) within which the given bit-field lives. The
18326 type of this hypothetical "containing object" is always the same as the
18327 declared type of the individual bit-field itself (for GCC anyway... the
18328 DWARF spec doesn't actually mandate this). Note that it is the size (in
18329 bytes) of the hypothetical "containing object" which will be given in the
18330 DW_AT_byte_size attribute for this bit-field. (See the
18331 `byte_size_attribute' function below.) It is also used when calculating the
18332 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
18333 function below.)
18335 CTX is required: see the comment for VLR_CONTEXT. */
18337 static void
18338 add_data_member_location_attribute (dw_die_ref die,
18339 tree decl,
18340 struct vlr_context *ctx)
18342 HOST_WIDE_INT offset;
18343 dw_loc_descr_ref loc_descr = 0;
18345 if (TREE_CODE (decl) == TREE_BINFO)
18347 /* We're working on the TAG_inheritance for a base class. */
18348 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
18350 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
18351 aren't at a fixed offset from all (sub)objects of the same
18352 type. We need to extract the appropriate offset from our
18353 vtable. The following dwarf expression means
18355 BaseAddr = ObAddr + *((*ObAddr) - Offset)
18357 This is specific to the V3 ABI, of course. */
18359 dw_loc_descr_ref tmp;
18361 /* Make a copy of the object address. */
18362 tmp = new_loc_descr (DW_OP_dup, 0, 0);
18363 add_loc_descr (&loc_descr, tmp);
18365 /* Extract the vtable address. */
18366 tmp = new_loc_descr (DW_OP_deref, 0, 0);
18367 add_loc_descr (&loc_descr, tmp);
18369 /* Calculate the address of the offset. */
18370 offset = tree_to_shwi (BINFO_VPTR_FIELD (decl));
18371 gcc_assert (offset < 0);
18373 tmp = int_loc_descriptor (-offset);
18374 add_loc_descr (&loc_descr, tmp);
18375 tmp = new_loc_descr (DW_OP_minus, 0, 0);
18376 add_loc_descr (&loc_descr, tmp);
18378 /* Extract the offset. */
18379 tmp = new_loc_descr (DW_OP_deref, 0, 0);
18380 add_loc_descr (&loc_descr, tmp);
18382 /* Add it to the object address. */
18383 tmp = new_loc_descr (DW_OP_plus, 0, 0);
18384 add_loc_descr (&loc_descr, tmp);
18386 else
18387 offset = tree_to_shwi (BINFO_OFFSET (decl));
18389 else
18391 loc_descr = field_byte_offset (decl, ctx, &offset);
18393 /* If loc_descr is available then we know the field offset is dynamic.
18394 However, GDB does not handle dynamic field offsets very well at the
18395 moment. */
18396 if (loc_descr != NULL && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL)
18398 loc_descr = NULL;
18399 offset = 0;
18402 /* Data member location evalutation starts with the base address on the
18403 stack. Compute the field offset and add it to this base address. */
18404 else if (loc_descr != NULL)
18405 add_loc_descr (&loc_descr, new_loc_descr (DW_OP_plus, 0, 0));
18408 if (! loc_descr)
18410 /* While DW_AT_data_bit_offset has been added already in DWARF4,
18411 e.g. GDB only added support to it in November 2016. For DWARF5
18412 we need newer debug info consumers anyway. We might change this
18413 to dwarf_version >= 4 once most consumers catched up. */
18414 if (dwarf_version >= 5
18415 && TREE_CODE (decl) == FIELD_DECL
18416 && DECL_BIT_FIELD_TYPE (decl))
18418 tree off = bit_position (decl);
18419 if (tree_fits_uhwi_p (off) && get_AT (die, DW_AT_bit_size))
18421 remove_AT (die, DW_AT_byte_size);
18422 remove_AT (die, DW_AT_bit_offset);
18423 add_AT_unsigned (die, DW_AT_data_bit_offset, tree_to_uhwi (off));
18424 return;
18427 if (dwarf_version > 2)
18429 /* Don't need to output a location expression, just the constant. */
18430 if (offset < 0)
18431 add_AT_int (die, DW_AT_data_member_location, offset);
18432 else
18433 add_AT_unsigned (die, DW_AT_data_member_location, offset);
18434 return;
18436 else
18438 enum dwarf_location_atom op;
18440 /* The DWARF2 standard says that we should assume that the structure
18441 address is already on the stack, so we can specify a structure
18442 field address by using DW_OP_plus_uconst. */
18443 op = DW_OP_plus_uconst;
18444 loc_descr = new_loc_descr (op, offset, 0);
18448 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
18451 /* Writes integer values to dw_vec_const array. */
18453 static void
18454 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
18456 while (size != 0)
18458 *dest++ = val & 0xff;
18459 val >>= 8;
18460 --size;
18464 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
18466 static HOST_WIDE_INT
18467 extract_int (const unsigned char *src, unsigned int size)
18469 HOST_WIDE_INT val = 0;
18471 src += size;
18472 while (size != 0)
18474 val <<= 8;
18475 val |= *--src & 0xff;
18476 --size;
18478 return val;
18481 /* Writes wide_int values to dw_vec_const array. */
18483 static void
18484 insert_wide_int (const wide_int &val, unsigned char *dest, int elt_size)
18486 int i;
18488 if (elt_size <= HOST_BITS_PER_WIDE_INT/BITS_PER_UNIT)
18490 insert_int ((HOST_WIDE_INT) val.elt (0), elt_size, dest);
18491 return;
18494 /* We'd have to extend this code to support odd sizes. */
18495 gcc_assert (elt_size % (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT) == 0);
18497 int n = elt_size / (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
18499 if (WORDS_BIG_ENDIAN)
18500 for (i = n - 1; i >= 0; i--)
18502 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
18503 dest += sizeof (HOST_WIDE_INT);
18505 else
18506 for (i = 0; i < n; i++)
18508 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
18509 dest += sizeof (HOST_WIDE_INT);
18513 /* Writes floating point values to dw_vec_const array. */
18515 static void
18516 insert_float (const_rtx rtl, unsigned char *array)
18518 long val[4];
18519 int i;
18521 real_to_target (val, CONST_DOUBLE_REAL_VALUE (rtl), GET_MODE (rtl));
18523 /* real_to_target puts 32-bit pieces in each long. Pack them. */
18524 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
18526 insert_int (val[i], 4, array);
18527 array += 4;
18531 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
18532 does not have a "location" either in memory or in a register. These
18533 things can arise in GNU C when a constant is passed as an actual parameter
18534 to an inlined function. They can also arise in C++ where declared
18535 constants do not necessarily get memory "homes". */
18537 static bool
18538 add_const_value_attribute (dw_die_ref die, rtx rtl)
18540 switch (GET_CODE (rtl))
18542 case CONST_INT:
18544 HOST_WIDE_INT val = INTVAL (rtl);
18546 if (val < 0)
18547 add_AT_int (die, DW_AT_const_value, val);
18548 else
18549 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
18551 return true;
18553 case CONST_WIDE_INT:
18555 wide_int w1 = rtx_mode_t (rtl, MAX_MODE_INT);
18556 unsigned int prec = MIN (wi::min_precision (w1, UNSIGNED),
18557 (unsigned int)CONST_WIDE_INT_NUNITS (rtl) * HOST_BITS_PER_WIDE_INT);
18558 wide_int w = wi::zext (w1, prec);
18559 add_AT_wide (die, DW_AT_const_value, w);
18561 return true;
18563 case CONST_DOUBLE:
18564 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
18565 floating-point constant. A CONST_DOUBLE is used whenever the
18566 constant requires more than one word in order to be adequately
18567 represented. */
18569 machine_mode mode = GET_MODE (rtl);
18571 if (TARGET_SUPPORTS_WIDE_INT == 0 && !SCALAR_FLOAT_MODE_P (mode))
18572 add_AT_double (die, DW_AT_const_value,
18573 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
18574 else
18576 unsigned int length = GET_MODE_SIZE (mode);
18577 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
18579 insert_float (rtl, array);
18580 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
18583 return true;
18585 case CONST_VECTOR:
18587 machine_mode mode = GET_MODE (rtl);
18588 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
18589 unsigned int length = CONST_VECTOR_NUNITS (rtl);
18590 unsigned char *array
18591 = ggc_vec_alloc<unsigned char> (length * elt_size);
18592 unsigned int i;
18593 unsigned char *p;
18594 machine_mode imode = GET_MODE_INNER (mode);
18596 switch (GET_MODE_CLASS (mode))
18598 case MODE_VECTOR_INT:
18599 for (i = 0, p = array; i < length; i++, p += elt_size)
18601 rtx elt = CONST_VECTOR_ELT (rtl, i);
18602 insert_wide_int (rtx_mode_t (elt, imode), p, elt_size);
18604 break;
18606 case MODE_VECTOR_FLOAT:
18607 for (i = 0, p = array; i < length; i++, p += elt_size)
18609 rtx elt = CONST_VECTOR_ELT (rtl, i);
18610 insert_float (elt, p);
18612 break;
18614 default:
18615 gcc_unreachable ();
18618 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
18620 return true;
18622 case CONST_STRING:
18623 if (dwarf_version >= 4 || !dwarf_strict)
18625 dw_loc_descr_ref loc_result;
18626 resolve_one_addr (&rtl);
18627 rtl_addr:
18628 loc_result = new_addr_loc_descr (rtl, dtprel_false);
18629 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
18630 add_AT_loc (die, DW_AT_location, loc_result);
18631 vec_safe_push (used_rtx_array, rtl);
18632 return true;
18634 return false;
18636 case CONST:
18637 if (CONSTANT_P (XEXP (rtl, 0)))
18638 return add_const_value_attribute (die, XEXP (rtl, 0));
18639 /* FALLTHROUGH */
18640 case SYMBOL_REF:
18641 if (!const_ok_for_output (rtl))
18642 return false;
18643 /* FALLTHROUGH */
18644 case LABEL_REF:
18645 if (dwarf_version >= 4 || !dwarf_strict)
18646 goto rtl_addr;
18647 return false;
18649 case PLUS:
18650 /* In cases where an inlined instance of an inline function is passed
18651 the address of an `auto' variable (which is local to the caller) we
18652 can get a situation where the DECL_RTL of the artificial local
18653 variable (for the inlining) which acts as a stand-in for the
18654 corresponding formal parameter (of the inline function) will look
18655 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
18656 exactly a compile-time constant expression, but it isn't the address
18657 of the (artificial) local variable either. Rather, it represents the
18658 *value* which the artificial local variable always has during its
18659 lifetime. We currently have no way to represent such quasi-constant
18660 values in Dwarf, so for now we just punt and generate nothing. */
18661 return false;
18663 case HIGH:
18664 case CONST_FIXED:
18665 return false;
18667 case MEM:
18668 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
18669 && MEM_READONLY_P (rtl)
18670 && GET_MODE (rtl) == BLKmode)
18672 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
18673 return true;
18675 return false;
18677 default:
18678 /* No other kinds of rtx should be possible here. */
18679 gcc_unreachable ();
18681 return false;
18684 /* Determine whether the evaluation of EXPR references any variables
18685 or functions which aren't otherwise used (and therefore may not be
18686 output). */
18687 static tree
18688 reference_to_unused (tree * tp, int * walk_subtrees,
18689 void * data ATTRIBUTE_UNUSED)
18691 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
18692 *walk_subtrees = 0;
18694 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
18695 && ! TREE_ASM_WRITTEN (*tp))
18696 return *tp;
18697 /* ??? The C++ FE emits debug information for using decls, so
18698 putting gcc_unreachable here falls over. See PR31899. For now
18699 be conservative. */
18700 else if (!symtab->global_info_ready && VAR_OR_FUNCTION_DECL_P (*tp))
18701 return *tp;
18702 else if (VAR_P (*tp))
18704 varpool_node *node = varpool_node::get (*tp);
18705 if (!node || !node->definition)
18706 return *tp;
18708 else if (TREE_CODE (*tp) == FUNCTION_DECL
18709 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
18711 /* The call graph machinery must have finished analyzing,
18712 optimizing and gimplifying the CU by now.
18713 So if *TP has no call graph node associated
18714 to it, it means *TP will not be emitted. */
18715 if (!cgraph_node::get (*tp))
18716 return *tp;
18718 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
18719 return *tp;
18721 return NULL_TREE;
18724 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
18725 for use in a later add_const_value_attribute call. */
18727 static rtx
18728 rtl_for_decl_init (tree init, tree type)
18730 rtx rtl = NULL_RTX;
18732 STRIP_NOPS (init);
18734 /* If a variable is initialized with a string constant without embedded
18735 zeros, build CONST_STRING. */
18736 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
18738 tree enttype = TREE_TYPE (type);
18739 tree domain = TYPE_DOMAIN (type);
18740 machine_mode mode = TYPE_MODE (enttype);
18742 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
18743 && domain
18744 && integer_zerop (TYPE_MIN_VALUE (domain))
18745 && compare_tree_int (TYPE_MAX_VALUE (domain),
18746 TREE_STRING_LENGTH (init) - 1) == 0
18747 && ((size_t) TREE_STRING_LENGTH (init)
18748 == strlen (TREE_STRING_POINTER (init)) + 1))
18750 rtl = gen_rtx_CONST_STRING (VOIDmode,
18751 ggc_strdup (TREE_STRING_POINTER (init)));
18752 rtl = gen_rtx_MEM (BLKmode, rtl);
18753 MEM_READONLY_P (rtl) = 1;
18756 /* Other aggregates, and complex values, could be represented using
18757 CONCAT: FIXME! */
18758 else if (AGGREGATE_TYPE_P (type)
18759 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
18760 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
18761 || TREE_CODE (type) == COMPLEX_TYPE)
18763 /* Vectors only work if their mode is supported by the target.
18764 FIXME: generic vectors ought to work too. */
18765 else if (TREE_CODE (type) == VECTOR_TYPE
18766 && !VECTOR_MODE_P (TYPE_MODE (type)))
18768 /* If the initializer is something that we know will expand into an
18769 immediate RTL constant, expand it now. We must be careful not to
18770 reference variables which won't be output. */
18771 else if (initializer_constant_valid_p (init, type)
18772 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
18774 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
18775 possible. */
18776 if (TREE_CODE (type) == VECTOR_TYPE)
18777 switch (TREE_CODE (init))
18779 case VECTOR_CST:
18780 break;
18781 case CONSTRUCTOR:
18782 if (TREE_CONSTANT (init))
18784 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
18785 bool constant_p = true;
18786 tree value;
18787 unsigned HOST_WIDE_INT ix;
18789 /* Even when ctor is constant, it might contain non-*_CST
18790 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
18791 belong into VECTOR_CST nodes. */
18792 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
18793 if (!CONSTANT_CLASS_P (value))
18795 constant_p = false;
18796 break;
18799 if (constant_p)
18801 init = build_vector_from_ctor (type, elts);
18802 break;
18805 /* FALLTHRU */
18807 default:
18808 return NULL;
18811 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
18813 /* If expand_expr returns a MEM, it wasn't immediate. */
18814 gcc_assert (!rtl || !MEM_P (rtl));
18817 return rtl;
18820 /* Generate RTL for the variable DECL to represent its location. */
18822 static rtx
18823 rtl_for_decl_location (tree decl)
18825 rtx rtl;
18827 /* Here we have to decide where we are going to say the parameter "lives"
18828 (as far as the debugger is concerned). We only have a couple of
18829 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
18831 DECL_RTL normally indicates where the parameter lives during most of the
18832 activation of the function. If optimization is enabled however, this
18833 could be either NULL or else a pseudo-reg. Both of those cases indicate
18834 that the parameter doesn't really live anywhere (as far as the code
18835 generation parts of GCC are concerned) during most of the function's
18836 activation. That will happen (for example) if the parameter is never
18837 referenced within the function.
18839 We could just generate a location descriptor here for all non-NULL
18840 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
18841 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
18842 where DECL_RTL is NULL or is a pseudo-reg.
18844 Note however that we can only get away with using DECL_INCOMING_RTL as
18845 a backup substitute for DECL_RTL in certain limited cases. In cases
18846 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
18847 we can be sure that the parameter was passed using the same type as it is
18848 declared to have within the function, and that its DECL_INCOMING_RTL
18849 points us to a place where a value of that type is passed.
18851 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
18852 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
18853 because in these cases DECL_INCOMING_RTL points us to a value of some
18854 type which is *different* from the type of the parameter itself. Thus,
18855 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
18856 such cases, the debugger would end up (for example) trying to fetch a
18857 `float' from a place which actually contains the first part of a
18858 `double'. That would lead to really incorrect and confusing
18859 output at debug-time.
18861 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
18862 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
18863 are a couple of exceptions however. On little-endian machines we can
18864 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
18865 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
18866 an integral type that is smaller than TREE_TYPE (decl). These cases arise
18867 when (on a little-endian machine) a non-prototyped function has a
18868 parameter declared to be of type `short' or `char'. In such cases,
18869 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
18870 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
18871 passed `int' value. If the debugger then uses that address to fetch
18872 a `short' or a `char' (on a little-endian machine) the result will be
18873 the correct data, so we allow for such exceptional cases below.
18875 Note that our goal here is to describe the place where the given formal
18876 parameter lives during most of the function's activation (i.e. between the
18877 end of the prologue and the start of the epilogue). We'll do that as best
18878 as we can. Note however that if the given formal parameter is modified
18879 sometime during the execution of the function, then a stack backtrace (at
18880 debug-time) will show the function as having been called with the *new*
18881 value rather than the value which was originally passed in. This happens
18882 rarely enough that it is not a major problem, but it *is* a problem, and
18883 I'd like to fix it.
18885 A future version of dwarf2out.c may generate two additional attributes for
18886 any given DW_TAG_formal_parameter DIE which will describe the "passed
18887 type" and the "passed location" for the given formal parameter in addition
18888 to the attributes we now generate to indicate the "declared type" and the
18889 "active location" for each parameter. This additional set of attributes
18890 could be used by debuggers for stack backtraces. Separately, note that
18891 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
18892 This happens (for example) for inlined-instances of inline function formal
18893 parameters which are never referenced. This really shouldn't be
18894 happening. All PARM_DECL nodes should get valid non-NULL
18895 DECL_INCOMING_RTL values. FIXME. */
18897 /* Use DECL_RTL as the "location" unless we find something better. */
18898 rtl = DECL_RTL_IF_SET (decl);
18900 /* When generating abstract instances, ignore everything except
18901 constants, symbols living in memory, and symbols living in
18902 fixed registers. */
18903 if (! reload_completed)
18905 if (rtl
18906 && (CONSTANT_P (rtl)
18907 || (MEM_P (rtl)
18908 && CONSTANT_P (XEXP (rtl, 0)))
18909 || (REG_P (rtl)
18910 && VAR_P (decl)
18911 && TREE_STATIC (decl))))
18913 rtl = targetm.delegitimize_address (rtl);
18914 return rtl;
18916 rtl = NULL_RTX;
18918 else if (TREE_CODE (decl) == PARM_DECL)
18920 if (rtl == NULL_RTX
18921 || is_pseudo_reg (rtl)
18922 || (MEM_P (rtl)
18923 && is_pseudo_reg (XEXP (rtl, 0))
18924 && DECL_INCOMING_RTL (decl)
18925 && MEM_P (DECL_INCOMING_RTL (decl))
18926 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
18928 tree declared_type = TREE_TYPE (decl);
18929 tree passed_type = DECL_ARG_TYPE (decl);
18930 machine_mode dmode = TYPE_MODE (declared_type);
18931 machine_mode pmode = TYPE_MODE (passed_type);
18933 /* This decl represents a formal parameter which was optimized out.
18934 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
18935 all cases where (rtl == NULL_RTX) just below. */
18936 if (dmode == pmode)
18937 rtl = DECL_INCOMING_RTL (decl);
18938 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
18939 && SCALAR_INT_MODE_P (dmode)
18940 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
18941 && DECL_INCOMING_RTL (decl))
18943 rtx inc = DECL_INCOMING_RTL (decl);
18944 if (REG_P (inc))
18945 rtl = inc;
18946 else if (MEM_P (inc))
18948 if (BYTES_BIG_ENDIAN)
18949 rtl = adjust_address_nv (inc, dmode,
18950 GET_MODE_SIZE (pmode)
18951 - GET_MODE_SIZE (dmode));
18952 else
18953 rtl = inc;
18958 /* If the parm was passed in registers, but lives on the stack, then
18959 make a big endian correction if the mode of the type of the
18960 parameter is not the same as the mode of the rtl. */
18961 /* ??? This is the same series of checks that are made in dbxout.c before
18962 we reach the big endian correction code there. It isn't clear if all
18963 of these checks are necessary here, but keeping them all is the safe
18964 thing to do. */
18965 else if (MEM_P (rtl)
18966 && XEXP (rtl, 0) != const0_rtx
18967 && ! CONSTANT_P (XEXP (rtl, 0))
18968 /* Not passed in memory. */
18969 && !MEM_P (DECL_INCOMING_RTL (decl))
18970 /* Not passed by invisible reference. */
18971 && (!REG_P (XEXP (rtl, 0))
18972 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
18973 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
18974 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
18975 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
18976 #endif
18978 /* Big endian correction check. */
18979 && BYTES_BIG_ENDIAN
18980 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
18981 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
18982 < UNITS_PER_WORD))
18984 machine_mode addr_mode = get_address_mode (rtl);
18985 int offset = (UNITS_PER_WORD
18986 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
18988 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
18989 plus_constant (addr_mode, XEXP (rtl, 0), offset));
18992 else if (VAR_P (decl)
18993 && rtl
18994 && MEM_P (rtl)
18995 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
18996 && BYTES_BIG_ENDIAN)
18998 machine_mode addr_mode = get_address_mode (rtl);
18999 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
19000 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
19002 /* If a variable is declared "register" yet is smaller than
19003 a register, then if we store the variable to memory, it
19004 looks like we're storing a register-sized value, when in
19005 fact we are not. We need to adjust the offset of the
19006 storage location to reflect the actual value's bytes,
19007 else gdb will not be able to display it. */
19008 if (rsize > dsize)
19009 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
19010 plus_constant (addr_mode, XEXP (rtl, 0),
19011 rsize - dsize));
19014 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
19015 and will have been substituted directly into all expressions that use it.
19016 C does not have such a concept, but C++ and other languages do. */
19017 if (!rtl && VAR_P (decl) && DECL_INITIAL (decl))
19018 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
19020 if (rtl)
19021 rtl = targetm.delegitimize_address (rtl);
19023 /* If we don't look past the constant pool, we risk emitting a
19024 reference to a constant pool entry that isn't referenced from
19025 code, and thus is not emitted. */
19026 if (rtl)
19027 rtl = avoid_constant_pool_reference (rtl);
19029 /* Try harder to get a rtl. If this symbol ends up not being emitted
19030 in the current CU, resolve_addr will remove the expression referencing
19031 it. */
19032 if (rtl == NULL_RTX
19033 && VAR_P (decl)
19034 && !DECL_EXTERNAL (decl)
19035 && TREE_STATIC (decl)
19036 && DECL_NAME (decl)
19037 && !DECL_HARD_REGISTER (decl)
19038 && DECL_MODE (decl) != VOIDmode)
19040 rtl = make_decl_rtl_for_debug (decl);
19041 if (!MEM_P (rtl)
19042 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
19043 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
19044 rtl = NULL_RTX;
19047 return rtl;
19050 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
19051 returned. If so, the decl for the COMMON block is returned, and the
19052 value is the offset into the common block for the symbol. */
19054 static tree
19055 fortran_common (tree decl, HOST_WIDE_INT *value)
19057 tree val_expr, cvar;
19058 machine_mode mode;
19059 HOST_WIDE_INT bitsize, bitpos;
19060 tree offset;
19061 int unsignedp, reversep, volatilep = 0;
19063 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
19064 it does not have a value (the offset into the common area), or if it
19065 is thread local (as opposed to global) then it isn't common, and shouldn't
19066 be handled as such. */
19067 if (!VAR_P (decl)
19068 || !TREE_STATIC (decl)
19069 || !DECL_HAS_VALUE_EXPR_P (decl)
19070 || !is_fortran ())
19071 return NULL_TREE;
19073 val_expr = DECL_VALUE_EXPR (decl);
19074 if (TREE_CODE (val_expr) != COMPONENT_REF)
19075 return NULL_TREE;
19077 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset, &mode,
19078 &unsignedp, &reversep, &volatilep);
19080 if (cvar == NULL_TREE
19081 || !VAR_P (cvar)
19082 || DECL_ARTIFICIAL (cvar)
19083 || !TREE_PUBLIC (cvar))
19084 return NULL_TREE;
19086 *value = 0;
19087 if (offset != NULL)
19089 if (!tree_fits_shwi_p (offset))
19090 return NULL_TREE;
19091 *value = tree_to_shwi (offset);
19093 if (bitpos != 0)
19094 *value += bitpos / BITS_PER_UNIT;
19096 return cvar;
19099 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
19100 data attribute for a variable or a parameter. We generate the
19101 DW_AT_const_value attribute only in those cases where the given variable
19102 or parameter does not have a true "location" either in memory or in a
19103 register. This can happen (for example) when a constant is passed as an
19104 actual argument in a call to an inline function. (It's possible that
19105 these things can crop up in other ways also.) Note that one type of
19106 constant value which can be passed into an inlined function is a constant
19107 pointer. This can happen for example if an actual argument in an inlined
19108 function call evaluates to a compile-time constant address.
19110 CACHE_P is true if it is worth caching the location list for DECL,
19111 so that future calls can reuse it rather than regenerate it from scratch.
19112 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
19113 since we will need to refer to them each time the function is inlined. */
19115 static bool
19116 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p)
19118 rtx rtl;
19119 dw_loc_list_ref list;
19120 var_loc_list *loc_list;
19121 cached_dw_loc_list *cache;
19123 if (early_dwarf)
19124 return false;
19126 if (TREE_CODE (decl) == ERROR_MARK)
19127 return false;
19129 if (get_AT (die, DW_AT_location)
19130 || get_AT (die, DW_AT_const_value))
19131 return true;
19133 gcc_assert (VAR_P (decl) || TREE_CODE (decl) == PARM_DECL
19134 || TREE_CODE (decl) == RESULT_DECL);
19136 /* Try to get some constant RTL for this decl, and use that as the value of
19137 the location. */
19139 rtl = rtl_for_decl_location (decl);
19140 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
19141 && add_const_value_attribute (die, rtl))
19142 return true;
19144 /* See if we have single element location list that is equivalent to
19145 a constant value. That way we are better to use add_const_value_attribute
19146 rather than expanding constant value equivalent. */
19147 loc_list = lookup_decl_loc (decl);
19148 if (loc_list
19149 && loc_list->first
19150 && loc_list->first->next == NULL
19151 && NOTE_P (loc_list->first->loc)
19152 && NOTE_VAR_LOCATION (loc_list->first->loc)
19153 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
19155 struct var_loc_node *node;
19157 node = loc_list->first;
19158 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
19159 if (GET_CODE (rtl) == EXPR_LIST)
19160 rtl = XEXP (rtl, 0);
19161 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
19162 && add_const_value_attribute (die, rtl))
19163 return true;
19165 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
19166 list several times. See if we've already cached the contents. */
19167 list = NULL;
19168 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
19169 cache_p = false;
19170 if (cache_p)
19172 cache = cached_dw_loc_list_table->find_with_hash (decl, DECL_UID (decl));
19173 if (cache)
19174 list = cache->loc_list;
19176 if (list == NULL)
19178 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2,
19179 NULL);
19180 /* It is usually worth caching this result if the decl is from
19181 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
19182 if (cache_p && list && list->dw_loc_next)
19184 cached_dw_loc_list **slot
19185 = cached_dw_loc_list_table->find_slot_with_hash (decl,
19186 DECL_UID (decl),
19187 INSERT);
19188 cache = ggc_cleared_alloc<cached_dw_loc_list> ();
19189 cache->decl_id = DECL_UID (decl);
19190 cache->loc_list = list;
19191 *slot = cache;
19194 if (list)
19196 add_AT_location_description (die, DW_AT_location, list);
19197 return true;
19199 /* None of that worked, so it must not really have a location;
19200 try adding a constant value attribute from the DECL_INITIAL. */
19201 return tree_add_const_value_attribute_for_decl (die, decl);
19204 /* Helper function for tree_add_const_value_attribute. Natively encode
19205 initializer INIT into an array. Return true if successful. */
19207 static bool
19208 native_encode_initializer (tree init, unsigned char *array, int size)
19210 tree type;
19212 if (init == NULL_TREE)
19213 return false;
19215 STRIP_NOPS (init);
19216 switch (TREE_CODE (init))
19218 case STRING_CST:
19219 type = TREE_TYPE (init);
19220 if (TREE_CODE (type) == ARRAY_TYPE)
19222 tree enttype = TREE_TYPE (type);
19223 machine_mode mode = TYPE_MODE (enttype);
19225 if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
19226 return false;
19227 if (int_size_in_bytes (type) != size)
19228 return false;
19229 if (size > TREE_STRING_LENGTH (init))
19231 memcpy (array, TREE_STRING_POINTER (init),
19232 TREE_STRING_LENGTH (init));
19233 memset (array + TREE_STRING_LENGTH (init),
19234 '\0', size - TREE_STRING_LENGTH (init));
19236 else
19237 memcpy (array, TREE_STRING_POINTER (init), size);
19238 return true;
19240 return false;
19241 case CONSTRUCTOR:
19242 type = TREE_TYPE (init);
19243 if (int_size_in_bytes (type) != size)
19244 return false;
19245 if (TREE_CODE (type) == ARRAY_TYPE)
19247 HOST_WIDE_INT min_index;
19248 unsigned HOST_WIDE_INT cnt;
19249 int curpos = 0, fieldsize;
19250 constructor_elt *ce;
19252 if (TYPE_DOMAIN (type) == NULL_TREE
19253 || !tree_fits_shwi_p (TYPE_MIN_VALUE (TYPE_DOMAIN (type))))
19254 return false;
19256 fieldsize = int_size_in_bytes (TREE_TYPE (type));
19257 if (fieldsize <= 0)
19258 return false;
19260 min_index = tree_to_shwi (TYPE_MIN_VALUE (TYPE_DOMAIN (type)));
19261 memset (array, '\0', size);
19262 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
19264 tree val = ce->value;
19265 tree index = ce->index;
19266 int pos = curpos;
19267 if (index && TREE_CODE (index) == RANGE_EXPR)
19268 pos = (tree_to_shwi (TREE_OPERAND (index, 0)) - min_index)
19269 * fieldsize;
19270 else if (index)
19271 pos = (tree_to_shwi (index) - min_index) * fieldsize;
19273 if (val)
19275 STRIP_NOPS (val);
19276 if (!native_encode_initializer (val, array + pos, fieldsize))
19277 return false;
19279 curpos = pos + fieldsize;
19280 if (index && TREE_CODE (index) == RANGE_EXPR)
19282 int count = tree_to_shwi (TREE_OPERAND (index, 1))
19283 - tree_to_shwi (TREE_OPERAND (index, 0));
19284 while (count-- > 0)
19286 if (val)
19287 memcpy (array + curpos, array + pos, fieldsize);
19288 curpos += fieldsize;
19291 gcc_assert (curpos <= size);
19293 return true;
19295 else if (TREE_CODE (type) == RECORD_TYPE
19296 || TREE_CODE (type) == UNION_TYPE)
19298 tree field = NULL_TREE;
19299 unsigned HOST_WIDE_INT cnt;
19300 constructor_elt *ce;
19302 if (int_size_in_bytes (type) != size)
19303 return false;
19305 if (TREE_CODE (type) == RECORD_TYPE)
19306 field = TYPE_FIELDS (type);
19308 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
19310 tree val = ce->value;
19311 int pos, fieldsize;
19313 if (ce->index != 0)
19314 field = ce->index;
19316 if (val)
19317 STRIP_NOPS (val);
19319 if (field == NULL_TREE || DECL_BIT_FIELD (field))
19320 return false;
19322 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
19323 && TYPE_DOMAIN (TREE_TYPE (field))
19324 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
19325 return false;
19326 else if (DECL_SIZE_UNIT (field) == NULL_TREE
19327 || !tree_fits_shwi_p (DECL_SIZE_UNIT (field)))
19328 return false;
19329 fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
19330 pos = int_byte_position (field);
19331 gcc_assert (pos + fieldsize <= size);
19332 if (val && fieldsize != 0
19333 && !native_encode_initializer (val, array + pos, fieldsize))
19334 return false;
19336 return true;
19338 return false;
19339 case VIEW_CONVERT_EXPR:
19340 case NON_LVALUE_EXPR:
19341 return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
19342 default:
19343 return native_encode_expr (init, array, size) == size;
19347 /* Attach a DW_AT_const_value attribute to DIE. The value of the
19348 attribute is the const value T. */
19350 static bool
19351 tree_add_const_value_attribute (dw_die_ref die, tree t)
19353 tree init;
19354 tree type = TREE_TYPE (t);
19355 rtx rtl;
19357 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
19358 return false;
19360 init = t;
19361 gcc_assert (!DECL_P (init));
19363 if (! early_dwarf)
19365 rtl = rtl_for_decl_init (init, type);
19366 if (rtl)
19367 return add_const_value_attribute (die, rtl);
19369 /* If the host and target are sane, try harder. */
19370 if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
19371 && initializer_constant_valid_p (init, type))
19373 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
19374 if (size > 0 && (int) size == size)
19376 unsigned char *array = ggc_cleared_vec_alloc<unsigned char> (size);
19378 if (native_encode_initializer (init, array, size))
19380 add_AT_vec (die, DW_AT_const_value, size, 1, array);
19381 return true;
19383 ggc_free (array);
19386 return false;
19389 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
19390 attribute is the const value of T, where T is an integral constant
19391 variable with static storage duration
19392 (so it can't be a PARM_DECL or a RESULT_DECL). */
19394 static bool
19395 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
19398 if (!decl
19399 || (!VAR_P (decl) && TREE_CODE (decl) != CONST_DECL)
19400 || (VAR_P (decl) && !TREE_STATIC (decl)))
19401 return false;
19403 if (TREE_READONLY (decl)
19404 && ! TREE_THIS_VOLATILE (decl)
19405 && DECL_INITIAL (decl))
19406 /* OK */;
19407 else
19408 return false;
19410 /* Don't add DW_AT_const_value if abstract origin already has one. */
19411 if (get_AT (var_die, DW_AT_const_value))
19412 return false;
19414 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
19417 /* Convert the CFI instructions for the current function into a
19418 location list. This is used for DW_AT_frame_base when we targeting
19419 a dwarf2 consumer that does not support the dwarf3
19420 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
19421 expressions. */
19423 static dw_loc_list_ref
19424 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
19426 int ix;
19427 dw_fde_ref fde;
19428 dw_loc_list_ref list, *list_tail;
19429 dw_cfi_ref cfi;
19430 dw_cfa_location last_cfa, next_cfa;
19431 const char *start_label, *last_label, *section;
19432 dw_cfa_location remember;
19434 fde = cfun->fde;
19435 gcc_assert (fde != NULL);
19437 section = secname_for_decl (current_function_decl);
19438 list_tail = &list;
19439 list = NULL;
19441 memset (&next_cfa, 0, sizeof (next_cfa));
19442 next_cfa.reg = INVALID_REGNUM;
19443 remember = next_cfa;
19445 start_label = fde->dw_fde_begin;
19447 /* ??? Bald assumption that the CIE opcode list does not contain
19448 advance opcodes. */
19449 FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
19450 lookup_cfa_1 (cfi, &next_cfa, &remember);
19452 last_cfa = next_cfa;
19453 last_label = start_label;
19455 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
19457 /* If the first partition contained no CFI adjustments, the
19458 CIE opcodes apply to the whole first partition. */
19459 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19460 fde->dw_fde_begin, fde->dw_fde_end, section);
19461 list_tail =&(*list_tail)->dw_loc_next;
19462 start_label = last_label = fde->dw_fde_second_begin;
19465 FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
19467 switch (cfi->dw_cfi_opc)
19469 case DW_CFA_set_loc:
19470 case DW_CFA_advance_loc1:
19471 case DW_CFA_advance_loc2:
19472 case DW_CFA_advance_loc4:
19473 if (!cfa_equal_p (&last_cfa, &next_cfa))
19475 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19476 start_label, last_label, section);
19478 list_tail = &(*list_tail)->dw_loc_next;
19479 last_cfa = next_cfa;
19480 start_label = last_label;
19482 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
19483 break;
19485 case DW_CFA_advance_loc:
19486 /* The encoding is complex enough that we should never emit this. */
19487 gcc_unreachable ();
19489 default:
19490 lookup_cfa_1 (cfi, &next_cfa, &remember);
19491 break;
19493 if (ix + 1 == fde->dw_fde_switch_cfi_index)
19495 if (!cfa_equal_p (&last_cfa, &next_cfa))
19497 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19498 start_label, last_label, section);
19500 list_tail = &(*list_tail)->dw_loc_next;
19501 last_cfa = next_cfa;
19502 start_label = last_label;
19504 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19505 start_label, fde->dw_fde_end, section);
19506 list_tail = &(*list_tail)->dw_loc_next;
19507 start_label = last_label = fde->dw_fde_second_begin;
19511 if (!cfa_equal_p (&last_cfa, &next_cfa))
19513 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19514 start_label, last_label, section);
19515 list_tail = &(*list_tail)->dw_loc_next;
19516 start_label = last_label;
19519 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
19520 start_label,
19521 fde->dw_fde_second_begin
19522 ? fde->dw_fde_second_end : fde->dw_fde_end,
19523 section);
19525 if (list && list->dw_loc_next)
19526 gen_llsym (list);
19528 return list;
19531 /* Compute a displacement from the "steady-state frame pointer" to the
19532 frame base (often the same as the CFA), and store it in
19533 frame_pointer_fb_offset. OFFSET is added to the displacement
19534 before the latter is negated. */
19536 static void
19537 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
19539 rtx reg, elim;
19541 #ifdef FRAME_POINTER_CFA_OFFSET
19542 reg = frame_pointer_rtx;
19543 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
19544 #else
19545 reg = arg_pointer_rtx;
19546 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
19547 #endif
19549 elim = (ira_use_lra_p
19550 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
19551 : eliminate_regs (reg, VOIDmode, NULL_RTX));
19552 if (GET_CODE (elim) == PLUS)
19554 offset += INTVAL (XEXP (elim, 1));
19555 elim = XEXP (elim, 0);
19558 frame_pointer_fb_offset = -offset;
19560 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
19561 in which to eliminate. This is because it's stack pointer isn't
19562 directly accessible as a register within the ISA. To work around
19563 this, assume that while we cannot provide a proper value for
19564 frame_pointer_fb_offset, we won't need one either. */
19565 frame_pointer_fb_offset_valid
19566 = ((SUPPORTS_STACK_ALIGNMENT
19567 && (elim == hard_frame_pointer_rtx
19568 || elim == stack_pointer_rtx))
19569 || elim == (frame_pointer_needed
19570 ? hard_frame_pointer_rtx
19571 : stack_pointer_rtx));
19574 /* Generate a DW_AT_name attribute given some string value to be included as
19575 the value of the attribute. */
19577 static void
19578 add_name_attribute (dw_die_ref die, const char *name_string)
19580 if (name_string != NULL && *name_string != 0)
19582 if (demangle_name_func)
19583 name_string = (*demangle_name_func) (name_string);
19585 add_AT_string (die, DW_AT_name, name_string);
19589 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
19590 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
19591 of TYPE accordingly.
19593 ??? This is a temporary measure until after we're able to generate
19594 regular DWARF for the complex Ada type system. */
19596 static void
19597 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
19598 dw_die_ref context_die)
19600 tree dtype;
19601 dw_die_ref dtype_die;
19603 if (!lang_hooks.types.descriptive_type)
19604 return;
19606 dtype = lang_hooks.types.descriptive_type (type);
19607 if (!dtype)
19608 return;
19610 dtype_die = lookup_type_die (dtype);
19611 if (!dtype_die)
19613 gen_type_die (dtype, context_die);
19614 dtype_die = lookup_type_die (dtype);
19615 gcc_assert (dtype_die);
19618 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
19621 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir. */
19623 static const char *
19624 comp_dir_string (void)
19626 const char *wd;
19627 char *wd1;
19628 static const char *cached_wd = NULL;
19630 if (cached_wd != NULL)
19631 return cached_wd;
19633 wd = get_src_pwd ();
19634 if (wd == NULL)
19635 return NULL;
19637 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
19639 int wdlen;
19641 wdlen = strlen (wd);
19642 wd1 = ggc_vec_alloc<char> (wdlen + 2);
19643 strcpy (wd1, wd);
19644 wd1 [wdlen] = DIR_SEPARATOR;
19645 wd1 [wdlen + 1] = 0;
19646 wd = wd1;
19649 cached_wd = remap_debug_filename (wd);
19650 return cached_wd;
19653 /* Generate a DW_AT_comp_dir attribute for DIE. */
19655 static void
19656 add_comp_dir_attribute (dw_die_ref die)
19658 const char * wd = comp_dir_string ();
19659 if (wd != NULL)
19660 add_AT_string (die, DW_AT_comp_dir, wd);
19663 /* Given a tree node VALUE describing a scalar attribute ATTR (i.e. a bound, a
19664 pointer computation, ...), output a representation for that bound according
19665 to the accepted FORMS (see enum dw_scalar_form) and add it to DIE. See
19666 loc_list_from_tree for the meaning of CONTEXT. */
19668 static void
19669 add_scalar_info (dw_die_ref die, enum dwarf_attribute attr, tree value,
19670 int forms, struct loc_descr_context *context)
19672 dw_die_ref context_die, decl_die;
19673 dw_loc_list_ref list;
19674 bool strip_conversions = true;
19675 bool placeholder_seen = false;
19677 while (strip_conversions)
19678 switch (TREE_CODE (value))
19680 case ERROR_MARK:
19681 case SAVE_EXPR:
19682 return;
19684 CASE_CONVERT:
19685 case VIEW_CONVERT_EXPR:
19686 value = TREE_OPERAND (value, 0);
19687 break;
19689 default:
19690 strip_conversions = false;
19691 break;
19694 /* If possible and permitted, output the attribute as a constant. */
19695 if ((forms & dw_scalar_form_constant) != 0
19696 && TREE_CODE (value) == INTEGER_CST)
19698 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (value));
19700 /* If HOST_WIDE_INT is big enough then represent the bound as
19701 a constant value. We need to choose a form based on
19702 whether the type is signed or unsigned. We cannot just
19703 call add_AT_unsigned if the value itself is positive
19704 (add_AT_unsigned might add the unsigned value encoded as
19705 DW_FORM_data[1248]). Some DWARF consumers will lookup the
19706 bounds type and then sign extend any unsigned values found
19707 for signed types. This is needed only for
19708 DW_AT_{lower,upper}_bound, since for most other attributes,
19709 consumers will treat DW_FORM_data[1248] as unsigned values,
19710 regardless of the underlying type. */
19711 if (prec <= HOST_BITS_PER_WIDE_INT
19712 || tree_fits_uhwi_p (value))
19714 if (TYPE_UNSIGNED (TREE_TYPE (value)))
19715 add_AT_unsigned (die, attr, TREE_INT_CST_LOW (value));
19716 else
19717 add_AT_int (die, attr, TREE_INT_CST_LOW (value));
19719 else
19720 /* Otherwise represent the bound as an unsigned value with
19721 the precision of its type. The precision and signedness
19722 of the type will be necessary to re-interpret it
19723 unambiguously. */
19724 add_AT_wide (die, attr, value);
19725 return;
19728 /* Otherwise, if it's possible and permitted too, output a reference to
19729 another DIE. */
19730 if ((forms & dw_scalar_form_reference) != 0)
19732 tree decl = NULL_TREE;
19734 /* Some type attributes reference an outer type. For instance, the upper
19735 bound of an array may reference an embedding record (this happens in
19736 Ada). */
19737 if (TREE_CODE (value) == COMPONENT_REF
19738 && TREE_CODE (TREE_OPERAND (value, 0)) == PLACEHOLDER_EXPR
19739 && TREE_CODE (TREE_OPERAND (value, 1)) == FIELD_DECL)
19740 decl = TREE_OPERAND (value, 1);
19742 else if (VAR_P (value)
19743 || TREE_CODE (value) == PARM_DECL
19744 || TREE_CODE (value) == RESULT_DECL)
19745 decl = value;
19747 if (decl != NULL_TREE)
19749 dw_die_ref decl_die = lookup_decl_die (decl);
19751 /* ??? Can this happen, or should the variable have been bound
19752 first? Probably it can, since I imagine that we try to create
19753 the types of parameters in the order in which they exist in
19754 the list, and won't have created a forward reference to a
19755 later parameter. */
19756 if (decl_die != NULL)
19758 add_AT_die_ref (die, attr, decl_die);
19759 return;
19764 /* Last chance: try to create a stack operation procedure to evaluate the
19765 value. Do nothing if even that is not possible or permitted. */
19766 if ((forms & dw_scalar_form_exprloc) == 0)
19767 return;
19769 list = loc_list_from_tree (value, 2, context);
19770 if (context && context->placeholder_arg)
19772 placeholder_seen = context->placeholder_seen;
19773 context->placeholder_seen = false;
19775 if (list == NULL || single_element_loc_list_p (list))
19777 /* If this attribute is not a reference nor constant, it is
19778 a DWARF expression rather than location description. For that
19779 loc_list_from_tree (value, 0, &context) is needed. */
19780 dw_loc_list_ref list2 = loc_list_from_tree (value, 0, context);
19781 if (list2 && single_element_loc_list_p (list2))
19783 if (placeholder_seen)
19785 struct dwarf_procedure_info dpi;
19786 dpi.fndecl = NULL_TREE;
19787 dpi.args_count = 1;
19788 if (!resolve_args_picking (list2->expr, 1, &dpi))
19789 return;
19791 add_AT_loc (die, attr, list2->expr);
19792 return;
19796 /* If that failed to give a single element location list, fall back to
19797 outputting this as a reference... still if permitted. */
19798 if (list == NULL
19799 || (forms & dw_scalar_form_reference) == 0
19800 || placeholder_seen)
19801 return;
19803 if (current_function_decl == 0)
19804 context_die = comp_unit_die ();
19805 else
19806 context_die = lookup_decl_die (current_function_decl);
19808 decl_die = new_die (DW_TAG_variable, context_die, value);
19809 add_AT_flag (decl_die, DW_AT_artificial, 1);
19810 add_type_attribute (decl_die, TREE_TYPE (value), TYPE_QUAL_CONST, false,
19811 context_die);
19812 add_AT_location_description (decl_die, DW_AT_location, list);
19813 add_AT_die_ref (die, attr, decl_die);
19816 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
19817 default. */
19819 static int
19820 lower_bound_default (void)
19822 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
19824 case DW_LANG_C:
19825 case DW_LANG_C89:
19826 case DW_LANG_C99:
19827 case DW_LANG_C11:
19828 case DW_LANG_C_plus_plus:
19829 case DW_LANG_C_plus_plus_11:
19830 case DW_LANG_C_plus_plus_14:
19831 case DW_LANG_ObjC:
19832 case DW_LANG_ObjC_plus_plus:
19833 case DW_LANG_Java:
19834 return 0;
19835 case DW_LANG_Fortran77:
19836 case DW_LANG_Fortran90:
19837 case DW_LANG_Fortran95:
19838 case DW_LANG_Fortran03:
19839 case DW_LANG_Fortran08:
19840 return 1;
19841 case DW_LANG_UPC:
19842 case DW_LANG_D:
19843 case DW_LANG_Python:
19844 return dwarf_version >= 4 ? 0 : -1;
19845 case DW_LANG_Ada95:
19846 case DW_LANG_Ada83:
19847 case DW_LANG_Cobol74:
19848 case DW_LANG_Cobol85:
19849 case DW_LANG_Pascal83:
19850 case DW_LANG_Modula2:
19851 case DW_LANG_PLI:
19852 return dwarf_version >= 4 ? 1 : -1;
19853 default:
19854 return -1;
19858 /* Given a tree node describing an array bound (either lower or upper) output
19859 a representation for that bound. */
19861 static void
19862 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr,
19863 tree bound, struct loc_descr_context *context)
19865 int dflt;
19867 while (1)
19868 switch (TREE_CODE (bound))
19870 /* Strip all conversions. */
19871 CASE_CONVERT:
19872 case VIEW_CONVERT_EXPR:
19873 bound = TREE_OPERAND (bound, 0);
19874 break;
19876 /* All fixed-bounds are represented by INTEGER_CST nodes. Lower bounds
19877 are even omitted when they are the default. */
19878 case INTEGER_CST:
19879 /* If the value for this bound is the default one, we can even omit the
19880 attribute. */
19881 if (bound_attr == DW_AT_lower_bound
19882 && tree_fits_shwi_p (bound)
19883 && (dflt = lower_bound_default ()) != -1
19884 && tree_to_shwi (bound) == dflt)
19885 return;
19887 /* FALLTHRU */
19889 default:
19890 /* Because of the complex interaction there can be with other GNAT
19891 encodings, GDB isn't ready yet to handle proper DWARF description
19892 for self-referencial subrange bounds: let GNAT encodings do the
19893 magic in such a case. */
19894 if (is_ada ()
19895 && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL
19896 && contains_placeholder_p (bound))
19897 return;
19899 add_scalar_info (subrange_die, bound_attr, bound,
19900 dw_scalar_form_constant
19901 | dw_scalar_form_exprloc
19902 | dw_scalar_form_reference,
19903 context);
19904 return;
19908 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
19909 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
19910 Note that the block of subscript information for an array type also
19911 includes information about the element type of the given array type.
19913 This function reuses previously set type and bound information if
19914 available. */
19916 static void
19917 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
19919 unsigned dimension_number;
19920 tree lower, upper;
19921 dw_die_ref child = type_die->die_child;
19923 for (dimension_number = 0;
19924 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
19925 type = TREE_TYPE (type), dimension_number++)
19927 tree domain = TYPE_DOMAIN (type);
19929 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
19930 break;
19932 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
19933 and (in GNU C only) variable bounds. Handle all three forms
19934 here. */
19936 /* Find and reuse a previously generated DW_TAG_subrange_type if
19937 available.
19939 For multi-dimensional arrays, as we iterate through the
19940 various dimensions in the enclosing for loop above, we also
19941 iterate through the DIE children and pick at each
19942 DW_TAG_subrange_type previously generated (if available).
19943 Each child DW_TAG_subrange_type DIE describes the range of
19944 the current dimension. At this point we should have as many
19945 DW_TAG_subrange_type's as we have dimensions in the
19946 array. */
19947 dw_die_ref subrange_die = NULL;
19948 if (child)
19949 while (1)
19951 child = child->die_sib;
19952 if (child->die_tag == DW_TAG_subrange_type)
19953 subrange_die = child;
19954 if (child == type_die->die_child)
19956 /* If we wrapped around, stop looking next time. */
19957 child = NULL;
19958 break;
19960 if (child->die_tag == DW_TAG_subrange_type)
19961 break;
19963 if (!subrange_die)
19964 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
19966 if (domain)
19968 /* We have an array type with specified bounds. */
19969 lower = TYPE_MIN_VALUE (domain);
19970 upper = TYPE_MAX_VALUE (domain);
19972 /* Define the index type. */
19973 if (TREE_TYPE (domain)
19974 && !get_AT (subrange_die, DW_AT_type))
19976 /* ??? This is probably an Ada unnamed subrange type. Ignore the
19977 TREE_TYPE field. We can't emit debug info for this
19978 because it is an unnamed integral type. */
19979 if (TREE_CODE (domain) == INTEGER_TYPE
19980 && TYPE_NAME (domain) == NULL_TREE
19981 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
19982 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
19984 else
19985 add_type_attribute (subrange_die, TREE_TYPE (domain),
19986 TYPE_UNQUALIFIED, false, type_die);
19989 /* ??? If upper is NULL, the array has unspecified length,
19990 but it does have a lower bound. This happens with Fortran
19991 dimension arr(N:*)
19992 Since the debugger is definitely going to need to know N
19993 to produce useful results, go ahead and output the lower
19994 bound solo, and hope the debugger can cope. */
19996 if (!get_AT (subrange_die, DW_AT_lower_bound))
19997 add_bound_info (subrange_die, DW_AT_lower_bound, lower, NULL);
19998 if (upper && !get_AT (subrange_die, DW_AT_upper_bound))
19999 add_bound_info (subrange_die, DW_AT_upper_bound, upper, NULL);
20002 /* Otherwise we have an array type with an unspecified length. The
20003 DWARF-2 spec does not say how to handle this; let's just leave out the
20004 bounds. */
20008 /* Add a DW_AT_byte_size attribute to DIE with TREE_NODE's size. */
20010 static void
20011 add_byte_size_attribute (dw_die_ref die, tree tree_node)
20013 dw_die_ref decl_die;
20014 HOST_WIDE_INT size;
20015 dw_loc_descr_ref size_expr = NULL;
20017 switch (TREE_CODE (tree_node))
20019 case ERROR_MARK:
20020 size = 0;
20021 break;
20022 case ENUMERAL_TYPE:
20023 case RECORD_TYPE:
20024 case UNION_TYPE:
20025 case QUAL_UNION_TYPE:
20026 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
20027 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
20029 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
20030 return;
20032 size_expr = type_byte_size (tree_node, &size);
20033 break;
20034 case FIELD_DECL:
20035 /* For a data member of a struct or union, the DW_AT_byte_size is
20036 generally given as the number of bytes normally allocated for an
20037 object of the *declared* type of the member itself. This is true
20038 even for bit-fields. */
20039 size = int_size_in_bytes (field_type (tree_node));
20040 break;
20041 default:
20042 gcc_unreachable ();
20045 /* Support for dynamically-sized objects was introduced by DWARFv3.
20046 At the moment, GDB does not handle variable byte sizes very well,
20047 though. */
20048 if ((dwarf_version >= 3 || !dwarf_strict)
20049 && gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL
20050 && size_expr != NULL)
20051 add_AT_loc (die, DW_AT_byte_size, size_expr);
20053 /* Note that `size' might be -1 when we get to this point. If it is, that
20054 indicates that the byte size of the entity in question is variable and
20055 that we could not generate a DWARF expression that computes it. */
20056 if (size >= 0)
20057 add_AT_unsigned (die, DW_AT_byte_size, size);
20060 /* Add a DW_AT_alignment attribute to DIE with TREE_NODE's non-default
20061 alignment. */
20063 static void
20064 add_alignment_attribute (dw_die_ref die, tree tree_node)
20066 if (dwarf_version < 5 && dwarf_strict)
20067 return;
20069 unsigned align;
20071 if (DECL_P (tree_node))
20073 if (!DECL_USER_ALIGN (tree_node))
20074 return;
20076 align = DECL_ALIGN_UNIT (tree_node);
20078 else if (TYPE_P (tree_node))
20080 if (!TYPE_USER_ALIGN (tree_node))
20081 return;
20083 align = TYPE_ALIGN_UNIT (tree_node);
20085 else
20086 gcc_unreachable ();
20088 add_AT_unsigned (die, DW_AT_alignment, align);
20091 /* For a FIELD_DECL node which represents a bit-field, output an attribute
20092 which specifies the distance in bits from the highest order bit of the
20093 "containing object" for the bit-field to the highest order bit of the
20094 bit-field itself.
20096 For any given bit-field, the "containing object" is a hypothetical object
20097 (of some integral or enum type) within which the given bit-field lives. The
20098 type of this hypothetical "containing object" is always the same as the
20099 declared type of the individual bit-field itself. The determination of the
20100 exact location of the "containing object" for a bit-field is rather
20101 complicated. It's handled by the `field_byte_offset' function (above).
20103 CTX is required: see the comment for VLR_CONTEXT.
20105 Note that it is the size (in bytes) of the hypothetical "containing object"
20106 which will be given in the DW_AT_byte_size attribute for this bit-field.
20107 (See `byte_size_attribute' above). */
20109 static inline void
20110 add_bit_offset_attribute (dw_die_ref die, tree decl, struct vlr_context *ctx)
20112 HOST_WIDE_INT object_offset_in_bytes;
20113 tree original_type = DECL_BIT_FIELD_TYPE (decl);
20114 HOST_WIDE_INT bitpos_int;
20115 HOST_WIDE_INT highest_order_object_bit_offset;
20116 HOST_WIDE_INT highest_order_field_bit_offset;
20117 HOST_WIDE_INT bit_offset;
20119 field_byte_offset (decl, ctx, &object_offset_in_bytes);
20121 /* Must be a field and a bit field. */
20122 gcc_assert (original_type && TREE_CODE (decl) == FIELD_DECL);
20124 /* We can't yet handle bit-fields whose offsets are variable, so if we
20125 encounter such things, just return without generating any attribute
20126 whatsoever. Likewise for variable or too large size. */
20127 if (! tree_fits_shwi_p (bit_position (decl))
20128 || ! tree_fits_uhwi_p (DECL_SIZE (decl)))
20129 return;
20131 bitpos_int = int_bit_position (decl);
20133 /* Note that the bit offset is always the distance (in bits) from the
20134 highest-order bit of the "containing object" to the highest-order bit of
20135 the bit-field itself. Since the "high-order end" of any object or field
20136 is different on big-endian and little-endian machines, the computation
20137 below must take account of these differences. */
20138 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
20139 highest_order_field_bit_offset = bitpos_int;
20141 if (! BYTES_BIG_ENDIAN)
20143 highest_order_field_bit_offset += tree_to_shwi (DECL_SIZE (decl));
20144 highest_order_object_bit_offset +=
20145 simple_type_size_in_bits (original_type);
20148 bit_offset
20149 = (! BYTES_BIG_ENDIAN
20150 ? highest_order_object_bit_offset - highest_order_field_bit_offset
20151 : highest_order_field_bit_offset - highest_order_object_bit_offset);
20153 if (bit_offset < 0)
20154 add_AT_int (die, DW_AT_bit_offset, bit_offset);
20155 else
20156 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
20159 /* For a FIELD_DECL node which represents a bit field, output an attribute
20160 which specifies the length in bits of the given field. */
20162 static inline void
20163 add_bit_size_attribute (dw_die_ref die, tree decl)
20165 /* Must be a field and a bit field. */
20166 gcc_assert (TREE_CODE (decl) == FIELD_DECL
20167 && DECL_BIT_FIELD_TYPE (decl));
20169 if (tree_fits_uhwi_p (DECL_SIZE (decl)))
20170 add_AT_unsigned (die, DW_AT_bit_size, tree_to_uhwi (DECL_SIZE (decl)));
20173 /* If the compiled language is ANSI C, then add a 'prototyped'
20174 attribute, if arg types are given for the parameters of a function. */
20176 static inline void
20177 add_prototyped_attribute (dw_die_ref die, tree func_type)
20179 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
20181 case DW_LANG_C:
20182 case DW_LANG_C89:
20183 case DW_LANG_C99:
20184 case DW_LANG_C11:
20185 case DW_LANG_ObjC:
20186 if (prototype_p (func_type))
20187 add_AT_flag (die, DW_AT_prototyped, 1);
20188 break;
20189 default:
20190 break;
20194 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
20195 by looking in the type declaration, the object declaration equate table or
20196 the block mapping. */
20198 static inline dw_die_ref
20199 add_abstract_origin_attribute (dw_die_ref die, tree origin)
20201 dw_die_ref origin_die = NULL;
20203 if (TREE_CODE (origin) != FUNCTION_DECL
20204 && TREE_CODE (origin) != BLOCK)
20206 /* We may have gotten separated from the block for the inlined
20207 function, if we're in an exception handler or some such; make
20208 sure that the abstract function has been written out.
20210 Doing this for nested functions is wrong, however; functions are
20211 distinct units, and our context might not even be inline. */
20212 tree fn = origin;
20214 if (TYPE_P (fn))
20215 fn = TYPE_STUB_DECL (fn);
20217 fn = decl_function_context (fn);
20218 if (fn)
20219 dwarf2out_abstract_function (fn);
20222 if (DECL_P (origin))
20223 origin_die = lookup_decl_die (origin);
20224 else if (TYPE_P (origin))
20225 origin_die = lookup_type_die (origin);
20226 else if (TREE_CODE (origin) == BLOCK)
20227 origin_die = BLOCK_DIE (origin);
20229 /* XXX: Functions that are never lowered don't always have correct block
20230 trees (in the case of java, they simply have no block tree, in some other
20231 languages). For these functions, there is nothing we can really do to
20232 output correct debug info for inlined functions in all cases. Rather
20233 than die, we'll just produce deficient debug info now, in that we will
20234 have variables without a proper abstract origin. In the future, when all
20235 functions are lowered, we should re-add a gcc_assert (origin_die)
20236 here. */
20238 if (origin_die)
20239 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
20240 return origin_die;
20243 /* We do not currently support the pure_virtual attribute. */
20245 static inline void
20246 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
20248 if (DECL_VINDEX (func_decl))
20250 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
20252 if (tree_fits_shwi_p (DECL_VINDEX (func_decl)))
20253 add_AT_loc (die, DW_AT_vtable_elem_location,
20254 new_loc_descr (DW_OP_constu,
20255 tree_to_shwi (DECL_VINDEX (func_decl)),
20256 0));
20258 /* GNU extension: Record what type this method came from originally. */
20259 if (debug_info_level > DINFO_LEVEL_TERSE
20260 && DECL_CONTEXT (func_decl))
20261 add_AT_die_ref (die, DW_AT_containing_type,
20262 lookup_type_die (DECL_CONTEXT (func_decl)));
20266 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
20267 given decl. This used to be a vendor extension until after DWARF 4
20268 standardized it. */
20270 static void
20271 add_linkage_attr (dw_die_ref die, tree decl)
20273 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
20275 /* Mimic what assemble_name_raw does with a leading '*'. */
20276 if (name[0] == '*')
20277 name = &name[1];
20279 if (dwarf_version >= 4)
20280 add_AT_string (die, DW_AT_linkage_name, name);
20281 else
20282 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
20285 /* Add source coordinate attributes for the given decl. */
20287 static void
20288 add_src_coords_attributes (dw_die_ref die, tree decl)
20290 expanded_location s;
20292 if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
20293 return;
20294 s = expand_location (DECL_SOURCE_LOCATION (decl));
20295 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
20296 add_AT_unsigned (die, DW_AT_decl_line, s.line);
20297 if (debug_column_info && s.column)
20298 add_AT_unsigned (die, DW_AT_decl_column, s.column);
20301 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
20303 static void
20304 add_linkage_name_raw (dw_die_ref die, tree decl)
20306 /* Defer until we have an assembler name set. */
20307 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
20309 limbo_die_node *asm_name;
20311 asm_name = ggc_cleared_alloc<limbo_die_node> ();
20312 asm_name->die = die;
20313 asm_name->created_for = decl;
20314 asm_name->next = deferred_asm_name;
20315 deferred_asm_name = asm_name;
20317 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
20318 add_linkage_attr (die, decl);
20321 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl if desired. */
20323 static void
20324 add_linkage_name (dw_die_ref die, tree decl)
20326 if (debug_info_level > DINFO_LEVEL_NONE
20327 && VAR_OR_FUNCTION_DECL_P (decl)
20328 && TREE_PUBLIC (decl)
20329 && !(VAR_P (decl) && DECL_REGISTER (decl))
20330 && die->die_tag != DW_TAG_member)
20331 add_linkage_name_raw (die, decl);
20334 /* Add a DW_AT_name attribute and source coordinate attribute for the
20335 given decl, but only if it actually has a name. */
20337 static void
20338 add_name_and_src_coords_attributes (dw_die_ref die, tree decl,
20339 bool no_linkage_name)
20341 tree decl_name;
20343 decl_name = DECL_NAME (decl);
20344 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
20346 const char *name = dwarf2_name (decl, 0);
20347 if (name)
20348 add_name_attribute (die, name);
20349 if (! DECL_ARTIFICIAL (decl))
20350 add_src_coords_attributes (die, decl);
20352 if (!no_linkage_name)
20353 add_linkage_name (die, decl);
20356 #ifdef VMS_DEBUGGING_INFO
20357 /* Get the function's name, as described by its RTL. This may be different
20358 from the DECL_NAME name used in the source file. */
20359 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
20361 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
20362 XEXP (DECL_RTL (decl), 0), false);
20363 vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
20365 #endif /* VMS_DEBUGGING_INFO */
20368 /* Add VALUE as a DW_AT_discr_value attribute to DIE. */
20370 static void
20371 add_discr_value (dw_die_ref die, dw_discr_value *value)
20373 dw_attr_node attr;
20375 attr.dw_attr = DW_AT_discr_value;
20376 attr.dw_attr_val.val_class = dw_val_class_discr_value;
20377 attr.dw_attr_val.val_entry = NULL;
20378 attr.dw_attr_val.v.val_discr_value.pos = value->pos;
20379 if (value->pos)
20380 attr.dw_attr_val.v.val_discr_value.v.uval = value->v.uval;
20381 else
20382 attr.dw_attr_val.v.val_discr_value.v.sval = value->v.sval;
20383 add_dwarf_attr (die, &attr);
20386 /* Add DISCR_LIST as a DW_AT_discr_list to DIE. */
20388 static void
20389 add_discr_list (dw_die_ref die, dw_discr_list_ref discr_list)
20391 dw_attr_node attr;
20393 attr.dw_attr = DW_AT_discr_list;
20394 attr.dw_attr_val.val_class = dw_val_class_discr_list;
20395 attr.dw_attr_val.val_entry = NULL;
20396 attr.dw_attr_val.v.val_discr_list = discr_list;
20397 add_dwarf_attr (die, &attr);
20400 static inline dw_discr_list_ref
20401 AT_discr_list (dw_attr_node *attr)
20403 return attr->dw_attr_val.v.val_discr_list;
20406 #ifdef VMS_DEBUGGING_INFO
20407 /* Output the debug main pointer die for VMS */
20409 void
20410 dwarf2out_vms_debug_main_pointer (void)
20412 char label[MAX_ARTIFICIAL_LABEL_BYTES];
20413 dw_die_ref die;
20415 /* Allocate the VMS debug main subprogram die. */
20416 die = ggc_cleared_alloc<die_node> ();
20417 die->die_tag = DW_TAG_subprogram;
20418 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
20419 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
20420 current_function_funcdef_no);
20421 add_AT_lbl_id (die, DW_AT_entry_pc, label);
20423 /* Make it the first child of comp_unit_die (). */
20424 die->die_parent = comp_unit_die ();
20425 if (comp_unit_die ()->die_child)
20427 die->die_sib = comp_unit_die ()->die_child->die_sib;
20428 comp_unit_die ()->die_child->die_sib = die;
20430 else
20432 die->die_sib = die;
20433 comp_unit_die ()->die_child = die;
20436 #endif /* VMS_DEBUGGING_INFO */
20438 /* Push a new declaration scope. */
20440 static void
20441 push_decl_scope (tree scope)
20443 vec_safe_push (decl_scope_table, scope);
20446 /* Pop a declaration scope. */
20448 static inline void
20449 pop_decl_scope (void)
20451 decl_scope_table->pop ();
20454 /* walk_tree helper function for uses_local_type, below. */
20456 static tree
20457 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
20459 if (!TYPE_P (*tp))
20460 *walk_subtrees = 0;
20461 else
20463 tree name = TYPE_NAME (*tp);
20464 if (name && DECL_P (name) && decl_function_context (name))
20465 return *tp;
20467 return NULL_TREE;
20470 /* If TYPE involves a function-local type (including a local typedef to a
20471 non-local type), returns that type; otherwise returns NULL_TREE. */
20473 static tree
20474 uses_local_type (tree type)
20476 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
20477 return used;
20480 /* Return the DIE for the scope that immediately contains this type.
20481 Non-named types that do not involve a function-local type get global
20482 scope. Named types nested in namespaces or other types get their
20483 containing scope. All other types (i.e. function-local named types) get
20484 the current active scope. */
20486 static dw_die_ref
20487 scope_die_for (tree t, dw_die_ref context_die)
20489 dw_die_ref scope_die = NULL;
20490 tree containing_scope;
20492 /* Non-types always go in the current scope. */
20493 gcc_assert (TYPE_P (t));
20495 /* Use the scope of the typedef, rather than the scope of the type
20496 it refers to. */
20497 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
20498 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
20499 else
20500 containing_scope = TYPE_CONTEXT (t);
20502 /* Use the containing namespace if there is one. */
20503 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
20505 if (context_die == lookup_decl_die (containing_scope))
20506 /* OK */;
20507 else if (debug_info_level > DINFO_LEVEL_TERSE)
20508 context_die = get_context_die (containing_scope);
20509 else
20510 containing_scope = NULL_TREE;
20513 /* Ignore function type "scopes" from the C frontend. They mean that
20514 a tagged type is local to a parmlist of a function declarator, but
20515 that isn't useful to DWARF. */
20516 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
20517 containing_scope = NULL_TREE;
20519 if (SCOPE_FILE_SCOPE_P (containing_scope))
20521 /* If T uses a local type keep it local as well, to avoid references
20522 to function-local DIEs from outside the function. */
20523 if (current_function_decl && uses_local_type (t))
20524 scope_die = context_die;
20525 else
20526 scope_die = comp_unit_die ();
20528 else if (TYPE_P (containing_scope))
20530 /* For types, we can just look up the appropriate DIE. */
20531 if (debug_info_level > DINFO_LEVEL_TERSE)
20532 scope_die = get_context_die (containing_scope);
20533 else
20535 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
20536 if (scope_die == NULL)
20537 scope_die = comp_unit_die ();
20540 else
20541 scope_die = context_die;
20543 return scope_die;
20546 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
20548 static inline int
20549 local_scope_p (dw_die_ref context_die)
20551 for (; context_die; context_die = context_die->die_parent)
20552 if (context_die->die_tag == DW_TAG_inlined_subroutine
20553 || context_die->die_tag == DW_TAG_subprogram)
20554 return 1;
20556 return 0;
20559 /* Returns nonzero if CONTEXT_DIE is a class. */
20561 static inline int
20562 class_scope_p (dw_die_ref context_die)
20564 return (context_die
20565 && (context_die->die_tag == DW_TAG_structure_type
20566 || context_die->die_tag == DW_TAG_class_type
20567 || context_die->die_tag == DW_TAG_interface_type
20568 || context_die->die_tag == DW_TAG_union_type));
20571 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
20572 whether or not to treat a DIE in this context as a declaration. */
20574 static inline int
20575 class_or_namespace_scope_p (dw_die_ref context_die)
20577 return (class_scope_p (context_die)
20578 || (context_die && context_die->die_tag == DW_TAG_namespace));
20581 /* Many forms of DIEs require a "type description" attribute. This
20582 routine locates the proper "type descriptor" die for the type given
20583 by 'type' plus any additional qualifiers given by 'cv_quals', and
20584 adds a DW_AT_type attribute below the given die. */
20586 static void
20587 add_type_attribute (dw_die_ref object_die, tree type, int cv_quals,
20588 bool reverse, dw_die_ref context_die)
20590 enum tree_code code = TREE_CODE (type);
20591 dw_die_ref type_die = NULL;
20593 /* ??? If this type is an unnamed subrange type of an integral, floating-point
20594 or fixed-point type, use the inner type. This is because we have no
20595 support for unnamed types in base_type_die. This can happen if this is
20596 an Ada subrange type. Correct solution is emit a subrange type die. */
20597 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
20598 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
20599 type = TREE_TYPE (type), code = TREE_CODE (type);
20601 if (code == ERROR_MARK
20602 /* Handle a special case. For functions whose return type is void, we
20603 generate *no* type attribute. (Note that no object may have type
20604 `void', so this only applies to function return types). */
20605 || code == VOID_TYPE)
20606 return;
20608 type_die = modified_type_die (type,
20609 cv_quals | TYPE_QUALS_NO_ADDR_SPACE (type),
20610 reverse,
20611 context_die);
20613 if (type_die != NULL)
20614 add_AT_die_ref (object_die, DW_AT_type, type_die);
20617 /* Given an object die, add the calling convention attribute for the
20618 function call type. */
20619 static void
20620 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
20622 enum dwarf_calling_convention value = DW_CC_normal;
20624 value = ((enum dwarf_calling_convention)
20625 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
20627 if (is_fortran ()
20628 && id_equal (DECL_ASSEMBLER_NAME (decl), "MAIN__"))
20630 /* DWARF 2 doesn't provide a way to identify a program's source-level
20631 entry point. DW_AT_calling_convention attributes are only meant
20632 to describe functions' calling conventions. However, lacking a
20633 better way to signal the Fortran main program, we used this for
20634 a long time, following existing custom. Now, DWARF 4 has
20635 DW_AT_main_subprogram, which we add below, but some tools still
20636 rely on the old way, which we thus keep. */
20637 value = DW_CC_program;
20639 if (dwarf_version >= 4 || !dwarf_strict)
20640 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
20643 /* Only add the attribute if the backend requests it, and
20644 is not DW_CC_normal. */
20645 if (value && (value != DW_CC_normal))
20646 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
20649 /* Given a tree pointer to a struct, class, union, or enum type node, return
20650 a pointer to the (string) tag name for the given type, or zero if the type
20651 was declared without a tag. */
20653 static const char *
20654 type_tag (const_tree type)
20656 const char *name = 0;
20658 if (TYPE_NAME (type) != 0)
20660 tree t = 0;
20662 /* Find the IDENTIFIER_NODE for the type name. */
20663 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
20664 && !TYPE_NAMELESS (type))
20665 t = TYPE_NAME (type);
20667 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
20668 a TYPE_DECL node, regardless of whether or not a `typedef' was
20669 involved. */
20670 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
20671 && ! DECL_IGNORED_P (TYPE_NAME (type)))
20673 /* We want to be extra verbose. Don't call dwarf_name if
20674 DECL_NAME isn't set. The default hook for decl_printable_name
20675 doesn't like that, and in this context it's correct to return
20676 0, instead of "<anonymous>" or the like. */
20677 if (DECL_NAME (TYPE_NAME (type))
20678 && !DECL_NAMELESS (TYPE_NAME (type)))
20679 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
20682 /* Now get the name as a string, or invent one. */
20683 if (!name && t != 0)
20684 name = IDENTIFIER_POINTER (t);
20687 return (name == 0 || *name == '\0') ? 0 : name;
20690 /* Return the type associated with a data member, make a special check
20691 for bit field types. */
20693 static inline tree
20694 member_declared_type (const_tree member)
20696 return (DECL_BIT_FIELD_TYPE (member)
20697 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
20700 /* Get the decl's label, as described by its RTL. This may be different
20701 from the DECL_NAME name used in the source file. */
20703 #if 0
20704 static const char *
20705 decl_start_label (tree decl)
20707 rtx x;
20708 const char *fnname;
20710 x = DECL_RTL (decl);
20711 gcc_assert (MEM_P (x));
20713 x = XEXP (x, 0);
20714 gcc_assert (GET_CODE (x) == SYMBOL_REF);
20716 fnname = XSTR (x, 0);
20717 return fnname;
20719 #endif
20721 /* For variable-length arrays that have been previously generated, but
20722 may be incomplete due to missing subscript info, fill the subscript
20723 info. Return TRUE if this is one of those cases. */
20724 static bool
20725 fill_variable_array_bounds (tree type)
20727 if (TREE_ASM_WRITTEN (type)
20728 && TREE_CODE (type) == ARRAY_TYPE
20729 && variably_modified_type_p (type, NULL))
20731 dw_die_ref array_die = lookup_type_die (type);
20732 if (!array_die)
20733 return false;
20734 add_subscript_info (array_die, type, !is_ada ());
20735 return true;
20737 return false;
20740 /* These routines generate the internal representation of the DIE's for
20741 the compilation unit. Debugging information is collected by walking
20742 the declaration trees passed in from dwarf2out_decl(). */
20744 static void
20745 gen_array_type_die (tree type, dw_die_ref context_die)
20747 dw_die_ref array_die;
20749 /* GNU compilers represent multidimensional array types as sequences of one
20750 dimensional array types whose element types are themselves array types.
20751 We sometimes squish that down to a single array_type DIE with multiple
20752 subscripts in the Dwarf debugging info. The draft Dwarf specification
20753 say that we are allowed to do this kind of compression in C, because
20754 there is no difference between an array of arrays and a multidimensional
20755 array. We don't do this for Ada to remain as close as possible to the
20756 actual representation, which is especially important against the language
20757 flexibilty wrt arrays of variable size. */
20759 bool collapse_nested_arrays = !is_ada ();
20761 if (fill_variable_array_bounds (type))
20762 return;
20764 dw_die_ref scope_die = scope_die_for (type, context_die);
20765 tree element_type;
20767 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
20768 DW_TAG_string_type doesn't have DW_AT_type attribute). */
20769 if (TYPE_STRING_FLAG (type)
20770 && TREE_CODE (type) == ARRAY_TYPE
20771 && is_fortran ()
20772 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
20774 HOST_WIDE_INT size;
20776 array_die = new_die (DW_TAG_string_type, scope_die, type);
20777 add_name_attribute (array_die, type_tag (type));
20778 equate_type_number_to_die (type, array_die);
20779 size = int_size_in_bytes (type);
20780 if (size >= 0)
20781 add_AT_unsigned (array_die, DW_AT_byte_size, size);
20782 else if (TYPE_DOMAIN (type) != NULL_TREE
20783 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE)
20785 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
20786 tree rszdecl = szdecl;
20788 size = int_size_in_bytes (TREE_TYPE (szdecl));
20789 if (!DECL_P (szdecl))
20791 if (TREE_CODE (szdecl) == INDIRECT_REF
20792 && DECL_P (TREE_OPERAND (szdecl, 0)))
20794 rszdecl = TREE_OPERAND (szdecl, 0);
20795 if (int_size_in_bytes (TREE_TYPE (rszdecl))
20796 != DWARF2_ADDR_SIZE)
20797 size = 0;
20799 else
20800 size = 0;
20802 if (size > 0)
20804 dw_loc_list_ref loc
20805 = loc_list_from_tree (rszdecl, szdecl == rszdecl ? 2 : 0,
20806 NULL);
20807 if (loc)
20809 add_AT_location_description (array_die, DW_AT_string_length,
20810 loc);
20811 if (size != DWARF2_ADDR_SIZE)
20812 add_AT_unsigned (array_die, dwarf_version >= 5
20813 ? DW_AT_string_length_byte_size
20814 : DW_AT_byte_size, size);
20818 return;
20821 array_die = new_die (DW_TAG_array_type, scope_die, type);
20822 add_name_attribute (array_die, type_tag (type));
20823 equate_type_number_to_die (type, array_die);
20825 if (TREE_CODE (type) == VECTOR_TYPE)
20826 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
20828 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
20829 if (is_fortran ()
20830 && TREE_CODE (type) == ARRAY_TYPE
20831 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
20832 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
20833 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
20835 #if 0
20836 /* We default the array ordering. SDB will probably do
20837 the right things even if DW_AT_ordering is not present. It's not even
20838 an issue until we start to get into multidimensional arrays anyway. If
20839 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
20840 then we'll have to put the DW_AT_ordering attribute back in. (But if
20841 and when we find out that we need to put these in, we will only do so
20842 for multidimensional arrays. */
20843 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
20844 #endif
20846 if (TREE_CODE (type) == VECTOR_TYPE)
20848 /* For VECTOR_TYPEs we use an array die with appropriate bounds. */
20849 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
20850 add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node, NULL);
20851 add_bound_info (subrange_die, DW_AT_upper_bound,
20852 size_int (TYPE_VECTOR_SUBPARTS (type) - 1), NULL);
20854 else
20855 add_subscript_info (array_die, type, collapse_nested_arrays);
20857 /* Add representation of the type of the elements of this array type and
20858 emit the corresponding DIE if we haven't done it already. */
20859 element_type = TREE_TYPE (type);
20860 if (collapse_nested_arrays)
20861 while (TREE_CODE (element_type) == ARRAY_TYPE)
20863 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
20864 break;
20865 element_type = TREE_TYPE (element_type);
20868 add_type_attribute (array_die, element_type, TYPE_UNQUALIFIED,
20869 TREE_CODE (type) == ARRAY_TYPE
20870 && TYPE_REVERSE_STORAGE_ORDER (type),
20871 context_die);
20873 add_gnat_descriptive_type_attribute (array_die, type, context_die);
20874 if (TYPE_ARTIFICIAL (type))
20875 add_AT_flag (array_die, DW_AT_artificial, 1);
20877 if (get_AT (array_die, DW_AT_name))
20878 add_pubtype (type, array_die);
20880 add_alignment_attribute (array_die, type);
20883 /* This routine generates DIE for array with hidden descriptor, details
20884 are filled into *info by a langhook. */
20886 static void
20887 gen_descr_array_type_die (tree type, struct array_descr_info *info,
20888 dw_die_ref context_die)
20890 const dw_die_ref scope_die = scope_die_for (type, context_die);
20891 const dw_die_ref array_die = new_die (DW_TAG_array_type, scope_die, type);
20892 struct loc_descr_context context = { type, info->base_decl, NULL,
20893 false, false };
20894 enum dwarf_tag subrange_tag = DW_TAG_subrange_type;
20895 int dim;
20897 add_name_attribute (array_die, type_tag (type));
20898 equate_type_number_to_die (type, array_die);
20900 if (info->ndimensions > 1)
20901 switch (info->ordering)
20903 case array_descr_ordering_row_major:
20904 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
20905 break;
20906 case array_descr_ordering_column_major:
20907 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
20908 break;
20909 default:
20910 break;
20913 if (dwarf_version >= 3 || !dwarf_strict)
20915 if (info->data_location)
20916 add_scalar_info (array_die, DW_AT_data_location, info->data_location,
20917 dw_scalar_form_exprloc, &context);
20918 if (info->associated)
20919 add_scalar_info (array_die, DW_AT_associated, info->associated,
20920 dw_scalar_form_constant
20921 | dw_scalar_form_exprloc
20922 | dw_scalar_form_reference, &context);
20923 if (info->allocated)
20924 add_scalar_info (array_die, DW_AT_allocated, info->allocated,
20925 dw_scalar_form_constant
20926 | dw_scalar_form_exprloc
20927 | dw_scalar_form_reference, &context);
20928 if (info->stride)
20930 const enum dwarf_attribute attr
20931 = (info->stride_in_bits) ? DW_AT_bit_stride : DW_AT_byte_stride;
20932 const int forms
20933 = (info->stride_in_bits)
20934 ? dw_scalar_form_constant
20935 : (dw_scalar_form_constant
20936 | dw_scalar_form_exprloc
20937 | dw_scalar_form_reference);
20939 add_scalar_info (array_die, attr, info->stride, forms, &context);
20942 if (dwarf_version >= 5)
20944 if (info->rank)
20946 add_scalar_info (array_die, DW_AT_rank, info->rank,
20947 dw_scalar_form_constant
20948 | dw_scalar_form_exprloc, &context);
20949 subrange_tag = DW_TAG_generic_subrange;
20950 context.placeholder_arg = true;
20954 add_gnat_descriptive_type_attribute (array_die, type, context_die);
20956 for (dim = 0; dim < info->ndimensions; dim++)
20958 dw_die_ref subrange_die = new_die (subrange_tag, array_die, NULL);
20960 if (info->dimen[dim].bounds_type)
20961 add_type_attribute (subrange_die,
20962 info->dimen[dim].bounds_type, TYPE_UNQUALIFIED,
20963 false, context_die);
20964 if (info->dimen[dim].lower_bound)
20965 add_bound_info (subrange_die, DW_AT_lower_bound,
20966 info->dimen[dim].lower_bound, &context);
20967 if (info->dimen[dim].upper_bound)
20968 add_bound_info (subrange_die, DW_AT_upper_bound,
20969 info->dimen[dim].upper_bound, &context);
20970 if ((dwarf_version >= 3 || !dwarf_strict) && info->dimen[dim].stride)
20971 add_scalar_info (subrange_die, DW_AT_byte_stride,
20972 info->dimen[dim].stride,
20973 dw_scalar_form_constant
20974 | dw_scalar_form_exprloc
20975 | dw_scalar_form_reference,
20976 &context);
20979 gen_type_die (info->element_type, context_die);
20980 add_type_attribute (array_die, info->element_type, TYPE_UNQUALIFIED,
20981 TREE_CODE (type) == ARRAY_TYPE
20982 && TYPE_REVERSE_STORAGE_ORDER (type),
20983 context_die);
20985 if (get_AT (array_die, DW_AT_name))
20986 add_pubtype (type, array_die);
20988 add_alignment_attribute (array_die, type);
20991 #if 0
20992 static void
20993 gen_entry_point_die (tree decl, dw_die_ref context_die)
20995 tree origin = decl_ultimate_origin (decl);
20996 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
20998 if (origin != NULL)
20999 add_abstract_origin_attribute (decl_die, origin);
21000 else
21002 add_name_and_src_coords_attributes (decl_die, decl);
21003 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
21004 TYPE_UNQUALIFIED, false, context_die);
21007 if (DECL_ABSTRACT_P (decl))
21008 equate_decl_number_to_die (decl, decl_die);
21009 else
21010 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
21012 #endif
21014 /* Walk through the list of incomplete types again, trying once more to
21015 emit full debugging info for them. */
21017 static void
21018 retry_incomplete_types (void)
21020 set_early_dwarf s;
21021 int i;
21023 for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
21024 if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
21025 gen_type_die ((*incomplete_types)[i], comp_unit_die ());
21026 vec_safe_truncate (incomplete_types, 0);
21029 /* Determine what tag to use for a record type. */
21031 static enum dwarf_tag
21032 record_type_tag (tree type)
21034 if (! lang_hooks.types.classify_record)
21035 return DW_TAG_structure_type;
21037 switch (lang_hooks.types.classify_record (type))
21039 case RECORD_IS_STRUCT:
21040 return DW_TAG_structure_type;
21042 case RECORD_IS_CLASS:
21043 return DW_TAG_class_type;
21045 case RECORD_IS_INTERFACE:
21046 if (dwarf_version >= 3 || !dwarf_strict)
21047 return DW_TAG_interface_type;
21048 return DW_TAG_structure_type;
21050 default:
21051 gcc_unreachable ();
21055 /* Generate a DIE to represent an enumeration type. Note that these DIEs
21056 include all of the information about the enumeration values also. Each
21057 enumerated type name/value is listed as a child of the enumerated type
21058 DIE. */
21060 static dw_die_ref
21061 gen_enumeration_type_die (tree type, dw_die_ref context_die)
21063 dw_die_ref type_die = lookup_type_die (type);
21065 if (type_die == NULL)
21067 type_die = new_die (DW_TAG_enumeration_type,
21068 scope_die_for (type, context_die), type);
21069 equate_type_number_to_die (type, type_die);
21070 add_name_attribute (type_die, type_tag (type));
21071 if (dwarf_version >= 4 || !dwarf_strict)
21073 if (ENUM_IS_SCOPED (type))
21074 add_AT_flag (type_die, DW_AT_enum_class, 1);
21075 if (ENUM_IS_OPAQUE (type))
21076 add_AT_flag (type_die, DW_AT_declaration, 1);
21078 if (!dwarf_strict)
21079 add_AT_unsigned (type_die, DW_AT_encoding,
21080 TYPE_UNSIGNED (type)
21081 ? DW_ATE_unsigned
21082 : DW_ATE_signed);
21084 else if (! TYPE_SIZE (type))
21085 return type_die;
21086 else
21087 remove_AT (type_die, DW_AT_declaration);
21089 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
21090 given enum type is incomplete, do not generate the DW_AT_byte_size
21091 attribute or the DW_AT_element_list attribute. */
21092 if (TYPE_SIZE (type))
21094 tree link;
21096 TREE_ASM_WRITTEN (type) = 1;
21097 add_byte_size_attribute (type_die, type);
21098 add_alignment_attribute (type_die, type);
21099 if (dwarf_version >= 3 || !dwarf_strict)
21101 tree underlying = lang_hooks.types.enum_underlying_base_type (type);
21102 add_type_attribute (type_die, underlying, TYPE_UNQUALIFIED, false,
21103 context_die);
21105 if (TYPE_STUB_DECL (type) != NULL_TREE)
21107 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
21108 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
21111 /* If the first reference to this type was as the return type of an
21112 inline function, then it may not have a parent. Fix this now. */
21113 if (type_die->die_parent == NULL)
21114 add_child_die (scope_die_for (type, context_die), type_die);
21116 for (link = TYPE_VALUES (type);
21117 link != NULL; link = TREE_CHAIN (link))
21119 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
21120 tree value = TREE_VALUE (link);
21122 add_name_attribute (enum_die,
21123 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
21125 if (TREE_CODE (value) == CONST_DECL)
21126 value = DECL_INITIAL (value);
21128 if (simple_type_size_in_bits (TREE_TYPE (value))
21129 <= HOST_BITS_PER_WIDE_INT || tree_fits_shwi_p (value))
21131 /* For constant forms created by add_AT_unsigned DWARF
21132 consumers (GDB, elfutils, etc.) always zero extend
21133 the value. Only when the actual value is negative
21134 do we need to use add_AT_int to generate a constant
21135 form that can represent negative values. */
21136 HOST_WIDE_INT val = TREE_INT_CST_LOW (value);
21137 if (TYPE_UNSIGNED (TREE_TYPE (value)) || val >= 0)
21138 add_AT_unsigned (enum_die, DW_AT_const_value,
21139 (unsigned HOST_WIDE_INT) val);
21140 else
21141 add_AT_int (enum_die, DW_AT_const_value, val);
21143 else
21144 /* Enumeration constants may be wider than HOST_WIDE_INT. Handle
21145 that here. TODO: This should be re-worked to use correct
21146 signed/unsigned double tags for all cases. */
21147 add_AT_wide (enum_die, DW_AT_const_value, value);
21150 add_gnat_descriptive_type_attribute (type_die, type, context_die);
21151 if (TYPE_ARTIFICIAL (type))
21152 add_AT_flag (type_die, DW_AT_artificial, 1);
21154 else
21155 add_AT_flag (type_die, DW_AT_declaration, 1);
21157 add_alignment_attribute (type_die, type);
21159 add_pubtype (type, type_die);
21161 return type_die;
21164 /* Generate a DIE to represent either a real live formal parameter decl or to
21165 represent just the type of some formal parameter position in some function
21166 type.
21168 Note that this routine is a bit unusual because its argument may be a
21169 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
21170 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
21171 node. If it's the former then this function is being called to output a
21172 DIE to represent a formal parameter object (or some inlining thereof). If
21173 it's the latter, then this function is only being called to output a
21174 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
21175 argument type of some subprogram type.
21176 If EMIT_NAME_P is true, name and source coordinate attributes
21177 are emitted. */
21179 static dw_die_ref
21180 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
21181 dw_die_ref context_die)
21183 tree node_or_origin = node ? node : origin;
21184 tree ultimate_origin;
21185 dw_die_ref parm_die = NULL;
21187 if (TREE_CODE_CLASS (TREE_CODE (node_or_origin)) == tcc_declaration)
21189 parm_die = lookup_decl_die (node);
21191 /* If the contexts differ, we may not be talking about the same
21192 thing. */
21193 if (parm_die && parm_die->die_parent != context_die)
21195 if (!DECL_ABSTRACT_P (node))
21197 /* This can happen when creating an inlined instance, in
21198 which case we need to create a new DIE that will get
21199 annotated with DW_AT_abstract_origin. */
21200 parm_die = NULL;
21202 else
21204 /* FIXME: Reuse DIE even with a differing context.
21206 This can happen when calling
21207 dwarf2out_abstract_function to build debug info for
21208 the abstract instance of a function for which we have
21209 already generated a DIE in
21210 dwarf2out_early_global_decl.
21212 Once we remove dwarf2out_abstract_function, we should
21213 have a call to gcc_unreachable here. */
21217 if (parm_die && parm_die->die_parent == NULL)
21219 /* Check that parm_die already has the right attributes that
21220 we would have added below. If any attributes are
21221 missing, fall through to add them. */
21222 if (! DECL_ABSTRACT_P (node_or_origin)
21223 && !get_AT (parm_die, DW_AT_location)
21224 && !get_AT (parm_die, DW_AT_const_value))
21225 /* We are missing location info, and are about to add it. */
21227 else
21229 add_child_die (context_die, parm_die);
21230 return parm_die;
21235 /* If we have a previously generated DIE, use it, unless this is an
21236 concrete instance (origin != NULL), in which case we need a new
21237 DIE with a corresponding DW_AT_abstract_origin. */
21238 bool reusing_die;
21239 if (parm_die && origin == NULL)
21240 reusing_die = true;
21241 else
21243 parm_die = new_die (DW_TAG_formal_parameter, context_die, node);
21244 reusing_die = false;
21247 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
21249 case tcc_declaration:
21250 ultimate_origin = decl_ultimate_origin (node_or_origin);
21251 if (node || ultimate_origin)
21252 origin = ultimate_origin;
21254 if (reusing_die)
21255 goto add_location;
21257 if (origin != NULL)
21258 add_abstract_origin_attribute (parm_die, origin);
21259 else if (emit_name_p)
21260 add_name_and_src_coords_attributes (parm_die, node);
21261 if (origin == NULL
21262 || (! DECL_ABSTRACT_P (node_or_origin)
21263 && variably_modified_type_p (TREE_TYPE (node_or_origin),
21264 decl_function_context
21265 (node_or_origin))))
21267 tree type = TREE_TYPE (node_or_origin);
21268 if (decl_by_reference_p (node_or_origin))
21269 add_type_attribute (parm_die, TREE_TYPE (type),
21270 TYPE_UNQUALIFIED,
21271 false, context_die);
21272 else
21273 add_type_attribute (parm_die, type,
21274 decl_quals (node_or_origin),
21275 false, context_die);
21277 if (origin == NULL && DECL_ARTIFICIAL (node))
21278 add_AT_flag (parm_die, DW_AT_artificial, 1);
21279 add_location:
21280 if (node && node != origin)
21281 equate_decl_number_to_die (node, parm_die);
21282 if (! DECL_ABSTRACT_P (node_or_origin))
21283 add_location_or_const_value_attribute (parm_die, node_or_origin,
21284 node == NULL);
21286 break;
21288 case tcc_type:
21289 /* We were called with some kind of a ..._TYPE node. */
21290 add_type_attribute (parm_die, node_or_origin, TYPE_UNQUALIFIED, false,
21291 context_die);
21292 break;
21294 default:
21295 gcc_unreachable ();
21298 return parm_die;
21301 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
21302 children DW_TAG_formal_parameter DIEs representing the arguments of the
21303 parameter pack.
21305 PARM_PACK must be a function parameter pack.
21306 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
21307 must point to the subsequent arguments of the function PACK_ARG belongs to.
21308 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
21309 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
21310 following the last one for which a DIE was generated. */
21312 static dw_die_ref
21313 gen_formal_parameter_pack_die (tree parm_pack,
21314 tree pack_arg,
21315 dw_die_ref subr_die,
21316 tree *next_arg)
21318 tree arg;
21319 dw_die_ref parm_pack_die;
21321 gcc_assert (parm_pack
21322 && lang_hooks.function_parameter_pack_p (parm_pack)
21323 && subr_die);
21325 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
21326 add_src_coords_attributes (parm_pack_die, parm_pack);
21328 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
21330 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
21331 parm_pack))
21332 break;
21333 gen_formal_parameter_die (arg, NULL,
21334 false /* Don't emit name attribute. */,
21335 parm_pack_die);
21337 if (next_arg)
21338 *next_arg = arg;
21339 return parm_pack_die;
21342 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
21343 at the end of an (ANSI prototyped) formal parameters list. */
21345 static void
21346 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
21348 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
21351 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
21352 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
21353 parameters as specified in some function type specification (except for
21354 those which appear as part of a function *definition*). */
21356 static void
21357 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
21359 tree link;
21360 tree formal_type = NULL;
21361 tree first_parm_type;
21362 tree arg;
21364 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
21366 arg = DECL_ARGUMENTS (function_or_method_type);
21367 function_or_method_type = TREE_TYPE (function_or_method_type);
21369 else
21370 arg = NULL_TREE;
21372 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
21374 /* Make our first pass over the list of formal parameter types and output a
21375 DW_TAG_formal_parameter DIE for each one. */
21376 for (link = first_parm_type; link; )
21378 dw_die_ref parm_die;
21380 formal_type = TREE_VALUE (link);
21381 if (formal_type == void_type_node)
21382 break;
21384 /* Output a (nameless) DIE to represent the formal parameter itself. */
21385 if (!POINTER_BOUNDS_TYPE_P (formal_type))
21387 parm_die = gen_formal_parameter_die (formal_type, NULL,
21388 true /* Emit name attribute. */,
21389 context_die);
21390 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
21391 && link == first_parm_type)
21393 add_AT_flag (parm_die, DW_AT_artificial, 1);
21394 if (dwarf_version >= 3 || !dwarf_strict)
21395 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
21397 else if (arg && DECL_ARTIFICIAL (arg))
21398 add_AT_flag (parm_die, DW_AT_artificial, 1);
21401 link = TREE_CHAIN (link);
21402 if (arg)
21403 arg = DECL_CHAIN (arg);
21406 /* If this function type has an ellipsis, add a
21407 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
21408 if (formal_type != void_type_node)
21409 gen_unspecified_parameters_die (function_or_method_type, context_die);
21411 /* Make our second (and final) pass over the list of formal parameter types
21412 and output DIEs to represent those types (as necessary). */
21413 for (link = TYPE_ARG_TYPES (function_or_method_type);
21414 link && TREE_VALUE (link);
21415 link = TREE_CHAIN (link))
21416 gen_type_die (TREE_VALUE (link), context_die);
21419 /* We want to generate the DIE for TYPE so that we can generate the
21420 die for MEMBER, which has been defined; we will need to refer back
21421 to the member declaration nested within TYPE. If we're trying to
21422 generate minimal debug info for TYPE, processing TYPE won't do the
21423 trick; we need to attach the member declaration by hand. */
21425 static void
21426 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
21428 gen_type_die (type, context_die);
21430 /* If we're trying to avoid duplicate debug info, we may not have
21431 emitted the member decl for this function. Emit it now. */
21432 if (TYPE_STUB_DECL (type)
21433 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
21434 && ! lookup_decl_die (member))
21436 dw_die_ref type_die;
21437 gcc_assert (!decl_ultimate_origin (member));
21439 push_decl_scope (type);
21440 type_die = lookup_type_die_strip_naming_typedef (type);
21441 if (TREE_CODE (member) == FUNCTION_DECL)
21442 gen_subprogram_die (member, type_die);
21443 else if (TREE_CODE (member) == FIELD_DECL)
21445 /* Ignore the nameless fields that are used to skip bits but handle
21446 C++ anonymous unions and structs. */
21447 if (DECL_NAME (member) != NULL_TREE
21448 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
21449 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
21451 struct vlr_context vlr_ctx = {
21452 DECL_CONTEXT (member), /* struct_type */
21453 NULL_TREE /* variant_part_offset */
21455 gen_type_die (member_declared_type (member), type_die);
21456 gen_field_die (member, &vlr_ctx, type_die);
21459 else
21460 gen_variable_die (member, NULL_TREE, type_die);
21462 pop_decl_scope ();
21466 /* Forward declare these functions, because they are mutually recursive
21467 with their set_block_* pairing functions. */
21468 static void set_decl_origin_self (tree);
21469 static void set_decl_abstract_flags (tree, vec<tree> &);
21471 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
21472 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
21473 that it points to the node itself, thus indicating that the node is its
21474 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
21475 the given node is NULL, recursively descend the decl/block tree which
21476 it is the root of, and for each other ..._DECL or BLOCK node contained
21477 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
21478 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
21479 values to point to themselves. */
21481 static void
21482 set_block_origin_self (tree stmt)
21484 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
21486 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
21489 tree local_decl;
21491 for (local_decl = BLOCK_VARS (stmt);
21492 local_decl != NULL_TREE;
21493 local_decl = DECL_CHAIN (local_decl))
21494 /* Do not recurse on nested functions since the inlining status
21495 of parent and child can be different as per the DWARF spec. */
21496 if (TREE_CODE (local_decl) != FUNCTION_DECL
21497 && !DECL_EXTERNAL (local_decl))
21498 set_decl_origin_self (local_decl);
21502 tree subblock;
21504 for (subblock = BLOCK_SUBBLOCKS (stmt);
21505 subblock != NULL_TREE;
21506 subblock = BLOCK_CHAIN (subblock))
21507 set_block_origin_self (subblock); /* Recurse. */
21512 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
21513 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
21514 node to so that it points to the node itself, thus indicating that the
21515 node represents its own (abstract) origin. Additionally, if the
21516 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
21517 the decl/block tree of which the given node is the root of, and for
21518 each other ..._DECL or BLOCK node contained therein whose
21519 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
21520 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
21521 point to themselves. */
21523 static void
21524 set_decl_origin_self (tree decl)
21526 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
21528 DECL_ABSTRACT_ORIGIN (decl) = decl;
21529 if (TREE_CODE (decl) == FUNCTION_DECL)
21531 tree arg;
21533 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
21534 DECL_ABSTRACT_ORIGIN (arg) = arg;
21535 if (DECL_INITIAL (decl) != NULL_TREE
21536 && DECL_INITIAL (decl) != error_mark_node)
21537 set_block_origin_self (DECL_INITIAL (decl));
21542 /* Given a pointer to some BLOCK node, set the BLOCK_ABSTRACT flag to 1
21543 and if it wasn't 1 before, push it to abstract_vec vector.
21544 For all local decls and all local sub-blocks (recursively) do it
21545 too. */
21547 static void
21548 set_block_abstract_flags (tree stmt, vec<tree> &abstract_vec)
21550 tree local_decl;
21551 tree subblock;
21552 unsigned int i;
21554 if (!BLOCK_ABSTRACT (stmt))
21556 abstract_vec.safe_push (stmt);
21557 BLOCK_ABSTRACT (stmt) = 1;
21560 for (local_decl = BLOCK_VARS (stmt);
21561 local_decl != NULL_TREE;
21562 local_decl = DECL_CHAIN (local_decl))
21563 if (! DECL_EXTERNAL (local_decl))
21564 set_decl_abstract_flags (local_decl, abstract_vec);
21566 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
21568 local_decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
21569 if ((VAR_P (local_decl) && !TREE_STATIC (local_decl))
21570 || TREE_CODE (local_decl) == PARM_DECL)
21571 set_decl_abstract_flags (local_decl, abstract_vec);
21574 for (subblock = BLOCK_SUBBLOCKS (stmt);
21575 subblock != NULL_TREE;
21576 subblock = BLOCK_CHAIN (subblock))
21577 set_block_abstract_flags (subblock, abstract_vec);
21580 /* Given a pointer to some ..._DECL node, set DECL_ABSTRACT_P flag on it
21581 to 1 and if it wasn't 1 before, push to abstract_vec vector.
21582 In the case where the decl is a FUNCTION_DECL also set the abstract
21583 flags for all of the parameters, local vars, local
21584 blocks and sub-blocks (recursively). */
21586 static void
21587 set_decl_abstract_flags (tree decl, vec<tree> &abstract_vec)
21589 if (!DECL_ABSTRACT_P (decl))
21591 abstract_vec.safe_push (decl);
21592 DECL_ABSTRACT_P (decl) = 1;
21595 if (TREE_CODE (decl) == FUNCTION_DECL)
21597 tree arg;
21599 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
21600 if (!DECL_ABSTRACT_P (arg))
21602 abstract_vec.safe_push (arg);
21603 DECL_ABSTRACT_P (arg) = 1;
21605 if (DECL_INITIAL (decl) != NULL_TREE
21606 && DECL_INITIAL (decl) != error_mark_node)
21607 set_block_abstract_flags (DECL_INITIAL (decl), abstract_vec);
21611 /* Generate the DWARF2 info for the "abstract" instance of a function which we
21612 may later generate inlined and/or out-of-line instances of.
21614 FIXME: In the early-dwarf world, this function, and most of the
21615 DECL_ABSTRACT code should be obsoleted. The early DIE _is_
21616 the abstract instance. All we would need to do is annotate
21617 the early DIE with the appropriate DW_AT_inline in late
21618 dwarf (perhaps in gen_inlined_subroutine_die).
21620 However, we can't do this yet, because LTO streaming of DIEs
21621 has not been implemented yet. */
21623 static void
21624 dwarf2out_abstract_function (tree decl)
21626 dw_die_ref old_die;
21627 tree save_fn;
21628 tree context;
21629 hash_table<decl_loc_hasher> *old_decl_loc_table;
21630 hash_table<dw_loc_list_hasher> *old_cached_dw_loc_list_table;
21631 int old_call_site_count, old_tail_call_site_count;
21632 struct call_arg_loc_node *old_call_arg_locations;
21634 /* Make sure we have the actual abstract inline, not a clone. */
21635 decl = DECL_ORIGIN (decl);
21637 old_die = lookup_decl_die (decl);
21638 if (old_die && get_AT (old_die, DW_AT_inline))
21639 /* We've already generated the abstract instance. */
21640 return;
21642 /* We can be called while recursively when seeing block defining inlined subroutine
21643 DIE. Be sure to not clobber the outer location table nor use it or we would
21644 get locations in abstract instantces. */
21645 old_decl_loc_table = decl_loc_table;
21646 decl_loc_table = NULL;
21647 old_cached_dw_loc_list_table = cached_dw_loc_list_table;
21648 cached_dw_loc_list_table = NULL;
21649 old_call_arg_locations = call_arg_locations;
21650 call_arg_locations = NULL;
21651 old_call_site_count = call_site_count;
21652 call_site_count = -1;
21653 old_tail_call_site_count = tail_call_site_count;
21654 tail_call_site_count = -1;
21656 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
21657 we don't get confused by DECL_ABSTRACT_P. */
21658 if (debug_info_level > DINFO_LEVEL_TERSE)
21660 context = decl_class_context (decl);
21661 if (context)
21662 gen_type_die_for_member
21663 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
21666 /* Pretend we've just finished compiling this function. */
21667 save_fn = current_function_decl;
21668 current_function_decl = decl;
21670 auto_vec<tree, 64> abstract_vec;
21671 set_decl_abstract_flags (decl, abstract_vec);
21672 dwarf2out_decl (decl);
21673 unsigned int i;
21674 tree t;
21675 FOR_EACH_VEC_ELT (abstract_vec, i, t)
21676 if (TREE_CODE (t) == BLOCK)
21677 BLOCK_ABSTRACT (t) = 0;
21678 else
21679 DECL_ABSTRACT_P (t) = 0;
21681 current_function_decl = save_fn;
21682 decl_loc_table = old_decl_loc_table;
21683 cached_dw_loc_list_table = old_cached_dw_loc_list_table;
21684 call_arg_locations = old_call_arg_locations;
21685 call_site_count = old_call_site_count;
21686 tail_call_site_count = old_tail_call_site_count;
21689 /* Helper function of premark_used_types() which gets called through
21690 htab_traverse.
21692 Marks the DIE of a given type in *SLOT as perennial, so it never gets
21693 marked as unused by prune_unused_types. */
21695 bool
21696 premark_used_types_helper (tree const &type, void *)
21698 dw_die_ref die;
21700 die = lookup_type_die (type);
21701 if (die != NULL)
21702 die->die_perennial_p = 1;
21703 return true;
21706 /* Helper function of premark_types_used_by_global_vars which gets called
21707 through htab_traverse.
21709 Marks the DIE of a given type in *SLOT as perennial, so it never gets
21710 marked as unused by prune_unused_types. The DIE of the type is marked
21711 only if the global variable using the type will actually be emitted. */
21714 premark_types_used_by_global_vars_helper (types_used_by_vars_entry **slot,
21715 void *)
21717 struct types_used_by_vars_entry *entry;
21718 dw_die_ref die;
21720 entry = (struct types_used_by_vars_entry *) *slot;
21721 gcc_assert (entry->type != NULL
21722 && entry->var_decl != NULL);
21723 die = lookup_type_die (entry->type);
21724 if (die)
21726 /* Ask cgraph if the global variable really is to be emitted.
21727 If yes, then we'll keep the DIE of ENTRY->TYPE. */
21728 varpool_node *node = varpool_node::get (entry->var_decl);
21729 if (node && node->definition)
21731 die->die_perennial_p = 1;
21732 /* Keep the parent DIEs as well. */
21733 while ((die = die->die_parent) && die->die_perennial_p == 0)
21734 die->die_perennial_p = 1;
21737 return 1;
21740 /* Mark all members of used_types_hash as perennial. */
21742 static void
21743 premark_used_types (struct function *fun)
21745 if (fun && fun->used_types_hash)
21746 fun->used_types_hash->traverse<void *, premark_used_types_helper> (NULL);
21749 /* Mark all members of types_used_by_vars_entry as perennial. */
21751 static void
21752 premark_types_used_by_global_vars (void)
21754 if (types_used_by_vars_hash)
21755 types_used_by_vars_hash
21756 ->traverse<void *, premark_types_used_by_global_vars_helper> (NULL);
21759 /* Generate a DW_TAG_call_site DIE in function DECL under SUBR_DIE
21760 for CA_LOC call arg loc node. */
21762 static dw_die_ref
21763 gen_call_site_die (tree decl, dw_die_ref subr_die,
21764 struct call_arg_loc_node *ca_loc)
21766 dw_die_ref stmt_die = NULL, die;
21767 tree block = ca_loc->block;
21769 while (block
21770 && block != DECL_INITIAL (decl)
21771 && TREE_CODE (block) == BLOCK)
21773 stmt_die = BLOCK_DIE (block);
21774 if (stmt_die)
21775 break;
21776 block = BLOCK_SUPERCONTEXT (block);
21778 if (stmt_die == NULL)
21779 stmt_die = subr_die;
21780 die = new_die (dwarf_TAG (DW_TAG_call_site), stmt_die, NULL_TREE);
21781 add_AT_lbl_id (die, dwarf_AT (DW_AT_call_return_pc), ca_loc->label);
21782 if (ca_loc->tail_call_p)
21783 add_AT_flag (die, dwarf_AT (DW_AT_call_tail_call), 1);
21784 if (ca_loc->symbol_ref)
21786 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
21787 if (tdie)
21788 add_AT_die_ref (die, dwarf_AT (DW_AT_call_origin), tdie);
21789 else
21790 add_AT_addr (die, dwarf_AT (DW_AT_call_origin), ca_loc->symbol_ref,
21791 false);
21793 return die;
21796 /* Generate a DIE to represent a declared function (either file-scope or
21797 block-local). */
21799 static void
21800 gen_subprogram_die (tree decl, dw_die_ref context_die)
21802 tree origin = decl_ultimate_origin (decl);
21803 dw_die_ref subr_die;
21804 dw_die_ref old_die = lookup_decl_die (decl);
21806 /* This function gets called multiple times for different stages of
21807 the debug process. For example, for func() in this code:
21809 namespace S
21811 void func() { ... }
21814 ...we get called 4 times. Twice in early debug and twice in
21815 late debug:
21817 Early debug
21818 -----------
21820 1. Once while generating func() within the namespace. This is
21821 the declaration. The declaration bit below is set, as the
21822 context is the namespace.
21824 A new DIE will be generated with DW_AT_declaration set.
21826 2. Once for func() itself. This is the specification. The
21827 declaration bit below is clear as the context is the CU.
21829 We will use the cached DIE from (1) to create a new DIE with
21830 DW_AT_specification pointing to the declaration in (1).
21832 Late debug via rest_of_handle_final()
21833 -------------------------------------
21835 3. Once generating func() within the namespace. This is also the
21836 declaration, as in (1), but this time we will early exit below
21837 as we have a cached DIE and a declaration needs no additional
21838 annotations (no locations), as the source declaration line
21839 info is enough.
21841 4. Once for func() itself. As in (2), this is the specification,
21842 but this time we will re-use the cached DIE, and just annotate
21843 it with the location information that should now be available.
21845 For something without namespaces, but with abstract instances, we
21846 are also called a multiple times:
21848 class Base
21850 public:
21851 Base (); // constructor declaration (1)
21854 Base::Base () { } // constructor specification (2)
21856 Early debug
21857 -----------
21859 1. Once for the Base() constructor by virtue of it being a
21860 member of the Base class. This is done via
21861 rest_of_type_compilation.
21863 This is a declaration, so a new DIE will be created with
21864 DW_AT_declaration.
21866 2. Once for the Base() constructor definition, but this time
21867 while generating the abstract instance of the base
21868 constructor (__base_ctor) which is being generated via early
21869 debug of reachable functions.
21871 Even though we have a cached version of the declaration (1),
21872 we will create a DW_AT_specification of the declaration DIE
21873 in (1).
21875 3. Once for the __base_ctor itself, but this time, we generate
21876 an DW_AT_abstract_origin version of the DW_AT_specification in
21877 (2).
21879 Late debug via rest_of_handle_final
21880 -----------------------------------
21882 4. One final time for the __base_ctor (which will have a cached
21883 DIE with DW_AT_abstract_origin created in (3). This time,
21884 we will just annotate the location information now
21885 available.
21887 int declaration = (current_function_decl != decl
21888 || class_or_namespace_scope_p (context_die));
21890 /* Now that the C++ front end lazily declares artificial member fns, we
21891 might need to retrofit the declaration into its class. */
21892 if (!declaration && !origin && !old_die
21893 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
21894 && !class_or_namespace_scope_p (context_die)
21895 && debug_info_level > DINFO_LEVEL_TERSE)
21896 old_die = force_decl_die (decl);
21898 /* An inlined instance, tag a new DIE with DW_AT_abstract_origin. */
21899 if (origin != NULL)
21901 gcc_assert (!declaration || local_scope_p (context_die));
21903 /* Fixup die_parent for the abstract instance of a nested
21904 inline function. */
21905 if (old_die && old_die->die_parent == NULL)
21906 add_child_die (context_die, old_die);
21908 if (old_die && get_AT_ref (old_die, DW_AT_abstract_origin))
21910 /* If we have a DW_AT_abstract_origin we have a working
21911 cached version. */
21912 subr_die = old_die;
21914 else
21916 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
21917 add_abstract_origin_attribute (subr_die, origin);
21918 /* This is where the actual code for a cloned function is.
21919 Let's emit linkage name attribute for it. This helps
21920 debuggers to e.g, set breakpoints into
21921 constructors/destructors when the user asks "break
21922 K::K". */
21923 add_linkage_name (subr_die, decl);
21926 /* A cached copy, possibly from early dwarf generation. Reuse as
21927 much as possible. */
21928 else if (old_die)
21930 /* A declaration that has been previously dumped needs no
21931 additional information. */
21932 if (declaration)
21933 return;
21935 if (!get_AT_flag (old_die, DW_AT_declaration)
21936 /* We can have a normal definition following an inline one in the
21937 case of redefinition of GNU C extern inlines.
21938 It seems reasonable to use AT_specification in this case. */
21939 && !get_AT (old_die, DW_AT_inline))
21941 /* Detect and ignore this case, where we are trying to output
21942 something we have already output. */
21943 if (get_AT (old_die, DW_AT_low_pc)
21944 || get_AT (old_die, DW_AT_ranges))
21945 return;
21947 /* If we have no location information, this must be a
21948 partially generated DIE from early dwarf generation.
21949 Fall through and generate it. */
21952 /* If the definition comes from the same place as the declaration,
21953 maybe use the old DIE. We always want the DIE for this function
21954 that has the *_pc attributes to be under comp_unit_die so the
21955 debugger can find it. We also need to do this for abstract
21956 instances of inlines, since the spec requires the out-of-line copy
21957 to have the same parent. For local class methods, this doesn't
21958 apply; we just use the old DIE. */
21959 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
21960 struct dwarf_file_data * file_index = lookup_filename (s.file);
21961 if ((is_cu_die (old_die->die_parent)
21962 /* This condition fixes the inconsistency/ICE with the
21963 following Fortran test (or some derivative thereof) while
21964 building libgfortran:
21966 module some_m
21967 contains
21968 logical function funky (FLAG)
21969 funky = .true.
21970 end function
21971 end module
21973 || (old_die->die_parent
21974 && old_die->die_parent->die_tag == DW_TAG_module)
21975 || context_die == NULL)
21976 && (DECL_ARTIFICIAL (decl)
21977 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
21978 && (get_AT_unsigned (old_die, DW_AT_decl_line)
21979 == (unsigned) s.line)
21980 && (!debug_column_info
21981 || s.column == 0
21982 || (get_AT_unsigned (old_die, DW_AT_decl_column)
21983 == (unsigned) s.column)))))
21985 subr_die = old_die;
21987 /* Clear out the declaration attribute, but leave the
21988 parameters so they can be augmented with location
21989 information later. Unless this was a declaration, in
21990 which case, wipe out the nameless parameters and recreate
21991 them further down. */
21992 if (remove_AT (subr_die, DW_AT_declaration))
21995 remove_AT (subr_die, DW_AT_object_pointer);
21996 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
21999 /* Make a specification pointing to the previously built
22000 declaration. */
22001 else
22003 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
22004 add_AT_specification (subr_die, old_die);
22005 add_pubname (decl, subr_die);
22006 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
22007 add_AT_file (subr_die, DW_AT_decl_file, file_index);
22008 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
22009 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
22010 if (debug_column_info
22011 && s.column
22012 && (get_AT_unsigned (old_die, DW_AT_decl_column)
22013 != (unsigned) s.column))
22014 add_AT_unsigned (subr_die, DW_AT_decl_column, s.column);
22016 /* If the prototype had an 'auto' or 'decltype(auto)' return type,
22017 emit the real type on the definition die. */
22018 if (is_cxx () && debug_info_level > DINFO_LEVEL_TERSE)
22020 dw_die_ref die = get_AT_ref (old_die, DW_AT_type);
22021 if (die == auto_die || die == decltype_auto_die)
22022 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
22023 TYPE_UNQUALIFIED, false, context_die);
22026 /* When we process the method declaration, we haven't seen
22027 the out-of-class defaulted definition yet, so we have to
22028 recheck now. */
22029 if ((dwarf_version >= 5 || ! dwarf_strict)
22030 && !get_AT (subr_die, DW_AT_defaulted))
22032 int defaulted
22033 = lang_hooks.decls.decl_dwarf_attribute (decl,
22034 DW_AT_defaulted);
22035 if (defaulted != -1)
22037 /* Other values must have been handled before. */
22038 gcc_assert (defaulted == DW_DEFAULTED_out_of_class);
22039 add_AT_unsigned (subr_die, DW_AT_defaulted, defaulted);
22044 /* Create a fresh DIE for anything else. */
22045 else
22047 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
22049 if (TREE_PUBLIC (decl))
22050 add_AT_flag (subr_die, DW_AT_external, 1);
22052 add_name_and_src_coords_attributes (subr_die, decl);
22053 add_pubname (decl, subr_die);
22054 if (debug_info_level > DINFO_LEVEL_TERSE)
22056 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
22057 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
22058 TYPE_UNQUALIFIED, false, context_die);
22061 add_pure_or_virtual_attribute (subr_die, decl);
22062 if (DECL_ARTIFICIAL (decl))
22063 add_AT_flag (subr_die, DW_AT_artificial, 1);
22065 if (TREE_THIS_VOLATILE (decl) && (dwarf_version >= 5 || !dwarf_strict))
22066 add_AT_flag (subr_die, DW_AT_noreturn, 1);
22068 add_alignment_attribute (subr_die, decl);
22070 add_accessibility_attribute (subr_die, decl);
22073 /* Unless we have an existing non-declaration DIE, equate the new
22074 DIE. */
22075 if (!old_die || is_declaration_die (old_die))
22076 equate_decl_number_to_die (decl, subr_die);
22078 if (declaration)
22080 if (!old_die || !get_AT (old_die, DW_AT_inline))
22082 add_AT_flag (subr_die, DW_AT_declaration, 1);
22084 /* If this is an explicit function declaration then generate
22085 a DW_AT_explicit attribute. */
22086 if ((dwarf_version >= 3 || !dwarf_strict)
22087 && lang_hooks.decls.decl_dwarf_attribute (decl,
22088 DW_AT_explicit) == 1)
22089 add_AT_flag (subr_die, DW_AT_explicit, 1);
22091 /* If this is a C++11 deleted special function member then generate
22092 a DW_AT_deleted attribute. */
22093 if ((dwarf_version >= 5 || !dwarf_strict)
22094 && lang_hooks.decls.decl_dwarf_attribute (decl,
22095 DW_AT_deleted) == 1)
22096 add_AT_flag (subr_die, DW_AT_deleted, 1);
22098 /* If this is a C++11 defaulted special function member then
22099 generate a DW_AT_defaulted attribute. */
22100 if (dwarf_version >= 5 || !dwarf_strict)
22102 int defaulted
22103 = lang_hooks.decls.decl_dwarf_attribute (decl,
22104 DW_AT_defaulted);
22105 if (defaulted != -1)
22106 add_AT_unsigned (subr_die, DW_AT_defaulted, defaulted);
22109 /* If this is a C++11 non-static member function with & ref-qualifier
22110 then generate a DW_AT_reference attribute. */
22111 if ((dwarf_version >= 5 || !dwarf_strict)
22112 && lang_hooks.decls.decl_dwarf_attribute (decl,
22113 DW_AT_reference) == 1)
22114 add_AT_flag (subr_die, DW_AT_reference, 1);
22116 /* If this is a C++11 non-static member function with &&
22117 ref-qualifier then generate a DW_AT_reference attribute. */
22118 if ((dwarf_version >= 5 || !dwarf_strict)
22119 && lang_hooks.decls.decl_dwarf_attribute (decl,
22120 DW_AT_rvalue_reference)
22121 == 1)
22122 add_AT_flag (subr_die, DW_AT_rvalue_reference, 1);
22125 /* Tag abstract instances with DW_AT_inline. */
22126 else if (DECL_ABSTRACT_P (decl))
22128 if (DECL_DECLARED_INLINE_P (decl))
22130 if (cgraph_function_possibly_inlined_p (decl))
22131 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
22132 else
22133 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
22135 else
22137 if (cgraph_function_possibly_inlined_p (decl))
22138 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
22139 else
22140 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
22143 if (DECL_DECLARED_INLINE_P (decl)
22144 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
22145 add_AT_flag (subr_die, DW_AT_artificial, 1);
22147 /* For non DECL_EXTERNALs, if range information is available, fill
22148 the DIE with it. */
22149 else if (!DECL_EXTERNAL (decl) && !early_dwarf)
22151 HOST_WIDE_INT cfa_fb_offset;
22153 struct function *fun = DECL_STRUCT_FUNCTION (decl);
22155 if (!crtl->has_bb_partition)
22157 dw_fde_ref fde = fun->fde;
22158 if (fde->dw_fde_begin)
22160 /* We have already generated the labels. */
22161 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
22162 fde->dw_fde_end, false);
22164 else
22166 /* Create start/end labels and add the range. */
22167 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
22168 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
22169 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
22170 current_function_funcdef_no);
22171 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
22172 current_function_funcdef_no);
22173 add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
22174 false);
22177 #if VMS_DEBUGGING_INFO
22178 /* HP OpenVMS Industry Standard 64: DWARF Extensions
22179 Section 2.3 Prologue and Epilogue Attributes:
22180 When a breakpoint is set on entry to a function, it is generally
22181 desirable for execution to be suspended, not on the very first
22182 instruction of the function, but rather at a point after the
22183 function's frame has been set up, after any language defined local
22184 declaration processing has been completed, and before execution of
22185 the first statement of the function begins. Debuggers generally
22186 cannot properly determine where this point is. Similarly for a
22187 breakpoint set on exit from a function. The prologue and epilogue
22188 attributes allow a compiler to communicate the location(s) to use. */
22191 if (fde->dw_fde_vms_end_prologue)
22192 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
22193 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
22195 if (fde->dw_fde_vms_begin_epilogue)
22196 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
22197 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
22199 #endif
22202 else
22204 /* Generate pubnames entries for the split function code ranges. */
22205 dw_fde_ref fde = fun->fde;
22207 if (fde->dw_fde_second_begin)
22209 if (dwarf_version >= 3 || !dwarf_strict)
22211 /* We should use ranges for non-contiguous code section
22212 addresses. Use the actual code range for the initial
22213 section, since the HOT/COLD labels might precede an
22214 alignment offset. */
22215 bool range_list_added = false;
22216 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
22217 fde->dw_fde_end, &range_list_added,
22218 false);
22219 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
22220 fde->dw_fde_second_end,
22221 &range_list_added, false);
22222 if (range_list_added)
22223 add_ranges (NULL);
22225 else
22227 /* There is no real support in DW2 for this .. so we make
22228 a work-around. First, emit the pub name for the segment
22229 containing the function label. Then make and emit a
22230 simplified subprogram DIE for the second segment with the
22231 name pre-fixed by __hot/cold_sect_of_. We use the same
22232 linkage name for the second die so that gdb will find both
22233 sections when given "b foo". */
22234 const char *name = NULL;
22235 tree decl_name = DECL_NAME (decl);
22236 dw_die_ref seg_die;
22238 /* Do the 'primary' section. */
22239 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
22240 fde->dw_fde_end, false);
22242 /* Build a minimal DIE for the secondary section. */
22243 seg_die = new_die (DW_TAG_subprogram,
22244 subr_die->die_parent, decl);
22246 if (TREE_PUBLIC (decl))
22247 add_AT_flag (seg_die, DW_AT_external, 1);
22249 if (decl_name != NULL
22250 && IDENTIFIER_POINTER (decl_name) != NULL)
22252 name = dwarf2_name (decl, 1);
22253 if (! DECL_ARTIFICIAL (decl))
22254 add_src_coords_attributes (seg_die, decl);
22256 add_linkage_name (seg_die, decl);
22258 gcc_assert (name != NULL);
22259 add_pure_or_virtual_attribute (seg_die, decl);
22260 if (DECL_ARTIFICIAL (decl))
22261 add_AT_flag (seg_die, DW_AT_artificial, 1);
22263 name = concat ("__second_sect_of_", name, NULL);
22264 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
22265 fde->dw_fde_second_end, false);
22266 add_name_attribute (seg_die, name);
22267 if (want_pubnames ())
22268 add_pubname_string (name, seg_die);
22271 else
22272 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
22273 false);
22276 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
22278 /* We define the "frame base" as the function's CFA. This is more
22279 convenient for several reasons: (1) It's stable across the prologue
22280 and epilogue, which makes it better than just a frame pointer,
22281 (2) With dwarf3, there exists a one-byte encoding that allows us
22282 to reference the .debug_frame data by proxy, but failing that,
22283 (3) We can at least reuse the code inspection and interpretation
22284 code that determines the CFA position at various points in the
22285 function. */
22286 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
22288 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
22289 add_AT_loc (subr_die, DW_AT_frame_base, op);
22291 else
22293 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
22294 if (list->dw_loc_next)
22295 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
22296 else
22297 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
22300 /* Compute a displacement from the "steady-state frame pointer" to
22301 the CFA. The former is what all stack slots and argument slots
22302 will reference in the rtl; the latter is what we've told the
22303 debugger about. We'll need to adjust all frame_base references
22304 by this displacement. */
22305 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
22307 if (fun->static_chain_decl)
22309 /* DWARF requires here a location expression that computes the
22310 address of the enclosing subprogram's frame base. The machinery
22311 in tree-nested.c is supposed to store this specific address in the
22312 last field of the FRAME record. */
22313 const tree frame_type
22314 = TREE_TYPE (TREE_TYPE (fun->static_chain_decl));
22315 const tree fb_decl = tree_last (TYPE_FIELDS (frame_type));
22317 tree fb_expr
22318 = build1 (INDIRECT_REF, frame_type, fun->static_chain_decl);
22319 fb_expr = build3 (COMPONENT_REF, TREE_TYPE (fb_decl),
22320 fb_expr, fb_decl, NULL_TREE);
22322 add_AT_location_description (subr_die, DW_AT_static_link,
22323 loc_list_from_tree (fb_expr, 0, NULL));
22326 resolve_variable_values ();
22329 /* Generate child dies for template paramaters. */
22330 if (early_dwarf && debug_info_level > DINFO_LEVEL_TERSE)
22331 gen_generic_params_dies (decl);
22333 /* Now output descriptions of the arguments for this function. This gets
22334 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
22335 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
22336 `...' at the end of the formal parameter list. In order to find out if
22337 there was a trailing ellipsis or not, we must instead look at the type
22338 associated with the FUNCTION_DECL. This will be a node of type
22339 FUNCTION_TYPE. If the chain of type nodes hanging off of this
22340 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
22341 an ellipsis at the end. */
22343 /* In the case where we are describing a mere function declaration, all we
22344 need to do here (and all we *can* do here) is to describe the *types* of
22345 its formal parameters. */
22346 if (debug_info_level <= DINFO_LEVEL_TERSE)
22348 else if (declaration)
22349 gen_formal_types_die (decl, subr_die);
22350 else
22352 /* Generate DIEs to represent all known formal parameters. */
22353 tree parm = DECL_ARGUMENTS (decl);
22354 tree generic_decl = early_dwarf
22355 ? lang_hooks.decls.get_generic_function_decl (decl) : NULL;
22356 tree generic_decl_parm = generic_decl
22357 ? DECL_ARGUMENTS (generic_decl)
22358 : NULL;
22360 /* Now we want to walk the list of parameters of the function and
22361 emit their relevant DIEs.
22363 We consider the case of DECL being an instance of a generic function
22364 as well as it being a normal function.
22366 If DECL is an instance of a generic function we walk the
22367 parameters of the generic function declaration _and_ the parameters of
22368 DECL itself. This is useful because we want to emit specific DIEs for
22369 function parameter packs and those are declared as part of the
22370 generic function declaration. In that particular case,
22371 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
22372 That DIE has children DIEs representing the set of arguments
22373 of the pack. Note that the set of pack arguments can be empty.
22374 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
22375 children DIE.
22377 Otherwise, we just consider the parameters of DECL. */
22378 while (generic_decl_parm || parm)
22380 if (generic_decl_parm
22381 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
22382 gen_formal_parameter_pack_die (generic_decl_parm,
22383 parm, subr_die,
22384 &parm);
22385 else if (parm && !POINTER_BOUNDS_P (parm))
22387 dw_die_ref parm_die = gen_decl_die (parm, NULL, NULL, subr_die);
22389 if (parm == DECL_ARGUMENTS (decl)
22390 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
22391 && parm_die
22392 && (dwarf_version >= 3 || !dwarf_strict))
22393 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
22395 parm = DECL_CHAIN (parm);
22397 else if (parm)
22398 parm = DECL_CHAIN (parm);
22400 if (generic_decl_parm)
22401 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
22404 /* Decide whether we need an unspecified_parameters DIE at the end.
22405 There are 2 more cases to do this for: 1) the ansi ... declaration -
22406 this is detectable when the end of the arg list is not a
22407 void_type_node 2) an unprototyped function declaration (not a
22408 definition). This just means that we have no info about the
22409 parameters at all. */
22410 if (early_dwarf)
22412 if (prototype_p (TREE_TYPE (decl)))
22414 /* This is the prototyped case, check for.... */
22415 if (stdarg_p (TREE_TYPE (decl)))
22416 gen_unspecified_parameters_die (decl, subr_die);
22418 else if (DECL_INITIAL (decl) == NULL_TREE)
22419 gen_unspecified_parameters_die (decl, subr_die);
22423 if (subr_die != old_die)
22424 /* Add the calling convention attribute if requested. */
22425 add_calling_convention_attribute (subr_die, decl);
22427 /* Output Dwarf info for all of the stuff within the body of the function
22428 (if it has one - it may be just a declaration).
22430 OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
22431 a function. This BLOCK actually represents the outermost binding contour
22432 for the function, i.e. the contour in which the function's formal
22433 parameters and labels get declared. Curiously, it appears that the front
22434 end doesn't actually put the PARM_DECL nodes for the current function onto
22435 the BLOCK_VARS list for this outer scope, but are strung off of the
22436 DECL_ARGUMENTS list for the function instead.
22438 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
22439 the LABEL_DECL nodes for the function however, and we output DWARF info
22440 for those in decls_for_scope. Just within the `outer_scope' there will be
22441 a BLOCK node representing the function's outermost pair of curly braces,
22442 and any blocks used for the base and member initializers of a C++
22443 constructor function. */
22444 tree outer_scope = DECL_INITIAL (decl);
22445 if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
22447 int call_site_note_count = 0;
22448 int tail_call_site_note_count = 0;
22450 /* Emit a DW_TAG_variable DIE for a named return value. */
22451 if (DECL_NAME (DECL_RESULT (decl)))
22452 gen_decl_die (DECL_RESULT (decl), NULL, NULL, subr_die);
22454 /* The first time through decls_for_scope we will generate the
22455 DIEs for the locals. The second time, we fill in the
22456 location info. */
22457 decls_for_scope (outer_scope, subr_die);
22459 if (call_arg_locations && (!dwarf_strict || dwarf_version >= 5))
22461 struct call_arg_loc_node *ca_loc;
22462 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
22464 dw_die_ref die = NULL;
22465 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
22466 rtx arg, next_arg;
22468 for (arg = (ca_loc->call_arg_loc_note != NULL_RTX
22469 ? NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note)
22470 : NULL_RTX);
22471 arg; arg = next_arg)
22473 dw_loc_descr_ref reg, val;
22474 machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
22475 dw_die_ref cdie, tdie = NULL;
22477 next_arg = XEXP (arg, 1);
22478 if (REG_P (XEXP (XEXP (arg, 0), 0))
22479 && next_arg
22480 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
22481 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
22482 && REGNO (XEXP (XEXP (arg, 0), 0))
22483 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
22484 next_arg = XEXP (next_arg, 1);
22485 if (mode == VOIDmode)
22487 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
22488 if (mode == VOIDmode)
22489 mode = GET_MODE (XEXP (arg, 0));
22491 if (mode == VOIDmode || mode == BLKmode)
22492 continue;
22493 /* Get dynamic information about call target only if we
22494 have no static information: we cannot generate both
22495 DW_AT_call_origin and DW_AT_call_target
22496 attributes. */
22497 if (ca_loc->symbol_ref == NULL_RTX)
22499 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
22501 tloc = XEXP (XEXP (arg, 0), 1);
22502 continue;
22504 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
22505 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
22507 tlocc = XEXP (XEXP (arg, 0), 1);
22508 continue;
22511 reg = NULL;
22512 if (REG_P (XEXP (XEXP (arg, 0), 0)))
22513 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
22514 VAR_INIT_STATUS_INITIALIZED);
22515 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
22517 rtx mem = XEXP (XEXP (arg, 0), 0);
22518 reg = mem_loc_descriptor (XEXP (mem, 0),
22519 get_address_mode (mem),
22520 GET_MODE (mem),
22521 VAR_INIT_STATUS_INITIALIZED);
22523 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
22524 == DEBUG_PARAMETER_REF)
22526 tree tdecl
22527 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
22528 tdie = lookup_decl_die (tdecl);
22529 if (tdie == NULL)
22530 continue;
22532 else
22533 continue;
22534 if (reg == NULL
22535 && GET_CODE (XEXP (XEXP (arg, 0), 0))
22536 != DEBUG_PARAMETER_REF)
22537 continue;
22538 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
22539 VOIDmode,
22540 VAR_INIT_STATUS_INITIALIZED);
22541 if (val == NULL)
22542 continue;
22543 if (die == NULL)
22544 die = gen_call_site_die (decl, subr_die, ca_loc);
22545 cdie = new_die (dwarf_TAG (DW_TAG_call_site_parameter), die,
22546 NULL_TREE);
22547 if (reg != NULL)
22548 add_AT_loc (cdie, DW_AT_location, reg);
22549 else if (tdie != NULL)
22550 add_AT_die_ref (cdie, dwarf_AT (DW_AT_call_parameter),
22551 tdie);
22552 add_AT_loc (cdie, dwarf_AT (DW_AT_call_value), val);
22553 if (next_arg != XEXP (arg, 1))
22555 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
22556 if (mode == VOIDmode)
22557 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
22558 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
22559 0), 1),
22560 mode, VOIDmode,
22561 VAR_INIT_STATUS_INITIALIZED);
22562 if (val != NULL)
22563 add_AT_loc (cdie, dwarf_AT (DW_AT_call_data_value),
22564 val);
22567 if (die == NULL
22568 && (ca_loc->symbol_ref || tloc))
22569 die = gen_call_site_die (decl, subr_die, ca_loc);
22570 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
22572 dw_loc_descr_ref tval = NULL;
22574 if (tloc != NULL_RTX)
22575 tval = mem_loc_descriptor (tloc,
22576 GET_MODE (tloc) == VOIDmode
22577 ? Pmode : GET_MODE (tloc),
22578 VOIDmode,
22579 VAR_INIT_STATUS_INITIALIZED);
22580 if (tval)
22581 add_AT_loc (die, dwarf_AT (DW_AT_call_target), tval);
22582 else if (tlocc != NULL_RTX)
22584 tval = mem_loc_descriptor (tlocc,
22585 GET_MODE (tlocc) == VOIDmode
22586 ? Pmode : GET_MODE (tlocc),
22587 VOIDmode,
22588 VAR_INIT_STATUS_INITIALIZED);
22589 if (tval)
22590 add_AT_loc (die,
22591 dwarf_AT (DW_AT_call_target_clobbered),
22592 tval);
22595 if (die != NULL)
22597 call_site_note_count++;
22598 if (ca_loc->tail_call_p)
22599 tail_call_site_note_count++;
22603 call_arg_locations = NULL;
22604 call_arg_loc_last = NULL;
22605 if (tail_call_site_count >= 0
22606 && tail_call_site_count == tail_call_site_note_count
22607 && (!dwarf_strict || dwarf_version >= 5))
22609 if (call_site_count >= 0
22610 && call_site_count == call_site_note_count)
22611 add_AT_flag (subr_die, dwarf_AT (DW_AT_call_all_calls), 1);
22612 else
22613 add_AT_flag (subr_die, dwarf_AT (DW_AT_call_all_tail_calls), 1);
22615 call_site_count = -1;
22616 tail_call_site_count = -1;
22619 /* Mark used types after we have created DIEs for the functions scopes. */
22620 premark_used_types (DECL_STRUCT_FUNCTION (decl));
22623 /* Returns a hash value for X (which really is a die_struct). */
22625 hashval_t
22626 block_die_hasher::hash (die_struct *d)
22628 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
22631 /* Return nonzero if decl_id and die_parent of die_struct X is the same
22632 as decl_id and die_parent of die_struct Y. */
22634 bool
22635 block_die_hasher::equal (die_struct *x, die_struct *y)
22637 return x->decl_id == y->decl_id && x->die_parent == y->die_parent;
22640 /* Return TRUE if DECL, which may have been previously generated as
22641 OLD_DIE, is a candidate for a DW_AT_specification. DECLARATION is
22642 true if decl (or its origin) is either an extern declaration or a
22643 class/namespace scoped declaration.
22645 The declare_in_namespace support causes us to get two DIEs for one
22646 variable, both of which are declarations. We want to avoid
22647 considering one to be a specification, so we must test for
22648 DECLARATION and DW_AT_declaration. */
22649 static inline bool
22650 decl_will_get_specification_p (dw_die_ref old_die, tree decl, bool declaration)
22652 return (old_die && TREE_STATIC (decl) && !declaration
22653 && get_AT_flag (old_die, DW_AT_declaration) == 1);
22656 /* Return true if DECL is a local static. */
22658 static inline bool
22659 local_function_static (tree decl)
22661 gcc_assert (VAR_P (decl));
22662 return TREE_STATIC (decl)
22663 && DECL_CONTEXT (decl)
22664 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL;
22667 /* Generate a DIE to represent a declared data object.
22668 Either DECL or ORIGIN must be non-null. */
22670 static void
22671 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
22673 HOST_WIDE_INT off = 0;
22674 tree com_decl;
22675 tree decl_or_origin = decl ? decl : origin;
22676 tree ultimate_origin;
22677 dw_die_ref var_die;
22678 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
22679 bool declaration = (DECL_EXTERNAL (decl_or_origin)
22680 || class_or_namespace_scope_p (context_die));
22681 bool specialization_p = false;
22682 bool no_linkage_name = false;
22684 /* While C++ inline static data members have definitions inside of the
22685 class, force the first DIE to be a declaration, then let gen_member_die
22686 reparent it to the class context and call gen_variable_die again
22687 to create the outside of the class DIE for the definition. */
22688 if (!declaration
22689 && old_die == NULL
22690 && decl
22691 && DECL_CONTEXT (decl)
22692 && TYPE_P (DECL_CONTEXT (decl))
22693 && lang_hooks.decls.decl_dwarf_attribute (decl, DW_AT_inline) != -1)
22695 declaration = true;
22696 if (dwarf_version < 5)
22697 no_linkage_name = true;
22700 ultimate_origin = decl_ultimate_origin (decl_or_origin);
22701 if (decl || ultimate_origin)
22702 origin = ultimate_origin;
22703 com_decl = fortran_common (decl_or_origin, &off);
22705 /* Symbol in common gets emitted as a child of the common block, in the form
22706 of a data member. */
22707 if (com_decl)
22709 dw_die_ref com_die;
22710 dw_loc_list_ref loc = NULL;
22711 die_node com_die_arg;
22713 var_die = lookup_decl_die (decl_or_origin);
22714 if (var_die)
22716 if (! early_dwarf && get_AT (var_die, DW_AT_location) == NULL)
22718 loc = loc_list_from_tree (com_decl, off ? 1 : 2, NULL);
22719 if (loc)
22721 if (off)
22723 /* Optimize the common case. */
22724 if (single_element_loc_list_p (loc)
22725 && loc->expr->dw_loc_opc == DW_OP_addr
22726 && loc->expr->dw_loc_next == NULL
22727 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
22728 == SYMBOL_REF)
22730 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
22731 loc->expr->dw_loc_oprnd1.v.val_addr
22732 = plus_constant (GET_MODE (x), x , off);
22734 else
22735 loc_list_plus_const (loc, off);
22737 add_AT_location_description (var_die, DW_AT_location, loc);
22738 remove_AT (var_die, DW_AT_declaration);
22741 return;
22744 if (common_block_die_table == NULL)
22745 common_block_die_table = hash_table<block_die_hasher>::create_ggc (10);
22747 com_die_arg.decl_id = DECL_UID (com_decl);
22748 com_die_arg.die_parent = context_die;
22749 com_die = common_block_die_table->find (&com_die_arg);
22750 if (! early_dwarf)
22751 loc = loc_list_from_tree (com_decl, 2, NULL);
22752 if (com_die == NULL)
22754 const char *cnam
22755 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
22756 die_node **slot;
22758 com_die = new_die (DW_TAG_common_block, context_die, decl);
22759 add_name_and_src_coords_attributes (com_die, com_decl);
22760 if (loc)
22762 add_AT_location_description (com_die, DW_AT_location, loc);
22763 /* Avoid sharing the same loc descriptor between
22764 DW_TAG_common_block and DW_TAG_variable. */
22765 loc = loc_list_from_tree (com_decl, 2, NULL);
22767 else if (DECL_EXTERNAL (decl_or_origin))
22768 add_AT_flag (com_die, DW_AT_declaration, 1);
22769 if (want_pubnames ())
22770 add_pubname_string (cnam, com_die); /* ??? needed? */
22771 com_die->decl_id = DECL_UID (com_decl);
22772 slot = common_block_die_table->find_slot (com_die, INSERT);
22773 *slot = com_die;
22775 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
22777 add_AT_location_description (com_die, DW_AT_location, loc);
22778 loc = loc_list_from_tree (com_decl, 2, NULL);
22779 remove_AT (com_die, DW_AT_declaration);
22781 var_die = new_die (DW_TAG_variable, com_die, decl);
22782 add_name_and_src_coords_attributes (var_die, decl_or_origin);
22783 add_type_attribute (var_die, TREE_TYPE (decl_or_origin),
22784 decl_quals (decl_or_origin), false,
22785 context_die);
22786 add_alignment_attribute (var_die, decl);
22787 add_AT_flag (var_die, DW_AT_external, 1);
22788 if (loc)
22790 if (off)
22792 /* Optimize the common case. */
22793 if (single_element_loc_list_p (loc)
22794 && loc->expr->dw_loc_opc == DW_OP_addr
22795 && loc->expr->dw_loc_next == NULL
22796 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
22798 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
22799 loc->expr->dw_loc_oprnd1.v.val_addr
22800 = plus_constant (GET_MODE (x), x, off);
22802 else
22803 loc_list_plus_const (loc, off);
22805 add_AT_location_description (var_die, DW_AT_location, loc);
22807 else if (DECL_EXTERNAL (decl_or_origin))
22808 add_AT_flag (var_die, DW_AT_declaration, 1);
22809 if (decl)
22810 equate_decl_number_to_die (decl, var_die);
22811 return;
22814 if (old_die)
22816 if (declaration)
22818 /* A declaration that has been previously dumped, needs no
22819 further annotations, since it doesn't need location on
22820 the second pass. */
22821 return;
22823 else if (decl_will_get_specification_p (old_die, decl, declaration)
22824 && !get_AT (old_die, DW_AT_specification))
22826 /* Fall-thru so we can make a new variable die along with a
22827 DW_AT_specification. */
22829 else if (origin && old_die->die_parent != context_die)
22831 /* If we will be creating an inlined instance, we need a
22832 new DIE that will get annotated with
22833 DW_AT_abstract_origin. Clear things so we can get a
22834 new DIE. */
22835 gcc_assert (!DECL_ABSTRACT_P (decl));
22836 old_die = NULL;
22838 else
22840 /* If a DIE was dumped early, it still needs location info.
22841 Skip to where we fill the location bits. */
22842 var_die = old_die;
22843 goto gen_variable_die_location;
22847 /* For static data members, the declaration in the class is supposed
22848 to have DW_TAG_member tag in DWARF{3,4} and we emit it for compatibility
22849 also in DWARF2; the specification should still be DW_TAG_variable
22850 referencing the DW_TAG_member DIE. */
22851 if (declaration && class_scope_p (context_die) && dwarf_version < 5)
22852 var_die = new_die (DW_TAG_member, context_die, decl);
22853 else
22854 var_die = new_die (DW_TAG_variable, context_die, decl);
22856 if (origin != NULL)
22857 add_abstract_origin_attribute (var_die, origin);
22859 /* Loop unrolling can create multiple blocks that refer to the same
22860 static variable, so we must test for the DW_AT_declaration flag.
22862 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
22863 copy decls and set the DECL_ABSTRACT_P flag on them instead of
22864 sharing them.
22866 ??? Duplicated blocks have been rewritten to use .debug_ranges. */
22867 else if (decl_will_get_specification_p (old_die, decl, declaration))
22869 /* This is a definition of a C++ class level static. */
22870 add_AT_specification (var_die, old_die);
22871 specialization_p = true;
22872 if (DECL_NAME (decl))
22874 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
22875 struct dwarf_file_data * file_index = lookup_filename (s.file);
22877 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
22878 add_AT_file (var_die, DW_AT_decl_file, file_index);
22880 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
22881 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
22883 if (debug_column_info
22884 && s.column
22885 && (get_AT_unsigned (old_die, DW_AT_decl_column)
22886 != (unsigned) s.column))
22887 add_AT_unsigned (var_die, DW_AT_decl_column, s.column);
22889 if (old_die->die_tag == DW_TAG_member)
22890 add_linkage_name (var_die, decl);
22893 else
22894 add_name_and_src_coords_attributes (var_die, decl, no_linkage_name);
22896 if ((origin == NULL && !specialization_p)
22897 || (origin != NULL
22898 && !DECL_ABSTRACT_P (decl_or_origin)
22899 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
22900 decl_function_context
22901 (decl_or_origin))))
22903 tree type = TREE_TYPE (decl_or_origin);
22905 if (decl_by_reference_p (decl_or_origin))
22906 add_type_attribute (var_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
22907 context_die);
22908 else
22909 add_type_attribute (var_die, type, decl_quals (decl_or_origin), false,
22910 context_die);
22913 if (origin == NULL && !specialization_p)
22915 if (TREE_PUBLIC (decl))
22916 add_AT_flag (var_die, DW_AT_external, 1);
22918 if (DECL_ARTIFICIAL (decl))
22919 add_AT_flag (var_die, DW_AT_artificial, 1);
22921 add_alignment_attribute (var_die, decl);
22923 add_accessibility_attribute (var_die, decl);
22926 if (declaration)
22927 add_AT_flag (var_die, DW_AT_declaration, 1);
22929 if (decl && (DECL_ABSTRACT_P (decl)
22930 || !old_die || is_declaration_die (old_die)))
22931 equate_decl_number_to_die (decl, var_die);
22933 gen_variable_die_location:
22934 if (! declaration
22935 && (! DECL_ABSTRACT_P (decl_or_origin)
22936 /* Local static vars are shared between all clones/inlines,
22937 so emit DW_AT_location on the abstract DIE if DECL_RTL is
22938 already set. */
22939 || (VAR_P (decl_or_origin)
22940 && TREE_STATIC (decl_or_origin)
22941 && DECL_RTL_SET_P (decl_or_origin))))
22943 if (early_dwarf)
22944 add_pubname (decl_or_origin, var_die);
22945 else
22946 add_location_or_const_value_attribute (var_die, decl_or_origin,
22947 decl == NULL);
22949 else
22950 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
22952 if ((dwarf_version >= 4 || !dwarf_strict)
22953 && lang_hooks.decls.decl_dwarf_attribute (decl_or_origin,
22954 DW_AT_const_expr) == 1
22955 && !get_AT (var_die, DW_AT_const_expr)
22956 && !specialization_p)
22957 add_AT_flag (var_die, DW_AT_const_expr, 1);
22959 if (!dwarf_strict)
22961 int inl = lang_hooks.decls.decl_dwarf_attribute (decl_or_origin,
22962 DW_AT_inline);
22963 if (inl != -1
22964 && !get_AT (var_die, DW_AT_inline)
22965 && !specialization_p)
22966 add_AT_unsigned (var_die, DW_AT_inline, inl);
22970 /* Generate a DIE to represent a named constant. */
22972 static void
22973 gen_const_die (tree decl, dw_die_ref context_die)
22975 dw_die_ref const_die;
22976 tree type = TREE_TYPE (decl);
22978 const_die = lookup_decl_die (decl);
22979 if (const_die)
22980 return;
22982 const_die = new_die (DW_TAG_constant, context_die, decl);
22983 equate_decl_number_to_die (decl, const_die);
22984 add_name_and_src_coords_attributes (const_die, decl);
22985 add_type_attribute (const_die, type, TYPE_QUAL_CONST, false, context_die);
22986 if (TREE_PUBLIC (decl))
22987 add_AT_flag (const_die, DW_AT_external, 1);
22988 if (DECL_ARTIFICIAL (decl))
22989 add_AT_flag (const_die, DW_AT_artificial, 1);
22990 tree_add_const_value_attribute_for_decl (const_die, decl);
22993 /* Generate a DIE to represent a label identifier. */
22995 static void
22996 gen_label_die (tree decl, dw_die_ref context_die)
22998 tree origin = decl_ultimate_origin (decl);
22999 dw_die_ref lbl_die = lookup_decl_die (decl);
23000 rtx insn;
23001 char label[MAX_ARTIFICIAL_LABEL_BYTES];
23003 if (!lbl_die)
23005 lbl_die = new_die (DW_TAG_label, context_die, decl);
23006 equate_decl_number_to_die (decl, lbl_die);
23008 if (origin != NULL)
23009 add_abstract_origin_attribute (lbl_die, origin);
23010 else
23011 add_name_and_src_coords_attributes (lbl_die, decl);
23014 if (DECL_ABSTRACT_P (decl))
23015 equate_decl_number_to_die (decl, lbl_die);
23016 else if (! early_dwarf)
23018 insn = DECL_RTL_IF_SET (decl);
23020 /* Deleted labels are programmer specified labels which have been
23021 eliminated because of various optimizations. We still emit them
23022 here so that it is possible to put breakpoints on them. */
23023 if (insn
23024 && (LABEL_P (insn)
23025 || ((NOTE_P (insn)
23026 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
23028 /* When optimization is enabled (via -O) some parts of the compiler
23029 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
23030 represent source-level labels which were explicitly declared by
23031 the user. This really shouldn't be happening though, so catch
23032 it if it ever does happen. */
23033 gcc_assert (!as_a<rtx_insn *> (insn)->deleted ());
23035 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
23036 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
23038 else if (insn
23039 && NOTE_P (insn)
23040 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
23041 && CODE_LABEL_NUMBER (insn) != -1)
23043 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
23044 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
23049 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
23050 attributes to the DIE for a block STMT, to describe where the inlined
23051 function was called from. This is similar to add_src_coords_attributes. */
23053 static inline void
23054 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
23056 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
23058 if (dwarf_version >= 3 || !dwarf_strict)
23060 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
23061 add_AT_unsigned (die, DW_AT_call_line, s.line);
23062 if (debug_column_info && s.column)
23063 add_AT_unsigned (die, DW_AT_call_column, s.column);
23068 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
23069 Add low_pc and high_pc attributes to the DIE for a block STMT. */
23071 static inline void
23072 add_high_low_attributes (tree stmt, dw_die_ref die)
23074 char label[MAX_ARTIFICIAL_LABEL_BYTES];
23076 if (BLOCK_FRAGMENT_CHAIN (stmt)
23077 && (dwarf_version >= 3 || !dwarf_strict))
23079 tree chain, superblock = NULL_TREE;
23080 dw_die_ref pdie;
23081 dw_attr_node *attr = NULL;
23083 if (inlined_function_outer_scope_p (stmt))
23085 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
23086 BLOCK_NUMBER (stmt));
23087 add_AT_lbl_id (die, DW_AT_entry_pc, label);
23090 /* Optimize duplicate .debug_ranges lists or even tails of
23091 lists. If this BLOCK has same ranges as its supercontext,
23092 lookup DW_AT_ranges attribute in the supercontext (and
23093 recursively so), verify that the ranges_table contains the
23094 right values and use it instead of adding a new .debug_range. */
23095 for (chain = stmt, pdie = die;
23096 BLOCK_SAME_RANGE (chain);
23097 chain = BLOCK_SUPERCONTEXT (chain))
23099 dw_attr_node *new_attr;
23101 pdie = pdie->die_parent;
23102 if (pdie == NULL)
23103 break;
23104 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
23105 break;
23106 new_attr = get_AT (pdie, DW_AT_ranges);
23107 if (new_attr == NULL
23108 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
23109 break;
23110 attr = new_attr;
23111 superblock = BLOCK_SUPERCONTEXT (chain);
23113 if (attr != NULL
23114 && ((*ranges_table)[attr->dw_attr_val.v.val_offset].num
23115 == BLOCK_NUMBER (superblock))
23116 && BLOCK_FRAGMENT_CHAIN (superblock))
23118 unsigned long off = attr->dw_attr_val.v.val_offset;
23119 unsigned long supercnt = 0, thiscnt = 0;
23120 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
23121 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
23123 ++supercnt;
23124 gcc_checking_assert ((*ranges_table)[off + supercnt].num
23125 == BLOCK_NUMBER (chain));
23127 gcc_checking_assert ((*ranges_table)[off + supercnt + 1].num == 0);
23128 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
23129 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
23130 ++thiscnt;
23131 gcc_assert (supercnt >= thiscnt);
23132 add_AT_range_list (die, DW_AT_ranges, off + supercnt - thiscnt,
23133 false);
23134 note_rnglist_head (off + supercnt - thiscnt);
23135 return;
23138 unsigned int offset = add_ranges (stmt, true);
23139 add_AT_range_list (die, DW_AT_ranges, offset, false);
23140 note_rnglist_head (offset);
23142 bool prev_in_cold = BLOCK_IN_COLD_SECTION_P (stmt);
23143 chain = BLOCK_FRAGMENT_CHAIN (stmt);
23146 add_ranges (chain, prev_in_cold != BLOCK_IN_COLD_SECTION_P (chain));
23147 prev_in_cold = BLOCK_IN_COLD_SECTION_P (chain);
23148 chain = BLOCK_FRAGMENT_CHAIN (chain);
23150 while (chain);
23151 add_ranges (NULL);
23153 else
23155 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
23156 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
23157 BLOCK_NUMBER (stmt));
23158 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
23159 BLOCK_NUMBER (stmt));
23160 add_AT_low_high_pc (die, label, label_high, false);
23164 /* Generate a DIE for a lexical block. */
23166 static void
23167 gen_lexical_block_die (tree stmt, dw_die_ref context_die)
23169 dw_die_ref old_die = BLOCK_DIE (stmt);
23170 dw_die_ref stmt_die = NULL;
23171 if (!old_die)
23173 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
23174 BLOCK_DIE (stmt) = stmt_die;
23177 if (BLOCK_ABSTRACT (stmt))
23179 if (old_die)
23181 /* This must have been generated early and it won't even
23182 need location information since it's a DW_AT_inline
23183 function. */
23184 if (flag_checking)
23185 for (dw_die_ref c = context_die; c; c = c->die_parent)
23186 if (c->die_tag == DW_TAG_inlined_subroutine
23187 || c->die_tag == DW_TAG_subprogram)
23189 gcc_assert (get_AT (c, DW_AT_inline));
23190 break;
23192 return;
23195 else if (BLOCK_ABSTRACT_ORIGIN (stmt))
23197 /* If this is an inlined instance, create a new lexical die for
23198 anything below to attach DW_AT_abstract_origin to. */
23199 if (old_die)
23201 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
23202 BLOCK_DIE (stmt) = stmt_die;
23203 old_die = NULL;
23206 tree origin = block_ultimate_origin (stmt);
23207 if (origin != NULL_TREE && origin != stmt)
23208 add_abstract_origin_attribute (stmt_die, origin);
23211 if (old_die)
23212 stmt_die = old_die;
23214 /* A non abstract block whose blocks have already been reordered
23215 should have the instruction range for this block. If so, set the
23216 high/low attributes. */
23217 if (!early_dwarf && !BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
23219 gcc_assert (stmt_die);
23220 add_high_low_attributes (stmt, stmt_die);
23223 decls_for_scope (stmt, stmt_die);
23226 /* Generate a DIE for an inlined subprogram. */
23228 static void
23229 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die)
23231 tree decl;
23233 /* The instance of function that is effectively being inlined shall not
23234 be abstract. */
23235 gcc_assert (! BLOCK_ABSTRACT (stmt));
23237 decl = block_ultimate_origin (stmt);
23239 /* Make sure any inlined functions are known to be inlineable. */
23240 gcc_checking_assert (DECL_ABSTRACT_P (decl)
23241 || cgraph_function_possibly_inlined_p (decl));
23243 /* Emit info for the abstract instance first, if we haven't yet. We
23244 must emit this even if the block is abstract, otherwise when we
23245 emit the block below (or elsewhere), we may end up trying to emit
23246 a die whose origin die hasn't been emitted, and crashing. */
23247 dwarf2out_abstract_function (decl);
23249 if (! BLOCK_ABSTRACT (stmt))
23251 dw_die_ref subr_die
23252 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
23254 if (call_arg_locations)
23255 BLOCK_DIE (stmt) = subr_die;
23256 add_abstract_origin_attribute (subr_die, decl);
23257 if (TREE_ASM_WRITTEN (stmt))
23258 add_high_low_attributes (stmt, subr_die);
23259 add_call_src_coords_attributes (stmt, subr_die);
23261 decls_for_scope (stmt, subr_die);
23265 /* Generate a DIE for a field in a record, or structure. CTX is required: see
23266 the comment for VLR_CONTEXT. */
23268 static void
23269 gen_field_die (tree decl, struct vlr_context *ctx, dw_die_ref context_die)
23271 dw_die_ref decl_die;
23273 if (TREE_TYPE (decl) == error_mark_node)
23274 return;
23276 decl_die = new_die (DW_TAG_member, context_die, decl);
23277 add_name_and_src_coords_attributes (decl_die, decl);
23278 add_type_attribute (decl_die, member_declared_type (decl), decl_quals (decl),
23279 TYPE_REVERSE_STORAGE_ORDER (DECL_FIELD_CONTEXT (decl)),
23280 context_die);
23282 if (DECL_BIT_FIELD_TYPE (decl))
23284 add_byte_size_attribute (decl_die, decl);
23285 add_bit_size_attribute (decl_die, decl);
23286 add_bit_offset_attribute (decl_die, decl, ctx);
23289 add_alignment_attribute (decl_die, decl);
23291 /* If we have a variant part offset, then we are supposed to process a member
23292 of a QUAL_UNION_TYPE, which is how we represent variant parts in
23293 trees. */
23294 gcc_assert (ctx->variant_part_offset == NULL_TREE
23295 || TREE_CODE (DECL_FIELD_CONTEXT (decl)) != QUAL_UNION_TYPE);
23296 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
23297 add_data_member_location_attribute (decl_die, decl, ctx);
23299 if (DECL_ARTIFICIAL (decl))
23300 add_AT_flag (decl_die, DW_AT_artificial, 1);
23302 add_accessibility_attribute (decl_die, decl);
23304 /* Equate decl number to die, so that we can look up this decl later on. */
23305 equate_decl_number_to_die (decl, decl_die);
23308 #if 0
23309 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
23310 Use modified_type_die instead.
23311 We keep this code here just in case these types of DIEs may be needed to
23312 represent certain things in other languages (e.g. Pascal) someday. */
23314 static void
23315 gen_pointer_type_die (tree type, dw_die_ref context_die)
23317 dw_die_ref ptr_die
23318 = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
23320 equate_type_number_to_die (type, ptr_die);
23321 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
23322 context_die);
23323 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
23326 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
23327 Use modified_type_die instead.
23328 We keep this code here just in case these types of DIEs may be needed to
23329 represent certain things in other languages (e.g. Pascal) someday. */
23331 static void
23332 gen_reference_type_die (tree type, dw_die_ref context_die)
23334 dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
23336 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
23337 ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
23338 else
23339 ref_die = new_die (DW_TAG_reference_type, scope_die, type);
23341 equate_type_number_to_die (type, ref_die);
23342 add_type_attribute (ref_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
23343 context_die);
23344 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
23346 #endif
23348 /* Generate a DIE for a pointer to a member type. TYPE can be an
23349 OFFSET_TYPE, for a pointer to data member, or a RECORD_TYPE, for a
23350 pointer to member function. */
23352 static void
23353 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
23355 if (lookup_type_die (type))
23356 return;
23358 dw_die_ref ptr_die = new_die (DW_TAG_ptr_to_member_type,
23359 scope_die_for (type, context_die), type);
23361 equate_type_number_to_die (type, ptr_die);
23362 add_AT_die_ref (ptr_die, DW_AT_containing_type,
23363 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
23364 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
23365 context_die);
23366 add_alignment_attribute (ptr_die, type);
23368 if (TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE
23369 && TREE_CODE (TREE_TYPE (type)) != METHOD_TYPE)
23371 dw_loc_descr_ref op = new_loc_descr (DW_OP_plus, 0, 0);
23372 add_AT_loc (ptr_die, DW_AT_use_location, op);
23376 static char *producer_string;
23378 /* Return a heap allocated producer string including command line options
23379 if -grecord-gcc-switches. */
23381 static char *
23382 gen_producer_string (void)
23384 size_t j;
23385 auto_vec<const char *> switches;
23386 const char *language_string = lang_hooks.name;
23387 char *producer, *tail;
23388 const char *p;
23389 size_t len = dwarf_record_gcc_switches ? 0 : 3;
23390 size_t plen = strlen (language_string) + 1 + strlen (version_string);
23392 for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
23393 switch (save_decoded_options[j].opt_index)
23395 case OPT_o:
23396 case OPT_d:
23397 case OPT_dumpbase:
23398 case OPT_dumpdir:
23399 case OPT_auxbase:
23400 case OPT_auxbase_strip:
23401 case OPT_quiet:
23402 case OPT_version:
23403 case OPT_v:
23404 case OPT_w:
23405 case OPT_L:
23406 case OPT_D:
23407 case OPT_I:
23408 case OPT_U:
23409 case OPT_SPECIAL_unknown:
23410 case OPT_SPECIAL_ignore:
23411 case OPT_SPECIAL_program_name:
23412 case OPT_SPECIAL_input_file:
23413 case OPT_grecord_gcc_switches:
23414 case OPT_gno_record_gcc_switches:
23415 case OPT__output_pch_:
23416 case OPT_fdiagnostics_show_location_:
23417 case OPT_fdiagnostics_show_option:
23418 case OPT_fdiagnostics_show_caret:
23419 case OPT_fdiagnostics_color_:
23420 case OPT_fverbose_asm:
23421 case OPT____:
23422 case OPT__sysroot_:
23423 case OPT_nostdinc:
23424 case OPT_nostdinc__:
23425 case OPT_fpreprocessed:
23426 case OPT_fltrans_output_list_:
23427 case OPT_fresolution_:
23428 case OPT_fdebug_prefix_map_:
23429 /* Ignore these. */
23430 continue;
23431 default:
23432 if (cl_options[save_decoded_options[j].opt_index].flags
23433 & CL_NO_DWARF_RECORD)
23434 continue;
23435 gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
23436 == '-');
23437 switch (save_decoded_options[j].canonical_option[0][1])
23439 case 'M':
23440 case 'i':
23441 case 'W':
23442 continue;
23443 case 'f':
23444 if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
23445 "dump", 4) == 0)
23446 continue;
23447 break;
23448 default:
23449 break;
23451 switches.safe_push (save_decoded_options[j].orig_option_with_args_text);
23452 len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
23453 break;
23456 producer = XNEWVEC (char, plen + 1 + len + 1);
23457 tail = producer;
23458 sprintf (tail, "%s %s", language_string, version_string);
23459 tail += plen;
23461 FOR_EACH_VEC_ELT (switches, j, p)
23463 len = strlen (p);
23464 *tail = ' ';
23465 memcpy (tail + 1, p, len);
23466 tail += len + 1;
23469 *tail = '\0';
23470 return producer;
23473 /* Given a C and/or C++ language/version string return the "highest".
23474 C++ is assumed to be "higher" than C in this case. Used for merging
23475 LTO translation unit languages. */
23476 static const char *
23477 highest_c_language (const char *lang1, const char *lang2)
23479 if (strcmp ("GNU C++14", lang1) == 0 || strcmp ("GNU C++14", lang2) == 0)
23480 return "GNU C++14";
23481 if (strcmp ("GNU C++11", lang1) == 0 || strcmp ("GNU C++11", lang2) == 0)
23482 return "GNU C++11";
23483 if (strcmp ("GNU C++98", lang1) == 0 || strcmp ("GNU C++98", lang2) == 0)
23484 return "GNU C++98";
23486 if (strcmp ("GNU C11", lang1) == 0 || strcmp ("GNU C11", lang2) == 0)
23487 return "GNU C11";
23488 if (strcmp ("GNU C99", lang1) == 0 || strcmp ("GNU C99", lang2) == 0)
23489 return "GNU C99";
23490 if (strcmp ("GNU C89", lang1) == 0 || strcmp ("GNU C89", lang2) == 0)
23491 return "GNU C89";
23493 gcc_unreachable ();
23497 /* Generate the DIE for the compilation unit. */
23499 static dw_die_ref
23500 gen_compile_unit_die (const char *filename)
23502 dw_die_ref die;
23503 const char *language_string = lang_hooks.name;
23504 int language;
23506 die = new_die (DW_TAG_compile_unit, NULL, NULL);
23508 if (filename)
23510 add_name_attribute (die, filename);
23511 /* Don't add cwd for <built-in>. */
23512 if (filename[0] != '<')
23513 add_comp_dir_attribute (die);
23516 add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
23518 /* If our producer is LTO try to figure out a common language to use
23519 from the global list of translation units. */
23520 if (strcmp (language_string, "GNU GIMPLE") == 0)
23522 unsigned i;
23523 tree t;
23524 const char *common_lang = NULL;
23526 FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
23528 if (!TRANSLATION_UNIT_LANGUAGE (t))
23529 continue;
23530 if (!common_lang)
23531 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
23532 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
23534 else if (strncmp (common_lang, "GNU C", 5) == 0
23535 && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
23536 /* Mixing C and C++ is ok, use C++ in that case. */
23537 common_lang = highest_c_language (common_lang,
23538 TRANSLATION_UNIT_LANGUAGE (t));
23539 else
23541 /* Fall back to C. */
23542 common_lang = NULL;
23543 break;
23547 if (common_lang)
23548 language_string = common_lang;
23551 language = DW_LANG_C;
23552 if (strncmp (language_string, "GNU C", 5) == 0
23553 && ISDIGIT (language_string[5]))
23555 language = DW_LANG_C89;
23556 if (dwarf_version >= 3 || !dwarf_strict)
23558 if (strcmp (language_string, "GNU C89") != 0)
23559 language = DW_LANG_C99;
23561 if (dwarf_version >= 5 /* || !dwarf_strict */)
23562 if (strcmp (language_string, "GNU C11") == 0)
23563 language = DW_LANG_C11;
23566 else if (strncmp (language_string, "GNU C++", 7) == 0)
23568 language = DW_LANG_C_plus_plus;
23569 if (dwarf_version >= 5 /* || !dwarf_strict */)
23571 if (strcmp (language_string, "GNU C++11") == 0)
23572 language = DW_LANG_C_plus_plus_11;
23573 else if (strcmp (language_string, "GNU C++14") == 0)
23574 language = DW_LANG_C_plus_plus_14;
23577 else if (strcmp (language_string, "GNU F77") == 0)
23578 language = DW_LANG_Fortran77;
23579 else if (strcmp (language_string, "GNU Pascal") == 0)
23580 language = DW_LANG_Pascal83;
23581 else if (dwarf_version >= 3 || !dwarf_strict)
23583 if (strcmp (language_string, "GNU Ada") == 0)
23584 language = DW_LANG_Ada95;
23585 else if (strncmp (language_string, "GNU Fortran", 11) == 0)
23587 language = DW_LANG_Fortran95;
23588 if (dwarf_version >= 5 /* || !dwarf_strict */)
23590 if (strcmp (language_string, "GNU Fortran2003") == 0)
23591 language = DW_LANG_Fortran03;
23592 else if (strcmp (language_string, "GNU Fortran2008") == 0)
23593 language = DW_LANG_Fortran08;
23596 else if (strcmp (language_string, "GNU Java") == 0)
23597 language = DW_LANG_Java;
23598 else if (strcmp (language_string, "GNU Objective-C") == 0)
23599 language = DW_LANG_ObjC;
23600 else if (strcmp (language_string, "GNU Objective-C++") == 0)
23601 language = DW_LANG_ObjC_plus_plus;
23602 else if (dwarf_version >= 5 || !dwarf_strict)
23604 if (strcmp (language_string, "GNU Go") == 0)
23605 language = DW_LANG_Go;
23608 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
23609 else if (strncmp (language_string, "GNU Fortran", 11) == 0)
23610 language = DW_LANG_Fortran90;
23612 add_AT_unsigned (die, DW_AT_language, language);
23614 switch (language)
23616 case DW_LANG_Fortran77:
23617 case DW_LANG_Fortran90:
23618 case DW_LANG_Fortran95:
23619 case DW_LANG_Fortran03:
23620 case DW_LANG_Fortran08:
23621 /* Fortran has case insensitive identifiers and the front-end
23622 lowercases everything. */
23623 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
23624 break;
23625 default:
23626 /* The default DW_ID_case_sensitive doesn't need to be specified. */
23627 break;
23629 return die;
23632 /* Generate the DIE for a base class. */
23634 static void
23635 gen_inheritance_die (tree binfo, tree access, tree type,
23636 dw_die_ref context_die)
23638 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
23639 struct vlr_context ctx = { type, NULL };
23641 add_type_attribute (die, BINFO_TYPE (binfo), TYPE_UNQUALIFIED, false,
23642 context_die);
23643 add_data_member_location_attribute (die, binfo, &ctx);
23645 if (BINFO_VIRTUAL_P (binfo))
23646 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
23648 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
23649 children, otherwise the default is DW_ACCESS_public. In DWARF2
23650 the default has always been DW_ACCESS_private. */
23651 if (access == access_public_node)
23653 if (dwarf_version == 2
23654 || context_die->die_tag == DW_TAG_class_type)
23655 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
23657 else if (access == access_protected_node)
23658 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
23659 else if (dwarf_version > 2
23660 && context_die->die_tag != DW_TAG_class_type)
23661 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
23664 /* Return whether DECL is a FIELD_DECL that represents the variant part of a
23665 structure. */
23666 static bool
23667 is_variant_part (tree decl)
23669 return (TREE_CODE (decl) == FIELD_DECL
23670 && TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE);
23673 /* Check that OPERAND is a reference to a field in STRUCT_TYPE. If it is,
23674 return the FIELD_DECL. Return NULL_TREE otherwise. */
23676 static tree
23677 analyze_discr_in_predicate (tree operand, tree struct_type)
23679 bool continue_stripping = true;
23680 while (continue_stripping)
23681 switch (TREE_CODE (operand))
23683 CASE_CONVERT:
23684 operand = TREE_OPERAND (operand, 0);
23685 break;
23686 default:
23687 continue_stripping = false;
23688 break;
23691 /* Match field access to members of struct_type only. */
23692 if (TREE_CODE (operand) == COMPONENT_REF
23693 && TREE_CODE (TREE_OPERAND (operand, 0)) == PLACEHOLDER_EXPR
23694 && TREE_TYPE (TREE_OPERAND (operand, 0)) == struct_type
23695 && TREE_CODE (TREE_OPERAND (operand, 1)) == FIELD_DECL)
23696 return TREE_OPERAND (operand, 1);
23697 else
23698 return NULL_TREE;
23701 /* Check that SRC is a constant integer that can be represented as a native
23702 integer constant (either signed or unsigned). If so, store it into DEST and
23703 return true. Return false otherwise. */
23705 static bool
23706 get_discr_value (tree src, dw_discr_value *dest)
23708 tree discr_type = TREE_TYPE (src);
23710 if (lang_hooks.types.get_debug_type)
23712 tree debug_type = lang_hooks.types.get_debug_type (discr_type);
23713 if (debug_type != NULL)
23714 discr_type = debug_type;
23717 if (TREE_CODE (src) != INTEGER_CST || !INTEGRAL_TYPE_P (discr_type))
23718 return false;
23720 /* Signedness can vary between the original type and the debug type. This
23721 can happen for character types in Ada for instance: the character type
23722 used for code generation can be signed, to be compatible with the C one,
23723 but from a debugger point of view, it must be unsigned. */
23724 bool is_orig_unsigned = TYPE_UNSIGNED (TREE_TYPE (src));
23725 bool is_debug_unsigned = TYPE_UNSIGNED (discr_type);
23727 if (is_orig_unsigned != is_debug_unsigned)
23728 src = fold_convert (discr_type, src);
23730 if (!(is_debug_unsigned ? tree_fits_uhwi_p (src) : tree_fits_shwi_p (src)))
23731 return false;
23733 dest->pos = is_debug_unsigned;
23734 if (is_debug_unsigned)
23735 dest->v.uval = tree_to_uhwi (src);
23736 else
23737 dest->v.sval = tree_to_shwi (src);
23739 return true;
23742 /* Try to extract synthetic properties out of VARIANT_PART_DECL, which is a
23743 FIELD_DECL in STRUCT_TYPE that represents a variant part. If unsuccessful,
23744 store NULL_TREE in DISCR_DECL. Otherwise:
23746 - store the discriminant field in STRUCT_TYPE that controls the variant
23747 part to *DISCR_DECL
23749 - put in *DISCR_LISTS_P an array where for each variant, the item
23750 represents the corresponding matching list of discriminant values.
23752 - put in *DISCR_LISTS_LENGTH the number of variants, which is the size of
23753 the above array.
23755 Note that when the array is allocated (i.e. when the analysis is
23756 successful), it is up to the caller to free the array. */
23758 static void
23759 analyze_variants_discr (tree variant_part_decl,
23760 tree struct_type,
23761 tree *discr_decl,
23762 dw_discr_list_ref **discr_lists_p,
23763 unsigned *discr_lists_length)
23765 tree variant_part_type = TREE_TYPE (variant_part_decl);
23766 tree variant;
23767 dw_discr_list_ref *discr_lists;
23768 unsigned i;
23770 /* Compute how many variants there are in this variant part. */
23771 *discr_lists_length = 0;
23772 for (variant = TYPE_FIELDS (variant_part_type);
23773 variant != NULL_TREE;
23774 variant = DECL_CHAIN (variant))
23775 ++*discr_lists_length;
23777 *discr_decl = NULL_TREE;
23778 *discr_lists_p
23779 = (dw_discr_list_ref *) xcalloc (*discr_lists_length,
23780 sizeof (**discr_lists_p));
23781 discr_lists = *discr_lists_p;
23783 /* And then analyze all variants to extract discriminant information for all
23784 of them. This analysis is conservative: as soon as we detect something we
23785 do not support, abort everything and pretend we found nothing. */
23786 for (variant = TYPE_FIELDS (variant_part_type), i = 0;
23787 variant != NULL_TREE;
23788 variant = DECL_CHAIN (variant), ++i)
23790 tree match_expr = DECL_QUALIFIER (variant);
23792 /* Now, try to analyze the predicate and deduce a discriminant for
23793 it. */
23794 if (match_expr == boolean_true_node)
23795 /* Typically happens for the default variant: it matches all cases that
23796 previous variants rejected. Don't output any matching value for
23797 this one. */
23798 continue;
23800 /* The following loop tries to iterate over each discriminant
23801 possibility: single values or ranges. */
23802 while (match_expr != NULL_TREE)
23804 tree next_round_match_expr;
23805 tree candidate_discr = NULL_TREE;
23806 dw_discr_list_ref new_node = NULL;
23808 /* Possibilities are matched one after the other by nested
23809 TRUTH_ORIF_EXPR expressions. Process the current possibility and
23810 continue with the rest at next iteration. */
23811 if (TREE_CODE (match_expr) == TRUTH_ORIF_EXPR)
23813 next_round_match_expr = TREE_OPERAND (match_expr, 0);
23814 match_expr = TREE_OPERAND (match_expr, 1);
23816 else
23817 next_round_match_expr = NULL_TREE;
23819 if (match_expr == boolean_false_node)
23820 /* This sub-expression matches nothing: just wait for the next
23821 one. */
23824 else if (TREE_CODE (match_expr) == EQ_EXPR)
23826 /* We are matching: <discr_field> == <integer_cst>
23827 This sub-expression matches a single value. */
23828 tree integer_cst = TREE_OPERAND (match_expr, 1);
23830 candidate_discr
23831 = analyze_discr_in_predicate (TREE_OPERAND (match_expr, 0),
23832 struct_type);
23834 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
23835 if (!get_discr_value (integer_cst,
23836 &new_node->dw_discr_lower_bound))
23837 goto abort;
23838 new_node->dw_discr_range = false;
23841 else if (TREE_CODE (match_expr) == TRUTH_ANDIF_EXPR)
23843 /* We are matching:
23844 <discr_field> > <integer_cst>
23845 && <discr_field> < <integer_cst>.
23846 This sub-expression matches the range of values between the
23847 two matched integer constants. Note that comparisons can be
23848 inclusive or exclusive. */
23849 tree candidate_discr_1, candidate_discr_2;
23850 tree lower_cst, upper_cst;
23851 bool lower_cst_included, upper_cst_included;
23852 tree lower_op = TREE_OPERAND (match_expr, 0);
23853 tree upper_op = TREE_OPERAND (match_expr, 1);
23855 /* When the comparison is exclusive, the integer constant is not
23856 the discriminant range bound we are looking for: we will have
23857 to increment or decrement it. */
23858 if (TREE_CODE (lower_op) == GE_EXPR)
23859 lower_cst_included = true;
23860 else if (TREE_CODE (lower_op) == GT_EXPR)
23861 lower_cst_included = false;
23862 else
23863 goto abort;
23865 if (TREE_CODE (upper_op) == LE_EXPR)
23866 upper_cst_included = true;
23867 else if (TREE_CODE (upper_op) == LT_EXPR)
23868 upper_cst_included = false;
23869 else
23870 goto abort;
23872 /* Extract the discriminant from the first operand and check it
23873 is consistant with the same analysis in the second
23874 operand. */
23875 candidate_discr_1
23876 = analyze_discr_in_predicate (TREE_OPERAND (lower_op, 0),
23877 struct_type);
23878 candidate_discr_2
23879 = analyze_discr_in_predicate (TREE_OPERAND (upper_op, 0),
23880 struct_type);
23881 if (candidate_discr_1 == candidate_discr_2)
23882 candidate_discr = candidate_discr_1;
23883 else
23884 goto abort;
23886 /* Extract bounds from both. */
23887 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
23888 lower_cst = TREE_OPERAND (lower_op, 1);
23889 upper_cst = TREE_OPERAND (upper_op, 1);
23891 if (!lower_cst_included)
23892 lower_cst
23893 = fold_build2 (PLUS_EXPR, TREE_TYPE (lower_cst), lower_cst,
23894 build_int_cst (TREE_TYPE (lower_cst), 1));
23895 if (!upper_cst_included)
23896 upper_cst
23897 = fold_build2 (MINUS_EXPR, TREE_TYPE (upper_cst), upper_cst,
23898 build_int_cst (TREE_TYPE (upper_cst), 1));
23900 if (!get_discr_value (lower_cst,
23901 &new_node->dw_discr_lower_bound)
23902 || !get_discr_value (upper_cst,
23903 &new_node->dw_discr_upper_bound))
23904 goto abort;
23906 new_node->dw_discr_range = true;
23909 else
23910 /* Unsupported sub-expression: we cannot determine the set of
23911 matching discriminant values. Abort everything. */
23912 goto abort;
23914 /* If the discriminant info is not consistant with what we saw so
23915 far, consider the analysis failed and abort everything. */
23916 if (candidate_discr == NULL_TREE
23917 || (*discr_decl != NULL_TREE && candidate_discr != *discr_decl))
23918 goto abort;
23919 else
23920 *discr_decl = candidate_discr;
23922 if (new_node != NULL)
23924 new_node->dw_discr_next = discr_lists[i];
23925 discr_lists[i] = new_node;
23927 match_expr = next_round_match_expr;
23931 /* If we reach this point, we could match everything we were interested
23932 in. */
23933 return;
23935 abort:
23936 /* Clean all data structure and return no result. */
23937 free (*discr_lists_p);
23938 *discr_lists_p = NULL;
23939 *discr_decl = NULL_TREE;
23942 /* Generate a DIE to represent VARIANT_PART_DECL, a variant part that is part
23943 of STRUCT_TYPE, a record type. This new DIE is emitted as the next child
23944 under CONTEXT_DIE.
23946 Variant parts are supposed to be implemented as a FIELD_DECL whose type is a
23947 QUAL_UNION_TYPE: this is the VARIANT_PART_DECL parameter. The members for
23948 this type, which are record types, represent the available variants and each
23949 has a DECL_QUALIFIER attribute. The discriminant and the discriminant
23950 values are inferred from these attributes.
23952 In trees, the offsets for the fields inside these sub-records are relative
23953 to the variant part itself, whereas the corresponding DIEs should have
23954 offset attributes that are relative to the embedding record base address.
23955 This is why the caller must provide a VARIANT_PART_OFFSET expression: it
23956 must be an expression that computes the offset of the variant part to
23957 describe in DWARF. */
23959 static void
23960 gen_variant_part (tree variant_part_decl, struct vlr_context *vlr_ctx,
23961 dw_die_ref context_die)
23963 const tree variant_part_type = TREE_TYPE (variant_part_decl);
23964 tree variant_part_offset = vlr_ctx->variant_part_offset;
23965 struct loc_descr_context ctx = {
23966 vlr_ctx->struct_type, /* context_type */
23967 NULL_TREE, /* base_decl */
23968 NULL, /* dpi */
23969 false, /* placeholder_arg */
23970 false /* placeholder_seen */
23973 /* The FIELD_DECL node in STRUCT_TYPE that acts as the discriminant, or
23974 NULL_TREE if there is no such field. */
23975 tree discr_decl = NULL_TREE;
23976 dw_discr_list_ref *discr_lists;
23977 unsigned discr_lists_length = 0;
23978 unsigned i;
23980 dw_die_ref dwarf_proc_die = NULL;
23981 dw_die_ref variant_part_die
23982 = new_die (DW_TAG_variant_part, context_die, variant_part_type);
23984 equate_decl_number_to_die (variant_part_decl, variant_part_die);
23986 analyze_variants_discr (variant_part_decl, vlr_ctx->struct_type,
23987 &discr_decl, &discr_lists, &discr_lists_length);
23989 if (discr_decl != NULL_TREE)
23991 dw_die_ref discr_die = lookup_decl_die (discr_decl);
23993 if (discr_die)
23994 add_AT_die_ref (variant_part_die, DW_AT_discr, discr_die);
23995 else
23996 /* We have no DIE for the discriminant, so just discard all
23997 discrimimant information in the output. */
23998 discr_decl = NULL_TREE;
24001 /* If the offset for this variant part is more complex than a constant,
24002 create a DWARF procedure for it so that we will not have to generate DWARF
24003 expressions for it for each member. */
24004 if (TREE_CODE (variant_part_offset) != INTEGER_CST
24005 && (dwarf_version >= 3 || !dwarf_strict))
24007 const tree dwarf_proc_fndecl
24008 = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL, NULL_TREE,
24009 build_function_type (TREE_TYPE (variant_part_offset),
24010 NULL_TREE));
24011 const tree dwarf_proc_call = build_call_expr (dwarf_proc_fndecl, 0);
24012 const dw_loc_descr_ref dwarf_proc_body
24013 = loc_descriptor_from_tree (variant_part_offset, 0, &ctx);
24015 dwarf_proc_die = new_dwarf_proc_die (dwarf_proc_body,
24016 dwarf_proc_fndecl, context_die);
24017 if (dwarf_proc_die != NULL)
24018 variant_part_offset = dwarf_proc_call;
24021 /* Output DIEs for all variants. */
24022 i = 0;
24023 for (tree variant = TYPE_FIELDS (variant_part_type);
24024 variant != NULL_TREE;
24025 variant = DECL_CHAIN (variant), ++i)
24027 tree variant_type = TREE_TYPE (variant);
24028 dw_die_ref variant_die;
24030 /* All variants (i.e. members of a variant part) are supposed to be
24031 encoded as structures. Sub-variant parts are QUAL_UNION_TYPE fields
24032 under these records. */
24033 gcc_assert (TREE_CODE (variant_type) == RECORD_TYPE);
24035 variant_die = new_die (DW_TAG_variant, variant_part_die, variant_type);
24036 equate_decl_number_to_die (variant, variant_die);
24038 /* Output discriminant values this variant matches, if any. */
24039 if (discr_decl == NULL || discr_lists[i] == NULL)
24040 /* In the case we have discriminant information at all, this is
24041 probably the default variant: as the standard says, don't
24042 output any discriminant value/list attribute. */
24044 else if (discr_lists[i]->dw_discr_next == NULL
24045 && !discr_lists[i]->dw_discr_range)
24046 /* If there is only one accepted value, don't bother outputting a
24047 list. */
24048 add_discr_value (variant_die, &discr_lists[i]->dw_discr_lower_bound);
24049 else
24050 add_discr_list (variant_die, discr_lists[i]);
24052 for (tree member = TYPE_FIELDS (variant_type);
24053 member != NULL_TREE;
24054 member = DECL_CHAIN (member))
24056 struct vlr_context vlr_sub_ctx = {
24057 vlr_ctx->struct_type, /* struct_type */
24058 NULL /* variant_part_offset */
24060 if (is_variant_part (member))
24062 /* All offsets for fields inside variant parts are relative to
24063 the top-level embedding RECORD_TYPE's base address. On the
24064 other hand, offsets in GCC's types are relative to the
24065 nested-most variant part. So we have to sum offsets each time
24066 we recurse. */
24068 vlr_sub_ctx.variant_part_offset
24069 = fold_build2 (PLUS_EXPR, TREE_TYPE (variant_part_offset),
24070 variant_part_offset, byte_position (member));
24071 gen_variant_part (member, &vlr_sub_ctx, variant_die);
24073 else
24075 vlr_sub_ctx.variant_part_offset = variant_part_offset;
24076 gen_decl_die (member, NULL, &vlr_sub_ctx, variant_die);
24081 free (discr_lists);
24084 /* Generate a DIE for a class member. */
24086 static void
24087 gen_member_die (tree type, dw_die_ref context_die)
24089 tree member;
24090 tree binfo = TYPE_BINFO (type);
24091 dw_die_ref child;
24093 /* If this is not an incomplete type, output descriptions of each of its
24094 members. Note that as we output the DIEs necessary to represent the
24095 members of this record or union type, we will also be trying to output
24096 DIEs to represent the *types* of those members. However the `type'
24097 function (above) will specifically avoid generating type DIEs for member
24098 types *within* the list of member DIEs for this (containing) type except
24099 for those types (of members) which are explicitly marked as also being
24100 members of this (containing) type themselves. The g++ front- end can
24101 force any given type to be treated as a member of some other (containing)
24102 type by setting the TYPE_CONTEXT of the given (member) type to point to
24103 the TREE node representing the appropriate (containing) type. */
24105 /* First output info about the base classes. */
24106 if (binfo)
24108 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
24109 int i;
24110 tree base;
24112 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
24113 gen_inheritance_die (base,
24114 (accesses ? (*accesses)[i] : access_public_node),
24115 type,
24116 context_die);
24119 /* Now output info about the data members and type members. */
24120 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
24122 struct vlr_context vlr_ctx = { type, NULL_TREE };
24123 bool static_inline_p
24124 = (TREE_STATIC (member)
24125 && (lang_hooks.decls.decl_dwarf_attribute (member, DW_AT_inline)
24126 != -1));
24128 /* If we thought we were generating minimal debug info for TYPE
24129 and then changed our minds, some of the member declarations
24130 may have already been defined. Don't define them again, but
24131 do put them in the right order. */
24133 child = lookup_decl_die (member);
24134 if (child)
24136 /* Handle inline static data members, which only have in-class
24137 declarations. */
24138 dw_die_ref ref = NULL;
24139 if (child->die_tag == DW_TAG_variable
24140 && child->die_parent == comp_unit_die ())
24142 ref = get_AT_ref (child, DW_AT_specification);
24143 /* For C++17 inline static data members followed by redundant
24144 out of class redeclaration, we might get here with
24145 child being the DIE created for the out of class
24146 redeclaration and with its DW_AT_specification being
24147 the DIE created for in-class definition. We want to
24148 reparent the latter, and don't want to create another
24149 DIE with DW_AT_specification in that case, because
24150 we already have one. */
24151 if (ref
24152 && static_inline_p
24153 && ref->die_tag == DW_TAG_variable
24154 && ref->die_parent == comp_unit_die ()
24155 && get_AT (ref, DW_AT_specification) == NULL)
24157 child = ref;
24158 ref = NULL;
24159 static_inline_p = false;
24162 if (child->die_tag == DW_TAG_variable
24163 && child->die_parent == comp_unit_die ()
24164 && ref == NULL)
24166 reparent_child (child, context_die);
24167 if (dwarf_version < 5)
24168 child->die_tag = DW_TAG_member;
24170 else
24171 splice_child_die (context_die, child);
24174 /* Do not generate standard DWARF for variant parts if we are generating
24175 the corresponding GNAT encodings: DIEs generated for both would
24176 conflict in our mappings. */
24177 else if (is_variant_part (member)
24178 && gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL)
24180 vlr_ctx.variant_part_offset = byte_position (member);
24181 gen_variant_part (member, &vlr_ctx, context_die);
24183 else
24185 vlr_ctx.variant_part_offset = NULL_TREE;
24186 gen_decl_die (member, NULL, &vlr_ctx, context_die);
24189 /* For C++ inline static data members emit immediately a DW_TAG_variable
24190 DIE that will refer to that DW_TAG_member/DW_TAG_variable through
24191 DW_AT_specification. */
24192 if (static_inline_p)
24194 int old_extern = DECL_EXTERNAL (member);
24195 DECL_EXTERNAL (member) = 0;
24196 gen_decl_die (member, NULL, NULL, comp_unit_die ());
24197 DECL_EXTERNAL (member) = old_extern;
24201 /* We do not keep type methods in type variants. */
24202 gcc_assert (TYPE_MAIN_VARIANT (type) == type);
24203 /* Now output info about the function members (if any). */
24204 if (TYPE_METHODS (type) != error_mark_node)
24205 for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
24207 /* Don't include clones in the member list. */
24208 if (DECL_ABSTRACT_ORIGIN (member))
24209 continue;
24210 /* Nor constructors for anonymous classes. */
24211 if (DECL_ARTIFICIAL (member)
24212 && dwarf2_name (member, 0) == NULL)
24213 continue;
24215 child = lookup_decl_die (member);
24216 if (child)
24217 splice_child_die (context_die, child);
24218 else
24219 gen_decl_die (member, NULL, NULL, context_die);
24223 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
24224 is set, we pretend that the type was never defined, so we only get the
24225 member DIEs needed by later specification DIEs. */
24227 static void
24228 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
24229 enum debug_info_usage usage)
24231 if (TREE_ASM_WRITTEN (type))
24233 /* Fill in the bound of variable-length fields in late dwarf if
24234 still incomplete. */
24235 if (!early_dwarf && variably_modified_type_p (type, NULL))
24236 for (tree member = TYPE_FIELDS (type);
24237 member;
24238 member = DECL_CHAIN (member))
24239 fill_variable_array_bounds (TREE_TYPE (member));
24240 return;
24243 dw_die_ref type_die = lookup_type_die (type);
24244 dw_die_ref scope_die = 0;
24245 int nested = 0;
24246 int complete = (TYPE_SIZE (type)
24247 && (! TYPE_STUB_DECL (type)
24248 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
24249 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
24250 complete = complete && should_emit_struct_debug (type, usage);
24252 if (type_die && ! complete)
24253 return;
24255 if (TYPE_CONTEXT (type) != NULL_TREE
24256 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
24257 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
24258 nested = 1;
24260 scope_die = scope_die_for (type, context_die);
24262 /* Generate child dies for template paramaters. */
24263 if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
24264 schedule_generic_params_dies_gen (type);
24266 if (! type_die || (nested && is_cu_die (scope_die)))
24267 /* First occurrence of type or toplevel definition of nested class. */
24269 dw_die_ref old_die = type_die;
24271 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
24272 ? record_type_tag (type) : DW_TAG_union_type,
24273 scope_die, type);
24274 equate_type_number_to_die (type, type_die);
24275 if (old_die)
24276 add_AT_specification (type_die, old_die);
24277 else
24278 add_name_attribute (type_die, type_tag (type));
24280 else
24281 remove_AT (type_die, DW_AT_declaration);
24283 /* If this type has been completed, then give it a byte_size attribute and
24284 then give a list of members. */
24285 if (complete && !ns_decl)
24287 /* Prevent infinite recursion in cases where the type of some member of
24288 this type is expressed in terms of this type itself. */
24289 TREE_ASM_WRITTEN (type) = 1;
24290 add_byte_size_attribute (type_die, type);
24291 add_alignment_attribute (type_die, type);
24292 if (TYPE_STUB_DECL (type) != NULL_TREE)
24294 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
24295 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
24298 /* If the first reference to this type was as the return type of an
24299 inline function, then it may not have a parent. Fix this now. */
24300 if (type_die->die_parent == NULL)
24301 add_child_die (scope_die, type_die);
24303 push_decl_scope (type);
24304 gen_member_die (type, type_die);
24305 pop_decl_scope ();
24307 add_gnat_descriptive_type_attribute (type_die, type, context_die);
24308 if (TYPE_ARTIFICIAL (type))
24309 add_AT_flag (type_die, DW_AT_artificial, 1);
24311 /* GNU extension: Record what type our vtable lives in. */
24312 if (TYPE_VFIELD (type))
24314 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
24316 gen_type_die (vtype, context_die);
24317 add_AT_die_ref (type_die, DW_AT_containing_type,
24318 lookup_type_die (vtype));
24321 else
24323 add_AT_flag (type_die, DW_AT_declaration, 1);
24325 /* We don't need to do this for function-local types. */
24326 if (TYPE_STUB_DECL (type)
24327 && ! decl_function_context (TYPE_STUB_DECL (type)))
24328 vec_safe_push (incomplete_types, type);
24331 if (get_AT (type_die, DW_AT_name))
24332 add_pubtype (type, type_die);
24335 /* Generate a DIE for a subroutine _type_. */
24337 static void
24338 gen_subroutine_type_die (tree type, dw_die_ref context_die)
24340 tree return_type = TREE_TYPE (type);
24341 dw_die_ref subr_die
24342 = new_die (DW_TAG_subroutine_type,
24343 scope_die_for (type, context_die), type);
24345 equate_type_number_to_die (type, subr_die);
24346 add_prototyped_attribute (subr_die, type);
24347 add_type_attribute (subr_die, return_type, TYPE_UNQUALIFIED, false,
24348 context_die);
24349 add_alignment_attribute (subr_die, type);
24350 gen_formal_types_die (type, subr_die);
24352 if (get_AT (subr_die, DW_AT_name))
24353 add_pubtype (type, subr_die);
24354 if ((dwarf_version >= 5 || !dwarf_strict)
24355 && lang_hooks.types.type_dwarf_attribute (type, DW_AT_reference) != -1)
24356 add_AT_flag (subr_die, DW_AT_reference, 1);
24357 if ((dwarf_version >= 5 || !dwarf_strict)
24358 && lang_hooks.types.type_dwarf_attribute (type,
24359 DW_AT_rvalue_reference) != -1)
24360 add_AT_flag (subr_die, DW_AT_rvalue_reference, 1);
24363 /* Generate a DIE for a type definition. */
24365 static void
24366 gen_typedef_die (tree decl, dw_die_ref context_die)
24368 dw_die_ref type_die;
24369 tree origin;
24371 if (TREE_ASM_WRITTEN (decl))
24373 if (DECL_ORIGINAL_TYPE (decl))
24374 fill_variable_array_bounds (DECL_ORIGINAL_TYPE (decl));
24375 return;
24378 TREE_ASM_WRITTEN (decl) = 1;
24379 type_die = new_die (DW_TAG_typedef, context_die, decl);
24380 origin = decl_ultimate_origin (decl);
24381 if (origin != NULL)
24382 add_abstract_origin_attribute (type_die, origin);
24383 else
24385 tree type = TREE_TYPE (decl);
24387 if (type == error_mark_node)
24388 return;
24390 add_name_and_src_coords_attributes (type_die, decl);
24391 if (DECL_ORIGINAL_TYPE (decl))
24393 type = DECL_ORIGINAL_TYPE (decl);
24395 if (type == error_mark_node)
24396 return;
24398 gcc_assert (type != TREE_TYPE (decl));
24399 equate_type_number_to_die (TREE_TYPE (decl), type_die);
24401 else
24403 if (is_naming_typedef_decl (TYPE_NAME (type)))
24405 /* Here, we are in the case of decl being a typedef naming
24406 an anonymous type, e.g:
24407 typedef struct {...} foo;
24408 In that case TREE_TYPE (decl) is not a typedef variant
24409 type and TYPE_NAME of the anonymous type is set to the
24410 TYPE_DECL of the typedef. This construct is emitted by
24411 the C++ FE.
24413 TYPE is the anonymous struct named by the typedef
24414 DECL. As we need the DW_AT_type attribute of the
24415 DW_TAG_typedef to point to the DIE of TYPE, let's
24416 generate that DIE right away. add_type_attribute
24417 called below will then pick (via lookup_type_die) that
24418 anonymous struct DIE. */
24419 if (!TREE_ASM_WRITTEN (type))
24420 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
24422 /* This is a GNU Extension. We are adding a
24423 DW_AT_linkage_name attribute to the DIE of the
24424 anonymous struct TYPE. The value of that attribute
24425 is the name of the typedef decl naming the anonymous
24426 struct. This greatly eases the work of consumers of
24427 this debug info. */
24428 add_linkage_name_raw (lookup_type_die (type), decl);
24432 add_type_attribute (type_die, type, decl_quals (decl), false,
24433 context_die);
24435 if (is_naming_typedef_decl (decl))
24436 /* We want that all subsequent calls to lookup_type_die with
24437 TYPE in argument yield the DW_TAG_typedef we have just
24438 created. */
24439 equate_type_number_to_die (type, type_die);
24441 type = TREE_TYPE (decl);
24443 add_alignment_attribute (type_die, type);
24445 add_accessibility_attribute (type_die, decl);
24448 if (DECL_ABSTRACT_P (decl))
24449 equate_decl_number_to_die (decl, type_die);
24451 if (get_AT (type_die, DW_AT_name))
24452 add_pubtype (decl, type_die);
24455 /* Generate a DIE for a struct, class, enum or union type. */
24457 static void
24458 gen_tagged_type_die (tree type,
24459 dw_die_ref context_die,
24460 enum debug_info_usage usage)
24462 int need_pop;
24464 if (type == NULL_TREE
24465 || !is_tagged_type (type))
24466 return;
24468 if (TREE_ASM_WRITTEN (type))
24469 need_pop = 0;
24470 /* If this is a nested type whose containing class hasn't been written
24471 out yet, writing it out will cover this one, too. This does not apply
24472 to instantiations of member class templates; they need to be added to
24473 the containing class as they are generated. FIXME: This hurts the
24474 idea of combining type decls from multiple TUs, since we can't predict
24475 what set of template instantiations we'll get. */
24476 else if (TYPE_CONTEXT (type)
24477 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
24478 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
24480 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
24482 if (TREE_ASM_WRITTEN (type))
24483 return;
24485 /* If that failed, attach ourselves to the stub. */
24486 push_decl_scope (TYPE_CONTEXT (type));
24487 context_die = lookup_type_die (TYPE_CONTEXT (type));
24488 need_pop = 1;
24490 else if (TYPE_CONTEXT (type) != NULL_TREE
24491 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
24493 /* If this type is local to a function that hasn't been written
24494 out yet, use a NULL context for now; it will be fixed up in
24495 decls_for_scope. */
24496 context_die = lookup_decl_die (TYPE_CONTEXT (type));
24497 /* A declaration DIE doesn't count; nested types need to go in the
24498 specification. */
24499 if (context_die && is_declaration_die (context_die))
24500 context_die = NULL;
24501 need_pop = 0;
24503 else
24505 context_die = declare_in_namespace (type, context_die);
24506 need_pop = 0;
24509 if (TREE_CODE (type) == ENUMERAL_TYPE)
24511 /* This might have been written out by the call to
24512 declare_in_namespace. */
24513 if (!TREE_ASM_WRITTEN (type))
24514 gen_enumeration_type_die (type, context_die);
24516 else
24517 gen_struct_or_union_type_die (type, context_die, usage);
24519 if (need_pop)
24520 pop_decl_scope ();
24522 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
24523 it up if it is ever completed. gen_*_type_die will set it for us
24524 when appropriate. */
24527 /* Generate a type description DIE. */
24529 static void
24530 gen_type_die_with_usage (tree type, dw_die_ref context_die,
24531 enum debug_info_usage usage)
24533 struct array_descr_info info;
24535 if (type == NULL_TREE || type == error_mark_node)
24536 return;
24538 if (flag_checking && type)
24539 verify_type (type);
24541 if (TYPE_NAME (type) != NULL_TREE
24542 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
24543 && is_redundant_typedef (TYPE_NAME (type))
24544 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
24545 /* The DECL of this type is a typedef we don't want to emit debug
24546 info for but we want debug info for its underlying typedef.
24547 This can happen for e.g, the injected-class-name of a C++
24548 type. */
24549 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
24551 /* If TYPE is a typedef type variant, let's generate debug info
24552 for the parent typedef which TYPE is a type of. */
24553 if (typedef_variant_p (type))
24555 if (TREE_ASM_WRITTEN (type))
24556 return;
24558 /* Prevent broken recursion; we can't hand off to the same type. */
24559 gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
24561 /* Give typedefs the right scope. */
24562 context_die = scope_die_for (type, context_die);
24564 TREE_ASM_WRITTEN (type) = 1;
24566 gen_decl_die (TYPE_NAME (type), NULL, NULL, context_die);
24567 return;
24570 /* If type is an anonymous tagged type named by a typedef, let's
24571 generate debug info for the typedef. */
24572 if (is_naming_typedef_decl (TYPE_NAME (type)))
24574 /* Use the DIE of the containing namespace as the parent DIE of
24575 the type description DIE we want to generate. */
24576 if (DECL_CONTEXT (TYPE_NAME (type))
24577 && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
24578 context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
24580 gen_decl_die (TYPE_NAME (type), NULL, NULL, context_die);
24581 return;
24584 if (lang_hooks.types.get_debug_type)
24586 tree debug_type = lang_hooks.types.get_debug_type (type);
24588 if (debug_type != NULL_TREE && debug_type != type)
24590 gen_type_die_with_usage (debug_type, context_die, usage);
24591 return;
24595 /* We are going to output a DIE to represent the unqualified version
24596 of this type (i.e. without any const or volatile qualifiers) so
24597 get the main variant (i.e. the unqualified version) of this type
24598 now. (Vectors and arrays are special because the debugging info is in the
24599 cloned type itself. Similarly function/method types can contain extra
24600 ref-qualification). */
24601 if (TREE_CODE (type) == FUNCTION_TYPE
24602 || TREE_CODE (type) == METHOD_TYPE)
24604 /* For function/method types, can't use type_main_variant here,
24605 because that can have different ref-qualifiers for C++,
24606 but try to canonicalize. */
24607 tree main = TYPE_MAIN_VARIANT (type);
24608 for (tree t = main; t; t = TYPE_NEXT_VARIANT (t))
24609 if (TYPE_QUALS_NO_ADDR_SPACE (t) == 0
24610 && check_base_type (t, main)
24611 && check_lang_type (t, type))
24613 type = t;
24614 break;
24617 else if (TREE_CODE (type) != VECTOR_TYPE
24618 && TREE_CODE (type) != ARRAY_TYPE)
24619 type = type_main_variant (type);
24621 /* If this is an array type with hidden descriptor, handle it first. */
24622 if (!TREE_ASM_WRITTEN (type)
24623 && lang_hooks.types.get_array_descr_info)
24625 memset (&info, 0, sizeof (info));
24626 if (lang_hooks.types.get_array_descr_info (type, &info))
24628 /* Fortran sometimes emits array types with no dimension. */
24629 gcc_assert (info.ndimensions >= 0
24630 && (info.ndimensions
24631 <= DWARF2OUT_ARRAY_DESCR_INFO_MAX_DIMEN));
24632 gen_descr_array_type_die (type, &info, context_die);
24633 TREE_ASM_WRITTEN (type) = 1;
24634 return;
24638 if (TREE_ASM_WRITTEN (type))
24640 /* Variable-length types may be incomplete even if
24641 TREE_ASM_WRITTEN. For such types, fall through to
24642 gen_array_type_die() and possibly fill in
24643 DW_AT_{upper,lower}_bound attributes. */
24644 if ((TREE_CODE (type) != ARRAY_TYPE
24645 && TREE_CODE (type) != RECORD_TYPE
24646 && TREE_CODE (type) != UNION_TYPE
24647 && TREE_CODE (type) != QUAL_UNION_TYPE)
24648 || !variably_modified_type_p (type, NULL))
24649 return;
24652 switch (TREE_CODE (type))
24654 case ERROR_MARK:
24655 break;
24657 case POINTER_TYPE:
24658 case REFERENCE_TYPE:
24659 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
24660 ensures that the gen_type_die recursion will terminate even if the
24661 type is recursive. Recursive types are possible in Ada. */
24662 /* ??? We could perhaps do this for all types before the switch
24663 statement. */
24664 TREE_ASM_WRITTEN (type) = 1;
24666 /* For these types, all that is required is that we output a DIE (or a
24667 set of DIEs) to represent the "basis" type. */
24668 gen_type_die_with_usage (TREE_TYPE (type), context_die,
24669 DINFO_USAGE_IND_USE);
24670 break;
24672 case OFFSET_TYPE:
24673 /* This code is used for C++ pointer-to-data-member types.
24674 Output a description of the relevant class type. */
24675 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
24676 DINFO_USAGE_IND_USE);
24678 /* Output a description of the type of the object pointed to. */
24679 gen_type_die_with_usage (TREE_TYPE (type), context_die,
24680 DINFO_USAGE_IND_USE);
24682 /* Now output a DIE to represent this pointer-to-data-member type
24683 itself. */
24684 gen_ptr_to_mbr_type_die (type, context_die);
24685 break;
24687 case FUNCTION_TYPE:
24688 /* Force out return type (in case it wasn't forced out already). */
24689 gen_type_die_with_usage (TREE_TYPE (type), context_die,
24690 DINFO_USAGE_DIR_USE);
24691 gen_subroutine_type_die (type, context_die);
24692 break;
24694 case METHOD_TYPE:
24695 /* Force out return type (in case it wasn't forced out already). */
24696 gen_type_die_with_usage (TREE_TYPE (type), context_die,
24697 DINFO_USAGE_DIR_USE);
24698 gen_subroutine_type_die (type, context_die);
24699 break;
24701 case ARRAY_TYPE:
24702 case VECTOR_TYPE:
24703 gen_array_type_die (type, context_die);
24704 break;
24706 case ENUMERAL_TYPE:
24707 case RECORD_TYPE:
24708 case UNION_TYPE:
24709 case QUAL_UNION_TYPE:
24710 gen_tagged_type_die (type, context_die, usage);
24711 return;
24713 case VOID_TYPE:
24714 case INTEGER_TYPE:
24715 case REAL_TYPE:
24716 case FIXED_POINT_TYPE:
24717 case COMPLEX_TYPE:
24718 case BOOLEAN_TYPE:
24719 case POINTER_BOUNDS_TYPE:
24720 /* No DIEs needed for fundamental types. */
24721 break;
24723 case NULLPTR_TYPE:
24724 case LANG_TYPE:
24725 /* Just use DW_TAG_unspecified_type. */
24727 dw_die_ref type_die = lookup_type_die (type);
24728 if (type_die == NULL)
24730 tree name = TYPE_IDENTIFIER (type);
24731 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (),
24732 type);
24733 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
24734 equate_type_number_to_die (type, type_die);
24737 break;
24739 default:
24740 if (is_cxx_auto (type))
24742 tree name = TYPE_IDENTIFIER (type);
24743 dw_die_ref *die = (name == get_identifier ("auto")
24744 ? &auto_die : &decltype_auto_die);
24745 if (!*die)
24747 *die = new_die (DW_TAG_unspecified_type,
24748 comp_unit_die (), NULL_TREE);
24749 add_name_attribute (*die, IDENTIFIER_POINTER (name));
24751 equate_type_number_to_die (type, *die);
24752 break;
24754 gcc_unreachable ();
24757 TREE_ASM_WRITTEN (type) = 1;
24760 static void
24761 gen_type_die (tree type, dw_die_ref context_die)
24763 if (type != error_mark_node)
24765 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
24766 if (flag_checking)
24768 dw_die_ref die = lookup_type_die (type);
24769 if (die)
24770 check_die (die);
24775 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
24776 things which are local to the given block. */
24778 static void
24779 gen_block_die (tree stmt, dw_die_ref context_die)
24781 int must_output_die = 0;
24782 bool inlined_func;
24784 /* Ignore blocks that are NULL. */
24785 if (stmt == NULL_TREE)
24786 return;
24788 inlined_func = inlined_function_outer_scope_p (stmt);
24790 /* If the block is one fragment of a non-contiguous block, do not
24791 process the variables, since they will have been done by the
24792 origin block. Do process subblocks. */
24793 if (BLOCK_FRAGMENT_ORIGIN (stmt))
24795 tree sub;
24797 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
24798 gen_block_die (sub, context_die);
24800 return;
24803 /* Determine if we need to output any Dwarf DIEs at all to represent this
24804 block. */
24805 if (inlined_func)
24806 /* The outer scopes for inlinings *must* always be represented. We
24807 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
24808 must_output_die = 1;
24809 else
24811 /* Determine if this block directly contains any "significant"
24812 local declarations which we will need to output DIEs for. */
24813 if (debug_info_level > DINFO_LEVEL_TERSE)
24814 /* We are not in terse mode so *any* local declaration counts
24815 as being a "significant" one. */
24816 must_output_die = ((BLOCK_VARS (stmt) != NULL
24817 || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
24818 && (TREE_USED (stmt)
24819 || TREE_ASM_WRITTEN (stmt)
24820 || BLOCK_ABSTRACT (stmt)));
24821 else if ((TREE_USED (stmt)
24822 || TREE_ASM_WRITTEN (stmt)
24823 || BLOCK_ABSTRACT (stmt))
24824 && !dwarf2out_ignore_block (stmt))
24825 must_output_die = 1;
24828 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
24829 DIE for any block which contains no significant local declarations at
24830 all. Rather, in such cases we just call `decls_for_scope' so that any
24831 needed Dwarf info for any sub-blocks will get properly generated. Note
24832 that in terse mode, our definition of what constitutes a "significant"
24833 local declaration gets restricted to include only inlined function
24834 instances and local (nested) function definitions. */
24835 if (must_output_die)
24837 if (inlined_func)
24839 /* If STMT block is abstract, that means we have been called
24840 indirectly from dwarf2out_abstract_function.
24841 That function rightfully marks the descendent blocks (of
24842 the abstract function it is dealing with) as being abstract,
24843 precisely to prevent us from emitting any
24844 DW_TAG_inlined_subroutine DIE as a descendent
24845 of an abstract function instance. So in that case, we should
24846 not call gen_inlined_subroutine_die.
24848 Later though, when cgraph asks dwarf2out to emit info
24849 for the concrete instance of the function decl into which
24850 the concrete instance of STMT got inlined, the later will lead
24851 to the generation of a DW_TAG_inlined_subroutine DIE. */
24852 if (! BLOCK_ABSTRACT (stmt))
24853 gen_inlined_subroutine_die (stmt, context_die);
24855 else
24856 gen_lexical_block_die (stmt, context_die);
24858 else
24859 decls_for_scope (stmt, context_die);
24862 /* Process variable DECL (or variable with origin ORIGIN) within
24863 block STMT and add it to CONTEXT_DIE. */
24864 static void
24865 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
24867 dw_die_ref die;
24868 tree decl_or_origin = decl ? decl : origin;
24870 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
24871 die = lookup_decl_die (decl_or_origin);
24872 else if (TREE_CODE (decl_or_origin) == TYPE_DECL)
24874 if (TYPE_DECL_IS_STUB (decl_or_origin))
24875 die = lookup_type_die (TREE_TYPE (decl_or_origin));
24876 else
24877 die = lookup_decl_die (decl_or_origin);
24878 /* Avoid re-creating the DIE late if it was optimized as unused early. */
24879 if (! die && ! early_dwarf)
24880 return;
24882 else
24883 die = NULL;
24885 if (die != NULL && die->die_parent == NULL)
24886 add_child_die (context_die, die);
24887 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
24889 if (early_dwarf)
24890 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
24891 stmt, context_die);
24893 else
24894 gen_decl_die (decl, origin, NULL, context_die);
24897 /* Generate all of the decls declared within a given scope and (recursively)
24898 all of its sub-blocks. */
24900 static void
24901 decls_for_scope (tree stmt, dw_die_ref context_die)
24903 tree decl;
24904 unsigned int i;
24905 tree subblocks;
24907 /* Ignore NULL blocks. */
24908 if (stmt == NULL_TREE)
24909 return;
24911 /* Output the DIEs to represent all of the data objects and typedefs
24912 declared directly within this block but not within any nested
24913 sub-blocks. Also, nested function and tag DIEs have been
24914 generated with a parent of NULL; fix that up now. We don't
24915 have to do this if we're at -g1. */
24916 if (debug_info_level > DINFO_LEVEL_TERSE)
24918 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
24919 process_scope_var (stmt, decl, NULL_TREE, context_die);
24920 /* BLOCK_NONLOCALIZED_VARs simply generate DIE stubs with abstract
24921 origin - avoid doing this twice as we have no good way to see
24922 if we've done it once already. */
24923 if (! early_dwarf)
24924 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
24926 decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
24927 if (decl == current_function_decl)
24928 /* Ignore declarations of the current function, while they
24929 are declarations, gen_subprogram_die would treat them
24930 as definitions again, because they are equal to
24931 current_function_decl and endlessly recurse. */;
24932 else if (TREE_CODE (decl) == FUNCTION_DECL)
24933 process_scope_var (stmt, decl, NULL_TREE, context_die);
24934 else
24935 process_scope_var (stmt, NULL_TREE, decl, context_die);
24939 /* Even if we're at -g1, we need to process the subblocks in order to get
24940 inlined call information. */
24942 /* Output the DIEs to represent all sub-blocks (and the items declared
24943 therein) of this block. */
24944 for (subblocks = BLOCK_SUBBLOCKS (stmt);
24945 subblocks != NULL;
24946 subblocks = BLOCK_CHAIN (subblocks))
24947 gen_block_die (subblocks, context_die);
24950 /* Is this a typedef we can avoid emitting? */
24952 bool
24953 is_redundant_typedef (const_tree decl)
24955 if (TYPE_DECL_IS_STUB (decl))
24956 return true;
24958 if (DECL_ARTIFICIAL (decl)
24959 && DECL_CONTEXT (decl)
24960 && is_tagged_type (DECL_CONTEXT (decl))
24961 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
24962 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
24963 /* Also ignore the artificial member typedef for the class name. */
24964 return true;
24966 return false;
24969 /* Return TRUE if TYPE is a typedef that names a type for linkage
24970 purposes. This kind of typedefs is produced by the C++ FE for
24971 constructs like:
24973 typedef struct {...} foo;
24975 In that case, there is no typedef variant type produced for foo.
24976 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
24977 struct type. */
24979 static bool
24980 is_naming_typedef_decl (const_tree decl)
24982 if (decl == NULL_TREE
24983 || TREE_CODE (decl) != TYPE_DECL
24984 || DECL_NAMELESS (decl)
24985 || !is_tagged_type (TREE_TYPE (decl))
24986 || DECL_IS_BUILTIN (decl)
24987 || is_redundant_typedef (decl)
24988 /* It looks like Ada produces TYPE_DECLs that are very similar
24989 to C++ naming typedefs but that have different
24990 semantics. Let's be specific to c++ for now. */
24991 || !is_cxx (decl))
24992 return FALSE;
24994 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
24995 && TYPE_NAME (TREE_TYPE (decl)) == decl
24996 && (TYPE_STUB_DECL (TREE_TYPE (decl))
24997 != TYPE_NAME (TREE_TYPE (decl))));
25000 /* Looks up the DIE for a context. */
25002 static inline dw_die_ref
25003 lookup_context_die (tree context)
25005 if (context)
25007 /* Find die that represents this context. */
25008 if (TYPE_P (context))
25010 context = TYPE_MAIN_VARIANT (context);
25011 dw_die_ref ctx = lookup_type_die (context);
25012 if (!ctx)
25013 return NULL;
25014 return strip_naming_typedef (context, ctx);
25016 else
25017 return lookup_decl_die (context);
25019 return comp_unit_die ();
25022 /* Returns the DIE for a context. */
25024 static inline dw_die_ref
25025 get_context_die (tree context)
25027 if (context)
25029 /* Find die that represents this context. */
25030 if (TYPE_P (context))
25032 context = TYPE_MAIN_VARIANT (context);
25033 return strip_naming_typedef (context, force_type_die (context));
25035 else
25036 return force_decl_die (context);
25038 return comp_unit_die ();
25041 /* Returns the DIE for decl. A DIE will always be returned. */
25043 static dw_die_ref
25044 force_decl_die (tree decl)
25046 dw_die_ref decl_die;
25047 unsigned saved_external_flag;
25048 tree save_fn = NULL_TREE;
25049 decl_die = lookup_decl_die (decl);
25050 if (!decl_die)
25052 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
25054 decl_die = lookup_decl_die (decl);
25055 if (decl_die)
25056 return decl_die;
25058 switch (TREE_CODE (decl))
25060 case FUNCTION_DECL:
25061 /* Clear current_function_decl, so that gen_subprogram_die thinks
25062 that this is a declaration. At this point, we just want to force
25063 declaration die. */
25064 save_fn = current_function_decl;
25065 current_function_decl = NULL_TREE;
25066 gen_subprogram_die (decl, context_die);
25067 current_function_decl = save_fn;
25068 break;
25070 case VAR_DECL:
25071 /* Set external flag to force declaration die. Restore it after
25072 gen_decl_die() call. */
25073 saved_external_flag = DECL_EXTERNAL (decl);
25074 DECL_EXTERNAL (decl) = 1;
25075 gen_decl_die (decl, NULL, NULL, context_die);
25076 DECL_EXTERNAL (decl) = saved_external_flag;
25077 break;
25079 case NAMESPACE_DECL:
25080 if (dwarf_version >= 3 || !dwarf_strict)
25081 dwarf2out_decl (decl);
25082 else
25083 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
25084 decl_die = comp_unit_die ();
25085 break;
25087 case TRANSLATION_UNIT_DECL:
25088 decl_die = comp_unit_die ();
25089 break;
25091 default:
25092 gcc_unreachable ();
25095 /* We should be able to find the DIE now. */
25096 if (!decl_die)
25097 decl_die = lookup_decl_die (decl);
25098 gcc_assert (decl_die);
25101 return decl_die;
25104 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
25105 always returned. */
25107 static dw_die_ref
25108 force_type_die (tree type)
25110 dw_die_ref type_die;
25112 type_die = lookup_type_die (type);
25113 if (!type_die)
25115 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
25117 type_die = modified_type_die (type, TYPE_QUALS_NO_ADDR_SPACE (type),
25118 false, context_die);
25119 gcc_assert (type_die);
25121 return type_die;
25124 /* Force out any required namespaces to be able to output DECL,
25125 and return the new context_die for it, if it's changed. */
25127 static dw_die_ref
25128 setup_namespace_context (tree thing, dw_die_ref context_die)
25130 tree context = (DECL_P (thing)
25131 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
25132 if (context && TREE_CODE (context) == NAMESPACE_DECL)
25133 /* Force out the namespace. */
25134 context_die = force_decl_die (context);
25136 return context_die;
25139 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
25140 type) within its namespace, if appropriate.
25142 For compatibility with older debuggers, namespace DIEs only contain
25143 declarations; all definitions are emitted at CU scope, with
25144 DW_AT_specification pointing to the declaration (like with class
25145 members). */
25147 static dw_die_ref
25148 declare_in_namespace (tree thing, dw_die_ref context_die)
25150 dw_die_ref ns_context;
25152 if (debug_info_level <= DINFO_LEVEL_TERSE)
25153 return context_die;
25155 /* External declarations in the local scope only need to be emitted
25156 once, not once in the namespace and once in the scope.
25158 This avoids declaring the `extern' below in the
25159 namespace DIE as well as in the innermost scope:
25161 namespace S
25163 int i=5;
25164 int foo()
25166 int i=8;
25167 extern int i;
25168 return i;
25172 if (DECL_P (thing) && DECL_EXTERNAL (thing) && local_scope_p (context_die))
25173 return context_die;
25175 /* If this decl is from an inlined function, then don't try to emit it in its
25176 namespace, as we will get confused. It would have already been emitted
25177 when the abstract instance of the inline function was emitted anyways. */
25178 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
25179 return context_die;
25181 ns_context = setup_namespace_context (thing, context_die);
25183 if (ns_context != context_die)
25185 if (is_fortran ())
25186 return ns_context;
25187 if (DECL_P (thing))
25188 gen_decl_die (thing, NULL, NULL, ns_context);
25189 else
25190 gen_type_die (thing, ns_context);
25192 return context_die;
25195 /* Generate a DIE for a namespace or namespace alias. */
25197 static void
25198 gen_namespace_die (tree decl, dw_die_ref context_die)
25200 dw_die_ref namespace_die;
25202 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
25203 they are an alias of. */
25204 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
25206 /* Output a real namespace or module. */
25207 context_die = setup_namespace_context (decl, comp_unit_die ());
25208 namespace_die = new_die (is_fortran ()
25209 ? DW_TAG_module : DW_TAG_namespace,
25210 context_die, decl);
25211 /* For Fortran modules defined in different CU don't add src coords. */
25212 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
25214 const char *name = dwarf2_name (decl, 0);
25215 if (name)
25216 add_name_attribute (namespace_die, name);
25218 else
25219 add_name_and_src_coords_attributes (namespace_die, decl);
25220 if (DECL_EXTERNAL (decl))
25221 add_AT_flag (namespace_die, DW_AT_declaration, 1);
25222 equate_decl_number_to_die (decl, namespace_die);
25224 else
25226 /* Output a namespace alias. */
25228 /* Force out the namespace we are an alias of, if necessary. */
25229 dw_die_ref origin_die
25230 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
25232 if (DECL_FILE_SCOPE_P (decl)
25233 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
25234 context_die = setup_namespace_context (decl, comp_unit_die ());
25235 /* Now create the namespace alias DIE. */
25236 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
25237 add_name_and_src_coords_attributes (namespace_die, decl);
25238 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
25239 equate_decl_number_to_die (decl, namespace_die);
25241 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
25242 if (want_pubnames ())
25243 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
25246 /* Generate Dwarf debug information for a decl described by DECL.
25247 The return value is currently only meaningful for PARM_DECLs,
25248 for all other decls it returns NULL.
25250 If DECL is a FIELD_DECL, CTX is required: see the comment for VLR_CONTEXT.
25251 It can be NULL otherwise. */
25253 static dw_die_ref
25254 gen_decl_die (tree decl, tree origin, struct vlr_context *ctx,
25255 dw_die_ref context_die)
25257 tree decl_or_origin = decl ? decl : origin;
25258 tree class_origin = NULL, ultimate_origin;
25260 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
25261 return NULL;
25263 /* Ignore pointer bounds decls. */
25264 if (DECL_P (decl_or_origin)
25265 && TREE_TYPE (decl_or_origin)
25266 && POINTER_BOUNDS_P (decl_or_origin))
25267 return NULL;
25269 switch (TREE_CODE (decl_or_origin))
25271 case ERROR_MARK:
25272 break;
25274 case CONST_DECL:
25275 if (!is_fortran () && !is_ada ())
25277 /* The individual enumerators of an enum type get output when we output
25278 the Dwarf representation of the relevant enum type itself. */
25279 break;
25282 /* Emit its type. */
25283 gen_type_die (TREE_TYPE (decl), context_die);
25285 /* And its containing namespace. */
25286 context_die = declare_in_namespace (decl, context_die);
25288 gen_const_die (decl, context_die);
25289 break;
25291 case FUNCTION_DECL:
25292 #if 0
25293 /* FIXME */
25294 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
25295 on local redeclarations of global functions. That seems broken. */
25296 if (current_function_decl != decl)
25297 /* This is only a declaration. */;
25298 #endif
25300 /* If we're emitting a clone, emit info for the abstract instance. */
25301 if (origin || DECL_ORIGIN (decl) != decl)
25302 dwarf2out_abstract_function (origin
25303 ? DECL_ORIGIN (origin)
25304 : DECL_ABSTRACT_ORIGIN (decl));
25306 /* If we're emitting an out-of-line copy of an inline function,
25307 emit info for the abstract instance and set up to refer to it. */
25308 else if (cgraph_function_possibly_inlined_p (decl)
25309 && ! DECL_ABSTRACT_P (decl)
25310 && ! class_or_namespace_scope_p (context_die)
25311 /* dwarf2out_abstract_function won't emit a die if this is just
25312 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
25313 that case, because that works only if we have a die. */
25314 && DECL_INITIAL (decl) != NULL_TREE)
25316 dwarf2out_abstract_function (decl);
25317 set_decl_origin_self (decl);
25320 /* Otherwise we're emitting the primary DIE for this decl. */
25321 else if (debug_info_level > DINFO_LEVEL_TERSE)
25323 /* Before we describe the FUNCTION_DECL itself, make sure that we
25324 have its containing type. */
25325 if (!origin)
25326 origin = decl_class_context (decl);
25327 if (origin != NULL_TREE)
25328 gen_type_die (origin, context_die);
25330 /* And its return type. */
25331 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
25333 /* And its virtual context. */
25334 if (DECL_VINDEX (decl) != NULL_TREE)
25335 gen_type_die (DECL_CONTEXT (decl), context_die);
25337 /* Make sure we have a member DIE for decl. */
25338 if (origin != NULL_TREE)
25339 gen_type_die_for_member (origin, decl, context_die);
25341 /* And its containing namespace. */
25342 context_die = declare_in_namespace (decl, context_die);
25345 /* Now output a DIE to represent the function itself. */
25346 if (decl)
25347 gen_subprogram_die (decl, context_die);
25348 break;
25350 case TYPE_DECL:
25351 /* If we are in terse mode, don't generate any DIEs to represent any
25352 actual typedefs. */
25353 if (debug_info_level <= DINFO_LEVEL_TERSE)
25354 break;
25356 /* In the special case of a TYPE_DECL node representing the declaration
25357 of some type tag, if the given TYPE_DECL is marked as having been
25358 instantiated from some other (original) TYPE_DECL node (e.g. one which
25359 was generated within the original definition of an inline function) we
25360 used to generate a special (abbreviated) DW_TAG_structure_type,
25361 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
25362 should be actually referencing those DIEs, as variable DIEs with that
25363 type would be emitted already in the abstract origin, so it was always
25364 removed during unused type prunning. Don't add anything in this
25365 case. */
25366 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
25367 break;
25369 if (is_redundant_typedef (decl))
25370 gen_type_die (TREE_TYPE (decl), context_die);
25371 else
25372 /* Output a DIE to represent the typedef itself. */
25373 gen_typedef_die (decl, context_die);
25374 break;
25376 case LABEL_DECL:
25377 if (debug_info_level >= DINFO_LEVEL_NORMAL)
25378 gen_label_die (decl, context_die);
25379 break;
25381 case VAR_DECL:
25382 case RESULT_DECL:
25383 /* If we are in terse mode, don't generate any DIEs to represent any
25384 variable declarations or definitions. */
25385 if (debug_info_level <= DINFO_LEVEL_TERSE)
25386 break;
25388 /* Output any DIEs that are needed to specify the type of this data
25389 object. */
25390 if (decl_by_reference_p (decl_or_origin))
25391 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
25392 else
25393 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
25395 /* And its containing type. */
25396 class_origin = decl_class_context (decl_or_origin);
25397 if (class_origin != NULL_TREE)
25398 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
25400 /* And its containing namespace. */
25401 context_die = declare_in_namespace (decl_or_origin, context_die);
25403 /* Now output the DIE to represent the data object itself. This gets
25404 complicated because of the possibility that the VAR_DECL really
25405 represents an inlined instance of a formal parameter for an inline
25406 function. */
25407 ultimate_origin = decl_ultimate_origin (decl_or_origin);
25408 if (ultimate_origin != NULL_TREE
25409 && TREE_CODE (ultimate_origin) == PARM_DECL)
25410 gen_formal_parameter_die (decl, origin,
25411 true /* Emit name attribute. */,
25412 context_die);
25413 else
25414 gen_variable_die (decl, origin, context_die);
25415 break;
25417 case FIELD_DECL:
25418 gcc_assert (ctx != NULL && ctx->struct_type != NULL);
25419 /* Ignore the nameless fields that are used to skip bits but handle C++
25420 anonymous unions and structs. */
25421 if (DECL_NAME (decl) != NULL_TREE
25422 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
25423 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
25425 gen_type_die (member_declared_type (decl), context_die);
25426 gen_field_die (decl, ctx, context_die);
25428 break;
25430 case PARM_DECL:
25431 if (DECL_BY_REFERENCE (decl_or_origin))
25432 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
25433 else
25434 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
25435 return gen_formal_parameter_die (decl, origin,
25436 true /* Emit name attribute. */,
25437 context_die);
25439 case NAMESPACE_DECL:
25440 if (dwarf_version >= 3 || !dwarf_strict)
25441 gen_namespace_die (decl, context_die);
25442 break;
25444 case IMPORTED_DECL:
25445 dwarf2out_imported_module_or_decl_1 (decl, DECL_NAME (decl),
25446 DECL_CONTEXT (decl), context_die);
25447 break;
25449 case NAMELIST_DECL:
25450 gen_namelist_decl (DECL_NAME (decl), context_die,
25451 NAMELIST_DECL_ASSOCIATED_DECL (decl));
25452 break;
25454 default:
25455 /* Probably some frontend-internal decl. Assume we don't care. */
25456 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
25457 break;
25460 return NULL;
25463 /* Output initial debug information for global DECL. Called at the
25464 end of the parsing process.
25466 This is the initial debug generation process. As such, the DIEs
25467 generated may be incomplete. A later debug generation pass
25468 (dwarf2out_late_global_decl) will augment the information generated
25469 in this pass (e.g., with complete location info). */
25471 static void
25472 dwarf2out_early_global_decl (tree decl)
25474 set_early_dwarf s;
25476 /* gen_decl_die() will set DECL_ABSTRACT because
25477 cgraph_function_possibly_inlined_p() returns true. This is in
25478 turn will cause DW_AT_inline attributes to be set.
25480 This happens because at early dwarf generation, there is no
25481 cgraph information, causing cgraph_function_possibly_inlined_p()
25482 to return true. Trick cgraph_function_possibly_inlined_p()
25483 while we generate dwarf early. */
25484 bool save = symtab->global_info_ready;
25485 symtab->global_info_ready = true;
25487 /* We don't handle TYPE_DECLs. If required, they'll be reached via
25488 other DECLs and they can point to template types or other things
25489 that dwarf2out can't handle when done via dwarf2out_decl. */
25490 if (TREE_CODE (decl) != TYPE_DECL
25491 && TREE_CODE (decl) != PARM_DECL)
25493 tree save_fndecl = current_function_decl;
25494 if (TREE_CODE (decl) == FUNCTION_DECL)
25496 /* For nested functions, make sure we have DIEs for the parents first
25497 so that all nested DIEs are generated at the proper scope in the
25498 first shot. */
25499 tree context = decl_function_context (decl);
25500 if (context != NULL && lookup_decl_die (context) == NULL)
25502 current_function_decl = context;
25503 dwarf2out_decl (context);
25506 current_function_decl = decl;
25508 dwarf2out_decl (decl);
25509 if (TREE_CODE (decl) == FUNCTION_DECL)
25510 current_function_decl = save_fndecl;
25512 symtab->global_info_ready = save;
25515 /* Output debug information for global decl DECL. Called from
25516 toplev.c after compilation proper has finished. */
25518 static void
25519 dwarf2out_late_global_decl (tree decl)
25521 /* Fill-in any location information we were unable to determine
25522 on the first pass. */
25523 if (VAR_P (decl) && !POINTER_BOUNDS_P (decl))
25525 dw_die_ref die = lookup_decl_die (decl);
25527 /* We have to generate early debug late for LTO. */
25528 if (! die && in_lto_p)
25530 dwarf2out_decl (decl);
25531 die = lookup_decl_die (decl);
25534 if (die)
25536 /* We get called via the symtab code invoking late_global_decl
25537 for symbols that are optimized out. Do not add locations
25538 for those, except if they have a DECL_VALUE_EXPR, in which case
25539 they are relevant for debuggers. */
25540 varpool_node *node = varpool_node::get (decl);
25541 if ((! node || ! node->definition) && ! DECL_HAS_VALUE_EXPR_P (decl))
25542 tree_add_const_value_attribute_for_decl (die, decl);
25543 else
25544 add_location_or_const_value_attribute (die, decl, false);
25549 /* Output debug information for type decl DECL. Called from toplev.c
25550 and from language front ends (to record built-in types). */
25551 static void
25552 dwarf2out_type_decl (tree decl, int local)
25554 if (!local)
25556 set_early_dwarf s;
25557 dwarf2out_decl (decl);
25561 /* Output debug information for imported module or decl DECL.
25562 NAME is non-NULL name in the lexical block if the decl has been renamed.
25563 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
25564 that DECL belongs to.
25565 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
25566 static void
25567 dwarf2out_imported_module_or_decl_1 (tree decl,
25568 tree name,
25569 tree lexical_block,
25570 dw_die_ref lexical_block_die)
25572 expanded_location xloc;
25573 dw_die_ref imported_die = NULL;
25574 dw_die_ref at_import_die;
25576 if (TREE_CODE (decl) == IMPORTED_DECL)
25578 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
25579 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
25580 gcc_assert (decl);
25582 else
25583 xloc = expand_location (input_location);
25585 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
25587 at_import_die = force_type_die (TREE_TYPE (decl));
25588 /* For namespace N { typedef void T; } using N::T; base_type_die
25589 returns NULL, but DW_TAG_imported_declaration requires
25590 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
25591 if (!at_import_die)
25593 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
25594 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
25595 at_import_die = lookup_type_die (TREE_TYPE (decl));
25596 gcc_assert (at_import_die);
25599 else
25601 at_import_die = lookup_decl_die (decl);
25602 if (!at_import_die)
25604 /* If we're trying to avoid duplicate debug info, we may not have
25605 emitted the member decl for this field. Emit it now. */
25606 if (TREE_CODE (decl) == FIELD_DECL)
25608 tree type = DECL_CONTEXT (decl);
25610 if (TYPE_CONTEXT (type)
25611 && TYPE_P (TYPE_CONTEXT (type))
25612 && !should_emit_struct_debug (TYPE_CONTEXT (type),
25613 DINFO_USAGE_DIR_USE))
25614 return;
25615 gen_type_die_for_member (type, decl,
25616 get_context_die (TYPE_CONTEXT (type)));
25618 if (TREE_CODE (decl) == NAMELIST_DECL)
25619 at_import_die = gen_namelist_decl (DECL_NAME (decl),
25620 get_context_die (DECL_CONTEXT (decl)),
25621 NULL_TREE);
25622 else
25623 at_import_die = force_decl_die (decl);
25627 if (TREE_CODE (decl) == NAMESPACE_DECL)
25629 if (dwarf_version >= 3 || !dwarf_strict)
25630 imported_die = new_die (DW_TAG_imported_module,
25631 lexical_block_die,
25632 lexical_block);
25633 else
25634 return;
25636 else
25637 imported_die = new_die (DW_TAG_imported_declaration,
25638 lexical_block_die,
25639 lexical_block);
25641 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
25642 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
25643 if (debug_column_info && xloc.column)
25644 add_AT_unsigned (imported_die, DW_AT_decl_column, xloc.column);
25645 if (name)
25646 add_AT_string (imported_die, DW_AT_name,
25647 IDENTIFIER_POINTER (name));
25648 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
25651 /* Output debug information for imported module or decl DECL.
25652 NAME is non-NULL name in context if the decl has been renamed.
25653 CHILD is true if decl is one of the renamed decls as part of
25654 importing whole module. */
25656 static void
25657 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
25658 bool child)
25660 /* dw_die_ref at_import_die; */
25661 dw_die_ref scope_die;
25663 if (debug_info_level <= DINFO_LEVEL_TERSE)
25664 return;
25666 gcc_assert (decl);
25668 set_early_dwarf s;
25670 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
25671 We need decl DIE for reference and scope die. First, get DIE for the decl
25672 itself. */
25674 /* Get the scope die for decl context. Use comp_unit_die for global module
25675 or decl. If die is not found for non globals, force new die. */
25676 if (context
25677 && TYPE_P (context)
25678 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
25679 return;
25681 scope_die = get_context_die (context);
25683 if (child)
25685 /* DW_TAG_imported_module was introduced in the DWARFv3 specification, so
25686 there is nothing we can do, here. */
25687 if (dwarf_version < 3 && dwarf_strict)
25688 return;
25690 gcc_assert (scope_die->die_child);
25691 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
25692 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
25693 scope_die = scope_die->die_child;
25696 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
25697 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
25700 /* Output debug information for namelists. */
25702 static dw_die_ref
25703 gen_namelist_decl (tree name, dw_die_ref scope_die, tree item_decls)
25705 dw_die_ref nml_die, nml_item_die, nml_item_ref_die;
25706 tree value;
25707 unsigned i;
25709 if (debug_info_level <= DINFO_LEVEL_TERSE)
25710 return NULL;
25712 gcc_assert (scope_die != NULL);
25713 nml_die = new_die (DW_TAG_namelist, scope_die, NULL);
25714 add_AT_string (nml_die, DW_AT_name, IDENTIFIER_POINTER (name));
25716 /* If there are no item_decls, we have a nondefining namelist, e.g.
25717 with USE association; hence, set DW_AT_declaration. */
25718 if (item_decls == NULL_TREE)
25720 add_AT_flag (nml_die, DW_AT_declaration, 1);
25721 return nml_die;
25724 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (item_decls), i, value)
25726 nml_item_ref_die = lookup_decl_die (value);
25727 if (!nml_item_ref_die)
25728 nml_item_ref_die = force_decl_die (value);
25730 nml_item_die = new_die (DW_TAG_namelist_item, nml_die, NULL);
25731 add_AT_die_ref (nml_item_die, DW_AT_namelist_items, nml_item_ref_die);
25733 return nml_die;
25737 /* Write the debugging output for DECL and return the DIE. */
25739 static void
25740 dwarf2out_decl (tree decl)
25742 dw_die_ref context_die = comp_unit_die ();
25744 switch (TREE_CODE (decl))
25746 case ERROR_MARK:
25747 return;
25749 case FUNCTION_DECL:
25750 /* If we're a nested function, initially use a parent of NULL; if we're
25751 a plain function, this will be fixed up in decls_for_scope. If
25752 we're a method, it will be ignored, since we already have a DIE. */
25753 if (decl_function_context (decl)
25754 /* But if we're in terse mode, we don't care about scope. */
25755 && debug_info_level > DINFO_LEVEL_TERSE)
25756 context_die = NULL;
25757 break;
25759 case VAR_DECL:
25760 /* For local statics lookup proper context die. */
25761 if (local_function_static (decl))
25762 context_die = lookup_decl_die (DECL_CONTEXT (decl));
25764 /* If we are in terse mode, don't generate any DIEs to represent any
25765 variable declarations or definitions. */
25766 if (debug_info_level <= DINFO_LEVEL_TERSE)
25767 return;
25768 break;
25770 case CONST_DECL:
25771 if (debug_info_level <= DINFO_LEVEL_TERSE)
25772 return;
25773 if (!is_fortran () && !is_ada ())
25774 return;
25775 if (TREE_STATIC (decl) && decl_function_context (decl))
25776 context_die = lookup_decl_die (DECL_CONTEXT (decl));
25777 break;
25779 case NAMESPACE_DECL:
25780 case IMPORTED_DECL:
25781 if (debug_info_level <= DINFO_LEVEL_TERSE)
25782 return;
25783 if (lookup_decl_die (decl) != NULL)
25784 return;
25785 break;
25787 case TYPE_DECL:
25788 /* Don't emit stubs for types unless they are needed by other DIEs. */
25789 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
25790 return;
25792 /* Don't bother trying to generate any DIEs to represent any of the
25793 normal built-in types for the language we are compiling. */
25794 if (DECL_IS_BUILTIN (decl))
25795 return;
25797 /* If we are in terse mode, don't generate any DIEs for types. */
25798 if (debug_info_level <= DINFO_LEVEL_TERSE)
25799 return;
25801 /* If we're a function-scope tag, initially use a parent of NULL;
25802 this will be fixed up in decls_for_scope. */
25803 if (decl_function_context (decl))
25804 context_die = NULL;
25806 break;
25808 case NAMELIST_DECL:
25809 break;
25811 default:
25812 return;
25815 gen_decl_die (decl, NULL, NULL, context_die);
25817 if (flag_checking)
25819 dw_die_ref die = lookup_decl_die (decl);
25820 if (die)
25821 check_die (die);
25825 /* Write the debugging output for DECL. */
25827 static void
25828 dwarf2out_function_decl (tree decl)
25830 dwarf2out_decl (decl);
25831 call_arg_locations = NULL;
25832 call_arg_loc_last = NULL;
25833 call_site_count = -1;
25834 tail_call_site_count = -1;
25835 decl_loc_table->empty ();
25836 cached_dw_loc_list_table->empty ();
25839 /* Output a marker (i.e. a label) for the beginning of the generated code for
25840 a lexical block. */
25842 static void
25843 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
25844 unsigned int blocknum)
25846 switch_to_section (current_function_section ());
25847 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
25850 /* Output a marker (i.e. a label) for the end of the generated code for a
25851 lexical block. */
25853 static void
25854 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
25856 switch_to_section (current_function_section ());
25857 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
25860 /* Returns nonzero if it is appropriate not to emit any debugging
25861 information for BLOCK, because it doesn't contain any instructions.
25863 Don't allow this for blocks with nested functions or local classes
25864 as we would end up with orphans, and in the presence of scheduling
25865 we may end up calling them anyway. */
25867 static bool
25868 dwarf2out_ignore_block (const_tree block)
25870 tree decl;
25871 unsigned int i;
25873 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
25874 if (TREE_CODE (decl) == FUNCTION_DECL
25875 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
25876 return 0;
25877 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
25879 decl = BLOCK_NONLOCALIZED_VAR (block, i);
25880 if (TREE_CODE (decl) == FUNCTION_DECL
25881 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
25882 return 0;
25885 return 1;
25888 /* Hash table routines for file_hash. */
25890 bool
25891 dwarf_file_hasher::equal (dwarf_file_data *p1, const char *p2)
25893 return filename_cmp (p1->filename, p2) == 0;
25896 hashval_t
25897 dwarf_file_hasher::hash (dwarf_file_data *p)
25899 return htab_hash_string (p->filename);
25902 /* Lookup FILE_NAME (in the list of filenames that we know about here in
25903 dwarf2out.c) and return its "index". The index of each (known) filename is
25904 just a unique number which is associated with only that one filename. We
25905 need such numbers for the sake of generating labels (in the .debug_sfnames
25906 section) and references to those files numbers (in the .debug_srcinfo
25907 and .debug_macinfo sections). If the filename given as an argument is not
25908 found in our current list, add it to the list and assign it the next
25909 available unique index number. */
25911 static struct dwarf_file_data *
25912 lookup_filename (const char *file_name)
25914 struct dwarf_file_data * created;
25916 if (!file_name)
25917 return NULL;
25919 dwarf_file_data **slot
25920 = file_table->find_slot_with_hash (file_name, htab_hash_string (file_name),
25921 INSERT);
25922 if (*slot)
25923 return *slot;
25925 created = ggc_alloc<dwarf_file_data> ();
25926 created->filename = file_name;
25927 created->emitted_number = 0;
25928 *slot = created;
25929 return created;
25932 /* If the assembler will construct the file table, then translate the compiler
25933 internal file table number into the assembler file table number, and emit
25934 a .file directive if we haven't already emitted one yet. The file table
25935 numbers are different because we prune debug info for unused variables and
25936 types, which may include filenames. */
25938 static int
25939 maybe_emit_file (struct dwarf_file_data * fd)
25941 if (! fd->emitted_number)
25943 if (last_emitted_file)
25944 fd->emitted_number = last_emitted_file->emitted_number + 1;
25945 else
25946 fd->emitted_number = 1;
25947 last_emitted_file = fd;
25949 if (DWARF2_ASM_LINE_DEBUG_INFO)
25951 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
25952 output_quoted_string (asm_out_file,
25953 remap_debug_filename (fd->filename));
25954 fputc ('\n', asm_out_file);
25958 return fd->emitted_number;
25961 /* Schedule generation of a DW_AT_const_value attribute to DIE.
25962 That generation should happen after function debug info has been
25963 generated. The value of the attribute is the constant value of ARG. */
25965 static void
25966 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
25968 die_arg_entry entry;
25970 if (!die || !arg)
25971 return;
25973 gcc_assert (early_dwarf);
25975 if (!tmpl_value_parm_die_table)
25976 vec_alloc (tmpl_value_parm_die_table, 32);
25978 entry.die = die;
25979 entry.arg = arg;
25980 vec_safe_push (tmpl_value_parm_die_table, entry);
25983 /* Return TRUE if T is an instance of generic type, FALSE
25984 otherwise. */
25986 static bool
25987 generic_type_p (tree t)
25989 if (t == NULL_TREE || !TYPE_P (t))
25990 return false;
25991 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
25994 /* Schedule the generation of the generic parameter dies for the
25995 instance of generic type T. The proper generation itself is later
25996 done by gen_scheduled_generic_parms_dies. */
25998 static void
25999 schedule_generic_params_dies_gen (tree t)
26001 if (!generic_type_p (t))
26002 return;
26004 gcc_assert (early_dwarf);
26006 if (!generic_type_instances)
26007 vec_alloc (generic_type_instances, 256);
26009 vec_safe_push (generic_type_instances, t);
26012 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
26013 by append_entry_to_tmpl_value_parm_die_table. This function must
26014 be called after function DIEs have been generated. */
26016 static void
26017 gen_remaining_tmpl_value_param_die_attribute (void)
26019 if (tmpl_value_parm_die_table)
26021 unsigned i, j;
26022 die_arg_entry *e;
26024 /* We do this in two phases - first get the cases we can
26025 handle during early-finish, preserving those we cannot
26026 (containing symbolic constants where we don't yet know
26027 whether we are going to output the referenced symbols).
26028 For those we try again at late-finish. */
26029 j = 0;
26030 FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
26032 if (!tree_add_const_value_attribute (e->die, e->arg))
26034 dw_loc_descr_ref loc = NULL;
26035 if (! early_dwarf
26036 && (dwarf_version >= 5 || !dwarf_strict))
26037 loc = loc_descriptor_from_tree (e->arg, 2, NULL);
26038 if (loc)
26039 add_AT_loc (e->die, DW_AT_location, loc);
26040 else
26041 (*tmpl_value_parm_die_table)[j++] = *e;
26044 tmpl_value_parm_die_table->truncate (j);
26048 /* Generate generic parameters DIEs for instances of generic types
26049 that have been previously scheduled by
26050 schedule_generic_params_dies_gen. This function must be called
26051 after all the types of the CU have been laid out. */
26053 static void
26054 gen_scheduled_generic_parms_dies (void)
26056 unsigned i;
26057 tree t;
26059 if (!generic_type_instances)
26060 return;
26062 FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
26063 if (COMPLETE_TYPE_P (t))
26064 gen_generic_params_dies (t);
26066 generic_type_instances = NULL;
26070 /* Replace DW_AT_name for the decl with name. */
26072 static void
26073 dwarf2out_set_name (tree decl, tree name)
26075 dw_die_ref die;
26076 dw_attr_node *attr;
26077 const char *dname;
26079 die = TYPE_SYMTAB_DIE (decl);
26080 if (!die)
26081 return;
26083 dname = dwarf2_name (name, 0);
26084 if (!dname)
26085 return;
26087 attr = get_AT (die, DW_AT_name);
26088 if (attr)
26090 struct indirect_string_node *node;
26092 node = find_AT_string (dname);
26093 /* replace the string. */
26094 attr->dw_attr_val.v.val_str = node;
26097 else
26098 add_name_attribute (die, dname);
26101 /* True if before or during processing of the first function being emitted. */
26102 static bool in_first_function_p = true;
26103 /* True if loc_note during dwarf2out_var_location call might still be
26104 before first real instruction at address equal to .Ltext0. */
26105 static bool maybe_at_text_label_p = true;
26106 /* One above highest N where .LVLN label might be equal to .Ltext0 label. */
26107 static unsigned int first_loclabel_num_not_at_text_label;
26109 /* Called by the final INSN scan whenever we see a var location. We
26110 use it to drop labels in the right places, and throw the location in
26111 our lookup table. */
26113 static void
26114 dwarf2out_var_location (rtx_insn *loc_note)
26116 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
26117 struct var_loc_node *newloc;
26118 rtx_insn *next_real, *next_note;
26119 rtx_insn *call_insn = NULL;
26120 static const char *last_label;
26121 static const char *last_postcall_label;
26122 static bool last_in_cold_section_p;
26123 static rtx_insn *expected_next_loc_note;
26124 tree decl;
26125 bool var_loc_p;
26127 if (!NOTE_P (loc_note))
26129 if (CALL_P (loc_note))
26131 call_site_count++;
26132 if (SIBLING_CALL_P (loc_note))
26133 tail_call_site_count++;
26134 if (optimize == 0 && !flag_var_tracking)
26136 /* When the var-tracking pass is not running, there is no note
26137 for indirect calls whose target is compile-time known. In this
26138 case, process such calls specifically so that we generate call
26139 sites for them anyway. */
26140 rtx x = PATTERN (loc_note);
26141 if (GET_CODE (x) == PARALLEL)
26142 x = XVECEXP (x, 0, 0);
26143 if (GET_CODE (x) == SET)
26144 x = SET_SRC (x);
26145 if (GET_CODE (x) == CALL)
26146 x = XEXP (x, 0);
26147 if (!MEM_P (x)
26148 || GET_CODE (XEXP (x, 0)) != SYMBOL_REF
26149 || !SYMBOL_REF_DECL (XEXP (x, 0))
26150 || (TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0)))
26151 != FUNCTION_DECL))
26153 call_insn = loc_note;
26154 loc_note = NULL;
26155 var_loc_p = false;
26157 next_real = next_real_insn (call_insn);
26158 next_note = NULL;
26159 cached_next_real_insn = NULL;
26160 goto create_label;
26164 return;
26167 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
26168 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
26169 return;
26171 /* Optimize processing a large consecutive sequence of location
26172 notes so we don't spend too much time in next_real_insn. If the
26173 next insn is another location note, remember the next_real_insn
26174 calculation for next time. */
26175 next_real = cached_next_real_insn;
26176 if (next_real)
26178 if (expected_next_loc_note != loc_note)
26179 next_real = NULL;
26182 next_note = NEXT_INSN (loc_note);
26183 if (! next_note
26184 || next_note->deleted ()
26185 || ! NOTE_P (next_note)
26186 || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
26187 && NOTE_KIND (next_note) != NOTE_INSN_CALL_ARG_LOCATION))
26188 next_note = NULL;
26190 if (! next_real)
26191 next_real = next_real_insn (loc_note);
26193 if (next_note)
26195 expected_next_loc_note = next_note;
26196 cached_next_real_insn = next_real;
26198 else
26199 cached_next_real_insn = NULL;
26201 /* If there are no instructions which would be affected by this note,
26202 don't do anything. */
26203 if (var_loc_p
26204 && next_real == NULL_RTX
26205 && !NOTE_DURING_CALL_P (loc_note))
26206 return;
26208 create_label:
26210 if (next_real == NULL_RTX)
26211 next_real = get_last_insn ();
26213 /* If there were any real insns between note we processed last time
26214 and this note (or if it is the first note), clear
26215 last_{,postcall_}label so that they are not reused this time. */
26216 if (last_var_location_insn == NULL_RTX
26217 || last_var_location_insn != next_real
26218 || last_in_cold_section_p != in_cold_section_p)
26220 last_label = NULL;
26221 last_postcall_label = NULL;
26224 if (var_loc_p)
26226 decl = NOTE_VAR_LOCATION_DECL (loc_note);
26227 newloc = add_var_loc_to_decl (decl, loc_note,
26228 NOTE_DURING_CALL_P (loc_note)
26229 ? last_postcall_label : last_label);
26230 if (newloc == NULL)
26231 return;
26233 else
26235 decl = NULL_TREE;
26236 newloc = NULL;
26239 /* If there were no real insns between note we processed last time
26240 and this note, use the label we emitted last time. Otherwise
26241 create a new label and emit it. */
26242 if (last_label == NULL)
26244 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
26245 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
26246 loclabel_num++;
26247 last_label = ggc_strdup (loclabel);
26248 /* See if loclabel might be equal to .Ltext0. If yes,
26249 bump first_loclabel_num_not_at_text_label. */
26250 if (!have_multiple_function_sections
26251 && in_first_function_p
26252 && maybe_at_text_label_p)
26254 static rtx_insn *last_start;
26255 rtx_insn *insn;
26256 for (insn = loc_note; insn; insn = previous_insn (insn))
26257 if (insn == last_start)
26258 break;
26259 else if (!NONDEBUG_INSN_P (insn))
26260 continue;
26261 else
26263 rtx body = PATTERN (insn);
26264 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
26265 continue;
26266 /* Inline asm could occupy zero bytes. */
26267 else if (GET_CODE (body) == ASM_INPUT
26268 || asm_noperands (body) >= 0)
26269 continue;
26270 #ifdef HAVE_attr_length
26271 else if (get_attr_min_length (insn) == 0)
26272 continue;
26273 #endif
26274 else
26276 /* Assume insn has non-zero length. */
26277 maybe_at_text_label_p = false;
26278 break;
26281 if (maybe_at_text_label_p)
26283 last_start = loc_note;
26284 first_loclabel_num_not_at_text_label = loclabel_num;
26289 gcc_assert ((loc_note == NULL_RTX && call_insn != NULL_RTX)
26290 || (loc_note != NULL_RTX && call_insn == NULL_RTX));
26292 if (!var_loc_p)
26294 struct call_arg_loc_node *ca_loc
26295 = ggc_cleared_alloc<call_arg_loc_node> ();
26296 rtx_insn *prev
26297 = loc_note != NULL_RTX ? prev_real_insn (loc_note) : call_insn;
26299 ca_loc->call_arg_loc_note = loc_note;
26300 ca_loc->next = NULL;
26301 ca_loc->label = last_label;
26302 gcc_assert (prev
26303 && (CALL_P (prev)
26304 || (NONJUMP_INSN_P (prev)
26305 && GET_CODE (PATTERN (prev)) == SEQUENCE
26306 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
26307 if (!CALL_P (prev))
26308 prev = as_a <rtx_sequence *> (PATTERN (prev))->insn (0);
26309 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
26311 /* Look for a SYMBOL_REF in the "prev" instruction. */
26312 rtx x = get_call_rtx_from (PATTERN (prev));
26313 if (x)
26315 /* Try to get the call symbol, if any. */
26316 if (MEM_P (XEXP (x, 0)))
26317 x = XEXP (x, 0);
26318 /* First, look for a memory access to a symbol_ref. */
26319 if (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
26320 && SYMBOL_REF_DECL (XEXP (x, 0))
26321 && TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0))) == FUNCTION_DECL)
26322 ca_loc->symbol_ref = XEXP (x, 0);
26323 /* Otherwise, look at a compile-time known user-level function
26324 declaration. */
26325 else if (MEM_P (x)
26326 && MEM_EXPR (x)
26327 && TREE_CODE (MEM_EXPR (x)) == FUNCTION_DECL)
26328 ca_loc->symbol_ref = XEXP (DECL_RTL (MEM_EXPR (x)), 0);
26331 ca_loc->block = insn_scope (prev);
26332 if (call_arg_locations)
26333 call_arg_loc_last->next = ca_loc;
26334 else
26335 call_arg_locations = ca_loc;
26336 call_arg_loc_last = ca_loc;
26338 else if (loc_note != NULL_RTX && !NOTE_DURING_CALL_P (loc_note))
26339 newloc->label = last_label;
26340 else
26342 if (!last_postcall_label)
26344 sprintf (loclabel, "%s-1", last_label);
26345 last_postcall_label = ggc_strdup (loclabel);
26347 newloc->label = last_postcall_label;
26350 last_var_location_insn = next_real;
26351 last_in_cold_section_p = in_cold_section_p;
26354 /* Called from finalize_size_functions for size functions so that their body
26355 can be encoded in the debug info to describe the layout of variable-length
26356 structures. */
26358 static void
26359 dwarf2out_size_function (tree decl)
26361 function_to_dwarf_procedure (decl);
26364 /* Note in one location list that text section has changed. */
26367 var_location_switch_text_section_1 (var_loc_list **slot, void *)
26369 var_loc_list *list = *slot;
26370 if (list->first)
26371 list->last_before_switch
26372 = list->last->next ? list->last->next : list->last;
26373 return 1;
26376 /* Note in all location lists that text section has changed. */
26378 static void
26379 var_location_switch_text_section (void)
26381 if (decl_loc_table == NULL)
26382 return;
26384 decl_loc_table->traverse<void *, var_location_switch_text_section_1> (NULL);
26387 /* Create a new line number table. */
26389 static dw_line_info_table *
26390 new_line_info_table (void)
26392 dw_line_info_table *table;
26394 table = ggc_cleared_alloc<dw_line_info_table> ();
26395 table->file_num = 1;
26396 table->line_num = 1;
26397 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
26399 return table;
26402 /* Lookup the "current" table into which we emit line info, so
26403 that we don't have to do it for every source line. */
26405 static void
26406 set_cur_line_info_table (section *sec)
26408 dw_line_info_table *table;
26410 if (sec == text_section)
26411 table = text_section_line_info;
26412 else if (sec == cold_text_section)
26414 table = cold_text_section_line_info;
26415 if (!table)
26417 cold_text_section_line_info = table = new_line_info_table ();
26418 table->end_label = cold_end_label;
26421 else
26423 const char *end_label;
26425 if (crtl->has_bb_partition)
26427 if (in_cold_section_p)
26428 end_label = crtl->subsections.cold_section_end_label;
26429 else
26430 end_label = crtl->subsections.hot_section_end_label;
26432 else
26434 char label[MAX_ARTIFICIAL_LABEL_BYTES];
26435 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
26436 current_function_funcdef_no);
26437 end_label = ggc_strdup (label);
26440 table = new_line_info_table ();
26441 table->end_label = end_label;
26443 vec_safe_push (separate_line_info, table);
26446 if (DWARF2_ASM_LINE_DEBUG_INFO)
26447 table->is_stmt = (cur_line_info_table
26448 ? cur_line_info_table->is_stmt
26449 : DWARF_LINE_DEFAULT_IS_STMT_START);
26450 cur_line_info_table = table;
26454 /* We need to reset the locations at the beginning of each
26455 function. We can't do this in the end_function hook, because the
26456 declarations that use the locations won't have been output when
26457 that hook is called. Also compute have_multiple_function_sections here. */
26459 static void
26460 dwarf2out_begin_function (tree fun)
26462 section *sec = function_section (fun);
26464 if (sec != text_section)
26465 have_multiple_function_sections = true;
26467 if (crtl->has_bb_partition && !cold_text_section)
26469 gcc_assert (current_function_decl == fun);
26470 cold_text_section = unlikely_text_section ();
26471 switch_to_section (cold_text_section);
26472 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
26473 switch_to_section (sec);
26476 dwarf2out_note_section_used ();
26477 call_site_count = 0;
26478 tail_call_site_count = 0;
26480 set_cur_line_info_table (sec);
26483 /* Helper function of dwarf2out_end_function, called only after emitting
26484 the very first function into assembly. Check if some .debug_loc range
26485 might end with a .LVL* label that could be equal to .Ltext0.
26486 In that case we must force using absolute addresses in .debug_loc ranges,
26487 because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
26488 .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
26489 list terminator.
26490 Set have_multiple_function_sections to true in that case and
26491 terminate htab traversal. */
26494 find_empty_loc_ranges_at_text_label (var_loc_list **slot, int)
26496 var_loc_list *entry = *slot;
26497 struct var_loc_node *node;
26499 node = entry->first;
26500 if (node && node->next && node->next->label)
26502 unsigned int i;
26503 const char *label = node->next->label;
26504 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
26506 for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
26508 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
26509 if (strcmp (label, loclabel) == 0)
26511 have_multiple_function_sections = true;
26512 return 0;
26516 return 1;
26519 /* Hook called after emitting a function into assembly.
26520 This does something only for the very first function emitted. */
26522 static void
26523 dwarf2out_end_function (unsigned int)
26525 if (in_first_function_p
26526 && !have_multiple_function_sections
26527 && first_loclabel_num_not_at_text_label
26528 && decl_loc_table)
26529 decl_loc_table->traverse<int, find_empty_loc_ranges_at_text_label> (0);
26530 in_first_function_p = false;
26531 maybe_at_text_label_p = false;
26534 /* Temporary holder for dwarf2out_register_main_translation_unit. Used to let
26535 front-ends register a translation unit even before dwarf2out_init is
26536 called. */
26537 static tree main_translation_unit = NULL_TREE;
26539 /* Hook called by front-ends after they built their main translation unit.
26540 Associate comp_unit_die to UNIT. */
26542 static void
26543 dwarf2out_register_main_translation_unit (tree unit)
26545 gcc_assert (TREE_CODE (unit) == TRANSLATION_UNIT_DECL
26546 && main_translation_unit == NULL_TREE);
26547 main_translation_unit = unit;
26548 /* If dwarf2out_init has not been called yet, it will perform the association
26549 itself looking at main_translation_unit. */
26550 if (decl_die_table != NULL)
26551 equate_decl_number_to_die (unit, comp_unit_die ());
26554 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
26556 static void
26557 push_dw_line_info_entry (dw_line_info_table *table,
26558 enum dw_line_info_opcode opcode, unsigned int val)
26560 dw_line_info_entry e;
26561 e.opcode = opcode;
26562 e.val = val;
26563 vec_safe_push (table->entries, e);
26566 /* Output a label to mark the beginning of a source code line entry
26567 and record information relating to this source line, in
26568 'line_info_table' for later output of the .debug_line section. */
26569 /* ??? The discriminator parameter ought to be unsigned. */
26571 static void
26572 dwarf2out_source_line (unsigned int line, unsigned int column,
26573 const char *filename,
26574 int discriminator, bool is_stmt)
26576 unsigned int file_num;
26577 dw_line_info_table *table;
26579 if (debug_info_level < DINFO_LEVEL_TERSE || line == 0)
26580 return;
26582 /* The discriminator column was added in dwarf4. Simplify the below
26583 by simply removing it if we're not supposed to output it. */
26584 if (dwarf_version < 4 && dwarf_strict)
26585 discriminator = 0;
26587 if (!debug_column_info)
26588 column = 0;
26590 table = cur_line_info_table;
26591 file_num = maybe_emit_file (lookup_filename (filename));
26593 /* ??? TODO: Elide duplicate line number entries. Traditionally,
26594 the debugger has used the second (possibly duplicate) line number
26595 at the beginning of the function to mark the end of the prologue.
26596 We could eliminate any other duplicates within the function. For
26597 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
26598 that second line number entry. */
26599 /* Recall that this end-of-prologue indication is *not* the same thing
26600 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
26601 to which the hook corresponds, follows the last insn that was
26602 emitted by gen_prologue. What we need is to precede the first insn
26603 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
26604 insn that corresponds to something the user wrote. These may be
26605 very different locations once scheduling is enabled. */
26607 if (0 && file_num == table->file_num
26608 && line == table->line_num
26609 && column == table->column_num
26610 && discriminator == table->discrim_num
26611 && is_stmt == table->is_stmt)
26612 return;
26614 switch_to_section (current_function_section ());
26616 /* If requested, emit something human-readable. */
26617 if (flag_debug_asm)
26619 if (debug_column_info)
26620 fprintf (asm_out_file, "\t%s %s:%d:%d\n", ASM_COMMENT_START,
26621 filename, line, column);
26622 else
26623 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
26624 filename, line);
26627 if (DWARF2_ASM_LINE_DEBUG_INFO)
26629 /* Emit the .loc directive understood by GNU as. */
26630 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
26631 file_num, line, is_stmt, discriminator */
26632 fputs ("\t.loc ", asm_out_file);
26633 fprint_ul (asm_out_file, file_num);
26634 putc (' ', asm_out_file);
26635 fprint_ul (asm_out_file, line);
26636 putc (' ', asm_out_file);
26637 if (debug_column_info)
26638 fprint_ul (asm_out_file, column);
26639 else
26640 putc ('0', asm_out_file);
26642 if (is_stmt != table->is_stmt)
26644 fputs (" is_stmt ", asm_out_file);
26645 putc (is_stmt ? '1' : '0', asm_out_file);
26647 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
26649 gcc_assert (discriminator > 0);
26650 fputs (" discriminator ", asm_out_file);
26651 fprint_ul (asm_out_file, (unsigned long) discriminator);
26653 putc ('\n', asm_out_file);
26655 else
26657 unsigned int label_num = ++line_info_label_num;
26659 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
26661 push_dw_line_info_entry (table, LI_set_address, label_num);
26662 if (file_num != table->file_num)
26663 push_dw_line_info_entry (table, LI_set_file, file_num);
26664 if (discriminator != table->discrim_num)
26665 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
26666 if (is_stmt != table->is_stmt)
26667 push_dw_line_info_entry (table, LI_negate_stmt, 0);
26668 push_dw_line_info_entry (table, LI_set_line, line);
26669 if (debug_column_info)
26670 push_dw_line_info_entry (table, LI_set_column, column);
26673 table->file_num = file_num;
26674 table->line_num = line;
26675 table->column_num = column;
26676 table->discrim_num = discriminator;
26677 table->is_stmt = is_stmt;
26678 table->in_use = true;
26681 /* Record the beginning of a new source file. */
26683 static void
26684 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
26686 if (flag_eliminate_dwarf2_dups)
26688 /* Record the beginning of the file for break_out_includes. */
26689 dw_die_ref bincl_die;
26691 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
26692 add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
26695 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
26697 macinfo_entry e;
26698 e.code = DW_MACINFO_start_file;
26699 e.lineno = lineno;
26700 e.info = ggc_strdup (filename);
26701 vec_safe_push (macinfo_table, e);
26705 /* Record the end of a source file. */
26707 static void
26708 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
26710 if (flag_eliminate_dwarf2_dups)
26711 /* Record the end of the file for break_out_includes. */
26712 new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
26714 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
26716 macinfo_entry e;
26717 e.code = DW_MACINFO_end_file;
26718 e.lineno = lineno;
26719 e.info = NULL;
26720 vec_safe_push (macinfo_table, e);
26724 /* Called from debug_define in toplev.c. The `buffer' parameter contains
26725 the tail part of the directive line, i.e. the part which is past the
26726 initial whitespace, #, whitespace, directive-name, whitespace part. */
26728 static void
26729 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
26730 const char *buffer ATTRIBUTE_UNUSED)
26732 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
26734 macinfo_entry e;
26735 /* Insert a dummy first entry to be able to optimize the whole
26736 predefined macro block using DW_MACRO_import. */
26737 if (macinfo_table->is_empty () && lineno <= 1)
26739 e.code = 0;
26740 e.lineno = 0;
26741 e.info = NULL;
26742 vec_safe_push (macinfo_table, e);
26744 e.code = DW_MACINFO_define;
26745 e.lineno = lineno;
26746 e.info = ggc_strdup (buffer);
26747 vec_safe_push (macinfo_table, e);
26751 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
26752 the tail part of the directive line, i.e. the part which is past the
26753 initial whitespace, #, whitespace, directive-name, whitespace part. */
26755 static void
26756 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
26757 const char *buffer ATTRIBUTE_UNUSED)
26759 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
26761 macinfo_entry e;
26762 /* Insert a dummy first entry to be able to optimize the whole
26763 predefined macro block using DW_MACRO_import. */
26764 if (macinfo_table->is_empty () && lineno <= 1)
26766 e.code = 0;
26767 e.lineno = 0;
26768 e.info = NULL;
26769 vec_safe_push (macinfo_table, e);
26771 e.code = DW_MACINFO_undef;
26772 e.lineno = lineno;
26773 e.info = ggc_strdup (buffer);
26774 vec_safe_push (macinfo_table, e);
26778 /* Helpers to manipulate hash table of CUs. */
26780 struct macinfo_entry_hasher : nofree_ptr_hash <macinfo_entry>
26782 static inline hashval_t hash (const macinfo_entry *);
26783 static inline bool equal (const macinfo_entry *, const macinfo_entry *);
26786 inline hashval_t
26787 macinfo_entry_hasher::hash (const macinfo_entry *entry)
26789 return htab_hash_string (entry->info);
26792 inline bool
26793 macinfo_entry_hasher::equal (const macinfo_entry *entry1,
26794 const macinfo_entry *entry2)
26796 return !strcmp (entry1->info, entry2->info);
26799 typedef hash_table<macinfo_entry_hasher> macinfo_hash_type;
26801 /* Output a single .debug_macinfo entry. */
26803 static void
26804 output_macinfo_op (macinfo_entry *ref)
26806 int file_num;
26807 size_t len;
26808 struct indirect_string_node *node;
26809 char label[MAX_ARTIFICIAL_LABEL_BYTES];
26810 struct dwarf_file_data *fd;
26812 switch (ref->code)
26814 case DW_MACINFO_start_file:
26815 fd = lookup_filename (ref->info);
26816 file_num = maybe_emit_file (fd);
26817 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
26818 dw2_asm_output_data_uleb128 (ref->lineno,
26819 "Included from line number %lu",
26820 (unsigned long) ref->lineno);
26821 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
26822 break;
26823 case DW_MACINFO_end_file:
26824 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
26825 break;
26826 case DW_MACINFO_define:
26827 case DW_MACINFO_undef:
26828 len = strlen (ref->info) + 1;
26829 if (!dwarf_strict
26830 && len > DWARF_OFFSET_SIZE
26831 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
26832 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
26834 ref->code = ref->code == DW_MACINFO_define
26835 ? DW_MACRO_define_strp : DW_MACRO_undef_strp;
26836 output_macinfo_op (ref);
26837 return;
26839 dw2_asm_output_data (1, ref->code,
26840 ref->code == DW_MACINFO_define
26841 ? "Define macro" : "Undefine macro");
26842 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
26843 (unsigned long) ref->lineno);
26844 dw2_asm_output_nstring (ref->info, -1, "The macro");
26845 break;
26846 case DW_MACRO_define_strp:
26847 case DW_MACRO_undef_strp:
26848 node = find_AT_string (ref->info);
26849 gcc_assert (node
26850 && (node->form == DW_FORM_strp
26851 || node->form == DW_FORM_GNU_str_index));
26852 dw2_asm_output_data (1, ref->code,
26853 ref->code == DW_MACRO_define_strp
26854 ? "Define macro strp"
26855 : "Undefine macro strp");
26856 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
26857 (unsigned long) ref->lineno);
26858 if (node->form == DW_FORM_strp)
26859 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
26860 debug_str_section, "The macro: \"%s\"",
26861 ref->info);
26862 else
26863 dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
26864 ref->info);
26865 break;
26866 case DW_MACRO_import:
26867 dw2_asm_output_data (1, ref->code, "Import");
26868 ASM_GENERATE_INTERNAL_LABEL (label,
26869 DEBUG_MACRO_SECTION_LABEL, ref->lineno);
26870 dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
26871 break;
26872 default:
26873 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
26874 ASM_COMMENT_START, (unsigned long) ref->code);
26875 break;
26879 /* Attempt to make a sequence of define/undef macinfo ops shareable with
26880 other compilation unit .debug_macinfo sections. IDX is the first
26881 index of a define/undef, return the number of ops that should be
26882 emitted in a comdat .debug_macinfo section and emit
26883 a DW_MACRO_import entry referencing it.
26884 If the define/undef entry should be emitted normally, return 0. */
26886 static unsigned
26887 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
26888 macinfo_hash_type **macinfo_htab)
26890 macinfo_entry *first, *second, *cur, *inc;
26891 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
26892 unsigned char checksum[16];
26893 struct md5_ctx ctx;
26894 char *grp_name, *tail;
26895 const char *base;
26896 unsigned int i, count, encoded_filename_len, linebuf_len;
26897 macinfo_entry **slot;
26899 first = &(*macinfo_table)[idx];
26900 second = &(*macinfo_table)[idx + 1];
26902 /* Optimize only if there are at least two consecutive define/undef ops,
26903 and either all of them are before first DW_MACINFO_start_file
26904 with lineno {0,1} (i.e. predefined macro block), or all of them are
26905 in some included header file. */
26906 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
26907 return 0;
26908 if (vec_safe_is_empty (files))
26910 if (first->lineno > 1 || second->lineno > 1)
26911 return 0;
26913 else if (first->lineno == 0)
26914 return 0;
26916 /* Find the last define/undef entry that can be grouped together
26917 with first and at the same time compute md5 checksum of their
26918 codes, linenumbers and strings. */
26919 md5_init_ctx (&ctx);
26920 for (i = idx; macinfo_table->iterate (i, &cur); i++)
26921 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
26922 break;
26923 else if (vec_safe_is_empty (files) && cur->lineno > 1)
26924 break;
26925 else
26927 unsigned char code = cur->code;
26928 md5_process_bytes (&code, 1, &ctx);
26929 checksum_uleb128 (cur->lineno, &ctx);
26930 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
26932 md5_finish_ctx (&ctx, checksum);
26933 count = i - idx;
26935 /* From the containing include filename (if any) pick up just
26936 usable characters from its basename. */
26937 if (vec_safe_is_empty (files))
26938 base = "";
26939 else
26940 base = lbasename (files->last ().info);
26941 for (encoded_filename_len = 0, i = 0; base[i]; i++)
26942 if (ISIDNUM (base[i]) || base[i] == '.')
26943 encoded_filename_len++;
26944 /* Count . at the end. */
26945 if (encoded_filename_len)
26946 encoded_filename_len++;
26948 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
26949 linebuf_len = strlen (linebuf);
26951 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
26952 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
26953 + 16 * 2 + 1);
26954 memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
26955 tail = grp_name + 4;
26956 if (encoded_filename_len)
26958 for (i = 0; base[i]; i++)
26959 if (ISIDNUM (base[i]) || base[i] == '.')
26960 *tail++ = base[i];
26961 *tail++ = '.';
26963 memcpy (tail, linebuf, linebuf_len);
26964 tail += linebuf_len;
26965 *tail++ = '.';
26966 for (i = 0; i < 16; i++)
26967 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
26969 /* Construct a macinfo_entry for DW_MACRO_import
26970 in the empty vector entry before the first define/undef. */
26971 inc = &(*macinfo_table)[idx - 1];
26972 inc->code = DW_MACRO_import;
26973 inc->lineno = 0;
26974 inc->info = ggc_strdup (grp_name);
26975 if (!*macinfo_htab)
26976 *macinfo_htab = new macinfo_hash_type (10);
26977 /* Avoid emitting duplicates. */
26978 slot = (*macinfo_htab)->find_slot (inc, INSERT);
26979 if (*slot != NULL)
26981 inc->code = 0;
26982 inc->info = NULL;
26983 /* If such an entry has been used before, just emit
26984 a DW_MACRO_import op. */
26985 inc = *slot;
26986 output_macinfo_op (inc);
26987 /* And clear all macinfo_entry in the range to avoid emitting them
26988 in the second pass. */
26989 for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
26991 cur->code = 0;
26992 cur->info = NULL;
26995 else
26997 *slot = inc;
26998 inc->lineno = (*macinfo_htab)->elements ();
26999 output_macinfo_op (inc);
27001 return count;
27004 /* Save any strings needed by the macinfo table in the debug str
27005 table. All strings must be collected into the table by the time
27006 index_string is called. */
27008 static void
27009 save_macinfo_strings (void)
27011 unsigned len;
27012 unsigned i;
27013 macinfo_entry *ref;
27015 for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
27017 switch (ref->code)
27019 /* Match the logic in output_macinfo_op to decide on
27020 indirect strings. */
27021 case DW_MACINFO_define:
27022 case DW_MACINFO_undef:
27023 len = strlen (ref->info) + 1;
27024 if (!dwarf_strict
27025 && len > DWARF_OFFSET_SIZE
27026 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
27027 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
27028 set_indirect_string (find_AT_string (ref->info));
27029 break;
27030 case DW_MACRO_define_strp:
27031 case DW_MACRO_undef_strp:
27032 set_indirect_string (find_AT_string (ref->info));
27033 break;
27034 default:
27035 break;
27040 /* Output macinfo section(s). */
27042 static void
27043 output_macinfo (void)
27045 unsigned i;
27046 unsigned long length = vec_safe_length (macinfo_table);
27047 macinfo_entry *ref;
27048 vec<macinfo_entry, va_gc> *files = NULL;
27049 macinfo_hash_type *macinfo_htab = NULL;
27051 if (! length)
27052 return;
27054 /* output_macinfo* uses these interchangeably. */
27055 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_define
27056 && (int) DW_MACINFO_undef == (int) DW_MACRO_undef
27057 && (int) DW_MACINFO_start_file == (int) DW_MACRO_start_file
27058 && (int) DW_MACINFO_end_file == (int) DW_MACRO_end_file);
27060 /* For .debug_macro emit the section header. */
27061 if (!dwarf_strict || dwarf_version >= 5)
27063 dw2_asm_output_data (2, dwarf_version >= 5 ? 5 : 4,
27064 "DWARF macro version number");
27065 if (DWARF_OFFSET_SIZE == 8)
27066 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
27067 else
27068 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
27069 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
27070 (!dwarf_split_debug_info ? debug_line_section_label
27071 : debug_skeleton_line_section_label),
27072 debug_line_section, NULL);
27075 /* In the first loop, it emits the primary .debug_macinfo section
27076 and after each emitted op the macinfo_entry is cleared.
27077 If a longer range of define/undef ops can be optimized using
27078 DW_MACRO_import, the DW_MACRO_import op is emitted and kept in
27079 the vector before the first define/undef in the range and the
27080 whole range of define/undef ops is not emitted and kept. */
27081 for (i = 0; macinfo_table->iterate (i, &ref); i++)
27083 switch (ref->code)
27085 case DW_MACINFO_start_file:
27086 vec_safe_push (files, *ref);
27087 break;
27088 case DW_MACINFO_end_file:
27089 if (!vec_safe_is_empty (files))
27090 files->pop ();
27091 break;
27092 case DW_MACINFO_define:
27093 case DW_MACINFO_undef:
27094 if ((!dwarf_strict || dwarf_version >= 5)
27095 && HAVE_COMDAT_GROUP
27096 && vec_safe_length (files) != 1
27097 && i > 0
27098 && i + 1 < length
27099 && (*macinfo_table)[i - 1].code == 0)
27101 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
27102 if (count)
27104 i += count - 1;
27105 continue;
27108 break;
27109 case 0:
27110 /* A dummy entry may be inserted at the beginning to be able
27111 to optimize the whole block of predefined macros. */
27112 if (i == 0)
27113 continue;
27114 default:
27115 break;
27117 output_macinfo_op (ref);
27118 ref->info = NULL;
27119 ref->code = 0;
27122 if (!macinfo_htab)
27123 return;
27125 delete macinfo_htab;
27126 macinfo_htab = NULL;
27128 /* If any DW_MACRO_import were used, on those DW_MACRO_import entries
27129 terminate the current chain and switch to a new comdat .debug_macinfo
27130 section and emit the define/undef entries within it. */
27131 for (i = 0; macinfo_table->iterate (i, &ref); i++)
27132 switch (ref->code)
27134 case 0:
27135 continue;
27136 case DW_MACRO_import:
27138 char label[MAX_ARTIFICIAL_LABEL_BYTES];
27139 tree comdat_key = get_identifier (ref->info);
27140 /* Terminate the previous .debug_macinfo section. */
27141 dw2_asm_output_data (1, 0, "End compilation unit");
27142 targetm.asm_out.named_section (debug_macinfo_section_name,
27143 SECTION_DEBUG
27144 | SECTION_LINKONCE,
27145 comdat_key);
27146 ASM_GENERATE_INTERNAL_LABEL (label,
27147 DEBUG_MACRO_SECTION_LABEL,
27148 ref->lineno);
27149 ASM_OUTPUT_LABEL (asm_out_file, label);
27150 ref->code = 0;
27151 ref->info = NULL;
27152 dw2_asm_output_data (2, dwarf_version >= 5 ? 5 : 4,
27153 "DWARF macro version number");
27154 if (DWARF_OFFSET_SIZE == 8)
27155 dw2_asm_output_data (1, 1, "Flags: 64-bit");
27156 else
27157 dw2_asm_output_data (1, 0, "Flags: 32-bit");
27159 break;
27160 case DW_MACINFO_define:
27161 case DW_MACINFO_undef:
27162 output_macinfo_op (ref);
27163 ref->code = 0;
27164 ref->info = NULL;
27165 break;
27166 default:
27167 gcc_unreachable ();
27171 /* Initialize the various sections and labels for dwarf output. */
27173 static void
27174 init_sections_and_labels (void)
27176 if (!dwarf_split_debug_info)
27178 debug_info_section = get_section (DEBUG_INFO_SECTION,
27179 SECTION_DEBUG, NULL);
27180 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
27181 SECTION_DEBUG, NULL);
27182 debug_loc_section = get_section (dwarf_version >= 5
27183 ? DEBUG_LOCLISTS_SECTION
27184 : DEBUG_LOC_SECTION,
27185 SECTION_DEBUG, NULL);
27186 debug_macinfo_section_name
27187 = (dwarf_strict && dwarf_version < 5)
27188 ? DEBUG_MACINFO_SECTION : DEBUG_MACRO_SECTION;
27189 debug_macinfo_section = get_section (debug_macinfo_section_name,
27190 SECTION_DEBUG, NULL);
27192 else
27194 debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
27195 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
27196 debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
27197 SECTION_DEBUG | SECTION_EXCLUDE,
27198 NULL);
27199 debug_addr_section = get_section (DEBUG_ADDR_SECTION,
27200 SECTION_DEBUG, NULL);
27201 debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
27202 SECTION_DEBUG, NULL);
27203 debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
27204 SECTION_DEBUG, NULL);
27205 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
27206 DEBUG_SKELETON_ABBREV_SECTION_LABEL, 0);
27208 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections stay in
27209 the main .o, but the skeleton_line goes into the split off dwo. */
27210 debug_skeleton_line_section
27211 = get_section (DEBUG_DWO_LINE_SECTION,
27212 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
27213 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
27214 DEBUG_SKELETON_LINE_SECTION_LABEL, 0);
27215 debug_str_offsets_section = get_section (DEBUG_DWO_STR_OFFSETS_SECTION,
27216 SECTION_DEBUG | SECTION_EXCLUDE,
27217 NULL);
27218 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
27219 DEBUG_SKELETON_INFO_SECTION_LABEL, 0);
27220 debug_loc_section = get_section (dwarf_version >= 5
27221 ? DEBUG_DWO_LOCLISTS_SECTION
27222 : DEBUG_DWO_LOC_SECTION,
27223 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
27224 debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
27225 DEBUG_STR_DWO_SECTION_FLAGS, NULL);
27226 debug_macinfo_section_name
27227 = (dwarf_strict && dwarf_version < 5)
27228 ? DEBUG_DWO_MACINFO_SECTION : DEBUG_DWO_MACRO_SECTION;
27229 debug_macinfo_section = get_section (debug_macinfo_section_name,
27230 SECTION_DEBUG | SECTION_EXCLUDE,
27231 NULL);
27233 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
27234 SECTION_DEBUG, NULL);
27235 debug_line_section = get_section (DEBUG_LINE_SECTION,
27236 SECTION_DEBUG, NULL);
27237 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
27238 SECTION_DEBUG, NULL);
27239 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
27240 SECTION_DEBUG, NULL);
27241 debug_str_section = get_section (DEBUG_STR_SECTION,
27242 DEBUG_STR_SECTION_FLAGS, NULL);
27243 if (!dwarf_split_debug_info && !DWARF2_ASM_LINE_DEBUG_INFO)
27244 debug_line_str_section = get_section (DEBUG_LINE_STR_SECTION,
27245 DEBUG_STR_SECTION_FLAGS, NULL);
27247 debug_ranges_section = get_section (dwarf_version >= 5
27248 ? DEBUG_RNGLISTS_SECTION
27249 : DEBUG_RANGES_SECTION,
27250 SECTION_DEBUG, NULL);
27251 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
27252 SECTION_DEBUG, NULL);
27254 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
27255 DEBUG_ABBREV_SECTION_LABEL, 0);
27256 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
27257 DEBUG_INFO_SECTION_LABEL, 0);
27258 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
27259 DEBUG_LINE_SECTION_LABEL, 0);
27260 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
27261 DEBUG_RANGES_SECTION_LABEL, 0);
27262 if (dwarf_version >= 5 && dwarf_split_debug_info)
27263 ASM_GENERATE_INTERNAL_LABEL (ranges_base_label,
27264 DEBUG_RANGES_SECTION_LABEL, 1);
27265 ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
27266 DEBUG_ADDR_SECTION_LABEL, 0);
27267 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
27268 (dwarf_strict && dwarf_version < 5)
27269 ? DEBUG_MACINFO_SECTION_LABEL
27270 : DEBUG_MACRO_SECTION_LABEL, 0);
27271 ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL, 0);
27274 /* Set up for Dwarf output at the start of compilation. */
27276 static void
27277 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
27279 /* This option is currently broken, see (PR53118 and PR46102). */
27280 if (flag_eliminate_dwarf2_dups
27281 && strstr (lang_hooks.name, "C++"))
27283 warning (0, "-feliminate-dwarf2-dups is broken for C++, ignoring");
27284 flag_eliminate_dwarf2_dups = 0;
27287 /* Allocate the file_table. */
27288 file_table = hash_table<dwarf_file_hasher>::create_ggc (50);
27290 #ifndef DWARF2_LINENO_DEBUGGING_INFO
27291 /* Allocate the decl_die_table. */
27292 decl_die_table = hash_table<decl_die_hasher>::create_ggc (10);
27294 /* Allocate the decl_loc_table. */
27295 decl_loc_table = hash_table<decl_loc_hasher>::create_ggc (10);
27297 /* Allocate the cached_dw_loc_list_table. */
27298 cached_dw_loc_list_table = hash_table<dw_loc_list_hasher>::create_ggc (10);
27300 /* Allocate the initial hunk of the decl_scope_table. */
27301 vec_alloc (decl_scope_table, 256);
27303 /* Allocate the initial hunk of the abbrev_die_table. */
27304 vec_alloc (abbrev_die_table, 256);
27305 /* Zero-th entry is allocated, but unused. */
27306 abbrev_die_table->quick_push (NULL);
27308 /* Allocate the dwarf_proc_stack_usage_map. */
27309 dwarf_proc_stack_usage_map = new hash_map<dw_die_ref, int>;
27311 /* Allocate the pubtypes and pubnames vectors. */
27312 vec_alloc (pubname_table, 32);
27313 vec_alloc (pubtype_table, 32);
27315 vec_alloc (incomplete_types, 64);
27317 vec_alloc (used_rtx_array, 32);
27319 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
27320 vec_alloc (macinfo_table, 64);
27321 #endif
27323 /* If front-ends already registered a main translation unit but we were not
27324 ready to perform the association, do this now. */
27325 if (main_translation_unit != NULL_TREE)
27326 equate_decl_number_to_die (main_translation_unit, comp_unit_die ());
27329 /* Called before compile () starts outputtting functions, variables
27330 and toplevel asms into assembly. */
27332 static void
27333 dwarf2out_assembly_start (void)
27335 #ifndef DWARF2_LINENO_DEBUGGING_INFO
27336 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
27337 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
27338 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
27339 COLD_TEXT_SECTION_LABEL, 0);
27340 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
27342 switch_to_section (text_section);
27343 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
27344 #endif
27346 /* Make sure the line number table for .text always exists. */
27347 text_section_line_info = new_line_info_table ();
27348 text_section_line_info->end_label = text_end_label;
27350 #ifdef DWARF2_LINENO_DEBUGGING_INFO
27351 cur_line_info_table = text_section_line_info;
27352 #endif
27354 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
27355 && dwarf2out_do_cfi_asm ()
27356 && (!(flag_unwind_tables || flag_exceptions)
27357 || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
27358 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
27361 /* A helper function for dwarf2out_finish called through
27362 htab_traverse. Assign a string its index. All strings must be
27363 collected into the table by the time index_string is called,
27364 because the indexing code relies on htab_traverse to traverse nodes
27365 in the same order for each run. */
27368 index_string (indirect_string_node **h, unsigned int *index)
27370 indirect_string_node *node = *h;
27372 find_string_form (node);
27373 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
27375 gcc_assert (node->index == NO_INDEX_ASSIGNED);
27376 node->index = *index;
27377 *index += 1;
27379 return 1;
27382 /* A helper function for output_indirect_strings called through
27383 htab_traverse. Output the offset to a string and update the
27384 current offset. */
27387 output_index_string_offset (indirect_string_node **h, unsigned int *offset)
27389 indirect_string_node *node = *h;
27391 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
27393 /* Assert that this node has been assigned an index. */
27394 gcc_assert (node->index != NO_INDEX_ASSIGNED
27395 && node->index != NOT_INDEXED);
27396 dw2_asm_output_data (DWARF_OFFSET_SIZE, *offset,
27397 "indexed string 0x%x: %s", node->index, node->str);
27398 *offset += strlen (node->str) + 1;
27400 return 1;
27403 /* A helper function for dwarf2out_finish called through
27404 htab_traverse. Output the indexed string. */
27407 output_index_string (indirect_string_node **h, unsigned int *cur_idx)
27409 struct indirect_string_node *node = *h;
27411 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
27413 /* Assert that the strings are output in the same order as their
27414 indexes were assigned. */
27415 gcc_assert (*cur_idx == node->index);
27416 assemble_string (node->str, strlen (node->str) + 1);
27417 *cur_idx += 1;
27419 return 1;
27422 /* A helper function for dwarf2out_finish called through
27423 htab_traverse. Emit one queued .debug_str string. */
27426 output_indirect_string (indirect_string_node **h, enum dwarf_form form)
27428 struct indirect_string_node *node = *h;
27430 node->form = find_string_form (node);
27431 if (node->form == form && node->refcount > 0)
27433 ASM_OUTPUT_LABEL (asm_out_file, node->label);
27434 assemble_string (node->str, strlen (node->str) + 1);
27437 return 1;
27440 /* Output the indexed string table. */
27442 static void
27443 output_indirect_strings (void)
27445 switch_to_section (debug_str_section);
27446 if (!dwarf_split_debug_info)
27447 debug_str_hash->traverse<enum dwarf_form,
27448 output_indirect_string> (DW_FORM_strp);
27449 else
27451 unsigned int offset = 0;
27452 unsigned int cur_idx = 0;
27454 skeleton_debug_str_hash->traverse<enum dwarf_form,
27455 output_indirect_string> (DW_FORM_strp);
27457 switch_to_section (debug_str_offsets_section);
27458 debug_str_hash->traverse_noresize
27459 <unsigned int *, output_index_string_offset> (&offset);
27460 switch_to_section (debug_str_dwo_section);
27461 debug_str_hash->traverse_noresize<unsigned int *, output_index_string>
27462 (&cur_idx);
27466 /* Callback for htab_traverse to assign an index to an entry in the
27467 table, and to write that entry to the .debug_addr section. */
27470 output_addr_table_entry (addr_table_entry **slot, unsigned int *cur_index)
27472 addr_table_entry *entry = *slot;
27474 if (entry->refcount == 0)
27476 gcc_assert (entry->index == NO_INDEX_ASSIGNED
27477 || entry->index == NOT_INDEXED);
27478 return 1;
27481 gcc_assert (entry->index == *cur_index);
27482 (*cur_index)++;
27484 switch (entry->kind)
27486 case ate_kind_rtx:
27487 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
27488 "0x%x", entry->index);
27489 break;
27490 case ate_kind_rtx_dtprel:
27491 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
27492 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
27493 DWARF2_ADDR_SIZE,
27494 entry->addr.rtl);
27495 fputc ('\n', asm_out_file);
27496 break;
27497 case ate_kind_label:
27498 dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
27499 "0x%x", entry->index);
27500 break;
27501 default:
27502 gcc_unreachable ();
27504 return 1;
27507 /* Produce the .debug_addr section. */
27509 static void
27510 output_addr_table (void)
27512 unsigned int index = 0;
27513 if (addr_index_table == NULL || addr_index_table->size () == 0)
27514 return;
27516 switch_to_section (debug_addr_section);
27517 addr_index_table
27518 ->traverse_noresize<unsigned int *, output_addr_table_entry> (&index);
27521 #if ENABLE_ASSERT_CHECKING
27522 /* Verify that all marks are clear. */
27524 static void
27525 verify_marks_clear (dw_die_ref die)
27527 dw_die_ref c;
27529 gcc_assert (! die->die_mark);
27530 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
27532 #endif /* ENABLE_ASSERT_CHECKING */
27534 /* Clear the marks for a die and its children.
27535 Be cool if the mark isn't set. */
27537 static void
27538 prune_unmark_dies (dw_die_ref die)
27540 dw_die_ref c;
27542 if (die->die_mark)
27543 die->die_mark = 0;
27544 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
27547 /* Given LOC that is referenced by a DIE we're marking as used, find all
27548 referenced DWARF procedures it references and mark them as used. */
27550 static void
27551 prune_unused_types_walk_loc_descr (dw_loc_descr_ref loc)
27553 for (; loc != NULL; loc = loc->dw_loc_next)
27554 switch (loc->dw_loc_opc)
27556 case DW_OP_implicit_pointer:
27557 case DW_OP_convert:
27558 case DW_OP_reinterpret:
27559 case DW_OP_GNU_implicit_pointer:
27560 case DW_OP_GNU_convert:
27561 case DW_OP_GNU_reinterpret:
27562 if (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref)
27563 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
27564 break;
27565 case DW_OP_GNU_variable_value:
27566 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
27568 dw_die_ref ref
27569 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
27570 if (ref == NULL)
27571 break;
27572 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
27573 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
27574 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
27576 /* FALLTHRU */
27577 case DW_OP_call2:
27578 case DW_OP_call4:
27579 case DW_OP_call_ref:
27580 case DW_OP_const_type:
27581 case DW_OP_GNU_const_type:
27582 case DW_OP_GNU_parameter_ref:
27583 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref);
27584 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
27585 break;
27586 case DW_OP_regval_type:
27587 case DW_OP_deref_type:
27588 case DW_OP_GNU_regval_type:
27589 case DW_OP_GNU_deref_type:
27590 gcc_assert (loc->dw_loc_oprnd2.val_class == dw_val_class_die_ref);
27591 prune_unused_types_mark (loc->dw_loc_oprnd2.v.val_die_ref.die, 1);
27592 break;
27593 case DW_OP_entry_value:
27594 case DW_OP_GNU_entry_value:
27595 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_loc);
27596 prune_unused_types_walk_loc_descr (loc->dw_loc_oprnd1.v.val_loc);
27597 break;
27598 default:
27599 break;
27603 /* Given DIE that we're marking as used, find any other dies
27604 it references as attributes and mark them as used. */
27606 static void
27607 prune_unused_types_walk_attribs (dw_die_ref die)
27609 dw_attr_node *a;
27610 unsigned ix;
27612 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
27614 switch (AT_class (a))
27616 /* Make sure DWARF procedures referenced by location descriptions will
27617 get emitted. */
27618 case dw_val_class_loc:
27619 prune_unused_types_walk_loc_descr (AT_loc (a));
27620 break;
27621 case dw_val_class_loc_list:
27622 for (dw_loc_list_ref list = AT_loc_list (a);
27623 list != NULL;
27624 list = list->dw_loc_next)
27625 prune_unused_types_walk_loc_descr (list->expr);
27626 break;
27628 case dw_val_class_die_ref:
27629 /* A reference to another DIE.
27630 Make sure that it will get emitted.
27631 If it was broken out into a comdat group, don't follow it. */
27632 if (! AT_ref (a)->comdat_type_p
27633 || a->dw_attr == DW_AT_specification)
27634 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
27635 break;
27637 case dw_val_class_str:
27638 /* Set the string's refcount to 0 so that prune_unused_types_mark
27639 accounts properly for it. */
27640 a->dw_attr_val.v.val_str->refcount = 0;
27641 break;
27643 default:
27644 break;
27649 /* Mark the generic parameters and arguments children DIEs of DIE. */
27651 static void
27652 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
27654 dw_die_ref c;
27656 if (die == NULL || die->die_child == NULL)
27657 return;
27658 c = die->die_child;
27661 if (is_template_parameter (c))
27662 prune_unused_types_mark (c, 1);
27663 c = c->die_sib;
27664 } while (c && c != die->die_child);
27667 /* Mark DIE as being used. If DOKIDS is true, then walk down
27668 to DIE's children. */
27670 static void
27671 prune_unused_types_mark (dw_die_ref die, int dokids)
27673 dw_die_ref c;
27675 if (die->die_mark == 0)
27677 /* We haven't done this node yet. Mark it as used. */
27678 die->die_mark = 1;
27679 /* If this is the DIE of a generic type instantiation,
27680 mark the children DIEs that describe its generic parms and
27681 args. */
27682 prune_unused_types_mark_generic_parms_dies (die);
27684 /* We also have to mark its parents as used.
27685 (But we don't want to mark our parent's kids due to this,
27686 unless it is a class.) */
27687 if (die->die_parent)
27688 prune_unused_types_mark (die->die_parent,
27689 class_scope_p (die->die_parent));
27691 /* Mark any referenced nodes. */
27692 prune_unused_types_walk_attribs (die);
27694 /* If this node is a specification,
27695 also mark the definition, if it exists. */
27696 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
27697 prune_unused_types_mark (die->die_definition, 1);
27700 if (dokids && die->die_mark != 2)
27702 /* We need to walk the children, but haven't done so yet.
27703 Remember that we've walked the kids. */
27704 die->die_mark = 2;
27706 /* If this is an array type, we need to make sure our
27707 kids get marked, even if they're types. If we're
27708 breaking out types into comdat sections, do this
27709 for all type definitions. */
27710 if (die->die_tag == DW_TAG_array_type
27711 || (use_debug_types
27712 && is_type_die (die) && ! is_declaration_die (die)))
27713 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
27714 else
27715 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
27719 /* For local classes, look if any static member functions were emitted
27720 and if so, mark them. */
27722 static void
27723 prune_unused_types_walk_local_classes (dw_die_ref die)
27725 dw_die_ref c;
27727 if (die->die_mark == 2)
27728 return;
27730 switch (die->die_tag)
27732 case DW_TAG_structure_type:
27733 case DW_TAG_union_type:
27734 case DW_TAG_class_type:
27735 break;
27737 case DW_TAG_subprogram:
27738 if (!get_AT_flag (die, DW_AT_declaration)
27739 || die->die_definition != NULL)
27740 prune_unused_types_mark (die, 1);
27741 return;
27743 default:
27744 return;
27747 /* Mark children. */
27748 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
27751 /* Walk the tree DIE and mark types that we actually use. */
27753 static void
27754 prune_unused_types_walk (dw_die_ref die)
27756 dw_die_ref c;
27758 /* Don't do anything if this node is already marked and
27759 children have been marked as well. */
27760 if (die->die_mark == 2)
27761 return;
27763 switch (die->die_tag)
27765 case DW_TAG_structure_type:
27766 case DW_TAG_union_type:
27767 case DW_TAG_class_type:
27768 if (die->die_perennial_p)
27769 break;
27771 for (c = die->die_parent; c; c = c->die_parent)
27772 if (c->die_tag == DW_TAG_subprogram)
27773 break;
27775 /* Finding used static member functions inside of classes
27776 is needed just for local classes, because for other classes
27777 static member function DIEs with DW_AT_specification
27778 are emitted outside of the DW_TAG_*_type. If we ever change
27779 it, we'd need to call this even for non-local classes. */
27780 if (c)
27781 prune_unused_types_walk_local_classes (die);
27783 /* It's a type node --- don't mark it. */
27784 return;
27786 case DW_TAG_const_type:
27787 case DW_TAG_packed_type:
27788 case DW_TAG_pointer_type:
27789 case DW_TAG_reference_type:
27790 case DW_TAG_rvalue_reference_type:
27791 case DW_TAG_volatile_type:
27792 case DW_TAG_typedef:
27793 case DW_TAG_array_type:
27794 case DW_TAG_interface_type:
27795 case DW_TAG_friend:
27796 case DW_TAG_enumeration_type:
27797 case DW_TAG_subroutine_type:
27798 case DW_TAG_string_type:
27799 case DW_TAG_set_type:
27800 case DW_TAG_subrange_type:
27801 case DW_TAG_ptr_to_member_type:
27802 case DW_TAG_file_type:
27803 /* Type nodes are useful only when other DIEs reference them --- don't
27804 mark them. */
27805 /* FALLTHROUGH */
27807 case DW_TAG_dwarf_procedure:
27808 /* Likewise for DWARF procedures. */
27810 if (die->die_perennial_p)
27811 break;
27813 return;
27815 default:
27816 /* Mark everything else. */
27817 break;
27820 if (die->die_mark == 0)
27822 die->die_mark = 1;
27824 /* Now, mark any dies referenced from here. */
27825 prune_unused_types_walk_attribs (die);
27828 die->die_mark = 2;
27830 /* Mark children. */
27831 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
27834 /* Increment the string counts on strings referred to from DIE's
27835 attributes. */
27837 static void
27838 prune_unused_types_update_strings (dw_die_ref die)
27840 dw_attr_node *a;
27841 unsigned ix;
27843 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
27844 if (AT_class (a) == dw_val_class_str)
27846 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
27847 s->refcount++;
27848 /* Avoid unnecessarily putting strings that are used less than
27849 twice in the hash table. */
27850 if (s->refcount
27851 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
27853 indirect_string_node **slot
27854 = debug_str_hash->find_slot_with_hash (s->str,
27855 htab_hash_string (s->str),
27856 INSERT);
27857 gcc_assert (*slot == NULL);
27858 *slot = s;
27863 /* Mark DIE and its children as removed. */
27865 static void
27866 mark_removed (dw_die_ref die)
27868 dw_die_ref c;
27869 die->removed = true;
27870 FOR_EACH_CHILD (die, c, mark_removed (c));
27873 /* Remove from the tree DIE any dies that aren't marked. */
27875 static void
27876 prune_unused_types_prune (dw_die_ref die)
27878 dw_die_ref c;
27880 gcc_assert (die->die_mark);
27881 prune_unused_types_update_strings (die);
27883 if (! die->die_child)
27884 return;
27886 c = die->die_child;
27887 do {
27888 dw_die_ref prev = c, next;
27889 for (c = c->die_sib; ! c->die_mark; c = next)
27890 if (c == die->die_child)
27892 /* No marked children between 'prev' and the end of the list. */
27893 if (prev == c)
27894 /* No marked children at all. */
27895 die->die_child = NULL;
27896 else
27898 prev->die_sib = c->die_sib;
27899 die->die_child = prev;
27901 c->die_sib = NULL;
27902 mark_removed (c);
27903 return;
27905 else
27907 next = c->die_sib;
27908 c->die_sib = NULL;
27909 mark_removed (c);
27912 if (c != prev->die_sib)
27913 prev->die_sib = c;
27914 prune_unused_types_prune (c);
27915 } while (c != die->die_child);
27918 /* Remove dies representing declarations that we never use. */
27920 static void
27921 prune_unused_types (void)
27923 unsigned int i;
27924 limbo_die_node *node;
27925 comdat_type_node *ctnode;
27926 pubname_entry *pub;
27927 dw_die_ref base_type;
27929 #if ENABLE_ASSERT_CHECKING
27930 /* All the marks should already be clear. */
27931 verify_marks_clear (comp_unit_die ());
27932 for (node = limbo_die_list; node; node = node->next)
27933 verify_marks_clear (node->die);
27934 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
27935 verify_marks_clear (ctnode->root_die);
27936 #endif /* ENABLE_ASSERT_CHECKING */
27938 /* Mark types that are used in global variables. */
27939 premark_types_used_by_global_vars ();
27941 /* Set the mark on nodes that are actually used. */
27942 prune_unused_types_walk (comp_unit_die ());
27943 for (node = limbo_die_list; node; node = node->next)
27944 prune_unused_types_walk (node->die);
27945 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
27947 prune_unused_types_walk (ctnode->root_die);
27948 prune_unused_types_mark (ctnode->type_die, 1);
27951 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
27952 are unusual in that they are pubnames that are the children of pubtypes.
27953 They should only be marked via their parent DW_TAG_enumeration_type die,
27954 not as roots in themselves. */
27955 FOR_EACH_VEC_ELT (*pubname_table, i, pub)
27956 if (pub->die->die_tag != DW_TAG_enumerator)
27957 prune_unused_types_mark (pub->die, 1);
27958 for (i = 0; base_types.iterate (i, &base_type); i++)
27959 prune_unused_types_mark (base_type, 1);
27961 /* For -fvar-tracking-assignments, also set the mark on nodes that could be
27962 referenced by DW_TAG_call_site DW_AT_call_origin (i.e. direct call
27963 callees). */
27964 cgraph_node *cnode;
27965 FOR_EACH_FUNCTION (cnode)
27966 if (cnode->referred_to_p (false))
27968 dw_die_ref die = lookup_decl_die (cnode->decl);
27969 if (die == NULL || die->die_mark)
27970 continue;
27971 for (cgraph_edge *e = cnode->callers; e; e = e->next_caller)
27972 if (e->caller != cnode
27973 && opt_for_fn (e->caller->decl, flag_var_tracking_assignments))
27975 prune_unused_types_mark (die, 1);
27976 break;
27980 if (debug_str_hash)
27981 debug_str_hash->empty ();
27982 if (skeleton_debug_str_hash)
27983 skeleton_debug_str_hash->empty ();
27984 prune_unused_types_prune (comp_unit_die ());
27985 for (limbo_die_node **pnode = &limbo_die_list; *pnode; )
27987 node = *pnode;
27988 if (!node->die->die_mark)
27989 *pnode = node->next;
27990 else
27992 prune_unused_types_prune (node->die);
27993 pnode = &node->next;
27996 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
27997 prune_unused_types_prune (ctnode->root_die);
27999 /* Leave the marks clear. */
28000 prune_unmark_dies (comp_unit_die ());
28001 for (node = limbo_die_list; node; node = node->next)
28002 prune_unmark_dies (node->die);
28003 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
28004 prune_unmark_dies (ctnode->root_die);
28007 /* Helpers to manipulate hash table of comdat type units. */
28009 struct comdat_type_hasher : nofree_ptr_hash <comdat_type_node>
28011 static inline hashval_t hash (const comdat_type_node *);
28012 static inline bool equal (const comdat_type_node *, const comdat_type_node *);
28015 inline hashval_t
28016 comdat_type_hasher::hash (const comdat_type_node *type_node)
28018 hashval_t h;
28019 memcpy (&h, type_node->signature, sizeof (h));
28020 return h;
28023 inline bool
28024 comdat_type_hasher::equal (const comdat_type_node *type_node_1,
28025 const comdat_type_node *type_node_2)
28027 return (! memcmp (type_node_1->signature, type_node_2->signature,
28028 DWARF_TYPE_SIGNATURE_SIZE));
28031 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
28032 to the location it would have been added, should we know its
28033 DECL_ASSEMBLER_NAME when we added other attributes. This will
28034 probably improve compactness of debug info, removing equivalent
28035 abbrevs, and hide any differences caused by deferring the
28036 computation of the assembler name, triggered by e.g. PCH. */
28038 static inline void
28039 move_linkage_attr (dw_die_ref die)
28041 unsigned ix = vec_safe_length (die->die_attr);
28042 dw_attr_node linkage = (*die->die_attr)[ix - 1];
28044 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
28045 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
28047 while (--ix > 0)
28049 dw_attr_node *prev = &(*die->die_attr)[ix - 1];
28051 if (prev->dw_attr == DW_AT_decl_line
28052 || prev->dw_attr == DW_AT_decl_column
28053 || prev->dw_attr == DW_AT_name)
28054 break;
28057 if (ix != vec_safe_length (die->die_attr) - 1)
28059 die->die_attr->pop ();
28060 die->die_attr->quick_insert (ix, linkage);
28064 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
28065 referenced from typed stack ops and count how often they are used. */
28067 static void
28068 mark_base_types (dw_loc_descr_ref loc)
28070 dw_die_ref base_type = NULL;
28072 for (; loc; loc = loc->dw_loc_next)
28074 switch (loc->dw_loc_opc)
28076 case DW_OP_regval_type:
28077 case DW_OP_deref_type:
28078 case DW_OP_GNU_regval_type:
28079 case DW_OP_GNU_deref_type:
28080 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
28081 break;
28082 case DW_OP_convert:
28083 case DW_OP_reinterpret:
28084 case DW_OP_GNU_convert:
28085 case DW_OP_GNU_reinterpret:
28086 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
28087 continue;
28088 /* FALLTHRU */
28089 case DW_OP_const_type:
28090 case DW_OP_GNU_const_type:
28091 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
28092 break;
28093 case DW_OP_entry_value:
28094 case DW_OP_GNU_entry_value:
28095 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
28096 continue;
28097 default:
28098 continue;
28100 gcc_assert (base_type->die_parent == comp_unit_die ());
28101 if (base_type->die_mark)
28102 base_type->die_mark++;
28103 else
28105 base_types.safe_push (base_type);
28106 base_type->die_mark = 1;
28111 /* Comparison function for sorting marked base types. */
28113 static int
28114 base_type_cmp (const void *x, const void *y)
28116 dw_die_ref dx = *(const dw_die_ref *) x;
28117 dw_die_ref dy = *(const dw_die_ref *) y;
28118 unsigned int byte_size1, byte_size2;
28119 unsigned int encoding1, encoding2;
28120 unsigned int align1, align2;
28121 if (dx->die_mark > dy->die_mark)
28122 return -1;
28123 if (dx->die_mark < dy->die_mark)
28124 return 1;
28125 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
28126 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
28127 if (byte_size1 < byte_size2)
28128 return 1;
28129 if (byte_size1 > byte_size2)
28130 return -1;
28131 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
28132 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
28133 if (encoding1 < encoding2)
28134 return 1;
28135 if (encoding1 > encoding2)
28136 return -1;
28137 align1 = get_AT_unsigned (dx, DW_AT_alignment);
28138 align2 = get_AT_unsigned (dy, DW_AT_alignment);
28139 if (align1 < align2)
28140 return 1;
28141 if (align1 > align2)
28142 return -1;
28143 return 0;
28146 /* Move base types marked by mark_base_types as early as possible
28147 in the CU, sorted by decreasing usage count both to make the
28148 uleb128 references as small as possible and to make sure they
28149 will have die_offset already computed by calc_die_sizes when
28150 sizes of typed stack loc ops is computed. */
28152 static void
28153 move_marked_base_types (void)
28155 unsigned int i;
28156 dw_die_ref base_type, die, c;
28158 if (base_types.is_empty ())
28159 return;
28161 /* Sort by decreasing usage count, they will be added again in that
28162 order later on. */
28163 base_types.qsort (base_type_cmp);
28164 die = comp_unit_die ();
28165 c = die->die_child;
28168 dw_die_ref prev = c;
28169 c = c->die_sib;
28170 while (c->die_mark)
28172 remove_child_with_prev (c, prev);
28173 /* As base types got marked, there must be at least
28174 one node other than DW_TAG_base_type. */
28175 gcc_assert (die->die_child != NULL);
28176 c = prev->die_sib;
28179 while (c != die->die_child);
28180 gcc_assert (die->die_child);
28181 c = die->die_child;
28182 for (i = 0; base_types.iterate (i, &base_type); i++)
28184 base_type->die_mark = 0;
28185 base_type->die_sib = c->die_sib;
28186 c->die_sib = base_type;
28187 c = base_type;
28191 /* Helper function for resolve_addr, attempt to resolve
28192 one CONST_STRING, return true if successful. Similarly verify that
28193 SYMBOL_REFs refer to variables emitted in the current CU. */
28195 static bool
28196 resolve_one_addr (rtx *addr)
28198 rtx rtl = *addr;
28200 if (GET_CODE (rtl) == CONST_STRING)
28202 size_t len = strlen (XSTR (rtl, 0)) + 1;
28203 tree t = build_string (len, XSTR (rtl, 0));
28204 tree tlen = size_int (len - 1);
28205 TREE_TYPE (t)
28206 = build_array_type (char_type_node, build_index_type (tlen));
28207 rtl = lookup_constant_def (t);
28208 if (!rtl || !MEM_P (rtl))
28209 return false;
28210 rtl = XEXP (rtl, 0);
28211 if (GET_CODE (rtl) == SYMBOL_REF
28212 && SYMBOL_REF_DECL (rtl)
28213 && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
28214 return false;
28215 vec_safe_push (used_rtx_array, rtl);
28216 *addr = rtl;
28217 return true;
28220 if (GET_CODE (rtl) == SYMBOL_REF
28221 && SYMBOL_REF_DECL (rtl))
28223 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
28225 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
28226 return false;
28228 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
28229 return false;
28232 if (GET_CODE (rtl) == CONST)
28234 subrtx_ptr_iterator::array_type array;
28235 FOR_EACH_SUBRTX_PTR (iter, array, &XEXP (rtl, 0), ALL)
28236 if (!resolve_one_addr (*iter))
28237 return false;
28240 return true;
28243 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
28244 if possible, and create DW_TAG_dwarf_procedure that can be referenced
28245 from DW_OP_implicit_pointer if the string hasn't been seen yet. */
28247 static rtx
28248 string_cst_pool_decl (tree t)
28250 rtx rtl = output_constant_def (t, 1);
28251 unsigned char *array;
28252 dw_loc_descr_ref l;
28253 tree decl;
28254 size_t len;
28255 dw_die_ref ref;
28257 if (!rtl || !MEM_P (rtl))
28258 return NULL_RTX;
28259 rtl = XEXP (rtl, 0);
28260 if (GET_CODE (rtl) != SYMBOL_REF
28261 || SYMBOL_REF_DECL (rtl) == NULL_TREE)
28262 return NULL_RTX;
28264 decl = SYMBOL_REF_DECL (rtl);
28265 if (!lookup_decl_die (decl))
28267 len = TREE_STRING_LENGTH (t);
28268 vec_safe_push (used_rtx_array, rtl);
28269 ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
28270 array = ggc_vec_alloc<unsigned char> (len);
28271 memcpy (array, TREE_STRING_POINTER (t), len);
28272 l = new_loc_descr (DW_OP_implicit_value, len, 0);
28273 l->dw_loc_oprnd2.val_class = dw_val_class_vec;
28274 l->dw_loc_oprnd2.v.val_vec.length = len;
28275 l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
28276 l->dw_loc_oprnd2.v.val_vec.array = array;
28277 add_AT_loc (ref, DW_AT_location, l);
28278 equate_decl_number_to_die (decl, ref);
28280 return rtl;
28283 /* Helper function of resolve_addr_in_expr. LOC is
28284 a DW_OP_addr followed by DW_OP_stack_value, either at the start
28285 of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
28286 resolved. Replace it (both DW_OP_addr and DW_OP_stack_value)
28287 with DW_OP_implicit_pointer if possible
28288 and return true, if unsuccessful, return false. */
28290 static bool
28291 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
28293 rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
28294 HOST_WIDE_INT offset = 0;
28295 dw_die_ref ref = NULL;
28296 tree decl;
28298 if (GET_CODE (rtl) == CONST
28299 && GET_CODE (XEXP (rtl, 0)) == PLUS
28300 && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
28302 offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
28303 rtl = XEXP (XEXP (rtl, 0), 0);
28305 if (GET_CODE (rtl) == CONST_STRING)
28307 size_t len = strlen (XSTR (rtl, 0)) + 1;
28308 tree t = build_string (len, XSTR (rtl, 0));
28309 tree tlen = size_int (len - 1);
28311 TREE_TYPE (t)
28312 = build_array_type (char_type_node, build_index_type (tlen));
28313 rtl = string_cst_pool_decl (t);
28314 if (!rtl)
28315 return false;
28317 if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
28319 decl = SYMBOL_REF_DECL (rtl);
28320 if (VAR_P (decl) && !DECL_EXTERNAL (decl))
28322 ref = lookup_decl_die (decl);
28323 if (ref && (get_AT (ref, DW_AT_location)
28324 || get_AT (ref, DW_AT_const_value)))
28326 loc->dw_loc_opc = dwarf_OP (DW_OP_implicit_pointer);
28327 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
28328 loc->dw_loc_oprnd1.val_entry = NULL;
28329 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
28330 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
28331 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
28332 loc->dw_loc_oprnd2.v.val_int = offset;
28333 return true;
28337 return false;
28340 /* Helper function for resolve_addr, handle one location
28341 expression, return false if at least one CONST_STRING or SYMBOL_REF in
28342 the location list couldn't be resolved. */
28344 static bool
28345 resolve_addr_in_expr (dw_attr_node *a, dw_loc_descr_ref loc)
28347 dw_loc_descr_ref keep = NULL;
28348 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
28349 switch (loc->dw_loc_opc)
28351 case DW_OP_addr:
28352 if (!resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
28354 if ((prev == NULL
28355 || prev->dw_loc_opc == DW_OP_piece
28356 || prev->dw_loc_opc == DW_OP_bit_piece)
28357 && loc->dw_loc_next
28358 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
28359 && (!dwarf_strict || dwarf_version >= 5)
28360 && optimize_one_addr_into_implicit_ptr (loc))
28361 break;
28362 return false;
28364 break;
28365 case DW_OP_GNU_addr_index:
28366 case DW_OP_GNU_const_index:
28367 if (loc->dw_loc_opc == DW_OP_GNU_addr_index
28368 || (loc->dw_loc_opc == DW_OP_GNU_const_index && loc->dtprel))
28370 rtx rtl = loc->dw_loc_oprnd1.val_entry->addr.rtl;
28371 if (!resolve_one_addr (&rtl))
28372 return false;
28373 remove_addr_table_entry (loc->dw_loc_oprnd1.val_entry);
28374 loc->dw_loc_oprnd1.val_entry
28375 = add_addr_table_entry (rtl, ate_kind_rtx);
28377 break;
28378 case DW_OP_const4u:
28379 case DW_OP_const8u:
28380 if (loc->dtprel
28381 && !resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
28382 return false;
28383 break;
28384 case DW_OP_plus_uconst:
28385 if (size_of_loc_descr (loc)
28386 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
28388 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
28390 dw_loc_descr_ref repl
28391 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
28392 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
28393 add_loc_descr (&repl, loc->dw_loc_next);
28394 *loc = *repl;
28396 break;
28397 case DW_OP_implicit_value:
28398 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
28399 && !resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr))
28400 return false;
28401 break;
28402 case DW_OP_implicit_pointer:
28403 case DW_OP_GNU_implicit_pointer:
28404 case DW_OP_GNU_parameter_ref:
28405 case DW_OP_GNU_variable_value:
28406 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
28408 dw_die_ref ref
28409 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
28410 if (ref == NULL)
28411 return false;
28412 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
28413 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
28414 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
28416 if (loc->dw_loc_opc == DW_OP_GNU_variable_value)
28418 if (prev == NULL
28419 && loc->dw_loc_next == NULL
28420 && AT_class (a) == dw_val_class_loc)
28421 switch (a->dw_attr)
28423 /* Following attributes allow both exprloc and reference,
28424 so if the whole expression is DW_OP_GNU_variable_value
28425 alone we could transform it into reference. */
28426 case DW_AT_byte_size:
28427 case DW_AT_bit_size:
28428 case DW_AT_lower_bound:
28429 case DW_AT_upper_bound:
28430 case DW_AT_bit_stride:
28431 case DW_AT_count:
28432 case DW_AT_allocated:
28433 case DW_AT_associated:
28434 case DW_AT_byte_stride:
28435 a->dw_attr_val.val_class = dw_val_class_die_ref;
28436 a->dw_attr_val.val_entry = NULL;
28437 a->dw_attr_val.v.val_die_ref.die
28438 = loc->dw_loc_oprnd1.v.val_die_ref.die;
28439 a->dw_attr_val.v.val_die_ref.external = 0;
28440 return true;
28441 default:
28442 break;
28444 if (dwarf_strict)
28445 return false;
28447 break;
28448 case DW_OP_const_type:
28449 case DW_OP_regval_type:
28450 case DW_OP_deref_type:
28451 case DW_OP_convert:
28452 case DW_OP_reinterpret:
28453 case DW_OP_GNU_const_type:
28454 case DW_OP_GNU_regval_type:
28455 case DW_OP_GNU_deref_type:
28456 case DW_OP_GNU_convert:
28457 case DW_OP_GNU_reinterpret:
28458 while (loc->dw_loc_next
28459 && (loc->dw_loc_next->dw_loc_opc == DW_OP_convert
28460 || loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert))
28462 dw_die_ref base1, base2;
28463 unsigned enc1, enc2, size1, size2;
28464 if (loc->dw_loc_opc == DW_OP_regval_type
28465 || loc->dw_loc_opc == DW_OP_deref_type
28466 || loc->dw_loc_opc == DW_OP_GNU_regval_type
28467 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
28468 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
28469 else if (loc->dw_loc_oprnd1.val_class
28470 == dw_val_class_unsigned_const)
28471 break;
28472 else
28473 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
28474 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
28475 == dw_val_class_unsigned_const)
28476 break;
28477 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
28478 gcc_assert (base1->die_tag == DW_TAG_base_type
28479 && base2->die_tag == DW_TAG_base_type);
28480 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
28481 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
28482 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
28483 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
28484 if (size1 == size2
28485 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
28486 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
28487 && loc != keep)
28488 || enc1 == enc2))
28490 /* Optimize away next DW_OP_convert after
28491 adjusting LOC's base type die reference. */
28492 if (loc->dw_loc_opc == DW_OP_regval_type
28493 || loc->dw_loc_opc == DW_OP_deref_type
28494 || loc->dw_loc_opc == DW_OP_GNU_regval_type
28495 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
28496 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
28497 else
28498 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
28499 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
28500 continue;
28502 /* Don't change integer DW_OP_convert after e.g. floating
28503 point typed stack entry. */
28504 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
28505 keep = loc->dw_loc_next;
28506 break;
28508 break;
28509 default:
28510 break;
28512 return true;
28515 /* Helper function of resolve_addr. DIE had DW_AT_location of
28516 DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
28517 and DW_OP_addr couldn't be resolved. resolve_addr has already
28518 removed the DW_AT_location attribute. This function attempts to
28519 add a new DW_AT_location attribute with DW_OP_implicit_pointer
28520 to it or DW_AT_const_value attribute, if possible. */
28522 static void
28523 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
28525 if (!VAR_P (decl)
28526 || lookup_decl_die (decl) != die
28527 || DECL_EXTERNAL (decl)
28528 || !TREE_STATIC (decl)
28529 || DECL_INITIAL (decl) == NULL_TREE
28530 || DECL_P (DECL_INITIAL (decl))
28531 || get_AT (die, DW_AT_const_value))
28532 return;
28534 tree init = DECL_INITIAL (decl);
28535 HOST_WIDE_INT offset = 0;
28536 /* For variables that have been optimized away and thus
28537 don't have a memory location, see if we can emit
28538 DW_AT_const_value instead. */
28539 if (tree_add_const_value_attribute (die, init))
28540 return;
28541 if (dwarf_strict && dwarf_version < 5)
28542 return;
28543 /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
28544 and ADDR_EXPR refers to a decl that has DW_AT_location or
28545 DW_AT_const_value (but isn't addressable, otherwise
28546 resolving the original DW_OP_addr wouldn't fail), see if
28547 we can add DW_OP_implicit_pointer. */
28548 STRIP_NOPS (init);
28549 if (TREE_CODE (init) == POINTER_PLUS_EXPR
28550 && tree_fits_shwi_p (TREE_OPERAND (init, 1)))
28552 offset = tree_to_shwi (TREE_OPERAND (init, 1));
28553 init = TREE_OPERAND (init, 0);
28554 STRIP_NOPS (init);
28556 if (TREE_CODE (init) != ADDR_EXPR)
28557 return;
28558 if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
28559 && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
28560 || (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL
28561 && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
28562 && TREE_OPERAND (init, 0) != decl))
28564 dw_die_ref ref;
28565 dw_loc_descr_ref l;
28567 if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
28569 rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
28570 if (!rtl)
28571 return;
28572 decl = SYMBOL_REF_DECL (rtl);
28574 else
28575 decl = TREE_OPERAND (init, 0);
28576 ref = lookup_decl_die (decl);
28577 if (ref == NULL
28578 || (!get_AT (ref, DW_AT_location)
28579 && !get_AT (ref, DW_AT_const_value)))
28580 return;
28581 l = new_loc_descr (dwarf_OP (DW_OP_implicit_pointer), 0, offset);
28582 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
28583 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
28584 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
28585 add_AT_loc (die, DW_AT_location, l);
28589 /* Return NULL if l is a DWARF expression, or first op that is not
28590 valid DWARF expression. */
28592 static dw_loc_descr_ref
28593 non_dwarf_expression (dw_loc_descr_ref l)
28595 while (l)
28597 if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
28598 return l;
28599 switch (l->dw_loc_opc)
28601 case DW_OP_regx:
28602 case DW_OP_implicit_value:
28603 case DW_OP_stack_value:
28604 case DW_OP_implicit_pointer:
28605 case DW_OP_GNU_implicit_pointer:
28606 case DW_OP_GNU_parameter_ref:
28607 case DW_OP_piece:
28608 case DW_OP_bit_piece:
28609 return l;
28610 default:
28611 break;
28613 l = l->dw_loc_next;
28615 return NULL;
28618 /* Return adjusted copy of EXPR:
28619 If it is empty DWARF expression, return it.
28620 If it is valid non-empty DWARF expression,
28621 return copy of EXPR with DW_OP_deref appended to it.
28622 If it is DWARF expression followed by DW_OP_reg{N,x}, return
28623 copy of the DWARF expression with DW_OP_breg{N,x} <0> appended.
28624 If it is DWARF expression followed by DW_OP_stack_value, return
28625 copy of the DWARF expression without anything appended.
28626 Otherwise, return NULL. */
28628 static dw_loc_descr_ref
28629 copy_deref_exprloc (dw_loc_descr_ref expr)
28631 dw_loc_descr_ref tail = NULL;
28633 if (expr == NULL)
28634 return NULL;
28636 dw_loc_descr_ref l = non_dwarf_expression (expr);
28637 if (l && l->dw_loc_next)
28638 return NULL;
28640 if (l)
28642 if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
28643 tail = new_loc_descr ((enum dwarf_location_atom)
28644 (DW_OP_breg0 + (l->dw_loc_opc - DW_OP_reg0)),
28645 0, 0);
28646 else
28647 switch (l->dw_loc_opc)
28649 case DW_OP_regx:
28650 tail = new_loc_descr (DW_OP_bregx,
28651 l->dw_loc_oprnd1.v.val_unsigned, 0);
28652 break;
28653 case DW_OP_stack_value:
28654 break;
28655 default:
28656 return NULL;
28659 else
28660 tail = new_loc_descr (DW_OP_deref, 0, 0);
28662 dw_loc_descr_ref ret = NULL, *p = &ret;
28663 while (expr != l)
28665 *p = new_loc_descr (expr->dw_loc_opc, 0, 0);
28666 (*p)->dw_loc_oprnd1 = expr->dw_loc_oprnd1;
28667 (*p)->dw_loc_oprnd2 = expr->dw_loc_oprnd2;
28668 p = &(*p)->dw_loc_next;
28669 expr = expr->dw_loc_next;
28671 *p = tail;
28672 return ret;
28675 /* For DW_AT_string_length attribute with DW_OP_GNU_variable_value
28676 reference to a variable or argument, adjust it if needed and return:
28677 -1 if the DW_AT_string_length attribute and DW_AT_{string_length_,}byte_size
28678 attribute if present should be removed
28679 0 keep the attribute perhaps with minor modifications, no need to rescan
28680 1 if the attribute has been successfully adjusted. */
28682 static int
28683 optimize_string_length (dw_attr_node *a)
28685 dw_loc_descr_ref l = AT_loc (a), lv;
28686 dw_die_ref die;
28687 if (l->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
28689 tree decl = l->dw_loc_oprnd1.v.val_decl_ref;
28690 die = lookup_decl_die (decl);
28691 if (die)
28693 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
28694 l->dw_loc_oprnd1.v.val_die_ref.die = die;
28695 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
28697 else
28698 return -1;
28700 else
28701 die = l->dw_loc_oprnd1.v.val_die_ref.die;
28703 /* DWARF5 allows reference class, so we can then reference the DIE.
28704 Only do this for DW_OP_GNU_variable_value DW_OP_stack_value. */
28705 if (l->dw_loc_next != NULL && dwarf_version >= 5)
28707 a->dw_attr_val.val_class = dw_val_class_die_ref;
28708 a->dw_attr_val.val_entry = NULL;
28709 a->dw_attr_val.v.val_die_ref.die = die;
28710 a->dw_attr_val.v.val_die_ref.external = 0;
28711 return 0;
28714 dw_attr_node *av = get_AT (die, DW_AT_location);
28715 dw_loc_list_ref d;
28716 bool non_dwarf_expr = false;
28718 if (av == NULL)
28719 return dwarf_strict ? -1 : 0;
28720 switch (AT_class (av))
28722 case dw_val_class_loc_list:
28723 for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
28724 if (d->expr && non_dwarf_expression (d->expr))
28725 non_dwarf_expr = true;
28726 break;
28727 case dw_val_class_loc:
28728 lv = AT_loc (av);
28729 if (lv == NULL)
28730 return dwarf_strict ? -1 : 0;
28731 if (non_dwarf_expression (lv))
28732 non_dwarf_expr = true;
28733 break;
28734 default:
28735 return dwarf_strict ? -1 : 0;
28738 /* If it is safe to transform DW_OP_GNU_variable_value DW_OP_stack_value
28739 into DW_OP_call4 or DW_OP_GNU_variable_value into
28740 DW_OP_call4 DW_OP_deref, do so. */
28741 if (!non_dwarf_expr
28742 && (l->dw_loc_next != NULL || AT_class (av) == dw_val_class_loc))
28744 l->dw_loc_opc = DW_OP_call4;
28745 if (l->dw_loc_next)
28746 l->dw_loc_next = NULL;
28747 else
28748 l->dw_loc_next = new_loc_descr (DW_OP_deref, 0, 0);
28749 return 0;
28752 /* For DW_OP_GNU_variable_value DW_OP_stack_value, we can just
28753 copy over the DW_AT_location attribute from die to a. */
28754 if (l->dw_loc_next != NULL)
28756 a->dw_attr_val = av->dw_attr_val;
28757 return 1;
28760 dw_loc_list_ref list, *p;
28761 switch (AT_class (av))
28763 case dw_val_class_loc_list:
28764 p = &list;
28765 list = NULL;
28766 for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
28768 lv = copy_deref_exprloc (d->expr);
28769 if (lv)
28771 *p = new_loc_list (lv, d->begin, d->end, d->section);
28772 p = &(*p)->dw_loc_next;
28774 else if (!dwarf_strict && d->expr)
28775 return 0;
28777 if (list == NULL)
28778 return dwarf_strict ? -1 : 0;
28779 a->dw_attr_val.val_class = dw_val_class_loc_list;
28780 gen_llsym (list);
28781 *AT_loc_list_ptr (a) = list;
28782 return 1;
28783 case dw_val_class_loc:
28784 lv = copy_deref_exprloc (AT_loc (av));
28785 if (lv == NULL)
28786 return dwarf_strict ? -1 : 0;
28787 a->dw_attr_val.v.val_loc = lv;
28788 return 1;
28789 default:
28790 gcc_unreachable ();
28794 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
28795 an address in .rodata section if the string literal is emitted there,
28796 or remove the containing location list or replace DW_AT_const_value
28797 with DW_AT_location and empty location expression, if it isn't found
28798 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
28799 to something that has been emitted in the current CU. */
28801 static void
28802 resolve_addr (dw_die_ref die)
28804 dw_die_ref c;
28805 dw_attr_node *a;
28806 dw_loc_list_ref *curr, *start, loc;
28807 unsigned ix;
28808 bool remove_AT_byte_size = false;
28810 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
28811 switch (AT_class (a))
28813 case dw_val_class_loc_list:
28814 start = curr = AT_loc_list_ptr (a);
28815 loc = *curr;
28816 gcc_assert (loc);
28817 /* The same list can be referenced more than once. See if we have
28818 already recorded the result from a previous pass. */
28819 if (loc->replaced)
28820 *curr = loc->dw_loc_next;
28821 else if (!loc->resolved_addr)
28823 /* As things stand, we do not expect or allow one die to
28824 reference a suffix of another die's location list chain.
28825 References must be identical or completely separate.
28826 There is therefore no need to cache the result of this
28827 pass on any list other than the first; doing so
28828 would lead to unnecessary writes. */
28829 while (*curr)
28831 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
28832 if (!resolve_addr_in_expr (a, (*curr)->expr))
28834 dw_loc_list_ref next = (*curr)->dw_loc_next;
28835 dw_loc_descr_ref l = (*curr)->expr;
28837 if (next && (*curr)->ll_symbol)
28839 gcc_assert (!next->ll_symbol);
28840 next->ll_symbol = (*curr)->ll_symbol;
28842 if (dwarf_split_debug_info)
28843 remove_loc_list_addr_table_entries (l);
28844 *curr = next;
28846 else
28848 mark_base_types ((*curr)->expr);
28849 curr = &(*curr)->dw_loc_next;
28852 if (loc == *start)
28853 loc->resolved_addr = 1;
28854 else
28856 loc->replaced = 1;
28857 loc->dw_loc_next = *start;
28860 if (!*start)
28862 remove_AT (die, a->dw_attr);
28863 ix--;
28865 break;
28866 case dw_val_class_loc:
28868 dw_loc_descr_ref l = AT_loc (a);
28869 /* DW_OP_GNU_variable_value DW_OP_stack_value or
28870 DW_OP_GNU_variable_value in DW_AT_string_length can be converted
28871 into DW_OP_call4 or DW_OP_call4 DW_OP_deref, which is standard
28872 DWARF4 unlike DW_OP_GNU_variable_value. Or for DWARF5
28873 DW_OP_GNU_variable_value DW_OP_stack_value can be replaced
28874 with DW_FORM_ref referencing the same DIE as
28875 DW_OP_GNU_variable_value used to reference. */
28876 if (a->dw_attr == DW_AT_string_length
28877 && l
28878 && l->dw_loc_opc == DW_OP_GNU_variable_value
28879 && (l->dw_loc_next == NULL
28880 || (l->dw_loc_next->dw_loc_next == NULL
28881 && l->dw_loc_next->dw_loc_opc == DW_OP_stack_value)))
28883 switch (optimize_string_length (a))
28885 case -1:
28886 remove_AT (die, a->dw_attr);
28887 ix--;
28888 /* If we drop DW_AT_string_length, we need to drop also
28889 DW_AT_{string_length_,}byte_size. */
28890 remove_AT_byte_size = true;
28891 continue;
28892 default:
28893 break;
28894 case 1:
28895 /* Even if we keep the optimized DW_AT_string_length,
28896 it might have changed AT_class, so process it again. */
28897 ix--;
28898 continue;
28901 /* For -gdwarf-2 don't attempt to optimize
28902 DW_AT_data_member_location containing
28903 DW_OP_plus_uconst - older consumers might
28904 rely on it being that op instead of a more complex,
28905 but shorter, location description. */
28906 if ((dwarf_version > 2
28907 || a->dw_attr != DW_AT_data_member_location
28908 || l == NULL
28909 || l->dw_loc_opc != DW_OP_plus_uconst
28910 || l->dw_loc_next != NULL)
28911 && !resolve_addr_in_expr (a, l))
28913 if (dwarf_split_debug_info)
28914 remove_loc_list_addr_table_entries (l);
28915 if (l != NULL
28916 && l->dw_loc_next == NULL
28917 && l->dw_loc_opc == DW_OP_addr
28918 && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
28919 && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
28920 && a->dw_attr == DW_AT_location)
28922 tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
28923 remove_AT (die, a->dw_attr);
28924 ix--;
28925 optimize_location_into_implicit_ptr (die, decl);
28926 break;
28928 if (a->dw_attr == DW_AT_string_length)
28929 /* If we drop DW_AT_string_length, we need to drop also
28930 DW_AT_{string_length_,}byte_size. */
28931 remove_AT_byte_size = true;
28932 remove_AT (die, a->dw_attr);
28933 ix--;
28935 else
28936 mark_base_types (l);
28938 break;
28939 case dw_val_class_addr:
28940 if (a->dw_attr == DW_AT_const_value
28941 && !resolve_one_addr (&a->dw_attr_val.v.val_addr))
28943 if (AT_index (a) != NOT_INDEXED)
28944 remove_addr_table_entry (a->dw_attr_val.val_entry);
28945 remove_AT (die, a->dw_attr);
28946 ix--;
28948 if ((die->die_tag == DW_TAG_call_site
28949 && a->dw_attr == DW_AT_call_origin)
28950 || (die->die_tag == DW_TAG_GNU_call_site
28951 && a->dw_attr == DW_AT_abstract_origin))
28953 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
28954 dw_die_ref tdie = lookup_decl_die (tdecl);
28955 dw_die_ref cdie;
28956 if (tdie == NULL
28957 && DECL_EXTERNAL (tdecl)
28958 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE
28959 && (cdie = lookup_context_die (DECL_CONTEXT (tdecl))))
28961 dw_die_ref pdie = cdie;
28962 /* Make sure we don't add these DIEs into type units.
28963 We could emit skeleton DIEs for context (namespaces,
28964 outer structs/classes) and a skeleton DIE for the
28965 innermost context with DW_AT_signature pointing to the
28966 type unit. See PR78835. */
28967 while (pdie && pdie->die_tag != DW_TAG_type_unit)
28968 pdie = pdie->die_parent;
28969 if (pdie == NULL)
28971 /* Creating a full DIE for tdecl is overly expensive and
28972 at this point even wrong when in the LTO phase
28973 as it can end up generating new type DIEs we didn't
28974 output and thus optimize_external_refs will crash. */
28975 tdie = new_die (DW_TAG_subprogram, cdie, NULL_TREE);
28976 add_AT_flag (tdie, DW_AT_external, 1);
28977 add_AT_flag (tdie, DW_AT_declaration, 1);
28978 add_linkage_attr (tdie, tdecl);
28979 add_name_and_src_coords_attributes (tdie, tdecl);
28980 equate_decl_number_to_die (tdecl, tdie);
28983 if (tdie)
28985 a->dw_attr_val.val_class = dw_val_class_die_ref;
28986 a->dw_attr_val.v.val_die_ref.die = tdie;
28987 a->dw_attr_val.v.val_die_ref.external = 0;
28989 else
28991 if (AT_index (a) != NOT_INDEXED)
28992 remove_addr_table_entry (a->dw_attr_val.val_entry);
28993 remove_AT (die, a->dw_attr);
28994 ix--;
28997 break;
28998 default:
28999 break;
29002 if (remove_AT_byte_size)
29003 remove_AT (die, dwarf_version >= 5
29004 ? DW_AT_string_length_byte_size
29005 : DW_AT_byte_size);
29007 FOR_EACH_CHILD (die, c, resolve_addr (c));
29010 /* Helper routines for optimize_location_lists.
29011 This pass tries to share identical local lists in .debug_loc
29012 section. */
29014 /* Iteratively hash operands of LOC opcode into HSTATE. */
29016 static void
29017 hash_loc_operands (dw_loc_descr_ref loc, inchash::hash &hstate)
29019 dw_val_ref val1 = &loc->dw_loc_oprnd1;
29020 dw_val_ref val2 = &loc->dw_loc_oprnd2;
29022 switch (loc->dw_loc_opc)
29024 case DW_OP_const4u:
29025 case DW_OP_const8u:
29026 if (loc->dtprel)
29027 goto hash_addr;
29028 /* FALLTHRU */
29029 case DW_OP_const1u:
29030 case DW_OP_const1s:
29031 case DW_OP_const2u:
29032 case DW_OP_const2s:
29033 case DW_OP_const4s:
29034 case DW_OP_const8s:
29035 case DW_OP_constu:
29036 case DW_OP_consts:
29037 case DW_OP_pick:
29038 case DW_OP_plus_uconst:
29039 case DW_OP_breg0:
29040 case DW_OP_breg1:
29041 case DW_OP_breg2:
29042 case DW_OP_breg3:
29043 case DW_OP_breg4:
29044 case DW_OP_breg5:
29045 case DW_OP_breg6:
29046 case DW_OP_breg7:
29047 case DW_OP_breg8:
29048 case DW_OP_breg9:
29049 case DW_OP_breg10:
29050 case DW_OP_breg11:
29051 case DW_OP_breg12:
29052 case DW_OP_breg13:
29053 case DW_OP_breg14:
29054 case DW_OP_breg15:
29055 case DW_OP_breg16:
29056 case DW_OP_breg17:
29057 case DW_OP_breg18:
29058 case DW_OP_breg19:
29059 case DW_OP_breg20:
29060 case DW_OP_breg21:
29061 case DW_OP_breg22:
29062 case DW_OP_breg23:
29063 case DW_OP_breg24:
29064 case DW_OP_breg25:
29065 case DW_OP_breg26:
29066 case DW_OP_breg27:
29067 case DW_OP_breg28:
29068 case DW_OP_breg29:
29069 case DW_OP_breg30:
29070 case DW_OP_breg31:
29071 case DW_OP_regx:
29072 case DW_OP_fbreg:
29073 case DW_OP_piece:
29074 case DW_OP_deref_size:
29075 case DW_OP_xderef_size:
29076 hstate.add_object (val1->v.val_int);
29077 break;
29078 case DW_OP_skip:
29079 case DW_OP_bra:
29081 int offset;
29083 gcc_assert (val1->val_class == dw_val_class_loc);
29084 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
29085 hstate.add_object (offset);
29087 break;
29088 case DW_OP_implicit_value:
29089 hstate.add_object (val1->v.val_unsigned);
29090 switch (val2->val_class)
29092 case dw_val_class_const:
29093 hstate.add_object (val2->v.val_int);
29094 break;
29095 case dw_val_class_vec:
29097 unsigned int elt_size = val2->v.val_vec.elt_size;
29098 unsigned int len = val2->v.val_vec.length;
29100 hstate.add_int (elt_size);
29101 hstate.add_int (len);
29102 hstate.add (val2->v.val_vec.array, len * elt_size);
29104 break;
29105 case dw_val_class_const_double:
29106 hstate.add_object (val2->v.val_double.low);
29107 hstate.add_object (val2->v.val_double.high);
29108 break;
29109 case dw_val_class_wide_int:
29110 hstate.add (val2->v.val_wide->get_val (),
29111 get_full_len (*val2->v.val_wide)
29112 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
29113 break;
29114 case dw_val_class_addr:
29115 inchash::add_rtx (val2->v.val_addr, hstate);
29116 break;
29117 default:
29118 gcc_unreachable ();
29120 break;
29121 case DW_OP_bregx:
29122 case DW_OP_bit_piece:
29123 hstate.add_object (val1->v.val_int);
29124 hstate.add_object (val2->v.val_int);
29125 break;
29126 case DW_OP_addr:
29127 hash_addr:
29128 if (loc->dtprel)
29130 unsigned char dtprel = 0xd1;
29131 hstate.add_object (dtprel);
29133 inchash::add_rtx (val1->v.val_addr, hstate);
29134 break;
29135 case DW_OP_GNU_addr_index:
29136 case DW_OP_GNU_const_index:
29138 if (loc->dtprel)
29140 unsigned char dtprel = 0xd1;
29141 hstate.add_object (dtprel);
29143 inchash::add_rtx (val1->val_entry->addr.rtl, hstate);
29145 break;
29146 case DW_OP_implicit_pointer:
29147 case DW_OP_GNU_implicit_pointer:
29148 hstate.add_int (val2->v.val_int);
29149 break;
29150 case DW_OP_entry_value:
29151 case DW_OP_GNU_entry_value:
29152 hstate.add_object (val1->v.val_loc);
29153 break;
29154 case DW_OP_regval_type:
29155 case DW_OP_deref_type:
29156 case DW_OP_GNU_regval_type:
29157 case DW_OP_GNU_deref_type:
29159 unsigned int byte_size
29160 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
29161 unsigned int encoding
29162 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
29163 hstate.add_object (val1->v.val_int);
29164 hstate.add_object (byte_size);
29165 hstate.add_object (encoding);
29167 break;
29168 case DW_OP_convert:
29169 case DW_OP_reinterpret:
29170 case DW_OP_GNU_convert:
29171 case DW_OP_GNU_reinterpret:
29172 if (val1->val_class == dw_val_class_unsigned_const)
29174 hstate.add_object (val1->v.val_unsigned);
29175 break;
29177 /* FALLTHRU */
29178 case DW_OP_const_type:
29179 case DW_OP_GNU_const_type:
29181 unsigned int byte_size
29182 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
29183 unsigned int encoding
29184 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
29185 hstate.add_object (byte_size);
29186 hstate.add_object (encoding);
29187 if (loc->dw_loc_opc != DW_OP_const_type
29188 && loc->dw_loc_opc != DW_OP_GNU_const_type)
29189 break;
29190 hstate.add_object (val2->val_class);
29191 switch (val2->val_class)
29193 case dw_val_class_const:
29194 hstate.add_object (val2->v.val_int);
29195 break;
29196 case dw_val_class_vec:
29198 unsigned int elt_size = val2->v.val_vec.elt_size;
29199 unsigned int len = val2->v.val_vec.length;
29201 hstate.add_object (elt_size);
29202 hstate.add_object (len);
29203 hstate.add (val2->v.val_vec.array, len * elt_size);
29205 break;
29206 case dw_val_class_const_double:
29207 hstate.add_object (val2->v.val_double.low);
29208 hstate.add_object (val2->v.val_double.high);
29209 break;
29210 case dw_val_class_wide_int:
29211 hstate.add (val2->v.val_wide->get_val (),
29212 get_full_len (*val2->v.val_wide)
29213 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
29214 break;
29215 default:
29216 gcc_unreachable ();
29219 break;
29221 default:
29222 /* Other codes have no operands. */
29223 break;
29227 /* Iteratively hash the whole DWARF location expression LOC into HSTATE. */
29229 static inline void
29230 hash_locs (dw_loc_descr_ref loc, inchash::hash &hstate)
29232 dw_loc_descr_ref l;
29233 bool sizes_computed = false;
29234 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
29235 size_of_locs (loc);
29237 for (l = loc; l != NULL; l = l->dw_loc_next)
29239 enum dwarf_location_atom opc = l->dw_loc_opc;
29240 hstate.add_object (opc);
29241 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
29243 size_of_locs (loc);
29244 sizes_computed = true;
29246 hash_loc_operands (l, hstate);
29250 /* Compute hash of the whole location list LIST_HEAD. */
29252 static inline void
29253 hash_loc_list (dw_loc_list_ref list_head)
29255 dw_loc_list_ref curr = list_head;
29256 inchash::hash hstate;
29258 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
29260 hstate.add (curr->begin, strlen (curr->begin) + 1);
29261 hstate.add (curr->end, strlen (curr->end) + 1);
29262 if (curr->section)
29263 hstate.add (curr->section, strlen (curr->section) + 1);
29264 hash_locs (curr->expr, hstate);
29266 list_head->hash = hstate.end ();
29269 /* Return true if X and Y opcodes have the same operands. */
29271 static inline bool
29272 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
29274 dw_val_ref valx1 = &x->dw_loc_oprnd1;
29275 dw_val_ref valx2 = &x->dw_loc_oprnd2;
29276 dw_val_ref valy1 = &y->dw_loc_oprnd1;
29277 dw_val_ref valy2 = &y->dw_loc_oprnd2;
29279 switch (x->dw_loc_opc)
29281 case DW_OP_const4u:
29282 case DW_OP_const8u:
29283 if (x->dtprel)
29284 goto hash_addr;
29285 /* FALLTHRU */
29286 case DW_OP_const1u:
29287 case DW_OP_const1s:
29288 case DW_OP_const2u:
29289 case DW_OP_const2s:
29290 case DW_OP_const4s:
29291 case DW_OP_const8s:
29292 case DW_OP_constu:
29293 case DW_OP_consts:
29294 case DW_OP_pick:
29295 case DW_OP_plus_uconst:
29296 case DW_OP_breg0:
29297 case DW_OP_breg1:
29298 case DW_OP_breg2:
29299 case DW_OP_breg3:
29300 case DW_OP_breg4:
29301 case DW_OP_breg5:
29302 case DW_OP_breg6:
29303 case DW_OP_breg7:
29304 case DW_OP_breg8:
29305 case DW_OP_breg9:
29306 case DW_OP_breg10:
29307 case DW_OP_breg11:
29308 case DW_OP_breg12:
29309 case DW_OP_breg13:
29310 case DW_OP_breg14:
29311 case DW_OP_breg15:
29312 case DW_OP_breg16:
29313 case DW_OP_breg17:
29314 case DW_OP_breg18:
29315 case DW_OP_breg19:
29316 case DW_OP_breg20:
29317 case DW_OP_breg21:
29318 case DW_OP_breg22:
29319 case DW_OP_breg23:
29320 case DW_OP_breg24:
29321 case DW_OP_breg25:
29322 case DW_OP_breg26:
29323 case DW_OP_breg27:
29324 case DW_OP_breg28:
29325 case DW_OP_breg29:
29326 case DW_OP_breg30:
29327 case DW_OP_breg31:
29328 case DW_OP_regx:
29329 case DW_OP_fbreg:
29330 case DW_OP_piece:
29331 case DW_OP_deref_size:
29332 case DW_OP_xderef_size:
29333 return valx1->v.val_int == valy1->v.val_int;
29334 case DW_OP_skip:
29335 case DW_OP_bra:
29336 /* If splitting debug info, the use of DW_OP_GNU_addr_index
29337 can cause irrelevant differences in dw_loc_addr. */
29338 gcc_assert (valx1->val_class == dw_val_class_loc
29339 && valy1->val_class == dw_val_class_loc
29340 && (dwarf_split_debug_info
29341 || x->dw_loc_addr == y->dw_loc_addr));
29342 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
29343 case DW_OP_implicit_value:
29344 if (valx1->v.val_unsigned != valy1->v.val_unsigned
29345 || valx2->val_class != valy2->val_class)
29346 return false;
29347 switch (valx2->val_class)
29349 case dw_val_class_const:
29350 return valx2->v.val_int == valy2->v.val_int;
29351 case dw_val_class_vec:
29352 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
29353 && valx2->v.val_vec.length == valy2->v.val_vec.length
29354 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
29355 valx2->v.val_vec.elt_size
29356 * valx2->v.val_vec.length) == 0;
29357 case dw_val_class_const_double:
29358 return valx2->v.val_double.low == valy2->v.val_double.low
29359 && valx2->v.val_double.high == valy2->v.val_double.high;
29360 case dw_val_class_wide_int:
29361 return *valx2->v.val_wide == *valy2->v.val_wide;
29362 case dw_val_class_addr:
29363 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
29364 default:
29365 gcc_unreachable ();
29367 case DW_OP_bregx:
29368 case DW_OP_bit_piece:
29369 return valx1->v.val_int == valy1->v.val_int
29370 && valx2->v.val_int == valy2->v.val_int;
29371 case DW_OP_addr:
29372 hash_addr:
29373 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
29374 case DW_OP_GNU_addr_index:
29375 case DW_OP_GNU_const_index:
29377 rtx ax1 = valx1->val_entry->addr.rtl;
29378 rtx ay1 = valy1->val_entry->addr.rtl;
29379 return rtx_equal_p (ax1, ay1);
29381 case DW_OP_implicit_pointer:
29382 case DW_OP_GNU_implicit_pointer:
29383 return valx1->val_class == dw_val_class_die_ref
29384 && valx1->val_class == valy1->val_class
29385 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
29386 && valx2->v.val_int == valy2->v.val_int;
29387 case DW_OP_entry_value:
29388 case DW_OP_GNU_entry_value:
29389 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
29390 case DW_OP_const_type:
29391 case DW_OP_GNU_const_type:
29392 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
29393 || valx2->val_class != valy2->val_class)
29394 return false;
29395 switch (valx2->val_class)
29397 case dw_val_class_const:
29398 return valx2->v.val_int == valy2->v.val_int;
29399 case dw_val_class_vec:
29400 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
29401 && valx2->v.val_vec.length == valy2->v.val_vec.length
29402 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
29403 valx2->v.val_vec.elt_size
29404 * valx2->v.val_vec.length) == 0;
29405 case dw_val_class_const_double:
29406 return valx2->v.val_double.low == valy2->v.val_double.low
29407 && valx2->v.val_double.high == valy2->v.val_double.high;
29408 case dw_val_class_wide_int:
29409 return *valx2->v.val_wide == *valy2->v.val_wide;
29410 default:
29411 gcc_unreachable ();
29413 case DW_OP_regval_type:
29414 case DW_OP_deref_type:
29415 case DW_OP_GNU_regval_type:
29416 case DW_OP_GNU_deref_type:
29417 return valx1->v.val_int == valy1->v.val_int
29418 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
29419 case DW_OP_convert:
29420 case DW_OP_reinterpret:
29421 case DW_OP_GNU_convert:
29422 case DW_OP_GNU_reinterpret:
29423 if (valx1->val_class != valy1->val_class)
29424 return false;
29425 if (valx1->val_class == dw_val_class_unsigned_const)
29426 return valx1->v.val_unsigned == valy1->v.val_unsigned;
29427 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
29428 case DW_OP_GNU_parameter_ref:
29429 return valx1->val_class == dw_val_class_die_ref
29430 && valx1->val_class == valy1->val_class
29431 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
29432 default:
29433 /* Other codes have no operands. */
29434 return true;
29438 /* Return true if DWARF location expressions X and Y are the same. */
29440 static inline bool
29441 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
29443 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
29444 if (x->dw_loc_opc != y->dw_loc_opc
29445 || x->dtprel != y->dtprel
29446 || !compare_loc_operands (x, y))
29447 break;
29448 return x == NULL && y == NULL;
29451 /* Hashtable helpers. */
29453 struct loc_list_hasher : nofree_ptr_hash <dw_loc_list_struct>
29455 static inline hashval_t hash (const dw_loc_list_struct *);
29456 static inline bool equal (const dw_loc_list_struct *,
29457 const dw_loc_list_struct *);
29460 /* Return precomputed hash of location list X. */
29462 inline hashval_t
29463 loc_list_hasher::hash (const dw_loc_list_struct *x)
29465 return x->hash;
29468 /* Return true if location lists A and B are the same. */
29470 inline bool
29471 loc_list_hasher::equal (const dw_loc_list_struct *a,
29472 const dw_loc_list_struct *b)
29474 if (a == b)
29475 return 1;
29476 if (a->hash != b->hash)
29477 return 0;
29478 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
29479 if (strcmp (a->begin, b->begin) != 0
29480 || strcmp (a->end, b->end) != 0
29481 || (a->section == NULL) != (b->section == NULL)
29482 || (a->section && strcmp (a->section, b->section) != 0)
29483 || !compare_locs (a->expr, b->expr))
29484 break;
29485 return a == NULL && b == NULL;
29488 typedef hash_table<loc_list_hasher> loc_list_hash_type;
29491 /* Recursively optimize location lists referenced from DIE
29492 children and share them whenever possible. */
29494 static void
29495 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type *htab)
29497 dw_die_ref c;
29498 dw_attr_node *a;
29499 unsigned ix;
29500 dw_loc_list_struct **slot;
29502 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
29503 if (AT_class (a) == dw_val_class_loc_list)
29505 dw_loc_list_ref list = AT_loc_list (a);
29506 /* TODO: perform some optimizations here, before hashing
29507 it and storing into the hash table. */
29508 hash_loc_list (list);
29509 slot = htab->find_slot_with_hash (list, list->hash, INSERT);
29510 if (*slot == NULL)
29511 *slot = list;
29512 else
29513 a->dw_attr_val.v.val_loc_list = *slot;
29516 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
29520 /* Recursively assign each location list a unique index into the debug_addr
29521 section. */
29523 static void
29524 index_location_lists (dw_die_ref die)
29526 dw_die_ref c;
29527 dw_attr_node *a;
29528 unsigned ix;
29530 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
29531 if (AT_class (a) == dw_val_class_loc_list)
29533 dw_loc_list_ref list = AT_loc_list (a);
29534 dw_loc_list_ref curr;
29535 for (curr = list; curr != NULL; curr = curr->dw_loc_next)
29537 /* Don't index an entry that has already been indexed
29538 or won't be output. */
29539 if (curr->begin_entry != NULL
29540 || (strcmp (curr->begin, curr->end) == 0 && !curr->force))
29541 continue;
29543 curr->begin_entry
29544 = add_addr_table_entry (xstrdup (curr->begin), ate_kind_label);
29548 FOR_EACH_CHILD (die, c, index_location_lists (c));
29551 /* Optimize location lists referenced from DIE
29552 children and share them whenever possible. */
29554 static void
29555 optimize_location_lists (dw_die_ref die)
29557 loc_list_hash_type htab (500);
29558 optimize_location_lists_1 (die, &htab);
29561 /* Traverse the limbo die list, and add parent/child links. The only
29562 dies without parents that should be here are concrete instances of
29563 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
29564 For concrete instances, we can get the parent die from the abstract
29565 instance. */
29567 static void
29568 flush_limbo_die_list (void)
29570 limbo_die_node *node;
29572 /* get_context_die calls force_decl_die, which can put new DIEs on the
29573 limbo list in LTO mode when nested functions are put in a different
29574 partition than that of their parent function. */
29575 while ((node = limbo_die_list))
29577 dw_die_ref die = node->die;
29578 limbo_die_list = node->next;
29580 if (die->die_parent == NULL)
29582 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
29584 if (origin && origin->die_parent)
29585 add_child_die (origin->die_parent, die);
29586 else if (is_cu_die (die))
29588 else if (seen_error ())
29589 /* It's OK to be confused by errors in the input. */
29590 add_child_die (comp_unit_die (), die);
29591 else
29593 /* In certain situations, the lexical block containing a
29594 nested function can be optimized away, which results
29595 in the nested function die being orphaned. Likewise
29596 with the return type of that nested function. Force
29597 this to be a child of the containing function.
29599 It may happen that even the containing function got fully
29600 inlined and optimized out. In that case we are lost and
29601 assign the empty child. This should not be big issue as
29602 the function is likely unreachable too. */
29603 gcc_assert (node->created_for);
29605 if (DECL_P (node->created_for))
29606 origin = get_context_die (DECL_CONTEXT (node->created_for));
29607 else if (TYPE_P (node->created_for))
29608 origin = scope_die_for (node->created_for, comp_unit_die ());
29609 else
29610 origin = comp_unit_die ();
29612 add_child_die (origin, die);
29618 /* Output stuff that dwarf requires at the end of every file,
29619 and generate the DWARF-2 debugging info. */
29621 static void
29622 dwarf2out_finish (const char *)
29624 comdat_type_node *ctnode;
29625 dw_die_ref main_comp_unit_die;
29626 unsigned char checksum[16];
29627 char dl_section_ref[MAX_ARTIFICIAL_LABEL_BYTES];
29629 /* Flush out any latecomers to the limbo party. */
29630 flush_limbo_die_list ();
29632 if (flag_checking)
29634 verify_die (comp_unit_die ());
29635 for (limbo_die_node *node = cu_die_list; node; node = node->next)
29636 verify_die (node->die);
29639 /* We shouldn't have any symbols with delayed asm names for
29640 DIEs generated after early finish. */
29641 gcc_assert (deferred_asm_name == NULL);
29643 gen_remaining_tmpl_value_param_die_attribute ();
29645 #if ENABLE_ASSERT_CHECKING
29647 dw_die_ref die = comp_unit_die (), c;
29648 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
29650 #endif
29651 resolve_addr (comp_unit_die ());
29652 move_marked_base_types ();
29654 /* Initialize sections and labels used for actual assembler output. */
29655 init_sections_and_labels ();
29657 /* Traverse the DIE's and add sibling attributes to those DIE's that
29658 have children. */
29659 add_sibling_attributes (comp_unit_die ());
29660 limbo_die_node *node;
29661 for (node = cu_die_list; node; node = node->next)
29662 add_sibling_attributes (node->die);
29663 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
29664 add_sibling_attributes (ctnode->root_die);
29666 /* When splitting DWARF info, we put some attributes in the
29667 skeleton compile_unit DIE that remains in the .o, while
29668 most attributes go in the DWO compile_unit_die. */
29669 if (dwarf_split_debug_info)
29671 limbo_die_node *cu;
29672 main_comp_unit_die = gen_compile_unit_die (NULL);
29673 if (dwarf_version >= 5)
29674 main_comp_unit_die->die_tag = DW_TAG_skeleton_unit;
29675 cu = limbo_die_list;
29676 gcc_assert (cu->die == main_comp_unit_die);
29677 limbo_die_list = limbo_die_list->next;
29678 cu->next = cu_die_list;
29679 cu_die_list = cu;
29681 else
29682 main_comp_unit_die = comp_unit_die ();
29684 /* Output a terminator label for the .text section. */
29685 switch_to_section (text_section);
29686 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
29687 if (cold_text_section)
29689 switch_to_section (cold_text_section);
29690 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
29693 /* We can only use the low/high_pc attributes if all of the code was
29694 in .text. */
29695 if (!have_multiple_function_sections
29696 || (dwarf_version < 3 && dwarf_strict))
29698 /* Don't add if the CU has no associated code. */
29699 if (text_section_used)
29700 add_AT_low_high_pc (main_comp_unit_die, text_section_label,
29701 text_end_label, true);
29703 else
29705 unsigned fde_idx;
29706 dw_fde_ref fde;
29707 bool range_list_added = false;
29709 if (text_section_used)
29710 add_ranges_by_labels (main_comp_unit_die, text_section_label,
29711 text_end_label, &range_list_added, true);
29712 if (cold_text_section_used)
29713 add_ranges_by_labels (main_comp_unit_die, cold_text_section_label,
29714 cold_end_label, &range_list_added, true);
29716 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
29718 if (DECL_IGNORED_P (fde->decl))
29719 continue;
29720 if (!fde->in_std_section)
29721 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
29722 fde->dw_fde_end, &range_list_added,
29723 true);
29724 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
29725 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
29726 fde->dw_fde_second_end, &range_list_added,
29727 true);
29730 if (range_list_added)
29732 /* We need to give .debug_loc and .debug_ranges an appropriate
29733 "base address". Use zero so that these addresses become
29734 absolute. Historically, we've emitted the unexpected
29735 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
29736 Emit both to give time for other tools to adapt. */
29737 add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
29738 if (! dwarf_strict && dwarf_version < 4)
29739 add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
29741 add_ranges (NULL);
29745 /* AIX Assembler inserts the length, so adjust the reference to match the
29746 offset expected by debuggers. */
29747 strcpy (dl_section_ref, debug_line_section_label);
29748 if (XCOFF_DEBUGGING_INFO)
29749 strcat (dl_section_ref, DWARF_INITIAL_LENGTH_SIZE_STR);
29751 if (debug_info_level >= DINFO_LEVEL_TERSE)
29752 add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
29753 dl_section_ref);
29755 if (have_macinfo)
29756 add_AT_macptr (comp_unit_die (),
29757 dwarf_version >= 5 ? DW_AT_macros
29758 : dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros,
29759 macinfo_section_label);
29761 if (dwarf_split_debug_info)
29763 if (have_location_lists)
29765 if (dwarf_version >= 5)
29766 add_AT_loclistsptr (comp_unit_die (), DW_AT_loclists_base,
29767 loc_section_label);
29768 /* optimize_location_lists calculates the size of the lists,
29769 so index them first, and assign indices to the entries.
29770 Although optimize_location_lists will remove entries from
29771 the table, it only does so for duplicates, and therefore
29772 only reduces ref_counts to 1. */
29773 index_location_lists (comp_unit_die ());
29776 if (addr_index_table != NULL)
29778 unsigned int index = 0;
29779 addr_index_table
29780 ->traverse_noresize<unsigned int *, index_addr_table_entry>
29781 (&index);
29785 loc_list_idx = 0;
29786 if (have_location_lists)
29788 optimize_location_lists (comp_unit_die ());
29789 /* And finally assign indexes to the entries for -gsplit-dwarf. */
29790 if (dwarf_version >= 5 && dwarf_split_debug_info)
29791 assign_location_list_indexes (comp_unit_die ());
29794 save_macinfo_strings ();
29796 if (dwarf_split_debug_info)
29798 unsigned int index = 0;
29800 /* Add attributes common to skeleton compile_units and
29801 type_units. Because these attributes include strings, it
29802 must be done before freezing the string table. Top-level
29803 skeleton die attrs are added when the skeleton type unit is
29804 created, so ensure it is created by this point. */
29805 add_top_level_skeleton_die_attrs (main_comp_unit_die);
29806 debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
29809 /* Output all of the compilation units. We put the main one last so that
29810 the offsets are available to output_pubnames. */
29811 for (node = cu_die_list; node; node = node->next)
29812 output_comp_unit (node->die, 0, NULL);
29814 hash_table<comdat_type_hasher> comdat_type_table (100);
29815 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
29817 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
29819 /* Don't output duplicate types. */
29820 if (*slot != HTAB_EMPTY_ENTRY)
29821 continue;
29823 /* Add a pointer to the line table for the main compilation unit
29824 so that the debugger can make sense of DW_AT_decl_file
29825 attributes. */
29826 if (debug_info_level >= DINFO_LEVEL_TERSE)
29827 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
29828 (!dwarf_split_debug_info
29829 ? dl_section_ref
29830 : debug_skeleton_line_section_label));
29832 output_comdat_type_unit (ctnode);
29833 *slot = ctnode;
29836 /* The AT_pubnames attribute needs to go in all skeleton dies, including
29837 both the main_cu and all skeleton TUs. Making this call unconditional
29838 would end up either adding a second copy of the AT_pubnames attribute, or
29839 requiring a special case in add_top_level_skeleton_die_attrs. */
29840 if (!dwarf_split_debug_info)
29841 add_AT_pubnames (comp_unit_die ());
29843 if (dwarf_split_debug_info)
29845 int mark;
29846 struct md5_ctx ctx;
29848 if (dwarf_version >= 5 && !vec_safe_is_empty (ranges_table))
29849 index_rnglists ();
29851 /* Compute a checksum of the comp_unit to use as the dwo_id. */
29852 md5_init_ctx (&ctx);
29853 mark = 0;
29854 die_checksum (comp_unit_die (), &ctx, &mark);
29855 unmark_all_dies (comp_unit_die ());
29856 md5_finish_ctx (&ctx, checksum);
29858 if (dwarf_version < 5)
29860 /* Use the first 8 bytes of the checksum as the dwo_id,
29861 and add it to both comp-unit DIEs. */
29862 add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
29863 add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
29866 /* Add the base offset of the ranges table to the skeleton
29867 comp-unit DIE. */
29868 if (!vec_safe_is_empty (ranges_table))
29870 if (dwarf_version >= 5)
29871 add_AT_lineptr (main_comp_unit_die, DW_AT_rnglists_base,
29872 ranges_base_label);
29873 else
29874 add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
29875 ranges_section_label);
29878 switch_to_section (debug_addr_section);
29879 ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
29880 output_addr_table ();
29883 /* Output the main compilation unit if non-empty or if .debug_macinfo
29884 or .debug_macro will be emitted. */
29885 output_comp_unit (comp_unit_die (), have_macinfo,
29886 dwarf_split_debug_info ? checksum : NULL);
29888 if (dwarf_split_debug_info && info_section_emitted)
29889 output_skeleton_debug_sections (main_comp_unit_die, checksum);
29891 /* Output the abbreviation table. */
29892 if (vec_safe_length (abbrev_die_table) != 1)
29894 switch_to_section (debug_abbrev_section);
29895 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
29896 output_abbrev_section ();
29899 /* Output location list section if necessary. */
29900 if (have_location_lists)
29902 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
29903 char l2[MAX_ARTIFICIAL_LABEL_BYTES];
29904 /* Output the location lists info. */
29905 switch_to_section (debug_loc_section);
29906 if (dwarf_version >= 5)
29908 ASM_GENERATE_INTERNAL_LABEL (l1, DEBUG_LOC_SECTION_LABEL, 1);
29909 ASM_GENERATE_INTERNAL_LABEL (l2, DEBUG_LOC_SECTION_LABEL, 2);
29910 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
29911 dw2_asm_output_data (4, 0xffffffff,
29912 "Initial length escape value indicating "
29913 "64-bit DWARF extension");
29914 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
29915 "Length of Location Lists");
29916 ASM_OUTPUT_LABEL (asm_out_file, l1);
29917 dw2_asm_output_data (2, dwarf_version, "DWARF Version");
29918 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
29919 dw2_asm_output_data (1, 0, "Segment Size");
29920 dw2_asm_output_data (4, dwarf_split_debug_info ? loc_list_idx : 0,
29921 "Offset Entry Count");
29923 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
29924 if (dwarf_version >= 5 && dwarf_split_debug_info)
29926 unsigned int save_loc_list_idx = loc_list_idx;
29927 loc_list_idx = 0;
29928 output_loclists_offsets (comp_unit_die ());
29929 gcc_assert (save_loc_list_idx == loc_list_idx);
29931 output_location_lists (comp_unit_die ());
29932 if (dwarf_version >= 5)
29933 ASM_OUTPUT_LABEL (asm_out_file, l2);
29936 output_pubtables ();
29938 /* Output the address range information if a CU (.debug_info section)
29939 was emitted. We output an empty table even if we had no functions
29940 to put in it. This because the consumer has no way to tell the
29941 difference between an empty table that we omitted and failure to
29942 generate a table that would have contained data. */
29943 if (info_section_emitted)
29945 switch_to_section (debug_aranges_section);
29946 output_aranges ();
29949 /* Output ranges section if necessary. */
29950 if (!vec_safe_is_empty (ranges_table))
29952 if (dwarf_version >= 5)
29953 output_rnglists ();
29954 else
29955 output_ranges ();
29958 /* Have to end the macro section. */
29959 if (have_macinfo)
29961 switch_to_section (debug_macinfo_section);
29962 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
29963 output_macinfo ();
29964 dw2_asm_output_data (1, 0, "End compilation unit");
29967 /* Output the source line correspondence table. We must do this
29968 even if there is no line information. Otherwise, on an empty
29969 translation unit, we will generate a present, but empty,
29970 .debug_info section. IRIX 6.5 `nm' will then complain when
29971 examining the file. This is done late so that any filenames
29972 used by the debug_info section are marked as 'used'. */
29973 switch_to_section (debug_line_section);
29974 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
29975 if (! DWARF2_ASM_LINE_DEBUG_INFO)
29976 output_line_info (false);
29978 if (dwarf_split_debug_info && info_section_emitted)
29980 switch_to_section (debug_skeleton_line_section);
29981 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
29982 output_line_info (true);
29985 /* If we emitted any indirect strings, output the string table too. */
29986 if (debug_str_hash || skeleton_debug_str_hash)
29987 output_indirect_strings ();
29988 if (debug_line_str_hash)
29990 switch_to_section (debug_line_str_section);
29991 const enum dwarf_form form = DW_FORM_line_strp;
29992 debug_line_str_hash->traverse<enum dwarf_form,
29993 output_indirect_string> (form);
29997 /* Returns a hash value for X (which really is a variable_value_struct). */
29999 inline hashval_t
30000 variable_value_hasher::hash (variable_value_struct *x)
30002 return (hashval_t) x->decl_id;
30005 /* Return nonzero if decl_id of variable_value_struct X is the same as
30006 UID of decl Y. */
30008 inline bool
30009 variable_value_hasher::equal (variable_value_struct *x, tree y)
30011 return x->decl_id == DECL_UID (y);
30014 /* Helper function for resolve_variable_value, handle
30015 DW_OP_GNU_variable_value in one location expression.
30016 Return true if exprloc has been changed into loclist. */
30018 static bool
30019 resolve_variable_value_in_expr (dw_attr_node *a, dw_loc_descr_ref loc)
30021 dw_loc_descr_ref next;
30022 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = next)
30024 next = loc->dw_loc_next;
30025 if (loc->dw_loc_opc != DW_OP_GNU_variable_value
30026 || loc->dw_loc_oprnd1.val_class != dw_val_class_decl_ref)
30027 continue;
30029 tree decl = loc->dw_loc_oprnd1.v.val_decl_ref;
30030 if (DECL_CONTEXT (decl) != current_function_decl)
30031 continue;
30033 dw_die_ref ref = lookup_decl_die (decl);
30034 if (ref)
30036 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30037 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
30038 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
30039 continue;
30041 dw_loc_list_ref l = loc_list_from_tree (decl, 0, NULL);
30042 if (l == NULL)
30043 continue;
30044 if (l->dw_loc_next)
30046 if (AT_class (a) != dw_val_class_loc)
30047 continue;
30048 switch (a->dw_attr)
30050 /* Following attributes allow both exprloc and loclist
30051 classes, so we can change them into a loclist. */
30052 case DW_AT_location:
30053 case DW_AT_string_length:
30054 case DW_AT_return_addr:
30055 case DW_AT_data_member_location:
30056 case DW_AT_frame_base:
30057 case DW_AT_segment:
30058 case DW_AT_static_link:
30059 case DW_AT_use_location:
30060 case DW_AT_vtable_elem_location:
30061 if (prev)
30063 prev->dw_loc_next = NULL;
30064 prepend_loc_descr_to_each (l, AT_loc (a));
30066 if (next)
30067 add_loc_descr_to_each (l, next);
30068 a->dw_attr_val.val_class = dw_val_class_loc_list;
30069 a->dw_attr_val.val_entry = NULL;
30070 a->dw_attr_val.v.val_loc_list = l;
30071 have_location_lists = true;
30072 return true;
30073 /* Following attributes allow both exprloc and reference,
30074 so if the whole expression is DW_OP_GNU_variable_value alone
30075 we could transform it into reference. */
30076 case DW_AT_byte_size:
30077 case DW_AT_bit_size:
30078 case DW_AT_lower_bound:
30079 case DW_AT_upper_bound:
30080 case DW_AT_bit_stride:
30081 case DW_AT_count:
30082 case DW_AT_allocated:
30083 case DW_AT_associated:
30084 case DW_AT_byte_stride:
30085 if (prev == NULL && next == NULL)
30086 break;
30087 /* FALLTHRU */
30088 default:
30089 if (dwarf_strict)
30090 continue;
30091 break;
30093 /* Create DW_TAG_variable that we can refer to. */
30094 gen_decl_die (decl, NULL_TREE, NULL,
30095 lookup_decl_die (current_function_decl));
30096 ref = lookup_decl_die (decl);
30097 if (ref)
30099 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30100 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
30101 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
30103 continue;
30105 if (prev)
30107 prev->dw_loc_next = l->expr;
30108 add_loc_descr (&prev->dw_loc_next, next);
30109 free_loc_descr (loc, NULL);
30110 next = prev->dw_loc_next;
30112 else
30114 memcpy (loc, l->expr, sizeof (dw_loc_descr_node));
30115 add_loc_descr (&loc, next);
30116 next = loc;
30118 loc = prev;
30120 return false;
30123 /* Attempt to resolve DW_OP_GNU_variable_value using loc_list_from_tree. */
30125 static void
30126 resolve_variable_value (dw_die_ref die)
30128 dw_attr_node *a;
30129 dw_loc_list_ref loc;
30130 unsigned ix;
30132 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
30133 switch (AT_class (a))
30135 case dw_val_class_loc:
30136 if (!resolve_variable_value_in_expr (a, AT_loc (a)))
30137 break;
30138 /* FALLTHRU */
30139 case dw_val_class_loc_list:
30140 loc = AT_loc_list (a);
30141 gcc_assert (loc);
30142 for (; loc; loc = loc->dw_loc_next)
30143 resolve_variable_value_in_expr (a, loc->expr);
30144 break;
30145 default:
30146 break;
30150 /* Attempt to optimize DW_OP_GNU_variable_value refering to
30151 temporaries in the current function. */
30153 static void
30154 resolve_variable_values (void)
30156 if (!variable_value_hash || !current_function_decl)
30157 return;
30159 struct variable_value_struct *node
30160 = variable_value_hash->find_with_hash (current_function_decl,
30161 DECL_UID (current_function_decl));
30163 if (node == NULL)
30164 return;
30166 unsigned int i;
30167 dw_die_ref die;
30168 FOR_EACH_VEC_SAFE_ELT (node->dies, i, die)
30169 resolve_variable_value (die);
30172 /* Helper function for note_variable_value, handle one location
30173 expression. */
30175 static void
30176 note_variable_value_in_expr (dw_die_ref die, dw_loc_descr_ref loc)
30178 for (; loc; loc = loc->dw_loc_next)
30179 if (loc->dw_loc_opc == DW_OP_GNU_variable_value
30180 && loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
30182 tree decl = loc->dw_loc_oprnd1.v.val_decl_ref;
30183 dw_die_ref ref = lookup_decl_die (decl);
30184 if (ref)
30186 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30187 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
30188 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
30189 continue;
30191 if (VAR_P (decl)
30192 && DECL_CONTEXT (decl)
30193 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL
30194 && lookup_decl_die (DECL_CONTEXT (decl)))
30196 if (!variable_value_hash)
30197 variable_value_hash
30198 = hash_table<variable_value_hasher>::create_ggc (10);
30200 tree fndecl = DECL_CONTEXT (decl);
30201 struct variable_value_struct *node;
30202 struct variable_value_struct **slot
30203 = variable_value_hash->find_slot_with_hash (fndecl,
30204 DECL_UID (fndecl),
30205 INSERT);
30206 if (*slot == NULL)
30208 node = ggc_cleared_alloc<variable_value_struct> ();
30209 node->decl_id = DECL_UID (fndecl);
30210 *slot = node;
30212 else
30213 node = *slot;
30215 vec_safe_push (node->dies, die);
30220 /* Walk the tree DIE and note DIEs with DW_OP_GNU_variable_value still
30221 with dw_val_class_decl_ref operand. */
30223 static void
30224 note_variable_value (dw_die_ref die)
30226 dw_die_ref c;
30227 dw_attr_node *a;
30228 dw_loc_list_ref loc;
30229 unsigned ix;
30231 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
30232 switch (AT_class (a))
30234 case dw_val_class_loc_list:
30235 loc = AT_loc_list (a);
30236 gcc_assert (loc);
30237 if (!loc->noted_variable_value)
30239 loc->noted_variable_value = 1;
30240 for (; loc; loc = loc->dw_loc_next)
30241 note_variable_value_in_expr (die, loc->expr);
30243 break;
30244 case dw_val_class_loc:
30245 note_variable_value_in_expr (die, AT_loc (a));
30246 break;
30247 default:
30248 break;
30251 /* Mark children. */
30252 FOR_EACH_CHILD (die, c, note_variable_value (c));
30255 /* Perform any cleanups needed after the early debug generation pass
30256 has run. */
30258 static void
30259 dwarf2out_early_finish (const char *filename)
30261 set_early_dwarf s;
30263 /* PCH might result in DW_AT_producer string being restored from the
30264 header compilation, so always fill it with empty string initially
30265 and overwrite only here. */
30266 dw_attr_node *producer = get_AT (comp_unit_die (), DW_AT_producer);
30267 producer_string = gen_producer_string ();
30268 producer->dw_attr_val.v.val_str->refcount--;
30269 producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
30271 /* Add the name for the main input file now. We delayed this from
30272 dwarf2out_init to avoid complications with PCH. */
30273 add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
30274 add_comp_dir_attribute (comp_unit_die ());
30276 /* When emitting DWARF5 .debug_line_str, move DW_AT_name and
30277 DW_AT_comp_dir into .debug_line_str section. */
30278 if (!DWARF2_ASM_LINE_DEBUG_INFO
30279 && dwarf_version >= 5
30280 && DWARF5_USE_DEBUG_LINE_STR)
30282 for (int i = 0; i < 2; i++)
30284 dw_attr_node *a = get_AT (comp_unit_die (),
30285 i ? DW_AT_comp_dir : DW_AT_name);
30286 if (a == NULL
30287 || AT_class (a) != dw_val_class_str
30288 || strlen (AT_string (a)) + 1 <= DWARF_OFFSET_SIZE)
30289 continue;
30291 if (! debug_line_str_hash)
30292 debug_line_str_hash
30293 = hash_table<indirect_string_hasher>::create_ggc (10);
30295 struct indirect_string_node *node
30296 = find_AT_string_in_table (AT_string (a), debug_line_str_hash);
30297 set_indirect_string (node);
30298 node->form = DW_FORM_line_strp;
30299 a->dw_attr_val.v.val_str->refcount--;
30300 a->dw_attr_val.v.val_str = node;
30304 /* With LTO early dwarf was really finished at compile-time, so make
30305 sure to adjust the phase after annotating the LTRANS CU DIE. */
30306 if (in_lto_p)
30308 early_dwarf_finished = true;
30309 return;
30312 /* Walk through the list of incomplete types again, trying once more to
30313 emit full debugging info for them. */
30314 retry_incomplete_types ();
30316 /* The point here is to flush out the limbo list so that it is empty
30317 and we don't need to stream it for LTO. */
30318 flush_limbo_die_list ();
30320 gen_scheduled_generic_parms_dies ();
30321 gen_remaining_tmpl_value_param_die_attribute ();
30323 /* Add DW_AT_linkage_name for all deferred DIEs. */
30324 for (limbo_die_node *node = deferred_asm_name; node; node = node->next)
30326 tree decl = node->created_for;
30327 if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
30328 /* A missing DECL_ASSEMBLER_NAME can be a constant DIE that
30329 ended up in deferred_asm_name before we knew it was
30330 constant and never written to disk. */
30331 && DECL_ASSEMBLER_NAME (decl))
30333 add_linkage_attr (node->die, decl);
30334 move_linkage_attr (node->die);
30337 deferred_asm_name = NULL;
30339 if (flag_eliminate_unused_debug_types)
30340 prune_unused_types ();
30342 /* Generate separate COMDAT sections for type DIEs. */
30343 if (use_debug_types)
30345 break_out_comdat_types (comp_unit_die ());
30347 /* Each new type_unit DIE was added to the limbo die list when created.
30348 Since these have all been added to comdat_type_list, clear the
30349 limbo die list. */
30350 limbo_die_list = NULL;
30352 /* For each new comdat type unit, copy declarations for incomplete
30353 types to make the new unit self-contained (i.e., no direct
30354 references to the main compile unit). */
30355 for (comdat_type_node *ctnode = comdat_type_list;
30356 ctnode != NULL; ctnode = ctnode->next)
30357 copy_decls_for_unworthy_types (ctnode->root_die);
30358 copy_decls_for_unworthy_types (comp_unit_die ());
30360 /* In the process of copying declarations from one unit to another,
30361 we may have left some declarations behind that are no longer
30362 referenced. Prune them. */
30363 prune_unused_types ();
30366 /* Generate separate CUs for each of the include files we've seen.
30367 They will go into limbo_die_list and from there to cu_die_list. */
30368 if (flag_eliminate_dwarf2_dups)
30370 gcc_assert (limbo_die_list == NULL);
30371 break_out_includes (comp_unit_die ());
30372 limbo_die_node *cu;
30373 while ((cu = limbo_die_list))
30375 limbo_die_list = cu->next;
30376 cu->next = cu_die_list;
30377 cu_die_list = cu;
30381 /* Traverse the DIE's and note DIEs with DW_OP_GNU_variable_value still
30382 with dw_val_class_decl_ref operand. */
30383 note_variable_value (comp_unit_die ());
30384 for (limbo_die_node *node = cu_die_list; node; node = node->next)
30385 note_variable_value (node->die);
30386 for (comdat_type_node *ctnode = comdat_type_list; ctnode != NULL;
30387 ctnode = ctnode->next)
30388 note_variable_value (ctnode->root_die);
30389 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
30390 note_variable_value (node->die);
30392 /* The early debug phase is now finished. */
30393 early_dwarf_finished = true;
30396 /* Reset all state within dwarf2out.c so that we can rerun the compiler
30397 within the same process. For use by toplev::finalize. */
30399 void
30400 dwarf2out_c_finalize (void)
30402 last_var_location_insn = NULL;
30403 cached_next_real_insn = NULL;
30404 used_rtx_array = NULL;
30405 incomplete_types = NULL;
30406 decl_scope_table = NULL;
30407 debug_info_section = NULL;
30408 debug_skeleton_info_section = NULL;
30409 debug_abbrev_section = NULL;
30410 debug_skeleton_abbrev_section = NULL;
30411 debug_aranges_section = NULL;
30412 debug_addr_section = NULL;
30413 debug_macinfo_section = NULL;
30414 debug_line_section = NULL;
30415 debug_skeleton_line_section = NULL;
30416 debug_loc_section = NULL;
30417 debug_pubnames_section = NULL;
30418 debug_pubtypes_section = NULL;
30419 debug_str_section = NULL;
30420 debug_line_str_section = NULL;
30421 debug_str_dwo_section = NULL;
30422 debug_str_offsets_section = NULL;
30423 debug_ranges_section = NULL;
30424 debug_frame_section = NULL;
30425 fde_vec = NULL;
30426 debug_str_hash = NULL;
30427 debug_line_str_hash = NULL;
30428 skeleton_debug_str_hash = NULL;
30429 dw2_string_counter = 0;
30430 have_multiple_function_sections = false;
30431 text_section_used = false;
30432 cold_text_section_used = false;
30433 cold_text_section = NULL;
30434 current_unit_personality = NULL;
30436 early_dwarf = false;
30437 early_dwarf_finished = false;
30439 next_die_offset = 0;
30440 single_comp_unit_die = NULL;
30441 comdat_type_list = NULL;
30442 limbo_die_list = NULL;
30443 file_table = NULL;
30444 decl_die_table = NULL;
30445 common_block_die_table = NULL;
30446 decl_loc_table = NULL;
30447 call_arg_locations = NULL;
30448 call_arg_loc_last = NULL;
30449 call_site_count = -1;
30450 tail_call_site_count = -1;
30451 cached_dw_loc_list_table = NULL;
30452 abbrev_die_table = NULL;
30453 delete dwarf_proc_stack_usage_map;
30454 dwarf_proc_stack_usage_map = NULL;
30455 line_info_label_num = 0;
30456 cur_line_info_table = NULL;
30457 text_section_line_info = NULL;
30458 cold_text_section_line_info = NULL;
30459 separate_line_info = NULL;
30460 info_section_emitted = false;
30461 pubname_table = NULL;
30462 pubtype_table = NULL;
30463 macinfo_table = NULL;
30464 ranges_table = NULL;
30465 ranges_by_label = NULL;
30466 rnglist_idx = 0;
30467 have_location_lists = false;
30468 loclabel_num = 0;
30469 poc_label_num = 0;
30470 last_emitted_file = NULL;
30471 label_num = 0;
30472 tmpl_value_parm_die_table = NULL;
30473 generic_type_instances = NULL;
30474 frame_pointer_fb_offset = 0;
30475 frame_pointer_fb_offset_valid = false;
30476 base_types.release ();
30477 XDELETEVEC (producer_string);
30478 producer_string = NULL;
30481 #include "gt-dwarf2out.h"