PR tree-optimization/78496
[official-gcc.git] / gcc / dwarf2out.c
blob98c51576ec2ac43e6d5f13613ee2aa9713ba651a
1 /* Output Dwarf2 format symbol table information from GCC.
2 Copyright (C) 1992-2017 Free Software Foundation, Inc.
3 Contributed by Gary Funck (gary@intrepid.com).
4 Derived from DWARF 1 implementation of Ron Guilmette (rfg@monkeys.com).
5 Extensively modified by Jason Merrill (jason@cygnus.com).
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
23 /* TODO: Emit .debug_line header even when there are no functions, since
24 the file numbers are used by .debug_info. Alternately, leave
25 out locations for types and decls.
26 Avoid talking about ctors and op= for PODs.
27 Factor out common prologue sequences into multiple CIEs. */
29 /* The first part of this file deals with the DWARF 2 frame unwind
30 information, which is also used by the GCC efficient exception handling
31 mechanism. The second part, controlled only by an #ifdef
32 DWARF2_DEBUGGING_INFO, deals with the other DWARF 2 debugging
33 information. */
35 /* DWARF2 Abbreviation Glossary:
37 CFA = Canonical Frame Address
38 a fixed address on the stack which identifies a call frame.
39 We define it to be the value of SP just before the call insn.
40 The CFA register and offset, which may change during the course
41 of the function, are used to calculate its value at runtime.
43 CFI = Call Frame Instruction
44 an instruction for the DWARF2 abstract machine
46 CIE = Common Information Entry
47 information describing information common to one or more FDEs
49 DIE = Debugging Information Entry
51 FDE = Frame Description Entry
52 information describing the stack call frame, in particular,
53 how to restore registers
55 DW_CFA_... = DWARF2 CFA call frame instruction
56 DW_TAG_... = DWARF2 DIE tag */
58 #include "config.h"
59 #include "system.h"
60 #include "coretypes.h"
61 #include "target.h"
62 #include "function.h"
63 #include "rtl.h"
64 #include "tree.h"
65 #include "memmodel.h"
66 #include "tm_p.h"
67 #include "stringpool.h"
68 #include "insn-config.h"
69 #include "ira.h"
70 #include "cgraph.h"
71 #include "diagnostic.h"
72 #include "fold-const.h"
73 #include "stor-layout.h"
74 #include "varasm.h"
75 #include "version.h"
76 #include "flags.h"
77 #include "rtlhash.h"
78 #include "reload.h"
79 #include "output.h"
80 #include "expr.h"
81 #include "dwarf2out.h"
82 #include "dwarf2asm.h"
83 #include "toplev.h"
84 #include "md5.h"
85 #include "tree-pretty-print.h"
86 #include "debug.h"
87 #include "common/common-target.h"
88 #include "langhooks.h"
89 #include "lra.h"
90 #include "dumpfile.h"
91 #include "opts.h"
92 #include "tree-dfa.h"
93 #include "gdb/gdb-index.h"
94 #include "rtl-iter.h"
96 static void dwarf2out_source_line (unsigned int, unsigned int, const char *,
97 int, bool);
98 static rtx_insn *last_var_location_insn;
99 static rtx_insn *cached_next_real_insn;
100 static void dwarf2out_decl (tree);
102 #ifndef XCOFF_DEBUGGING_INFO
103 #define XCOFF_DEBUGGING_INFO 0
104 #endif
106 #ifndef HAVE_XCOFF_DWARF_EXTRAS
107 #define HAVE_XCOFF_DWARF_EXTRAS 0
108 #endif
110 #ifdef VMS_DEBUGGING_INFO
111 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
113 /* Define this macro to be a nonzero value if the directory specifications
114 which are output in the debug info should end with a separator. */
115 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
116 /* Define this macro to evaluate to a nonzero value if GCC should refrain
117 from generating indirect strings in DWARF2 debug information, for instance
118 if your target is stuck with an old version of GDB that is unable to
119 process them properly or uses VMS Debug. */
120 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
121 #else
122 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
123 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
124 #endif
126 /* ??? Poison these here until it can be done generically. They've been
127 totally replaced in this file; make sure it stays that way. */
128 #undef DWARF2_UNWIND_INFO
129 #undef DWARF2_FRAME_INFO
130 #if (GCC_VERSION >= 3000)
131 #pragma GCC poison DWARF2_UNWIND_INFO DWARF2_FRAME_INFO
132 #endif
134 /* The size of the target's pointer type. */
135 #ifndef PTR_SIZE
136 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
137 #endif
139 /* Array of RTXes referenced by the debugging information, which therefore
140 must be kept around forever. */
141 static GTY(()) vec<rtx, va_gc> *used_rtx_array;
143 /* A pointer to the base of a list of incomplete types which might be
144 completed at some later time. incomplete_types_list needs to be a
145 vec<tree, va_gc> *because we want to tell the garbage collector about
146 it. */
147 static GTY(()) vec<tree, va_gc> *incomplete_types;
149 /* A pointer to the base of a table of references to declaration
150 scopes. This table is a display which tracks the nesting
151 of declaration scopes at the current scope and containing
152 scopes. This table is used to find the proper place to
153 define type declaration DIE's. */
154 static GTY(()) vec<tree, va_gc> *decl_scope_table;
156 /* Pointers to various DWARF2 sections. */
157 static GTY(()) section *debug_info_section;
158 static GTY(()) section *debug_skeleton_info_section;
159 static GTY(()) section *debug_abbrev_section;
160 static GTY(()) section *debug_skeleton_abbrev_section;
161 static GTY(()) section *debug_aranges_section;
162 static GTY(()) section *debug_addr_section;
163 static GTY(()) section *debug_macinfo_section;
164 static const char *debug_macinfo_section_name;
165 static GTY(()) section *debug_line_section;
166 static GTY(()) section *debug_skeleton_line_section;
167 static GTY(()) section *debug_loc_section;
168 static GTY(()) section *debug_pubnames_section;
169 static GTY(()) section *debug_pubtypes_section;
170 static GTY(()) section *debug_str_section;
171 static GTY(()) section *debug_line_str_section;
172 static GTY(()) section *debug_str_dwo_section;
173 static GTY(()) section *debug_str_offsets_section;
174 static GTY(()) section *debug_ranges_section;
175 static GTY(()) section *debug_frame_section;
177 /* Maximum size (in bytes) of an artificially generated label. */
178 #define MAX_ARTIFICIAL_LABEL_BYTES 30
180 /* According to the (draft) DWARF 3 specification, the initial length
181 should either be 4 or 12 bytes. When it's 12 bytes, the first 4
182 bytes are 0xffffffff, followed by the length stored in the next 8
183 bytes.
185 However, the SGI/MIPS ABI uses an initial length which is equal to
186 DWARF_OFFSET_SIZE. It is defined (elsewhere) accordingly. */
188 #ifndef DWARF_INITIAL_LENGTH_SIZE
189 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
190 #endif
192 /* Round SIZE up to the nearest BOUNDARY. */
193 #define DWARF_ROUND(SIZE,BOUNDARY) \
194 ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
196 /* CIE identifier. */
197 #if HOST_BITS_PER_WIDE_INT >= 64
198 #define DWARF_CIE_ID \
199 (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
200 #else
201 #define DWARF_CIE_ID DW_CIE_ID
202 #endif
205 /* A vector for a table that contains frame description
206 information for each routine. */
207 #define NOT_INDEXED (-1U)
208 #define NO_INDEX_ASSIGNED (-2U)
210 static GTY(()) vec<dw_fde_ref, va_gc> *fde_vec;
212 struct GTY((for_user)) indirect_string_node {
213 const char *str;
214 unsigned int refcount;
215 enum dwarf_form form;
216 char *label;
217 unsigned int index;
220 struct indirect_string_hasher : ggc_ptr_hash<indirect_string_node>
222 typedef const char *compare_type;
224 static hashval_t hash (indirect_string_node *);
225 static bool equal (indirect_string_node *, const char *);
228 static GTY (()) hash_table<indirect_string_hasher> *debug_str_hash;
230 static GTY (()) hash_table<indirect_string_hasher> *debug_line_str_hash;
232 /* With split_debug_info, both the comp_dir and dwo_name go in the
233 main object file, rather than the dwo, similar to the force_direct
234 parameter elsewhere but with additional complications:
236 1) The string is needed in both the main object file and the dwo.
237 That is, the comp_dir and dwo_name will appear in both places.
239 2) Strings can use four forms: DW_FORM_string, DW_FORM_strp,
240 DW_FORM_line_strp or DW_FORM_GNU_str_index.
242 3) GCC chooses the form to use late, depending on the size and
243 reference count.
245 Rather than forcing the all debug string handling functions and
246 callers to deal with these complications, simply use a separate,
247 special-cased string table for any attribute that should go in the
248 main object file. This limits the complexity to just the places
249 that need it. */
251 static GTY (()) hash_table<indirect_string_hasher> *skeleton_debug_str_hash;
253 static GTY(()) int dw2_string_counter;
255 /* True if the compilation unit places functions in more than one section. */
256 static GTY(()) bool have_multiple_function_sections = false;
258 /* Whether the default text and cold text sections have been used at all. */
260 static GTY(()) bool text_section_used = false;
261 static GTY(()) bool cold_text_section_used = false;
263 /* The default cold text section. */
264 static GTY(()) section *cold_text_section;
266 /* The DIE for C++14 'auto' in a function return type. */
267 static GTY(()) dw_die_ref auto_die;
269 /* The DIE for C++14 'decltype(auto)' in a function return type. */
270 static GTY(()) dw_die_ref decltype_auto_die;
272 /* Forward declarations for functions defined in this file. */
274 static void output_call_frame_info (int);
275 static void dwarf2out_note_section_used (void);
277 /* Personality decl of current unit. Used only when assembler does not support
278 personality CFI. */
279 static GTY(()) rtx current_unit_personality;
281 /* .debug_rnglists next index. */
282 static unsigned int rnglist_idx;
284 /* Data and reference forms for relocatable data. */
285 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
286 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
288 #ifndef DEBUG_FRAME_SECTION
289 #define DEBUG_FRAME_SECTION ".debug_frame"
290 #endif
292 #ifndef FUNC_BEGIN_LABEL
293 #define FUNC_BEGIN_LABEL "LFB"
294 #endif
296 #ifndef FUNC_END_LABEL
297 #define FUNC_END_LABEL "LFE"
298 #endif
300 #ifndef PROLOGUE_END_LABEL
301 #define PROLOGUE_END_LABEL "LPE"
302 #endif
304 #ifndef EPILOGUE_BEGIN_LABEL
305 #define EPILOGUE_BEGIN_LABEL "LEB"
306 #endif
308 #ifndef FRAME_BEGIN_LABEL
309 #define FRAME_BEGIN_LABEL "Lframe"
310 #endif
311 #define CIE_AFTER_SIZE_LABEL "LSCIE"
312 #define CIE_END_LABEL "LECIE"
313 #define FDE_LABEL "LSFDE"
314 #define FDE_AFTER_SIZE_LABEL "LASFDE"
315 #define FDE_END_LABEL "LEFDE"
316 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
317 #define LINE_NUMBER_END_LABEL "LELT"
318 #define LN_PROLOG_AS_LABEL "LASLTP"
319 #define LN_PROLOG_END_LABEL "LELTP"
320 #define DIE_LABEL_PREFIX "DW"
322 /* Match the base name of a file to the base name of a compilation unit. */
324 static int
325 matches_main_base (const char *path)
327 /* Cache the last query. */
328 static const char *last_path = NULL;
329 static int last_match = 0;
330 if (path != last_path)
332 const char *base;
333 int length = base_of_path (path, &base);
334 last_path = path;
335 last_match = (length == main_input_baselength
336 && memcmp (base, main_input_basename, length) == 0);
338 return last_match;
341 #ifdef DEBUG_DEBUG_STRUCT
343 static int
344 dump_struct_debug (tree type, enum debug_info_usage usage,
345 enum debug_struct_file criterion, int generic,
346 int matches, int result)
348 /* Find the type name. */
349 tree type_decl = TYPE_STUB_DECL (type);
350 tree t = type_decl;
351 const char *name = 0;
352 if (TREE_CODE (t) == TYPE_DECL)
353 t = DECL_NAME (t);
354 if (t)
355 name = IDENTIFIER_POINTER (t);
357 fprintf (stderr, " struct %d %s %s %s %s %d %p %s\n",
358 criterion,
359 DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
360 matches ? "bas" : "hdr",
361 generic ? "gen" : "ord",
362 usage == DINFO_USAGE_DFN ? ";" :
363 usage == DINFO_USAGE_DIR_USE ? "." : "*",
364 result,
365 (void*) type_decl, name);
366 return result;
368 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
369 dump_struct_debug (type, usage, criterion, generic, matches, result)
371 #else
373 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
374 (result)
376 #endif
378 /* Get the number of HOST_WIDE_INTs needed to represent the precision
379 of the number. Some constants have a large uniform precision, so
380 we get the precision needed for the actual value of the number. */
382 static unsigned int
383 get_full_len (const wide_int &op)
385 int prec = wi::min_precision (op, UNSIGNED);
386 return ((prec + HOST_BITS_PER_WIDE_INT - 1)
387 / HOST_BITS_PER_WIDE_INT);
390 static bool
391 should_emit_struct_debug (tree type, enum debug_info_usage usage)
393 enum debug_struct_file criterion;
394 tree type_decl;
395 bool generic = lang_hooks.types.generic_p (type);
397 if (generic)
398 criterion = debug_struct_generic[usage];
399 else
400 criterion = debug_struct_ordinary[usage];
402 if (criterion == DINFO_STRUCT_FILE_NONE)
403 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
404 if (criterion == DINFO_STRUCT_FILE_ANY)
405 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
407 type_decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type));
409 if (type_decl != NULL)
411 if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
412 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
414 if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
415 return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
418 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
421 /* Switch [BACK] to eh_frame_section. If we don't have an eh_frame_section,
422 switch to the data section instead, and write out a synthetic start label
423 for collect2 the first time around. */
425 static void
426 switch_to_eh_frame_section (bool back ATTRIBUTE_UNUSED)
428 if (eh_frame_section == 0)
430 int flags;
432 if (EH_TABLES_CAN_BE_READ_ONLY)
434 int fde_encoding;
435 int per_encoding;
436 int lsda_encoding;
438 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
439 /*global=*/0);
440 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
441 /*global=*/1);
442 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
443 /*global=*/0);
444 flags = ((! flag_pic
445 || ((fde_encoding & 0x70) != DW_EH_PE_absptr
446 && (fde_encoding & 0x70) != DW_EH_PE_aligned
447 && (per_encoding & 0x70) != DW_EH_PE_absptr
448 && (per_encoding & 0x70) != DW_EH_PE_aligned
449 && (lsda_encoding & 0x70) != DW_EH_PE_absptr
450 && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
451 ? 0 : SECTION_WRITE);
453 else
454 flags = SECTION_WRITE;
456 #ifdef EH_FRAME_SECTION_NAME
457 eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
458 #else
459 eh_frame_section = ((flags == SECTION_WRITE)
460 ? data_section : readonly_data_section);
461 #endif /* EH_FRAME_SECTION_NAME */
464 switch_to_section (eh_frame_section);
466 #ifdef EH_FRAME_THROUGH_COLLECT2
467 /* We have no special eh_frame section. Emit special labels to guide
468 collect2. */
469 if (!back)
471 tree label = get_file_function_name ("F");
472 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
473 targetm.asm_out.globalize_label (asm_out_file,
474 IDENTIFIER_POINTER (label));
475 ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
477 #endif
480 /* Switch [BACK] to the eh or debug frame table section, depending on
481 FOR_EH. */
483 static void
484 switch_to_frame_table_section (int for_eh, bool back)
486 if (for_eh)
487 switch_to_eh_frame_section (back);
488 else
490 if (!debug_frame_section)
491 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
492 SECTION_DEBUG, NULL);
493 switch_to_section (debug_frame_section);
497 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used. */
499 enum dw_cfi_oprnd_type
500 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
502 switch (cfi)
504 case DW_CFA_nop:
505 case DW_CFA_GNU_window_save:
506 case DW_CFA_remember_state:
507 case DW_CFA_restore_state:
508 return dw_cfi_oprnd_unused;
510 case DW_CFA_set_loc:
511 case DW_CFA_advance_loc1:
512 case DW_CFA_advance_loc2:
513 case DW_CFA_advance_loc4:
514 case DW_CFA_MIPS_advance_loc8:
515 return dw_cfi_oprnd_addr;
517 case DW_CFA_offset:
518 case DW_CFA_offset_extended:
519 case DW_CFA_def_cfa:
520 case DW_CFA_offset_extended_sf:
521 case DW_CFA_def_cfa_sf:
522 case DW_CFA_restore:
523 case DW_CFA_restore_extended:
524 case DW_CFA_undefined:
525 case DW_CFA_same_value:
526 case DW_CFA_def_cfa_register:
527 case DW_CFA_register:
528 case DW_CFA_expression:
529 case DW_CFA_val_expression:
530 return dw_cfi_oprnd_reg_num;
532 case DW_CFA_def_cfa_offset:
533 case DW_CFA_GNU_args_size:
534 case DW_CFA_def_cfa_offset_sf:
535 return dw_cfi_oprnd_offset;
537 case DW_CFA_def_cfa_expression:
538 return dw_cfi_oprnd_loc;
540 default:
541 gcc_unreachable ();
545 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used. */
547 enum dw_cfi_oprnd_type
548 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
550 switch (cfi)
552 case DW_CFA_def_cfa:
553 case DW_CFA_def_cfa_sf:
554 case DW_CFA_offset:
555 case DW_CFA_offset_extended_sf:
556 case DW_CFA_offset_extended:
557 return dw_cfi_oprnd_offset;
559 case DW_CFA_register:
560 return dw_cfi_oprnd_reg_num;
562 case DW_CFA_expression:
563 case DW_CFA_val_expression:
564 return dw_cfi_oprnd_loc;
566 default:
567 return dw_cfi_oprnd_unused;
571 /* Output one FDE. */
573 static void
574 output_fde (dw_fde_ref fde, bool for_eh, bool second,
575 char *section_start_label, int fde_encoding, char *augmentation,
576 bool any_lsda_needed, int lsda_encoding)
578 const char *begin, *end;
579 static unsigned int j;
580 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
582 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
583 /* empty */ 0);
584 targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
585 for_eh + j);
586 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
587 ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
588 if (!XCOFF_DEBUGGING_INFO || for_eh)
590 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
591 dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
592 " indicating 64-bit DWARF extension");
593 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
594 "FDE Length");
596 ASM_OUTPUT_LABEL (asm_out_file, l1);
598 if (for_eh)
599 dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
600 else
601 dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
602 debug_frame_section, "FDE CIE offset");
604 begin = second ? fde->dw_fde_second_begin : fde->dw_fde_begin;
605 end = second ? fde->dw_fde_second_end : fde->dw_fde_end;
607 if (for_eh)
609 rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
610 SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
611 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
612 "FDE initial location");
613 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
614 end, begin, "FDE address range");
616 else
618 dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
619 dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
622 if (augmentation[0])
624 if (any_lsda_needed)
626 int size = size_of_encoded_value (lsda_encoding);
628 if (lsda_encoding == DW_EH_PE_aligned)
630 int offset = ( 4 /* Length */
631 + 4 /* CIE offset */
632 + 2 * size_of_encoded_value (fde_encoding)
633 + 1 /* Augmentation size */ );
634 int pad = -offset & (PTR_SIZE - 1);
636 size += pad;
637 gcc_assert (size_of_uleb128 (size) == 1);
640 dw2_asm_output_data_uleb128 (size, "Augmentation size");
642 if (fde->uses_eh_lsda)
644 ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
645 fde->funcdef_number);
646 dw2_asm_output_encoded_addr_rtx (lsda_encoding,
647 gen_rtx_SYMBOL_REF (Pmode, l1),
648 false,
649 "Language Specific Data Area");
651 else
653 if (lsda_encoding == DW_EH_PE_aligned)
654 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
655 dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
656 "Language Specific Data Area (none)");
659 else
660 dw2_asm_output_data_uleb128 (0, "Augmentation size");
663 /* Loop through the Call Frame Instructions associated with this FDE. */
664 fde->dw_fde_current_label = begin;
666 size_t from, until, i;
668 from = 0;
669 until = vec_safe_length (fde->dw_fde_cfi);
671 if (fde->dw_fde_second_begin == NULL)
673 else if (!second)
674 until = fde->dw_fde_switch_cfi_index;
675 else
676 from = fde->dw_fde_switch_cfi_index;
678 for (i = from; i < until; i++)
679 output_cfi ((*fde->dw_fde_cfi)[i], fde, for_eh);
682 /* If we are to emit a ref/link from function bodies to their frame tables,
683 do it now. This is typically performed to make sure that tables
684 associated with functions are dragged with them and not discarded in
685 garbage collecting links. We need to do this on a per function basis to
686 cope with -ffunction-sections. */
688 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
689 /* Switch to the function section, emit the ref to the tables, and
690 switch *back* into the table section. */
691 switch_to_section (function_section (fde->decl));
692 ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
693 switch_to_frame_table_section (for_eh, true);
694 #endif
696 /* Pad the FDE out to an address sized boundary. */
697 ASM_OUTPUT_ALIGN (asm_out_file,
698 floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
699 ASM_OUTPUT_LABEL (asm_out_file, l2);
701 j += 2;
704 /* Return true if frame description entry FDE is needed for EH. */
706 static bool
707 fde_needed_for_eh_p (dw_fde_ref fde)
709 if (flag_asynchronous_unwind_tables)
710 return true;
712 if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
713 return true;
715 if (fde->uses_eh_lsda)
716 return true;
718 /* If exceptions are enabled, we have collected nothrow info. */
719 if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
720 return false;
722 return true;
725 /* Output the call frame information used to record information
726 that relates to calculating the frame pointer, and records the
727 location of saved registers. */
729 static void
730 output_call_frame_info (int for_eh)
732 unsigned int i;
733 dw_fde_ref fde;
734 dw_cfi_ref cfi;
735 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
736 char section_start_label[MAX_ARTIFICIAL_LABEL_BYTES];
737 bool any_lsda_needed = false;
738 char augmentation[6];
739 int augmentation_size;
740 int fde_encoding = DW_EH_PE_absptr;
741 int per_encoding = DW_EH_PE_absptr;
742 int lsda_encoding = DW_EH_PE_absptr;
743 int return_reg;
744 rtx personality = NULL;
745 int dw_cie_version;
747 /* Don't emit a CIE if there won't be any FDEs. */
748 if (!fde_vec)
749 return;
751 /* Nothing to do if the assembler's doing it all. */
752 if (dwarf2out_do_cfi_asm ())
753 return;
755 /* If we don't have any functions we'll want to unwind out of, don't emit
756 any EH unwind information. If we make FDEs linkonce, we may have to
757 emit an empty label for an FDE that wouldn't otherwise be emitted. We
758 want to avoid having an FDE kept around when the function it refers to
759 is discarded. Example where this matters: a primary function template
760 in C++ requires EH information, an explicit specialization doesn't. */
761 if (for_eh)
763 bool any_eh_needed = false;
765 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
767 if (fde->uses_eh_lsda)
768 any_eh_needed = any_lsda_needed = true;
769 else if (fde_needed_for_eh_p (fde))
770 any_eh_needed = true;
771 else if (TARGET_USES_WEAK_UNWIND_INFO)
772 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, 1, 1);
775 if (!any_eh_needed)
776 return;
779 /* We're going to be generating comments, so turn on app. */
780 if (flag_debug_asm)
781 app_enable ();
783 /* Switch to the proper frame section, first time. */
784 switch_to_frame_table_section (for_eh, false);
786 ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
787 ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
789 /* Output the CIE. */
790 ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
791 ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
792 if (!XCOFF_DEBUGGING_INFO || for_eh)
794 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
795 dw2_asm_output_data (4, 0xffffffff,
796 "Initial length escape value indicating 64-bit DWARF extension");
797 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
798 "Length of Common Information Entry");
800 ASM_OUTPUT_LABEL (asm_out_file, l1);
802 /* Now that the CIE pointer is PC-relative for EH,
803 use 0 to identify the CIE. */
804 dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
805 (for_eh ? 0 : DWARF_CIE_ID),
806 "CIE Identifier Tag");
808 /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
809 use CIE version 1, unless that would produce incorrect results
810 due to overflowing the return register column. */
811 return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
812 dw_cie_version = 1;
813 if (return_reg >= 256 || dwarf_version > 2)
814 dw_cie_version = 3;
815 dw2_asm_output_data (1, dw_cie_version, "CIE Version");
817 augmentation[0] = 0;
818 augmentation_size = 0;
820 personality = current_unit_personality;
821 if (for_eh)
823 char *p;
825 /* Augmentation:
826 z Indicates that a uleb128 is present to size the
827 augmentation section.
828 L Indicates the encoding (and thus presence) of
829 an LSDA pointer in the FDE augmentation.
830 R Indicates a non-default pointer encoding for
831 FDE code pointers.
832 P Indicates the presence of an encoding + language
833 personality routine in the CIE augmentation. */
835 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
836 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
837 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
839 p = augmentation + 1;
840 if (personality)
842 *p++ = 'P';
843 augmentation_size += 1 + size_of_encoded_value (per_encoding);
844 assemble_external_libcall (personality);
846 if (any_lsda_needed)
848 *p++ = 'L';
849 augmentation_size += 1;
851 if (fde_encoding != DW_EH_PE_absptr)
853 *p++ = 'R';
854 augmentation_size += 1;
856 if (p > augmentation + 1)
858 augmentation[0] = 'z';
859 *p = '\0';
862 /* Ug. Some platforms can't do unaligned dynamic relocations at all. */
863 if (personality && per_encoding == DW_EH_PE_aligned)
865 int offset = ( 4 /* Length */
866 + 4 /* CIE Id */
867 + 1 /* CIE version */
868 + strlen (augmentation) + 1 /* Augmentation */
869 + size_of_uleb128 (1) /* Code alignment */
870 + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
871 + 1 /* RA column */
872 + 1 /* Augmentation size */
873 + 1 /* Personality encoding */ );
874 int pad = -offset & (PTR_SIZE - 1);
876 augmentation_size += pad;
878 /* Augmentations should be small, so there's scarce need to
879 iterate for a solution. Die if we exceed one uleb128 byte. */
880 gcc_assert (size_of_uleb128 (augmentation_size) == 1);
884 dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
885 if (dw_cie_version >= 4)
887 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
888 dw2_asm_output_data (1, 0, "CIE Segment Size");
890 dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
891 dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
892 "CIE Data Alignment Factor");
894 if (dw_cie_version == 1)
895 dw2_asm_output_data (1, return_reg, "CIE RA Column");
896 else
897 dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
899 if (augmentation[0])
901 dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
902 if (personality)
904 dw2_asm_output_data (1, per_encoding, "Personality (%s)",
905 eh_data_format_name (per_encoding));
906 dw2_asm_output_encoded_addr_rtx (per_encoding,
907 personality,
908 true, NULL);
911 if (any_lsda_needed)
912 dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
913 eh_data_format_name (lsda_encoding));
915 if (fde_encoding != DW_EH_PE_absptr)
916 dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
917 eh_data_format_name (fde_encoding));
920 FOR_EACH_VEC_ELT (*cie_cfi_vec, i, cfi)
921 output_cfi (cfi, NULL, for_eh);
923 /* Pad the CIE out to an address sized boundary. */
924 ASM_OUTPUT_ALIGN (asm_out_file,
925 floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
926 ASM_OUTPUT_LABEL (asm_out_file, l2);
928 /* Loop through all of the FDE's. */
929 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
931 unsigned int k;
933 /* Don't emit EH unwind info for leaf functions that don't need it. */
934 if (for_eh && !fde_needed_for_eh_p (fde))
935 continue;
937 for (k = 0; k < (fde->dw_fde_second_begin ? 2 : 1); k++)
938 output_fde (fde, for_eh, k, section_start_label, fde_encoding,
939 augmentation, any_lsda_needed, lsda_encoding);
942 if (for_eh && targetm.terminate_dw2_eh_frame_info)
943 dw2_asm_output_data (4, 0, "End of Table");
945 /* Turn off app to make assembly quicker. */
946 if (flag_debug_asm)
947 app_disable ();
950 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed. */
952 static void
953 dwarf2out_do_cfi_startproc (bool second)
955 int enc;
956 rtx ref;
957 rtx personality = get_personality_function (current_function_decl);
959 fprintf (asm_out_file, "\t.cfi_startproc\n");
961 if (personality)
963 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
964 ref = personality;
966 /* ??? The GAS support isn't entirely consistent. We have to
967 handle indirect support ourselves, but PC-relative is done
968 in the assembler. Further, the assembler can't handle any
969 of the weirder relocation types. */
970 if (enc & DW_EH_PE_indirect)
971 ref = dw2_force_const_mem (ref, true);
973 fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
974 output_addr_const (asm_out_file, ref);
975 fputc ('\n', asm_out_file);
978 if (crtl->uses_eh_lsda)
980 char lab[MAX_ARTIFICIAL_LABEL_BYTES];
982 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
983 ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
984 current_function_funcdef_no);
985 ref = gen_rtx_SYMBOL_REF (Pmode, lab);
986 SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
988 if (enc & DW_EH_PE_indirect)
989 ref = dw2_force_const_mem (ref, true);
991 fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
992 output_addr_const (asm_out_file, ref);
993 fputc ('\n', asm_out_file);
997 /* Allocate CURRENT_FDE. Immediately initialize all we can, noting that
998 this allocation may be done before pass_final. */
1000 dw_fde_ref
1001 dwarf2out_alloc_current_fde (void)
1003 dw_fde_ref fde;
1005 fde = ggc_cleared_alloc<dw_fde_node> ();
1006 fde->decl = current_function_decl;
1007 fde->funcdef_number = current_function_funcdef_no;
1008 fde->fde_index = vec_safe_length (fde_vec);
1009 fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
1010 fde->uses_eh_lsda = crtl->uses_eh_lsda;
1011 fde->nothrow = crtl->nothrow;
1012 fde->drap_reg = INVALID_REGNUM;
1013 fde->vdrap_reg = INVALID_REGNUM;
1015 /* Record the FDE associated with this function. */
1016 cfun->fde = fde;
1017 vec_safe_push (fde_vec, fde);
1019 return fde;
1022 /* Output a marker (i.e. a label) for the beginning of a function, before
1023 the prologue. */
1025 void
1026 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
1027 unsigned int column ATTRIBUTE_UNUSED,
1028 const char *file ATTRIBUTE_UNUSED)
1030 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1031 char * dup_label;
1032 dw_fde_ref fde;
1033 section *fnsec;
1034 bool do_frame;
1036 current_function_func_begin_label = NULL;
1038 do_frame = dwarf2out_do_frame ();
1040 /* ??? current_function_func_begin_label is also used by except.c for
1041 call-site information. We must emit this label if it might be used. */
1042 if (!do_frame
1043 && (!flag_exceptions
1044 || targetm_common.except_unwind_info (&global_options) == UI_SJLJ))
1045 return;
1047 fnsec = function_section (current_function_decl);
1048 switch_to_section (fnsec);
1049 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
1050 current_function_funcdef_no);
1051 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
1052 current_function_funcdef_no);
1053 dup_label = xstrdup (label);
1054 current_function_func_begin_label = dup_label;
1056 /* We can elide the fde allocation if we're not emitting debug info. */
1057 if (!do_frame)
1058 return;
1060 /* Cater to the various TARGET_ASM_OUTPUT_MI_THUNK implementations that
1061 emit insns as rtx but bypass the bulk of rest_of_compilation, which
1062 would include pass_dwarf2_frame. If we've not created the FDE yet,
1063 do so now. */
1064 fde = cfun->fde;
1065 if (fde == NULL)
1066 fde = dwarf2out_alloc_current_fde ();
1068 /* Initialize the bits of CURRENT_FDE that were not available earlier. */
1069 fde->dw_fde_begin = dup_label;
1070 fde->dw_fde_current_label = dup_label;
1071 fde->in_std_section = (fnsec == text_section
1072 || (cold_text_section && fnsec == cold_text_section));
1074 /* We only want to output line number information for the genuine dwarf2
1075 prologue case, not the eh frame case. */
1076 #ifdef DWARF2_DEBUGGING_INFO
1077 if (file)
1078 dwarf2out_source_line (line, column, file, 0, true);
1079 #endif
1081 if (dwarf2out_do_cfi_asm ())
1082 dwarf2out_do_cfi_startproc (false);
1083 else
1085 rtx personality = get_personality_function (current_function_decl);
1086 if (!current_unit_personality)
1087 current_unit_personality = personality;
1089 /* We cannot keep a current personality per function as without CFI
1090 asm, at the point where we emit the CFI data, there is no current
1091 function anymore. */
1092 if (personality && current_unit_personality != personality)
1093 sorry ("multiple EH personalities are supported only with assemblers "
1094 "supporting .cfi_personality directive");
1098 /* Output a marker (i.e. a label) for the end of the generated code
1099 for a function prologue. This gets called *after* the prologue code has
1100 been generated. */
1102 void
1103 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
1104 const char *file ATTRIBUTE_UNUSED)
1106 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1108 /* Output a label to mark the endpoint of the code generated for this
1109 function. */
1110 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
1111 current_function_funcdef_no);
1112 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
1113 current_function_funcdef_no);
1114 cfun->fde->dw_fde_vms_end_prologue = xstrdup (label);
1117 /* Output a marker (i.e. a label) for the beginning of the generated code
1118 for a function epilogue. This gets called *before* the prologue code has
1119 been generated. */
1121 void
1122 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1123 const char *file ATTRIBUTE_UNUSED)
1125 dw_fde_ref fde = cfun->fde;
1126 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1128 if (fde->dw_fde_vms_begin_epilogue)
1129 return;
1131 /* Output a label to mark the endpoint of the code generated for this
1132 function. */
1133 ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
1134 current_function_funcdef_no);
1135 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
1136 current_function_funcdef_no);
1137 fde->dw_fde_vms_begin_epilogue = xstrdup (label);
1140 /* Output a marker (i.e. a label) for the absolute end of the generated code
1141 for a function definition. This gets called *after* the epilogue code has
1142 been generated. */
1144 void
1145 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1146 const char *file ATTRIBUTE_UNUSED)
1148 dw_fde_ref fde;
1149 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1151 last_var_location_insn = NULL;
1152 cached_next_real_insn = NULL;
1154 if (dwarf2out_do_cfi_asm ())
1155 fprintf (asm_out_file, "\t.cfi_endproc\n");
1157 /* Output a label to mark the endpoint of the code generated for this
1158 function. */
1159 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
1160 current_function_funcdef_no);
1161 ASM_OUTPUT_LABEL (asm_out_file, label);
1162 fde = cfun->fde;
1163 gcc_assert (fde != NULL);
1164 if (fde->dw_fde_second_begin == NULL)
1165 fde->dw_fde_end = xstrdup (label);
1168 void
1169 dwarf2out_frame_finish (void)
1171 /* Output call frame information. */
1172 if (targetm.debug_unwind_info () == UI_DWARF2)
1173 output_call_frame_info (0);
1175 /* Output another copy for the unwinder. */
1176 if ((flag_unwind_tables || flag_exceptions)
1177 && targetm_common.except_unwind_info (&global_options) == UI_DWARF2)
1178 output_call_frame_info (1);
1181 /* Note that the current function section is being used for code. */
1183 static void
1184 dwarf2out_note_section_used (void)
1186 section *sec = current_function_section ();
1187 if (sec == text_section)
1188 text_section_used = true;
1189 else if (sec == cold_text_section)
1190 cold_text_section_used = true;
1193 static void var_location_switch_text_section (void);
1194 static void set_cur_line_info_table (section *);
1196 void
1197 dwarf2out_switch_text_section (void)
1199 section *sect;
1200 dw_fde_ref fde = cfun->fde;
1202 gcc_assert (cfun && fde && fde->dw_fde_second_begin == NULL);
1204 if (!in_cold_section_p)
1206 fde->dw_fde_end = crtl->subsections.cold_section_end_label;
1207 fde->dw_fde_second_begin = crtl->subsections.hot_section_label;
1208 fde->dw_fde_second_end = crtl->subsections.hot_section_end_label;
1210 else
1212 fde->dw_fde_end = crtl->subsections.hot_section_end_label;
1213 fde->dw_fde_second_begin = crtl->subsections.cold_section_label;
1214 fde->dw_fde_second_end = crtl->subsections.cold_section_end_label;
1216 have_multiple_function_sections = true;
1218 /* There is no need to mark used sections when not debugging. */
1219 if (cold_text_section != NULL)
1220 dwarf2out_note_section_used ();
1222 if (dwarf2out_do_cfi_asm ())
1223 fprintf (asm_out_file, "\t.cfi_endproc\n");
1225 /* Now do the real section switch. */
1226 sect = current_function_section ();
1227 switch_to_section (sect);
1229 fde->second_in_std_section
1230 = (sect == text_section
1231 || (cold_text_section && sect == cold_text_section));
1233 if (dwarf2out_do_cfi_asm ())
1234 dwarf2out_do_cfi_startproc (true);
1236 var_location_switch_text_section ();
1238 if (cold_text_section != NULL)
1239 set_cur_line_info_table (sect);
1242 /* And now, the subset of the debugging information support code necessary
1243 for emitting location expressions. */
1245 /* Data about a single source file. */
1246 struct GTY((for_user)) dwarf_file_data {
1247 const char * filename;
1248 int emitted_number;
1251 /* Describe an entry into the .debug_addr section. */
1253 enum ate_kind {
1254 ate_kind_rtx,
1255 ate_kind_rtx_dtprel,
1256 ate_kind_label
1259 struct GTY((for_user)) addr_table_entry {
1260 enum ate_kind kind;
1261 unsigned int refcount;
1262 unsigned int index;
1263 union addr_table_entry_struct_union
1265 rtx GTY ((tag ("0"))) rtl;
1266 char * GTY ((tag ("1"))) label;
1268 GTY ((desc ("%1.kind"))) addr;
1271 /* Location lists are ranges + location descriptions for that range,
1272 so you can track variables that are in different places over
1273 their entire life. */
1274 typedef struct GTY(()) dw_loc_list_struct {
1275 dw_loc_list_ref dw_loc_next;
1276 const char *begin; /* Label and addr_entry for start of range */
1277 addr_table_entry *begin_entry;
1278 const char *end; /* Label for end of range */
1279 char *ll_symbol; /* Label for beginning of location list.
1280 Only on head of list */
1281 const char *section; /* Section this loclist is relative to */
1282 dw_loc_descr_ref expr;
1283 hashval_t hash;
1284 /* True if all addresses in this and subsequent lists are known to be
1285 resolved. */
1286 bool resolved_addr;
1287 /* True if this list has been replaced by dw_loc_next. */
1288 bool replaced;
1289 /* True if it has been emitted into .debug_loc* / .debug_loclists*
1290 section. */
1291 unsigned char emitted : 1;
1292 /* True if hash field is index rather than hash value. */
1293 unsigned char num_assigned : 1;
1294 /* True if .debug_loclists.dwo offset has been emitted for it already. */
1295 unsigned char offset_emitted : 1;
1296 /* True if note_variable_value_in_expr has been called on it. */
1297 unsigned char noted_variable_value : 1;
1298 /* True if the range should be emitted even if begin and end
1299 are the same. */
1300 bool force;
1301 } dw_loc_list_node;
1303 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
1304 static dw_loc_descr_ref uint_loc_descriptor (unsigned HOST_WIDE_INT);
1306 /* Convert a DWARF stack opcode into its string name. */
1308 static const char *
1309 dwarf_stack_op_name (unsigned int op)
1311 const char *name = get_DW_OP_name (op);
1313 if (name != NULL)
1314 return name;
1316 return "OP_<unknown>";
1319 /* Return a pointer to a newly allocated location description. Location
1320 descriptions are simple expression terms that can be strung
1321 together to form more complicated location (address) descriptions. */
1323 static inline dw_loc_descr_ref
1324 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
1325 unsigned HOST_WIDE_INT oprnd2)
1327 dw_loc_descr_ref descr = ggc_cleared_alloc<dw_loc_descr_node> ();
1329 descr->dw_loc_opc = op;
1330 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
1331 descr->dw_loc_oprnd1.val_entry = NULL;
1332 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
1333 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
1334 descr->dw_loc_oprnd2.val_entry = NULL;
1335 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
1337 return descr;
1340 /* Return a pointer to a newly allocated location description for
1341 REG and OFFSET. */
1343 static inline dw_loc_descr_ref
1344 new_reg_loc_descr (unsigned int reg, unsigned HOST_WIDE_INT offset)
1346 if (reg <= 31)
1347 return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
1348 offset, 0);
1349 else
1350 return new_loc_descr (DW_OP_bregx, reg, offset);
1353 /* Add a location description term to a location description expression. */
1355 static inline void
1356 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
1358 dw_loc_descr_ref *d;
1360 /* Find the end of the chain. */
1361 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
1364 *d = descr;
1367 /* Compare two location operands for exact equality. */
1369 static bool
1370 dw_val_equal_p (dw_val_node *a, dw_val_node *b)
1372 if (a->val_class != b->val_class)
1373 return false;
1374 switch (a->val_class)
1376 case dw_val_class_none:
1377 return true;
1378 case dw_val_class_addr:
1379 return rtx_equal_p (a->v.val_addr, b->v.val_addr);
1381 case dw_val_class_offset:
1382 case dw_val_class_unsigned_const:
1383 case dw_val_class_const:
1384 case dw_val_class_unsigned_const_implicit:
1385 case dw_val_class_const_implicit:
1386 case dw_val_class_range_list:
1387 /* These are all HOST_WIDE_INT, signed or unsigned. */
1388 return a->v.val_unsigned == b->v.val_unsigned;
1390 case dw_val_class_loc:
1391 return a->v.val_loc == b->v.val_loc;
1392 case dw_val_class_loc_list:
1393 return a->v.val_loc_list == b->v.val_loc_list;
1394 case dw_val_class_die_ref:
1395 return a->v.val_die_ref.die == b->v.val_die_ref.die;
1396 case dw_val_class_fde_ref:
1397 return a->v.val_fde_index == b->v.val_fde_index;
1398 case dw_val_class_lbl_id:
1399 case dw_val_class_lineptr:
1400 case dw_val_class_macptr:
1401 case dw_val_class_loclistsptr:
1402 case dw_val_class_high_pc:
1403 return strcmp (a->v.val_lbl_id, b->v.val_lbl_id) == 0;
1404 case dw_val_class_str:
1405 return a->v.val_str == b->v.val_str;
1406 case dw_val_class_flag:
1407 return a->v.val_flag == b->v.val_flag;
1408 case dw_val_class_file:
1409 case dw_val_class_file_implicit:
1410 return a->v.val_file == b->v.val_file;
1411 case dw_val_class_decl_ref:
1412 return a->v.val_decl_ref == b->v.val_decl_ref;
1414 case dw_val_class_const_double:
1415 return (a->v.val_double.high == b->v.val_double.high
1416 && a->v.val_double.low == b->v.val_double.low);
1418 case dw_val_class_wide_int:
1419 return *a->v.val_wide == *b->v.val_wide;
1421 case dw_val_class_vec:
1423 size_t a_len = a->v.val_vec.elt_size * a->v.val_vec.length;
1424 size_t b_len = b->v.val_vec.elt_size * b->v.val_vec.length;
1426 return (a_len == b_len
1427 && !memcmp (a->v.val_vec.array, b->v.val_vec.array, a_len));
1430 case dw_val_class_data8:
1431 return memcmp (a->v.val_data8, b->v.val_data8, 8) == 0;
1433 case dw_val_class_vms_delta:
1434 return (!strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1)
1435 && !strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1));
1437 case dw_val_class_discr_value:
1438 return (a->v.val_discr_value.pos == b->v.val_discr_value.pos
1439 && a->v.val_discr_value.v.uval == b->v.val_discr_value.v.uval);
1440 case dw_val_class_discr_list:
1441 /* It makes no sense comparing two discriminant value lists. */
1442 return false;
1444 gcc_unreachable ();
1447 /* Compare two location atoms for exact equality. */
1449 static bool
1450 loc_descr_equal_p_1 (dw_loc_descr_ref a, dw_loc_descr_ref b)
1452 if (a->dw_loc_opc != b->dw_loc_opc)
1453 return false;
1455 /* ??? This is only ever set for DW_OP_constNu, for N equal to the
1456 address size, but since we always allocate cleared storage it
1457 should be zero for other types of locations. */
1458 if (a->dtprel != b->dtprel)
1459 return false;
1461 return (dw_val_equal_p (&a->dw_loc_oprnd1, &b->dw_loc_oprnd1)
1462 && dw_val_equal_p (&a->dw_loc_oprnd2, &b->dw_loc_oprnd2));
1465 /* Compare two complete location expressions for exact equality. */
1467 bool
1468 loc_descr_equal_p (dw_loc_descr_ref a, dw_loc_descr_ref b)
1470 while (1)
1472 if (a == b)
1473 return true;
1474 if (a == NULL || b == NULL)
1475 return false;
1476 if (!loc_descr_equal_p_1 (a, b))
1477 return false;
1479 a = a->dw_loc_next;
1480 b = b->dw_loc_next;
1485 /* Add a constant OFFSET to a location expression. */
1487 static void
1488 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
1490 dw_loc_descr_ref loc;
1491 HOST_WIDE_INT *p;
1493 gcc_assert (*list_head != NULL);
1495 if (!offset)
1496 return;
1498 /* Find the end of the chain. */
1499 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
1502 p = NULL;
1503 if (loc->dw_loc_opc == DW_OP_fbreg
1504 || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
1505 p = &loc->dw_loc_oprnd1.v.val_int;
1506 else if (loc->dw_loc_opc == DW_OP_bregx)
1507 p = &loc->dw_loc_oprnd2.v.val_int;
1509 /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
1510 offset. Don't optimize if an signed integer overflow would happen. */
1511 if (p != NULL
1512 && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
1513 || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
1514 *p += offset;
1516 else if (offset > 0)
1517 loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
1519 else
1521 loc->dw_loc_next
1522 = uint_loc_descriptor (-(unsigned HOST_WIDE_INT) offset);
1523 add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
1527 /* Add a constant OFFSET to a location list. */
1529 static void
1530 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
1532 dw_loc_list_ref d;
1533 for (d = list_head; d != NULL; d = d->dw_loc_next)
1534 loc_descr_plus_const (&d->expr, offset);
1537 #define DWARF_REF_SIZE \
1538 (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
1540 /* The number of bits that can be encoded by largest DW_FORM_dataN.
1541 In DWARF4 and earlier it is DW_FORM_data8 with 64 bits, in DWARF5
1542 DW_FORM_data16 with 128 bits. */
1543 #define DWARF_LARGEST_DATA_FORM_BITS \
1544 (dwarf_version >= 5 ? 128 : 64)
1546 /* Utility inline function for construction of ops that were GNU extension
1547 before DWARF 5. */
1548 static inline enum dwarf_location_atom
1549 dwarf_OP (enum dwarf_location_atom op)
1551 switch (op)
1553 case DW_OP_implicit_pointer:
1554 if (dwarf_version < 5)
1555 return DW_OP_GNU_implicit_pointer;
1556 break;
1558 case DW_OP_entry_value:
1559 if (dwarf_version < 5)
1560 return DW_OP_GNU_entry_value;
1561 break;
1563 case DW_OP_const_type:
1564 if (dwarf_version < 5)
1565 return DW_OP_GNU_const_type;
1566 break;
1568 case DW_OP_regval_type:
1569 if (dwarf_version < 5)
1570 return DW_OP_GNU_regval_type;
1571 break;
1573 case DW_OP_deref_type:
1574 if (dwarf_version < 5)
1575 return DW_OP_GNU_deref_type;
1576 break;
1578 case DW_OP_convert:
1579 if (dwarf_version < 5)
1580 return DW_OP_GNU_convert;
1581 break;
1583 case DW_OP_reinterpret:
1584 if (dwarf_version < 5)
1585 return DW_OP_GNU_reinterpret;
1586 break;
1588 default:
1589 break;
1591 return op;
1594 /* Similarly for attributes. */
1595 static inline enum dwarf_attribute
1596 dwarf_AT (enum dwarf_attribute at)
1598 switch (at)
1600 case DW_AT_call_return_pc:
1601 if (dwarf_version < 5)
1602 return DW_AT_low_pc;
1603 break;
1605 case DW_AT_call_tail_call:
1606 if (dwarf_version < 5)
1607 return DW_AT_GNU_tail_call;
1608 break;
1610 case DW_AT_call_origin:
1611 if (dwarf_version < 5)
1612 return DW_AT_abstract_origin;
1613 break;
1615 case DW_AT_call_target:
1616 if (dwarf_version < 5)
1617 return DW_AT_GNU_call_site_target;
1618 break;
1620 case DW_AT_call_target_clobbered:
1621 if (dwarf_version < 5)
1622 return DW_AT_GNU_call_site_target_clobbered;
1623 break;
1625 case DW_AT_call_parameter:
1626 if (dwarf_version < 5)
1627 return DW_AT_abstract_origin;
1628 break;
1630 case DW_AT_call_value:
1631 if (dwarf_version < 5)
1632 return DW_AT_GNU_call_site_value;
1633 break;
1635 case DW_AT_call_data_value:
1636 if (dwarf_version < 5)
1637 return DW_AT_GNU_call_site_data_value;
1638 break;
1640 case DW_AT_call_all_calls:
1641 if (dwarf_version < 5)
1642 return DW_AT_GNU_all_call_sites;
1643 break;
1645 case DW_AT_call_all_tail_calls:
1646 if (dwarf_version < 5)
1647 return DW_AT_GNU_all_tail_call_sites;
1648 break;
1650 case DW_AT_dwo_name:
1651 if (dwarf_version < 5)
1652 return DW_AT_GNU_dwo_name;
1653 break;
1655 default:
1656 break;
1658 return at;
1661 /* And similarly for tags. */
1662 static inline enum dwarf_tag
1663 dwarf_TAG (enum dwarf_tag tag)
1665 switch (tag)
1667 case DW_TAG_call_site:
1668 if (dwarf_version < 5)
1669 return DW_TAG_GNU_call_site;
1670 break;
1672 case DW_TAG_call_site_parameter:
1673 if (dwarf_version < 5)
1674 return DW_TAG_GNU_call_site_parameter;
1675 break;
1677 default:
1678 break;
1680 return tag;
1683 static unsigned long int get_base_type_offset (dw_die_ref);
1685 /* Return the size of a location descriptor. */
1687 static unsigned long
1688 size_of_loc_descr (dw_loc_descr_ref loc)
1690 unsigned long size = 1;
1692 switch (loc->dw_loc_opc)
1694 case DW_OP_addr:
1695 size += DWARF2_ADDR_SIZE;
1696 break;
1697 case DW_OP_GNU_addr_index:
1698 case DW_OP_GNU_const_index:
1699 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1700 size += size_of_uleb128 (loc->dw_loc_oprnd1.val_entry->index);
1701 break;
1702 case DW_OP_const1u:
1703 case DW_OP_const1s:
1704 size += 1;
1705 break;
1706 case DW_OP_const2u:
1707 case DW_OP_const2s:
1708 size += 2;
1709 break;
1710 case DW_OP_const4u:
1711 case DW_OP_const4s:
1712 size += 4;
1713 break;
1714 case DW_OP_const8u:
1715 case DW_OP_const8s:
1716 size += 8;
1717 break;
1718 case DW_OP_constu:
1719 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1720 break;
1721 case DW_OP_consts:
1722 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1723 break;
1724 case DW_OP_pick:
1725 size += 1;
1726 break;
1727 case DW_OP_plus_uconst:
1728 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1729 break;
1730 case DW_OP_skip:
1731 case DW_OP_bra:
1732 size += 2;
1733 break;
1734 case DW_OP_breg0:
1735 case DW_OP_breg1:
1736 case DW_OP_breg2:
1737 case DW_OP_breg3:
1738 case DW_OP_breg4:
1739 case DW_OP_breg5:
1740 case DW_OP_breg6:
1741 case DW_OP_breg7:
1742 case DW_OP_breg8:
1743 case DW_OP_breg9:
1744 case DW_OP_breg10:
1745 case DW_OP_breg11:
1746 case DW_OP_breg12:
1747 case DW_OP_breg13:
1748 case DW_OP_breg14:
1749 case DW_OP_breg15:
1750 case DW_OP_breg16:
1751 case DW_OP_breg17:
1752 case DW_OP_breg18:
1753 case DW_OP_breg19:
1754 case DW_OP_breg20:
1755 case DW_OP_breg21:
1756 case DW_OP_breg22:
1757 case DW_OP_breg23:
1758 case DW_OP_breg24:
1759 case DW_OP_breg25:
1760 case DW_OP_breg26:
1761 case DW_OP_breg27:
1762 case DW_OP_breg28:
1763 case DW_OP_breg29:
1764 case DW_OP_breg30:
1765 case DW_OP_breg31:
1766 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1767 break;
1768 case DW_OP_regx:
1769 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1770 break;
1771 case DW_OP_fbreg:
1772 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1773 break;
1774 case DW_OP_bregx:
1775 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1776 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1777 break;
1778 case DW_OP_piece:
1779 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1780 break;
1781 case DW_OP_bit_piece:
1782 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1783 size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
1784 break;
1785 case DW_OP_deref_size:
1786 case DW_OP_xderef_size:
1787 size += 1;
1788 break;
1789 case DW_OP_call2:
1790 size += 2;
1791 break;
1792 case DW_OP_call4:
1793 size += 4;
1794 break;
1795 case DW_OP_call_ref:
1796 case DW_OP_GNU_variable_value:
1797 size += DWARF_REF_SIZE;
1798 break;
1799 case DW_OP_implicit_value:
1800 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1801 + loc->dw_loc_oprnd1.v.val_unsigned;
1802 break;
1803 case DW_OP_implicit_pointer:
1804 case DW_OP_GNU_implicit_pointer:
1805 size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1806 break;
1807 case DW_OP_entry_value:
1808 case DW_OP_GNU_entry_value:
1810 unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
1811 size += size_of_uleb128 (op_size) + op_size;
1812 break;
1814 case DW_OP_const_type:
1815 case DW_OP_GNU_const_type:
1817 unsigned long o
1818 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1819 size += size_of_uleb128 (o) + 1;
1820 switch (loc->dw_loc_oprnd2.val_class)
1822 case dw_val_class_vec:
1823 size += loc->dw_loc_oprnd2.v.val_vec.length
1824 * loc->dw_loc_oprnd2.v.val_vec.elt_size;
1825 break;
1826 case dw_val_class_const:
1827 size += HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
1828 break;
1829 case dw_val_class_const_double:
1830 size += HOST_BITS_PER_DOUBLE_INT / BITS_PER_UNIT;
1831 break;
1832 case dw_val_class_wide_int:
1833 size += (get_full_len (*loc->dw_loc_oprnd2.v.val_wide)
1834 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
1835 break;
1836 default:
1837 gcc_unreachable ();
1839 break;
1841 case DW_OP_regval_type:
1842 case DW_OP_GNU_regval_type:
1844 unsigned long o
1845 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1846 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1847 + size_of_uleb128 (o);
1849 break;
1850 case DW_OP_deref_type:
1851 case DW_OP_GNU_deref_type:
1853 unsigned long o
1854 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1855 size += 1 + size_of_uleb128 (o);
1857 break;
1858 case DW_OP_convert:
1859 case DW_OP_reinterpret:
1860 case DW_OP_GNU_convert:
1861 case DW_OP_GNU_reinterpret:
1862 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
1863 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1864 else
1866 unsigned long o
1867 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1868 size += size_of_uleb128 (o);
1870 break;
1871 case DW_OP_GNU_parameter_ref:
1872 size += 4;
1873 break;
1874 default:
1875 break;
1878 return size;
1881 /* Return the size of a series of location descriptors. */
1883 unsigned long
1884 size_of_locs (dw_loc_descr_ref loc)
1886 dw_loc_descr_ref l;
1887 unsigned long size;
1889 /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
1890 field, to avoid writing to a PCH file. */
1891 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1893 if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
1894 break;
1895 size += size_of_loc_descr (l);
1897 if (! l)
1898 return size;
1900 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1902 l->dw_loc_addr = size;
1903 size += size_of_loc_descr (l);
1906 return size;
1909 /* Return the size of the value in a DW_AT_discr_value attribute. */
1911 static int
1912 size_of_discr_value (dw_discr_value *discr_value)
1914 if (discr_value->pos)
1915 return size_of_uleb128 (discr_value->v.uval);
1916 else
1917 return size_of_sleb128 (discr_value->v.sval);
1920 /* Return the size of the value in a DW_AT_discr_list attribute. */
1922 static int
1923 size_of_discr_list (dw_discr_list_ref discr_list)
1925 int size = 0;
1927 for (dw_discr_list_ref list = discr_list;
1928 list != NULL;
1929 list = list->dw_discr_next)
1931 /* One byte for the discriminant value descriptor, and then one or two
1932 LEB128 numbers, depending on whether it's a single case label or a
1933 range label. */
1934 size += 1;
1935 size += size_of_discr_value (&list->dw_discr_lower_bound);
1936 if (list->dw_discr_range != 0)
1937 size += size_of_discr_value (&list->dw_discr_upper_bound);
1939 return size;
1942 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
1943 static void get_ref_die_offset_label (char *, dw_die_ref);
1944 static unsigned long int get_ref_die_offset (dw_die_ref);
1946 /* Output location description stack opcode's operands (if any).
1947 The for_eh_or_skip parameter controls whether register numbers are
1948 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
1949 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
1950 info). This should be suppressed for the cases that have not been converted
1951 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
1953 static void
1954 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
1956 dw_val_ref val1 = &loc->dw_loc_oprnd1;
1957 dw_val_ref val2 = &loc->dw_loc_oprnd2;
1959 switch (loc->dw_loc_opc)
1961 #ifdef DWARF2_DEBUGGING_INFO
1962 case DW_OP_const2u:
1963 case DW_OP_const2s:
1964 dw2_asm_output_data (2, val1->v.val_int, NULL);
1965 break;
1966 case DW_OP_const4u:
1967 if (loc->dtprel)
1969 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1970 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
1971 val1->v.val_addr);
1972 fputc ('\n', asm_out_file);
1973 break;
1975 /* FALLTHRU */
1976 case DW_OP_const4s:
1977 dw2_asm_output_data (4, val1->v.val_int, NULL);
1978 break;
1979 case DW_OP_const8u:
1980 if (loc->dtprel)
1982 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1983 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
1984 val1->v.val_addr);
1985 fputc ('\n', asm_out_file);
1986 break;
1988 /* FALLTHRU */
1989 case DW_OP_const8s:
1990 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
1991 dw2_asm_output_data (8, val1->v.val_int, NULL);
1992 break;
1993 case DW_OP_skip:
1994 case DW_OP_bra:
1996 int offset;
1998 gcc_assert (val1->val_class == dw_val_class_loc);
1999 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2001 dw2_asm_output_data (2, offset, NULL);
2003 break;
2004 case DW_OP_implicit_value:
2005 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2006 switch (val2->val_class)
2008 case dw_val_class_const:
2009 dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
2010 break;
2011 case dw_val_class_vec:
2013 unsigned int elt_size = val2->v.val_vec.elt_size;
2014 unsigned int len = val2->v.val_vec.length;
2015 unsigned int i;
2016 unsigned char *p;
2018 if (elt_size > sizeof (HOST_WIDE_INT))
2020 elt_size /= 2;
2021 len *= 2;
2023 for (i = 0, p = (unsigned char *) val2->v.val_vec.array;
2024 i < len;
2025 i++, p += elt_size)
2026 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2027 "fp or vector constant word %u", i);
2029 break;
2030 case dw_val_class_const_double:
2032 unsigned HOST_WIDE_INT first, second;
2034 if (WORDS_BIG_ENDIAN)
2036 first = val2->v.val_double.high;
2037 second = val2->v.val_double.low;
2039 else
2041 first = val2->v.val_double.low;
2042 second = val2->v.val_double.high;
2044 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2045 first, NULL);
2046 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2047 second, NULL);
2049 break;
2050 case dw_val_class_wide_int:
2052 int i;
2053 int len = get_full_len (*val2->v.val_wide);
2054 if (WORDS_BIG_ENDIAN)
2055 for (i = len - 1; i >= 0; --i)
2056 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2057 val2->v.val_wide->elt (i), NULL);
2058 else
2059 for (i = 0; i < len; ++i)
2060 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2061 val2->v.val_wide->elt (i), NULL);
2063 break;
2064 case dw_val_class_addr:
2065 gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
2066 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
2067 break;
2068 default:
2069 gcc_unreachable ();
2071 break;
2072 #else
2073 case DW_OP_const2u:
2074 case DW_OP_const2s:
2075 case DW_OP_const4u:
2076 case DW_OP_const4s:
2077 case DW_OP_const8u:
2078 case DW_OP_const8s:
2079 case DW_OP_skip:
2080 case DW_OP_bra:
2081 case DW_OP_implicit_value:
2082 /* We currently don't make any attempt to make sure these are
2083 aligned properly like we do for the main unwind info, so
2084 don't support emitting things larger than a byte if we're
2085 only doing unwinding. */
2086 gcc_unreachable ();
2087 #endif
2088 case DW_OP_const1u:
2089 case DW_OP_const1s:
2090 dw2_asm_output_data (1, val1->v.val_int, NULL);
2091 break;
2092 case DW_OP_constu:
2093 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2094 break;
2095 case DW_OP_consts:
2096 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2097 break;
2098 case DW_OP_pick:
2099 dw2_asm_output_data (1, val1->v.val_int, NULL);
2100 break;
2101 case DW_OP_plus_uconst:
2102 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2103 break;
2104 case DW_OP_breg0:
2105 case DW_OP_breg1:
2106 case DW_OP_breg2:
2107 case DW_OP_breg3:
2108 case DW_OP_breg4:
2109 case DW_OP_breg5:
2110 case DW_OP_breg6:
2111 case DW_OP_breg7:
2112 case DW_OP_breg8:
2113 case DW_OP_breg9:
2114 case DW_OP_breg10:
2115 case DW_OP_breg11:
2116 case DW_OP_breg12:
2117 case DW_OP_breg13:
2118 case DW_OP_breg14:
2119 case DW_OP_breg15:
2120 case DW_OP_breg16:
2121 case DW_OP_breg17:
2122 case DW_OP_breg18:
2123 case DW_OP_breg19:
2124 case DW_OP_breg20:
2125 case DW_OP_breg21:
2126 case DW_OP_breg22:
2127 case DW_OP_breg23:
2128 case DW_OP_breg24:
2129 case DW_OP_breg25:
2130 case DW_OP_breg26:
2131 case DW_OP_breg27:
2132 case DW_OP_breg28:
2133 case DW_OP_breg29:
2134 case DW_OP_breg30:
2135 case DW_OP_breg31:
2136 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2137 break;
2138 case DW_OP_regx:
2140 unsigned r = val1->v.val_unsigned;
2141 if (for_eh_or_skip >= 0)
2142 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2143 gcc_assert (size_of_uleb128 (r)
2144 == size_of_uleb128 (val1->v.val_unsigned));
2145 dw2_asm_output_data_uleb128 (r, NULL);
2147 break;
2148 case DW_OP_fbreg:
2149 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2150 break;
2151 case DW_OP_bregx:
2153 unsigned r = val1->v.val_unsigned;
2154 if (for_eh_or_skip >= 0)
2155 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2156 gcc_assert (size_of_uleb128 (r)
2157 == size_of_uleb128 (val1->v.val_unsigned));
2158 dw2_asm_output_data_uleb128 (r, NULL);
2159 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2161 break;
2162 case DW_OP_piece:
2163 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2164 break;
2165 case DW_OP_bit_piece:
2166 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2167 dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
2168 break;
2169 case DW_OP_deref_size:
2170 case DW_OP_xderef_size:
2171 dw2_asm_output_data (1, val1->v.val_int, NULL);
2172 break;
2174 case DW_OP_addr:
2175 if (loc->dtprel)
2177 if (targetm.asm_out.output_dwarf_dtprel)
2179 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
2180 DWARF2_ADDR_SIZE,
2181 val1->v.val_addr);
2182 fputc ('\n', asm_out_file);
2184 else
2185 gcc_unreachable ();
2187 else
2189 #ifdef DWARF2_DEBUGGING_INFO
2190 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
2191 #else
2192 gcc_unreachable ();
2193 #endif
2195 break;
2197 case DW_OP_GNU_addr_index:
2198 case DW_OP_GNU_const_index:
2199 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
2200 dw2_asm_output_data_uleb128 (loc->dw_loc_oprnd1.val_entry->index,
2201 "(index into .debug_addr)");
2202 break;
2204 case DW_OP_call2:
2205 case DW_OP_call4:
2207 unsigned long die_offset
2208 = get_ref_die_offset (val1->v.val_die_ref.die);
2209 /* Make sure the offset has been computed and that we can encode it as
2210 an operand. */
2211 gcc_assert (die_offset > 0
2212 && die_offset <= (loc->dw_loc_opc == DW_OP_call2
2213 ? 0xffff
2214 : 0xffffffff));
2215 dw2_asm_output_data ((loc->dw_loc_opc == DW_OP_call2) ? 2 : 4,
2216 die_offset, NULL);
2218 break;
2220 case DW_OP_call_ref:
2221 case DW_OP_GNU_variable_value:
2223 char label[MAX_ARTIFICIAL_LABEL_BYTES
2224 + HOST_BITS_PER_WIDE_INT / 2 + 2];
2225 gcc_assert (val1->val_class == dw_val_class_die_ref);
2226 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2227 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2229 break;
2231 case DW_OP_implicit_pointer:
2232 case DW_OP_GNU_implicit_pointer:
2234 char label[MAX_ARTIFICIAL_LABEL_BYTES
2235 + HOST_BITS_PER_WIDE_INT / 2 + 2];
2236 gcc_assert (val1->val_class == dw_val_class_die_ref);
2237 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2238 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2239 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2241 break;
2243 case DW_OP_entry_value:
2244 case DW_OP_GNU_entry_value:
2245 dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
2246 output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
2247 break;
2249 case DW_OP_const_type:
2250 case DW_OP_GNU_const_type:
2252 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
2253 gcc_assert (o);
2254 dw2_asm_output_data_uleb128 (o, NULL);
2255 switch (val2->val_class)
2257 case dw_val_class_const:
2258 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2259 dw2_asm_output_data (1, l, NULL);
2260 dw2_asm_output_data (l, val2->v.val_int, NULL);
2261 break;
2262 case dw_val_class_vec:
2264 unsigned int elt_size = val2->v.val_vec.elt_size;
2265 unsigned int len = val2->v.val_vec.length;
2266 unsigned int i;
2267 unsigned char *p;
2269 l = len * elt_size;
2270 dw2_asm_output_data (1, l, NULL);
2271 if (elt_size > sizeof (HOST_WIDE_INT))
2273 elt_size /= 2;
2274 len *= 2;
2276 for (i = 0, p = (unsigned char *) val2->v.val_vec.array;
2277 i < len;
2278 i++, p += elt_size)
2279 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2280 "fp or vector constant word %u", i);
2282 break;
2283 case dw_val_class_const_double:
2285 unsigned HOST_WIDE_INT first, second;
2286 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2288 dw2_asm_output_data (1, 2 * l, NULL);
2289 if (WORDS_BIG_ENDIAN)
2291 first = val2->v.val_double.high;
2292 second = val2->v.val_double.low;
2294 else
2296 first = val2->v.val_double.low;
2297 second = val2->v.val_double.high;
2299 dw2_asm_output_data (l, first, NULL);
2300 dw2_asm_output_data (l, second, NULL);
2302 break;
2303 case dw_val_class_wide_int:
2305 int i;
2306 int len = get_full_len (*val2->v.val_wide);
2307 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2309 dw2_asm_output_data (1, len * l, NULL);
2310 if (WORDS_BIG_ENDIAN)
2311 for (i = len - 1; i >= 0; --i)
2312 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2313 else
2314 for (i = 0; i < len; ++i)
2315 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2317 break;
2318 default:
2319 gcc_unreachable ();
2322 break;
2323 case DW_OP_regval_type:
2324 case DW_OP_GNU_regval_type:
2326 unsigned r = val1->v.val_unsigned;
2327 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2328 gcc_assert (o);
2329 if (for_eh_or_skip >= 0)
2331 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2332 gcc_assert (size_of_uleb128 (r)
2333 == size_of_uleb128 (val1->v.val_unsigned));
2335 dw2_asm_output_data_uleb128 (r, NULL);
2336 dw2_asm_output_data_uleb128 (o, NULL);
2338 break;
2339 case DW_OP_deref_type:
2340 case DW_OP_GNU_deref_type:
2342 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2343 gcc_assert (o);
2344 dw2_asm_output_data (1, val1->v.val_int, NULL);
2345 dw2_asm_output_data_uleb128 (o, NULL);
2347 break;
2348 case DW_OP_convert:
2349 case DW_OP_reinterpret:
2350 case DW_OP_GNU_convert:
2351 case DW_OP_GNU_reinterpret:
2352 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2353 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2354 else
2356 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
2357 gcc_assert (o);
2358 dw2_asm_output_data_uleb128 (o, NULL);
2360 break;
2362 case DW_OP_GNU_parameter_ref:
2364 unsigned long o;
2365 gcc_assert (val1->val_class == dw_val_class_die_ref);
2366 o = get_ref_die_offset (val1->v.val_die_ref.die);
2367 dw2_asm_output_data (4, o, NULL);
2369 break;
2371 default:
2372 /* Other codes have no operands. */
2373 break;
2377 /* Output a sequence of location operations.
2378 The for_eh_or_skip parameter controls whether register numbers are
2379 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2380 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2381 info). This should be suppressed for the cases that have not been converted
2382 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
2384 void
2385 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
2387 for (; loc != NULL; loc = loc->dw_loc_next)
2389 enum dwarf_location_atom opc = loc->dw_loc_opc;
2390 /* Output the opcode. */
2391 if (for_eh_or_skip >= 0
2392 && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2394 unsigned r = (opc - DW_OP_breg0);
2395 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2396 gcc_assert (r <= 31);
2397 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2399 else if (for_eh_or_skip >= 0
2400 && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2402 unsigned r = (opc - DW_OP_reg0);
2403 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2404 gcc_assert (r <= 31);
2405 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2408 dw2_asm_output_data (1, opc,
2409 "%s", dwarf_stack_op_name (opc));
2411 /* Output the operand(s) (if any). */
2412 output_loc_operands (loc, for_eh_or_skip);
2416 /* Output location description stack opcode's operands (if any).
2417 The output is single bytes on a line, suitable for .cfi_escape. */
2419 static void
2420 output_loc_operands_raw (dw_loc_descr_ref loc)
2422 dw_val_ref val1 = &loc->dw_loc_oprnd1;
2423 dw_val_ref val2 = &loc->dw_loc_oprnd2;
2425 switch (loc->dw_loc_opc)
2427 case DW_OP_addr:
2428 case DW_OP_GNU_addr_index:
2429 case DW_OP_GNU_const_index:
2430 case DW_OP_implicit_value:
2431 /* We cannot output addresses in .cfi_escape, only bytes. */
2432 gcc_unreachable ();
2434 case DW_OP_const1u:
2435 case DW_OP_const1s:
2436 case DW_OP_pick:
2437 case DW_OP_deref_size:
2438 case DW_OP_xderef_size:
2439 fputc (',', asm_out_file);
2440 dw2_asm_output_data_raw (1, val1->v.val_int);
2441 break;
2443 case DW_OP_const2u:
2444 case DW_OP_const2s:
2445 fputc (',', asm_out_file);
2446 dw2_asm_output_data_raw (2, val1->v.val_int);
2447 break;
2449 case DW_OP_const4u:
2450 case DW_OP_const4s:
2451 fputc (',', asm_out_file);
2452 dw2_asm_output_data_raw (4, val1->v.val_int);
2453 break;
2455 case DW_OP_const8u:
2456 case DW_OP_const8s:
2457 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2458 fputc (',', asm_out_file);
2459 dw2_asm_output_data_raw (8, val1->v.val_int);
2460 break;
2462 case DW_OP_skip:
2463 case DW_OP_bra:
2465 int offset;
2467 gcc_assert (val1->val_class == dw_val_class_loc);
2468 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2470 fputc (',', asm_out_file);
2471 dw2_asm_output_data_raw (2, offset);
2473 break;
2475 case DW_OP_regx:
2477 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2478 gcc_assert (size_of_uleb128 (r)
2479 == size_of_uleb128 (val1->v.val_unsigned));
2480 fputc (',', asm_out_file);
2481 dw2_asm_output_data_uleb128_raw (r);
2483 break;
2485 case DW_OP_constu:
2486 case DW_OP_plus_uconst:
2487 case DW_OP_piece:
2488 fputc (',', asm_out_file);
2489 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2490 break;
2492 case DW_OP_bit_piece:
2493 fputc (',', asm_out_file);
2494 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2495 dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
2496 break;
2498 case DW_OP_consts:
2499 case DW_OP_breg0:
2500 case DW_OP_breg1:
2501 case DW_OP_breg2:
2502 case DW_OP_breg3:
2503 case DW_OP_breg4:
2504 case DW_OP_breg5:
2505 case DW_OP_breg6:
2506 case DW_OP_breg7:
2507 case DW_OP_breg8:
2508 case DW_OP_breg9:
2509 case DW_OP_breg10:
2510 case DW_OP_breg11:
2511 case DW_OP_breg12:
2512 case DW_OP_breg13:
2513 case DW_OP_breg14:
2514 case DW_OP_breg15:
2515 case DW_OP_breg16:
2516 case DW_OP_breg17:
2517 case DW_OP_breg18:
2518 case DW_OP_breg19:
2519 case DW_OP_breg20:
2520 case DW_OP_breg21:
2521 case DW_OP_breg22:
2522 case DW_OP_breg23:
2523 case DW_OP_breg24:
2524 case DW_OP_breg25:
2525 case DW_OP_breg26:
2526 case DW_OP_breg27:
2527 case DW_OP_breg28:
2528 case DW_OP_breg29:
2529 case DW_OP_breg30:
2530 case DW_OP_breg31:
2531 case DW_OP_fbreg:
2532 fputc (',', asm_out_file);
2533 dw2_asm_output_data_sleb128_raw (val1->v.val_int);
2534 break;
2536 case DW_OP_bregx:
2538 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2539 gcc_assert (size_of_uleb128 (r)
2540 == size_of_uleb128 (val1->v.val_unsigned));
2541 fputc (',', asm_out_file);
2542 dw2_asm_output_data_uleb128_raw (r);
2543 fputc (',', asm_out_file);
2544 dw2_asm_output_data_sleb128_raw (val2->v.val_int);
2546 break;
2548 case DW_OP_implicit_pointer:
2549 case DW_OP_entry_value:
2550 case DW_OP_const_type:
2551 case DW_OP_regval_type:
2552 case DW_OP_deref_type:
2553 case DW_OP_convert:
2554 case DW_OP_reinterpret:
2555 case DW_OP_GNU_implicit_pointer:
2556 case DW_OP_GNU_entry_value:
2557 case DW_OP_GNU_const_type:
2558 case DW_OP_GNU_regval_type:
2559 case DW_OP_GNU_deref_type:
2560 case DW_OP_GNU_convert:
2561 case DW_OP_GNU_reinterpret:
2562 case DW_OP_GNU_parameter_ref:
2563 gcc_unreachable ();
2564 break;
2566 default:
2567 /* Other codes have no operands. */
2568 break;
2572 void
2573 output_loc_sequence_raw (dw_loc_descr_ref loc)
2575 while (1)
2577 enum dwarf_location_atom opc = loc->dw_loc_opc;
2578 /* Output the opcode. */
2579 if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2581 unsigned r = (opc - DW_OP_breg0);
2582 r = DWARF2_FRAME_REG_OUT (r, 1);
2583 gcc_assert (r <= 31);
2584 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2586 else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2588 unsigned r = (opc - DW_OP_reg0);
2589 r = DWARF2_FRAME_REG_OUT (r, 1);
2590 gcc_assert (r <= 31);
2591 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2593 /* Output the opcode. */
2594 fprintf (asm_out_file, "%#x", opc);
2595 output_loc_operands_raw (loc);
2597 if (!loc->dw_loc_next)
2598 break;
2599 loc = loc->dw_loc_next;
2601 fputc (',', asm_out_file);
2605 /* This function builds a dwarf location descriptor sequence from a
2606 dw_cfa_location, adding the given OFFSET to the result of the
2607 expression. */
2609 struct dw_loc_descr_node *
2610 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
2612 struct dw_loc_descr_node *head, *tmp;
2614 offset += cfa->offset;
2616 if (cfa->indirect)
2618 head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
2619 head->dw_loc_oprnd1.val_class = dw_val_class_const;
2620 head->dw_loc_oprnd1.val_entry = NULL;
2621 tmp = new_loc_descr (DW_OP_deref, 0, 0);
2622 add_loc_descr (&head, tmp);
2623 if (offset != 0)
2625 tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
2626 add_loc_descr (&head, tmp);
2629 else
2630 head = new_reg_loc_descr (cfa->reg, offset);
2632 return head;
2635 /* This function builds a dwarf location descriptor sequence for
2636 the address at OFFSET from the CFA when stack is aligned to
2637 ALIGNMENT byte. */
2639 struct dw_loc_descr_node *
2640 build_cfa_aligned_loc (dw_cfa_location *cfa,
2641 HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
2643 struct dw_loc_descr_node *head;
2644 unsigned int dwarf_fp
2645 = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
2647 /* When CFA is defined as FP+OFFSET, emulate stack alignment. */
2648 if (cfa->reg == HARD_FRAME_POINTER_REGNUM && cfa->indirect == 0)
2650 head = new_reg_loc_descr (dwarf_fp, 0);
2651 add_loc_descr (&head, int_loc_descriptor (alignment));
2652 add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
2653 loc_descr_plus_const (&head, offset);
2655 else
2656 head = new_reg_loc_descr (dwarf_fp, offset);
2657 return head;
2660 /* And now, the support for symbolic debugging information. */
2662 /* .debug_str support. */
2664 static void dwarf2out_init (const char *);
2665 static void dwarf2out_finish (const char *);
2666 static void dwarf2out_early_finish (const char *);
2667 static void dwarf2out_assembly_start (void);
2668 static void dwarf2out_define (unsigned int, const char *);
2669 static void dwarf2out_undef (unsigned int, const char *);
2670 static void dwarf2out_start_source_file (unsigned, const char *);
2671 static void dwarf2out_end_source_file (unsigned);
2672 static void dwarf2out_function_decl (tree);
2673 static void dwarf2out_begin_block (unsigned, unsigned);
2674 static void dwarf2out_end_block (unsigned, unsigned);
2675 static bool dwarf2out_ignore_block (const_tree);
2676 static void dwarf2out_early_global_decl (tree);
2677 static void dwarf2out_late_global_decl (tree);
2678 static void dwarf2out_type_decl (tree, int);
2679 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
2680 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
2681 dw_die_ref);
2682 static void dwarf2out_abstract_function (tree);
2683 static void dwarf2out_var_location (rtx_insn *);
2684 static void dwarf2out_size_function (tree);
2685 static void dwarf2out_begin_function (tree);
2686 static void dwarf2out_end_function (unsigned int);
2687 static void dwarf2out_register_main_translation_unit (tree unit);
2688 static void dwarf2out_set_name (tree, tree);
2690 /* The debug hooks structure. */
2692 const struct gcc_debug_hooks dwarf2_debug_hooks =
2694 dwarf2out_init,
2695 dwarf2out_finish,
2696 dwarf2out_early_finish,
2697 dwarf2out_assembly_start,
2698 dwarf2out_define,
2699 dwarf2out_undef,
2700 dwarf2out_start_source_file,
2701 dwarf2out_end_source_file,
2702 dwarf2out_begin_block,
2703 dwarf2out_end_block,
2704 dwarf2out_ignore_block,
2705 dwarf2out_source_line,
2706 dwarf2out_begin_prologue,
2707 #if VMS_DEBUGGING_INFO
2708 dwarf2out_vms_end_prologue,
2709 dwarf2out_vms_begin_epilogue,
2710 #else
2711 debug_nothing_int_charstar,
2712 debug_nothing_int_charstar,
2713 #endif
2714 dwarf2out_end_epilogue,
2715 dwarf2out_begin_function,
2716 dwarf2out_end_function, /* end_function */
2717 dwarf2out_register_main_translation_unit,
2718 dwarf2out_function_decl, /* function_decl */
2719 dwarf2out_early_global_decl,
2720 dwarf2out_late_global_decl,
2721 dwarf2out_type_decl, /* type_decl */
2722 dwarf2out_imported_module_or_decl,
2723 debug_nothing_tree, /* deferred_inline_function */
2724 /* The DWARF 2 backend tries to reduce debugging bloat by not
2725 emitting the abstract description of inline functions until
2726 something tries to reference them. */
2727 dwarf2out_abstract_function, /* outlining_inline_function */
2728 debug_nothing_rtx_code_label, /* label */
2729 debug_nothing_int, /* handle_pch */
2730 dwarf2out_var_location,
2731 dwarf2out_size_function, /* size_function */
2732 dwarf2out_switch_text_section,
2733 dwarf2out_set_name,
2734 1, /* start_end_main_source_file */
2735 TYPE_SYMTAB_IS_DIE /* tree_type_symtab_field */
2738 const struct gcc_debug_hooks dwarf2_lineno_debug_hooks =
2740 dwarf2out_init,
2741 debug_nothing_charstar,
2742 debug_nothing_charstar,
2743 dwarf2out_assembly_start,
2744 debug_nothing_int_charstar,
2745 debug_nothing_int_charstar,
2746 debug_nothing_int_charstar,
2747 debug_nothing_int,
2748 debug_nothing_int_int, /* begin_block */
2749 debug_nothing_int_int, /* end_block */
2750 debug_true_const_tree, /* ignore_block */
2751 dwarf2out_source_line, /* source_line */
2752 debug_nothing_int_int_charstar, /* begin_prologue */
2753 debug_nothing_int_charstar, /* end_prologue */
2754 debug_nothing_int_charstar, /* begin_epilogue */
2755 debug_nothing_int_charstar, /* end_epilogue */
2756 debug_nothing_tree, /* begin_function */
2757 debug_nothing_int, /* end_function */
2758 debug_nothing_tree, /* register_main_translation_unit */
2759 debug_nothing_tree, /* function_decl */
2760 debug_nothing_tree, /* early_global_decl */
2761 debug_nothing_tree, /* late_global_decl */
2762 debug_nothing_tree_int, /* type_decl */
2763 debug_nothing_tree_tree_tree_bool, /* imported_module_or_decl */
2764 debug_nothing_tree, /* deferred_inline_function */
2765 debug_nothing_tree, /* outlining_inline_function */
2766 debug_nothing_rtx_code_label, /* label */
2767 debug_nothing_int, /* handle_pch */
2768 debug_nothing_rtx_insn, /* var_location */
2769 debug_nothing_tree, /* size_function */
2770 debug_nothing_void, /* switch_text_section */
2771 debug_nothing_tree_tree, /* set_name */
2772 0, /* start_end_main_source_file */
2773 TYPE_SYMTAB_IS_ADDRESS /* tree_type_symtab_field */
2776 /* NOTE: In the comments in this file, many references are made to
2777 "Debugging Information Entries". This term is abbreviated as `DIE'
2778 throughout the remainder of this file. */
2780 /* An internal representation of the DWARF output is built, and then
2781 walked to generate the DWARF debugging info. The walk of the internal
2782 representation is done after the entire program has been compiled.
2783 The types below are used to describe the internal representation. */
2785 /* Whether to put type DIEs into their own section .debug_types instead
2786 of making them part of the .debug_info section. Only supported for
2787 Dwarf V4 or higher and the user didn't disable them through
2788 -fno-debug-types-section. It is more efficient to put them in a
2789 separate comdat sections since the linker will then be able to
2790 remove duplicates. But not all tools support .debug_types sections
2791 yet. For Dwarf V5 or higher .debug_types doesn't exist any more,
2792 it is DW_UT_type unit type in .debug_info section. */
2794 #define use_debug_types (dwarf_version >= 4 && flag_debug_types_section)
2796 /* Various DIE's use offsets relative to the beginning of the
2797 .debug_info section to refer to each other. */
2799 typedef long int dw_offset;
2801 struct comdat_type_node;
2803 /* The entries in the line_info table more-or-less mirror the opcodes
2804 that are used in the real dwarf line table. Arrays of these entries
2805 are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
2806 supported. */
2808 enum dw_line_info_opcode {
2809 /* Emit DW_LNE_set_address; the operand is the label index. */
2810 LI_set_address,
2812 /* Emit a row to the matrix with the given line. This may be done
2813 via any combination of DW_LNS_copy, DW_LNS_advance_line, and
2814 special opcodes. */
2815 LI_set_line,
2817 /* Emit a DW_LNS_set_file. */
2818 LI_set_file,
2820 /* Emit a DW_LNS_set_column. */
2821 LI_set_column,
2823 /* Emit a DW_LNS_negate_stmt; the operand is ignored. */
2824 LI_negate_stmt,
2826 /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored. */
2827 LI_set_prologue_end,
2828 LI_set_epilogue_begin,
2830 /* Emit a DW_LNE_set_discriminator. */
2831 LI_set_discriminator
2834 typedef struct GTY(()) dw_line_info_struct {
2835 enum dw_line_info_opcode opcode;
2836 unsigned int val;
2837 } dw_line_info_entry;
2840 struct GTY(()) dw_line_info_table {
2841 /* The label that marks the end of this section. */
2842 const char *end_label;
2844 /* The values for the last row of the matrix, as collected in the table.
2845 These are used to minimize the changes to the next row. */
2846 unsigned int file_num;
2847 unsigned int line_num;
2848 unsigned int column_num;
2849 int discrim_num;
2850 bool is_stmt;
2851 bool in_use;
2853 vec<dw_line_info_entry, va_gc> *entries;
2857 /* Each DIE attribute has a field specifying the attribute kind,
2858 a link to the next attribute in the chain, and an attribute value.
2859 Attributes are typically linked below the DIE they modify. */
2861 typedef struct GTY(()) dw_attr_struct {
2862 enum dwarf_attribute dw_attr;
2863 dw_val_node dw_attr_val;
2865 dw_attr_node;
2868 /* The Debugging Information Entry (DIE) structure. DIEs form a tree.
2869 The children of each node form a circular list linked by
2870 die_sib. die_child points to the node *before* the "first" child node. */
2872 typedef struct GTY((chain_circular ("%h.die_sib"), for_user)) die_struct {
2873 union die_symbol_or_type_node
2875 const char * GTY ((tag ("0"))) die_symbol;
2876 comdat_type_node *GTY ((tag ("1"))) die_type_node;
2878 GTY ((desc ("%0.comdat_type_p"))) die_id;
2879 vec<dw_attr_node, va_gc> *die_attr;
2880 dw_die_ref die_parent;
2881 dw_die_ref die_child;
2882 dw_die_ref die_sib;
2883 dw_die_ref die_definition; /* ref from a specification to its definition */
2884 dw_offset die_offset;
2885 unsigned long die_abbrev;
2886 int die_mark;
2887 unsigned int decl_id;
2888 enum dwarf_tag die_tag;
2889 /* Die is used and must not be pruned as unused. */
2890 BOOL_BITFIELD die_perennial_p : 1;
2891 BOOL_BITFIELD comdat_type_p : 1; /* DIE has a type signature */
2892 /* Whether this DIE was removed from the DIE tree, for example via
2893 prune_unused_types. We don't consider those present from the
2894 DIE lookup routines. */
2895 BOOL_BITFIELD removed : 1;
2896 /* Lots of spare bits. */
2898 die_node;
2900 /* Set to TRUE while dwarf2out_early_global_decl is running. */
2901 static bool early_dwarf;
2902 static bool early_dwarf_finished;
2903 struct set_early_dwarf {
2904 bool saved;
2905 set_early_dwarf () : saved(early_dwarf)
2907 gcc_assert (! early_dwarf_finished);
2908 early_dwarf = true;
2910 ~set_early_dwarf () { early_dwarf = saved; }
2913 /* Evaluate 'expr' while 'c' is set to each child of DIE in order. */
2914 #define FOR_EACH_CHILD(die, c, expr) do { \
2915 c = die->die_child; \
2916 if (c) do { \
2917 c = c->die_sib; \
2918 expr; \
2919 } while (c != die->die_child); \
2920 } while (0)
2922 /* The pubname structure */
2924 typedef struct GTY(()) pubname_struct {
2925 dw_die_ref die;
2926 const char *name;
2928 pubname_entry;
2931 struct GTY(()) dw_ranges {
2932 const char *label;
2933 /* If this is positive, it's a block number, otherwise it's a
2934 bitwise-negated index into dw_ranges_by_label. */
2935 int num;
2936 /* Index for the range list for DW_FORM_rnglistx. */
2937 unsigned int idx : 31;
2938 /* True if this range might be possibly in a different section
2939 from previous entry. */
2940 unsigned int maybe_new_sec : 1;
2943 /* A structure to hold a macinfo entry. */
2945 typedef struct GTY(()) macinfo_struct {
2946 unsigned char code;
2947 unsigned HOST_WIDE_INT lineno;
2948 const char *info;
2950 macinfo_entry;
2953 struct GTY(()) dw_ranges_by_label {
2954 const char *begin;
2955 const char *end;
2958 /* The comdat type node structure. */
2959 struct GTY(()) comdat_type_node
2961 dw_die_ref root_die;
2962 dw_die_ref type_die;
2963 dw_die_ref skeleton_die;
2964 char signature[DWARF_TYPE_SIGNATURE_SIZE];
2965 comdat_type_node *next;
2968 /* A list of DIEs for which we can't determine ancestry (parent_die
2969 field) just yet. Later in dwarf2out_finish we will fill in the
2970 missing bits. */
2971 typedef struct GTY(()) limbo_die_struct {
2972 dw_die_ref die;
2973 /* The tree for which this DIE was created. We use this to
2974 determine ancestry later. */
2975 tree created_for;
2976 struct limbo_die_struct *next;
2978 limbo_die_node;
2980 typedef struct skeleton_chain_struct
2982 dw_die_ref old_die;
2983 dw_die_ref new_die;
2984 struct skeleton_chain_struct *parent;
2986 skeleton_chain_node;
2988 /* Define a macro which returns nonzero for a TYPE_DECL which was
2989 implicitly generated for a type.
2991 Note that, unlike the C front-end (which generates a NULL named
2992 TYPE_DECL node for each complete tagged type, each array type,
2993 and each function type node created) the C++ front-end generates
2994 a _named_ TYPE_DECL node for each tagged type node created.
2995 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
2996 generate a DW_TAG_typedef DIE for them. Likewise with the Ada
2997 front-end, but for each type, tagged or not. */
2999 #define TYPE_DECL_IS_STUB(decl) \
3000 (DECL_NAME (decl) == NULL_TREE \
3001 || (DECL_ARTIFICIAL (decl) \
3002 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
3003 /* This is necessary for stub decls that \
3004 appear in nested inline functions. */ \
3005 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
3006 && (decl_ultimate_origin (decl) \
3007 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
3009 /* Information concerning the compilation unit's programming
3010 language, and compiler version. */
3012 /* Fixed size portion of the DWARF compilation unit header. */
3013 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
3014 (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE \
3015 + (dwarf_version >= 5 ? 4 : 3))
3017 /* Fixed size portion of the DWARF comdat type unit header. */
3018 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
3019 (DWARF_COMPILE_UNIT_HEADER_SIZE \
3020 + DWARF_TYPE_SIGNATURE_SIZE + DWARF_OFFSET_SIZE)
3022 /* Fixed size portion of the DWARF skeleton compilation unit header. */
3023 #define DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE \
3024 (DWARF_COMPILE_UNIT_HEADER_SIZE + (dwarf_version >= 5 ? 8 : 0))
3026 /* Fixed size portion of public names info. */
3027 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
3029 /* Fixed size portion of the address range info. */
3030 #define DWARF_ARANGES_HEADER_SIZE \
3031 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
3032 DWARF2_ADDR_SIZE * 2) \
3033 - DWARF_INITIAL_LENGTH_SIZE)
3035 /* Size of padding portion in the address range info. It must be
3036 aligned to twice the pointer size. */
3037 #define DWARF_ARANGES_PAD_SIZE \
3038 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
3039 DWARF2_ADDR_SIZE * 2) \
3040 - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
3042 /* Use assembler line directives if available. */
3043 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
3044 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
3045 #define DWARF2_ASM_LINE_DEBUG_INFO 1
3046 #else
3047 #define DWARF2_ASM_LINE_DEBUG_INFO 0
3048 #endif
3049 #endif
3051 /* Minimum line offset in a special line info. opcode.
3052 This value was chosen to give a reasonable range of values. */
3053 #define DWARF_LINE_BASE -10
3055 /* First special line opcode - leave room for the standard opcodes. */
3056 #define DWARF_LINE_OPCODE_BASE ((int)DW_LNS_set_isa + 1)
3058 /* Range of line offsets in a special line info. opcode. */
3059 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
3061 /* Flag that indicates the initial value of the is_stmt_start flag.
3062 In the present implementation, we do not mark any lines as
3063 the beginning of a source statement, because that information
3064 is not made available by the GCC front-end. */
3065 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
3067 /* Maximum number of operations per instruction bundle. */
3068 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
3069 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
3070 #endif
3072 /* This location is used by calc_die_sizes() to keep track
3073 the offset of each DIE within the .debug_info section. */
3074 static unsigned long next_die_offset;
3076 /* Record the root of the DIE's built for the current compilation unit. */
3077 static GTY(()) dw_die_ref single_comp_unit_die;
3079 /* A list of type DIEs that have been separated into comdat sections. */
3080 static GTY(()) comdat_type_node *comdat_type_list;
3082 /* A list of CU DIEs that have been separated. */
3083 static GTY(()) limbo_die_node *cu_die_list;
3085 /* A list of DIEs with a NULL parent waiting to be relocated. */
3086 static GTY(()) limbo_die_node *limbo_die_list;
3088 /* A list of DIEs for which we may have to generate
3089 DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set. */
3090 static GTY(()) limbo_die_node *deferred_asm_name;
3092 struct dwarf_file_hasher : ggc_ptr_hash<dwarf_file_data>
3094 typedef const char *compare_type;
3096 static hashval_t hash (dwarf_file_data *);
3097 static bool equal (dwarf_file_data *, const char *);
3100 /* Filenames referenced by this compilation unit. */
3101 static GTY(()) hash_table<dwarf_file_hasher> *file_table;
3103 struct decl_die_hasher : ggc_ptr_hash<die_node>
3105 typedef tree compare_type;
3107 static hashval_t hash (die_node *);
3108 static bool equal (die_node *, tree);
3110 /* A hash table of references to DIE's that describe declarations.
3111 The key is a DECL_UID() which is a unique number identifying each decl. */
3112 static GTY (()) hash_table<decl_die_hasher> *decl_die_table;
3114 struct GTY ((for_user)) variable_value_struct {
3115 unsigned int decl_id;
3116 vec<dw_die_ref, va_gc> *dies;
3119 struct variable_value_hasher : ggc_ptr_hash<variable_value_struct>
3121 typedef tree compare_type;
3123 static hashval_t hash (variable_value_struct *);
3124 static bool equal (variable_value_struct *, tree);
3126 /* A hash table of DIEs that contain DW_OP_GNU_variable_value with
3127 dw_val_class_decl_ref class, indexed by FUNCTION_DECLs which is
3128 DECL_CONTEXT of the referenced VAR_DECLs. */
3129 static GTY (()) hash_table<variable_value_hasher> *variable_value_hash;
3131 struct block_die_hasher : ggc_ptr_hash<die_struct>
3133 static hashval_t hash (die_struct *);
3134 static bool equal (die_struct *, die_struct *);
3137 /* A hash table of references to DIE's that describe COMMON blocks.
3138 The key is DECL_UID() ^ die_parent. */
3139 static GTY (()) hash_table<block_die_hasher> *common_block_die_table;
3141 typedef struct GTY(()) die_arg_entry_struct {
3142 dw_die_ref die;
3143 tree arg;
3144 } die_arg_entry;
3147 /* Node of the variable location list. */
3148 struct GTY ((chain_next ("%h.next"))) var_loc_node {
3149 /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
3150 EXPR_LIST chain. For small bitsizes, bitsize is encoded
3151 in mode of the EXPR_LIST node and first EXPR_LIST operand
3152 is either NOTE_INSN_VAR_LOCATION for a piece with a known
3153 location or NULL for padding. For larger bitsizes,
3154 mode is 0 and first operand is a CONCAT with bitsize
3155 as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
3156 NULL as second operand. */
3157 rtx GTY (()) loc;
3158 const char * GTY (()) label;
3159 struct var_loc_node * GTY (()) next;
3162 /* Variable location list. */
3163 struct GTY ((for_user)) var_loc_list_def {
3164 struct var_loc_node * GTY (()) first;
3166 /* Pointer to the last but one or last element of the
3167 chained list. If the list is empty, both first and
3168 last are NULL, if the list contains just one node
3169 or the last node certainly is not redundant, it points
3170 to the last node, otherwise points to the last but one.
3171 Do not mark it for GC because it is marked through the chain. */
3172 struct var_loc_node * GTY ((skip ("%h"))) last;
3174 /* Pointer to the last element before section switch,
3175 if NULL, either sections weren't switched or first
3176 is after section switch. */
3177 struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
3179 /* DECL_UID of the variable decl. */
3180 unsigned int decl_id;
3182 typedef struct var_loc_list_def var_loc_list;
3184 /* Call argument location list. */
3185 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
3186 rtx GTY (()) call_arg_loc_note;
3187 const char * GTY (()) label;
3188 tree GTY (()) block;
3189 bool tail_call_p;
3190 rtx GTY (()) symbol_ref;
3191 struct call_arg_loc_node * GTY (()) next;
3195 struct decl_loc_hasher : ggc_ptr_hash<var_loc_list>
3197 typedef const_tree compare_type;
3199 static hashval_t hash (var_loc_list *);
3200 static bool equal (var_loc_list *, const_tree);
3203 /* Table of decl location linked lists. */
3204 static GTY (()) hash_table<decl_loc_hasher> *decl_loc_table;
3206 /* Head and tail of call_arg_loc chain. */
3207 static GTY (()) struct call_arg_loc_node *call_arg_locations;
3208 static struct call_arg_loc_node *call_arg_loc_last;
3210 /* Number of call sites in the current function. */
3211 static int call_site_count = -1;
3212 /* Number of tail call sites in the current function. */
3213 static int tail_call_site_count = -1;
3215 /* A cached location list. */
3216 struct GTY ((for_user)) cached_dw_loc_list_def {
3217 /* The DECL_UID of the decl that this entry describes. */
3218 unsigned int decl_id;
3220 /* The cached location list. */
3221 dw_loc_list_ref loc_list;
3223 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
3225 struct dw_loc_list_hasher : ggc_ptr_hash<cached_dw_loc_list>
3228 typedef const_tree compare_type;
3230 static hashval_t hash (cached_dw_loc_list *);
3231 static bool equal (cached_dw_loc_list *, const_tree);
3234 /* Table of cached location lists. */
3235 static GTY (()) hash_table<dw_loc_list_hasher> *cached_dw_loc_list_table;
3237 /* A vector of references to DIE's that are uniquely identified by their tag,
3238 presence/absence of children DIE's, and list of attribute/value pairs. */
3239 static GTY(()) vec<dw_die_ref, va_gc> *abbrev_die_table;
3241 /* A hash map to remember the stack usage for DWARF procedures. The value
3242 stored is the stack size difference between before the DWARF procedure
3243 invokation and after it returned. In other words, for a DWARF procedure
3244 that consumes N stack slots and that pushes M ones, this stores M - N. */
3245 static hash_map<dw_die_ref, int> *dwarf_proc_stack_usage_map;
3247 /* A global counter for generating labels for line number data. */
3248 static unsigned int line_info_label_num;
3250 /* The current table to which we should emit line number information
3251 for the current function. This will be set up at the beginning of
3252 assembly for the function. */
3253 static GTY(()) dw_line_info_table *cur_line_info_table;
3255 /* The two default tables of line number info. */
3256 static GTY(()) dw_line_info_table *text_section_line_info;
3257 static GTY(()) dw_line_info_table *cold_text_section_line_info;
3259 /* The set of all non-default tables of line number info. */
3260 static GTY(()) vec<dw_line_info_table *, va_gc> *separate_line_info;
3262 /* A flag to tell pubnames/types export if there is an info section to
3263 refer to. */
3264 static bool info_section_emitted;
3266 /* A pointer to the base of a table that contains a list of publicly
3267 accessible names. */
3268 static GTY (()) vec<pubname_entry, va_gc> *pubname_table;
3270 /* A pointer to the base of a table that contains a list of publicly
3271 accessible types. */
3272 static GTY (()) vec<pubname_entry, va_gc> *pubtype_table;
3274 /* A pointer to the base of a table that contains a list of macro
3275 defines/undefines (and file start/end markers). */
3276 static GTY (()) vec<macinfo_entry, va_gc> *macinfo_table;
3278 /* True if .debug_macinfo or .debug_macros section is going to be
3279 emitted. */
3280 #define have_macinfo \
3281 ((!XCOFF_DEBUGGING_INFO || HAVE_XCOFF_DWARF_EXTRAS) \
3282 && debug_info_level >= DINFO_LEVEL_VERBOSE \
3283 && !macinfo_table->is_empty ())
3285 /* Vector of dies for which we should generate .debug_ranges info. */
3286 static GTY (()) vec<dw_ranges, va_gc> *ranges_table;
3288 /* Vector of pairs of labels referenced in ranges_table. */
3289 static GTY (()) vec<dw_ranges_by_label, va_gc> *ranges_by_label;
3291 /* Whether we have location lists that need outputting */
3292 static GTY(()) bool have_location_lists;
3294 /* Unique label counter. */
3295 static GTY(()) unsigned int loclabel_num;
3297 /* Unique label counter for point-of-call tables. */
3298 static GTY(()) unsigned int poc_label_num;
3300 /* The last file entry emitted by maybe_emit_file(). */
3301 static GTY(()) struct dwarf_file_data * last_emitted_file;
3303 /* Number of internal labels generated by gen_internal_sym(). */
3304 static GTY(()) int label_num;
3306 static GTY(()) vec<die_arg_entry, va_gc> *tmpl_value_parm_die_table;
3308 /* Instances of generic types for which we need to generate debug
3309 info that describe their generic parameters and arguments. That
3310 generation needs to happen once all types are properly laid out so
3311 we do it at the end of compilation. */
3312 static GTY(()) vec<tree, va_gc> *generic_type_instances;
3314 /* Offset from the "steady-state frame pointer" to the frame base,
3315 within the current function. */
3316 static HOST_WIDE_INT frame_pointer_fb_offset;
3317 static bool frame_pointer_fb_offset_valid;
3319 static vec<dw_die_ref> base_types;
3321 /* Flags to represent a set of attribute classes for attributes that represent
3322 a scalar value (bounds, pointers, ...). */
3323 enum dw_scalar_form
3325 dw_scalar_form_constant = 0x01,
3326 dw_scalar_form_exprloc = 0x02,
3327 dw_scalar_form_reference = 0x04
3330 /* Forward declarations for functions defined in this file. */
3332 static int is_pseudo_reg (const_rtx);
3333 static tree type_main_variant (tree);
3334 static int is_tagged_type (const_tree);
3335 static const char *dwarf_tag_name (unsigned);
3336 static const char *dwarf_attr_name (unsigned);
3337 static const char *dwarf_form_name (unsigned);
3338 static tree decl_ultimate_origin (const_tree);
3339 static tree decl_class_context (tree);
3340 static void add_dwarf_attr (dw_die_ref, dw_attr_node *);
3341 static inline enum dw_val_class AT_class (dw_attr_node *);
3342 static inline unsigned int AT_index (dw_attr_node *);
3343 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
3344 static inline unsigned AT_flag (dw_attr_node *);
3345 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
3346 static inline HOST_WIDE_INT AT_int (dw_attr_node *);
3347 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
3348 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_node *);
3349 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
3350 HOST_WIDE_INT, unsigned HOST_WIDE_INT);
3351 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
3352 unsigned int, unsigned char *);
3353 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
3354 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
3355 static inline const char *AT_string (dw_attr_node *);
3356 static enum dwarf_form AT_string_form (dw_attr_node *);
3357 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
3358 static void add_AT_specification (dw_die_ref, dw_die_ref);
3359 static inline dw_die_ref AT_ref (dw_attr_node *);
3360 static inline int AT_ref_external (dw_attr_node *);
3361 static inline void set_AT_ref_external (dw_attr_node *, int);
3362 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
3363 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
3364 static inline dw_loc_descr_ref AT_loc (dw_attr_node *);
3365 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
3366 dw_loc_list_ref);
3367 static inline dw_loc_list_ref AT_loc_list (dw_attr_node *);
3368 static addr_table_entry *add_addr_table_entry (void *, enum ate_kind);
3369 static void remove_addr_table_entry (addr_table_entry *);
3370 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx, bool);
3371 static inline rtx AT_addr (dw_attr_node *);
3372 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
3373 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
3374 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
3375 static void add_AT_loclistsptr (dw_die_ref, enum dwarf_attribute,
3376 const char *);
3377 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
3378 unsigned HOST_WIDE_INT);
3379 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
3380 unsigned long, bool);
3381 static inline const char *AT_lbl (dw_attr_node *);
3382 static dw_attr_node *get_AT (dw_die_ref, enum dwarf_attribute);
3383 static const char *get_AT_low_pc (dw_die_ref);
3384 static const char *get_AT_hi_pc (dw_die_ref);
3385 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
3386 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
3387 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
3388 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
3389 static bool is_cxx (void);
3390 static bool is_cxx (const_tree);
3391 static bool is_fortran (void);
3392 static bool is_ada (void);
3393 static bool remove_AT (dw_die_ref, enum dwarf_attribute);
3394 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
3395 static void add_child_die (dw_die_ref, dw_die_ref);
3396 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
3397 static dw_die_ref lookup_type_die (tree);
3398 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
3399 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
3400 static void equate_type_number_to_die (tree, dw_die_ref);
3401 static dw_die_ref lookup_decl_die (tree);
3402 static var_loc_list *lookup_decl_loc (const_tree);
3403 static void equate_decl_number_to_die (tree, dw_die_ref);
3404 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
3405 static void print_spaces (FILE *);
3406 static void print_die (dw_die_ref, FILE *);
3407 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
3408 static dw_die_ref pop_compile_unit (dw_die_ref);
3409 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3410 static void attr_checksum (dw_attr_node *, struct md5_ctx *, int *);
3411 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3412 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
3413 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
3414 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
3415 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_node *,
3416 struct md5_ctx *, int *);
3417 struct checksum_attributes;
3418 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
3419 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
3420 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
3421 static void generate_type_signature (dw_die_ref, comdat_type_node *);
3422 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3423 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
3424 static int same_attr_p (dw_attr_node *, dw_attr_node *, int *);
3425 static int same_die_p (dw_die_ref, dw_die_ref, int *);
3426 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
3427 static void compute_section_prefix (dw_die_ref);
3428 static int is_type_die (dw_die_ref);
3429 static int is_comdat_die (dw_die_ref);
3430 static int is_symbol_die (dw_die_ref);
3431 static inline bool is_template_instantiation (dw_die_ref);
3432 static void assign_symbol_names (dw_die_ref);
3433 static void break_out_includes (dw_die_ref);
3434 static int is_declaration_die (dw_die_ref);
3435 static int should_move_die_to_comdat (dw_die_ref);
3436 static dw_die_ref clone_as_declaration (dw_die_ref);
3437 static dw_die_ref clone_die (dw_die_ref);
3438 static dw_die_ref clone_tree (dw_die_ref);
3439 static dw_die_ref copy_declaration_context (dw_die_ref, dw_die_ref);
3440 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
3441 static void generate_skeleton_bottom_up (skeleton_chain_node *);
3442 static dw_die_ref generate_skeleton (dw_die_ref);
3443 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
3444 dw_die_ref,
3445 dw_die_ref);
3446 static void break_out_comdat_types (dw_die_ref);
3447 static void copy_decls_for_unworthy_types (dw_die_ref);
3449 static void add_sibling_attributes (dw_die_ref);
3450 static void output_location_lists (dw_die_ref);
3451 static int constant_size (unsigned HOST_WIDE_INT);
3452 static unsigned long size_of_die (dw_die_ref);
3453 static void calc_die_sizes (dw_die_ref);
3454 static void calc_base_type_die_sizes (void);
3455 static void mark_dies (dw_die_ref);
3456 static void unmark_dies (dw_die_ref);
3457 static void unmark_all_dies (dw_die_ref);
3458 static unsigned long size_of_pubnames (vec<pubname_entry, va_gc> *);
3459 static unsigned long size_of_aranges (void);
3460 static enum dwarf_form value_format (dw_attr_node *);
3461 static void output_value_format (dw_attr_node *);
3462 static void output_abbrev_section (void);
3463 static void output_die_abbrevs (unsigned long, dw_die_ref);
3464 static void output_die_symbol (dw_die_ref);
3465 static void output_die (dw_die_ref);
3466 static void output_compilation_unit_header (enum dwarf_unit_type);
3467 static void output_comp_unit (dw_die_ref, int, const unsigned char *);
3468 static void output_comdat_type_unit (comdat_type_node *);
3469 static const char *dwarf2_name (tree, int);
3470 static void add_pubname (tree, dw_die_ref);
3471 static void add_enumerator_pubname (const char *, dw_die_ref);
3472 static void add_pubname_string (const char *, dw_die_ref);
3473 static void add_pubtype (tree, dw_die_ref);
3474 static void output_pubnames (vec<pubname_entry, va_gc> *);
3475 static void output_aranges (void);
3476 static unsigned int add_ranges (const_tree, bool = false);
3477 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3478 bool *, bool);
3479 static void output_ranges (void);
3480 static dw_line_info_table *new_line_info_table (void);
3481 static void output_line_info (bool);
3482 static void output_file_names (void);
3483 static dw_die_ref base_type_die (tree, bool);
3484 static int is_base_type (tree);
3485 static dw_die_ref subrange_type_die (tree, tree, tree, tree, dw_die_ref);
3486 static int decl_quals (const_tree);
3487 static dw_die_ref modified_type_die (tree, int, bool, dw_die_ref);
3488 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3489 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3490 static int type_is_enum (const_tree);
3491 static unsigned int dbx_reg_number (const_rtx);
3492 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3493 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3494 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3495 enum var_init_status);
3496 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3497 enum var_init_status);
3498 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
3499 enum var_init_status);
3500 static int is_based_loc (const_rtx);
3501 static bool resolve_one_addr (rtx *);
3502 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3503 enum var_init_status);
3504 static dw_loc_descr_ref loc_descriptor (rtx, machine_mode mode,
3505 enum var_init_status);
3506 struct loc_descr_context;
3507 static void add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref);
3508 static void add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list);
3509 static dw_loc_list_ref loc_list_from_tree (tree, int,
3510 struct loc_descr_context *);
3511 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int,
3512 struct loc_descr_context *);
3513 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
3514 static tree field_type (const_tree);
3515 static unsigned int simple_type_align_in_bits (const_tree);
3516 static unsigned int simple_decl_align_in_bits (const_tree);
3517 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3518 struct vlr_context;
3519 static dw_loc_descr_ref field_byte_offset (const_tree, struct vlr_context *,
3520 HOST_WIDE_INT *);
3521 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3522 dw_loc_list_ref);
3523 static void add_data_member_location_attribute (dw_die_ref, tree,
3524 struct vlr_context *);
3525 static bool add_const_value_attribute (dw_die_ref, rtx);
3526 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3527 static void insert_wide_int (const wide_int &, unsigned char *, int);
3528 static void insert_float (const_rtx, unsigned char *);
3529 static rtx rtl_for_decl_location (tree);
3530 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool);
3531 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3532 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3533 static void add_name_attribute (dw_die_ref, const char *);
3534 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3535 static void add_comp_dir_attribute (dw_die_ref);
3536 static void add_scalar_info (dw_die_ref, enum dwarf_attribute, tree, int,
3537 struct loc_descr_context *);
3538 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree,
3539 struct loc_descr_context *);
3540 static void add_subscript_info (dw_die_ref, tree, bool);
3541 static void add_byte_size_attribute (dw_die_ref, tree);
3542 static void add_alignment_attribute (dw_die_ref, tree);
3543 static inline void add_bit_offset_attribute (dw_die_ref, tree,
3544 struct vlr_context *);
3545 static void add_bit_size_attribute (dw_die_ref, tree);
3546 static void add_prototyped_attribute (dw_die_ref, tree);
3547 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
3548 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3549 static void add_src_coords_attributes (dw_die_ref, tree);
3550 static void add_name_and_src_coords_attributes (dw_die_ref, tree, bool = false);
3551 static void add_discr_value (dw_die_ref, dw_discr_value *);
3552 static void add_discr_list (dw_die_ref, dw_discr_list_ref);
3553 static inline dw_discr_list_ref AT_discr_list (dw_attr_node *);
3554 static void push_decl_scope (tree);
3555 static void pop_decl_scope (void);
3556 static dw_die_ref scope_die_for (tree, dw_die_ref);
3557 static inline int local_scope_p (dw_die_ref);
3558 static inline int class_scope_p (dw_die_ref);
3559 static inline int class_or_namespace_scope_p (dw_die_ref);
3560 static void add_type_attribute (dw_die_ref, tree, int, bool, dw_die_ref);
3561 static void add_calling_convention_attribute (dw_die_ref, tree);
3562 static const char *type_tag (const_tree);
3563 static tree member_declared_type (const_tree);
3564 #if 0
3565 static const char *decl_start_label (tree);
3566 #endif
3567 static void gen_array_type_die (tree, dw_die_ref);
3568 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3569 #if 0
3570 static void gen_entry_point_die (tree, dw_die_ref);
3571 #endif
3572 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3573 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3574 static dw_die_ref gen_formal_parameter_pack_die (tree, tree, dw_die_ref, tree*);
3575 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3576 static void gen_formal_types_die (tree, dw_die_ref);
3577 static void gen_subprogram_die (tree, dw_die_ref);
3578 static void gen_variable_die (tree, tree, dw_die_ref);
3579 static void gen_const_die (tree, dw_die_ref);
3580 static void gen_label_die (tree, dw_die_ref);
3581 static void gen_lexical_block_die (tree, dw_die_ref);
3582 static void gen_inlined_subroutine_die (tree, dw_die_ref);
3583 static void gen_field_die (tree, struct vlr_context *, dw_die_ref);
3584 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3585 static dw_die_ref gen_compile_unit_die (const char *);
3586 static void gen_inheritance_die (tree, tree, tree, dw_die_ref);
3587 static void gen_member_die (tree, dw_die_ref);
3588 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3589 enum debug_info_usage);
3590 static void gen_subroutine_type_die (tree, dw_die_ref);
3591 static void gen_typedef_die (tree, dw_die_ref);
3592 static void gen_type_die (tree, dw_die_ref);
3593 static void gen_block_die (tree, dw_die_ref);
3594 static void decls_for_scope (tree, dw_die_ref);
3595 static bool is_naming_typedef_decl (const_tree);
3596 static inline dw_die_ref get_context_die (tree);
3597 static void gen_namespace_die (tree, dw_die_ref);
3598 static dw_die_ref gen_namelist_decl (tree, dw_die_ref, tree);
3599 static dw_die_ref gen_decl_die (tree, tree, struct vlr_context *, dw_die_ref);
3600 static dw_die_ref force_decl_die (tree);
3601 static dw_die_ref force_type_die (tree);
3602 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3603 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3604 static struct dwarf_file_data * lookup_filename (const char *);
3605 static void retry_incomplete_types (void);
3606 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3607 static void gen_generic_params_dies (tree);
3608 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
3609 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
3610 static void splice_child_die (dw_die_ref, dw_die_ref);
3611 static int file_info_cmp (const void *, const void *);
3612 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
3613 const char *, const char *);
3614 static void output_loc_list (dw_loc_list_ref);
3615 static char *gen_internal_sym (const char *);
3616 static bool want_pubnames (void);
3618 static void prune_unmark_dies (dw_die_ref);
3619 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3620 static void prune_unused_types_mark (dw_die_ref, int);
3621 static void prune_unused_types_walk (dw_die_ref);
3622 static void prune_unused_types_walk_attribs (dw_die_ref);
3623 static void prune_unused_types_prune (dw_die_ref);
3624 static void prune_unused_types (void);
3625 static int maybe_emit_file (struct dwarf_file_data *fd);
3626 static inline const char *AT_vms_delta1 (dw_attr_node *);
3627 static inline const char *AT_vms_delta2 (dw_attr_node *);
3628 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
3629 const char *, const char *);
3630 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
3631 static void gen_remaining_tmpl_value_param_die_attribute (void);
3632 static bool generic_type_p (tree);
3633 static void schedule_generic_params_dies_gen (tree t);
3634 static void gen_scheduled_generic_parms_dies (void);
3635 static void resolve_variable_values (void);
3637 static const char *comp_dir_string (void);
3639 static void hash_loc_operands (dw_loc_descr_ref, inchash::hash &);
3641 /* enum for tracking thread-local variables whose address is really an offset
3642 relative to the TLS pointer, which will need link-time relocation, but will
3643 not need relocation by the DWARF consumer. */
3645 enum dtprel_bool
3647 dtprel_false = 0,
3648 dtprel_true = 1
3651 /* Return the operator to use for an address of a variable. For dtprel_true, we
3652 use DW_OP_const*. For regular variables, which need both link-time
3653 relocation and consumer-level relocation (e.g., to account for shared objects
3654 loaded at a random address), we use DW_OP_addr*. */
3656 static inline enum dwarf_location_atom
3657 dw_addr_op (enum dtprel_bool dtprel)
3659 if (dtprel == dtprel_true)
3660 return (dwarf_split_debug_info ? DW_OP_GNU_const_index
3661 : (DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u));
3662 else
3663 return dwarf_split_debug_info ? DW_OP_GNU_addr_index : DW_OP_addr;
3666 /* Return a pointer to a newly allocated address location description. If
3667 dwarf_split_debug_info is true, then record the address with the appropriate
3668 relocation. */
3669 static inline dw_loc_descr_ref
3670 new_addr_loc_descr (rtx addr, enum dtprel_bool dtprel)
3672 dw_loc_descr_ref ref = new_loc_descr (dw_addr_op (dtprel), 0, 0);
3674 ref->dw_loc_oprnd1.val_class = dw_val_class_addr;
3675 ref->dw_loc_oprnd1.v.val_addr = addr;
3676 ref->dtprel = dtprel;
3677 if (dwarf_split_debug_info)
3678 ref->dw_loc_oprnd1.val_entry
3679 = add_addr_table_entry (addr,
3680 dtprel ? ate_kind_rtx_dtprel : ate_kind_rtx);
3681 else
3682 ref->dw_loc_oprnd1.val_entry = NULL;
3684 return ref;
3687 /* Section names used to hold DWARF debugging information. */
3689 #ifndef DEBUG_INFO_SECTION
3690 #define DEBUG_INFO_SECTION ".debug_info"
3691 #endif
3692 #ifndef DEBUG_DWO_INFO_SECTION
3693 #define DEBUG_DWO_INFO_SECTION ".debug_info.dwo"
3694 #endif
3695 #ifndef DEBUG_ABBREV_SECTION
3696 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
3697 #endif
3698 #ifndef DEBUG_DWO_ABBREV_SECTION
3699 #define DEBUG_DWO_ABBREV_SECTION ".debug_abbrev.dwo"
3700 #endif
3701 #ifndef DEBUG_ARANGES_SECTION
3702 #define DEBUG_ARANGES_SECTION ".debug_aranges"
3703 #endif
3704 #ifndef DEBUG_ADDR_SECTION
3705 #define DEBUG_ADDR_SECTION ".debug_addr"
3706 #endif
3707 #ifndef DEBUG_MACINFO_SECTION
3708 #define DEBUG_MACINFO_SECTION ".debug_macinfo"
3709 #endif
3710 #ifndef DEBUG_DWO_MACINFO_SECTION
3711 #define DEBUG_DWO_MACINFO_SECTION ".debug_macinfo.dwo"
3712 #endif
3713 #ifndef DEBUG_DWO_MACRO_SECTION
3714 #define DEBUG_DWO_MACRO_SECTION ".debug_macro.dwo"
3715 #endif
3716 #ifndef DEBUG_MACRO_SECTION
3717 #define DEBUG_MACRO_SECTION ".debug_macro"
3718 #endif
3719 #ifndef DEBUG_LINE_SECTION
3720 #define DEBUG_LINE_SECTION ".debug_line"
3721 #endif
3722 #ifndef DEBUG_DWO_LINE_SECTION
3723 #define DEBUG_DWO_LINE_SECTION ".debug_line.dwo"
3724 #endif
3725 #ifndef DEBUG_LOC_SECTION
3726 #define DEBUG_LOC_SECTION ".debug_loc"
3727 #endif
3728 #ifndef DEBUG_DWO_LOC_SECTION
3729 #define DEBUG_DWO_LOC_SECTION ".debug_loc.dwo"
3730 #endif
3731 #ifndef DEBUG_LOCLISTS_SECTION
3732 #define DEBUG_LOCLISTS_SECTION ".debug_loclists"
3733 #endif
3734 #ifndef DEBUG_DWO_LOCLISTS_SECTION
3735 #define DEBUG_DWO_LOCLISTS_SECTION ".debug_loclists.dwo"
3736 #endif
3737 #ifndef DEBUG_PUBNAMES_SECTION
3738 #define DEBUG_PUBNAMES_SECTION \
3739 ((debug_generate_pub_sections == 2) \
3740 ? ".debug_gnu_pubnames" : ".debug_pubnames")
3741 #endif
3742 #ifndef DEBUG_PUBTYPES_SECTION
3743 #define DEBUG_PUBTYPES_SECTION \
3744 ((debug_generate_pub_sections == 2) \
3745 ? ".debug_gnu_pubtypes" : ".debug_pubtypes")
3746 #endif
3747 #ifndef DEBUG_STR_OFFSETS_SECTION
3748 #define DEBUG_STR_OFFSETS_SECTION ".debug_str_offsets"
3749 #endif
3750 #ifndef DEBUG_DWO_STR_OFFSETS_SECTION
3751 #define DEBUG_DWO_STR_OFFSETS_SECTION ".debug_str_offsets.dwo"
3752 #endif
3753 #ifndef DEBUG_STR_DWO_SECTION
3754 #define DEBUG_STR_DWO_SECTION ".debug_str.dwo"
3755 #endif
3756 #ifndef DEBUG_STR_SECTION
3757 #define DEBUG_STR_SECTION ".debug_str"
3758 #endif
3759 #ifndef DEBUG_RANGES_SECTION
3760 #define DEBUG_RANGES_SECTION ".debug_ranges"
3761 #endif
3762 #ifndef DEBUG_RNGLISTS_SECTION
3763 #define DEBUG_RNGLISTS_SECTION ".debug_rnglists"
3764 #endif
3765 #ifndef DEBUG_LINE_STR_SECTION
3766 #define DEBUG_LINE_STR_SECTION ".debug_line_str"
3767 #endif
3769 /* Standard ELF section names for compiled code and data. */
3770 #ifndef TEXT_SECTION_NAME
3771 #define TEXT_SECTION_NAME ".text"
3772 #endif
3774 /* Section flags for .debug_str section. */
3775 #define DEBUG_STR_SECTION_FLAGS \
3776 (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings \
3777 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
3778 : SECTION_DEBUG)
3780 /* Section flags for .debug_str.dwo section. */
3781 #define DEBUG_STR_DWO_SECTION_FLAGS (SECTION_DEBUG | SECTION_EXCLUDE)
3783 /* Labels we insert at beginning sections we can reference instead of
3784 the section names themselves. */
3786 #ifndef TEXT_SECTION_LABEL
3787 #define TEXT_SECTION_LABEL "Ltext"
3788 #endif
3789 #ifndef COLD_TEXT_SECTION_LABEL
3790 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
3791 #endif
3792 #ifndef DEBUG_LINE_SECTION_LABEL
3793 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
3794 #endif
3795 #ifndef DEBUG_SKELETON_LINE_SECTION_LABEL
3796 #define DEBUG_SKELETON_LINE_SECTION_LABEL "Lskeleton_debug_line"
3797 #endif
3798 #ifndef DEBUG_INFO_SECTION_LABEL
3799 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
3800 #endif
3801 #ifndef DEBUG_SKELETON_INFO_SECTION_LABEL
3802 #define DEBUG_SKELETON_INFO_SECTION_LABEL "Lskeleton_debug_info"
3803 #endif
3804 #ifndef DEBUG_ABBREV_SECTION_LABEL
3805 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
3806 #endif
3807 #ifndef DEBUG_SKELETON_ABBREV_SECTION_LABEL
3808 #define DEBUG_SKELETON_ABBREV_SECTION_LABEL "Lskeleton_debug_abbrev"
3809 #endif
3810 #ifndef DEBUG_ADDR_SECTION_LABEL
3811 #define DEBUG_ADDR_SECTION_LABEL "Ldebug_addr"
3812 #endif
3813 #ifndef DEBUG_LOC_SECTION_LABEL
3814 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
3815 #endif
3816 #ifndef DEBUG_RANGES_SECTION_LABEL
3817 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
3818 #endif
3819 #ifndef DEBUG_MACINFO_SECTION_LABEL
3820 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
3821 #endif
3822 #ifndef DEBUG_MACRO_SECTION_LABEL
3823 #define DEBUG_MACRO_SECTION_LABEL "Ldebug_macro"
3824 #endif
3825 #define SKELETON_COMP_DIE_ABBREV 1
3826 #define SKELETON_TYPE_DIE_ABBREV 2
3828 /* Definitions of defaults for formats and names of various special
3829 (artificial) labels which may be generated within this file (when the -g
3830 options is used and DWARF2_DEBUGGING_INFO is in effect.
3831 If necessary, these may be overridden from within the tm.h file, but
3832 typically, overriding these defaults is unnecessary. */
3834 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3835 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3836 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3837 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3838 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3839 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3840 static char debug_skeleton_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3841 static char debug_skeleton_abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3842 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3843 static char debug_addr_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3844 static char debug_skeleton_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3845 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3846 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3847 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3848 static char ranges_base_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3850 #ifndef TEXT_END_LABEL
3851 #define TEXT_END_LABEL "Letext"
3852 #endif
3853 #ifndef COLD_END_LABEL
3854 #define COLD_END_LABEL "Letext_cold"
3855 #endif
3856 #ifndef BLOCK_BEGIN_LABEL
3857 #define BLOCK_BEGIN_LABEL "LBB"
3858 #endif
3859 #ifndef BLOCK_END_LABEL
3860 #define BLOCK_END_LABEL "LBE"
3861 #endif
3862 #ifndef LINE_CODE_LABEL
3863 #define LINE_CODE_LABEL "LM"
3864 #endif
3867 /* Return the root of the DIE's built for the current compilation unit. */
3868 static dw_die_ref
3869 comp_unit_die (void)
3871 if (!single_comp_unit_die)
3872 single_comp_unit_die = gen_compile_unit_die (NULL);
3873 return single_comp_unit_die;
3876 /* We allow a language front-end to designate a function that is to be
3877 called to "demangle" any name before it is put into a DIE. */
3879 static const char *(*demangle_name_func) (const char *);
3881 void
3882 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
3884 demangle_name_func = func;
3887 /* Test if rtl node points to a pseudo register. */
3889 static inline int
3890 is_pseudo_reg (const_rtx rtl)
3892 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
3893 || (GET_CODE (rtl) == SUBREG
3894 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
3897 /* Return a reference to a type, with its const and volatile qualifiers
3898 removed. */
3900 static inline tree
3901 type_main_variant (tree type)
3903 type = TYPE_MAIN_VARIANT (type);
3905 /* ??? There really should be only one main variant among any group of
3906 variants of a given type (and all of the MAIN_VARIANT values for all
3907 members of the group should point to that one type) but sometimes the C
3908 front-end messes this up for array types, so we work around that bug
3909 here. */
3910 if (TREE_CODE (type) == ARRAY_TYPE)
3911 while (type != TYPE_MAIN_VARIANT (type))
3912 type = TYPE_MAIN_VARIANT (type);
3914 return type;
3917 /* Return nonzero if the given type node represents a tagged type. */
3919 static inline int
3920 is_tagged_type (const_tree type)
3922 enum tree_code code = TREE_CODE (type);
3924 return (code == RECORD_TYPE || code == UNION_TYPE
3925 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
3928 /* Set label to debug_info_section_label + die_offset of a DIE reference. */
3930 static void
3931 get_ref_die_offset_label (char *label, dw_die_ref ref)
3933 sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
3936 /* Return die_offset of a DIE reference to a base type. */
3938 static unsigned long int
3939 get_base_type_offset (dw_die_ref ref)
3941 if (ref->die_offset)
3942 return ref->die_offset;
3943 if (comp_unit_die ()->die_abbrev)
3945 calc_base_type_die_sizes ();
3946 gcc_assert (ref->die_offset);
3948 return ref->die_offset;
3951 /* Return die_offset of a DIE reference other than base type. */
3953 static unsigned long int
3954 get_ref_die_offset (dw_die_ref ref)
3956 gcc_assert (ref->die_offset);
3957 return ref->die_offset;
3960 /* Convert a DIE tag into its string name. */
3962 static const char *
3963 dwarf_tag_name (unsigned int tag)
3965 const char *name = get_DW_TAG_name (tag);
3967 if (name != NULL)
3968 return name;
3970 return "DW_TAG_<unknown>";
3973 /* Convert a DWARF attribute code into its string name. */
3975 static const char *
3976 dwarf_attr_name (unsigned int attr)
3978 const char *name;
3980 switch (attr)
3982 #if VMS_DEBUGGING_INFO
3983 case DW_AT_HP_prologue:
3984 return "DW_AT_HP_prologue";
3985 #else
3986 case DW_AT_MIPS_loop_unroll_factor:
3987 return "DW_AT_MIPS_loop_unroll_factor";
3988 #endif
3990 #if VMS_DEBUGGING_INFO
3991 case DW_AT_HP_epilogue:
3992 return "DW_AT_HP_epilogue";
3993 #else
3994 case DW_AT_MIPS_stride:
3995 return "DW_AT_MIPS_stride";
3996 #endif
3999 name = get_DW_AT_name (attr);
4001 if (name != NULL)
4002 return name;
4004 return "DW_AT_<unknown>";
4007 /* Convert a DWARF value form code into its string name. */
4009 static const char *
4010 dwarf_form_name (unsigned int form)
4012 const char *name = get_DW_FORM_name (form);
4014 if (name != NULL)
4015 return name;
4017 return "DW_FORM_<unknown>";
4020 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
4021 instance of an inlined instance of a decl which is local to an inline
4022 function, so we have to trace all of the way back through the origin chain
4023 to find out what sort of node actually served as the original seed for the
4024 given block. */
4026 static tree
4027 decl_ultimate_origin (const_tree decl)
4029 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
4030 return NULL_TREE;
4032 /* DECL_ABSTRACT_ORIGIN can point to itself; ignore that if
4033 we're trying to output the abstract instance of this function. */
4034 if (DECL_ABSTRACT_P (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
4035 return NULL_TREE;
4037 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
4038 most distant ancestor, this should never happen. */
4039 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
4041 return DECL_ABSTRACT_ORIGIN (decl);
4044 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
4045 of a virtual function may refer to a base class, so we check the 'this'
4046 parameter. */
4048 static tree
4049 decl_class_context (tree decl)
4051 tree context = NULL_TREE;
4053 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
4054 context = DECL_CONTEXT (decl);
4055 else
4056 context = TYPE_MAIN_VARIANT
4057 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
4059 if (context && !TYPE_P (context))
4060 context = NULL_TREE;
4062 return context;
4065 /* Add an attribute/value pair to a DIE. */
4067 static inline void
4068 add_dwarf_attr (dw_die_ref die, dw_attr_node *attr)
4070 /* Maybe this should be an assert? */
4071 if (die == NULL)
4072 return;
4074 vec_safe_reserve (die->die_attr, 1);
4075 vec_safe_push (die->die_attr, *attr);
4078 static inline enum dw_val_class
4079 AT_class (dw_attr_node *a)
4081 return a->dw_attr_val.val_class;
4084 /* Return the index for any attribute that will be referenced with a
4085 DW_FORM_GNU_addr_index or DW_FORM_GNU_str_index. String indices
4086 are stored in dw_attr_val.v.val_str for reference counting
4087 pruning. */
4089 static inline unsigned int
4090 AT_index (dw_attr_node *a)
4092 if (AT_class (a) == dw_val_class_str)
4093 return a->dw_attr_val.v.val_str->index;
4094 else if (a->dw_attr_val.val_entry != NULL)
4095 return a->dw_attr_val.val_entry->index;
4096 return NOT_INDEXED;
4099 /* Add a flag value attribute to a DIE. */
4101 static inline void
4102 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
4104 dw_attr_node attr;
4106 attr.dw_attr = attr_kind;
4107 attr.dw_attr_val.val_class = dw_val_class_flag;
4108 attr.dw_attr_val.val_entry = NULL;
4109 attr.dw_attr_val.v.val_flag = flag;
4110 add_dwarf_attr (die, &attr);
4113 static inline unsigned
4114 AT_flag (dw_attr_node *a)
4116 gcc_assert (a && AT_class (a) == dw_val_class_flag);
4117 return a->dw_attr_val.v.val_flag;
4120 /* Add a signed integer attribute value to a DIE. */
4122 static inline void
4123 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
4125 dw_attr_node attr;
4127 attr.dw_attr = attr_kind;
4128 attr.dw_attr_val.val_class = dw_val_class_const;
4129 attr.dw_attr_val.val_entry = NULL;
4130 attr.dw_attr_val.v.val_int = int_val;
4131 add_dwarf_attr (die, &attr);
4134 static inline HOST_WIDE_INT
4135 AT_int (dw_attr_node *a)
4137 gcc_assert (a && (AT_class (a) == dw_val_class_const
4138 || AT_class (a) == dw_val_class_const_implicit));
4139 return a->dw_attr_val.v.val_int;
4142 /* Add an unsigned integer attribute value to a DIE. */
4144 static inline void
4145 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
4146 unsigned HOST_WIDE_INT unsigned_val)
4148 dw_attr_node attr;
4150 attr.dw_attr = attr_kind;
4151 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
4152 attr.dw_attr_val.val_entry = NULL;
4153 attr.dw_attr_val.v.val_unsigned = unsigned_val;
4154 add_dwarf_attr (die, &attr);
4157 static inline unsigned HOST_WIDE_INT
4158 AT_unsigned (dw_attr_node *a)
4160 gcc_assert (a && (AT_class (a) == dw_val_class_unsigned_const
4161 || AT_class (a) == dw_val_class_unsigned_const_implicit));
4162 return a->dw_attr_val.v.val_unsigned;
4165 /* Add an unsigned wide integer attribute value to a DIE. */
4167 static inline void
4168 add_AT_wide (dw_die_ref die, enum dwarf_attribute attr_kind,
4169 const wide_int& w)
4171 dw_attr_node attr;
4173 attr.dw_attr = attr_kind;
4174 attr.dw_attr_val.val_class = dw_val_class_wide_int;
4175 attr.dw_attr_val.val_entry = NULL;
4176 attr.dw_attr_val.v.val_wide = ggc_alloc<wide_int> ();
4177 *attr.dw_attr_val.v.val_wide = w;
4178 add_dwarf_attr (die, &attr);
4181 /* Add an unsigned double integer attribute value to a DIE. */
4183 static inline void
4184 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
4185 HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
4187 dw_attr_node attr;
4189 attr.dw_attr = attr_kind;
4190 attr.dw_attr_val.val_class = dw_val_class_const_double;
4191 attr.dw_attr_val.val_entry = NULL;
4192 attr.dw_attr_val.v.val_double.high = high;
4193 attr.dw_attr_val.v.val_double.low = low;
4194 add_dwarf_attr (die, &attr);
4197 /* Add a floating point attribute value to a DIE and return it. */
4199 static inline void
4200 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
4201 unsigned int length, unsigned int elt_size, unsigned char *array)
4203 dw_attr_node attr;
4205 attr.dw_attr = attr_kind;
4206 attr.dw_attr_val.val_class = dw_val_class_vec;
4207 attr.dw_attr_val.val_entry = NULL;
4208 attr.dw_attr_val.v.val_vec.length = length;
4209 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
4210 attr.dw_attr_val.v.val_vec.array = array;
4211 add_dwarf_attr (die, &attr);
4214 /* Add an 8-byte data attribute value to a DIE. */
4216 static inline void
4217 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
4218 unsigned char data8[8])
4220 dw_attr_node attr;
4222 attr.dw_attr = attr_kind;
4223 attr.dw_attr_val.val_class = dw_val_class_data8;
4224 attr.dw_attr_val.val_entry = NULL;
4225 memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
4226 add_dwarf_attr (die, &attr);
4229 /* Add DW_AT_low_pc and DW_AT_high_pc to a DIE. When using
4230 dwarf_split_debug_info, address attributes in dies destined for the
4231 final executable have force_direct set to avoid using indexed
4232 references. */
4234 static inline void
4235 add_AT_low_high_pc (dw_die_ref die, const char *lbl_low, const char *lbl_high,
4236 bool force_direct)
4238 dw_attr_node attr;
4239 char * lbl_id;
4241 lbl_id = xstrdup (lbl_low);
4242 attr.dw_attr = DW_AT_low_pc;
4243 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4244 attr.dw_attr_val.v.val_lbl_id = lbl_id;
4245 if (dwarf_split_debug_info && !force_direct)
4246 attr.dw_attr_val.val_entry
4247 = add_addr_table_entry (lbl_id, ate_kind_label);
4248 else
4249 attr.dw_attr_val.val_entry = NULL;
4250 add_dwarf_attr (die, &attr);
4252 attr.dw_attr = DW_AT_high_pc;
4253 if (dwarf_version < 4)
4254 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4255 else
4256 attr.dw_attr_val.val_class = dw_val_class_high_pc;
4257 lbl_id = xstrdup (lbl_high);
4258 attr.dw_attr_val.v.val_lbl_id = lbl_id;
4259 if (attr.dw_attr_val.val_class == dw_val_class_lbl_id
4260 && dwarf_split_debug_info && !force_direct)
4261 attr.dw_attr_val.val_entry
4262 = add_addr_table_entry (lbl_id, ate_kind_label);
4263 else
4264 attr.dw_attr_val.val_entry = NULL;
4265 add_dwarf_attr (die, &attr);
4268 /* Hash and equality functions for debug_str_hash. */
4270 hashval_t
4271 indirect_string_hasher::hash (indirect_string_node *x)
4273 return htab_hash_string (x->str);
4276 bool
4277 indirect_string_hasher::equal (indirect_string_node *x1, const char *x2)
4279 return strcmp (x1->str, x2) == 0;
4282 /* Add STR to the given string hash table. */
4284 static struct indirect_string_node *
4285 find_AT_string_in_table (const char *str,
4286 hash_table<indirect_string_hasher> *table)
4288 struct indirect_string_node *node;
4290 indirect_string_node **slot
4291 = table->find_slot_with_hash (str, htab_hash_string (str), INSERT);
4292 if (*slot == NULL)
4294 node = ggc_cleared_alloc<indirect_string_node> ();
4295 node->str = ggc_strdup (str);
4296 *slot = node;
4298 else
4299 node = *slot;
4301 node->refcount++;
4302 return node;
4305 /* Add STR to the indirect string hash table. */
4307 static struct indirect_string_node *
4308 find_AT_string (const char *str)
4310 if (! debug_str_hash)
4311 debug_str_hash = hash_table<indirect_string_hasher>::create_ggc (10);
4313 return find_AT_string_in_table (str, debug_str_hash);
4316 /* Add a string attribute value to a DIE. */
4318 static inline void
4319 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
4321 dw_attr_node attr;
4322 struct indirect_string_node *node;
4324 node = find_AT_string (str);
4326 attr.dw_attr = attr_kind;
4327 attr.dw_attr_val.val_class = dw_val_class_str;
4328 attr.dw_attr_val.val_entry = NULL;
4329 attr.dw_attr_val.v.val_str = node;
4330 add_dwarf_attr (die, &attr);
4333 static inline const char *
4334 AT_string (dw_attr_node *a)
4336 gcc_assert (a && AT_class (a) == dw_val_class_str);
4337 return a->dw_attr_val.v.val_str->str;
4340 /* Call this function directly to bypass AT_string_form's logic to put
4341 the string inline in the die. */
4343 static void
4344 set_indirect_string (struct indirect_string_node *node)
4346 char label[MAX_ARTIFICIAL_LABEL_BYTES];
4347 /* Already indirect is a no op. */
4348 if (node->form == DW_FORM_strp
4349 || node->form == DW_FORM_line_strp
4350 || node->form == DW_FORM_GNU_str_index)
4352 gcc_assert (node->label);
4353 return;
4355 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
4356 ++dw2_string_counter;
4357 node->label = xstrdup (label);
4359 if (!dwarf_split_debug_info)
4361 node->form = DW_FORM_strp;
4362 node->index = NOT_INDEXED;
4364 else
4366 node->form = DW_FORM_GNU_str_index;
4367 node->index = NO_INDEX_ASSIGNED;
4371 /* Find out whether a string should be output inline in DIE
4372 or out-of-line in .debug_str section. */
4374 static enum dwarf_form
4375 find_string_form (struct indirect_string_node *node)
4377 unsigned int len;
4379 if (node->form)
4380 return node->form;
4382 len = strlen (node->str) + 1;
4384 /* If the string is shorter or equal to the size of the reference, it is
4385 always better to put it inline. */
4386 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
4387 return node->form = DW_FORM_string;
4389 /* If we cannot expect the linker to merge strings in .debug_str
4390 section, only put it into .debug_str if it is worth even in this
4391 single module. */
4392 if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
4393 || ((debug_str_section->common.flags & SECTION_MERGE) == 0
4394 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
4395 return node->form = DW_FORM_string;
4397 set_indirect_string (node);
4399 return node->form;
4402 /* Find out whether the string referenced from the attribute should be
4403 output inline in DIE or out-of-line in .debug_str section. */
4405 static enum dwarf_form
4406 AT_string_form (dw_attr_node *a)
4408 gcc_assert (a && AT_class (a) == dw_val_class_str);
4409 return find_string_form (a->dw_attr_val.v.val_str);
4412 /* Add a DIE reference attribute value to a DIE. */
4414 static inline void
4415 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
4417 dw_attr_node attr;
4418 gcc_checking_assert (targ_die != NULL);
4420 /* With LTO we can end up trying to reference something we didn't create
4421 a DIE for. Avoid crashing later on a NULL referenced DIE. */
4422 if (targ_die == NULL)
4423 return;
4425 attr.dw_attr = attr_kind;
4426 attr.dw_attr_val.val_class = dw_val_class_die_ref;
4427 attr.dw_attr_val.val_entry = NULL;
4428 attr.dw_attr_val.v.val_die_ref.die = targ_die;
4429 attr.dw_attr_val.v.val_die_ref.external = 0;
4430 add_dwarf_attr (die, &attr);
4433 /* Change DIE reference REF to point to NEW_DIE instead. */
4435 static inline void
4436 change_AT_die_ref (dw_attr_node *ref, dw_die_ref new_die)
4438 gcc_assert (ref->dw_attr_val.val_class == dw_val_class_die_ref);
4439 ref->dw_attr_val.v.val_die_ref.die = new_die;
4440 ref->dw_attr_val.v.val_die_ref.external = 0;
4443 /* Add an AT_specification attribute to a DIE, and also make the back
4444 pointer from the specification to the definition. */
4446 static inline void
4447 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4449 add_AT_die_ref (die, DW_AT_specification, targ_die);
4450 gcc_assert (!targ_die->die_definition);
4451 targ_die->die_definition = die;
4454 static inline dw_die_ref
4455 AT_ref (dw_attr_node *a)
4457 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4458 return a->dw_attr_val.v.val_die_ref.die;
4461 static inline int
4462 AT_ref_external (dw_attr_node *a)
4464 if (a && AT_class (a) == dw_val_class_die_ref)
4465 return a->dw_attr_val.v.val_die_ref.external;
4467 return 0;
4470 static inline void
4471 set_AT_ref_external (dw_attr_node *a, int i)
4473 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4474 a->dw_attr_val.v.val_die_ref.external = i;
4477 /* Add an FDE reference attribute value to a DIE. */
4479 static inline void
4480 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
4482 dw_attr_node attr;
4484 attr.dw_attr = attr_kind;
4485 attr.dw_attr_val.val_class = dw_val_class_fde_ref;
4486 attr.dw_attr_val.val_entry = NULL;
4487 attr.dw_attr_val.v.val_fde_index = targ_fde;
4488 add_dwarf_attr (die, &attr);
4491 /* Add a location description attribute value to a DIE. */
4493 static inline void
4494 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4496 dw_attr_node attr;
4498 attr.dw_attr = attr_kind;
4499 attr.dw_attr_val.val_class = dw_val_class_loc;
4500 attr.dw_attr_val.val_entry = NULL;
4501 attr.dw_attr_val.v.val_loc = loc;
4502 add_dwarf_attr (die, &attr);
4505 static inline dw_loc_descr_ref
4506 AT_loc (dw_attr_node *a)
4508 gcc_assert (a && AT_class (a) == dw_val_class_loc);
4509 return a->dw_attr_val.v.val_loc;
4512 static inline void
4513 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4515 dw_attr_node attr;
4517 if (XCOFF_DEBUGGING_INFO && !HAVE_XCOFF_DWARF_EXTRAS)
4518 return;
4520 attr.dw_attr = attr_kind;
4521 attr.dw_attr_val.val_class = dw_val_class_loc_list;
4522 attr.dw_attr_val.val_entry = NULL;
4523 attr.dw_attr_val.v.val_loc_list = loc_list;
4524 add_dwarf_attr (die, &attr);
4525 have_location_lists = true;
4528 static inline dw_loc_list_ref
4529 AT_loc_list (dw_attr_node *a)
4531 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4532 return a->dw_attr_val.v.val_loc_list;
4535 static inline dw_loc_list_ref *
4536 AT_loc_list_ptr (dw_attr_node *a)
4538 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4539 return &a->dw_attr_val.v.val_loc_list;
4542 struct addr_hasher : ggc_ptr_hash<addr_table_entry>
4544 static hashval_t hash (addr_table_entry *);
4545 static bool equal (addr_table_entry *, addr_table_entry *);
4548 /* Table of entries into the .debug_addr section. */
4550 static GTY (()) hash_table<addr_hasher> *addr_index_table;
4552 /* Hash an address_table_entry. */
4554 hashval_t
4555 addr_hasher::hash (addr_table_entry *a)
4557 inchash::hash hstate;
4558 switch (a->kind)
4560 case ate_kind_rtx:
4561 hstate.add_int (0);
4562 break;
4563 case ate_kind_rtx_dtprel:
4564 hstate.add_int (1);
4565 break;
4566 case ate_kind_label:
4567 return htab_hash_string (a->addr.label);
4568 default:
4569 gcc_unreachable ();
4571 inchash::add_rtx (a->addr.rtl, hstate);
4572 return hstate.end ();
4575 /* Determine equality for two address_table_entries. */
4577 bool
4578 addr_hasher::equal (addr_table_entry *a1, addr_table_entry *a2)
4580 if (a1->kind != a2->kind)
4581 return 0;
4582 switch (a1->kind)
4584 case ate_kind_rtx:
4585 case ate_kind_rtx_dtprel:
4586 return rtx_equal_p (a1->addr.rtl, a2->addr.rtl);
4587 case ate_kind_label:
4588 return strcmp (a1->addr.label, a2->addr.label) == 0;
4589 default:
4590 gcc_unreachable ();
4594 /* Initialize an addr_table_entry. */
4596 void
4597 init_addr_table_entry (addr_table_entry *e, enum ate_kind kind, void *addr)
4599 e->kind = kind;
4600 switch (kind)
4602 case ate_kind_rtx:
4603 case ate_kind_rtx_dtprel:
4604 e->addr.rtl = (rtx) addr;
4605 break;
4606 case ate_kind_label:
4607 e->addr.label = (char *) addr;
4608 break;
4610 e->refcount = 0;
4611 e->index = NO_INDEX_ASSIGNED;
4614 /* Add attr to the address table entry to the table. Defer setting an
4615 index until output time. */
4617 static addr_table_entry *
4618 add_addr_table_entry (void *addr, enum ate_kind kind)
4620 addr_table_entry *node;
4621 addr_table_entry finder;
4623 gcc_assert (dwarf_split_debug_info);
4624 if (! addr_index_table)
4625 addr_index_table = hash_table<addr_hasher>::create_ggc (10);
4626 init_addr_table_entry (&finder, kind, addr);
4627 addr_table_entry **slot = addr_index_table->find_slot (&finder, INSERT);
4629 if (*slot == HTAB_EMPTY_ENTRY)
4631 node = ggc_cleared_alloc<addr_table_entry> ();
4632 init_addr_table_entry (node, kind, addr);
4633 *slot = node;
4635 else
4636 node = *slot;
4638 node->refcount++;
4639 return node;
4642 /* Remove an entry from the addr table by decrementing its refcount.
4643 Strictly, decrementing the refcount would be enough, but the
4644 assertion that the entry is actually in the table has found
4645 bugs. */
4647 static void
4648 remove_addr_table_entry (addr_table_entry *entry)
4650 gcc_assert (dwarf_split_debug_info && addr_index_table);
4651 /* After an index is assigned, the table is frozen. */
4652 gcc_assert (entry->refcount > 0 && entry->index == NO_INDEX_ASSIGNED);
4653 entry->refcount--;
4656 /* Given a location list, remove all addresses it refers to from the
4657 address_table. */
4659 static void
4660 remove_loc_list_addr_table_entries (dw_loc_descr_ref descr)
4662 for (; descr; descr = descr->dw_loc_next)
4663 if (descr->dw_loc_oprnd1.val_entry != NULL)
4665 gcc_assert (descr->dw_loc_oprnd1.val_entry->index == NO_INDEX_ASSIGNED);
4666 remove_addr_table_entry (descr->dw_loc_oprnd1.val_entry);
4670 /* A helper function for dwarf2out_finish called through
4671 htab_traverse. Assign an addr_table_entry its index. All entries
4672 must be collected into the table when this function is called,
4673 because the indexing code relies on htab_traverse to traverse nodes
4674 in the same order for each run. */
4677 index_addr_table_entry (addr_table_entry **h, unsigned int *index)
4679 addr_table_entry *node = *h;
4681 /* Don't index unreferenced nodes. */
4682 if (node->refcount == 0)
4683 return 1;
4685 gcc_assert (node->index == NO_INDEX_ASSIGNED);
4686 node->index = *index;
4687 *index += 1;
4689 return 1;
4692 /* Add an address constant attribute value to a DIE. When using
4693 dwarf_split_debug_info, address attributes in dies destined for the
4694 final executable should be direct references--setting the parameter
4695 force_direct ensures this behavior. */
4697 static inline void
4698 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr,
4699 bool force_direct)
4701 dw_attr_node attr;
4703 attr.dw_attr = attr_kind;
4704 attr.dw_attr_val.val_class = dw_val_class_addr;
4705 attr.dw_attr_val.v.val_addr = addr;
4706 if (dwarf_split_debug_info && !force_direct)
4707 attr.dw_attr_val.val_entry = add_addr_table_entry (addr, ate_kind_rtx);
4708 else
4709 attr.dw_attr_val.val_entry = NULL;
4710 add_dwarf_attr (die, &attr);
4713 /* Get the RTX from to an address DIE attribute. */
4715 static inline rtx
4716 AT_addr (dw_attr_node *a)
4718 gcc_assert (a && AT_class (a) == dw_val_class_addr);
4719 return a->dw_attr_val.v.val_addr;
4722 /* Add a file attribute value to a DIE. */
4724 static inline void
4725 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
4726 struct dwarf_file_data *fd)
4728 dw_attr_node attr;
4730 attr.dw_attr = attr_kind;
4731 attr.dw_attr_val.val_class = dw_val_class_file;
4732 attr.dw_attr_val.val_entry = NULL;
4733 attr.dw_attr_val.v.val_file = fd;
4734 add_dwarf_attr (die, &attr);
4737 /* Get the dwarf_file_data from a file DIE attribute. */
4739 static inline struct dwarf_file_data *
4740 AT_file (dw_attr_node *a)
4742 gcc_assert (a && (AT_class (a) == dw_val_class_file
4743 || AT_class (a) == dw_val_class_file_implicit));
4744 return a->dw_attr_val.v.val_file;
4747 /* Add a vms delta attribute value to a DIE. */
4749 static inline void
4750 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
4751 const char *lbl1, const char *lbl2)
4753 dw_attr_node attr;
4755 attr.dw_attr = attr_kind;
4756 attr.dw_attr_val.val_class = dw_val_class_vms_delta;
4757 attr.dw_attr_val.val_entry = NULL;
4758 attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
4759 attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
4760 add_dwarf_attr (die, &attr);
4763 /* Add a label identifier attribute value to a DIE. */
4765 static inline void
4766 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind,
4767 const char *lbl_id)
4769 dw_attr_node attr;
4771 attr.dw_attr = attr_kind;
4772 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4773 attr.dw_attr_val.val_entry = NULL;
4774 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
4775 if (dwarf_split_debug_info)
4776 attr.dw_attr_val.val_entry
4777 = add_addr_table_entry (attr.dw_attr_val.v.val_lbl_id,
4778 ate_kind_label);
4779 add_dwarf_attr (die, &attr);
4782 /* Add a section offset attribute value to a DIE, an offset into the
4783 debug_line section. */
4785 static inline void
4786 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4787 const char *label)
4789 dw_attr_node attr;
4791 attr.dw_attr = attr_kind;
4792 attr.dw_attr_val.val_class = dw_val_class_lineptr;
4793 attr.dw_attr_val.val_entry = NULL;
4794 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4795 add_dwarf_attr (die, &attr);
4798 /* Add a section offset attribute value to a DIE, an offset into the
4799 debug_loclists section. */
4801 static inline void
4802 add_AT_loclistsptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4803 const char *label)
4805 dw_attr_node attr;
4807 attr.dw_attr = attr_kind;
4808 attr.dw_attr_val.val_class = dw_val_class_loclistsptr;
4809 attr.dw_attr_val.val_entry = NULL;
4810 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4811 add_dwarf_attr (die, &attr);
4814 /* Add a section offset attribute value to a DIE, an offset into the
4815 debug_macinfo section. */
4817 static inline void
4818 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4819 const char *label)
4821 dw_attr_node attr;
4823 attr.dw_attr = attr_kind;
4824 attr.dw_attr_val.val_class = dw_val_class_macptr;
4825 attr.dw_attr_val.val_entry = NULL;
4826 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4827 add_dwarf_attr (die, &attr);
4830 /* Add an offset attribute value to a DIE. */
4832 static inline void
4833 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
4834 unsigned HOST_WIDE_INT offset)
4836 dw_attr_node attr;
4838 attr.dw_attr = attr_kind;
4839 attr.dw_attr_val.val_class = dw_val_class_offset;
4840 attr.dw_attr_val.val_entry = NULL;
4841 attr.dw_attr_val.v.val_offset = offset;
4842 add_dwarf_attr (die, &attr);
4845 /* Add a range_list attribute value to a DIE. When using
4846 dwarf_split_debug_info, address attributes in dies destined for the
4847 final executable should be direct references--setting the parameter
4848 force_direct ensures this behavior. */
4850 #define UNRELOCATED_OFFSET ((addr_table_entry *) 1)
4851 #define RELOCATED_OFFSET (NULL)
4853 static void
4854 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
4855 long unsigned int offset, bool force_direct)
4857 dw_attr_node attr;
4859 attr.dw_attr = attr_kind;
4860 attr.dw_attr_val.val_class = dw_val_class_range_list;
4861 /* For the range_list attribute, use val_entry to store whether the
4862 offset should follow split-debug-info or normal semantics. This
4863 value is read in output_range_list_offset. */
4864 if (dwarf_split_debug_info && !force_direct)
4865 attr.dw_attr_val.val_entry = UNRELOCATED_OFFSET;
4866 else
4867 attr.dw_attr_val.val_entry = RELOCATED_OFFSET;
4868 attr.dw_attr_val.v.val_offset = offset;
4869 add_dwarf_attr (die, &attr);
4872 /* Return the start label of a delta attribute. */
4874 static inline const char *
4875 AT_vms_delta1 (dw_attr_node *a)
4877 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4878 return a->dw_attr_val.v.val_vms_delta.lbl1;
4881 /* Return the end label of a delta attribute. */
4883 static inline const char *
4884 AT_vms_delta2 (dw_attr_node *a)
4886 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4887 return a->dw_attr_val.v.val_vms_delta.lbl2;
4890 static inline const char *
4891 AT_lbl (dw_attr_node *a)
4893 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
4894 || AT_class (a) == dw_val_class_lineptr
4895 || AT_class (a) == dw_val_class_macptr
4896 || AT_class (a) == dw_val_class_loclistsptr
4897 || AT_class (a) == dw_val_class_high_pc));
4898 return a->dw_attr_val.v.val_lbl_id;
4901 /* Get the attribute of type attr_kind. */
4903 static dw_attr_node *
4904 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4906 dw_attr_node *a;
4907 unsigned ix;
4908 dw_die_ref spec = NULL;
4910 if (! die)
4911 return NULL;
4913 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4914 if (a->dw_attr == attr_kind)
4915 return a;
4916 else if (a->dw_attr == DW_AT_specification
4917 || a->dw_attr == DW_AT_abstract_origin)
4918 spec = AT_ref (a);
4920 if (spec)
4921 return get_AT (spec, attr_kind);
4923 return NULL;
4926 /* Returns the parent of the declaration of DIE. */
4928 static dw_die_ref
4929 get_die_parent (dw_die_ref die)
4931 dw_die_ref t;
4933 if (!die)
4934 return NULL;
4936 if ((t = get_AT_ref (die, DW_AT_abstract_origin))
4937 || (t = get_AT_ref (die, DW_AT_specification)))
4938 die = t;
4940 return die->die_parent;
4943 /* Return the "low pc" attribute value, typically associated with a subprogram
4944 DIE. Return null if the "low pc" attribute is either not present, or if it
4945 cannot be represented as an assembler label identifier. */
4947 static inline const char *
4948 get_AT_low_pc (dw_die_ref die)
4950 dw_attr_node *a = get_AT (die, DW_AT_low_pc);
4952 return a ? AT_lbl (a) : NULL;
4955 /* Return the "high pc" attribute value, typically associated with a subprogram
4956 DIE. Return null if the "high pc" attribute is either not present, or if it
4957 cannot be represented as an assembler label identifier. */
4959 static inline const char *
4960 get_AT_hi_pc (dw_die_ref die)
4962 dw_attr_node *a = get_AT (die, DW_AT_high_pc);
4964 return a ? AT_lbl (a) : NULL;
4967 /* Return the value of the string attribute designated by ATTR_KIND, or
4968 NULL if it is not present. */
4970 static inline const char *
4971 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
4973 dw_attr_node *a = get_AT (die, attr_kind);
4975 return a ? AT_string (a) : NULL;
4978 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
4979 if it is not present. */
4981 static inline int
4982 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
4984 dw_attr_node *a = get_AT (die, attr_kind);
4986 return a ? AT_flag (a) : 0;
4989 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
4990 if it is not present. */
4992 static inline unsigned
4993 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
4995 dw_attr_node *a = get_AT (die, attr_kind);
4997 return a ? AT_unsigned (a) : 0;
5000 static inline dw_die_ref
5001 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
5003 dw_attr_node *a = get_AT (die, attr_kind);
5005 return a ? AT_ref (a) : NULL;
5008 static inline struct dwarf_file_data *
5009 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
5011 dw_attr_node *a = get_AT (die, attr_kind);
5013 return a ? AT_file (a) : NULL;
5016 /* Return TRUE if the language is C++. */
5018 static inline bool
5019 is_cxx (void)
5021 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5023 return (lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus
5024 || lang == DW_LANG_C_plus_plus_11 || lang == DW_LANG_C_plus_plus_14);
5027 /* Return TRUE if DECL was created by the C++ frontend. */
5029 static bool
5030 is_cxx (const_tree decl)
5032 if (in_lto_p)
5034 const_tree context = decl;
5035 while (context && TREE_CODE (context) != TRANSLATION_UNIT_DECL)
5037 if (TREE_CODE (context) == BLOCK)
5038 context = BLOCK_SUPERCONTEXT (context);
5039 else
5040 context = get_containing_scope (context);
5042 if (context && TRANSLATION_UNIT_LANGUAGE (context))
5043 return strncmp (TRANSLATION_UNIT_LANGUAGE (context), "GNU C++", 7) == 0;
5045 return is_cxx ();
5048 /* Return TRUE if the language is Java. */
5050 static inline bool
5051 is_java (void)
5053 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5055 return lang == DW_LANG_Java;
5058 /* Return TRUE if the language is Fortran. */
5060 static inline bool
5061 is_fortran (void)
5063 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5065 return (lang == DW_LANG_Fortran77
5066 || lang == DW_LANG_Fortran90
5067 || lang == DW_LANG_Fortran95
5068 || lang == DW_LANG_Fortran03
5069 || lang == DW_LANG_Fortran08);
5072 /* Return TRUE if the language is Ada. */
5074 static inline bool
5075 is_ada (void)
5077 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5079 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
5082 /* Remove the specified attribute if present. Return TRUE if removal
5083 was successful. */
5085 static bool
5086 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5088 dw_attr_node *a;
5089 unsigned ix;
5091 if (! die)
5092 return false;
5094 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5095 if (a->dw_attr == attr_kind)
5097 if (AT_class (a) == dw_val_class_str)
5098 if (a->dw_attr_val.v.val_str->refcount)
5099 a->dw_attr_val.v.val_str->refcount--;
5101 /* vec::ordered_remove should help reduce the number of abbrevs
5102 that are needed. */
5103 die->die_attr->ordered_remove (ix);
5104 return true;
5106 return false;
5109 /* Remove CHILD from its parent. PREV must have the property that
5110 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
5112 static void
5113 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
5115 gcc_assert (child->die_parent == prev->die_parent);
5116 gcc_assert (prev->die_sib == child);
5117 if (prev == child)
5119 gcc_assert (child->die_parent->die_child == child);
5120 prev = NULL;
5122 else
5123 prev->die_sib = child->die_sib;
5124 if (child->die_parent->die_child == child)
5125 child->die_parent->die_child = prev;
5126 child->die_sib = NULL;
5129 /* Replace OLD_CHILD with NEW_CHILD. PREV must have the property that
5130 PREV->DIE_SIB == OLD_CHILD. Does not alter OLD_CHILD. */
5132 static void
5133 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
5135 dw_die_ref parent = old_child->die_parent;
5137 gcc_assert (parent == prev->die_parent);
5138 gcc_assert (prev->die_sib == old_child);
5140 new_child->die_parent = parent;
5141 if (prev == old_child)
5143 gcc_assert (parent->die_child == old_child);
5144 new_child->die_sib = new_child;
5146 else
5148 prev->die_sib = new_child;
5149 new_child->die_sib = old_child->die_sib;
5151 if (old_child->die_parent->die_child == old_child)
5152 old_child->die_parent->die_child = new_child;
5153 old_child->die_sib = NULL;
5156 /* Move all children from OLD_PARENT to NEW_PARENT. */
5158 static void
5159 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
5161 dw_die_ref c;
5162 new_parent->die_child = old_parent->die_child;
5163 old_parent->die_child = NULL;
5164 FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
5167 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
5168 matches TAG. */
5170 static void
5171 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
5173 dw_die_ref c;
5175 c = die->die_child;
5176 if (c) do {
5177 dw_die_ref prev = c;
5178 c = c->die_sib;
5179 while (c->die_tag == tag)
5181 remove_child_with_prev (c, prev);
5182 c->die_parent = NULL;
5183 /* Might have removed every child. */
5184 if (die->die_child == NULL)
5185 return;
5186 c = prev->die_sib;
5188 } while (c != die->die_child);
5191 /* Add a CHILD_DIE as the last child of DIE. */
5193 static void
5194 add_child_die (dw_die_ref die, dw_die_ref child_die)
5196 /* FIXME this should probably be an assert. */
5197 if (! die || ! child_die)
5198 return;
5199 gcc_assert (die != child_die);
5201 child_die->die_parent = die;
5202 if (die->die_child)
5204 child_die->die_sib = die->die_child->die_sib;
5205 die->die_child->die_sib = child_die;
5207 else
5208 child_die->die_sib = child_die;
5209 die->die_child = child_die;
5212 /* Like add_child_die, but put CHILD_DIE after AFTER_DIE. */
5214 static void
5215 add_child_die_after (dw_die_ref die, dw_die_ref child_die,
5216 dw_die_ref after_die)
5218 gcc_assert (die
5219 && child_die
5220 && after_die
5221 && die->die_child
5222 && die != child_die);
5224 child_die->die_parent = die;
5225 child_die->die_sib = after_die->die_sib;
5226 after_die->die_sib = child_die;
5227 if (die->die_child == after_die)
5228 die->die_child = child_die;
5231 /* Unassociate CHILD from its parent, and make its parent be
5232 NEW_PARENT. */
5234 static void
5235 reparent_child (dw_die_ref child, dw_die_ref new_parent)
5237 for (dw_die_ref p = child->die_parent->die_child; ; p = p->die_sib)
5238 if (p->die_sib == child)
5240 remove_child_with_prev (child, p);
5241 break;
5243 add_child_die (new_parent, child);
5246 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
5247 is the specification, to the end of PARENT's list of children.
5248 This is done by removing and re-adding it. */
5250 static void
5251 splice_child_die (dw_die_ref parent, dw_die_ref child)
5253 /* We want the declaration DIE from inside the class, not the
5254 specification DIE at toplevel. */
5255 if (child->die_parent != parent)
5257 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
5259 if (tmp)
5260 child = tmp;
5263 gcc_assert (child->die_parent == parent
5264 || (child->die_parent
5265 == get_AT_ref (parent, DW_AT_specification)));
5267 reparent_child (child, parent);
5270 /* Create and return a new die with a parent of PARENT_DIE. If
5271 PARENT_DIE is NULL, the new DIE is placed in limbo and an
5272 associated tree T must be supplied to determine parenthood
5273 later. */
5275 static inline dw_die_ref
5276 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
5278 dw_die_ref die = ggc_cleared_alloc<die_node> ();
5280 die->die_tag = tag_value;
5282 if (parent_die != NULL)
5283 add_child_die (parent_die, die);
5284 else
5286 limbo_die_node *limbo_node;
5288 /* No DIEs created after early dwarf should end up in limbo,
5289 because the limbo list should not persist past LTO
5290 streaming. */
5291 if (tag_value != DW_TAG_compile_unit
5292 /* These are allowed because they're generated while
5293 breaking out COMDAT units late. */
5294 && tag_value != DW_TAG_type_unit
5295 && tag_value != DW_TAG_skeleton_unit
5296 && !early_dwarf
5297 /* Allow nested functions to live in limbo because they will
5298 only temporarily live there, as decls_for_scope will fix
5299 them up. */
5300 && (TREE_CODE (t) != FUNCTION_DECL
5301 || !decl_function_context (t))
5302 /* Same as nested functions above but for types. Types that
5303 are local to a function will be fixed in
5304 decls_for_scope. */
5305 && (!RECORD_OR_UNION_TYPE_P (t)
5306 || !TYPE_CONTEXT (t)
5307 || TREE_CODE (TYPE_CONTEXT (t)) != FUNCTION_DECL)
5308 /* FIXME debug-early: Allow late limbo DIE creation for LTO,
5309 especially in the ltrans stage, but once we implement LTO
5310 dwarf streaming, we should remove this exception. */
5311 && !in_lto_p)
5313 fprintf (stderr, "symbol ended up in limbo too late:");
5314 debug_generic_stmt (t);
5315 gcc_unreachable ();
5318 limbo_node = ggc_cleared_alloc<limbo_die_node> ();
5319 limbo_node->die = die;
5320 limbo_node->created_for = t;
5321 limbo_node->next = limbo_die_list;
5322 limbo_die_list = limbo_node;
5325 return die;
5328 /* Return the DIE associated with the given type specifier. */
5330 static inline dw_die_ref
5331 lookup_type_die (tree type)
5333 dw_die_ref die = TYPE_SYMTAB_DIE (type);
5334 if (die && die->removed)
5336 TYPE_SYMTAB_DIE (type) = NULL;
5337 return NULL;
5339 return die;
5342 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
5343 anonymous type named by the typedef TYPE_DIE, return the DIE of the
5344 anonymous type instead the one of the naming typedef. */
5346 static inline dw_die_ref
5347 strip_naming_typedef (tree type, dw_die_ref type_die)
5349 if (type
5350 && TREE_CODE (type) == RECORD_TYPE
5351 && type_die
5352 && type_die->die_tag == DW_TAG_typedef
5353 && is_naming_typedef_decl (TYPE_NAME (type)))
5354 type_die = get_AT_ref (type_die, DW_AT_type);
5355 return type_die;
5358 /* Like lookup_type_die, but if type is an anonymous type named by a
5359 typedef[1], return the DIE of the anonymous type instead the one of
5360 the naming typedef. This is because in gen_typedef_die, we did
5361 equate the anonymous struct named by the typedef with the DIE of
5362 the naming typedef. So by default, lookup_type_die on an anonymous
5363 struct yields the DIE of the naming typedef.
5365 [1]: Read the comment of is_naming_typedef_decl to learn about what
5366 a naming typedef is. */
5368 static inline dw_die_ref
5369 lookup_type_die_strip_naming_typedef (tree type)
5371 dw_die_ref die = lookup_type_die (type);
5372 return strip_naming_typedef (type, die);
5375 /* Equate a DIE to a given type specifier. */
5377 static inline void
5378 equate_type_number_to_die (tree type, dw_die_ref type_die)
5380 TYPE_SYMTAB_DIE (type) = type_die;
5383 /* Returns a hash value for X (which really is a die_struct). */
5385 inline hashval_t
5386 decl_die_hasher::hash (die_node *x)
5388 return (hashval_t) x->decl_id;
5391 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
5393 inline bool
5394 decl_die_hasher::equal (die_node *x, tree y)
5396 return (x->decl_id == DECL_UID (y));
5399 /* Return the DIE associated with a given declaration. */
5401 static inline dw_die_ref
5402 lookup_decl_die (tree decl)
5404 dw_die_ref *die = decl_die_table->find_slot_with_hash (decl, DECL_UID (decl),
5405 NO_INSERT);
5406 if (!die)
5407 return NULL;
5408 if ((*die)->removed)
5410 decl_die_table->clear_slot (die);
5411 return NULL;
5413 return *die;
5416 /* Returns a hash value for X (which really is a var_loc_list). */
5418 inline hashval_t
5419 decl_loc_hasher::hash (var_loc_list *x)
5421 return (hashval_t) x->decl_id;
5424 /* Return nonzero if decl_id of var_loc_list X is the same as
5425 UID of decl *Y. */
5427 inline bool
5428 decl_loc_hasher::equal (var_loc_list *x, const_tree y)
5430 return (x->decl_id == DECL_UID (y));
5433 /* Return the var_loc list associated with a given declaration. */
5435 static inline var_loc_list *
5436 lookup_decl_loc (const_tree decl)
5438 if (!decl_loc_table)
5439 return NULL;
5440 return decl_loc_table->find_with_hash (decl, DECL_UID (decl));
5443 /* Returns a hash value for X (which really is a cached_dw_loc_list_list). */
5445 inline hashval_t
5446 dw_loc_list_hasher::hash (cached_dw_loc_list *x)
5448 return (hashval_t) x->decl_id;
5451 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
5452 UID of decl *Y. */
5454 inline bool
5455 dw_loc_list_hasher::equal (cached_dw_loc_list *x, const_tree y)
5457 return (x->decl_id == DECL_UID (y));
5460 /* Equate a DIE to a particular declaration. */
5462 static void
5463 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
5465 unsigned int decl_id = DECL_UID (decl);
5467 *decl_die_table->find_slot_with_hash (decl, decl_id, INSERT) = decl_die;
5468 decl_die->decl_id = decl_id;
5471 /* Return how many bits covers PIECE EXPR_LIST. */
5473 static HOST_WIDE_INT
5474 decl_piece_bitsize (rtx piece)
5476 int ret = (int) GET_MODE (piece);
5477 if (ret)
5478 return ret;
5479 gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
5480 && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
5481 return INTVAL (XEXP (XEXP (piece, 0), 0));
5484 /* Return pointer to the location of location note in PIECE EXPR_LIST. */
5486 static rtx *
5487 decl_piece_varloc_ptr (rtx piece)
5489 if ((int) GET_MODE (piece))
5490 return &XEXP (piece, 0);
5491 else
5492 return &XEXP (XEXP (piece, 0), 1);
5495 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
5496 Next is the chain of following piece nodes. */
5498 static rtx_expr_list *
5499 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
5501 if (bitsize > 0 && bitsize <= (int) MAX_MACHINE_MODE)
5502 return alloc_EXPR_LIST (bitsize, loc_note, next);
5503 else
5504 return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
5505 GEN_INT (bitsize),
5506 loc_note), next);
5509 /* Return rtx that should be stored into loc field for
5510 LOC_NOTE and BITPOS/BITSIZE. */
5512 static rtx
5513 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
5514 HOST_WIDE_INT bitsize)
5516 if (bitsize != -1)
5518 loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
5519 if (bitpos != 0)
5520 loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
5522 return loc_note;
5525 /* This function either modifies location piece list *DEST in
5526 place (if SRC and INNER is NULL), or copies location piece list
5527 *SRC to *DEST while modifying it. Location BITPOS is modified
5528 to contain LOC_NOTE, any pieces overlapping it are removed resp.
5529 not copied and if needed some padding around it is added.
5530 When modifying in place, DEST should point to EXPR_LIST where
5531 earlier pieces cover PIECE_BITPOS bits, when copying SRC points
5532 to the start of the whole list and INNER points to the EXPR_LIST
5533 where earlier pieces cover PIECE_BITPOS bits. */
5535 static void
5536 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
5537 HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
5538 HOST_WIDE_INT bitsize, rtx loc_note)
5540 HOST_WIDE_INT diff;
5541 bool copy = inner != NULL;
5543 if (copy)
5545 /* First copy all nodes preceding the current bitpos. */
5546 while (src != inner)
5548 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5549 decl_piece_bitsize (*src), NULL_RTX);
5550 dest = &XEXP (*dest, 1);
5551 src = &XEXP (*src, 1);
5554 /* Add padding if needed. */
5555 if (bitpos != piece_bitpos)
5557 *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
5558 copy ? NULL_RTX : *dest);
5559 dest = &XEXP (*dest, 1);
5561 else if (*dest && decl_piece_bitsize (*dest) == bitsize)
5563 gcc_assert (!copy);
5564 /* A piece with correct bitpos and bitsize already exist,
5565 just update the location for it and return. */
5566 *decl_piece_varloc_ptr (*dest) = loc_note;
5567 return;
5569 /* Add the piece that changed. */
5570 *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
5571 dest = &XEXP (*dest, 1);
5572 /* Skip over pieces that overlap it. */
5573 diff = bitpos - piece_bitpos + bitsize;
5574 if (!copy)
5575 src = dest;
5576 while (diff > 0 && *src)
5578 rtx piece = *src;
5579 diff -= decl_piece_bitsize (piece);
5580 if (copy)
5581 src = &XEXP (piece, 1);
5582 else
5584 *src = XEXP (piece, 1);
5585 free_EXPR_LIST_node (piece);
5588 /* Add padding if needed. */
5589 if (diff < 0 && *src)
5591 if (!copy)
5592 dest = src;
5593 *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
5594 dest = &XEXP (*dest, 1);
5596 if (!copy)
5597 return;
5598 /* Finally copy all nodes following it. */
5599 while (*src)
5601 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5602 decl_piece_bitsize (*src), NULL_RTX);
5603 dest = &XEXP (*dest, 1);
5604 src = &XEXP (*src, 1);
5608 /* Add a variable location node to the linked list for DECL. */
5610 static struct var_loc_node *
5611 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
5613 unsigned int decl_id;
5614 var_loc_list *temp;
5615 struct var_loc_node *loc = NULL;
5616 HOST_WIDE_INT bitsize = -1, bitpos = -1;
5618 if (VAR_P (decl) && DECL_HAS_DEBUG_EXPR_P (decl))
5620 tree realdecl = DECL_DEBUG_EXPR (decl);
5621 if (handled_component_p (realdecl)
5622 || (TREE_CODE (realdecl) == MEM_REF
5623 && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR))
5625 HOST_WIDE_INT maxsize;
5626 bool reverse;
5627 tree innerdecl
5628 = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize,
5629 &reverse);
5630 if (!DECL_P (innerdecl)
5631 || DECL_IGNORED_P (innerdecl)
5632 || TREE_STATIC (innerdecl)
5633 || bitsize <= 0
5634 || bitpos + bitsize > 256
5635 || bitsize != maxsize)
5636 return NULL;
5637 decl = innerdecl;
5641 decl_id = DECL_UID (decl);
5642 var_loc_list **slot
5643 = decl_loc_table->find_slot_with_hash (decl, decl_id, INSERT);
5644 if (*slot == NULL)
5646 temp = ggc_cleared_alloc<var_loc_list> ();
5647 temp->decl_id = decl_id;
5648 *slot = temp;
5650 else
5651 temp = *slot;
5653 /* For PARM_DECLs try to keep around the original incoming value,
5654 even if that means we'll emit a zero-range .debug_loc entry. */
5655 if (temp->last
5656 && temp->first == temp->last
5657 && TREE_CODE (decl) == PARM_DECL
5658 && NOTE_P (temp->first->loc)
5659 && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
5660 && DECL_INCOMING_RTL (decl)
5661 && NOTE_VAR_LOCATION_LOC (temp->first->loc)
5662 && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
5663 == GET_CODE (DECL_INCOMING_RTL (decl))
5664 && prev_real_insn (as_a<rtx_insn *> (temp->first->loc)) == NULL_RTX
5665 && (bitsize != -1
5666 || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
5667 NOTE_VAR_LOCATION_LOC (loc_note))
5668 || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
5669 != NOTE_VAR_LOCATION_STATUS (loc_note))))
5671 loc = ggc_cleared_alloc<var_loc_node> ();
5672 temp->first->next = loc;
5673 temp->last = loc;
5674 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5676 else if (temp->last)
5678 struct var_loc_node *last = temp->last, *unused = NULL;
5679 rtx *piece_loc = NULL, last_loc_note;
5680 HOST_WIDE_INT piece_bitpos = 0;
5681 if (last->next)
5683 last = last->next;
5684 gcc_assert (last->next == NULL);
5686 if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
5688 piece_loc = &last->loc;
5691 HOST_WIDE_INT cur_bitsize = decl_piece_bitsize (*piece_loc);
5692 if (piece_bitpos + cur_bitsize > bitpos)
5693 break;
5694 piece_bitpos += cur_bitsize;
5695 piece_loc = &XEXP (*piece_loc, 1);
5697 while (*piece_loc);
5699 /* TEMP->LAST here is either pointer to the last but one or
5700 last element in the chained list, LAST is pointer to the
5701 last element. */
5702 if (label && strcmp (last->label, label) == 0)
5704 /* For SRA optimized variables if there weren't any real
5705 insns since last note, just modify the last node. */
5706 if (piece_loc != NULL)
5708 adjust_piece_list (piece_loc, NULL, NULL,
5709 bitpos, piece_bitpos, bitsize, loc_note);
5710 return NULL;
5712 /* If the last note doesn't cover any instructions, remove it. */
5713 if (temp->last != last)
5715 temp->last->next = NULL;
5716 unused = last;
5717 last = temp->last;
5718 gcc_assert (strcmp (last->label, label) != 0);
5720 else
5722 gcc_assert (temp->first == temp->last
5723 || (temp->first->next == temp->last
5724 && TREE_CODE (decl) == PARM_DECL));
5725 memset (temp->last, '\0', sizeof (*temp->last));
5726 temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
5727 return temp->last;
5730 if (bitsize == -1 && NOTE_P (last->loc))
5731 last_loc_note = last->loc;
5732 else if (piece_loc != NULL
5733 && *piece_loc != NULL_RTX
5734 && piece_bitpos == bitpos
5735 && decl_piece_bitsize (*piece_loc) == bitsize)
5736 last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
5737 else
5738 last_loc_note = NULL_RTX;
5739 /* If the current location is the same as the end of the list,
5740 and either both or neither of the locations is uninitialized,
5741 we have nothing to do. */
5742 if (last_loc_note == NULL_RTX
5743 || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
5744 NOTE_VAR_LOCATION_LOC (loc_note)))
5745 || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5746 != NOTE_VAR_LOCATION_STATUS (loc_note))
5747 && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5748 == VAR_INIT_STATUS_UNINITIALIZED)
5749 || (NOTE_VAR_LOCATION_STATUS (loc_note)
5750 == VAR_INIT_STATUS_UNINITIALIZED))))
5752 /* Add LOC to the end of list and update LAST. If the last
5753 element of the list has been removed above, reuse its
5754 memory for the new node, otherwise allocate a new one. */
5755 if (unused)
5757 loc = unused;
5758 memset (loc, '\0', sizeof (*loc));
5760 else
5761 loc = ggc_cleared_alloc<var_loc_node> ();
5762 if (bitsize == -1 || piece_loc == NULL)
5763 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5764 else
5765 adjust_piece_list (&loc->loc, &last->loc, piece_loc,
5766 bitpos, piece_bitpos, bitsize, loc_note);
5767 last->next = loc;
5768 /* Ensure TEMP->LAST will point either to the new last but one
5769 element of the chain, or to the last element in it. */
5770 if (last != temp->last)
5771 temp->last = last;
5773 else if (unused)
5774 ggc_free (unused);
5776 else
5778 loc = ggc_cleared_alloc<var_loc_node> ();
5779 temp->first = loc;
5780 temp->last = loc;
5781 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5783 return loc;
5786 /* Keep track of the number of spaces used to indent the
5787 output of the debugging routines that print the structure of
5788 the DIE internal representation. */
5789 static int print_indent;
5791 /* Indent the line the number of spaces given by print_indent. */
5793 static inline void
5794 print_spaces (FILE *outfile)
5796 fprintf (outfile, "%*s", print_indent, "");
5799 /* Print a type signature in hex. */
5801 static inline void
5802 print_signature (FILE *outfile, char *sig)
5804 int i;
5806 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
5807 fprintf (outfile, "%02x", sig[i] & 0xff);
5810 static inline void
5811 print_discr_value (FILE *outfile, dw_discr_value *discr_value)
5813 if (discr_value->pos)
5814 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, discr_value->v.sval);
5815 else
5816 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, discr_value->v.uval);
5819 static void print_loc_descr (dw_loc_descr_ref, FILE *);
5821 /* Print the value associated to the VAL DWARF value node to OUTFILE. If
5822 RECURSE, output location descriptor operations. */
5824 static void
5825 print_dw_val (dw_val_node *val, bool recurse, FILE *outfile)
5827 switch (val->val_class)
5829 case dw_val_class_addr:
5830 fprintf (outfile, "address");
5831 break;
5832 case dw_val_class_offset:
5833 fprintf (outfile, "offset");
5834 break;
5835 case dw_val_class_loc:
5836 fprintf (outfile, "location descriptor");
5837 if (val->v.val_loc == NULL)
5838 fprintf (outfile, " -> <null>\n");
5839 else if (recurse)
5841 fprintf (outfile, ":\n");
5842 print_indent += 4;
5843 print_loc_descr (val->v.val_loc, outfile);
5844 print_indent -= 4;
5846 else
5847 fprintf (outfile, " (%p)\n", (void *) val->v.val_loc);
5848 break;
5849 case dw_val_class_loc_list:
5850 fprintf (outfile, "location list -> label:%s",
5851 val->v.val_loc_list->ll_symbol);
5852 break;
5853 case dw_val_class_range_list:
5854 fprintf (outfile, "range list");
5855 break;
5856 case dw_val_class_const:
5857 case dw_val_class_const_implicit:
5858 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, val->v.val_int);
5859 break;
5860 case dw_val_class_unsigned_const:
5861 case dw_val_class_unsigned_const_implicit:
5862 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, val->v.val_unsigned);
5863 break;
5864 case dw_val_class_const_double:
5865 fprintf (outfile, "constant (" HOST_WIDE_INT_PRINT_DEC","\
5866 HOST_WIDE_INT_PRINT_UNSIGNED")",
5867 val->v.val_double.high,
5868 val->v.val_double.low);
5869 break;
5870 case dw_val_class_wide_int:
5872 int i = val->v.val_wide->get_len ();
5873 fprintf (outfile, "constant (");
5874 gcc_assert (i > 0);
5875 if (val->v.val_wide->elt (i - 1) == 0)
5876 fprintf (outfile, "0x");
5877 fprintf (outfile, HOST_WIDE_INT_PRINT_HEX,
5878 val->v.val_wide->elt (--i));
5879 while (--i >= 0)
5880 fprintf (outfile, HOST_WIDE_INT_PRINT_PADDED_HEX,
5881 val->v.val_wide->elt (i));
5882 fprintf (outfile, ")");
5883 break;
5885 case dw_val_class_vec:
5886 fprintf (outfile, "floating-point or vector constant");
5887 break;
5888 case dw_val_class_flag:
5889 fprintf (outfile, "%u", val->v.val_flag);
5890 break;
5891 case dw_val_class_die_ref:
5892 if (val->v.val_die_ref.die != NULL)
5894 dw_die_ref die = val->v.val_die_ref.die;
5896 if (die->comdat_type_p)
5898 fprintf (outfile, "die -> signature: ");
5899 print_signature (outfile,
5900 die->die_id.die_type_node->signature);
5902 else if (die->die_id.die_symbol)
5903 fprintf (outfile, "die -> label: %s", die->die_id.die_symbol);
5904 else
5905 fprintf (outfile, "die -> %ld", die->die_offset);
5906 fprintf (outfile, " (%p)", (void *) die);
5908 else
5909 fprintf (outfile, "die -> <null>");
5910 break;
5911 case dw_val_class_vms_delta:
5912 fprintf (outfile, "delta: @slotcount(%s-%s)",
5913 val->v.val_vms_delta.lbl2, val->v.val_vms_delta.lbl1);
5914 break;
5915 case dw_val_class_lbl_id:
5916 case dw_val_class_lineptr:
5917 case dw_val_class_macptr:
5918 case dw_val_class_loclistsptr:
5919 case dw_val_class_high_pc:
5920 fprintf (outfile, "label: %s", val->v.val_lbl_id);
5921 break;
5922 case dw_val_class_str:
5923 if (val->v.val_str->str != NULL)
5924 fprintf (outfile, "\"%s\"", val->v.val_str->str);
5925 else
5926 fprintf (outfile, "<null>");
5927 break;
5928 case dw_val_class_file:
5929 case dw_val_class_file_implicit:
5930 fprintf (outfile, "\"%s\" (%d)", val->v.val_file->filename,
5931 val->v.val_file->emitted_number);
5932 break;
5933 case dw_val_class_data8:
5935 int i;
5937 for (i = 0; i < 8; i++)
5938 fprintf (outfile, "%02x", val->v.val_data8[i]);
5939 break;
5941 case dw_val_class_discr_value:
5942 print_discr_value (outfile, &val->v.val_discr_value);
5943 break;
5944 case dw_val_class_discr_list:
5945 for (dw_discr_list_ref node = val->v.val_discr_list;
5946 node != NULL;
5947 node = node->dw_discr_next)
5949 if (node->dw_discr_range)
5951 fprintf (outfile, " .. ");
5952 print_discr_value (outfile, &node->dw_discr_lower_bound);
5953 print_discr_value (outfile, &node->dw_discr_upper_bound);
5955 else
5956 print_discr_value (outfile, &node->dw_discr_lower_bound);
5958 if (node->dw_discr_next != NULL)
5959 fprintf (outfile, " | ");
5961 default:
5962 break;
5966 /* Likewise, for a DIE attribute. */
5968 static void
5969 print_attribute (dw_attr_node *a, bool recurse, FILE *outfile)
5971 print_dw_val (&a->dw_attr_val, recurse, outfile);
5975 /* Print the list of operands in the LOC location description to OUTFILE. This
5976 routine is a debugging aid only. */
5978 static void
5979 print_loc_descr (dw_loc_descr_ref loc, FILE *outfile)
5981 dw_loc_descr_ref l = loc;
5983 if (loc == NULL)
5985 print_spaces (outfile);
5986 fprintf (outfile, "<null>\n");
5987 return;
5990 for (l = loc; l != NULL; l = l->dw_loc_next)
5992 print_spaces (outfile);
5993 fprintf (outfile, "(%p) %s",
5994 (void *) l,
5995 dwarf_stack_op_name (l->dw_loc_opc));
5996 if (l->dw_loc_oprnd1.val_class != dw_val_class_none)
5998 fprintf (outfile, " ");
5999 print_dw_val (&l->dw_loc_oprnd1, false, outfile);
6001 if (l->dw_loc_oprnd2.val_class != dw_val_class_none)
6003 fprintf (outfile, ", ");
6004 print_dw_val (&l->dw_loc_oprnd2, false, outfile);
6006 fprintf (outfile, "\n");
6010 /* Print the information associated with a given DIE, and its children.
6011 This routine is a debugging aid only. */
6013 static void
6014 print_die (dw_die_ref die, FILE *outfile)
6016 dw_attr_node *a;
6017 dw_die_ref c;
6018 unsigned ix;
6020 print_spaces (outfile);
6021 fprintf (outfile, "DIE %4ld: %s (%p)\n",
6022 die->die_offset, dwarf_tag_name (die->die_tag),
6023 (void*) die);
6024 print_spaces (outfile);
6025 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
6026 fprintf (outfile, " offset: %ld", die->die_offset);
6027 fprintf (outfile, " mark: %d\n", die->die_mark);
6029 if (die->comdat_type_p)
6031 print_spaces (outfile);
6032 fprintf (outfile, " signature: ");
6033 print_signature (outfile, die->die_id.die_type_node->signature);
6034 fprintf (outfile, "\n");
6037 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6039 print_spaces (outfile);
6040 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
6042 print_attribute (a, true, outfile);
6043 fprintf (outfile, "\n");
6046 if (die->die_child != NULL)
6048 print_indent += 4;
6049 FOR_EACH_CHILD (die, c, print_die (c, outfile));
6050 print_indent -= 4;
6052 if (print_indent == 0)
6053 fprintf (outfile, "\n");
6056 /* Print the list of operations in the LOC location description. */
6058 DEBUG_FUNCTION void
6059 debug_dwarf_loc_descr (dw_loc_descr_ref loc)
6061 print_loc_descr (loc, stderr);
6064 /* Print the information collected for a given DIE. */
6066 DEBUG_FUNCTION void
6067 debug_dwarf_die (dw_die_ref die)
6069 print_die (die, stderr);
6072 DEBUG_FUNCTION void
6073 debug (die_struct &ref)
6075 print_die (&ref, stderr);
6078 DEBUG_FUNCTION void
6079 debug (die_struct *ptr)
6081 if (ptr)
6082 debug (*ptr);
6083 else
6084 fprintf (stderr, "<nil>\n");
6088 /* Print all DWARF information collected for the compilation unit.
6089 This routine is a debugging aid only. */
6091 DEBUG_FUNCTION void
6092 debug_dwarf (void)
6094 print_indent = 0;
6095 print_die (comp_unit_die (), stderr);
6098 /* Verify the DIE tree structure. */
6100 DEBUG_FUNCTION void
6101 verify_die (dw_die_ref die)
6103 gcc_assert (!die->die_mark);
6104 if (die->die_parent == NULL
6105 && die->die_sib == NULL)
6106 return;
6107 /* Verify the die_sib list is cyclic. */
6108 dw_die_ref x = die;
6111 x->die_mark = 1;
6112 x = x->die_sib;
6114 while (x && !x->die_mark);
6115 gcc_assert (x == die);
6116 x = die;
6119 /* Verify all dies have the same parent. */
6120 gcc_assert (x->die_parent == die->die_parent);
6121 if (x->die_child)
6123 /* Verify the child has the proper parent and recurse. */
6124 gcc_assert (x->die_child->die_parent == x);
6125 verify_die (x->die_child);
6127 x->die_mark = 0;
6128 x = x->die_sib;
6130 while (x && x->die_mark);
6133 /* Sanity checks on DIEs. */
6135 static void
6136 check_die (dw_die_ref die)
6138 unsigned ix;
6139 dw_attr_node *a;
6140 bool inline_found = false;
6141 int n_location = 0, n_low_pc = 0, n_high_pc = 0, n_artificial = 0;
6142 int n_decl_line = 0, n_decl_column = 0, n_decl_file = 0;
6143 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6145 switch (a->dw_attr)
6147 case DW_AT_inline:
6148 if (a->dw_attr_val.v.val_unsigned)
6149 inline_found = true;
6150 break;
6151 case DW_AT_location:
6152 ++n_location;
6153 break;
6154 case DW_AT_low_pc:
6155 ++n_low_pc;
6156 break;
6157 case DW_AT_high_pc:
6158 ++n_high_pc;
6159 break;
6160 case DW_AT_artificial:
6161 ++n_artificial;
6162 break;
6163 case DW_AT_decl_column:
6164 ++n_decl_column;
6165 break;
6166 case DW_AT_decl_line:
6167 ++n_decl_line;
6168 break;
6169 case DW_AT_decl_file:
6170 ++n_decl_file;
6171 break;
6172 default:
6173 break;
6176 if (n_location > 1 || n_low_pc > 1 || n_high_pc > 1 || n_artificial > 1
6177 || n_decl_column > 1 || n_decl_line > 1 || n_decl_file > 1)
6179 fprintf (stderr, "Duplicate attributes in DIE:\n");
6180 debug_dwarf_die (die);
6181 gcc_unreachable ();
6183 if (inline_found)
6185 /* A debugging information entry that is a member of an abstract
6186 instance tree [that has DW_AT_inline] should not contain any
6187 attributes which describe aspects of the subroutine which vary
6188 between distinct inlined expansions or distinct out-of-line
6189 expansions. */
6190 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6191 gcc_assert (a->dw_attr != DW_AT_low_pc
6192 && a->dw_attr != DW_AT_high_pc
6193 && a->dw_attr != DW_AT_location
6194 && a->dw_attr != DW_AT_frame_base
6195 && a->dw_attr != DW_AT_call_all_calls
6196 && a->dw_attr != DW_AT_GNU_all_call_sites);
6200 /* Start a new compilation unit DIE for an include file. OLD_UNIT is the CU
6201 for the enclosing include file, if any. BINCL_DIE is the DW_TAG_GNU_BINCL
6202 DIE that marks the start of the DIEs for this include file. */
6204 static dw_die_ref
6205 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
6207 const char *filename = get_AT_string (bincl_die, DW_AT_name);
6208 dw_die_ref new_unit = gen_compile_unit_die (filename);
6210 new_unit->die_sib = old_unit;
6211 return new_unit;
6214 /* Close an include-file CU and reopen the enclosing one. */
6216 static dw_die_ref
6217 pop_compile_unit (dw_die_ref old_unit)
6219 dw_die_ref new_unit = old_unit->die_sib;
6221 old_unit->die_sib = NULL;
6222 return new_unit;
6225 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
6226 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
6227 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
6229 /* Calculate the checksum of a location expression. */
6231 static inline void
6232 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
6234 int tem;
6235 inchash::hash hstate;
6236 hashval_t hash;
6238 tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
6239 CHECKSUM (tem);
6240 hash_loc_operands (loc, hstate);
6241 hash = hstate.end();
6242 CHECKSUM (hash);
6245 /* Calculate the checksum of an attribute. */
6247 static void
6248 attr_checksum (dw_attr_node *at, struct md5_ctx *ctx, int *mark)
6250 dw_loc_descr_ref loc;
6251 rtx r;
6253 CHECKSUM (at->dw_attr);
6255 /* We don't care that this was compiled with a different compiler
6256 snapshot; if the output is the same, that's what matters. */
6257 if (at->dw_attr == DW_AT_producer)
6258 return;
6260 switch (AT_class (at))
6262 case dw_val_class_const:
6263 case dw_val_class_const_implicit:
6264 CHECKSUM (at->dw_attr_val.v.val_int);
6265 break;
6266 case dw_val_class_unsigned_const:
6267 case dw_val_class_unsigned_const_implicit:
6268 CHECKSUM (at->dw_attr_val.v.val_unsigned);
6269 break;
6270 case dw_val_class_const_double:
6271 CHECKSUM (at->dw_attr_val.v.val_double);
6272 break;
6273 case dw_val_class_wide_int:
6274 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
6275 get_full_len (*at->dw_attr_val.v.val_wide)
6276 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
6277 break;
6278 case dw_val_class_vec:
6279 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
6280 (at->dw_attr_val.v.val_vec.length
6281 * at->dw_attr_val.v.val_vec.elt_size));
6282 break;
6283 case dw_val_class_flag:
6284 CHECKSUM (at->dw_attr_val.v.val_flag);
6285 break;
6286 case dw_val_class_str:
6287 CHECKSUM_STRING (AT_string (at));
6288 break;
6290 case dw_val_class_addr:
6291 r = AT_addr (at);
6292 gcc_assert (GET_CODE (r) == SYMBOL_REF);
6293 CHECKSUM_STRING (XSTR (r, 0));
6294 break;
6296 case dw_val_class_offset:
6297 CHECKSUM (at->dw_attr_val.v.val_offset);
6298 break;
6300 case dw_val_class_loc:
6301 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6302 loc_checksum (loc, ctx);
6303 break;
6305 case dw_val_class_die_ref:
6306 die_checksum (AT_ref (at), ctx, mark);
6307 break;
6309 case dw_val_class_fde_ref:
6310 case dw_val_class_vms_delta:
6311 case dw_val_class_lbl_id:
6312 case dw_val_class_lineptr:
6313 case dw_val_class_macptr:
6314 case dw_val_class_loclistsptr:
6315 case dw_val_class_high_pc:
6316 break;
6318 case dw_val_class_file:
6319 case dw_val_class_file_implicit:
6320 CHECKSUM_STRING (AT_file (at)->filename);
6321 break;
6323 case dw_val_class_data8:
6324 CHECKSUM (at->dw_attr_val.v.val_data8);
6325 break;
6327 default:
6328 break;
6332 /* Calculate the checksum of a DIE. */
6334 static void
6335 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6337 dw_die_ref c;
6338 dw_attr_node *a;
6339 unsigned ix;
6341 /* To avoid infinite recursion. */
6342 if (die->die_mark)
6344 CHECKSUM (die->die_mark);
6345 return;
6347 die->die_mark = ++(*mark);
6349 CHECKSUM (die->die_tag);
6351 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6352 attr_checksum (a, ctx, mark);
6354 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
6357 #undef CHECKSUM
6358 #undef CHECKSUM_BLOCK
6359 #undef CHECKSUM_STRING
6361 /* For DWARF-4 types, include the trailing NULL when checksumming strings. */
6362 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
6363 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
6364 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
6365 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
6366 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
6367 #define CHECKSUM_ATTR(FOO) \
6368 if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
6370 /* Calculate the checksum of a number in signed LEB128 format. */
6372 static void
6373 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
6375 unsigned char byte;
6376 bool more;
6378 while (1)
6380 byte = (value & 0x7f);
6381 value >>= 7;
6382 more = !((value == 0 && (byte & 0x40) == 0)
6383 || (value == -1 && (byte & 0x40) != 0));
6384 if (more)
6385 byte |= 0x80;
6386 CHECKSUM (byte);
6387 if (!more)
6388 break;
6392 /* Calculate the checksum of a number in unsigned LEB128 format. */
6394 static void
6395 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
6397 while (1)
6399 unsigned char byte = (value & 0x7f);
6400 value >>= 7;
6401 if (value != 0)
6402 /* More bytes to follow. */
6403 byte |= 0x80;
6404 CHECKSUM (byte);
6405 if (value == 0)
6406 break;
6410 /* Checksum the context of the DIE. This adds the names of any
6411 surrounding namespaces or structures to the checksum. */
6413 static void
6414 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
6416 const char *name;
6417 dw_die_ref spec;
6418 int tag = die->die_tag;
6420 if (tag != DW_TAG_namespace
6421 && tag != DW_TAG_structure_type
6422 && tag != DW_TAG_class_type)
6423 return;
6425 name = get_AT_string (die, DW_AT_name);
6427 spec = get_AT_ref (die, DW_AT_specification);
6428 if (spec != NULL)
6429 die = spec;
6431 if (die->die_parent != NULL)
6432 checksum_die_context (die->die_parent, ctx);
6434 CHECKSUM_ULEB128 ('C');
6435 CHECKSUM_ULEB128 (tag);
6436 if (name != NULL)
6437 CHECKSUM_STRING (name);
6440 /* Calculate the checksum of a location expression. */
6442 static inline void
6443 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
6445 /* Special case for lone DW_OP_plus_uconst: checksum as if the location
6446 were emitted as a DW_FORM_sdata instead of a location expression. */
6447 if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
6449 CHECKSUM_ULEB128 (DW_FORM_sdata);
6450 CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
6451 return;
6454 /* Otherwise, just checksum the raw location expression. */
6455 while (loc != NULL)
6457 inchash::hash hstate;
6458 hashval_t hash;
6460 CHECKSUM_ULEB128 (loc->dtprel);
6461 CHECKSUM_ULEB128 (loc->dw_loc_opc);
6462 hash_loc_operands (loc, hstate);
6463 hash = hstate.end ();
6464 CHECKSUM (hash);
6465 loc = loc->dw_loc_next;
6469 /* Calculate the checksum of an attribute. */
6471 static void
6472 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_node *at,
6473 struct md5_ctx *ctx, int *mark)
6475 dw_loc_descr_ref loc;
6476 rtx r;
6478 if (AT_class (at) == dw_val_class_die_ref)
6480 dw_die_ref target_die = AT_ref (at);
6482 /* For pointer and reference types, we checksum only the (qualified)
6483 name of the target type (if there is a name). For friend entries,
6484 we checksum only the (qualified) name of the target type or function.
6485 This allows the checksum to remain the same whether the target type
6486 is complete or not. */
6487 if ((at->dw_attr == DW_AT_type
6488 && (tag == DW_TAG_pointer_type
6489 || tag == DW_TAG_reference_type
6490 || tag == DW_TAG_rvalue_reference_type
6491 || tag == DW_TAG_ptr_to_member_type))
6492 || (at->dw_attr == DW_AT_friend
6493 && tag == DW_TAG_friend))
6495 dw_attr_node *name_attr = get_AT (target_die, DW_AT_name);
6497 if (name_attr != NULL)
6499 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
6501 if (decl == NULL)
6502 decl = target_die;
6503 CHECKSUM_ULEB128 ('N');
6504 CHECKSUM_ULEB128 (at->dw_attr);
6505 if (decl->die_parent != NULL)
6506 checksum_die_context (decl->die_parent, ctx);
6507 CHECKSUM_ULEB128 ('E');
6508 CHECKSUM_STRING (AT_string (name_attr));
6509 return;
6513 /* For all other references to another DIE, we check to see if the
6514 target DIE has already been visited. If it has, we emit a
6515 backward reference; if not, we descend recursively. */
6516 if (target_die->die_mark > 0)
6518 CHECKSUM_ULEB128 ('R');
6519 CHECKSUM_ULEB128 (at->dw_attr);
6520 CHECKSUM_ULEB128 (target_die->die_mark);
6522 else
6524 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
6526 if (decl == NULL)
6527 decl = target_die;
6528 target_die->die_mark = ++(*mark);
6529 CHECKSUM_ULEB128 ('T');
6530 CHECKSUM_ULEB128 (at->dw_attr);
6531 if (decl->die_parent != NULL)
6532 checksum_die_context (decl->die_parent, ctx);
6533 die_checksum_ordered (target_die, ctx, mark);
6535 return;
6538 CHECKSUM_ULEB128 ('A');
6539 CHECKSUM_ULEB128 (at->dw_attr);
6541 switch (AT_class (at))
6543 case dw_val_class_const:
6544 case dw_val_class_const_implicit:
6545 CHECKSUM_ULEB128 (DW_FORM_sdata);
6546 CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
6547 break;
6549 case dw_val_class_unsigned_const:
6550 case dw_val_class_unsigned_const_implicit:
6551 CHECKSUM_ULEB128 (DW_FORM_sdata);
6552 CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
6553 break;
6555 case dw_val_class_const_double:
6556 CHECKSUM_ULEB128 (DW_FORM_block);
6557 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
6558 CHECKSUM (at->dw_attr_val.v.val_double);
6559 break;
6561 case dw_val_class_wide_int:
6562 CHECKSUM_ULEB128 (DW_FORM_block);
6563 CHECKSUM_ULEB128 (get_full_len (*at->dw_attr_val.v.val_wide)
6564 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
6565 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
6566 get_full_len (*at->dw_attr_val.v.val_wide)
6567 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
6568 break;
6570 case dw_val_class_vec:
6571 CHECKSUM_ULEB128 (DW_FORM_block);
6572 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_vec.length
6573 * at->dw_attr_val.v.val_vec.elt_size);
6574 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
6575 (at->dw_attr_val.v.val_vec.length
6576 * at->dw_attr_val.v.val_vec.elt_size));
6577 break;
6579 case dw_val_class_flag:
6580 CHECKSUM_ULEB128 (DW_FORM_flag);
6581 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
6582 break;
6584 case dw_val_class_str:
6585 CHECKSUM_ULEB128 (DW_FORM_string);
6586 CHECKSUM_STRING (AT_string (at));
6587 break;
6589 case dw_val_class_addr:
6590 r = AT_addr (at);
6591 gcc_assert (GET_CODE (r) == SYMBOL_REF);
6592 CHECKSUM_ULEB128 (DW_FORM_string);
6593 CHECKSUM_STRING (XSTR (r, 0));
6594 break;
6596 case dw_val_class_offset:
6597 CHECKSUM_ULEB128 (DW_FORM_sdata);
6598 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
6599 break;
6601 case dw_val_class_loc:
6602 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6603 loc_checksum_ordered (loc, ctx);
6604 break;
6606 case dw_val_class_fde_ref:
6607 case dw_val_class_lbl_id:
6608 case dw_val_class_lineptr:
6609 case dw_val_class_macptr:
6610 case dw_val_class_loclistsptr:
6611 case dw_val_class_high_pc:
6612 break;
6614 case dw_val_class_file:
6615 case dw_val_class_file_implicit:
6616 CHECKSUM_ULEB128 (DW_FORM_string);
6617 CHECKSUM_STRING (AT_file (at)->filename);
6618 break;
6620 case dw_val_class_data8:
6621 CHECKSUM (at->dw_attr_val.v.val_data8);
6622 break;
6624 default:
6625 break;
6629 struct checksum_attributes
6631 dw_attr_node *at_name;
6632 dw_attr_node *at_type;
6633 dw_attr_node *at_friend;
6634 dw_attr_node *at_accessibility;
6635 dw_attr_node *at_address_class;
6636 dw_attr_node *at_alignment;
6637 dw_attr_node *at_allocated;
6638 dw_attr_node *at_artificial;
6639 dw_attr_node *at_associated;
6640 dw_attr_node *at_binary_scale;
6641 dw_attr_node *at_bit_offset;
6642 dw_attr_node *at_bit_size;
6643 dw_attr_node *at_bit_stride;
6644 dw_attr_node *at_byte_size;
6645 dw_attr_node *at_byte_stride;
6646 dw_attr_node *at_const_value;
6647 dw_attr_node *at_containing_type;
6648 dw_attr_node *at_count;
6649 dw_attr_node *at_data_location;
6650 dw_attr_node *at_data_member_location;
6651 dw_attr_node *at_decimal_scale;
6652 dw_attr_node *at_decimal_sign;
6653 dw_attr_node *at_default_value;
6654 dw_attr_node *at_digit_count;
6655 dw_attr_node *at_discr;
6656 dw_attr_node *at_discr_list;
6657 dw_attr_node *at_discr_value;
6658 dw_attr_node *at_encoding;
6659 dw_attr_node *at_endianity;
6660 dw_attr_node *at_explicit;
6661 dw_attr_node *at_is_optional;
6662 dw_attr_node *at_location;
6663 dw_attr_node *at_lower_bound;
6664 dw_attr_node *at_mutable;
6665 dw_attr_node *at_ordering;
6666 dw_attr_node *at_picture_string;
6667 dw_attr_node *at_prototyped;
6668 dw_attr_node *at_small;
6669 dw_attr_node *at_segment;
6670 dw_attr_node *at_string_length;
6671 dw_attr_node *at_string_length_bit_size;
6672 dw_attr_node *at_string_length_byte_size;
6673 dw_attr_node *at_threads_scaled;
6674 dw_attr_node *at_upper_bound;
6675 dw_attr_node *at_use_location;
6676 dw_attr_node *at_use_UTF8;
6677 dw_attr_node *at_variable_parameter;
6678 dw_attr_node *at_virtuality;
6679 dw_attr_node *at_visibility;
6680 dw_attr_node *at_vtable_elem_location;
6683 /* Collect the attributes that we will want to use for the checksum. */
6685 static void
6686 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
6688 dw_attr_node *a;
6689 unsigned ix;
6691 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6693 switch (a->dw_attr)
6695 case DW_AT_name:
6696 attrs->at_name = a;
6697 break;
6698 case DW_AT_type:
6699 attrs->at_type = a;
6700 break;
6701 case DW_AT_friend:
6702 attrs->at_friend = a;
6703 break;
6704 case DW_AT_accessibility:
6705 attrs->at_accessibility = a;
6706 break;
6707 case DW_AT_address_class:
6708 attrs->at_address_class = a;
6709 break;
6710 case DW_AT_alignment:
6711 attrs->at_alignment = a;
6712 break;
6713 case DW_AT_allocated:
6714 attrs->at_allocated = a;
6715 break;
6716 case DW_AT_artificial:
6717 attrs->at_artificial = a;
6718 break;
6719 case DW_AT_associated:
6720 attrs->at_associated = a;
6721 break;
6722 case DW_AT_binary_scale:
6723 attrs->at_binary_scale = a;
6724 break;
6725 case DW_AT_bit_offset:
6726 attrs->at_bit_offset = a;
6727 break;
6728 case DW_AT_bit_size:
6729 attrs->at_bit_size = a;
6730 break;
6731 case DW_AT_bit_stride:
6732 attrs->at_bit_stride = a;
6733 break;
6734 case DW_AT_byte_size:
6735 attrs->at_byte_size = a;
6736 break;
6737 case DW_AT_byte_stride:
6738 attrs->at_byte_stride = a;
6739 break;
6740 case DW_AT_const_value:
6741 attrs->at_const_value = a;
6742 break;
6743 case DW_AT_containing_type:
6744 attrs->at_containing_type = a;
6745 break;
6746 case DW_AT_count:
6747 attrs->at_count = a;
6748 break;
6749 case DW_AT_data_location:
6750 attrs->at_data_location = a;
6751 break;
6752 case DW_AT_data_member_location:
6753 attrs->at_data_member_location = a;
6754 break;
6755 case DW_AT_decimal_scale:
6756 attrs->at_decimal_scale = a;
6757 break;
6758 case DW_AT_decimal_sign:
6759 attrs->at_decimal_sign = a;
6760 break;
6761 case DW_AT_default_value:
6762 attrs->at_default_value = a;
6763 break;
6764 case DW_AT_digit_count:
6765 attrs->at_digit_count = a;
6766 break;
6767 case DW_AT_discr:
6768 attrs->at_discr = a;
6769 break;
6770 case DW_AT_discr_list:
6771 attrs->at_discr_list = a;
6772 break;
6773 case DW_AT_discr_value:
6774 attrs->at_discr_value = a;
6775 break;
6776 case DW_AT_encoding:
6777 attrs->at_encoding = a;
6778 break;
6779 case DW_AT_endianity:
6780 attrs->at_endianity = a;
6781 break;
6782 case DW_AT_explicit:
6783 attrs->at_explicit = a;
6784 break;
6785 case DW_AT_is_optional:
6786 attrs->at_is_optional = a;
6787 break;
6788 case DW_AT_location:
6789 attrs->at_location = a;
6790 break;
6791 case DW_AT_lower_bound:
6792 attrs->at_lower_bound = a;
6793 break;
6794 case DW_AT_mutable:
6795 attrs->at_mutable = a;
6796 break;
6797 case DW_AT_ordering:
6798 attrs->at_ordering = a;
6799 break;
6800 case DW_AT_picture_string:
6801 attrs->at_picture_string = a;
6802 break;
6803 case DW_AT_prototyped:
6804 attrs->at_prototyped = a;
6805 break;
6806 case DW_AT_small:
6807 attrs->at_small = a;
6808 break;
6809 case DW_AT_segment:
6810 attrs->at_segment = a;
6811 break;
6812 case DW_AT_string_length:
6813 attrs->at_string_length = a;
6814 break;
6815 case DW_AT_string_length_bit_size:
6816 attrs->at_string_length_bit_size = a;
6817 break;
6818 case DW_AT_string_length_byte_size:
6819 attrs->at_string_length_byte_size = a;
6820 break;
6821 case DW_AT_threads_scaled:
6822 attrs->at_threads_scaled = a;
6823 break;
6824 case DW_AT_upper_bound:
6825 attrs->at_upper_bound = a;
6826 break;
6827 case DW_AT_use_location:
6828 attrs->at_use_location = a;
6829 break;
6830 case DW_AT_use_UTF8:
6831 attrs->at_use_UTF8 = a;
6832 break;
6833 case DW_AT_variable_parameter:
6834 attrs->at_variable_parameter = a;
6835 break;
6836 case DW_AT_virtuality:
6837 attrs->at_virtuality = a;
6838 break;
6839 case DW_AT_visibility:
6840 attrs->at_visibility = a;
6841 break;
6842 case DW_AT_vtable_elem_location:
6843 attrs->at_vtable_elem_location = a;
6844 break;
6845 default:
6846 break;
6851 /* Calculate the checksum of a DIE, using an ordered subset of attributes. */
6853 static void
6854 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6856 dw_die_ref c;
6857 dw_die_ref decl;
6858 struct checksum_attributes attrs;
6860 CHECKSUM_ULEB128 ('D');
6861 CHECKSUM_ULEB128 (die->die_tag);
6863 memset (&attrs, 0, sizeof (attrs));
6865 decl = get_AT_ref (die, DW_AT_specification);
6866 if (decl != NULL)
6867 collect_checksum_attributes (&attrs, decl);
6868 collect_checksum_attributes (&attrs, die);
6870 CHECKSUM_ATTR (attrs.at_name);
6871 CHECKSUM_ATTR (attrs.at_accessibility);
6872 CHECKSUM_ATTR (attrs.at_address_class);
6873 CHECKSUM_ATTR (attrs.at_allocated);
6874 CHECKSUM_ATTR (attrs.at_artificial);
6875 CHECKSUM_ATTR (attrs.at_associated);
6876 CHECKSUM_ATTR (attrs.at_binary_scale);
6877 CHECKSUM_ATTR (attrs.at_bit_offset);
6878 CHECKSUM_ATTR (attrs.at_bit_size);
6879 CHECKSUM_ATTR (attrs.at_bit_stride);
6880 CHECKSUM_ATTR (attrs.at_byte_size);
6881 CHECKSUM_ATTR (attrs.at_byte_stride);
6882 CHECKSUM_ATTR (attrs.at_const_value);
6883 CHECKSUM_ATTR (attrs.at_containing_type);
6884 CHECKSUM_ATTR (attrs.at_count);
6885 CHECKSUM_ATTR (attrs.at_data_location);
6886 CHECKSUM_ATTR (attrs.at_data_member_location);
6887 CHECKSUM_ATTR (attrs.at_decimal_scale);
6888 CHECKSUM_ATTR (attrs.at_decimal_sign);
6889 CHECKSUM_ATTR (attrs.at_default_value);
6890 CHECKSUM_ATTR (attrs.at_digit_count);
6891 CHECKSUM_ATTR (attrs.at_discr);
6892 CHECKSUM_ATTR (attrs.at_discr_list);
6893 CHECKSUM_ATTR (attrs.at_discr_value);
6894 CHECKSUM_ATTR (attrs.at_encoding);
6895 CHECKSUM_ATTR (attrs.at_endianity);
6896 CHECKSUM_ATTR (attrs.at_explicit);
6897 CHECKSUM_ATTR (attrs.at_is_optional);
6898 CHECKSUM_ATTR (attrs.at_location);
6899 CHECKSUM_ATTR (attrs.at_lower_bound);
6900 CHECKSUM_ATTR (attrs.at_mutable);
6901 CHECKSUM_ATTR (attrs.at_ordering);
6902 CHECKSUM_ATTR (attrs.at_picture_string);
6903 CHECKSUM_ATTR (attrs.at_prototyped);
6904 CHECKSUM_ATTR (attrs.at_small);
6905 CHECKSUM_ATTR (attrs.at_segment);
6906 CHECKSUM_ATTR (attrs.at_string_length);
6907 CHECKSUM_ATTR (attrs.at_string_length_bit_size);
6908 CHECKSUM_ATTR (attrs.at_string_length_byte_size);
6909 CHECKSUM_ATTR (attrs.at_threads_scaled);
6910 CHECKSUM_ATTR (attrs.at_upper_bound);
6911 CHECKSUM_ATTR (attrs.at_use_location);
6912 CHECKSUM_ATTR (attrs.at_use_UTF8);
6913 CHECKSUM_ATTR (attrs.at_variable_parameter);
6914 CHECKSUM_ATTR (attrs.at_virtuality);
6915 CHECKSUM_ATTR (attrs.at_visibility);
6916 CHECKSUM_ATTR (attrs.at_vtable_elem_location);
6917 CHECKSUM_ATTR (attrs.at_type);
6918 CHECKSUM_ATTR (attrs.at_friend);
6919 CHECKSUM_ATTR (attrs.at_alignment);
6921 /* Checksum the child DIEs. */
6922 c = die->die_child;
6923 if (c) do {
6924 dw_attr_node *name_attr;
6926 c = c->die_sib;
6927 name_attr = get_AT (c, DW_AT_name);
6928 if (is_template_instantiation (c))
6930 /* Ignore instantiations of member type and function templates. */
6932 else if (name_attr != NULL
6933 && (is_type_die (c) || c->die_tag == DW_TAG_subprogram))
6935 /* Use a shallow checksum for named nested types and member
6936 functions. */
6937 CHECKSUM_ULEB128 ('S');
6938 CHECKSUM_ULEB128 (c->die_tag);
6939 CHECKSUM_STRING (AT_string (name_attr));
6941 else
6943 /* Use a deep checksum for other children. */
6944 /* Mark this DIE so it gets processed when unmarking. */
6945 if (c->die_mark == 0)
6946 c->die_mark = -1;
6947 die_checksum_ordered (c, ctx, mark);
6949 } while (c != die->die_child);
6951 CHECKSUM_ULEB128 (0);
6954 /* Add a type name and tag to a hash. */
6955 static void
6956 die_odr_checksum (int tag, const char *name, md5_ctx *ctx)
6958 CHECKSUM_ULEB128 (tag);
6959 CHECKSUM_STRING (name);
6962 #undef CHECKSUM
6963 #undef CHECKSUM_STRING
6964 #undef CHECKSUM_ATTR
6965 #undef CHECKSUM_LEB128
6966 #undef CHECKSUM_ULEB128
6968 /* Generate the type signature for DIE. This is computed by generating an
6969 MD5 checksum over the DIE's tag, its relevant attributes, and its
6970 children. Attributes that are references to other DIEs are processed
6971 by recursion, using the MARK field to prevent infinite recursion.
6972 If the DIE is nested inside a namespace or another type, we also
6973 need to include that context in the signature. The lower 64 bits
6974 of the resulting MD5 checksum comprise the signature. */
6976 static void
6977 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
6979 int mark;
6980 const char *name;
6981 unsigned char checksum[16];
6982 struct md5_ctx ctx;
6983 dw_die_ref decl;
6984 dw_die_ref parent;
6986 name = get_AT_string (die, DW_AT_name);
6987 decl = get_AT_ref (die, DW_AT_specification);
6988 parent = get_die_parent (die);
6990 /* First, compute a signature for just the type name (and its surrounding
6991 context, if any. This is stored in the type unit DIE for link-time
6992 ODR (one-definition rule) checking. */
6994 if (is_cxx () && name != NULL)
6996 md5_init_ctx (&ctx);
6998 /* Checksum the names of surrounding namespaces and structures. */
6999 if (parent != NULL)
7000 checksum_die_context (parent, &ctx);
7002 /* Checksum the current DIE. */
7003 die_odr_checksum (die->die_tag, name, &ctx);
7004 md5_finish_ctx (&ctx, checksum);
7006 add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
7009 /* Next, compute the complete type signature. */
7011 md5_init_ctx (&ctx);
7012 mark = 1;
7013 die->die_mark = mark;
7015 /* Checksum the names of surrounding namespaces and structures. */
7016 if (parent != NULL)
7017 checksum_die_context (parent, &ctx);
7019 /* Checksum the DIE and its children. */
7020 die_checksum_ordered (die, &ctx, &mark);
7021 unmark_all_dies (die);
7022 md5_finish_ctx (&ctx, checksum);
7024 /* Store the signature in the type node and link the type DIE and the
7025 type node together. */
7026 memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
7027 DWARF_TYPE_SIGNATURE_SIZE);
7028 die->comdat_type_p = true;
7029 die->die_id.die_type_node = type_node;
7030 type_node->type_die = die;
7032 /* If the DIE is a specification, link its declaration to the type node
7033 as well. */
7034 if (decl != NULL)
7036 decl->comdat_type_p = true;
7037 decl->die_id.die_type_node = type_node;
7041 /* Do the location expressions look same? */
7042 static inline int
7043 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
7045 return loc1->dw_loc_opc == loc2->dw_loc_opc
7046 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
7047 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
7050 /* Do the values look the same? */
7051 static int
7052 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
7054 dw_loc_descr_ref loc1, loc2;
7055 rtx r1, r2;
7057 if (v1->val_class != v2->val_class)
7058 return 0;
7060 switch (v1->val_class)
7062 case dw_val_class_const:
7063 case dw_val_class_const_implicit:
7064 return v1->v.val_int == v2->v.val_int;
7065 case dw_val_class_unsigned_const:
7066 case dw_val_class_unsigned_const_implicit:
7067 return v1->v.val_unsigned == v2->v.val_unsigned;
7068 case dw_val_class_const_double:
7069 return v1->v.val_double.high == v2->v.val_double.high
7070 && v1->v.val_double.low == v2->v.val_double.low;
7071 case dw_val_class_wide_int:
7072 return *v1->v.val_wide == *v2->v.val_wide;
7073 case dw_val_class_vec:
7074 if (v1->v.val_vec.length != v2->v.val_vec.length
7075 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
7076 return 0;
7077 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
7078 v1->v.val_vec.length * v1->v.val_vec.elt_size))
7079 return 0;
7080 return 1;
7081 case dw_val_class_flag:
7082 return v1->v.val_flag == v2->v.val_flag;
7083 case dw_val_class_str:
7084 return !strcmp (v1->v.val_str->str, v2->v.val_str->str);
7086 case dw_val_class_addr:
7087 r1 = v1->v.val_addr;
7088 r2 = v2->v.val_addr;
7089 if (GET_CODE (r1) != GET_CODE (r2))
7090 return 0;
7091 return !rtx_equal_p (r1, r2);
7093 case dw_val_class_offset:
7094 return v1->v.val_offset == v2->v.val_offset;
7096 case dw_val_class_loc:
7097 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
7098 loc1 && loc2;
7099 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
7100 if (!same_loc_p (loc1, loc2, mark))
7101 return 0;
7102 return !loc1 && !loc2;
7104 case dw_val_class_die_ref:
7105 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
7107 case dw_val_class_fde_ref:
7108 case dw_val_class_vms_delta:
7109 case dw_val_class_lbl_id:
7110 case dw_val_class_lineptr:
7111 case dw_val_class_macptr:
7112 case dw_val_class_loclistsptr:
7113 case dw_val_class_high_pc:
7114 return 1;
7116 case dw_val_class_file:
7117 case dw_val_class_file_implicit:
7118 return v1->v.val_file == v2->v.val_file;
7120 case dw_val_class_data8:
7121 return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
7123 default:
7124 return 1;
7128 /* Do the attributes look the same? */
7130 static int
7131 same_attr_p (dw_attr_node *at1, dw_attr_node *at2, int *mark)
7133 if (at1->dw_attr != at2->dw_attr)
7134 return 0;
7136 /* We don't care that this was compiled with a different compiler
7137 snapshot; if the output is the same, that's what matters. */
7138 if (at1->dw_attr == DW_AT_producer)
7139 return 1;
7141 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
7144 /* Do the dies look the same? */
7146 static int
7147 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
7149 dw_die_ref c1, c2;
7150 dw_attr_node *a1;
7151 unsigned ix;
7153 /* To avoid infinite recursion. */
7154 if (die1->die_mark)
7155 return die1->die_mark == die2->die_mark;
7156 die1->die_mark = die2->die_mark = ++(*mark);
7158 if (die1->die_tag != die2->die_tag)
7159 return 0;
7161 if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
7162 return 0;
7164 FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
7165 if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
7166 return 0;
7168 c1 = die1->die_child;
7169 c2 = die2->die_child;
7170 if (! c1)
7172 if (c2)
7173 return 0;
7175 else
7176 for (;;)
7178 if (!same_die_p (c1, c2, mark))
7179 return 0;
7180 c1 = c1->die_sib;
7181 c2 = c2->die_sib;
7182 if (c1 == die1->die_child)
7184 if (c2 == die2->die_child)
7185 break;
7186 else
7187 return 0;
7191 return 1;
7194 /* Do the dies look the same? Wrapper around same_die_p. */
7196 static int
7197 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
7199 int mark = 0;
7200 int ret = same_die_p (die1, die2, &mark);
7202 unmark_all_dies (die1);
7203 unmark_all_dies (die2);
7205 return ret;
7208 /* The prefix to attach to symbols on DIEs in the current comdat debug
7209 info section. */
7210 static const char *comdat_symbol_id;
7212 /* The index of the current symbol within the current comdat CU. */
7213 static unsigned int comdat_symbol_number;
7215 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
7216 children, and set comdat_symbol_id accordingly. */
7218 static void
7219 compute_section_prefix (dw_die_ref unit_die)
7221 const char *die_name = get_AT_string (unit_die, DW_AT_name);
7222 const char *base = die_name ? lbasename (die_name) : "anonymous";
7223 char *name = XALLOCAVEC (char, strlen (base) + 64);
7224 char *p;
7225 int i, mark;
7226 unsigned char checksum[16];
7227 struct md5_ctx ctx;
7229 /* Compute the checksum of the DIE, then append part of it as hex digits to
7230 the name filename of the unit. */
7232 md5_init_ctx (&ctx);
7233 mark = 0;
7234 die_checksum (unit_die, &ctx, &mark);
7235 unmark_all_dies (unit_die);
7236 md5_finish_ctx (&ctx, checksum);
7238 sprintf (name, "%s.", base);
7239 clean_symbol_name (name);
7241 p = name + strlen (name);
7242 for (i = 0; i < 4; i++)
7244 sprintf (p, "%.2x", checksum[i]);
7245 p += 2;
7248 comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
7249 comdat_symbol_number = 0;
7252 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
7254 static int
7255 is_type_die (dw_die_ref die)
7257 switch (die->die_tag)
7259 case DW_TAG_array_type:
7260 case DW_TAG_class_type:
7261 case DW_TAG_interface_type:
7262 case DW_TAG_enumeration_type:
7263 case DW_TAG_pointer_type:
7264 case DW_TAG_reference_type:
7265 case DW_TAG_rvalue_reference_type:
7266 case DW_TAG_string_type:
7267 case DW_TAG_structure_type:
7268 case DW_TAG_subroutine_type:
7269 case DW_TAG_union_type:
7270 case DW_TAG_ptr_to_member_type:
7271 case DW_TAG_set_type:
7272 case DW_TAG_subrange_type:
7273 case DW_TAG_base_type:
7274 case DW_TAG_const_type:
7275 case DW_TAG_file_type:
7276 case DW_TAG_packed_type:
7277 case DW_TAG_volatile_type:
7278 case DW_TAG_typedef:
7279 return 1;
7280 default:
7281 return 0;
7285 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
7286 Basically, we want to choose the bits that are likely to be shared between
7287 compilations (types) and leave out the bits that are specific to individual
7288 compilations (functions). */
7290 static int
7291 is_comdat_die (dw_die_ref c)
7293 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
7294 we do for stabs. The advantage is a greater likelihood of sharing between
7295 objects that don't include headers in the same order (and therefore would
7296 put the base types in a different comdat). jason 8/28/00 */
7298 if (c->die_tag == DW_TAG_base_type)
7299 return 0;
7301 if (c->die_tag == DW_TAG_pointer_type
7302 || c->die_tag == DW_TAG_reference_type
7303 || c->die_tag == DW_TAG_rvalue_reference_type
7304 || c->die_tag == DW_TAG_const_type
7305 || c->die_tag == DW_TAG_volatile_type)
7307 dw_die_ref t = get_AT_ref (c, DW_AT_type);
7309 return t ? is_comdat_die (t) : 0;
7312 return is_type_die (c);
7315 /* Returns 1 iff C is the sort of DIE that might be referred to from another
7316 compilation unit. */
7318 static int
7319 is_symbol_die (dw_die_ref c)
7321 return (is_type_die (c)
7322 || is_declaration_die (c)
7323 || c->die_tag == DW_TAG_namespace
7324 || c->die_tag == DW_TAG_module);
7327 /* Returns true iff C is a compile-unit DIE. */
7329 static inline bool
7330 is_cu_die (dw_die_ref c)
7332 return c && (c->die_tag == DW_TAG_compile_unit
7333 || c->die_tag == DW_TAG_skeleton_unit);
7336 /* Returns true iff C is a unit DIE of some sort. */
7338 static inline bool
7339 is_unit_die (dw_die_ref c)
7341 return c && (c->die_tag == DW_TAG_compile_unit
7342 || c->die_tag == DW_TAG_partial_unit
7343 || c->die_tag == DW_TAG_type_unit
7344 || c->die_tag == DW_TAG_skeleton_unit);
7347 /* Returns true iff C is a namespace DIE. */
7349 static inline bool
7350 is_namespace_die (dw_die_ref c)
7352 return c && c->die_tag == DW_TAG_namespace;
7355 /* Returns true iff C is a class or structure DIE. */
7357 static inline bool
7358 is_class_die (dw_die_ref c)
7360 return c && (c->die_tag == DW_TAG_class_type
7361 || c->die_tag == DW_TAG_structure_type);
7364 /* Return non-zero if this DIE is a template parameter. */
7366 static inline bool
7367 is_template_parameter (dw_die_ref die)
7369 switch (die->die_tag)
7371 case DW_TAG_template_type_param:
7372 case DW_TAG_template_value_param:
7373 case DW_TAG_GNU_template_template_param:
7374 case DW_TAG_GNU_template_parameter_pack:
7375 return true;
7376 default:
7377 return false;
7381 /* Return non-zero if this DIE represents a template instantiation. */
7383 static inline bool
7384 is_template_instantiation (dw_die_ref die)
7386 dw_die_ref c;
7388 if (!is_type_die (die) && die->die_tag != DW_TAG_subprogram)
7389 return false;
7390 FOR_EACH_CHILD (die, c, if (is_template_parameter (c)) return true);
7391 return false;
7394 static char *
7395 gen_internal_sym (const char *prefix)
7397 char buf[MAX_ARTIFICIAL_LABEL_BYTES];
7399 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
7400 return xstrdup (buf);
7403 /* Assign symbols to all worthy DIEs under DIE. */
7405 static void
7406 assign_symbol_names (dw_die_ref die)
7408 dw_die_ref c;
7410 if (is_symbol_die (die) && !die->comdat_type_p)
7412 if (comdat_symbol_id)
7414 char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
7416 sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
7417 comdat_symbol_id, comdat_symbol_number++);
7418 die->die_id.die_symbol = xstrdup (p);
7420 else
7421 die->die_id.die_symbol = gen_internal_sym ("LDIE");
7424 FOR_EACH_CHILD (die, c, assign_symbol_names (c));
7427 struct cu_hash_table_entry
7429 dw_die_ref cu;
7430 unsigned min_comdat_num, max_comdat_num;
7431 struct cu_hash_table_entry *next;
7434 /* Helpers to manipulate hash table of CUs. */
7436 struct cu_hash_table_entry_hasher : pointer_hash <cu_hash_table_entry>
7438 typedef die_struct *compare_type;
7439 static inline hashval_t hash (const cu_hash_table_entry *);
7440 static inline bool equal (const cu_hash_table_entry *, const die_struct *);
7441 static inline void remove (cu_hash_table_entry *);
7444 inline hashval_t
7445 cu_hash_table_entry_hasher::hash (const cu_hash_table_entry *entry)
7447 return htab_hash_string (entry->cu->die_id.die_symbol);
7450 inline bool
7451 cu_hash_table_entry_hasher::equal (const cu_hash_table_entry *entry1,
7452 const die_struct *entry2)
7454 return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
7457 inline void
7458 cu_hash_table_entry_hasher::remove (cu_hash_table_entry *entry)
7460 struct cu_hash_table_entry *next;
7462 while (entry)
7464 next = entry->next;
7465 free (entry);
7466 entry = next;
7470 typedef hash_table<cu_hash_table_entry_hasher> cu_hash_type;
7472 /* Check whether we have already seen this CU and set up SYM_NUM
7473 accordingly. */
7474 static int
7475 check_duplicate_cu (dw_die_ref cu, cu_hash_type *htable, unsigned int *sym_num)
7477 struct cu_hash_table_entry dummy;
7478 struct cu_hash_table_entry **slot, *entry, *last = &dummy;
7480 dummy.max_comdat_num = 0;
7482 slot = htable->find_slot_with_hash (cu,
7483 htab_hash_string (cu->die_id.die_symbol),
7484 INSERT);
7485 entry = *slot;
7487 for (; entry; last = entry, entry = entry->next)
7489 if (same_die_p_wrap (cu, entry->cu))
7490 break;
7493 if (entry)
7495 *sym_num = entry->min_comdat_num;
7496 return 1;
7499 entry = XCNEW (struct cu_hash_table_entry);
7500 entry->cu = cu;
7501 entry->min_comdat_num = *sym_num = last->max_comdat_num;
7502 entry->next = *slot;
7503 *slot = entry;
7505 return 0;
7508 /* Record SYM_NUM to record of CU in HTABLE. */
7509 static void
7510 record_comdat_symbol_number (dw_die_ref cu, cu_hash_type *htable,
7511 unsigned int sym_num)
7513 struct cu_hash_table_entry **slot, *entry;
7515 slot = htable->find_slot_with_hash (cu,
7516 htab_hash_string (cu->die_id.die_symbol),
7517 NO_INSERT);
7518 entry = *slot;
7520 entry->max_comdat_num = sym_num;
7523 /* Traverse the DIE (which is always comp_unit_die), and set up
7524 additional compilation units for each of the include files we see
7525 bracketed by BINCL/EINCL. */
7527 static void
7528 break_out_includes (dw_die_ref die)
7530 dw_die_ref c;
7531 dw_die_ref unit = NULL;
7532 limbo_die_node *node, **pnode;
7534 c = die->die_child;
7535 if (c) do {
7536 dw_die_ref prev = c;
7537 c = c->die_sib;
7538 while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
7539 || (unit && is_comdat_die (c)))
7541 dw_die_ref next = c->die_sib;
7543 /* This DIE is for a secondary CU; remove it from the main one. */
7544 remove_child_with_prev (c, prev);
7546 if (c->die_tag == DW_TAG_GNU_BINCL)
7547 unit = push_new_compile_unit (unit, c);
7548 else if (c->die_tag == DW_TAG_GNU_EINCL)
7549 unit = pop_compile_unit (unit);
7550 else
7551 add_child_die (unit, c);
7552 c = next;
7553 if (c == die->die_child)
7554 break;
7556 } while (c != die->die_child);
7558 #if 0
7559 /* We can only use this in debugging, since the frontend doesn't check
7560 to make sure that we leave every include file we enter. */
7561 gcc_assert (!unit);
7562 #endif
7564 assign_symbol_names (die);
7565 cu_hash_type cu_hash_table (10);
7566 for (node = limbo_die_list, pnode = &limbo_die_list;
7567 node;
7568 node = node->next)
7570 int is_dupl;
7572 compute_section_prefix (node->die);
7573 is_dupl = check_duplicate_cu (node->die, &cu_hash_table,
7574 &comdat_symbol_number);
7575 assign_symbol_names (node->die);
7576 if (is_dupl)
7577 *pnode = node->next;
7578 else
7580 pnode = &node->next;
7581 record_comdat_symbol_number (node->die, &cu_hash_table,
7582 comdat_symbol_number);
7587 /* Return non-zero if this DIE is a declaration. */
7589 static int
7590 is_declaration_die (dw_die_ref die)
7592 dw_attr_node *a;
7593 unsigned ix;
7595 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7596 if (a->dw_attr == DW_AT_declaration)
7597 return 1;
7599 return 0;
7602 /* Return non-zero if this DIE is nested inside a subprogram. */
7604 static int
7605 is_nested_in_subprogram (dw_die_ref die)
7607 dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
7609 if (decl == NULL)
7610 decl = die;
7611 return local_scope_p (decl);
7614 /* Return non-zero if this DIE contains a defining declaration of a
7615 subprogram. */
7617 static int
7618 contains_subprogram_definition (dw_die_ref die)
7620 dw_die_ref c;
7622 if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
7623 return 1;
7624 FOR_EACH_CHILD (die, c, if (contains_subprogram_definition (c)) return 1);
7625 return 0;
7628 /* Return non-zero if this is a type DIE that should be moved to a
7629 COMDAT .debug_types section or .debug_info section with DW_UT_*type
7630 unit type. */
7632 static int
7633 should_move_die_to_comdat (dw_die_ref die)
7635 switch (die->die_tag)
7637 case DW_TAG_class_type:
7638 case DW_TAG_structure_type:
7639 case DW_TAG_enumeration_type:
7640 case DW_TAG_union_type:
7641 /* Don't move declarations, inlined instances, types nested in a
7642 subprogram, or types that contain subprogram definitions. */
7643 if (is_declaration_die (die)
7644 || get_AT (die, DW_AT_abstract_origin)
7645 || is_nested_in_subprogram (die)
7646 || contains_subprogram_definition (die))
7647 return 0;
7648 return 1;
7649 case DW_TAG_array_type:
7650 case DW_TAG_interface_type:
7651 case DW_TAG_pointer_type:
7652 case DW_TAG_reference_type:
7653 case DW_TAG_rvalue_reference_type:
7654 case DW_TAG_string_type:
7655 case DW_TAG_subroutine_type:
7656 case DW_TAG_ptr_to_member_type:
7657 case DW_TAG_set_type:
7658 case DW_TAG_subrange_type:
7659 case DW_TAG_base_type:
7660 case DW_TAG_const_type:
7661 case DW_TAG_file_type:
7662 case DW_TAG_packed_type:
7663 case DW_TAG_volatile_type:
7664 case DW_TAG_typedef:
7665 default:
7666 return 0;
7670 /* Make a clone of DIE. */
7672 static dw_die_ref
7673 clone_die (dw_die_ref die)
7675 dw_die_ref clone;
7676 dw_attr_node *a;
7677 unsigned ix;
7679 clone = ggc_cleared_alloc<die_node> ();
7680 clone->die_tag = die->die_tag;
7682 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7683 add_dwarf_attr (clone, a);
7685 return clone;
7688 /* Make a clone of the tree rooted at DIE. */
7690 static dw_die_ref
7691 clone_tree (dw_die_ref die)
7693 dw_die_ref c;
7694 dw_die_ref clone = clone_die (die);
7696 FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree (c)));
7698 return clone;
7701 /* Make a clone of DIE as a declaration. */
7703 static dw_die_ref
7704 clone_as_declaration (dw_die_ref die)
7706 dw_die_ref clone;
7707 dw_die_ref decl;
7708 dw_attr_node *a;
7709 unsigned ix;
7711 /* If the DIE is already a declaration, just clone it. */
7712 if (is_declaration_die (die))
7713 return clone_die (die);
7715 /* If the DIE is a specification, just clone its declaration DIE. */
7716 decl = get_AT_ref (die, DW_AT_specification);
7717 if (decl != NULL)
7719 clone = clone_die (decl);
7720 if (die->comdat_type_p)
7721 add_AT_die_ref (clone, DW_AT_signature, die);
7722 return clone;
7725 clone = ggc_cleared_alloc<die_node> ();
7726 clone->die_tag = die->die_tag;
7728 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7730 /* We don't want to copy over all attributes.
7731 For example we don't want DW_AT_byte_size because otherwise we will no
7732 longer have a declaration and GDB will treat it as a definition. */
7734 switch (a->dw_attr)
7736 case DW_AT_abstract_origin:
7737 case DW_AT_artificial:
7738 case DW_AT_containing_type:
7739 case DW_AT_external:
7740 case DW_AT_name:
7741 case DW_AT_type:
7742 case DW_AT_virtuality:
7743 case DW_AT_linkage_name:
7744 case DW_AT_MIPS_linkage_name:
7745 add_dwarf_attr (clone, a);
7746 break;
7747 case DW_AT_byte_size:
7748 case DW_AT_alignment:
7749 default:
7750 break;
7754 if (die->comdat_type_p)
7755 add_AT_die_ref (clone, DW_AT_signature, die);
7757 add_AT_flag (clone, DW_AT_declaration, 1);
7758 return clone;
7762 /* Structure to map a DIE in one CU to its copy in a comdat type unit. */
7764 struct decl_table_entry
7766 dw_die_ref orig;
7767 dw_die_ref copy;
7770 /* Helpers to manipulate hash table of copied declarations. */
7772 /* Hashtable helpers. */
7774 struct decl_table_entry_hasher : free_ptr_hash <decl_table_entry>
7776 typedef die_struct *compare_type;
7777 static inline hashval_t hash (const decl_table_entry *);
7778 static inline bool equal (const decl_table_entry *, const die_struct *);
7781 inline hashval_t
7782 decl_table_entry_hasher::hash (const decl_table_entry *entry)
7784 return htab_hash_pointer (entry->orig);
7787 inline bool
7788 decl_table_entry_hasher::equal (const decl_table_entry *entry1,
7789 const die_struct *entry2)
7791 return entry1->orig == entry2;
7794 typedef hash_table<decl_table_entry_hasher> decl_hash_type;
7796 /* Copy DIE and its ancestors, up to, but not including, the compile unit
7797 or type unit entry, to a new tree. Adds the new tree to UNIT and returns
7798 a pointer to the copy of DIE. If DECL_TABLE is provided, it is used
7799 to check if the ancestor has already been copied into UNIT. */
7801 static dw_die_ref
7802 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die,
7803 decl_hash_type *decl_table)
7805 dw_die_ref parent = die->die_parent;
7806 dw_die_ref new_parent = unit;
7807 dw_die_ref copy;
7808 decl_table_entry **slot = NULL;
7809 struct decl_table_entry *entry = NULL;
7811 if (decl_table)
7813 /* Check if the entry has already been copied to UNIT. */
7814 slot = decl_table->find_slot_with_hash (die, htab_hash_pointer (die),
7815 INSERT);
7816 if (*slot != HTAB_EMPTY_ENTRY)
7818 entry = *slot;
7819 return entry->copy;
7822 /* Record in DECL_TABLE that DIE has been copied to UNIT. */
7823 entry = XCNEW (struct decl_table_entry);
7824 entry->orig = die;
7825 entry->copy = NULL;
7826 *slot = entry;
7829 if (parent != NULL)
7831 dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
7832 if (spec != NULL)
7833 parent = spec;
7834 if (!is_unit_die (parent))
7835 new_parent = copy_ancestor_tree (unit, parent, decl_table);
7838 copy = clone_as_declaration (die);
7839 add_child_die (new_parent, copy);
7841 if (decl_table)
7843 /* Record the pointer to the copy. */
7844 entry->copy = copy;
7847 return copy;
7849 /* Copy the declaration context to the new type unit DIE. This includes
7850 any surrounding namespace or type declarations. If the DIE has an
7851 AT_specification attribute, it also includes attributes and children
7852 attached to the specification, and returns a pointer to the original
7853 parent of the declaration DIE. Returns NULL otherwise. */
7855 static dw_die_ref
7856 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
7858 dw_die_ref decl;
7859 dw_die_ref new_decl;
7860 dw_die_ref orig_parent = NULL;
7862 decl = get_AT_ref (die, DW_AT_specification);
7863 if (decl == NULL)
7864 decl = die;
7865 else
7867 unsigned ix;
7868 dw_die_ref c;
7869 dw_attr_node *a;
7871 /* The original DIE will be changed to a declaration, and must
7872 be moved to be a child of the original declaration DIE. */
7873 orig_parent = decl->die_parent;
7875 /* Copy the type node pointer from the new DIE to the original
7876 declaration DIE so we can forward references later. */
7877 decl->comdat_type_p = true;
7878 decl->die_id.die_type_node = die->die_id.die_type_node;
7880 remove_AT (die, DW_AT_specification);
7882 FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
7884 if (a->dw_attr != DW_AT_name
7885 && a->dw_attr != DW_AT_declaration
7886 && a->dw_attr != DW_AT_external)
7887 add_dwarf_attr (die, a);
7890 FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree (c)));
7893 if (decl->die_parent != NULL
7894 && !is_unit_die (decl->die_parent))
7896 new_decl = copy_ancestor_tree (unit, decl, NULL);
7897 if (new_decl != NULL)
7899 remove_AT (new_decl, DW_AT_signature);
7900 add_AT_specification (die, new_decl);
7904 return orig_parent;
7907 /* Generate the skeleton ancestor tree for the given NODE, then clone
7908 the DIE and add the clone into the tree. */
7910 static void
7911 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
7913 if (node->new_die != NULL)
7914 return;
7916 node->new_die = clone_as_declaration (node->old_die);
7918 if (node->parent != NULL)
7920 generate_skeleton_ancestor_tree (node->parent);
7921 add_child_die (node->parent->new_die, node->new_die);
7925 /* Generate a skeleton tree of DIEs containing any declarations that are
7926 found in the original tree. We traverse the tree looking for declaration
7927 DIEs, and construct the skeleton from the bottom up whenever we find one. */
7929 static void
7930 generate_skeleton_bottom_up (skeleton_chain_node *parent)
7932 skeleton_chain_node node;
7933 dw_die_ref c;
7934 dw_die_ref first;
7935 dw_die_ref prev = NULL;
7936 dw_die_ref next = NULL;
7938 node.parent = parent;
7940 first = c = parent->old_die->die_child;
7941 if (c)
7942 next = c->die_sib;
7943 if (c) do {
7944 if (prev == NULL || prev->die_sib == c)
7945 prev = c;
7946 c = next;
7947 next = (c == first ? NULL : c->die_sib);
7948 node.old_die = c;
7949 node.new_die = NULL;
7950 if (is_declaration_die (c))
7952 if (is_template_instantiation (c))
7954 /* Instantiated templates do not need to be cloned into the
7955 type unit. Just move the DIE and its children back to
7956 the skeleton tree (in the main CU). */
7957 remove_child_with_prev (c, prev);
7958 add_child_die (parent->new_die, c);
7959 c = prev;
7961 else if (c->comdat_type_p)
7963 /* This is the skeleton of earlier break_out_comdat_types
7964 type. Clone the existing DIE, but keep the children
7965 under the original (which is in the main CU). */
7966 dw_die_ref clone = clone_die (c);
7968 replace_child (c, clone, prev);
7969 generate_skeleton_ancestor_tree (parent);
7970 add_child_die (parent->new_die, c);
7971 c = clone;
7972 continue;
7974 else
7976 /* Clone the existing DIE, move the original to the skeleton
7977 tree (which is in the main CU), and put the clone, with
7978 all the original's children, where the original came from
7979 (which is about to be moved to the type unit). */
7980 dw_die_ref clone = clone_die (c);
7981 move_all_children (c, clone);
7983 /* If the original has a DW_AT_object_pointer attribute,
7984 it would now point to a child DIE just moved to the
7985 cloned tree, so we need to remove that attribute from
7986 the original. */
7987 remove_AT (c, DW_AT_object_pointer);
7989 replace_child (c, clone, prev);
7990 generate_skeleton_ancestor_tree (parent);
7991 add_child_die (parent->new_die, c);
7992 node.old_die = clone;
7993 node.new_die = c;
7994 c = clone;
7997 generate_skeleton_bottom_up (&node);
7998 } while (next != NULL);
8001 /* Wrapper function for generate_skeleton_bottom_up. */
8003 static dw_die_ref
8004 generate_skeleton (dw_die_ref die)
8006 skeleton_chain_node node;
8008 node.old_die = die;
8009 node.new_die = NULL;
8010 node.parent = NULL;
8012 /* If this type definition is nested inside another type,
8013 and is not an instantiation of a template, always leave
8014 at least a declaration in its place. */
8015 if (die->die_parent != NULL
8016 && is_type_die (die->die_parent)
8017 && !is_template_instantiation (die))
8018 node.new_die = clone_as_declaration (die);
8020 generate_skeleton_bottom_up (&node);
8021 return node.new_die;
8024 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
8025 declaration. The original DIE is moved to a new compile unit so that
8026 existing references to it follow it to the new location. If any of the
8027 original DIE's descendants is a declaration, we need to replace the
8028 original DIE with a skeleton tree and move the declarations back into the
8029 skeleton tree. */
8031 static dw_die_ref
8032 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
8033 dw_die_ref prev)
8035 dw_die_ref skeleton, orig_parent;
8037 /* Copy the declaration context to the type unit DIE. If the returned
8038 ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
8039 that DIE. */
8040 orig_parent = copy_declaration_context (unit, child);
8042 skeleton = generate_skeleton (child);
8043 if (skeleton == NULL)
8044 remove_child_with_prev (child, prev);
8045 else
8047 skeleton->comdat_type_p = true;
8048 skeleton->die_id.die_type_node = child->die_id.die_type_node;
8050 /* If the original DIE was a specification, we need to put
8051 the skeleton under the parent DIE of the declaration.
8052 This leaves the original declaration in the tree, but
8053 it will be pruned later since there are no longer any
8054 references to it. */
8055 if (orig_parent != NULL)
8057 remove_child_with_prev (child, prev);
8058 add_child_die (orig_parent, skeleton);
8060 else
8061 replace_child (child, skeleton, prev);
8064 return skeleton;
8067 static void
8068 copy_dwarf_procs_ref_in_attrs (dw_die_ref die,
8069 comdat_type_node *type_node,
8070 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs);
8072 /* Helper for copy_dwarf_procs_ref_in_dies. Make a copy of the DIE DWARF
8073 procedure, put it under TYPE_NODE and return the copy. Continue looking for
8074 DWARF procedure references in the DW_AT_location attribute. */
8076 static dw_die_ref
8077 copy_dwarf_procedure (dw_die_ref die,
8078 comdat_type_node *type_node,
8079 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8081 gcc_assert (die->die_tag == DW_TAG_dwarf_procedure);
8083 /* DWARF procedures are not supposed to have children... */
8084 gcc_assert (die->die_child == NULL);
8086 /* ... and they are supposed to have only one attribute: DW_AT_location. */
8087 gcc_assert (vec_safe_length (die->die_attr) == 1
8088 && ((*die->die_attr)[0].dw_attr == DW_AT_location));
8090 /* Do not copy more than once DWARF procedures. */
8091 bool existed;
8092 dw_die_ref &die_copy = copied_dwarf_procs.get_or_insert (die, &existed);
8093 if (existed)
8094 return die_copy;
8096 die_copy = clone_die (die);
8097 add_child_die (type_node->root_die, die_copy);
8098 copy_dwarf_procs_ref_in_attrs (die_copy, type_node, copied_dwarf_procs);
8099 return die_copy;
8102 /* Helper for copy_dwarf_procs_ref_in_dies. Look for references to DWARF
8103 procedures in DIE's attributes. */
8105 static void
8106 copy_dwarf_procs_ref_in_attrs (dw_die_ref die,
8107 comdat_type_node *type_node,
8108 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8110 dw_attr_node *a;
8111 unsigned i;
8113 FOR_EACH_VEC_SAFE_ELT (die->die_attr, i, a)
8115 dw_loc_descr_ref loc;
8117 if (a->dw_attr_val.val_class != dw_val_class_loc)
8118 continue;
8120 for (loc = a->dw_attr_val.v.val_loc; loc != NULL; loc = loc->dw_loc_next)
8122 switch (loc->dw_loc_opc)
8124 case DW_OP_call2:
8125 case DW_OP_call4:
8126 case DW_OP_call_ref:
8127 gcc_assert (loc->dw_loc_oprnd1.val_class
8128 == dw_val_class_die_ref);
8129 loc->dw_loc_oprnd1.v.val_die_ref.die
8130 = copy_dwarf_procedure (loc->dw_loc_oprnd1.v.val_die_ref.die,
8131 type_node,
8132 copied_dwarf_procs);
8134 default:
8135 break;
8141 /* Copy DWARF procedures that are referenced by the DIE tree to TREE_NODE and
8142 rewrite references to point to the copies.
8144 References are looked for in DIE's attributes and recursively in all its
8145 children attributes that are location descriptions. COPIED_DWARF_PROCS is a
8146 mapping from old DWARF procedures to their copy. It is used not to copy
8147 twice the same DWARF procedure under TYPE_NODE. */
8149 static void
8150 copy_dwarf_procs_ref_in_dies (dw_die_ref die,
8151 comdat_type_node *type_node,
8152 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8154 dw_die_ref c;
8156 copy_dwarf_procs_ref_in_attrs (die, type_node, copied_dwarf_procs);
8157 FOR_EACH_CHILD (die, c, copy_dwarf_procs_ref_in_dies (c,
8158 type_node,
8159 copied_dwarf_procs));
8162 /* Traverse the DIE and set up additional .debug_types or .debug_info
8163 DW_UT_*type sections for each type worthy of being placed in a COMDAT
8164 section. */
8166 static void
8167 break_out_comdat_types (dw_die_ref die)
8169 dw_die_ref c;
8170 dw_die_ref first;
8171 dw_die_ref prev = NULL;
8172 dw_die_ref next = NULL;
8173 dw_die_ref unit = NULL;
8175 first = c = die->die_child;
8176 if (c)
8177 next = c->die_sib;
8178 if (c) do {
8179 if (prev == NULL || prev->die_sib == c)
8180 prev = c;
8181 c = next;
8182 next = (c == first ? NULL : c->die_sib);
8183 if (should_move_die_to_comdat (c))
8185 dw_die_ref replacement;
8186 comdat_type_node *type_node;
8188 /* Break out nested types into their own type units. */
8189 break_out_comdat_types (c);
8191 /* Create a new type unit DIE as the root for the new tree, and
8192 add it to the list of comdat types. */
8193 unit = new_die (DW_TAG_type_unit, NULL, NULL);
8194 add_AT_unsigned (unit, DW_AT_language,
8195 get_AT_unsigned (comp_unit_die (), DW_AT_language));
8196 type_node = ggc_cleared_alloc<comdat_type_node> ();
8197 type_node->root_die = unit;
8198 type_node->next = comdat_type_list;
8199 comdat_type_list = type_node;
8201 /* Generate the type signature. */
8202 generate_type_signature (c, type_node);
8204 /* Copy the declaration context, attributes, and children of the
8205 declaration into the new type unit DIE, then remove this DIE
8206 from the main CU (or replace it with a skeleton if necessary). */
8207 replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
8208 type_node->skeleton_die = replacement;
8210 /* Add the DIE to the new compunit. */
8211 add_child_die (unit, c);
8213 /* Types can reference DWARF procedures for type size or data location
8214 expressions. Calls in DWARF expressions cannot target procedures
8215 that are not in the same section. So we must copy DWARF procedures
8216 along with this type and then rewrite references to them. */
8217 hash_map<dw_die_ref, dw_die_ref> copied_dwarf_procs;
8218 copy_dwarf_procs_ref_in_dies (c, type_node, copied_dwarf_procs);
8220 if (replacement != NULL)
8221 c = replacement;
8223 else if (c->die_tag == DW_TAG_namespace
8224 || c->die_tag == DW_TAG_class_type
8225 || c->die_tag == DW_TAG_structure_type
8226 || c->die_tag == DW_TAG_union_type)
8228 /* Look for nested types that can be broken out. */
8229 break_out_comdat_types (c);
8231 } while (next != NULL);
8234 /* Like clone_tree, but copy DW_TAG_subprogram DIEs as declarations.
8235 Enter all the cloned children into the hash table decl_table. */
8237 static dw_die_ref
8238 clone_tree_partial (dw_die_ref die, decl_hash_type *decl_table)
8240 dw_die_ref c;
8241 dw_die_ref clone;
8242 struct decl_table_entry *entry;
8243 decl_table_entry **slot;
8245 if (die->die_tag == DW_TAG_subprogram)
8246 clone = clone_as_declaration (die);
8247 else
8248 clone = clone_die (die);
8250 slot = decl_table->find_slot_with_hash (die,
8251 htab_hash_pointer (die), INSERT);
8253 /* Assert that DIE isn't in the hash table yet. If it would be there
8254 before, the ancestors would be necessarily there as well, therefore
8255 clone_tree_partial wouldn't be called. */
8256 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
8258 entry = XCNEW (struct decl_table_entry);
8259 entry->orig = die;
8260 entry->copy = clone;
8261 *slot = entry;
8263 if (die->die_tag != DW_TAG_subprogram)
8264 FOR_EACH_CHILD (die, c,
8265 add_child_die (clone, clone_tree_partial (c, decl_table)));
8267 return clone;
8270 /* Walk the DIE and its children, looking for references to incomplete
8271 or trivial types that are unmarked (i.e., that are not in the current
8272 type_unit). */
8274 static void
8275 copy_decls_walk (dw_die_ref unit, dw_die_ref die, decl_hash_type *decl_table)
8277 dw_die_ref c;
8278 dw_attr_node *a;
8279 unsigned ix;
8281 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8283 if (AT_class (a) == dw_val_class_die_ref)
8285 dw_die_ref targ = AT_ref (a);
8286 decl_table_entry **slot;
8287 struct decl_table_entry *entry;
8289 if (targ->die_mark != 0 || targ->comdat_type_p)
8290 continue;
8292 slot = decl_table->find_slot_with_hash (targ,
8293 htab_hash_pointer (targ),
8294 INSERT);
8296 if (*slot != HTAB_EMPTY_ENTRY)
8298 /* TARG has already been copied, so we just need to
8299 modify the reference to point to the copy. */
8300 entry = *slot;
8301 a->dw_attr_val.v.val_die_ref.die = entry->copy;
8303 else
8305 dw_die_ref parent = unit;
8306 dw_die_ref copy = clone_die (targ);
8308 /* Record in DECL_TABLE that TARG has been copied.
8309 Need to do this now, before the recursive call,
8310 because DECL_TABLE may be expanded and SLOT
8311 would no longer be a valid pointer. */
8312 entry = XCNEW (struct decl_table_entry);
8313 entry->orig = targ;
8314 entry->copy = copy;
8315 *slot = entry;
8317 /* If TARG is not a declaration DIE, we need to copy its
8318 children. */
8319 if (!is_declaration_die (targ))
8321 FOR_EACH_CHILD (
8322 targ, c,
8323 add_child_die (copy,
8324 clone_tree_partial (c, decl_table)));
8327 /* Make sure the cloned tree is marked as part of the
8328 type unit. */
8329 mark_dies (copy);
8331 /* If TARG has surrounding context, copy its ancestor tree
8332 into the new type unit. */
8333 if (targ->die_parent != NULL
8334 && !is_unit_die (targ->die_parent))
8335 parent = copy_ancestor_tree (unit, targ->die_parent,
8336 decl_table);
8338 add_child_die (parent, copy);
8339 a->dw_attr_val.v.val_die_ref.die = copy;
8341 /* Make sure the newly-copied DIE is walked. If it was
8342 installed in a previously-added context, it won't
8343 get visited otherwise. */
8344 if (parent != unit)
8346 /* Find the highest point of the newly-added tree,
8347 mark each node along the way, and walk from there. */
8348 parent->die_mark = 1;
8349 while (parent->die_parent
8350 && parent->die_parent->die_mark == 0)
8352 parent = parent->die_parent;
8353 parent->die_mark = 1;
8355 copy_decls_walk (unit, parent, decl_table);
8361 FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
8364 /* Copy declarations for "unworthy" types into the new comdat section.
8365 Incomplete types, modified types, and certain other types aren't broken
8366 out into comdat sections of their own, so they don't have a signature,
8367 and we need to copy the declaration into the same section so that we
8368 don't have an external reference. */
8370 static void
8371 copy_decls_for_unworthy_types (dw_die_ref unit)
8373 mark_dies (unit);
8374 decl_hash_type decl_table (10);
8375 copy_decls_walk (unit, unit, &decl_table);
8376 unmark_dies (unit);
8379 /* Traverse the DIE and add a sibling attribute if it may have the
8380 effect of speeding up access to siblings. To save some space,
8381 avoid generating sibling attributes for DIE's without children. */
8383 static void
8384 add_sibling_attributes (dw_die_ref die)
8386 dw_die_ref c;
8388 if (! die->die_child)
8389 return;
8391 if (die->die_parent && die != die->die_parent->die_child)
8392 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
8394 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
8397 /* Output all location lists for the DIE and its children. */
8399 static void
8400 output_location_lists (dw_die_ref die)
8402 dw_die_ref c;
8403 dw_attr_node *a;
8404 unsigned ix;
8406 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8407 if (AT_class (a) == dw_val_class_loc_list)
8408 output_loc_list (AT_loc_list (a));
8410 FOR_EACH_CHILD (die, c, output_location_lists (c));
8413 /* During assign_location_list_indexes and output_loclists_offset the
8414 current index, after it the number of assigned indexes (i.e. how
8415 large the .debug_loclists* offset table should be). */
8416 static unsigned int loc_list_idx;
8418 /* Output all location list offsets for the DIE and its children. */
8420 static void
8421 output_loclists_offsets (dw_die_ref die)
8423 dw_die_ref c;
8424 dw_attr_node *a;
8425 unsigned ix;
8427 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8428 if (AT_class (a) == dw_val_class_loc_list)
8430 dw_loc_list_ref l = AT_loc_list (a);
8431 if (l->offset_emitted)
8432 continue;
8433 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l->ll_symbol,
8434 loc_section_label, NULL);
8435 gcc_assert (l->hash == loc_list_idx);
8436 loc_list_idx++;
8437 l->offset_emitted = true;
8440 FOR_EACH_CHILD (die, c, output_loclists_offsets (c));
8443 /* Recursively set indexes of location lists. */
8445 static void
8446 assign_location_list_indexes (dw_die_ref die)
8448 dw_die_ref c;
8449 dw_attr_node *a;
8450 unsigned ix;
8452 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8453 if (AT_class (a) == dw_val_class_loc_list)
8455 dw_loc_list_ref list = AT_loc_list (a);
8456 if (!list->num_assigned)
8458 list->num_assigned = true;
8459 list->hash = loc_list_idx++;
8463 FOR_EACH_CHILD (die, c, assign_location_list_indexes (c));
8466 /* We want to limit the number of external references, because they are
8467 larger than local references: a relocation takes multiple words, and
8468 even a sig8 reference is always eight bytes, whereas a local reference
8469 can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
8470 So if we encounter multiple external references to the same type DIE, we
8471 make a local typedef stub for it and redirect all references there.
8473 This is the element of the hash table for keeping track of these
8474 references. */
8476 struct external_ref
8478 dw_die_ref type;
8479 dw_die_ref stub;
8480 unsigned n_refs;
8483 /* Hashtable helpers. */
8485 struct external_ref_hasher : free_ptr_hash <external_ref>
8487 static inline hashval_t hash (const external_ref *);
8488 static inline bool equal (const external_ref *, const external_ref *);
8491 inline hashval_t
8492 external_ref_hasher::hash (const external_ref *r)
8494 dw_die_ref die = r->type;
8495 hashval_t h = 0;
8497 /* We can't use the address of the DIE for hashing, because
8498 that will make the order of the stub DIEs non-deterministic. */
8499 if (! die->comdat_type_p)
8500 /* We have a symbol; use it to compute a hash. */
8501 h = htab_hash_string (die->die_id.die_symbol);
8502 else
8504 /* We have a type signature; use a subset of the bits as the hash.
8505 The 8-byte signature is at least as large as hashval_t. */
8506 comdat_type_node *type_node = die->die_id.die_type_node;
8507 memcpy (&h, type_node->signature, sizeof (h));
8509 return h;
8512 inline bool
8513 external_ref_hasher::equal (const external_ref *r1, const external_ref *r2)
8515 return r1->type == r2->type;
8518 typedef hash_table<external_ref_hasher> external_ref_hash_type;
8520 /* Return a pointer to the external_ref for references to DIE. */
8522 static struct external_ref *
8523 lookup_external_ref (external_ref_hash_type *map, dw_die_ref die)
8525 struct external_ref ref, *ref_p;
8526 external_ref **slot;
8528 ref.type = die;
8529 slot = map->find_slot (&ref, INSERT);
8530 if (*slot != HTAB_EMPTY_ENTRY)
8531 return *slot;
8533 ref_p = XCNEW (struct external_ref);
8534 ref_p->type = die;
8535 *slot = ref_p;
8536 return ref_p;
8539 /* Subroutine of optimize_external_refs, below.
8541 If we see a type skeleton, record it as our stub. If we see external
8542 references, remember how many we've seen. */
8544 static void
8545 optimize_external_refs_1 (dw_die_ref die, external_ref_hash_type *map)
8547 dw_die_ref c;
8548 dw_attr_node *a;
8549 unsigned ix;
8550 struct external_ref *ref_p;
8552 if (is_type_die (die)
8553 && (c = get_AT_ref (die, DW_AT_signature)))
8555 /* This is a local skeleton; use it for local references. */
8556 ref_p = lookup_external_ref (map, c);
8557 ref_p->stub = die;
8560 /* Scan the DIE references, and remember any that refer to DIEs from
8561 other CUs (i.e. those which are not marked). */
8562 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8563 if (AT_class (a) == dw_val_class_die_ref
8564 && (c = AT_ref (a))->die_mark == 0
8565 && is_type_die (c))
8567 ref_p = lookup_external_ref (map, c);
8568 ref_p->n_refs++;
8571 FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
8574 /* htab_traverse callback function for optimize_external_refs, below. SLOT
8575 points to an external_ref, DATA is the CU we're processing. If we don't
8576 already have a local stub, and we have multiple refs, build a stub. */
8579 dwarf2_build_local_stub (external_ref **slot, dw_die_ref data)
8581 struct external_ref *ref_p = *slot;
8583 if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
8585 /* We have multiple references to this type, so build a small stub.
8586 Both of these forms are a bit dodgy from the perspective of the
8587 DWARF standard, since technically they should have names. */
8588 dw_die_ref cu = data;
8589 dw_die_ref type = ref_p->type;
8590 dw_die_ref stub = NULL;
8592 if (type->comdat_type_p)
8594 /* If we refer to this type via sig8, use AT_signature. */
8595 stub = new_die (type->die_tag, cu, NULL_TREE);
8596 add_AT_die_ref (stub, DW_AT_signature, type);
8598 else
8600 /* Otherwise, use a typedef with no name. */
8601 stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
8602 add_AT_die_ref (stub, DW_AT_type, type);
8605 stub->die_mark++;
8606 ref_p->stub = stub;
8608 return 1;
8611 /* DIE is a unit; look through all the DIE references to see if there are
8612 any external references to types, and if so, create local stubs for
8613 them which will be applied in build_abbrev_table. This is useful because
8614 references to local DIEs are smaller. */
8616 static external_ref_hash_type *
8617 optimize_external_refs (dw_die_ref die)
8619 external_ref_hash_type *map = new external_ref_hash_type (10);
8620 optimize_external_refs_1 (die, map);
8621 map->traverse <dw_die_ref, dwarf2_build_local_stub> (die);
8622 return map;
8625 /* The following 3 variables are temporaries that are computed only during the
8626 build_abbrev_table call and used and released during the following
8627 optimize_abbrev_table call. */
8629 /* First abbrev_id that can be optimized based on usage. */
8630 static unsigned int abbrev_opt_start;
8632 /* Maximum abbrev_id of a base type plus one (we can't optimize DIEs with
8633 abbrev_id smaller than this, because they must be already sized
8634 during build_abbrev_table). */
8635 static unsigned int abbrev_opt_base_type_end;
8637 /* Vector of usage counts during build_abbrev_table. Indexed by
8638 abbrev_id - abbrev_opt_start. */
8639 static vec<unsigned int> abbrev_usage_count;
8641 /* Vector of all DIEs added with die_abbrev >= abbrev_opt_start. */
8642 static vec<dw_die_ref> sorted_abbrev_dies;
8644 /* The format of each DIE (and its attribute value pairs) is encoded in an
8645 abbreviation table. This routine builds the abbreviation table and assigns
8646 a unique abbreviation id for each abbreviation entry. The children of each
8647 die are visited recursively. */
8649 static void
8650 build_abbrev_table (dw_die_ref die, external_ref_hash_type *extern_map)
8652 unsigned int abbrev_id = 0;
8653 dw_die_ref c;
8654 dw_attr_node *a;
8655 unsigned ix;
8656 dw_die_ref abbrev;
8658 /* Scan the DIE references, and replace any that refer to
8659 DIEs from other CUs (i.e. those which are not marked) with
8660 the local stubs we built in optimize_external_refs. */
8661 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8662 if (AT_class (a) == dw_val_class_die_ref
8663 && (c = AT_ref (a))->die_mark == 0)
8665 struct external_ref *ref_p;
8666 gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
8668 ref_p = lookup_external_ref (extern_map, c);
8669 if (ref_p->stub && ref_p->stub != die)
8670 change_AT_die_ref (a, ref_p->stub);
8671 else
8672 /* We aren't changing this reference, so mark it external. */
8673 set_AT_ref_external (a, 1);
8676 FOR_EACH_VEC_SAFE_ELT (abbrev_die_table, abbrev_id, abbrev)
8678 dw_attr_node *die_a, *abbrev_a;
8679 unsigned ix;
8680 bool ok = true;
8682 if (abbrev_id == 0)
8683 continue;
8684 if (abbrev->die_tag != die->die_tag)
8685 continue;
8686 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
8687 continue;
8689 if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
8690 continue;
8692 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
8694 abbrev_a = &(*abbrev->die_attr)[ix];
8695 if ((abbrev_a->dw_attr != die_a->dw_attr)
8696 || (value_format (abbrev_a) != value_format (die_a)))
8698 ok = false;
8699 break;
8702 if (ok)
8703 break;
8706 if (abbrev_id >= vec_safe_length (abbrev_die_table))
8708 vec_safe_push (abbrev_die_table, die);
8709 if (abbrev_opt_start)
8710 abbrev_usage_count.safe_push (0);
8712 if (abbrev_opt_start && abbrev_id >= abbrev_opt_start)
8714 abbrev_usage_count[abbrev_id - abbrev_opt_start]++;
8715 sorted_abbrev_dies.safe_push (die);
8718 die->die_abbrev = abbrev_id;
8719 FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
8722 /* Callback function for sorted_abbrev_dies vector sorting. We sort
8723 by die_abbrev's usage count, from the most commonly used
8724 abbreviation to the least. */
8726 static int
8727 die_abbrev_cmp (const void *p1, const void *p2)
8729 dw_die_ref die1 = *(const dw_die_ref *) p1;
8730 dw_die_ref die2 = *(const dw_die_ref *) p2;
8732 gcc_checking_assert (die1->die_abbrev >= abbrev_opt_start);
8733 gcc_checking_assert (die2->die_abbrev >= abbrev_opt_start);
8735 if (die1->die_abbrev >= abbrev_opt_base_type_end
8736 && die2->die_abbrev >= abbrev_opt_base_type_end)
8738 if (abbrev_usage_count[die1->die_abbrev - abbrev_opt_start]
8739 > abbrev_usage_count[die2->die_abbrev - abbrev_opt_start])
8740 return -1;
8741 if (abbrev_usage_count[die1->die_abbrev - abbrev_opt_start]
8742 < abbrev_usage_count[die2->die_abbrev - abbrev_opt_start])
8743 return 1;
8746 /* Stabilize the sort. */
8747 if (die1->die_abbrev < die2->die_abbrev)
8748 return -1;
8749 if (die1->die_abbrev > die2->die_abbrev)
8750 return 1;
8752 return 0;
8755 /* Convert dw_val_class_const and dw_val_class_unsigned_const class attributes
8756 of DIEs in between sorted_abbrev_dies[first_id] and abbrev_dies[end_id - 1]
8757 into dw_val_class_const_implicit or
8758 dw_val_class_unsigned_const_implicit. */
8760 static void
8761 optimize_implicit_const (unsigned int first_id, unsigned int end,
8762 vec<bool> &implicit_consts)
8764 /* It never makes sense if there is just one DIE using the abbreviation. */
8765 if (end < first_id + 2)
8766 return;
8768 dw_attr_node *a;
8769 unsigned ix, i;
8770 dw_die_ref die = sorted_abbrev_dies[first_id];
8771 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8772 if (implicit_consts[ix])
8774 enum dw_val_class new_class = dw_val_class_none;
8775 switch (AT_class (a))
8777 case dw_val_class_unsigned_const:
8778 if ((HOST_WIDE_INT) AT_unsigned (a) < 0)
8779 continue;
8781 /* The .debug_abbrev section will grow by
8782 size_of_sleb128 (AT_unsigned (a)) and we avoid the constants
8783 in all the DIEs using that abbreviation. */
8784 if (constant_size (AT_unsigned (a)) * (end - first_id)
8785 <= (unsigned) size_of_sleb128 (AT_unsigned (a)))
8786 continue;
8788 new_class = dw_val_class_unsigned_const_implicit;
8789 break;
8791 case dw_val_class_const:
8792 new_class = dw_val_class_const_implicit;
8793 break;
8795 case dw_val_class_file:
8796 new_class = dw_val_class_file_implicit;
8797 break;
8799 default:
8800 continue;
8802 for (i = first_id; i < end; i++)
8803 (*sorted_abbrev_dies[i]->die_attr)[ix].dw_attr_val.val_class
8804 = new_class;
8808 /* Attempt to optimize abbreviation table from abbrev_opt_start
8809 abbreviation above. */
8811 static void
8812 optimize_abbrev_table (void)
8814 if (abbrev_opt_start
8815 && vec_safe_length (abbrev_die_table) > abbrev_opt_start
8816 && (dwarf_version >= 5 || vec_safe_length (abbrev_die_table) > 127))
8818 auto_vec<bool, 32> implicit_consts;
8819 sorted_abbrev_dies.qsort (die_abbrev_cmp);
8821 unsigned int abbrev_id = abbrev_opt_start - 1;
8822 unsigned int first_id = ~0U;
8823 unsigned int last_abbrev_id = 0;
8824 unsigned int i;
8825 dw_die_ref die;
8826 if (abbrev_opt_base_type_end > abbrev_opt_start)
8827 abbrev_id = abbrev_opt_base_type_end - 1;
8828 /* Reassign abbreviation ids from abbrev_opt_start above, so that
8829 most commonly used abbreviations come first. */
8830 FOR_EACH_VEC_ELT (sorted_abbrev_dies, i, die)
8832 dw_attr_node *a;
8833 unsigned ix;
8835 /* If calc_base_type_die_sizes has been called, the CU and
8836 base types after it can't be optimized, because we've already
8837 calculated their DIE offsets. We've sorted them first. */
8838 if (die->die_abbrev < abbrev_opt_base_type_end)
8839 continue;
8840 if (die->die_abbrev != last_abbrev_id)
8842 last_abbrev_id = die->die_abbrev;
8843 if (dwarf_version >= 5 && first_id != ~0U)
8844 optimize_implicit_const (first_id, i, implicit_consts);
8845 abbrev_id++;
8846 (*abbrev_die_table)[abbrev_id] = die;
8847 if (dwarf_version >= 5)
8849 first_id = i;
8850 implicit_consts.truncate (0);
8852 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8853 switch (AT_class (a))
8855 case dw_val_class_const:
8856 case dw_val_class_unsigned_const:
8857 case dw_val_class_file:
8858 implicit_consts.safe_push (true);
8859 break;
8860 default:
8861 implicit_consts.safe_push (false);
8862 break;
8866 else if (dwarf_version >= 5)
8868 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8869 if (!implicit_consts[ix])
8870 continue;
8871 else
8873 dw_attr_node *other_a
8874 = &(*(*abbrev_die_table)[abbrev_id]->die_attr)[ix];
8875 if (!dw_val_equal_p (&a->dw_attr_val,
8876 &other_a->dw_attr_val))
8877 implicit_consts[ix] = false;
8880 die->die_abbrev = abbrev_id;
8882 gcc_assert (abbrev_id == vec_safe_length (abbrev_die_table) - 1);
8883 if (dwarf_version >= 5 && first_id != ~0U)
8884 optimize_implicit_const (first_id, i, implicit_consts);
8887 abbrev_opt_start = 0;
8888 abbrev_opt_base_type_end = 0;
8889 abbrev_usage_count.release ();
8890 sorted_abbrev_dies.release ();
8893 /* Return the power-of-two number of bytes necessary to represent VALUE. */
8895 static int
8896 constant_size (unsigned HOST_WIDE_INT value)
8898 int log;
8900 if (value == 0)
8901 log = 0;
8902 else
8903 log = floor_log2 (value);
8905 log = log / 8;
8906 log = 1 << (floor_log2 (log) + 1);
8908 return log;
8911 /* Return the size of a DIE as it is represented in the
8912 .debug_info section. */
8914 static unsigned long
8915 size_of_die (dw_die_ref die)
8917 unsigned long size = 0;
8918 dw_attr_node *a;
8919 unsigned ix;
8920 enum dwarf_form form;
8922 size += size_of_uleb128 (die->die_abbrev);
8923 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8925 switch (AT_class (a))
8927 case dw_val_class_addr:
8928 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8930 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8931 size += size_of_uleb128 (AT_index (a));
8933 else
8934 size += DWARF2_ADDR_SIZE;
8935 break;
8936 case dw_val_class_offset:
8937 size += DWARF_OFFSET_SIZE;
8938 break;
8939 case dw_val_class_loc:
8941 unsigned long lsize = size_of_locs (AT_loc (a));
8943 /* Block length. */
8944 if (dwarf_version >= 4)
8945 size += size_of_uleb128 (lsize);
8946 else
8947 size += constant_size (lsize);
8948 size += lsize;
8950 break;
8951 case dw_val_class_loc_list:
8952 if (dwarf_split_debug_info && dwarf_version >= 5)
8954 gcc_assert (AT_loc_list (a)->num_assigned);
8955 size += size_of_uleb128 (AT_loc_list (a)->hash);
8957 else
8958 size += DWARF_OFFSET_SIZE;
8959 break;
8960 case dw_val_class_range_list:
8961 if (value_format (a) == DW_FORM_rnglistx)
8963 gcc_assert (rnglist_idx);
8964 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
8965 size += size_of_uleb128 (r->idx);
8967 else
8968 size += DWARF_OFFSET_SIZE;
8969 break;
8970 case dw_val_class_const:
8971 size += size_of_sleb128 (AT_int (a));
8972 break;
8973 case dw_val_class_unsigned_const:
8975 int csize = constant_size (AT_unsigned (a));
8976 if (dwarf_version == 3
8977 && a->dw_attr == DW_AT_data_member_location
8978 && csize >= 4)
8979 size += size_of_uleb128 (AT_unsigned (a));
8980 else
8981 size += csize;
8983 break;
8984 case dw_val_class_const_implicit:
8985 case dw_val_class_unsigned_const_implicit:
8986 case dw_val_class_file_implicit:
8987 /* These occupy no size in the DIE, just an extra sleb128 in
8988 .debug_abbrev. */
8989 break;
8990 case dw_val_class_const_double:
8991 size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
8992 if (HOST_BITS_PER_WIDE_INT >= DWARF_LARGEST_DATA_FORM_BITS)
8993 size++; /* block */
8994 break;
8995 case dw_val_class_wide_int:
8996 size += (get_full_len (*a->dw_attr_val.v.val_wide)
8997 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
8998 if (get_full_len (*a->dw_attr_val.v.val_wide)
8999 * HOST_BITS_PER_WIDE_INT > DWARF_LARGEST_DATA_FORM_BITS)
9000 size++; /* block */
9001 break;
9002 case dw_val_class_vec:
9003 size += constant_size (a->dw_attr_val.v.val_vec.length
9004 * a->dw_attr_val.v.val_vec.elt_size)
9005 + a->dw_attr_val.v.val_vec.length
9006 * a->dw_attr_val.v.val_vec.elt_size; /* block */
9007 break;
9008 case dw_val_class_flag:
9009 if (dwarf_version >= 4)
9010 /* Currently all add_AT_flag calls pass in 1 as last argument,
9011 so DW_FORM_flag_present can be used. If that ever changes,
9012 we'll need to use DW_FORM_flag and have some optimization
9013 in build_abbrev_table that will change those to
9014 DW_FORM_flag_present if it is set to 1 in all DIEs using
9015 the same abbrev entry. */
9016 gcc_assert (a->dw_attr_val.v.val_flag == 1);
9017 else
9018 size += 1;
9019 break;
9020 case dw_val_class_die_ref:
9021 if (AT_ref_external (a))
9023 /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
9024 we use DW_FORM_ref_addr. In DWARF2, DW_FORM_ref_addr
9025 is sized by target address length, whereas in DWARF3
9026 it's always sized as an offset. */
9027 if (use_debug_types)
9028 size += DWARF_TYPE_SIGNATURE_SIZE;
9029 else if (dwarf_version == 2)
9030 size += DWARF2_ADDR_SIZE;
9031 else
9032 size += DWARF_OFFSET_SIZE;
9034 else
9035 size += DWARF_OFFSET_SIZE;
9036 break;
9037 case dw_val_class_fde_ref:
9038 size += DWARF_OFFSET_SIZE;
9039 break;
9040 case dw_val_class_lbl_id:
9041 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
9043 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
9044 size += size_of_uleb128 (AT_index (a));
9046 else
9047 size += DWARF2_ADDR_SIZE;
9048 break;
9049 case dw_val_class_lineptr:
9050 case dw_val_class_macptr:
9051 case dw_val_class_loclistsptr:
9052 size += DWARF_OFFSET_SIZE;
9053 break;
9054 case dw_val_class_str:
9055 form = AT_string_form (a);
9056 if (form == DW_FORM_strp || form == DW_FORM_line_strp)
9057 size += DWARF_OFFSET_SIZE;
9058 else if (form == DW_FORM_GNU_str_index)
9059 size += size_of_uleb128 (AT_index (a));
9060 else
9061 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
9062 break;
9063 case dw_val_class_file:
9064 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
9065 break;
9066 case dw_val_class_data8:
9067 size += 8;
9068 break;
9069 case dw_val_class_vms_delta:
9070 size += DWARF_OFFSET_SIZE;
9071 break;
9072 case dw_val_class_high_pc:
9073 size += DWARF2_ADDR_SIZE;
9074 break;
9075 case dw_val_class_discr_value:
9076 size += size_of_discr_value (&a->dw_attr_val.v.val_discr_value);
9077 break;
9078 case dw_val_class_discr_list:
9080 unsigned block_size = size_of_discr_list (AT_discr_list (a));
9082 /* This is a block, so we have the block length and then its
9083 data. */
9084 size += constant_size (block_size) + block_size;
9086 break;
9087 default:
9088 gcc_unreachable ();
9092 return size;
9095 /* Size the debugging information associated with a given DIE. Visits the
9096 DIE's children recursively. Updates the global variable next_die_offset, on
9097 each time through. Uses the current value of next_die_offset to update the
9098 die_offset field in each DIE. */
9100 static void
9101 calc_die_sizes (dw_die_ref die)
9103 dw_die_ref c;
9105 gcc_assert (die->die_offset == 0
9106 || (unsigned long int) die->die_offset == next_die_offset);
9107 die->die_offset = next_die_offset;
9108 next_die_offset += size_of_die (die);
9110 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
9112 if (die->die_child != NULL)
9113 /* Count the null byte used to terminate sibling lists. */
9114 next_die_offset += 1;
9117 /* Size just the base type children at the start of the CU.
9118 This is needed because build_abbrev needs to size locs
9119 and sizing of type based stack ops needs to know die_offset
9120 values for the base types. */
9122 static void
9123 calc_base_type_die_sizes (void)
9125 unsigned long die_offset = (dwarf_split_debug_info
9126 ? DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
9127 : DWARF_COMPILE_UNIT_HEADER_SIZE);
9128 unsigned int i;
9129 dw_die_ref base_type;
9130 #if ENABLE_ASSERT_CHECKING
9131 dw_die_ref prev = comp_unit_die ()->die_child;
9132 #endif
9134 die_offset += size_of_die (comp_unit_die ());
9135 for (i = 0; base_types.iterate (i, &base_type); i++)
9137 #if ENABLE_ASSERT_CHECKING
9138 gcc_assert (base_type->die_offset == 0
9139 && prev->die_sib == base_type
9140 && base_type->die_child == NULL
9141 && base_type->die_abbrev);
9142 prev = base_type;
9143 #endif
9144 if (abbrev_opt_start
9145 && base_type->die_abbrev >= abbrev_opt_base_type_end)
9146 abbrev_opt_base_type_end = base_type->die_abbrev + 1;
9147 base_type->die_offset = die_offset;
9148 die_offset += size_of_die (base_type);
9152 /* Set the marks for a die and its children. We do this so
9153 that we know whether or not a reference needs to use FORM_ref_addr; only
9154 DIEs in the same CU will be marked. We used to clear out the offset
9155 and use that as the flag, but ran into ordering problems. */
9157 static void
9158 mark_dies (dw_die_ref die)
9160 dw_die_ref c;
9162 gcc_assert (!die->die_mark);
9164 die->die_mark = 1;
9165 FOR_EACH_CHILD (die, c, mark_dies (c));
9168 /* Clear the marks for a die and its children. */
9170 static void
9171 unmark_dies (dw_die_ref die)
9173 dw_die_ref c;
9175 if (! use_debug_types)
9176 gcc_assert (die->die_mark);
9178 die->die_mark = 0;
9179 FOR_EACH_CHILD (die, c, unmark_dies (c));
9182 /* Clear the marks for a die, its children and referred dies. */
9184 static void
9185 unmark_all_dies (dw_die_ref die)
9187 dw_die_ref c;
9188 dw_attr_node *a;
9189 unsigned ix;
9191 if (!die->die_mark)
9192 return;
9193 die->die_mark = 0;
9195 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
9197 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9198 if (AT_class (a) == dw_val_class_die_ref)
9199 unmark_all_dies (AT_ref (a));
9202 /* Calculate if the entry should appear in the final output file. It may be
9203 from a pruned a type. */
9205 static bool
9206 include_pubname_in_output (vec<pubname_entry, va_gc> *table, pubname_entry *p)
9208 /* By limiting gnu pubnames to definitions only, gold can generate a
9209 gdb index without entries for declarations, which don't include
9210 enough information to be useful. */
9211 if (debug_generate_pub_sections == 2 && is_declaration_die (p->die))
9212 return false;
9214 if (table == pubname_table)
9216 /* Enumerator names are part of the pubname table, but the
9217 parent DW_TAG_enumeration_type die may have been pruned.
9218 Don't output them if that is the case. */
9219 if (p->die->die_tag == DW_TAG_enumerator &&
9220 (p->die->die_parent == NULL
9221 || !p->die->die_parent->die_perennial_p))
9222 return false;
9224 /* Everything else in the pubname table is included. */
9225 return true;
9228 /* The pubtypes table shouldn't include types that have been
9229 pruned. */
9230 return (p->die->die_offset != 0
9231 || !flag_eliminate_unused_debug_types);
9234 /* Return the size of the .debug_pubnames or .debug_pubtypes table
9235 generated for the compilation unit. */
9237 static unsigned long
9238 size_of_pubnames (vec<pubname_entry, va_gc> *names)
9240 unsigned long size;
9241 unsigned i;
9242 pubname_entry *p;
9243 int space_for_flags = (debug_generate_pub_sections == 2) ? 1 : 0;
9245 size = DWARF_PUBNAMES_HEADER_SIZE;
9246 FOR_EACH_VEC_ELT (*names, i, p)
9247 if (include_pubname_in_output (names, p))
9248 size += strlen (p->name) + DWARF_OFFSET_SIZE + 1 + space_for_flags;
9250 size += DWARF_OFFSET_SIZE;
9251 return size;
9254 /* Return the size of the information in the .debug_aranges section. */
9256 static unsigned long
9257 size_of_aranges (void)
9259 unsigned long size;
9261 size = DWARF_ARANGES_HEADER_SIZE;
9263 /* Count the address/length pair for this compilation unit. */
9264 if (text_section_used)
9265 size += 2 * DWARF2_ADDR_SIZE;
9266 if (cold_text_section_used)
9267 size += 2 * DWARF2_ADDR_SIZE;
9268 if (have_multiple_function_sections)
9270 unsigned fde_idx;
9271 dw_fde_ref fde;
9273 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
9275 if (DECL_IGNORED_P (fde->decl))
9276 continue;
9277 if (!fde->in_std_section)
9278 size += 2 * DWARF2_ADDR_SIZE;
9279 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
9280 size += 2 * DWARF2_ADDR_SIZE;
9284 /* Count the two zero words used to terminated the address range table. */
9285 size += 2 * DWARF2_ADDR_SIZE;
9286 return size;
9289 /* Select the encoding of an attribute value. */
9291 static enum dwarf_form
9292 value_format (dw_attr_node *a)
9294 switch (AT_class (a))
9296 case dw_val_class_addr:
9297 /* Only very few attributes allow DW_FORM_addr. */
9298 switch (a->dw_attr)
9300 case DW_AT_low_pc:
9301 case DW_AT_high_pc:
9302 case DW_AT_entry_pc:
9303 case DW_AT_trampoline:
9304 return (AT_index (a) == NOT_INDEXED
9305 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
9306 default:
9307 break;
9309 switch (DWARF2_ADDR_SIZE)
9311 case 1:
9312 return DW_FORM_data1;
9313 case 2:
9314 return DW_FORM_data2;
9315 case 4:
9316 return DW_FORM_data4;
9317 case 8:
9318 return DW_FORM_data8;
9319 default:
9320 gcc_unreachable ();
9322 case dw_val_class_loc_list:
9323 if (dwarf_split_debug_info
9324 && dwarf_version >= 5
9325 && AT_loc_list (a)->num_assigned)
9326 return DW_FORM_loclistx;
9327 /* FALLTHRU */
9328 case dw_val_class_range_list:
9329 /* For range lists in DWARF 5, use DW_FORM_rnglistx from .debug_info.dwo
9330 but in .debug_info use DW_FORM_sec_offset, which is shorter if we
9331 care about sizes of .debug* sections in shared libraries and
9332 executables and don't take into account relocations that affect just
9333 relocatable objects - for DW_FORM_rnglistx we'd have to emit offset
9334 table in the .debug_rnglists section. */
9335 if (dwarf_split_debug_info
9336 && dwarf_version >= 5
9337 && AT_class (a) == dw_val_class_range_list
9338 && rnglist_idx
9339 && a->dw_attr_val.val_entry != RELOCATED_OFFSET)
9340 return DW_FORM_rnglistx;
9341 if (dwarf_version >= 4)
9342 return DW_FORM_sec_offset;
9343 /* FALLTHRU */
9344 case dw_val_class_vms_delta:
9345 case dw_val_class_offset:
9346 switch (DWARF_OFFSET_SIZE)
9348 case 4:
9349 return DW_FORM_data4;
9350 case 8:
9351 return DW_FORM_data8;
9352 default:
9353 gcc_unreachable ();
9355 case dw_val_class_loc:
9356 if (dwarf_version >= 4)
9357 return DW_FORM_exprloc;
9358 switch (constant_size (size_of_locs (AT_loc (a))))
9360 case 1:
9361 return DW_FORM_block1;
9362 case 2:
9363 return DW_FORM_block2;
9364 case 4:
9365 return DW_FORM_block4;
9366 default:
9367 gcc_unreachable ();
9369 case dw_val_class_const:
9370 return DW_FORM_sdata;
9371 case dw_val_class_unsigned_const:
9372 switch (constant_size (AT_unsigned (a)))
9374 case 1:
9375 return DW_FORM_data1;
9376 case 2:
9377 return DW_FORM_data2;
9378 case 4:
9379 /* In DWARF3 DW_AT_data_member_location with
9380 DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
9381 constant, so we need to use DW_FORM_udata if we need
9382 a large constant. */
9383 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
9384 return DW_FORM_udata;
9385 return DW_FORM_data4;
9386 case 8:
9387 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
9388 return DW_FORM_udata;
9389 return DW_FORM_data8;
9390 default:
9391 gcc_unreachable ();
9393 case dw_val_class_const_implicit:
9394 case dw_val_class_unsigned_const_implicit:
9395 case dw_val_class_file_implicit:
9396 return DW_FORM_implicit_const;
9397 case dw_val_class_const_double:
9398 switch (HOST_BITS_PER_WIDE_INT)
9400 case 8:
9401 return DW_FORM_data2;
9402 case 16:
9403 return DW_FORM_data4;
9404 case 32:
9405 return DW_FORM_data8;
9406 case 64:
9407 if (dwarf_version >= 5)
9408 return DW_FORM_data16;
9409 /* FALLTHRU */
9410 default:
9411 return DW_FORM_block1;
9413 case dw_val_class_wide_int:
9414 switch (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT)
9416 case 8:
9417 return DW_FORM_data1;
9418 case 16:
9419 return DW_FORM_data2;
9420 case 32:
9421 return DW_FORM_data4;
9422 case 64:
9423 return DW_FORM_data8;
9424 case 128:
9425 if (dwarf_version >= 5)
9426 return DW_FORM_data16;
9427 /* FALLTHRU */
9428 default:
9429 return DW_FORM_block1;
9431 case dw_val_class_vec:
9432 switch (constant_size (a->dw_attr_val.v.val_vec.length
9433 * a->dw_attr_val.v.val_vec.elt_size))
9435 case 1:
9436 return DW_FORM_block1;
9437 case 2:
9438 return DW_FORM_block2;
9439 case 4:
9440 return DW_FORM_block4;
9441 default:
9442 gcc_unreachable ();
9444 case dw_val_class_flag:
9445 if (dwarf_version >= 4)
9447 /* Currently all add_AT_flag calls pass in 1 as last argument,
9448 so DW_FORM_flag_present can be used. If that ever changes,
9449 we'll need to use DW_FORM_flag and have some optimization
9450 in build_abbrev_table that will change those to
9451 DW_FORM_flag_present if it is set to 1 in all DIEs using
9452 the same abbrev entry. */
9453 gcc_assert (a->dw_attr_val.v.val_flag == 1);
9454 return DW_FORM_flag_present;
9456 return DW_FORM_flag;
9457 case dw_val_class_die_ref:
9458 if (AT_ref_external (a))
9459 return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
9460 else
9461 return DW_FORM_ref;
9462 case dw_val_class_fde_ref:
9463 return DW_FORM_data;
9464 case dw_val_class_lbl_id:
9465 return (AT_index (a) == NOT_INDEXED
9466 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
9467 case dw_val_class_lineptr:
9468 case dw_val_class_macptr:
9469 case dw_val_class_loclistsptr:
9470 return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
9471 case dw_val_class_str:
9472 return AT_string_form (a);
9473 case dw_val_class_file:
9474 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
9476 case 1:
9477 return DW_FORM_data1;
9478 case 2:
9479 return DW_FORM_data2;
9480 case 4:
9481 return DW_FORM_data4;
9482 default:
9483 gcc_unreachable ();
9486 case dw_val_class_data8:
9487 return DW_FORM_data8;
9489 case dw_val_class_high_pc:
9490 switch (DWARF2_ADDR_SIZE)
9492 case 1:
9493 return DW_FORM_data1;
9494 case 2:
9495 return DW_FORM_data2;
9496 case 4:
9497 return DW_FORM_data4;
9498 case 8:
9499 return DW_FORM_data8;
9500 default:
9501 gcc_unreachable ();
9504 case dw_val_class_discr_value:
9505 return (a->dw_attr_val.v.val_discr_value.pos
9506 ? DW_FORM_udata
9507 : DW_FORM_sdata);
9508 case dw_val_class_discr_list:
9509 switch (constant_size (size_of_discr_list (AT_discr_list (a))))
9511 case 1:
9512 return DW_FORM_block1;
9513 case 2:
9514 return DW_FORM_block2;
9515 case 4:
9516 return DW_FORM_block4;
9517 default:
9518 gcc_unreachable ();
9521 default:
9522 gcc_unreachable ();
9526 /* Output the encoding of an attribute value. */
9528 static void
9529 output_value_format (dw_attr_node *a)
9531 enum dwarf_form form = value_format (a);
9533 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
9536 /* Given a die and id, produce the appropriate abbreviations. */
9538 static void
9539 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
9541 unsigned ix;
9542 dw_attr_node *a_attr;
9544 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
9545 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
9546 dwarf_tag_name (abbrev->die_tag));
9548 if (abbrev->die_child != NULL)
9549 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
9550 else
9551 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
9553 for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
9555 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
9556 dwarf_attr_name (a_attr->dw_attr));
9557 output_value_format (a_attr);
9558 if (value_format (a_attr) == DW_FORM_implicit_const)
9560 if (AT_class (a_attr) == dw_val_class_file_implicit)
9562 int f = maybe_emit_file (a_attr->dw_attr_val.v.val_file);
9563 const char *filename = a_attr->dw_attr_val.v.val_file->filename;
9564 dw2_asm_output_data_sleb128 (f, "(%s)", filename);
9566 else
9567 dw2_asm_output_data_sleb128 (a_attr->dw_attr_val.v.val_int, NULL);
9571 dw2_asm_output_data (1, 0, NULL);
9572 dw2_asm_output_data (1, 0, NULL);
9576 /* Output the .debug_abbrev section which defines the DIE abbreviation
9577 table. */
9579 static void
9580 output_abbrev_section (void)
9582 unsigned int abbrev_id;
9583 dw_die_ref abbrev;
9585 FOR_EACH_VEC_SAFE_ELT (abbrev_die_table, abbrev_id, abbrev)
9586 if (abbrev_id != 0)
9587 output_die_abbrevs (abbrev_id, abbrev);
9589 /* Terminate the table. */
9590 dw2_asm_output_data (1, 0, NULL);
9593 /* Output a symbol we can use to refer to this DIE from another CU. */
9595 static inline void
9596 output_die_symbol (dw_die_ref die)
9598 const char *sym = die->die_id.die_symbol;
9600 gcc_assert (!die->comdat_type_p);
9602 if (sym == 0)
9603 return;
9605 if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
9606 /* We make these global, not weak; if the target doesn't support
9607 .linkonce, it doesn't support combining the sections, so debugging
9608 will break. */
9609 targetm.asm_out.globalize_label (asm_out_file, sym);
9611 ASM_OUTPUT_LABEL (asm_out_file, sym);
9614 /* Return a new location list, given the begin and end range, and the
9615 expression. */
9617 static inline dw_loc_list_ref
9618 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
9619 const char *section)
9621 dw_loc_list_ref retlist = ggc_cleared_alloc<dw_loc_list_node> ();
9623 retlist->begin = begin;
9624 retlist->begin_entry = NULL;
9625 retlist->end = end;
9626 retlist->expr = expr;
9627 retlist->section = section;
9629 return retlist;
9632 /* Generate a new internal symbol for this location list node, if it
9633 hasn't got one yet. */
9635 static inline void
9636 gen_llsym (dw_loc_list_ref list)
9638 gcc_assert (!list->ll_symbol);
9639 list->ll_symbol = gen_internal_sym ("LLST");
9642 /* Output the location list given to us. */
9644 static void
9645 output_loc_list (dw_loc_list_ref list_head)
9647 if (list_head->emitted)
9648 return;
9649 list_head->emitted = true;
9651 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
9653 dw_loc_list_ref curr = list_head;
9654 const char *last_section = NULL;
9655 const char *base_label = NULL;
9657 /* Walk the location list, and output each range + expression. */
9658 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
9660 unsigned long size;
9661 /* Don't output an entry that starts and ends at the same address. */
9662 if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
9663 continue;
9664 size = size_of_locs (curr->expr);
9665 /* If the expression is too large, drop it on the floor. We could
9666 perhaps put it into DW_TAG_dwarf_procedure and refer to that
9667 in the expression, but >= 64KB expressions for a single value
9668 in a single range are unlikely very useful. */
9669 if (dwarf_version < 5 && size > 0xffff)
9670 continue;
9671 if (dwarf_version >= 5)
9673 if (dwarf_split_debug_info)
9675 /* For -gsplit-dwarf, emit DW_LLE_starx_length, which has
9676 uleb128 index into .debug_addr and uleb128 length. */
9677 dw2_asm_output_data (1, DW_LLE_startx_length,
9678 "DW_LLE_startx_length (%s)",
9679 list_head->ll_symbol);
9680 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
9681 "Location list range start index "
9682 "(%s)", curr->begin);
9683 /* FIXME: This will ICE ifndef HAVE_AS_LEB128.
9684 For that case we probably need to emit DW_LLE_startx_endx,
9685 but we'd need 2 .debug_addr entries rather than just one. */
9686 dw2_asm_output_delta_uleb128 (curr->end, curr->begin,
9687 "Location list length (%s)",
9688 list_head->ll_symbol);
9690 else if (!have_multiple_function_sections && HAVE_AS_LEB128)
9692 /* If all code is in .text section, the base address is
9693 already provided by the CU attributes. Use
9694 DW_LLE_offset_pair where both addresses are uleb128 encoded
9695 offsets against that base. */
9696 dw2_asm_output_data (1, DW_LLE_offset_pair,
9697 "DW_LLE_offset_pair (%s)",
9698 list_head->ll_symbol);
9699 dw2_asm_output_delta_uleb128 (curr->begin, curr->section,
9700 "Location list begin address (%s)",
9701 list_head->ll_symbol);
9702 dw2_asm_output_delta_uleb128 (curr->end, curr->section,
9703 "Location list end address (%s)",
9704 list_head->ll_symbol);
9706 else if (HAVE_AS_LEB128)
9708 /* Otherwise, find out how many consecutive entries could share
9709 the same base entry. If just one, emit DW_LLE_start_length,
9710 otherwise emit DW_LLE_base_address for the base address
9711 followed by a series of DW_LLE_offset_pair. */
9712 if (last_section == NULL || curr->section != last_section)
9714 dw_loc_list_ref curr2;
9715 for (curr2 = curr->dw_loc_next; curr2 != NULL;
9716 curr2 = curr2->dw_loc_next)
9718 if (strcmp (curr2->begin, curr2->end) == 0
9719 && !curr2->force)
9720 continue;
9721 break;
9723 if (curr2 == NULL || curr->section != curr2->section)
9724 last_section = NULL;
9725 else
9727 last_section = curr->section;
9728 base_label = curr->begin;
9729 dw2_asm_output_data (1, DW_LLE_base_address,
9730 "DW_LLE_base_address (%s)",
9731 list_head->ll_symbol);
9732 dw2_asm_output_addr (DWARF2_ADDR_SIZE, base_label,
9733 "Base address (%s)",
9734 list_head->ll_symbol);
9737 /* Only one entry with the same base address. Use
9738 DW_LLE_start_length with absolute address and uleb128
9739 length. */
9740 if (last_section == NULL)
9742 dw2_asm_output_data (1, DW_LLE_start_length,
9743 "DW_LLE_start_length (%s)",
9744 list_head->ll_symbol);
9745 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
9746 "Location list begin address (%s)",
9747 list_head->ll_symbol);
9748 dw2_asm_output_delta_uleb128 (curr->end, curr->begin,
9749 "Location list length "
9750 "(%s)", list_head->ll_symbol);
9752 /* Otherwise emit DW_LLE_offset_pair, relative to above emitted
9753 DW_LLE_base_address. */
9754 else
9756 dw2_asm_output_data (1, DW_LLE_offset_pair,
9757 "DW_LLE_offset_pair (%s)",
9758 list_head->ll_symbol);
9759 dw2_asm_output_delta_uleb128 (curr->begin, base_label,
9760 "Location list begin address "
9761 "(%s)", list_head->ll_symbol);
9762 dw2_asm_output_delta_uleb128 (curr->end, base_label,
9763 "Location list end address "
9764 "(%s)", list_head->ll_symbol);
9767 /* The assembler does not support .uleb128 directive. Emit
9768 DW_LLE_start_end with a pair of absolute addresses. */
9769 else
9771 dw2_asm_output_data (1, DW_LLE_start_end,
9772 "DW_LLE_start_end (%s)",
9773 list_head->ll_symbol);
9774 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
9775 "Location list begin address (%s)",
9776 list_head->ll_symbol);
9777 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
9778 "Location list end address (%s)",
9779 list_head->ll_symbol);
9782 else if (dwarf_split_debug_info)
9784 /* For -gsplit-dwarf -gdwarf-{2,3,4} emit index into .debug_addr
9785 and 4 byte length. */
9786 dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
9787 "Location list start/length entry (%s)",
9788 list_head->ll_symbol);
9789 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
9790 "Location list range start index (%s)",
9791 curr->begin);
9792 /* The length field is 4 bytes. If we ever need to support
9793 an 8-byte length, we can add a new DW_LLE code or fall back
9794 to DW_LLE_GNU_start_end_entry. */
9795 dw2_asm_output_delta (4, curr->end, curr->begin,
9796 "Location list range length (%s)",
9797 list_head->ll_symbol);
9799 else if (!have_multiple_function_sections)
9801 /* Pair of relative addresses against start of text section. */
9802 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
9803 "Location list begin address (%s)",
9804 list_head->ll_symbol);
9805 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
9806 "Location list end address (%s)",
9807 list_head->ll_symbol);
9809 else
9811 /* Pair of absolute addresses. */
9812 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
9813 "Location list begin address (%s)",
9814 list_head->ll_symbol);
9815 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
9816 "Location list end address (%s)",
9817 list_head->ll_symbol);
9820 /* Output the block length for this list of location operations. */
9821 if (dwarf_version >= 5)
9822 dw2_asm_output_data_uleb128 (size, "Location expression size");
9823 else
9825 gcc_assert (size <= 0xffff);
9826 dw2_asm_output_data (2, size, "Location expression size");
9829 output_loc_sequence (curr->expr, -1);
9832 /* And finally list termination. */
9833 if (dwarf_version >= 5)
9834 dw2_asm_output_data (1, DW_LLE_end_of_list,
9835 "DW_LLE_end_of_list (%s)", list_head->ll_symbol);
9836 else if (dwarf_split_debug_info)
9837 dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
9838 "Location list terminator (%s)",
9839 list_head->ll_symbol);
9840 else
9842 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
9843 "Location list terminator begin (%s)",
9844 list_head->ll_symbol);
9845 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
9846 "Location list terminator end (%s)",
9847 list_head->ll_symbol);
9851 /* Output a range_list offset into the .debug_ranges or .debug_rnglists
9852 section. Emit a relocated reference if val_entry is NULL, otherwise,
9853 emit an indirect reference. */
9855 static void
9856 output_range_list_offset (dw_attr_node *a)
9858 const char *name = dwarf_attr_name (a->dw_attr);
9860 if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
9862 if (dwarf_version >= 5)
9864 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
9865 dw2_asm_output_offset (DWARF_OFFSET_SIZE, r->label,
9866 debug_ranges_section, "%s", name);
9868 else
9870 char *p = strchr (ranges_section_label, '\0');
9871 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
9872 a->dw_attr_val.v.val_offset * 2 * DWARF2_ADDR_SIZE);
9873 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
9874 debug_ranges_section, "%s", name);
9875 *p = '\0';
9878 else if (dwarf_version >= 5)
9880 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
9881 gcc_assert (rnglist_idx);
9882 dw2_asm_output_data_uleb128 (r->idx, "%s", name);
9884 else
9885 dw2_asm_output_data (DWARF_OFFSET_SIZE,
9886 a->dw_attr_val.v.val_offset * 2 * DWARF2_ADDR_SIZE,
9887 "%s (offset from %s)", name, ranges_section_label);
9890 /* Output the offset into the debug_loc section. */
9892 static void
9893 output_loc_list_offset (dw_attr_node *a)
9895 char *sym = AT_loc_list (a)->ll_symbol;
9897 gcc_assert (sym);
9898 if (!dwarf_split_debug_info)
9899 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
9900 "%s", dwarf_attr_name (a->dw_attr));
9901 else if (dwarf_version >= 5)
9903 gcc_assert (AT_loc_list (a)->num_assigned);
9904 dw2_asm_output_data_uleb128 (AT_loc_list (a)->hash, "%s (%s)",
9905 dwarf_attr_name (a->dw_attr),
9906 sym);
9908 else
9909 dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
9910 "%s", dwarf_attr_name (a->dw_attr));
9913 /* Output an attribute's index or value appropriately. */
9915 static void
9916 output_attr_index_or_value (dw_attr_node *a)
9918 const char *name = dwarf_attr_name (a->dw_attr);
9920 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
9922 dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
9923 return;
9925 switch (AT_class (a))
9927 case dw_val_class_addr:
9928 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
9929 break;
9930 case dw_val_class_high_pc:
9931 case dw_val_class_lbl_id:
9932 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
9933 break;
9934 default:
9935 gcc_unreachable ();
9939 /* Output a type signature. */
9941 static inline void
9942 output_signature (const char *sig, const char *name)
9944 int i;
9946 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9947 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
9950 /* Output a discriminant value. */
9952 static inline void
9953 output_discr_value (dw_discr_value *discr_value, const char *name)
9955 if (discr_value->pos)
9956 dw2_asm_output_data_uleb128 (discr_value->v.uval, "%s", name);
9957 else
9958 dw2_asm_output_data_sleb128 (discr_value->v.sval, "%s", name);
9961 /* Output the DIE and its attributes. Called recursively to generate
9962 the definitions of each child DIE. */
9964 static void
9965 output_die (dw_die_ref die)
9967 dw_attr_node *a;
9968 dw_die_ref c;
9969 unsigned long size;
9970 unsigned ix;
9972 /* If someone in another CU might refer to us, set up a symbol for
9973 them to point to. */
9974 if (! die->comdat_type_p && die->die_id.die_symbol)
9975 output_die_symbol (die);
9977 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
9978 (unsigned long)die->die_offset,
9979 dwarf_tag_name (die->die_tag));
9981 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9983 const char *name = dwarf_attr_name (a->dw_attr);
9985 switch (AT_class (a))
9987 case dw_val_class_addr:
9988 output_attr_index_or_value (a);
9989 break;
9991 case dw_val_class_offset:
9992 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
9993 "%s", name);
9994 break;
9996 case dw_val_class_range_list:
9997 output_range_list_offset (a);
9998 break;
10000 case dw_val_class_loc:
10001 size = size_of_locs (AT_loc (a));
10003 /* Output the block length for this list of location operations. */
10004 if (dwarf_version >= 4)
10005 dw2_asm_output_data_uleb128 (size, "%s", name);
10006 else
10007 dw2_asm_output_data (constant_size (size), size, "%s", name);
10009 output_loc_sequence (AT_loc (a), -1);
10010 break;
10012 case dw_val_class_const:
10013 /* ??? It would be slightly more efficient to use a scheme like is
10014 used for unsigned constants below, but gdb 4.x does not sign
10015 extend. Gdb 5.x does sign extend. */
10016 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
10017 break;
10019 case dw_val_class_unsigned_const:
10021 int csize = constant_size (AT_unsigned (a));
10022 if (dwarf_version == 3
10023 && a->dw_attr == DW_AT_data_member_location
10024 && csize >= 4)
10025 dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
10026 else
10027 dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
10029 break;
10031 case dw_val_class_const_implicit:
10032 if (flag_debug_asm)
10033 fprintf (asm_out_file, "\t\t\t%s %s ("
10034 HOST_WIDE_INT_PRINT_DEC ")\n",
10035 ASM_COMMENT_START, name, AT_int (a));
10036 break;
10038 case dw_val_class_unsigned_const_implicit:
10039 if (flag_debug_asm)
10040 fprintf (asm_out_file, "\t\t\t%s %s ("
10041 HOST_WIDE_INT_PRINT_HEX ")\n",
10042 ASM_COMMENT_START, name, AT_unsigned (a));
10043 break;
10045 case dw_val_class_const_double:
10047 unsigned HOST_WIDE_INT first, second;
10049 if (HOST_BITS_PER_WIDE_INT >= DWARF_LARGEST_DATA_FORM_BITS)
10050 dw2_asm_output_data (1,
10051 HOST_BITS_PER_DOUBLE_INT
10052 / HOST_BITS_PER_CHAR,
10053 NULL);
10055 if (WORDS_BIG_ENDIAN)
10057 first = a->dw_attr_val.v.val_double.high;
10058 second = a->dw_attr_val.v.val_double.low;
10060 else
10062 first = a->dw_attr_val.v.val_double.low;
10063 second = a->dw_attr_val.v.val_double.high;
10066 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
10067 first, "%s", name);
10068 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
10069 second, NULL);
10071 break;
10073 case dw_val_class_wide_int:
10075 int i;
10076 int len = get_full_len (*a->dw_attr_val.v.val_wide);
10077 int l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
10078 if (len * HOST_BITS_PER_WIDE_INT > DWARF_LARGEST_DATA_FORM_BITS)
10079 dw2_asm_output_data (1, get_full_len (*a->dw_attr_val.v.val_wide)
10080 * l, NULL);
10082 if (WORDS_BIG_ENDIAN)
10083 for (i = len - 1; i >= 0; --i)
10085 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
10086 "%s", name);
10087 name = "";
10089 else
10090 for (i = 0; i < len; ++i)
10092 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
10093 "%s", name);
10094 name = "";
10097 break;
10099 case dw_val_class_vec:
10101 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
10102 unsigned int len = a->dw_attr_val.v.val_vec.length;
10103 unsigned int i;
10104 unsigned char *p;
10106 dw2_asm_output_data (constant_size (len * elt_size),
10107 len * elt_size, "%s", name);
10108 if (elt_size > sizeof (HOST_WIDE_INT))
10110 elt_size /= 2;
10111 len *= 2;
10113 for (i = 0, p = (unsigned char *) a->dw_attr_val.v.val_vec.array;
10114 i < len;
10115 i++, p += elt_size)
10116 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
10117 "fp or vector constant word %u", i);
10118 break;
10121 case dw_val_class_flag:
10122 if (dwarf_version >= 4)
10124 /* Currently all add_AT_flag calls pass in 1 as last argument,
10125 so DW_FORM_flag_present can be used. If that ever changes,
10126 we'll need to use DW_FORM_flag and have some optimization
10127 in build_abbrev_table that will change those to
10128 DW_FORM_flag_present if it is set to 1 in all DIEs using
10129 the same abbrev entry. */
10130 gcc_assert (AT_flag (a) == 1);
10131 if (flag_debug_asm)
10132 fprintf (asm_out_file, "\t\t\t%s %s\n",
10133 ASM_COMMENT_START, name);
10134 break;
10136 dw2_asm_output_data (1, AT_flag (a), "%s", name);
10137 break;
10139 case dw_val_class_loc_list:
10140 output_loc_list_offset (a);
10141 break;
10143 case dw_val_class_die_ref:
10144 if (AT_ref_external (a))
10146 if (AT_ref (a)->comdat_type_p)
10148 comdat_type_node *type_node
10149 = AT_ref (a)->die_id.die_type_node;
10151 gcc_assert (type_node);
10152 output_signature (type_node->signature, name);
10154 else
10156 const char *sym = AT_ref (a)->die_id.die_symbol;
10157 int size;
10159 gcc_assert (sym);
10160 /* In DWARF2, DW_FORM_ref_addr is sized by target address
10161 length, whereas in DWARF3 it's always sized as an
10162 offset. */
10163 if (dwarf_version == 2)
10164 size = DWARF2_ADDR_SIZE;
10165 else
10166 size = DWARF_OFFSET_SIZE;
10167 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
10168 name);
10171 else
10173 gcc_assert (AT_ref (a)->die_offset);
10174 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
10175 "%s", name);
10177 break;
10179 case dw_val_class_fde_ref:
10181 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
10183 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
10184 a->dw_attr_val.v.val_fde_index * 2);
10185 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
10186 "%s", name);
10188 break;
10190 case dw_val_class_vms_delta:
10191 #ifdef ASM_OUTPUT_DWARF_VMS_DELTA
10192 dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
10193 AT_vms_delta2 (a), AT_vms_delta1 (a),
10194 "%s", name);
10195 #else
10196 dw2_asm_output_delta (DWARF_OFFSET_SIZE,
10197 AT_vms_delta2 (a), AT_vms_delta1 (a),
10198 "%s", name);
10199 #endif
10200 break;
10202 case dw_val_class_lbl_id:
10203 output_attr_index_or_value (a);
10204 break;
10206 case dw_val_class_lineptr:
10207 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
10208 debug_line_section, "%s", name);
10209 break;
10211 case dw_val_class_macptr:
10212 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
10213 debug_macinfo_section, "%s", name);
10214 break;
10216 case dw_val_class_loclistsptr:
10217 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
10218 debug_loc_section, "%s", name);
10219 break;
10221 case dw_val_class_str:
10222 if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
10223 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
10224 a->dw_attr_val.v.val_str->label,
10225 debug_str_section,
10226 "%s: \"%s\"", name, AT_string (a));
10227 else if (a->dw_attr_val.v.val_str->form == DW_FORM_line_strp)
10228 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
10229 a->dw_attr_val.v.val_str->label,
10230 debug_line_str_section,
10231 "%s: \"%s\"", name, AT_string (a));
10232 else if (a->dw_attr_val.v.val_str->form == DW_FORM_GNU_str_index)
10233 dw2_asm_output_data_uleb128 (AT_index (a),
10234 "%s: \"%s\"", name, AT_string (a));
10235 else
10236 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
10237 break;
10239 case dw_val_class_file:
10241 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
10243 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
10244 a->dw_attr_val.v.val_file->filename);
10245 break;
10248 case dw_val_class_file_implicit:
10249 if (flag_debug_asm)
10250 fprintf (asm_out_file, "\t\t\t%s %s (%d, %s)\n",
10251 ASM_COMMENT_START, name,
10252 maybe_emit_file (a->dw_attr_val.v.val_file),
10253 a->dw_attr_val.v.val_file->filename);
10254 break;
10256 case dw_val_class_data8:
10258 int i;
10260 for (i = 0; i < 8; i++)
10261 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
10262 i == 0 ? "%s" : NULL, name);
10263 break;
10266 case dw_val_class_high_pc:
10267 dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
10268 get_AT_low_pc (die), "DW_AT_high_pc");
10269 break;
10271 case dw_val_class_discr_value:
10272 output_discr_value (&a->dw_attr_val.v.val_discr_value, name);
10273 break;
10275 case dw_val_class_discr_list:
10277 dw_discr_list_ref list = AT_discr_list (a);
10278 const int size = size_of_discr_list (list);
10280 /* This is a block, so output its length first. */
10281 dw2_asm_output_data (constant_size (size), size,
10282 "%s: block size", name);
10284 for (; list != NULL; list = list->dw_discr_next)
10286 /* One byte for the discriminant value descriptor, and then as
10287 many LEB128 numbers as required. */
10288 if (list->dw_discr_range)
10289 dw2_asm_output_data (1, DW_DSC_range,
10290 "%s: DW_DSC_range", name);
10291 else
10292 dw2_asm_output_data (1, DW_DSC_label,
10293 "%s: DW_DSC_label", name);
10295 output_discr_value (&list->dw_discr_lower_bound, name);
10296 if (list->dw_discr_range)
10297 output_discr_value (&list->dw_discr_upper_bound, name);
10299 break;
10302 default:
10303 gcc_unreachable ();
10307 FOR_EACH_CHILD (die, c, output_die (c));
10309 /* Add null byte to terminate sibling list. */
10310 if (die->die_child != NULL)
10311 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
10312 (unsigned long) die->die_offset);
10315 /* Output the compilation unit that appears at the beginning of the
10316 .debug_info section, and precedes the DIE descriptions. */
10318 static void
10319 output_compilation_unit_header (enum dwarf_unit_type ut)
10321 if (!XCOFF_DEBUGGING_INFO)
10323 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10324 dw2_asm_output_data (4, 0xffffffff,
10325 "Initial length escape value indicating 64-bit DWARF extension");
10326 dw2_asm_output_data (DWARF_OFFSET_SIZE,
10327 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
10328 "Length of Compilation Unit Info");
10331 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
10332 if (dwarf_version >= 5)
10334 const char *name;
10335 switch (ut)
10337 case DW_UT_compile: name = "DW_UT_compile"; break;
10338 case DW_UT_type: name = "DW_UT_type"; break;
10339 case DW_UT_split_compile: name = "DW_UT_split_compile"; break;
10340 case DW_UT_split_type: name = "DW_UT_split_type"; break;
10341 default: gcc_unreachable ();
10343 dw2_asm_output_data (1, ut, "%s", name);
10344 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10346 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
10347 debug_abbrev_section,
10348 "Offset Into Abbrev. Section");
10349 if (dwarf_version < 5)
10350 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10353 /* Output the compilation unit DIE and its children. */
10355 static void
10356 output_comp_unit (dw_die_ref die, int output_if_empty,
10357 const unsigned char *dwo_id)
10359 const char *secname, *oldsym;
10360 char *tmp;
10362 /* Unless we are outputting main CU, we may throw away empty ones. */
10363 if (!output_if_empty && die->die_child == NULL)
10364 return;
10366 /* Even if there are no children of this DIE, we must output the information
10367 about the compilation unit. Otherwise, on an empty translation unit, we
10368 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
10369 will then complain when examining the file. First mark all the DIEs in
10370 this CU so we know which get local refs. */
10371 mark_dies (die);
10373 external_ref_hash_type *extern_map = optimize_external_refs (die);
10375 /* For now, optimize only the main CU, in order to optimize the rest
10376 we'd need to see all of them earlier. Leave the rest for post-linking
10377 tools like DWZ. */
10378 if (die == comp_unit_die ())
10379 abbrev_opt_start = vec_safe_length (abbrev_die_table);
10381 build_abbrev_table (die, extern_map);
10383 optimize_abbrev_table ();
10385 delete extern_map;
10387 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
10388 next_die_offset = (dwo_id
10389 ? DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
10390 : DWARF_COMPILE_UNIT_HEADER_SIZE);
10391 calc_die_sizes (die);
10393 oldsym = die->die_id.die_symbol;
10394 if (oldsym)
10396 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
10398 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
10399 secname = tmp;
10400 die->die_id.die_symbol = NULL;
10401 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
10403 else
10405 switch_to_section (debug_info_section);
10406 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
10407 info_section_emitted = true;
10410 /* Output debugging information. */
10411 output_compilation_unit_header (dwo_id
10412 ? DW_UT_split_compile : DW_UT_compile);
10413 if (dwarf_version >= 5)
10415 if (dwo_id != NULL)
10416 for (int i = 0; i < 8; i++)
10417 dw2_asm_output_data (1, dwo_id[i], i == 0 ? "DWO id" : NULL);
10419 output_die (die);
10421 /* Leave the marks on the main CU, so we can check them in
10422 output_pubnames. */
10423 if (oldsym)
10425 unmark_dies (die);
10426 die->die_id.die_symbol = oldsym;
10430 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
10431 and .debug_pubtypes. This is configured per-target, but can be
10432 overridden by the -gpubnames or -gno-pubnames options. */
10434 static inline bool
10435 want_pubnames (void)
10437 if (debug_info_level <= DINFO_LEVEL_TERSE)
10438 return false;
10439 if (debug_generate_pub_sections != -1)
10440 return debug_generate_pub_sections;
10441 return targetm.want_debug_pub_sections;
10444 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes. */
10446 static void
10447 add_AT_pubnames (dw_die_ref die)
10449 if (want_pubnames ())
10450 add_AT_flag (die, DW_AT_GNU_pubnames, 1);
10453 /* Add a string attribute value to a skeleton DIE. */
10455 static inline void
10456 add_skeleton_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
10457 const char *str)
10459 dw_attr_node attr;
10460 struct indirect_string_node *node;
10462 if (! skeleton_debug_str_hash)
10463 skeleton_debug_str_hash
10464 = hash_table<indirect_string_hasher>::create_ggc (10);
10466 node = find_AT_string_in_table (str, skeleton_debug_str_hash);
10467 find_string_form (node);
10468 if (node->form == DW_FORM_GNU_str_index)
10469 node->form = DW_FORM_strp;
10471 attr.dw_attr = attr_kind;
10472 attr.dw_attr_val.val_class = dw_val_class_str;
10473 attr.dw_attr_val.val_entry = NULL;
10474 attr.dw_attr_val.v.val_str = node;
10475 add_dwarf_attr (die, &attr);
10478 /* Helper function to generate top-level dies for skeleton debug_info and
10479 debug_types. */
10481 static void
10482 add_top_level_skeleton_die_attrs (dw_die_ref die)
10484 const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
10485 const char *comp_dir = comp_dir_string ();
10487 add_skeleton_AT_string (die, dwarf_AT (DW_AT_dwo_name), dwo_file_name);
10488 if (comp_dir != NULL)
10489 add_skeleton_AT_string (die, DW_AT_comp_dir, comp_dir);
10490 add_AT_pubnames (die);
10491 add_AT_lineptr (die, DW_AT_GNU_addr_base, debug_addr_section_label);
10494 /* Output skeleton debug sections that point to the dwo file. */
10496 static void
10497 output_skeleton_debug_sections (dw_die_ref comp_unit,
10498 const unsigned char *dwo_id)
10500 /* These attributes will be found in the full debug_info section. */
10501 remove_AT (comp_unit, DW_AT_producer);
10502 remove_AT (comp_unit, DW_AT_language);
10504 switch_to_section (debug_skeleton_info_section);
10505 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
10507 /* Produce the skeleton compilation-unit header. This one differs enough from
10508 a normal CU header that it's better not to call output_compilation_unit
10509 header. */
10510 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10511 dw2_asm_output_data (4, 0xffffffff,
10512 "Initial length escape value indicating 64-bit "
10513 "DWARF extension");
10515 dw2_asm_output_data (DWARF_OFFSET_SIZE,
10516 DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
10517 - DWARF_INITIAL_LENGTH_SIZE
10518 + size_of_die (comp_unit),
10519 "Length of Compilation Unit Info");
10520 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
10521 if (dwarf_version >= 5)
10523 dw2_asm_output_data (1, DW_UT_skeleton, "DW_UT_skeleton");
10524 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10526 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_abbrev_section_label,
10527 debug_skeleton_abbrev_section,
10528 "Offset Into Abbrev. Section");
10529 if (dwarf_version < 5)
10530 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10531 else
10532 for (int i = 0; i < 8; i++)
10533 dw2_asm_output_data (1, dwo_id[i], i == 0 ? "DWO id" : NULL);
10535 comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
10536 output_die (comp_unit);
10538 /* Build the skeleton debug_abbrev section. */
10539 switch_to_section (debug_skeleton_abbrev_section);
10540 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
10542 output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
10544 dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
10547 /* Output a comdat type unit DIE and its children. */
10549 static void
10550 output_comdat_type_unit (comdat_type_node *node)
10552 const char *secname;
10553 char *tmp;
10554 int i;
10555 #if defined (OBJECT_FORMAT_ELF)
10556 tree comdat_key;
10557 #endif
10559 /* First mark all the DIEs in this CU so we know which get local refs. */
10560 mark_dies (node->root_die);
10562 external_ref_hash_type *extern_map = optimize_external_refs (node->root_die);
10564 build_abbrev_table (node->root_die, extern_map);
10566 delete extern_map;
10567 extern_map = NULL;
10569 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
10570 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
10571 calc_die_sizes (node->root_die);
10573 #if defined (OBJECT_FORMAT_ELF)
10574 if (dwarf_version >= 5)
10576 if (!dwarf_split_debug_info)
10577 secname = ".debug_info";
10578 else
10579 secname = ".debug_info.dwo";
10581 else if (!dwarf_split_debug_info)
10582 secname = ".debug_types";
10583 else
10584 secname = ".debug_types.dwo";
10586 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
10587 sprintf (tmp, dwarf_version >= 5 ? "wi." : "wt.");
10588 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
10589 sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
10590 comdat_key = get_identifier (tmp);
10591 targetm.asm_out.named_section (secname,
10592 SECTION_DEBUG | SECTION_LINKONCE,
10593 comdat_key);
10594 #else
10595 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
10596 sprintf (tmp, (dwarf_version >= 5
10597 ? ".gnu.linkonce.wi." : ".gnu.linkonce.wt."));
10598 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
10599 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
10600 secname = tmp;
10601 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
10602 #endif
10604 /* Output debugging information. */
10605 output_compilation_unit_header (dwarf_split_debug_info
10606 ? DW_UT_split_type : DW_UT_type);
10607 output_signature (node->signature, "Type Signature");
10608 dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
10609 "Offset to Type DIE");
10610 output_die (node->root_die);
10612 unmark_dies (node->root_die);
10615 /* Return the DWARF2/3 pubname associated with a decl. */
10617 static const char *
10618 dwarf2_name (tree decl, int scope)
10620 if (DECL_NAMELESS (decl))
10621 return NULL;
10622 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
10625 /* Add a new entry to .debug_pubnames if appropriate. */
10627 static void
10628 add_pubname_string (const char *str, dw_die_ref die)
10630 pubname_entry e;
10632 e.die = die;
10633 e.name = xstrdup (str);
10634 vec_safe_push (pubname_table, e);
10637 static void
10638 add_pubname (tree decl, dw_die_ref die)
10640 if (!want_pubnames ())
10641 return;
10643 /* Don't add items to the table when we expect that the consumer will have
10644 just read the enclosing die. For example, if the consumer is looking at a
10645 class_member, it will either be inside the class already, or will have just
10646 looked up the class to find the member. Either way, searching the class is
10647 faster than searching the index. */
10648 if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
10649 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
10651 const char *name = dwarf2_name (decl, 1);
10653 if (name)
10654 add_pubname_string (name, die);
10658 /* Add an enumerator to the pubnames section. */
10660 static void
10661 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
10663 pubname_entry e;
10665 gcc_assert (scope_name);
10666 e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
10667 e.die = die;
10668 vec_safe_push (pubname_table, e);
10671 /* Add a new entry to .debug_pubtypes if appropriate. */
10673 static void
10674 add_pubtype (tree decl, dw_die_ref die)
10676 pubname_entry e;
10678 if (!want_pubnames ())
10679 return;
10681 if ((TREE_PUBLIC (decl)
10682 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
10683 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
10685 tree scope = NULL;
10686 const char *scope_name = "";
10687 const char *sep = is_cxx () ? "::" : ".";
10688 const char *name;
10690 scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
10691 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
10693 scope_name = lang_hooks.dwarf_name (scope, 1);
10694 if (scope_name != NULL && scope_name[0] != '\0')
10695 scope_name = concat (scope_name, sep, NULL);
10696 else
10697 scope_name = "";
10700 if (TYPE_P (decl))
10701 name = type_tag (decl);
10702 else
10703 name = lang_hooks.dwarf_name (decl, 1);
10705 /* If we don't have a name for the type, there's no point in adding
10706 it to the table. */
10707 if (name != NULL && name[0] != '\0')
10709 e.die = die;
10710 e.name = concat (scope_name, name, NULL);
10711 vec_safe_push (pubtype_table, e);
10714 /* Although it might be more consistent to add the pubinfo for the
10715 enumerators as their dies are created, they should only be added if the
10716 enum type meets the criteria above. So rather than re-check the parent
10717 enum type whenever an enumerator die is created, just output them all
10718 here. This isn't protected by the name conditional because anonymous
10719 enums don't have names. */
10720 if (die->die_tag == DW_TAG_enumeration_type)
10722 dw_die_ref c;
10724 FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
10729 /* Output a single entry in the pubnames table. */
10731 static void
10732 output_pubname (dw_offset die_offset, pubname_entry *entry)
10734 dw_die_ref die = entry->die;
10735 int is_static = get_AT_flag (die, DW_AT_external) ? 0 : 1;
10737 dw2_asm_output_data (DWARF_OFFSET_SIZE, die_offset, "DIE offset");
10739 if (debug_generate_pub_sections == 2)
10741 /* This logic follows gdb's method for determining the value of the flag
10742 byte. */
10743 uint32_t flags = GDB_INDEX_SYMBOL_KIND_NONE;
10744 switch (die->die_tag)
10746 case DW_TAG_typedef:
10747 case DW_TAG_base_type:
10748 case DW_TAG_subrange_type:
10749 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
10750 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
10751 break;
10752 case DW_TAG_enumerator:
10753 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
10754 GDB_INDEX_SYMBOL_KIND_VARIABLE);
10755 if (!is_cxx () && !is_java ())
10756 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
10757 break;
10758 case DW_TAG_subprogram:
10759 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
10760 GDB_INDEX_SYMBOL_KIND_FUNCTION);
10761 if (!is_ada ())
10762 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
10763 break;
10764 case DW_TAG_constant:
10765 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
10766 GDB_INDEX_SYMBOL_KIND_VARIABLE);
10767 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
10768 break;
10769 case DW_TAG_variable:
10770 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
10771 GDB_INDEX_SYMBOL_KIND_VARIABLE);
10772 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
10773 break;
10774 case DW_TAG_namespace:
10775 case DW_TAG_imported_declaration:
10776 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
10777 break;
10778 case DW_TAG_class_type:
10779 case DW_TAG_interface_type:
10780 case DW_TAG_structure_type:
10781 case DW_TAG_union_type:
10782 case DW_TAG_enumeration_type:
10783 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
10784 if (!is_cxx () && !is_java ())
10785 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
10786 break;
10787 default:
10788 /* An unusual tag. Leave the flag-byte empty. */
10789 break;
10791 dw2_asm_output_data (1, flags >> GDB_INDEX_CU_BITSIZE,
10792 "GDB-index flags");
10795 dw2_asm_output_nstring (entry->name, -1, "external name");
10799 /* Output the public names table used to speed up access to externally
10800 visible names; or the public types table used to find type definitions. */
10802 static void
10803 output_pubnames (vec<pubname_entry, va_gc> *names)
10805 unsigned i;
10806 unsigned long pubnames_length = size_of_pubnames (names);
10807 pubname_entry *pub;
10809 if (!XCOFF_DEBUGGING_INFO)
10811 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10812 dw2_asm_output_data (4, 0xffffffff,
10813 "Initial length escape value indicating 64-bit DWARF extension");
10814 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
10815 "Pub Info Length");
10818 /* Version number for pubnames/pubtypes is independent of dwarf version. */
10819 dw2_asm_output_data (2, 2, "DWARF Version");
10821 if (dwarf_split_debug_info)
10822 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
10823 debug_skeleton_info_section,
10824 "Offset of Compilation Unit Info");
10825 else
10826 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
10827 debug_info_section,
10828 "Offset of Compilation Unit Info");
10829 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
10830 "Compilation Unit Length");
10832 FOR_EACH_VEC_ELT (*names, i, pub)
10834 if (include_pubname_in_output (names, pub))
10836 dw_offset die_offset = pub->die->die_offset;
10838 /* We shouldn't see pubnames for DIEs outside of the main CU. */
10839 if (names == pubname_table && pub->die->die_tag != DW_TAG_enumerator)
10840 gcc_assert (pub->die->die_mark);
10842 /* If we're putting types in their own .debug_types sections,
10843 the .debug_pubtypes table will still point to the compile
10844 unit (not the type unit), so we want to use the offset of
10845 the skeleton DIE (if there is one). */
10846 if (pub->die->comdat_type_p && names == pubtype_table)
10848 comdat_type_node *type_node = pub->die->die_id.die_type_node;
10850 if (type_node != NULL)
10851 die_offset = (type_node->skeleton_die != NULL
10852 ? type_node->skeleton_die->die_offset
10853 : comp_unit_die ()->die_offset);
10856 output_pubname (die_offset, pub);
10860 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
10863 /* Output public names and types tables if necessary. */
10865 static void
10866 output_pubtables (void)
10868 if (!want_pubnames () || !info_section_emitted)
10869 return;
10871 switch_to_section (debug_pubnames_section);
10872 output_pubnames (pubname_table);
10873 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
10874 It shouldn't hurt to emit it always, since pure DWARF2 consumers
10875 simply won't look for the section. */
10876 switch_to_section (debug_pubtypes_section);
10877 output_pubnames (pubtype_table);
10881 /* Output the information that goes into the .debug_aranges table.
10882 Namely, define the beginning and ending address range of the
10883 text section generated for this compilation unit. */
10885 static void
10886 output_aranges (void)
10888 unsigned i;
10889 unsigned long aranges_length = size_of_aranges ();
10891 if (!XCOFF_DEBUGGING_INFO)
10893 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10894 dw2_asm_output_data (4, 0xffffffff,
10895 "Initial length escape value indicating 64-bit DWARF extension");
10896 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
10897 "Length of Address Ranges Info");
10900 /* Version number for aranges is still 2, even up to DWARF5. */
10901 dw2_asm_output_data (2, 2, "DWARF Version");
10902 if (dwarf_split_debug_info)
10903 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
10904 debug_skeleton_info_section,
10905 "Offset of Compilation Unit Info");
10906 else
10907 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
10908 debug_info_section,
10909 "Offset of Compilation Unit Info");
10910 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
10911 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
10913 /* We need to align to twice the pointer size here. */
10914 if (DWARF_ARANGES_PAD_SIZE)
10916 /* Pad using a 2 byte words so that padding is correct for any
10917 pointer size. */
10918 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
10919 2 * DWARF2_ADDR_SIZE);
10920 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
10921 dw2_asm_output_data (2, 0, NULL);
10924 /* It is necessary not to output these entries if the sections were
10925 not used; if the sections were not used, the length will be 0 and
10926 the address may end up as 0 if the section is discarded by ld
10927 --gc-sections, leaving an invalid (0, 0) entry that can be
10928 confused with the terminator. */
10929 if (text_section_used)
10931 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
10932 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
10933 text_section_label, "Length");
10935 if (cold_text_section_used)
10937 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
10938 "Address");
10939 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
10940 cold_text_section_label, "Length");
10943 if (have_multiple_function_sections)
10945 unsigned fde_idx;
10946 dw_fde_ref fde;
10948 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
10950 if (DECL_IGNORED_P (fde->decl))
10951 continue;
10952 if (!fde->in_std_section)
10954 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
10955 "Address");
10956 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
10957 fde->dw_fde_begin, "Length");
10959 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
10961 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
10962 "Address");
10963 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
10964 fde->dw_fde_second_begin, "Length");
10969 /* Output the terminator words. */
10970 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
10971 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
10974 /* Add a new entry to .debug_ranges. Return its index into
10975 ranges_table vector. */
10977 static unsigned int
10978 add_ranges_num (int num, bool maybe_new_sec)
10980 dw_ranges r = { NULL, num, 0, maybe_new_sec };
10981 vec_safe_push (ranges_table, r);
10982 return vec_safe_length (ranges_table) - 1;
10985 /* Add a new entry to .debug_ranges corresponding to a block, or a
10986 range terminator if BLOCK is NULL. MAYBE_NEW_SEC is true if
10987 this entry might be in a different section from previous range. */
10989 static unsigned int
10990 add_ranges (const_tree block, bool maybe_new_sec)
10992 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0, maybe_new_sec);
10995 /* Note that (*rnglist_table)[offset] is either a head of a rnglist
10996 chain, or middle entry of a chain that will be directly referred to. */
10998 static void
10999 note_rnglist_head (unsigned int offset)
11001 if (dwarf_version < 5 || (*ranges_table)[offset].label)
11002 return;
11003 (*ranges_table)[offset].label = gen_internal_sym ("LLRL");
11006 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
11007 When using dwarf_split_debug_info, address attributes in dies destined
11008 for the final executable should be direct references--setting the
11009 parameter force_direct ensures this behavior. */
11011 static void
11012 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
11013 bool *added, bool force_direct)
11015 unsigned int in_use = vec_safe_length (ranges_by_label);
11016 unsigned int offset;
11017 dw_ranges_by_label rbl = { begin, end };
11018 vec_safe_push (ranges_by_label, rbl);
11019 offset = add_ranges_num (-(int)in_use - 1, true);
11020 if (!*added)
11022 add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
11023 *added = true;
11024 note_rnglist_head (offset);
11028 /* Emit .debug_ranges section. */
11030 static void
11031 output_ranges (void)
11033 unsigned i;
11034 static const char *const start_fmt = "Offset %#x";
11035 const char *fmt = start_fmt;
11036 dw_ranges *r;
11038 switch_to_section (debug_ranges_section);
11039 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
11040 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11042 int block_num = r->num;
11044 if (block_num > 0)
11046 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
11047 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
11049 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
11050 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
11052 /* If all code is in the text section, then the compilation
11053 unit base address defaults to DW_AT_low_pc, which is the
11054 base of the text section. */
11055 if (!have_multiple_function_sections)
11057 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
11058 text_section_label,
11059 fmt, i * 2 * DWARF2_ADDR_SIZE);
11060 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
11061 text_section_label, NULL);
11064 /* Otherwise, the compilation unit base address is zero,
11065 which allows us to use absolute addresses, and not worry
11066 about whether the target supports cross-section
11067 arithmetic. */
11068 else
11070 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11071 fmt, i * 2 * DWARF2_ADDR_SIZE);
11072 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
11075 fmt = NULL;
11078 /* Negative block_num stands for an index into ranges_by_label. */
11079 else if (block_num < 0)
11081 int lab_idx = - block_num - 1;
11083 if (!have_multiple_function_sections)
11085 gcc_unreachable ();
11086 #if 0
11087 /* If we ever use add_ranges_by_labels () for a single
11088 function section, all we have to do is to take out
11089 the #if 0 above. */
11090 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11091 (*ranges_by_label)[lab_idx].begin,
11092 text_section_label,
11093 fmt, i * 2 * DWARF2_ADDR_SIZE);
11094 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11095 (*ranges_by_label)[lab_idx].end,
11096 text_section_label, NULL);
11097 #endif
11099 else
11101 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11102 (*ranges_by_label)[lab_idx].begin,
11103 fmt, i * 2 * DWARF2_ADDR_SIZE);
11104 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11105 (*ranges_by_label)[lab_idx].end,
11106 NULL);
11109 else
11111 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11112 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11113 fmt = start_fmt;
11118 /* Non-zero if .debug_line_str should be used for .debug_line section
11119 strings or strings that are likely shareable with those. */
11120 #define DWARF5_USE_DEBUG_LINE_STR \
11121 (!DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET \
11122 && (DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) != 0 \
11123 /* FIXME: there is no .debug_line_str.dwo section, \
11124 for -gsplit-dwarf we should use DW_FORM_strx instead. */ \
11125 && !dwarf_split_debug_info)
11127 /* Assign .debug_rnglists indexes. */
11129 static void
11130 index_rnglists (void)
11132 unsigned i;
11133 dw_ranges *r;
11135 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11136 if (r->label)
11137 r->idx = rnglist_idx++;
11140 /* Emit .debug_rnglists section. */
11142 static void
11143 output_rnglists (void)
11145 unsigned i;
11146 dw_ranges *r;
11147 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
11148 char l2[MAX_ARTIFICIAL_LABEL_BYTES];
11149 char basebuf[MAX_ARTIFICIAL_LABEL_BYTES];
11151 switch_to_section (debug_ranges_section);
11152 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
11153 ASM_GENERATE_INTERNAL_LABEL (l1, DEBUG_RANGES_SECTION_LABEL, 2);
11154 ASM_GENERATE_INTERNAL_LABEL (l2, DEBUG_RANGES_SECTION_LABEL, 3);
11155 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11156 dw2_asm_output_data (4, 0xffffffff,
11157 "Initial length escape value indicating "
11158 "64-bit DWARF extension");
11159 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
11160 "Length of Range Lists");
11161 ASM_OUTPUT_LABEL (asm_out_file, l1);
11162 dw2_asm_output_data (2, dwarf_version, "DWARF Version");
11163 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
11164 dw2_asm_output_data (1, 0, "Segment Size");
11165 /* Emit the offset table only for -gsplit-dwarf. If we don't care
11166 about relocation sizes and primarily care about the size of .debug*
11167 sections in linked shared libraries and executables, then
11168 the offset table plus corresponding DW_FORM_rnglistx uleb128 indexes
11169 into it are usually larger than just DW_FORM_sec_offset offsets
11170 into the .debug_rnglists section. */
11171 dw2_asm_output_data (4, dwarf_split_debug_info ? rnglist_idx : 0,
11172 "Offset Entry Count");
11173 if (dwarf_split_debug_info)
11175 ASM_OUTPUT_LABEL (asm_out_file, ranges_base_label);
11176 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11177 if (r->label)
11178 dw2_asm_output_delta (DWARF_OFFSET_SIZE, r->label,
11179 ranges_base_label, NULL);
11182 const char *lab = "";
11183 unsigned int len = vec_safe_length (ranges_table);
11184 const char *base = NULL;
11185 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11187 int block_num = r->num;
11189 if (r->label)
11191 ASM_OUTPUT_LABEL (asm_out_file, r->label);
11192 lab = r->label;
11194 if (HAVE_AS_LEB128 && (r->label || r->maybe_new_sec))
11195 base = NULL;
11196 if (block_num > 0)
11198 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
11199 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
11201 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
11202 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
11204 if (HAVE_AS_LEB128)
11206 /* If all code is in the text section, then the compilation
11207 unit base address defaults to DW_AT_low_pc, which is the
11208 base of the text section. */
11209 if (!have_multiple_function_sections)
11211 dw2_asm_output_data (1, DW_RLE_offset_pair,
11212 "DW_RLE_offset_pair (%s)", lab);
11213 dw2_asm_output_delta_uleb128 (blabel, text_section_label,
11214 "Range begin address (%s)", lab);
11215 dw2_asm_output_delta_uleb128 (elabel, text_section_label,
11216 "Range end address (%s)", lab);
11217 continue;
11219 if (base == NULL)
11221 dw_ranges *r2 = NULL;
11222 if (i < len - 1)
11223 r2 = &(*ranges_table)[i + 1];
11224 if (r2
11225 && r2->num != 0
11226 && r2->label == NULL
11227 && !r2->maybe_new_sec)
11229 dw2_asm_output_data (1, DW_RLE_base_address,
11230 "DW_RLE_base_address (%s)", lab);
11231 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11232 "Base address (%s)", lab);
11233 strcpy (basebuf, blabel);
11234 base = basebuf;
11237 if (base)
11239 dw2_asm_output_data (1, DW_RLE_offset_pair,
11240 "DW_RLE_offset_pair (%s)", lab);
11241 dw2_asm_output_delta_uleb128 (blabel, base,
11242 "Range begin address (%s)", lab);
11243 dw2_asm_output_delta_uleb128 (elabel, base,
11244 "Range end address (%s)", lab);
11245 continue;
11247 dw2_asm_output_data (1, DW_RLE_start_length,
11248 "DW_RLE_start_length (%s)", lab);
11249 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11250 "Range begin address (%s)", lab);
11251 dw2_asm_output_delta_uleb128 (elabel, blabel,
11252 "Range length (%s)", lab);
11254 else
11256 dw2_asm_output_data (1, DW_RLE_start_end,
11257 "DW_RLE_start_end (%s)", lab);
11258 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11259 "Range begin address (%s)", lab);
11260 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel,
11261 "Range end address (%s)", lab);
11265 /* Negative block_num stands for an index into ranges_by_label. */
11266 else if (block_num < 0)
11268 int lab_idx = - block_num - 1;
11269 const char *blabel = (*ranges_by_label)[lab_idx].begin;
11270 const char *elabel = (*ranges_by_label)[lab_idx].end;
11272 if (!have_multiple_function_sections)
11273 gcc_unreachable ();
11274 if (HAVE_AS_LEB128)
11276 dw2_asm_output_data (1, DW_RLE_start_length,
11277 "DW_RLE_start_length (%s)", lab);
11278 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11279 "Range begin address (%s)", lab);
11280 dw2_asm_output_delta_uleb128 (elabel, blabel,
11281 "Range length (%s)", lab);
11283 else
11285 dw2_asm_output_data (1, DW_RLE_start_end,
11286 "DW_RLE_start_end (%s)", lab);
11287 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11288 "Range begin address (%s)", lab);
11289 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel,
11290 "Range end address (%s)", lab);
11293 else
11294 dw2_asm_output_data (1, DW_RLE_end_of_list,
11295 "DW_RLE_end_of_list (%s)", lab);
11297 ASM_OUTPUT_LABEL (asm_out_file, l2);
11300 /* Data structure containing information about input files. */
11301 struct file_info
11303 const char *path; /* Complete file name. */
11304 const char *fname; /* File name part. */
11305 int length; /* Length of entire string. */
11306 struct dwarf_file_data * file_idx; /* Index in input file table. */
11307 int dir_idx; /* Index in directory table. */
11310 /* Data structure containing information about directories with source
11311 files. */
11312 struct dir_info
11314 const char *path; /* Path including directory name. */
11315 int length; /* Path length. */
11316 int prefix; /* Index of directory entry which is a prefix. */
11317 int count; /* Number of files in this directory. */
11318 int dir_idx; /* Index of directory used as base. */
11321 /* Callback function for file_info comparison. We sort by looking at
11322 the directories in the path. */
11324 static int
11325 file_info_cmp (const void *p1, const void *p2)
11327 const struct file_info *const s1 = (const struct file_info *) p1;
11328 const struct file_info *const s2 = (const struct file_info *) p2;
11329 const unsigned char *cp1;
11330 const unsigned char *cp2;
11332 /* Take care of file names without directories. We need to make sure that
11333 we return consistent values to qsort since some will get confused if
11334 we return the same value when identical operands are passed in opposite
11335 orders. So if neither has a directory, return 0 and otherwise return
11336 1 or -1 depending on which one has the directory. */
11337 if ((s1->path == s1->fname || s2->path == s2->fname))
11338 return (s2->path == s2->fname) - (s1->path == s1->fname);
11340 cp1 = (const unsigned char *) s1->path;
11341 cp2 = (const unsigned char *) s2->path;
11343 while (1)
11345 ++cp1;
11346 ++cp2;
11347 /* Reached the end of the first path? If so, handle like above. */
11348 if ((cp1 == (const unsigned char *) s1->fname)
11349 || (cp2 == (const unsigned char *) s2->fname))
11350 return ((cp2 == (const unsigned char *) s2->fname)
11351 - (cp1 == (const unsigned char *) s1->fname));
11353 /* Character of current path component the same? */
11354 else if (*cp1 != *cp2)
11355 return *cp1 - *cp2;
11359 struct file_name_acquire_data
11361 struct file_info *files;
11362 int used_files;
11363 int max_files;
11366 /* Traversal function for the hash table. */
11369 file_name_acquire (dwarf_file_data **slot, file_name_acquire_data *fnad)
11371 struct dwarf_file_data *d = *slot;
11372 struct file_info *fi;
11373 const char *f;
11375 gcc_assert (fnad->max_files >= d->emitted_number);
11377 if (! d->emitted_number)
11378 return 1;
11380 gcc_assert (fnad->max_files != fnad->used_files);
11382 fi = fnad->files + fnad->used_files++;
11384 /* Skip all leading "./". */
11385 f = d->filename;
11386 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
11387 f += 2;
11389 /* Create a new array entry. */
11390 fi->path = f;
11391 fi->length = strlen (f);
11392 fi->file_idx = d;
11394 /* Search for the file name part. */
11395 f = strrchr (f, DIR_SEPARATOR);
11396 #if defined (DIR_SEPARATOR_2)
11398 char *g = strrchr (fi->path, DIR_SEPARATOR_2);
11400 if (g != NULL)
11402 if (f == NULL || f < g)
11403 f = g;
11406 #endif
11408 fi->fname = f == NULL ? fi->path : f + 1;
11409 return 1;
11412 /* Helper function for output_file_names. Emit a FORM encoded
11413 string STR, with assembly comment start ENTRY_KIND and
11414 index IDX */
11416 static void
11417 output_line_string (enum dwarf_form form, const char *str,
11418 const char *entry_kind, unsigned int idx)
11420 switch (form)
11422 case DW_FORM_string:
11423 dw2_asm_output_nstring (str, -1, "%s: %#x", entry_kind, idx);
11424 break;
11425 case DW_FORM_line_strp:
11426 if (!debug_line_str_hash)
11427 debug_line_str_hash
11428 = hash_table<indirect_string_hasher>::create_ggc (10);
11430 struct indirect_string_node *node;
11431 node = find_AT_string_in_table (str, debug_line_str_hash);
11432 set_indirect_string (node);
11433 node->form = form;
11434 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
11435 debug_line_str_section, "%s: %#x: \"%s\"",
11436 entry_kind, 0, node->str);
11437 break;
11438 default:
11439 gcc_unreachable ();
11443 /* Output the directory table and the file name table. We try to minimize
11444 the total amount of memory needed. A heuristic is used to avoid large
11445 slowdowns with many input files. */
11447 static void
11448 output_file_names (void)
11450 struct file_name_acquire_data fnad;
11451 int numfiles;
11452 struct file_info *files;
11453 struct dir_info *dirs;
11454 int *saved;
11455 int *savehere;
11456 int *backmap;
11457 int ndirs;
11458 int idx_offset;
11459 int i;
11461 if (!last_emitted_file)
11463 if (dwarf_version >= 5)
11465 dw2_asm_output_data (1, 0, "Directory entry format count");
11466 dw2_asm_output_data_uleb128 (0, "Directories count");
11467 dw2_asm_output_data (1, 0, "File name entry format count");
11468 dw2_asm_output_data_uleb128 (0, "File names count");
11470 else
11472 dw2_asm_output_data (1, 0, "End directory table");
11473 dw2_asm_output_data (1, 0, "End file name table");
11475 return;
11478 numfiles = last_emitted_file->emitted_number;
11480 /* Allocate the various arrays we need. */
11481 files = XALLOCAVEC (struct file_info, numfiles);
11482 dirs = XALLOCAVEC (struct dir_info, numfiles);
11484 fnad.files = files;
11485 fnad.used_files = 0;
11486 fnad.max_files = numfiles;
11487 file_table->traverse<file_name_acquire_data *, file_name_acquire> (&fnad);
11488 gcc_assert (fnad.used_files == fnad.max_files);
11490 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
11492 /* Find all the different directories used. */
11493 dirs[0].path = files[0].path;
11494 dirs[0].length = files[0].fname - files[0].path;
11495 dirs[0].prefix = -1;
11496 dirs[0].count = 1;
11497 dirs[0].dir_idx = 0;
11498 files[0].dir_idx = 0;
11499 ndirs = 1;
11501 for (i = 1; i < numfiles; i++)
11502 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
11503 && memcmp (dirs[ndirs - 1].path, files[i].path,
11504 dirs[ndirs - 1].length) == 0)
11506 /* Same directory as last entry. */
11507 files[i].dir_idx = ndirs - 1;
11508 ++dirs[ndirs - 1].count;
11510 else
11512 int j;
11514 /* This is a new directory. */
11515 dirs[ndirs].path = files[i].path;
11516 dirs[ndirs].length = files[i].fname - files[i].path;
11517 dirs[ndirs].count = 1;
11518 dirs[ndirs].dir_idx = ndirs;
11519 files[i].dir_idx = ndirs;
11521 /* Search for a prefix. */
11522 dirs[ndirs].prefix = -1;
11523 for (j = 0; j < ndirs; j++)
11524 if (dirs[j].length < dirs[ndirs].length
11525 && dirs[j].length > 1
11526 && (dirs[ndirs].prefix == -1
11527 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
11528 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
11529 dirs[ndirs].prefix = j;
11531 ++ndirs;
11534 /* Now to the actual work. We have to find a subset of the directories which
11535 allow expressing the file name using references to the directory table
11536 with the least amount of characters. We do not do an exhaustive search
11537 where we would have to check out every combination of every single
11538 possible prefix. Instead we use a heuristic which provides nearly optimal
11539 results in most cases and never is much off. */
11540 saved = XALLOCAVEC (int, ndirs);
11541 savehere = XALLOCAVEC (int, ndirs);
11543 memset (saved, '\0', ndirs * sizeof (saved[0]));
11544 for (i = 0; i < ndirs; i++)
11546 int j;
11547 int total;
11549 /* We can always save some space for the current directory. But this
11550 does not mean it will be enough to justify adding the directory. */
11551 savehere[i] = dirs[i].length;
11552 total = (savehere[i] - saved[i]) * dirs[i].count;
11554 for (j = i + 1; j < ndirs; j++)
11556 savehere[j] = 0;
11557 if (saved[j] < dirs[i].length)
11559 /* Determine whether the dirs[i] path is a prefix of the
11560 dirs[j] path. */
11561 int k;
11563 k = dirs[j].prefix;
11564 while (k != -1 && k != (int) i)
11565 k = dirs[k].prefix;
11567 if (k == (int) i)
11569 /* Yes it is. We can possibly save some memory by
11570 writing the filenames in dirs[j] relative to
11571 dirs[i]. */
11572 savehere[j] = dirs[i].length;
11573 total += (savehere[j] - saved[j]) * dirs[j].count;
11578 /* Check whether we can save enough to justify adding the dirs[i]
11579 directory. */
11580 if (total > dirs[i].length + 1)
11582 /* It's worthwhile adding. */
11583 for (j = i; j < ndirs; j++)
11584 if (savehere[j] > 0)
11586 /* Remember how much we saved for this directory so far. */
11587 saved[j] = savehere[j];
11589 /* Remember the prefix directory. */
11590 dirs[j].dir_idx = i;
11595 /* Emit the directory name table. */
11596 idx_offset = dirs[0].length > 0 ? 1 : 0;
11597 enum dwarf_form str_form = DW_FORM_string;
11598 enum dwarf_form idx_form = DW_FORM_udata;
11599 if (dwarf_version >= 5)
11601 const char *comp_dir = comp_dir_string ();
11602 if (comp_dir == NULL)
11603 comp_dir = "";
11604 dw2_asm_output_data (1, 1, "Directory entry format count");
11605 if (DWARF5_USE_DEBUG_LINE_STR)
11606 str_form = DW_FORM_line_strp;
11607 dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
11608 dw2_asm_output_data_uleb128 (str_form, "%s",
11609 get_DW_FORM_name (str_form));
11610 dw2_asm_output_data_uleb128 (ndirs + idx_offset, "Directories count");
11611 if (str_form == DW_FORM_string)
11613 dw2_asm_output_nstring (comp_dir, -1, "Directory Entry: %#x", 0);
11614 for (i = 1 - idx_offset; i < ndirs; i++)
11615 dw2_asm_output_nstring (dirs[i].path,
11616 dirs[i].length
11617 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
11618 "Directory Entry: %#x", i + idx_offset);
11620 else
11622 output_line_string (str_form, comp_dir, "Directory Entry", 0);
11623 for (i = 1 - idx_offset; i < ndirs; i++)
11625 const char *str
11626 = ggc_alloc_string (dirs[i].path,
11627 dirs[i].length
11628 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR);
11629 output_line_string (str_form, str, "Directory Entry",
11630 (unsigned) i + idx_offset);
11634 else
11636 for (i = 1 - idx_offset; i < ndirs; i++)
11637 dw2_asm_output_nstring (dirs[i].path,
11638 dirs[i].length
11639 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
11640 "Directory Entry: %#x", i + idx_offset);
11642 dw2_asm_output_data (1, 0, "End directory table");
11645 /* We have to emit them in the order of emitted_number since that's
11646 used in the debug info generation. To do this efficiently we
11647 generate a back-mapping of the indices first. */
11648 backmap = XALLOCAVEC (int, numfiles);
11649 for (i = 0; i < numfiles; i++)
11650 backmap[files[i].file_idx->emitted_number - 1] = i;
11652 if (dwarf_version >= 5)
11654 const char *filename0 = get_AT_string (comp_unit_die (), DW_AT_name);
11655 if (filename0 == NULL)
11656 filename0 = "";
11657 /* DW_LNCT_directory_index can use DW_FORM_udata, DW_FORM_data1 and
11658 DW_FORM_data2. Choose one based on the number of directories
11659 and how much space would they occupy in each encoding.
11660 If we have at most 256 directories, all indexes fit into
11661 a single byte, so DW_FORM_data1 is most compact (if there
11662 are at most 128 directories, DW_FORM_udata would be as
11663 compact as that, but not shorter and slower to decode). */
11664 if (ndirs + idx_offset <= 256)
11665 idx_form = DW_FORM_data1;
11666 /* If there are more than 65536 directories, we have to use
11667 DW_FORM_udata, DW_FORM_data2 can't refer to them.
11668 Otherwise, compute what space would occupy if all the indexes
11669 used DW_FORM_udata - sum - and compare that to how large would
11670 be DW_FORM_data2 encoding, and pick the more efficient one. */
11671 else if (ndirs + idx_offset <= 65536)
11673 unsigned HOST_WIDE_INT sum = 1;
11674 for (i = 0; i < numfiles; i++)
11676 int file_idx = backmap[i];
11677 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
11678 sum += size_of_uleb128 (dir_idx);
11680 if (sum >= HOST_WIDE_INT_UC (2) * (numfiles + 1))
11681 idx_form = DW_FORM_data2;
11683 #ifdef VMS_DEBUGGING_INFO
11684 dw2_asm_output_data (1, 4, "File name entry format count");
11685 #else
11686 dw2_asm_output_data (1, 2, "File name entry format count");
11687 #endif
11688 dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
11689 dw2_asm_output_data_uleb128 (str_form, "%s",
11690 get_DW_FORM_name (str_form));
11691 dw2_asm_output_data_uleb128 (DW_LNCT_directory_index,
11692 "DW_LNCT_directory_index");
11693 dw2_asm_output_data_uleb128 (idx_form, "%s",
11694 get_DW_FORM_name (idx_form));
11695 #ifdef VMS_DEBUGGING_INFO
11696 dw2_asm_output_data_uleb128 (DW_LNCT_timestamp, "DW_LNCT_timestamp");
11697 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
11698 dw2_asm_output_data_uleb128 (DW_LNCT_size, "DW_LNCT_size");
11699 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
11700 #endif
11701 dw2_asm_output_data_uleb128 (numfiles + 1, "File names count");
11703 output_line_string (str_form, filename0, "File Entry", 0);
11705 /* Include directory index. */
11706 if (dwarf_version >= 5 && idx_form != DW_FORM_udata)
11707 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
11708 0, NULL);
11709 else
11710 dw2_asm_output_data_uleb128 (0, NULL);
11712 #ifdef VMS_DEBUGGING_INFO
11713 dw2_asm_output_data_uleb128 (0, NULL);
11714 dw2_asm_output_data_uleb128 (0, NULL);
11715 #endif
11718 /* Now write all the file names. */
11719 for (i = 0; i < numfiles; i++)
11721 int file_idx = backmap[i];
11722 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
11724 #ifdef VMS_DEBUGGING_INFO
11725 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
11727 /* Setting these fields can lead to debugger miscomparisons,
11728 but VMS Debug requires them to be set correctly. */
11730 int ver;
11731 long long cdt;
11732 long siz;
11733 int maxfilelen = (strlen (files[file_idx].path)
11734 + dirs[dir_idx].length
11735 + MAX_VMS_VERSION_LEN + 1);
11736 char *filebuf = XALLOCAVEC (char, maxfilelen);
11738 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
11739 snprintf (filebuf, maxfilelen, "%s;%d",
11740 files[file_idx].path + dirs[dir_idx].length, ver);
11742 output_line_string (str_form, filebuf, "File Entry", (unsigned) i + 1);
11744 /* Include directory index. */
11745 if (dwarf_version >= 5 && idx_form != DW_FORM_udata)
11746 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
11747 dir_idx + idx_offset, NULL);
11748 else
11749 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
11751 /* Modification time. */
11752 dw2_asm_output_data_uleb128 ((vms_file_stats_name (files[file_idx].path,
11753 &cdt, 0, 0, 0) == 0)
11754 ? cdt : 0, NULL);
11756 /* File length in bytes. */
11757 dw2_asm_output_data_uleb128 ((vms_file_stats_name (files[file_idx].path,
11758 0, &siz, 0, 0) == 0)
11759 ? siz : 0, NULL);
11760 #else
11761 output_line_string (str_form,
11762 files[file_idx].path + dirs[dir_idx].length,
11763 "File Entry", (unsigned) i + 1);
11765 /* Include directory index. */
11766 if (dwarf_version >= 5 && idx_form != DW_FORM_udata)
11767 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
11768 dir_idx + idx_offset, NULL);
11769 else
11770 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
11772 if (dwarf_version >= 5)
11773 continue;
11775 /* Modification time. */
11776 dw2_asm_output_data_uleb128 (0, NULL);
11778 /* File length in bytes. */
11779 dw2_asm_output_data_uleb128 (0, NULL);
11780 #endif /* VMS_DEBUGGING_INFO */
11783 if (dwarf_version < 5)
11784 dw2_asm_output_data (1, 0, "End file name table");
11788 /* Output one line number table into the .debug_line section. */
11790 static void
11791 output_one_line_info_table (dw_line_info_table *table)
11793 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
11794 unsigned int current_line = 1;
11795 bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
11796 dw_line_info_entry *ent;
11797 size_t i;
11799 FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
11801 switch (ent->opcode)
11803 case LI_set_address:
11804 /* ??? Unfortunately, we have little choice here currently, and
11805 must always use the most general form. GCC does not know the
11806 address delta itself, so we can't use DW_LNS_advance_pc. Many
11807 ports do have length attributes which will give an upper bound
11808 on the address range. We could perhaps use length attributes
11809 to determine when it is safe to use DW_LNS_fixed_advance_pc. */
11810 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
11812 /* This can handle any delta. This takes
11813 4+DWARF2_ADDR_SIZE bytes. */
11814 dw2_asm_output_data (1, 0, "set address %s", line_label);
11815 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
11816 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
11817 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
11818 break;
11820 case LI_set_line:
11821 if (ent->val == current_line)
11823 /* We still need to start a new row, so output a copy insn. */
11824 dw2_asm_output_data (1, DW_LNS_copy,
11825 "copy line %u", current_line);
11827 else
11829 int line_offset = ent->val - current_line;
11830 int line_delta = line_offset - DWARF_LINE_BASE;
11832 current_line = ent->val;
11833 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
11835 /* This can handle deltas from -10 to 234, using the current
11836 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
11837 This takes 1 byte. */
11838 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
11839 "line %u", current_line);
11841 else
11843 /* This can handle any delta. This takes at least 4 bytes,
11844 depending on the value being encoded. */
11845 dw2_asm_output_data (1, DW_LNS_advance_line,
11846 "advance to line %u", current_line);
11847 dw2_asm_output_data_sleb128 (line_offset, NULL);
11848 dw2_asm_output_data (1, DW_LNS_copy, NULL);
11851 break;
11853 case LI_set_file:
11854 dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
11855 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
11856 break;
11858 case LI_set_column:
11859 dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
11860 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
11861 break;
11863 case LI_negate_stmt:
11864 current_is_stmt = !current_is_stmt;
11865 dw2_asm_output_data (1, DW_LNS_negate_stmt,
11866 "is_stmt %d", current_is_stmt);
11867 break;
11869 case LI_set_prologue_end:
11870 dw2_asm_output_data (1, DW_LNS_set_prologue_end,
11871 "set prologue end");
11872 break;
11874 case LI_set_epilogue_begin:
11875 dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
11876 "set epilogue begin");
11877 break;
11879 case LI_set_discriminator:
11880 dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
11881 dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
11882 dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
11883 dw2_asm_output_data_uleb128 (ent->val, NULL);
11884 break;
11888 /* Emit debug info for the address of the end of the table. */
11889 dw2_asm_output_data (1, 0, "set address %s", table->end_label);
11890 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
11891 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
11892 dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
11894 dw2_asm_output_data (1, 0, "end sequence");
11895 dw2_asm_output_data_uleb128 (1, NULL);
11896 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
11899 /* Output the source line number correspondence information. This
11900 information goes into the .debug_line section. */
11902 static void
11903 output_line_info (bool prologue_only)
11905 static unsigned int generation;
11906 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
11907 char p1[MAX_ARTIFICIAL_LABEL_BYTES], p2[MAX_ARTIFICIAL_LABEL_BYTES];
11908 bool saw_one = false;
11909 int opc;
11911 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, generation);
11912 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, generation);
11913 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, generation);
11914 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, generation++);
11916 if (!XCOFF_DEBUGGING_INFO)
11918 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11919 dw2_asm_output_data (4, 0xffffffff,
11920 "Initial length escape value indicating 64-bit DWARF extension");
11921 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
11922 "Length of Source Line Info");
11925 ASM_OUTPUT_LABEL (asm_out_file, l1);
11927 dw2_asm_output_data (2, dwarf_version, "DWARF Version");
11928 if (dwarf_version >= 5)
11930 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
11931 dw2_asm_output_data (1, 0, "Segment Size");
11933 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
11934 ASM_OUTPUT_LABEL (asm_out_file, p1);
11936 /* Define the architecture-dependent minimum instruction length (in bytes).
11937 In this implementation of DWARF, this field is used for information
11938 purposes only. Since GCC generates assembly language, we have no
11939 a priori knowledge of how many instruction bytes are generated for each
11940 source line, and therefore can use only the DW_LNE_set_address and
11941 DW_LNS_fixed_advance_pc line information commands. Accordingly, we fix
11942 this as '1', which is "correct enough" for all architectures,
11943 and don't let the target override. */
11944 dw2_asm_output_data (1, 1, "Minimum Instruction Length");
11946 if (dwarf_version >= 4)
11947 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
11948 "Maximum Operations Per Instruction");
11949 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
11950 "Default is_stmt_start flag");
11951 dw2_asm_output_data (1, DWARF_LINE_BASE,
11952 "Line Base Value (Special Opcodes)");
11953 dw2_asm_output_data (1, DWARF_LINE_RANGE,
11954 "Line Range Value (Special Opcodes)");
11955 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
11956 "Special Opcode Base");
11958 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
11960 int n_op_args;
11961 switch (opc)
11963 case DW_LNS_advance_pc:
11964 case DW_LNS_advance_line:
11965 case DW_LNS_set_file:
11966 case DW_LNS_set_column:
11967 case DW_LNS_fixed_advance_pc:
11968 case DW_LNS_set_isa:
11969 n_op_args = 1;
11970 break;
11971 default:
11972 n_op_args = 0;
11973 break;
11976 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
11977 opc, n_op_args);
11980 /* Write out the information about the files we use. */
11981 output_file_names ();
11982 ASM_OUTPUT_LABEL (asm_out_file, p2);
11983 if (prologue_only)
11985 /* Output the marker for the end of the line number info. */
11986 ASM_OUTPUT_LABEL (asm_out_file, l2);
11987 return;
11990 if (separate_line_info)
11992 dw_line_info_table *table;
11993 size_t i;
11995 FOR_EACH_VEC_ELT (*separate_line_info, i, table)
11996 if (table->in_use)
11998 output_one_line_info_table (table);
11999 saw_one = true;
12002 if (cold_text_section_line_info && cold_text_section_line_info->in_use)
12004 output_one_line_info_table (cold_text_section_line_info);
12005 saw_one = true;
12008 /* ??? Some Darwin linkers crash on a .debug_line section with no
12009 sequences. Further, merely a DW_LNE_end_sequence entry is not
12010 sufficient -- the address column must also be initialized.
12011 Make sure to output at least one set_address/end_sequence pair,
12012 choosing .text since that section is always present. */
12013 if (text_section_line_info->in_use || !saw_one)
12014 output_one_line_info_table (text_section_line_info);
12016 /* Output the marker for the end of the line number info. */
12017 ASM_OUTPUT_LABEL (asm_out_file, l2);
12020 /* Return true if DW_AT_endianity should be emitted according to REVERSE. */
12022 static inline bool
12023 need_endianity_attribute_p (bool reverse)
12025 return reverse && (dwarf_version >= 3 || !dwarf_strict);
12028 /* Given a pointer to a tree node for some base type, return a pointer to
12029 a DIE that describes the given type. REVERSE is true if the type is
12030 to be interpreted in the reverse storage order wrt the target order.
12032 This routine must only be called for GCC type nodes that correspond to
12033 Dwarf base (fundamental) types. */
12035 static dw_die_ref
12036 base_type_die (tree type, bool reverse)
12038 dw_die_ref base_type_result;
12039 enum dwarf_type encoding;
12040 bool fpt_used = false;
12041 struct fixed_point_type_info fpt_info;
12042 tree type_bias = NULL_TREE;
12044 if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
12045 return 0;
12047 /* If this is a subtype that should not be emitted as a subrange type,
12048 use the base type. See subrange_type_for_debug_p. */
12049 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
12050 type = TREE_TYPE (type);
12052 switch (TREE_CODE (type))
12054 case INTEGER_TYPE:
12055 if ((dwarf_version >= 4 || !dwarf_strict)
12056 && TYPE_NAME (type)
12057 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12058 && DECL_IS_BUILTIN (TYPE_NAME (type))
12059 && DECL_NAME (TYPE_NAME (type)))
12061 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
12062 if (strcmp (name, "char16_t") == 0
12063 || strcmp (name, "char32_t") == 0)
12065 encoding = DW_ATE_UTF;
12066 break;
12069 if ((dwarf_version >= 3 || !dwarf_strict)
12070 && lang_hooks.types.get_fixed_point_type_info)
12072 memset (&fpt_info, 0, sizeof (fpt_info));
12073 if (lang_hooks.types.get_fixed_point_type_info (type, &fpt_info))
12075 fpt_used = true;
12076 encoding = ((TYPE_UNSIGNED (type))
12077 ? DW_ATE_unsigned_fixed
12078 : DW_ATE_signed_fixed);
12079 break;
12082 if (TYPE_STRING_FLAG (type))
12084 if (TYPE_UNSIGNED (type))
12085 encoding = DW_ATE_unsigned_char;
12086 else
12087 encoding = DW_ATE_signed_char;
12089 else if (TYPE_UNSIGNED (type))
12090 encoding = DW_ATE_unsigned;
12091 else
12092 encoding = DW_ATE_signed;
12094 if (!dwarf_strict
12095 && lang_hooks.types.get_type_bias)
12096 type_bias = lang_hooks.types.get_type_bias (type);
12097 break;
12099 case REAL_TYPE:
12100 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
12102 if (dwarf_version >= 3 || !dwarf_strict)
12103 encoding = DW_ATE_decimal_float;
12104 else
12105 encoding = DW_ATE_lo_user;
12107 else
12108 encoding = DW_ATE_float;
12109 break;
12111 case FIXED_POINT_TYPE:
12112 if (!(dwarf_version >= 3 || !dwarf_strict))
12113 encoding = DW_ATE_lo_user;
12114 else if (TYPE_UNSIGNED (type))
12115 encoding = DW_ATE_unsigned_fixed;
12116 else
12117 encoding = DW_ATE_signed_fixed;
12118 break;
12120 /* Dwarf2 doesn't know anything about complex ints, so use
12121 a user defined type for it. */
12122 case COMPLEX_TYPE:
12123 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
12124 encoding = DW_ATE_complex_float;
12125 else
12126 encoding = DW_ATE_lo_user;
12127 break;
12129 case BOOLEAN_TYPE:
12130 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
12131 encoding = DW_ATE_boolean;
12132 break;
12134 default:
12135 /* No other TREE_CODEs are Dwarf fundamental types. */
12136 gcc_unreachable ();
12139 base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
12141 add_AT_unsigned (base_type_result, DW_AT_byte_size,
12142 int_size_in_bytes (type));
12143 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
12145 if (need_endianity_attribute_p (reverse))
12146 add_AT_unsigned (base_type_result, DW_AT_endianity,
12147 BYTES_BIG_ENDIAN ? DW_END_little : DW_END_big);
12149 add_alignment_attribute (base_type_result, type);
12151 if (fpt_used)
12153 switch (fpt_info.scale_factor_kind)
12155 case fixed_point_scale_factor_binary:
12156 add_AT_int (base_type_result, DW_AT_binary_scale,
12157 fpt_info.scale_factor.binary);
12158 break;
12160 case fixed_point_scale_factor_decimal:
12161 add_AT_int (base_type_result, DW_AT_decimal_scale,
12162 fpt_info.scale_factor.decimal);
12163 break;
12165 case fixed_point_scale_factor_arbitrary:
12166 /* Arbitrary scale factors cannot be described in standard DWARF,
12167 yet. */
12168 if (!dwarf_strict)
12170 /* Describe the scale factor as a rational constant. */
12171 const dw_die_ref scale_factor
12172 = new_die (DW_TAG_constant, comp_unit_die (), type);
12174 add_AT_unsigned (scale_factor, DW_AT_GNU_numerator,
12175 fpt_info.scale_factor.arbitrary.numerator);
12176 add_AT_int (scale_factor, DW_AT_GNU_denominator,
12177 fpt_info.scale_factor.arbitrary.denominator);
12179 add_AT_die_ref (base_type_result, DW_AT_small, scale_factor);
12181 break;
12183 default:
12184 gcc_unreachable ();
12188 if (type_bias)
12189 add_scalar_info (base_type_result, DW_AT_GNU_bias, type_bias,
12190 dw_scalar_form_constant
12191 | dw_scalar_form_exprloc
12192 | dw_scalar_form_reference,
12193 NULL);
12195 add_pubtype (type, base_type_result);
12197 return base_type_result;
12200 /* A C++ function with deduced return type can have a TEMPLATE_TYPE_PARM
12201 named 'auto' in its type: return true for it, false otherwise. */
12203 static inline bool
12204 is_cxx_auto (tree type)
12206 if (is_cxx ())
12208 tree name = TYPE_IDENTIFIER (type);
12209 if (name == get_identifier ("auto")
12210 || name == get_identifier ("decltype(auto)"))
12211 return true;
12213 return false;
12216 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
12217 given input type is a Dwarf "fundamental" type. Otherwise return null. */
12219 static inline int
12220 is_base_type (tree type)
12222 switch (TREE_CODE (type))
12224 case ERROR_MARK:
12225 case VOID_TYPE:
12226 case INTEGER_TYPE:
12227 case REAL_TYPE:
12228 case FIXED_POINT_TYPE:
12229 case COMPLEX_TYPE:
12230 case BOOLEAN_TYPE:
12231 case POINTER_BOUNDS_TYPE:
12232 return 1;
12234 case ARRAY_TYPE:
12235 case RECORD_TYPE:
12236 case UNION_TYPE:
12237 case QUAL_UNION_TYPE:
12238 case ENUMERAL_TYPE:
12239 case FUNCTION_TYPE:
12240 case METHOD_TYPE:
12241 case POINTER_TYPE:
12242 case REFERENCE_TYPE:
12243 case NULLPTR_TYPE:
12244 case OFFSET_TYPE:
12245 case LANG_TYPE:
12246 case VECTOR_TYPE:
12247 return 0;
12249 default:
12250 if (is_cxx_auto (type))
12251 return 0;
12252 gcc_unreachable ();
12255 return 0;
12258 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
12259 node, return the size in bits for the type if it is a constant, or else
12260 return the alignment for the type if the type's size is not constant, or
12261 else return BITS_PER_WORD if the type actually turns out to be an
12262 ERROR_MARK node. */
12264 static inline unsigned HOST_WIDE_INT
12265 simple_type_size_in_bits (const_tree type)
12267 if (TREE_CODE (type) == ERROR_MARK)
12268 return BITS_PER_WORD;
12269 else if (TYPE_SIZE (type) == NULL_TREE)
12270 return 0;
12271 else if (tree_fits_uhwi_p (TYPE_SIZE (type)))
12272 return tree_to_uhwi (TYPE_SIZE (type));
12273 else
12274 return TYPE_ALIGN (type);
12277 /* Similarly, but return an offset_int instead of UHWI. */
12279 static inline offset_int
12280 offset_int_type_size_in_bits (const_tree type)
12282 if (TREE_CODE (type) == ERROR_MARK)
12283 return BITS_PER_WORD;
12284 else if (TYPE_SIZE (type) == NULL_TREE)
12285 return 0;
12286 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
12287 return wi::to_offset (TYPE_SIZE (type));
12288 else
12289 return TYPE_ALIGN (type);
12292 /* Given a pointer to a tree node for a subrange type, return a pointer
12293 to a DIE that describes the given type. */
12295 static dw_die_ref
12296 subrange_type_die (tree type, tree low, tree high, tree bias,
12297 dw_die_ref context_die)
12299 dw_die_ref subrange_die;
12300 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
12302 if (context_die == NULL)
12303 context_die = comp_unit_die ();
12305 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
12307 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
12309 /* The size of the subrange type and its base type do not match,
12310 so we need to generate a size attribute for the subrange type. */
12311 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
12314 add_alignment_attribute (subrange_die, type);
12316 if (low)
12317 add_bound_info (subrange_die, DW_AT_lower_bound, low, NULL);
12318 if (high)
12319 add_bound_info (subrange_die, DW_AT_upper_bound, high, NULL);
12320 if (bias && !dwarf_strict)
12321 add_scalar_info (subrange_die, DW_AT_GNU_bias, bias,
12322 dw_scalar_form_constant
12323 | dw_scalar_form_exprloc
12324 | dw_scalar_form_reference,
12325 NULL);
12327 return subrange_die;
12330 /* Returns the (const and/or volatile) cv_qualifiers associated with
12331 the decl node. This will normally be augmented with the
12332 cv_qualifiers of the underlying type in add_type_attribute. */
12334 static int
12335 decl_quals (const_tree decl)
12337 return ((TREE_READONLY (decl)
12338 /* The C++ front-end correctly marks reference-typed
12339 variables as readonly, but from a language (and debug
12340 info) standpoint they are not const-qualified. */
12341 && TREE_CODE (TREE_TYPE (decl)) != REFERENCE_TYPE
12342 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED)
12343 | (TREE_THIS_VOLATILE (decl)
12344 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED));
12347 /* Determine the TYPE whose qualifiers match the largest strict subset
12348 of the given TYPE_QUALS, and return its qualifiers. Ignore all
12349 qualifiers outside QUAL_MASK. */
12351 static int
12352 get_nearest_type_subqualifiers (tree type, int type_quals, int qual_mask)
12354 tree t;
12355 int best_rank = 0, best_qual = 0, max_rank;
12357 type_quals &= qual_mask;
12358 max_rank = popcount_hwi (type_quals) - 1;
12360 for (t = TYPE_MAIN_VARIANT (type); t && best_rank < max_rank;
12361 t = TYPE_NEXT_VARIANT (t))
12363 int q = TYPE_QUALS (t) & qual_mask;
12365 if ((q & type_quals) == q && q != type_quals
12366 && check_base_type (t, type))
12368 int rank = popcount_hwi (q);
12370 if (rank > best_rank)
12372 best_rank = rank;
12373 best_qual = q;
12378 return best_qual;
12381 struct dwarf_qual_info_t { int q; enum dwarf_tag t; };
12382 static const dwarf_qual_info_t dwarf_qual_info[] =
12384 { TYPE_QUAL_CONST, DW_TAG_const_type },
12385 { TYPE_QUAL_VOLATILE, DW_TAG_volatile_type },
12386 { TYPE_QUAL_RESTRICT, DW_TAG_restrict_type },
12387 { TYPE_QUAL_ATOMIC, DW_TAG_atomic_type }
12389 static const unsigned int dwarf_qual_info_size
12390 = sizeof (dwarf_qual_info) / sizeof (dwarf_qual_info[0]);
12392 /* If DIE is a qualified DIE of some base DIE with the same parent,
12393 return the base DIE, otherwise return NULL. Set MASK to the
12394 qualifiers added compared to the returned DIE. */
12396 static dw_die_ref
12397 qualified_die_p (dw_die_ref die, int *mask, unsigned int depth)
12399 unsigned int i;
12400 for (i = 0; i < dwarf_qual_info_size; i++)
12401 if (die->die_tag == dwarf_qual_info[i].t)
12402 break;
12403 if (i == dwarf_qual_info_size)
12404 return NULL;
12405 if (vec_safe_length (die->die_attr) != 1)
12406 return NULL;
12407 dw_die_ref type = get_AT_ref (die, DW_AT_type);
12408 if (type == NULL || type->die_parent != die->die_parent)
12409 return NULL;
12410 *mask |= dwarf_qual_info[i].q;
12411 if (depth)
12413 dw_die_ref ret = qualified_die_p (type, mask, depth - 1);
12414 if (ret)
12415 return ret;
12417 return type;
12420 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
12421 entry that chains the modifiers specified by CV_QUALS in front of the
12422 given type. REVERSE is true if the type is to be interpreted in the
12423 reverse storage order wrt the target order. */
12425 static dw_die_ref
12426 modified_type_die (tree type, int cv_quals, bool reverse,
12427 dw_die_ref context_die)
12429 enum tree_code code = TREE_CODE (type);
12430 dw_die_ref mod_type_die;
12431 dw_die_ref sub_die = NULL;
12432 tree item_type = NULL;
12433 tree qualified_type;
12434 tree name, low, high;
12435 dw_die_ref mod_scope;
12436 /* Only these cv-qualifiers are currently handled. */
12437 const int cv_qual_mask = (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE
12438 | TYPE_QUAL_RESTRICT | TYPE_QUAL_ATOMIC);
12440 if (code == ERROR_MARK)
12441 return NULL;
12443 if (lang_hooks.types.get_debug_type)
12445 tree debug_type = lang_hooks.types.get_debug_type (type);
12447 if (debug_type != NULL_TREE && debug_type != type)
12448 return modified_type_die (debug_type, cv_quals, reverse, context_die);
12451 cv_quals &= cv_qual_mask;
12453 /* Don't emit DW_TAG_restrict_type for DWARFv2, since it is a type
12454 tag modifier (and not an attribute) old consumers won't be able
12455 to handle it. */
12456 if (dwarf_version < 3)
12457 cv_quals &= ~TYPE_QUAL_RESTRICT;
12459 /* Likewise for DW_TAG_atomic_type for DWARFv5. */
12460 if (dwarf_version < 5)
12461 cv_quals &= ~TYPE_QUAL_ATOMIC;
12463 /* See if we already have the appropriately qualified variant of
12464 this type. */
12465 qualified_type = get_qualified_type (type, cv_quals);
12467 if (qualified_type == sizetype)
12469 /* Try not to expose the internal sizetype type's name. */
12470 if (TYPE_NAME (qualified_type)
12471 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
12473 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
12475 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
12476 && (TYPE_PRECISION (t)
12477 == TYPE_PRECISION (qualified_type))
12478 && (TYPE_UNSIGNED (t)
12479 == TYPE_UNSIGNED (qualified_type)));
12480 qualified_type = t;
12482 else if (qualified_type == sizetype
12483 && TREE_CODE (sizetype) == TREE_CODE (size_type_node)
12484 && TYPE_PRECISION (sizetype) == TYPE_PRECISION (size_type_node)
12485 && TYPE_UNSIGNED (sizetype) == TYPE_UNSIGNED (size_type_node))
12486 qualified_type = size_type_node;
12490 /* If we do, then we can just use its DIE, if it exists. */
12491 if (qualified_type)
12493 mod_type_die = lookup_type_die (qualified_type);
12495 /* DW_AT_endianity doesn't come from a qualifier on the type. */
12496 if (mod_type_die
12497 && (!need_endianity_attribute_p (reverse)
12498 || !is_base_type (type)
12499 || get_AT_unsigned (mod_type_die, DW_AT_endianity)))
12500 return mod_type_die;
12503 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
12505 /* Handle C typedef types. */
12506 if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
12507 && !DECL_ARTIFICIAL (name))
12509 tree dtype = TREE_TYPE (name);
12511 if (qualified_type == dtype)
12513 /* For a named type, use the typedef. */
12514 gen_type_die (qualified_type, context_die);
12515 return lookup_type_die (qualified_type);
12517 else
12519 int dquals = TYPE_QUALS_NO_ADDR_SPACE (dtype);
12520 dquals &= cv_qual_mask;
12521 if ((dquals & ~cv_quals) != TYPE_UNQUALIFIED
12522 || (cv_quals == dquals && DECL_ORIGINAL_TYPE (name) != type))
12523 /* cv-unqualified version of named type. Just use
12524 the unnamed type to which it refers. */
12525 return modified_type_die (DECL_ORIGINAL_TYPE (name), cv_quals,
12526 reverse, context_die);
12527 /* Else cv-qualified version of named type; fall through. */
12531 mod_scope = scope_die_for (type, context_die);
12533 if (cv_quals)
12535 int sub_quals = 0, first_quals = 0;
12536 unsigned i;
12537 dw_die_ref first = NULL, last = NULL;
12539 /* Determine a lesser qualified type that most closely matches
12540 this one. Then generate DW_TAG_* entries for the remaining
12541 qualifiers. */
12542 sub_quals = get_nearest_type_subqualifiers (type, cv_quals,
12543 cv_qual_mask);
12544 if (sub_quals && use_debug_types)
12546 bool needed = false;
12547 /* If emitting type units, make sure the order of qualifiers
12548 is canonical. Thus, start from unqualified type if
12549 an earlier qualifier is missing in sub_quals, but some later
12550 one is present there. */
12551 for (i = 0; i < dwarf_qual_info_size; i++)
12552 if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
12553 needed = true;
12554 else if (needed && (dwarf_qual_info[i].q & cv_quals))
12556 sub_quals = 0;
12557 break;
12560 mod_type_die = modified_type_die (type, sub_quals, reverse, context_die);
12561 if (mod_scope && mod_type_die && mod_type_die->die_parent == mod_scope)
12563 /* As not all intermediate qualified DIEs have corresponding
12564 tree types, ensure that qualified DIEs in the same scope
12565 as their DW_AT_type are emitted after their DW_AT_type,
12566 only with other qualified DIEs for the same type possibly
12567 in between them. Determine the range of such qualified
12568 DIEs now (first being the base type, last being corresponding
12569 last qualified DIE for it). */
12570 unsigned int count = 0;
12571 first = qualified_die_p (mod_type_die, &first_quals,
12572 dwarf_qual_info_size);
12573 if (first == NULL)
12574 first = mod_type_die;
12575 gcc_assert ((first_quals & ~sub_quals) == 0);
12576 for (count = 0, last = first;
12577 count < (1U << dwarf_qual_info_size);
12578 count++, last = last->die_sib)
12580 int quals = 0;
12581 if (last == mod_scope->die_child)
12582 break;
12583 if (qualified_die_p (last->die_sib, &quals, dwarf_qual_info_size)
12584 != first)
12585 break;
12589 for (i = 0; i < dwarf_qual_info_size; i++)
12590 if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
12592 dw_die_ref d;
12593 if (first && first != last)
12595 for (d = first->die_sib; ; d = d->die_sib)
12597 int quals = 0;
12598 qualified_die_p (d, &quals, dwarf_qual_info_size);
12599 if (quals == (first_quals | dwarf_qual_info[i].q))
12600 break;
12601 if (d == last)
12603 d = NULL;
12604 break;
12607 if (d)
12609 mod_type_die = d;
12610 continue;
12613 if (first)
12615 d = ggc_cleared_alloc<die_node> ();
12616 d->die_tag = dwarf_qual_info[i].t;
12617 add_child_die_after (mod_scope, d, last);
12618 last = d;
12620 else
12621 d = new_die (dwarf_qual_info[i].t, mod_scope, type);
12622 if (mod_type_die)
12623 add_AT_die_ref (d, DW_AT_type, mod_type_die);
12624 mod_type_die = d;
12625 first_quals |= dwarf_qual_info[i].q;
12628 else if (code == POINTER_TYPE || code == REFERENCE_TYPE)
12630 dwarf_tag tag = DW_TAG_pointer_type;
12631 if (code == REFERENCE_TYPE)
12633 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
12634 tag = DW_TAG_rvalue_reference_type;
12635 else
12636 tag = DW_TAG_reference_type;
12638 mod_type_die = new_die (tag, mod_scope, type);
12640 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
12641 simple_type_size_in_bits (type) / BITS_PER_UNIT);
12642 add_alignment_attribute (mod_type_die, type);
12643 item_type = TREE_TYPE (type);
12645 addr_space_t as = TYPE_ADDR_SPACE (item_type);
12646 if (!ADDR_SPACE_GENERIC_P (as))
12648 int action = targetm.addr_space.debug (as);
12649 if (action >= 0)
12651 /* Positive values indicate an address_class. */
12652 add_AT_unsigned (mod_type_die, DW_AT_address_class, action);
12654 else
12656 /* Negative values indicate an (inverted) segment base reg. */
12657 dw_loc_descr_ref d
12658 = one_reg_loc_descriptor (~action, VAR_INIT_STATUS_INITIALIZED);
12659 add_AT_loc (mod_type_die, DW_AT_segment, d);
12663 else if (code == INTEGER_TYPE
12664 && TREE_TYPE (type) != NULL_TREE
12665 && subrange_type_for_debug_p (type, &low, &high))
12667 tree bias = NULL_TREE;
12668 if (lang_hooks.types.get_type_bias)
12669 bias = lang_hooks.types.get_type_bias (type);
12670 mod_type_die = subrange_type_die (type, low, high, bias, context_die);
12671 item_type = TREE_TYPE (type);
12673 else if (is_base_type (type))
12674 mod_type_die = base_type_die (type, reverse);
12675 else
12677 gen_type_die (type, context_die);
12679 /* We have to get the type_main_variant here (and pass that to the
12680 `lookup_type_die' routine) because the ..._TYPE node we have
12681 might simply be a *copy* of some original type node (where the
12682 copy was created to help us keep track of typedef names) and
12683 that copy might have a different TYPE_UID from the original
12684 ..._TYPE node. */
12685 if (TREE_CODE (type) == FUNCTION_TYPE
12686 || TREE_CODE (type) == METHOD_TYPE)
12688 /* For function/method types, can't just use type_main_variant here,
12689 because that can have different ref-qualifiers for C++,
12690 but try to canonicalize. */
12691 tree main = TYPE_MAIN_VARIANT (type);
12692 for (tree t = main; t; t = TYPE_NEXT_VARIANT (t))
12693 if (TYPE_QUALS_NO_ADDR_SPACE (t) == 0
12694 && check_base_type (t, main)
12695 && check_lang_type (t, type))
12696 return lookup_type_die (t);
12697 return lookup_type_die (type);
12699 else if (TREE_CODE (type) != VECTOR_TYPE
12700 && TREE_CODE (type) != ARRAY_TYPE)
12701 return lookup_type_die (type_main_variant (type));
12702 else
12703 /* Vectors have the debugging information in the type,
12704 not the main variant. */
12705 return lookup_type_die (type);
12708 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
12709 don't output a DW_TAG_typedef, since there isn't one in the
12710 user's program; just attach a DW_AT_name to the type.
12711 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
12712 if the base type already has the same name. */
12713 if (name
12714 && ((TREE_CODE (name) != TYPE_DECL
12715 && (qualified_type == TYPE_MAIN_VARIANT (type)
12716 || (cv_quals == TYPE_UNQUALIFIED)))
12717 || (TREE_CODE (name) == TYPE_DECL
12718 && TREE_TYPE (name) == qualified_type
12719 && DECL_NAME (name))))
12721 if (TREE_CODE (name) == TYPE_DECL)
12722 /* Could just call add_name_and_src_coords_attributes here,
12723 but since this is a builtin type it doesn't have any
12724 useful source coordinates anyway. */
12725 name = DECL_NAME (name);
12726 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
12728 /* This probably indicates a bug. */
12729 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
12731 name = TYPE_IDENTIFIER (type);
12732 add_name_attribute (mod_type_die,
12733 name ? IDENTIFIER_POINTER (name) : "__unknown__");
12736 if (qualified_type)
12737 equate_type_number_to_die (qualified_type, mod_type_die);
12739 if (item_type)
12740 /* We must do this after the equate_type_number_to_die call, in case
12741 this is a recursive type. This ensures that the modified_type_die
12742 recursion will terminate even if the type is recursive. Recursive
12743 types are possible in Ada. */
12744 sub_die = modified_type_die (item_type,
12745 TYPE_QUALS_NO_ADDR_SPACE (item_type),
12746 reverse,
12747 context_die);
12749 if (sub_die != NULL)
12750 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
12752 add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
12753 if (TYPE_ARTIFICIAL (type))
12754 add_AT_flag (mod_type_die, DW_AT_artificial, 1);
12756 return mod_type_die;
12759 /* Generate DIEs for the generic parameters of T.
12760 T must be either a generic type or a generic function.
12761 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
12763 static void
12764 gen_generic_params_dies (tree t)
12766 tree parms, args;
12767 int parms_num, i;
12768 dw_die_ref die = NULL;
12769 int non_default;
12771 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
12772 return;
12774 if (TYPE_P (t))
12775 die = lookup_type_die (t);
12776 else if (DECL_P (t))
12777 die = lookup_decl_die (t);
12779 gcc_assert (die);
12781 parms = lang_hooks.get_innermost_generic_parms (t);
12782 if (!parms)
12783 /* T has no generic parameter. It means T is neither a generic type
12784 or function. End of story. */
12785 return;
12787 parms_num = TREE_VEC_LENGTH (parms);
12788 args = lang_hooks.get_innermost_generic_args (t);
12789 if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
12790 non_default = int_cst_value (TREE_CHAIN (args));
12791 else
12792 non_default = TREE_VEC_LENGTH (args);
12793 for (i = 0; i < parms_num; i++)
12795 tree parm, arg, arg_pack_elems;
12796 dw_die_ref parm_die;
12798 parm = TREE_VEC_ELT (parms, i);
12799 arg = TREE_VEC_ELT (args, i);
12800 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
12801 gcc_assert (parm && TREE_VALUE (parm) && arg);
12803 if (parm && TREE_VALUE (parm) && arg)
12805 /* If PARM represents a template parameter pack,
12806 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
12807 by DW_TAG_template_*_parameter DIEs for the argument
12808 pack elements of ARG. Note that ARG would then be
12809 an argument pack. */
12810 if (arg_pack_elems)
12811 parm_die = template_parameter_pack_die (TREE_VALUE (parm),
12812 arg_pack_elems,
12813 die);
12814 else
12815 parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
12816 true /* emit name */, die);
12817 if (i >= non_default)
12818 add_AT_flag (parm_die, DW_AT_default_value, 1);
12823 /* Create and return a DIE for PARM which should be
12824 the representation of a generic type parameter.
12825 For instance, in the C++ front end, PARM would be a template parameter.
12826 ARG is the argument to PARM.
12827 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
12828 name of the PARM.
12829 PARENT_DIE is the parent DIE which the new created DIE should be added to,
12830 as a child node. */
12832 static dw_die_ref
12833 generic_parameter_die (tree parm, tree arg,
12834 bool emit_name_p,
12835 dw_die_ref parent_die)
12837 dw_die_ref tmpl_die = NULL;
12838 const char *name = NULL;
12840 if (!parm || !DECL_NAME (parm) || !arg)
12841 return NULL;
12843 /* We support non-type generic parameters and arguments,
12844 type generic parameters and arguments, as well as
12845 generic generic parameters (a.k.a. template template parameters in C++)
12846 and arguments. */
12847 if (TREE_CODE (parm) == PARM_DECL)
12848 /* PARM is a nontype generic parameter */
12849 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
12850 else if (TREE_CODE (parm) == TYPE_DECL)
12851 /* PARM is a type generic parameter. */
12852 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
12853 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
12854 /* PARM is a generic generic parameter.
12855 Its DIE is a GNU extension. It shall have a
12856 DW_AT_name attribute to represent the name of the template template
12857 parameter, and a DW_AT_GNU_template_name attribute to represent the
12858 name of the template template argument. */
12859 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
12860 parent_die, parm);
12861 else
12862 gcc_unreachable ();
12864 if (tmpl_die)
12866 tree tmpl_type;
12868 /* If PARM is a generic parameter pack, it means we are
12869 emitting debug info for a template argument pack element.
12870 In other terms, ARG is a template argument pack element.
12871 In that case, we don't emit any DW_AT_name attribute for
12872 the die. */
12873 if (emit_name_p)
12875 name = IDENTIFIER_POINTER (DECL_NAME (parm));
12876 gcc_assert (name);
12877 add_AT_string (tmpl_die, DW_AT_name, name);
12880 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
12882 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
12883 TMPL_DIE should have a child DW_AT_type attribute that is set
12884 to the type of the argument to PARM, which is ARG.
12885 If PARM is a type generic parameter, TMPL_DIE should have a
12886 child DW_AT_type that is set to ARG. */
12887 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
12888 add_type_attribute (tmpl_die, tmpl_type,
12889 (TREE_THIS_VOLATILE (tmpl_type)
12890 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED),
12891 false, parent_die);
12893 else
12895 /* So TMPL_DIE is a DIE representing a
12896 a generic generic template parameter, a.k.a template template
12897 parameter in C++ and arg is a template. */
12899 /* The DW_AT_GNU_template_name attribute of the DIE must be set
12900 to the name of the argument. */
12901 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
12902 if (name)
12903 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
12906 if (TREE_CODE (parm) == PARM_DECL)
12907 /* So PARM is a non-type generic parameter.
12908 DWARF3 5.6.8 says we must set a DW_AT_const_value child
12909 attribute of TMPL_DIE which value represents the value
12910 of ARG.
12911 We must be careful here:
12912 The value of ARG might reference some function decls.
12913 We might currently be emitting debug info for a generic
12914 type and types are emitted before function decls, we don't
12915 know if the function decls referenced by ARG will actually be
12916 emitted after cgraph computations.
12917 So must defer the generation of the DW_AT_const_value to
12918 after cgraph is ready. */
12919 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
12922 return tmpl_die;
12925 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
12926 PARM_PACK must be a template parameter pack. The returned DIE
12927 will be child DIE of PARENT_DIE. */
12929 static dw_die_ref
12930 template_parameter_pack_die (tree parm_pack,
12931 tree parm_pack_args,
12932 dw_die_ref parent_die)
12934 dw_die_ref die;
12935 int j;
12937 gcc_assert (parent_die && parm_pack);
12939 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
12940 add_name_and_src_coords_attributes (die, parm_pack);
12941 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
12942 generic_parameter_die (parm_pack,
12943 TREE_VEC_ELT (parm_pack_args, j),
12944 false /* Don't emit DW_AT_name */,
12945 die);
12946 return die;
12949 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
12950 an enumerated type. */
12952 static inline int
12953 type_is_enum (const_tree type)
12955 return TREE_CODE (type) == ENUMERAL_TYPE;
12958 /* Return the DBX register number described by a given RTL node. */
12960 static unsigned int
12961 dbx_reg_number (const_rtx rtl)
12963 unsigned regno = REGNO (rtl);
12965 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
12967 #ifdef LEAF_REG_REMAP
12968 if (crtl->uses_only_leaf_regs)
12970 int leaf_reg = LEAF_REG_REMAP (regno);
12971 if (leaf_reg != -1)
12972 regno = (unsigned) leaf_reg;
12974 #endif
12976 regno = DBX_REGISTER_NUMBER (regno);
12977 gcc_assert (regno != INVALID_REGNUM);
12978 return regno;
12981 /* Optionally add a DW_OP_piece term to a location description expression.
12982 DW_OP_piece is only added if the location description expression already
12983 doesn't end with DW_OP_piece. */
12985 static void
12986 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
12988 dw_loc_descr_ref loc;
12990 if (*list_head != NULL)
12992 /* Find the end of the chain. */
12993 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
12996 if (loc->dw_loc_opc != DW_OP_piece)
12997 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
13001 /* Return a location descriptor that designates a machine register or
13002 zero if there is none. */
13004 static dw_loc_descr_ref
13005 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
13007 rtx regs;
13009 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
13010 return 0;
13012 /* We only use "frame base" when we're sure we're talking about the
13013 post-prologue local stack frame. We do this by *not* running
13014 register elimination until this point, and recognizing the special
13015 argument pointer and soft frame pointer rtx's.
13016 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
13017 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
13018 && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
13020 dw_loc_descr_ref result = NULL;
13022 if (dwarf_version >= 4 || !dwarf_strict)
13024 result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
13025 initialized);
13026 if (result)
13027 add_loc_descr (&result,
13028 new_loc_descr (DW_OP_stack_value, 0, 0));
13030 return result;
13033 regs = targetm.dwarf_register_span (rtl);
13035 if (REG_NREGS (rtl) > 1 || regs)
13036 return multiple_reg_loc_descriptor (rtl, regs, initialized);
13037 else
13039 unsigned int dbx_regnum = dbx_reg_number (rtl);
13040 if (dbx_regnum == IGNORED_DWARF_REGNUM)
13041 return 0;
13042 return one_reg_loc_descriptor (dbx_regnum, initialized);
13046 /* Return a location descriptor that designates a machine register for
13047 a given hard register number. */
13049 static dw_loc_descr_ref
13050 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
13052 dw_loc_descr_ref reg_loc_descr;
13054 if (regno <= 31)
13055 reg_loc_descr
13056 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
13057 else
13058 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
13060 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13061 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13063 return reg_loc_descr;
13066 /* Given an RTL of a register, return a location descriptor that
13067 designates a value that spans more than one register. */
13069 static dw_loc_descr_ref
13070 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
13071 enum var_init_status initialized)
13073 int size, i;
13074 dw_loc_descr_ref loc_result = NULL;
13076 /* Simple, contiguous registers. */
13077 if (regs == NULL_RTX)
13079 unsigned reg = REGNO (rtl);
13080 int nregs;
13082 #ifdef LEAF_REG_REMAP
13083 if (crtl->uses_only_leaf_regs)
13085 int leaf_reg = LEAF_REG_REMAP (reg);
13086 if (leaf_reg != -1)
13087 reg = (unsigned) leaf_reg;
13089 #endif
13091 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
13092 nregs = REG_NREGS (rtl);
13094 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
13096 loc_result = NULL;
13097 while (nregs--)
13099 dw_loc_descr_ref t;
13101 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
13102 VAR_INIT_STATUS_INITIALIZED);
13103 add_loc_descr (&loc_result, t);
13104 add_loc_descr_op_piece (&loc_result, size);
13105 ++reg;
13107 return loc_result;
13110 /* Now onto stupid register sets in non contiguous locations. */
13112 gcc_assert (GET_CODE (regs) == PARALLEL);
13114 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
13115 loc_result = NULL;
13117 for (i = 0; i < XVECLEN (regs, 0); ++i)
13119 dw_loc_descr_ref t;
13121 t = one_reg_loc_descriptor (dbx_reg_number (XVECEXP (regs, 0, i)),
13122 VAR_INIT_STATUS_INITIALIZED);
13123 add_loc_descr (&loc_result, t);
13124 add_loc_descr_op_piece (&loc_result, size);
13127 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13128 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13129 return loc_result;
13132 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
13134 /* Return a location descriptor that designates a constant i,
13135 as a compound operation from constant (i >> shift), constant shift
13136 and DW_OP_shl. */
13138 static dw_loc_descr_ref
13139 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
13141 dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
13142 add_loc_descr (&ret, int_loc_descriptor (shift));
13143 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
13144 return ret;
13147 /* Return a location descriptor that designates a constant. */
13149 static dw_loc_descr_ref
13150 int_loc_descriptor (HOST_WIDE_INT i)
13152 enum dwarf_location_atom op;
13154 /* Pick the smallest representation of a constant, rather than just
13155 defaulting to the LEB encoding. */
13156 if (i >= 0)
13158 int clz = clz_hwi (i);
13159 int ctz = ctz_hwi (i);
13160 if (i <= 31)
13161 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
13162 else if (i <= 0xff)
13163 op = DW_OP_const1u;
13164 else if (i <= 0xffff)
13165 op = DW_OP_const2u;
13166 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
13167 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
13168 /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
13169 DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
13170 while DW_OP_const4u is 5 bytes. */
13171 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
13172 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13173 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
13174 /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
13175 while DW_OP_const4u is 5 bytes. */
13176 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
13178 else if (DWARF2_ADDR_SIZE == 4 && i > 0x7fffffff
13179 && size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i)
13180 <= 4)
13182 /* As i >= 2**31, the double cast above will yield a negative number.
13183 Since wrapping is defined in DWARF expressions we can output big
13184 positive integers as small negative ones, regardless of the size
13185 of host wide ints.
13187 Here, since the evaluator will handle 32-bit values and since i >=
13188 2**31, we know it's going to be interpreted as a negative literal:
13189 store it this way if we can do better than 5 bytes this way. */
13190 return int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i);
13192 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
13193 op = DW_OP_const4u;
13195 /* Past this point, i >= 0x100000000 and thus DW_OP_constu will take at
13196 least 6 bytes: see if we can do better before falling back to it. */
13197 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13198 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
13199 /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes. */
13200 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
13201 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
13202 && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
13203 >= HOST_BITS_PER_WIDE_INT)
13204 /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
13205 DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes. */
13206 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
13207 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
13208 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
13209 && size_of_uleb128 (i) > 6)
13210 /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes. */
13211 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
13212 else
13213 op = DW_OP_constu;
13215 else
13217 if (i >= -0x80)
13218 op = DW_OP_const1s;
13219 else if (i >= -0x8000)
13220 op = DW_OP_const2s;
13221 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
13223 if (size_of_int_loc_descriptor (i) < 5)
13225 dw_loc_descr_ref ret = int_loc_descriptor (-i);
13226 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
13227 return ret;
13229 op = DW_OP_const4s;
13231 else
13233 if (size_of_int_loc_descriptor (i)
13234 < (unsigned long) 1 + size_of_sleb128 (i))
13236 dw_loc_descr_ref ret = int_loc_descriptor (-i);
13237 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
13238 return ret;
13240 op = DW_OP_consts;
13244 return new_loc_descr (op, i, 0);
13247 /* Likewise, for unsigned constants. */
13249 static dw_loc_descr_ref
13250 uint_loc_descriptor (unsigned HOST_WIDE_INT i)
13252 const unsigned HOST_WIDE_INT max_int = INTTYPE_MAXIMUM (HOST_WIDE_INT);
13253 const unsigned HOST_WIDE_INT max_uint
13254 = INTTYPE_MAXIMUM (unsigned HOST_WIDE_INT);
13256 /* If possible, use the clever signed constants handling. */
13257 if (i <= max_int)
13258 return int_loc_descriptor ((HOST_WIDE_INT) i);
13260 /* Here, we are left with positive numbers that cannot be represented as
13261 HOST_WIDE_INT, i.e.:
13262 max (HOST_WIDE_INT) < i <= max (unsigned HOST_WIDE_INT)
13264 Using DW_OP_const4/8/./u operation to encode them consumes a lot of bytes
13265 whereas may be better to output a negative integer: thanks to integer
13266 wrapping, we know that:
13267 x = x - 2 ** DWARF2_ADDR_SIZE
13268 = x - 2 * (max (HOST_WIDE_INT) + 1)
13269 So numbers close to max (unsigned HOST_WIDE_INT) could be represented as
13270 small negative integers. Let's try that in cases it will clearly improve
13271 the encoding: there is no gain turning DW_OP_const4u into
13272 DW_OP_const4s. */
13273 if (DWARF2_ADDR_SIZE * 8 == HOST_BITS_PER_WIDE_INT
13274 && ((DWARF2_ADDR_SIZE == 4 && i > max_uint - 0x8000)
13275 || (DWARF2_ADDR_SIZE == 8 && i > max_uint - 0x80000000)))
13277 const unsigned HOST_WIDE_INT first_shift = i - max_int - 1;
13279 /* Now, -1 < first_shift <= max (HOST_WIDE_INT)
13280 i.e. 0 <= first_shift <= max (HOST_WIDE_INT). */
13281 const HOST_WIDE_INT second_shift
13282 = (HOST_WIDE_INT) first_shift - (HOST_WIDE_INT) max_int - 1;
13284 /* So we finally have:
13285 -max (HOST_WIDE_INT) - 1 <= second_shift <= -1.
13286 i.e. min (HOST_WIDE_INT) <= second_shift < 0. */
13287 return int_loc_descriptor (second_shift);
13290 /* Last chance: fallback to a simple constant operation. */
13291 return new_loc_descr
13292 ((HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
13293 ? DW_OP_const4u
13294 : DW_OP_const8u,
13295 i, 0);
13298 /* Generate and return a location description that computes the unsigned
13299 comparison of the two stack top entries (a OP b where b is the top-most
13300 entry and a is the second one). The KIND of comparison can be LT_EXPR,
13301 LE_EXPR, GT_EXPR or GE_EXPR. */
13303 static dw_loc_descr_ref
13304 uint_comparison_loc_list (enum tree_code kind)
13306 enum dwarf_location_atom op, flip_op;
13307 dw_loc_descr_ref ret, bra_node, jmp_node, tmp;
13309 switch (kind)
13311 case LT_EXPR:
13312 op = DW_OP_lt;
13313 break;
13314 case LE_EXPR:
13315 op = DW_OP_le;
13316 break;
13317 case GT_EXPR:
13318 op = DW_OP_gt;
13319 break;
13320 case GE_EXPR:
13321 op = DW_OP_ge;
13322 break;
13323 default:
13324 gcc_unreachable ();
13327 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
13328 jmp_node = new_loc_descr (DW_OP_skip, 0, 0);
13330 /* Until DWARFv4, operations all work on signed integers. It is nevertheless
13331 possible to perform unsigned comparisons: we just have to distinguish
13332 three cases:
13334 1. when a and b have the same sign (as signed integers); then we should
13335 return: a OP(signed) b;
13337 2. when a is a negative signed integer while b is a positive one, then a
13338 is a greater unsigned integer than b; likewise when a and b's roles
13339 are flipped.
13341 So first, compare the sign of the two operands. */
13342 ret = new_loc_descr (DW_OP_over, 0, 0);
13343 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
13344 add_loc_descr (&ret, new_loc_descr (DW_OP_xor, 0, 0));
13345 /* If they have different signs (i.e. they have different sign bits), then
13346 the stack top value has now the sign bit set and thus it's smaller than
13347 zero. */
13348 add_loc_descr (&ret, new_loc_descr (DW_OP_lit0, 0, 0));
13349 add_loc_descr (&ret, new_loc_descr (DW_OP_lt, 0, 0));
13350 add_loc_descr (&ret, bra_node);
13352 /* We are in case 1. At this point, we know both operands have the same
13353 sign, to it's safe to use the built-in signed comparison. */
13354 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
13355 add_loc_descr (&ret, jmp_node);
13357 /* We are in case 2. Here, we know both operands do not have the same sign,
13358 so we have to flip the signed comparison. */
13359 flip_op = (kind == LT_EXPR || kind == LE_EXPR) ? DW_OP_gt : DW_OP_lt;
13360 tmp = new_loc_descr (flip_op, 0, 0);
13361 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13362 bra_node->dw_loc_oprnd1.v.val_loc = tmp;
13363 add_loc_descr (&ret, tmp);
13365 /* This dummy operation is necessary to make the two branches join. */
13366 tmp = new_loc_descr (DW_OP_nop, 0, 0);
13367 jmp_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13368 jmp_node->dw_loc_oprnd1.v.val_loc = tmp;
13369 add_loc_descr (&ret, tmp);
13371 return ret;
13374 /* Likewise, but takes the location description lists (might be destructive on
13375 them). Return NULL if either is NULL or if concatenation fails. */
13377 static dw_loc_list_ref
13378 loc_list_from_uint_comparison (dw_loc_list_ref left, dw_loc_list_ref right,
13379 enum tree_code kind)
13381 if (left == NULL || right == NULL)
13382 return NULL;
13384 add_loc_list (&left, right);
13385 if (left == NULL)
13386 return NULL;
13388 add_loc_descr_to_each (left, uint_comparison_loc_list (kind));
13389 return left;
13392 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
13393 without actually allocating it. */
13395 static unsigned long
13396 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
13398 return size_of_int_loc_descriptor (i >> shift)
13399 + size_of_int_loc_descriptor (shift)
13400 + 1;
13403 /* Return size_of_locs (int_loc_descriptor (i)) without
13404 actually allocating it. */
13406 static unsigned long
13407 size_of_int_loc_descriptor (HOST_WIDE_INT i)
13409 unsigned long s;
13411 if (i >= 0)
13413 int clz, ctz;
13414 if (i <= 31)
13415 return 1;
13416 else if (i <= 0xff)
13417 return 2;
13418 else if (i <= 0xffff)
13419 return 3;
13420 clz = clz_hwi (i);
13421 ctz = ctz_hwi (i);
13422 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
13423 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
13424 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13425 - clz - 5);
13426 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13427 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
13428 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13429 - clz - 8);
13430 else if (DWARF2_ADDR_SIZE == 4 && i > 0x7fffffff
13431 && size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i)
13432 <= 4)
13433 return size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i);
13434 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
13435 return 5;
13436 s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
13437 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13438 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
13439 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13440 - clz - 8);
13441 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
13442 && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
13443 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13444 - clz - 16);
13445 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
13446 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
13447 && s > 6)
13448 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13449 - clz - 32);
13450 else
13451 return 1 + s;
13453 else
13455 if (i >= -0x80)
13456 return 2;
13457 else if (i >= -0x8000)
13458 return 3;
13459 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
13461 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
13463 s = size_of_int_loc_descriptor (-i) + 1;
13464 if (s < 5)
13465 return s;
13467 return 5;
13469 else
13471 unsigned long r = 1 + size_of_sleb128 (i);
13472 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
13474 s = size_of_int_loc_descriptor (-i) + 1;
13475 if (s < r)
13476 return s;
13478 return r;
13483 /* Return loc description representing "address" of integer value.
13484 This can appear only as toplevel expression. */
13486 static dw_loc_descr_ref
13487 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
13489 int litsize;
13490 dw_loc_descr_ref loc_result = NULL;
13492 if (!(dwarf_version >= 4 || !dwarf_strict))
13493 return NULL;
13495 litsize = size_of_int_loc_descriptor (i);
13496 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
13497 is more compact. For DW_OP_stack_value we need:
13498 litsize + 1 (DW_OP_stack_value)
13499 and for DW_OP_implicit_value:
13500 1 (DW_OP_implicit_value) + 1 (length) + size. */
13501 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
13503 loc_result = int_loc_descriptor (i);
13504 add_loc_descr (&loc_result,
13505 new_loc_descr (DW_OP_stack_value, 0, 0));
13506 return loc_result;
13509 loc_result = new_loc_descr (DW_OP_implicit_value,
13510 size, 0);
13511 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
13512 loc_result->dw_loc_oprnd2.v.val_int = i;
13513 return loc_result;
13516 /* Return a location descriptor that designates a base+offset location. */
13518 static dw_loc_descr_ref
13519 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
13520 enum var_init_status initialized)
13522 unsigned int regno;
13523 dw_loc_descr_ref result;
13524 dw_fde_ref fde = cfun->fde;
13526 /* We only use "frame base" when we're sure we're talking about the
13527 post-prologue local stack frame. We do this by *not* running
13528 register elimination until this point, and recognizing the special
13529 argument pointer and soft frame pointer rtx's. */
13530 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
13532 rtx elim = (ira_use_lra_p
13533 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
13534 : eliminate_regs (reg, VOIDmode, NULL_RTX));
13536 if (elim != reg)
13538 if (GET_CODE (elim) == PLUS)
13540 offset += INTVAL (XEXP (elim, 1));
13541 elim = XEXP (elim, 0);
13543 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
13544 && (elim == hard_frame_pointer_rtx
13545 || elim == stack_pointer_rtx))
13546 || elim == (frame_pointer_needed
13547 ? hard_frame_pointer_rtx
13548 : stack_pointer_rtx));
13550 /* If drap register is used to align stack, use frame
13551 pointer + offset to access stack variables. If stack
13552 is aligned without drap, use stack pointer + offset to
13553 access stack variables. */
13554 if (crtl->stack_realign_tried
13555 && reg == frame_pointer_rtx)
13557 int base_reg
13558 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
13559 ? HARD_FRAME_POINTER_REGNUM
13560 : REGNO (elim));
13561 return new_reg_loc_descr (base_reg, offset);
13564 gcc_assert (frame_pointer_fb_offset_valid);
13565 offset += frame_pointer_fb_offset;
13566 return new_loc_descr (DW_OP_fbreg, offset, 0);
13570 regno = REGNO (reg);
13571 #ifdef LEAF_REG_REMAP
13572 if (crtl->uses_only_leaf_regs)
13574 int leaf_reg = LEAF_REG_REMAP (regno);
13575 if (leaf_reg != -1)
13576 regno = (unsigned) leaf_reg;
13578 #endif
13579 regno = DWARF_FRAME_REGNUM (regno);
13581 if (!optimize && fde
13582 && (fde->drap_reg == regno || fde->vdrap_reg == regno))
13584 /* Use cfa+offset to represent the location of arguments passed
13585 on the stack when drap is used to align stack.
13586 Only do this when not optimizing, for optimized code var-tracking
13587 is supposed to track where the arguments live and the register
13588 used as vdrap or drap in some spot might be used for something
13589 else in other part of the routine. */
13590 return new_loc_descr (DW_OP_fbreg, offset, 0);
13593 if (regno <= 31)
13594 result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
13595 offset, 0);
13596 else
13597 result = new_loc_descr (DW_OP_bregx, regno, offset);
13599 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13600 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13602 return result;
13605 /* Return true if this RTL expression describes a base+offset calculation. */
13607 static inline int
13608 is_based_loc (const_rtx rtl)
13610 return (GET_CODE (rtl) == PLUS
13611 && ((REG_P (XEXP (rtl, 0))
13612 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
13613 && CONST_INT_P (XEXP (rtl, 1)))));
13616 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
13617 failed. */
13619 static dw_loc_descr_ref
13620 tls_mem_loc_descriptor (rtx mem)
13622 tree base;
13623 dw_loc_descr_ref loc_result;
13625 if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
13626 return NULL;
13628 base = get_base_address (MEM_EXPR (mem));
13629 if (base == NULL
13630 || !VAR_P (base)
13631 || !DECL_THREAD_LOCAL_P (base))
13632 return NULL;
13634 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1, NULL);
13635 if (loc_result == NULL)
13636 return NULL;
13638 if (MEM_OFFSET (mem))
13639 loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
13641 return loc_result;
13644 /* Output debug info about reason why we failed to expand expression as dwarf
13645 expression. */
13647 static void
13648 expansion_failed (tree expr, rtx rtl, char const *reason)
13650 if (dump_file && (dump_flags & TDF_DETAILS))
13652 fprintf (dump_file, "Failed to expand as dwarf: ");
13653 if (expr)
13654 print_generic_expr (dump_file, expr, dump_flags);
13655 if (rtl)
13657 fprintf (dump_file, "\n");
13658 print_rtl (dump_file, rtl);
13660 fprintf (dump_file, "\nReason: %s\n", reason);
13664 /* Helper function for const_ok_for_output. */
13666 static bool
13667 const_ok_for_output_1 (rtx rtl)
13669 if (GET_CODE (rtl) == UNSPEC)
13671 /* If delegitimize_address couldn't do anything with the UNSPEC, assume
13672 we can't express it in the debug info. */
13673 /* Don't complain about TLS UNSPECs, those are just too hard to
13674 delegitimize. Note this could be a non-decl SYMBOL_REF such as
13675 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
13676 rather than DECL_THREAD_LOCAL_P is not just an optimization. */
13677 if (flag_checking
13678 && (XVECLEN (rtl, 0) == 0
13679 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
13680 || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE))
13681 inform (current_function_decl
13682 ? DECL_SOURCE_LOCATION (current_function_decl)
13683 : UNKNOWN_LOCATION,
13684 #if NUM_UNSPEC_VALUES > 0
13685 "non-delegitimized UNSPEC %s (%d) found in variable location",
13686 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
13687 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
13688 XINT (rtl, 1));
13689 #else
13690 "non-delegitimized UNSPEC %d found in variable location",
13691 XINT (rtl, 1));
13692 #endif
13693 expansion_failed (NULL_TREE, rtl,
13694 "UNSPEC hasn't been delegitimized.\n");
13695 return false;
13698 if (targetm.const_not_ok_for_debug_p (rtl))
13700 expansion_failed (NULL_TREE, rtl,
13701 "Expression rejected for debug by the backend.\n");
13702 return false;
13705 /* FIXME: Refer to PR60655. It is possible for simplification
13706 of rtl expressions in var tracking to produce such expressions.
13707 We should really identify / validate expressions
13708 enclosed in CONST that can be handled by assemblers on various
13709 targets and only handle legitimate cases here. */
13710 if (GET_CODE (rtl) != SYMBOL_REF)
13712 if (GET_CODE (rtl) == NOT)
13713 return false;
13714 return true;
13717 if (CONSTANT_POOL_ADDRESS_P (rtl))
13719 bool marked;
13720 get_pool_constant_mark (rtl, &marked);
13721 /* If all references to this pool constant were optimized away,
13722 it was not output and thus we can't represent it. */
13723 if (!marked)
13725 expansion_failed (NULL_TREE, rtl,
13726 "Constant was removed from constant pool.\n");
13727 return false;
13731 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
13732 return false;
13734 /* Avoid references to external symbols in debug info, on several targets
13735 the linker might even refuse to link when linking a shared library,
13736 and in many other cases the relocations for .debug_info/.debug_loc are
13737 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
13738 to be defined within the same shared library or executable are fine. */
13739 if (SYMBOL_REF_EXTERNAL_P (rtl))
13741 tree decl = SYMBOL_REF_DECL (rtl);
13743 if (decl == NULL || !targetm.binds_local_p (decl))
13745 expansion_failed (NULL_TREE, rtl,
13746 "Symbol not defined in current TU.\n");
13747 return false;
13751 return true;
13754 /* Return true if constant RTL can be emitted in DW_OP_addr or
13755 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
13756 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
13758 static bool
13759 const_ok_for_output (rtx rtl)
13761 if (GET_CODE (rtl) == SYMBOL_REF)
13762 return const_ok_for_output_1 (rtl);
13764 if (GET_CODE (rtl) == CONST)
13766 subrtx_var_iterator::array_type array;
13767 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 0), ALL)
13768 if (!const_ok_for_output_1 (*iter))
13769 return false;
13770 return true;
13773 return true;
13776 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
13777 if possible, NULL otherwise. */
13779 static dw_die_ref
13780 base_type_for_mode (machine_mode mode, bool unsignedp)
13782 dw_die_ref type_die;
13783 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
13785 if (type == NULL)
13786 return NULL;
13787 switch (TREE_CODE (type))
13789 case INTEGER_TYPE:
13790 case REAL_TYPE:
13791 break;
13792 default:
13793 return NULL;
13795 type_die = lookup_type_die (type);
13796 if (!type_die)
13797 type_die = modified_type_die (type, TYPE_UNQUALIFIED, false,
13798 comp_unit_die ());
13799 if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
13800 return NULL;
13801 return type_die;
13804 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
13805 type matching MODE, or, if MODE is narrower than or as wide as
13806 DWARF2_ADDR_SIZE, untyped. Return NULL if the conversion is not
13807 possible. */
13809 static dw_loc_descr_ref
13810 convert_descriptor_to_mode (machine_mode mode, dw_loc_descr_ref op)
13812 machine_mode outer_mode = mode;
13813 dw_die_ref type_die;
13814 dw_loc_descr_ref cvt;
13816 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
13818 add_loc_descr (&op, new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0));
13819 return op;
13821 type_die = base_type_for_mode (outer_mode, 1);
13822 if (type_die == NULL)
13823 return NULL;
13824 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
13825 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13826 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13827 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13828 add_loc_descr (&op, cvt);
13829 return op;
13832 /* Return location descriptor for comparison OP with operands OP0 and OP1. */
13834 static dw_loc_descr_ref
13835 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
13836 dw_loc_descr_ref op1)
13838 dw_loc_descr_ref ret = op0;
13839 add_loc_descr (&ret, op1);
13840 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
13841 if (STORE_FLAG_VALUE != 1)
13843 add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
13844 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
13846 return ret;
13849 /* Return location descriptor for signed comparison OP RTL. */
13851 static dw_loc_descr_ref
13852 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
13853 machine_mode mem_mode)
13855 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
13856 dw_loc_descr_ref op0, op1;
13857 int shift;
13859 if (op_mode == VOIDmode)
13860 op_mode = GET_MODE (XEXP (rtl, 1));
13861 if (op_mode == VOIDmode)
13862 return NULL;
13864 if (dwarf_strict
13865 && dwarf_version < 5
13866 && (!SCALAR_INT_MODE_P (op_mode)
13867 || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
13868 return NULL;
13870 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
13871 VAR_INIT_STATUS_INITIALIZED);
13872 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
13873 VAR_INIT_STATUS_INITIALIZED);
13875 if (op0 == NULL || op1 == NULL)
13876 return NULL;
13878 if (!SCALAR_INT_MODE_P (op_mode)
13879 || GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
13880 return compare_loc_descriptor (op, op0, op1);
13882 if (GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
13884 dw_die_ref type_die = base_type_for_mode (op_mode, 0);
13885 dw_loc_descr_ref cvt;
13887 if (type_die == NULL)
13888 return NULL;
13889 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
13890 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13891 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13892 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13893 add_loc_descr (&op0, cvt);
13894 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
13895 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13896 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13897 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13898 add_loc_descr (&op1, cvt);
13899 return compare_loc_descriptor (op, op0, op1);
13902 shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
13903 /* For eq/ne, if the operands are known to be zero-extended,
13904 there is no need to do the fancy shifting up. */
13905 if (op == DW_OP_eq || op == DW_OP_ne)
13907 dw_loc_descr_ref last0, last1;
13908 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
13910 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
13912 /* deref_size zero extends, and for constants we can check
13913 whether they are zero extended or not. */
13914 if (((last0->dw_loc_opc == DW_OP_deref_size
13915 && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
13916 || (CONST_INT_P (XEXP (rtl, 0))
13917 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
13918 == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
13919 && ((last1->dw_loc_opc == DW_OP_deref_size
13920 && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
13921 || (CONST_INT_P (XEXP (rtl, 1))
13922 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
13923 == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
13924 return compare_loc_descriptor (op, op0, op1);
13926 /* EQ/NE comparison against constant in narrower type than
13927 DWARF2_ADDR_SIZE can be performed either as
13928 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
13929 DW_OP_{eq,ne}
13931 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
13932 DW_OP_{eq,ne}. Pick whatever is shorter. */
13933 if (CONST_INT_P (XEXP (rtl, 1))
13934 && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
13935 && (size_of_int_loc_descriptor (shift) + 1
13936 + size_of_int_loc_descriptor (UINTVAL (XEXP (rtl, 1)) << shift)
13937 >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
13938 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
13939 & GET_MODE_MASK (op_mode))))
13941 add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
13942 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
13943 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
13944 & GET_MODE_MASK (op_mode));
13945 return compare_loc_descriptor (op, op0, op1);
13948 add_loc_descr (&op0, int_loc_descriptor (shift));
13949 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
13950 if (CONST_INT_P (XEXP (rtl, 1)))
13951 op1 = int_loc_descriptor (UINTVAL (XEXP (rtl, 1)) << shift);
13952 else
13954 add_loc_descr (&op1, int_loc_descriptor (shift));
13955 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
13957 return compare_loc_descriptor (op, op0, op1);
13960 /* Return location descriptor for unsigned comparison OP RTL. */
13962 static dw_loc_descr_ref
13963 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
13964 machine_mode mem_mode)
13966 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
13967 dw_loc_descr_ref op0, op1;
13969 if (op_mode == VOIDmode)
13970 op_mode = GET_MODE (XEXP (rtl, 1));
13971 if (op_mode == VOIDmode)
13972 return NULL;
13973 if (!SCALAR_INT_MODE_P (op_mode))
13974 return NULL;
13976 if (dwarf_strict
13977 && dwarf_version < 5
13978 && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
13979 return NULL;
13981 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
13982 VAR_INIT_STATUS_INITIALIZED);
13983 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
13984 VAR_INIT_STATUS_INITIALIZED);
13986 if (op0 == NULL || op1 == NULL)
13987 return NULL;
13989 if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
13991 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
13992 dw_loc_descr_ref last0, last1;
13993 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
13995 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
13997 if (CONST_INT_P (XEXP (rtl, 0)))
13998 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
13999 /* deref_size zero extends, so no need to mask it again. */
14000 else if (last0->dw_loc_opc != DW_OP_deref_size
14001 || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
14003 add_loc_descr (&op0, int_loc_descriptor (mask));
14004 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14006 if (CONST_INT_P (XEXP (rtl, 1)))
14007 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
14008 /* deref_size zero extends, so no need to mask it again. */
14009 else if (last1->dw_loc_opc != DW_OP_deref_size
14010 || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
14012 add_loc_descr (&op1, int_loc_descriptor (mask));
14013 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
14016 else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
14018 HOST_WIDE_INT bias = 1;
14019 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
14020 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14021 if (CONST_INT_P (XEXP (rtl, 1)))
14022 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
14023 + INTVAL (XEXP (rtl, 1)));
14024 else
14025 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
14026 bias, 0));
14028 return compare_loc_descriptor (op, op0, op1);
14031 /* Return location descriptor for {U,S}{MIN,MAX}. */
14033 static dw_loc_descr_ref
14034 minmax_loc_descriptor (rtx rtl, machine_mode mode,
14035 machine_mode mem_mode)
14037 enum dwarf_location_atom op;
14038 dw_loc_descr_ref op0, op1, ret;
14039 dw_loc_descr_ref bra_node, drop_node;
14041 if (dwarf_strict
14042 && dwarf_version < 5
14043 && (!SCALAR_INT_MODE_P (mode)
14044 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
14045 return NULL;
14047 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14048 VAR_INIT_STATUS_INITIALIZED);
14049 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14050 VAR_INIT_STATUS_INITIALIZED);
14052 if (op0 == NULL || op1 == NULL)
14053 return NULL;
14055 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
14056 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
14057 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
14058 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
14060 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
14062 HOST_WIDE_INT mask = GET_MODE_MASK (mode);
14063 add_loc_descr (&op0, int_loc_descriptor (mask));
14064 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14065 add_loc_descr (&op1, int_loc_descriptor (mask));
14066 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
14068 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
14070 HOST_WIDE_INT bias = 1;
14071 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
14072 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14073 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14076 else if (!SCALAR_INT_MODE_P (mode)
14077 && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
14079 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode)) * BITS_PER_UNIT;
14080 add_loc_descr (&op0, int_loc_descriptor (shift));
14081 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
14082 add_loc_descr (&op1, int_loc_descriptor (shift));
14083 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
14085 else if (SCALAR_INT_MODE_P (mode)
14086 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
14088 dw_die_ref type_die = base_type_for_mode (mode, 0);
14089 dw_loc_descr_ref cvt;
14090 if (type_die == NULL)
14091 return NULL;
14092 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14093 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14094 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14095 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14096 add_loc_descr (&op0, cvt);
14097 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14098 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14099 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14100 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14101 add_loc_descr (&op1, cvt);
14104 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
14105 op = DW_OP_lt;
14106 else
14107 op = DW_OP_gt;
14108 ret = op0;
14109 add_loc_descr (&ret, op1);
14110 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
14111 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14112 add_loc_descr (&ret, bra_node);
14113 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14114 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
14115 add_loc_descr (&ret, drop_node);
14116 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14117 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
14118 if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
14119 && SCALAR_INT_MODE_P (mode)
14120 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
14121 ret = convert_descriptor_to_mode (mode, ret);
14122 return ret;
14125 /* Helper function for mem_loc_descriptor. Perform OP binary op,
14126 but after converting arguments to type_die, afterwards
14127 convert back to unsigned. */
14129 static dw_loc_descr_ref
14130 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
14131 machine_mode mode, machine_mode mem_mode)
14133 dw_loc_descr_ref cvt, op0, op1;
14135 if (type_die == NULL)
14136 return NULL;
14137 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14138 VAR_INIT_STATUS_INITIALIZED);
14139 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14140 VAR_INIT_STATUS_INITIALIZED);
14141 if (op0 == NULL || op1 == NULL)
14142 return NULL;
14143 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14144 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14145 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14146 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14147 add_loc_descr (&op0, cvt);
14148 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14149 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14150 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14151 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14152 add_loc_descr (&op1, cvt);
14153 add_loc_descr (&op0, op1);
14154 add_loc_descr (&op0, new_loc_descr (op, 0, 0));
14155 return convert_descriptor_to_mode (mode, op0);
14158 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
14159 const0 is DW_OP_lit0 or corresponding typed constant,
14160 const1 is DW_OP_lit1 or corresponding typed constant
14161 and constMSB is constant with just the MSB bit set
14162 for the mode):
14163 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
14164 L1: const0 DW_OP_swap
14165 L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
14166 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
14167 L3: DW_OP_drop
14168 L4: DW_OP_nop
14170 CTZ is similar:
14171 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
14172 L1: const0 DW_OP_swap
14173 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
14174 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
14175 L3: DW_OP_drop
14176 L4: DW_OP_nop
14178 FFS is similar:
14179 DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
14180 L1: const1 DW_OP_swap
14181 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
14182 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
14183 L3: DW_OP_drop
14184 L4: DW_OP_nop */
14186 static dw_loc_descr_ref
14187 clz_loc_descriptor (rtx rtl, machine_mode mode,
14188 machine_mode mem_mode)
14190 dw_loc_descr_ref op0, ret, tmp;
14191 HOST_WIDE_INT valv;
14192 dw_loc_descr_ref l1jump, l1label;
14193 dw_loc_descr_ref l2jump, l2label;
14194 dw_loc_descr_ref l3jump, l3label;
14195 dw_loc_descr_ref l4jump, l4label;
14196 rtx msb;
14198 if (!SCALAR_INT_MODE_P (mode)
14199 || GET_MODE (XEXP (rtl, 0)) != mode)
14200 return NULL;
14202 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14203 VAR_INIT_STATUS_INITIALIZED);
14204 if (op0 == NULL)
14205 return NULL;
14206 ret = op0;
14207 if (GET_CODE (rtl) == CLZ)
14209 if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
14210 valv = GET_MODE_BITSIZE (mode);
14212 else if (GET_CODE (rtl) == FFS)
14213 valv = 0;
14214 else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
14215 valv = GET_MODE_BITSIZE (mode);
14216 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
14217 l1jump = new_loc_descr (DW_OP_bra, 0, 0);
14218 add_loc_descr (&ret, l1jump);
14219 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
14220 tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
14221 VAR_INIT_STATUS_INITIALIZED);
14222 if (tmp == NULL)
14223 return NULL;
14224 add_loc_descr (&ret, tmp);
14225 l4jump = new_loc_descr (DW_OP_skip, 0, 0);
14226 add_loc_descr (&ret, l4jump);
14227 l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
14228 ? const1_rtx : const0_rtx,
14229 mode, mem_mode,
14230 VAR_INIT_STATUS_INITIALIZED);
14231 if (l1label == NULL)
14232 return NULL;
14233 add_loc_descr (&ret, l1label);
14234 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14235 l2label = new_loc_descr (DW_OP_dup, 0, 0);
14236 add_loc_descr (&ret, l2label);
14237 if (GET_CODE (rtl) != CLZ)
14238 msb = const1_rtx;
14239 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
14240 msb = GEN_INT (HOST_WIDE_INT_1U
14241 << (GET_MODE_BITSIZE (mode) - 1));
14242 else
14243 msb = immed_wide_int_const
14244 (wi::set_bit_in_zero (GET_MODE_PRECISION (mode) - 1,
14245 GET_MODE_PRECISION (mode)), mode);
14246 if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
14247 tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
14248 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
14249 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
14250 else
14251 tmp = mem_loc_descriptor (msb, mode, mem_mode,
14252 VAR_INIT_STATUS_INITIALIZED);
14253 if (tmp == NULL)
14254 return NULL;
14255 add_loc_descr (&ret, tmp);
14256 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
14257 l3jump = new_loc_descr (DW_OP_bra, 0, 0);
14258 add_loc_descr (&ret, l3jump);
14259 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
14260 VAR_INIT_STATUS_INITIALIZED);
14261 if (tmp == NULL)
14262 return NULL;
14263 add_loc_descr (&ret, tmp);
14264 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
14265 ? DW_OP_shl : DW_OP_shr, 0, 0));
14266 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14267 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
14268 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14269 l2jump = new_loc_descr (DW_OP_skip, 0, 0);
14270 add_loc_descr (&ret, l2jump);
14271 l3label = new_loc_descr (DW_OP_drop, 0, 0);
14272 add_loc_descr (&ret, l3label);
14273 l4label = new_loc_descr (DW_OP_nop, 0, 0);
14274 add_loc_descr (&ret, l4label);
14275 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14276 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
14277 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14278 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
14279 l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14280 l3jump->dw_loc_oprnd1.v.val_loc = l3label;
14281 l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14282 l4jump->dw_loc_oprnd1.v.val_loc = l4label;
14283 return ret;
14286 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
14287 const1 is DW_OP_lit1 or corresponding typed constant):
14288 const0 DW_OP_swap
14289 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
14290 DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
14291 L2: DW_OP_drop
14293 PARITY is similar:
14294 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
14295 DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
14296 L2: DW_OP_drop */
14298 static dw_loc_descr_ref
14299 popcount_loc_descriptor (rtx rtl, machine_mode mode,
14300 machine_mode mem_mode)
14302 dw_loc_descr_ref op0, ret, tmp;
14303 dw_loc_descr_ref l1jump, l1label;
14304 dw_loc_descr_ref l2jump, l2label;
14306 if (!SCALAR_INT_MODE_P (mode)
14307 || GET_MODE (XEXP (rtl, 0)) != mode)
14308 return NULL;
14310 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14311 VAR_INIT_STATUS_INITIALIZED);
14312 if (op0 == NULL)
14313 return NULL;
14314 ret = op0;
14315 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
14316 VAR_INIT_STATUS_INITIALIZED);
14317 if (tmp == NULL)
14318 return NULL;
14319 add_loc_descr (&ret, tmp);
14320 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14321 l1label = new_loc_descr (DW_OP_dup, 0, 0);
14322 add_loc_descr (&ret, l1label);
14323 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
14324 add_loc_descr (&ret, l2jump);
14325 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
14326 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
14327 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
14328 VAR_INIT_STATUS_INITIALIZED);
14329 if (tmp == NULL)
14330 return NULL;
14331 add_loc_descr (&ret, tmp);
14332 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
14333 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
14334 ? DW_OP_plus : DW_OP_xor, 0, 0));
14335 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14336 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
14337 VAR_INIT_STATUS_INITIALIZED);
14338 add_loc_descr (&ret, tmp);
14339 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
14340 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
14341 add_loc_descr (&ret, l1jump);
14342 l2label = new_loc_descr (DW_OP_drop, 0, 0);
14343 add_loc_descr (&ret, l2label);
14344 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14345 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
14346 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14347 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
14348 return ret;
14351 /* BSWAP (constS is initial shift count, either 56 or 24):
14352 constS const0
14353 L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
14354 const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
14355 DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
14356 DW_OP_minus DW_OP_swap DW_OP_skip <L1>
14357 L2: DW_OP_drop DW_OP_swap DW_OP_drop */
14359 static dw_loc_descr_ref
14360 bswap_loc_descriptor (rtx rtl, machine_mode mode,
14361 machine_mode mem_mode)
14363 dw_loc_descr_ref op0, ret, tmp;
14364 dw_loc_descr_ref l1jump, l1label;
14365 dw_loc_descr_ref l2jump, l2label;
14367 if (!SCALAR_INT_MODE_P (mode)
14368 || BITS_PER_UNIT != 8
14369 || (GET_MODE_BITSIZE (mode) != 32
14370 && GET_MODE_BITSIZE (mode) != 64))
14371 return NULL;
14373 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14374 VAR_INIT_STATUS_INITIALIZED);
14375 if (op0 == NULL)
14376 return NULL;
14378 ret = op0;
14379 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
14380 mode, mem_mode,
14381 VAR_INIT_STATUS_INITIALIZED);
14382 if (tmp == NULL)
14383 return NULL;
14384 add_loc_descr (&ret, tmp);
14385 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
14386 VAR_INIT_STATUS_INITIALIZED);
14387 if (tmp == NULL)
14388 return NULL;
14389 add_loc_descr (&ret, tmp);
14390 l1label = new_loc_descr (DW_OP_pick, 2, 0);
14391 add_loc_descr (&ret, l1label);
14392 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
14393 mode, mem_mode,
14394 VAR_INIT_STATUS_INITIALIZED);
14395 add_loc_descr (&ret, tmp);
14396 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
14397 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
14398 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
14399 tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
14400 VAR_INIT_STATUS_INITIALIZED);
14401 if (tmp == NULL)
14402 return NULL;
14403 add_loc_descr (&ret, tmp);
14404 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
14405 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
14406 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
14407 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
14408 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14409 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
14410 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
14411 VAR_INIT_STATUS_INITIALIZED);
14412 add_loc_descr (&ret, tmp);
14413 add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
14414 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
14415 add_loc_descr (&ret, l2jump);
14416 tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
14417 VAR_INIT_STATUS_INITIALIZED);
14418 add_loc_descr (&ret, tmp);
14419 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
14420 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14421 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
14422 add_loc_descr (&ret, l1jump);
14423 l2label = new_loc_descr (DW_OP_drop, 0, 0);
14424 add_loc_descr (&ret, l2label);
14425 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14426 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
14427 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14428 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
14429 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14430 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
14431 return ret;
14434 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
14435 DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
14436 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
14437 DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
14439 ROTATERT is similar:
14440 DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
14441 DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
14442 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or */
14444 static dw_loc_descr_ref
14445 rotate_loc_descriptor (rtx rtl, machine_mode mode,
14446 machine_mode mem_mode)
14448 rtx rtlop1 = XEXP (rtl, 1);
14449 dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
14450 int i;
14452 if (!SCALAR_INT_MODE_P (mode))
14453 return NULL;
14455 if (GET_MODE (rtlop1) != VOIDmode
14456 && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
14457 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
14458 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14459 VAR_INIT_STATUS_INITIALIZED);
14460 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
14461 VAR_INIT_STATUS_INITIALIZED);
14462 if (op0 == NULL || op1 == NULL)
14463 return NULL;
14464 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
14465 for (i = 0; i < 2; i++)
14467 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
14468 mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
14469 mode, mem_mode,
14470 VAR_INIT_STATUS_INITIALIZED);
14471 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
14472 mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
14473 ? DW_OP_const4u
14474 : HOST_BITS_PER_WIDE_INT == 64
14475 ? DW_OP_const8u : DW_OP_constu,
14476 GET_MODE_MASK (mode), 0);
14477 else
14478 mask[i] = NULL;
14479 if (mask[i] == NULL)
14480 return NULL;
14481 add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
14483 ret = op0;
14484 add_loc_descr (&ret, op1);
14485 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
14486 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
14487 if (GET_CODE (rtl) == ROTATERT)
14489 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
14490 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
14491 GET_MODE_BITSIZE (mode), 0));
14493 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
14494 if (mask[0] != NULL)
14495 add_loc_descr (&ret, mask[0]);
14496 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
14497 if (mask[1] != NULL)
14499 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14500 add_loc_descr (&ret, mask[1]);
14501 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14503 if (GET_CODE (rtl) == ROTATE)
14505 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
14506 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
14507 GET_MODE_BITSIZE (mode), 0));
14509 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
14510 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
14511 return ret;
14514 /* Helper function for mem_loc_descriptor. Return DW_OP_GNU_parameter_ref
14515 for DEBUG_PARAMETER_REF RTL. */
14517 static dw_loc_descr_ref
14518 parameter_ref_descriptor (rtx rtl)
14520 dw_loc_descr_ref ret;
14521 dw_die_ref ref;
14523 if (dwarf_strict)
14524 return NULL;
14525 gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
14526 ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
14527 ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
14528 if (ref)
14530 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14531 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
14532 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
14534 else
14536 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
14537 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
14539 return ret;
14542 /* The following routine converts the RTL for a variable or parameter
14543 (resident in memory) into an equivalent Dwarf representation of a
14544 mechanism for getting the address of that same variable onto the top of a
14545 hypothetical "address evaluation" stack.
14547 When creating memory location descriptors, we are effectively transforming
14548 the RTL for a memory-resident object into its Dwarf postfix expression
14549 equivalent. This routine recursively descends an RTL tree, turning
14550 it into Dwarf postfix code as it goes.
14552 MODE is the mode that should be assumed for the rtl if it is VOIDmode.
14554 MEM_MODE is the mode of the memory reference, needed to handle some
14555 autoincrement addressing modes.
14557 Return 0 if we can't represent the location. */
14559 dw_loc_descr_ref
14560 mem_loc_descriptor (rtx rtl, machine_mode mode,
14561 machine_mode mem_mode,
14562 enum var_init_status initialized)
14564 dw_loc_descr_ref mem_loc_result = NULL;
14565 enum dwarf_location_atom op;
14566 dw_loc_descr_ref op0, op1;
14567 rtx inner = NULL_RTX;
14569 if (mode == VOIDmode)
14570 mode = GET_MODE (rtl);
14572 /* Note that for a dynamically sized array, the location we will generate a
14573 description of here will be the lowest numbered location which is
14574 actually within the array. That's *not* necessarily the same as the
14575 zeroth element of the array. */
14577 rtl = targetm.delegitimize_address (rtl);
14579 if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
14580 return NULL;
14582 switch (GET_CODE (rtl))
14584 case POST_INC:
14585 case POST_DEC:
14586 case POST_MODIFY:
14587 return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
14589 case SUBREG:
14590 /* The case of a subreg may arise when we have a local (register)
14591 variable or a formal (register) parameter which doesn't quite fill
14592 up an entire register. For now, just assume that it is
14593 legitimate to make the Dwarf info refer to the whole register which
14594 contains the given subreg. */
14595 if (!subreg_lowpart_p (rtl))
14596 break;
14597 inner = SUBREG_REG (rtl);
14598 /* FALLTHRU */
14599 case TRUNCATE:
14600 if (inner == NULL_RTX)
14601 inner = XEXP (rtl, 0);
14602 if (SCALAR_INT_MODE_P (mode)
14603 && SCALAR_INT_MODE_P (GET_MODE (inner))
14604 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
14605 #ifdef POINTERS_EXTEND_UNSIGNED
14606 || (mode == Pmode && mem_mode != VOIDmode)
14607 #endif
14609 && GET_MODE_SIZE (GET_MODE (inner)) <= DWARF2_ADDR_SIZE)
14611 mem_loc_result = mem_loc_descriptor (inner,
14612 GET_MODE (inner),
14613 mem_mode, initialized);
14614 break;
14616 if (dwarf_strict && dwarf_version < 5)
14617 break;
14618 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (inner)))
14619 break;
14620 if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (inner))
14621 && (!SCALAR_INT_MODE_P (mode)
14622 || !SCALAR_INT_MODE_P (GET_MODE (inner))))
14623 break;
14624 else
14626 dw_die_ref type_die;
14627 dw_loc_descr_ref cvt;
14629 mem_loc_result = mem_loc_descriptor (inner,
14630 GET_MODE (inner),
14631 mem_mode, initialized);
14632 if (mem_loc_result == NULL)
14633 break;
14634 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
14635 if (type_die == NULL)
14637 mem_loc_result = NULL;
14638 break;
14640 if (GET_MODE_SIZE (mode)
14641 != GET_MODE_SIZE (GET_MODE (inner)))
14642 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14643 else
14644 cvt = new_loc_descr (dwarf_OP (DW_OP_reinterpret), 0, 0);
14645 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14646 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14647 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14648 add_loc_descr (&mem_loc_result, cvt);
14649 if (SCALAR_INT_MODE_P (mode)
14650 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
14652 /* Convert it to untyped afterwards. */
14653 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14654 add_loc_descr (&mem_loc_result, cvt);
14657 break;
14659 case REG:
14660 if (! SCALAR_INT_MODE_P (mode)
14661 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
14662 && rtl != arg_pointer_rtx
14663 && rtl != frame_pointer_rtx
14664 #ifdef POINTERS_EXTEND_UNSIGNED
14665 && (mode != Pmode || mem_mode == VOIDmode)
14666 #endif
14669 dw_die_ref type_die;
14670 unsigned int dbx_regnum;
14672 if (dwarf_strict && dwarf_version < 5)
14673 break;
14674 if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
14675 break;
14676 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
14677 if (type_die == NULL)
14678 break;
14680 dbx_regnum = dbx_reg_number (rtl);
14681 if (dbx_regnum == IGNORED_DWARF_REGNUM)
14682 break;
14683 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_regval_type),
14684 dbx_regnum, 0);
14685 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
14686 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
14687 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
14688 break;
14690 /* Whenever a register number forms a part of the description of the
14691 method for calculating the (dynamic) address of a memory resident
14692 object, DWARF rules require the register number be referred to as
14693 a "base register". This distinction is not based in any way upon
14694 what category of register the hardware believes the given register
14695 belongs to. This is strictly DWARF terminology we're dealing with
14696 here. Note that in cases where the location of a memory-resident
14697 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
14698 OP_CONST (0)) the actual DWARF location descriptor that we generate
14699 may just be OP_BASEREG (basereg). This may look deceptively like
14700 the object in question was allocated to a register (rather than in
14701 memory) so DWARF consumers need to be aware of the subtle
14702 distinction between OP_REG and OP_BASEREG. */
14703 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
14704 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
14705 else if (stack_realign_drap
14706 && crtl->drap_reg
14707 && crtl->args.internal_arg_pointer == rtl
14708 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
14710 /* If RTL is internal_arg_pointer, which has been optimized
14711 out, use DRAP instead. */
14712 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
14713 VAR_INIT_STATUS_INITIALIZED);
14715 break;
14717 case SIGN_EXTEND:
14718 case ZERO_EXTEND:
14719 if (!SCALAR_INT_MODE_P (mode))
14720 break;
14721 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
14722 mem_mode, VAR_INIT_STATUS_INITIALIZED);
14723 if (op0 == 0)
14724 break;
14725 else if (GET_CODE (rtl) == ZERO_EXTEND
14726 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
14727 && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
14728 < HOST_BITS_PER_WIDE_INT
14729 /* If DW_OP_const{1,2,4}u won't be used, it is shorter
14730 to expand zero extend as two shifts instead of
14731 masking. */
14732 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= 4)
14734 machine_mode imode = GET_MODE (XEXP (rtl, 0));
14735 mem_loc_result = op0;
14736 add_loc_descr (&mem_loc_result,
14737 int_loc_descriptor (GET_MODE_MASK (imode)));
14738 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
14740 else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
14742 int shift = DWARF2_ADDR_SIZE
14743 - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
14744 shift *= BITS_PER_UNIT;
14745 if (GET_CODE (rtl) == SIGN_EXTEND)
14746 op = DW_OP_shra;
14747 else
14748 op = DW_OP_shr;
14749 mem_loc_result = op0;
14750 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
14751 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
14752 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
14753 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14755 else if (!dwarf_strict || dwarf_version >= 5)
14757 dw_die_ref type_die1, type_die2;
14758 dw_loc_descr_ref cvt;
14760 type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
14761 GET_CODE (rtl) == ZERO_EXTEND);
14762 if (type_die1 == NULL)
14763 break;
14764 type_die2 = base_type_for_mode (mode, 1);
14765 if (type_die2 == NULL)
14766 break;
14767 mem_loc_result = op0;
14768 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14769 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14770 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
14771 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14772 add_loc_descr (&mem_loc_result, cvt);
14773 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14774 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14775 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
14776 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14777 add_loc_descr (&mem_loc_result, cvt);
14779 break;
14781 case MEM:
14783 rtx new_rtl = avoid_constant_pool_reference (rtl);
14784 if (new_rtl != rtl)
14786 mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
14787 initialized);
14788 if (mem_loc_result != NULL)
14789 return mem_loc_result;
14792 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
14793 get_address_mode (rtl), mode,
14794 VAR_INIT_STATUS_INITIALIZED);
14795 if (mem_loc_result == NULL)
14796 mem_loc_result = tls_mem_loc_descriptor (rtl);
14797 if (mem_loc_result != NULL)
14799 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
14800 || !SCALAR_INT_MODE_P(mode))
14802 dw_die_ref type_die;
14803 dw_loc_descr_ref deref;
14805 if (dwarf_strict && dwarf_version < 5)
14806 return NULL;
14807 type_die
14808 = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
14809 if (type_die == NULL)
14810 return NULL;
14811 deref = new_loc_descr (dwarf_OP (DW_OP_deref_type),
14812 GET_MODE_SIZE (mode), 0);
14813 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
14814 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
14815 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
14816 add_loc_descr (&mem_loc_result, deref);
14818 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
14819 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
14820 else
14821 add_loc_descr (&mem_loc_result,
14822 new_loc_descr (DW_OP_deref_size,
14823 GET_MODE_SIZE (mode), 0));
14825 break;
14827 case LO_SUM:
14828 return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
14830 case LABEL_REF:
14831 /* Some ports can transform a symbol ref into a label ref, because
14832 the symbol ref is too far away and has to be dumped into a constant
14833 pool. */
14834 case CONST:
14835 case SYMBOL_REF:
14836 if (!SCALAR_INT_MODE_P (mode)
14837 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
14838 #ifdef POINTERS_EXTEND_UNSIGNED
14839 && (mode != Pmode || mem_mode == VOIDmode)
14840 #endif
14842 break;
14843 if (GET_CODE (rtl) == SYMBOL_REF
14844 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
14846 dw_loc_descr_ref temp;
14848 /* If this is not defined, we have no way to emit the data. */
14849 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
14850 break;
14852 temp = new_addr_loc_descr (rtl, dtprel_true);
14854 /* We check for DWARF 5 here because gdb did not implement
14855 DW_OP_form_tls_address until after 7.12. */
14856 mem_loc_result = new_loc_descr ((dwarf_version >= 5
14857 ? DW_OP_form_tls_address
14858 : DW_OP_GNU_push_tls_address),
14859 0, 0);
14860 add_loc_descr (&mem_loc_result, temp);
14862 break;
14865 if (!const_ok_for_output (rtl))
14867 if (GET_CODE (rtl) == CONST)
14868 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14869 initialized);
14870 break;
14873 symref:
14874 mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
14875 vec_safe_push (used_rtx_array, rtl);
14876 break;
14878 case CONCAT:
14879 case CONCATN:
14880 case VAR_LOCATION:
14881 case DEBUG_IMPLICIT_PTR:
14882 expansion_failed (NULL_TREE, rtl,
14883 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
14884 return 0;
14886 case ENTRY_VALUE:
14887 if (dwarf_strict && dwarf_version < 5)
14888 return NULL;
14889 if (REG_P (ENTRY_VALUE_EXP (rtl)))
14891 if (!SCALAR_INT_MODE_P (mode)
14892 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
14893 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
14894 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14895 else
14897 unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
14898 if (dbx_regnum == IGNORED_DWARF_REGNUM)
14899 return NULL;
14900 op0 = one_reg_loc_descriptor (dbx_regnum,
14901 VAR_INIT_STATUS_INITIALIZED);
14904 else if (MEM_P (ENTRY_VALUE_EXP (rtl))
14905 && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
14907 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
14908 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14909 if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
14910 return NULL;
14912 else
14913 gcc_unreachable ();
14914 if (op0 == NULL)
14915 return NULL;
14916 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_entry_value), 0, 0);
14917 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
14918 mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
14919 break;
14921 case DEBUG_PARAMETER_REF:
14922 mem_loc_result = parameter_ref_descriptor (rtl);
14923 break;
14925 case PRE_MODIFY:
14926 /* Extract the PLUS expression nested inside and fall into
14927 PLUS code below. */
14928 rtl = XEXP (rtl, 1);
14929 goto plus;
14931 case PRE_INC:
14932 case PRE_DEC:
14933 /* Turn these into a PLUS expression and fall into the PLUS code
14934 below. */
14935 rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
14936 gen_int_mode (GET_CODE (rtl) == PRE_INC
14937 ? GET_MODE_UNIT_SIZE (mem_mode)
14938 : -GET_MODE_UNIT_SIZE (mem_mode),
14939 mode));
14941 /* fall through */
14943 case PLUS:
14944 plus:
14945 if (is_based_loc (rtl)
14946 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
14947 || XEXP (rtl, 0) == arg_pointer_rtx
14948 || XEXP (rtl, 0) == frame_pointer_rtx)
14949 && SCALAR_INT_MODE_P (mode))
14950 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
14951 INTVAL (XEXP (rtl, 1)),
14952 VAR_INIT_STATUS_INITIALIZED);
14953 else
14955 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14956 VAR_INIT_STATUS_INITIALIZED);
14957 if (mem_loc_result == 0)
14958 break;
14960 if (CONST_INT_P (XEXP (rtl, 1))
14961 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
14962 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
14963 else
14965 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14966 VAR_INIT_STATUS_INITIALIZED);
14967 if (op1 == 0)
14968 return NULL;
14969 add_loc_descr (&mem_loc_result, op1);
14970 add_loc_descr (&mem_loc_result,
14971 new_loc_descr (DW_OP_plus, 0, 0));
14974 break;
14976 /* If a pseudo-reg is optimized away, it is possible for it to
14977 be replaced with a MEM containing a multiply or shift. */
14978 case MINUS:
14979 op = DW_OP_minus;
14980 goto do_binop;
14982 case MULT:
14983 op = DW_OP_mul;
14984 goto do_binop;
14986 case DIV:
14987 if ((!dwarf_strict || dwarf_version >= 5)
14988 && SCALAR_INT_MODE_P (mode)
14989 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
14991 mem_loc_result = typed_binop (DW_OP_div, rtl,
14992 base_type_for_mode (mode, 0),
14993 mode, mem_mode);
14994 break;
14996 op = DW_OP_div;
14997 goto do_binop;
14999 case UMOD:
15000 op = DW_OP_mod;
15001 goto do_binop;
15003 case ASHIFT:
15004 op = DW_OP_shl;
15005 goto do_shift;
15007 case ASHIFTRT:
15008 op = DW_OP_shra;
15009 goto do_shift;
15011 case LSHIFTRT:
15012 op = DW_OP_shr;
15013 goto do_shift;
15015 do_shift:
15016 if (!SCALAR_INT_MODE_P (mode))
15017 break;
15018 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15019 VAR_INIT_STATUS_INITIALIZED);
15021 rtx rtlop1 = XEXP (rtl, 1);
15022 if (GET_MODE (rtlop1) != VOIDmode
15023 && GET_MODE_BITSIZE (GET_MODE (rtlop1))
15024 < GET_MODE_BITSIZE (mode))
15025 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
15026 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
15027 VAR_INIT_STATUS_INITIALIZED);
15030 if (op0 == 0 || op1 == 0)
15031 break;
15033 mem_loc_result = op0;
15034 add_loc_descr (&mem_loc_result, op1);
15035 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15036 break;
15038 case AND:
15039 op = DW_OP_and;
15040 goto do_binop;
15042 case IOR:
15043 op = DW_OP_or;
15044 goto do_binop;
15046 case XOR:
15047 op = DW_OP_xor;
15048 goto do_binop;
15050 do_binop:
15051 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15052 VAR_INIT_STATUS_INITIALIZED);
15053 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15054 VAR_INIT_STATUS_INITIALIZED);
15056 if (op0 == 0 || op1 == 0)
15057 break;
15059 mem_loc_result = op0;
15060 add_loc_descr (&mem_loc_result, op1);
15061 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15062 break;
15064 case MOD:
15065 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
15066 && (!dwarf_strict || dwarf_version >= 5))
15068 mem_loc_result = typed_binop (DW_OP_mod, rtl,
15069 base_type_for_mode (mode, 0),
15070 mode, mem_mode);
15071 break;
15074 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15075 VAR_INIT_STATUS_INITIALIZED);
15076 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15077 VAR_INIT_STATUS_INITIALIZED);
15079 if (op0 == 0 || op1 == 0)
15080 break;
15082 mem_loc_result = op0;
15083 add_loc_descr (&mem_loc_result, op1);
15084 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
15085 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
15086 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
15087 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
15088 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
15089 break;
15091 case UDIV:
15092 if ((!dwarf_strict || dwarf_version >= 5)
15093 && SCALAR_INT_MODE_P (mode))
15095 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
15097 op = DW_OP_div;
15098 goto do_binop;
15100 mem_loc_result = typed_binop (DW_OP_div, rtl,
15101 base_type_for_mode (mode, 1),
15102 mode, mem_mode);
15104 break;
15106 case NOT:
15107 op = DW_OP_not;
15108 goto do_unop;
15110 case ABS:
15111 op = DW_OP_abs;
15112 goto do_unop;
15114 case NEG:
15115 op = DW_OP_neg;
15116 goto do_unop;
15118 do_unop:
15119 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15120 VAR_INIT_STATUS_INITIALIZED);
15122 if (op0 == 0)
15123 break;
15125 mem_loc_result = op0;
15126 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15127 break;
15129 case CONST_INT:
15130 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
15131 #ifdef POINTERS_EXTEND_UNSIGNED
15132 || (mode == Pmode
15133 && mem_mode != VOIDmode
15134 && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
15135 #endif
15138 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
15139 break;
15141 if ((!dwarf_strict || dwarf_version >= 5)
15142 && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
15143 || GET_MODE_BITSIZE (mode) == HOST_BITS_PER_DOUBLE_INT))
15145 dw_die_ref type_die = base_type_for_mode (mode, 1);
15146 machine_mode amode;
15147 if (type_die == NULL)
15148 return NULL;
15149 amode = mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT,
15150 MODE_INT, 0);
15151 if (INTVAL (rtl) >= 0
15152 && amode != BLKmode
15153 && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
15154 /* const DW_OP_convert <XXX> vs.
15155 DW_OP_const_type <XXX, 1, const>. */
15156 && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
15157 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (mode))
15159 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
15160 op0 = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15161 op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15162 op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15163 op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
15164 add_loc_descr (&mem_loc_result, op0);
15165 return mem_loc_result;
15167 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0,
15168 INTVAL (rtl));
15169 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15170 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15171 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
15172 if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
15173 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
15174 else
15176 mem_loc_result->dw_loc_oprnd2.val_class
15177 = dw_val_class_const_double;
15178 mem_loc_result->dw_loc_oprnd2.v.val_double
15179 = double_int::from_shwi (INTVAL (rtl));
15182 break;
15184 case CONST_DOUBLE:
15185 if (!dwarf_strict || dwarf_version >= 5)
15187 dw_die_ref type_die;
15189 /* Note that if TARGET_SUPPORTS_WIDE_INT == 0, a
15190 CONST_DOUBLE rtx could represent either a large integer
15191 or a floating-point constant. If TARGET_SUPPORTS_WIDE_INT != 0,
15192 the value is always a floating point constant.
15194 When it is an integer, a CONST_DOUBLE is used whenever
15195 the constant requires 2 HWIs to be adequately represented.
15196 We output CONST_DOUBLEs as blocks. */
15197 if (mode == VOIDmode
15198 || (GET_MODE (rtl) == VOIDmode
15199 && GET_MODE_BITSIZE (mode) != HOST_BITS_PER_DOUBLE_INT))
15200 break;
15201 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
15202 if (type_die == NULL)
15203 return NULL;
15204 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0, 0);
15205 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15206 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15207 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
15208 #if TARGET_SUPPORTS_WIDE_INT == 0
15209 if (!SCALAR_FLOAT_MODE_P (mode))
15211 mem_loc_result->dw_loc_oprnd2.val_class
15212 = dw_val_class_const_double;
15213 mem_loc_result->dw_loc_oprnd2.v.val_double
15214 = rtx_to_double_int (rtl);
15216 else
15217 #endif
15219 unsigned int length = GET_MODE_SIZE (mode);
15220 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
15222 insert_float (rtl, array);
15223 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
15224 mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
15225 mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
15226 mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
15229 break;
15231 case CONST_WIDE_INT:
15232 if (!dwarf_strict || dwarf_version >= 5)
15234 dw_die_ref type_die;
15236 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
15237 if (type_die == NULL)
15238 return NULL;
15239 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0, 0);
15240 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15241 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15242 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
15243 mem_loc_result->dw_loc_oprnd2.val_class
15244 = dw_val_class_wide_int;
15245 mem_loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
15246 *mem_loc_result->dw_loc_oprnd2.v.val_wide = rtx_mode_t (rtl, mode);
15248 break;
15250 case EQ:
15251 mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
15252 break;
15254 case GE:
15255 mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
15256 break;
15258 case GT:
15259 mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
15260 break;
15262 case LE:
15263 mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
15264 break;
15266 case LT:
15267 mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
15268 break;
15270 case NE:
15271 mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
15272 break;
15274 case GEU:
15275 mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
15276 break;
15278 case GTU:
15279 mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
15280 break;
15282 case LEU:
15283 mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
15284 break;
15286 case LTU:
15287 mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
15288 break;
15290 case UMIN:
15291 case UMAX:
15292 if (!SCALAR_INT_MODE_P (mode))
15293 break;
15294 /* FALLTHRU */
15295 case SMIN:
15296 case SMAX:
15297 mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
15298 break;
15300 case ZERO_EXTRACT:
15301 case SIGN_EXTRACT:
15302 if (CONST_INT_P (XEXP (rtl, 1))
15303 && CONST_INT_P (XEXP (rtl, 2))
15304 && ((unsigned) INTVAL (XEXP (rtl, 1))
15305 + (unsigned) INTVAL (XEXP (rtl, 2))
15306 <= GET_MODE_BITSIZE (mode))
15307 && SCALAR_INT_MODE_P (mode)
15308 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
15309 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
15311 int shift, size;
15312 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
15313 mem_mode, VAR_INIT_STATUS_INITIALIZED);
15314 if (op0 == 0)
15315 break;
15316 if (GET_CODE (rtl) == SIGN_EXTRACT)
15317 op = DW_OP_shra;
15318 else
15319 op = DW_OP_shr;
15320 mem_loc_result = op0;
15321 size = INTVAL (XEXP (rtl, 1));
15322 shift = INTVAL (XEXP (rtl, 2));
15323 if (BITS_BIG_ENDIAN)
15324 shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
15325 - shift - size;
15326 if (shift + size != (int) DWARF2_ADDR_SIZE)
15328 add_loc_descr (&mem_loc_result,
15329 int_loc_descriptor (DWARF2_ADDR_SIZE
15330 - shift - size));
15331 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
15333 if (size != (int) DWARF2_ADDR_SIZE)
15335 add_loc_descr (&mem_loc_result,
15336 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
15337 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15340 break;
15342 case IF_THEN_ELSE:
15344 dw_loc_descr_ref op2, bra_node, drop_node;
15345 op0 = mem_loc_descriptor (XEXP (rtl, 0),
15346 GET_MODE (XEXP (rtl, 0)) == VOIDmode
15347 ? word_mode : GET_MODE (XEXP (rtl, 0)),
15348 mem_mode, VAR_INIT_STATUS_INITIALIZED);
15349 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15350 VAR_INIT_STATUS_INITIALIZED);
15351 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
15352 VAR_INIT_STATUS_INITIALIZED);
15353 if (op0 == NULL || op1 == NULL || op2 == NULL)
15354 break;
15356 mem_loc_result = op1;
15357 add_loc_descr (&mem_loc_result, op2);
15358 add_loc_descr (&mem_loc_result, op0);
15359 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
15360 add_loc_descr (&mem_loc_result, bra_node);
15361 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
15362 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
15363 add_loc_descr (&mem_loc_result, drop_node);
15364 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15365 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
15367 break;
15369 case FLOAT_EXTEND:
15370 case FLOAT_TRUNCATE:
15371 case FLOAT:
15372 case UNSIGNED_FLOAT:
15373 case FIX:
15374 case UNSIGNED_FIX:
15375 if (!dwarf_strict || dwarf_version >= 5)
15377 dw_die_ref type_die;
15378 dw_loc_descr_ref cvt;
15380 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
15381 mem_mode, VAR_INIT_STATUS_INITIALIZED);
15382 if (op0 == NULL)
15383 break;
15384 if (SCALAR_INT_MODE_P (GET_MODE (XEXP (rtl, 0)))
15385 && (GET_CODE (rtl) == FLOAT
15386 || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
15387 <= DWARF2_ADDR_SIZE))
15389 type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
15390 GET_CODE (rtl) == UNSIGNED_FLOAT);
15391 if (type_die == NULL)
15392 break;
15393 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15394 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15395 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15396 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15397 add_loc_descr (&op0, cvt);
15399 type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
15400 if (type_die == NULL)
15401 break;
15402 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15403 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15404 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15405 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15406 add_loc_descr (&op0, cvt);
15407 if (SCALAR_INT_MODE_P (mode)
15408 && (GET_CODE (rtl) == FIX
15409 || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
15411 op0 = convert_descriptor_to_mode (mode, op0);
15412 if (op0 == NULL)
15413 break;
15415 mem_loc_result = op0;
15417 break;
15419 case CLZ:
15420 case CTZ:
15421 case FFS:
15422 mem_loc_result = clz_loc_descriptor (rtl, mode, mem_mode);
15423 break;
15425 case POPCOUNT:
15426 case PARITY:
15427 mem_loc_result = popcount_loc_descriptor (rtl, mode, mem_mode);
15428 break;
15430 case BSWAP:
15431 mem_loc_result = bswap_loc_descriptor (rtl, mode, mem_mode);
15432 break;
15434 case ROTATE:
15435 case ROTATERT:
15436 mem_loc_result = rotate_loc_descriptor (rtl, mode, mem_mode);
15437 break;
15439 case COMPARE:
15440 /* In theory, we could implement the above. */
15441 /* DWARF cannot represent the unsigned compare operations
15442 natively. */
15443 case SS_MULT:
15444 case US_MULT:
15445 case SS_DIV:
15446 case US_DIV:
15447 case SS_PLUS:
15448 case US_PLUS:
15449 case SS_MINUS:
15450 case US_MINUS:
15451 case SS_NEG:
15452 case US_NEG:
15453 case SS_ABS:
15454 case SS_ASHIFT:
15455 case US_ASHIFT:
15456 case SS_TRUNCATE:
15457 case US_TRUNCATE:
15458 case UNORDERED:
15459 case ORDERED:
15460 case UNEQ:
15461 case UNGE:
15462 case UNGT:
15463 case UNLE:
15464 case UNLT:
15465 case LTGT:
15466 case FRACT_CONVERT:
15467 case UNSIGNED_FRACT_CONVERT:
15468 case SAT_FRACT:
15469 case UNSIGNED_SAT_FRACT:
15470 case SQRT:
15471 case ASM_OPERANDS:
15472 case VEC_MERGE:
15473 case VEC_SELECT:
15474 case VEC_CONCAT:
15475 case VEC_DUPLICATE:
15476 case UNSPEC:
15477 case HIGH:
15478 case FMA:
15479 case STRICT_LOW_PART:
15480 case CONST_VECTOR:
15481 case CONST_FIXED:
15482 case CLRSB:
15483 case CLOBBER:
15484 /* If delegitimize_address couldn't do anything with the UNSPEC, we
15485 can't express it in the debug info. This can happen e.g. with some
15486 TLS UNSPECs. */
15487 break;
15489 case CONST_STRING:
15490 resolve_one_addr (&rtl);
15491 goto symref;
15493 /* RTL sequences inside PARALLEL record a series of DWARF operations for
15494 the expression. An UNSPEC rtx represents a raw DWARF operation,
15495 new_loc_descr is called for it to build the operation directly.
15496 Otherwise mem_loc_descriptor is called recursively. */
15497 case PARALLEL:
15499 int index = 0;
15500 dw_loc_descr_ref exp_result = NULL;
15502 for (; index < XVECLEN (rtl, 0); index++)
15504 rtx elem = XVECEXP (rtl, 0, index);
15505 if (GET_CODE (elem) == UNSPEC)
15507 /* Each DWARF operation UNSPEC contain two operands, if
15508 one operand is not used for the operation, const0_rtx is
15509 passed. */
15510 gcc_assert (XVECLEN (elem, 0) == 2);
15512 HOST_WIDE_INT dw_op = XINT (elem, 1);
15513 HOST_WIDE_INT oprnd1 = INTVAL (XVECEXP (elem, 0, 0));
15514 HOST_WIDE_INT oprnd2 = INTVAL (XVECEXP (elem, 0, 1));
15515 exp_result
15516 = new_loc_descr ((enum dwarf_location_atom) dw_op, oprnd1,
15517 oprnd2);
15519 else
15520 exp_result
15521 = mem_loc_descriptor (elem, mode, mem_mode,
15522 VAR_INIT_STATUS_INITIALIZED);
15524 if (!mem_loc_result)
15525 mem_loc_result = exp_result;
15526 else
15527 add_loc_descr (&mem_loc_result, exp_result);
15530 break;
15533 default:
15534 if (flag_checking)
15536 print_rtl (stderr, rtl);
15537 gcc_unreachable ();
15539 break;
15542 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
15543 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
15545 return mem_loc_result;
15548 /* Return a descriptor that describes the concatenation of two locations.
15549 This is typically a complex variable. */
15551 static dw_loc_descr_ref
15552 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
15554 dw_loc_descr_ref cc_loc_result = NULL;
15555 dw_loc_descr_ref x0_ref
15556 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15557 dw_loc_descr_ref x1_ref
15558 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15560 if (x0_ref == 0 || x1_ref == 0)
15561 return 0;
15563 cc_loc_result = x0_ref;
15564 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
15566 add_loc_descr (&cc_loc_result, x1_ref);
15567 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
15569 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
15570 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
15572 return cc_loc_result;
15575 /* Return a descriptor that describes the concatenation of N
15576 locations. */
15578 static dw_loc_descr_ref
15579 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
15581 unsigned int i;
15582 dw_loc_descr_ref cc_loc_result = NULL;
15583 unsigned int n = XVECLEN (concatn, 0);
15585 for (i = 0; i < n; ++i)
15587 dw_loc_descr_ref ref;
15588 rtx x = XVECEXP (concatn, 0, i);
15590 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15591 if (ref == NULL)
15592 return NULL;
15594 add_loc_descr (&cc_loc_result, ref);
15595 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
15598 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
15599 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
15601 return cc_loc_result;
15604 /* Helper function for loc_descriptor. Return DW_OP_implicit_pointer
15605 for DEBUG_IMPLICIT_PTR RTL. */
15607 static dw_loc_descr_ref
15608 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
15610 dw_loc_descr_ref ret;
15611 dw_die_ref ref;
15613 if (dwarf_strict && dwarf_version < 5)
15614 return NULL;
15615 gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
15616 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
15617 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
15618 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
15619 ret = new_loc_descr (dwarf_OP (DW_OP_implicit_pointer), 0, offset);
15620 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
15621 if (ref)
15623 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15624 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
15625 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
15627 else
15629 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
15630 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
15632 return ret;
15635 /* Output a proper Dwarf location descriptor for a variable or parameter
15636 which is either allocated in a register or in a memory location. For a
15637 register, we just generate an OP_REG and the register number. For a
15638 memory location we provide a Dwarf postfix expression describing how to
15639 generate the (dynamic) address of the object onto the address stack.
15641 MODE is mode of the decl if this loc_descriptor is going to be used in
15642 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
15643 allowed, VOIDmode otherwise.
15645 If we don't know how to describe it, return 0. */
15647 static dw_loc_descr_ref
15648 loc_descriptor (rtx rtl, machine_mode mode,
15649 enum var_init_status initialized)
15651 dw_loc_descr_ref loc_result = NULL;
15653 switch (GET_CODE (rtl))
15655 case SUBREG:
15656 /* The case of a subreg may arise when we have a local (register)
15657 variable or a formal (register) parameter which doesn't quite fill
15658 up an entire register. For now, just assume that it is
15659 legitimate to make the Dwarf info refer to the whole register which
15660 contains the given subreg. */
15661 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
15662 loc_result = loc_descriptor (SUBREG_REG (rtl),
15663 GET_MODE (SUBREG_REG (rtl)), initialized);
15664 else
15665 goto do_default;
15666 break;
15668 case REG:
15669 loc_result = reg_loc_descriptor (rtl, initialized);
15670 break;
15672 case MEM:
15673 loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
15674 GET_MODE (rtl), initialized);
15675 if (loc_result == NULL)
15676 loc_result = tls_mem_loc_descriptor (rtl);
15677 if (loc_result == NULL)
15679 rtx new_rtl = avoid_constant_pool_reference (rtl);
15680 if (new_rtl != rtl)
15681 loc_result = loc_descriptor (new_rtl, mode, initialized);
15683 break;
15685 case CONCAT:
15686 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
15687 initialized);
15688 break;
15690 case CONCATN:
15691 loc_result = concatn_loc_descriptor (rtl, initialized);
15692 break;
15694 case VAR_LOCATION:
15695 /* Single part. */
15696 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
15698 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
15699 if (GET_CODE (loc) == EXPR_LIST)
15700 loc = XEXP (loc, 0);
15701 loc_result = loc_descriptor (loc, mode, initialized);
15702 break;
15705 rtl = XEXP (rtl, 1);
15706 /* FALLTHRU */
15708 case PARALLEL:
15710 rtvec par_elems = XVEC (rtl, 0);
15711 int num_elem = GET_NUM_ELEM (par_elems);
15712 machine_mode mode;
15713 int i;
15715 /* Create the first one, so we have something to add to. */
15716 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
15717 VOIDmode, initialized);
15718 if (loc_result == NULL)
15719 return NULL;
15720 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
15721 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
15722 for (i = 1; i < num_elem; i++)
15724 dw_loc_descr_ref temp;
15726 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
15727 VOIDmode, initialized);
15728 if (temp == NULL)
15729 return NULL;
15730 add_loc_descr (&loc_result, temp);
15731 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
15732 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
15735 break;
15737 case CONST_INT:
15738 if (mode != VOIDmode && mode != BLKmode)
15739 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
15740 INTVAL (rtl));
15741 break;
15743 case CONST_DOUBLE:
15744 if (mode == VOIDmode)
15745 mode = GET_MODE (rtl);
15747 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
15749 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
15751 /* Note that a CONST_DOUBLE rtx could represent either an integer
15752 or a floating-point constant. A CONST_DOUBLE is used whenever
15753 the constant requires more than one word in order to be
15754 adequately represented. We output CONST_DOUBLEs as blocks. */
15755 loc_result = new_loc_descr (DW_OP_implicit_value,
15756 GET_MODE_SIZE (mode), 0);
15757 #if TARGET_SUPPORTS_WIDE_INT == 0
15758 if (!SCALAR_FLOAT_MODE_P (mode))
15760 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
15761 loc_result->dw_loc_oprnd2.v.val_double
15762 = rtx_to_double_int (rtl);
15764 else
15765 #endif
15767 unsigned int length = GET_MODE_SIZE (mode);
15768 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
15770 insert_float (rtl, array);
15771 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
15772 loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
15773 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
15774 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
15777 break;
15779 case CONST_WIDE_INT:
15780 if (mode == VOIDmode)
15781 mode = GET_MODE (rtl);
15783 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
15785 loc_result = new_loc_descr (DW_OP_implicit_value,
15786 GET_MODE_SIZE (mode), 0);
15787 loc_result->dw_loc_oprnd2.val_class = dw_val_class_wide_int;
15788 loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
15789 *loc_result->dw_loc_oprnd2.v.val_wide = rtx_mode_t (rtl, mode);
15791 break;
15793 case CONST_VECTOR:
15794 if (mode == VOIDmode)
15795 mode = GET_MODE (rtl);
15797 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
15799 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
15800 unsigned int length = CONST_VECTOR_NUNITS (rtl);
15801 unsigned char *array
15802 = ggc_vec_alloc<unsigned char> (length * elt_size);
15803 unsigned int i;
15804 unsigned char *p;
15805 machine_mode imode = GET_MODE_INNER (mode);
15807 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
15808 switch (GET_MODE_CLASS (mode))
15810 case MODE_VECTOR_INT:
15811 for (i = 0, p = array; i < length; i++, p += elt_size)
15813 rtx elt = CONST_VECTOR_ELT (rtl, i);
15814 insert_wide_int (rtx_mode_t (elt, imode), p, elt_size);
15816 break;
15818 case MODE_VECTOR_FLOAT:
15819 for (i = 0, p = array; i < length; i++, p += elt_size)
15821 rtx elt = CONST_VECTOR_ELT (rtl, i);
15822 insert_float (elt, p);
15824 break;
15826 default:
15827 gcc_unreachable ();
15830 loc_result = new_loc_descr (DW_OP_implicit_value,
15831 length * elt_size, 0);
15832 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
15833 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
15834 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
15835 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
15837 break;
15839 case CONST:
15840 if (mode == VOIDmode
15841 || CONST_SCALAR_INT_P (XEXP (rtl, 0))
15842 || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
15843 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
15845 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
15846 break;
15848 /* FALLTHROUGH */
15849 case SYMBOL_REF:
15850 if (!const_ok_for_output (rtl))
15851 break;
15852 /* FALLTHROUGH */
15853 case LABEL_REF:
15854 if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
15855 && (dwarf_version >= 4 || !dwarf_strict))
15857 loc_result = new_addr_loc_descr (rtl, dtprel_false);
15858 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
15859 vec_safe_push (used_rtx_array, rtl);
15861 break;
15863 case DEBUG_IMPLICIT_PTR:
15864 loc_result = implicit_ptr_descriptor (rtl, 0);
15865 break;
15867 case PLUS:
15868 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
15869 && CONST_INT_P (XEXP (rtl, 1)))
15871 loc_result
15872 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
15873 break;
15875 /* FALLTHRU */
15876 do_default:
15877 default:
15878 if ((SCALAR_INT_MODE_P (mode)
15879 && GET_MODE (rtl) == mode
15880 && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
15881 && dwarf_version >= 4)
15882 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
15884 /* Value expression. */
15885 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
15886 if (loc_result)
15887 add_loc_descr (&loc_result,
15888 new_loc_descr (DW_OP_stack_value, 0, 0));
15890 break;
15893 return loc_result;
15896 /* We need to figure out what section we should use as the base for the
15897 address ranges where a given location is valid.
15898 1. If this particular DECL has a section associated with it, use that.
15899 2. If this function has a section associated with it, use that.
15900 3. Otherwise, use the text section.
15901 XXX: If you split a variable across multiple sections, we won't notice. */
15903 static const char *
15904 secname_for_decl (const_tree decl)
15906 const char *secname;
15908 if (VAR_OR_FUNCTION_DECL_P (decl)
15909 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl) || TREE_STATIC (decl))
15910 && DECL_SECTION_NAME (decl))
15911 secname = DECL_SECTION_NAME (decl);
15912 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
15913 secname = DECL_SECTION_NAME (current_function_decl);
15914 else if (cfun && in_cold_section_p)
15915 secname = crtl->subsections.cold_section_label;
15916 else
15917 secname = text_section_label;
15919 return secname;
15922 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
15924 static bool
15925 decl_by_reference_p (tree decl)
15927 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
15928 || VAR_P (decl))
15929 && DECL_BY_REFERENCE (decl));
15932 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
15933 for VARLOC. */
15935 static dw_loc_descr_ref
15936 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
15937 enum var_init_status initialized)
15939 int have_address = 0;
15940 dw_loc_descr_ref descr;
15941 machine_mode mode;
15943 if (want_address != 2)
15945 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
15946 /* Single part. */
15947 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
15949 varloc = PAT_VAR_LOCATION_LOC (varloc);
15950 if (GET_CODE (varloc) == EXPR_LIST)
15951 varloc = XEXP (varloc, 0);
15952 mode = GET_MODE (varloc);
15953 if (MEM_P (varloc))
15955 rtx addr = XEXP (varloc, 0);
15956 descr = mem_loc_descriptor (addr, get_address_mode (varloc),
15957 mode, initialized);
15958 if (descr)
15959 have_address = 1;
15960 else
15962 rtx x = avoid_constant_pool_reference (varloc);
15963 if (x != varloc)
15964 descr = mem_loc_descriptor (x, mode, VOIDmode,
15965 initialized);
15968 else
15969 descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
15971 else
15972 return 0;
15974 else
15976 if (GET_CODE (varloc) == VAR_LOCATION)
15977 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
15978 else
15979 mode = DECL_MODE (loc);
15980 descr = loc_descriptor (varloc, mode, initialized);
15981 have_address = 1;
15984 if (!descr)
15985 return 0;
15987 if (want_address == 2 && !have_address
15988 && (dwarf_version >= 4 || !dwarf_strict))
15990 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
15992 expansion_failed (loc, NULL_RTX,
15993 "DWARF address size mismatch");
15994 return 0;
15996 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
15997 have_address = 1;
15999 /* Show if we can't fill the request for an address. */
16000 if (want_address && !have_address)
16002 expansion_failed (loc, NULL_RTX,
16003 "Want address and only have value");
16004 return 0;
16007 /* If we've got an address and don't want one, dereference. */
16008 if (!want_address && have_address)
16010 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
16011 enum dwarf_location_atom op;
16013 if (size > DWARF2_ADDR_SIZE || size == -1)
16015 expansion_failed (loc, NULL_RTX,
16016 "DWARF address size mismatch");
16017 return 0;
16019 else if (size == DWARF2_ADDR_SIZE)
16020 op = DW_OP_deref;
16021 else
16022 op = DW_OP_deref_size;
16024 add_loc_descr (&descr, new_loc_descr (op, size, 0));
16027 return descr;
16030 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
16031 if it is not possible. */
16033 static dw_loc_descr_ref
16034 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
16036 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
16037 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
16038 else if (dwarf_version >= 3 || !dwarf_strict)
16039 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
16040 else
16041 return NULL;
16044 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
16045 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
16047 static dw_loc_descr_ref
16048 dw_sra_loc_expr (tree decl, rtx loc)
16050 rtx p;
16051 unsigned HOST_WIDE_INT padsize = 0;
16052 dw_loc_descr_ref descr, *descr_tail;
16053 unsigned HOST_WIDE_INT decl_size;
16054 rtx varloc;
16055 enum var_init_status initialized;
16057 if (DECL_SIZE (decl) == NULL
16058 || !tree_fits_uhwi_p (DECL_SIZE (decl)))
16059 return NULL;
16061 decl_size = tree_to_uhwi (DECL_SIZE (decl));
16062 descr = NULL;
16063 descr_tail = &descr;
16065 for (p = loc; p; p = XEXP (p, 1))
16067 unsigned HOST_WIDE_INT bitsize = decl_piece_bitsize (p);
16068 rtx loc_note = *decl_piece_varloc_ptr (p);
16069 dw_loc_descr_ref cur_descr;
16070 dw_loc_descr_ref *tail, last = NULL;
16071 unsigned HOST_WIDE_INT opsize = 0;
16073 if (loc_note == NULL_RTX
16074 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
16076 padsize += bitsize;
16077 continue;
16079 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
16080 varloc = NOTE_VAR_LOCATION (loc_note);
16081 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
16082 if (cur_descr == NULL)
16084 padsize += bitsize;
16085 continue;
16088 /* Check that cur_descr either doesn't use
16089 DW_OP_*piece operations, or their sum is equal
16090 to bitsize. Otherwise we can't embed it. */
16091 for (tail = &cur_descr; *tail != NULL;
16092 tail = &(*tail)->dw_loc_next)
16093 if ((*tail)->dw_loc_opc == DW_OP_piece)
16095 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
16096 * BITS_PER_UNIT;
16097 last = *tail;
16099 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
16101 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
16102 last = *tail;
16105 if (last != NULL && opsize != bitsize)
16107 padsize += bitsize;
16108 /* Discard the current piece of the descriptor and release any
16109 addr_table entries it uses. */
16110 remove_loc_list_addr_table_entries (cur_descr);
16111 continue;
16114 /* If there is a hole, add DW_OP_*piece after empty DWARF
16115 expression, which means that those bits are optimized out. */
16116 if (padsize)
16118 if (padsize > decl_size)
16120 remove_loc_list_addr_table_entries (cur_descr);
16121 goto discard_descr;
16123 decl_size -= padsize;
16124 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
16125 if (*descr_tail == NULL)
16127 remove_loc_list_addr_table_entries (cur_descr);
16128 goto discard_descr;
16130 descr_tail = &(*descr_tail)->dw_loc_next;
16131 padsize = 0;
16133 *descr_tail = cur_descr;
16134 descr_tail = tail;
16135 if (bitsize > decl_size)
16136 goto discard_descr;
16137 decl_size -= bitsize;
16138 if (last == NULL)
16140 HOST_WIDE_INT offset = 0;
16141 if (GET_CODE (varloc) == VAR_LOCATION
16142 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
16144 varloc = PAT_VAR_LOCATION_LOC (varloc);
16145 if (GET_CODE (varloc) == EXPR_LIST)
16146 varloc = XEXP (varloc, 0);
16150 if (GET_CODE (varloc) == CONST
16151 || GET_CODE (varloc) == SIGN_EXTEND
16152 || GET_CODE (varloc) == ZERO_EXTEND)
16153 varloc = XEXP (varloc, 0);
16154 else if (GET_CODE (varloc) == SUBREG)
16155 varloc = SUBREG_REG (varloc);
16156 else
16157 break;
16159 while (1);
16160 /* DW_OP_bit_size offset should be zero for register
16161 or implicit location descriptions and empty location
16162 descriptions, but for memory addresses needs big endian
16163 adjustment. */
16164 if (MEM_P (varloc))
16166 unsigned HOST_WIDE_INT memsize
16167 = MEM_SIZE (varloc) * BITS_PER_UNIT;
16168 if (memsize != bitsize)
16170 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
16171 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
16172 goto discard_descr;
16173 if (memsize < bitsize)
16174 goto discard_descr;
16175 if (BITS_BIG_ENDIAN)
16176 offset = memsize - bitsize;
16180 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
16181 if (*descr_tail == NULL)
16182 goto discard_descr;
16183 descr_tail = &(*descr_tail)->dw_loc_next;
16187 /* If there were any non-empty expressions, add padding till the end of
16188 the decl. */
16189 if (descr != NULL && decl_size != 0)
16191 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
16192 if (*descr_tail == NULL)
16193 goto discard_descr;
16195 return descr;
16197 discard_descr:
16198 /* Discard the descriptor and release any addr_table entries it uses. */
16199 remove_loc_list_addr_table_entries (descr);
16200 return NULL;
16203 /* Return the dwarf representation of the location list LOC_LIST of
16204 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
16205 function. */
16207 static dw_loc_list_ref
16208 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
16210 const char *endname, *secname;
16211 rtx varloc;
16212 enum var_init_status initialized;
16213 struct var_loc_node *node;
16214 dw_loc_descr_ref descr;
16215 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
16216 dw_loc_list_ref list = NULL;
16217 dw_loc_list_ref *listp = &list;
16219 /* Now that we know what section we are using for a base,
16220 actually construct the list of locations.
16221 The first location information is what is passed to the
16222 function that creates the location list, and the remaining
16223 locations just get added on to that list.
16224 Note that we only know the start address for a location
16225 (IE location changes), so to build the range, we use
16226 the range [current location start, next location start].
16227 This means we have to special case the last node, and generate
16228 a range of [last location start, end of function label]. */
16230 secname = secname_for_decl (decl);
16232 for (node = loc_list->first; node; node = node->next)
16233 if (GET_CODE (node->loc) == EXPR_LIST
16234 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
16236 if (GET_CODE (node->loc) == EXPR_LIST)
16238 /* This requires DW_OP_{,bit_}piece, which is not usable
16239 inside DWARF expressions. */
16240 if (want_address != 2)
16241 continue;
16242 descr = dw_sra_loc_expr (decl, node->loc);
16243 if (descr == NULL)
16244 continue;
16246 else
16248 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
16249 varloc = NOTE_VAR_LOCATION (node->loc);
16250 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
16252 if (descr)
16254 bool range_across_switch = false;
16255 /* If section switch happens in between node->label
16256 and node->next->label (or end of function) and
16257 we can't emit it as a single entry list,
16258 emit two ranges, first one ending at the end
16259 of first partition and second one starting at the
16260 beginning of second partition. */
16261 if (node == loc_list->last_before_switch
16262 && (node != loc_list->first || loc_list->first->next)
16263 && current_function_decl)
16265 endname = cfun->fde->dw_fde_end;
16266 range_across_switch = true;
16268 /* The variable has a location between NODE->LABEL and
16269 NODE->NEXT->LABEL. */
16270 else if (node->next)
16271 endname = node->next->label;
16272 /* If the variable has a location at the last label
16273 it keeps its location until the end of function. */
16274 else if (!current_function_decl)
16275 endname = text_end_label;
16276 else
16278 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
16279 current_function_funcdef_no);
16280 endname = ggc_strdup (label_id);
16283 *listp = new_loc_list (descr, node->label, endname, secname);
16284 if (TREE_CODE (decl) == PARM_DECL
16285 && node == loc_list->first
16286 && NOTE_P (node->loc)
16287 && strcmp (node->label, endname) == 0)
16288 (*listp)->force = true;
16289 listp = &(*listp)->dw_loc_next;
16291 if (range_across_switch)
16293 if (GET_CODE (node->loc) == EXPR_LIST)
16294 descr = dw_sra_loc_expr (decl, node->loc);
16295 else
16297 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
16298 varloc = NOTE_VAR_LOCATION (node->loc);
16299 descr = dw_loc_list_1 (decl, varloc, want_address,
16300 initialized);
16302 gcc_assert (descr);
16303 /* The variable has a location between NODE->LABEL and
16304 NODE->NEXT->LABEL. */
16305 if (node->next)
16306 endname = node->next->label;
16307 else
16308 endname = cfun->fde->dw_fde_second_end;
16309 *listp = new_loc_list (descr,
16310 cfun->fde->dw_fde_second_begin,
16311 endname, secname);
16312 listp = &(*listp)->dw_loc_next;
16317 /* Try to avoid the overhead of a location list emitting a location
16318 expression instead, but only if we didn't have more than one
16319 location entry in the first place. If some entries were not
16320 representable, we don't want to pretend a single entry that was
16321 applies to the entire scope in which the variable is
16322 available. */
16323 if (list && loc_list->first->next)
16324 gen_llsym (list);
16326 return list;
16329 /* Return if the loc_list has only single element and thus can be represented
16330 as location description. */
16332 static bool
16333 single_element_loc_list_p (dw_loc_list_ref list)
16335 gcc_assert (!list->dw_loc_next || list->ll_symbol);
16336 return !list->ll_symbol;
16339 /* Duplicate a single element of location list. */
16341 static inline dw_loc_descr_ref
16342 copy_loc_descr (dw_loc_descr_ref ref)
16344 dw_loc_descr_ref copy = ggc_alloc<dw_loc_descr_node> ();
16345 memcpy (copy, ref, sizeof (dw_loc_descr_node));
16346 return copy;
16349 /* To each location in list LIST append loc descr REF. */
16351 static void
16352 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
16354 dw_loc_descr_ref copy;
16355 add_loc_descr (&list->expr, ref);
16356 list = list->dw_loc_next;
16357 while (list)
16359 copy = copy_loc_descr (ref);
16360 add_loc_descr (&list->expr, copy);
16361 while (copy->dw_loc_next)
16362 copy = copy->dw_loc_next = copy_loc_descr (copy->dw_loc_next);
16363 list = list->dw_loc_next;
16367 /* To each location in list LIST prepend loc descr REF. */
16369 static void
16370 prepend_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
16372 dw_loc_descr_ref copy;
16373 dw_loc_descr_ref ref_end = list->expr;
16374 add_loc_descr (&ref, list->expr);
16375 list->expr = ref;
16376 list = list->dw_loc_next;
16377 while (list)
16379 dw_loc_descr_ref end = list->expr;
16380 list->expr = copy = copy_loc_descr (ref);
16381 while (copy->dw_loc_next != ref_end)
16382 copy = copy->dw_loc_next = copy_loc_descr (copy->dw_loc_next);
16383 copy->dw_loc_next = end;
16384 list = list->dw_loc_next;
16388 /* Given two lists RET and LIST
16389 produce location list that is result of adding expression in LIST
16390 to expression in RET on each position in program.
16391 Might be destructive on both RET and LIST.
16393 TODO: We handle only simple cases of RET or LIST having at most one
16394 element. General case would involve sorting the lists in program order
16395 and merging them that will need some additional work.
16396 Adding that will improve quality of debug info especially for SRA-ed
16397 structures. */
16399 static void
16400 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
16402 if (!list)
16403 return;
16404 if (!*ret)
16406 *ret = list;
16407 return;
16409 if (!list->dw_loc_next)
16411 add_loc_descr_to_each (*ret, list->expr);
16412 return;
16414 if (!(*ret)->dw_loc_next)
16416 prepend_loc_descr_to_each (list, (*ret)->expr);
16417 *ret = list;
16418 return;
16420 expansion_failed (NULL_TREE, NULL_RTX,
16421 "Don't know how to merge two non-trivial"
16422 " location lists.\n");
16423 *ret = NULL;
16424 return;
16427 /* LOC is constant expression. Try a luck, look it up in constant
16428 pool and return its loc_descr of its address. */
16430 static dw_loc_descr_ref
16431 cst_pool_loc_descr (tree loc)
16433 /* Get an RTL for this, if something has been emitted. */
16434 rtx rtl = lookup_constant_def (loc);
16436 if (!rtl || !MEM_P (rtl))
16438 gcc_assert (!rtl);
16439 return 0;
16441 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
16443 /* TODO: We might get more coverage if we was actually delaying expansion
16444 of all expressions till end of compilation when constant pools are fully
16445 populated. */
16446 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
16448 expansion_failed (loc, NULL_RTX,
16449 "CST value in contant pool but not marked.");
16450 return 0;
16452 return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
16453 GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
16456 /* Return dw_loc_list representing address of addr_expr LOC
16457 by looking for inner INDIRECT_REF expression and turning
16458 it into simple arithmetics.
16460 See loc_list_from_tree for the meaning of CONTEXT. */
16462 static dw_loc_list_ref
16463 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev,
16464 loc_descr_context *context)
16466 tree obj, offset;
16467 HOST_WIDE_INT bitsize, bitpos, bytepos;
16468 machine_mode mode;
16469 int unsignedp, reversep, volatilep = 0;
16470 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
16472 obj = get_inner_reference (TREE_OPERAND (loc, 0),
16473 &bitsize, &bitpos, &offset, &mode,
16474 &unsignedp, &reversep, &volatilep);
16475 STRIP_NOPS (obj);
16476 if (bitpos % BITS_PER_UNIT)
16478 expansion_failed (loc, NULL_RTX, "bitfield access");
16479 return 0;
16481 if (!INDIRECT_REF_P (obj))
16483 expansion_failed (obj,
16484 NULL_RTX, "no indirect ref in inner refrence");
16485 return 0;
16487 if (!offset && !bitpos)
16488 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1,
16489 context);
16490 else if (toplev
16491 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
16492 && (dwarf_version >= 4 || !dwarf_strict))
16494 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0, context);
16495 if (!list_ret)
16496 return 0;
16497 if (offset)
16499 /* Variable offset. */
16500 list_ret1 = loc_list_from_tree (offset, 0, context);
16501 if (list_ret1 == 0)
16502 return 0;
16503 add_loc_list (&list_ret, list_ret1);
16504 if (!list_ret)
16505 return 0;
16506 add_loc_descr_to_each (list_ret,
16507 new_loc_descr (DW_OP_plus, 0, 0));
16509 bytepos = bitpos / BITS_PER_UNIT;
16510 if (bytepos > 0)
16511 add_loc_descr_to_each (list_ret,
16512 new_loc_descr (DW_OP_plus_uconst,
16513 bytepos, 0));
16514 else if (bytepos < 0)
16515 loc_list_plus_const (list_ret, bytepos);
16516 add_loc_descr_to_each (list_ret,
16517 new_loc_descr (DW_OP_stack_value, 0, 0));
16519 return list_ret;
16522 /* Set LOC to the next operation that is not a DW_OP_nop operation. In the case
16523 all operations from LOC are nops, move to the last one. Insert in NOPS all
16524 operations that are skipped. */
16526 static void
16527 loc_descr_to_next_no_nop (dw_loc_descr_ref &loc,
16528 hash_set<dw_loc_descr_ref> &nops)
16530 while (loc->dw_loc_next != NULL && loc->dw_loc_opc == DW_OP_nop)
16532 nops.add (loc);
16533 loc = loc->dw_loc_next;
16537 /* Helper for loc_descr_without_nops: free the location description operation
16538 P. */
16540 bool
16541 free_loc_descr (const dw_loc_descr_ref &loc, void *data ATTRIBUTE_UNUSED)
16543 ggc_free (loc);
16544 return true;
16547 /* Remove all DW_OP_nop operations from LOC except, if it exists, the one that
16548 finishes LOC. */
16550 static void
16551 loc_descr_without_nops (dw_loc_descr_ref &loc)
16553 if (loc->dw_loc_opc == DW_OP_nop && loc->dw_loc_next == NULL)
16554 return;
16556 /* Set of all DW_OP_nop operations we remove. */
16557 hash_set<dw_loc_descr_ref> nops;
16559 /* First, strip all prefix NOP operations in order to keep the head of the
16560 operations list. */
16561 loc_descr_to_next_no_nop (loc, nops);
16563 for (dw_loc_descr_ref cur = loc; cur != NULL;)
16565 /* For control flow operations: strip "prefix" nops in destination
16566 labels. */
16567 if (cur->dw_loc_oprnd1.val_class == dw_val_class_loc)
16568 loc_descr_to_next_no_nop (cur->dw_loc_oprnd1.v.val_loc, nops);
16569 if (cur->dw_loc_oprnd2.val_class == dw_val_class_loc)
16570 loc_descr_to_next_no_nop (cur->dw_loc_oprnd2.v.val_loc, nops);
16572 /* Do the same for the operations that follow, then move to the next
16573 iteration. */
16574 if (cur->dw_loc_next != NULL)
16575 loc_descr_to_next_no_nop (cur->dw_loc_next, nops);
16576 cur = cur->dw_loc_next;
16579 nops.traverse<void *, free_loc_descr> (NULL);
16583 struct dwarf_procedure_info;
16585 /* Helper structure for location descriptions generation. */
16586 struct loc_descr_context
16588 /* The type that is implicitly referenced by DW_OP_push_object_address, or
16589 NULL_TREE if DW_OP_push_object_address in invalid for this location
16590 description. This is used when processing PLACEHOLDER_EXPR nodes. */
16591 tree context_type;
16592 /* The ..._DECL node that should be translated as a
16593 DW_OP_push_object_address operation. */
16594 tree base_decl;
16595 /* Information about the DWARF procedure we are currently generating. NULL if
16596 we are not generating a DWARF procedure. */
16597 struct dwarf_procedure_info *dpi;
16598 /* True if integral PLACEHOLDER_EXPR stands for the first argument passed
16599 by consumer. Used for DW_TAG_generic_subrange attributes. */
16600 bool placeholder_arg;
16601 /* True if PLACEHOLDER_EXPR has been seen. */
16602 bool placeholder_seen;
16605 /* DWARF procedures generation
16607 DWARF expressions (aka. location descriptions) are used to encode variable
16608 things such as sizes or offsets. Such computations can have redundant parts
16609 that can be factorized in order to reduce the size of the output debug
16610 information. This is the whole point of DWARF procedures.
16612 Thanks to stor-layout.c, size and offset expressions in GENERIC trees are
16613 already factorized into functions ("size functions") in order to handle very
16614 big and complex types. Such functions are quite simple: they have integral
16615 arguments, they return an integral result and their body contains only a
16616 return statement with arithmetic expressions. This is the only kind of
16617 function we are interested in translating into DWARF procedures, here.
16619 DWARF expressions and DWARF procedure are executed using a stack, so we have
16620 to define some calling convention for them to interact. Let's say that:
16622 - Before calling a DWARF procedure, DWARF expressions must push on the stack
16623 all arguments in reverse order (right-to-left) so that when the DWARF
16624 procedure execution starts, the first argument is the top of the stack.
16626 - Then, when returning, the DWARF procedure must have consumed all arguments
16627 on the stack, must have pushed the result and touched nothing else.
16629 - Each integral argument and the result are integral types can be hold in a
16630 single stack slot.
16632 - We call "frame offset" the number of stack slots that are "under DWARF
16633 procedure control": it includes the arguments slots, the temporaries and
16634 the result slot. Thus, it is equal to the number of arguments when the
16635 procedure execution starts and must be equal to one (the result) when it
16636 returns. */
16638 /* Helper structure used when generating operations for a DWARF procedure. */
16639 struct dwarf_procedure_info
16641 /* The FUNCTION_DECL node corresponding to the DWARF procedure that is
16642 currently translated. */
16643 tree fndecl;
16644 /* The number of arguments FNDECL takes. */
16645 unsigned args_count;
16648 /* Return a pointer to a newly created DIE node for a DWARF procedure. Add
16649 LOCATION as its DW_AT_location attribute. If FNDECL is not NULL_TREE,
16650 equate it to this DIE. */
16652 static dw_die_ref
16653 new_dwarf_proc_die (dw_loc_descr_ref location, tree fndecl,
16654 dw_die_ref parent_die)
16656 dw_die_ref dwarf_proc_die;
16658 if ((dwarf_version < 3 && dwarf_strict)
16659 || location == NULL)
16660 return NULL;
16662 dwarf_proc_die = new_die (DW_TAG_dwarf_procedure, parent_die, fndecl);
16663 if (fndecl)
16664 equate_decl_number_to_die (fndecl, dwarf_proc_die);
16665 add_AT_loc (dwarf_proc_die, DW_AT_location, location);
16666 return dwarf_proc_die;
16669 /* Return whether TYPE is a supported type as a DWARF procedure argument
16670 type or return type (we handle only scalar types and pointer types that
16671 aren't wider than the DWARF expression evaluation stack. */
16673 static bool
16674 is_handled_procedure_type (tree type)
16676 return ((INTEGRAL_TYPE_P (type)
16677 || TREE_CODE (type) == OFFSET_TYPE
16678 || TREE_CODE (type) == POINTER_TYPE)
16679 && int_size_in_bytes (type) <= DWARF2_ADDR_SIZE);
16682 /* Helper for resolve_args_picking: do the same but stop when coming across
16683 visited nodes. For each node we visit, register in FRAME_OFFSETS the frame
16684 offset *before* evaluating the corresponding operation. */
16686 static bool
16687 resolve_args_picking_1 (dw_loc_descr_ref loc, unsigned initial_frame_offset,
16688 struct dwarf_procedure_info *dpi,
16689 hash_map<dw_loc_descr_ref, unsigned> &frame_offsets)
16691 /* The "frame_offset" identifier is already used to name a macro... */
16692 unsigned frame_offset_ = initial_frame_offset;
16693 dw_loc_descr_ref l;
16695 for (l = loc; l != NULL;)
16697 bool existed;
16698 unsigned &l_frame_offset = frame_offsets.get_or_insert (l, &existed);
16700 /* If we already met this node, there is nothing to compute anymore. */
16701 if (existed)
16703 /* Make sure that the stack size is consistent wherever the execution
16704 flow comes from. */
16705 gcc_assert ((unsigned) l_frame_offset == frame_offset_);
16706 break;
16708 l_frame_offset = frame_offset_;
16710 /* If needed, relocate the picking offset with respect to the frame
16711 offset. */
16712 if (l->frame_offset_rel)
16714 unsigned HOST_WIDE_INT off;
16715 switch (l->dw_loc_opc)
16717 case DW_OP_pick:
16718 off = l->dw_loc_oprnd1.v.val_unsigned;
16719 break;
16720 case DW_OP_dup:
16721 off = 0;
16722 break;
16723 case DW_OP_over:
16724 off = 1;
16725 break;
16726 default:
16727 gcc_unreachable ();
16729 /* frame_offset_ is the size of the current stack frame, including
16730 incoming arguments. Besides, the arguments are pushed
16731 right-to-left. Thus, in order to access the Nth argument from
16732 this operation node, the picking has to skip temporaries *plus*
16733 one stack slot per argument (0 for the first one, 1 for the second
16734 one, etc.).
16736 The targetted argument number (N) is already set as the operand,
16737 and the number of temporaries can be computed with:
16738 frame_offsets_ - dpi->args_count */
16739 off += frame_offset_ - dpi->args_count;
16741 /* DW_OP_pick handles only offsets from 0 to 255 (inclusive)... */
16742 if (off > 255)
16743 return false;
16745 if (off == 0)
16747 l->dw_loc_opc = DW_OP_dup;
16748 l->dw_loc_oprnd1.v.val_unsigned = 0;
16750 else if (off == 1)
16752 l->dw_loc_opc = DW_OP_over;
16753 l->dw_loc_oprnd1.v.val_unsigned = 0;
16755 else
16757 l->dw_loc_opc = DW_OP_pick;
16758 l->dw_loc_oprnd1.v.val_unsigned = off;
16762 /* Update frame_offset according to the effect the current operation has
16763 on the stack. */
16764 switch (l->dw_loc_opc)
16766 case DW_OP_deref:
16767 case DW_OP_swap:
16768 case DW_OP_rot:
16769 case DW_OP_abs:
16770 case DW_OP_neg:
16771 case DW_OP_not:
16772 case DW_OP_plus_uconst:
16773 case DW_OP_skip:
16774 case DW_OP_reg0:
16775 case DW_OP_reg1:
16776 case DW_OP_reg2:
16777 case DW_OP_reg3:
16778 case DW_OP_reg4:
16779 case DW_OP_reg5:
16780 case DW_OP_reg6:
16781 case DW_OP_reg7:
16782 case DW_OP_reg8:
16783 case DW_OP_reg9:
16784 case DW_OP_reg10:
16785 case DW_OP_reg11:
16786 case DW_OP_reg12:
16787 case DW_OP_reg13:
16788 case DW_OP_reg14:
16789 case DW_OP_reg15:
16790 case DW_OP_reg16:
16791 case DW_OP_reg17:
16792 case DW_OP_reg18:
16793 case DW_OP_reg19:
16794 case DW_OP_reg20:
16795 case DW_OP_reg21:
16796 case DW_OP_reg22:
16797 case DW_OP_reg23:
16798 case DW_OP_reg24:
16799 case DW_OP_reg25:
16800 case DW_OP_reg26:
16801 case DW_OP_reg27:
16802 case DW_OP_reg28:
16803 case DW_OP_reg29:
16804 case DW_OP_reg30:
16805 case DW_OP_reg31:
16806 case DW_OP_bregx:
16807 case DW_OP_piece:
16808 case DW_OP_deref_size:
16809 case DW_OP_nop:
16810 case DW_OP_bit_piece:
16811 case DW_OP_implicit_value:
16812 case DW_OP_stack_value:
16813 break;
16815 case DW_OP_addr:
16816 case DW_OP_const1u:
16817 case DW_OP_const1s:
16818 case DW_OP_const2u:
16819 case DW_OP_const2s:
16820 case DW_OP_const4u:
16821 case DW_OP_const4s:
16822 case DW_OP_const8u:
16823 case DW_OP_const8s:
16824 case DW_OP_constu:
16825 case DW_OP_consts:
16826 case DW_OP_dup:
16827 case DW_OP_over:
16828 case DW_OP_pick:
16829 case DW_OP_lit0:
16830 case DW_OP_lit1:
16831 case DW_OP_lit2:
16832 case DW_OP_lit3:
16833 case DW_OP_lit4:
16834 case DW_OP_lit5:
16835 case DW_OP_lit6:
16836 case DW_OP_lit7:
16837 case DW_OP_lit8:
16838 case DW_OP_lit9:
16839 case DW_OP_lit10:
16840 case DW_OP_lit11:
16841 case DW_OP_lit12:
16842 case DW_OP_lit13:
16843 case DW_OP_lit14:
16844 case DW_OP_lit15:
16845 case DW_OP_lit16:
16846 case DW_OP_lit17:
16847 case DW_OP_lit18:
16848 case DW_OP_lit19:
16849 case DW_OP_lit20:
16850 case DW_OP_lit21:
16851 case DW_OP_lit22:
16852 case DW_OP_lit23:
16853 case DW_OP_lit24:
16854 case DW_OP_lit25:
16855 case DW_OP_lit26:
16856 case DW_OP_lit27:
16857 case DW_OP_lit28:
16858 case DW_OP_lit29:
16859 case DW_OP_lit30:
16860 case DW_OP_lit31:
16861 case DW_OP_breg0:
16862 case DW_OP_breg1:
16863 case DW_OP_breg2:
16864 case DW_OP_breg3:
16865 case DW_OP_breg4:
16866 case DW_OP_breg5:
16867 case DW_OP_breg6:
16868 case DW_OP_breg7:
16869 case DW_OP_breg8:
16870 case DW_OP_breg9:
16871 case DW_OP_breg10:
16872 case DW_OP_breg11:
16873 case DW_OP_breg12:
16874 case DW_OP_breg13:
16875 case DW_OP_breg14:
16876 case DW_OP_breg15:
16877 case DW_OP_breg16:
16878 case DW_OP_breg17:
16879 case DW_OP_breg18:
16880 case DW_OP_breg19:
16881 case DW_OP_breg20:
16882 case DW_OP_breg21:
16883 case DW_OP_breg22:
16884 case DW_OP_breg23:
16885 case DW_OP_breg24:
16886 case DW_OP_breg25:
16887 case DW_OP_breg26:
16888 case DW_OP_breg27:
16889 case DW_OP_breg28:
16890 case DW_OP_breg29:
16891 case DW_OP_breg30:
16892 case DW_OP_breg31:
16893 case DW_OP_fbreg:
16894 case DW_OP_push_object_address:
16895 case DW_OP_call_frame_cfa:
16896 case DW_OP_GNU_variable_value:
16897 ++frame_offset_;
16898 break;
16900 case DW_OP_drop:
16901 case DW_OP_xderef:
16902 case DW_OP_and:
16903 case DW_OP_div:
16904 case DW_OP_minus:
16905 case DW_OP_mod:
16906 case DW_OP_mul:
16907 case DW_OP_or:
16908 case DW_OP_plus:
16909 case DW_OP_shl:
16910 case DW_OP_shr:
16911 case DW_OP_shra:
16912 case DW_OP_xor:
16913 case DW_OP_bra:
16914 case DW_OP_eq:
16915 case DW_OP_ge:
16916 case DW_OP_gt:
16917 case DW_OP_le:
16918 case DW_OP_lt:
16919 case DW_OP_ne:
16920 case DW_OP_regx:
16921 case DW_OP_xderef_size:
16922 --frame_offset_;
16923 break;
16925 case DW_OP_call2:
16926 case DW_OP_call4:
16927 case DW_OP_call_ref:
16929 dw_die_ref dwarf_proc = l->dw_loc_oprnd1.v.val_die_ref.die;
16930 int *stack_usage = dwarf_proc_stack_usage_map->get (dwarf_proc);
16932 if (stack_usage == NULL)
16933 return false;
16934 frame_offset_ += *stack_usage;
16935 break;
16938 case DW_OP_implicit_pointer:
16939 case DW_OP_entry_value:
16940 case DW_OP_const_type:
16941 case DW_OP_regval_type:
16942 case DW_OP_deref_type:
16943 case DW_OP_convert:
16944 case DW_OP_reinterpret:
16945 case DW_OP_form_tls_address:
16946 case DW_OP_GNU_push_tls_address:
16947 case DW_OP_GNU_uninit:
16948 case DW_OP_GNU_encoded_addr:
16949 case DW_OP_GNU_implicit_pointer:
16950 case DW_OP_GNU_entry_value:
16951 case DW_OP_GNU_const_type:
16952 case DW_OP_GNU_regval_type:
16953 case DW_OP_GNU_deref_type:
16954 case DW_OP_GNU_convert:
16955 case DW_OP_GNU_reinterpret:
16956 case DW_OP_GNU_parameter_ref:
16957 /* loc_list_from_tree will probably not output these operations for
16958 size functions, so assume they will not appear here. */
16959 /* Fall through... */
16961 default:
16962 gcc_unreachable ();
16965 /* Now, follow the control flow (except subroutine calls). */
16966 switch (l->dw_loc_opc)
16968 case DW_OP_bra:
16969 if (!resolve_args_picking_1 (l->dw_loc_next, frame_offset_, dpi,
16970 frame_offsets))
16971 return false;
16972 /* Fall through. */
16974 case DW_OP_skip:
16975 l = l->dw_loc_oprnd1.v.val_loc;
16976 break;
16978 case DW_OP_stack_value:
16979 return true;
16981 default:
16982 l = l->dw_loc_next;
16983 break;
16987 return true;
16990 /* Make a DFS over operations reachable through LOC (i.e. follow branch
16991 operations) in order to resolve the operand of DW_OP_pick operations that
16992 target DWARF procedure arguments (DPI). INITIAL_FRAME_OFFSET is the frame
16993 offset *before* LOC is executed. Return if all relocations were
16994 successful. */
16996 static bool
16997 resolve_args_picking (dw_loc_descr_ref loc, unsigned initial_frame_offset,
16998 struct dwarf_procedure_info *dpi)
17000 /* Associate to all visited operations the frame offset *before* evaluating
17001 this operation. */
17002 hash_map<dw_loc_descr_ref, unsigned> frame_offsets;
17004 return resolve_args_picking_1 (loc, initial_frame_offset, dpi,
17005 frame_offsets);
17008 /* Try to generate a DWARF procedure that computes the same result as FNDECL.
17009 Return NULL if it is not possible. */
17011 static dw_die_ref
17012 function_to_dwarf_procedure (tree fndecl)
17014 struct loc_descr_context ctx;
17015 struct dwarf_procedure_info dpi;
17016 dw_die_ref dwarf_proc_die;
17017 tree tree_body = DECL_SAVED_TREE (fndecl);
17018 dw_loc_descr_ref loc_body, epilogue;
17020 tree cursor;
17021 unsigned i;
17023 /* Do not generate multiple DWARF procedures for the same function
17024 declaration. */
17025 dwarf_proc_die = lookup_decl_die (fndecl);
17026 if (dwarf_proc_die != NULL)
17027 return dwarf_proc_die;
17029 /* DWARF procedures are available starting with the DWARFv3 standard. */
17030 if (dwarf_version < 3 && dwarf_strict)
17031 return NULL;
17033 /* We handle only functions for which we still have a body, that return a
17034 supported type and that takes arguments with supported types. Note that
17035 there is no point translating functions that return nothing. */
17036 if (tree_body == NULL_TREE
17037 || DECL_RESULT (fndecl) == NULL_TREE
17038 || !is_handled_procedure_type (TREE_TYPE (DECL_RESULT (fndecl))))
17039 return NULL;
17041 for (cursor = DECL_ARGUMENTS (fndecl);
17042 cursor != NULL_TREE;
17043 cursor = TREE_CHAIN (cursor))
17044 if (!is_handled_procedure_type (TREE_TYPE (cursor)))
17045 return NULL;
17047 /* Match only "expr" in: RETURN_EXPR (MODIFY_EXPR (RESULT_DECL, expr)). */
17048 if (TREE_CODE (tree_body) != RETURN_EXPR)
17049 return NULL;
17050 tree_body = TREE_OPERAND (tree_body, 0);
17051 if (TREE_CODE (tree_body) != MODIFY_EXPR
17052 || TREE_OPERAND (tree_body, 0) != DECL_RESULT (fndecl))
17053 return NULL;
17054 tree_body = TREE_OPERAND (tree_body, 1);
17056 /* Try to translate the body expression itself. Note that this will probably
17057 cause an infinite recursion if its call graph has a cycle. This is very
17058 unlikely for size functions, however, so don't bother with such things at
17059 the moment. */
17060 ctx.context_type = NULL_TREE;
17061 ctx.base_decl = NULL_TREE;
17062 ctx.dpi = &dpi;
17063 ctx.placeholder_arg = false;
17064 ctx.placeholder_seen = false;
17065 dpi.fndecl = fndecl;
17066 dpi.args_count = list_length (DECL_ARGUMENTS (fndecl));
17067 loc_body = loc_descriptor_from_tree (tree_body, 0, &ctx);
17068 if (!loc_body)
17069 return NULL;
17071 /* After evaluating all operands in "loc_body", we should still have on the
17072 stack all arguments plus the desired function result (top of the stack).
17073 Generate code in order to keep only the result in our stack frame. */
17074 epilogue = NULL;
17075 for (i = 0; i < dpi.args_count; ++i)
17077 dw_loc_descr_ref op_couple = new_loc_descr (DW_OP_swap, 0, 0);
17078 op_couple->dw_loc_next = new_loc_descr (DW_OP_drop, 0, 0);
17079 op_couple->dw_loc_next->dw_loc_next = epilogue;
17080 epilogue = op_couple;
17082 add_loc_descr (&loc_body, epilogue);
17083 if (!resolve_args_picking (loc_body, dpi.args_count, &dpi))
17084 return NULL;
17086 /* Trailing nops from loc_descriptor_from_tree (if any) cannot be removed
17087 because they are considered useful. Now there is an epilogue, they are
17088 not anymore, so give it another try. */
17089 loc_descr_without_nops (loc_body);
17091 /* fndecl may be used both as a regular DW_TAG_subprogram DIE and as
17092 a DW_TAG_dwarf_procedure, so we may have a conflict, here. It's unlikely,
17093 though, given that size functions do not come from source, so they should
17094 not have a dedicated DW_TAG_subprogram DIE. */
17095 dwarf_proc_die
17096 = new_dwarf_proc_die (loc_body, fndecl,
17097 get_context_die (DECL_CONTEXT (fndecl)));
17099 /* The called DWARF procedure consumes one stack slot per argument and
17100 returns one stack slot. */
17101 dwarf_proc_stack_usage_map->put (dwarf_proc_die, 1 - dpi.args_count);
17103 return dwarf_proc_die;
17107 /* Generate Dwarf location list representing LOC.
17108 If WANT_ADDRESS is false, expression computing LOC will be computed
17109 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
17110 if WANT_ADDRESS is 2, expression computing address useable in location
17111 will be returned (i.e. DW_OP_reg can be used
17112 to refer to register values).
17114 CONTEXT provides information to customize the location descriptions
17115 generation. Its context_type field specifies what type is implicitly
17116 referenced by DW_OP_push_object_address. If it is NULL_TREE, this operation
17117 will not be generated.
17119 Its DPI field determines whether we are generating a DWARF expression for a
17120 DWARF procedure, so PARM_DECL references are processed specifically.
17122 If CONTEXT is NULL, the behavior is the same as if context_type, base_decl
17123 and dpi fields were null. */
17125 static dw_loc_list_ref
17126 loc_list_from_tree_1 (tree loc, int want_address,
17127 struct loc_descr_context *context)
17129 dw_loc_descr_ref ret = NULL, ret1 = NULL;
17130 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
17131 int have_address = 0;
17132 enum dwarf_location_atom op;
17134 /* ??? Most of the time we do not take proper care for sign/zero
17135 extending the values properly. Hopefully this won't be a real
17136 problem... */
17138 if (context != NULL
17139 && context->base_decl == loc
17140 && want_address == 0)
17142 if (dwarf_version >= 3 || !dwarf_strict)
17143 return new_loc_list (new_loc_descr (DW_OP_push_object_address, 0, 0),
17144 NULL, NULL, NULL);
17145 else
17146 return NULL;
17149 switch (TREE_CODE (loc))
17151 case ERROR_MARK:
17152 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
17153 return 0;
17155 case PLACEHOLDER_EXPR:
17156 /* This case involves extracting fields from an object to determine the
17157 position of other fields. It is supposed to appear only as the first
17158 operand of COMPONENT_REF nodes and to reference precisely the type
17159 that the context allows. */
17160 if (context != NULL
17161 && TREE_TYPE (loc) == context->context_type
17162 && want_address >= 1)
17164 if (dwarf_version >= 3 || !dwarf_strict)
17166 ret = new_loc_descr (DW_OP_push_object_address, 0, 0);
17167 have_address = 1;
17168 break;
17170 else
17171 return NULL;
17173 /* For DW_TAG_generic_subrange attributes, PLACEHOLDER_EXPR stands for
17174 the single argument passed by consumer. */
17175 else if (context != NULL
17176 && context->placeholder_arg
17177 && INTEGRAL_TYPE_P (TREE_TYPE (loc))
17178 && want_address == 0)
17180 ret = new_loc_descr (DW_OP_pick, 0, 0);
17181 ret->frame_offset_rel = 1;
17182 context->placeholder_seen = true;
17183 break;
17185 else
17186 expansion_failed (loc, NULL_RTX,
17187 "PLACEHOLDER_EXPR for an unexpected type");
17188 break;
17190 case CALL_EXPR:
17192 const int nargs = call_expr_nargs (loc);
17193 tree callee = get_callee_fndecl (loc);
17194 int i;
17195 dw_die_ref dwarf_proc;
17197 if (callee == NULL_TREE)
17198 goto call_expansion_failed;
17200 /* We handle only functions that return an integer. */
17201 if (!is_handled_procedure_type (TREE_TYPE (TREE_TYPE (callee))))
17202 goto call_expansion_failed;
17204 dwarf_proc = function_to_dwarf_procedure (callee);
17205 if (dwarf_proc == NULL)
17206 goto call_expansion_failed;
17208 /* Evaluate arguments right-to-left so that the first argument will
17209 be the top-most one on the stack. */
17210 for (i = nargs - 1; i >= 0; --i)
17212 dw_loc_descr_ref loc_descr
17213 = loc_descriptor_from_tree (CALL_EXPR_ARG (loc, i), 0,
17214 context);
17216 if (loc_descr == NULL)
17217 goto call_expansion_failed;
17219 add_loc_descr (&ret, loc_descr);
17222 ret1 = new_loc_descr (DW_OP_call4, 0, 0);
17223 ret1->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
17224 ret1->dw_loc_oprnd1.v.val_die_ref.die = dwarf_proc;
17225 ret1->dw_loc_oprnd1.v.val_die_ref.external = 0;
17226 add_loc_descr (&ret, ret1);
17227 break;
17229 call_expansion_failed:
17230 expansion_failed (loc, NULL_RTX, "CALL_EXPR");
17231 /* There are no opcodes for these operations. */
17232 return 0;
17235 case PREINCREMENT_EXPR:
17236 case PREDECREMENT_EXPR:
17237 case POSTINCREMENT_EXPR:
17238 case POSTDECREMENT_EXPR:
17239 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
17240 /* There are no opcodes for these operations. */
17241 return 0;
17243 case ADDR_EXPR:
17244 /* If we already want an address, see if there is INDIRECT_REF inside
17245 e.g. for &this->field. */
17246 if (want_address)
17248 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
17249 (loc, want_address == 2, context);
17250 if (list_ret)
17251 have_address = 1;
17252 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
17253 && (ret = cst_pool_loc_descr (loc)))
17254 have_address = 1;
17256 /* Otherwise, process the argument and look for the address. */
17257 if (!list_ret && !ret)
17258 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 1, context);
17259 else
17261 if (want_address)
17262 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
17263 return NULL;
17265 break;
17267 case VAR_DECL:
17268 if (DECL_THREAD_LOCAL_P (loc))
17270 rtx rtl;
17271 enum dwarf_location_atom tls_op;
17272 enum dtprel_bool dtprel = dtprel_false;
17274 if (targetm.have_tls)
17276 /* If this is not defined, we have no way to emit the
17277 data. */
17278 if (!targetm.asm_out.output_dwarf_dtprel)
17279 return 0;
17281 /* The way DW_OP_GNU_push_tls_address is specified, we
17282 can only look up addresses of objects in the current
17283 module. We used DW_OP_addr as first op, but that's
17284 wrong, because DW_OP_addr is relocated by the debug
17285 info consumer, while DW_OP_GNU_push_tls_address
17286 operand shouldn't be. */
17287 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
17288 return 0;
17289 dtprel = dtprel_true;
17290 /* We check for DWARF 5 here because gdb did not implement
17291 DW_OP_form_tls_address until after 7.12. */
17292 tls_op = (dwarf_version >= 5 ? DW_OP_form_tls_address
17293 : DW_OP_GNU_push_tls_address);
17295 else
17297 if (!targetm.emutls.debug_form_tls_address
17298 || !(dwarf_version >= 3 || !dwarf_strict))
17299 return 0;
17300 /* We stuffed the control variable into the DECL_VALUE_EXPR
17301 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
17302 no longer appear in gimple code. We used the control
17303 variable in specific so that we could pick it up here. */
17304 loc = DECL_VALUE_EXPR (loc);
17305 tls_op = DW_OP_form_tls_address;
17308 rtl = rtl_for_decl_location (loc);
17309 if (rtl == NULL_RTX)
17310 return 0;
17312 if (!MEM_P (rtl))
17313 return 0;
17314 rtl = XEXP (rtl, 0);
17315 if (! CONSTANT_P (rtl))
17316 return 0;
17318 ret = new_addr_loc_descr (rtl, dtprel);
17319 ret1 = new_loc_descr (tls_op, 0, 0);
17320 add_loc_descr (&ret, ret1);
17322 have_address = 1;
17323 break;
17325 /* FALLTHRU */
17327 case PARM_DECL:
17328 if (context != NULL && context->dpi != NULL
17329 && DECL_CONTEXT (loc) == context->dpi->fndecl)
17331 /* We are generating code for a DWARF procedure and we want to access
17332 one of its arguments: find the appropriate argument offset and let
17333 the resolve_args_picking pass compute the offset that complies
17334 with the stack frame size. */
17335 unsigned i = 0;
17336 tree cursor;
17338 for (cursor = DECL_ARGUMENTS (context->dpi->fndecl);
17339 cursor != NULL_TREE && cursor != loc;
17340 cursor = TREE_CHAIN (cursor), ++i)
17342 /* If we are translating a DWARF procedure, all referenced parameters
17343 must belong to the current function. */
17344 gcc_assert (cursor != NULL_TREE);
17346 ret = new_loc_descr (DW_OP_pick, i, 0);
17347 ret->frame_offset_rel = 1;
17348 break;
17350 /* FALLTHRU */
17352 case RESULT_DECL:
17353 if (DECL_HAS_VALUE_EXPR_P (loc))
17354 return loc_list_from_tree_1 (DECL_VALUE_EXPR (loc),
17355 want_address, context);
17356 /* FALLTHRU */
17358 case FUNCTION_DECL:
17360 rtx rtl;
17361 var_loc_list *loc_list = lookup_decl_loc (loc);
17363 if (loc_list && loc_list->first)
17365 list_ret = dw_loc_list (loc_list, loc, want_address);
17366 have_address = want_address != 0;
17367 break;
17369 rtl = rtl_for_decl_location (loc);
17370 if (rtl == NULL_RTX)
17372 if (TREE_CODE (loc) != FUNCTION_DECL
17373 && early_dwarf
17374 && current_function_decl
17375 && want_address != 1
17376 && (INTEGRAL_TYPE_P (TREE_TYPE (loc))
17377 || POINTER_TYPE_P (TREE_TYPE (loc)))
17378 && DECL_CONTEXT (loc) == current_function_decl
17379 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (loc)))
17380 <= DWARF2_ADDR_SIZE))
17382 dw_die_ref ref = lookup_decl_die (loc);
17383 ret = new_loc_descr (DW_OP_GNU_variable_value, 0, 0);
17384 if (ref)
17386 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
17387 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
17388 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
17390 else
17392 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
17393 ret->dw_loc_oprnd1.v.val_decl_ref = loc;
17395 break;
17397 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
17398 return 0;
17400 else if (CONST_INT_P (rtl))
17402 HOST_WIDE_INT val = INTVAL (rtl);
17403 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
17404 val &= GET_MODE_MASK (DECL_MODE (loc));
17405 ret = int_loc_descriptor (val);
17407 else if (GET_CODE (rtl) == CONST_STRING)
17409 expansion_failed (loc, NULL_RTX, "CONST_STRING");
17410 return 0;
17412 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
17413 ret = new_addr_loc_descr (rtl, dtprel_false);
17414 else
17416 machine_mode mode, mem_mode;
17418 /* Certain constructs can only be represented at top-level. */
17419 if (want_address == 2)
17421 ret = loc_descriptor (rtl, VOIDmode,
17422 VAR_INIT_STATUS_INITIALIZED);
17423 have_address = 1;
17425 else
17427 mode = GET_MODE (rtl);
17428 mem_mode = VOIDmode;
17429 if (MEM_P (rtl))
17431 mem_mode = mode;
17432 mode = get_address_mode (rtl);
17433 rtl = XEXP (rtl, 0);
17434 have_address = 1;
17436 ret = mem_loc_descriptor (rtl, mode, mem_mode,
17437 VAR_INIT_STATUS_INITIALIZED);
17439 if (!ret)
17440 expansion_failed (loc, rtl,
17441 "failed to produce loc descriptor for rtl");
17444 break;
17446 case MEM_REF:
17447 if (!integer_zerop (TREE_OPERAND (loc, 1)))
17449 have_address = 1;
17450 goto do_plus;
17452 /* Fallthru. */
17453 case INDIRECT_REF:
17454 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17455 have_address = 1;
17456 break;
17458 case TARGET_MEM_REF:
17459 case SSA_NAME:
17460 case DEBUG_EXPR_DECL:
17461 return NULL;
17463 case COMPOUND_EXPR:
17464 return loc_list_from_tree_1 (TREE_OPERAND (loc, 1), want_address,
17465 context);
17467 CASE_CONVERT:
17468 case VIEW_CONVERT_EXPR:
17469 case SAVE_EXPR:
17470 case MODIFY_EXPR:
17471 case NON_LVALUE_EXPR:
17472 return loc_list_from_tree_1 (TREE_OPERAND (loc, 0), want_address,
17473 context);
17475 case COMPONENT_REF:
17476 case BIT_FIELD_REF:
17477 case ARRAY_REF:
17478 case ARRAY_RANGE_REF:
17479 case REALPART_EXPR:
17480 case IMAGPART_EXPR:
17482 tree obj, offset;
17483 HOST_WIDE_INT bitsize, bitpos, bytepos;
17484 machine_mode mode;
17485 int unsignedp, reversep, volatilep = 0;
17487 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
17488 &unsignedp, &reversep, &volatilep);
17490 gcc_assert (obj != loc);
17492 list_ret = loc_list_from_tree_1 (obj,
17493 want_address == 2
17494 && !bitpos && !offset ? 2 : 1,
17495 context);
17496 /* TODO: We can extract value of the small expression via shifting even
17497 for nonzero bitpos. */
17498 if (list_ret == 0)
17499 return 0;
17500 if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
17502 expansion_failed (loc, NULL_RTX,
17503 "bitfield access");
17504 return 0;
17507 if (offset != NULL_TREE)
17509 /* Variable offset. */
17510 list_ret1 = loc_list_from_tree_1 (offset, 0, context);
17511 if (list_ret1 == 0)
17512 return 0;
17513 add_loc_list (&list_ret, list_ret1);
17514 if (!list_ret)
17515 return 0;
17516 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
17519 bytepos = bitpos / BITS_PER_UNIT;
17520 if (bytepos > 0)
17521 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
17522 else if (bytepos < 0)
17523 loc_list_plus_const (list_ret, bytepos);
17525 have_address = 1;
17526 break;
17529 case INTEGER_CST:
17530 if ((want_address || !tree_fits_shwi_p (loc))
17531 && (ret = cst_pool_loc_descr (loc)))
17532 have_address = 1;
17533 else if (want_address == 2
17534 && tree_fits_shwi_p (loc)
17535 && (ret = address_of_int_loc_descriptor
17536 (int_size_in_bytes (TREE_TYPE (loc)),
17537 tree_to_shwi (loc))))
17538 have_address = 1;
17539 else if (tree_fits_shwi_p (loc))
17540 ret = int_loc_descriptor (tree_to_shwi (loc));
17541 else if (tree_fits_uhwi_p (loc))
17542 ret = uint_loc_descriptor (tree_to_uhwi (loc));
17543 else
17545 expansion_failed (loc, NULL_RTX,
17546 "Integer operand is not host integer");
17547 return 0;
17549 break;
17551 case CONSTRUCTOR:
17552 case REAL_CST:
17553 case STRING_CST:
17554 case COMPLEX_CST:
17555 if ((ret = cst_pool_loc_descr (loc)))
17556 have_address = 1;
17557 else if (TREE_CODE (loc) == CONSTRUCTOR)
17559 tree type = TREE_TYPE (loc);
17560 unsigned HOST_WIDE_INT size = int_size_in_bytes (type);
17561 unsigned HOST_WIDE_INT offset = 0;
17562 unsigned HOST_WIDE_INT cnt;
17563 constructor_elt *ce;
17565 if (TREE_CODE (type) == RECORD_TYPE)
17567 /* This is very limited, but it's enough to output
17568 pointers to member functions, as long as the
17569 referenced function is defined in the current
17570 translation unit. */
17571 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (loc), cnt, ce)
17573 tree val = ce->value;
17575 tree field = ce->index;
17577 if (val)
17578 STRIP_NOPS (val);
17580 if (!field || DECL_BIT_FIELD (field))
17582 expansion_failed (loc, NULL_RTX,
17583 "bitfield in record type constructor");
17584 size = offset = (unsigned HOST_WIDE_INT)-1;
17585 ret = NULL;
17586 break;
17589 HOST_WIDE_INT fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
17590 unsigned HOST_WIDE_INT pos = int_byte_position (field);
17591 gcc_assert (pos + fieldsize <= size);
17592 if (pos < offset)
17594 expansion_failed (loc, NULL_RTX,
17595 "out-of-order fields in record constructor");
17596 size = offset = (unsigned HOST_WIDE_INT)-1;
17597 ret = NULL;
17598 break;
17600 if (pos > offset)
17602 ret1 = new_loc_descr (DW_OP_piece, pos - offset, 0);
17603 add_loc_descr (&ret, ret1);
17604 offset = pos;
17606 if (val && fieldsize != 0)
17608 ret1 = loc_descriptor_from_tree (val, want_address, context);
17609 if (!ret1)
17611 expansion_failed (loc, NULL_RTX,
17612 "unsupported expression in field");
17613 size = offset = (unsigned HOST_WIDE_INT)-1;
17614 ret = NULL;
17615 break;
17617 add_loc_descr (&ret, ret1);
17619 if (fieldsize)
17621 ret1 = new_loc_descr (DW_OP_piece, fieldsize, 0);
17622 add_loc_descr (&ret, ret1);
17623 offset = pos + fieldsize;
17627 if (offset != size)
17629 ret1 = new_loc_descr (DW_OP_piece, size - offset, 0);
17630 add_loc_descr (&ret, ret1);
17631 offset = size;
17634 have_address = !!want_address;
17636 else
17637 expansion_failed (loc, NULL_RTX,
17638 "constructor of non-record type");
17640 else
17641 /* We can construct small constants here using int_loc_descriptor. */
17642 expansion_failed (loc, NULL_RTX,
17643 "constructor or constant not in constant pool");
17644 break;
17646 case TRUTH_AND_EXPR:
17647 case TRUTH_ANDIF_EXPR:
17648 case BIT_AND_EXPR:
17649 op = DW_OP_and;
17650 goto do_binop;
17652 case TRUTH_XOR_EXPR:
17653 case BIT_XOR_EXPR:
17654 op = DW_OP_xor;
17655 goto do_binop;
17657 case TRUTH_OR_EXPR:
17658 case TRUTH_ORIF_EXPR:
17659 case BIT_IOR_EXPR:
17660 op = DW_OP_or;
17661 goto do_binop;
17663 case FLOOR_DIV_EXPR:
17664 case CEIL_DIV_EXPR:
17665 case ROUND_DIV_EXPR:
17666 case TRUNC_DIV_EXPR:
17667 case EXACT_DIV_EXPR:
17668 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
17669 return 0;
17670 op = DW_OP_div;
17671 goto do_binop;
17673 case MINUS_EXPR:
17674 op = DW_OP_minus;
17675 goto do_binop;
17677 case FLOOR_MOD_EXPR:
17678 case CEIL_MOD_EXPR:
17679 case ROUND_MOD_EXPR:
17680 case TRUNC_MOD_EXPR:
17681 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
17683 op = DW_OP_mod;
17684 goto do_binop;
17686 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17687 list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
17688 if (list_ret == 0 || list_ret1 == 0)
17689 return 0;
17691 add_loc_list (&list_ret, list_ret1);
17692 if (list_ret == 0)
17693 return 0;
17694 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
17695 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
17696 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
17697 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
17698 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
17699 break;
17701 case MULT_EXPR:
17702 op = DW_OP_mul;
17703 goto do_binop;
17705 case LSHIFT_EXPR:
17706 op = DW_OP_shl;
17707 goto do_binop;
17709 case RSHIFT_EXPR:
17710 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
17711 goto do_binop;
17713 case POINTER_PLUS_EXPR:
17714 case PLUS_EXPR:
17715 do_plus:
17716 if (tree_fits_shwi_p (TREE_OPERAND (loc, 1)))
17718 /* Big unsigned numbers can fit in HOST_WIDE_INT but it may be
17719 smarter to encode their opposite. The DW_OP_plus_uconst operation
17720 takes 1 + X bytes, X being the size of the ULEB128 addend. On the
17721 other hand, a "<push literal>; DW_OP_minus" pattern takes 1 + Y
17722 bytes, Y being the size of the operation that pushes the opposite
17723 of the addend. So let's choose the smallest representation. */
17724 const tree tree_addend = TREE_OPERAND (loc, 1);
17725 offset_int wi_addend;
17726 HOST_WIDE_INT shwi_addend;
17727 dw_loc_descr_ref loc_naddend;
17729 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17730 if (list_ret == 0)
17731 return 0;
17733 /* Try to get the literal to push. It is the opposite of the addend,
17734 so as we rely on wrapping during DWARF evaluation, first decode
17735 the literal as a "DWARF-sized" signed number. */
17736 wi_addend = wi::to_offset (tree_addend);
17737 wi_addend = wi::sext (wi_addend, DWARF2_ADDR_SIZE * 8);
17738 shwi_addend = wi_addend.to_shwi ();
17739 loc_naddend = (shwi_addend != INTTYPE_MINIMUM (HOST_WIDE_INT))
17740 ? int_loc_descriptor (-shwi_addend)
17741 : NULL;
17743 if (loc_naddend != NULL
17744 && ((unsigned) size_of_uleb128 (shwi_addend)
17745 > size_of_loc_descr (loc_naddend)))
17747 add_loc_descr_to_each (list_ret, loc_naddend);
17748 add_loc_descr_to_each (list_ret,
17749 new_loc_descr (DW_OP_minus, 0, 0));
17751 else
17753 for (dw_loc_descr_ref loc_cur = loc_naddend; loc_cur != NULL; )
17755 loc_naddend = loc_cur;
17756 loc_cur = loc_cur->dw_loc_next;
17757 ggc_free (loc_naddend);
17759 loc_list_plus_const (list_ret, wi_addend.to_shwi ());
17761 break;
17764 op = DW_OP_plus;
17765 goto do_binop;
17767 case LE_EXPR:
17768 op = DW_OP_le;
17769 goto do_comp_binop;
17771 case GE_EXPR:
17772 op = DW_OP_ge;
17773 goto do_comp_binop;
17775 case LT_EXPR:
17776 op = DW_OP_lt;
17777 goto do_comp_binop;
17779 case GT_EXPR:
17780 op = DW_OP_gt;
17781 goto do_comp_binop;
17783 do_comp_binop:
17784 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
17786 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
17787 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
17788 list_ret = loc_list_from_uint_comparison (list_ret, list_ret1,
17789 TREE_CODE (loc));
17790 break;
17792 else
17793 goto do_binop;
17795 case EQ_EXPR:
17796 op = DW_OP_eq;
17797 goto do_binop;
17799 case NE_EXPR:
17800 op = DW_OP_ne;
17801 goto do_binop;
17803 do_binop:
17804 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17805 list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
17806 if (list_ret == 0 || list_ret1 == 0)
17807 return 0;
17809 add_loc_list (&list_ret, list_ret1);
17810 if (list_ret == 0)
17811 return 0;
17812 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
17813 break;
17815 case TRUTH_NOT_EXPR:
17816 case BIT_NOT_EXPR:
17817 op = DW_OP_not;
17818 goto do_unop;
17820 case ABS_EXPR:
17821 op = DW_OP_abs;
17822 goto do_unop;
17824 case NEGATE_EXPR:
17825 op = DW_OP_neg;
17826 goto do_unop;
17828 do_unop:
17829 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17830 if (list_ret == 0)
17831 return 0;
17833 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
17834 break;
17836 case MIN_EXPR:
17837 case MAX_EXPR:
17839 const enum tree_code code =
17840 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
17842 loc = build3 (COND_EXPR, TREE_TYPE (loc),
17843 build2 (code, integer_type_node,
17844 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
17845 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
17848 /* fall through */
17850 case COND_EXPR:
17852 dw_loc_descr_ref lhs
17853 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0, context);
17854 dw_loc_list_ref rhs
17855 = loc_list_from_tree_1 (TREE_OPERAND (loc, 2), 0, context);
17856 dw_loc_descr_ref bra_node, jump_node, tmp;
17858 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17859 if (list_ret == 0 || lhs == 0 || rhs == 0)
17860 return 0;
17862 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
17863 add_loc_descr_to_each (list_ret, bra_node);
17865 add_loc_list (&list_ret, rhs);
17866 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
17867 add_loc_descr_to_each (list_ret, jump_node);
17869 add_loc_descr_to_each (list_ret, lhs);
17870 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
17871 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
17873 /* ??? Need a node to point the skip at. Use a nop. */
17874 tmp = new_loc_descr (DW_OP_nop, 0, 0);
17875 add_loc_descr_to_each (list_ret, tmp);
17876 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
17877 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
17879 break;
17881 case FIX_TRUNC_EXPR:
17882 return 0;
17884 default:
17885 /* Leave front-end specific codes as simply unknown. This comes
17886 up, for instance, with the C STMT_EXPR. */
17887 if ((unsigned int) TREE_CODE (loc)
17888 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
17890 expansion_failed (loc, NULL_RTX,
17891 "language specific tree node");
17892 return 0;
17895 /* Otherwise this is a generic code; we should just lists all of
17896 these explicitly. We forgot one. */
17897 if (flag_checking)
17898 gcc_unreachable ();
17900 /* In a release build, we want to degrade gracefully: better to
17901 generate incomplete debugging information than to crash. */
17902 return NULL;
17905 if (!ret && !list_ret)
17906 return 0;
17908 if (want_address == 2 && !have_address
17909 && (dwarf_version >= 4 || !dwarf_strict))
17911 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
17913 expansion_failed (loc, NULL_RTX,
17914 "DWARF address size mismatch");
17915 return 0;
17917 if (ret)
17918 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
17919 else
17920 add_loc_descr_to_each (list_ret,
17921 new_loc_descr (DW_OP_stack_value, 0, 0));
17922 have_address = 1;
17924 /* Show if we can't fill the request for an address. */
17925 if (want_address && !have_address)
17927 expansion_failed (loc, NULL_RTX,
17928 "Want address and only have value");
17929 return 0;
17932 gcc_assert (!ret || !list_ret);
17934 /* If we've got an address and don't want one, dereference. */
17935 if (!want_address && have_address)
17937 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
17939 if (size > DWARF2_ADDR_SIZE || size == -1)
17941 expansion_failed (loc, NULL_RTX,
17942 "DWARF address size mismatch");
17943 return 0;
17945 else if (size == DWARF2_ADDR_SIZE)
17946 op = DW_OP_deref;
17947 else
17948 op = DW_OP_deref_size;
17950 if (ret)
17951 add_loc_descr (&ret, new_loc_descr (op, size, 0));
17952 else
17953 add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
17955 if (ret)
17956 list_ret = new_loc_list (ret, NULL, NULL, NULL);
17958 return list_ret;
17961 /* Likewise, but strip useless DW_OP_nop operations in the resulting
17962 expressions. */
17964 static dw_loc_list_ref
17965 loc_list_from_tree (tree loc, int want_address,
17966 struct loc_descr_context *context)
17968 dw_loc_list_ref result = loc_list_from_tree_1 (loc, want_address, context);
17970 for (dw_loc_list_ref loc_cur = result;
17971 loc_cur != NULL; loc_cur = loc_cur->dw_loc_next)
17972 loc_descr_without_nops (loc_cur->expr);
17973 return result;
17976 /* Same as above but return only single location expression. */
17977 static dw_loc_descr_ref
17978 loc_descriptor_from_tree (tree loc, int want_address,
17979 struct loc_descr_context *context)
17981 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address, context);
17982 if (!ret)
17983 return NULL;
17984 if (ret->dw_loc_next)
17986 expansion_failed (loc, NULL_RTX,
17987 "Location list where only loc descriptor needed");
17988 return NULL;
17990 return ret->expr;
17993 /* Given a value, round it up to the lowest multiple of `boundary'
17994 which is not less than the value itself. */
17996 static inline HOST_WIDE_INT
17997 ceiling (HOST_WIDE_INT value, unsigned int boundary)
17999 return (((value + boundary - 1) / boundary) * boundary);
18002 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
18003 pointer to the declared type for the relevant field variable, or return
18004 `integer_type_node' if the given node turns out to be an
18005 ERROR_MARK node. */
18007 static inline tree
18008 field_type (const_tree decl)
18010 tree type;
18012 if (TREE_CODE (decl) == ERROR_MARK)
18013 return integer_type_node;
18015 type = DECL_BIT_FIELD_TYPE (decl);
18016 if (type == NULL_TREE)
18017 type = TREE_TYPE (decl);
18019 return type;
18022 /* Given a pointer to a tree node, return the alignment in bits for
18023 it, or else return BITS_PER_WORD if the node actually turns out to
18024 be an ERROR_MARK node. */
18026 static inline unsigned
18027 simple_type_align_in_bits (const_tree type)
18029 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
18032 static inline unsigned
18033 simple_decl_align_in_bits (const_tree decl)
18035 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
18038 /* Return the result of rounding T up to ALIGN. */
18040 static inline offset_int
18041 round_up_to_align (const offset_int &t, unsigned int align)
18043 return wi::udiv_trunc (t + align - 1, align) * align;
18046 /* Compute the size of TYPE in bytes. If possible, return NULL and store the
18047 size as an integer constant in CST_SIZE. Otherwise, if possible, return a
18048 DWARF expression that computes the size. Return NULL and set CST_SIZE to -1
18049 if we fail to return the size in one of these two forms. */
18051 static dw_loc_descr_ref
18052 type_byte_size (const_tree type, HOST_WIDE_INT *cst_size)
18054 tree tree_size;
18055 struct loc_descr_context ctx;
18057 /* Return a constant integer in priority, if possible. */
18058 *cst_size = int_size_in_bytes (type);
18059 if (*cst_size != -1)
18060 return NULL;
18062 ctx.context_type = const_cast<tree> (type);
18063 ctx.base_decl = NULL_TREE;
18064 ctx.dpi = NULL;
18065 ctx.placeholder_arg = false;
18066 ctx.placeholder_seen = false;
18068 type = TYPE_MAIN_VARIANT (type);
18069 tree_size = TYPE_SIZE_UNIT (type);
18070 return ((tree_size != NULL_TREE)
18071 ? loc_descriptor_from_tree (tree_size, 0, &ctx)
18072 : NULL);
18075 /* Helper structure for RECORD_TYPE processing. */
18076 struct vlr_context
18078 /* Root RECORD_TYPE. It is needed to generate data member location
18079 descriptions in variable-length records (VLR), but also to cope with
18080 variants, which are composed of nested structures multiplexed with
18081 QUAL_UNION_TYPE nodes. Each time such a structure is passed to a
18082 function processing a FIELD_DECL, it is required to be non null. */
18083 tree struct_type;
18084 /* When generating a variant part in a RECORD_TYPE (i.e. a nested
18085 QUAL_UNION_TYPE), this holds an expression that computes the offset for
18086 this variant part as part of the root record (in storage units). For
18087 regular records, it must be NULL_TREE. */
18088 tree variant_part_offset;
18091 /* Given a pointer to a FIELD_DECL, compute the byte offset of the lowest
18092 addressed byte of the "containing object" for the given FIELD_DECL. If
18093 possible, return a native constant through CST_OFFSET (in which case NULL is
18094 returned); otherwise return a DWARF expression that computes the offset.
18096 Set *CST_OFFSET to 0 and return NULL if we are unable to determine what
18097 that offset is, either because the argument turns out to be a pointer to an
18098 ERROR_MARK node, or because the offset expression is too complex for us.
18100 CTX is required: see the comment for VLR_CONTEXT. */
18102 static dw_loc_descr_ref
18103 field_byte_offset (const_tree decl, struct vlr_context *ctx,
18104 HOST_WIDE_INT *cst_offset)
18106 tree tree_result;
18107 dw_loc_list_ref loc_result;
18109 *cst_offset = 0;
18111 if (TREE_CODE (decl) == ERROR_MARK)
18112 return NULL;
18113 else
18114 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
18116 /* We cannot handle variable bit offsets at the moment, so abort if it's the
18117 case. */
18118 if (TREE_CODE (DECL_FIELD_BIT_OFFSET (decl)) != INTEGER_CST)
18119 return NULL;
18121 #ifdef PCC_BITFIELD_TYPE_MATTERS
18122 /* We used to handle only constant offsets in all cases. Now, we handle
18123 properly dynamic byte offsets only when PCC bitfield type doesn't
18124 matter. */
18125 if (PCC_BITFIELD_TYPE_MATTERS
18126 && TREE_CODE (DECL_FIELD_OFFSET (decl)) == INTEGER_CST)
18128 offset_int object_offset_in_bits;
18129 offset_int object_offset_in_bytes;
18130 offset_int bitpos_int;
18131 tree type;
18132 tree field_size_tree;
18133 offset_int deepest_bitpos;
18134 offset_int field_size_in_bits;
18135 unsigned int type_align_in_bits;
18136 unsigned int decl_align_in_bits;
18137 offset_int type_size_in_bits;
18139 bitpos_int = wi::to_offset (bit_position (decl));
18140 type = field_type (decl);
18141 type_size_in_bits = offset_int_type_size_in_bits (type);
18142 type_align_in_bits = simple_type_align_in_bits (type);
18144 field_size_tree = DECL_SIZE (decl);
18146 /* The size could be unspecified if there was an error, or for
18147 a flexible array member. */
18148 if (!field_size_tree)
18149 field_size_tree = bitsize_zero_node;
18151 /* If the size of the field is not constant, use the type size. */
18152 if (TREE_CODE (field_size_tree) == INTEGER_CST)
18153 field_size_in_bits = wi::to_offset (field_size_tree);
18154 else
18155 field_size_in_bits = type_size_in_bits;
18157 decl_align_in_bits = simple_decl_align_in_bits (decl);
18159 /* The GCC front-end doesn't make any attempt to keep track of the
18160 starting bit offset (relative to the start of the containing
18161 structure type) of the hypothetical "containing object" for a
18162 bit-field. Thus, when computing the byte offset value for the
18163 start of the "containing object" of a bit-field, we must deduce
18164 this information on our own. This can be rather tricky to do in
18165 some cases. For example, handling the following structure type
18166 definition when compiling for an i386/i486 target (which only
18167 aligns long long's to 32-bit boundaries) can be very tricky:
18169 struct S { int field1; long long field2:31; };
18171 Fortunately, there is a simple rule-of-thumb which can be used
18172 in such cases. When compiling for an i386/i486, GCC will
18173 allocate 8 bytes for the structure shown above. It decides to
18174 do this based upon one simple rule for bit-field allocation.
18175 GCC allocates each "containing object" for each bit-field at
18176 the first (i.e. lowest addressed) legitimate alignment boundary
18177 (based upon the required minimum alignment for the declared
18178 type of the field) which it can possibly use, subject to the
18179 condition that there is still enough available space remaining
18180 in the containing object (when allocated at the selected point)
18181 to fully accommodate all of the bits of the bit-field itself.
18183 This simple rule makes it obvious why GCC allocates 8 bytes for
18184 each object of the structure type shown above. When looking
18185 for a place to allocate the "containing object" for `field2',
18186 the compiler simply tries to allocate a 64-bit "containing
18187 object" at each successive 32-bit boundary (starting at zero)
18188 until it finds a place to allocate that 64- bit field such that
18189 at least 31 contiguous (and previously unallocated) bits remain
18190 within that selected 64 bit field. (As it turns out, for the
18191 example above, the compiler finds it is OK to allocate the
18192 "containing object" 64-bit field at bit-offset zero within the
18193 structure type.)
18195 Here we attempt to work backwards from the limited set of facts
18196 we're given, and we try to deduce from those facts, where GCC
18197 must have believed that the containing object started (within
18198 the structure type). The value we deduce is then used (by the
18199 callers of this routine) to generate DW_AT_location and
18200 DW_AT_bit_offset attributes for fields (both bit-fields and, in
18201 the case of DW_AT_location, regular fields as well). */
18203 /* Figure out the bit-distance from the start of the structure to
18204 the "deepest" bit of the bit-field. */
18205 deepest_bitpos = bitpos_int + field_size_in_bits;
18207 /* This is the tricky part. Use some fancy footwork to deduce
18208 where the lowest addressed bit of the containing object must
18209 be. */
18210 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
18212 /* Round up to type_align by default. This works best for
18213 bitfields. */
18214 object_offset_in_bits
18215 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
18217 if (wi::gtu_p (object_offset_in_bits, bitpos_int))
18219 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
18221 /* Round up to decl_align instead. */
18222 object_offset_in_bits
18223 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
18226 object_offset_in_bytes
18227 = wi::lrshift (object_offset_in_bits, LOG2_BITS_PER_UNIT);
18228 if (ctx->variant_part_offset == NULL_TREE)
18230 *cst_offset = object_offset_in_bytes.to_shwi ();
18231 return NULL;
18233 tree_result = wide_int_to_tree (sizetype, object_offset_in_bytes);
18235 else
18236 #endif /* PCC_BITFIELD_TYPE_MATTERS */
18237 tree_result = byte_position (decl);
18239 if (ctx->variant_part_offset != NULL_TREE)
18240 tree_result = fold_build2 (PLUS_EXPR, TREE_TYPE (tree_result),
18241 ctx->variant_part_offset, tree_result);
18243 /* If the byte offset is a constant, it's simplier to handle a native
18244 constant rather than a DWARF expression. */
18245 if (TREE_CODE (tree_result) == INTEGER_CST)
18247 *cst_offset = wi::to_offset (tree_result).to_shwi ();
18248 return NULL;
18250 struct loc_descr_context loc_ctx = {
18251 ctx->struct_type, /* context_type */
18252 NULL_TREE, /* base_decl */
18253 NULL, /* dpi */
18254 false, /* placeholder_arg */
18255 false /* placeholder_seen */
18257 loc_result = loc_list_from_tree (tree_result, 0, &loc_ctx);
18259 /* We want a DWARF expression: abort if we only have a location list with
18260 multiple elements. */
18261 if (!loc_result || !single_element_loc_list_p (loc_result))
18262 return NULL;
18263 else
18264 return loc_result->expr;
18267 /* The following routines define various Dwarf attributes and any data
18268 associated with them. */
18270 /* Add a location description attribute value to a DIE.
18272 This emits location attributes suitable for whole variables and
18273 whole parameters. Note that the location attributes for struct fields are
18274 generated by the routine `data_member_location_attribute' below. */
18276 static inline void
18277 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
18278 dw_loc_list_ref descr)
18280 if (descr == 0)
18281 return;
18282 if (single_element_loc_list_p (descr))
18283 add_AT_loc (die, attr_kind, descr->expr);
18284 else
18285 add_AT_loc_list (die, attr_kind, descr);
18288 /* Add DW_AT_accessibility attribute to DIE if needed. */
18290 static void
18291 add_accessibility_attribute (dw_die_ref die, tree decl)
18293 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
18294 children, otherwise the default is DW_ACCESS_public. In DWARF2
18295 the default has always been DW_ACCESS_public. */
18296 if (TREE_PROTECTED (decl))
18297 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
18298 else if (TREE_PRIVATE (decl))
18300 if (dwarf_version == 2
18301 || die->die_parent == NULL
18302 || die->die_parent->die_tag != DW_TAG_class_type)
18303 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
18305 else if (dwarf_version > 2
18306 && die->die_parent
18307 && die->die_parent->die_tag == DW_TAG_class_type)
18308 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
18311 /* Attach the specialized form of location attribute used for data members of
18312 struct and union types. In the special case of a FIELD_DECL node which
18313 represents a bit-field, the "offset" part of this special location
18314 descriptor must indicate the distance in bytes from the lowest-addressed
18315 byte of the containing struct or union type to the lowest-addressed byte of
18316 the "containing object" for the bit-field. (See the `field_byte_offset'
18317 function above).
18319 For any given bit-field, the "containing object" is a hypothetical object
18320 (of some integral or enum type) within which the given bit-field lives. The
18321 type of this hypothetical "containing object" is always the same as the
18322 declared type of the individual bit-field itself (for GCC anyway... the
18323 DWARF spec doesn't actually mandate this). Note that it is the size (in
18324 bytes) of the hypothetical "containing object" which will be given in the
18325 DW_AT_byte_size attribute for this bit-field. (See the
18326 `byte_size_attribute' function below.) It is also used when calculating the
18327 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
18328 function below.)
18330 CTX is required: see the comment for VLR_CONTEXT. */
18332 static void
18333 add_data_member_location_attribute (dw_die_ref die,
18334 tree decl,
18335 struct vlr_context *ctx)
18337 HOST_WIDE_INT offset;
18338 dw_loc_descr_ref loc_descr = 0;
18340 if (TREE_CODE (decl) == TREE_BINFO)
18342 /* We're working on the TAG_inheritance for a base class. */
18343 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
18345 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
18346 aren't at a fixed offset from all (sub)objects of the same
18347 type. We need to extract the appropriate offset from our
18348 vtable. The following dwarf expression means
18350 BaseAddr = ObAddr + *((*ObAddr) - Offset)
18352 This is specific to the V3 ABI, of course. */
18354 dw_loc_descr_ref tmp;
18356 /* Make a copy of the object address. */
18357 tmp = new_loc_descr (DW_OP_dup, 0, 0);
18358 add_loc_descr (&loc_descr, tmp);
18360 /* Extract the vtable address. */
18361 tmp = new_loc_descr (DW_OP_deref, 0, 0);
18362 add_loc_descr (&loc_descr, tmp);
18364 /* Calculate the address of the offset. */
18365 offset = tree_to_shwi (BINFO_VPTR_FIELD (decl));
18366 gcc_assert (offset < 0);
18368 tmp = int_loc_descriptor (-offset);
18369 add_loc_descr (&loc_descr, tmp);
18370 tmp = new_loc_descr (DW_OP_minus, 0, 0);
18371 add_loc_descr (&loc_descr, tmp);
18373 /* Extract the offset. */
18374 tmp = new_loc_descr (DW_OP_deref, 0, 0);
18375 add_loc_descr (&loc_descr, tmp);
18377 /* Add it to the object address. */
18378 tmp = new_loc_descr (DW_OP_plus, 0, 0);
18379 add_loc_descr (&loc_descr, tmp);
18381 else
18382 offset = tree_to_shwi (BINFO_OFFSET (decl));
18384 else
18386 loc_descr = field_byte_offset (decl, ctx, &offset);
18388 /* If loc_descr is available then we know the field offset is dynamic.
18389 However, GDB does not handle dynamic field offsets very well at the
18390 moment. */
18391 if (loc_descr != NULL && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL)
18393 loc_descr = NULL;
18394 offset = 0;
18397 /* Data member location evalutation starts with the base address on the
18398 stack. Compute the field offset and add it to this base address. */
18399 else if (loc_descr != NULL)
18400 add_loc_descr (&loc_descr, new_loc_descr (DW_OP_plus, 0, 0));
18403 if (! loc_descr)
18405 /* While DW_AT_data_bit_offset has been added already in DWARF4,
18406 e.g. GDB only added support to it in November 2016. For DWARF5
18407 we need newer debug info consumers anyway. We might change this
18408 to dwarf_version >= 4 once most consumers catched up. */
18409 if (dwarf_version >= 5
18410 && TREE_CODE (decl) == FIELD_DECL
18411 && DECL_BIT_FIELD_TYPE (decl))
18413 tree off = bit_position (decl);
18414 if (tree_fits_uhwi_p (off) && get_AT (die, DW_AT_bit_size))
18416 remove_AT (die, DW_AT_byte_size);
18417 remove_AT (die, DW_AT_bit_offset);
18418 add_AT_unsigned (die, DW_AT_data_bit_offset, tree_to_uhwi (off));
18419 return;
18422 if (dwarf_version > 2)
18424 /* Don't need to output a location expression, just the constant. */
18425 if (offset < 0)
18426 add_AT_int (die, DW_AT_data_member_location, offset);
18427 else
18428 add_AT_unsigned (die, DW_AT_data_member_location, offset);
18429 return;
18431 else
18433 enum dwarf_location_atom op;
18435 /* The DWARF2 standard says that we should assume that the structure
18436 address is already on the stack, so we can specify a structure
18437 field address by using DW_OP_plus_uconst. */
18438 op = DW_OP_plus_uconst;
18439 loc_descr = new_loc_descr (op, offset, 0);
18443 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
18446 /* Writes integer values to dw_vec_const array. */
18448 static void
18449 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
18451 while (size != 0)
18453 *dest++ = val & 0xff;
18454 val >>= 8;
18455 --size;
18459 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
18461 static HOST_WIDE_INT
18462 extract_int (const unsigned char *src, unsigned int size)
18464 HOST_WIDE_INT val = 0;
18466 src += size;
18467 while (size != 0)
18469 val <<= 8;
18470 val |= *--src & 0xff;
18471 --size;
18473 return val;
18476 /* Writes wide_int values to dw_vec_const array. */
18478 static void
18479 insert_wide_int (const wide_int &val, unsigned char *dest, int elt_size)
18481 int i;
18483 if (elt_size <= HOST_BITS_PER_WIDE_INT/BITS_PER_UNIT)
18485 insert_int ((HOST_WIDE_INT) val.elt (0), elt_size, dest);
18486 return;
18489 /* We'd have to extend this code to support odd sizes. */
18490 gcc_assert (elt_size % (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT) == 0);
18492 int n = elt_size / (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
18494 if (WORDS_BIG_ENDIAN)
18495 for (i = n - 1; i >= 0; i--)
18497 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
18498 dest += sizeof (HOST_WIDE_INT);
18500 else
18501 for (i = 0; i < n; i++)
18503 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
18504 dest += sizeof (HOST_WIDE_INT);
18508 /* Writes floating point values to dw_vec_const array. */
18510 static void
18511 insert_float (const_rtx rtl, unsigned char *array)
18513 long val[4];
18514 int i;
18516 real_to_target (val, CONST_DOUBLE_REAL_VALUE (rtl), GET_MODE (rtl));
18518 /* real_to_target puts 32-bit pieces in each long. Pack them. */
18519 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
18521 insert_int (val[i], 4, array);
18522 array += 4;
18526 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
18527 does not have a "location" either in memory or in a register. These
18528 things can arise in GNU C when a constant is passed as an actual parameter
18529 to an inlined function. They can also arise in C++ where declared
18530 constants do not necessarily get memory "homes". */
18532 static bool
18533 add_const_value_attribute (dw_die_ref die, rtx rtl)
18535 switch (GET_CODE (rtl))
18537 case CONST_INT:
18539 HOST_WIDE_INT val = INTVAL (rtl);
18541 if (val < 0)
18542 add_AT_int (die, DW_AT_const_value, val);
18543 else
18544 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
18546 return true;
18548 case CONST_WIDE_INT:
18550 wide_int w1 = rtx_mode_t (rtl, MAX_MODE_INT);
18551 unsigned int prec = MIN (wi::min_precision (w1, UNSIGNED),
18552 (unsigned int)CONST_WIDE_INT_NUNITS (rtl) * HOST_BITS_PER_WIDE_INT);
18553 wide_int w = wi::zext (w1, prec);
18554 add_AT_wide (die, DW_AT_const_value, w);
18556 return true;
18558 case CONST_DOUBLE:
18559 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
18560 floating-point constant. A CONST_DOUBLE is used whenever the
18561 constant requires more than one word in order to be adequately
18562 represented. */
18564 machine_mode mode = GET_MODE (rtl);
18566 if (TARGET_SUPPORTS_WIDE_INT == 0 && !SCALAR_FLOAT_MODE_P (mode))
18567 add_AT_double (die, DW_AT_const_value,
18568 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
18569 else
18571 unsigned int length = GET_MODE_SIZE (mode);
18572 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
18574 insert_float (rtl, array);
18575 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
18578 return true;
18580 case CONST_VECTOR:
18582 machine_mode mode = GET_MODE (rtl);
18583 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
18584 unsigned int length = CONST_VECTOR_NUNITS (rtl);
18585 unsigned char *array
18586 = ggc_vec_alloc<unsigned char> (length * elt_size);
18587 unsigned int i;
18588 unsigned char *p;
18589 machine_mode imode = GET_MODE_INNER (mode);
18591 switch (GET_MODE_CLASS (mode))
18593 case MODE_VECTOR_INT:
18594 for (i = 0, p = array; i < length; i++, p += elt_size)
18596 rtx elt = CONST_VECTOR_ELT (rtl, i);
18597 insert_wide_int (rtx_mode_t (elt, imode), p, elt_size);
18599 break;
18601 case MODE_VECTOR_FLOAT:
18602 for (i = 0, p = array; i < length; i++, p += elt_size)
18604 rtx elt = CONST_VECTOR_ELT (rtl, i);
18605 insert_float (elt, p);
18607 break;
18609 default:
18610 gcc_unreachable ();
18613 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
18615 return true;
18617 case CONST_STRING:
18618 if (dwarf_version >= 4 || !dwarf_strict)
18620 dw_loc_descr_ref loc_result;
18621 resolve_one_addr (&rtl);
18622 rtl_addr:
18623 loc_result = new_addr_loc_descr (rtl, dtprel_false);
18624 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
18625 add_AT_loc (die, DW_AT_location, loc_result);
18626 vec_safe_push (used_rtx_array, rtl);
18627 return true;
18629 return false;
18631 case CONST:
18632 if (CONSTANT_P (XEXP (rtl, 0)))
18633 return add_const_value_attribute (die, XEXP (rtl, 0));
18634 /* FALLTHROUGH */
18635 case SYMBOL_REF:
18636 if (!const_ok_for_output (rtl))
18637 return false;
18638 /* FALLTHROUGH */
18639 case LABEL_REF:
18640 if (dwarf_version >= 4 || !dwarf_strict)
18641 goto rtl_addr;
18642 return false;
18644 case PLUS:
18645 /* In cases where an inlined instance of an inline function is passed
18646 the address of an `auto' variable (which is local to the caller) we
18647 can get a situation where the DECL_RTL of the artificial local
18648 variable (for the inlining) which acts as a stand-in for the
18649 corresponding formal parameter (of the inline function) will look
18650 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
18651 exactly a compile-time constant expression, but it isn't the address
18652 of the (artificial) local variable either. Rather, it represents the
18653 *value* which the artificial local variable always has during its
18654 lifetime. We currently have no way to represent such quasi-constant
18655 values in Dwarf, so for now we just punt and generate nothing. */
18656 return false;
18658 case HIGH:
18659 case CONST_FIXED:
18660 return false;
18662 case MEM:
18663 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
18664 && MEM_READONLY_P (rtl)
18665 && GET_MODE (rtl) == BLKmode)
18667 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
18668 return true;
18670 return false;
18672 default:
18673 /* No other kinds of rtx should be possible here. */
18674 gcc_unreachable ();
18676 return false;
18679 /* Determine whether the evaluation of EXPR references any variables
18680 or functions which aren't otherwise used (and therefore may not be
18681 output). */
18682 static tree
18683 reference_to_unused (tree * tp, int * walk_subtrees,
18684 void * data ATTRIBUTE_UNUSED)
18686 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
18687 *walk_subtrees = 0;
18689 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
18690 && ! TREE_ASM_WRITTEN (*tp))
18691 return *tp;
18692 /* ??? The C++ FE emits debug information for using decls, so
18693 putting gcc_unreachable here falls over. See PR31899. For now
18694 be conservative. */
18695 else if (!symtab->global_info_ready && VAR_OR_FUNCTION_DECL_P (*tp))
18696 return *tp;
18697 else if (VAR_P (*tp))
18699 varpool_node *node = varpool_node::get (*tp);
18700 if (!node || !node->definition)
18701 return *tp;
18703 else if (TREE_CODE (*tp) == FUNCTION_DECL
18704 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
18706 /* The call graph machinery must have finished analyzing,
18707 optimizing and gimplifying the CU by now.
18708 So if *TP has no call graph node associated
18709 to it, it means *TP will not be emitted. */
18710 if (!cgraph_node::get (*tp))
18711 return *tp;
18713 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
18714 return *tp;
18716 return NULL_TREE;
18719 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
18720 for use in a later add_const_value_attribute call. */
18722 static rtx
18723 rtl_for_decl_init (tree init, tree type)
18725 rtx rtl = NULL_RTX;
18727 STRIP_NOPS (init);
18729 /* If a variable is initialized with a string constant without embedded
18730 zeros, build CONST_STRING. */
18731 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
18733 tree enttype = TREE_TYPE (type);
18734 tree domain = TYPE_DOMAIN (type);
18735 machine_mode mode = TYPE_MODE (enttype);
18737 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
18738 && domain
18739 && integer_zerop (TYPE_MIN_VALUE (domain))
18740 && compare_tree_int (TYPE_MAX_VALUE (domain),
18741 TREE_STRING_LENGTH (init) - 1) == 0
18742 && ((size_t) TREE_STRING_LENGTH (init)
18743 == strlen (TREE_STRING_POINTER (init)) + 1))
18745 rtl = gen_rtx_CONST_STRING (VOIDmode,
18746 ggc_strdup (TREE_STRING_POINTER (init)));
18747 rtl = gen_rtx_MEM (BLKmode, rtl);
18748 MEM_READONLY_P (rtl) = 1;
18751 /* Other aggregates, and complex values, could be represented using
18752 CONCAT: FIXME! */
18753 else if (AGGREGATE_TYPE_P (type)
18754 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
18755 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
18756 || TREE_CODE (type) == COMPLEX_TYPE)
18758 /* Vectors only work if their mode is supported by the target.
18759 FIXME: generic vectors ought to work too. */
18760 else if (TREE_CODE (type) == VECTOR_TYPE
18761 && !VECTOR_MODE_P (TYPE_MODE (type)))
18763 /* If the initializer is something that we know will expand into an
18764 immediate RTL constant, expand it now. We must be careful not to
18765 reference variables which won't be output. */
18766 else if (initializer_constant_valid_p (init, type)
18767 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
18769 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
18770 possible. */
18771 if (TREE_CODE (type) == VECTOR_TYPE)
18772 switch (TREE_CODE (init))
18774 case VECTOR_CST:
18775 break;
18776 case CONSTRUCTOR:
18777 if (TREE_CONSTANT (init))
18779 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
18780 bool constant_p = true;
18781 tree value;
18782 unsigned HOST_WIDE_INT ix;
18784 /* Even when ctor is constant, it might contain non-*_CST
18785 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
18786 belong into VECTOR_CST nodes. */
18787 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
18788 if (!CONSTANT_CLASS_P (value))
18790 constant_p = false;
18791 break;
18794 if (constant_p)
18796 init = build_vector_from_ctor (type, elts);
18797 break;
18800 /* FALLTHRU */
18802 default:
18803 return NULL;
18806 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
18808 /* If expand_expr returns a MEM, it wasn't immediate. */
18809 gcc_assert (!rtl || !MEM_P (rtl));
18812 return rtl;
18815 /* Generate RTL for the variable DECL to represent its location. */
18817 static rtx
18818 rtl_for_decl_location (tree decl)
18820 rtx rtl;
18822 /* Here we have to decide where we are going to say the parameter "lives"
18823 (as far as the debugger is concerned). We only have a couple of
18824 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
18826 DECL_RTL normally indicates where the parameter lives during most of the
18827 activation of the function. If optimization is enabled however, this
18828 could be either NULL or else a pseudo-reg. Both of those cases indicate
18829 that the parameter doesn't really live anywhere (as far as the code
18830 generation parts of GCC are concerned) during most of the function's
18831 activation. That will happen (for example) if the parameter is never
18832 referenced within the function.
18834 We could just generate a location descriptor here for all non-NULL
18835 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
18836 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
18837 where DECL_RTL is NULL or is a pseudo-reg.
18839 Note however that we can only get away with using DECL_INCOMING_RTL as
18840 a backup substitute for DECL_RTL in certain limited cases. In cases
18841 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
18842 we can be sure that the parameter was passed using the same type as it is
18843 declared to have within the function, and that its DECL_INCOMING_RTL
18844 points us to a place where a value of that type is passed.
18846 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
18847 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
18848 because in these cases DECL_INCOMING_RTL points us to a value of some
18849 type which is *different* from the type of the parameter itself. Thus,
18850 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
18851 such cases, the debugger would end up (for example) trying to fetch a
18852 `float' from a place which actually contains the first part of a
18853 `double'. That would lead to really incorrect and confusing
18854 output at debug-time.
18856 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
18857 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
18858 are a couple of exceptions however. On little-endian machines we can
18859 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
18860 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
18861 an integral type that is smaller than TREE_TYPE (decl). These cases arise
18862 when (on a little-endian machine) a non-prototyped function has a
18863 parameter declared to be of type `short' or `char'. In such cases,
18864 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
18865 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
18866 passed `int' value. If the debugger then uses that address to fetch
18867 a `short' or a `char' (on a little-endian machine) the result will be
18868 the correct data, so we allow for such exceptional cases below.
18870 Note that our goal here is to describe the place where the given formal
18871 parameter lives during most of the function's activation (i.e. between the
18872 end of the prologue and the start of the epilogue). We'll do that as best
18873 as we can. Note however that if the given formal parameter is modified
18874 sometime during the execution of the function, then a stack backtrace (at
18875 debug-time) will show the function as having been called with the *new*
18876 value rather than the value which was originally passed in. This happens
18877 rarely enough that it is not a major problem, but it *is* a problem, and
18878 I'd like to fix it.
18880 A future version of dwarf2out.c may generate two additional attributes for
18881 any given DW_TAG_formal_parameter DIE which will describe the "passed
18882 type" and the "passed location" for the given formal parameter in addition
18883 to the attributes we now generate to indicate the "declared type" and the
18884 "active location" for each parameter. This additional set of attributes
18885 could be used by debuggers for stack backtraces. Separately, note that
18886 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
18887 This happens (for example) for inlined-instances of inline function formal
18888 parameters which are never referenced. This really shouldn't be
18889 happening. All PARM_DECL nodes should get valid non-NULL
18890 DECL_INCOMING_RTL values. FIXME. */
18892 /* Use DECL_RTL as the "location" unless we find something better. */
18893 rtl = DECL_RTL_IF_SET (decl);
18895 /* When generating abstract instances, ignore everything except
18896 constants, symbols living in memory, and symbols living in
18897 fixed registers. */
18898 if (! reload_completed)
18900 if (rtl
18901 && (CONSTANT_P (rtl)
18902 || (MEM_P (rtl)
18903 && CONSTANT_P (XEXP (rtl, 0)))
18904 || (REG_P (rtl)
18905 && VAR_P (decl)
18906 && TREE_STATIC (decl))))
18908 rtl = targetm.delegitimize_address (rtl);
18909 return rtl;
18911 rtl = NULL_RTX;
18913 else if (TREE_CODE (decl) == PARM_DECL)
18915 if (rtl == NULL_RTX
18916 || is_pseudo_reg (rtl)
18917 || (MEM_P (rtl)
18918 && is_pseudo_reg (XEXP (rtl, 0))
18919 && DECL_INCOMING_RTL (decl)
18920 && MEM_P (DECL_INCOMING_RTL (decl))
18921 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
18923 tree declared_type = TREE_TYPE (decl);
18924 tree passed_type = DECL_ARG_TYPE (decl);
18925 machine_mode dmode = TYPE_MODE (declared_type);
18926 machine_mode pmode = TYPE_MODE (passed_type);
18928 /* This decl represents a formal parameter which was optimized out.
18929 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
18930 all cases where (rtl == NULL_RTX) just below. */
18931 if (dmode == pmode)
18932 rtl = DECL_INCOMING_RTL (decl);
18933 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
18934 && SCALAR_INT_MODE_P (dmode)
18935 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
18936 && DECL_INCOMING_RTL (decl))
18938 rtx inc = DECL_INCOMING_RTL (decl);
18939 if (REG_P (inc))
18940 rtl = inc;
18941 else if (MEM_P (inc))
18943 if (BYTES_BIG_ENDIAN)
18944 rtl = adjust_address_nv (inc, dmode,
18945 GET_MODE_SIZE (pmode)
18946 - GET_MODE_SIZE (dmode));
18947 else
18948 rtl = inc;
18953 /* If the parm was passed in registers, but lives on the stack, then
18954 make a big endian correction if the mode of the type of the
18955 parameter is not the same as the mode of the rtl. */
18956 /* ??? This is the same series of checks that are made in dbxout.c before
18957 we reach the big endian correction code there. It isn't clear if all
18958 of these checks are necessary here, but keeping them all is the safe
18959 thing to do. */
18960 else if (MEM_P (rtl)
18961 && XEXP (rtl, 0) != const0_rtx
18962 && ! CONSTANT_P (XEXP (rtl, 0))
18963 /* Not passed in memory. */
18964 && !MEM_P (DECL_INCOMING_RTL (decl))
18965 /* Not passed by invisible reference. */
18966 && (!REG_P (XEXP (rtl, 0))
18967 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
18968 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
18969 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
18970 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
18971 #endif
18973 /* Big endian correction check. */
18974 && BYTES_BIG_ENDIAN
18975 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
18976 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
18977 < UNITS_PER_WORD))
18979 machine_mode addr_mode = get_address_mode (rtl);
18980 int offset = (UNITS_PER_WORD
18981 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
18983 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
18984 plus_constant (addr_mode, XEXP (rtl, 0), offset));
18987 else if (VAR_P (decl)
18988 && rtl
18989 && MEM_P (rtl)
18990 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
18991 && BYTES_BIG_ENDIAN)
18993 machine_mode addr_mode = get_address_mode (rtl);
18994 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
18995 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
18997 /* If a variable is declared "register" yet is smaller than
18998 a register, then if we store the variable to memory, it
18999 looks like we're storing a register-sized value, when in
19000 fact we are not. We need to adjust the offset of the
19001 storage location to reflect the actual value's bytes,
19002 else gdb will not be able to display it. */
19003 if (rsize > dsize)
19004 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
19005 plus_constant (addr_mode, XEXP (rtl, 0),
19006 rsize - dsize));
19009 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
19010 and will have been substituted directly into all expressions that use it.
19011 C does not have such a concept, but C++ and other languages do. */
19012 if (!rtl && VAR_P (decl) && DECL_INITIAL (decl))
19013 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
19015 if (rtl)
19016 rtl = targetm.delegitimize_address (rtl);
19018 /* If we don't look past the constant pool, we risk emitting a
19019 reference to a constant pool entry that isn't referenced from
19020 code, and thus is not emitted. */
19021 if (rtl)
19022 rtl = avoid_constant_pool_reference (rtl);
19024 /* Try harder to get a rtl. If this symbol ends up not being emitted
19025 in the current CU, resolve_addr will remove the expression referencing
19026 it. */
19027 if (rtl == NULL_RTX
19028 && VAR_P (decl)
19029 && !DECL_EXTERNAL (decl)
19030 && TREE_STATIC (decl)
19031 && DECL_NAME (decl)
19032 && !DECL_HARD_REGISTER (decl)
19033 && DECL_MODE (decl) != VOIDmode)
19035 rtl = make_decl_rtl_for_debug (decl);
19036 if (!MEM_P (rtl)
19037 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
19038 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
19039 rtl = NULL_RTX;
19042 return rtl;
19045 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
19046 returned. If so, the decl for the COMMON block is returned, and the
19047 value is the offset into the common block for the symbol. */
19049 static tree
19050 fortran_common (tree decl, HOST_WIDE_INT *value)
19052 tree val_expr, cvar;
19053 machine_mode mode;
19054 HOST_WIDE_INT bitsize, bitpos;
19055 tree offset;
19056 int unsignedp, reversep, volatilep = 0;
19058 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
19059 it does not have a value (the offset into the common area), or if it
19060 is thread local (as opposed to global) then it isn't common, and shouldn't
19061 be handled as such. */
19062 if (!VAR_P (decl)
19063 || !TREE_STATIC (decl)
19064 || !DECL_HAS_VALUE_EXPR_P (decl)
19065 || !is_fortran ())
19066 return NULL_TREE;
19068 val_expr = DECL_VALUE_EXPR (decl);
19069 if (TREE_CODE (val_expr) != COMPONENT_REF)
19070 return NULL_TREE;
19072 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset, &mode,
19073 &unsignedp, &reversep, &volatilep);
19075 if (cvar == NULL_TREE
19076 || !VAR_P (cvar)
19077 || DECL_ARTIFICIAL (cvar)
19078 || !TREE_PUBLIC (cvar))
19079 return NULL_TREE;
19081 *value = 0;
19082 if (offset != NULL)
19084 if (!tree_fits_shwi_p (offset))
19085 return NULL_TREE;
19086 *value = tree_to_shwi (offset);
19088 if (bitpos != 0)
19089 *value += bitpos / BITS_PER_UNIT;
19091 return cvar;
19094 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
19095 data attribute for a variable or a parameter. We generate the
19096 DW_AT_const_value attribute only in those cases where the given variable
19097 or parameter does not have a true "location" either in memory or in a
19098 register. This can happen (for example) when a constant is passed as an
19099 actual argument in a call to an inline function. (It's possible that
19100 these things can crop up in other ways also.) Note that one type of
19101 constant value which can be passed into an inlined function is a constant
19102 pointer. This can happen for example if an actual argument in an inlined
19103 function call evaluates to a compile-time constant address.
19105 CACHE_P is true if it is worth caching the location list for DECL,
19106 so that future calls can reuse it rather than regenerate it from scratch.
19107 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
19108 since we will need to refer to them each time the function is inlined. */
19110 static bool
19111 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p)
19113 rtx rtl;
19114 dw_loc_list_ref list;
19115 var_loc_list *loc_list;
19116 cached_dw_loc_list *cache;
19118 if (early_dwarf)
19119 return false;
19121 if (TREE_CODE (decl) == ERROR_MARK)
19122 return false;
19124 if (get_AT (die, DW_AT_location)
19125 || get_AT (die, DW_AT_const_value))
19126 return true;
19128 gcc_assert (VAR_P (decl) || TREE_CODE (decl) == PARM_DECL
19129 || TREE_CODE (decl) == RESULT_DECL);
19131 /* Try to get some constant RTL for this decl, and use that as the value of
19132 the location. */
19134 rtl = rtl_for_decl_location (decl);
19135 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
19136 && add_const_value_attribute (die, rtl))
19137 return true;
19139 /* See if we have single element location list that is equivalent to
19140 a constant value. That way we are better to use add_const_value_attribute
19141 rather than expanding constant value equivalent. */
19142 loc_list = lookup_decl_loc (decl);
19143 if (loc_list
19144 && loc_list->first
19145 && loc_list->first->next == NULL
19146 && NOTE_P (loc_list->first->loc)
19147 && NOTE_VAR_LOCATION (loc_list->first->loc)
19148 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
19150 struct var_loc_node *node;
19152 node = loc_list->first;
19153 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
19154 if (GET_CODE (rtl) == EXPR_LIST)
19155 rtl = XEXP (rtl, 0);
19156 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
19157 && add_const_value_attribute (die, rtl))
19158 return true;
19160 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
19161 list several times. See if we've already cached the contents. */
19162 list = NULL;
19163 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
19164 cache_p = false;
19165 if (cache_p)
19167 cache = cached_dw_loc_list_table->find_with_hash (decl, DECL_UID (decl));
19168 if (cache)
19169 list = cache->loc_list;
19171 if (list == NULL)
19173 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2,
19174 NULL);
19175 /* It is usually worth caching this result if the decl is from
19176 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
19177 if (cache_p && list && list->dw_loc_next)
19179 cached_dw_loc_list **slot
19180 = cached_dw_loc_list_table->find_slot_with_hash (decl,
19181 DECL_UID (decl),
19182 INSERT);
19183 cache = ggc_cleared_alloc<cached_dw_loc_list> ();
19184 cache->decl_id = DECL_UID (decl);
19185 cache->loc_list = list;
19186 *slot = cache;
19189 if (list)
19191 add_AT_location_description (die, DW_AT_location, list);
19192 return true;
19194 /* None of that worked, so it must not really have a location;
19195 try adding a constant value attribute from the DECL_INITIAL. */
19196 return tree_add_const_value_attribute_for_decl (die, decl);
19199 /* Helper function for tree_add_const_value_attribute. Natively encode
19200 initializer INIT into an array. Return true if successful. */
19202 static bool
19203 native_encode_initializer (tree init, unsigned char *array, int size)
19205 tree type;
19207 if (init == NULL_TREE)
19208 return false;
19210 STRIP_NOPS (init);
19211 switch (TREE_CODE (init))
19213 case STRING_CST:
19214 type = TREE_TYPE (init);
19215 if (TREE_CODE (type) == ARRAY_TYPE)
19217 tree enttype = TREE_TYPE (type);
19218 machine_mode mode = TYPE_MODE (enttype);
19220 if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
19221 return false;
19222 if (int_size_in_bytes (type) != size)
19223 return false;
19224 if (size > TREE_STRING_LENGTH (init))
19226 memcpy (array, TREE_STRING_POINTER (init),
19227 TREE_STRING_LENGTH (init));
19228 memset (array + TREE_STRING_LENGTH (init),
19229 '\0', size - TREE_STRING_LENGTH (init));
19231 else
19232 memcpy (array, TREE_STRING_POINTER (init), size);
19233 return true;
19235 return false;
19236 case CONSTRUCTOR:
19237 type = TREE_TYPE (init);
19238 if (int_size_in_bytes (type) != size)
19239 return false;
19240 if (TREE_CODE (type) == ARRAY_TYPE)
19242 HOST_WIDE_INT min_index;
19243 unsigned HOST_WIDE_INT cnt;
19244 int curpos = 0, fieldsize;
19245 constructor_elt *ce;
19247 if (TYPE_DOMAIN (type) == NULL_TREE
19248 || !tree_fits_shwi_p (TYPE_MIN_VALUE (TYPE_DOMAIN (type))))
19249 return false;
19251 fieldsize = int_size_in_bytes (TREE_TYPE (type));
19252 if (fieldsize <= 0)
19253 return false;
19255 min_index = tree_to_shwi (TYPE_MIN_VALUE (TYPE_DOMAIN (type)));
19256 memset (array, '\0', size);
19257 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
19259 tree val = ce->value;
19260 tree index = ce->index;
19261 int pos = curpos;
19262 if (index && TREE_CODE (index) == RANGE_EXPR)
19263 pos = (tree_to_shwi (TREE_OPERAND (index, 0)) - min_index)
19264 * fieldsize;
19265 else if (index)
19266 pos = (tree_to_shwi (index) - min_index) * fieldsize;
19268 if (val)
19270 STRIP_NOPS (val);
19271 if (!native_encode_initializer (val, array + pos, fieldsize))
19272 return false;
19274 curpos = pos + fieldsize;
19275 if (index && TREE_CODE (index) == RANGE_EXPR)
19277 int count = tree_to_shwi (TREE_OPERAND (index, 1))
19278 - tree_to_shwi (TREE_OPERAND (index, 0));
19279 while (count-- > 0)
19281 if (val)
19282 memcpy (array + curpos, array + pos, fieldsize);
19283 curpos += fieldsize;
19286 gcc_assert (curpos <= size);
19288 return true;
19290 else if (TREE_CODE (type) == RECORD_TYPE
19291 || TREE_CODE (type) == UNION_TYPE)
19293 tree field = NULL_TREE;
19294 unsigned HOST_WIDE_INT cnt;
19295 constructor_elt *ce;
19297 if (int_size_in_bytes (type) != size)
19298 return false;
19300 if (TREE_CODE (type) == RECORD_TYPE)
19301 field = TYPE_FIELDS (type);
19303 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
19305 tree val = ce->value;
19306 int pos, fieldsize;
19308 if (ce->index != 0)
19309 field = ce->index;
19311 if (val)
19312 STRIP_NOPS (val);
19314 if (field == NULL_TREE || DECL_BIT_FIELD (field))
19315 return false;
19317 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
19318 && TYPE_DOMAIN (TREE_TYPE (field))
19319 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
19320 return false;
19321 else if (DECL_SIZE_UNIT (field) == NULL_TREE
19322 || !tree_fits_shwi_p (DECL_SIZE_UNIT (field)))
19323 return false;
19324 fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
19325 pos = int_byte_position (field);
19326 gcc_assert (pos + fieldsize <= size);
19327 if (val && fieldsize != 0
19328 && !native_encode_initializer (val, array + pos, fieldsize))
19329 return false;
19331 return true;
19333 return false;
19334 case VIEW_CONVERT_EXPR:
19335 case NON_LVALUE_EXPR:
19336 return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
19337 default:
19338 return native_encode_expr (init, array, size) == size;
19342 /* Attach a DW_AT_const_value attribute to DIE. The value of the
19343 attribute is the const value T. */
19345 static bool
19346 tree_add_const_value_attribute (dw_die_ref die, tree t)
19348 tree init;
19349 tree type = TREE_TYPE (t);
19350 rtx rtl;
19352 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
19353 return false;
19355 init = t;
19356 gcc_assert (!DECL_P (init));
19358 if (! early_dwarf)
19360 rtl = rtl_for_decl_init (init, type);
19361 if (rtl)
19362 return add_const_value_attribute (die, rtl);
19364 /* If the host and target are sane, try harder. */
19365 if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
19366 && initializer_constant_valid_p (init, type))
19368 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
19369 if (size > 0 && (int) size == size)
19371 unsigned char *array = ggc_cleared_vec_alloc<unsigned char> (size);
19373 if (native_encode_initializer (init, array, size))
19375 add_AT_vec (die, DW_AT_const_value, size, 1, array);
19376 return true;
19378 ggc_free (array);
19381 return false;
19384 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
19385 attribute is the const value of T, where T is an integral constant
19386 variable with static storage duration
19387 (so it can't be a PARM_DECL or a RESULT_DECL). */
19389 static bool
19390 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
19393 if (!decl
19394 || (!VAR_P (decl) && TREE_CODE (decl) != CONST_DECL)
19395 || (VAR_P (decl) && !TREE_STATIC (decl)))
19396 return false;
19398 if (TREE_READONLY (decl)
19399 && ! TREE_THIS_VOLATILE (decl)
19400 && DECL_INITIAL (decl))
19401 /* OK */;
19402 else
19403 return false;
19405 /* Don't add DW_AT_const_value if abstract origin already has one. */
19406 if (get_AT (var_die, DW_AT_const_value))
19407 return false;
19409 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
19412 /* Convert the CFI instructions for the current function into a
19413 location list. This is used for DW_AT_frame_base when we targeting
19414 a dwarf2 consumer that does not support the dwarf3
19415 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
19416 expressions. */
19418 static dw_loc_list_ref
19419 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
19421 int ix;
19422 dw_fde_ref fde;
19423 dw_loc_list_ref list, *list_tail;
19424 dw_cfi_ref cfi;
19425 dw_cfa_location last_cfa, next_cfa;
19426 const char *start_label, *last_label, *section;
19427 dw_cfa_location remember;
19429 fde = cfun->fde;
19430 gcc_assert (fde != NULL);
19432 section = secname_for_decl (current_function_decl);
19433 list_tail = &list;
19434 list = NULL;
19436 memset (&next_cfa, 0, sizeof (next_cfa));
19437 next_cfa.reg = INVALID_REGNUM;
19438 remember = next_cfa;
19440 start_label = fde->dw_fde_begin;
19442 /* ??? Bald assumption that the CIE opcode list does not contain
19443 advance opcodes. */
19444 FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
19445 lookup_cfa_1 (cfi, &next_cfa, &remember);
19447 last_cfa = next_cfa;
19448 last_label = start_label;
19450 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
19452 /* If the first partition contained no CFI adjustments, the
19453 CIE opcodes apply to the whole first partition. */
19454 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19455 fde->dw_fde_begin, fde->dw_fde_end, section);
19456 list_tail =&(*list_tail)->dw_loc_next;
19457 start_label = last_label = fde->dw_fde_second_begin;
19460 FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
19462 switch (cfi->dw_cfi_opc)
19464 case DW_CFA_set_loc:
19465 case DW_CFA_advance_loc1:
19466 case DW_CFA_advance_loc2:
19467 case DW_CFA_advance_loc4:
19468 if (!cfa_equal_p (&last_cfa, &next_cfa))
19470 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19471 start_label, last_label, section);
19473 list_tail = &(*list_tail)->dw_loc_next;
19474 last_cfa = next_cfa;
19475 start_label = last_label;
19477 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
19478 break;
19480 case DW_CFA_advance_loc:
19481 /* The encoding is complex enough that we should never emit this. */
19482 gcc_unreachable ();
19484 default:
19485 lookup_cfa_1 (cfi, &next_cfa, &remember);
19486 break;
19488 if (ix + 1 == fde->dw_fde_switch_cfi_index)
19490 if (!cfa_equal_p (&last_cfa, &next_cfa))
19492 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19493 start_label, last_label, section);
19495 list_tail = &(*list_tail)->dw_loc_next;
19496 last_cfa = next_cfa;
19497 start_label = last_label;
19499 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19500 start_label, fde->dw_fde_end, section);
19501 list_tail = &(*list_tail)->dw_loc_next;
19502 start_label = last_label = fde->dw_fde_second_begin;
19506 if (!cfa_equal_p (&last_cfa, &next_cfa))
19508 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19509 start_label, last_label, section);
19510 list_tail = &(*list_tail)->dw_loc_next;
19511 start_label = last_label;
19514 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
19515 start_label,
19516 fde->dw_fde_second_begin
19517 ? fde->dw_fde_second_end : fde->dw_fde_end,
19518 section);
19520 if (list && list->dw_loc_next)
19521 gen_llsym (list);
19523 return list;
19526 /* Compute a displacement from the "steady-state frame pointer" to the
19527 frame base (often the same as the CFA), and store it in
19528 frame_pointer_fb_offset. OFFSET is added to the displacement
19529 before the latter is negated. */
19531 static void
19532 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
19534 rtx reg, elim;
19536 #ifdef FRAME_POINTER_CFA_OFFSET
19537 reg = frame_pointer_rtx;
19538 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
19539 #else
19540 reg = arg_pointer_rtx;
19541 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
19542 #endif
19544 elim = (ira_use_lra_p
19545 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
19546 : eliminate_regs (reg, VOIDmode, NULL_RTX));
19547 if (GET_CODE (elim) == PLUS)
19549 offset += INTVAL (XEXP (elim, 1));
19550 elim = XEXP (elim, 0);
19553 frame_pointer_fb_offset = -offset;
19555 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
19556 in which to eliminate. This is because it's stack pointer isn't
19557 directly accessible as a register within the ISA. To work around
19558 this, assume that while we cannot provide a proper value for
19559 frame_pointer_fb_offset, we won't need one either. */
19560 frame_pointer_fb_offset_valid
19561 = ((SUPPORTS_STACK_ALIGNMENT
19562 && (elim == hard_frame_pointer_rtx
19563 || elim == stack_pointer_rtx))
19564 || elim == (frame_pointer_needed
19565 ? hard_frame_pointer_rtx
19566 : stack_pointer_rtx));
19569 /* Generate a DW_AT_name attribute given some string value to be included as
19570 the value of the attribute. */
19572 static void
19573 add_name_attribute (dw_die_ref die, const char *name_string)
19575 if (name_string != NULL && *name_string != 0)
19577 if (demangle_name_func)
19578 name_string = (*demangle_name_func) (name_string);
19580 add_AT_string (die, DW_AT_name, name_string);
19584 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
19585 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
19586 of TYPE accordingly.
19588 ??? This is a temporary measure until after we're able to generate
19589 regular DWARF for the complex Ada type system. */
19591 static void
19592 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
19593 dw_die_ref context_die)
19595 tree dtype;
19596 dw_die_ref dtype_die;
19598 if (!lang_hooks.types.descriptive_type)
19599 return;
19601 dtype = lang_hooks.types.descriptive_type (type);
19602 if (!dtype)
19603 return;
19605 dtype_die = lookup_type_die (dtype);
19606 if (!dtype_die)
19608 gen_type_die (dtype, context_die);
19609 dtype_die = lookup_type_die (dtype);
19610 gcc_assert (dtype_die);
19613 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
19616 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir. */
19618 static const char *
19619 comp_dir_string (void)
19621 const char *wd;
19622 char *wd1;
19623 static const char *cached_wd = NULL;
19625 if (cached_wd != NULL)
19626 return cached_wd;
19628 wd = get_src_pwd ();
19629 if (wd == NULL)
19630 return NULL;
19632 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
19634 int wdlen;
19636 wdlen = strlen (wd);
19637 wd1 = ggc_vec_alloc<char> (wdlen + 2);
19638 strcpy (wd1, wd);
19639 wd1 [wdlen] = DIR_SEPARATOR;
19640 wd1 [wdlen + 1] = 0;
19641 wd = wd1;
19644 cached_wd = remap_debug_filename (wd);
19645 return cached_wd;
19648 /* Generate a DW_AT_comp_dir attribute for DIE. */
19650 static void
19651 add_comp_dir_attribute (dw_die_ref die)
19653 const char * wd = comp_dir_string ();
19654 if (wd != NULL)
19655 add_AT_string (die, DW_AT_comp_dir, wd);
19658 /* Given a tree node VALUE describing a scalar attribute ATTR (i.e. a bound, a
19659 pointer computation, ...), output a representation for that bound according
19660 to the accepted FORMS (see enum dw_scalar_form) and add it to DIE. See
19661 loc_list_from_tree for the meaning of CONTEXT. */
19663 static void
19664 add_scalar_info (dw_die_ref die, enum dwarf_attribute attr, tree value,
19665 int forms, struct loc_descr_context *context)
19667 dw_die_ref context_die, decl_die;
19668 dw_loc_list_ref list;
19669 bool strip_conversions = true;
19670 bool placeholder_seen = false;
19672 while (strip_conversions)
19673 switch (TREE_CODE (value))
19675 case ERROR_MARK:
19676 case SAVE_EXPR:
19677 return;
19679 CASE_CONVERT:
19680 case VIEW_CONVERT_EXPR:
19681 value = TREE_OPERAND (value, 0);
19682 break;
19684 default:
19685 strip_conversions = false;
19686 break;
19689 /* If possible and permitted, output the attribute as a constant. */
19690 if ((forms & dw_scalar_form_constant) != 0
19691 && TREE_CODE (value) == INTEGER_CST)
19693 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (value));
19695 /* If HOST_WIDE_INT is big enough then represent the bound as
19696 a constant value. We need to choose a form based on
19697 whether the type is signed or unsigned. We cannot just
19698 call add_AT_unsigned if the value itself is positive
19699 (add_AT_unsigned might add the unsigned value encoded as
19700 DW_FORM_data[1248]). Some DWARF consumers will lookup the
19701 bounds type and then sign extend any unsigned values found
19702 for signed types. This is needed only for
19703 DW_AT_{lower,upper}_bound, since for most other attributes,
19704 consumers will treat DW_FORM_data[1248] as unsigned values,
19705 regardless of the underlying type. */
19706 if (prec <= HOST_BITS_PER_WIDE_INT
19707 || tree_fits_uhwi_p (value))
19709 if (TYPE_UNSIGNED (TREE_TYPE (value)))
19710 add_AT_unsigned (die, attr, TREE_INT_CST_LOW (value));
19711 else
19712 add_AT_int (die, attr, TREE_INT_CST_LOW (value));
19714 else
19715 /* Otherwise represent the bound as an unsigned value with
19716 the precision of its type. The precision and signedness
19717 of the type will be necessary to re-interpret it
19718 unambiguously. */
19719 add_AT_wide (die, attr, value);
19720 return;
19723 /* Otherwise, if it's possible and permitted too, output a reference to
19724 another DIE. */
19725 if ((forms & dw_scalar_form_reference) != 0)
19727 tree decl = NULL_TREE;
19729 /* Some type attributes reference an outer type. For instance, the upper
19730 bound of an array may reference an embedding record (this happens in
19731 Ada). */
19732 if (TREE_CODE (value) == COMPONENT_REF
19733 && TREE_CODE (TREE_OPERAND (value, 0)) == PLACEHOLDER_EXPR
19734 && TREE_CODE (TREE_OPERAND (value, 1)) == FIELD_DECL)
19735 decl = TREE_OPERAND (value, 1);
19737 else if (VAR_P (value)
19738 || TREE_CODE (value) == PARM_DECL
19739 || TREE_CODE (value) == RESULT_DECL)
19740 decl = value;
19742 if (decl != NULL_TREE)
19744 dw_die_ref decl_die = lookup_decl_die (decl);
19746 /* ??? Can this happen, or should the variable have been bound
19747 first? Probably it can, since I imagine that we try to create
19748 the types of parameters in the order in which they exist in
19749 the list, and won't have created a forward reference to a
19750 later parameter. */
19751 if (decl_die != NULL)
19753 add_AT_die_ref (die, attr, decl_die);
19754 return;
19759 /* Last chance: try to create a stack operation procedure to evaluate the
19760 value. Do nothing if even that is not possible or permitted. */
19761 if ((forms & dw_scalar_form_exprloc) == 0)
19762 return;
19764 list = loc_list_from_tree (value, 2, context);
19765 if (context && context->placeholder_arg)
19767 placeholder_seen = context->placeholder_seen;
19768 context->placeholder_seen = false;
19770 if (list == NULL || single_element_loc_list_p (list))
19772 /* If this attribute is not a reference nor constant, it is
19773 a DWARF expression rather than location description. For that
19774 loc_list_from_tree (value, 0, &context) is needed. */
19775 dw_loc_list_ref list2 = loc_list_from_tree (value, 0, context);
19776 if (list2 && single_element_loc_list_p (list2))
19778 if (placeholder_seen)
19780 struct dwarf_procedure_info dpi;
19781 dpi.fndecl = NULL_TREE;
19782 dpi.args_count = 1;
19783 if (!resolve_args_picking (list2->expr, 1, &dpi))
19784 return;
19786 add_AT_loc (die, attr, list2->expr);
19787 return;
19791 /* If that failed to give a single element location list, fall back to
19792 outputting this as a reference... still if permitted. */
19793 if (list == NULL
19794 || (forms & dw_scalar_form_reference) == 0
19795 || placeholder_seen)
19796 return;
19798 if (current_function_decl == 0)
19799 context_die = comp_unit_die ();
19800 else
19801 context_die = lookup_decl_die (current_function_decl);
19803 decl_die = new_die (DW_TAG_variable, context_die, value);
19804 add_AT_flag (decl_die, DW_AT_artificial, 1);
19805 add_type_attribute (decl_die, TREE_TYPE (value), TYPE_QUAL_CONST, false,
19806 context_die);
19807 add_AT_location_description (decl_die, DW_AT_location, list);
19808 add_AT_die_ref (die, attr, decl_die);
19811 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
19812 default. */
19814 static int
19815 lower_bound_default (void)
19817 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
19819 case DW_LANG_C:
19820 case DW_LANG_C89:
19821 case DW_LANG_C99:
19822 case DW_LANG_C11:
19823 case DW_LANG_C_plus_plus:
19824 case DW_LANG_C_plus_plus_11:
19825 case DW_LANG_C_plus_plus_14:
19826 case DW_LANG_ObjC:
19827 case DW_LANG_ObjC_plus_plus:
19828 case DW_LANG_Java:
19829 return 0;
19830 case DW_LANG_Fortran77:
19831 case DW_LANG_Fortran90:
19832 case DW_LANG_Fortran95:
19833 case DW_LANG_Fortran03:
19834 case DW_LANG_Fortran08:
19835 return 1;
19836 case DW_LANG_UPC:
19837 case DW_LANG_D:
19838 case DW_LANG_Python:
19839 return dwarf_version >= 4 ? 0 : -1;
19840 case DW_LANG_Ada95:
19841 case DW_LANG_Ada83:
19842 case DW_LANG_Cobol74:
19843 case DW_LANG_Cobol85:
19844 case DW_LANG_Pascal83:
19845 case DW_LANG_Modula2:
19846 case DW_LANG_PLI:
19847 return dwarf_version >= 4 ? 1 : -1;
19848 default:
19849 return -1;
19853 /* Given a tree node describing an array bound (either lower or upper) output
19854 a representation for that bound. */
19856 static void
19857 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr,
19858 tree bound, struct loc_descr_context *context)
19860 int dflt;
19862 while (1)
19863 switch (TREE_CODE (bound))
19865 /* Strip all conversions. */
19866 CASE_CONVERT:
19867 case VIEW_CONVERT_EXPR:
19868 bound = TREE_OPERAND (bound, 0);
19869 break;
19871 /* All fixed-bounds are represented by INTEGER_CST nodes. Lower bounds
19872 are even omitted when they are the default. */
19873 case INTEGER_CST:
19874 /* If the value for this bound is the default one, we can even omit the
19875 attribute. */
19876 if (bound_attr == DW_AT_lower_bound
19877 && tree_fits_shwi_p (bound)
19878 && (dflt = lower_bound_default ()) != -1
19879 && tree_to_shwi (bound) == dflt)
19880 return;
19882 /* FALLTHRU */
19884 default:
19885 /* Because of the complex interaction there can be with other GNAT
19886 encodings, GDB isn't ready yet to handle proper DWARF description
19887 for self-referencial subrange bounds: let GNAT encodings do the
19888 magic in such a case. */
19889 if (is_ada ()
19890 && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL
19891 && contains_placeholder_p (bound))
19892 return;
19894 add_scalar_info (subrange_die, bound_attr, bound,
19895 dw_scalar_form_constant
19896 | dw_scalar_form_exprloc
19897 | dw_scalar_form_reference,
19898 context);
19899 return;
19903 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
19904 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
19905 Note that the block of subscript information for an array type also
19906 includes information about the element type of the given array type.
19908 This function reuses previously set type and bound information if
19909 available. */
19911 static void
19912 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
19914 unsigned dimension_number;
19915 tree lower, upper;
19916 dw_die_ref child = type_die->die_child;
19918 for (dimension_number = 0;
19919 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
19920 type = TREE_TYPE (type), dimension_number++)
19922 tree domain = TYPE_DOMAIN (type);
19924 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
19925 break;
19927 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
19928 and (in GNU C only) variable bounds. Handle all three forms
19929 here. */
19931 /* Find and reuse a previously generated DW_TAG_subrange_type if
19932 available.
19934 For multi-dimensional arrays, as we iterate through the
19935 various dimensions in the enclosing for loop above, we also
19936 iterate through the DIE children and pick at each
19937 DW_TAG_subrange_type previously generated (if available).
19938 Each child DW_TAG_subrange_type DIE describes the range of
19939 the current dimension. At this point we should have as many
19940 DW_TAG_subrange_type's as we have dimensions in the
19941 array. */
19942 dw_die_ref subrange_die = NULL;
19943 if (child)
19944 while (1)
19946 child = child->die_sib;
19947 if (child->die_tag == DW_TAG_subrange_type)
19948 subrange_die = child;
19949 if (child == type_die->die_child)
19951 /* If we wrapped around, stop looking next time. */
19952 child = NULL;
19953 break;
19955 if (child->die_tag == DW_TAG_subrange_type)
19956 break;
19958 if (!subrange_die)
19959 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
19961 if (domain)
19963 /* We have an array type with specified bounds. */
19964 lower = TYPE_MIN_VALUE (domain);
19965 upper = TYPE_MAX_VALUE (domain);
19967 /* Define the index type. */
19968 if (TREE_TYPE (domain)
19969 && !get_AT (subrange_die, DW_AT_type))
19971 /* ??? This is probably an Ada unnamed subrange type. Ignore the
19972 TREE_TYPE field. We can't emit debug info for this
19973 because it is an unnamed integral type. */
19974 if (TREE_CODE (domain) == INTEGER_TYPE
19975 && TYPE_NAME (domain) == NULL_TREE
19976 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
19977 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
19979 else
19980 add_type_attribute (subrange_die, TREE_TYPE (domain),
19981 TYPE_UNQUALIFIED, false, type_die);
19984 /* ??? If upper is NULL, the array has unspecified length,
19985 but it does have a lower bound. This happens with Fortran
19986 dimension arr(N:*)
19987 Since the debugger is definitely going to need to know N
19988 to produce useful results, go ahead and output the lower
19989 bound solo, and hope the debugger can cope. */
19991 if (!get_AT (subrange_die, DW_AT_lower_bound))
19992 add_bound_info (subrange_die, DW_AT_lower_bound, lower, NULL);
19993 if (upper && !get_AT (subrange_die, DW_AT_upper_bound))
19994 add_bound_info (subrange_die, DW_AT_upper_bound, upper, NULL);
19997 /* Otherwise we have an array type with an unspecified length. The
19998 DWARF-2 spec does not say how to handle this; let's just leave out the
19999 bounds. */
20003 /* Add a DW_AT_byte_size attribute to DIE with TREE_NODE's size. */
20005 static void
20006 add_byte_size_attribute (dw_die_ref die, tree tree_node)
20008 dw_die_ref decl_die;
20009 HOST_WIDE_INT size;
20010 dw_loc_descr_ref size_expr = NULL;
20012 switch (TREE_CODE (tree_node))
20014 case ERROR_MARK:
20015 size = 0;
20016 break;
20017 case ENUMERAL_TYPE:
20018 case RECORD_TYPE:
20019 case UNION_TYPE:
20020 case QUAL_UNION_TYPE:
20021 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
20022 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
20024 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
20025 return;
20027 size_expr = type_byte_size (tree_node, &size);
20028 break;
20029 case FIELD_DECL:
20030 /* For a data member of a struct or union, the DW_AT_byte_size is
20031 generally given as the number of bytes normally allocated for an
20032 object of the *declared* type of the member itself. This is true
20033 even for bit-fields. */
20034 size = int_size_in_bytes (field_type (tree_node));
20035 break;
20036 default:
20037 gcc_unreachable ();
20040 /* Support for dynamically-sized objects was introduced by DWARFv3.
20041 At the moment, GDB does not handle variable byte sizes very well,
20042 though. */
20043 if ((dwarf_version >= 3 || !dwarf_strict)
20044 && gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL
20045 && size_expr != NULL)
20046 add_AT_loc (die, DW_AT_byte_size, size_expr);
20048 /* Note that `size' might be -1 when we get to this point. If it is, that
20049 indicates that the byte size of the entity in question is variable and
20050 that we could not generate a DWARF expression that computes it. */
20051 if (size >= 0)
20052 add_AT_unsigned (die, DW_AT_byte_size, size);
20055 /* Add a DW_AT_alignment attribute to DIE with TREE_NODE's non-default
20056 alignment. */
20058 static void
20059 add_alignment_attribute (dw_die_ref die, tree tree_node)
20061 if (dwarf_version < 5 && dwarf_strict)
20062 return;
20064 unsigned align;
20066 if (DECL_P (tree_node))
20068 if (!DECL_USER_ALIGN (tree_node))
20069 return;
20071 align = DECL_ALIGN_UNIT (tree_node);
20073 else if (TYPE_P (tree_node))
20075 if (!TYPE_USER_ALIGN (tree_node))
20076 return;
20078 align = TYPE_ALIGN_UNIT (tree_node);
20080 else
20081 gcc_unreachable ();
20083 add_AT_unsigned (die, DW_AT_alignment, align);
20086 /* For a FIELD_DECL node which represents a bit-field, output an attribute
20087 which specifies the distance in bits from the highest order bit of the
20088 "containing object" for the bit-field to the highest order bit of the
20089 bit-field itself.
20091 For any given bit-field, the "containing object" is a hypothetical object
20092 (of some integral or enum type) within which the given bit-field lives. The
20093 type of this hypothetical "containing object" is always the same as the
20094 declared type of the individual bit-field itself. The determination of the
20095 exact location of the "containing object" for a bit-field is rather
20096 complicated. It's handled by the `field_byte_offset' function (above).
20098 CTX is required: see the comment for VLR_CONTEXT.
20100 Note that it is the size (in bytes) of the hypothetical "containing object"
20101 which will be given in the DW_AT_byte_size attribute for this bit-field.
20102 (See `byte_size_attribute' above). */
20104 static inline void
20105 add_bit_offset_attribute (dw_die_ref die, tree decl, struct vlr_context *ctx)
20107 HOST_WIDE_INT object_offset_in_bytes;
20108 tree original_type = DECL_BIT_FIELD_TYPE (decl);
20109 HOST_WIDE_INT bitpos_int;
20110 HOST_WIDE_INT highest_order_object_bit_offset;
20111 HOST_WIDE_INT highest_order_field_bit_offset;
20112 HOST_WIDE_INT bit_offset;
20114 field_byte_offset (decl, ctx, &object_offset_in_bytes);
20116 /* Must be a field and a bit field. */
20117 gcc_assert (original_type && TREE_CODE (decl) == FIELD_DECL);
20119 /* We can't yet handle bit-fields whose offsets are variable, so if we
20120 encounter such things, just return without generating any attribute
20121 whatsoever. Likewise for variable or too large size. */
20122 if (! tree_fits_shwi_p (bit_position (decl))
20123 || ! tree_fits_uhwi_p (DECL_SIZE (decl)))
20124 return;
20126 bitpos_int = int_bit_position (decl);
20128 /* Note that the bit offset is always the distance (in bits) from the
20129 highest-order bit of the "containing object" to the highest-order bit of
20130 the bit-field itself. Since the "high-order end" of any object or field
20131 is different on big-endian and little-endian machines, the computation
20132 below must take account of these differences. */
20133 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
20134 highest_order_field_bit_offset = bitpos_int;
20136 if (! BYTES_BIG_ENDIAN)
20138 highest_order_field_bit_offset += tree_to_shwi (DECL_SIZE (decl));
20139 highest_order_object_bit_offset +=
20140 simple_type_size_in_bits (original_type);
20143 bit_offset
20144 = (! BYTES_BIG_ENDIAN
20145 ? highest_order_object_bit_offset - highest_order_field_bit_offset
20146 : highest_order_field_bit_offset - highest_order_object_bit_offset);
20148 if (bit_offset < 0)
20149 add_AT_int (die, DW_AT_bit_offset, bit_offset);
20150 else
20151 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
20154 /* For a FIELD_DECL node which represents a bit field, output an attribute
20155 which specifies the length in bits of the given field. */
20157 static inline void
20158 add_bit_size_attribute (dw_die_ref die, tree decl)
20160 /* Must be a field and a bit field. */
20161 gcc_assert (TREE_CODE (decl) == FIELD_DECL
20162 && DECL_BIT_FIELD_TYPE (decl));
20164 if (tree_fits_uhwi_p (DECL_SIZE (decl)))
20165 add_AT_unsigned (die, DW_AT_bit_size, tree_to_uhwi (DECL_SIZE (decl)));
20168 /* If the compiled language is ANSI C, then add a 'prototyped'
20169 attribute, if arg types are given for the parameters of a function. */
20171 static inline void
20172 add_prototyped_attribute (dw_die_ref die, tree func_type)
20174 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
20176 case DW_LANG_C:
20177 case DW_LANG_C89:
20178 case DW_LANG_C99:
20179 case DW_LANG_C11:
20180 case DW_LANG_ObjC:
20181 if (prototype_p (func_type))
20182 add_AT_flag (die, DW_AT_prototyped, 1);
20183 break;
20184 default:
20185 break;
20189 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
20190 by looking in the type declaration, the object declaration equate table or
20191 the block mapping. */
20193 static inline dw_die_ref
20194 add_abstract_origin_attribute (dw_die_ref die, tree origin)
20196 dw_die_ref origin_die = NULL;
20198 if (TREE_CODE (origin) != FUNCTION_DECL
20199 && TREE_CODE (origin) != BLOCK)
20201 /* We may have gotten separated from the block for the inlined
20202 function, if we're in an exception handler or some such; make
20203 sure that the abstract function has been written out.
20205 Doing this for nested functions is wrong, however; functions are
20206 distinct units, and our context might not even be inline. */
20207 tree fn = origin;
20209 if (TYPE_P (fn))
20210 fn = TYPE_STUB_DECL (fn);
20212 fn = decl_function_context (fn);
20213 if (fn)
20214 dwarf2out_abstract_function (fn);
20217 if (DECL_P (origin))
20218 origin_die = lookup_decl_die (origin);
20219 else if (TYPE_P (origin))
20220 origin_die = lookup_type_die (origin);
20221 else if (TREE_CODE (origin) == BLOCK)
20222 origin_die = BLOCK_DIE (origin);
20224 /* XXX: Functions that are never lowered don't always have correct block
20225 trees (in the case of java, they simply have no block tree, in some other
20226 languages). For these functions, there is nothing we can really do to
20227 output correct debug info for inlined functions in all cases. Rather
20228 than die, we'll just produce deficient debug info now, in that we will
20229 have variables without a proper abstract origin. In the future, when all
20230 functions are lowered, we should re-add a gcc_assert (origin_die)
20231 here. */
20233 if (origin_die)
20234 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
20235 return origin_die;
20238 /* We do not currently support the pure_virtual attribute. */
20240 static inline void
20241 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
20243 if (DECL_VINDEX (func_decl))
20245 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
20247 if (tree_fits_shwi_p (DECL_VINDEX (func_decl)))
20248 add_AT_loc (die, DW_AT_vtable_elem_location,
20249 new_loc_descr (DW_OP_constu,
20250 tree_to_shwi (DECL_VINDEX (func_decl)),
20251 0));
20253 /* GNU extension: Record what type this method came from originally. */
20254 if (debug_info_level > DINFO_LEVEL_TERSE
20255 && DECL_CONTEXT (func_decl))
20256 add_AT_die_ref (die, DW_AT_containing_type,
20257 lookup_type_die (DECL_CONTEXT (func_decl)));
20261 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
20262 given decl. This used to be a vendor extension until after DWARF 4
20263 standardized it. */
20265 static void
20266 add_linkage_attr (dw_die_ref die, tree decl)
20268 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
20270 /* Mimic what assemble_name_raw does with a leading '*'. */
20271 if (name[0] == '*')
20272 name = &name[1];
20274 if (dwarf_version >= 4)
20275 add_AT_string (die, DW_AT_linkage_name, name);
20276 else
20277 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
20280 /* Add source coordinate attributes for the given decl. */
20282 static void
20283 add_src_coords_attributes (dw_die_ref die, tree decl)
20285 expanded_location s;
20287 if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
20288 return;
20289 s = expand_location (DECL_SOURCE_LOCATION (decl));
20290 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
20291 add_AT_unsigned (die, DW_AT_decl_line, s.line);
20292 if (debug_column_info && s.column)
20293 add_AT_unsigned (die, DW_AT_decl_column, s.column);
20296 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
20298 static void
20299 add_linkage_name_raw (dw_die_ref die, tree decl)
20301 /* Defer until we have an assembler name set. */
20302 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
20304 limbo_die_node *asm_name;
20306 asm_name = ggc_cleared_alloc<limbo_die_node> ();
20307 asm_name->die = die;
20308 asm_name->created_for = decl;
20309 asm_name->next = deferred_asm_name;
20310 deferred_asm_name = asm_name;
20312 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
20313 add_linkage_attr (die, decl);
20316 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl if desired. */
20318 static void
20319 add_linkage_name (dw_die_ref die, tree decl)
20321 if (debug_info_level > DINFO_LEVEL_NONE
20322 && VAR_OR_FUNCTION_DECL_P (decl)
20323 && TREE_PUBLIC (decl)
20324 && !(VAR_P (decl) && DECL_REGISTER (decl))
20325 && die->die_tag != DW_TAG_member)
20326 add_linkage_name_raw (die, decl);
20329 /* Add a DW_AT_name attribute and source coordinate attribute for the
20330 given decl, but only if it actually has a name. */
20332 static void
20333 add_name_and_src_coords_attributes (dw_die_ref die, tree decl,
20334 bool no_linkage_name)
20336 tree decl_name;
20338 decl_name = DECL_NAME (decl);
20339 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
20341 const char *name = dwarf2_name (decl, 0);
20342 if (name)
20343 add_name_attribute (die, name);
20344 if (! DECL_ARTIFICIAL (decl))
20345 add_src_coords_attributes (die, decl);
20347 if (!no_linkage_name)
20348 add_linkage_name (die, decl);
20351 #ifdef VMS_DEBUGGING_INFO
20352 /* Get the function's name, as described by its RTL. This may be different
20353 from the DECL_NAME name used in the source file. */
20354 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
20356 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
20357 XEXP (DECL_RTL (decl), 0), false);
20358 vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
20360 #endif /* VMS_DEBUGGING_INFO */
20363 /* Add VALUE as a DW_AT_discr_value attribute to DIE. */
20365 static void
20366 add_discr_value (dw_die_ref die, dw_discr_value *value)
20368 dw_attr_node attr;
20370 attr.dw_attr = DW_AT_discr_value;
20371 attr.dw_attr_val.val_class = dw_val_class_discr_value;
20372 attr.dw_attr_val.val_entry = NULL;
20373 attr.dw_attr_val.v.val_discr_value.pos = value->pos;
20374 if (value->pos)
20375 attr.dw_attr_val.v.val_discr_value.v.uval = value->v.uval;
20376 else
20377 attr.dw_attr_val.v.val_discr_value.v.sval = value->v.sval;
20378 add_dwarf_attr (die, &attr);
20381 /* Add DISCR_LIST as a DW_AT_discr_list to DIE. */
20383 static void
20384 add_discr_list (dw_die_ref die, dw_discr_list_ref discr_list)
20386 dw_attr_node attr;
20388 attr.dw_attr = DW_AT_discr_list;
20389 attr.dw_attr_val.val_class = dw_val_class_discr_list;
20390 attr.dw_attr_val.val_entry = NULL;
20391 attr.dw_attr_val.v.val_discr_list = discr_list;
20392 add_dwarf_attr (die, &attr);
20395 static inline dw_discr_list_ref
20396 AT_discr_list (dw_attr_node *attr)
20398 return attr->dw_attr_val.v.val_discr_list;
20401 #ifdef VMS_DEBUGGING_INFO
20402 /* Output the debug main pointer die for VMS */
20404 void
20405 dwarf2out_vms_debug_main_pointer (void)
20407 char label[MAX_ARTIFICIAL_LABEL_BYTES];
20408 dw_die_ref die;
20410 /* Allocate the VMS debug main subprogram die. */
20411 die = ggc_cleared_alloc<die_node> ();
20412 die->die_tag = DW_TAG_subprogram;
20413 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
20414 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
20415 current_function_funcdef_no);
20416 add_AT_lbl_id (die, DW_AT_entry_pc, label);
20418 /* Make it the first child of comp_unit_die (). */
20419 die->die_parent = comp_unit_die ();
20420 if (comp_unit_die ()->die_child)
20422 die->die_sib = comp_unit_die ()->die_child->die_sib;
20423 comp_unit_die ()->die_child->die_sib = die;
20425 else
20427 die->die_sib = die;
20428 comp_unit_die ()->die_child = die;
20431 #endif /* VMS_DEBUGGING_INFO */
20433 /* Push a new declaration scope. */
20435 static void
20436 push_decl_scope (tree scope)
20438 vec_safe_push (decl_scope_table, scope);
20441 /* Pop a declaration scope. */
20443 static inline void
20444 pop_decl_scope (void)
20446 decl_scope_table->pop ();
20449 /* walk_tree helper function for uses_local_type, below. */
20451 static tree
20452 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
20454 if (!TYPE_P (*tp))
20455 *walk_subtrees = 0;
20456 else
20458 tree name = TYPE_NAME (*tp);
20459 if (name && DECL_P (name) && decl_function_context (name))
20460 return *tp;
20462 return NULL_TREE;
20465 /* If TYPE involves a function-local type (including a local typedef to a
20466 non-local type), returns that type; otherwise returns NULL_TREE. */
20468 static tree
20469 uses_local_type (tree type)
20471 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
20472 return used;
20475 /* Return the DIE for the scope that immediately contains this type.
20476 Non-named types that do not involve a function-local type get global
20477 scope. Named types nested in namespaces or other types get their
20478 containing scope. All other types (i.e. function-local named types) get
20479 the current active scope. */
20481 static dw_die_ref
20482 scope_die_for (tree t, dw_die_ref context_die)
20484 dw_die_ref scope_die = NULL;
20485 tree containing_scope;
20487 /* Non-types always go in the current scope. */
20488 gcc_assert (TYPE_P (t));
20490 /* Use the scope of the typedef, rather than the scope of the type
20491 it refers to. */
20492 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
20493 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
20494 else
20495 containing_scope = TYPE_CONTEXT (t);
20497 /* Use the containing namespace if there is one. */
20498 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
20500 if (context_die == lookup_decl_die (containing_scope))
20501 /* OK */;
20502 else if (debug_info_level > DINFO_LEVEL_TERSE)
20503 context_die = get_context_die (containing_scope);
20504 else
20505 containing_scope = NULL_TREE;
20508 /* Ignore function type "scopes" from the C frontend. They mean that
20509 a tagged type is local to a parmlist of a function declarator, but
20510 that isn't useful to DWARF. */
20511 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
20512 containing_scope = NULL_TREE;
20514 if (SCOPE_FILE_SCOPE_P (containing_scope))
20516 /* If T uses a local type keep it local as well, to avoid references
20517 to function-local DIEs from outside the function. */
20518 if (current_function_decl && uses_local_type (t))
20519 scope_die = context_die;
20520 else
20521 scope_die = comp_unit_die ();
20523 else if (TYPE_P (containing_scope))
20525 /* For types, we can just look up the appropriate DIE. */
20526 if (debug_info_level > DINFO_LEVEL_TERSE)
20527 scope_die = get_context_die (containing_scope);
20528 else
20530 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
20531 if (scope_die == NULL)
20532 scope_die = comp_unit_die ();
20535 else
20536 scope_die = context_die;
20538 return scope_die;
20541 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
20543 static inline int
20544 local_scope_p (dw_die_ref context_die)
20546 for (; context_die; context_die = context_die->die_parent)
20547 if (context_die->die_tag == DW_TAG_inlined_subroutine
20548 || context_die->die_tag == DW_TAG_subprogram)
20549 return 1;
20551 return 0;
20554 /* Returns nonzero if CONTEXT_DIE is a class. */
20556 static inline int
20557 class_scope_p (dw_die_ref context_die)
20559 return (context_die
20560 && (context_die->die_tag == DW_TAG_structure_type
20561 || context_die->die_tag == DW_TAG_class_type
20562 || context_die->die_tag == DW_TAG_interface_type
20563 || context_die->die_tag == DW_TAG_union_type));
20566 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
20567 whether or not to treat a DIE in this context as a declaration. */
20569 static inline int
20570 class_or_namespace_scope_p (dw_die_ref context_die)
20572 return (class_scope_p (context_die)
20573 || (context_die && context_die->die_tag == DW_TAG_namespace));
20576 /* Many forms of DIEs require a "type description" attribute. This
20577 routine locates the proper "type descriptor" die for the type given
20578 by 'type' plus any additional qualifiers given by 'cv_quals', and
20579 adds a DW_AT_type attribute below the given die. */
20581 static void
20582 add_type_attribute (dw_die_ref object_die, tree type, int cv_quals,
20583 bool reverse, dw_die_ref context_die)
20585 enum tree_code code = TREE_CODE (type);
20586 dw_die_ref type_die = NULL;
20588 /* ??? If this type is an unnamed subrange type of an integral, floating-point
20589 or fixed-point type, use the inner type. This is because we have no
20590 support for unnamed types in base_type_die. This can happen if this is
20591 an Ada subrange type. Correct solution is emit a subrange type die. */
20592 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
20593 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
20594 type = TREE_TYPE (type), code = TREE_CODE (type);
20596 if (code == ERROR_MARK
20597 /* Handle a special case. For functions whose return type is void, we
20598 generate *no* type attribute. (Note that no object may have type
20599 `void', so this only applies to function return types). */
20600 || code == VOID_TYPE)
20601 return;
20603 type_die = modified_type_die (type,
20604 cv_quals | TYPE_QUALS_NO_ADDR_SPACE (type),
20605 reverse,
20606 context_die);
20608 if (type_die != NULL)
20609 add_AT_die_ref (object_die, DW_AT_type, type_die);
20612 /* Given an object die, add the calling convention attribute for the
20613 function call type. */
20614 static void
20615 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
20617 enum dwarf_calling_convention value = DW_CC_normal;
20619 value = ((enum dwarf_calling_convention)
20620 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
20622 if (is_fortran ()
20623 && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
20625 /* DWARF 2 doesn't provide a way to identify a program's source-level
20626 entry point. DW_AT_calling_convention attributes are only meant
20627 to describe functions' calling conventions. However, lacking a
20628 better way to signal the Fortran main program, we used this for
20629 a long time, following existing custom. Now, DWARF 4 has
20630 DW_AT_main_subprogram, which we add below, but some tools still
20631 rely on the old way, which we thus keep. */
20632 value = DW_CC_program;
20634 if (dwarf_version >= 4 || !dwarf_strict)
20635 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
20638 /* Only add the attribute if the backend requests it, and
20639 is not DW_CC_normal. */
20640 if (value && (value != DW_CC_normal))
20641 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
20644 /* Given a tree pointer to a struct, class, union, or enum type node, return
20645 a pointer to the (string) tag name for the given type, or zero if the type
20646 was declared without a tag. */
20648 static const char *
20649 type_tag (const_tree type)
20651 const char *name = 0;
20653 if (TYPE_NAME (type) != 0)
20655 tree t = 0;
20657 /* Find the IDENTIFIER_NODE for the type name. */
20658 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
20659 && !TYPE_NAMELESS (type))
20660 t = TYPE_NAME (type);
20662 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
20663 a TYPE_DECL node, regardless of whether or not a `typedef' was
20664 involved. */
20665 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
20666 && ! DECL_IGNORED_P (TYPE_NAME (type)))
20668 /* We want to be extra verbose. Don't call dwarf_name if
20669 DECL_NAME isn't set. The default hook for decl_printable_name
20670 doesn't like that, and in this context it's correct to return
20671 0, instead of "<anonymous>" or the like. */
20672 if (DECL_NAME (TYPE_NAME (type))
20673 && !DECL_NAMELESS (TYPE_NAME (type)))
20674 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
20677 /* Now get the name as a string, or invent one. */
20678 if (!name && t != 0)
20679 name = IDENTIFIER_POINTER (t);
20682 return (name == 0 || *name == '\0') ? 0 : name;
20685 /* Return the type associated with a data member, make a special check
20686 for bit field types. */
20688 static inline tree
20689 member_declared_type (const_tree member)
20691 return (DECL_BIT_FIELD_TYPE (member)
20692 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
20695 /* Get the decl's label, as described by its RTL. This may be different
20696 from the DECL_NAME name used in the source file. */
20698 #if 0
20699 static const char *
20700 decl_start_label (tree decl)
20702 rtx x;
20703 const char *fnname;
20705 x = DECL_RTL (decl);
20706 gcc_assert (MEM_P (x));
20708 x = XEXP (x, 0);
20709 gcc_assert (GET_CODE (x) == SYMBOL_REF);
20711 fnname = XSTR (x, 0);
20712 return fnname;
20714 #endif
20716 /* For variable-length arrays that have been previously generated, but
20717 may be incomplete due to missing subscript info, fill the subscript
20718 info. Return TRUE if this is one of those cases. */
20719 static bool
20720 fill_variable_array_bounds (tree type)
20722 if (TREE_ASM_WRITTEN (type)
20723 && TREE_CODE (type) == ARRAY_TYPE
20724 && variably_modified_type_p (type, NULL))
20726 dw_die_ref array_die = lookup_type_die (type);
20727 if (!array_die)
20728 return false;
20729 add_subscript_info (array_die, type, !is_ada ());
20730 return true;
20732 return false;
20735 /* These routines generate the internal representation of the DIE's for
20736 the compilation unit. Debugging information is collected by walking
20737 the declaration trees passed in from dwarf2out_decl(). */
20739 static void
20740 gen_array_type_die (tree type, dw_die_ref context_die)
20742 dw_die_ref array_die;
20744 /* GNU compilers represent multidimensional array types as sequences of one
20745 dimensional array types whose element types are themselves array types.
20746 We sometimes squish that down to a single array_type DIE with multiple
20747 subscripts in the Dwarf debugging info. The draft Dwarf specification
20748 say that we are allowed to do this kind of compression in C, because
20749 there is no difference between an array of arrays and a multidimensional
20750 array. We don't do this for Ada to remain as close as possible to the
20751 actual representation, which is especially important against the language
20752 flexibilty wrt arrays of variable size. */
20754 bool collapse_nested_arrays = !is_ada ();
20756 if (fill_variable_array_bounds (type))
20757 return;
20759 dw_die_ref scope_die = scope_die_for (type, context_die);
20760 tree element_type;
20762 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
20763 DW_TAG_string_type doesn't have DW_AT_type attribute). */
20764 if (TYPE_STRING_FLAG (type)
20765 && TREE_CODE (type) == ARRAY_TYPE
20766 && is_fortran ()
20767 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
20769 HOST_WIDE_INT size;
20771 array_die = new_die (DW_TAG_string_type, scope_die, type);
20772 add_name_attribute (array_die, type_tag (type));
20773 equate_type_number_to_die (type, array_die);
20774 size = int_size_in_bytes (type);
20775 if (size >= 0)
20776 add_AT_unsigned (array_die, DW_AT_byte_size, size);
20777 else if (TYPE_DOMAIN (type) != NULL_TREE
20778 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE)
20780 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
20781 tree rszdecl = szdecl;
20783 size = int_size_in_bytes (TREE_TYPE (szdecl));
20784 if (!DECL_P (szdecl))
20786 if (TREE_CODE (szdecl) == INDIRECT_REF
20787 && DECL_P (TREE_OPERAND (szdecl, 0)))
20789 rszdecl = TREE_OPERAND (szdecl, 0);
20790 if (int_size_in_bytes (TREE_TYPE (rszdecl))
20791 != DWARF2_ADDR_SIZE)
20792 size = 0;
20794 else
20795 size = 0;
20797 if (size > 0)
20799 dw_loc_list_ref loc
20800 = loc_list_from_tree (rszdecl, szdecl == rszdecl ? 2 : 0,
20801 NULL);
20802 if (loc)
20804 add_AT_location_description (array_die, DW_AT_string_length,
20805 loc);
20806 if (size != DWARF2_ADDR_SIZE)
20807 add_AT_unsigned (array_die, dwarf_version >= 5
20808 ? DW_AT_string_length_byte_size
20809 : DW_AT_byte_size, size);
20813 return;
20816 array_die = new_die (DW_TAG_array_type, scope_die, type);
20817 add_name_attribute (array_die, type_tag (type));
20818 equate_type_number_to_die (type, array_die);
20820 if (TREE_CODE (type) == VECTOR_TYPE)
20821 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
20823 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
20824 if (is_fortran ()
20825 && TREE_CODE (type) == ARRAY_TYPE
20826 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
20827 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
20828 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
20830 #if 0
20831 /* We default the array ordering. SDB will probably do
20832 the right things even if DW_AT_ordering is not present. It's not even
20833 an issue until we start to get into multidimensional arrays anyway. If
20834 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
20835 then we'll have to put the DW_AT_ordering attribute back in. (But if
20836 and when we find out that we need to put these in, we will only do so
20837 for multidimensional arrays. */
20838 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
20839 #endif
20841 if (TREE_CODE (type) == VECTOR_TYPE)
20843 /* For VECTOR_TYPEs we use an array die with appropriate bounds. */
20844 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
20845 add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node, NULL);
20846 add_bound_info (subrange_die, DW_AT_upper_bound,
20847 size_int (TYPE_VECTOR_SUBPARTS (type) - 1), NULL);
20849 else
20850 add_subscript_info (array_die, type, collapse_nested_arrays);
20852 /* Add representation of the type of the elements of this array type and
20853 emit the corresponding DIE if we haven't done it already. */
20854 element_type = TREE_TYPE (type);
20855 if (collapse_nested_arrays)
20856 while (TREE_CODE (element_type) == ARRAY_TYPE)
20858 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
20859 break;
20860 element_type = TREE_TYPE (element_type);
20863 add_type_attribute (array_die, element_type, TYPE_UNQUALIFIED,
20864 TREE_CODE (type) == ARRAY_TYPE
20865 && TYPE_REVERSE_STORAGE_ORDER (type),
20866 context_die);
20868 add_gnat_descriptive_type_attribute (array_die, type, context_die);
20869 if (TYPE_ARTIFICIAL (type))
20870 add_AT_flag (array_die, DW_AT_artificial, 1);
20872 if (get_AT (array_die, DW_AT_name))
20873 add_pubtype (type, array_die);
20875 add_alignment_attribute (array_die, type);
20878 /* This routine generates DIE for array with hidden descriptor, details
20879 are filled into *info by a langhook. */
20881 static void
20882 gen_descr_array_type_die (tree type, struct array_descr_info *info,
20883 dw_die_ref context_die)
20885 const dw_die_ref scope_die = scope_die_for (type, context_die);
20886 const dw_die_ref array_die = new_die (DW_TAG_array_type, scope_die, type);
20887 struct loc_descr_context context = { type, info->base_decl, NULL,
20888 false, false };
20889 enum dwarf_tag subrange_tag = DW_TAG_subrange_type;
20890 int dim;
20892 add_name_attribute (array_die, type_tag (type));
20893 equate_type_number_to_die (type, array_die);
20895 if (info->ndimensions > 1)
20896 switch (info->ordering)
20898 case array_descr_ordering_row_major:
20899 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
20900 break;
20901 case array_descr_ordering_column_major:
20902 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
20903 break;
20904 default:
20905 break;
20908 if (dwarf_version >= 3 || !dwarf_strict)
20910 if (info->data_location)
20911 add_scalar_info (array_die, DW_AT_data_location, info->data_location,
20912 dw_scalar_form_exprloc, &context);
20913 if (info->associated)
20914 add_scalar_info (array_die, DW_AT_associated, info->associated,
20915 dw_scalar_form_constant
20916 | dw_scalar_form_exprloc
20917 | dw_scalar_form_reference, &context);
20918 if (info->allocated)
20919 add_scalar_info (array_die, DW_AT_allocated, info->allocated,
20920 dw_scalar_form_constant
20921 | dw_scalar_form_exprloc
20922 | dw_scalar_form_reference, &context);
20923 if (info->stride)
20925 const enum dwarf_attribute attr
20926 = (info->stride_in_bits) ? DW_AT_bit_stride : DW_AT_byte_stride;
20927 const int forms
20928 = (info->stride_in_bits)
20929 ? dw_scalar_form_constant
20930 : (dw_scalar_form_constant
20931 | dw_scalar_form_exprloc
20932 | dw_scalar_form_reference);
20934 add_scalar_info (array_die, attr, info->stride, forms, &context);
20937 if (dwarf_version >= 5)
20939 if (info->rank)
20941 add_scalar_info (array_die, DW_AT_rank, info->rank,
20942 dw_scalar_form_constant
20943 | dw_scalar_form_exprloc, &context);
20944 subrange_tag = DW_TAG_generic_subrange;
20945 context.placeholder_arg = true;
20949 add_gnat_descriptive_type_attribute (array_die, type, context_die);
20951 for (dim = 0; dim < info->ndimensions; dim++)
20953 dw_die_ref subrange_die = new_die (subrange_tag, array_die, NULL);
20955 if (info->dimen[dim].bounds_type)
20956 add_type_attribute (subrange_die,
20957 info->dimen[dim].bounds_type, TYPE_UNQUALIFIED,
20958 false, context_die);
20959 if (info->dimen[dim].lower_bound)
20960 add_bound_info (subrange_die, DW_AT_lower_bound,
20961 info->dimen[dim].lower_bound, &context);
20962 if (info->dimen[dim].upper_bound)
20963 add_bound_info (subrange_die, DW_AT_upper_bound,
20964 info->dimen[dim].upper_bound, &context);
20965 if ((dwarf_version >= 3 || !dwarf_strict) && info->dimen[dim].stride)
20966 add_scalar_info (subrange_die, DW_AT_byte_stride,
20967 info->dimen[dim].stride,
20968 dw_scalar_form_constant
20969 | dw_scalar_form_exprloc
20970 | dw_scalar_form_reference,
20971 &context);
20974 gen_type_die (info->element_type, context_die);
20975 add_type_attribute (array_die, info->element_type, TYPE_UNQUALIFIED,
20976 TREE_CODE (type) == ARRAY_TYPE
20977 && TYPE_REVERSE_STORAGE_ORDER (type),
20978 context_die);
20980 if (get_AT (array_die, DW_AT_name))
20981 add_pubtype (type, array_die);
20983 add_alignment_attribute (array_die, type);
20986 #if 0
20987 static void
20988 gen_entry_point_die (tree decl, dw_die_ref context_die)
20990 tree origin = decl_ultimate_origin (decl);
20991 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
20993 if (origin != NULL)
20994 add_abstract_origin_attribute (decl_die, origin);
20995 else
20997 add_name_and_src_coords_attributes (decl_die, decl);
20998 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
20999 TYPE_UNQUALIFIED, false, context_die);
21002 if (DECL_ABSTRACT_P (decl))
21003 equate_decl_number_to_die (decl, decl_die);
21004 else
21005 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
21007 #endif
21009 /* Walk through the list of incomplete types again, trying once more to
21010 emit full debugging info for them. */
21012 static void
21013 retry_incomplete_types (void)
21015 set_early_dwarf s;
21016 int i;
21018 for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
21019 if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
21020 gen_type_die ((*incomplete_types)[i], comp_unit_die ());
21021 vec_safe_truncate (incomplete_types, 0);
21024 /* Determine what tag to use for a record type. */
21026 static enum dwarf_tag
21027 record_type_tag (tree type)
21029 if (! lang_hooks.types.classify_record)
21030 return DW_TAG_structure_type;
21032 switch (lang_hooks.types.classify_record (type))
21034 case RECORD_IS_STRUCT:
21035 return DW_TAG_structure_type;
21037 case RECORD_IS_CLASS:
21038 return DW_TAG_class_type;
21040 case RECORD_IS_INTERFACE:
21041 if (dwarf_version >= 3 || !dwarf_strict)
21042 return DW_TAG_interface_type;
21043 return DW_TAG_structure_type;
21045 default:
21046 gcc_unreachable ();
21050 /* Generate a DIE to represent an enumeration type. Note that these DIEs
21051 include all of the information about the enumeration values also. Each
21052 enumerated type name/value is listed as a child of the enumerated type
21053 DIE. */
21055 static dw_die_ref
21056 gen_enumeration_type_die (tree type, dw_die_ref context_die)
21058 dw_die_ref type_die = lookup_type_die (type);
21060 if (type_die == NULL)
21062 type_die = new_die (DW_TAG_enumeration_type,
21063 scope_die_for (type, context_die), type);
21064 equate_type_number_to_die (type, type_die);
21065 add_name_attribute (type_die, type_tag (type));
21066 if (dwarf_version >= 4 || !dwarf_strict)
21068 if (ENUM_IS_SCOPED (type))
21069 add_AT_flag (type_die, DW_AT_enum_class, 1);
21070 if (ENUM_IS_OPAQUE (type))
21071 add_AT_flag (type_die, DW_AT_declaration, 1);
21073 if (!dwarf_strict)
21074 add_AT_unsigned (type_die, DW_AT_encoding,
21075 TYPE_UNSIGNED (type)
21076 ? DW_ATE_unsigned
21077 : DW_ATE_signed);
21079 else if (! TYPE_SIZE (type))
21080 return type_die;
21081 else
21082 remove_AT (type_die, DW_AT_declaration);
21084 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
21085 given enum type is incomplete, do not generate the DW_AT_byte_size
21086 attribute or the DW_AT_element_list attribute. */
21087 if (TYPE_SIZE (type))
21089 tree link;
21091 TREE_ASM_WRITTEN (type) = 1;
21092 add_byte_size_attribute (type_die, type);
21093 add_alignment_attribute (type_die, type);
21094 if (dwarf_version >= 3 || !dwarf_strict)
21096 tree underlying = lang_hooks.types.enum_underlying_base_type (type);
21097 add_type_attribute (type_die, underlying, TYPE_UNQUALIFIED, false,
21098 context_die);
21100 if (TYPE_STUB_DECL (type) != NULL_TREE)
21102 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
21103 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
21106 /* If the first reference to this type was as the return type of an
21107 inline function, then it may not have a parent. Fix this now. */
21108 if (type_die->die_parent == NULL)
21109 add_child_die (scope_die_for (type, context_die), type_die);
21111 for (link = TYPE_VALUES (type);
21112 link != NULL; link = TREE_CHAIN (link))
21114 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
21115 tree value = TREE_VALUE (link);
21117 add_name_attribute (enum_die,
21118 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
21120 if (TREE_CODE (value) == CONST_DECL)
21121 value = DECL_INITIAL (value);
21123 if (simple_type_size_in_bits (TREE_TYPE (value))
21124 <= HOST_BITS_PER_WIDE_INT || tree_fits_shwi_p (value))
21126 /* For constant forms created by add_AT_unsigned DWARF
21127 consumers (GDB, elfutils, etc.) always zero extend
21128 the value. Only when the actual value is negative
21129 do we need to use add_AT_int to generate a constant
21130 form that can represent negative values. */
21131 HOST_WIDE_INT val = TREE_INT_CST_LOW (value);
21132 if (TYPE_UNSIGNED (TREE_TYPE (value)) || val >= 0)
21133 add_AT_unsigned (enum_die, DW_AT_const_value,
21134 (unsigned HOST_WIDE_INT) val);
21135 else
21136 add_AT_int (enum_die, DW_AT_const_value, val);
21138 else
21139 /* Enumeration constants may be wider than HOST_WIDE_INT. Handle
21140 that here. TODO: This should be re-worked to use correct
21141 signed/unsigned double tags for all cases. */
21142 add_AT_wide (enum_die, DW_AT_const_value, value);
21145 add_gnat_descriptive_type_attribute (type_die, type, context_die);
21146 if (TYPE_ARTIFICIAL (type))
21147 add_AT_flag (type_die, DW_AT_artificial, 1);
21149 else
21150 add_AT_flag (type_die, DW_AT_declaration, 1);
21152 add_alignment_attribute (type_die, type);
21154 add_pubtype (type, type_die);
21156 return type_die;
21159 /* Generate a DIE to represent either a real live formal parameter decl or to
21160 represent just the type of some formal parameter position in some function
21161 type.
21163 Note that this routine is a bit unusual because its argument may be a
21164 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
21165 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
21166 node. If it's the former then this function is being called to output a
21167 DIE to represent a formal parameter object (or some inlining thereof). If
21168 it's the latter, then this function is only being called to output a
21169 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
21170 argument type of some subprogram type.
21171 If EMIT_NAME_P is true, name and source coordinate attributes
21172 are emitted. */
21174 static dw_die_ref
21175 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
21176 dw_die_ref context_die)
21178 tree node_or_origin = node ? node : origin;
21179 tree ultimate_origin;
21180 dw_die_ref parm_die = NULL;
21182 if (TREE_CODE_CLASS (TREE_CODE (node_or_origin)) == tcc_declaration)
21184 parm_die = lookup_decl_die (node);
21186 /* If the contexts differ, we may not be talking about the same
21187 thing. */
21188 if (parm_die && parm_die->die_parent != context_die)
21190 if (!DECL_ABSTRACT_P (node))
21192 /* This can happen when creating an inlined instance, in
21193 which case we need to create a new DIE that will get
21194 annotated with DW_AT_abstract_origin. */
21195 parm_die = NULL;
21197 else
21199 /* FIXME: Reuse DIE even with a differing context.
21201 This can happen when calling
21202 dwarf2out_abstract_function to build debug info for
21203 the abstract instance of a function for which we have
21204 already generated a DIE in
21205 dwarf2out_early_global_decl.
21207 Once we remove dwarf2out_abstract_function, we should
21208 have a call to gcc_unreachable here. */
21212 if (parm_die && parm_die->die_parent == NULL)
21214 /* Check that parm_die already has the right attributes that
21215 we would have added below. If any attributes are
21216 missing, fall through to add them. */
21217 if (! DECL_ABSTRACT_P (node_or_origin)
21218 && !get_AT (parm_die, DW_AT_location)
21219 && !get_AT (parm_die, DW_AT_const_value))
21220 /* We are missing location info, and are about to add it. */
21222 else
21224 add_child_die (context_die, parm_die);
21225 return parm_die;
21230 /* If we have a previously generated DIE, use it, unless this is an
21231 concrete instance (origin != NULL), in which case we need a new
21232 DIE with a corresponding DW_AT_abstract_origin. */
21233 bool reusing_die;
21234 if (parm_die && origin == NULL)
21235 reusing_die = true;
21236 else
21238 parm_die = new_die (DW_TAG_formal_parameter, context_die, node);
21239 reusing_die = false;
21242 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
21244 case tcc_declaration:
21245 ultimate_origin = decl_ultimate_origin (node_or_origin);
21246 if (node || ultimate_origin)
21247 origin = ultimate_origin;
21249 if (reusing_die)
21250 goto add_location;
21252 if (origin != NULL)
21253 add_abstract_origin_attribute (parm_die, origin);
21254 else if (emit_name_p)
21255 add_name_and_src_coords_attributes (parm_die, node);
21256 if (origin == NULL
21257 || (! DECL_ABSTRACT_P (node_or_origin)
21258 && variably_modified_type_p (TREE_TYPE (node_or_origin),
21259 decl_function_context
21260 (node_or_origin))))
21262 tree type = TREE_TYPE (node_or_origin);
21263 if (decl_by_reference_p (node_or_origin))
21264 add_type_attribute (parm_die, TREE_TYPE (type),
21265 TYPE_UNQUALIFIED,
21266 false, context_die);
21267 else
21268 add_type_attribute (parm_die, type,
21269 decl_quals (node_or_origin),
21270 false, context_die);
21272 if (origin == NULL && DECL_ARTIFICIAL (node))
21273 add_AT_flag (parm_die, DW_AT_artificial, 1);
21274 add_location:
21275 if (node && node != origin)
21276 equate_decl_number_to_die (node, parm_die);
21277 if (! DECL_ABSTRACT_P (node_or_origin))
21278 add_location_or_const_value_attribute (parm_die, node_or_origin,
21279 node == NULL);
21281 break;
21283 case tcc_type:
21284 /* We were called with some kind of a ..._TYPE node. */
21285 add_type_attribute (parm_die, node_or_origin, TYPE_UNQUALIFIED, false,
21286 context_die);
21287 break;
21289 default:
21290 gcc_unreachable ();
21293 return parm_die;
21296 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
21297 children DW_TAG_formal_parameter DIEs representing the arguments of the
21298 parameter pack.
21300 PARM_PACK must be a function parameter pack.
21301 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
21302 must point to the subsequent arguments of the function PACK_ARG belongs to.
21303 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
21304 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
21305 following the last one for which a DIE was generated. */
21307 static dw_die_ref
21308 gen_formal_parameter_pack_die (tree parm_pack,
21309 tree pack_arg,
21310 dw_die_ref subr_die,
21311 tree *next_arg)
21313 tree arg;
21314 dw_die_ref parm_pack_die;
21316 gcc_assert (parm_pack
21317 && lang_hooks.function_parameter_pack_p (parm_pack)
21318 && subr_die);
21320 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
21321 add_src_coords_attributes (parm_pack_die, parm_pack);
21323 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
21325 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
21326 parm_pack))
21327 break;
21328 gen_formal_parameter_die (arg, NULL,
21329 false /* Don't emit name attribute. */,
21330 parm_pack_die);
21332 if (next_arg)
21333 *next_arg = arg;
21334 return parm_pack_die;
21337 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
21338 at the end of an (ANSI prototyped) formal parameters list. */
21340 static void
21341 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
21343 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
21346 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
21347 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
21348 parameters as specified in some function type specification (except for
21349 those which appear as part of a function *definition*). */
21351 static void
21352 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
21354 tree link;
21355 tree formal_type = NULL;
21356 tree first_parm_type;
21357 tree arg;
21359 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
21361 arg = DECL_ARGUMENTS (function_or_method_type);
21362 function_or_method_type = TREE_TYPE (function_or_method_type);
21364 else
21365 arg = NULL_TREE;
21367 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
21369 /* Make our first pass over the list of formal parameter types and output a
21370 DW_TAG_formal_parameter DIE for each one. */
21371 for (link = first_parm_type; link; )
21373 dw_die_ref parm_die;
21375 formal_type = TREE_VALUE (link);
21376 if (formal_type == void_type_node)
21377 break;
21379 /* Output a (nameless) DIE to represent the formal parameter itself. */
21380 if (!POINTER_BOUNDS_TYPE_P (formal_type))
21382 parm_die = gen_formal_parameter_die (formal_type, NULL,
21383 true /* Emit name attribute. */,
21384 context_die);
21385 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
21386 && link == first_parm_type)
21388 add_AT_flag (parm_die, DW_AT_artificial, 1);
21389 if (dwarf_version >= 3 || !dwarf_strict)
21390 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
21392 else if (arg && DECL_ARTIFICIAL (arg))
21393 add_AT_flag (parm_die, DW_AT_artificial, 1);
21396 link = TREE_CHAIN (link);
21397 if (arg)
21398 arg = DECL_CHAIN (arg);
21401 /* If this function type has an ellipsis, add a
21402 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
21403 if (formal_type != void_type_node)
21404 gen_unspecified_parameters_die (function_or_method_type, context_die);
21406 /* Make our second (and final) pass over the list of formal parameter types
21407 and output DIEs to represent those types (as necessary). */
21408 for (link = TYPE_ARG_TYPES (function_or_method_type);
21409 link && TREE_VALUE (link);
21410 link = TREE_CHAIN (link))
21411 gen_type_die (TREE_VALUE (link), context_die);
21414 /* We want to generate the DIE for TYPE so that we can generate the
21415 die for MEMBER, which has been defined; we will need to refer back
21416 to the member declaration nested within TYPE. If we're trying to
21417 generate minimal debug info for TYPE, processing TYPE won't do the
21418 trick; we need to attach the member declaration by hand. */
21420 static void
21421 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
21423 gen_type_die (type, context_die);
21425 /* If we're trying to avoid duplicate debug info, we may not have
21426 emitted the member decl for this function. Emit it now. */
21427 if (TYPE_STUB_DECL (type)
21428 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
21429 && ! lookup_decl_die (member))
21431 dw_die_ref type_die;
21432 gcc_assert (!decl_ultimate_origin (member));
21434 push_decl_scope (type);
21435 type_die = lookup_type_die_strip_naming_typedef (type);
21436 if (TREE_CODE (member) == FUNCTION_DECL)
21437 gen_subprogram_die (member, type_die);
21438 else if (TREE_CODE (member) == FIELD_DECL)
21440 /* Ignore the nameless fields that are used to skip bits but handle
21441 C++ anonymous unions and structs. */
21442 if (DECL_NAME (member) != NULL_TREE
21443 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
21444 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
21446 struct vlr_context vlr_ctx = {
21447 DECL_CONTEXT (member), /* struct_type */
21448 NULL_TREE /* variant_part_offset */
21450 gen_type_die (member_declared_type (member), type_die);
21451 gen_field_die (member, &vlr_ctx, type_die);
21454 else
21455 gen_variable_die (member, NULL_TREE, type_die);
21457 pop_decl_scope ();
21461 /* Forward declare these functions, because they are mutually recursive
21462 with their set_block_* pairing functions. */
21463 static void set_decl_origin_self (tree);
21464 static void set_decl_abstract_flags (tree, vec<tree> &);
21466 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
21467 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
21468 that it points to the node itself, thus indicating that the node is its
21469 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
21470 the given node is NULL, recursively descend the decl/block tree which
21471 it is the root of, and for each other ..._DECL or BLOCK node contained
21472 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
21473 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
21474 values to point to themselves. */
21476 static void
21477 set_block_origin_self (tree stmt)
21479 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
21481 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
21484 tree local_decl;
21486 for (local_decl = BLOCK_VARS (stmt);
21487 local_decl != NULL_TREE;
21488 local_decl = DECL_CHAIN (local_decl))
21489 /* Do not recurse on nested functions since the inlining status
21490 of parent and child can be different as per the DWARF spec. */
21491 if (TREE_CODE (local_decl) != FUNCTION_DECL
21492 && !DECL_EXTERNAL (local_decl))
21493 set_decl_origin_self (local_decl);
21497 tree subblock;
21499 for (subblock = BLOCK_SUBBLOCKS (stmt);
21500 subblock != NULL_TREE;
21501 subblock = BLOCK_CHAIN (subblock))
21502 set_block_origin_self (subblock); /* Recurse. */
21507 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
21508 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
21509 node to so that it points to the node itself, thus indicating that the
21510 node represents its own (abstract) origin. Additionally, if the
21511 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
21512 the decl/block tree of which the given node is the root of, and for
21513 each other ..._DECL or BLOCK node contained therein whose
21514 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
21515 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
21516 point to themselves. */
21518 static void
21519 set_decl_origin_self (tree decl)
21521 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
21523 DECL_ABSTRACT_ORIGIN (decl) = decl;
21524 if (TREE_CODE (decl) == FUNCTION_DECL)
21526 tree arg;
21528 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
21529 DECL_ABSTRACT_ORIGIN (arg) = arg;
21530 if (DECL_INITIAL (decl) != NULL_TREE
21531 && DECL_INITIAL (decl) != error_mark_node)
21532 set_block_origin_self (DECL_INITIAL (decl));
21537 /* Given a pointer to some BLOCK node, set the BLOCK_ABSTRACT flag to 1
21538 and if it wasn't 1 before, push it to abstract_vec vector.
21539 For all local decls and all local sub-blocks (recursively) do it
21540 too. */
21542 static void
21543 set_block_abstract_flags (tree stmt, vec<tree> &abstract_vec)
21545 tree local_decl;
21546 tree subblock;
21547 unsigned int i;
21549 if (!BLOCK_ABSTRACT (stmt))
21551 abstract_vec.safe_push (stmt);
21552 BLOCK_ABSTRACT (stmt) = 1;
21555 for (local_decl = BLOCK_VARS (stmt);
21556 local_decl != NULL_TREE;
21557 local_decl = DECL_CHAIN (local_decl))
21558 if (! DECL_EXTERNAL (local_decl))
21559 set_decl_abstract_flags (local_decl, abstract_vec);
21561 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
21563 local_decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
21564 if ((VAR_P (local_decl) && !TREE_STATIC (local_decl))
21565 || TREE_CODE (local_decl) == PARM_DECL)
21566 set_decl_abstract_flags (local_decl, abstract_vec);
21569 for (subblock = BLOCK_SUBBLOCKS (stmt);
21570 subblock != NULL_TREE;
21571 subblock = BLOCK_CHAIN (subblock))
21572 set_block_abstract_flags (subblock, abstract_vec);
21575 /* Given a pointer to some ..._DECL node, set DECL_ABSTRACT_P flag on it
21576 to 1 and if it wasn't 1 before, push to abstract_vec vector.
21577 In the case where the decl is a FUNCTION_DECL also set the abstract
21578 flags for all of the parameters, local vars, local
21579 blocks and sub-blocks (recursively). */
21581 static void
21582 set_decl_abstract_flags (tree decl, vec<tree> &abstract_vec)
21584 if (!DECL_ABSTRACT_P (decl))
21586 abstract_vec.safe_push (decl);
21587 DECL_ABSTRACT_P (decl) = 1;
21590 if (TREE_CODE (decl) == FUNCTION_DECL)
21592 tree arg;
21594 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
21595 if (!DECL_ABSTRACT_P (arg))
21597 abstract_vec.safe_push (arg);
21598 DECL_ABSTRACT_P (arg) = 1;
21600 if (DECL_INITIAL (decl) != NULL_TREE
21601 && DECL_INITIAL (decl) != error_mark_node)
21602 set_block_abstract_flags (DECL_INITIAL (decl), abstract_vec);
21606 /* Generate the DWARF2 info for the "abstract" instance of a function which we
21607 may later generate inlined and/or out-of-line instances of.
21609 FIXME: In the early-dwarf world, this function, and most of the
21610 DECL_ABSTRACT code should be obsoleted. The early DIE _is_
21611 the abstract instance. All we would need to do is annotate
21612 the early DIE with the appropriate DW_AT_inline in late
21613 dwarf (perhaps in gen_inlined_subroutine_die).
21615 However, we can't do this yet, because LTO streaming of DIEs
21616 has not been implemented yet. */
21618 static void
21619 dwarf2out_abstract_function (tree decl)
21621 dw_die_ref old_die;
21622 tree save_fn;
21623 tree context;
21624 hash_table<decl_loc_hasher> *old_decl_loc_table;
21625 hash_table<dw_loc_list_hasher> *old_cached_dw_loc_list_table;
21626 int old_call_site_count, old_tail_call_site_count;
21627 struct call_arg_loc_node *old_call_arg_locations;
21629 /* Make sure we have the actual abstract inline, not a clone. */
21630 decl = DECL_ORIGIN (decl);
21632 old_die = lookup_decl_die (decl);
21633 if (old_die && get_AT (old_die, DW_AT_inline))
21634 /* We've already generated the abstract instance. */
21635 return;
21637 /* We can be called while recursively when seeing block defining inlined subroutine
21638 DIE. Be sure to not clobber the outer location table nor use it or we would
21639 get locations in abstract instantces. */
21640 old_decl_loc_table = decl_loc_table;
21641 decl_loc_table = NULL;
21642 old_cached_dw_loc_list_table = cached_dw_loc_list_table;
21643 cached_dw_loc_list_table = NULL;
21644 old_call_arg_locations = call_arg_locations;
21645 call_arg_locations = NULL;
21646 old_call_site_count = call_site_count;
21647 call_site_count = -1;
21648 old_tail_call_site_count = tail_call_site_count;
21649 tail_call_site_count = -1;
21651 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
21652 we don't get confused by DECL_ABSTRACT_P. */
21653 if (debug_info_level > DINFO_LEVEL_TERSE)
21655 context = decl_class_context (decl);
21656 if (context)
21657 gen_type_die_for_member
21658 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
21661 /* Pretend we've just finished compiling this function. */
21662 save_fn = current_function_decl;
21663 current_function_decl = decl;
21665 auto_vec<tree, 64> abstract_vec;
21666 set_decl_abstract_flags (decl, abstract_vec);
21667 dwarf2out_decl (decl);
21668 unsigned int i;
21669 tree t;
21670 FOR_EACH_VEC_ELT (abstract_vec, i, t)
21671 if (TREE_CODE (t) == BLOCK)
21672 BLOCK_ABSTRACT (t) = 0;
21673 else
21674 DECL_ABSTRACT_P (t) = 0;
21676 current_function_decl = save_fn;
21677 decl_loc_table = old_decl_loc_table;
21678 cached_dw_loc_list_table = old_cached_dw_loc_list_table;
21679 call_arg_locations = old_call_arg_locations;
21680 call_site_count = old_call_site_count;
21681 tail_call_site_count = old_tail_call_site_count;
21684 /* Helper function of premark_used_types() which gets called through
21685 htab_traverse.
21687 Marks the DIE of a given type in *SLOT as perennial, so it never gets
21688 marked as unused by prune_unused_types. */
21690 bool
21691 premark_used_types_helper (tree const &type, void *)
21693 dw_die_ref die;
21695 die = lookup_type_die (type);
21696 if (die != NULL)
21697 die->die_perennial_p = 1;
21698 return true;
21701 /* Helper function of premark_types_used_by_global_vars which gets called
21702 through htab_traverse.
21704 Marks the DIE of a given type in *SLOT as perennial, so it never gets
21705 marked as unused by prune_unused_types. The DIE of the type is marked
21706 only if the global variable using the type will actually be emitted. */
21709 premark_types_used_by_global_vars_helper (types_used_by_vars_entry **slot,
21710 void *)
21712 struct types_used_by_vars_entry *entry;
21713 dw_die_ref die;
21715 entry = (struct types_used_by_vars_entry *) *slot;
21716 gcc_assert (entry->type != NULL
21717 && entry->var_decl != NULL);
21718 die = lookup_type_die (entry->type);
21719 if (die)
21721 /* Ask cgraph if the global variable really is to be emitted.
21722 If yes, then we'll keep the DIE of ENTRY->TYPE. */
21723 varpool_node *node = varpool_node::get (entry->var_decl);
21724 if (node && node->definition)
21726 die->die_perennial_p = 1;
21727 /* Keep the parent DIEs as well. */
21728 while ((die = die->die_parent) && die->die_perennial_p == 0)
21729 die->die_perennial_p = 1;
21732 return 1;
21735 /* Mark all members of used_types_hash as perennial. */
21737 static void
21738 premark_used_types (struct function *fun)
21740 if (fun && fun->used_types_hash)
21741 fun->used_types_hash->traverse<void *, premark_used_types_helper> (NULL);
21744 /* Mark all members of types_used_by_vars_entry as perennial. */
21746 static void
21747 premark_types_used_by_global_vars (void)
21749 if (types_used_by_vars_hash)
21750 types_used_by_vars_hash
21751 ->traverse<void *, premark_types_used_by_global_vars_helper> (NULL);
21754 /* Generate a DW_TAG_call_site DIE in function DECL under SUBR_DIE
21755 for CA_LOC call arg loc node. */
21757 static dw_die_ref
21758 gen_call_site_die (tree decl, dw_die_ref subr_die,
21759 struct call_arg_loc_node *ca_loc)
21761 dw_die_ref stmt_die = NULL, die;
21762 tree block = ca_loc->block;
21764 while (block
21765 && block != DECL_INITIAL (decl)
21766 && TREE_CODE (block) == BLOCK)
21768 stmt_die = BLOCK_DIE (block);
21769 if (stmt_die)
21770 break;
21771 block = BLOCK_SUPERCONTEXT (block);
21773 if (stmt_die == NULL)
21774 stmt_die = subr_die;
21775 die = new_die (dwarf_TAG (DW_TAG_call_site), stmt_die, NULL_TREE);
21776 add_AT_lbl_id (die, dwarf_AT (DW_AT_call_return_pc), ca_loc->label);
21777 if (ca_loc->tail_call_p)
21778 add_AT_flag (die, dwarf_AT (DW_AT_call_tail_call), 1);
21779 if (ca_loc->symbol_ref)
21781 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
21782 if (tdie)
21783 add_AT_die_ref (die, dwarf_AT (DW_AT_call_origin), tdie);
21784 else
21785 add_AT_addr (die, dwarf_AT (DW_AT_call_origin), ca_loc->symbol_ref,
21786 false);
21788 return die;
21791 /* Generate a DIE to represent a declared function (either file-scope or
21792 block-local). */
21794 static void
21795 gen_subprogram_die (tree decl, dw_die_ref context_die)
21797 tree origin = decl_ultimate_origin (decl);
21798 dw_die_ref subr_die;
21799 dw_die_ref old_die = lookup_decl_die (decl);
21801 /* This function gets called multiple times for different stages of
21802 the debug process. For example, for func() in this code:
21804 namespace S
21806 void func() { ... }
21809 ...we get called 4 times. Twice in early debug and twice in
21810 late debug:
21812 Early debug
21813 -----------
21815 1. Once while generating func() within the namespace. This is
21816 the declaration. The declaration bit below is set, as the
21817 context is the namespace.
21819 A new DIE will be generated with DW_AT_declaration set.
21821 2. Once for func() itself. This is the specification. The
21822 declaration bit below is clear as the context is the CU.
21824 We will use the cached DIE from (1) to create a new DIE with
21825 DW_AT_specification pointing to the declaration in (1).
21827 Late debug via rest_of_handle_final()
21828 -------------------------------------
21830 3. Once generating func() within the namespace. This is also the
21831 declaration, as in (1), but this time we will early exit below
21832 as we have a cached DIE and a declaration needs no additional
21833 annotations (no locations), as the source declaration line
21834 info is enough.
21836 4. Once for func() itself. As in (2), this is the specification,
21837 but this time we will re-use the cached DIE, and just annotate
21838 it with the location information that should now be available.
21840 For something without namespaces, but with abstract instances, we
21841 are also called a multiple times:
21843 class Base
21845 public:
21846 Base (); // constructor declaration (1)
21849 Base::Base () { } // constructor specification (2)
21851 Early debug
21852 -----------
21854 1. Once for the Base() constructor by virtue of it being a
21855 member of the Base class. This is done via
21856 rest_of_type_compilation.
21858 This is a declaration, so a new DIE will be created with
21859 DW_AT_declaration.
21861 2. Once for the Base() constructor definition, but this time
21862 while generating the abstract instance of the base
21863 constructor (__base_ctor) which is being generated via early
21864 debug of reachable functions.
21866 Even though we have a cached version of the declaration (1),
21867 we will create a DW_AT_specification of the declaration DIE
21868 in (1).
21870 3. Once for the __base_ctor itself, but this time, we generate
21871 an DW_AT_abstract_origin version of the DW_AT_specification in
21872 (2).
21874 Late debug via rest_of_handle_final
21875 -----------------------------------
21877 4. One final time for the __base_ctor (which will have a cached
21878 DIE with DW_AT_abstract_origin created in (3). This time,
21879 we will just annotate the location information now
21880 available.
21882 int declaration = (current_function_decl != decl
21883 || class_or_namespace_scope_p (context_die));
21885 /* Now that the C++ front end lazily declares artificial member fns, we
21886 might need to retrofit the declaration into its class. */
21887 if (!declaration && !origin && !old_die
21888 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
21889 && !class_or_namespace_scope_p (context_die)
21890 && debug_info_level > DINFO_LEVEL_TERSE)
21891 old_die = force_decl_die (decl);
21893 /* An inlined instance, tag a new DIE with DW_AT_abstract_origin. */
21894 if (origin != NULL)
21896 gcc_assert (!declaration || local_scope_p (context_die));
21898 /* Fixup die_parent for the abstract instance of a nested
21899 inline function. */
21900 if (old_die && old_die->die_parent == NULL)
21901 add_child_die (context_die, old_die);
21903 if (old_die && get_AT_ref (old_die, DW_AT_abstract_origin))
21905 /* If we have a DW_AT_abstract_origin we have a working
21906 cached version. */
21907 subr_die = old_die;
21909 else
21911 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
21912 add_abstract_origin_attribute (subr_die, origin);
21913 /* This is where the actual code for a cloned function is.
21914 Let's emit linkage name attribute for it. This helps
21915 debuggers to e.g, set breakpoints into
21916 constructors/destructors when the user asks "break
21917 K::K". */
21918 add_linkage_name (subr_die, decl);
21921 /* A cached copy, possibly from early dwarf generation. Reuse as
21922 much as possible. */
21923 else if (old_die)
21925 /* A declaration that has been previously dumped needs no
21926 additional information. */
21927 if (declaration)
21928 return;
21930 if (!get_AT_flag (old_die, DW_AT_declaration)
21931 /* We can have a normal definition following an inline one in the
21932 case of redefinition of GNU C extern inlines.
21933 It seems reasonable to use AT_specification in this case. */
21934 && !get_AT (old_die, DW_AT_inline))
21936 /* Detect and ignore this case, where we are trying to output
21937 something we have already output. */
21938 if (get_AT (old_die, DW_AT_low_pc)
21939 || get_AT (old_die, DW_AT_ranges))
21940 return;
21942 /* If we have no location information, this must be a
21943 partially generated DIE from early dwarf generation.
21944 Fall through and generate it. */
21947 /* If the definition comes from the same place as the declaration,
21948 maybe use the old DIE. We always want the DIE for this function
21949 that has the *_pc attributes to be under comp_unit_die so the
21950 debugger can find it. We also need to do this for abstract
21951 instances of inlines, since the spec requires the out-of-line copy
21952 to have the same parent. For local class methods, this doesn't
21953 apply; we just use the old DIE. */
21954 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
21955 struct dwarf_file_data * file_index = lookup_filename (s.file);
21956 if ((is_cu_die (old_die->die_parent)
21957 /* This condition fixes the inconsistency/ICE with the
21958 following Fortran test (or some derivative thereof) while
21959 building libgfortran:
21961 module some_m
21962 contains
21963 logical function funky (FLAG)
21964 funky = .true.
21965 end function
21966 end module
21968 || (old_die->die_parent
21969 && old_die->die_parent->die_tag == DW_TAG_module)
21970 || context_die == NULL)
21971 && (DECL_ARTIFICIAL (decl)
21972 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
21973 && (get_AT_unsigned (old_die, DW_AT_decl_line)
21974 == (unsigned) s.line)
21975 && (!debug_column_info
21976 || s.column == 0
21977 || (get_AT_unsigned (old_die, DW_AT_decl_column)
21978 == (unsigned) s.column)))))
21980 subr_die = old_die;
21982 /* Clear out the declaration attribute, but leave the
21983 parameters so they can be augmented with location
21984 information later. Unless this was a declaration, in
21985 which case, wipe out the nameless parameters and recreate
21986 them further down. */
21987 if (remove_AT (subr_die, DW_AT_declaration))
21990 remove_AT (subr_die, DW_AT_object_pointer);
21991 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
21994 /* Make a specification pointing to the previously built
21995 declaration. */
21996 else
21998 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
21999 add_AT_specification (subr_die, old_die);
22000 add_pubname (decl, subr_die);
22001 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
22002 add_AT_file (subr_die, DW_AT_decl_file, file_index);
22003 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
22004 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
22005 if (debug_column_info
22006 && s.column
22007 && (get_AT_unsigned (old_die, DW_AT_decl_column)
22008 != (unsigned) s.column))
22009 add_AT_unsigned (subr_die, DW_AT_decl_column, s.column);
22011 /* If the prototype had an 'auto' or 'decltype(auto)' return type,
22012 emit the real type on the definition die. */
22013 if (is_cxx () && debug_info_level > DINFO_LEVEL_TERSE)
22015 dw_die_ref die = get_AT_ref (old_die, DW_AT_type);
22016 if (die == auto_die || die == decltype_auto_die)
22017 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
22018 TYPE_UNQUALIFIED, false, context_die);
22021 /* When we process the method declaration, we haven't seen
22022 the out-of-class defaulted definition yet, so we have to
22023 recheck now. */
22024 if ((dwarf_version >= 5 || ! dwarf_strict)
22025 && !get_AT (subr_die, DW_AT_defaulted))
22027 int defaulted
22028 = lang_hooks.decls.decl_dwarf_attribute (decl,
22029 DW_AT_defaulted);
22030 if (defaulted != -1)
22032 /* Other values must have been handled before. */
22033 gcc_assert (defaulted == DW_DEFAULTED_out_of_class);
22034 add_AT_unsigned (subr_die, DW_AT_defaulted, defaulted);
22039 /* Create a fresh DIE for anything else. */
22040 else
22042 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
22044 if (TREE_PUBLIC (decl))
22045 add_AT_flag (subr_die, DW_AT_external, 1);
22047 add_name_and_src_coords_attributes (subr_die, decl);
22048 add_pubname (decl, subr_die);
22049 if (debug_info_level > DINFO_LEVEL_TERSE)
22051 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
22052 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
22053 TYPE_UNQUALIFIED, false, context_die);
22056 add_pure_or_virtual_attribute (subr_die, decl);
22057 if (DECL_ARTIFICIAL (decl))
22058 add_AT_flag (subr_die, DW_AT_artificial, 1);
22060 if (TREE_THIS_VOLATILE (decl) && (dwarf_version >= 5 || !dwarf_strict))
22061 add_AT_flag (subr_die, DW_AT_noreturn, 1);
22063 add_alignment_attribute (subr_die, decl);
22065 add_accessibility_attribute (subr_die, decl);
22068 /* Unless we have an existing non-declaration DIE, equate the new
22069 DIE. */
22070 if (!old_die || is_declaration_die (old_die))
22071 equate_decl_number_to_die (decl, subr_die);
22073 if (declaration)
22075 if (!old_die || !get_AT (old_die, DW_AT_inline))
22077 add_AT_flag (subr_die, DW_AT_declaration, 1);
22079 /* If this is an explicit function declaration then generate
22080 a DW_AT_explicit attribute. */
22081 if ((dwarf_version >= 3 || !dwarf_strict)
22082 && lang_hooks.decls.decl_dwarf_attribute (decl,
22083 DW_AT_explicit) == 1)
22084 add_AT_flag (subr_die, DW_AT_explicit, 1);
22086 /* If this is a C++11 deleted special function member then generate
22087 a DW_AT_deleted attribute. */
22088 if ((dwarf_version >= 5 || !dwarf_strict)
22089 && lang_hooks.decls.decl_dwarf_attribute (decl,
22090 DW_AT_deleted) == 1)
22091 add_AT_flag (subr_die, DW_AT_deleted, 1);
22093 /* If this is a C++11 defaulted special function member then
22094 generate a DW_AT_defaulted attribute. */
22095 if (dwarf_version >= 5 || !dwarf_strict)
22097 int defaulted
22098 = lang_hooks.decls.decl_dwarf_attribute (decl,
22099 DW_AT_defaulted);
22100 if (defaulted != -1)
22101 add_AT_unsigned (subr_die, DW_AT_defaulted, defaulted);
22104 /* If this is a C++11 non-static member function with & ref-qualifier
22105 then generate a DW_AT_reference attribute. */
22106 if ((dwarf_version >= 5 || !dwarf_strict)
22107 && lang_hooks.decls.decl_dwarf_attribute (decl,
22108 DW_AT_reference) == 1)
22109 add_AT_flag (subr_die, DW_AT_reference, 1);
22111 /* If this is a C++11 non-static member function with &&
22112 ref-qualifier then generate a DW_AT_reference attribute. */
22113 if ((dwarf_version >= 5 || !dwarf_strict)
22114 && lang_hooks.decls.decl_dwarf_attribute (decl,
22115 DW_AT_rvalue_reference)
22116 == 1)
22117 add_AT_flag (subr_die, DW_AT_rvalue_reference, 1);
22120 /* Tag abstract instances with DW_AT_inline. */
22121 else if (DECL_ABSTRACT_P (decl))
22123 if (DECL_DECLARED_INLINE_P (decl))
22125 if (cgraph_function_possibly_inlined_p (decl))
22126 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
22127 else
22128 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
22130 else
22132 if (cgraph_function_possibly_inlined_p (decl))
22133 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
22134 else
22135 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
22138 if (DECL_DECLARED_INLINE_P (decl)
22139 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
22140 add_AT_flag (subr_die, DW_AT_artificial, 1);
22142 /* For non DECL_EXTERNALs, if range information is available, fill
22143 the DIE with it. */
22144 else if (!DECL_EXTERNAL (decl) && !early_dwarf)
22146 HOST_WIDE_INT cfa_fb_offset;
22148 struct function *fun = DECL_STRUCT_FUNCTION (decl);
22150 if (!flag_reorder_blocks_and_partition)
22152 dw_fde_ref fde = fun->fde;
22153 if (fde->dw_fde_begin)
22155 /* We have already generated the labels. */
22156 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
22157 fde->dw_fde_end, false);
22159 else
22161 /* Create start/end labels and add the range. */
22162 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
22163 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
22164 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
22165 current_function_funcdef_no);
22166 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
22167 current_function_funcdef_no);
22168 add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
22169 false);
22172 #if VMS_DEBUGGING_INFO
22173 /* HP OpenVMS Industry Standard 64: DWARF Extensions
22174 Section 2.3 Prologue and Epilogue Attributes:
22175 When a breakpoint is set on entry to a function, it is generally
22176 desirable for execution to be suspended, not on the very first
22177 instruction of the function, but rather at a point after the
22178 function's frame has been set up, after any language defined local
22179 declaration processing has been completed, and before execution of
22180 the first statement of the function begins. Debuggers generally
22181 cannot properly determine where this point is. Similarly for a
22182 breakpoint set on exit from a function. The prologue and epilogue
22183 attributes allow a compiler to communicate the location(s) to use. */
22186 if (fde->dw_fde_vms_end_prologue)
22187 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
22188 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
22190 if (fde->dw_fde_vms_begin_epilogue)
22191 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
22192 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
22194 #endif
22197 else
22199 /* Generate pubnames entries for the split function code ranges. */
22200 dw_fde_ref fde = fun->fde;
22202 if (fde->dw_fde_second_begin)
22204 if (dwarf_version >= 3 || !dwarf_strict)
22206 /* We should use ranges for non-contiguous code section
22207 addresses. Use the actual code range for the initial
22208 section, since the HOT/COLD labels might precede an
22209 alignment offset. */
22210 bool range_list_added = false;
22211 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
22212 fde->dw_fde_end, &range_list_added,
22213 false);
22214 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
22215 fde->dw_fde_second_end,
22216 &range_list_added, false);
22217 if (range_list_added)
22218 add_ranges (NULL);
22220 else
22222 /* There is no real support in DW2 for this .. so we make
22223 a work-around. First, emit the pub name for the segment
22224 containing the function label. Then make and emit a
22225 simplified subprogram DIE for the second segment with the
22226 name pre-fixed by __hot/cold_sect_of_. We use the same
22227 linkage name for the second die so that gdb will find both
22228 sections when given "b foo". */
22229 const char *name = NULL;
22230 tree decl_name = DECL_NAME (decl);
22231 dw_die_ref seg_die;
22233 /* Do the 'primary' section. */
22234 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
22235 fde->dw_fde_end, false);
22237 /* Build a minimal DIE for the secondary section. */
22238 seg_die = new_die (DW_TAG_subprogram,
22239 subr_die->die_parent, decl);
22241 if (TREE_PUBLIC (decl))
22242 add_AT_flag (seg_die, DW_AT_external, 1);
22244 if (decl_name != NULL
22245 && IDENTIFIER_POINTER (decl_name) != NULL)
22247 name = dwarf2_name (decl, 1);
22248 if (! DECL_ARTIFICIAL (decl))
22249 add_src_coords_attributes (seg_die, decl);
22251 add_linkage_name (seg_die, decl);
22253 gcc_assert (name != NULL);
22254 add_pure_or_virtual_attribute (seg_die, decl);
22255 if (DECL_ARTIFICIAL (decl))
22256 add_AT_flag (seg_die, DW_AT_artificial, 1);
22258 name = concat ("__second_sect_of_", name, NULL);
22259 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
22260 fde->dw_fde_second_end, false);
22261 add_name_attribute (seg_die, name);
22262 if (want_pubnames ())
22263 add_pubname_string (name, seg_die);
22266 else
22267 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
22268 false);
22271 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
22273 /* We define the "frame base" as the function's CFA. This is more
22274 convenient for several reasons: (1) It's stable across the prologue
22275 and epilogue, which makes it better than just a frame pointer,
22276 (2) With dwarf3, there exists a one-byte encoding that allows us
22277 to reference the .debug_frame data by proxy, but failing that,
22278 (3) We can at least reuse the code inspection and interpretation
22279 code that determines the CFA position at various points in the
22280 function. */
22281 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
22283 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
22284 add_AT_loc (subr_die, DW_AT_frame_base, op);
22286 else
22288 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
22289 if (list->dw_loc_next)
22290 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
22291 else
22292 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
22295 /* Compute a displacement from the "steady-state frame pointer" to
22296 the CFA. The former is what all stack slots and argument slots
22297 will reference in the rtl; the latter is what we've told the
22298 debugger about. We'll need to adjust all frame_base references
22299 by this displacement. */
22300 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
22302 if (fun->static_chain_decl)
22304 /* DWARF requires here a location expression that computes the
22305 address of the enclosing subprogram's frame base. The machinery
22306 in tree-nested.c is supposed to store this specific address in the
22307 last field of the FRAME record. */
22308 const tree frame_type
22309 = TREE_TYPE (TREE_TYPE (fun->static_chain_decl));
22310 const tree fb_decl = tree_last (TYPE_FIELDS (frame_type));
22312 tree fb_expr
22313 = build1 (INDIRECT_REF, frame_type, fun->static_chain_decl);
22314 fb_expr = build3 (COMPONENT_REF, TREE_TYPE (fb_decl),
22315 fb_expr, fb_decl, NULL_TREE);
22317 add_AT_location_description (subr_die, DW_AT_static_link,
22318 loc_list_from_tree (fb_expr, 0, NULL));
22321 resolve_variable_values ();
22324 /* Generate child dies for template paramaters. */
22325 if (early_dwarf && debug_info_level > DINFO_LEVEL_TERSE)
22326 gen_generic_params_dies (decl);
22328 /* Now output descriptions of the arguments for this function. This gets
22329 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
22330 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
22331 `...' at the end of the formal parameter list. In order to find out if
22332 there was a trailing ellipsis or not, we must instead look at the type
22333 associated with the FUNCTION_DECL. This will be a node of type
22334 FUNCTION_TYPE. If the chain of type nodes hanging off of this
22335 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
22336 an ellipsis at the end. */
22338 /* In the case where we are describing a mere function declaration, all we
22339 need to do here (and all we *can* do here) is to describe the *types* of
22340 its formal parameters. */
22341 if (debug_info_level <= DINFO_LEVEL_TERSE)
22343 else if (declaration)
22344 gen_formal_types_die (decl, subr_die);
22345 else
22347 /* Generate DIEs to represent all known formal parameters. */
22348 tree parm = DECL_ARGUMENTS (decl);
22349 tree generic_decl = early_dwarf
22350 ? lang_hooks.decls.get_generic_function_decl (decl) : NULL;
22351 tree generic_decl_parm = generic_decl
22352 ? DECL_ARGUMENTS (generic_decl)
22353 : NULL;
22355 /* Now we want to walk the list of parameters of the function and
22356 emit their relevant DIEs.
22358 We consider the case of DECL being an instance of a generic function
22359 as well as it being a normal function.
22361 If DECL is an instance of a generic function we walk the
22362 parameters of the generic function declaration _and_ the parameters of
22363 DECL itself. This is useful because we want to emit specific DIEs for
22364 function parameter packs and those are declared as part of the
22365 generic function declaration. In that particular case,
22366 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
22367 That DIE has children DIEs representing the set of arguments
22368 of the pack. Note that the set of pack arguments can be empty.
22369 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
22370 children DIE.
22372 Otherwise, we just consider the parameters of DECL. */
22373 while (generic_decl_parm || parm)
22375 if (generic_decl_parm
22376 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
22377 gen_formal_parameter_pack_die (generic_decl_parm,
22378 parm, subr_die,
22379 &parm);
22380 else if (parm && !POINTER_BOUNDS_P (parm))
22382 dw_die_ref parm_die = gen_decl_die (parm, NULL, NULL, subr_die);
22384 if (parm == DECL_ARGUMENTS (decl)
22385 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
22386 && parm_die
22387 && (dwarf_version >= 3 || !dwarf_strict))
22388 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
22390 parm = DECL_CHAIN (parm);
22392 else if (parm)
22393 parm = DECL_CHAIN (parm);
22395 if (generic_decl_parm)
22396 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
22399 /* Decide whether we need an unspecified_parameters DIE at the end.
22400 There are 2 more cases to do this for: 1) the ansi ... declaration -
22401 this is detectable when the end of the arg list is not a
22402 void_type_node 2) an unprototyped function declaration (not a
22403 definition). This just means that we have no info about the
22404 parameters at all. */
22405 if (early_dwarf)
22407 if (prototype_p (TREE_TYPE (decl)))
22409 /* This is the prototyped case, check for.... */
22410 if (stdarg_p (TREE_TYPE (decl)))
22411 gen_unspecified_parameters_die (decl, subr_die);
22413 else if (DECL_INITIAL (decl) == NULL_TREE)
22414 gen_unspecified_parameters_die (decl, subr_die);
22418 if (subr_die != old_die)
22419 /* Add the calling convention attribute if requested. */
22420 add_calling_convention_attribute (subr_die, decl);
22422 /* Output Dwarf info for all of the stuff within the body of the function
22423 (if it has one - it may be just a declaration).
22425 OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
22426 a function. This BLOCK actually represents the outermost binding contour
22427 for the function, i.e. the contour in which the function's formal
22428 parameters and labels get declared. Curiously, it appears that the front
22429 end doesn't actually put the PARM_DECL nodes for the current function onto
22430 the BLOCK_VARS list for this outer scope, but are strung off of the
22431 DECL_ARGUMENTS list for the function instead.
22433 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
22434 the LABEL_DECL nodes for the function however, and we output DWARF info
22435 for those in decls_for_scope. Just within the `outer_scope' there will be
22436 a BLOCK node representing the function's outermost pair of curly braces,
22437 and any blocks used for the base and member initializers of a C++
22438 constructor function. */
22439 tree outer_scope = DECL_INITIAL (decl);
22440 if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
22442 int call_site_note_count = 0;
22443 int tail_call_site_note_count = 0;
22445 /* Emit a DW_TAG_variable DIE for a named return value. */
22446 if (DECL_NAME (DECL_RESULT (decl)))
22447 gen_decl_die (DECL_RESULT (decl), NULL, NULL, subr_die);
22449 /* The first time through decls_for_scope we will generate the
22450 DIEs for the locals. The second time, we fill in the
22451 location info. */
22452 decls_for_scope (outer_scope, subr_die);
22454 if (call_arg_locations && (!dwarf_strict || dwarf_version >= 5))
22456 struct call_arg_loc_node *ca_loc;
22457 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
22459 dw_die_ref die = NULL;
22460 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
22461 rtx arg, next_arg;
22463 for (arg = (ca_loc->call_arg_loc_note != NULL_RTX
22464 ? NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note)
22465 : NULL_RTX);
22466 arg; arg = next_arg)
22468 dw_loc_descr_ref reg, val;
22469 machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
22470 dw_die_ref cdie, tdie = NULL;
22472 next_arg = XEXP (arg, 1);
22473 if (REG_P (XEXP (XEXP (arg, 0), 0))
22474 && next_arg
22475 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
22476 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
22477 && REGNO (XEXP (XEXP (arg, 0), 0))
22478 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
22479 next_arg = XEXP (next_arg, 1);
22480 if (mode == VOIDmode)
22482 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
22483 if (mode == VOIDmode)
22484 mode = GET_MODE (XEXP (arg, 0));
22486 if (mode == VOIDmode || mode == BLKmode)
22487 continue;
22488 /* Get dynamic information about call target only if we
22489 have no static information: we cannot generate both
22490 DW_AT_call_origin and DW_AT_call_target
22491 attributes. */
22492 if (ca_loc->symbol_ref == NULL_RTX)
22494 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
22496 tloc = XEXP (XEXP (arg, 0), 1);
22497 continue;
22499 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
22500 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
22502 tlocc = XEXP (XEXP (arg, 0), 1);
22503 continue;
22506 reg = NULL;
22507 if (REG_P (XEXP (XEXP (arg, 0), 0)))
22508 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
22509 VAR_INIT_STATUS_INITIALIZED);
22510 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
22512 rtx mem = XEXP (XEXP (arg, 0), 0);
22513 reg = mem_loc_descriptor (XEXP (mem, 0),
22514 get_address_mode (mem),
22515 GET_MODE (mem),
22516 VAR_INIT_STATUS_INITIALIZED);
22518 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
22519 == DEBUG_PARAMETER_REF)
22521 tree tdecl
22522 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
22523 tdie = lookup_decl_die (tdecl);
22524 if (tdie == NULL)
22525 continue;
22527 else
22528 continue;
22529 if (reg == NULL
22530 && GET_CODE (XEXP (XEXP (arg, 0), 0))
22531 != DEBUG_PARAMETER_REF)
22532 continue;
22533 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
22534 VOIDmode,
22535 VAR_INIT_STATUS_INITIALIZED);
22536 if (val == NULL)
22537 continue;
22538 if (die == NULL)
22539 die = gen_call_site_die (decl, subr_die, ca_loc);
22540 cdie = new_die (dwarf_TAG (DW_TAG_call_site_parameter), die,
22541 NULL_TREE);
22542 if (reg != NULL)
22543 add_AT_loc (cdie, DW_AT_location, reg);
22544 else if (tdie != NULL)
22545 add_AT_die_ref (cdie, dwarf_AT (DW_AT_call_parameter),
22546 tdie);
22547 add_AT_loc (cdie, dwarf_AT (DW_AT_call_value), val);
22548 if (next_arg != XEXP (arg, 1))
22550 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
22551 if (mode == VOIDmode)
22552 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
22553 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
22554 0), 1),
22555 mode, VOIDmode,
22556 VAR_INIT_STATUS_INITIALIZED);
22557 if (val != NULL)
22558 add_AT_loc (cdie, dwarf_AT (DW_AT_call_data_value),
22559 val);
22562 if (die == NULL
22563 && (ca_loc->symbol_ref || tloc))
22564 die = gen_call_site_die (decl, subr_die, ca_loc);
22565 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
22567 dw_loc_descr_ref tval = NULL;
22569 if (tloc != NULL_RTX)
22570 tval = mem_loc_descriptor (tloc,
22571 GET_MODE (tloc) == VOIDmode
22572 ? Pmode : GET_MODE (tloc),
22573 VOIDmode,
22574 VAR_INIT_STATUS_INITIALIZED);
22575 if (tval)
22576 add_AT_loc (die, dwarf_AT (DW_AT_call_target), tval);
22577 else if (tlocc != NULL_RTX)
22579 tval = mem_loc_descriptor (tlocc,
22580 GET_MODE (tlocc) == VOIDmode
22581 ? Pmode : GET_MODE (tlocc),
22582 VOIDmode,
22583 VAR_INIT_STATUS_INITIALIZED);
22584 if (tval)
22585 add_AT_loc (die,
22586 dwarf_AT (DW_AT_call_target_clobbered),
22587 tval);
22590 if (die != NULL)
22592 call_site_note_count++;
22593 if (ca_loc->tail_call_p)
22594 tail_call_site_note_count++;
22598 call_arg_locations = NULL;
22599 call_arg_loc_last = NULL;
22600 if (tail_call_site_count >= 0
22601 && tail_call_site_count == tail_call_site_note_count
22602 && (!dwarf_strict || dwarf_version >= 5))
22604 if (call_site_count >= 0
22605 && call_site_count == call_site_note_count)
22606 add_AT_flag (subr_die, dwarf_AT (DW_AT_call_all_calls), 1);
22607 else
22608 add_AT_flag (subr_die, dwarf_AT (DW_AT_call_all_tail_calls), 1);
22610 call_site_count = -1;
22611 tail_call_site_count = -1;
22614 /* Mark used types after we have created DIEs for the functions scopes. */
22615 premark_used_types (DECL_STRUCT_FUNCTION (decl));
22618 /* Returns a hash value for X (which really is a die_struct). */
22620 hashval_t
22621 block_die_hasher::hash (die_struct *d)
22623 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
22626 /* Return nonzero if decl_id and die_parent of die_struct X is the same
22627 as decl_id and die_parent of die_struct Y. */
22629 bool
22630 block_die_hasher::equal (die_struct *x, die_struct *y)
22632 return x->decl_id == y->decl_id && x->die_parent == y->die_parent;
22635 /* Return TRUE if DECL, which may have been previously generated as
22636 OLD_DIE, is a candidate for a DW_AT_specification. DECLARATION is
22637 true if decl (or its origin) is either an extern declaration or a
22638 class/namespace scoped declaration.
22640 The declare_in_namespace support causes us to get two DIEs for one
22641 variable, both of which are declarations. We want to avoid
22642 considering one to be a specification, so we must test for
22643 DECLARATION and DW_AT_declaration. */
22644 static inline bool
22645 decl_will_get_specification_p (dw_die_ref old_die, tree decl, bool declaration)
22647 return (old_die && TREE_STATIC (decl) && !declaration
22648 && get_AT_flag (old_die, DW_AT_declaration) == 1);
22651 /* Return true if DECL is a local static. */
22653 static inline bool
22654 local_function_static (tree decl)
22656 gcc_assert (VAR_P (decl));
22657 return TREE_STATIC (decl)
22658 && DECL_CONTEXT (decl)
22659 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL;
22662 /* Generate a DIE to represent a declared data object.
22663 Either DECL or ORIGIN must be non-null. */
22665 static void
22666 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
22668 HOST_WIDE_INT off = 0;
22669 tree com_decl;
22670 tree decl_or_origin = decl ? decl : origin;
22671 tree ultimate_origin;
22672 dw_die_ref var_die;
22673 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
22674 bool declaration = (DECL_EXTERNAL (decl_or_origin)
22675 || class_or_namespace_scope_p (context_die));
22676 bool specialization_p = false;
22677 bool no_linkage_name = false;
22679 /* While C++ inline static data members have definitions inside of the
22680 class, force the first DIE to be a declaration, then let gen_member_die
22681 reparent it to the class context and call gen_variable_die again
22682 to create the outside of the class DIE for the definition. */
22683 if (!declaration
22684 && old_die == NULL
22685 && decl
22686 && DECL_CONTEXT (decl)
22687 && TYPE_P (DECL_CONTEXT (decl))
22688 && lang_hooks.decls.decl_dwarf_attribute (decl, DW_AT_inline) != -1)
22690 declaration = true;
22691 if (dwarf_version < 5)
22692 no_linkage_name = true;
22695 ultimate_origin = decl_ultimate_origin (decl_or_origin);
22696 if (decl || ultimate_origin)
22697 origin = ultimate_origin;
22698 com_decl = fortran_common (decl_or_origin, &off);
22700 /* Symbol in common gets emitted as a child of the common block, in the form
22701 of a data member. */
22702 if (com_decl)
22704 dw_die_ref com_die;
22705 dw_loc_list_ref loc = NULL;
22706 die_node com_die_arg;
22708 var_die = lookup_decl_die (decl_or_origin);
22709 if (var_die)
22711 if (! early_dwarf && get_AT (var_die, DW_AT_location) == NULL)
22713 loc = loc_list_from_tree (com_decl, off ? 1 : 2, NULL);
22714 if (loc)
22716 if (off)
22718 /* Optimize the common case. */
22719 if (single_element_loc_list_p (loc)
22720 && loc->expr->dw_loc_opc == DW_OP_addr
22721 && loc->expr->dw_loc_next == NULL
22722 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
22723 == SYMBOL_REF)
22725 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
22726 loc->expr->dw_loc_oprnd1.v.val_addr
22727 = plus_constant (GET_MODE (x), x , off);
22729 else
22730 loc_list_plus_const (loc, off);
22732 add_AT_location_description (var_die, DW_AT_location, loc);
22733 remove_AT (var_die, DW_AT_declaration);
22736 return;
22739 if (common_block_die_table == NULL)
22740 common_block_die_table = hash_table<block_die_hasher>::create_ggc (10);
22742 com_die_arg.decl_id = DECL_UID (com_decl);
22743 com_die_arg.die_parent = context_die;
22744 com_die = common_block_die_table->find (&com_die_arg);
22745 if (! early_dwarf)
22746 loc = loc_list_from_tree (com_decl, 2, NULL);
22747 if (com_die == NULL)
22749 const char *cnam
22750 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
22751 die_node **slot;
22753 com_die = new_die (DW_TAG_common_block, context_die, decl);
22754 add_name_and_src_coords_attributes (com_die, com_decl);
22755 if (loc)
22757 add_AT_location_description (com_die, DW_AT_location, loc);
22758 /* Avoid sharing the same loc descriptor between
22759 DW_TAG_common_block and DW_TAG_variable. */
22760 loc = loc_list_from_tree (com_decl, 2, NULL);
22762 else if (DECL_EXTERNAL (decl_or_origin))
22763 add_AT_flag (com_die, DW_AT_declaration, 1);
22764 if (want_pubnames ())
22765 add_pubname_string (cnam, com_die); /* ??? needed? */
22766 com_die->decl_id = DECL_UID (com_decl);
22767 slot = common_block_die_table->find_slot (com_die, INSERT);
22768 *slot = com_die;
22770 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
22772 add_AT_location_description (com_die, DW_AT_location, loc);
22773 loc = loc_list_from_tree (com_decl, 2, NULL);
22774 remove_AT (com_die, DW_AT_declaration);
22776 var_die = new_die (DW_TAG_variable, com_die, decl);
22777 add_name_and_src_coords_attributes (var_die, decl_or_origin);
22778 add_type_attribute (var_die, TREE_TYPE (decl_or_origin),
22779 decl_quals (decl_or_origin), false,
22780 context_die);
22781 add_alignment_attribute (var_die, decl);
22782 add_AT_flag (var_die, DW_AT_external, 1);
22783 if (loc)
22785 if (off)
22787 /* Optimize the common case. */
22788 if (single_element_loc_list_p (loc)
22789 && loc->expr->dw_loc_opc == DW_OP_addr
22790 && loc->expr->dw_loc_next == NULL
22791 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
22793 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
22794 loc->expr->dw_loc_oprnd1.v.val_addr
22795 = plus_constant (GET_MODE (x), x, off);
22797 else
22798 loc_list_plus_const (loc, off);
22800 add_AT_location_description (var_die, DW_AT_location, loc);
22802 else if (DECL_EXTERNAL (decl_or_origin))
22803 add_AT_flag (var_die, DW_AT_declaration, 1);
22804 if (decl)
22805 equate_decl_number_to_die (decl, var_die);
22806 return;
22809 if (old_die)
22811 if (declaration)
22813 /* A declaration that has been previously dumped, needs no
22814 further annotations, since it doesn't need location on
22815 the second pass. */
22816 return;
22818 else if (decl_will_get_specification_p (old_die, decl, declaration)
22819 && !get_AT (old_die, DW_AT_specification))
22821 /* Fall-thru so we can make a new variable die along with a
22822 DW_AT_specification. */
22824 else if (origin && old_die->die_parent != context_die)
22826 /* If we will be creating an inlined instance, we need a
22827 new DIE that will get annotated with
22828 DW_AT_abstract_origin. Clear things so we can get a
22829 new DIE. */
22830 gcc_assert (!DECL_ABSTRACT_P (decl));
22831 old_die = NULL;
22833 else
22835 /* If a DIE was dumped early, it still needs location info.
22836 Skip to where we fill the location bits. */
22837 var_die = old_die;
22838 goto gen_variable_die_location;
22842 /* For static data members, the declaration in the class is supposed
22843 to have DW_TAG_member tag in DWARF{3,4} and we emit it for compatibility
22844 also in DWARF2; the specification should still be DW_TAG_variable
22845 referencing the DW_TAG_member DIE. */
22846 if (declaration && class_scope_p (context_die) && dwarf_version < 5)
22847 var_die = new_die (DW_TAG_member, context_die, decl);
22848 else
22849 var_die = new_die (DW_TAG_variable, context_die, decl);
22851 if (origin != NULL)
22852 add_abstract_origin_attribute (var_die, origin);
22854 /* Loop unrolling can create multiple blocks that refer to the same
22855 static variable, so we must test for the DW_AT_declaration flag.
22857 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
22858 copy decls and set the DECL_ABSTRACT_P flag on them instead of
22859 sharing them.
22861 ??? Duplicated blocks have been rewritten to use .debug_ranges. */
22862 else if (decl_will_get_specification_p (old_die, decl, declaration))
22864 /* This is a definition of a C++ class level static. */
22865 add_AT_specification (var_die, old_die);
22866 specialization_p = true;
22867 if (DECL_NAME (decl))
22869 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
22870 struct dwarf_file_data * file_index = lookup_filename (s.file);
22872 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
22873 add_AT_file (var_die, DW_AT_decl_file, file_index);
22875 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
22876 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
22878 if (debug_column_info
22879 && s.column
22880 && (get_AT_unsigned (old_die, DW_AT_decl_column)
22881 != (unsigned) s.column))
22882 add_AT_unsigned (var_die, DW_AT_decl_column, s.column);
22884 if (old_die->die_tag == DW_TAG_member)
22885 add_linkage_name (var_die, decl);
22888 else
22889 add_name_and_src_coords_attributes (var_die, decl, no_linkage_name);
22891 if ((origin == NULL && !specialization_p)
22892 || (origin != NULL
22893 && !DECL_ABSTRACT_P (decl_or_origin)
22894 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
22895 decl_function_context
22896 (decl_or_origin))))
22898 tree type = TREE_TYPE (decl_or_origin);
22900 if (decl_by_reference_p (decl_or_origin))
22901 add_type_attribute (var_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
22902 context_die);
22903 else
22904 add_type_attribute (var_die, type, decl_quals (decl_or_origin), false,
22905 context_die);
22908 if (origin == NULL && !specialization_p)
22910 if (TREE_PUBLIC (decl))
22911 add_AT_flag (var_die, DW_AT_external, 1);
22913 if (DECL_ARTIFICIAL (decl))
22914 add_AT_flag (var_die, DW_AT_artificial, 1);
22916 add_alignment_attribute (var_die, decl);
22918 add_accessibility_attribute (var_die, decl);
22921 if (declaration)
22922 add_AT_flag (var_die, DW_AT_declaration, 1);
22924 if (decl && (DECL_ABSTRACT_P (decl)
22925 || !old_die || is_declaration_die (old_die)))
22926 equate_decl_number_to_die (decl, var_die);
22928 gen_variable_die_location:
22929 if (! declaration
22930 && (! DECL_ABSTRACT_P (decl_or_origin)
22931 /* Local static vars are shared between all clones/inlines,
22932 so emit DW_AT_location on the abstract DIE if DECL_RTL is
22933 already set. */
22934 || (VAR_P (decl_or_origin)
22935 && TREE_STATIC (decl_or_origin)
22936 && DECL_RTL_SET_P (decl_or_origin))))
22938 if (early_dwarf)
22939 add_pubname (decl_or_origin, var_die);
22940 else
22941 add_location_or_const_value_attribute (var_die, decl_or_origin,
22942 decl == NULL);
22944 else
22945 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
22947 if ((dwarf_version >= 4 || !dwarf_strict)
22948 && lang_hooks.decls.decl_dwarf_attribute (decl_or_origin,
22949 DW_AT_const_expr) == 1
22950 && !get_AT (var_die, DW_AT_const_expr)
22951 && !specialization_p)
22952 add_AT_flag (var_die, DW_AT_const_expr, 1);
22954 if (!dwarf_strict)
22956 int inl = lang_hooks.decls.decl_dwarf_attribute (decl_or_origin,
22957 DW_AT_inline);
22958 if (inl != -1
22959 && !get_AT (var_die, DW_AT_inline)
22960 && !specialization_p)
22961 add_AT_unsigned (var_die, DW_AT_inline, inl);
22965 /* Generate a DIE to represent a named constant. */
22967 static void
22968 gen_const_die (tree decl, dw_die_ref context_die)
22970 dw_die_ref const_die;
22971 tree type = TREE_TYPE (decl);
22973 const_die = lookup_decl_die (decl);
22974 if (const_die)
22975 return;
22977 const_die = new_die (DW_TAG_constant, context_die, decl);
22978 equate_decl_number_to_die (decl, const_die);
22979 add_name_and_src_coords_attributes (const_die, decl);
22980 add_type_attribute (const_die, type, TYPE_QUAL_CONST, false, context_die);
22981 if (TREE_PUBLIC (decl))
22982 add_AT_flag (const_die, DW_AT_external, 1);
22983 if (DECL_ARTIFICIAL (decl))
22984 add_AT_flag (const_die, DW_AT_artificial, 1);
22985 tree_add_const_value_attribute_for_decl (const_die, decl);
22988 /* Generate a DIE to represent a label identifier. */
22990 static void
22991 gen_label_die (tree decl, dw_die_ref context_die)
22993 tree origin = decl_ultimate_origin (decl);
22994 dw_die_ref lbl_die = lookup_decl_die (decl);
22995 rtx insn;
22996 char label[MAX_ARTIFICIAL_LABEL_BYTES];
22998 if (!lbl_die)
23000 lbl_die = new_die (DW_TAG_label, context_die, decl);
23001 equate_decl_number_to_die (decl, lbl_die);
23003 if (origin != NULL)
23004 add_abstract_origin_attribute (lbl_die, origin);
23005 else
23006 add_name_and_src_coords_attributes (lbl_die, decl);
23009 if (DECL_ABSTRACT_P (decl))
23010 equate_decl_number_to_die (decl, lbl_die);
23011 else if (! early_dwarf)
23013 insn = DECL_RTL_IF_SET (decl);
23015 /* Deleted labels are programmer specified labels which have been
23016 eliminated because of various optimizations. We still emit them
23017 here so that it is possible to put breakpoints on them. */
23018 if (insn
23019 && (LABEL_P (insn)
23020 || ((NOTE_P (insn)
23021 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
23023 /* When optimization is enabled (via -O) some parts of the compiler
23024 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
23025 represent source-level labels which were explicitly declared by
23026 the user. This really shouldn't be happening though, so catch
23027 it if it ever does happen. */
23028 gcc_assert (!as_a<rtx_insn *> (insn)->deleted ());
23030 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
23031 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
23033 else if (insn
23034 && NOTE_P (insn)
23035 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
23036 && CODE_LABEL_NUMBER (insn) != -1)
23038 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
23039 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
23044 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
23045 attributes to the DIE for a block STMT, to describe where the inlined
23046 function was called from. This is similar to add_src_coords_attributes. */
23048 static inline void
23049 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
23051 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
23053 if (dwarf_version >= 3 || !dwarf_strict)
23055 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
23056 add_AT_unsigned (die, DW_AT_call_line, s.line);
23057 if (debug_column_info && s.column)
23058 add_AT_unsigned (die, DW_AT_call_column, s.column);
23063 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
23064 Add low_pc and high_pc attributes to the DIE for a block STMT. */
23066 static inline void
23067 add_high_low_attributes (tree stmt, dw_die_ref die)
23069 char label[MAX_ARTIFICIAL_LABEL_BYTES];
23071 if (BLOCK_FRAGMENT_CHAIN (stmt)
23072 && (dwarf_version >= 3 || !dwarf_strict))
23074 tree chain, superblock = NULL_TREE;
23075 dw_die_ref pdie;
23076 dw_attr_node *attr = NULL;
23078 if (inlined_function_outer_scope_p (stmt))
23080 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
23081 BLOCK_NUMBER (stmt));
23082 add_AT_lbl_id (die, DW_AT_entry_pc, label);
23085 /* Optimize duplicate .debug_ranges lists or even tails of
23086 lists. If this BLOCK has same ranges as its supercontext,
23087 lookup DW_AT_ranges attribute in the supercontext (and
23088 recursively so), verify that the ranges_table contains the
23089 right values and use it instead of adding a new .debug_range. */
23090 for (chain = stmt, pdie = die;
23091 BLOCK_SAME_RANGE (chain);
23092 chain = BLOCK_SUPERCONTEXT (chain))
23094 dw_attr_node *new_attr;
23096 pdie = pdie->die_parent;
23097 if (pdie == NULL)
23098 break;
23099 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
23100 break;
23101 new_attr = get_AT (pdie, DW_AT_ranges);
23102 if (new_attr == NULL
23103 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
23104 break;
23105 attr = new_attr;
23106 superblock = BLOCK_SUPERCONTEXT (chain);
23108 if (attr != NULL
23109 && ((*ranges_table)[attr->dw_attr_val.v.val_offset].num
23110 == BLOCK_NUMBER (superblock))
23111 && BLOCK_FRAGMENT_CHAIN (superblock))
23113 unsigned long off = attr->dw_attr_val.v.val_offset;
23114 unsigned long supercnt = 0, thiscnt = 0;
23115 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
23116 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
23118 ++supercnt;
23119 gcc_checking_assert ((*ranges_table)[off + supercnt].num
23120 == BLOCK_NUMBER (chain));
23122 gcc_checking_assert ((*ranges_table)[off + supercnt + 1].num == 0);
23123 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
23124 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
23125 ++thiscnt;
23126 gcc_assert (supercnt >= thiscnt);
23127 add_AT_range_list (die, DW_AT_ranges, off + supercnt - thiscnt,
23128 false);
23129 note_rnglist_head (off + supercnt - thiscnt);
23130 return;
23133 unsigned int offset = add_ranges (stmt, true);
23134 add_AT_range_list (die, DW_AT_ranges, offset, false);
23135 note_rnglist_head (offset);
23137 bool prev_in_cold = BLOCK_IN_COLD_SECTION_P (stmt);
23138 chain = BLOCK_FRAGMENT_CHAIN (stmt);
23141 add_ranges (chain, prev_in_cold != BLOCK_IN_COLD_SECTION_P (chain));
23142 prev_in_cold = BLOCK_IN_COLD_SECTION_P (chain);
23143 chain = BLOCK_FRAGMENT_CHAIN (chain);
23145 while (chain);
23146 add_ranges (NULL);
23148 else
23150 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
23151 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
23152 BLOCK_NUMBER (stmt));
23153 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
23154 BLOCK_NUMBER (stmt));
23155 add_AT_low_high_pc (die, label, label_high, false);
23159 /* Generate a DIE for a lexical block. */
23161 static void
23162 gen_lexical_block_die (tree stmt, dw_die_ref context_die)
23164 dw_die_ref old_die = BLOCK_DIE (stmt);
23165 dw_die_ref stmt_die = NULL;
23166 if (!old_die)
23168 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
23169 BLOCK_DIE (stmt) = stmt_die;
23172 if (BLOCK_ABSTRACT (stmt))
23174 if (old_die)
23176 /* This must have been generated early and it won't even
23177 need location information since it's a DW_AT_inline
23178 function. */
23179 if (flag_checking)
23180 for (dw_die_ref c = context_die; c; c = c->die_parent)
23181 if (c->die_tag == DW_TAG_inlined_subroutine
23182 || c->die_tag == DW_TAG_subprogram)
23184 gcc_assert (get_AT (c, DW_AT_inline));
23185 break;
23187 return;
23190 else if (BLOCK_ABSTRACT_ORIGIN (stmt))
23192 /* If this is an inlined instance, create a new lexical die for
23193 anything below to attach DW_AT_abstract_origin to. */
23194 if (old_die)
23196 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
23197 BLOCK_DIE (stmt) = stmt_die;
23198 old_die = NULL;
23201 tree origin = block_ultimate_origin (stmt);
23202 if (origin != NULL_TREE && origin != stmt)
23203 add_abstract_origin_attribute (stmt_die, origin);
23206 if (old_die)
23207 stmt_die = old_die;
23209 /* A non abstract block whose blocks have already been reordered
23210 should have the instruction range for this block. If so, set the
23211 high/low attributes. */
23212 if (!early_dwarf && !BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
23214 gcc_assert (stmt_die);
23215 add_high_low_attributes (stmt, stmt_die);
23218 decls_for_scope (stmt, stmt_die);
23221 /* Generate a DIE for an inlined subprogram. */
23223 static void
23224 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die)
23226 tree decl;
23228 /* The instance of function that is effectively being inlined shall not
23229 be abstract. */
23230 gcc_assert (! BLOCK_ABSTRACT (stmt));
23232 decl = block_ultimate_origin (stmt);
23234 /* Make sure any inlined functions are known to be inlineable. */
23235 gcc_checking_assert (DECL_ABSTRACT_P (decl)
23236 || cgraph_function_possibly_inlined_p (decl));
23238 /* Emit info for the abstract instance first, if we haven't yet. We
23239 must emit this even if the block is abstract, otherwise when we
23240 emit the block below (or elsewhere), we may end up trying to emit
23241 a die whose origin die hasn't been emitted, and crashing. */
23242 dwarf2out_abstract_function (decl);
23244 if (! BLOCK_ABSTRACT (stmt))
23246 dw_die_ref subr_die
23247 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
23249 if (call_arg_locations)
23250 BLOCK_DIE (stmt) = subr_die;
23251 add_abstract_origin_attribute (subr_die, decl);
23252 if (TREE_ASM_WRITTEN (stmt))
23253 add_high_low_attributes (stmt, subr_die);
23254 add_call_src_coords_attributes (stmt, subr_die);
23256 decls_for_scope (stmt, subr_die);
23260 /* Generate a DIE for a field in a record, or structure. CTX is required: see
23261 the comment for VLR_CONTEXT. */
23263 static void
23264 gen_field_die (tree decl, struct vlr_context *ctx, dw_die_ref context_die)
23266 dw_die_ref decl_die;
23268 if (TREE_TYPE (decl) == error_mark_node)
23269 return;
23271 decl_die = new_die (DW_TAG_member, context_die, decl);
23272 add_name_and_src_coords_attributes (decl_die, decl);
23273 add_type_attribute (decl_die, member_declared_type (decl), decl_quals (decl),
23274 TYPE_REVERSE_STORAGE_ORDER (DECL_FIELD_CONTEXT (decl)),
23275 context_die);
23277 if (DECL_BIT_FIELD_TYPE (decl))
23279 add_byte_size_attribute (decl_die, decl);
23280 add_bit_size_attribute (decl_die, decl);
23281 add_bit_offset_attribute (decl_die, decl, ctx);
23284 add_alignment_attribute (decl_die, decl);
23286 /* If we have a variant part offset, then we are supposed to process a member
23287 of a QUAL_UNION_TYPE, which is how we represent variant parts in
23288 trees. */
23289 gcc_assert (ctx->variant_part_offset == NULL_TREE
23290 || TREE_CODE (DECL_FIELD_CONTEXT (decl)) != QUAL_UNION_TYPE);
23291 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
23292 add_data_member_location_attribute (decl_die, decl, ctx);
23294 if (DECL_ARTIFICIAL (decl))
23295 add_AT_flag (decl_die, DW_AT_artificial, 1);
23297 add_accessibility_attribute (decl_die, decl);
23299 /* Equate decl number to die, so that we can look up this decl later on. */
23300 equate_decl_number_to_die (decl, decl_die);
23303 #if 0
23304 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
23305 Use modified_type_die instead.
23306 We keep this code here just in case these types of DIEs may be needed to
23307 represent certain things in other languages (e.g. Pascal) someday. */
23309 static void
23310 gen_pointer_type_die (tree type, dw_die_ref context_die)
23312 dw_die_ref ptr_die
23313 = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
23315 equate_type_number_to_die (type, ptr_die);
23316 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
23317 context_die);
23318 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
23321 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
23322 Use modified_type_die instead.
23323 We keep this code here just in case these types of DIEs may be needed to
23324 represent certain things in other languages (e.g. Pascal) someday. */
23326 static void
23327 gen_reference_type_die (tree type, dw_die_ref context_die)
23329 dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
23331 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
23332 ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
23333 else
23334 ref_die = new_die (DW_TAG_reference_type, scope_die, type);
23336 equate_type_number_to_die (type, ref_die);
23337 add_type_attribute (ref_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
23338 context_die);
23339 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
23341 #endif
23343 /* Generate a DIE for a pointer to a member type. TYPE can be an
23344 OFFSET_TYPE, for a pointer to data member, or a RECORD_TYPE, for a
23345 pointer to member function. */
23347 static void
23348 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
23350 if (lookup_type_die (type))
23351 return;
23353 dw_die_ref ptr_die = new_die (DW_TAG_ptr_to_member_type,
23354 scope_die_for (type, context_die), type);
23356 equate_type_number_to_die (type, ptr_die);
23357 add_AT_die_ref (ptr_die, DW_AT_containing_type,
23358 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
23359 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
23360 context_die);
23361 add_alignment_attribute (ptr_die, type);
23363 if (TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE
23364 && TREE_CODE (TREE_TYPE (type)) != METHOD_TYPE)
23366 dw_loc_descr_ref op = new_loc_descr (DW_OP_plus, 0, 0);
23367 add_AT_loc (ptr_die, DW_AT_use_location, op);
23371 static char *producer_string;
23373 /* Return a heap allocated producer string including command line options
23374 if -grecord-gcc-switches. */
23376 static char *
23377 gen_producer_string (void)
23379 size_t j;
23380 auto_vec<const char *> switches;
23381 const char *language_string = lang_hooks.name;
23382 char *producer, *tail;
23383 const char *p;
23384 size_t len = dwarf_record_gcc_switches ? 0 : 3;
23385 size_t plen = strlen (language_string) + 1 + strlen (version_string);
23387 for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
23388 switch (save_decoded_options[j].opt_index)
23390 case OPT_o:
23391 case OPT_d:
23392 case OPT_dumpbase:
23393 case OPT_dumpdir:
23394 case OPT_auxbase:
23395 case OPT_auxbase_strip:
23396 case OPT_quiet:
23397 case OPT_version:
23398 case OPT_v:
23399 case OPT_w:
23400 case OPT_L:
23401 case OPT_D:
23402 case OPT_I:
23403 case OPT_U:
23404 case OPT_SPECIAL_unknown:
23405 case OPT_SPECIAL_ignore:
23406 case OPT_SPECIAL_program_name:
23407 case OPT_SPECIAL_input_file:
23408 case OPT_grecord_gcc_switches:
23409 case OPT_gno_record_gcc_switches:
23410 case OPT__output_pch_:
23411 case OPT_fdiagnostics_show_location_:
23412 case OPT_fdiagnostics_show_option:
23413 case OPT_fdiagnostics_show_caret:
23414 case OPT_fdiagnostics_color_:
23415 case OPT_fverbose_asm:
23416 case OPT____:
23417 case OPT__sysroot_:
23418 case OPT_nostdinc:
23419 case OPT_nostdinc__:
23420 case OPT_fpreprocessed:
23421 case OPT_fltrans_output_list_:
23422 case OPT_fresolution_:
23423 case OPT_fdebug_prefix_map_:
23424 /* Ignore these. */
23425 continue;
23426 default:
23427 if (cl_options[save_decoded_options[j].opt_index].flags
23428 & CL_NO_DWARF_RECORD)
23429 continue;
23430 gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
23431 == '-');
23432 switch (save_decoded_options[j].canonical_option[0][1])
23434 case 'M':
23435 case 'i':
23436 case 'W':
23437 continue;
23438 case 'f':
23439 if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
23440 "dump", 4) == 0)
23441 continue;
23442 break;
23443 default:
23444 break;
23446 switches.safe_push (save_decoded_options[j].orig_option_with_args_text);
23447 len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
23448 break;
23451 producer = XNEWVEC (char, plen + 1 + len + 1);
23452 tail = producer;
23453 sprintf (tail, "%s %s", language_string, version_string);
23454 tail += plen;
23456 FOR_EACH_VEC_ELT (switches, j, p)
23458 len = strlen (p);
23459 *tail = ' ';
23460 memcpy (tail + 1, p, len);
23461 tail += len + 1;
23464 *tail = '\0';
23465 return producer;
23468 /* Given a C and/or C++ language/version string return the "highest".
23469 C++ is assumed to be "higher" than C in this case. Used for merging
23470 LTO translation unit languages. */
23471 static const char *
23472 highest_c_language (const char *lang1, const char *lang2)
23474 if (strcmp ("GNU C++14", lang1) == 0 || strcmp ("GNU C++14", lang2) == 0)
23475 return "GNU C++14";
23476 if (strcmp ("GNU C++11", lang1) == 0 || strcmp ("GNU C++11", lang2) == 0)
23477 return "GNU C++11";
23478 if (strcmp ("GNU C++98", lang1) == 0 || strcmp ("GNU C++98", lang2) == 0)
23479 return "GNU C++98";
23481 if (strcmp ("GNU C11", lang1) == 0 || strcmp ("GNU C11", lang2) == 0)
23482 return "GNU C11";
23483 if (strcmp ("GNU C99", lang1) == 0 || strcmp ("GNU C99", lang2) == 0)
23484 return "GNU C99";
23485 if (strcmp ("GNU C89", lang1) == 0 || strcmp ("GNU C89", lang2) == 0)
23486 return "GNU C89";
23488 gcc_unreachable ();
23492 /* Generate the DIE for the compilation unit. */
23494 static dw_die_ref
23495 gen_compile_unit_die (const char *filename)
23497 dw_die_ref die;
23498 const char *language_string = lang_hooks.name;
23499 int language;
23501 die = new_die (DW_TAG_compile_unit, NULL, NULL);
23503 if (filename)
23505 add_name_attribute (die, filename);
23506 /* Don't add cwd for <built-in>. */
23507 if (filename[0] != '<')
23508 add_comp_dir_attribute (die);
23511 add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
23513 /* If our producer is LTO try to figure out a common language to use
23514 from the global list of translation units. */
23515 if (strcmp (language_string, "GNU GIMPLE") == 0)
23517 unsigned i;
23518 tree t;
23519 const char *common_lang = NULL;
23521 FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
23523 if (!TRANSLATION_UNIT_LANGUAGE (t))
23524 continue;
23525 if (!common_lang)
23526 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
23527 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
23529 else if (strncmp (common_lang, "GNU C", 5) == 0
23530 && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
23531 /* Mixing C and C++ is ok, use C++ in that case. */
23532 common_lang = highest_c_language (common_lang,
23533 TRANSLATION_UNIT_LANGUAGE (t));
23534 else
23536 /* Fall back to C. */
23537 common_lang = NULL;
23538 break;
23542 if (common_lang)
23543 language_string = common_lang;
23546 language = DW_LANG_C;
23547 if (strncmp (language_string, "GNU C", 5) == 0
23548 && ISDIGIT (language_string[5]))
23550 language = DW_LANG_C89;
23551 if (dwarf_version >= 3 || !dwarf_strict)
23553 if (strcmp (language_string, "GNU C89") != 0)
23554 language = DW_LANG_C99;
23556 if (dwarf_version >= 5 /* || !dwarf_strict */)
23557 if (strcmp (language_string, "GNU C11") == 0)
23558 language = DW_LANG_C11;
23561 else if (strncmp (language_string, "GNU C++", 7) == 0)
23563 language = DW_LANG_C_plus_plus;
23564 if (dwarf_version >= 5 /* || !dwarf_strict */)
23566 if (strcmp (language_string, "GNU C++11") == 0)
23567 language = DW_LANG_C_plus_plus_11;
23568 else if (strcmp (language_string, "GNU C++14") == 0)
23569 language = DW_LANG_C_plus_plus_14;
23572 else if (strcmp (language_string, "GNU F77") == 0)
23573 language = DW_LANG_Fortran77;
23574 else if (strcmp (language_string, "GNU Pascal") == 0)
23575 language = DW_LANG_Pascal83;
23576 else if (dwarf_version >= 3 || !dwarf_strict)
23578 if (strcmp (language_string, "GNU Ada") == 0)
23579 language = DW_LANG_Ada95;
23580 else if (strncmp (language_string, "GNU Fortran", 11) == 0)
23582 language = DW_LANG_Fortran95;
23583 if (dwarf_version >= 5 /* || !dwarf_strict */)
23585 if (strcmp (language_string, "GNU Fortran2003") == 0)
23586 language = DW_LANG_Fortran03;
23587 else if (strcmp (language_string, "GNU Fortran2008") == 0)
23588 language = DW_LANG_Fortran08;
23591 else if (strcmp (language_string, "GNU Java") == 0)
23592 language = DW_LANG_Java;
23593 else if (strcmp (language_string, "GNU Objective-C") == 0)
23594 language = DW_LANG_ObjC;
23595 else if (strcmp (language_string, "GNU Objective-C++") == 0)
23596 language = DW_LANG_ObjC_plus_plus;
23597 else if (dwarf_version >= 5 || !dwarf_strict)
23599 if (strcmp (language_string, "GNU Go") == 0)
23600 language = DW_LANG_Go;
23603 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
23604 else if (strncmp (language_string, "GNU Fortran", 11) == 0)
23605 language = DW_LANG_Fortran90;
23607 add_AT_unsigned (die, DW_AT_language, language);
23609 switch (language)
23611 case DW_LANG_Fortran77:
23612 case DW_LANG_Fortran90:
23613 case DW_LANG_Fortran95:
23614 case DW_LANG_Fortran03:
23615 case DW_LANG_Fortran08:
23616 /* Fortran has case insensitive identifiers and the front-end
23617 lowercases everything. */
23618 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
23619 break;
23620 default:
23621 /* The default DW_ID_case_sensitive doesn't need to be specified. */
23622 break;
23624 return die;
23627 /* Generate the DIE for a base class. */
23629 static void
23630 gen_inheritance_die (tree binfo, tree access, tree type,
23631 dw_die_ref context_die)
23633 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
23634 struct vlr_context ctx = { type, NULL };
23636 add_type_attribute (die, BINFO_TYPE (binfo), TYPE_UNQUALIFIED, false,
23637 context_die);
23638 add_data_member_location_attribute (die, binfo, &ctx);
23640 if (BINFO_VIRTUAL_P (binfo))
23641 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
23643 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
23644 children, otherwise the default is DW_ACCESS_public. In DWARF2
23645 the default has always been DW_ACCESS_private. */
23646 if (access == access_public_node)
23648 if (dwarf_version == 2
23649 || context_die->die_tag == DW_TAG_class_type)
23650 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
23652 else if (access == access_protected_node)
23653 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
23654 else if (dwarf_version > 2
23655 && context_die->die_tag != DW_TAG_class_type)
23656 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
23659 /* Return whether DECL is a FIELD_DECL that represents the variant part of a
23660 structure. */
23661 static bool
23662 is_variant_part (tree decl)
23664 return (TREE_CODE (decl) == FIELD_DECL
23665 && TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE);
23668 /* Check that OPERAND is a reference to a field in STRUCT_TYPE. If it is,
23669 return the FIELD_DECL. Return NULL_TREE otherwise. */
23671 static tree
23672 analyze_discr_in_predicate (tree operand, tree struct_type)
23674 bool continue_stripping = true;
23675 while (continue_stripping)
23676 switch (TREE_CODE (operand))
23678 CASE_CONVERT:
23679 operand = TREE_OPERAND (operand, 0);
23680 break;
23681 default:
23682 continue_stripping = false;
23683 break;
23686 /* Match field access to members of struct_type only. */
23687 if (TREE_CODE (operand) == COMPONENT_REF
23688 && TREE_CODE (TREE_OPERAND (operand, 0)) == PLACEHOLDER_EXPR
23689 && TREE_TYPE (TREE_OPERAND (operand, 0)) == struct_type
23690 && TREE_CODE (TREE_OPERAND (operand, 1)) == FIELD_DECL)
23691 return TREE_OPERAND (operand, 1);
23692 else
23693 return NULL_TREE;
23696 /* Check that SRC is a constant integer that can be represented as a native
23697 integer constant (either signed or unsigned). If so, store it into DEST and
23698 return true. Return false otherwise. */
23700 static bool
23701 get_discr_value (tree src, dw_discr_value *dest)
23703 bool is_unsigned = TYPE_UNSIGNED (TREE_TYPE (src));
23705 if (TREE_CODE (src) != INTEGER_CST
23706 || !(is_unsigned ? tree_fits_uhwi_p (src) : tree_fits_shwi_p (src)))
23707 return false;
23709 dest->pos = is_unsigned;
23710 if (is_unsigned)
23711 dest->v.uval = tree_to_uhwi (src);
23712 else
23713 dest->v.sval = tree_to_shwi (src);
23715 return true;
23718 /* Try to extract synthetic properties out of VARIANT_PART_DECL, which is a
23719 FIELD_DECL in STRUCT_TYPE that represents a variant part. If unsuccessful,
23720 store NULL_TREE in DISCR_DECL. Otherwise:
23722 - store the discriminant field in STRUCT_TYPE that controls the variant
23723 part to *DISCR_DECL
23725 - put in *DISCR_LISTS_P an array where for each variant, the item
23726 represents the corresponding matching list of discriminant values.
23728 - put in *DISCR_LISTS_LENGTH the number of variants, which is the size of
23729 the above array.
23731 Note that when the array is allocated (i.e. when the analysis is
23732 successful), it is up to the caller to free the array. */
23734 static void
23735 analyze_variants_discr (tree variant_part_decl,
23736 tree struct_type,
23737 tree *discr_decl,
23738 dw_discr_list_ref **discr_lists_p,
23739 unsigned *discr_lists_length)
23741 tree variant_part_type = TREE_TYPE (variant_part_decl);
23742 tree variant;
23743 dw_discr_list_ref *discr_lists;
23744 unsigned i;
23746 /* Compute how many variants there are in this variant part. */
23747 *discr_lists_length = 0;
23748 for (variant = TYPE_FIELDS (variant_part_type);
23749 variant != NULL_TREE;
23750 variant = DECL_CHAIN (variant))
23751 ++*discr_lists_length;
23753 *discr_decl = NULL_TREE;
23754 *discr_lists_p
23755 = (dw_discr_list_ref *) xcalloc (*discr_lists_length,
23756 sizeof (**discr_lists_p));
23757 discr_lists = *discr_lists_p;
23759 /* And then analyze all variants to extract discriminant information for all
23760 of them. This analysis is conservative: as soon as we detect something we
23761 do not support, abort everything and pretend we found nothing. */
23762 for (variant = TYPE_FIELDS (variant_part_type), i = 0;
23763 variant != NULL_TREE;
23764 variant = DECL_CHAIN (variant), ++i)
23766 tree match_expr = DECL_QUALIFIER (variant);
23768 /* Now, try to analyze the predicate and deduce a discriminant for
23769 it. */
23770 if (match_expr == boolean_true_node)
23771 /* Typically happens for the default variant: it matches all cases that
23772 previous variants rejected. Don't output any matching value for
23773 this one. */
23774 continue;
23776 /* The following loop tries to iterate over each discriminant
23777 possibility: single values or ranges. */
23778 while (match_expr != NULL_TREE)
23780 tree next_round_match_expr;
23781 tree candidate_discr = NULL_TREE;
23782 dw_discr_list_ref new_node = NULL;
23784 /* Possibilities are matched one after the other by nested
23785 TRUTH_ORIF_EXPR expressions. Process the current possibility and
23786 continue with the rest at next iteration. */
23787 if (TREE_CODE (match_expr) == TRUTH_ORIF_EXPR)
23789 next_round_match_expr = TREE_OPERAND (match_expr, 0);
23790 match_expr = TREE_OPERAND (match_expr, 1);
23792 else
23793 next_round_match_expr = NULL_TREE;
23795 if (match_expr == boolean_false_node)
23796 /* This sub-expression matches nothing: just wait for the next
23797 one. */
23800 else if (TREE_CODE (match_expr) == EQ_EXPR)
23802 /* We are matching: <discr_field> == <integer_cst>
23803 This sub-expression matches a single value. */
23804 tree integer_cst = TREE_OPERAND (match_expr, 1);
23806 candidate_discr
23807 = analyze_discr_in_predicate (TREE_OPERAND (match_expr, 0),
23808 struct_type);
23810 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
23811 if (!get_discr_value (integer_cst,
23812 &new_node->dw_discr_lower_bound))
23813 goto abort;
23814 new_node->dw_discr_range = false;
23817 else if (TREE_CODE (match_expr) == TRUTH_ANDIF_EXPR)
23819 /* We are matching:
23820 <discr_field> > <integer_cst>
23821 && <discr_field> < <integer_cst>.
23822 This sub-expression matches the range of values between the
23823 two matched integer constants. Note that comparisons can be
23824 inclusive or exclusive. */
23825 tree candidate_discr_1, candidate_discr_2;
23826 tree lower_cst, upper_cst;
23827 bool lower_cst_included, upper_cst_included;
23828 tree lower_op = TREE_OPERAND (match_expr, 0);
23829 tree upper_op = TREE_OPERAND (match_expr, 1);
23831 /* When the comparison is exclusive, the integer constant is not
23832 the discriminant range bound we are looking for: we will have
23833 to increment or decrement it. */
23834 if (TREE_CODE (lower_op) == GE_EXPR)
23835 lower_cst_included = true;
23836 else if (TREE_CODE (lower_op) == GT_EXPR)
23837 lower_cst_included = false;
23838 else
23839 goto abort;
23841 if (TREE_CODE (upper_op) == LE_EXPR)
23842 upper_cst_included = true;
23843 else if (TREE_CODE (upper_op) == LT_EXPR)
23844 upper_cst_included = false;
23845 else
23846 goto abort;
23848 /* Extract the discriminant from the first operand and check it
23849 is consistant with the same analysis in the second
23850 operand. */
23851 candidate_discr_1
23852 = analyze_discr_in_predicate (TREE_OPERAND (lower_op, 0),
23853 struct_type);
23854 candidate_discr_2
23855 = analyze_discr_in_predicate (TREE_OPERAND (upper_op, 0),
23856 struct_type);
23857 if (candidate_discr_1 == candidate_discr_2)
23858 candidate_discr = candidate_discr_1;
23859 else
23860 goto abort;
23862 /* Extract bounds from both. */
23863 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
23864 lower_cst = TREE_OPERAND (lower_op, 1);
23865 upper_cst = TREE_OPERAND (upper_op, 1);
23867 if (!lower_cst_included)
23868 lower_cst
23869 = fold_build2 (PLUS_EXPR, TREE_TYPE (lower_cst), lower_cst,
23870 build_int_cst (TREE_TYPE (lower_cst), 1));
23871 if (!upper_cst_included)
23872 upper_cst
23873 = fold_build2 (MINUS_EXPR, TREE_TYPE (upper_cst), upper_cst,
23874 build_int_cst (TREE_TYPE (upper_cst), 1));
23876 if (!get_discr_value (lower_cst,
23877 &new_node->dw_discr_lower_bound)
23878 || !get_discr_value (upper_cst,
23879 &new_node->dw_discr_upper_bound))
23880 goto abort;
23882 new_node->dw_discr_range = true;
23885 else
23886 /* Unsupported sub-expression: we cannot determine the set of
23887 matching discriminant values. Abort everything. */
23888 goto abort;
23890 /* If the discriminant info is not consistant with what we saw so
23891 far, consider the analysis failed and abort everything. */
23892 if (candidate_discr == NULL_TREE
23893 || (*discr_decl != NULL_TREE && candidate_discr != *discr_decl))
23894 goto abort;
23895 else
23896 *discr_decl = candidate_discr;
23898 if (new_node != NULL)
23900 new_node->dw_discr_next = discr_lists[i];
23901 discr_lists[i] = new_node;
23903 match_expr = next_round_match_expr;
23907 /* If we reach this point, we could match everything we were interested
23908 in. */
23909 return;
23911 abort:
23912 /* Clean all data structure and return no result. */
23913 free (*discr_lists_p);
23914 *discr_lists_p = NULL;
23915 *discr_decl = NULL_TREE;
23918 /* Generate a DIE to represent VARIANT_PART_DECL, a variant part that is part
23919 of STRUCT_TYPE, a record type. This new DIE is emitted as the next child
23920 under CONTEXT_DIE.
23922 Variant parts are supposed to be implemented as a FIELD_DECL whose type is a
23923 QUAL_UNION_TYPE: this is the VARIANT_PART_DECL parameter. The members for
23924 this type, which are record types, represent the available variants and each
23925 has a DECL_QUALIFIER attribute. The discriminant and the discriminant
23926 values are inferred from these attributes.
23928 In trees, the offsets for the fields inside these sub-records are relative
23929 to the variant part itself, whereas the corresponding DIEs should have
23930 offset attributes that are relative to the embedding record base address.
23931 This is why the caller must provide a VARIANT_PART_OFFSET expression: it
23932 must be an expression that computes the offset of the variant part to
23933 describe in DWARF. */
23935 static void
23936 gen_variant_part (tree variant_part_decl, struct vlr_context *vlr_ctx,
23937 dw_die_ref context_die)
23939 const tree variant_part_type = TREE_TYPE (variant_part_decl);
23940 tree variant_part_offset = vlr_ctx->variant_part_offset;
23941 struct loc_descr_context ctx = {
23942 vlr_ctx->struct_type, /* context_type */
23943 NULL_TREE, /* base_decl */
23944 NULL, /* dpi */
23945 false, /* placeholder_arg */
23946 false /* placeholder_seen */
23949 /* The FIELD_DECL node in STRUCT_TYPE that acts as the discriminant, or
23950 NULL_TREE if there is no such field. */
23951 tree discr_decl = NULL_TREE;
23952 dw_discr_list_ref *discr_lists;
23953 unsigned discr_lists_length = 0;
23954 unsigned i;
23956 dw_die_ref dwarf_proc_die = NULL;
23957 dw_die_ref variant_part_die
23958 = new_die (DW_TAG_variant_part, context_die, variant_part_type);
23960 equate_decl_number_to_die (variant_part_decl, variant_part_die);
23962 analyze_variants_discr (variant_part_decl, vlr_ctx->struct_type,
23963 &discr_decl, &discr_lists, &discr_lists_length);
23965 if (discr_decl != NULL_TREE)
23967 dw_die_ref discr_die = lookup_decl_die (discr_decl);
23969 if (discr_die)
23970 add_AT_die_ref (variant_part_die, DW_AT_discr, discr_die);
23971 else
23972 /* We have no DIE for the discriminant, so just discard all
23973 discrimimant information in the output. */
23974 discr_decl = NULL_TREE;
23977 /* If the offset for this variant part is more complex than a constant,
23978 create a DWARF procedure for it so that we will not have to generate DWARF
23979 expressions for it for each member. */
23980 if (TREE_CODE (variant_part_offset) != INTEGER_CST
23981 && (dwarf_version >= 3 || !dwarf_strict))
23983 const tree dwarf_proc_fndecl
23984 = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL, NULL_TREE,
23985 build_function_type (TREE_TYPE (variant_part_offset),
23986 NULL_TREE));
23987 const tree dwarf_proc_call = build_call_expr (dwarf_proc_fndecl, 0);
23988 const dw_loc_descr_ref dwarf_proc_body
23989 = loc_descriptor_from_tree (variant_part_offset, 0, &ctx);
23991 dwarf_proc_die = new_dwarf_proc_die (dwarf_proc_body,
23992 dwarf_proc_fndecl, context_die);
23993 if (dwarf_proc_die != NULL)
23994 variant_part_offset = dwarf_proc_call;
23997 /* Output DIEs for all variants. */
23998 i = 0;
23999 for (tree variant = TYPE_FIELDS (variant_part_type);
24000 variant != NULL_TREE;
24001 variant = DECL_CHAIN (variant), ++i)
24003 tree variant_type = TREE_TYPE (variant);
24004 dw_die_ref variant_die;
24006 /* All variants (i.e. members of a variant part) are supposed to be
24007 encoded as structures. Sub-variant parts are QUAL_UNION_TYPE fields
24008 under these records. */
24009 gcc_assert (TREE_CODE (variant_type) == RECORD_TYPE);
24011 variant_die = new_die (DW_TAG_variant, variant_part_die, variant_type);
24012 equate_decl_number_to_die (variant, variant_die);
24014 /* Output discriminant values this variant matches, if any. */
24015 if (discr_decl == NULL || discr_lists[i] == NULL)
24016 /* In the case we have discriminant information at all, this is
24017 probably the default variant: as the standard says, don't
24018 output any discriminant value/list attribute. */
24020 else if (discr_lists[i]->dw_discr_next == NULL
24021 && !discr_lists[i]->dw_discr_range)
24022 /* If there is only one accepted value, don't bother outputting a
24023 list. */
24024 add_discr_value (variant_die, &discr_lists[i]->dw_discr_lower_bound);
24025 else
24026 add_discr_list (variant_die, discr_lists[i]);
24028 for (tree member = TYPE_FIELDS (variant_type);
24029 member != NULL_TREE;
24030 member = DECL_CHAIN (member))
24032 struct vlr_context vlr_sub_ctx = {
24033 vlr_ctx->struct_type, /* struct_type */
24034 NULL /* variant_part_offset */
24036 if (is_variant_part (member))
24038 /* All offsets for fields inside variant parts are relative to
24039 the top-level embedding RECORD_TYPE's base address. On the
24040 other hand, offsets in GCC's types are relative to the
24041 nested-most variant part. So we have to sum offsets each time
24042 we recurse. */
24044 vlr_sub_ctx.variant_part_offset
24045 = fold_build2 (PLUS_EXPR, TREE_TYPE (variant_part_offset),
24046 variant_part_offset, byte_position (member));
24047 gen_variant_part (member, &vlr_sub_ctx, variant_die);
24049 else
24051 vlr_sub_ctx.variant_part_offset = variant_part_offset;
24052 gen_decl_die (member, NULL, &vlr_sub_ctx, variant_die);
24057 free (discr_lists);
24060 /* Generate a DIE for a class member. */
24062 static void
24063 gen_member_die (tree type, dw_die_ref context_die)
24065 tree member;
24066 tree binfo = TYPE_BINFO (type);
24067 dw_die_ref child;
24069 /* If this is not an incomplete type, output descriptions of each of its
24070 members. Note that as we output the DIEs necessary to represent the
24071 members of this record or union type, we will also be trying to output
24072 DIEs to represent the *types* of those members. However the `type'
24073 function (above) will specifically avoid generating type DIEs for member
24074 types *within* the list of member DIEs for this (containing) type except
24075 for those types (of members) which are explicitly marked as also being
24076 members of this (containing) type themselves. The g++ front- end can
24077 force any given type to be treated as a member of some other (containing)
24078 type by setting the TYPE_CONTEXT of the given (member) type to point to
24079 the TREE node representing the appropriate (containing) type. */
24081 /* First output info about the base classes. */
24082 if (binfo)
24084 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
24085 int i;
24086 tree base;
24088 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
24089 gen_inheritance_die (base,
24090 (accesses ? (*accesses)[i] : access_public_node),
24091 type,
24092 context_die);
24095 /* Now output info about the data members and type members. */
24096 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
24098 struct vlr_context vlr_ctx = { type, NULL_TREE };
24099 bool static_inline_p
24100 = (TREE_STATIC (member)
24101 && (lang_hooks.decls.decl_dwarf_attribute (member, DW_AT_inline)
24102 != -1));
24104 /* If we thought we were generating minimal debug info for TYPE
24105 and then changed our minds, some of the member declarations
24106 may have already been defined. Don't define them again, but
24107 do put them in the right order. */
24109 child = lookup_decl_die (member);
24110 if (child)
24112 /* Handle inline static data members, which only have in-class
24113 declarations. */
24114 dw_die_ref ref = NULL;
24115 if (child->die_tag == DW_TAG_variable
24116 && child->die_parent == comp_unit_die ())
24118 ref = get_AT_ref (child, DW_AT_specification);
24119 /* For C++17 inline static data members followed by redundant
24120 out of class redeclaration, we might get here with
24121 child being the DIE created for the out of class
24122 redeclaration and with its DW_AT_specification being
24123 the DIE created for in-class definition. We want to
24124 reparent the latter, and don't want to create another
24125 DIE with DW_AT_specification in that case, because
24126 we already have one. */
24127 if (ref
24128 && static_inline_p
24129 && ref->die_tag == DW_TAG_variable
24130 && ref->die_parent == comp_unit_die ()
24131 && get_AT (ref, DW_AT_specification) == NULL)
24133 child = ref;
24134 ref = NULL;
24135 static_inline_p = false;
24138 if (child->die_tag == DW_TAG_variable
24139 && child->die_parent == comp_unit_die ()
24140 && ref == NULL)
24142 reparent_child (child, context_die);
24143 if (dwarf_version < 5)
24144 child->die_tag = DW_TAG_member;
24146 else
24147 splice_child_die (context_die, child);
24150 /* Do not generate standard DWARF for variant parts if we are generating
24151 the corresponding GNAT encodings: DIEs generated for both would
24152 conflict in our mappings. */
24153 else if (is_variant_part (member)
24154 && gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL)
24156 vlr_ctx.variant_part_offset = byte_position (member);
24157 gen_variant_part (member, &vlr_ctx, context_die);
24159 else
24161 vlr_ctx.variant_part_offset = NULL_TREE;
24162 gen_decl_die (member, NULL, &vlr_ctx, context_die);
24165 /* For C++ inline static data members emit immediately a DW_TAG_variable
24166 DIE that will refer to that DW_TAG_member/DW_TAG_variable through
24167 DW_AT_specification. */
24168 if (static_inline_p)
24170 int old_extern = DECL_EXTERNAL (member);
24171 DECL_EXTERNAL (member) = 0;
24172 gen_decl_die (member, NULL, NULL, comp_unit_die ());
24173 DECL_EXTERNAL (member) = old_extern;
24177 /* We do not keep type methods in type variants. */
24178 gcc_assert (TYPE_MAIN_VARIANT (type) == type);
24179 /* Now output info about the function members (if any). */
24180 if (TYPE_METHODS (type) != error_mark_node)
24181 for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
24183 /* Don't include clones in the member list. */
24184 if (DECL_ABSTRACT_ORIGIN (member))
24185 continue;
24186 /* Nor constructors for anonymous classes. */
24187 if (DECL_ARTIFICIAL (member)
24188 && dwarf2_name (member, 0) == NULL)
24189 continue;
24191 child = lookup_decl_die (member);
24192 if (child)
24193 splice_child_die (context_die, child);
24194 else
24195 gen_decl_die (member, NULL, NULL, context_die);
24199 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
24200 is set, we pretend that the type was never defined, so we only get the
24201 member DIEs needed by later specification DIEs. */
24203 static void
24204 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
24205 enum debug_info_usage usage)
24207 if (TREE_ASM_WRITTEN (type))
24209 /* Fill in the bound of variable-length fields in late dwarf if
24210 still incomplete. */
24211 if (!early_dwarf && variably_modified_type_p (type, NULL))
24212 for (tree member = TYPE_FIELDS (type);
24213 member;
24214 member = DECL_CHAIN (member))
24215 fill_variable_array_bounds (TREE_TYPE (member));
24216 return;
24219 dw_die_ref type_die = lookup_type_die (type);
24220 dw_die_ref scope_die = 0;
24221 int nested = 0;
24222 int complete = (TYPE_SIZE (type)
24223 && (! TYPE_STUB_DECL (type)
24224 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
24225 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
24226 complete = complete && should_emit_struct_debug (type, usage);
24228 if (type_die && ! complete)
24229 return;
24231 if (TYPE_CONTEXT (type) != NULL_TREE
24232 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
24233 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
24234 nested = 1;
24236 scope_die = scope_die_for (type, context_die);
24238 /* Generate child dies for template paramaters. */
24239 if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
24240 schedule_generic_params_dies_gen (type);
24242 if (! type_die || (nested && is_cu_die (scope_die)))
24243 /* First occurrence of type or toplevel definition of nested class. */
24245 dw_die_ref old_die = type_die;
24247 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
24248 ? record_type_tag (type) : DW_TAG_union_type,
24249 scope_die, type);
24250 equate_type_number_to_die (type, type_die);
24251 if (old_die)
24252 add_AT_specification (type_die, old_die);
24253 else
24254 add_name_attribute (type_die, type_tag (type));
24256 else
24257 remove_AT (type_die, DW_AT_declaration);
24259 /* If this type has been completed, then give it a byte_size attribute and
24260 then give a list of members. */
24261 if (complete && !ns_decl)
24263 /* Prevent infinite recursion in cases where the type of some member of
24264 this type is expressed in terms of this type itself. */
24265 TREE_ASM_WRITTEN (type) = 1;
24266 add_byte_size_attribute (type_die, type);
24267 add_alignment_attribute (type_die, type);
24268 if (TYPE_STUB_DECL (type) != NULL_TREE)
24270 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
24271 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
24274 /* If the first reference to this type was as the return type of an
24275 inline function, then it may not have a parent. Fix this now. */
24276 if (type_die->die_parent == NULL)
24277 add_child_die (scope_die, type_die);
24279 push_decl_scope (type);
24280 gen_member_die (type, type_die);
24281 pop_decl_scope ();
24283 add_gnat_descriptive_type_attribute (type_die, type, context_die);
24284 if (TYPE_ARTIFICIAL (type))
24285 add_AT_flag (type_die, DW_AT_artificial, 1);
24287 /* GNU extension: Record what type our vtable lives in. */
24288 if (TYPE_VFIELD (type))
24290 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
24292 gen_type_die (vtype, context_die);
24293 add_AT_die_ref (type_die, DW_AT_containing_type,
24294 lookup_type_die (vtype));
24297 else
24299 add_AT_flag (type_die, DW_AT_declaration, 1);
24301 /* We don't need to do this for function-local types. */
24302 if (TYPE_STUB_DECL (type)
24303 && ! decl_function_context (TYPE_STUB_DECL (type)))
24304 vec_safe_push (incomplete_types, type);
24307 if (get_AT (type_die, DW_AT_name))
24308 add_pubtype (type, type_die);
24311 /* Generate a DIE for a subroutine _type_. */
24313 static void
24314 gen_subroutine_type_die (tree type, dw_die_ref context_die)
24316 tree return_type = TREE_TYPE (type);
24317 dw_die_ref subr_die
24318 = new_die (DW_TAG_subroutine_type,
24319 scope_die_for (type, context_die), type);
24321 equate_type_number_to_die (type, subr_die);
24322 add_prototyped_attribute (subr_die, type);
24323 add_type_attribute (subr_die, return_type, TYPE_UNQUALIFIED, false,
24324 context_die);
24325 add_alignment_attribute (subr_die, type);
24326 gen_formal_types_die (type, subr_die);
24328 if (get_AT (subr_die, DW_AT_name))
24329 add_pubtype (type, subr_die);
24330 if ((dwarf_version >= 5 || !dwarf_strict)
24331 && lang_hooks.types.type_dwarf_attribute (type, DW_AT_reference) != -1)
24332 add_AT_flag (subr_die, DW_AT_reference, 1);
24333 if ((dwarf_version >= 5 || !dwarf_strict)
24334 && lang_hooks.types.type_dwarf_attribute (type,
24335 DW_AT_rvalue_reference) != -1)
24336 add_AT_flag (subr_die, DW_AT_rvalue_reference, 1);
24339 /* Generate a DIE for a type definition. */
24341 static void
24342 gen_typedef_die (tree decl, dw_die_ref context_die)
24344 dw_die_ref type_die;
24345 tree origin;
24347 if (TREE_ASM_WRITTEN (decl))
24349 if (DECL_ORIGINAL_TYPE (decl))
24350 fill_variable_array_bounds (DECL_ORIGINAL_TYPE (decl));
24351 return;
24354 TREE_ASM_WRITTEN (decl) = 1;
24355 type_die = new_die (DW_TAG_typedef, context_die, decl);
24356 origin = decl_ultimate_origin (decl);
24357 if (origin != NULL)
24358 add_abstract_origin_attribute (type_die, origin);
24359 else
24361 tree type = TREE_TYPE (decl);
24363 if (type == error_mark_node)
24364 return;
24366 add_name_and_src_coords_attributes (type_die, decl);
24367 if (DECL_ORIGINAL_TYPE (decl))
24369 type = DECL_ORIGINAL_TYPE (decl);
24371 if (type == error_mark_node)
24372 return;
24374 gcc_assert (type != TREE_TYPE (decl));
24375 equate_type_number_to_die (TREE_TYPE (decl), type_die);
24377 else
24379 if (is_naming_typedef_decl (TYPE_NAME (type)))
24381 /* Here, we are in the case of decl being a typedef naming
24382 an anonymous type, e.g:
24383 typedef struct {...} foo;
24384 In that case TREE_TYPE (decl) is not a typedef variant
24385 type and TYPE_NAME of the anonymous type is set to the
24386 TYPE_DECL of the typedef. This construct is emitted by
24387 the C++ FE.
24389 TYPE is the anonymous struct named by the typedef
24390 DECL. As we need the DW_AT_type attribute of the
24391 DW_TAG_typedef to point to the DIE of TYPE, let's
24392 generate that DIE right away. add_type_attribute
24393 called below will then pick (via lookup_type_die) that
24394 anonymous struct DIE. */
24395 if (!TREE_ASM_WRITTEN (type))
24396 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
24398 /* This is a GNU Extension. We are adding a
24399 DW_AT_linkage_name attribute to the DIE of the
24400 anonymous struct TYPE. The value of that attribute
24401 is the name of the typedef decl naming the anonymous
24402 struct. This greatly eases the work of consumers of
24403 this debug info. */
24404 add_linkage_name_raw (lookup_type_die (type), decl);
24408 add_type_attribute (type_die, type, decl_quals (decl), false,
24409 context_die);
24411 if (is_naming_typedef_decl (decl))
24412 /* We want that all subsequent calls to lookup_type_die with
24413 TYPE in argument yield the DW_TAG_typedef we have just
24414 created. */
24415 equate_type_number_to_die (type, type_die);
24417 type = TREE_TYPE (decl);
24419 add_alignment_attribute (type_die, type);
24421 add_accessibility_attribute (type_die, decl);
24424 if (DECL_ABSTRACT_P (decl))
24425 equate_decl_number_to_die (decl, type_die);
24427 if (get_AT (type_die, DW_AT_name))
24428 add_pubtype (decl, type_die);
24431 /* Generate a DIE for a struct, class, enum or union type. */
24433 static void
24434 gen_tagged_type_die (tree type,
24435 dw_die_ref context_die,
24436 enum debug_info_usage usage)
24438 int need_pop;
24440 if (type == NULL_TREE
24441 || !is_tagged_type (type))
24442 return;
24444 if (TREE_ASM_WRITTEN (type))
24445 need_pop = 0;
24446 /* If this is a nested type whose containing class hasn't been written
24447 out yet, writing it out will cover this one, too. This does not apply
24448 to instantiations of member class templates; they need to be added to
24449 the containing class as they are generated. FIXME: This hurts the
24450 idea of combining type decls from multiple TUs, since we can't predict
24451 what set of template instantiations we'll get. */
24452 else if (TYPE_CONTEXT (type)
24453 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
24454 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
24456 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
24458 if (TREE_ASM_WRITTEN (type))
24459 return;
24461 /* If that failed, attach ourselves to the stub. */
24462 push_decl_scope (TYPE_CONTEXT (type));
24463 context_die = lookup_type_die (TYPE_CONTEXT (type));
24464 need_pop = 1;
24466 else if (TYPE_CONTEXT (type) != NULL_TREE
24467 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
24469 /* If this type is local to a function that hasn't been written
24470 out yet, use a NULL context for now; it will be fixed up in
24471 decls_for_scope. */
24472 context_die = lookup_decl_die (TYPE_CONTEXT (type));
24473 /* A declaration DIE doesn't count; nested types need to go in the
24474 specification. */
24475 if (context_die && is_declaration_die (context_die))
24476 context_die = NULL;
24477 need_pop = 0;
24479 else
24481 context_die = declare_in_namespace (type, context_die);
24482 need_pop = 0;
24485 if (TREE_CODE (type) == ENUMERAL_TYPE)
24487 /* This might have been written out by the call to
24488 declare_in_namespace. */
24489 if (!TREE_ASM_WRITTEN (type))
24490 gen_enumeration_type_die (type, context_die);
24492 else
24493 gen_struct_or_union_type_die (type, context_die, usage);
24495 if (need_pop)
24496 pop_decl_scope ();
24498 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
24499 it up if it is ever completed. gen_*_type_die will set it for us
24500 when appropriate. */
24503 /* Generate a type description DIE. */
24505 static void
24506 gen_type_die_with_usage (tree type, dw_die_ref context_die,
24507 enum debug_info_usage usage)
24509 struct array_descr_info info;
24511 if (type == NULL_TREE || type == error_mark_node)
24512 return;
24514 if (flag_checking && type)
24515 verify_type (type);
24517 if (TYPE_NAME (type) != NULL_TREE
24518 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
24519 && is_redundant_typedef (TYPE_NAME (type))
24520 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
24521 /* The DECL of this type is a typedef we don't want to emit debug
24522 info for but we want debug info for its underlying typedef.
24523 This can happen for e.g, the injected-class-name of a C++
24524 type. */
24525 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
24527 /* If TYPE is a typedef type variant, let's generate debug info
24528 for the parent typedef which TYPE is a type of. */
24529 if (typedef_variant_p (type))
24531 if (TREE_ASM_WRITTEN (type))
24532 return;
24534 /* Prevent broken recursion; we can't hand off to the same type. */
24535 gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
24537 /* Give typedefs the right scope. */
24538 context_die = scope_die_for (type, context_die);
24540 TREE_ASM_WRITTEN (type) = 1;
24542 gen_decl_die (TYPE_NAME (type), NULL, NULL, context_die);
24543 return;
24546 /* If type is an anonymous tagged type named by a typedef, let's
24547 generate debug info for the typedef. */
24548 if (is_naming_typedef_decl (TYPE_NAME (type)))
24550 /* Use the DIE of the containing namespace as the parent DIE of
24551 the type description DIE we want to generate. */
24552 if (DECL_CONTEXT (TYPE_NAME (type))
24553 && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
24554 context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
24556 gen_decl_die (TYPE_NAME (type), NULL, NULL, context_die);
24557 return;
24560 if (lang_hooks.types.get_debug_type)
24562 tree debug_type = lang_hooks.types.get_debug_type (type);
24564 if (debug_type != NULL_TREE && debug_type != type)
24566 gen_type_die_with_usage (debug_type, context_die, usage);
24567 return;
24571 /* We are going to output a DIE to represent the unqualified version
24572 of this type (i.e. without any const or volatile qualifiers) so
24573 get the main variant (i.e. the unqualified version) of this type
24574 now. (Vectors and arrays are special because the debugging info is in the
24575 cloned type itself. Similarly function/method types can contain extra
24576 ref-qualification). */
24577 if (TREE_CODE (type) == FUNCTION_TYPE
24578 || TREE_CODE (type) == METHOD_TYPE)
24580 /* For function/method types, can't use type_main_variant here,
24581 because that can have different ref-qualifiers for C++,
24582 but try to canonicalize. */
24583 tree main = TYPE_MAIN_VARIANT (type);
24584 for (tree t = main; t; t = TYPE_NEXT_VARIANT (t))
24585 if (TYPE_QUALS_NO_ADDR_SPACE (t) == 0
24586 && check_base_type (t, main)
24587 && check_lang_type (t, type))
24589 type = t;
24590 break;
24593 else if (TREE_CODE (type) != VECTOR_TYPE
24594 && TREE_CODE (type) != ARRAY_TYPE)
24595 type = type_main_variant (type);
24597 /* If this is an array type with hidden descriptor, handle it first. */
24598 if (!TREE_ASM_WRITTEN (type)
24599 && lang_hooks.types.get_array_descr_info)
24601 memset (&info, 0, sizeof (info));
24602 if (lang_hooks.types.get_array_descr_info (type, &info))
24604 /* Fortran sometimes emits array types with no dimension. */
24605 gcc_assert (info.ndimensions >= 0
24606 && (info.ndimensions
24607 <= DWARF2OUT_ARRAY_DESCR_INFO_MAX_DIMEN));
24608 gen_descr_array_type_die (type, &info, context_die);
24609 TREE_ASM_WRITTEN (type) = 1;
24610 return;
24614 if (TREE_ASM_WRITTEN (type))
24616 /* Variable-length types may be incomplete even if
24617 TREE_ASM_WRITTEN. For such types, fall through to
24618 gen_array_type_die() and possibly fill in
24619 DW_AT_{upper,lower}_bound attributes. */
24620 if ((TREE_CODE (type) != ARRAY_TYPE
24621 && TREE_CODE (type) != RECORD_TYPE
24622 && TREE_CODE (type) != UNION_TYPE
24623 && TREE_CODE (type) != QUAL_UNION_TYPE)
24624 || !variably_modified_type_p (type, NULL))
24625 return;
24628 switch (TREE_CODE (type))
24630 case ERROR_MARK:
24631 break;
24633 case POINTER_TYPE:
24634 case REFERENCE_TYPE:
24635 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
24636 ensures that the gen_type_die recursion will terminate even if the
24637 type is recursive. Recursive types are possible in Ada. */
24638 /* ??? We could perhaps do this for all types before the switch
24639 statement. */
24640 TREE_ASM_WRITTEN (type) = 1;
24642 /* For these types, all that is required is that we output a DIE (or a
24643 set of DIEs) to represent the "basis" type. */
24644 gen_type_die_with_usage (TREE_TYPE (type), context_die,
24645 DINFO_USAGE_IND_USE);
24646 break;
24648 case OFFSET_TYPE:
24649 /* This code is used for C++ pointer-to-data-member types.
24650 Output a description of the relevant class type. */
24651 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
24652 DINFO_USAGE_IND_USE);
24654 /* Output a description of the type of the object pointed to. */
24655 gen_type_die_with_usage (TREE_TYPE (type), context_die,
24656 DINFO_USAGE_IND_USE);
24658 /* Now output a DIE to represent this pointer-to-data-member type
24659 itself. */
24660 gen_ptr_to_mbr_type_die (type, context_die);
24661 break;
24663 case FUNCTION_TYPE:
24664 /* Force out return type (in case it wasn't forced out already). */
24665 gen_type_die_with_usage (TREE_TYPE (type), context_die,
24666 DINFO_USAGE_DIR_USE);
24667 gen_subroutine_type_die (type, context_die);
24668 break;
24670 case METHOD_TYPE:
24671 /* Force out return type (in case it wasn't forced out already). */
24672 gen_type_die_with_usage (TREE_TYPE (type), context_die,
24673 DINFO_USAGE_DIR_USE);
24674 gen_subroutine_type_die (type, context_die);
24675 break;
24677 case ARRAY_TYPE:
24678 case VECTOR_TYPE:
24679 gen_array_type_die (type, context_die);
24680 break;
24682 case ENUMERAL_TYPE:
24683 case RECORD_TYPE:
24684 case UNION_TYPE:
24685 case QUAL_UNION_TYPE:
24686 gen_tagged_type_die (type, context_die, usage);
24687 return;
24689 case VOID_TYPE:
24690 case INTEGER_TYPE:
24691 case REAL_TYPE:
24692 case FIXED_POINT_TYPE:
24693 case COMPLEX_TYPE:
24694 case BOOLEAN_TYPE:
24695 case POINTER_BOUNDS_TYPE:
24696 /* No DIEs needed for fundamental types. */
24697 break;
24699 case NULLPTR_TYPE:
24700 case LANG_TYPE:
24701 /* Just use DW_TAG_unspecified_type. */
24703 dw_die_ref type_die = lookup_type_die (type);
24704 if (type_die == NULL)
24706 tree name = TYPE_IDENTIFIER (type);
24707 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (),
24708 type);
24709 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
24710 equate_type_number_to_die (type, type_die);
24713 break;
24715 default:
24716 if (is_cxx_auto (type))
24718 tree name = TYPE_IDENTIFIER (type);
24719 dw_die_ref *die = (name == get_identifier ("auto")
24720 ? &auto_die : &decltype_auto_die);
24721 if (!*die)
24723 *die = new_die (DW_TAG_unspecified_type,
24724 comp_unit_die (), NULL_TREE);
24725 add_name_attribute (*die, IDENTIFIER_POINTER (name));
24727 equate_type_number_to_die (type, *die);
24728 break;
24730 gcc_unreachable ();
24733 TREE_ASM_WRITTEN (type) = 1;
24736 static void
24737 gen_type_die (tree type, dw_die_ref context_die)
24739 if (type != error_mark_node)
24741 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
24742 if (flag_checking)
24744 dw_die_ref die = lookup_type_die (type);
24745 if (die)
24746 check_die (die);
24751 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
24752 things which are local to the given block. */
24754 static void
24755 gen_block_die (tree stmt, dw_die_ref context_die)
24757 int must_output_die = 0;
24758 bool inlined_func;
24760 /* Ignore blocks that are NULL. */
24761 if (stmt == NULL_TREE)
24762 return;
24764 inlined_func = inlined_function_outer_scope_p (stmt);
24766 /* If the block is one fragment of a non-contiguous block, do not
24767 process the variables, since they will have been done by the
24768 origin block. Do process subblocks. */
24769 if (BLOCK_FRAGMENT_ORIGIN (stmt))
24771 tree sub;
24773 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
24774 gen_block_die (sub, context_die);
24776 return;
24779 /* Determine if we need to output any Dwarf DIEs at all to represent this
24780 block. */
24781 if (inlined_func)
24782 /* The outer scopes for inlinings *must* always be represented. We
24783 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
24784 must_output_die = 1;
24785 else
24787 /* Determine if this block directly contains any "significant"
24788 local declarations which we will need to output DIEs for. */
24789 if (debug_info_level > DINFO_LEVEL_TERSE)
24790 /* We are not in terse mode so *any* local declaration counts
24791 as being a "significant" one. */
24792 must_output_die = ((BLOCK_VARS (stmt) != NULL
24793 || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
24794 && (TREE_USED (stmt)
24795 || TREE_ASM_WRITTEN (stmt)
24796 || BLOCK_ABSTRACT (stmt)));
24797 else if ((TREE_USED (stmt)
24798 || TREE_ASM_WRITTEN (stmt)
24799 || BLOCK_ABSTRACT (stmt))
24800 && !dwarf2out_ignore_block (stmt))
24801 must_output_die = 1;
24804 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
24805 DIE for any block which contains no significant local declarations at
24806 all. Rather, in such cases we just call `decls_for_scope' so that any
24807 needed Dwarf info for any sub-blocks will get properly generated. Note
24808 that in terse mode, our definition of what constitutes a "significant"
24809 local declaration gets restricted to include only inlined function
24810 instances and local (nested) function definitions. */
24811 if (must_output_die)
24813 if (inlined_func)
24815 /* If STMT block is abstract, that means we have been called
24816 indirectly from dwarf2out_abstract_function.
24817 That function rightfully marks the descendent blocks (of
24818 the abstract function it is dealing with) as being abstract,
24819 precisely to prevent us from emitting any
24820 DW_TAG_inlined_subroutine DIE as a descendent
24821 of an abstract function instance. So in that case, we should
24822 not call gen_inlined_subroutine_die.
24824 Later though, when cgraph asks dwarf2out to emit info
24825 for the concrete instance of the function decl into which
24826 the concrete instance of STMT got inlined, the later will lead
24827 to the generation of a DW_TAG_inlined_subroutine DIE. */
24828 if (! BLOCK_ABSTRACT (stmt))
24829 gen_inlined_subroutine_die (stmt, context_die);
24831 else
24832 gen_lexical_block_die (stmt, context_die);
24834 else
24835 decls_for_scope (stmt, context_die);
24838 /* Process variable DECL (or variable with origin ORIGIN) within
24839 block STMT and add it to CONTEXT_DIE. */
24840 static void
24841 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
24843 dw_die_ref die;
24844 tree decl_or_origin = decl ? decl : origin;
24846 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
24847 die = lookup_decl_die (decl_or_origin);
24848 else if (TREE_CODE (decl_or_origin) == TYPE_DECL)
24850 if (TYPE_DECL_IS_STUB (decl_or_origin))
24851 die = lookup_type_die (TREE_TYPE (decl_or_origin));
24852 else
24853 die = lookup_decl_die (decl_or_origin);
24854 /* Avoid re-creating the DIE late if it was optimized as unused early. */
24855 if (! die && ! early_dwarf)
24856 return;
24858 else
24859 die = NULL;
24861 if (die != NULL && die->die_parent == NULL)
24862 add_child_die (context_die, die);
24863 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
24865 if (early_dwarf)
24866 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
24867 stmt, context_die);
24869 else
24870 gen_decl_die (decl, origin, NULL, context_die);
24873 /* Generate all of the decls declared within a given scope and (recursively)
24874 all of its sub-blocks. */
24876 static void
24877 decls_for_scope (tree stmt, dw_die_ref context_die)
24879 tree decl;
24880 unsigned int i;
24881 tree subblocks;
24883 /* Ignore NULL blocks. */
24884 if (stmt == NULL_TREE)
24885 return;
24887 /* Output the DIEs to represent all of the data objects and typedefs
24888 declared directly within this block but not within any nested
24889 sub-blocks. Also, nested function and tag DIEs have been
24890 generated with a parent of NULL; fix that up now. We don't
24891 have to do this if we're at -g1. */
24892 if (debug_info_level > DINFO_LEVEL_TERSE)
24894 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
24895 process_scope_var (stmt, decl, NULL_TREE, context_die);
24896 /* BLOCK_NONLOCALIZED_VARs simply generate DIE stubs with abstract
24897 origin - avoid doing this twice as we have no good way to see
24898 if we've done it once already. */
24899 if (! early_dwarf)
24900 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
24902 decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
24903 if (decl == current_function_decl)
24904 /* Ignore declarations of the current function, while they
24905 are declarations, gen_subprogram_die would treat them
24906 as definitions again, because they are equal to
24907 current_function_decl and endlessly recurse. */;
24908 else if (TREE_CODE (decl) == FUNCTION_DECL)
24909 process_scope_var (stmt, decl, NULL_TREE, context_die);
24910 else
24911 process_scope_var (stmt, NULL_TREE, decl, context_die);
24915 /* Even if we're at -g1, we need to process the subblocks in order to get
24916 inlined call information. */
24918 /* Output the DIEs to represent all sub-blocks (and the items declared
24919 therein) of this block. */
24920 for (subblocks = BLOCK_SUBBLOCKS (stmt);
24921 subblocks != NULL;
24922 subblocks = BLOCK_CHAIN (subblocks))
24923 gen_block_die (subblocks, context_die);
24926 /* Is this a typedef we can avoid emitting? */
24928 bool
24929 is_redundant_typedef (const_tree decl)
24931 if (TYPE_DECL_IS_STUB (decl))
24932 return true;
24934 if (DECL_ARTIFICIAL (decl)
24935 && DECL_CONTEXT (decl)
24936 && is_tagged_type (DECL_CONTEXT (decl))
24937 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
24938 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
24939 /* Also ignore the artificial member typedef for the class name. */
24940 return true;
24942 return false;
24945 /* Return TRUE if TYPE is a typedef that names a type for linkage
24946 purposes. This kind of typedefs is produced by the C++ FE for
24947 constructs like:
24949 typedef struct {...} foo;
24951 In that case, there is no typedef variant type produced for foo.
24952 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
24953 struct type. */
24955 static bool
24956 is_naming_typedef_decl (const_tree decl)
24958 if (decl == NULL_TREE
24959 || TREE_CODE (decl) != TYPE_DECL
24960 || DECL_NAMELESS (decl)
24961 || !is_tagged_type (TREE_TYPE (decl))
24962 || DECL_IS_BUILTIN (decl)
24963 || is_redundant_typedef (decl)
24964 /* It looks like Ada produces TYPE_DECLs that are very similar
24965 to C++ naming typedefs but that have different
24966 semantics. Let's be specific to c++ for now. */
24967 || !is_cxx (decl))
24968 return FALSE;
24970 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
24971 && TYPE_NAME (TREE_TYPE (decl)) == decl
24972 && (TYPE_STUB_DECL (TREE_TYPE (decl))
24973 != TYPE_NAME (TREE_TYPE (decl))));
24976 /* Looks up the DIE for a context. */
24978 static inline dw_die_ref
24979 lookup_context_die (tree context)
24981 if (context)
24983 /* Find die that represents this context. */
24984 if (TYPE_P (context))
24986 context = TYPE_MAIN_VARIANT (context);
24987 dw_die_ref ctx = lookup_type_die (context);
24988 if (!ctx)
24989 return NULL;
24990 return strip_naming_typedef (context, ctx);
24992 else
24993 return lookup_decl_die (context);
24995 return comp_unit_die ();
24998 /* Returns the DIE for a context. */
25000 static inline dw_die_ref
25001 get_context_die (tree context)
25003 if (context)
25005 /* Find die that represents this context. */
25006 if (TYPE_P (context))
25008 context = TYPE_MAIN_VARIANT (context);
25009 return strip_naming_typedef (context, force_type_die (context));
25011 else
25012 return force_decl_die (context);
25014 return comp_unit_die ();
25017 /* Returns the DIE for decl. A DIE will always be returned. */
25019 static dw_die_ref
25020 force_decl_die (tree decl)
25022 dw_die_ref decl_die;
25023 unsigned saved_external_flag;
25024 tree save_fn = NULL_TREE;
25025 decl_die = lookup_decl_die (decl);
25026 if (!decl_die)
25028 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
25030 decl_die = lookup_decl_die (decl);
25031 if (decl_die)
25032 return decl_die;
25034 switch (TREE_CODE (decl))
25036 case FUNCTION_DECL:
25037 /* Clear current_function_decl, so that gen_subprogram_die thinks
25038 that this is a declaration. At this point, we just want to force
25039 declaration die. */
25040 save_fn = current_function_decl;
25041 current_function_decl = NULL_TREE;
25042 gen_subprogram_die (decl, context_die);
25043 current_function_decl = save_fn;
25044 break;
25046 case VAR_DECL:
25047 /* Set external flag to force declaration die. Restore it after
25048 gen_decl_die() call. */
25049 saved_external_flag = DECL_EXTERNAL (decl);
25050 DECL_EXTERNAL (decl) = 1;
25051 gen_decl_die (decl, NULL, NULL, context_die);
25052 DECL_EXTERNAL (decl) = saved_external_flag;
25053 break;
25055 case NAMESPACE_DECL:
25056 if (dwarf_version >= 3 || !dwarf_strict)
25057 dwarf2out_decl (decl);
25058 else
25059 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
25060 decl_die = comp_unit_die ();
25061 break;
25063 case TRANSLATION_UNIT_DECL:
25064 decl_die = comp_unit_die ();
25065 break;
25067 default:
25068 gcc_unreachable ();
25071 /* We should be able to find the DIE now. */
25072 if (!decl_die)
25073 decl_die = lookup_decl_die (decl);
25074 gcc_assert (decl_die);
25077 return decl_die;
25080 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
25081 always returned. */
25083 static dw_die_ref
25084 force_type_die (tree type)
25086 dw_die_ref type_die;
25088 type_die = lookup_type_die (type);
25089 if (!type_die)
25091 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
25093 type_die = modified_type_die (type, TYPE_QUALS_NO_ADDR_SPACE (type),
25094 false, context_die);
25095 gcc_assert (type_die);
25097 return type_die;
25100 /* Force out any required namespaces to be able to output DECL,
25101 and return the new context_die for it, if it's changed. */
25103 static dw_die_ref
25104 setup_namespace_context (tree thing, dw_die_ref context_die)
25106 tree context = (DECL_P (thing)
25107 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
25108 if (context && TREE_CODE (context) == NAMESPACE_DECL)
25109 /* Force out the namespace. */
25110 context_die = force_decl_die (context);
25112 return context_die;
25115 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
25116 type) within its namespace, if appropriate.
25118 For compatibility with older debuggers, namespace DIEs only contain
25119 declarations; all definitions are emitted at CU scope, with
25120 DW_AT_specification pointing to the declaration (like with class
25121 members). */
25123 static dw_die_ref
25124 declare_in_namespace (tree thing, dw_die_ref context_die)
25126 dw_die_ref ns_context;
25128 if (debug_info_level <= DINFO_LEVEL_TERSE)
25129 return context_die;
25131 /* External declarations in the local scope only need to be emitted
25132 once, not once in the namespace and once in the scope.
25134 This avoids declaring the `extern' below in the
25135 namespace DIE as well as in the innermost scope:
25137 namespace S
25139 int i=5;
25140 int foo()
25142 int i=8;
25143 extern int i;
25144 return i;
25148 if (DECL_P (thing) && DECL_EXTERNAL (thing) && local_scope_p (context_die))
25149 return context_die;
25151 /* If this decl is from an inlined function, then don't try to emit it in its
25152 namespace, as we will get confused. It would have already been emitted
25153 when the abstract instance of the inline function was emitted anyways. */
25154 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
25155 return context_die;
25157 ns_context = setup_namespace_context (thing, context_die);
25159 if (ns_context != context_die)
25161 if (is_fortran ())
25162 return ns_context;
25163 if (DECL_P (thing))
25164 gen_decl_die (thing, NULL, NULL, ns_context);
25165 else
25166 gen_type_die (thing, ns_context);
25168 return context_die;
25171 /* Generate a DIE for a namespace or namespace alias. */
25173 static void
25174 gen_namespace_die (tree decl, dw_die_ref context_die)
25176 dw_die_ref namespace_die;
25178 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
25179 they are an alias of. */
25180 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
25182 /* Output a real namespace or module. */
25183 context_die = setup_namespace_context (decl, comp_unit_die ());
25184 namespace_die = new_die (is_fortran ()
25185 ? DW_TAG_module : DW_TAG_namespace,
25186 context_die, decl);
25187 /* For Fortran modules defined in different CU don't add src coords. */
25188 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
25190 const char *name = dwarf2_name (decl, 0);
25191 if (name)
25192 add_name_attribute (namespace_die, name);
25194 else
25195 add_name_and_src_coords_attributes (namespace_die, decl);
25196 if (DECL_EXTERNAL (decl))
25197 add_AT_flag (namespace_die, DW_AT_declaration, 1);
25198 equate_decl_number_to_die (decl, namespace_die);
25200 else
25202 /* Output a namespace alias. */
25204 /* Force out the namespace we are an alias of, if necessary. */
25205 dw_die_ref origin_die
25206 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
25208 if (DECL_FILE_SCOPE_P (decl)
25209 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
25210 context_die = setup_namespace_context (decl, comp_unit_die ());
25211 /* Now create the namespace alias DIE. */
25212 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
25213 add_name_and_src_coords_attributes (namespace_die, decl);
25214 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
25215 equate_decl_number_to_die (decl, namespace_die);
25217 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
25218 if (want_pubnames ())
25219 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
25222 /* Generate Dwarf debug information for a decl described by DECL.
25223 The return value is currently only meaningful for PARM_DECLs,
25224 for all other decls it returns NULL.
25226 If DECL is a FIELD_DECL, CTX is required: see the comment for VLR_CONTEXT.
25227 It can be NULL otherwise. */
25229 static dw_die_ref
25230 gen_decl_die (tree decl, tree origin, struct vlr_context *ctx,
25231 dw_die_ref context_die)
25233 tree decl_or_origin = decl ? decl : origin;
25234 tree class_origin = NULL, ultimate_origin;
25236 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
25237 return NULL;
25239 /* Ignore pointer bounds decls. */
25240 if (DECL_P (decl_or_origin)
25241 && TREE_TYPE (decl_or_origin)
25242 && POINTER_BOUNDS_P (decl_or_origin))
25243 return NULL;
25245 switch (TREE_CODE (decl_or_origin))
25247 case ERROR_MARK:
25248 break;
25250 case CONST_DECL:
25251 if (!is_fortran () && !is_ada ())
25253 /* The individual enumerators of an enum type get output when we output
25254 the Dwarf representation of the relevant enum type itself. */
25255 break;
25258 /* Emit its type. */
25259 gen_type_die (TREE_TYPE (decl), context_die);
25261 /* And its containing namespace. */
25262 context_die = declare_in_namespace (decl, context_die);
25264 gen_const_die (decl, context_die);
25265 break;
25267 case FUNCTION_DECL:
25268 /* Don't output any DIEs to represent mere function declarations,
25269 unless they are class members or explicit block externs. */
25270 if (DECL_INITIAL (decl_or_origin) == NULL_TREE
25271 && DECL_FILE_SCOPE_P (decl_or_origin)
25272 && (current_function_decl == NULL_TREE
25273 || DECL_ARTIFICIAL (decl_or_origin)))
25274 break;
25276 #if 0
25277 /* FIXME */
25278 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
25279 on local redeclarations of global functions. That seems broken. */
25280 if (current_function_decl != decl)
25281 /* This is only a declaration. */;
25282 #endif
25284 /* If we're emitting a clone, emit info for the abstract instance. */
25285 if (origin || DECL_ORIGIN (decl) != decl)
25286 dwarf2out_abstract_function (origin
25287 ? DECL_ORIGIN (origin)
25288 : DECL_ABSTRACT_ORIGIN (decl));
25290 /* If we're emitting an out-of-line copy of an inline function,
25291 emit info for the abstract instance and set up to refer to it. */
25292 else if (cgraph_function_possibly_inlined_p (decl)
25293 && ! DECL_ABSTRACT_P (decl)
25294 && ! class_or_namespace_scope_p (context_die)
25295 /* dwarf2out_abstract_function won't emit a die if this is just
25296 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
25297 that case, because that works only if we have a die. */
25298 && DECL_INITIAL (decl) != NULL_TREE)
25300 dwarf2out_abstract_function (decl);
25301 set_decl_origin_self (decl);
25304 /* Otherwise we're emitting the primary DIE for this decl. */
25305 else if (debug_info_level > DINFO_LEVEL_TERSE)
25307 /* Before we describe the FUNCTION_DECL itself, make sure that we
25308 have its containing type. */
25309 if (!origin)
25310 origin = decl_class_context (decl);
25311 if (origin != NULL_TREE)
25312 gen_type_die (origin, context_die);
25314 /* And its return type. */
25315 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
25317 /* And its virtual context. */
25318 if (DECL_VINDEX (decl) != NULL_TREE)
25319 gen_type_die (DECL_CONTEXT (decl), context_die);
25321 /* Make sure we have a member DIE for decl. */
25322 if (origin != NULL_TREE)
25323 gen_type_die_for_member (origin, decl, context_die);
25325 /* And its containing namespace. */
25326 context_die = declare_in_namespace (decl, context_die);
25329 /* Now output a DIE to represent the function itself. */
25330 if (decl)
25331 gen_subprogram_die (decl, context_die);
25332 break;
25334 case TYPE_DECL:
25335 /* If we are in terse mode, don't generate any DIEs to represent any
25336 actual typedefs. */
25337 if (debug_info_level <= DINFO_LEVEL_TERSE)
25338 break;
25340 /* In the special case of a TYPE_DECL node representing the declaration
25341 of some type tag, if the given TYPE_DECL is marked as having been
25342 instantiated from some other (original) TYPE_DECL node (e.g. one which
25343 was generated within the original definition of an inline function) we
25344 used to generate a special (abbreviated) DW_TAG_structure_type,
25345 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
25346 should be actually referencing those DIEs, as variable DIEs with that
25347 type would be emitted already in the abstract origin, so it was always
25348 removed during unused type prunning. Don't add anything in this
25349 case. */
25350 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
25351 break;
25353 if (is_redundant_typedef (decl))
25354 gen_type_die (TREE_TYPE (decl), context_die);
25355 else
25356 /* Output a DIE to represent the typedef itself. */
25357 gen_typedef_die (decl, context_die);
25358 break;
25360 case LABEL_DECL:
25361 if (debug_info_level >= DINFO_LEVEL_NORMAL)
25362 gen_label_die (decl, context_die);
25363 break;
25365 case VAR_DECL:
25366 case RESULT_DECL:
25367 /* If we are in terse mode, don't generate any DIEs to represent any
25368 variable declarations or definitions. */
25369 if (debug_info_level <= DINFO_LEVEL_TERSE)
25370 break;
25372 /* Output any DIEs that are needed to specify the type of this data
25373 object. */
25374 if (decl_by_reference_p (decl_or_origin))
25375 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
25376 else
25377 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
25379 /* And its containing type. */
25380 class_origin = decl_class_context (decl_or_origin);
25381 if (class_origin != NULL_TREE)
25382 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
25384 /* And its containing namespace. */
25385 context_die = declare_in_namespace (decl_or_origin, context_die);
25387 /* Now output the DIE to represent the data object itself. This gets
25388 complicated because of the possibility that the VAR_DECL really
25389 represents an inlined instance of a formal parameter for an inline
25390 function. */
25391 ultimate_origin = decl_ultimate_origin (decl_or_origin);
25392 if (ultimate_origin != NULL_TREE
25393 && TREE_CODE (ultimate_origin) == PARM_DECL)
25394 gen_formal_parameter_die (decl, origin,
25395 true /* Emit name attribute. */,
25396 context_die);
25397 else
25398 gen_variable_die (decl, origin, context_die);
25399 break;
25401 case FIELD_DECL:
25402 gcc_assert (ctx != NULL && ctx->struct_type != NULL);
25403 /* Ignore the nameless fields that are used to skip bits but handle C++
25404 anonymous unions and structs. */
25405 if (DECL_NAME (decl) != NULL_TREE
25406 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
25407 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
25409 gen_type_die (member_declared_type (decl), context_die);
25410 gen_field_die (decl, ctx, context_die);
25412 break;
25414 case PARM_DECL:
25415 if (DECL_BY_REFERENCE (decl_or_origin))
25416 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
25417 else
25418 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
25419 return gen_formal_parameter_die (decl, origin,
25420 true /* Emit name attribute. */,
25421 context_die);
25423 case NAMESPACE_DECL:
25424 if (dwarf_version >= 3 || !dwarf_strict)
25425 gen_namespace_die (decl, context_die);
25426 break;
25428 case IMPORTED_DECL:
25429 dwarf2out_imported_module_or_decl_1 (decl, DECL_NAME (decl),
25430 DECL_CONTEXT (decl), context_die);
25431 break;
25433 case NAMELIST_DECL:
25434 gen_namelist_decl (DECL_NAME (decl), context_die,
25435 NAMELIST_DECL_ASSOCIATED_DECL (decl));
25436 break;
25438 default:
25439 /* Probably some frontend-internal decl. Assume we don't care. */
25440 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
25441 break;
25444 return NULL;
25447 /* Output initial debug information for global DECL. Called at the
25448 end of the parsing process.
25450 This is the initial debug generation process. As such, the DIEs
25451 generated may be incomplete. A later debug generation pass
25452 (dwarf2out_late_global_decl) will augment the information generated
25453 in this pass (e.g., with complete location info). */
25455 static void
25456 dwarf2out_early_global_decl (tree decl)
25458 set_early_dwarf s;
25460 /* gen_decl_die() will set DECL_ABSTRACT because
25461 cgraph_function_possibly_inlined_p() returns true. This is in
25462 turn will cause DW_AT_inline attributes to be set.
25464 This happens because at early dwarf generation, there is no
25465 cgraph information, causing cgraph_function_possibly_inlined_p()
25466 to return true. Trick cgraph_function_possibly_inlined_p()
25467 while we generate dwarf early. */
25468 bool save = symtab->global_info_ready;
25469 symtab->global_info_ready = true;
25471 /* We don't handle TYPE_DECLs. If required, they'll be reached via
25472 other DECLs and they can point to template types or other things
25473 that dwarf2out can't handle when done via dwarf2out_decl. */
25474 if (TREE_CODE (decl) != TYPE_DECL
25475 && TREE_CODE (decl) != PARM_DECL)
25477 tree save_fndecl = current_function_decl;
25478 if (TREE_CODE (decl) == FUNCTION_DECL)
25480 /* No cfun means the symbol has no body, so there's nothing
25481 to emit. */
25482 if (!DECL_STRUCT_FUNCTION (decl))
25483 goto early_decl_exit;
25485 /* For nested functions, make sure we have DIEs for the parents first
25486 so that all nested DIEs are generated at the proper scope in the
25487 first shot. */
25488 tree context = decl_function_context (decl);
25489 if (context != NULL && lookup_decl_die (context) == NULL)
25491 current_function_decl = context;
25492 dwarf2out_decl (context);
25495 current_function_decl = decl;
25497 dwarf2out_decl (decl);
25498 if (TREE_CODE (decl) == FUNCTION_DECL)
25499 current_function_decl = save_fndecl;
25501 early_decl_exit:
25502 symtab->global_info_ready = save;
25505 /* Output debug information for global decl DECL. Called from
25506 toplev.c after compilation proper has finished. */
25508 static void
25509 dwarf2out_late_global_decl (tree decl)
25511 /* Fill-in any location information we were unable to determine
25512 on the first pass. */
25513 if (VAR_P (decl) && !POINTER_BOUNDS_P (decl))
25515 dw_die_ref die = lookup_decl_die (decl);
25517 /* We have to generate early debug late for LTO. */
25518 if (! die && in_lto_p)
25520 dwarf2out_decl (decl);
25521 die = lookup_decl_die (decl);
25524 if (die)
25526 /* We get called via the symtab code invoking late_global_decl
25527 for symbols that are optimized out. Do not add locations
25528 for those. */
25529 varpool_node *node = varpool_node::get (decl);
25530 if (! node || ! node->definition)
25531 tree_add_const_value_attribute_for_decl (die, decl);
25532 else
25533 add_location_or_const_value_attribute (die, decl, false);
25538 /* Output debug information for type decl DECL. Called from toplev.c
25539 and from language front ends (to record built-in types). */
25540 static void
25541 dwarf2out_type_decl (tree decl, int local)
25543 if (!local)
25545 set_early_dwarf s;
25546 dwarf2out_decl (decl);
25550 /* Output debug information for imported module or decl DECL.
25551 NAME is non-NULL name in the lexical block if the decl has been renamed.
25552 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
25553 that DECL belongs to.
25554 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
25555 static void
25556 dwarf2out_imported_module_or_decl_1 (tree decl,
25557 tree name,
25558 tree lexical_block,
25559 dw_die_ref lexical_block_die)
25561 expanded_location xloc;
25562 dw_die_ref imported_die = NULL;
25563 dw_die_ref at_import_die;
25565 if (TREE_CODE (decl) == IMPORTED_DECL)
25567 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
25568 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
25569 gcc_assert (decl);
25571 else
25572 xloc = expand_location (input_location);
25574 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
25576 at_import_die = force_type_die (TREE_TYPE (decl));
25577 /* For namespace N { typedef void T; } using N::T; base_type_die
25578 returns NULL, but DW_TAG_imported_declaration requires
25579 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
25580 if (!at_import_die)
25582 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
25583 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
25584 at_import_die = lookup_type_die (TREE_TYPE (decl));
25585 gcc_assert (at_import_die);
25588 else
25590 at_import_die = lookup_decl_die (decl);
25591 if (!at_import_die)
25593 /* If we're trying to avoid duplicate debug info, we may not have
25594 emitted the member decl for this field. Emit it now. */
25595 if (TREE_CODE (decl) == FIELD_DECL)
25597 tree type = DECL_CONTEXT (decl);
25599 if (TYPE_CONTEXT (type)
25600 && TYPE_P (TYPE_CONTEXT (type))
25601 && !should_emit_struct_debug (TYPE_CONTEXT (type),
25602 DINFO_USAGE_DIR_USE))
25603 return;
25604 gen_type_die_for_member (type, decl,
25605 get_context_die (TYPE_CONTEXT (type)));
25607 if (TREE_CODE (decl) == NAMELIST_DECL)
25608 at_import_die = gen_namelist_decl (DECL_NAME (decl),
25609 get_context_die (DECL_CONTEXT (decl)),
25610 NULL_TREE);
25611 else
25612 at_import_die = force_decl_die (decl);
25616 if (TREE_CODE (decl) == NAMESPACE_DECL)
25618 if (dwarf_version >= 3 || !dwarf_strict)
25619 imported_die = new_die (DW_TAG_imported_module,
25620 lexical_block_die,
25621 lexical_block);
25622 else
25623 return;
25625 else
25626 imported_die = new_die (DW_TAG_imported_declaration,
25627 lexical_block_die,
25628 lexical_block);
25630 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
25631 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
25632 if (debug_column_info && xloc.column)
25633 add_AT_unsigned (imported_die, DW_AT_decl_column, xloc.column);
25634 if (name)
25635 add_AT_string (imported_die, DW_AT_name,
25636 IDENTIFIER_POINTER (name));
25637 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
25640 /* Output debug information for imported module or decl DECL.
25641 NAME is non-NULL name in context if the decl has been renamed.
25642 CHILD is true if decl is one of the renamed decls as part of
25643 importing whole module. */
25645 static void
25646 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
25647 bool child)
25649 /* dw_die_ref at_import_die; */
25650 dw_die_ref scope_die;
25652 if (debug_info_level <= DINFO_LEVEL_TERSE)
25653 return;
25655 gcc_assert (decl);
25657 set_early_dwarf s;
25659 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
25660 We need decl DIE for reference and scope die. First, get DIE for the decl
25661 itself. */
25663 /* Get the scope die for decl context. Use comp_unit_die for global module
25664 or decl. If die is not found for non globals, force new die. */
25665 if (context
25666 && TYPE_P (context)
25667 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
25668 return;
25670 scope_die = get_context_die (context);
25672 if (child)
25674 /* DW_TAG_imported_module was introduced in the DWARFv3 specification, so
25675 there is nothing we can do, here. */
25676 if (dwarf_version < 3 && dwarf_strict)
25677 return;
25679 gcc_assert (scope_die->die_child);
25680 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
25681 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
25682 scope_die = scope_die->die_child;
25685 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
25686 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
25689 /* Output debug information for namelists. */
25691 static dw_die_ref
25692 gen_namelist_decl (tree name, dw_die_ref scope_die, tree item_decls)
25694 dw_die_ref nml_die, nml_item_die, nml_item_ref_die;
25695 tree value;
25696 unsigned i;
25698 if (debug_info_level <= DINFO_LEVEL_TERSE)
25699 return NULL;
25701 gcc_assert (scope_die != NULL);
25702 nml_die = new_die (DW_TAG_namelist, scope_die, NULL);
25703 add_AT_string (nml_die, DW_AT_name, IDENTIFIER_POINTER (name));
25705 /* If there are no item_decls, we have a nondefining namelist, e.g.
25706 with USE association; hence, set DW_AT_declaration. */
25707 if (item_decls == NULL_TREE)
25709 add_AT_flag (nml_die, DW_AT_declaration, 1);
25710 return nml_die;
25713 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (item_decls), i, value)
25715 nml_item_ref_die = lookup_decl_die (value);
25716 if (!nml_item_ref_die)
25717 nml_item_ref_die = force_decl_die (value);
25719 nml_item_die = new_die (DW_TAG_namelist_item, nml_die, NULL);
25720 add_AT_die_ref (nml_item_die, DW_AT_namelist_items, nml_item_ref_die);
25722 return nml_die;
25726 /* Write the debugging output for DECL and return the DIE. */
25728 static void
25729 dwarf2out_decl (tree decl)
25731 dw_die_ref context_die = comp_unit_die ();
25733 switch (TREE_CODE (decl))
25735 case ERROR_MARK:
25736 return;
25738 case FUNCTION_DECL:
25739 /* What we would really like to do here is to filter out all mere
25740 file-scope declarations of file-scope functions which are never
25741 referenced later within this translation unit (and keep all of ones
25742 that *are* referenced later on) but we aren't clairvoyant, so we have
25743 no idea which functions will be referenced in the future (i.e. later
25744 on within the current translation unit). So here we just ignore all
25745 file-scope function declarations which are not also definitions. If
25746 and when the debugger needs to know something about these functions,
25747 it will have to hunt around and find the DWARF information associated
25748 with the definition of the function.
25750 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
25751 nodes represent definitions and which ones represent mere
25752 declarations. We have to check DECL_INITIAL instead. That's because
25753 the C front-end supports some weird semantics for "extern inline"
25754 function definitions. These can get inlined within the current
25755 translation unit (and thus, we need to generate Dwarf info for their
25756 abstract instances so that the Dwarf info for the concrete inlined
25757 instances can have something to refer to) but the compiler never
25758 generates any out-of-lines instances of such things (despite the fact
25759 that they *are* definitions).
25761 The important point is that the C front-end marks these "extern
25762 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
25763 them anyway. Note that the C++ front-end also plays some similar games
25764 for inline function definitions appearing within include files which
25765 also contain `#pragma interface' pragmas.
25767 If we are called from dwarf2out_abstract_function output a DIE
25768 anyway. We can end up here this way with early inlining and LTO
25769 where the inlined function is output in a different LTRANS unit
25770 or not at all. */
25771 if (DECL_INITIAL (decl) == NULL_TREE
25772 && ! DECL_ABSTRACT_P (decl))
25773 return;
25775 /* If we're a nested function, initially use a parent of NULL; if we're
25776 a plain function, this will be fixed up in decls_for_scope. If
25777 we're a method, it will be ignored, since we already have a DIE. */
25778 if (decl_function_context (decl)
25779 /* But if we're in terse mode, we don't care about scope. */
25780 && debug_info_level > DINFO_LEVEL_TERSE)
25781 context_die = NULL;
25782 break;
25784 case VAR_DECL:
25785 /* For local statics lookup proper context die. */
25786 if (local_function_static (decl))
25787 context_die = lookup_decl_die (DECL_CONTEXT (decl));
25789 /* If we are in terse mode, don't generate any DIEs to represent any
25790 variable declarations or definitions. */
25791 if (debug_info_level <= DINFO_LEVEL_TERSE)
25792 return;
25793 break;
25795 case CONST_DECL:
25796 if (debug_info_level <= DINFO_LEVEL_TERSE)
25797 return;
25798 if (!is_fortran () && !is_ada ())
25799 return;
25800 if (TREE_STATIC (decl) && decl_function_context (decl))
25801 context_die = lookup_decl_die (DECL_CONTEXT (decl));
25802 break;
25804 case NAMESPACE_DECL:
25805 case IMPORTED_DECL:
25806 if (debug_info_level <= DINFO_LEVEL_TERSE)
25807 return;
25808 if (lookup_decl_die (decl) != NULL)
25809 return;
25810 break;
25812 case TYPE_DECL:
25813 /* Don't emit stubs for types unless they are needed by other DIEs. */
25814 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
25815 return;
25817 /* Don't bother trying to generate any DIEs to represent any of the
25818 normal built-in types for the language we are compiling. */
25819 if (DECL_IS_BUILTIN (decl))
25820 return;
25822 /* If we are in terse mode, don't generate any DIEs for types. */
25823 if (debug_info_level <= DINFO_LEVEL_TERSE)
25824 return;
25826 /* If we're a function-scope tag, initially use a parent of NULL;
25827 this will be fixed up in decls_for_scope. */
25828 if (decl_function_context (decl))
25829 context_die = NULL;
25831 break;
25833 case NAMELIST_DECL:
25834 break;
25836 default:
25837 return;
25840 gen_decl_die (decl, NULL, NULL, context_die);
25842 if (flag_checking)
25844 dw_die_ref die = lookup_decl_die (decl);
25845 if (die)
25846 check_die (die);
25850 /* Write the debugging output for DECL. */
25852 static void
25853 dwarf2out_function_decl (tree decl)
25855 dwarf2out_decl (decl);
25856 call_arg_locations = NULL;
25857 call_arg_loc_last = NULL;
25858 call_site_count = -1;
25859 tail_call_site_count = -1;
25860 decl_loc_table->empty ();
25861 cached_dw_loc_list_table->empty ();
25864 /* Output a marker (i.e. a label) for the beginning of the generated code for
25865 a lexical block. */
25867 static void
25868 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
25869 unsigned int blocknum)
25871 switch_to_section (current_function_section ());
25872 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
25875 /* Output a marker (i.e. a label) for the end of the generated code for a
25876 lexical block. */
25878 static void
25879 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
25881 switch_to_section (current_function_section ());
25882 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
25885 /* Returns nonzero if it is appropriate not to emit any debugging
25886 information for BLOCK, because it doesn't contain any instructions.
25888 Don't allow this for blocks with nested functions or local classes
25889 as we would end up with orphans, and in the presence of scheduling
25890 we may end up calling them anyway. */
25892 static bool
25893 dwarf2out_ignore_block (const_tree block)
25895 tree decl;
25896 unsigned int i;
25898 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
25899 if (TREE_CODE (decl) == FUNCTION_DECL
25900 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
25901 return 0;
25902 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
25904 decl = BLOCK_NONLOCALIZED_VAR (block, i);
25905 if (TREE_CODE (decl) == FUNCTION_DECL
25906 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
25907 return 0;
25910 return 1;
25913 /* Hash table routines for file_hash. */
25915 bool
25916 dwarf_file_hasher::equal (dwarf_file_data *p1, const char *p2)
25918 return filename_cmp (p1->filename, p2) == 0;
25921 hashval_t
25922 dwarf_file_hasher::hash (dwarf_file_data *p)
25924 return htab_hash_string (p->filename);
25927 /* Lookup FILE_NAME (in the list of filenames that we know about here in
25928 dwarf2out.c) and return its "index". The index of each (known) filename is
25929 just a unique number which is associated with only that one filename. We
25930 need such numbers for the sake of generating labels (in the .debug_sfnames
25931 section) and references to those files numbers (in the .debug_srcinfo
25932 and .debug_macinfo sections). If the filename given as an argument is not
25933 found in our current list, add it to the list and assign it the next
25934 available unique index number. */
25936 static struct dwarf_file_data *
25937 lookup_filename (const char *file_name)
25939 struct dwarf_file_data * created;
25941 if (!file_name)
25942 return NULL;
25944 dwarf_file_data **slot
25945 = file_table->find_slot_with_hash (file_name, htab_hash_string (file_name),
25946 INSERT);
25947 if (*slot)
25948 return *slot;
25950 created = ggc_alloc<dwarf_file_data> ();
25951 created->filename = file_name;
25952 created->emitted_number = 0;
25953 *slot = created;
25954 return created;
25957 /* If the assembler will construct the file table, then translate the compiler
25958 internal file table number into the assembler file table number, and emit
25959 a .file directive if we haven't already emitted one yet. The file table
25960 numbers are different because we prune debug info for unused variables and
25961 types, which may include filenames. */
25963 static int
25964 maybe_emit_file (struct dwarf_file_data * fd)
25966 if (! fd->emitted_number)
25968 if (last_emitted_file)
25969 fd->emitted_number = last_emitted_file->emitted_number + 1;
25970 else
25971 fd->emitted_number = 1;
25972 last_emitted_file = fd;
25974 if (DWARF2_ASM_LINE_DEBUG_INFO)
25976 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
25977 output_quoted_string (asm_out_file,
25978 remap_debug_filename (fd->filename));
25979 fputc ('\n', asm_out_file);
25983 return fd->emitted_number;
25986 /* Schedule generation of a DW_AT_const_value attribute to DIE.
25987 That generation should happen after function debug info has been
25988 generated. The value of the attribute is the constant value of ARG. */
25990 static void
25991 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
25993 die_arg_entry entry;
25995 if (!die || !arg)
25996 return;
25998 gcc_assert (early_dwarf);
26000 if (!tmpl_value_parm_die_table)
26001 vec_alloc (tmpl_value_parm_die_table, 32);
26003 entry.die = die;
26004 entry.arg = arg;
26005 vec_safe_push (tmpl_value_parm_die_table, entry);
26008 /* Return TRUE if T is an instance of generic type, FALSE
26009 otherwise. */
26011 static bool
26012 generic_type_p (tree t)
26014 if (t == NULL_TREE || !TYPE_P (t))
26015 return false;
26016 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
26019 /* Schedule the generation of the generic parameter dies for the
26020 instance of generic type T. The proper generation itself is later
26021 done by gen_scheduled_generic_parms_dies. */
26023 static void
26024 schedule_generic_params_dies_gen (tree t)
26026 if (!generic_type_p (t))
26027 return;
26029 gcc_assert (early_dwarf);
26031 if (!generic_type_instances)
26032 vec_alloc (generic_type_instances, 256);
26034 vec_safe_push (generic_type_instances, t);
26037 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
26038 by append_entry_to_tmpl_value_parm_die_table. This function must
26039 be called after function DIEs have been generated. */
26041 static void
26042 gen_remaining_tmpl_value_param_die_attribute (void)
26044 if (tmpl_value_parm_die_table)
26046 unsigned i, j;
26047 die_arg_entry *e;
26049 /* We do this in two phases - first get the cases we can
26050 handle during early-finish, preserving those we cannot
26051 (containing symbolic constants where we don't yet know
26052 whether we are going to output the referenced symbols).
26053 For those we try again at late-finish. */
26054 j = 0;
26055 FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
26057 if (!tree_add_const_value_attribute (e->die, e->arg))
26059 dw_loc_descr_ref loc = NULL;
26060 if (! early_dwarf
26061 && (dwarf_version >= 5 || !dwarf_strict))
26062 loc = loc_descriptor_from_tree (e->arg, 2, NULL);
26063 if (loc)
26064 add_AT_loc (e->die, DW_AT_location, loc);
26065 else
26066 (*tmpl_value_parm_die_table)[j++] = *e;
26069 tmpl_value_parm_die_table->truncate (j);
26073 /* Generate generic parameters DIEs for instances of generic types
26074 that have been previously scheduled by
26075 schedule_generic_params_dies_gen. This function must be called
26076 after all the types of the CU have been laid out. */
26078 static void
26079 gen_scheduled_generic_parms_dies (void)
26081 unsigned i;
26082 tree t;
26084 if (!generic_type_instances)
26085 return;
26087 FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
26088 if (COMPLETE_TYPE_P (t))
26089 gen_generic_params_dies (t);
26091 generic_type_instances = NULL;
26095 /* Replace DW_AT_name for the decl with name. */
26097 static void
26098 dwarf2out_set_name (tree decl, tree name)
26100 dw_die_ref die;
26101 dw_attr_node *attr;
26102 const char *dname;
26104 die = TYPE_SYMTAB_DIE (decl);
26105 if (!die)
26106 return;
26108 dname = dwarf2_name (name, 0);
26109 if (!dname)
26110 return;
26112 attr = get_AT (die, DW_AT_name);
26113 if (attr)
26115 struct indirect_string_node *node;
26117 node = find_AT_string (dname);
26118 /* replace the string. */
26119 attr->dw_attr_val.v.val_str = node;
26122 else
26123 add_name_attribute (die, dname);
26126 /* True if before or during processing of the first function being emitted. */
26127 static bool in_first_function_p = true;
26128 /* True if loc_note during dwarf2out_var_location call might still be
26129 before first real instruction at address equal to .Ltext0. */
26130 static bool maybe_at_text_label_p = true;
26131 /* One above highest N where .LVLN label might be equal to .Ltext0 label. */
26132 static unsigned int first_loclabel_num_not_at_text_label;
26134 /* Called by the final INSN scan whenever we see a var location. We
26135 use it to drop labels in the right places, and throw the location in
26136 our lookup table. */
26138 static void
26139 dwarf2out_var_location (rtx_insn *loc_note)
26141 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
26142 struct var_loc_node *newloc;
26143 rtx_insn *next_real, *next_note;
26144 rtx_insn *call_insn = NULL;
26145 static const char *last_label;
26146 static const char *last_postcall_label;
26147 static bool last_in_cold_section_p;
26148 static rtx_insn *expected_next_loc_note;
26149 tree decl;
26150 bool var_loc_p;
26152 if (!NOTE_P (loc_note))
26154 if (CALL_P (loc_note))
26156 call_site_count++;
26157 if (SIBLING_CALL_P (loc_note))
26158 tail_call_site_count++;
26159 if (optimize == 0 && !flag_var_tracking)
26161 /* When the var-tracking pass is not running, there is no note
26162 for indirect calls whose target is compile-time known. In this
26163 case, process such calls specifically so that we generate call
26164 sites for them anyway. */
26165 rtx x = PATTERN (loc_note);
26166 if (GET_CODE (x) == PARALLEL)
26167 x = XVECEXP (x, 0, 0);
26168 if (GET_CODE (x) == SET)
26169 x = SET_SRC (x);
26170 if (GET_CODE (x) == CALL)
26171 x = XEXP (x, 0);
26172 if (!MEM_P (x)
26173 || GET_CODE (XEXP (x, 0)) != SYMBOL_REF
26174 || !SYMBOL_REF_DECL (XEXP (x, 0))
26175 || (TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0)))
26176 != FUNCTION_DECL))
26178 call_insn = loc_note;
26179 loc_note = NULL;
26180 var_loc_p = false;
26182 next_real = next_real_insn (call_insn);
26183 next_note = NULL;
26184 cached_next_real_insn = NULL;
26185 goto create_label;
26189 return;
26192 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
26193 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
26194 return;
26196 /* Optimize processing a large consecutive sequence of location
26197 notes so we don't spend too much time in next_real_insn. If the
26198 next insn is another location note, remember the next_real_insn
26199 calculation for next time. */
26200 next_real = cached_next_real_insn;
26201 if (next_real)
26203 if (expected_next_loc_note != loc_note)
26204 next_real = NULL;
26207 next_note = NEXT_INSN (loc_note);
26208 if (! next_note
26209 || next_note->deleted ()
26210 || ! NOTE_P (next_note)
26211 || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
26212 && NOTE_KIND (next_note) != NOTE_INSN_CALL_ARG_LOCATION))
26213 next_note = NULL;
26215 if (! next_real)
26216 next_real = next_real_insn (loc_note);
26218 if (next_note)
26220 expected_next_loc_note = next_note;
26221 cached_next_real_insn = next_real;
26223 else
26224 cached_next_real_insn = NULL;
26226 /* If there are no instructions which would be affected by this note,
26227 don't do anything. */
26228 if (var_loc_p
26229 && next_real == NULL_RTX
26230 && !NOTE_DURING_CALL_P (loc_note))
26231 return;
26233 create_label:
26235 if (next_real == NULL_RTX)
26236 next_real = get_last_insn ();
26238 /* If there were any real insns between note we processed last time
26239 and this note (or if it is the first note), clear
26240 last_{,postcall_}label so that they are not reused this time. */
26241 if (last_var_location_insn == NULL_RTX
26242 || last_var_location_insn != next_real
26243 || last_in_cold_section_p != in_cold_section_p)
26245 last_label = NULL;
26246 last_postcall_label = NULL;
26249 if (var_loc_p)
26251 decl = NOTE_VAR_LOCATION_DECL (loc_note);
26252 newloc = add_var_loc_to_decl (decl, loc_note,
26253 NOTE_DURING_CALL_P (loc_note)
26254 ? last_postcall_label : last_label);
26255 if (newloc == NULL)
26256 return;
26258 else
26260 decl = NULL_TREE;
26261 newloc = NULL;
26264 /* If there were no real insns between note we processed last time
26265 and this note, use the label we emitted last time. Otherwise
26266 create a new label and emit it. */
26267 if (last_label == NULL)
26269 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
26270 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
26271 loclabel_num++;
26272 last_label = ggc_strdup (loclabel);
26273 /* See if loclabel might be equal to .Ltext0. If yes,
26274 bump first_loclabel_num_not_at_text_label. */
26275 if (!have_multiple_function_sections
26276 && in_first_function_p
26277 && maybe_at_text_label_p)
26279 static rtx_insn *last_start;
26280 rtx_insn *insn;
26281 for (insn = loc_note; insn; insn = previous_insn (insn))
26282 if (insn == last_start)
26283 break;
26284 else if (!NONDEBUG_INSN_P (insn))
26285 continue;
26286 else
26288 rtx body = PATTERN (insn);
26289 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
26290 continue;
26291 /* Inline asm could occupy zero bytes. */
26292 else if (GET_CODE (body) == ASM_INPUT
26293 || asm_noperands (body) >= 0)
26294 continue;
26295 #ifdef HAVE_attr_length
26296 else if (get_attr_min_length (insn) == 0)
26297 continue;
26298 #endif
26299 else
26301 /* Assume insn has non-zero length. */
26302 maybe_at_text_label_p = false;
26303 break;
26306 if (maybe_at_text_label_p)
26308 last_start = loc_note;
26309 first_loclabel_num_not_at_text_label = loclabel_num;
26314 gcc_assert ((loc_note == NULL_RTX && call_insn != NULL_RTX)
26315 || (loc_note != NULL_RTX && call_insn == NULL_RTX));
26317 if (!var_loc_p)
26319 struct call_arg_loc_node *ca_loc
26320 = ggc_cleared_alloc<call_arg_loc_node> ();
26321 rtx_insn *prev
26322 = loc_note != NULL_RTX ? prev_real_insn (loc_note) : call_insn;
26324 ca_loc->call_arg_loc_note = loc_note;
26325 ca_loc->next = NULL;
26326 ca_loc->label = last_label;
26327 gcc_assert (prev
26328 && (CALL_P (prev)
26329 || (NONJUMP_INSN_P (prev)
26330 && GET_CODE (PATTERN (prev)) == SEQUENCE
26331 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
26332 if (!CALL_P (prev))
26333 prev = as_a <rtx_sequence *> (PATTERN (prev))->insn (0);
26334 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
26336 /* Look for a SYMBOL_REF in the "prev" instruction. */
26337 rtx x = get_call_rtx_from (PATTERN (prev));
26338 if (x)
26340 /* Try to get the call symbol, if any. */
26341 if (MEM_P (XEXP (x, 0)))
26342 x = XEXP (x, 0);
26343 /* First, look for a memory access to a symbol_ref. */
26344 if (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
26345 && SYMBOL_REF_DECL (XEXP (x, 0))
26346 && TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0))) == FUNCTION_DECL)
26347 ca_loc->symbol_ref = XEXP (x, 0);
26348 /* Otherwise, look at a compile-time known user-level function
26349 declaration. */
26350 else if (MEM_P (x)
26351 && MEM_EXPR (x)
26352 && TREE_CODE (MEM_EXPR (x)) == FUNCTION_DECL)
26353 ca_loc->symbol_ref = XEXP (DECL_RTL (MEM_EXPR (x)), 0);
26356 ca_loc->block = insn_scope (prev);
26357 if (call_arg_locations)
26358 call_arg_loc_last->next = ca_loc;
26359 else
26360 call_arg_locations = ca_loc;
26361 call_arg_loc_last = ca_loc;
26363 else if (loc_note != NULL_RTX && !NOTE_DURING_CALL_P (loc_note))
26364 newloc->label = last_label;
26365 else
26367 if (!last_postcall_label)
26369 sprintf (loclabel, "%s-1", last_label);
26370 last_postcall_label = ggc_strdup (loclabel);
26372 newloc->label = last_postcall_label;
26375 last_var_location_insn = next_real;
26376 last_in_cold_section_p = in_cold_section_p;
26379 /* Called from finalize_size_functions for size functions so that their body
26380 can be encoded in the debug info to describe the layout of variable-length
26381 structures. */
26383 static void
26384 dwarf2out_size_function (tree decl)
26386 function_to_dwarf_procedure (decl);
26389 /* Note in one location list that text section has changed. */
26392 var_location_switch_text_section_1 (var_loc_list **slot, void *)
26394 var_loc_list *list = *slot;
26395 if (list->first)
26396 list->last_before_switch
26397 = list->last->next ? list->last->next : list->last;
26398 return 1;
26401 /* Note in all location lists that text section has changed. */
26403 static void
26404 var_location_switch_text_section (void)
26406 if (decl_loc_table == NULL)
26407 return;
26409 decl_loc_table->traverse<void *, var_location_switch_text_section_1> (NULL);
26412 /* Create a new line number table. */
26414 static dw_line_info_table *
26415 new_line_info_table (void)
26417 dw_line_info_table *table;
26419 table = ggc_cleared_alloc<dw_line_info_table> ();
26420 table->file_num = 1;
26421 table->line_num = 1;
26422 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
26424 return table;
26427 /* Lookup the "current" table into which we emit line info, so
26428 that we don't have to do it for every source line. */
26430 static void
26431 set_cur_line_info_table (section *sec)
26433 dw_line_info_table *table;
26435 if (sec == text_section)
26436 table = text_section_line_info;
26437 else if (sec == cold_text_section)
26439 table = cold_text_section_line_info;
26440 if (!table)
26442 cold_text_section_line_info = table = new_line_info_table ();
26443 table->end_label = cold_end_label;
26446 else
26448 const char *end_label;
26450 if (flag_reorder_blocks_and_partition)
26452 if (in_cold_section_p)
26453 end_label = crtl->subsections.cold_section_end_label;
26454 else
26455 end_label = crtl->subsections.hot_section_end_label;
26457 else
26459 char label[MAX_ARTIFICIAL_LABEL_BYTES];
26460 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
26461 current_function_funcdef_no);
26462 end_label = ggc_strdup (label);
26465 table = new_line_info_table ();
26466 table->end_label = end_label;
26468 vec_safe_push (separate_line_info, table);
26471 if (DWARF2_ASM_LINE_DEBUG_INFO)
26472 table->is_stmt = (cur_line_info_table
26473 ? cur_line_info_table->is_stmt
26474 : DWARF_LINE_DEFAULT_IS_STMT_START);
26475 cur_line_info_table = table;
26479 /* We need to reset the locations at the beginning of each
26480 function. We can't do this in the end_function hook, because the
26481 declarations that use the locations won't have been output when
26482 that hook is called. Also compute have_multiple_function_sections here. */
26484 static void
26485 dwarf2out_begin_function (tree fun)
26487 section *sec = function_section (fun);
26489 if (sec != text_section)
26490 have_multiple_function_sections = true;
26492 if (flag_reorder_blocks_and_partition && !cold_text_section)
26494 gcc_assert (current_function_decl == fun);
26495 cold_text_section = unlikely_text_section ();
26496 switch_to_section (cold_text_section);
26497 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
26498 switch_to_section (sec);
26501 dwarf2out_note_section_used ();
26502 call_site_count = 0;
26503 tail_call_site_count = 0;
26505 set_cur_line_info_table (sec);
26508 /* Helper function of dwarf2out_end_function, called only after emitting
26509 the very first function into assembly. Check if some .debug_loc range
26510 might end with a .LVL* label that could be equal to .Ltext0.
26511 In that case we must force using absolute addresses in .debug_loc ranges,
26512 because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
26513 .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
26514 list terminator.
26515 Set have_multiple_function_sections to true in that case and
26516 terminate htab traversal. */
26519 find_empty_loc_ranges_at_text_label (var_loc_list **slot, int)
26521 var_loc_list *entry = *slot;
26522 struct var_loc_node *node;
26524 node = entry->first;
26525 if (node && node->next && node->next->label)
26527 unsigned int i;
26528 const char *label = node->next->label;
26529 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
26531 for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
26533 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
26534 if (strcmp (label, loclabel) == 0)
26536 have_multiple_function_sections = true;
26537 return 0;
26541 return 1;
26544 /* Hook called after emitting a function into assembly.
26545 This does something only for the very first function emitted. */
26547 static void
26548 dwarf2out_end_function (unsigned int)
26550 if (in_first_function_p
26551 && !have_multiple_function_sections
26552 && first_loclabel_num_not_at_text_label
26553 && decl_loc_table)
26554 decl_loc_table->traverse<int, find_empty_loc_ranges_at_text_label> (0);
26555 in_first_function_p = false;
26556 maybe_at_text_label_p = false;
26559 /* Temporary holder for dwarf2out_register_main_translation_unit. Used to let
26560 front-ends register a translation unit even before dwarf2out_init is
26561 called. */
26562 static tree main_translation_unit = NULL_TREE;
26564 /* Hook called by front-ends after they built their main translation unit.
26565 Associate comp_unit_die to UNIT. */
26567 static void
26568 dwarf2out_register_main_translation_unit (tree unit)
26570 gcc_assert (TREE_CODE (unit) == TRANSLATION_UNIT_DECL
26571 && main_translation_unit == NULL_TREE);
26572 main_translation_unit = unit;
26573 /* If dwarf2out_init has not been called yet, it will perform the association
26574 itself looking at main_translation_unit. */
26575 if (decl_die_table != NULL)
26576 equate_decl_number_to_die (unit, comp_unit_die ());
26579 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
26581 static void
26582 push_dw_line_info_entry (dw_line_info_table *table,
26583 enum dw_line_info_opcode opcode, unsigned int val)
26585 dw_line_info_entry e;
26586 e.opcode = opcode;
26587 e.val = val;
26588 vec_safe_push (table->entries, e);
26591 /* Output a label to mark the beginning of a source code line entry
26592 and record information relating to this source line, in
26593 'line_info_table' for later output of the .debug_line section. */
26594 /* ??? The discriminator parameter ought to be unsigned. */
26596 static void
26597 dwarf2out_source_line (unsigned int line, unsigned int column,
26598 const char *filename,
26599 int discriminator, bool is_stmt)
26601 unsigned int file_num;
26602 dw_line_info_table *table;
26604 if (debug_info_level < DINFO_LEVEL_TERSE || line == 0)
26605 return;
26607 /* The discriminator column was added in dwarf4. Simplify the below
26608 by simply removing it if we're not supposed to output it. */
26609 if (dwarf_version < 4 && dwarf_strict)
26610 discriminator = 0;
26612 if (!debug_column_info)
26613 column = 0;
26615 table = cur_line_info_table;
26616 file_num = maybe_emit_file (lookup_filename (filename));
26618 /* ??? TODO: Elide duplicate line number entries. Traditionally,
26619 the debugger has used the second (possibly duplicate) line number
26620 at the beginning of the function to mark the end of the prologue.
26621 We could eliminate any other duplicates within the function. For
26622 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
26623 that second line number entry. */
26624 /* Recall that this end-of-prologue indication is *not* the same thing
26625 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
26626 to which the hook corresponds, follows the last insn that was
26627 emitted by gen_prologue. What we need is to precede the first insn
26628 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
26629 insn that corresponds to something the user wrote. These may be
26630 very different locations once scheduling is enabled. */
26632 if (0 && file_num == table->file_num
26633 && line == table->line_num
26634 && column == table->column_num
26635 && discriminator == table->discrim_num
26636 && is_stmt == table->is_stmt)
26637 return;
26639 switch_to_section (current_function_section ());
26641 /* If requested, emit something human-readable. */
26642 if (flag_debug_asm)
26644 if (debug_column_info)
26645 fprintf (asm_out_file, "\t%s %s:%d:%d\n", ASM_COMMENT_START,
26646 filename, line, column);
26647 else
26648 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
26649 filename, line);
26652 if (DWARF2_ASM_LINE_DEBUG_INFO)
26654 /* Emit the .loc directive understood by GNU as. */
26655 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
26656 file_num, line, is_stmt, discriminator */
26657 fputs ("\t.loc ", asm_out_file);
26658 fprint_ul (asm_out_file, file_num);
26659 putc (' ', asm_out_file);
26660 fprint_ul (asm_out_file, line);
26661 putc (' ', asm_out_file);
26662 if (debug_column_info)
26663 fprint_ul (asm_out_file, column);
26664 else
26665 putc ('0', asm_out_file);
26667 if (is_stmt != table->is_stmt)
26669 fputs (" is_stmt ", asm_out_file);
26670 putc (is_stmt ? '1' : '0', asm_out_file);
26672 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
26674 gcc_assert (discriminator > 0);
26675 fputs (" discriminator ", asm_out_file);
26676 fprint_ul (asm_out_file, (unsigned long) discriminator);
26678 putc ('\n', asm_out_file);
26680 else
26682 unsigned int label_num = ++line_info_label_num;
26684 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
26686 push_dw_line_info_entry (table, LI_set_address, label_num);
26687 if (file_num != table->file_num)
26688 push_dw_line_info_entry (table, LI_set_file, file_num);
26689 if (discriminator != table->discrim_num)
26690 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
26691 if (is_stmt != table->is_stmt)
26692 push_dw_line_info_entry (table, LI_negate_stmt, 0);
26693 push_dw_line_info_entry (table, LI_set_line, line);
26694 if (debug_column_info)
26695 push_dw_line_info_entry (table, LI_set_column, column);
26698 table->file_num = file_num;
26699 table->line_num = line;
26700 table->column_num = column;
26701 table->discrim_num = discriminator;
26702 table->is_stmt = is_stmt;
26703 table->in_use = true;
26706 /* Record the beginning of a new source file. */
26708 static void
26709 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
26711 if (flag_eliminate_dwarf2_dups)
26713 /* Record the beginning of the file for break_out_includes. */
26714 dw_die_ref bincl_die;
26716 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
26717 add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
26720 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
26722 macinfo_entry e;
26723 e.code = DW_MACINFO_start_file;
26724 e.lineno = lineno;
26725 e.info = ggc_strdup (filename);
26726 vec_safe_push (macinfo_table, e);
26730 /* Record the end of a source file. */
26732 static void
26733 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
26735 if (flag_eliminate_dwarf2_dups)
26736 /* Record the end of the file for break_out_includes. */
26737 new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
26739 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
26741 macinfo_entry e;
26742 e.code = DW_MACINFO_end_file;
26743 e.lineno = lineno;
26744 e.info = NULL;
26745 vec_safe_push (macinfo_table, e);
26749 /* Called from debug_define in toplev.c. The `buffer' parameter contains
26750 the tail part of the directive line, i.e. the part which is past the
26751 initial whitespace, #, whitespace, directive-name, whitespace part. */
26753 static void
26754 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
26755 const char *buffer ATTRIBUTE_UNUSED)
26757 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
26759 macinfo_entry e;
26760 /* Insert a dummy first entry to be able to optimize the whole
26761 predefined macro block using DW_MACRO_import. */
26762 if (macinfo_table->is_empty () && lineno <= 1)
26764 e.code = 0;
26765 e.lineno = 0;
26766 e.info = NULL;
26767 vec_safe_push (macinfo_table, e);
26769 e.code = DW_MACINFO_define;
26770 e.lineno = lineno;
26771 e.info = ggc_strdup (buffer);
26772 vec_safe_push (macinfo_table, e);
26776 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
26777 the tail part of the directive line, i.e. the part which is past the
26778 initial whitespace, #, whitespace, directive-name, whitespace part. */
26780 static void
26781 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
26782 const char *buffer ATTRIBUTE_UNUSED)
26784 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
26786 macinfo_entry e;
26787 /* Insert a dummy first entry to be able to optimize the whole
26788 predefined macro block using DW_MACRO_import. */
26789 if (macinfo_table->is_empty () && lineno <= 1)
26791 e.code = 0;
26792 e.lineno = 0;
26793 e.info = NULL;
26794 vec_safe_push (macinfo_table, e);
26796 e.code = DW_MACINFO_undef;
26797 e.lineno = lineno;
26798 e.info = ggc_strdup (buffer);
26799 vec_safe_push (macinfo_table, e);
26803 /* Helpers to manipulate hash table of CUs. */
26805 struct macinfo_entry_hasher : nofree_ptr_hash <macinfo_entry>
26807 static inline hashval_t hash (const macinfo_entry *);
26808 static inline bool equal (const macinfo_entry *, const macinfo_entry *);
26811 inline hashval_t
26812 macinfo_entry_hasher::hash (const macinfo_entry *entry)
26814 return htab_hash_string (entry->info);
26817 inline bool
26818 macinfo_entry_hasher::equal (const macinfo_entry *entry1,
26819 const macinfo_entry *entry2)
26821 return !strcmp (entry1->info, entry2->info);
26824 typedef hash_table<macinfo_entry_hasher> macinfo_hash_type;
26826 /* Output a single .debug_macinfo entry. */
26828 static void
26829 output_macinfo_op (macinfo_entry *ref)
26831 int file_num;
26832 size_t len;
26833 struct indirect_string_node *node;
26834 char label[MAX_ARTIFICIAL_LABEL_BYTES];
26835 struct dwarf_file_data *fd;
26837 switch (ref->code)
26839 case DW_MACINFO_start_file:
26840 fd = lookup_filename (ref->info);
26841 file_num = maybe_emit_file (fd);
26842 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
26843 dw2_asm_output_data_uleb128 (ref->lineno,
26844 "Included from line number %lu",
26845 (unsigned long) ref->lineno);
26846 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
26847 break;
26848 case DW_MACINFO_end_file:
26849 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
26850 break;
26851 case DW_MACINFO_define:
26852 case DW_MACINFO_undef:
26853 len = strlen (ref->info) + 1;
26854 if (!dwarf_strict
26855 && len > DWARF_OFFSET_SIZE
26856 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
26857 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
26859 ref->code = ref->code == DW_MACINFO_define
26860 ? DW_MACRO_define_strp : DW_MACRO_undef_strp;
26861 output_macinfo_op (ref);
26862 return;
26864 dw2_asm_output_data (1, ref->code,
26865 ref->code == DW_MACINFO_define
26866 ? "Define macro" : "Undefine macro");
26867 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
26868 (unsigned long) ref->lineno);
26869 dw2_asm_output_nstring (ref->info, -1, "The macro");
26870 break;
26871 case DW_MACRO_define_strp:
26872 case DW_MACRO_undef_strp:
26873 node = find_AT_string (ref->info);
26874 gcc_assert (node
26875 && (node->form == DW_FORM_strp
26876 || node->form == DW_FORM_GNU_str_index));
26877 dw2_asm_output_data (1, ref->code,
26878 ref->code == DW_MACRO_define_strp
26879 ? "Define macro strp"
26880 : "Undefine macro strp");
26881 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
26882 (unsigned long) ref->lineno);
26883 if (node->form == DW_FORM_strp)
26884 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
26885 debug_str_section, "The macro: \"%s\"",
26886 ref->info);
26887 else
26888 dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
26889 ref->info);
26890 break;
26891 case DW_MACRO_import:
26892 dw2_asm_output_data (1, ref->code, "Import");
26893 ASM_GENERATE_INTERNAL_LABEL (label,
26894 DEBUG_MACRO_SECTION_LABEL, ref->lineno);
26895 dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
26896 break;
26897 default:
26898 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
26899 ASM_COMMENT_START, (unsigned long) ref->code);
26900 break;
26904 /* Attempt to make a sequence of define/undef macinfo ops shareable with
26905 other compilation unit .debug_macinfo sections. IDX is the first
26906 index of a define/undef, return the number of ops that should be
26907 emitted in a comdat .debug_macinfo section and emit
26908 a DW_MACRO_import entry referencing it.
26909 If the define/undef entry should be emitted normally, return 0. */
26911 static unsigned
26912 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
26913 macinfo_hash_type **macinfo_htab)
26915 macinfo_entry *first, *second, *cur, *inc;
26916 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
26917 unsigned char checksum[16];
26918 struct md5_ctx ctx;
26919 char *grp_name, *tail;
26920 const char *base;
26921 unsigned int i, count, encoded_filename_len, linebuf_len;
26922 macinfo_entry **slot;
26924 first = &(*macinfo_table)[idx];
26925 second = &(*macinfo_table)[idx + 1];
26927 /* Optimize only if there are at least two consecutive define/undef ops,
26928 and either all of them are before first DW_MACINFO_start_file
26929 with lineno {0,1} (i.e. predefined macro block), or all of them are
26930 in some included header file. */
26931 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
26932 return 0;
26933 if (vec_safe_is_empty (files))
26935 if (first->lineno > 1 || second->lineno > 1)
26936 return 0;
26938 else if (first->lineno == 0)
26939 return 0;
26941 /* Find the last define/undef entry that can be grouped together
26942 with first and at the same time compute md5 checksum of their
26943 codes, linenumbers and strings. */
26944 md5_init_ctx (&ctx);
26945 for (i = idx; macinfo_table->iterate (i, &cur); i++)
26946 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
26947 break;
26948 else if (vec_safe_is_empty (files) && cur->lineno > 1)
26949 break;
26950 else
26952 unsigned char code = cur->code;
26953 md5_process_bytes (&code, 1, &ctx);
26954 checksum_uleb128 (cur->lineno, &ctx);
26955 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
26957 md5_finish_ctx (&ctx, checksum);
26958 count = i - idx;
26960 /* From the containing include filename (if any) pick up just
26961 usable characters from its basename. */
26962 if (vec_safe_is_empty (files))
26963 base = "";
26964 else
26965 base = lbasename (files->last ().info);
26966 for (encoded_filename_len = 0, i = 0; base[i]; i++)
26967 if (ISIDNUM (base[i]) || base[i] == '.')
26968 encoded_filename_len++;
26969 /* Count . at the end. */
26970 if (encoded_filename_len)
26971 encoded_filename_len++;
26973 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
26974 linebuf_len = strlen (linebuf);
26976 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
26977 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
26978 + 16 * 2 + 1);
26979 memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
26980 tail = grp_name + 4;
26981 if (encoded_filename_len)
26983 for (i = 0; base[i]; i++)
26984 if (ISIDNUM (base[i]) || base[i] == '.')
26985 *tail++ = base[i];
26986 *tail++ = '.';
26988 memcpy (tail, linebuf, linebuf_len);
26989 tail += linebuf_len;
26990 *tail++ = '.';
26991 for (i = 0; i < 16; i++)
26992 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
26994 /* Construct a macinfo_entry for DW_MACRO_import
26995 in the empty vector entry before the first define/undef. */
26996 inc = &(*macinfo_table)[idx - 1];
26997 inc->code = DW_MACRO_import;
26998 inc->lineno = 0;
26999 inc->info = ggc_strdup (grp_name);
27000 if (!*macinfo_htab)
27001 *macinfo_htab = new macinfo_hash_type (10);
27002 /* Avoid emitting duplicates. */
27003 slot = (*macinfo_htab)->find_slot (inc, INSERT);
27004 if (*slot != NULL)
27006 inc->code = 0;
27007 inc->info = NULL;
27008 /* If such an entry has been used before, just emit
27009 a DW_MACRO_import op. */
27010 inc = *slot;
27011 output_macinfo_op (inc);
27012 /* And clear all macinfo_entry in the range to avoid emitting them
27013 in the second pass. */
27014 for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
27016 cur->code = 0;
27017 cur->info = NULL;
27020 else
27022 *slot = inc;
27023 inc->lineno = (*macinfo_htab)->elements ();
27024 output_macinfo_op (inc);
27026 return count;
27029 /* Save any strings needed by the macinfo table in the debug str
27030 table. All strings must be collected into the table by the time
27031 index_string is called. */
27033 static void
27034 save_macinfo_strings (void)
27036 unsigned len;
27037 unsigned i;
27038 macinfo_entry *ref;
27040 for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
27042 switch (ref->code)
27044 /* Match the logic in output_macinfo_op to decide on
27045 indirect strings. */
27046 case DW_MACINFO_define:
27047 case DW_MACINFO_undef:
27048 len = strlen (ref->info) + 1;
27049 if (!dwarf_strict
27050 && len > DWARF_OFFSET_SIZE
27051 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
27052 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
27053 set_indirect_string (find_AT_string (ref->info));
27054 break;
27055 case DW_MACRO_define_strp:
27056 case DW_MACRO_undef_strp:
27057 set_indirect_string (find_AT_string (ref->info));
27058 break;
27059 default:
27060 break;
27065 /* Output macinfo section(s). */
27067 static void
27068 output_macinfo (void)
27070 unsigned i;
27071 unsigned long length = vec_safe_length (macinfo_table);
27072 macinfo_entry *ref;
27073 vec<macinfo_entry, va_gc> *files = NULL;
27074 macinfo_hash_type *macinfo_htab = NULL;
27076 if (! length)
27077 return;
27079 /* output_macinfo* uses these interchangeably. */
27080 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_define
27081 && (int) DW_MACINFO_undef == (int) DW_MACRO_undef
27082 && (int) DW_MACINFO_start_file == (int) DW_MACRO_start_file
27083 && (int) DW_MACINFO_end_file == (int) DW_MACRO_end_file);
27085 /* For .debug_macro emit the section header. */
27086 if (!dwarf_strict || dwarf_version >= 5)
27088 dw2_asm_output_data (2, dwarf_version >= 5 ? 5 : 4,
27089 "DWARF macro version number");
27090 if (DWARF_OFFSET_SIZE == 8)
27091 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
27092 else
27093 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
27094 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
27095 (!dwarf_split_debug_info ? debug_line_section_label
27096 : debug_skeleton_line_section_label),
27097 debug_line_section, NULL);
27100 /* In the first loop, it emits the primary .debug_macinfo section
27101 and after each emitted op the macinfo_entry is cleared.
27102 If a longer range of define/undef ops can be optimized using
27103 DW_MACRO_import, the DW_MACRO_import op is emitted and kept in
27104 the vector before the first define/undef in the range and the
27105 whole range of define/undef ops is not emitted and kept. */
27106 for (i = 0; macinfo_table->iterate (i, &ref); i++)
27108 switch (ref->code)
27110 case DW_MACINFO_start_file:
27111 vec_safe_push (files, *ref);
27112 break;
27113 case DW_MACINFO_end_file:
27114 if (!vec_safe_is_empty (files))
27115 files->pop ();
27116 break;
27117 case DW_MACINFO_define:
27118 case DW_MACINFO_undef:
27119 if ((!dwarf_strict || dwarf_version >= 5)
27120 && HAVE_COMDAT_GROUP
27121 && vec_safe_length (files) != 1
27122 && i > 0
27123 && i + 1 < length
27124 && (*macinfo_table)[i - 1].code == 0)
27126 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
27127 if (count)
27129 i += count - 1;
27130 continue;
27133 break;
27134 case 0:
27135 /* A dummy entry may be inserted at the beginning to be able
27136 to optimize the whole block of predefined macros. */
27137 if (i == 0)
27138 continue;
27139 default:
27140 break;
27142 output_macinfo_op (ref);
27143 ref->info = NULL;
27144 ref->code = 0;
27147 if (!macinfo_htab)
27148 return;
27150 delete macinfo_htab;
27151 macinfo_htab = NULL;
27153 /* If any DW_MACRO_import were used, on those DW_MACRO_import entries
27154 terminate the current chain and switch to a new comdat .debug_macinfo
27155 section and emit the define/undef entries within it. */
27156 for (i = 0; macinfo_table->iterate (i, &ref); i++)
27157 switch (ref->code)
27159 case 0:
27160 continue;
27161 case DW_MACRO_import:
27163 char label[MAX_ARTIFICIAL_LABEL_BYTES];
27164 tree comdat_key = get_identifier (ref->info);
27165 /* Terminate the previous .debug_macinfo section. */
27166 dw2_asm_output_data (1, 0, "End compilation unit");
27167 targetm.asm_out.named_section (debug_macinfo_section_name,
27168 SECTION_DEBUG
27169 | SECTION_LINKONCE,
27170 comdat_key);
27171 ASM_GENERATE_INTERNAL_LABEL (label,
27172 DEBUG_MACRO_SECTION_LABEL,
27173 ref->lineno);
27174 ASM_OUTPUT_LABEL (asm_out_file, label);
27175 ref->code = 0;
27176 ref->info = NULL;
27177 dw2_asm_output_data (2, dwarf_version >= 5 ? 5 : 4,
27178 "DWARF macro version number");
27179 if (DWARF_OFFSET_SIZE == 8)
27180 dw2_asm_output_data (1, 1, "Flags: 64-bit");
27181 else
27182 dw2_asm_output_data (1, 0, "Flags: 32-bit");
27184 break;
27185 case DW_MACINFO_define:
27186 case DW_MACINFO_undef:
27187 output_macinfo_op (ref);
27188 ref->code = 0;
27189 ref->info = NULL;
27190 break;
27191 default:
27192 gcc_unreachable ();
27196 /* Initialize the various sections and labels for dwarf output. */
27198 static void
27199 init_sections_and_labels (void)
27201 if (!dwarf_split_debug_info)
27203 debug_info_section = get_section (DEBUG_INFO_SECTION,
27204 SECTION_DEBUG, NULL);
27205 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
27206 SECTION_DEBUG, NULL);
27207 debug_loc_section = get_section (dwarf_version >= 5
27208 ? DEBUG_LOCLISTS_SECTION
27209 : DEBUG_LOC_SECTION,
27210 SECTION_DEBUG, NULL);
27211 debug_macinfo_section_name
27212 = (dwarf_strict && dwarf_version < 5)
27213 ? DEBUG_MACINFO_SECTION : DEBUG_MACRO_SECTION;
27214 debug_macinfo_section = get_section (debug_macinfo_section_name,
27215 SECTION_DEBUG, NULL);
27217 else
27219 debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
27220 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
27221 debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
27222 SECTION_DEBUG | SECTION_EXCLUDE,
27223 NULL);
27224 debug_addr_section = get_section (DEBUG_ADDR_SECTION,
27225 SECTION_DEBUG, NULL);
27226 debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
27227 SECTION_DEBUG, NULL);
27228 debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
27229 SECTION_DEBUG, NULL);
27230 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
27231 DEBUG_SKELETON_ABBREV_SECTION_LABEL, 0);
27233 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections stay in
27234 the main .o, but the skeleton_line goes into the split off dwo. */
27235 debug_skeleton_line_section
27236 = get_section (DEBUG_DWO_LINE_SECTION,
27237 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
27238 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
27239 DEBUG_SKELETON_LINE_SECTION_LABEL, 0);
27240 debug_str_offsets_section = get_section (DEBUG_DWO_STR_OFFSETS_SECTION,
27241 SECTION_DEBUG | SECTION_EXCLUDE,
27242 NULL);
27243 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
27244 DEBUG_SKELETON_INFO_SECTION_LABEL, 0);
27245 debug_loc_section = get_section (dwarf_version >= 5
27246 ? DEBUG_DWO_LOCLISTS_SECTION
27247 : DEBUG_DWO_LOC_SECTION,
27248 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
27249 debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
27250 DEBUG_STR_DWO_SECTION_FLAGS, NULL);
27251 debug_macinfo_section_name
27252 = (dwarf_strict && dwarf_version < 5)
27253 ? DEBUG_DWO_MACINFO_SECTION : DEBUG_DWO_MACRO_SECTION;
27254 debug_macinfo_section = get_section (debug_macinfo_section_name,
27255 SECTION_DEBUG | SECTION_EXCLUDE,
27256 NULL);
27258 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
27259 SECTION_DEBUG, NULL);
27260 debug_line_section = get_section (DEBUG_LINE_SECTION,
27261 SECTION_DEBUG, NULL);
27262 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
27263 SECTION_DEBUG, NULL);
27264 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
27265 SECTION_DEBUG, NULL);
27266 debug_str_section = get_section (DEBUG_STR_SECTION,
27267 DEBUG_STR_SECTION_FLAGS, NULL);
27268 if (!dwarf_split_debug_info && !DWARF2_ASM_LINE_DEBUG_INFO)
27269 debug_line_str_section = get_section (DEBUG_LINE_STR_SECTION,
27270 DEBUG_STR_SECTION_FLAGS, NULL);
27272 debug_ranges_section = get_section (dwarf_version >= 5
27273 ? DEBUG_RNGLISTS_SECTION
27274 : DEBUG_RANGES_SECTION,
27275 SECTION_DEBUG, NULL);
27276 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
27277 SECTION_DEBUG, NULL);
27279 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
27280 DEBUG_ABBREV_SECTION_LABEL, 0);
27281 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
27282 DEBUG_INFO_SECTION_LABEL, 0);
27283 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
27284 DEBUG_LINE_SECTION_LABEL, 0);
27285 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
27286 DEBUG_RANGES_SECTION_LABEL, 0);
27287 if (dwarf_version >= 5 && dwarf_split_debug_info)
27288 ASM_GENERATE_INTERNAL_LABEL (ranges_base_label,
27289 DEBUG_RANGES_SECTION_LABEL, 1);
27290 ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
27291 DEBUG_ADDR_SECTION_LABEL, 0);
27292 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
27293 (dwarf_strict && dwarf_version < 5)
27294 ? DEBUG_MACINFO_SECTION_LABEL
27295 : DEBUG_MACRO_SECTION_LABEL, 0);
27296 ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL, 0);
27299 /* Set up for Dwarf output at the start of compilation. */
27301 static void
27302 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
27304 /* This option is currently broken, see (PR53118 and PR46102). */
27305 if (flag_eliminate_dwarf2_dups
27306 && strstr (lang_hooks.name, "C++"))
27308 warning (0, "-feliminate-dwarf2-dups is broken for C++, ignoring");
27309 flag_eliminate_dwarf2_dups = 0;
27312 /* Allocate the file_table. */
27313 file_table = hash_table<dwarf_file_hasher>::create_ggc (50);
27315 #ifndef DWARF2_LINENO_DEBUGGING_INFO
27316 /* Allocate the decl_die_table. */
27317 decl_die_table = hash_table<decl_die_hasher>::create_ggc (10);
27319 /* Allocate the decl_loc_table. */
27320 decl_loc_table = hash_table<decl_loc_hasher>::create_ggc (10);
27322 /* Allocate the cached_dw_loc_list_table. */
27323 cached_dw_loc_list_table = hash_table<dw_loc_list_hasher>::create_ggc (10);
27325 /* Allocate the initial hunk of the decl_scope_table. */
27326 vec_alloc (decl_scope_table, 256);
27328 /* Allocate the initial hunk of the abbrev_die_table. */
27329 vec_alloc (abbrev_die_table, 256);
27330 /* Zero-th entry is allocated, but unused. */
27331 abbrev_die_table->quick_push (NULL);
27333 /* Allocate the dwarf_proc_stack_usage_map. */
27334 dwarf_proc_stack_usage_map = new hash_map<dw_die_ref, int>;
27336 /* Allocate the pubtypes and pubnames vectors. */
27337 vec_alloc (pubname_table, 32);
27338 vec_alloc (pubtype_table, 32);
27340 vec_alloc (incomplete_types, 64);
27342 vec_alloc (used_rtx_array, 32);
27344 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
27345 vec_alloc (macinfo_table, 64);
27346 #endif
27348 /* If front-ends already registered a main translation unit but we were not
27349 ready to perform the association, do this now. */
27350 if (main_translation_unit != NULL_TREE)
27351 equate_decl_number_to_die (main_translation_unit, comp_unit_die ());
27354 /* Called before compile () starts outputtting functions, variables
27355 and toplevel asms into assembly. */
27357 static void
27358 dwarf2out_assembly_start (void)
27360 #ifndef DWARF2_LINENO_DEBUGGING_INFO
27361 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
27362 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
27363 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
27364 COLD_TEXT_SECTION_LABEL, 0);
27365 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
27367 switch_to_section (text_section);
27368 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
27369 #endif
27371 /* Make sure the line number table for .text always exists. */
27372 text_section_line_info = new_line_info_table ();
27373 text_section_line_info->end_label = text_end_label;
27375 #ifdef DWARF2_LINENO_DEBUGGING_INFO
27376 cur_line_info_table = text_section_line_info;
27377 #endif
27379 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
27380 && dwarf2out_do_cfi_asm ()
27381 && (!(flag_unwind_tables || flag_exceptions)
27382 || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
27383 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
27386 /* A helper function for dwarf2out_finish called through
27387 htab_traverse. Assign a string its index. All strings must be
27388 collected into the table by the time index_string is called,
27389 because the indexing code relies on htab_traverse to traverse nodes
27390 in the same order for each run. */
27393 index_string (indirect_string_node **h, unsigned int *index)
27395 indirect_string_node *node = *h;
27397 find_string_form (node);
27398 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
27400 gcc_assert (node->index == NO_INDEX_ASSIGNED);
27401 node->index = *index;
27402 *index += 1;
27404 return 1;
27407 /* A helper function for output_indirect_strings called through
27408 htab_traverse. Output the offset to a string and update the
27409 current offset. */
27412 output_index_string_offset (indirect_string_node **h, unsigned int *offset)
27414 indirect_string_node *node = *h;
27416 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
27418 /* Assert that this node has been assigned an index. */
27419 gcc_assert (node->index != NO_INDEX_ASSIGNED
27420 && node->index != NOT_INDEXED);
27421 dw2_asm_output_data (DWARF_OFFSET_SIZE, *offset,
27422 "indexed string 0x%x: %s", node->index, node->str);
27423 *offset += strlen (node->str) + 1;
27425 return 1;
27428 /* A helper function for dwarf2out_finish called through
27429 htab_traverse. Output the indexed string. */
27432 output_index_string (indirect_string_node **h, unsigned int *cur_idx)
27434 struct indirect_string_node *node = *h;
27436 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
27438 /* Assert that the strings are output in the same order as their
27439 indexes were assigned. */
27440 gcc_assert (*cur_idx == node->index);
27441 assemble_string (node->str, strlen (node->str) + 1);
27442 *cur_idx += 1;
27444 return 1;
27447 /* A helper function for dwarf2out_finish called through
27448 htab_traverse. Emit one queued .debug_str string. */
27451 output_indirect_string (indirect_string_node **h, enum dwarf_form form)
27453 struct indirect_string_node *node = *h;
27455 node->form = find_string_form (node);
27456 if (node->form == form && node->refcount > 0)
27458 ASM_OUTPUT_LABEL (asm_out_file, node->label);
27459 assemble_string (node->str, strlen (node->str) + 1);
27462 return 1;
27465 /* Output the indexed string table. */
27467 static void
27468 output_indirect_strings (void)
27470 switch_to_section (debug_str_section);
27471 if (!dwarf_split_debug_info)
27472 debug_str_hash->traverse<enum dwarf_form,
27473 output_indirect_string> (DW_FORM_strp);
27474 else
27476 unsigned int offset = 0;
27477 unsigned int cur_idx = 0;
27479 skeleton_debug_str_hash->traverse<enum dwarf_form,
27480 output_indirect_string> (DW_FORM_strp);
27482 switch_to_section (debug_str_offsets_section);
27483 debug_str_hash->traverse_noresize
27484 <unsigned int *, output_index_string_offset> (&offset);
27485 switch_to_section (debug_str_dwo_section);
27486 debug_str_hash->traverse_noresize<unsigned int *, output_index_string>
27487 (&cur_idx);
27491 /* Callback for htab_traverse to assign an index to an entry in the
27492 table, and to write that entry to the .debug_addr section. */
27495 output_addr_table_entry (addr_table_entry **slot, unsigned int *cur_index)
27497 addr_table_entry *entry = *slot;
27499 if (entry->refcount == 0)
27501 gcc_assert (entry->index == NO_INDEX_ASSIGNED
27502 || entry->index == NOT_INDEXED);
27503 return 1;
27506 gcc_assert (entry->index == *cur_index);
27507 (*cur_index)++;
27509 switch (entry->kind)
27511 case ate_kind_rtx:
27512 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
27513 "0x%x", entry->index);
27514 break;
27515 case ate_kind_rtx_dtprel:
27516 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
27517 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
27518 DWARF2_ADDR_SIZE,
27519 entry->addr.rtl);
27520 fputc ('\n', asm_out_file);
27521 break;
27522 case ate_kind_label:
27523 dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
27524 "0x%x", entry->index);
27525 break;
27526 default:
27527 gcc_unreachable ();
27529 return 1;
27532 /* Produce the .debug_addr section. */
27534 static void
27535 output_addr_table (void)
27537 unsigned int index = 0;
27538 if (addr_index_table == NULL || addr_index_table->size () == 0)
27539 return;
27541 switch_to_section (debug_addr_section);
27542 addr_index_table
27543 ->traverse_noresize<unsigned int *, output_addr_table_entry> (&index);
27546 #if ENABLE_ASSERT_CHECKING
27547 /* Verify that all marks are clear. */
27549 static void
27550 verify_marks_clear (dw_die_ref die)
27552 dw_die_ref c;
27554 gcc_assert (! die->die_mark);
27555 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
27557 #endif /* ENABLE_ASSERT_CHECKING */
27559 /* Clear the marks for a die and its children.
27560 Be cool if the mark isn't set. */
27562 static void
27563 prune_unmark_dies (dw_die_ref die)
27565 dw_die_ref c;
27567 if (die->die_mark)
27568 die->die_mark = 0;
27569 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
27572 /* Given LOC that is referenced by a DIE we're marking as used, find all
27573 referenced DWARF procedures it references and mark them as used. */
27575 static void
27576 prune_unused_types_walk_loc_descr (dw_loc_descr_ref loc)
27578 for (; loc != NULL; loc = loc->dw_loc_next)
27579 switch (loc->dw_loc_opc)
27581 case DW_OP_implicit_pointer:
27582 case DW_OP_convert:
27583 case DW_OP_reinterpret:
27584 case DW_OP_GNU_implicit_pointer:
27585 case DW_OP_GNU_convert:
27586 case DW_OP_GNU_reinterpret:
27587 if (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref)
27588 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
27589 break;
27590 case DW_OP_GNU_variable_value:
27591 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
27593 dw_die_ref ref
27594 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
27595 if (ref == NULL)
27596 break;
27597 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
27598 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
27599 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
27601 /* FALLTHRU */
27602 case DW_OP_call2:
27603 case DW_OP_call4:
27604 case DW_OP_call_ref:
27605 case DW_OP_const_type:
27606 case DW_OP_GNU_const_type:
27607 case DW_OP_GNU_parameter_ref:
27608 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref);
27609 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
27610 break;
27611 case DW_OP_regval_type:
27612 case DW_OP_deref_type:
27613 case DW_OP_GNU_regval_type:
27614 case DW_OP_GNU_deref_type:
27615 gcc_assert (loc->dw_loc_oprnd2.val_class == dw_val_class_die_ref);
27616 prune_unused_types_mark (loc->dw_loc_oprnd2.v.val_die_ref.die, 1);
27617 break;
27618 case DW_OP_entry_value:
27619 case DW_OP_GNU_entry_value:
27620 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_loc);
27621 prune_unused_types_walk_loc_descr (loc->dw_loc_oprnd1.v.val_loc);
27622 break;
27623 default:
27624 break;
27628 /* Given DIE that we're marking as used, find any other dies
27629 it references as attributes and mark them as used. */
27631 static void
27632 prune_unused_types_walk_attribs (dw_die_ref die)
27634 dw_attr_node *a;
27635 unsigned ix;
27637 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
27639 switch (AT_class (a))
27641 /* Make sure DWARF procedures referenced by location descriptions will
27642 get emitted. */
27643 case dw_val_class_loc:
27644 prune_unused_types_walk_loc_descr (AT_loc (a));
27645 break;
27646 case dw_val_class_loc_list:
27647 for (dw_loc_list_ref list = AT_loc_list (a);
27648 list != NULL;
27649 list = list->dw_loc_next)
27650 prune_unused_types_walk_loc_descr (list->expr);
27651 break;
27653 case dw_val_class_die_ref:
27654 /* A reference to another DIE.
27655 Make sure that it will get emitted.
27656 If it was broken out into a comdat group, don't follow it. */
27657 if (! AT_ref (a)->comdat_type_p
27658 || a->dw_attr == DW_AT_specification)
27659 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
27660 break;
27662 case dw_val_class_str:
27663 /* Set the string's refcount to 0 so that prune_unused_types_mark
27664 accounts properly for it. */
27665 a->dw_attr_val.v.val_str->refcount = 0;
27666 break;
27668 default:
27669 break;
27674 /* Mark the generic parameters and arguments children DIEs of DIE. */
27676 static void
27677 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
27679 dw_die_ref c;
27681 if (die == NULL || die->die_child == NULL)
27682 return;
27683 c = die->die_child;
27686 if (is_template_parameter (c))
27687 prune_unused_types_mark (c, 1);
27688 c = c->die_sib;
27689 } while (c && c != die->die_child);
27692 /* Mark DIE as being used. If DOKIDS is true, then walk down
27693 to DIE's children. */
27695 static void
27696 prune_unused_types_mark (dw_die_ref die, int dokids)
27698 dw_die_ref c;
27700 if (die->die_mark == 0)
27702 /* We haven't done this node yet. Mark it as used. */
27703 die->die_mark = 1;
27704 /* If this is the DIE of a generic type instantiation,
27705 mark the children DIEs that describe its generic parms and
27706 args. */
27707 prune_unused_types_mark_generic_parms_dies (die);
27709 /* We also have to mark its parents as used.
27710 (But we don't want to mark our parent's kids due to this,
27711 unless it is a class.) */
27712 if (die->die_parent)
27713 prune_unused_types_mark (die->die_parent,
27714 class_scope_p (die->die_parent));
27716 /* Mark any referenced nodes. */
27717 prune_unused_types_walk_attribs (die);
27719 /* If this node is a specification,
27720 also mark the definition, if it exists. */
27721 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
27722 prune_unused_types_mark (die->die_definition, 1);
27725 if (dokids && die->die_mark != 2)
27727 /* We need to walk the children, but haven't done so yet.
27728 Remember that we've walked the kids. */
27729 die->die_mark = 2;
27731 /* If this is an array type, we need to make sure our
27732 kids get marked, even if they're types. If we're
27733 breaking out types into comdat sections, do this
27734 for all type definitions. */
27735 if (die->die_tag == DW_TAG_array_type
27736 || (use_debug_types
27737 && is_type_die (die) && ! is_declaration_die (die)))
27738 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
27739 else
27740 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
27744 /* For local classes, look if any static member functions were emitted
27745 and if so, mark them. */
27747 static void
27748 prune_unused_types_walk_local_classes (dw_die_ref die)
27750 dw_die_ref c;
27752 if (die->die_mark == 2)
27753 return;
27755 switch (die->die_tag)
27757 case DW_TAG_structure_type:
27758 case DW_TAG_union_type:
27759 case DW_TAG_class_type:
27760 break;
27762 case DW_TAG_subprogram:
27763 if (!get_AT_flag (die, DW_AT_declaration)
27764 || die->die_definition != NULL)
27765 prune_unused_types_mark (die, 1);
27766 return;
27768 default:
27769 return;
27772 /* Mark children. */
27773 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
27776 /* Walk the tree DIE and mark types that we actually use. */
27778 static void
27779 prune_unused_types_walk (dw_die_ref die)
27781 dw_die_ref c;
27783 /* Don't do anything if this node is already marked and
27784 children have been marked as well. */
27785 if (die->die_mark == 2)
27786 return;
27788 switch (die->die_tag)
27790 case DW_TAG_structure_type:
27791 case DW_TAG_union_type:
27792 case DW_TAG_class_type:
27793 if (die->die_perennial_p)
27794 break;
27796 for (c = die->die_parent; c; c = c->die_parent)
27797 if (c->die_tag == DW_TAG_subprogram)
27798 break;
27800 /* Finding used static member functions inside of classes
27801 is needed just for local classes, because for other classes
27802 static member function DIEs with DW_AT_specification
27803 are emitted outside of the DW_TAG_*_type. If we ever change
27804 it, we'd need to call this even for non-local classes. */
27805 if (c)
27806 prune_unused_types_walk_local_classes (die);
27808 /* It's a type node --- don't mark it. */
27809 return;
27811 case DW_TAG_const_type:
27812 case DW_TAG_packed_type:
27813 case DW_TAG_pointer_type:
27814 case DW_TAG_reference_type:
27815 case DW_TAG_rvalue_reference_type:
27816 case DW_TAG_volatile_type:
27817 case DW_TAG_typedef:
27818 case DW_TAG_array_type:
27819 case DW_TAG_interface_type:
27820 case DW_TAG_friend:
27821 case DW_TAG_enumeration_type:
27822 case DW_TAG_subroutine_type:
27823 case DW_TAG_string_type:
27824 case DW_TAG_set_type:
27825 case DW_TAG_subrange_type:
27826 case DW_TAG_ptr_to_member_type:
27827 case DW_TAG_file_type:
27828 /* Type nodes are useful only when other DIEs reference them --- don't
27829 mark them. */
27830 /* FALLTHROUGH */
27832 case DW_TAG_dwarf_procedure:
27833 /* Likewise for DWARF procedures. */
27835 if (die->die_perennial_p)
27836 break;
27838 return;
27840 default:
27841 /* Mark everything else. */
27842 break;
27845 if (die->die_mark == 0)
27847 die->die_mark = 1;
27849 /* Now, mark any dies referenced from here. */
27850 prune_unused_types_walk_attribs (die);
27853 die->die_mark = 2;
27855 /* Mark children. */
27856 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
27859 /* Increment the string counts on strings referred to from DIE's
27860 attributes. */
27862 static void
27863 prune_unused_types_update_strings (dw_die_ref die)
27865 dw_attr_node *a;
27866 unsigned ix;
27868 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
27869 if (AT_class (a) == dw_val_class_str)
27871 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
27872 s->refcount++;
27873 /* Avoid unnecessarily putting strings that are used less than
27874 twice in the hash table. */
27875 if (s->refcount
27876 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
27878 indirect_string_node **slot
27879 = debug_str_hash->find_slot_with_hash (s->str,
27880 htab_hash_string (s->str),
27881 INSERT);
27882 gcc_assert (*slot == NULL);
27883 *slot = s;
27888 /* Mark DIE and its children as removed. */
27890 static void
27891 mark_removed (dw_die_ref die)
27893 dw_die_ref c;
27894 die->removed = true;
27895 FOR_EACH_CHILD (die, c, mark_removed (c));
27898 /* Remove from the tree DIE any dies that aren't marked. */
27900 static void
27901 prune_unused_types_prune (dw_die_ref die)
27903 dw_die_ref c;
27905 gcc_assert (die->die_mark);
27906 prune_unused_types_update_strings (die);
27908 if (! die->die_child)
27909 return;
27911 c = die->die_child;
27912 do {
27913 dw_die_ref prev = c, next;
27914 for (c = c->die_sib; ! c->die_mark; c = next)
27915 if (c == die->die_child)
27917 /* No marked children between 'prev' and the end of the list. */
27918 if (prev == c)
27919 /* No marked children at all. */
27920 die->die_child = NULL;
27921 else
27923 prev->die_sib = c->die_sib;
27924 die->die_child = prev;
27926 c->die_sib = NULL;
27927 mark_removed (c);
27928 return;
27930 else
27932 next = c->die_sib;
27933 c->die_sib = NULL;
27934 mark_removed (c);
27937 if (c != prev->die_sib)
27938 prev->die_sib = c;
27939 prune_unused_types_prune (c);
27940 } while (c != die->die_child);
27943 /* Remove dies representing declarations that we never use. */
27945 static void
27946 prune_unused_types (void)
27948 unsigned int i;
27949 limbo_die_node *node;
27950 comdat_type_node *ctnode;
27951 pubname_entry *pub;
27952 dw_die_ref base_type;
27954 #if ENABLE_ASSERT_CHECKING
27955 /* All the marks should already be clear. */
27956 verify_marks_clear (comp_unit_die ());
27957 for (node = limbo_die_list; node; node = node->next)
27958 verify_marks_clear (node->die);
27959 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
27960 verify_marks_clear (ctnode->root_die);
27961 #endif /* ENABLE_ASSERT_CHECKING */
27963 /* Mark types that are used in global variables. */
27964 premark_types_used_by_global_vars ();
27966 /* Set the mark on nodes that are actually used. */
27967 prune_unused_types_walk (comp_unit_die ());
27968 for (node = limbo_die_list; node; node = node->next)
27969 prune_unused_types_walk (node->die);
27970 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
27972 prune_unused_types_walk (ctnode->root_die);
27973 prune_unused_types_mark (ctnode->type_die, 1);
27976 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
27977 are unusual in that they are pubnames that are the children of pubtypes.
27978 They should only be marked via their parent DW_TAG_enumeration_type die,
27979 not as roots in themselves. */
27980 FOR_EACH_VEC_ELT (*pubname_table, i, pub)
27981 if (pub->die->die_tag != DW_TAG_enumerator)
27982 prune_unused_types_mark (pub->die, 1);
27983 for (i = 0; base_types.iterate (i, &base_type); i++)
27984 prune_unused_types_mark (base_type, 1);
27986 /* For -fvar-tracking-assignments, also set the mark on nodes that could be
27987 referenced by DW_TAG_call_site DW_AT_call_origin (i.e. direct call
27988 callees). */
27989 cgraph_node *cnode;
27990 FOR_EACH_FUNCTION (cnode)
27991 if (cnode->referred_to_p (false))
27993 dw_die_ref die = lookup_decl_die (cnode->decl);
27994 if (die == NULL || die->die_mark)
27995 continue;
27996 for (cgraph_edge *e = cnode->callers; e; e = e->next_caller)
27997 if (e->caller != cnode
27998 && opt_for_fn (e->caller->decl, flag_var_tracking_assignments))
28000 prune_unused_types_mark (die, 1);
28001 break;
28005 if (debug_str_hash)
28006 debug_str_hash->empty ();
28007 if (skeleton_debug_str_hash)
28008 skeleton_debug_str_hash->empty ();
28009 prune_unused_types_prune (comp_unit_die ());
28010 for (limbo_die_node **pnode = &limbo_die_list; *pnode; )
28012 node = *pnode;
28013 if (!node->die->die_mark)
28014 *pnode = node->next;
28015 else
28017 prune_unused_types_prune (node->die);
28018 pnode = &node->next;
28021 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
28022 prune_unused_types_prune (ctnode->root_die);
28024 /* Leave the marks clear. */
28025 prune_unmark_dies (comp_unit_die ());
28026 for (node = limbo_die_list; node; node = node->next)
28027 prune_unmark_dies (node->die);
28028 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
28029 prune_unmark_dies (ctnode->root_die);
28032 /* Helpers to manipulate hash table of comdat type units. */
28034 struct comdat_type_hasher : nofree_ptr_hash <comdat_type_node>
28036 static inline hashval_t hash (const comdat_type_node *);
28037 static inline bool equal (const comdat_type_node *, const comdat_type_node *);
28040 inline hashval_t
28041 comdat_type_hasher::hash (const comdat_type_node *type_node)
28043 hashval_t h;
28044 memcpy (&h, type_node->signature, sizeof (h));
28045 return h;
28048 inline bool
28049 comdat_type_hasher::equal (const comdat_type_node *type_node_1,
28050 const comdat_type_node *type_node_2)
28052 return (! memcmp (type_node_1->signature, type_node_2->signature,
28053 DWARF_TYPE_SIGNATURE_SIZE));
28056 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
28057 to the location it would have been added, should we know its
28058 DECL_ASSEMBLER_NAME when we added other attributes. This will
28059 probably improve compactness of debug info, removing equivalent
28060 abbrevs, and hide any differences caused by deferring the
28061 computation of the assembler name, triggered by e.g. PCH. */
28063 static inline void
28064 move_linkage_attr (dw_die_ref die)
28066 unsigned ix = vec_safe_length (die->die_attr);
28067 dw_attr_node linkage = (*die->die_attr)[ix - 1];
28069 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
28070 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
28072 while (--ix > 0)
28074 dw_attr_node *prev = &(*die->die_attr)[ix - 1];
28076 if (prev->dw_attr == DW_AT_decl_line
28077 || prev->dw_attr == DW_AT_decl_column
28078 || prev->dw_attr == DW_AT_name)
28079 break;
28082 if (ix != vec_safe_length (die->die_attr) - 1)
28084 die->die_attr->pop ();
28085 die->die_attr->quick_insert (ix, linkage);
28089 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
28090 referenced from typed stack ops and count how often they are used. */
28092 static void
28093 mark_base_types (dw_loc_descr_ref loc)
28095 dw_die_ref base_type = NULL;
28097 for (; loc; loc = loc->dw_loc_next)
28099 switch (loc->dw_loc_opc)
28101 case DW_OP_regval_type:
28102 case DW_OP_deref_type:
28103 case DW_OP_GNU_regval_type:
28104 case DW_OP_GNU_deref_type:
28105 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
28106 break;
28107 case DW_OP_convert:
28108 case DW_OP_reinterpret:
28109 case DW_OP_GNU_convert:
28110 case DW_OP_GNU_reinterpret:
28111 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
28112 continue;
28113 /* FALLTHRU */
28114 case DW_OP_const_type:
28115 case DW_OP_GNU_const_type:
28116 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
28117 break;
28118 case DW_OP_entry_value:
28119 case DW_OP_GNU_entry_value:
28120 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
28121 continue;
28122 default:
28123 continue;
28125 gcc_assert (base_type->die_parent == comp_unit_die ());
28126 if (base_type->die_mark)
28127 base_type->die_mark++;
28128 else
28130 base_types.safe_push (base_type);
28131 base_type->die_mark = 1;
28136 /* Comparison function for sorting marked base types. */
28138 static int
28139 base_type_cmp (const void *x, const void *y)
28141 dw_die_ref dx = *(const dw_die_ref *) x;
28142 dw_die_ref dy = *(const dw_die_ref *) y;
28143 unsigned int byte_size1, byte_size2;
28144 unsigned int encoding1, encoding2;
28145 unsigned int align1, align2;
28146 if (dx->die_mark > dy->die_mark)
28147 return -1;
28148 if (dx->die_mark < dy->die_mark)
28149 return 1;
28150 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
28151 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
28152 if (byte_size1 < byte_size2)
28153 return 1;
28154 if (byte_size1 > byte_size2)
28155 return -1;
28156 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
28157 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
28158 if (encoding1 < encoding2)
28159 return 1;
28160 if (encoding1 > encoding2)
28161 return -1;
28162 align1 = get_AT_unsigned (dx, DW_AT_alignment);
28163 align2 = get_AT_unsigned (dy, DW_AT_alignment);
28164 if (align1 < align2)
28165 return 1;
28166 if (align1 > align2)
28167 return -1;
28168 return 0;
28171 /* Move base types marked by mark_base_types as early as possible
28172 in the CU, sorted by decreasing usage count both to make the
28173 uleb128 references as small as possible and to make sure they
28174 will have die_offset already computed by calc_die_sizes when
28175 sizes of typed stack loc ops is computed. */
28177 static void
28178 move_marked_base_types (void)
28180 unsigned int i;
28181 dw_die_ref base_type, die, c;
28183 if (base_types.is_empty ())
28184 return;
28186 /* Sort by decreasing usage count, they will be added again in that
28187 order later on. */
28188 base_types.qsort (base_type_cmp);
28189 die = comp_unit_die ();
28190 c = die->die_child;
28193 dw_die_ref prev = c;
28194 c = c->die_sib;
28195 while (c->die_mark)
28197 remove_child_with_prev (c, prev);
28198 /* As base types got marked, there must be at least
28199 one node other than DW_TAG_base_type. */
28200 gcc_assert (die->die_child != NULL);
28201 c = prev->die_sib;
28204 while (c != die->die_child);
28205 gcc_assert (die->die_child);
28206 c = die->die_child;
28207 for (i = 0; base_types.iterate (i, &base_type); i++)
28209 base_type->die_mark = 0;
28210 base_type->die_sib = c->die_sib;
28211 c->die_sib = base_type;
28212 c = base_type;
28216 /* Helper function for resolve_addr, attempt to resolve
28217 one CONST_STRING, return true if successful. Similarly verify that
28218 SYMBOL_REFs refer to variables emitted in the current CU. */
28220 static bool
28221 resolve_one_addr (rtx *addr)
28223 rtx rtl = *addr;
28225 if (GET_CODE (rtl) == CONST_STRING)
28227 size_t len = strlen (XSTR (rtl, 0)) + 1;
28228 tree t = build_string (len, XSTR (rtl, 0));
28229 tree tlen = size_int (len - 1);
28230 TREE_TYPE (t)
28231 = build_array_type (char_type_node, build_index_type (tlen));
28232 rtl = lookup_constant_def (t);
28233 if (!rtl || !MEM_P (rtl))
28234 return false;
28235 rtl = XEXP (rtl, 0);
28236 if (GET_CODE (rtl) == SYMBOL_REF
28237 && SYMBOL_REF_DECL (rtl)
28238 && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
28239 return false;
28240 vec_safe_push (used_rtx_array, rtl);
28241 *addr = rtl;
28242 return true;
28245 if (GET_CODE (rtl) == SYMBOL_REF
28246 && SYMBOL_REF_DECL (rtl))
28248 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
28250 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
28251 return false;
28253 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
28254 return false;
28257 if (GET_CODE (rtl) == CONST)
28259 subrtx_ptr_iterator::array_type array;
28260 FOR_EACH_SUBRTX_PTR (iter, array, &XEXP (rtl, 0), ALL)
28261 if (!resolve_one_addr (*iter))
28262 return false;
28265 return true;
28268 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
28269 if possible, and create DW_TAG_dwarf_procedure that can be referenced
28270 from DW_OP_implicit_pointer if the string hasn't been seen yet. */
28272 static rtx
28273 string_cst_pool_decl (tree t)
28275 rtx rtl = output_constant_def (t, 1);
28276 unsigned char *array;
28277 dw_loc_descr_ref l;
28278 tree decl;
28279 size_t len;
28280 dw_die_ref ref;
28282 if (!rtl || !MEM_P (rtl))
28283 return NULL_RTX;
28284 rtl = XEXP (rtl, 0);
28285 if (GET_CODE (rtl) != SYMBOL_REF
28286 || SYMBOL_REF_DECL (rtl) == NULL_TREE)
28287 return NULL_RTX;
28289 decl = SYMBOL_REF_DECL (rtl);
28290 if (!lookup_decl_die (decl))
28292 len = TREE_STRING_LENGTH (t);
28293 vec_safe_push (used_rtx_array, rtl);
28294 ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
28295 array = ggc_vec_alloc<unsigned char> (len);
28296 memcpy (array, TREE_STRING_POINTER (t), len);
28297 l = new_loc_descr (DW_OP_implicit_value, len, 0);
28298 l->dw_loc_oprnd2.val_class = dw_val_class_vec;
28299 l->dw_loc_oprnd2.v.val_vec.length = len;
28300 l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
28301 l->dw_loc_oprnd2.v.val_vec.array = array;
28302 add_AT_loc (ref, DW_AT_location, l);
28303 equate_decl_number_to_die (decl, ref);
28305 return rtl;
28308 /* Helper function of resolve_addr_in_expr. LOC is
28309 a DW_OP_addr followed by DW_OP_stack_value, either at the start
28310 of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
28311 resolved. Replace it (both DW_OP_addr and DW_OP_stack_value)
28312 with DW_OP_implicit_pointer if possible
28313 and return true, if unsuccessful, return false. */
28315 static bool
28316 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
28318 rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
28319 HOST_WIDE_INT offset = 0;
28320 dw_die_ref ref = NULL;
28321 tree decl;
28323 if (GET_CODE (rtl) == CONST
28324 && GET_CODE (XEXP (rtl, 0)) == PLUS
28325 && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
28327 offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
28328 rtl = XEXP (XEXP (rtl, 0), 0);
28330 if (GET_CODE (rtl) == CONST_STRING)
28332 size_t len = strlen (XSTR (rtl, 0)) + 1;
28333 tree t = build_string (len, XSTR (rtl, 0));
28334 tree tlen = size_int (len - 1);
28336 TREE_TYPE (t)
28337 = build_array_type (char_type_node, build_index_type (tlen));
28338 rtl = string_cst_pool_decl (t);
28339 if (!rtl)
28340 return false;
28342 if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
28344 decl = SYMBOL_REF_DECL (rtl);
28345 if (VAR_P (decl) && !DECL_EXTERNAL (decl))
28347 ref = lookup_decl_die (decl);
28348 if (ref && (get_AT (ref, DW_AT_location)
28349 || get_AT (ref, DW_AT_const_value)))
28351 loc->dw_loc_opc = dwarf_OP (DW_OP_implicit_pointer);
28352 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
28353 loc->dw_loc_oprnd1.val_entry = NULL;
28354 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
28355 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
28356 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
28357 loc->dw_loc_oprnd2.v.val_int = offset;
28358 return true;
28362 return false;
28365 /* Helper function for resolve_addr, handle one location
28366 expression, return false if at least one CONST_STRING or SYMBOL_REF in
28367 the location list couldn't be resolved. */
28369 static bool
28370 resolve_addr_in_expr (dw_attr_node *a, dw_loc_descr_ref loc)
28372 dw_loc_descr_ref keep = NULL;
28373 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
28374 switch (loc->dw_loc_opc)
28376 case DW_OP_addr:
28377 if (!resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
28379 if ((prev == NULL
28380 || prev->dw_loc_opc == DW_OP_piece
28381 || prev->dw_loc_opc == DW_OP_bit_piece)
28382 && loc->dw_loc_next
28383 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
28384 && (!dwarf_strict || dwarf_version >= 5)
28385 && optimize_one_addr_into_implicit_ptr (loc))
28386 break;
28387 return false;
28389 break;
28390 case DW_OP_GNU_addr_index:
28391 case DW_OP_GNU_const_index:
28392 if (loc->dw_loc_opc == DW_OP_GNU_addr_index
28393 || (loc->dw_loc_opc == DW_OP_GNU_const_index && loc->dtprel))
28395 rtx rtl = loc->dw_loc_oprnd1.val_entry->addr.rtl;
28396 if (!resolve_one_addr (&rtl))
28397 return false;
28398 remove_addr_table_entry (loc->dw_loc_oprnd1.val_entry);
28399 loc->dw_loc_oprnd1.val_entry
28400 = add_addr_table_entry (rtl, ate_kind_rtx);
28402 break;
28403 case DW_OP_const4u:
28404 case DW_OP_const8u:
28405 if (loc->dtprel
28406 && !resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
28407 return false;
28408 break;
28409 case DW_OP_plus_uconst:
28410 if (size_of_loc_descr (loc)
28411 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
28413 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
28415 dw_loc_descr_ref repl
28416 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
28417 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
28418 add_loc_descr (&repl, loc->dw_loc_next);
28419 *loc = *repl;
28421 break;
28422 case DW_OP_implicit_value:
28423 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
28424 && !resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr))
28425 return false;
28426 break;
28427 case DW_OP_implicit_pointer:
28428 case DW_OP_GNU_implicit_pointer:
28429 case DW_OP_GNU_parameter_ref:
28430 case DW_OP_GNU_variable_value:
28431 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
28433 dw_die_ref ref
28434 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
28435 if (ref == NULL)
28436 return false;
28437 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
28438 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
28439 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
28441 if (loc->dw_loc_opc == DW_OP_GNU_variable_value)
28443 if (prev == NULL
28444 && loc->dw_loc_next == NULL
28445 && AT_class (a) == dw_val_class_loc)
28446 switch (a->dw_attr)
28448 /* Following attributes allow both exprloc and reference,
28449 so if the whole expression is DW_OP_GNU_variable_value
28450 alone we could transform it into reference. */
28451 case DW_AT_byte_size:
28452 case DW_AT_bit_size:
28453 case DW_AT_lower_bound:
28454 case DW_AT_upper_bound:
28455 case DW_AT_bit_stride:
28456 case DW_AT_count:
28457 case DW_AT_allocated:
28458 case DW_AT_associated:
28459 case DW_AT_byte_stride:
28460 a->dw_attr_val.val_class = dw_val_class_die_ref;
28461 a->dw_attr_val.val_entry = NULL;
28462 a->dw_attr_val.v.val_die_ref.die
28463 = loc->dw_loc_oprnd1.v.val_die_ref.die;
28464 a->dw_attr_val.v.val_die_ref.external = 0;
28465 return true;
28466 default:
28467 break;
28469 if (dwarf_strict)
28470 return false;
28472 break;
28473 case DW_OP_const_type:
28474 case DW_OP_regval_type:
28475 case DW_OP_deref_type:
28476 case DW_OP_convert:
28477 case DW_OP_reinterpret:
28478 case DW_OP_GNU_const_type:
28479 case DW_OP_GNU_regval_type:
28480 case DW_OP_GNU_deref_type:
28481 case DW_OP_GNU_convert:
28482 case DW_OP_GNU_reinterpret:
28483 while (loc->dw_loc_next
28484 && (loc->dw_loc_next->dw_loc_opc == DW_OP_convert
28485 || loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert))
28487 dw_die_ref base1, base2;
28488 unsigned enc1, enc2, size1, size2;
28489 if (loc->dw_loc_opc == DW_OP_regval_type
28490 || loc->dw_loc_opc == DW_OP_deref_type
28491 || loc->dw_loc_opc == DW_OP_GNU_regval_type
28492 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
28493 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
28494 else if (loc->dw_loc_oprnd1.val_class
28495 == dw_val_class_unsigned_const)
28496 break;
28497 else
28498 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
28499 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
28500 == dw_val_class_unsigned_const)
28501 break;
28502 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
28503 gcc_assert (base1->die_tag == DW_TAG_base_type
28504 && base2->die_tag == DW_TAG_base_type);
28505 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
28506 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
28507 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
28508 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
28509 if (size1 == size2
28510 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
28511 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
28512 && loc != keep)
28513 || enc1 == enc2))
28515 /* Optimize away next DW_OP_convert after
28516 adjusting LOC's base type die reference. */
28517 if (loc->dw_loc_opc == DW_OP_regval_type
28518 || loc->dw_loc_opc == DW_OP_deref_type
28519 || loc->dw_loc_opc == DW_OP_GNU_regval_type
28520 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
28521 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
28522 else
28523 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
28524 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
28525 continue;
28527 /* Don't change integer DW_OP_convert after e.g. floating
28528 point typed stack entry. */
28529 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
28530 keep = loc->dw_loc_next;
28531 break;
28533 break;
28534 default:
28535 break;
28537 return true;
28540 /* Helper function of resolve_addr. DIE had DW_AT_location of
28541 DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
28542 and DW_OP_addr couldn't be resolved. resolve_addr has already
28543 removed the DW_AT_location attribute. This function attempts to
28544 add a new DW_AT_location attribute with DW_OP_implicit_pointer
28545 to it or DW_AT_const_value attribute, if possible. */
28547 static void
28548 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
28550 if (!VAR_P (decl)
28551 || lookup_decl_die (decl) != die
28552 || DECL_EXTERNAL (decl)
28553 || !TREE_STATIC (decl)
28554 || DECL_INITIAL (decl) == NULL_TREE
28555 || DECL_P (DECL_INITIAL (decl))
28556 || get_AT (die, DW_AT_const_value))
28557 return;
28559 tree init = DECL_INITIAL (decl);
28560 HOST_WIDE_INT offset = 0;
28561 /* For variables that have been optimized away and thus
28562 don't have a memory location, see if we can emit
28563 DW_AT_const_value instead. */
28564 if (tree_add_const_value_attribute (die, init))
28565 return;
28566 if (dwarf_strict && dwarf_version < 5)
28567 return;
28568 /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
28569 and ADDR_EXPR refers to a decl that has DW_AT_location or
28570 DW_AT_const_value (but isn't addressable, otherwise
28571 resolving the original DW_OP_addr wouldn't fail), see if
28572 we can add DW_OP_implicit_pointer. */
28573 STRIP_NOPS (init);
28574 if (TREE_CODE (init) == POINTER_PLUS_EXPR
28575 && tree_fits_shwi_p (TREE_OPERAND (init, 1)))
28577 offset = tree_to_shwi (TREE_OPERAND (init, 1));
28578 init = TREE_OPERAND (init, 0);
28579 STRIP_NOPS (init);
28581 if (TREE_CODE (init) != ADDR_EXPR)
28582 return;
28583 if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
28584 && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
28585 || (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL
28586 && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
28587 && TREE_OPERAND (init, 0) != decl))
28589 dw_die_ref ref;
28590 dw_loc_descr_ref l;
28592 if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
28594 rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
28595 if (!rtl)
28596 return;
28597 decl = SYMBOL_REF_DECL (rtl);
28599 else
28600 decl = TREE_OPERAND (init, 0);
28601 ref = lookup_decl_die (decl);
28602 if (ref == NULL
28603 || (!get_AT (ref, DW_AT_location)
28604 && !get_AT (ref, DW_AT_const_value)))
28605 return;
28606 l = new_loc_descr (dwarf_OP (DW_OP_implicit_pointer), 0, offset);
28607 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
28608 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
28609 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
28610 add_AT_loc (die, DW_AT_location, l);
28614 /* Return NULL if l is a DWARF expression, or first op that is not
28615 valid DWARF expression. */
28617 static dw_loc_descr_ref
28618 non_dwarf_expression (dw_loc_descr_ref l)
28620 while (l)
28622 if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
28623 return l;
28624 switch (l->dw_loc_opc)
28626 case DW_OP_regx:
28627 case DW_OP_implicit_value:
28628 case DW_OP_stack_value:
28629 case DW_OP_implicit_pointer:
28630 case DW_OP_GNU_implicit_pointer:
28631 case DW_OP_GNU_parameter_ref:
28632 case DW_OP_piece:
28633 case DW_OP_bit_piece:
28634 return l;
28635 default:
28636 break;
28638 l = l->dw_loc_next;
28640 return NULL;
28643 /* Return adjusted copy of EXPR:
28644 If it is empty DWARF expression, return it.
28645 If it is valid non-empty DWARF expression,
28646 return copy of EXPR with DW_OP_deref appended to it.
28647 If it is DWARF expression followed by DW_OP_reg{N,x}, return
28648 copy of the DWARF expression with DW_OP_breg{N,x} <0> appended.
28649 If it is DWARF expression followed by DW_OP_stack_value, return
28650 copy of the DWARF expression without anything appended.
28651 Otherwise, return NULL. */
28653 static dw_loc_descr_ref
28654 copy_deref_exprloc (dw_loc_descr_ref expr)
28656 dw_loc_descr_ref tail = NULL;
28658 if (expr == NULL)
28659 return NULL;
28661 dw_loc_descr_ref l = non_dwarf_expression (expr);
28662 if (l && l->dw_loc_next)
28663 return NULL;
28665 if (l)
28667 if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
28668 tail = new_loc_descr ((enum dwarf_location_atom)
28669 (DW_OP_breg0 + (l->dw_loc_opc - DW_OP_reg0)),
28670 0, 0);
28671 else
28672 switch (l->dw_loc_opc)
28674 case DW_OP_regx:
28675 tail = new_loc_descr (DW_OP_bregx,
28676 l->dw_loc_oprnd1.v.val_unsigned, 0);
28677 break;
28678 case DW_OP_stack_value:
28679 break;
28680 default:
28681 return NULL;
28684 else
28685 tail = new_loc_descr (DW_OP_deref, 0, 0);
28687 dw_loc_descr_ref ret = NULL, *p = &ret;
28688 while (expr != l)
28690 *p = new_loc_descr (expr->dw_loc_opc, 0, 0);
28691 (*p)->dw_loc_oprnd1 = expr->dw_loc_oprnd1;
28692 (*p)->dw_loc_oprnd2 = expr->dw_loc_oprnd2;
28693 p = &(*p)->dw_loc_next;
28694 expr = expr->dw_loc_next;
28696 *p = tail;
28697 return ret;
28700 /* For DW_AT_string_length attribute with DW_OP_GNU_variable_value
28701 reference to a variable or argument, adjust it if needed and return:
28702 -1 if the DW_AT_string_length attribute and DW_AT_{string_length_,}byte_size
28703 attribute if present should be removed
28704 0 keep the attribute perhaps with minor modifications, no need to rescan
28705 1 if the attribute has been successfully adjusted. */
28707 static int
28708 optimize_string_length (dw_attr_node *a)
28710 dw_loc_descr_ref l = AT_loc (a), lv;
28711 dw_die_ref die;
28712 if (l->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
28714 tree decl = l->dw_loc_oprnd1.v.val_decl_ref;
28715 die = lookup_decl_die (decl);
28716 if (die)
28718 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
28719 l->dw_loc_oprnd1.v.val_die_ref.die = die;
28720 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
28722 else
28723 return -1;
28725 else
28726 die = l->dw_loc_oprnd1.v.val_die_ref.die;
28728 /* DWARF5 allows reference class, so we can then reference the DIE.
28729 Only do this for DW_OP_GNU_variable_value DW_OP_stack_value. */
28730 if (l->dw_loc_next != NULL && dwarf_version >= 5)
28732 a->dw_attr_val.val_class = dw_val_class_die_ref;
28733 a->dw_attr_val.val_entry = NULL;
28734 a->dw_attr_val.v.val_die_ref.die = die;
28735 a->dw_attr_val.v.val_die_ref.external = 0;
28736 return 0;
28739 dw_attr_node *av = get_AT (die, DW_AT_location);
28740 dw_loc_list_ref d;
28741 bool non_dwarf_expr = false;
28743 if (av == NULL)
28744 return dwarf_strict ? -1 : 0;
28745 switch (AT_class (av))
28747 case dw_val_class_loc_list:
28748 for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
28749 if (d->expr && non_dwarf_expression (d->expr))
28750 non_dwarf_expr = true;
28751 break;
28752 case dw_val_class_loc:
28753 lv = AT_loc (av);
28754 if (lv == NULL)
28755 return dwarf_strict ? -1 : 0;
28756 if (non_dwarf_expression (lv))
28757 non_dwarf_expr = true;
28758 break;
28759 default:
28760 return dwarf_strict ? -1 : 0;
28763 /* If it is safe to transform DW_OP_GNU_variable_value DW_OP_stack_value
28764 into DW_OP_call4 or DW_OP_GNU_variable_value into
28765 DW_OP_call4 DW_OP_deref, do so. */
28766 if (!non_dwarf_expr
28767 && (l->dw_loc_next != NULL || AT_class (av) == dw_val_class_loc))
28769 l->dw_loc_opc = DW_OP_call4;
28770 if (l->dw_loc_next)
28771 l->dw_loc_next = NULL;
28772 else
28773 l->dw_loc_next = new_loc_descr (DW_OP_deref, 0, 0);
28774 return 0;
28777 /* For DW_OP_GNU_variable_value DW_OP_stack_value, we can just
28778 copy over the DW_AT_location attribute from die to a. */
28779 if (l->dw_loc_next != NULL)
28781 a->dw_attr_val = av->dw_attr_val;
28782 return 1;
28785 dw_loc_list_ref list, *p;
28786 switch (AT_class (av))
28788 case dw_val_class_loc_list:
28789 p = &list;
28790 list = NULL;
28791 for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
28793 lv = copy_deref_exprloc (d->expr);
28794 if (lv)
28796 *p = new_loc_list (lv, d->begin, d->end, d->section);
28797 p = &(*p)->dw_loc_next;
28799 else if (!dwarf_strict && d->expr)
28800 return 0;
28802 if (list == NULL)
28803 return dwarf_strict ? -1 : 0;
28804 a->dw_attr_val.val_class = dw_val_class_loc_list;
28805 gen_llsym (list);
28806 *AT_loc_list_ptr (a) = list;
28807 return 1;
28808 case dw_val_class_loc:
28809 lv = copy_deref_exprloc (AT_loc (av));
28810 if (lv == NULL)
28811 return dwarf_strict ? -1 : 0;
28812 a->dw_attr_val.v.val_loc = lv;
28813 return 1;
28814 default:
28815 gcc_unreachable ();
28819 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
28820 an address in .rodata section if the string literal is emitted there,
28821 or remove the containing location list or replace DW_AT_const_value
28822 with DW_AT_location and empty location expression, if it isn't found
28823 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
28824 to something that has been emitted in the current CU. */
28826 static void
28827 resolve_addr (dw_die_ref die)
28829 dw_die_ref c;
28830 dw_attr_node *a;
28831 dw_loc_list_ref *curr, *start, loc;
28832 unsigned ix;
28833 bool remove_AT_byte_size = false;
28835 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
28836 switch (AT_class (a))
28838 case dw_val_class_loc_list:
28839 start = curr = AT_loc_list_ptr (a);
28840 loc = *curr;
28841 gcc_assert (loc);
28842 /* The same list can be referenced more than once. See if we have
28843 already recorded the result from a previous pass. */
28844 if (loc->replaced)
28845 *curr = loc->dw_loc_next;
28846 else if (!loc->resolved_addr)
28848 /* As things stand, we do not expect or allow one die to
28849 reference a suffix of another die's location list chain.
28850 References must be identical or completely separate.
28851 There is therefore no need to cache the result of this
28852 pass on any list other than the first; doing so
28853 would lead to unnecessary writes. */
28854 while (*curr)
28856 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
28857 if (!resolve_addr_in_expr (a, (*curr)->expr))
28859 dw_loc_list_ref next = (*curr)->dw_loc_next;
28860 dw_loc_descr_ref l = (*curr)->expr;
28862 if (next && (*curr)->ll_symbol)
28864 gcc_assert (!next->ll_symbol);
28865 next->ll_symbol = (*curr)->ll_symbol;
28867 if (dwarf_split_debug_info)
28868 remove_loc_list_addr_table_entries (l);
28869 *curr = next;
28871 else
28873 mark_base_types ((*curr)->expr);
28874 curr = &(*curr)->dw_loc_next;
28877 if (loc == *start)
28878 loc->resolved_addr = 1;
28879 else
28881 loc->replaced = 1;
28882 loc->dw_loc_next = *start;
28885 if (!*start)
28887 remove_AT (die, a->dw_attr);
28888 ix--;
28890 break;
28891 case dw_val_class_loc:
28893 dw_loc_descr_ref l = AT_loc (a);
28894 /* DW_OP_GNU_variable_value DW_OP_stack_value or
28895 DW_OP_GNU_variable_value in DW_AT_string_length can be converted
28896 into DW_OP_call4 or DW_OP_call4 DW_OP_deref, which is standard
28897 DWARF4 unlike DW_OP_GNU_variable_value. Or for DWARF5
28898 DW_OP_GNU_variable_value DW_OP_stack_value can be replaced
28899 with DW_FORM_ref referencing the same DIE as
28900 DW_OP_GNU_variable_value used to reference. */
28901 if (a->dw_attr == DW_AT_string_length
28902 && l
28903 && l->dw_loc_opc == DW_OP_GNU_variable_value
28904 && (l->dw_loc_next == NULL
28905 || (l->dw_loc_next->dw_loc_next == NULL
28906 && l->dw_loc_next->dw_loc_opc == DW_OP_stack_value)))
28908 switch (optimize_string_length (a))
28910 case -1:
28911 remove_AT (die, a->dw_attr);
28912 ix--;
28913 /* If we drop DW_AT_string_length, we need to drop also
28914 DW_AT_{string_length_,}byte_size. */
28915 remove_AT_byte_size = true;
28916 continue;
28917 default:
28918 break;
28919 case 1:
28920 /* Even if we keep the optimized DW_AT_string_length,
28921 it might have changed AT_class, so process it again. */
28922 ix--;
28923 continue;
28926 /* For -gdwarf-2 don't attempt to optimize
28927 DW_AT_data_member_location containing
28928 DW_OP_plus_uconst - older consumers might
28929 rely on it being that op instead of a more complex,
28930 but shorter, location description. */
28931 if ((dwarf_version > 2
28932 || a->dw_attr != DW_AT_data_member_location
28933 || l == NULL
28934 || l->dw_loc_opc != DW_OP_plus_uconst
28935 || l->dw_loc_next != NULL)
28936 && !resolve_addr_in_expr (a, l))
28938 if (dwarf_split_debug_info)
28939 remove_loc_list_addr_table_entries (l);
28940 if (l != NULL
28941 && l->dw_loc_next == NULL
28942 && l->dw_loc_opc == DW_OP_addr
28943 && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
28944 && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
28945 && a->dw_attr == DW_AT_location)
28947 tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
28948 remove_AT (die, a->dw_attr);
28949 ix--;
28950 optimize_location_into_implicit_ptr (die, decl);
28951 break;
28953 if (a->dw_attr == DW_AT_string_length)
28954 /* If we drop DW_AT_string_length, we need to drop also
28955 DW_AT_{string_length_,}byte_size. */
28956 remove_AT_byte_size = true;
28957 remove_AT (die, a->dw_attr);
28958 ix--;
28960 else
28961 mark_base_types (l);
28963 break;
28964 case dw_val_class_addr:
28965 if (a->dw_attr == DW_AT_const_value
28966 && !resolve_one_addr (&a->dw_attr_val.v.val_addr))
28968 if (AT_index (a) != NOT_INDEXED)
28969 remove_addr_table_entry (a->dw_attr_val.val_entry);
28970 remove_AT (die, a->dw_attr);
28971 ix--;
28973 if ((die->die_tag == DW_TAG_call_site
28974 && a->dw_attr == DW_AT_call_origin)
28975 || (die->die_tag == DW_TAG_GNU_call_site
28976 && a->dw_attr == DW_AT_abstract_origin))
28978 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
28979 dw_die_ref tdie = lookup_decl_die (tdecl);
28980 dw_die_ref cdie;
28981 if (tdie == NULL
28982 && DECL_EXTERNAL (tdecl)
28983 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE
28984 && (cdie = lookup_context_die (DECL_CONTEXT (tdecl))))
28986 dw_die_ref pdie = cdie;
28987 /* Make sure we don't add these DIEs into type units.
28988 We could emit skeleton DIEs for context (namespaces,
28989 outer structs/classes) and a skeleton DIE for the
28990 innermost context with DW_AT_signature pointing to the
28991 type unit. See PR78835. */
28992 while (pdie && pdie->die_tag != DW_TAG_type_unit)
28993 pdie = pdie->die_parent;
28994 if (pdie == NULL)
28996 /* Creating a full DIE for tdecl is overly expensive and
28997 at this point even wrong when in the LTO phase
28998 as it can end up generating new type DIEs we didn't
28999 output and thus optimize_external_refs will crash. */
29000 tdie = new_die (DW_TAG_subprogram, cdie, NULL_TREE);
29001 add_AT_flag (tdie, DW_AT_external, 1);
29002 add_AT_flag (tdie, DW_AT_declaration, 1);
29003 add_linkage_attr (tdie, tdecl);
29004 add_name_and_src_coords_attributes (tdie, tdecl);
29005 equate_decl_number_to_die (tdecl, tdie);
29008 if (tdie)
29010 a->dw_attr_val.val_class = dw_val_class_die_ref;
29011 a->dw_attr_val.v.val_die_ref.die = tdie;
29012 a->dw_attr_val.v.val_die_ref.external = 0;
29014 else
29016 if (AT_index (a) != NOT_INDEXED)
29017 remove_addr_table_entry (a->dw_attr_val.val_entry);
29018 remove_AT (die, a->dw_attr);
29019 ix--;
29022 break;
29023 default:
29024 break;
29027 if (remove_AT_byte_size)
29028 remove_AT (die, dwarf_version >= 5
29029 ? DW_AT_string_length_byte_size
29030 : DW_AT_byte_size);
29032 FOR_EACH_CHILD (die, c, resolve_addr (c));
29035 /* Helper routines for optimize_location_lists.
29036 This pass tries to share identical local lists in .debug_loc
29037 section. */
29039 /* Iteratively hash operands of LOC opcode into HSTATE. */
29041 static void
29042 hash_loc_operands (dw_loc_descr_ref loc, inchash::hash &hstate)
29044 dw_val_ref val1 = &loc->dw_loc_oprnd1;
29045 dw_val_ref val2 = &loc->dw_loc_oprnd2;
29047 switch (loc->dw_loc_opc)
29049 case DW_OP_const4u:
29050 case DW_OP_const8u:
29051 if (loc->dtprel)
29052 goto hash_addr;
29053 /* FALLTHRU */
29054 case DW_OP_const1u:
29055 case DW_OP_const1s:
29056 case DW_OP_const2u:
29057 case DW_OP_const2s:
29058 case DW_OP_const4s:
29059 case DW_OP_const8s:
29060 case DW_OP_constu:
29061 case DW_OP_consts:
29062 case DW_OP_pick:
29063 case DW_OP_plus_uconst:
29064 case DW_OP_breg0:
29065 case DW_OP_breg1:
29066 case DW_OP_breg2:
29067 case DW_OP_breg3:
29068 case DW_OP_breg4:
29069 case DW_OP_breg5:
29070 case DW_OP_breg6:
29071 case DW_OP_breg7:
29072 case DW_OP_breg8:
29073 case DW_OP_breg9:
29074 case DW_OP_breg10:
29075 case DW_OP_breg11:
29076 case DW_OP_breg12:
29077 case DW_OP_breg13:
29078 case DW_OP_breg14:
29079 case DW_OP_breg15:
29080 case DW_OP_breg16:
29081 case DW_OP_breg17:
29082 case DW_OP_breg18:
29083 case DW_OP_breg19:
29084 case DW_OP_breg20:
29085 case DW_OP_breg21:
29086 case DW_OP_breg22:
29087 case DW_OP_breg23:
29088 case DW_OP_breg24:
29089 case DW_OP_breg25:
29090 case DW_OP_breg26:
29091 case DW_OP_breg27:
29092 case DW_OP_breg28:
29093 case DW_OP_breg29:
29094 case DW_OP_breg30:
29095 case DW_OP_breg31:
29096 case DW_OP_regx:
29097 case DW_OP_fbreg:
29098 case DW_OP_piece:
29099 case DW_OP_deref_size:
29100 case DW_OP_xderef_size:
29101 hstate.add_object (val1->v.val_int);
29102 break;
29103 case DW_OP_skip:
29104 case DW_OP_bra:
29106 int offset;
29108 gcc_assert (val1->val_class == dw_val_class_loc);
29109 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
29110 hstate.add_object (offset);
29112 break;
29113 case DW_OP_implicit_value:
29114 hstate.add_object (val1->v.val_unsigned);
29115 switch (val2->val_class)
29117 case dw_val_class_const:
29118 hstate.add_object (val2->v.val_int);
29119 break;
29120 case dw_val_class_vec:
29122 unsigned int elt_size = val2->v.val_vec.elt_size;
29123 unsigned int len = val2->v.val_vec.length;
29125 hstate.add_int (elt_size);
29126 hstate.add_int (len);
29127 hstate.add (val2->v.val_vec.array, len * elt_size);
29129 break;
29130 case dw_val_class_const_double:
29131 hstate.add_object (val2->v.val_double.low);
29132 hstate.add_object (val2->v.val_double.high);
29133 break;
29134 case dw_val_class_wide_int:
29135 hstate.add (val2->v.val_wide->get_val (),
29136 get_full_len (*val2->v.val_wide)
29137 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
29138 break;
29139 case dw_val_class_addr:
29140 inchash::add_rtx (val2->v.val_addr, hstate);
29141 break;
29142 default:
29143 gcc_unreachable ();
29145 break;
29146 case DW_OP_bregx:
29147 case DW_OP_bit_piece:
29148 hstate.add_object (val1->v.val_int);
29149 hstate.add_object (val2->v.val_int);
29150 break;
29151 case DW_OP_addr:
29152 hash_addr:
29153 if (loc->dtprel)
29155 unsigned char dtprel = 0xd1;
29156 hstate.add_object (dtprel);
29158 inchash::add_rtx (val1->v.val_addr, hstate);
29159 break;
29160 case DW_OP_GNU_addr_index:
29161 case DW_OP_GNU_const_index:
29163 if (loc->dtprel)
29165 unsigned char dtprel = 0xd1;
29166 hstate.add_object (dtprel);
29168 inchash::add_rtx (val1->val_entry->addr.rtl, hstate);
29170 break;
29171 case DW_OP_implicit_pointer:
29172 case DW_OP_GNU_implicit_pointer:
29173 hstate.add_int (val2->v.val_int);
29174 break;
29175 case DW_OP_entry_value:
29176 case DW_OP_GNU_entry_value:
29177 hstate.add_object (val1->v.val_loc);
29178 break;
29179 case DW_OP_regval_type:
29180 case DW_OP_deref_type:
29181 case DW_OP_GNU_regval_type:
29182 case DW_OP_GNU_deref_type:
29184 unsigned int byte_size
29185 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
29186 unsigned int encoding
29187 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
29188 hstate.add_object (val1->v.val_int);
29189 hstate.add_object (byte_size);
29190 hstate.add_object (encoding);
29192 break;
29193 case DW_OP_convert:
29194 case DW_OP_reinterpret:
29195 case DW_OP_GNU_convert:
29196 case DW_OP_GNU_reinterpret:
29197 if (val1->val_class == dw_val_class_unsigned_const)
29199 hstate.add_object (val1->v.val_unsigned);
29200 break;
29202 /* FALLTHRU */
29203 case DW_OP_const_type:
29204 case DW_OP_GNU_const_type:
29206 unsigned int byte_size
29207 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
29208 unsigned int encoding
29209 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
29210 hstate.add_object (byte_size);
29211 hstate.add_object (encoding);
29212 if (loc->dw_loc_opc != DW_OP_const_type
29213 && loc->dw_loc_opc != DW_OP_GNU_const_type)
29214 break;
29215 hstate.add_object (val2->val_class);
29216 switch (val2->val_class)
29218 case dw_val_class_const:
29219 hstate.add_object (val2->v.val_int);
29220 break;
29221 case dw_val_class_vec:
29223 unsigned int elt_size = val2->v.val_vec.elt_size;
29224 unsigned int len = val2->v.val_vec.length;
29226 hstate.add_object (elt_size);
29227 hstate.add_object (len);
29228 hstate.add (val2->v.val_vec.array, len * elt_size);
29230 break;
29231 case dw_val_class_const_double:
29232 hstate.add_object (val2->v.val_double.low);
29233 hstate.add_object (val2->v.val_double.high);
29234 break;
29235 case dw_val_class_wide_int:
29236 hstate.add (val2->v.val_wide->get_val (),
29237 get_full_len (*val2->v.val_wide)
29238 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
29239 break;
29240 default:
29241 gcc_unreachable ();
29244 break;
29246 default:
29247 /* Other codes have no operands. */
29248 break;
29252 /* Iteratively hash the whole DWARF location expression LOC into HSTATE. */
29254 static inline void
29255 hash_locs (dw_loc_descr_ref loc, inchash::hash &hstate)
29257 dw_loc_descr_ref l;
29258 bool sizes_computed = false;
29259 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
29260 size_of_locs (loc);
29262 for (l = loc; l != NULL; l = l->dw_loc_next)
29264 enum dwarf_location_atom opc = l->dw_loc_opc;
29265 hstate.add_object (opc);
29266 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
29268 size_of_locs (loc);
29269 sizes_computed = true;
29271 hash_loc_operands (l, hstate);
29275 /* Compute hash of the whole location list LIST_HEAD. */
29277 static inline void
29278 hash_loc_list (dw_loc_list_ref list_head)
29280 dw_loc_list_ref curr = list_head;
29281 inchash::hash hstate;
29283 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
29285 hstate.add (curr->begin, strlen (curr->begin) + 1);
29286 hstate.add (curr->end, strlen (curr->end) + 1);
29287 if (curr->section)
29288 hstate.add (curr->section, strlen (curr->section) + 1);
29289 hash_locs (curr->expr, hstate);
29291 list_head->hash = hstate.end ();
29294 /* Return true if X and Y opcodes have the same operands. */
29296 static inline bool
29297 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
29299 dw_val_ref valx1 = &x->dw_loc_oprnd1;
29300 dw_val_ref valx2 = &x->dw_loc_oprnd2;
29301 dw_val_ref valy1 = &y->dw_loc_oprnd1;
29302 dw_val_ref valy2 = &y->dw_loc_oprnd2;
29304 switch (x->dw_loc_opc)
29306 case DW_OP_const4u:
29307 case DW_OP_const8u:
29308 if (x->dtprel)
29309 goto hash_addr;
29310 /* FALLTHRU */
29311 case DW_OP_const1u:
29312 case DW_OP_const1s:
29313 case DW_OP_const2u:
29314 case DW_OP_const2s:
29315 case DW_OP_const4s:
29316 case DW_OP_const8s:
29317 case DW_OP_constu:
29318 case DW_OP_consts:
29319 case DW_OP_pick:
29320 case DW_OP_plus_uconst:
29321 case DW_OP_breg0:
29322 case DW_OP_breg1:
29323 case DW_OP_breg2:
29324 case DW_OP_breg3:
29325 case DW_OP_breg4:
29326 case DW_OP_breg5:
29327 case DW_OP_breg6:
29328 case DW_OP_breg7:
29329 case DW_OP_breg8:
29330 case DW_OP_breg9:
29331 case DW_OP_breg10:
29332 case DW_OP_breg11:
29333 case DW_OP_breg12:
29334 case DW_OP_breg13:
29335 case DW_OP_breg14:
29336 case DW_OP_breg15:
29337 case DW_OP_breg16:
29338 case DW_OP_breg17:
29339 case DW_OP_breg18:
29340 case DW_OP_breg19:
29341 case DW_OP_breg20:
29342 case DW_OP_breg21:
29343 case DW_OP_breg22:
29344 case DW_OP_breg23:
29345 case DW_OP_breg24:
29346 case DW_OP_breg25:
29347 case DW_OP_breg26:
29348 case DW_OP_breg27:
29349 case DW_OP_breg28:
29350 case DW_OP_breg29:
29351 case DW_OP_breg30:
29352 case DW_OP_breg31:
29353 case DW_OP_regx:
29354 case DW_OP_fbreg:
29355 case DW_OP_piece:
29356 case DW_OP_deref_size:
29357 case DW_OP_xderef_size:
29358 return valx1->v.val_int == valy1->v.val_int;
29359 case DW_OP_skip:
29360 case DW_OP_bra:
29361 /* If splitting debug info, the use of DW_OP_GNU_addr_index
29362 can cause irrelevant differences in dw_loc_addr. */
29363 gcc_assert (valx1->val_class == dw_val_class_loc
29364 && valy1->val_class == dw_val_class_loc
29365 && (dwarf_split_debug_info
29366 || x->dw_loc_addr == y->dw_loc_addr));
29367 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
29368 case DW_OP_implicit_value:
29369 if (valx1->v.val_unsigned != valy1->v.val_unsigned
29370 || valx2->val_class != valy2->val_class)
29371 return false;
29372 switch (valx2->val_class)
29374 case dw_val_class_const:
29375 return valx2->v.val_int == valy2->v.val_int;
29376 case dw_val_class_vec:
29377 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
29378 && valx2->v.val_vec.length == valy2->v.val_vec.length
29379 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
29380 valx2->v.val_vec.elt_size
29381 * valx2->v.val_vec.length) == 0;
29382 case dw_val_class_const_double:
29383 return valx2->v.val_double.low == valy2->v.val_double.low
29384 && valx2->v.val_double.high == valy2->v.val_double.high;
29385 case dw_val_class_wide_int:
29386 return *valx2->v.val_wide == *valy2->v.val_wide;
29387 case dw_val_class_addr:
29388 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
29389 default:
29390 gcc_unreachable ();
29392 case DW_OP_bregx:
29393 case DW_OP_bit_piece:
29394 return valx1->v.val_int == valy1->v.val_int
29395 && valx2->v.val_int == valy2->v.val_int;
29396 case DW_OP_addr:
29397 hash_addr:
29398 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
29399 case DW_OP_GNU_addr_index:
29400 case DW_OP_GNU_const_index:
29402 rtx ax1 = valx1->val_entry->addr.rtl;
29403 rtx ay1 = valy1->val_entry->addr.rtl;
29404 return rtx_equal_p (ax1, ay1);
29406 case DW_OP_implicit_pointer:
29407 case DW_OP_GNU_implicit_pointer:
29408 return valx1->val_class == dw_val_class_die_ref
29409 && valx1->val_class == valy1->val_class
29410 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
29411 && valx2->v.val_int == valy2->v.val_int;
29412 case DW_OP_entry_value:
29413 case DW_OP_GNU_entry_value:
29414 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
29415 case DW_OP_const_type:
29416 case DW_OP_GNU_const_type:
29417 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
29418 || valx2->val_class != valy2->val_class)
29419 return false;
29420 switch (valx2->val_class)
29422 case dw_val_class_const:
29423 return valx2->v.val_int == valy2->v.val_int;
29424 case dw_val_class_vec:
29425 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
29426 && valx2->v.val_vec.length == valy2->v.val_vec.length
29427 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
29428 valx2->v.val_vec.elt_size
29429 * valx2->v.val_vec.length) == 0;
29430 case dw_val_class_const_double:
29431 return valx2->v.val_double.low == valy2->v.val_double.low
29432 && valx2->v.val_double.high == valy2->v.val_double.high;
29433 case dw_val_class_wide_int:
29434 return *valx2->v.val_wide == *valy2->v.val_wide;
29435 default:
29436 gcc_unreachable ();
29438 case DW_OP_regval_type:
29439 case DW_OP_deref_type:
29440 case DW_OP_GNU_regval_type:
29441 case DW_OP_GNU_deref_type:
29442 return valx1->v.val_int == valy1->v.val_int
29443 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
29444 case DW_OP_convert:
29445 case DW_OP_reinterpret:
29446 case DW_OP_GNU_convert:
29447 case DW_OP_GNU_reinterpret:
29448 if (valx1->val_class != valy1->val_class)
29449 return false;
29450 if (valx1->val_class == dw_val_class_unsigned_const)
29451 return valx1->v.val_unsigned == valy1->v.val_unsigned;
29452 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
29453 case DW_OP_GNU_parameter_ref:
29454 return valx1->val_class == dw_val_class_die_ref
29455 && valx1->val_class == valy1->val_class
29456 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
29457 default:
29458 /* Other codes have no operands. */
29459 return true;
29463 /* Return true if DWARF location expressions X and Y are the same. */
29465 static inline bool
29466 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
29468 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
29469 if (x->dw_loc_opc != y->dw_loc_opc
29470 || x->dtprel != y->dtprel
29471 || !compare_loc_operands (x, y))
29472 break;
29473 return x == NULL && y == NULL;
29476 /* Hashtable helpers. */
29478 struct loc_list_hasher : nofree_ptr_hash <dw_loc_list_struct>
29480 static inline hashval_t hash (const dw_loc_list_struct *);
29481 static inline bool equal (const dw_loc_list_struct *,
29482 const dw_loc_list_struct *);
29485 /* Return precomputed hash of location list X. */
29487 inline hashval_t
29488 loc_list_hasher::hash (const dw_loc_list_struct *x)
29490 return x->hash;
29493 /* Return true if location lists A and B are the same. */
29495 inline bool
29496 loc_list_hasher::equal (const dw_loc_list_struct *a,
29497 const dw_loc_list_struct *b)
29499 if (a == b)
29500 return 1;
29501 if (a->hash != b->hash)
29502 return 0;
29503 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
29504 if (strcmp (a->begin, b->begin) != 0
29505 || strcmp (a->end, b->end) != 0
29506 || (a->section == NULL) != (b->section == NULL)
29507 || (a->section && strcmp (a->section, b->section) != 0)
29508 || !compare_locs (a->expr, b->expr))
29509 break;
29510 return a == NULL && b == NULL;
29513 typedef hash_table<loc_list_hasher> loc_list_hash_type;
29516 /* Recursively optimize location lists referenced from DIE
29517 children and share them whenever possible. */
29519 static void
29520 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type *htab)
29522 dw_die_ref c;
29523 dw_attr_node *a;
29524 unsigned ix;
29525 dw_loc_list_struct **slot;
29527 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
29528 if (AT_class (a) == dw_val_class_loc_list)
29530 dw_loc_list_ref list = AT_loc_list (a);
29531 /* TODO: perform some optimizations here, before hashing
29532 it and storing into the hash table. */
29533 hash_loc_list (list);
29534 slot = htab->find_slot_with_hash (list, list->hash, INSERT);
29535 if (*slot == NULL)
29536 *slot = list;
29537 else
29538 a->dw_attr_val.v.val_loc_list = *slot;
29541 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
29545 /* Recursively assign each location list a unique index into the debug_addr
29546 section. */
29548 static void
29549 index_location_lists (dw_die_ref die)
29551 dw_die_ref c;
29552 dw_attr_node *a;
29553 unsigned ix;
29555 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
29556 if (AT_class (a) == dw_val_class_loc_list)
29558 dw_loc_list_ref list = AT_loc_list (a);
29559 dw_loc_list_ref curr;
29560 for (curr = list; curr != NULL; curr = curr->dw_loc_next)
29562 /* Don't index an entry that has already been indexed
29563 or won't be output. */
29564 if (curr->begin_entry != NULL
29565 || (strcmp (curr->begin, curr->end) == 0 && !curr->force))
29566 continue;
29568 curr->begin_entry
29569 = add_addr_table_entry (xstrdup (curr->begin), ate_kind_label);
29573 FOR_EACH_CHILD (die, c, index_location_lists (c));
29576 /* Optimize location lists referenced from DIE
29577 children and share them whenever possible. */
29579 static void
29580 optimize_location_lists (dw_die_ref die)
29582 loc_list_hash_type htab (500);
29583 optimize_location_lists_1 (die, &htab);
29586 /* Traverse the limbo die list, and add parent/child links. The only
29587 dies without parents that should be here are concrete instances of
29588 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
29589 For concrete instances, we can get the parent die from the abstract
29590 instance. */
29592 static void
29593 flush_limbo_die_list (void)
29595 limbo_die_node *node;
29597 /* get_context_die calls force_decl_die, which can put new DIEs on the
29598 limbo list in LTO mode when nested functions are put in a different
29599 partition than that of their parent function. */
29600 while ((node = limbo_die_list))
29602 dw_die_ref die = node->die;
29603 limbo_die_list = node->next;
29605 if (die->die_parent == NULL)
29607 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
29609 if (origin && origin->die_parent)
29610 add_child_die (origin->die_parent, die);
29611 else if (is_cu_die (die))
29613 else if (seen_error ())
29614 /* It's OK to be confused by errors in the input. */
29615 add_child_die (comp_unit_die (), die);
29616 else
29618 /* In certain situations, the lexical block containing a
29619 nested function can be optimized away, which results
29620 in the nested function die being orphaned. Likewise
29621 with the return type of that nested function. Force
29622 this to be a child of the containing function.
29624 It may happen that even the containing function got fully
29625 inlined and optimized out. In that case we are lost and
29626 assign the empty child. This should not be big issue as
29627 the function is likely unreachable too. */
29628 gcc_assert (node->created_for);
29630 if (DECL_P (node->created_for))
29631 origin = get_context_die (DECL_CONTEXT (node->created_for));
29632 else if (TYPE_P (node->created_for))
29633 origin = scope_die_for (node->created_for, comp_unit_die ());
29634 else
29635 origin = comp_unit_die ();
29637 add_child_die (origin, die);
29643 /* Output stuff that dwarf requires at the end of every file,
29644 and generate the DWARF-2 debugging info. */
29646 static void
29647 dwarf2out_finish (const char *)
29649 comdat_type_node *ctnode;
29650 dw_die_ref main_comp_unit_die;
29651 unsigned char checksum[16];
29653 /* Flush out any latecomers to the limbo party. */
29654 flush_limbo_die_list ();
29656 if (flag_checking)
29658 verify_die (comp_unit_die ());
29659 for (limbo_die_node *node = cu_die_list; node; node = node->next)
29660 verify_die (node->die);
29663 /* We shouldn't have any symbols with delayed asm names for
29664 DIEs generated after early finish. */
29665 gcc_assert (deferred_asm_name == NULL);
29667 gen_remaining_tmpl_value_param_die_attribute ();
29669 #if ENABLE_ASSERT_CHECKING
29671 dw_die_ref die = comp_unit_die (), c;
29672 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
29674 #endif
29675 resolve_addr (comp_unit_die ());
29676 move_marked_base_types ();
29678 /* Initialize sections and labels used for actual assembler output. */
29679 init_sections_and_labels ();
29681 /* Traverse the DIE's and add sibling attributes to those DIE's that
29682 have children. */
29683 add_sibling_attributes (comp_unit_die ());
29684 limbo_die_node *node;
29685 for (node = cu_die_list; node; node = node->next)
29686 add_sibling_attributes (node->die);
29687 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
29688 add_sibling_attributes (ctnode->root_die);
29690 /* When splitting DWARF info, we put some attributes in the
29691 skeleton compile_unit DIE that remains in the .o, while
29692 most attributes go in the DWO compile_unit_die. */
29693 if (dwarf_split_debug_info)
29695 limbo_die_node *cu;
29696 main_comp_unit_die = gen_compile_unit_die (NULL);
29697 if (dwarf_version >= 5)
29698 main_comp_unit_die->die_tag = DW_TAG_skeleton_unit;
29699 cu = limbo_die_list;
29700 gcc_assert (cu->die == main_comp_unit_die);
29701 limbo_die_list = limbo_die_list->next;
29702 cu->next = cu_die_list;
29703 cu_die_list = cu;
29705 else
29706 main_comp_unit_die = comp_unit_die ();
29708 /* Output a terminator label for the .text section. */
29709 switch_to_section (text_section);
29710 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
29711 if (cold_text_section)
29713 switch_to_section (cold_text_section);
29714 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
29717 /* We can only use the low/high_pc attributes if all of the code was
29718 in .text. */
29719 if (!have_multiple_function_sections
29720 || (dwarf_version < 3 && dwarf_strict))
29722 /* Don't add if the CU has no associated code. */
29723 if (text_section_used)
29724 add_AT_low_high_pc (main_comp_unit_die, text_section_label,
29725 text_end_label, true);
29727 else
29729 unsigned fde_idx;
29730 dw_fde_ref fde;
29731 bool range_list_added = false;
29733 if (text_section_used)
29734 add_ranges_by_labels (main_comp_unit_die, text_section_label,
29735 text_end_label, &range_list_added, true);
29736 if (cold_text_section_used)
29737 add_ranges_by_labels (main_comp_unit_die, cold_text_section_label,
29738 cold_end_label, &range_list_added, true);
29740 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
29742 if (DECL_IGNORED_P (fde->decl))
29743 continue;
29744 if (!fde->in_std_section)
29745 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
29746 fde->dw_fde_end, &range_list_added,
29747 true);
29748 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
29749 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
29750 fde->dw_fde_second_end, &range_list_added,
29751 true);
29754 if (range_list_added)
29756 /* We need to give .debug_loc and .debug_ranges an appropriate
29757 "base address". Use zero so that these addresses become
29758 absolute. Historically, we've emitted the unexpected
29759 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
29760 Emit both to give time for other tools to adapt. */
29761 add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
29762 if (! dwarf_strict && dwarf_version < 4)
29763 add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
29765 add_ranges (NULL);
29769 if (debug_info_level >= DINFO_LEVEL_TERSE)
29770 add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
29771 debug_line_section_label);
29773 if (have_macinfo)
29774 add_AT_macptr (comp_unit_die (),
29775 dwarf_version >= 5 ? DW_AT_macros
29776 : dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros,
29777 macinfo_section_label);
29779 if (dwarf_split_debug_info)
29781 if (have_location_lists)
29783 if (dwarf_version >= 5)
29784 add_AT_loclistsptr (comp_unit_die (), DW_AT_loclists_base,
29785 loc_section_label);
29786 /* optimize_location_lists calculates the size of the lists,
29787 so index them first, and assign indices to the entries.
29788 Although optimize_location_lists will remove entries from
29789 the table, it only does so for duplicates, and therefore
29790 only reduces ref_counts to 1. */
29791 index_location_lists (comp_unit_die ());
29794 if (addr_index_table != NULL)
29796 unsigned int index = 0;
29797 addr_index_table
29798 ->traverse_noresize<unsigned int *, index_addr_table_entry>
29799 (&index);
29803 loc_list_idx = 0;
29804 if (have_location_lists)
29806 optimize_location_lists (comp_unit_die ());
29807 /* And finally assign indexes to the entries for -gsplit-dwarf. */
29808 if (dwarf_version >= 5 && dwarf_split_debug_info)
29809 assign_location_list_indexes (comp_unit_die ());
29812 save_macinfo_strings ();
29814 if (dwarf_split_debug_info)
29816 unsigned int index = 0;
29818 /* Add attributes common to skeleton compile_units and
29819 type_units. Because these attributes include strings, it
29820 must be done before freezing the string table. Top-level
29821 skeleton die attrs are added when the skeleton type unit is
29822 created, so ensure it is created by this point. */
29823 add_top_level_skeleton_die_attrs (main_comp_unit_die);
29824 debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
29827 /* Output all of the compilation units. We put the main one last so that
29828 the offsets are available to output_pubnames. */
29829 for (node = cu_die_list; node; node = node->next)
29830 output_comp_unit (node->die, 0, NULL);
29832 hash_table<comdat_type_hasher> comdat_type_table (100);
29833 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
29835 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
29837 /* Don't output duplicate types. */
29838 if (*slot != HTAB_EMPTY_ENTRY)
29839 continue;
29841 /* Add a pointer to the line table for the main compilation unit
29842 so that the debugger can make sense of DW_AT_decl_file
29843 attributes. */
29844 if (debug_info_level >= DINFO_LEVEL_TERSE)
29845 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
29846 (!dwarf_split_debug_info
29847 ? debug_line_section_label
29848 : debug_skeleton_line_section_label));
29850 output_comdat_type_unit (ctnode);
29851 *slot = ctnode;
29854 /* The AT_pubnames attribute needs to go in all skeleton dies, including
29855 both the main_cu and all skeleton TUs. Making this call unconditional
29856 would end up either adding a second copy of the AT_pubnames attribute, or
29857 requiring a special case in add_top_level_skeleton_die_attrs. */
29858 if (!dwarf_split_debug_info)
29859 add_AT_pubnames (comp_unit_die ());
29861 if (dwarf_split_debug_info)
29863 int mark;
29864 struct md5_ctx ctx;
29866 if (dwarf_version >= 5 && !vec_safe_is_empty (ranges_table))
29867 index_rnglists ();
29869 /* Compute a checksum of the comp_unit to use as the dwo_id. */
29870 md5_init_ctx (&ctx);
29871 mark = 0;
29872 die_checksum (comp_unit_die (), &ctx, &mark);
29873 unmark_all_dies (comp_unit_die ());
29874 md5_finish_ctx (&ctx, checksum);
29876 if (dwarf_version < 5)
29878 /* Use the first 8 bytes of the checksum as the dwo_id,
29879 and add it to both comp-unit DIEs. */
29880 add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
29881 add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
29884 /* Add the base offset of the ranges table to the skeleton
29885 comp-unit DIE. */
29886 if (!vec_safe_is_empty (ranges_table))
29888 if (dwarf_version >= 5)
29889 add_AT_lineptr (main_comp_unit_die, DW_AT_rnglists_base,
29890 ranges_base_label);
29891 else
29892 add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
29893 ranges_section_label);
29896 switch_to_section (debug_addr_section);
29897 ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
29898 output_addr_table ();
29901 /* Output the main compilation unit if non-empty or if .debug_macinfo
29902 or .debug_macro will be emitted. */
29903 output_comp_unit (comp_unit_die (), have_macinfo,
29904 dwarf_split_debug_info ? checksum : NULL);
29906 if (dwarf_split_debug_info && info_section_emitted)
29907 output_skeleton_debug_sections (main_comp_unit_die, checksum);
29909 /* Output the abbreviation table. */
29910 if (vec_safe_length (abbrev_die_table) != 1)
29912 switch_to_section (debug_abbrev_section);
29913 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
29914 output_abbrev_section ();
29917 /* Output location list section if necessary. */
29918 if (have_location_lists)
29920 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
29921 char l2[MAX_ARTIFICIAL_LABEL_BYTES];
29922 /* Output the location lists info. */
29923 switch_to_section (debug_loc_section);
29924 if (dwarf_version >= 5)
29926 ASM_GENERATE_INTERNAL_LABEL (l1, DEBUG_LOC_SECTION_LABEL, 1);
29927 ASM_GENERATE_INTERNAL_LABEL (l2, DEBUG_LOC_SECTION_LABEL, 2);
29928 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
29929 dw2_asm_output_data (4, 0xffffffff,
29930 "Initial length escape value indicating "
29931 "64-bit DWARF extension");
29932 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
29933 "Length of Location Lists");
29934 ASM_OUTPUT_LABEL (asm_out_file, l1);
29935 dw2_asm_output_data (2, dwarf_version, "DWARF Version");
29936 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
29937 dw2_asm_output_data (1, 0, "Segment Size");
29938 dw2_asm_output_data (4, dwarf_split_debug_info ? loc_list_idx : 0,
29939 "Offset Entry Count");
29941 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
29942 if (dwarf_version >= 5 && dwarf_split_debug_info)
29944 unsigned int save_loc_list_idx = loc_list_idx;
29945 loc_list_idx = 0;
29946 output_loclists_offsets (comp_unit_die ());
29947 gcc_assert (save_loc_list_idx == loc_list_idx);
29949 output_location_lists (comp_unit_die ());
29950 if (dwarf_version >= 5)
29951 ASM_OUTPUT_LABEL (asm_out_file, l2);
29954 output_pubtables ();
29956 /* Output the address range information if a CU (.debug_info section)
29957 was emitted. We output an empty table even if we had no functions
29958 to put in it. This because the consumer has no way to tell the
29959 difference between an empty table that we omitted and failure to
29960 generate a table that would have contained data. */
29961 if (info_section_emitted)
29963 switch_to_section (debug_aranges_section);
29964 output_aranges ();
29967 /* Output ranges section if necessary. */
29968 if (!vec_safe_is_empty (ranges_table))
29970 if (dwarf_version >= 5)
29971 output_rnglists ();
29972 else
29973 output_ranges ();
29976 /* Have to end the macro section. */
29977 if (have_macinfo)
29979 switch_to_section (debug_macinfo_section);
29980 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
29981 output_macinfo ();
29982 dw2_asm_output_data (1, 0, "End compilation unit");
29985 /* Output the source line correspondence table. We must do this
29986 even if there is no line information. Otherwise, on an empty
29987 translation unit, we will generate a present, but empty,
29988 .debug_info section. IRIX 6.5 `nm' will then complain when
29989 examining the file. This is done late so that any filenames
29990 used by the debug_info section are marked as 'used'. */
29991 switch_to_section (debug_line_section);
29992 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
29993 if (! DWARF2_ASM_LINE_DEBUG_INFO)
29994 output_line_info (false);
29996 if (dwarf_split_debug_info && info_section_emitted)
29998 switch_to_section (debug_skeleton_line_section);
29999 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
30000 output_line_info (true);
30003 /* If we emitted any indirect strings, output the string table too. */
30004 if (debug_str_hash || skeleton_debug_str_hash)
30005 output_indirect_strings ();
30006 if (debug_line_str_hash)
30008 switch_to_section (debug_line_str_section);
30009 const enum dwarf_form form = DW_FORM_line_strp;
30010 debug_line_str_hash->traverse<enum dwarf_form,
30011 output_indirect_string> (form);
30015 /* Returns a hash value for X (which really is a variable_value_struct). */
30017 inline hashval_t
30018 variable_value_hasher::hash (variable_value_struct *x)
30020 return (hashval_t) x->decl_id;
30023 /* Return nonzero if decl_id of variable_value_struct X is the same as
30024 UID of decl Y. */
30026 inline bool
30027 variable_value_hasher::equal (variable_value_struct *x, tree y)
30029 return x->decl_id == DECL_UID (y);
30032 /* Helper function for resolve_variable_value, handle
30033 DW_OP_GNU_variable_value in one location expression.
30034 Return true if exprloc has been changed into loclist. */
30036 static bool
30037 resolve_variable_value_in_expr (dw_attr_node *a, dw_loc_descr_ref loc)
30039 dw_loc_descr_ref next;
30040 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = next)
30042 next = loc->dw_loc_next;
30043 if (loc->dw_loc_opc != DW_OP_GNU_variable_value
30044 || loc->dw_loc_oprnd1.val_class != dw_val_class_decl_ref)
30045 continue;
30047 tree decl = loc->dw_loc_oprnd1.v.val_decl_ref;
30048 if (DECL_CONTEXT (decl) != current_function_decl)
30049 continue;
30051 dw_die_ref ref = lookup_decl_die (decl);
30052 if (ref)
30054 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30055 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
30056 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
30057 continue;
30059 dw_loc_list_ref l = loc_list_from_tree (decl, 0, NULL);
30060 if (l == NULL)
30061 continue;
30062 if (l->dw_loc_next)
30064 if (AT_class (a) != dw_val_class_loc)
30065 continue;
30066 switch (a->dw_attr)
30068 /* Following attributes allow both exprloc and loclist
30069 classes, so we can change them into a loclist. */
30070 case DW_AT_location:
30071 case DW_AT_string_length:
30072 case DW_AT_return_addr:
30073 case DW_AT_data_member_location:
30074 case DW_AT_frame_base:
30075 case DW_AT_segment:
30076 case DW_AT_static_link:
30077 case DW_AT_use_location:
30078 case DW_AT_vtable_elem_location:
30079 if (prev)
30081 prev->dw_loc_next = NULL;
30082 prepend_loc_descr_to_each (l, AT_loc (a));
30084 if (next)
30085 add_loc_descr_to_each (l, next);
30086 a->dw_attr_val.val_class = dw_val_class_loc_list;
30087 a->dw_attr_val.val_entry = NULL;
30088 a->dw_attr_val.v.val_loc_list = l;
30089 have_location_lists = true;
30090 return true;
30091 /* Following attributes allow both exprloc and reference,
30092 so if the whole expression is DW_OP_GNU_variable_value alone
30093 we could transform it into reference. */
30094 case DW_AT_byte_size:
30095 case DW_AT_bit_size:
30096 case DW_AT_lower_bound:
30097 case DW_AT_upper_bound:
30098 case DW_AT_bit_stride:
30099 case DW_AT_count:
30100 case DW_AT_allocated:
30101 case DW_AT_associated:
30102 case DW_AT_byte_stride:
30103 if (prev == NULL && next == NULL)
30104 break;
30105 /* FALLTHRU */
30106 default:
30107 if (dwarf_strict)
30108 continue;
30109 break;
30111 /* Create DW_TAG_variable that we can refer to. */
30112 ref = gen_decl_die (decl, NULL_TREE, NULL,
30113 lookup_decl_die (current_function_decl));
30114 if (ref)
30116 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30117 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
30118 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
30120 continue;
30122 if (prev)
30124 prev->dw_loc_next = l->expr;
30125 add_loc_descr (&prev->dw_loc_next, next);
30126 free_loc_descr (loc, NULL);
30127 next = prev->dw_loc_next;
30129 else
30131 memcpy (loc, l->expr, sizeof (dw_loc_descr_node));
30132 add_loc_descr (&loc, next);
30133 next = loc;
30135 loc = prev;
30137 return false;
30140 /* Attempt to resolve DW_OP_GNU_variable_value using loc_list_from_tree. */
30142 static void
30143 resolve_variable_value (dw_die_ref die)
30145 dw_attr_node *a;
30146 dw_loc_list_ref loc;
30147 unsigned ix;
30149 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
30150 switch (AT_class (a))
30152 case dw_val_class_loc:
30153 if (!resolve_variable_value_in_expr (a, AT_loc (a)))
30154 break;
30155 /* FALLTHRU */
30156 case dw_val_class_loc_list:
30157 loc = AT_loc_list (a);
30158 gcc_assert (loc);
30159 for (; loc; loc = loc->dw_loc_next)
30160 resolve_variable_value_in_expr (a, loc->expr);
30161 break;
30162 default:
30163 break;
30167 /* Attempt to optimize DW_OP_GNU_variable_value refering to
30168 temporaries in the current function. */
30170 static void
30171 resolve_variable_values (void)
30173 if (!variable_value_hash || !current_function_decl)
30174 return;
30176 struct variable_value_struct *node
30177 = variable_value_hash->find_with_hash (current_function_decl,
30178 DECL_UID (current_function_decl));
30180 if (node == NULL)
30181 return;
30183 unsigned int i;
30184 dw_die_ref die;
30185 FOR_EACH_VEC_SAFE_ELT (node->dies, i, die)
30186 resolve_variable_value (die);
30189 /* Helper function for note_variable_value, handle one location
30190 expression. */
30192 static void
30193 note_variable_value_in_expr (dw_die_ref die, dw_loc_descr_ref loc)
30195 for (; loc; loc = loc->dw_loc_next)
30196 if (loc->dw_loc_opc == DW_OP_GNU_variable_value
30197 && loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
30199 tree decl = loc->dw_loc_oprnd1.v.val_decl_ref;
30200 dw_die_ref ref = lookup_decl_die (decl);
30201 if (ref)
30203 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30204 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
30205 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
30207 if (VAR_P (decl)
30208 && DECL_CONTEXT (decl)
30209 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL
30210 && lookup_decl_die (DECL_CONTEXT (decl)))
30212 if (!variable_value_hash)
30213 variable_value_hash
30214 = hash_table<variable_value_hasher>::create_ggc (10);
30216 tree fndecl = DECL_CONTEXT (decl);
30217 struct variable_value_struct *node;
30218 struct variable_value_struct **slot
30219 = variable_value_hash->find_slot_with_hash (fndecl,
30220 DECL_UID (fndecl),
30221 INSERT);
30222 if (*slot == NULL)
30224 node = ggc_cleared_alloc<variable_value_struct> ();
30225 node->decl_id = DECL_UID (fndecl);
30226 *slot = node;
30228 else
30229 node = *slot;
30231 vec_safe_push (node->dies, die);
30236 /* Walk the tree DIE and note DIEs with DW_OP_GNU_variable_value still
30237 with dw_val_class_decl_ref operand. */
30239 static void
30240 note_variable_value (dw_die_ref die)
30242 dw_die_ref c;
30243 dw_attr_node *a;
30244 dw_loc_list_ref loc;
30245 unsigned ix;
30247 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
30248 switch (AT_class (a))
30250 case dw_val_class_loc_list:
30251 loc = AT_loc_list (a);
30252 gcc_assert (loc);
30253 if (!loc->noted_variable_value)
30255 loc->noted_variable_value = 1;
30256 for (; loc; loc = loc->dw_loc_next)
30257 note_variable_value_in_expr (die, loc->expr);
30259 break;
30260 case dw_val_class_loc:
30261 note_variable_value_in_expr (die, AT_loc (a));
30262 break;
30263 default:
30264 break;
30267 /* Mark children. */
30268 FOR_EACH_CHILD (die, c, note_variable_value (c));
30271 /* Perform any cleanups needed after the early debug generation pass
30272 has run. */
30274 static void
30275 dwarf2out_early_finish (const char *filename)
30277 set_early_dwarf s;
30279 /* PCH might result in DW_AT_producer string being restored from the
30280 header compilation, so always fill it with empty string initially
30281 and overwrite only here. */
30282 dw_attr_node *producer = get_AT (comp_unit_die (), DW_AT_producer);
30283 producer_string = gen_producer_string ();
30284 producer->dw_attr_val.v.val_str->refcount--;
30285 producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
30287 /* Add the name for the main input file now. We delayed this from
30288 dwarf2out_init to avoid complications with PCH. */
30289 add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
30290 add_comp_dir_attribute (comp_unit_die ());
30292 /* When emitting DWARF5 .debug_line_str, move DW_AT_name and
30293 DW_AT_comp_dir into .debug_line_str section. */
30294 if (!DWARF2_ASM_LINE_DEBUG_INFO
30295 && dwarf_version >= 5
30296 && DWARF5_USE_DEBUG_LINE_STR)
30298 for (int i = 0; i < 2; i++)
30300 dw_attr_node *a = get_AT (comp_unit_die (),
30301 i ? DW_AT_comp_dir : DW_AT_name);
30302 if (a == NULL
30303 || AT_class (a) != dw_val_class_str
30304 || strlen (AT_string (a)) + 1 <= DWARF_OFFSET_SIZE)
30305 continue;
30307 if (! debug_line_str_hash)
30308 debug_line_str_hash
30309 = hash_table<indirect_string_hasher>::create_ggc (10);
30311 struct indirect_string_node *node
30312 = find_AT_string_in_table (AT_string (a), debug_line_str_hash);
30313 set_indirect_string (node);
30314 node->form = DW_FORM_line_strp;
30315 a->dw_attr_val.v.val_str->refcount--;
30316 a->dw_attr_val.v.val_str = node;
30320 /* With LTO early dwarf was really finished at compile-time, so make
30321 sure to adjust the phase after annotating the LTRANS CU DIE. */
30322 if (in_lto_p)
30324 early_dwarf_finished = true;
30325 return;
30328 /* Walk through the list of incomplete types again, trying once more to
30329 emit full debugging info for them. */
30330 retry_incomplete_types ();
30332 /* The point here is to flush out the limbo list so that it is empty
30333 and we don't need to stream it for LTO. */
30334 flush_limbo_die_list ();
30336 gen_scheduled_generic_parms_dies ();
30337 gen_remaining_tmpl_value_param_die_attribute ();
30339 /* Add DW_AT_linkage_name for all deferred DIEs. */
30340 for (limbo_die_node *node = deferred_asm_name; node; node = node->next)
30342 tree decl = node->created_for;
30343 if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
30344 /* A missing DECL_ASSEMBLER_NAME can be a constant DIE that
30345 ended up in deferred_asm_name before we knew it was
30346 constant and never written to disk. */
30347 && DECL_ASSEMBLER_NAME (decl))
30349 add_linkage_attr (node->die, decl);
30350 move_linkage_attr (node->die);
30353 deferred_asm_name = NULL;
30355 if (flag_eliminate_unused_debug_types)
30356 prune_unused_types ();
30358 /* Generate separate COMDAT sections for type DIEs. */
30359 if (use_debug_types)
30361 break_out_comdat_types (comp_unit_die ());
30363 /* Each new type_unit DIE was added to the limbo die list when created.
30364 Since these have all been added to comdat_type_list, clear the
30365 limbo die list. */
30366 limbo_die_list = NULL;
30368 /* For each new comdat type unit, copy declarations for incomplete
30369 types to make the new unit self-contained (i.e., no direct
30370 references to the main compile unit). */
30371 for (comdat_type_node *ctnode = comdat_type_list;
30372 ctnode != NULL; ctnode = ctnode->next)
30373 copy_decls_for_unworthy_types (ctnode->root_die);
30374 copy_decls_for_unworthy_types (comp_unit_die ());
30376 /* In the process of copying declarations from one unit to another,
30377 we may have left some declarations behind that are no longer
30378 referenced. Prune them. */
30379 prune_unused_types ();
30382 /* Generate separate CUs for each of the include files we've seen.
30383 They will go into limbo_die_list and from there to cu_die_list. */
30384 if (flag_eliminate_dwarf2_dups)
30386 gcc_assert (limbo_die_list == NULL);
30387 break_out_includes (comp_unit_die ());
30388 limbo_die_node *cu;
30389 while ((cu = limbo_die_list))
30391 limbo_die_list = cu->next;
30392 cu->next = cu_die_list;
30393 cu_die_list = cu;
30397 /* Traverse the DIE's and note DIEs with DW_OP_GNU_variable_value still
30398 with dw_val_class_decl_ref operand. */
30399 note_variable_value (comp_unit_die ());
30400 for (limbo_die_node *node = cu_die_list; node; node = node->next)
30401 note_variable_value (node->die);
30402 for (comdat_type_node *ctnode = comdat_type_list; ctnode != NULL;
30403 ctnode = ctnode->next)
30404 note_variable_value (ctnode->root_die);
30405 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
30406 note_variable_value (node->die);
30408 /* The early debug phase is now finished. */
30409 early_dwarf_finished = true;
30412 /* Reset all state within dwarf2out.c so that we can rerun the compiler
30413 within the same process. For use by toplev::finalize. */
30415 void
30416 dwarf2out_c_finalize (void)
30418 last_var_location_insn = NULL;
30419 cached_next_real_insn = NULL;
30420 used_rtx_array = NULL;
30421 incomplete_types = NULL;
30422 decl_scope_table = NULL;
30423 debug_info_section = NULL;
30424 debug_skeleton_info_section = NULL;
30425 debug_abbrev_section = NULL;
30426 debug_skeleton_abbrev_section = NULL;
30427 debug_aranges_section = NULL;
30428 debug_addr_section = NULL;
30429 debug_macinfo_section = NULL;
30430 debug_line_section = NULL;
30431 debug_skeleton_line_section = NULL;
30432 debug_loc_section = NULL;
30433 debug_pubnames_section = NULL;
30434 debug_pubtypes_section = NULL;
30435 debug_str_section = NULL;
30436 debug_line_str_section = NULL;
30437 debug_str_dwo_section = NULL;
30438 debug_str_offsets_section = NULL;
30439 debug_ranges_section = NULL;
30440 debug_frame_section = NULL;
30441 fde_vec = NULL;
30442 debug_str_hash = NULL;
30443 debug_line_str_hash = NULL;
30444 skeleton_debug_str_hash = NULL;
30445 dw2_string_counter = 0;
30446 have_multiple_function_sections = false;
30447 text_section_used = false;
30448 cold_text_section_used = false;
30449 cold_text_section = NULL;
30450 current_unit_personality = NULL;
30452 early_dwarf = false;
30453 early_dwarf_finished = false;
30455 next_die_offset = 0;
30456 single_comp_unit_die = NULL;
30457 comdat_type_list = NULL;
30458 limbo_die_list = NULL;
30459 file_table = NULL;
30460 decl_die_table = NULL;
30461 common_block_die_table = NULL;
30462 decl_loc_table = NULL;
30463 call_arg_locations = NULL;
30464 call_arg_loc_last = NULL;
30465 call_site_count = -1;
30466 tail_call_site_count = -1;
30467 cached_dw_loc_list_table = NULL;
30468 abbrev_die_table = NULL;
30469 delete dwarf_proc_stack_usage_map;
30470 dwarf_proc_stack_usage_map = NULL;
30471 line_info_label_num = 0;
30472 cur_line_info_table = NULL;
30473 text_section_line_info = NULL;
30474 cold_text_section_line_info = NULL;
30475 separate_line_info = NULL;
30476 info_section_emitted = false;
30477 pubname_table = NULL;
30478 pubtype_table = NULL;
30479 macinfo_table = NULL;
30480 ranges_table = NULL;
30481 ranges_by_label = NULL;
30482 rnglist_idx = 0;
30483 have_location_lists = false;
30484 loclabel_num = 0;
30485 poc_label_num = 0;
30486 last_emitted_file = NULL;
30487 label_num = 0;
30488 tmpl_value_parm_die_table = NULL;
30489 generic_type_instances = NULL;
30490 frame_pointer_fb_offset = 0;
30491 frame_pointer_fb_offset_valid = false;
30492 base_types.release ();
30493 XDELETEVEC (producer_string);
30494 producer_string = NULL;
30497 #include "gt-dwarf2out.h"