[ARM] Add source mode to coprocessor pattern SETs
[official-gcc.git] / gcc / dwarf2out.c
blob8c3b1868f69b3ef831a1217be485718291d527e6
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
12468 && TYPE_NAME (qualified_type)
12469 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
12471 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
12473 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
12474 && TYPE_PRECISION (t)
12475 == TYPE_PRECISION (qualified_type)
12476 && TYPE_UNSIGNED (t)
12477 == TYPE_UNSIGNED (qualified_type));
12478 qualified_type = t;
12481 /* If we do, then we can just use its DIE, if it exists. */
12482 if (qualified_type)
12484 mod_type_die = lookup_type_die (qualified_type);
12486 /* DW_AT_endianity doesn't come from a qualifier on the type. */
12487 if (mod_type_die
12488 && (!need_endianity_attribute_p (reverse)
12489 || !is_base_type (type)
12490 || get_AT_unsigned (mod_type_die, DW_AT_endianity)))
12491 return mod_type_die;
12494 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
12496 /* Handle C typedef types. */
12497 if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
12498 && !DECL_ARTIFICIAL (name))
12500 tree dtype = TREE_TYPE (name);
12502 if (qualified_type == dtype)
12504 /* For a named type, use the typedef. */
12505 gen_type_die (qualified_type, context_die);
12506 return lookup_type_die (qualified_type);
12508 else
12510 int dquals = TYPE_QUALS_NO_ADDR_SPACE (dtype);
12511 dquals &= cv_qual_mask;
12512 if ((dquals & ~cv_quals) != TYPE_UNQUALIFIED
12513 || (cv_quals == dquals && DECL_ORIGINAL_TYPE (name) != type))
12514 /* cv-unqualified version of named type. Just use
12515 the unnamed type to which it refers. */
12516 return modified_type_die (DECL_ORIGINAL_TYPE (name), cv_quals,
12517 reverse, context_die);
12518 /* Else cv-qualified version of named type; fall through. */
12522 mod_scope = scope_die_for (type, context_die);
12524 if (cv_quals)
12526 int sub_quals = 0, first_quals = 0;
12527 unsigned i;
12528 dw_die_ref first = NULL, last = NULL;
12530 /* Determine a lesser qualified type that most closely matches
12531 this one. Then generate DW_TAG_* entries for the remaining
12532 qualifiers. */
12533 sub_quals = get_nearest_type_subqualifiers (type, cv_quals,
12534 cv_qual_mask);
12535 if (sub_quals && use_debug_types)
12537 bool needed = false;
12538 /* If emitting type units, make sure the order of qualifiers
12539 is canonical. Thus, start from unqualified type if
12540 an earlier qualifier is missing in sub_quals, but some later
12541 one is present there. */
12542 for (i = 0; i < dwarf_qual_info_size; i++)
12543 if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
12544 needed = true;
12545 else if (needed && (dwarf_qual_info[i].q & cv_quals))
12547 sub_quals = 0;
12548 break;
12551 mod_type_die = modified_type_die (type, sub_quals, reverse, context_die);
12552 if (mod_scope && mod_type_die && mod_type_die->die_parent == mod_scope)
12554 /* As not all intermediate qualified DIEs have corresponding
12555 tree types, ensure that qualified DIEs in the same scope
12556 as their DW_AT_type are emitted after their DW_AT_type,
12557 only with other qualified DIEs for the same type possibly
12558 in between them. Determine the range of such qualified
12559 DIEs now (first being the base type, last being corresponding
12560 last qualified DIE for it). */
12561 unsigned int count = 0;
12562 first = qualified_die_p (mod_type_die, &first_quals,
12563 dwarf_qual_info_size);
12564 if (first == NULL)
12565 first = mod_type_die;
12566 gcc_assert ((first_quals & ~sub_quals) == 0);
12567 for (count = 0, last = first;
12568 count < (1U << dwarf_qual_info_size);
12569 count++, last = last->die_sib)
12571 int quals = 0;
12572 if (last == mod_scope->die_child)
12573 break;
12574 if (qualified_die_p (last->die_sib, &quals, dwarf_qual_info_size)
12575 != first)
12576 break;
12580 for (i = 0; i < dwarf_qual_info_size; i++)
12581 if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
12583 dw_die_ref d;
12584 if (first && first != last)
12586 for (d = first->die_sib; ; d = d->die_sib)
12588 int quals = 0;
12589 qualified_die_p (d, &quals, dwarf_qual_info_size);
12590 if (quals == (first_quals | dwarf_qual_info[i].q))
12591 break;
12592 if (d == last)
12594 d = NULL;
12595 break;
12598 if (d)
12600 mod_type_die = d;
12601 continue;
12604 if (first)
12606 d = ggc_cleared_alloc<die_node> ();
12607 d->die_tag = dwarf_qual_info[i].t;
12608 add_child_die_after (mod_scope, d, last);
12609 last = d;
12611 else
12612 d = new_die (dwarf_qual_info[i].t, mod_scope, type);
12613 if (mod_type_die)
12614 add_AT_die_ref (d, DW_AT_type, mod_type_die);
12615 mod_type_die = d;
12616 first_quals |= dwarf_qual_info[i].q;
12619 else if (code == POINTER_TYPE || code == REFERENCE_TYPE)
12621 dwarf_tag tag = DW_TAG_pointer_type;
12622 if (code == REFERENCE_TYPE)
12624 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
12625 tag = DW_TAG_rvalue_reference_type;
12626 else
12627 tag = DW_TAG_reference_type;
12629 mod_type_die = new_die (tag, mod_scope, type);
12631 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
12632 simple_type_size_in_bits (type) / BITS_PER_UNIT);
12633 add_alignment_attribute (mod_type_die, type);
12634 item_type = TREE_TYPE (type);
12636 addr_space_t as = TYPE_ADDR_SPACE (item_type);
12637 if (!ADDR_SPACE_GENERIC_P (as))
12639 int action = targetm.addr_space.debug (as);
12640 if (action >= 0)
12642 /* Positive values indicate an address_class. */
12643 add_AT_unsigned (mod_type_die, DW_AT_address_class, action);
12645 else
12647 /* Negative values indicate an (inverted) segment base reg. */
12648 dw_loc_descr_ref d
12649 = one_reg_loc_descriptor (~action, VAR_INIT_STATUS_INITIALIZED);
12650 add_AT_loc (mod_type_die, DW_AT_segment, d);
12654 else if (code == INTEGER_TYPE
12655 && TREE_TYPE (type) != NULL_TREE
12656 && subrange_type_for_debug_p (type, &low, &high))
12658 tree bias = NULL_TREE;
12659 if (lang_hooks.types.get_type_bias)
12660 bias = lang_hooks.types.get_type_bias (type);
12661 mod_type_die = subrange_type_die (type, low, high, bias, context_die);
12662 item_type = TREE_TYPE (type);
12664 else if (is_base_type (type))
12665 mod_type_die = base_type_die (type, reverse);
12666 else
12668 gen_type_die (type, context_die);
12670 /* We have to get the type_main_variant here (and pass that to the
12671 `lookup_type_die' routine) because the ..._TYPE node we have
12672 might simply be a *copy* of some original type node (where the
12673 copy was created to help us keep track of typedef names) and
12674 that copy might have a different TYPE_UID from the original
12675 ..._TYPE node. */
12676 if (TREE_CODE (type) == FUNCTION_TYPE
12677 || TREE_CODE (type) == METHOD_TYPE)
12679 /* For function/method types, can't just use type_main_variant here,
12680 because that can have different ref-qualifiers for C++,
12681 but try to canonicalize. */
12682 tree main = TYPE_MAIN_VARIANT (type);
12683 for (tree t = main; t; t = TYPE_NEXT_VARIANT (t))
12684 if (check_base_type (t, main) && check_lang_type (t, type))
12685 return lookup_type_die (t);
12686 return lookup_type_die (type);
12688 else if (TREE_CODE (type) != VECTOR_TYPE
12689 && TREE_CODE (type) != ARRAY_TYPE)
12690 return lookup_type_die (type_main_variant (type));
12691 else
12692 /* Vectors have the debugging information in the type,
12693 not the main variant. */
12694 return lookup_type_die (type);
12697 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
12698 don't output a DW_TAG_typedef, since there isn't one in the
12699 user's program; just attach a DW_AT_name to the type.
12700 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
12701 if the base type already has the same name. */
12702 if (name
12703 && ((TREE_CODE (name) != TYPE_DECL
12704 && (qualified_type == TYPE_MAIN_VARIANT (type)
12705 || (cv_quals == TYPE_UNQUALIFIED)))
12706 || (TREE_CODE (name) == TYPE_DECL
12707 && TREE_TYPE (name) == qualified_type
12708 && DECL_NAME (name))))
12710 if (TREE_CODE (name) == TYPE_DECL)
12711 /* Could just call add_name_and_src_coords_attributes here,
12712 but since this is a builtin type it doesn't have any
12713 useful source coordinates anyway. */
12714 name = DECL_NAME (name);
12715 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
12717 /* This probably indicates a bug. */
12718 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
12720 name = TYPE_IDENTIFIER (type);
12721 add_name_attribute (mod_type_die,
12722 name ? IDENTIFIER_POINTER (name) : "__unknown__");
12725 if (qualified_type)
12726 equate_type_number_to_die (qualified_type, mod_type_die);
12728 if (item_type)
12729 /* We must do this after the equate_type_number_to_die call, in case
12730 this is a recursive type. This ensures that the modified_type_die
12731 recursion will terminate even if the type is recursive. Recursive
12732 types are possible in Ada. */
12733 sub_die = modified_type_die (item_type,
12734 TYPE_QUALS_NO_ADDR_SPACE (item_type),
12735 reverse,
12736 context_die);
12738 if (sub_die != NULL)
12739 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
12741 add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
12742 if (TYPE_ARTIFICIAL (type))
12743 add_AT_flag (mod_type_die, DW_AT_artificial, 1);
12745 return mod_type_die;
12748 /* Generate DIEs for the generic parameters of T.
12749 T must be either a generic type or a generic function.
12750 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
12752 static void
12753 gen_generic_params_dies (tree t)
12755 tree parms, args;
12756 int parms_num, i;
12757 dw_die_ref die = NULL;
12758 int non_default;
12760 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
12761 return;
12763 if (TYPE_P (t))
12764 die = lookup_type_die (t);
12765 else if (DECL_P (t))
12766 die = lookup_decl_die (t);
12768 gcc_assert (die);
12770 parms = lang_hooks.get_innermost_generic_parms (t);
12771 if (!parms)
12772 /* T has no generic parameter. It means T is neither a generic type
12773 or function. End of story. */
12774 return;
12776 parms_num = TREE_VEC_LENGTH (parms);
12777 args = lang_hooks.get_innermost_generic_args (t);
12778 if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
12779 non_default = int_cst_value (TREE_CHAIN (args));
12780 else
12781 non_default = TREE_VEC_LENGTH (args);
12782 for (i = 0; i < parms_num; i++)
12784 tree parm, arg, arg_pack_elems;
12785 dw_die_ref parm_die;
12787 parm = TREE_VEC_ELT (parms, i);
12788 arg = TREE_VEC_ELT (args, i);
12789 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
12790 gcc_assert (parm && TREE_VALUE (parm) && arg);
12792 if (parm && TREE_VALUE (parm) && arg)
12794 /* If PARM represents a template parameter pack,
12795 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
12796 by DW_TAG_template_*_parameter DIEs for the argument
12797 pack elements of ARG. Note that ARG would then be
12798 an argument pack. */
12799 if (arg_pack_elems)
12800 parm_die = template_parameter_pack_die (TREE_VALUE (parm),
12801 arg_pack_elems,
12802 die);
12803 else
12804 parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
12805 true /* emit name */, die);
12806 if (i >= non_default)
12807 add_AT_flag (parm_die, DW_AT_default_value, 1);
12812 /* Create and return a DIE for PARM which should be
12813 the representation of a generic type parameter.
12814 For instance, in the C++ front end, PARM would be a template parameter.
12815 ARG is the argument to PARM.
12816 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
12817 name of the PARM.
12818 PARENT_DIE is the parent DIE which the new created DIE should be added to,
12819 as a child node. */
12821 static dw_die_ref
12822 generic_parameter_die (tree parm, tree arg,
12823 bool emit_name_p,
12824 dw_die_ref parent_die)
12826 dw_die_ref tmpl_die = NULL;
12827 const char *name = NULL;
12829 if (!parm || !DECL_NAME (parm) || !arg)
12830 return NULL;
12832 /* We support non-type generic parameters and arguments,
12833 type generic parameters and arguments, as well as
12834 generic generic parameters (a.k.a. template template parameters in C++)
12835 and arguments. */
12836 if (TREE_CODE (parm) == PARM_DECL)
12837 /* PARM is a nontype generic parameter */
12838 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
12839 else if (TREE_CODE (parm) == TYPE_DECL)
12840 /* PARM is a type generic parameter. */
12841 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
12842 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
12843 /* PARM is a generic generic parameter.
12844 Its DIE is a GNU extension. It shall have a
12845 DW_AT_name attribute to represent the name of the template template
12846 parameter, and a DW_AT_GNU_template_name attribute to represent the
12847 name of the template template argument. */
12848 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
12849 parent_die, parm);
12850 else
12851 gcc_unreachable ();
12853 if (tmpl_die)
12855 tree tmpl_type;
12857 /* If PARM is a generic parameter pack, it means we are
12858 emitting debug info for a template argument pack element.
12859 In other terms, ARG is a template argument pack element.
12860 In that case, we don't emit any DW_AT_name attribute for
12861 the die. */
12862 if (emit_name_p)
12864 name = IDENTIFIER_POINTER (DECL_NAME (parm));
12865 gcc_assert (name);
12866 add_AT_string (tmpl_die, DW_AT_name, name);
12869 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
12871 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
12872 TMPL_DIE should have a child DW_AT_type attribute that is set
12873 to the type of the argument to PARM, which is ARG.
12874 If PARM is a type generic parameter, TMPL_DIE should have a
12875 child DW_AT_type that is set to ARG. */
12876 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
12877 add_type_attribute (tmpl_die, tmpl_type,
12878 (TREE_THIS_VOLATILE (tmpl_type)
12879 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED),
12880 false, parent_die);
12882 else
12884 /* So TMPL_DIE is a DIE representing a
12885 a generic generic template parameter, a.k.a template template
12886 parameter in C++ and arg is a template. */
12888 /* The DW_AT_GNU_template_name attribute of the DIE must be set
12889 to the name of the argument. */
12890 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
12891 if (name)
12892 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
12895 if (TREE_CODE (parm) == PARM_DECL)
12896 /* So PARM is a non-type generic parameter.
12897 DWARF3 5.6.8 says we must set a DW_AT_const_value child
12898 attribute of TMPL_DIE which value represents the value
12899 of ARG.
12900 We must be careful here:
12901 The value of ARG might reference some function decls.
12902 We might currently be emitting debug info for a generic
12903 type and types are emitted before function decls, we don't
12904 know if the function decls referenced by ARG will actually be
12905 emitted after cgraph computations.
12906 So must defer the generation of the DW_AT_const_value to
12907 after cgraph is ready. */
12908 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
12911 return tmpl_die;
12914 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
12915 PARM_PACK must be a template parameter pack. The returned DIE
12916 will be child DIE of PARENT_DIE. */
12918 static dw_die_ref
12919 template_parameter_pack_die (tree parm_pack,
12920 tree parm_pack_args,
12921 dw_die_ref parent_die)
12923 dw_die_ref die;
12924 int j;
12926 gcc_assert (parent_die && parm_pack);
12928 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
12929 add_name_and_src_coords_attributes (die, parm_pack);
12930 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
12931 generic_parameter_die (parm_pack,
12932 TREE_VEC_ELT (parm_pack_args, j),
12933 false /* Don't emit DW_AT_name */,
12934 die);
12935 return die;
12938 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
12939 an enumerated type. */
12941 static inline int
12942 type_is_enum (const_tree type)
12944 return TREE_CODE (type) == ENUMERAL_TYPE;
12947 /* Return the DBX register number described by a given RTL node. */
12949 static unsigned int
12950 dbx_reg_number (const_rtx rtl)
12952 unsigned regno = REGNO (rtl);
12954 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
12956 #ifdef LEAF_REG_REMAP
12957 if (crtl->uses_only_leaf_regs)
12959 int leaf_reg = LEAF_REG_REMAP (regno);
12960 if (leaf_reg != -1)
12961 regno = (unsigned) leaf_reg;
12963 #endif
12965 regno = DBX_REGISTER_NUMBER (regno);
12966 gcc_assert (regno != INVALID_REGNUM);
12967 return regno;
12970 /* Optionally add a DW_OP_piece term to a location description expression.
12971 DW_OP_piece is only added if the location description expression already
12972 doesn't end with DW_OP_piece. */
12974 static void
12975 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
12977 dw_loc_descr_ref loc;
12979 if (*list_head != NULL)
12981 /* Find the end of the chain. */
12982 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
12985 if (loc->dw_loc_opc != DW_OP_piece)
12986 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
12990 /* Return a location descriptor that designates a machine register or
12991 zero if there is none. */
12993 static dw_loc_descr_ref
12994 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
12996 rtx regs;
12998 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
12999 return 0;
13001 /* We only use "frame base" when we're sure we're talking about the
13002 post-prologue local stack frame. We do this by *not* running
13003 register elimination until this point, and recognizing the special
13004 argument pointer and soft frame pointer rtx's.
13005 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
13006 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
13007 && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
13009 dw_loc_descr_ref result = NULL;
13011 if (dwarf_version >= 4 || !dwarf_strict)
13013 result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
13014 initialized);
13015 if (result)
13016 add_loc_descr (&result,
13017 new_loc_descr (DW_OP_stack_value, 0, 0));
13019 return result;
13022 regs = targetm.dwarf_register_span (rtl);
13024 if (REG_NREGS (rtl) > 1 || regs)
13025 return multiple_reg_loc_descriptor (rtl, regs, initialized);
13026 else
13028 unsigned int dbx_regnum = dbx_reg_number (rtl);
13029 if (dbx_regnum == IGNORED_DWARF_REGNUM)
13030 return 0;
13031 return one_reg_loc_descriptor (dbx_regnum, initialized);
13035 /* Return a location descriptor that designates a machine register for
13036 a given hard register number. */
13038 static dw_loc_descr_ref
13039 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
13041 dw_loc_descr_ref reg_loc_descr;
13043 if (regno <= 31)
13044 reg_loc_descr
13045 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
13046 else
13047 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
13049 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13050 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13052 return reg_loc_descr;
13055 /* Given an RTL of a register, return a location descriptor that
13056 designates a value that spans more than one register. */
13058 static dw_loc_descr_ref
13059 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
13060 enum var_init_status initialized)
13062 int size, i;
13063 dw_loc_descr_ref loc_result = NULL;
13065 /* Simple, contiguous registers. */
13066 if (regs == NULL_RTX)
13068 unsigned reg = REGNO (rtl);
13069 int nregs;
13071 #ifdef LEAF_REG_REMAP
13072 if (crtl->uses_only_leaf_regs)
13074 int leaf_reg = LEAF_REG_REMAP (reg);
13075 if (leaf_reg != -1)
13076 reg = (unsigned) leaf_reg;
13078 #endif
13080 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
13081 nregs = REG_NREGS (rtl);
13083 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
13085 loc_result = NULL;
13086 while (nregs--)
13088 dw_loc_descr_ref t;
13090 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
13091 VAR_INIT_STATUS_INITIALIZED);
13092 add_loc_descr (&loc_result, t);
13093 add_loc_descr_op_piece (&loc_result, size);
13094 ++reg;
13096 return loc_result;
13099 /* Now onto stupid register sets in non contiguous locations. */
13101 gcc_assert (GET_CODE (regs) == PARALLEL);
13103 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
13104 loc_result = NULL;
13106 for (i = 0; i < XVECLEN (regs, 0); ++i)
13108 dw_loc_descr_ref t;
13110 t = one_reg_loc_descriptor (dbx_reg_number (XVECEXP (regs, 0, i)),
13111 VAR_INIT_STATUS_INITIALIZED);
13112 add_loc_descr (&loc_result, t);
13113 add_loc_descr_op_piece (&loc_result, size);
13116 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13117 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13118 return loc_result;
13121 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
13123 /* Return a location descriptor that designates a constant i,
13124 as a compound operation from constant (i >> shift), constant shift
13125 and DW_OP_shl. */
13127 static dw_loc_descr_ref
13128 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
13130 dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
13131 add_loc_descr (&ret, int_loc_descriptor (shift));
13132 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
13133 return ret;
13136 /* Return a location descriptor that designates a constant. */
13138 static dw_loc_descr_ref
13139 int_loc_descriptor (HOST_WIDE_INT i)
13141 enum dwarf_location_atom op;
13143 /* Pick the smallest representation of a constant, rather than just
13144 defaulting to the LEB encoding. */
13145 if (i >= 0)
13147 int clz = clz_hwi (i);
13148 int ctz = ctz_hwi (i);
13149 if (i <= 31)
13150 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
13151 else if (i <= 0xff)
13152 op = DW_OP_const1u;
13153 else if (i <= 0xffff)
13154 op = DW_OP_const2u;
13155 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
13156 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
13157 /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
13158 DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
13159 while DW_OP_const4u is 5 bytes. */
13160 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
13161 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13162 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
13163 /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
13164 while DW_OP_const4u is 5 bytes. */
13165 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
13167 else if (DWARF2_ADDR_SIZE == 4 && i > 0x7fffffff
13168 && size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i)
13169 <= 4)
13171 /* As i >= 2**31, the double cast above will yield a negative number.
13172 Since wrapping is defined in DWARF expressions we can output big
13173 positive integers as small negative ones, regardless of the size
13174 of host wide ints.
13176 Here, since the evaluator will handle 32-bit values and since i >=
13177 2**31, we know it's going to be interpreted as a negative literal:
13178 store it this way if we can do better than 5 bytes this way. */
13179 return int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i);
13181 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
13182 op = DW_OP_const4u;
13184 /* Past this point, i >= 0x100000000 and thus DW_OP_constu will take at
13185 least 6 bytes: see if we can do better before falling back to it. */
13186 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13187 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
13188 /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes. */
13189 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
13190 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
13191 && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
13192 >= HOST_BITS_PER_WIDE_INT)
13193 /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
13194 DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes. */
13195 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
13196 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
13197 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
13198 && size_of_uleb128 (i) > 6)
13199 /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes. */
13200 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
13201 else
13202 op = DW_OP_constu;
13204 else
13206 if (i >= -0x80)
13207 op = DW_OP_const1s;
13208 else if (i >= -0x8000)
13209 op = DW_OP_const2s;
13210 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
13212 if (size_of_int_loc_descriptor (i) < 5)
13214 dw_loc_descr_ref ret = int_loc_descriptor (-i);
13215 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
13216 return ret;
13218 op = DW_OP_const4s;
13220 else
13222 if (size_of_int_loc_descriptor (i)
13223 < (unsigned long) 1 + size_of_sleb128 (i))
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_consts;
13233 return new_loc_descr (op, i, 0);
13236 /* Likewise, for unsigned constants. */
13238 static dw_loc_descr_ref
13239 uint_loc_descriptor (unsigned HOST_WIDE_INT i)
13241 const unsigned HOST_WIDE_INT max_int = INTTYPE_MAXIMUM (HOST_WIDE_INT);
13242 const unsigned HOST_WIDE_INT max_uint
13243 = INTTYPE_MAXIMUM (unsigned HOST_WIDE_INT);
13245 /* If possible, use the clever signed constants handling. */
13246 if (i <= max_int)
13247 return int_loc_descriptor ((HOST_WIDE_INT) i);
13249 /* Here, we are left with positive numbers that cannot be represented as
13250 HOST_WIDE_INT, i.e.:
13251 max (HOST_WIDE_INT) < i <= max (unsigned HOST_WIDE_INT)
13253 Using DW_OP_const4/8/./u operation to encode them consumes a lot of bytes
13254 whereas may be better to output a negative integer: thanks to integer
13255 wrapping, we know that:
13256 x = x - 2 ** DWARF2_ADDR_SIZE
13257 = x - 2 * (max (HOST_WIDE_INT) + 1)
13258 So numbers close to max (unsigned HOST_WIDE_INT) could be represented as
13259 small negative integers. Let's try that in cases it will clearly improve
13260 the encoding: there is no gain turning DW_OP_const4u into
13261 DW_OP_const4s. */
13262 if (DWARF2_ADDR_SIZE * 8 == HOST_BITS_PER_WIDE_INT
13263 && ((DWARF2_ADDR_SIZE == 4 && i > max_uint - 0x8000)
13264 || (DWARF2_ADDR_SIZE == 8 && i > max_uint - 0x80000000)))
13266 const unsigned HOST_WIDE_INT first_shift = i - max_int - 1;
13268 /* Now, -1 < first_shift <= max (HOST_WIDE_INT)
13269 i.e. 0 <= first_shift <= max (HOST_WIDE_INT). */
13270 const HOST_WIDE_INT second_shift
13271 = (HOST_WIDE_INT) first_shift - (HOST_WIDE_INT) max_int - 1;
13273 /* So we finally have:
13274 -max (HOST_WIDE_INT) - 1 <= second_shift <= -1.
13275 i.e. min (HOST_WIDE_INT) <= second_shift < 0. */
13276 return int_loc_descriptor (second_shift);
13279 /* Last chance: fallback to a simple constant operation. */
13280 return new_loc_descr
13281 ((HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
13282 ? DW_OP_const4u
13283 : DW_OP_const8u,
13284 i, 0);
13287 /* Generate and return a location description that computes the unsigned
13288 comparison of the two stack top entries (a OP b where b is the top-most
13289 entry and a is the second one). The KIND of comparison can be LT_EXPR,
13290 LE_EXPR, GT_EXPR or GE_EXPR. */
13292 static dw_loc_descr_ref
13293 uint_comparison_loc_list (enum tree_code kind)
13295 enum dwarf_location_atom op, flip_op;
13296 dw_loc_descr_ref ret, bra_node, jmp_node, tmp;
13298 switch (kind)
13300 case LT_EXPR:
13301 op = DW_OP_lt;
13302 break;
13303 case LE_EXPR:
13304 op = DW_OP_le;
13305 break;
13306 case GT_EXPR:
13307 op = DW_OP_gt;
13308 break;
13309 case GE_EXPR:
13310 op = DW_OP_ge;
13311 break;
13312 default:
13313 gcc_unreachable ();
13316 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
13317 jmp_node = new_loc_descr (DW_OP_skip, 0, 0);
13319 /* Until DWARFv4, operations all work on signed integers. It is nevertheless
13320 possible to perform unsigned comparisons: we just have to distinguish
13321 three cases:
13323 1. when a and b have the same sign (as signed integers); then we should
13324 return: a OP(signed) b;
13326 2. when a is a negative signed integer while b is a positive one, then a
13327 is a greater unsigned integer than b; likewise when a and b's roles
13328 are flipped.
13330 So first, compare the sign of the two operands. */
13331 ret = new_loc_descr (DW_OP_over, 0, 0);
13332 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
13333 add_loc_descr (&ret, new_loc_descr (DW_OP_xor, 0, 0));
13334 /* If they have different signs (i.e. they have different sign bits), then
13335 the stack top value has now the sign bit set and thus it's smaller than
13336 zero. */
13337 add_loc_descr (&ret, new_loc_descr (DW_OP_lit0, 0, 0));
13338 add_loc_descr (&ret, new_loc_descr (DW_OP_lt, 0, 0));
13339 add_loc_descr (&ret, bra_node);
13341 /* We are in case 1. At this point, we know both operands have the same
13342 sign, to it's safe to use the built-in signed comparison. */
13343 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
13344 add_loc_descr (&ret, jmp_node);
13346 /* We are in case 2. Here, we know both operands do not have the same sign,
13347 so we have to flip the signed comparison. */
13348 flip_op = (kind == LT_EXPR || kind == LE_EXPR) ? DW_OP_gt : DW_OP_lt;
13349 tmp = new_loc_descr (flip_op, 0, 0);
13350 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13351 bra_node->dw_loc_oprnd1.v.val_loc = tmp;
13352 add_loc_descr (&ret, tmp);
13354 /* This dummy operation is necessary to make the two branches join. */
13355 tmp = new_loc_descr (DW_OP_nop, 0, 0);
13356 jmp_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13357 jmp_node->dw_loc_oprnd1.v.val_loc = tmp;
13358 add_loc_descr (&ret, tmp);
13360 return ret;
13363 /* Likewise, but takes the location description lists (might be destructive on
13364 them). Return NULL if either is NULL or if concatenation fails. */
13366 static dw_loc_list_ref
13367 loc_list_from_uint_comparison (dw_loc_list_ref left, dw_loc_list_ref right,
13368 enum tree_code kind)
13370 if (left == NULL || right == NULL)
13371 return NULL;
13373 add_loc_list (&left, right);
13374 if (left == NULL)
13375 return NULL;
13377 add_loc_descr_to_each (left, uint_comparison_loc_list (kind));
13378 return left;
13381 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
13382 without actually allocating it. */
13384 static unsigned long
13385 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
13387 return size_of_int_loc_descriptor (i >> shift)
13388 + size_of_int_loc_descriptor (shift)
13389 + 1;
13392 /* Return size_of_locs (int_loc_descriptor (i)) without
13393 actually allocating it. */
13395 static unsigned long
13396 size_of_int_loc_descriptor (HOST_WIDE_INT i)
13398 unsigned long s;
13400 if (i >= 0)
13402 int clz, ctz;
13403 if (i <= 31)
13404 return 1;
13405 else if (i <= 0xff)
13406 return 2;
13407 else if (i <= 0xffff)
13408 return 3;
13409 clz = clz_hwi (i);
13410 ctz = ctz_hwi (i);
13411 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
13412 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
13413 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13414 - clz - 5);
13415 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13416 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
13417 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13418 - clz - 8);
13419 else if (DWARF2_ADDR_SIZE == 4 && i > 0x7fffffff
13420 && size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i)
13421 <= 4)
13422 return size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i);
13423 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
13424 return 5;
13425 s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
13426 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13427 && clz + 8 + 255 >= 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 (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
13431 && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
13432 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13433 - clz - 16);
13434 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
13435 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
13436 && s > 6)
13437 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13438 - clz - 32);
13439 else
13440 return 1 + s;
13442 else
13444 if (i >= -0x80)
13445 return 2;
13446 else if (i >= -0x8000)
13447 return 3;
13448 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
13450 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
13452 s = size_of_int_loc_descriptor (-i) + 1;
13453 if (s < 5)
13454 return s;
13456 return 5;
13458 else
13460 unsigned long r = 1 + size_of_sleb128 (i);
13461 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
13463 s = size_of_int_loc_descriptor (-i) + 1;
13464 if (s < r)
13465 return s;
13467 return r;
13472 /* Return loc description representing "address" of integer value.
13473 This can appear only as toplevel expression. */
13475 static dw_loc_descr_ref
13476 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
13478 int litsize;
13479 dw_loc_descr_ref loc_result = NULL;
13481 if (!(dwarf_version >= 4 || !dwarf_strict))
13482 return NULL;
13484 litsize = size_of_int_loc_descriptor (i);
13485 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
13486 is more compact. For DW_OP_stack_value we need:
13487 litsize + 1 (DW_OP_stack_value)
13488 and for DW_OP_implicit_value:
13489 1 (DW_OP_implicit_value) + 1 (length) + size. */
13490 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
13492 loc_result = int_loc_descriptor (i);
13493 add_loc_descr (&loc_result,
13494 new_loc_descr (DW_OP_stack_value, 0, 0));
13495 return loc_result;
13498 loc_result = new_loc_descr (DW_OP_implicit_value,
13499 size, 0);
13500 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
13501 loc_result->dw_loc_oprnd2.v.val_int = i;
13502 return loc_result;
13505 /* Return a location descriptor that designates a base+offset location. */
13507 static dw_loc_descr_ref
13508 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
13509 enum var_init_status initialized)
13511 unsigned int regno;
13512 dw_loc_descr_ref result;
13513 dw_fde_ref fde = cfun->fde;
13515 /* We only use "frame base" when we're sure we're talking about the
13516 post-prologue local stack frame. We do this by *not* running
13517 register elimination until this point, and recognizing the special
13518 argument pointer and soft frame pointer rtx's. */
13519 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
13521 rtx elim = (ira_use_lra_p
13522 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
13523 : eliminate_regs (reg, VOIDmode, NULL_RTX));
13525 if (elim != reg)
13527 if (GET_CODE (elim) == PLUS)
13529 offset += INTVAL (XEXP (elim, 1));
13530 elim = XEXP (elim, 0);
13532 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
13533 && (elim == hard_frame_pointer_rtx
13534 || elim == stack_pointer_rtx))
13535 || elim == (frame_pointer_needed
13536 ? hard_frame_pointer_rtx
13537 : stack_pointer_rtx));
13539 /* If drap register is used to align stack, use frame
13540 pointer + offset to access stack variables. If stack
13541 is aligned without drap, use stack pointer + offset to
13542 access stack variables. */
13543 if (crtl->stack_realign_tried
13544 && reg == frame_pointer_rtx)
13546 int base_reg
13547 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
13548 ? HARD_FRAME_POINTER_REGNUM
13549 : REGNO (elim));
13550 return new_reg_loc_descr (base_reg, offset);
13553 gcc_assert (frame_pointer_fb_offset_valid);
13554 offset += frame_pointer_fb_offset;
13555 return new_loc_descr (DW_OP_fbreg, offset, 0);
13559 regno = REGNO (reg);
13560 #ifdef LEAF_REG_REMAP
13561 if (crtl->uses_only_leaf_regs)
13563 int leaf_reg = LEAF_REG_REMAP (regno);
13564 if (leaf_reg != -1)
13565 regno = (unsigned) leaf_reg;
13567 #endif
13568 regno = DWARF_FRAME_REGNUM (regno);
13570 if (!optimize && fde
13571 && (fde->drap_reg == regno || fde->vdrap_reg == regno))
13573 /* Use cfa+offset to represent the location of arguments passed
13574 on the stack when drap is used to align stack.
13575 Only do this when not optimizing, for optimized code var-tracking
13576 is supposed to track where the arguments live and the register
13577 used as vdrap or drap in some spot might be used for something
13578 else in other part of the routine. */
13579 return new_loc_descr (DW_OP_fbreg, offset, 0);
13582 if (regno <= 31)
13583 result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
13584 offset, 0);
13585 else
13586 result = new_loc_descr (DW_OP_bregx, regno, offset);
13588 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13589 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13591 return result;
13594 /* Return true if this RTL expression describes a base+offset calculation. */
13596 static inline int
13597 is_based_loc (const_rtx rtl)
13599 return (GET_CODE (rtl) == PLUS
13600 && ((REG_P (XEXP (rtl, 0))
13601 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
13602 && CONST_INT_P (XEXP (rtl, 1)))));
13605 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
13606 failed. */
13608 static dw_loc_descr_ref
13609 tls_mem_loc_descriptor (rtx mem)
13611 tree base;
13612 dw_loc_descr_ref loc_result;
13614 if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
13615 return NULL;
13617 base = get_base_address (MEM_EXPR (mem));
13618 if (base == NULL
13619 || !VAR_P (base)
13620 || !DECL_THREAD_LOCAL_P (base))
13621 return NULL;
13623 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1, NULL);
13624 if (loc_result == NULL)
13625 return NULL;
13627 if (MEM_OFFSET (mem))
13628 loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
13630 return loc_result;
13633 /* Output debug info about reason why we failed to expand expression as dwarf
13634 expression. */
13636 static void
13637 expansion_failed (tree expr, rtx rtl, char const *reason)
13639 if (dump_file && (dump_flags & TDF_DETAILS))
13641 fprintf (dump_file, "Failed to expand as dwarf: ");
13642 if (expr)
13643 print_generic_expr (dump_file, expr, dump_flags);
13644 if (rtl)
13646 fprintf (dump_file, "\n");
13647 print_rtl (dump_file, rtl);
13649 fprintf (dump_file, "\nReason: %s\n", reason);
13653 /* Helper function for const_ok_for_output. */
13655 static bool
13656 const_ok_for_output_1 (rtx rtl)
13658 if (GET_CODE (rtl) == UNSPEC)
13660 /* If delegitimize_address couldn't do anything with the UNSPEC, assume
13661 we can't express it in the debug info. */
13662 /* Don't complain about TLS UNSPECs, those are just too hard to
13663 delegitimize. Note this could be a non-decl SYMBOL_REF such as
13664 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
13665 rather than DECL_THREAD_LOCAL_P is not just an optimization. */
13666 if (flag_checking
13667 && (XVECLEN (rtl, 0) == 0
13668 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
13669 || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE))
13670 inform (current_function_decl
13671 ? DECL_SOURCE_LOCATION (current_function_decl)
13672 : UNKNOWN_LOCATION,
13673 #if NUM_UNSPEC_VALUES > 0
13674 "non-delegitimized UNSPEC %s (%d) found in variable location",
13675 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
13676 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
13677 XINT (rtl, 1));
13678 #else
13679 "non-delegitimized UNSPEC %d found in variable location",
13680 XINT (rtl, 1));
13681 #endif
13682 expansion_failed (NULL_TREE, rtl,
13683 "UNSPEC hasn't been delegitimized.\n");
13684 return false;
13687 if (targetm.const_not_ok_for_debug_p (rtl))
13689 expansion_failed (NULL_TREE, rtl,
13690 "Expression rejected for debug by the backend.\n");
13691 return false;
13694 /* FIXME: Refer to PR60655. It is possible for simplification
13695 of rtl expressions in var tracking to produce such expressions.
13696 We should really identify / validate expressions
13697 enclosed in CONST that can be handled by assemblers on various
13698 targets and only handle legitimate cases here. */
13699 if (GET_CODE (rtl) != SYMBOL_REF)
13701 if (GET_CODE (rtl) == NOT)
13702 return false;
13703 return true;
13706 if (CONSTANT_POOL_ADDRESS_P (rtl))
13708 bool marked;
13709 get_pool_constant_mark (rtl, &marked);
13710 /* If all references to this pool constant were optimized away,
13711 it was not output and thus we can't represent it. */
13712 if (!marked)
13714 expansion_failed (NULL_TREE, rtl,
13715 "Constant was removed from constant pool.\n");
13716 return false;
13720 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
13721 return false;
13723 /* Avoid references to external symbols in debug info, on several targets
13724 the linker might even refuse to link when linking a shared library,
13725 and in many other cases the relocations for .debug_info/.debug_loc are
13726 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
13727 to be defined within the same shared library or executable are fine. */
13728 if (SYMBOL_REF_EXTERNAL_P (rtl))
13730 tree decl = SYMBOL_REF_DECL (rtl);
13732 if (decl == NULL || !targetm.binds_local_p (decl))
13734 expansion_failed (NULL_TREE, rtl,
13735 "Symbol not defined in current TU.\n");
13736 return false;
13740 return true;
13743 /* Return true if constant RTL can be emitted in DW_OP_addr or
13744 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
13745 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
13747 static bool
13748 const_ok_for_output (rtx rtl)
13750 if (GET_CODE (rtl) == SYMBOL_REF)
13751 return const_ok_for_output_1 (rtl);
13753 if (GET_CODE (rtl) == CONST)
13755 subrtx_var_iterator::array_type array;
13756 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 0), ALL)
13757 if (!const_ok_for_output_1 (*iter))
13758 return false;
13759 return true;
13762 return true;
13765 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
13766 if possible, NULL otherwise. */
13768 static dw_die_ref
13769 base_type_for_mode (machine_mode mode, bool unsignedp)
13771 dw_die_ref type_die;
13772 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
13774 if (type == NULL)
13775 return NULL;
13776 switch (TREE_CODE (type))
13778 case INTEGER_TYPE:
13779 case REAL_TYPE:
13780 break;
13781 default:
13782 return NULL;
13784 type_die = lookup_type_die (type);
13785 if (!type_die)
13786 type_die = modified_type_die (type, TYPE_UNQUALIFIED, false,
13787 comp_unit_die ());
13788 if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
13789 return NULL;
13790 return type_die;
13793 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
13794 type matching MODE, or, if MODE is narrower than or as wide as
13795 DWARF2_ADDR_SIZE, untyped. Return NULL if the conversion is not
13796 possible. */
13798 static dw_loc_descr_ref
13799 convert_descriptor_to_mode (machine_mode mode, dw_loc_descr_ref op)
13801 machine_mode outer_mode = mode;
13802 dw_die_ref type_die;
13803 dw_loc_descr_ref cvt;
13805 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
13807 add_loc_descr (&op, new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0));
13808 return op;
13810 type_die = base_type_for_mode (outer_mode, 1);
13811 if (type_die == NULL)
13812 return NULL;
13813 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
13814 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13815 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13816 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13817 add_loc_descr (&op, cvt);
13818 return op;
13821 /* Return location descriptor for comparison OP with operands OP0 and OP1. */
13823 static dw_loc_descr_ref
13824 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
13825 dw_loc_descr_ref op1)
13827 dw_loc_descr_ref ret = op0;
13828 add_loc_descr (&ret, op1);
13829 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
13830 if (STORE_FLAG_VALUE != 1)
13832 add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
13833 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
13835 return ret;
13838 /* Return location descriptor for signed comparison OP RTL. */
13840 static dw_loc_descr_ref
13841 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
13842 machine_mode mem_mode)
13844 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
13845 dw_loc_descr_ref op0, op1;
13846 int shift;
13848 if (op_mode == VOIDmode)
13849 op_mode = GET_MODE (XEXP (rtl, 1));
13850 if (op_mode == VOIDmode)
13851 return NULL;
13853 if (dwarf_strict
13854 && dwarf_version < 5
13855 && (!SCALAR_INT_MODE_P (op_mode)
13856 || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
13857 return NULL;
13859 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
13860 VAR_INIT_STATUS_INITIALIZED);
13861 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
13862 VAR_INIT_STATUS_INITIALIZED);
13864 if (op0 == NULL || op1 == NULL)
13865 return NULL;
13867 if (!SCALAR_INT_MODE_P (op_mode)
13868 || GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
13869 return compare_loc_descriptor (op, op0, op1);
13871 if (GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
13873 dw_die_ref type_die = base_type_for_mode (op_mode, 0);
13874 dw_loc_descr_ref cvt;
13876 if (type_die == NULL)
13877 return NULL;
13878 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
13879 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13880 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13881 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13882 add_loc_descr (&op0, cvt);
13883 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
13884 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13885 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13886 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13887 add_loc_descr (&op1, cvt);
13888 return compare_loc_descriptor (op, op0, op1);
13891 shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
13892 /* For eq/ne, if the operands are known to be zero-extended,
13893 there is no need to do the fancy shifting up. */
13894 if (op == DW_OP_eq || op == DW_OP_ne)
13896 dw_loc_descr_ref last0, last1;
13897 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
13899 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
13901 /* deref_size zero extends, and for constants we can check
13902 whether they are zero extended or not. */
13903 if (((last0->dw_loc_opc == DW_OP_deref_size
13904 && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
13905 || (CONST_INT_P (XEXP (rtl, 0))
13906 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
13907 == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
13908 && ((last1->dw_loc_opc == DW_OP_deref_size
13909 && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
13910 || (CONST_INT_P (XEXP (rtl, 1))
13911 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
13912 == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
13913 return compare_loc_descriptor (op, op0, op1);
13915 /* EQ/NE comparison against constant in narrower type than
13916 DWARF2_ADDR_SIZE can be performed either as
13917 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
13918 DW_OP_{eq,ne}
13920 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
13921 DW_OP_{eq,ne}. Pick whatever is shorter. */
13922 if (CONST_INT_P (XEXP (rtl, 1))
13923 && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
13924 && (size_of_int_loc_descriptor (shift) + 1
13925 + size_of_int_loc_descriptor (UINTVAL (XEXP (rtl, 1)) << shift)
13926 >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
13927 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
13928 & GET_MODE_MASK (op_mode))))
13930 add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
13931 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
13932 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
13933 & GET_MODE_MASK (op_mode));
13934 return compare_loc_descriptor (op, op0, op1);
13937 add_loc_descr (&op0, int_loc_descriptor (shift));
13938 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
13939 if (CONST_INT_P (XEXP (rtl, 1)))
13940 op1 = int_loc_descriptor (UINTVAL (XEXP (rtl, 1)) << shift);
13941 else
13943 add_loc_descr (&op1, int_loc_descriptor (shift));
13944 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
13946 return compare_loc_descriptor (op, op0, op1);
13949 /* Return location descriptor for unsigned comparison OP RTL. */
13951 static dw_loc_descr_ref
13952 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
13953 machine_mode mem_mode)
13955 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
13956 dw_loc_descr_ref op0, op1;
13958 if (op_mode == VOIDmode)
13959 op_mode = GET_MODE (XEXP (rtl, 1));
13960 if (op_mode == VOIDmode)
13961 return NULL;
13962 if (!SCALAR_INT_MODE_P (op_mode))
13963 return NULL;
13965 if (dwarf_strict
13966 && dwarf_version < 5
13967 && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
13968 return NULL;
13970 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
13971 VAR_INIT_STATUS_INITIALIZED);
13972 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
13973 VAR_INIT_STATUS_INITIALIZED);
13975 if (op0 == NULL || op1 == NULL)
13976 return NULL;
13978 if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
13980 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
13981 dw_loc_descr_ref last0, last1;
13982 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
13984 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
13986 if (CONST_INT_P (XEXP (rtl, 0)))
13987 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
13988 /* deref_size zero extends, so no need to mask it again. */
13989 else if (last0->dw_loc_opc != DW_OP_deref_size
13990 || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
13992 add_loc_descr (&op0, int_loc_descriptor (mask));
13993 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
13995 if (CONST_INT_P (XEXP (rtl, 1)))
13996 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
13997 /* deref_size zero extends, so no need to mask it again. */
13998 else if (last1->dw_loc_opc != DW_OP_deref_size
13999 || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
14001 add_loc_descr (&op1, int_loc_descriptor (mask));
14002 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
14005 else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
14007 HOST_WIDE_INT bias = 1;
14008 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
14009 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14010 if (CONST_INT_P (XEXP (rtl, 1)))
14011 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
14012 + INTVAL (XEXP (rtl, 1)));
14013 else
14014 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
14015 bias, 0));
14017 return compare_loc_descriptor (op, op0, op1);
14020 /* Return location descriptor for {U,S}{MIN,MAX}. */
14022 static dw_loc_descr_ref
14023 minmax_loc_descriptor (rtx rtl, machine_mode mode,
14024 machine_mode mem_mode)
14026 enum dwarf_location_atom op;
14027 dw_loc_descr_ref op0, op1, ret;
14028 dw_loc_descr_ref bra_node, drop_node;
14030 if (dwarf_strict
14031 && dwarf_version < 5
14032 && (!SCALAR_INT_MODE_P (mode)
14033 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
14034 return NULL;
14036 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14037 VAR_INIT_STATUS_INITIALIZED);
14038 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14039 VAR_INIT_STATUS_INITIALIZED);
14041 if (op0 == NULL || op1 == NULL)
14042 return NULL;
14044 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
14045 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
14046 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
14047 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
14049 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
14051 HOST_WIDE_INT mask = GET_MODE_MASK (mode);
14052 add_loc_descr (&op0, int_loc_descriptor (mask));
14053 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14054 add_loc_descr (&op1, int_loc_descriptor (mask));
14055 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
14057 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
14059 HOST_WIDE_INT bias = 1;
14060 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
14061 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14062 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14065 else if (!SCALAR_INT_MODE_P (mode)
14066 && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
14068 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode)) * BITS_PER_UNIT;
14069 add_loc_descr (&op0, int_loc_descriptor (shift));
14070 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
14071 add_loc_descr (&op1, int_loc_descriptor (shift));
14072 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
14074 else if (SCALAR_INT_MODE_P (mode)
14075 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
14077 dw_die_ref type_die = base_type_for_mode (mode, 0);
14078 dw_loc_descr_ref cvt;
14079 if (type_die == NULL)
14080 return NULL;
14081 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14082 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14083 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14084 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14085 add_loc_descr (&op0, cvt);
14086 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14087 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14088 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14089 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14090 add_loc_descr (&op1, cvt);
14093 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
14094 op = DW_OP_lt;
14095 else
14096 op = DW_OP_gt;
14097 ret = op0;
14098 add_loc_descr (&ret, op1);
14099 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
14100 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14101 add_loc_descr (&ret, bra_node);
14102 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14103 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
14104 add_loc_descr (&ret, drop_node);
14105 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14106 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
14107 if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
14108 && SCALAR_INT_MODE_P (mode)
14109 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
14110 ret = convert_descriptor_to_mode (mode, ret);
14111 return ret;
14114 /* Helper function for mem_loc_descriptor. Perform OP binary op,
14115 but after converting arguments to type_die, afterwards
14116 convert back to unsigned. */
14118 static dw_loc_descr_ref
14119 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
14120 machine_mode mode, machine_mode mem_mode)
14122 dw_loc_descr_ref cvt, op0, op1;
14124 if (type_die == NULL)
14125 return NULL;
14126 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14127 VAR_INIT_STATUS_INITIALIZED);
14128 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14129 VAR_INIT_STATUS_INITIALIZED);
14130 if (op0 == NULL || op1 == NULL)
14131 return NULL;
14132 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14133 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14134 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14135 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14136 add_loc_descr (&op0, cvt);
14137 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14138 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14139 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14140 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14141 add_loc_descr (&op1, cvt);
14142 add_loc_descr (&op0, op1);
14143 add_loc_descr (&op0, new_loc_descr (op, 0, 0));
14144 return convert_descriptor_to_mode (mode, op0);
14147 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
14148 const0 is DW_OP_lit0 or corresponding typed constant,
14149 const1 is DW_OP_lit1 or corresponding typed constant
14150 and constMSB is constant with just the MSB bit set
14151 for the mode):
14152 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
14153 L1: const0 DW_OP_swap
14154 L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
14155 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
14156 L3: DW_OP_drop
14157 L4: DW_OP_nop
14159 CTZ is similar:
14160 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
14161 L1: const0 DW_OP_swap
14162 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
14163 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
14164 L3: DW_OP_drop
14165 L4: DW_OP_nop
14167 FFS is similar:
14168 DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
14169 L1: const1 DW_OP_swap
14170 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
14171 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
14172 L3: DW_OP_drop
14173 L4: DW_OP_nop */
14175 static dw_loc_descr_ref
14176 clz_loc_descriptor (rtx rtl, machine_mode mode,
14177 machine_mode mem_mode)
14179 dw_loc_descr_ref op0, ret, tmp;
14180 HOST_WIDE_INT valv;
14181 dw_loc_descr_ref l1jump, l1label;
14182 dw_loc_descr_ref l2jump, l2label;
14183 dw_loc_descr_ref l3jump, l3label;
14184 dw_loc_descr_ref l4jump, l4label;
14185 rtx msb;
14187 if (!SCALAR_INT_MODE_P (mode)
14188 || GET_MODE (XEXP (rtl, 0)) != mode)
14189 return NULL;
14191 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14192 VAR_INIT_STATUS_INITIALIZED);
14193 if (op0 == NULL)
14194 return NULL;
14195 ret = op0;
14196 if (GET_CODE (rtl) == CLZ)
14198 if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
14199 valv = GET_MODE_BITSIZE (mode);
14201 else if (GET_CODE (rtl) == FFS)
14202 valv = 0;
14203 else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
14204 valv = GET_MODE_BITSIZE (mode);
14205 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
14206 l1jump = new_loc_descr (DW_OP_bra, 0, 0);
14207 add_loc_descr (&ret, l1jump);
14208 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
14209 tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
14210 VAR_INIT_STATUS_INITIALIZED);
14211 if (tmp == NULL)
14212 return NULL;
14213 add_loc_descr (&ret, tmp);
14214 l4jump = new_loc_descr (DW_OP_skip, 0, 0);
14215 add_loc_descr (&ret, l4jump);
14216 l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
14217 ? const1_rtx : const0_rtx,
14218 mode, mem_mode,
14219 VAR_INIT_STATUS_INITIALIZED);
14220 if (l1label == NULL)
14221 return NULL;
14222 add_loc_descr (&ret, l1label);
14223 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14224 l2label = new_loc_descr (DW_OP_dup, 0, 0);
14225 add_loc_descr (&ret, l2label);
14226 if (GET_CODE (rtl) != CLZ)
14227 msb = const1_rtx;
14228 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
14229 msb = GEN_INT (HOST_WIDE_INT_1U
14230 << (GET_MODE_BITSIZE (mode) - 1));
14231 else
14232 msb = immed_wide_int_const
14233 (wi::set_bit_in_zero (GET_MODE_PRECISION (mode) - 1,
14234 GET_MODE_PRECISION (mode)), mode);
14235 if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
14236 tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
14237 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
14238 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
14239 else
14240 tmp = mem_loc_descriptor (msb, mode, mem_mode,
14241 VAR_INIT_STATUS_INITIALIZED);
14242 if (tmp == NULL)
14243 return NULL;
14244 add_loc_descr (&ret, tmp);
14245 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
14246 l3jump = new_loc_descr (DW_OP_bra, 0, 0);
14247 add_loc_descr (&ret, l3jump);
14248 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
14249 VAR_INIT_STATUS_INITIALIZED);
14250 if (tmp == NULL)
14251 return NULL;
14252 add_loc_descr (&ret, tmp);
14253 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
14254 ? DW_OP_shl : DW_OP_shr, 0, 0));
14255 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14256 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
14257 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14258 l2jump = new_loc_descr (DW_OP_skip, 0, 0);
14259 add_loc_descr (&ret, l2jump);
14260 l3label = new_loc_descr (DW_OP_drop, 0, 0);
14261 add_loc_descr (&ret, l3label);
14262 l4label = new_loc_descr (DW_OP_nop, 0, 0);
14263 add_loc_descr (&ret, l4label);
14264 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14265 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
14266 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14267 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
14268 l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14269 l3jump->dw_loc_oprnd1.v.val_loc = l3label;
14270 l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14271 l4jump->dw_loc_oprnd1.v.val_loc = l4label;
14272 return ret;
14275 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
14276 const1 is DW_OP_lit1 or corresponding typed constant):
14277 const0 DW_OP_swap
14278 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
14279 DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
14280 L2: DW_OP_drop
14282 PARITY is similar:
14283 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
14284 DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
14285 L2: DW_OP_drop */
14287 static dw_loc_descr_ref
14288 popcount_loc_descriptor (rtx rtl, machine_mode mode,
14289 machine_mode mem_mode)
14291 dw_loc_descr_ref op0, ret, tmp;
14292 dw_loc_descr_ref l1jump, l1label;
14293 dw_loc_descr_ref l2jump, l2label;
14295 if (!SCALAR_INT_MODE_P (mode)
14296 || GET_MODE (XEXP (rtl, 0)) != mode)
14297 return NULL;
14299 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14300 VAR_INIT_STATUS_INITIALIZED);
14301 if (op0 == NULL)
14302 return NULL;
14303 ret = op0;
14304 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
14305 VAR_INIT_STATUS_INITIALIZED);
14306 if (tmp == NULL)
14307 return NULL;
14308 add_loc_descr (&ret, tmp);
14309 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14310 l1label = new_loc_descr (DW_OP_dup, 0, 0);
14311 add_loc_descr (&ret, l1label);
14312 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
14313 add_loc_descr (&ret, l2jump);
14314 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
14315 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
14316 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
14317 VAR_INIT_STATUS_INITIALIZED);
14318 if (tmp == NULL)
14319 return NULL;
14320 add_loc_descr (&ret, tmp);
14321 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
14322 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
14323 ? DW_OP_plus : DW_OP_xor, 0, 0));
14324 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14325 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
14326 VAR_INIT_STATUS_INITIALIZED);
14327 add_loc_descr (&ret, tmp);
14328 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
14329 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
14330 add_loc_descr (&ret, l1jump);
14331 l2label = new_loc_descr (DW_OP_drop, 0, 0);
14332 add_loc_descr (&ret, l2label);
14333 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14334 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
14335 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14336 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
14337 return ret;
14340 /* BSWAP (constS is initial shift count, either 56 or 24):
14341 constS const0
14342 L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
14343 const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
14344 DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
14345 DW_OP_minus DW_OP_swap DW_OP_skip <L1>
14346 L2: DW_OP_drop DW_OP_swap DW_OP_drop */
14348 static dw_loc_descr_ref
14349 bswap_loc_descriptor (rtx rtl, machine_mode mode,
14350 machine_mode mem_mode)
14352 dw_loc_descr_ref op0, ret, tmp;
14353 dw_loc_descr_ref l1jump, l1label;
14354 dw_loc_descr_ref l2jump, l2label;
14356 if (!SCALAR_INT_MODE_P (mode)
14357 || BITS_PER_UNIT != 8
14358 || (GET_MODE_BITSIZE (mode) != 32
14359 && GET_MODE_BITSIZE (mode) != 64))
14360 return NULL;
14362 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14363 VAR_INIT_STATUS_INITIALIZED);
14364 if (op0 == NULL)
14365 return NULL;
14367 ret = op0;
14368 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
14369 mode, mem_mode,
14370 VAR_INIT_STATUS_INITIALIZED);
14371 if (tmp == NULL)
14372 return NULL;
14373 add_loc_descr (&ret, tmp);
14374 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
14375 VAR_INIT_STATUS_INITIALIZED);
14376 if (tmp == NULL)
14377 return NULL;
14378 add_loc_descr (&ret, tmp);
14379 l1label = new_loc_descr (DW_OP_pick, 2, 0);
14380 add_loc_descr (&ret, l1label);
14381 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
14382 mode, mem_mode,
14383 VAR_INIT_STATUS_INITIALIZED);
14384 add_loc_descr (&ret, tmp);
14385 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
14386 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
14387 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
14388 tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
14389 VAR_INIT_STATUS_INITIALIZED);
14390 if (tmp == NULL)
14391 return NULL;
14392 add_loc_descr (&ret, tmp);
14393 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
14394 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
14395 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
14396 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
14397 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14398 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
14399 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
14400 VAR_INIT_STATUS_INITIALIZED);
14401 add_loc_descr (&ret, tmp);
14402 add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
14403 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
14404 add_loc_descr (&ret, l2jump);
14405 tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
14406 VAR_INIT_STATUS_INITIALIZED);
14407 add_loc_descr (&ret, tmp);
14408 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
14409 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14410 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
14411 add_loc_descr (&ret, l1jump);
14412 l2label = new_loc_descr (DW_OP_drop, 0, 0);
14413 add_loc_descr (&ret, l2label);
14414 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14415 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
14416 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14417 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
14418 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14419 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
14420 return ret;
14423 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
14424 DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
14425 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
14426 DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
14428 ROTATERT is similar:
14429 DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
14430 DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
14431 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or */
14433 static dw_loc_descr_ref
14434 rotate_loc_descriptor (rtx rtl, machine_mode mode,
14435 machine_mode mem_mode)
14437 rtx rtlop1 = XEXP (rtl, 1);
14438 dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
14439 int i;
14441 if (!SCALAR_INT_MODE_P (mode))
14442 return NULL;
14444 if (GET_MODE (rtlop1) != VOIDmode
14445 && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
14446 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
14447 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14448 VAR_INIT_STATUS_INITIALIZED);
14449 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
14450 VAR_INIT_STATUS_INITIALIZED);
14451 if (op0 == NULL || op1 == NULL)
14452 return NULL;
14453 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
14454 for (i = 0; i < 2; i++)
14456 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
14457 mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
14458 mode, mem_mode,
14459 VAR_INIT_STATUS_INITIALIZED);
14460 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
14461 mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
14462 ? DW_OP_const4u
14463 : HOST_BITS_PER_WIDE_INT == 64
14464 ? DW_OP_const8u : DW_OP_constu,
14465 GET_MODE_MASK (mode), 0);
14466 else
14467 mask[i] = NULL;
14468 if (mask[i] == NULL)
14469 return NULL;
14470 add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
14472 ret = op0;
14473 add_loc_descr (&ret, op1);
14474 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
14475 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
14476 if (GET_CODE (rtl) == ROTATERT)
14478 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
14479 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
14480 GET_MODE_BITSIZE (mode), 0));
14482 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
14483 if (mask[0] != NULL)
14484 add_loc_descr (&ret, mask[0]);
14485 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
14486 if (mask[1] != NULL)
14488 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14489 add_loc_descr (&ret, mask[1]);
14490 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14492 if (GET_CODE (rtl) == ROTATE)
14494 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
14495 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
14496 GET_MODE_BITSIZE (mode), 0));
14498 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
14499 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
14500 return ret;
14503 /* Helper function for mem_loc_descriptor. Return DW_OP_GNU_parameter_ref
14504 for DEBUG_PARAMETER_REF RTL. */
14506 static dw_loc_descr_ref
14507 parameter_ref_descriptor (rtx rtl)
14509 dw_loc_descr_ref ret;
14510 dw_die_ref ref;
14512 if (dwarf_strict)
14513 return NULL;
14514 gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
14515 ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
14516 ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
14517 if (ref)
14519 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14520 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
14521 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
14523 else
14525 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
14526 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
14528 return ret;
14531 /* The following routine converts the RTL for a variable or parameter
14532 (resident in memory) into an equivalent Dwarf representation of a
14533 mechanism for getting the address of that same variable onto the top of a
14534 hypothetical "address evaluation" stack.
14536 When creating memory location descriptors, we are effectively transforming
14537 the RTL for a memory-resident object into its Dwarf postfix expression
14538 equivalent. This routine recursively descends an RTL tree, turning
14539 it into Dwarf postfix code as it goes.
14541 MODE is the mode that should be assumed for the rtl if it is VOIDmode.
14543 MEM_MODE is the mode of the memory reference, needed to handle some
14544 autoincrement addressing modes.
14546 Return 0 if we can't represent the location. */
14548 dw_loc_descr_ref
14549 mem_loc_descriptor (rtx rtl, machine_mode mode,
14550 machine_mode mem_mode,
14551 enum var_init_status initialized)
14553 dw_loc_descr_ref mem_loc_result = NULL;
14554 enum dwarf_location_atom op;
14555 dw_loc_descr_ref op0, op1;
14556 rtx inner = NULL_RTX;
14558 if (mode == VOIDmode)
14559 mode = GET_MODE (rtl);
14561 /* Note that for a dynamically sized array, the location we will generate a
14562 description of here will be the lowest numbered location which is
14563 actually within the array. That's *not* necessarily the same as the
14564 zeroth element of the array. */
14566 rtl = targetm.delegitimize_address (rtl);
14568 if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
14569 return NULL;
14571 switch (GET_CODE (rtl))
14573 case POST_INC:
14574 case POST_DEC:
14575 case POST_MODIFY:
14576 return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
14578 case SUBREG:
14579 /* The case of a subreg may arise when we have a local (register)
14580 variable or a formal (register) parameter which doesn't quite fill
14581 up an entire register. For now, just assume that it is
14582 legitimate to make the Dwarf info refer to the whole register which
14583 contains the given subreg. */
14584 if (!subreg_lowpart_p (rtl))
14585 break;
14586 inner = SUBREG_REG (rtl);
14587 /* FALLTHRU */
14588 case TRUNCATE:
14589 if (inner == NULL_RTX)
14590 inner = XEXP (rtl, 0);
14591 if (SCALAR_INT_MODE_P (mode)
14592 && SCALAR_INT_MODE_P (GET_MODE (inner))
14593 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
14594 #ifdef POINTERS_EXTEND_UNSIGNED
14595 || (mode == Pmode && mem_mode != VOIDmode)
14596 #endif
14598 && GET_MODE_SIZE (GET_MODE (inner)) <= DWARF2_ADDR_SIZE)
14600 mem_loc_result = mem_loc_descriptor (inner,
14601 GET_MODE (inner),
14602 mem_mode, initialized);
14603 break;
14605 if (dwarf_strict && dwarf_version < 5)
14606 break;
14607 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (inner)))
14608 break;
14609 if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (inner))
14610 && (!SCALAR_INT_MODE_P (mode)
14611 || !SCALAR_INT_MODE_P (GET_MODE (inner))))
14612 break;
14613 else
14615 dw_die_ref type_die;
14616 dw_loc_descr_ref cvt;
14618 mem_loc_result = mem_loc_descriptor (inner,
14619 GET_MODE (inner),
14620 mem_mode, initialized);
14621 if (mem_loc_result == NULL)
14622 break;
14623 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
14624 if (type_die == NULL)
14626 mem_loc_result = NULL;
14627 break;
14629 if (GET_MODE_SIZE (mode)
14630 != GET_MODE_SIZE (GET_MODE (inner)))
14631 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14632 else
14633 cvt = new_loc_descr (dwarf_OP (DW_OP_reinterpret), 0, 0);
14634 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14635 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14636 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14637 add_loc_descr (&mem_loc_result, cvt);
14638 if (SCALAR_INT_MODE_P (mode)
14639 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
14641 /* Convert it to untyped afterwards. */
14642 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14643 add_loc_descr (&mem_loc_result, cvt);
14646 break;
14648 case REG:
14649 if (! SCALAR_INT_MODE_P (mode)
14650 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
14651 && rtl != arg_pointer_rtx
14652 && rtl != frame_pointer_rtx
14653 #ifdef POINTERS_EXTEND_UNSIGNED
14654 && (mode != Pmode || mem_mode == VOIDmode)
14655 #endif
14658 dw_die_ref type_die;
14659 unsigned int dbx_regnum;
14661 if (dwarf_strict && dwarf_version < 5)
14662 break;
14663 if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
14664 break;
14665 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
14666 if (type_die == NULL)
14667 break;
14669 dbx_regnum = dbx_reg_number (rtl);
14670 if (dbx_regnum == IGNORED_DWARF_REGNUM)
14671 break;
14672 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_regval_type),
14673 dbx_regnum, 0);
14674 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
14675 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
14676 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
14677 break;
14679 /* Whenever a register number forms a part of the description of the
14680 method for calculating the (dynamic) address of a memory resident
14681 object, DWARF rules require the register number be referred to as
14682 a "base register". This distinction is not based in any way upon
14683 what category of register the hardware believes the given register
14684 belongs to. This is strictly DWARF terminology we're dealing with
14685 here. Note that in cases where the location of a memory-resident
14686 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
14687 OP_CONST (0)) the actual DWARF location descriptor that we generate
14688 may just be OP_BASEREG (basereg). This may look deceptively like
14689 the object in question was allocated to a register (rather than in
14690 memory) so DWARF consumers need to be aware of the subtle
14691 distinction between OP_REG and OP_BASEREG. */
14692 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
14693 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
14694 else if (stack_realign_drap
14695 && crtl->drap_reg
14696 && crtl->args.internal_arg_pointer == rtl
14697 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
14699 /* If RTL is internal_arg_pointer, which has been optimized
14700 out, use DRAP instead. */
14701 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
14702 VAR_INIT_STATUS_INITIALIZED);
14704 break;
14706 case SIGN_EXTEND:
14707 case ZERO_EXTEND:
14708 if (!SCALAR_INT_MODE_P (mode))
14709 break;
14710 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
14711 mem_mode, VAR_INIT_STATUS_INITIALIZED);
14712 if (op0 == 0)
14713 break;
14714 else if (GET_CODE (rtl) == ZERO_EXTEND
14715 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
14716 && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
14717 < HOST_BITS_PER_WIDE_INT
14718 /* If DW_OP_const{1,2,4}u won't be used, it is shorter
14719 to expand zero extend as two shifts instead of
14720 masking. */
14721 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= 4)
14723 machine_mode imode = GET_MODE (XEXP (rtl, 0));
14724 mem_loc_result = op0;
14725 add_loc_descr (&mem_loc_result,
14726 int_loc_descriptor (GET_MODE_MASK (imode)));
14727 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
14729 else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
14731 int shift = DWARF2_ADDR_SIZE
14732 - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
14733 shift *= BITS_PER_UNIT;
14734 if (GET_CODE (rtl) == SIGN_EXTEND)
14735 op = DW_OP_shra;
14736 else
14737 op = DW_OP_shr;
14738 mem_loc_result = op0;
14739 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
14740 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
14741 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
14742 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14744 else if (!dwarf_strict || dwarf_version >= 5)
14746 dw_die_ref type_die1, type_die2;
14747 dw_loc_descr_ref cvt;
14749 type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
14750 GET_CODE (rtl) == ZERO_EXTEND);
14751 if (type_die1 == NULL)
14752 break;
14753 type_die2 = base_type_for_mode (mode, 1);
14754 if (type_die2 == NULL)
14755 break;
14756 mem_loc_result = op0;
14757 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14758 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14759 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
14760 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14761 add_loc_descr (&mem_loc_result, cvt);
14762 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14763 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14764 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
14765 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14766 add_loc_descr (&mem_loc_result, cvt);
14768 break;
14770 case MEM:
14772 rtx new_rtl = avoid_constant_pool_reference (rtl);
14773 if (new_rtl != rtl)
14775 mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
14776 initialized);
14777 if (mem_loc_result != NULL)
14778 return mem_loc_result;
14781 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
14782 get_address_mode (rtl), mode,
14783 VAR_INIT_STATUS_INITIALIZED);
14784 if (mem_loc_result == NULL)
14785 mem_loc_result = tls_mem_loc_descriptor (rtl);
14786 if (mem_loc_result != NULL)
14788 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
14789 || !SCALAR_INT_MODE_P(mode))
14791 dw_die_ref type_die;
14792 dw_loc_descr_ref deref;
14794 if (dwarf_strict && dwarf_version < 5)
14795 return NULL;
14796 type_die
14797 = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
14798 if (type_die == NULL)
14799 return NULL;
14800 deref = new_loc_descr (dwarf_OP (DW_OP_deref_type),
14801 GET_MODE_SIZE (mode), 0);
14802 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
14803 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
14804 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
14805 add_loc_descr (&mem_loc_result, deref);
14807 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
14808 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
14809 else
14810 add_loc_descr (&mem_loc_result,
14811 new_loc_descr (DW_OP_deref_size,
14812 GET_MODE_SIZE (mode), 0));
14814 break;
14816 case LO_SUM:
14817 return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
14819 case LABEL_REF:
14820 /* Some ports can transform a symbol ref into a label ref, because
14821 the symbol ref is too far away and has to be dumped into a constant
14822 pool. */
14823 case CONST:
14824 case SYMBOL_REF:
14825 if (!SCALAR_INT_MODE_P (mode)
14826 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
14827 #ifdef POINTERS_EXTEND_UNSIGNED
14828 && (mode != Pmode || mem_mode == VOIDmode)
14829 #endif
14831 break;
14832 if (GET_CODE (rtl) == SYMBOL_REF
14833 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
14835 dw_loc_descr_ref temp;
14837 /* If this is not defined, we have no way to emit the data. */
14838 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
14839 break;
14841 temp = new_addr_loc_descr (rtl, dtprel_true);
14843 /* We check for DWARF 5 here because gdb did not implement
14844 DW_OP_form_tls_address until after 7.12. */
14845 mem_loc_result = new_loc_descr ((dwarf_version >= 5
14846 ? DW_OP_form_tls_address
14847 : DW_OP_GNU_push_tls_address),
14848 0, 0);
14849 add_loc_descr (&mem_loc_result, temp);
14851 break;
14854 if (!const_ok_for_output (rtl))
14856 if (GET_CODE (rtl) == CONST)
14857 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14858 initialized);
14859 break;
14862 symref:
14863 mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
14864 vec_safe_push (used_rtx_array, rtl);
14865 break;
14867 case CONCAT:
14868 case CONCATN:
14869 case VAR_LOCATION:
14870 case DEBUG_IMPLICIT_PTR:
14871 expansion_failed (NULL_TREE, rtl,
14872 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
14873 return 0;
14875 case ENTRY_VALUE:
14876 if (dwarf_strict && dwarf_version < 5)
14877 return NULL;
14878 if (REG_P (ENTRY_VALUE_EXP (rtl)))
14880 if (!SCALAR_INT_MODE_P (mode)
14881 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
14882 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
14883 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14884 else
14886 unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
14887 if (dbx_regnum == IGNORED_DWARF_REGNUM)
14888 return NULL;
14889 op0 = one_reg_loc_descriptor (dbx_regnum,
14890 VAR_INIT_STATUS_INITIALIZED);
14893 else if (MEM_P (ENTRY_VALUE_EXP (rtl))
14894 && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
14896 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
14897 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14898 if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
14899 return NULL;
14901 else
14902 gcc_unreachable ();
14903 if (op0 == NULL)
14904 return NULL;
14905 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_entry_value), 0, 0);
14906 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
14907 mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
14908 break;
14910 case DEBUG_PARAMETER_REF:
14911 mem_loc_result = parameter_ref_descriptor (rtl);
14912 break;
14914 case PRE_MODIFY:
14915 /* Extract the PLUS expression nested inside and fall into
14916 PLUS code below. */
14917 rtl = XEXP (rtl, 1);
14918 goto plus;
14920 case PRE_INC:
14921 case PRE_DEC:
14922 /* Turn these into a PLUS expression and fall into the PLUS code
14923 below. */
14924 rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
14925 gen_int_mode (GET_CODE (rtl) == PRE_INC
14926 ? GET_MODE_UNIT_SIZE (mem_mode)
14927 : -GET_MODE_UNIT_SIZE (mem_mode),
14928 mode));
14930 /* fall through */
14932 case PLUS:
14933 plus:
14934 if (is_based_loc (rtl)
14935 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
14936 || XEXP (rtl, 0) == arg_pointer_rtx
14937 || XEXP (rtl, 0) == frame_pointer_rtx)
14938 && SCALAR_INT_MODE_P (mode))
14939 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
14940 INTVAL (XEXP (rtl, 1)),
14941 VAR_INIT_STATUS_INITIALIZED);
14942 else
14944 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14945 VAR_INIT_STATUS_INITIALIZED);
14946 if (mem_loc_result == 0)
14947 break;
14949 if (CONST_INT_P (XEXP (rtl, 1))
14950 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
14951 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
14952 else
14954 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14955 VAR_INIT_STATUS_INITIALIZED);
14956 if (op1 == 0)
14957 return NULL;
14958 add_loc_descr (&mem_loc_result, op1);
14959 add_loc_descr (&mem_loc_result,
14960 new_loc_descr (DW_OP_plus, 0, 0));
14963 break;
14965 /* If a pseudo-reg is optimized away, it is possible for it to
14966 be replaced with a MEM containing a multiply or shift. */
14967 case MINUS:
14968 op = DW_OP_minus;
14969 goto do_binop;
14971 case MULT:
14972 op = DW_OP_mul;
14973 goto do_binop;
14975 case DIV:
14976 if ((!dwarf_strict || dwarf_version >= 5)
14977 && SCALAR_INT_MODE_P (mode)
14978 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
14980 mem_loc_result = typed_binop (DW_OP_div, rtl,
14981 base_type_for_mode (mode, 0),
14982 mode, mem_mode);
14983 break;
14985 op = DW_OP_div;
14986 goto do_binop;
14988 case UMOD:
14989 op = DW_OP_mod;
14990 goto do_binop;
14992 case ASHIFT:
14993 op = DW_OP_shl;
14994 goto do_shift;
14996 case ASHIFTRT:
14997 op = DW_OP_shra;
14998 goto do_shift;
15000 case LSHIFTRT:
15001 op = DW_OP_shr;
15002 goto do_shift;
15004 do_shift:
15005 if (!SCALAR_INT_MODE_P (mode))
15006 break;
15007 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15008 VAR_INIT_STATUS_INITIALIZED);
15010 rtx rtlop1 = XEXP (rtl, 1);
15011 if (GET_MODE (rtlop1) != VOIDmode
15012 && GET_MODE_BITSIZE (GET_MODE (rtlop1))
15013 < GET_MODE_BITSIZE (mode))
15014 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
15015 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
15016 VAR_INIT_STATUS_INITIALIZED);
15019 if (op0 == 0 || op1 == 0)
15020 break;
15022 mem_loc_result = op0;
15023 add_loc_descr (&mem_loc_result, op1);
15024 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15025 break;
15027 case AND:
15028 op = DW_OP_and;
15029 goto do_binop;
15031 case IOR:
15032 op = DW_OP_or;
15033 goto do_binop;
15035 case XOR:
15036 op = DW_OP_xor;
15037 goto do_binop;
15039 do_binop:
15040 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15041 VAR_INIT_STATUS_INITIALIZED);
15042 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15043 VAR_INIT_STATUS_INITIALIZED);
15045 if (op0 == 0 || op1 == 0)
15046 break;
15048 mem_loc_result = op0;
15049 add_loc_descr (&mem_loc_result, op1);
15050 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15051 break;
15053 case MOD:
15054 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
15055 && (!dwarf_strict || dwarf_version >= 5))
15057 mem_loc_result = typed_binop (DW_OP_mod, rtl,
15058 base_type_for_mode (mode, 0),
15059 mode, mem_mode);
15060 break;
15063 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15064 VAR_INIT_STATUS_INITIALIZED);
15065 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15066 VAR_INIT_STATUS_INITIALIZED);
15068 if (op0 == 0 || op1 == 0)
15069 break;
15071 mem_loc_result = op0;
15072 add_loc_descr (&mem_loc_result, op1);
15073 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
15074 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
15075 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
15076 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
15077 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
15078 break;
15080 case UDIV:
15081 if ((!dwarf_strict || dwarf_version >= 5)
15082 && SCALAR_INT_MODE_P (mode))
15084 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
15086 op = DW_OP_div;
15087 goto do_binop;
15089 mem_loc_result = typed_binop (DW_OP_div, rtl,
15090 base_type_for_mode (mode, 1),
15091 mode, mem_mode);
15093 break;
15095 case NOT:
15096 op = DW_OP_not;
15097 goto do_unop;
15099 case ABS:
15100 op = DW_OP_abs;
15101 goto do_unop;
15103 case NEG:
15104 op = DW_OP_neg;
15105 goto do_unop;
15107 do_unop:
15108 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15109 VAR_INIT_STATUS_INITIALIZED);
15111 if (op0 == 0)
15112 break;
15114 mem_loc_result = op0;
15115 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15116 break;
15118 case CONST_INT:
15119 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
15120 #ifdef POINTERS_EXTEND_UNSIGNED
15121 || (mode == Pmode
15122 && mem_mode != VOIDmode
15123 && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
15124 #endif
15127 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
15128 break;
15130 if ((!dwarf_strict || dwarf_version >= 5)
15131 && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
15132 || GET_MODE_BITSIZE (mode) == HOST_BITS_PER_DOUBLE_INT))
15134 dw_die_ref type_die = base_type_for_mode (mode, 1);
15135 machine_mode amode;
15136 if (type_die == NULL)
15137 return NULL;
15138 amode = mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT,
15139 MODE_INT, 0);
15140 if (INTVAL (rtl) >= 0
15141 && amode != BLKmode
15142 && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
15143 /* const DW_OP_convert <XXX> vs.
15144 DW_OP_const_type <XXX, 1, const>. */
15145 && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
15146 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (mode))
15148 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
15149 op0 = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15150 op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15151 op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15152 op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
15153 add_loc_descr (&mem_loc_result, op0);
15154 return mem_loc_result;
15156 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0,
15157 INTVAL (rtl));
15158 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15159 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15160 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
15161 if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
15162 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
15163 else
15165 mem_loc_result->dw_loc_oprnd2.val_class
15166 = dw_val_class_const_double;
15167 mem_loc_result->dw_loc_oprnd2.v.val_double
15168 = double_int::from_shwi (INTVAL (rtl));
15171 break;
15173 case CONST_DOUBLE:
15174 if (!dwarf_strict || dwarf_version >= 5)
15176 dw_die_ref type_die;
15178 /* Note that if TARGET_SUPPORTS_WIDE_INT == 0, a
15179 CONST_DOUBLE rtx could represent either a large integer
15180 or a floating-point constant. If TARGET_SUPPORTS_WIDE_INT != 0,
15181 the value is always a floating point constant.
15183 When it is an integer, a CONST_DOUBLE is used whenever
15184 the constant requires 2 HWIs to be adequately represented.
15185 We output CONST_DOUBLEs as blocks. */
15186 if (mode == VOIDmode
15187 || (GET_MODE (rtl) == VOIDmode
15188 && GET_MODE_BITSIZE (mode) != HOST_BITS_PER_DOUBLE_INT))
15189 break;
15190 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
15191 if (type_die == NULL)
15192 return NULL;
15193 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0, 0);
15194 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15195 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15196 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
15197 #if TARGET_SUPPORTS_WIDE_INT == 0
15198 if (!SCALAR_FLOAT_MODE_P (mode))
15200 mem_loc_result->dw_loc_oprnd2.val_class
15201 = dw_val_class_const_double;
15202 mem_loc_result->dw_loc_oprnd2.v.val_double
15203 = rtx_to_double_int (rtl);
15205 else
15206 #endif
15208 unsigned int length = GET_MODE_SIZE (mode);
15209 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
15211 insert_float (rtl, array);
15212 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
15213 mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
15214 mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
15215 mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
15218 break;
15220 case CONST_WIDE_INT:
15221 if (!dwarf_strict || dwarf_version >= 5)
15223 dw_die_ref type_die;
15225 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
15226 if (type_die == NULL)
15227 return NULL;
15228 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0, 0);
15229 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15230 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15231 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
15232 mem_loc_result->dw_loc_oprnd2.val_class
15233 = dw_val_class_wide_int;
15234 mem_loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
15235 *mem_loc_result->dw_loc_oprnd2.v.val_wide = rtx_mode_t (rtl, mode);
15237 break;
15239 case EQ:
15240 mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
15241 break;
15243 case GE:
15244 mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
15245 break;
15247 case GT:
15248 mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
15249 break;
15251 case LE:
15252 mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
15253 break;
15255 case LT:
15256 mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
15257 break;
15259 case NE:
15260 mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
15261 break;
15263 case GEU:
15264 mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
15265 break;
15267 case GTU:
15268 mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
15269 break;
15271 case LEU:
15272 mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
15273 break;
15275 case LTU:
15276 mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
15277 break;
15279 case UMIN:
15280 case UMAX:
15281 if (!SCALAR_INT_MODE_P (mode))
15282 break;
15283 /* FALLTHRU */
15284 case SMIN:
15285 case SMAX:
15286 mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
15287 break;
15289 case ZERO_EXTRACT:
15290 case SIGN_EXTRACT:
15291 if (CONST_INT_P (XEXP (rtl, 1))
15292 && CONST_INT_P (XEXP (rtl, 2))
15293 && ((unsigned) INTVAL (XEXP (rtl, 1))
15294 + (unsigned) INTVAL (XEXP (rtl, 2))
15295 <= GET_MODE_BITSIZE (mode))
15296 && SCALAR_INT_MODE_P (mode)
15297 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
15298 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
15300 int shift, size;
15301 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
15302 mem_mode, VAR_INIT_STATUS_INITIALIZED);
15303 if (op0 == 0)
15304 break;
15305 if (GET_CODE (rtl) == SIGN_EXTRACT)
15306 op = DW_OP_shra;
15307 else
15308 op = DW_OP_shr;
15309 mem_loc_result = op0;
15310 size = INTVAL (XEXP (rtl, 1));
15311 shift = INTVAL (XEXP (rtl, 2));
15312 if (BITS_BIG_ENDIAN)
15313 shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
15314 - shift - size;
15315 if (shift + size != (int) DWARF2_ADDR_SIZE)
15317 add_loc_descr (&mem_loc_result,
15318 int_loc_descriptor (DWARF2_ADDR_SIZE
15319 - shift - size));
15320 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
15322 if (size != (int) DWARF2_ADDR_SIZE)
15324 add_loc_descr (&mem_loc_result,
15325 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
15326 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15329 break;
15331 case IF_THEN_ELSE:
15333 dw_loc_descr_ref op2, bra_node, drop_node;
15334 op0 = mem_loc_descriptor (XEXP (rtl, 0),
15335 GET_MODE (XEXP (rtl, 0)) == VOIDmode
15336 ? word_mode : GET_MODE (XEXP (rtl, 0)),
15337 mem_mode, VAR_INIT_STATUS_INITIALIZED);
15338 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15339 VAR_INIT_STATUS_INITIALIZED);
15340 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
15341 VAR_INIT_STATUS_INITIALIZED);
15342 if (op0 == NULL || op1 == NULL || op2 == NULL)
15343 break;
15345 mem_loc_result = op1;
15346 add_loc_descr (&mem_loc_result, op2);
15347 add_loc_descr (&mem_loc_result, op0);
15348 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
15349 add_loc_descr (&mem_loc_result, bra_node);
15350 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
15351 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
15352 add_loc_descr (&mem_loc_result, drop_node);
15353 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15354 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
15356 break;
15358 case FLOAT_EXTEND:
15359 case FLOAT_TRUNCATE:
15360 case FLOAT:
15361 case UNSIGNED_FLOAT:
15362 case FIX:
15363 case UNSIGNED_FIX:
15364 if (!dwarf_strict || dwarf_version >= 5)
15366 dw_die_ref type_die;
15367 dw_loc_descr_ref cvt;
15369 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
15370 mem_mode, VAR_INIT_STATUS_INITIALIZED);
15371 if (op0 == NULL)
15372 break;
15373 if (SCALAR_INT_MODE_P (GET_MODE (XEXP (rtl, 0)))
15374 && (GET_CODE (rtl) == FLOAT
15375 || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
15376 <= DWARF2_ADDR_SIZE))
15378 type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
15379 GET_CODE (rtl) == UNSIGNED_FLOAT);
15380 if (type_die == NULL)
15381 break;
15382 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15383 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15384 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15385 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15386 add_loc_descr (&op0, cvt);
15388 type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
15389 if (type_die == NULL)
15390 break;
15391 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15392 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15393 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15394 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15395 add_loc_descr (&op0, cvt);
15396 if (SCALAR_INT_MODE_P (mode)
15397 && (GET_CODE (rtl) == FIX
15398 || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
15400 op0 = convert_descriptor_to_mode (mode, op0);
15401 if (op0 == NULL)
15402 break;
15404 mem_loc_result = op0;
15406 break;
15408 case CLZ:
15409 case CTZ:
15410 case FFS:
15411 mem_loc_result = clz_loc_descriptor (rtl, mode, mem_mode);
15412 break;
15414 case POPCOUNT:
15415 case PARITY:
15416 mem_loc_result = popcount_loc_descriptor (rtl, mode, mem_mode);
15417 break;
15419 case BSWAP:
15420 mem_loc_result = bswap_loc_descriptor (rtl, mode, mem_mode);
15421 break;
15423 case ROTATE:
15424 case ROTATERT:
15425 mem_loc_result = rotate_loc_descriptor (rtl, mode, mem_mode);
15426 break;
15428 case COMPARE:
15429 /* In theory, we could implement the above. */
15430 /* DWARF cannot represent the unsigned compare operations
15431 natively. */
15432 case SS_MULT:
15433 case US_MULT:
15434 case SS_DIV:
15435 case US_DIV:
15436 case SS_PLUS:
15437 case US_PLUS:
15438 case SS_MINUS:
15439 case US_MINUS:
15440 case SS_NEG:
15441 case US_NEG:
15442 case SS_ABS:
15443 case SS_ASHIFT:
15444 case US_ASHIFT:
15445 case SS_TRUNCATE:
15446 case US_TRUNCATE:
15447 case UNORDERED:
15448 case ORDERED:
15449 case UNEQ:
15450 case UNGE:
15451 case UNGT:
15452 case UNLE:
15453 case UNLT:
15454 case LTGT:
15455 case FRACT_CONVERT:
15456 case UNSIGNED_FRACT_CONVERT:
15457 case SAT_FRACT:
15458 case UNSIGNED_SAT_FRACT:
15459 case SQRT:
15460 case ASM_OPERANDS:
15461 case VEC_MERGE:
15462 case VEC_SELECT:
15463 case VEC_CONCAT:
15464 case VEC_DUPLICATE:
15465 case UNSPEC:
15466 case HIGH:
15467 case FMA:
15468 case STRICT_LOW_PART:
15469 case CONST_VECTOR:
15470 case CONST_FIXED:
15471 case CLRSB:
15472 case CLOBBER:
15473 /* If delegitimize_address couldn't do anything with the UNSPEC, we
15474 can't express it in the debug info. This can happen e.g. with some
15475 TLS UNSPECs. */
15476 break;
15478 case CONST_STRING:
15479 resolve_one_addr (&rtl);
15480 goto symref;
15482 /* RTL sequences inside PARALLEL record a series of DWARF operations for
15483 the expression. An UNSPEC rtx represents a raw DWARF operation,
15484 new_loc_descr is called for it to build the operation directly.
15485 Otherwise mem_loc_descriptor is called recursively. */
15486 case PARALLEL:
15488 int index = 0;
15489 dw_loc_descr_ref exp_result = NULL;
15491 for (; index < XVECLEN (rtl, 0); index++)
15493 rtx elem = XVECEXP (rtl, 0, index);
15494 if (GET_CODE (elem) == UNSPEC)
15496 /* Each DWARF operation UNSPEC contain two operands, if
15497 one operand is not used for the operation, const0_rtx is
15498 passed. */
15499 gcc_assert (XVECLEN (elem, 0) == 2);
15501 HOST_WIDE_INT dw_op = XINT (elem, 1);
15502 HOST_WIDE_INT oprnd1 = INTVAL (XVECEXP (elem, 0, 0));
15503 HOST_WIDE_INT oprnd2 = INTVAL (XVECEXP (elem, 0, 1));
15504 exp_result
15505 = new_loc_descr ((enum dwarf_location_atom) dw_op, oprnd1,
15506 oprnd2);
15508 else
15509 exp_result
15510 = mem_loc_descriptor (elem, mode, mem_mode,
15511 VAR_INIT_STATUS_INITIALIZED);
15513 if (!mem_loc_result)
15514 mem_loc_result = exp_result;
15515 else
15516 add_loc_descr (&mem_loc_result, exp_result);
15519 break;
15522 default:
15523 if (flag_checking)
15525 print_rtl (stderr, rtl);
15526 gcc_unreachable ();
15528 break;
15531 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
15532 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
15534 return mem_loc_result;
15537 /* Return a descriptor that describes the concatenation of two locations.
15538 This is typically a complex variable. */
15540 static dw_loc_descr_ref
15541 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
15543 dw_loc_descr_ref cc_loc_result = NULL;
15544 dw_loc_descr_ref x0_ref
15545 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15546 dw_loc_descr_ref x1_ref
15547 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15549 if (x0_ref == 0 || x1_ref == 0)
15550 return 0;
15552 cc_loc_result = x0_ref;
15553 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
15555 add_loc_descr (&cc_loc_result, x1_ref);
15556 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
15558 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
15559 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
15561 return cc_loc_result;
15564 /* Return a descriptor that describes the concatenation of N
15565 locations. */
15567 static dw_loc_descr_ref
15568 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
15570 unsigned int i;
15571 dw_loc_descr_ref cc_loc_result = NULL;
15572 unsigned int n = XVECLEN (concatn, 0);
15574 for (i = 0; i < n; ++i)
15576 dw_loc_descr_ref ref;
15577 rtx x = XVECEXP (concatn, 0, i);
15579 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15580 if (ref == NULL)
15581 return NULL;
15583 add_loc_descr (&cc_loc_result, ref);
15584 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
15587 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
15588 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
15590 return cc_loc_result;
15593 /* Helper function for loc_descriptor. Return DW_OP_implicit_pointer
15594 for DEBUG_IMPLICIT_PTR RTL. */
15596 static dw_loc_descr_ref
15597 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
15599 dw_loc_descr_ref ret;
15600 dw_die_ref ref;
15602 if (dwarf_strict && dwarf_version < 5)
15603 return NULL;
15604 gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
15605 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
15606 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
15607 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
15608 ret = new_loc_descr (dwarf_OP (DW_OP_implicit_pointer), 0, offset);
15609 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
15610 if (ref)
15612 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15613 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
15614 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
15616 else
15618 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
15619 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
15621 return ret;
15624 /* Output a proper Dwarf location descriptor for a variable or parameter
15625 which is either allocated in a register or in a memory location. For a
15626 register, we just generate an OP_REG and the register number. For a
15627 memory location we provide a Dwarf postfix expression describing how to
15628 generate the (dynamic) address of the object onto the address stack.
15630 MODE is mode of the decl if this loc_descriptor is going to be used in
15631 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
15632 allowed, VOIDmode otherwise.
15634 If we don't know how to describe it, return 0. */
15636 static dw_loc_descr_ref
15637 loc_descriptor (rtx rtl, machine_mode mode,
15638 enum var_init_status initialized)
15640 dw_loc_descr_ref loc_result = NULL;
15642 switch (GET_CODE (rtl))
15644 case SUBREG:
15645 /* The case of a subreg may arise when we have a local (register)
15646 variable or a formal (register) parameter which doesn't quite fill
15647 up an entire register. For now, just assume that it is
15648 legitimate to make the Dwarf info refer to the whole register which
15649 contains the given subreg. */
15650 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
15651 loc_result = loc_descriptor (SUBREG_REG (rtl),
15652 GET_MODE (SUBREG_REG (rtl)), initialized);
15653 else
15654 goto do_default;
15655 break;
15657 case REG:
15658 loc_result = reg_loc_descriptor (rtl, initialized);
15659 break;
15661 case MEM:
15662 loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
15663 GET_MODE (rtl), initialized);
15664 if (loc_result == NULL)
15665 loc_result = tls_mem_loc_descriptor (rtl);
15666 if (loc_result == NULL)
15668 rtx new_rtl = avoid_constant_pool_reference (rtl);
15669 if (new_rtl != rtl)
15670 loc_result = loc_descriptor (new_rtl, mode, initialized);
15672 break;
15674 case CONCAT:
15675 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
15676 initialized);
15677 break;
15679 case CONCATN:
15680 loc_result = concatn_loc_descriptor (rtl, initialized);
15681 break;
15683 case VAR_LOCATION:
15684 /* Single part. */
15685 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
15687 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
15688 if (GET_CODE (loc) == EXPR_LIST)
15689 loc = XEXP (loc, 0);
15690 loc_result = loc_descriptor (loc, mode, initialized);
15691 break;
15694 rtl = XEXP (rtl, 1);
15695 /* FALLTHRU */
15697 case PARALLEL:
15699 rtvec par_elems = XVEC (rtl, 0);
15700 int num_elem = GET_NUM_ELEM (par_elems);
15701 machine_mode mode;
15702 int i;
15704 /* Create the first one, so we have something to add to. */
15705 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
15706 VOIDmode, initialized);
15707 if (loc_result == NULL)
15708 return NULL;
15709 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
15710 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
15711 for (i = 1; i < num_elem; i++)
15713 dw_loc_descr_ref temp;
15715 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
15716 VOIDmode, initialized);
15717 if (temp == NULL)
15718 return NULL;
15719 add_loc_descr (&loc_result, temp);
15720 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
15721 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
15724 break;
15726 case CONST_INT:
15727 if (mode != VOIDmode && mode != BLKmode)
15728 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
15729 INTVAL (rtl));
15730 break;
15732 case CONST_DOUBLE:
15733 if (mode == VOIDmode)
15734 mode = GET_MODE (rtl);
15736 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
15738 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
15740 /* Note that a CONST_DOUBLE rtx could represent either an integer
15741 or a floating-point constant. A CONST_DOUBLE is used whenever
15742 the constant requires more than one word in order to be
15743 adequately represented. We output CONST_DOUBLEs as blocks. */
15744 loc_result = new_loc_descr (DW_OP_implicit_value,
15745 GET_MODE_SIZE (mode), 0);
15746 #if TARGET_SUPPORTS_WIDE_INT == 0
15747 if (!SCALAR_FLOAT_MODE_P (mode))
15749 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
15750 loc_result->dw_loc_oprnd2.v.val_double
15751 = rtx_to_double_int (rtl);
15753 else
15754 #endif
15756 unsigned int length = GET_MODE_SIZE (mode);
15757 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
15759 insert_float (rtl, array);
15760 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
15761 loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
15762 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
15763 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
15766 break;
15768 case CONST_WIDE_INT:
15769 if (mode == VOIDmode)
15770 mode = GET_MODE (rtl);
15772 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
15774 loc_result = new_loc_descr (DW_OP_implicit_value,
15775 GET_MODE_SIZE (mode), 0);
15776 loc_result->dw_loc_oprnd2.val_class = dw_val_class_wide_int;
15777 loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
15778 *loc_result->dw_loc_oprnd2.v.val_wide = rtx_mode_t (rtl, mode);
15780 break;
15782 case CONST_VECTOR:
15783 if (mode == VOIDmode)
15784 mode = GET_MODE (rtl);
15786 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
15788 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
15789 unsigned int length = CONST_VECTOR_NUNITS (rtl);
15790 unsigned char *array
15791 = ggc_vec_alloc<unsigned char> (length * elt_size);
15792 unsigned int i;
15793 unsigned char *p;
15794 machine_mode imode = GET_MODE_INNER (mode);
15796 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
15797 switch (GET_MODE_CLASS (mode))
15799 case MODE_VECTOR_INT:
15800 for (i = 0, p = array; i < length; i++, p += elt_size)
15802 rtx elt = CONST_VECTOR_ELT (rtl, i);
15803 insert_wide_int (rtx_mode_t (elt, imode), p, elt_size);
15805 break;
15807 case MODE_VECTOR_FLOAT:
15808 for (i = 0, p = array; i < length; i++, p += elt_size)
15810 rtx elt = CONST_VECTOR_ELT (rtl, i);
15811 insert_float (elt, p);
15813 break;
15815 default:
15816 gcc_unreachable ();
15819 loc_result = new_loc_descr (DW_OP_implicit_value,
15820 length * elt_size, 0);
15821 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
15822 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
15823 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
15824 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
15826 break;
15828 case CONST:
15829 if (mode == VOIDmode
15830 || CONST_SCALAR_INT_P (XEXP (rtl, 0))
15831 || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
15832 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
15834 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
15835 break;
15837 /* FALLTHROUGH */
15838 case SYMBOL_REF:
15839 if (!const_ok_for_output (rtl))
15840 break;
15841 /* FALLTHROUGH */
15842 case LABEL_REF:
15843 if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
15844 && (dwarf_version >= 4 || !dwarf_strict))
15846 loc_result = new_addr_loc_descr (rtl, dtprel_false);
15847 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
15848 vec_safe_push (used_rtx_array, rtl);
15850 break;
15852 case DEBUG_IMPLICIT_PTR:
15853 loc_result = implicit_ptr_descriptor (rtl, 0);
15854 break;
15856 case PLUS:
15857 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
15858 && CONST_INT_P (XEXP (rtl, 1)))
15860 loc_result
15861 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
15862 break;
15864 /* FALLTHRU */
15865 do_default:
15866 default:
15867 if ((SCALAR_INT_MODE_P (mode)
15868 && GET_MODE (rtl) == mode
15869 && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
15870 && dwarf_version >= 4)
15871 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
15873 /* Value expression. */
15874 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
15875 if (loc_result)
15876 add_loc_descr (&loc_result,
15877 new_loc_descr (DW_OP_stack_value, 0, 0));
15879 break;
15882 return loc_result;
15885 /* We need to figure out what section we should use as the base for the
15886 address ranges where a given location is valid.
15887 1. If this particular DECL has a section associated with it, use that.
15888 2. If this function has a section associated with it, use that.
15889 3. Otherwise, use the text section.
15890 XXX: If you split a variable across multiple sections, we won't notice. */
15892 static const char *
15893 secname_for_decl (const_tree decl)
15895 const char *secname;
15897 if (VAR_OR_FUNCTION_DECL_P (decl)
15898 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl) || TREE_STATIC (decl))
15899 && DECL_SECTION_NAME (decl))
15900 secname = DECL_SECTION_NAME (decl);
15901 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
15902 secname = DECL_SECTION_NAME (current_function_decl);
15903 else if (cfun && in_cold_section_p)
15904 secname = crtl->subsections.cold_section_label;
15905 else
15906 secname = text_section_label;
15908 return secname;
15911 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
15913 static bool
15914 decl_by_reference_p (tree decl)
15916 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
15917 || VAR_P (decl))
15918 && DECL_BY_REFERENCE (decl));
15921 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
15922 for VARLOC. */
15924 static dw_loc_descr_ref
15925 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
15926 enum var_init_status initialized)
15928 int have_address = 0;
15929 dw_loc_descr_ref descr;
15930 machine_mode mode;
15932 if (want_address != 2)
15934 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
15935 /* Single part. */
15936 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
15938 varloc = PAT_VAR_LOCATION_LOC (varloc);
15939 if (GET_CODE (varloc) == EXPR_LIST)
15940 varloc = XEXP (varloc, 0);
15941 mode = GET_MODE (varloc);
15942 if (MEM_P (varloc))
15944 rtx addr = XEXP (varloc, 0);
15945 descr = mem_loc_descriptor (addr, get_address_mode (varloc),
15946 mode, initialized);
15947 if (descr)
15948 have_address = 1;
15949 else
15951 rtx x = avoid_constant_pool_reference (varloc);
15952 if (x != varloc)
15953 descr = mem_loc_descriptor (x, mode, VOIDmode,
15954 initialized);
15957 else
15958 descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
15960 else
15961 return 0;
15963 else
15965 if (GET_CODE (varloc) == VAR_LOCATION)
15966 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
15967 else
15968 mode = DECL_MODE (loc);
15969 descr = loc_descriptor (varloc, mode, initialized);
15970 have_address = 1;
15973 if (!descr)
15974 return 0;
15976 if (want_address == 2 && !have_address
15977 && (dwarf_version >= 4 || !dwarf_strict))
15979 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
15981 expansion_failed (loc, NULL_RTX,
15982 "DWARF address size mismatch");
15983 return 0;
15985 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
15986 have_address = 1;
15988 /* Show if we can't fill the request for an address. */
15989 if (want_address && !have_address)
15991 expansion_failed (loc, NULL_RTX,
15992 "Want address and only have value");
15993 return 0;
15996 /* If we've got an address and don't want one, dereference. */
15997 if (!want_address && have_address)
15999 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
16000 enum dwarf_location_atom op;
16002 if (size > DWARF2_ADDR_SIZE || size == -1)
16004 expansion_failed (loc, NULL_RTX,
16005 "DWARF address size mismatch");
16006 return 0;
16008 else if (size == DWARF2_ADDR_SIZE)
16009 op = DW_OP_deref;
16010 else
16011 op = DW_OP_deref_size;
16013 add_loc_descr (&descr, new_loc_descr (op, size, 0));
16016 return descr;
16019 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
16020 if it is not possible. */
16022 static dw_loc_descr_ref
16023 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
16025 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
16026 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
16027 else if (dwarf_version >= 3 || !dwarf_strict)
16028 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
16029 else
16030 return NULL;
16033 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
16034 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
16036 static dw_loc_descr_ref
16037 dw_sra_loc_expr (tree decl, rtx loc)
16039 rtx p;
16040 unsigned HOST_WIDE_INT padsize = 0;
16041 dw_loc_descr_ref descr, *descr_tail;
16042 unsigned HOST_WIDE_INT decl_size;
16043 rtx varloc;
16044 enum var_init_status initialized;
16046 if (DECL_SIZE (decl) == NULL
16047 || !tree_fits_uhwi_p (DECL_SIZE (decl)))
16048 return NULL;
16050 decl_size = tree_to_uhwi (DECL_SIZE (decl));
16051 descr = NULL;
16052 descr_tail = &descr;
16054 for (p = loc; p; p = XEXP (p, 1))
16056 unsigned HOST_WIDE_INT bitsize = decl_piece_bitsize (p);
16057 rtx loc_note = *decl_piece_varloc_ptr (p);
16058 dw_loc_descr_ref cur_descr;
16059 dw_loc_descr_ref *tail, last = NULL;
16060 unsigned HOST_WIDE_INT opsize = 0;
16062 if (loc_note == NULL_RTX
16063 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
16065 padsize += bitsize;
16066 continue;
16068 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
16069 varloc = NOTE_VAR_LOCATION (loc_note);
16070 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
16071 if (cur_descr == NULL)
16073 padsize += bitsize;
16074 continue;
16077 /* Check that cur_descr either doesn't use
16078 DW_OP_*piece operations, or their sum is equal
16079 to bitsize. Otherwise we can't embed it. */
16080 for (tail = &cur_descr; *tail != NULL;
16081 tail = &(*tail)->dw_loc_next)
16082 if ((*tail)->dw_loc_opc == DW_OP_piece)
16084 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
16085 * BITS_PER_UNIT;
16086 last = *tail;
16088 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
16090 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
16091 last = *tail;
16094 if (last != NULL && opsize != bitsize)
16096 padsize += bitsize;
16097 /* Discard the current piece of the descriptor and release any
16098 addr_table entries it uses. */
16099 remove_loc_list_addr_table_entries (cur_descr);
16100 continue;
16103 /* If there is a hole, add DW_OP_*piece after empty DWARF
16104 expression, which means that those bits are optimized out. */
16105 if (padsize)
16107 if (padsize > decl_size)
16109 remove_loc_list_addr_table_entries (cur_descr);
16110 goto discard_descr;
16112 decl_size -= padsize;
16113 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
16114 if (*descr_tail == NULL)
16116 remove_loc_list_addr_table_entries (cur_descr);
16117 goto discard_descr;
16119 descr_tail = &(*descr_tail)->dw_loc_next;
16120 padsize = 0;
16122 *descr_tail = cur_descr;
16123 descr_tail = tail;
16124 if (bitsize > decl_size)
16125 goto discard_descr;
16126 decl_size -= bitsize;
16127 if (last == NULL)
16129 HOST_WIDE_INT offset = 0;
16130 if (GET_CODE (varloc) == VAR_LOCATION
16131 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
16133 varloc = PAT_VAR_LOCATION_LOC (varloc);
16134 if (GET_CODE (varloc) == EXPR_LIST)
16135 varloc = XEXP (varloc, 0);
16139 if (GET_CODE (varloc) == CONST
16140 || GET_CODE (varloc) == SIGN_EXTEND
16141 || GET_CODE (varloc) == ZERO_EXTEND)
16142 varloc = XEXP (varloc, 0);
16143 else if (GET_CODE (varloc) == SUBREG)
16144 varloc = SUBREG_REG (varloc);
16145 else
16146 break;
16148 while (1);
16149 /* DW_OP_bit_size offset should be zero for register
16150 or implicit location descriptions and empty location
16151 descriptions, but for memory addresses needs big endian
16152 adjustment. */
16153 if (MEM_P (varloc))
16155 unsigned HOST_WIDE_INT memsize
16156 = MEM_SIZE (varloc) * BITS_PER_UNIT;
16157 if (memsize != bitsize)
16159 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
16160 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
16161 goto discard_descr;
16162 if (memsize < bitsize)
16163 goto discard_descr;
16164 if (BITS_BIG_ENDIAN)
16165 offset = memsize - bitsize;
16169 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
16170 if (*descr_tail == NULL)
16171 goto discard_descr;
16172 descr_tail = &(*descr_tail)->dw_loc_next;
16176 /* If there were any non-empty expressions, add padding till the end of
16177 the decl. */
16178 if (descr != NULL && decl_size != 0)
16180 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
16181 if (*descr_tail == NULL)
16182 goto discard_descr;
16184 return descr;
16186 discard_descr:
16187 /* Discard the descriptor and release any addr_table entries it uses. */
16188 remove_loc_list_addr_table_entries (descr);
16189 return NULL;
16192 /* Return the dwarf representation of the location list LOC_LIST of
16193 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
16194 function. */
16196 static dw_loc_list_ref
16197 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
16199 const char *endname, *secname;
16200 rtx varloc;
16201 enum var_init_status initialized;
16202 struct var_loc_node *node;
16203 dw_loc_descr_ref descr;
16204 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
16205 dw_loc_list_ref list = NULL;
16206 dw_loc_list_ref *listp = &list;
16208 /* Now that we know what section we are using for a base,
16209 actually construct the list of locations.
16210 The first location information is what is passed to the
16211 function that creates the location list, and the remaining
16212 locations just get added on to that list.
16213 Note that we only know the start address for a location
16214 (IE location changes), so to build the range, we use
16215 the range [current location start, next location start].
16216 This means we have to special case the last node, and generate
16217 a range of [last location start, end of function label]. */
16219 secname = secname_for_decl (decl);
16221 for (node = loc_list->first; node; node = node->next)
16222 if (GET_CODE (node->loc) == EXPR_LIST
16223 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
16225 if (GET_CODE (node->loc) == EXPR_LIST)
16227 /* This requires DW_OP_{,bit_}piece, which is not usable
16228 inside DWARF expressions. */
16229 if (want_address != 2)
16230 continue;
16231 descr = dw_sra_loc_expr (decl, node->loc);
16232 if (descr == NULL)
16233 continue;
16235 else
16237 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
16238 varloc = NOTE_VAR_LOCATION (node->loc);
16239 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
16241 if (descr)
16243 bool range_across_switch = false;
16244 /* If section switch happens in between node->label
16245 and node->next->label (or end of function) and
16246 we can't emit it as a single entry list,
16247 emit two ranges, first one ending at the end
16248 of first partition and second one starting at the
16249 beginning of second partition. */
16250 if (node == loc_list->last_before_switch
16251 && (node != loc_list->first || loc_list->first->next)
16252 && current_function_decl)
16254 endname = cfun->fde->dw_fde_end;
16255 range_across_switch = true;
16257 /* The variable has a location between NODE->LABEL and
16258 NODE->NEXT->LABEL. */
16259 else if (node->next)
16260 endname = node->next->label;
16261 /* If the variable has a location at the last label
16262 it keeps its location until the end of function. */
16263 else if (!current_function_decl)
16264 endname = text_end_label;
16265 else
16267 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
16268 current_function_funcdef_no);
16269 endname = ggc_strdup (label_id);
16272 *listp = new_loc_list (descr, node->label, endname, secname);
16273 if (TREE_CODE (decl) == PARM_DECL
16274 && node == loc_list->first
16275 && NOTE_P (node->loc)
16276 && strcmp (node->label, endname) == 0)
16277 (*listp)->force = true;
16278 listp = &(*listp)->dw_loc_next;
16280 if (range_across_switch)
16282 if (GET_CODE (node->loc) == EXPR_LIST)
16283 descr = dw_sra_loc_expr (decl, node->loc);
16284 else
16286 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
16287 varloc = NOTE_VAR_LOCATION (node->loc);
16288 descr = dw_loc_list_1 (decl, varloc, want_address,
16289 initialized);
16291 gcc_assert (descr);
16292 /* The variable has a location between NODE->LABEL and
16293 NODE->NEXT->LABEL. */
16294 if (node->next)
16295 endname = node->next->label;
16296 else
16297 endname = cfun->fde->dw_fde_second_end;
16298 *listp = new_loc_list (descr,
16299 cfun->fde->dw_fde_second_begin,
16300 endname, secname);
16301 listp = &(*listp)->dw_loc_next;
16306 /* Try to avoid the overhead of a location list emitting a location
16307 expression instead, but only if we didn't have more than one
16308 location entry in the first place. If some entries were not
16309 representable, we don't want to pretend a single entry that was
16310 applies to the entire scope in which the variable is
16311 available. */
16312 if (list && loc_list->first->next)
16313 gen_llsym (list);
16315 return list;
16318 /* Return if the loc_list has only single element and thus can be represented
16319 as location description. */
16321 static bool
16322 single_element_loc_list_p (dw_loc_list_ref list)
16324 gcc_assert (!list->dw_loc_next || list->ll_symbol);
16325 return !list->ll_symbol;
16328 /* Duplicate a single element of location list. */
16330 static inline dw_loc_descr_ref
16331 copy_loc_descr (dw_loc_descr_ref ref)
16333 dw_loc_descr_ref copy = ggc_alloc<dw_loc_descr_node> ();
16334 memcpy (copy, ref, sizeof (dw_loc_descr_node));
16335 return copy;
16338 /* To each location in list LIST append loc descr REF. */
16340 static void
16341 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
16343 dw_loc_descr_ref copy;
16344 add_loc_descr (&list->expr, ref);
16345 list = list->dw_loc_next;
16346 while (list)
16348 copy = copy_loc_descr (ref);
16349 add_loc_descr (&list->expr, copy);
16350 while (copy->dw_loc_next)
16351 copy = copy->dw_loc_next = copy_loc_descr (copy->dw_loc_next);
16352 list = list->dw_loc_next;
16356 /* To each location in list LIST prepend loc descr REF. */
16358 static void
16359 prepend_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
16361 dw_loc_descr_ref copy;
16362 dw_loc_descr_ref ref_end = list->expr;
16363 add_loc_descr (&ref, list->expr);
16364 list->expr = ref;
16365 list = list->dw_loc_next;
16366 while (list)
16368 dw_loc_descr_ref end = list->expr;
16369 list->expr = copy = copy_loc_descr (ref);
16370 while (copy->dw_loc_next != ref_end)
16371 copy = copy->dw_loc_next = copy_loc_descr (copy->dw_loc_next);
16372 copy->dw_loc_next = end;
16373 list = list->dw_loc_next;
16377 /* Given two lists RET and LIST
16378 produce location list that is result of adding expression in LIST
16379 to expression in RET on each position in program.
16380 Might be destructive on both RET and LIST.
16382 TODO: We handle only simple cases of RET or LIST having at most one
16383 element. General case would involve sorting the lists in program order
16384 and merging them that will need some additional work.
16385 Adding that will improve quality of debug info especially for SRA-ed
16386 structures. */
16388 static void
16389 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
16391 if (!list)
16392 return;
16393 if (!*ret)
16395 *ret = list;
16396 return;
16398 if (!list->dw_loc_next)
16400 add_loc_descr_to_each (*ret, list->expr);
16401 return;
16403 if (!(*ret)->dw_loc_next)
16405 prepend_loc_descr_to_each (list, (*ret)->expr);
16406 *ret = list;
16407 return;
16409 expansion_failed (NULL_TREE, NULL_RTX,
16410 "Don't know how to merge two non-trivial"
16411 " location lists.\n");
16412 *ret = NULL;
16413 return;
16416 /* LOC is constant expression. Try a luck, look it up in constant
16417 pool and return its loc_descr of its address. */
16419 static dw_loc_descr_ref
16420 cst_pool_loc_descr (tree loc)
16422 /* Get an RTL for this, if something has been emitted. */
16423 rtx rtl = lookup_constant_def (loc);
16425 if (!rtl || !MEM_P (rtl))
16427 gcc_assert (!rtl);
16428 return 0;
16430 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
16432 /* TODO: We might get more coverage if we was actually delaying expansion
16433 of all expressions till end of compilation when constant pools are fully
16434 populated. */
16435 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
16437 expansion_failed (loc, NULL_RTX,
16438 "CST value in contant pool but not marked.");
16439 return 0;
16441 return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
16442 GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
16445 /* Return dw_loc_list representing address of addr_expr LOC
16446 by looking for inner INDIRECT_REF expression and turning
16447 it into simple arithmetics.
16449 See loc_list_from_tree for the meaning of CONTEXT. */
16451 static dw_loc_list_ref
16452 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev,
16453 loc_descr_context *context)
16455 tree obj, offset;
16456 HOST_WIDE_INT bitsize, bitpos, bytepos;
16457 machine_mode mode;
16458 int unsignedp, reversep, volatilep = 0;
16459 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
16461 obj = get_inner_reference (TREE_OPERAND (loc, 0),
16462 &bitsize, &bitpos, &offset, &mode,
16463 &unsignedp, &reversep, &volatilep);
16464 STRIP_NOPS (obj);
16465 if (bitpos % BITS_PER_UNIT)
16467 expansion_failed (loc, NULL_RTX, "bitfield access");
16468 return 0;
16470 if (!INDIRECT_REF_P (obj))
16472 expansion_failed (obj,
16473 NULL_RTX, "no indirect ref in inner refrence");
16474 return 0;
16476 if (!offset && !bitpos)
16477 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1,
16478 context);
16479 else if (toplev
16480 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
16481 && (dwarf_version >= 4 || !dwarf_strict))
16483 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0, context);
16484 if (!list_ret)
16485 return 0;
16486 if (offset)
16488 /* Variable offset. */
16489 list_ret1 = loc_list_from_tree (offset, 0, context);
16490 if (list_ret1 == 0)
16491 return 0;
16492 add_loc_list (&list_ret, list_ret1);
16493 if (!list_ret)
16494 return 0;
16495 add_loc_descr_to_each (list_ret,
16496 new_loc_descr (DW_OP_plus, 0, 0));
16498 bytepos = bitpos / BITS_PER_UNIT;
16499 if (bytepos > 0)
16500 add_loc_descr_to_each (list_ret,
16501 new_loc_descr (DW_OP_plus_uconst,
16502 bytepos, 0));
16503 else if (bytepos < 0)
16504 loc_list_plus_const (list_ret, bytepos);
16505 add_loc_descr_to_each (list_ret,
16506 new_loc_descr (DW_OP_stack_value, 0, 0));
16508 return list_ret;
16511 /* Set LOC to the next operation that is not a DW_OP_nop operation. In the case
16512 all operations from LOC are nops, move to the last one. Insert in NOPS all
16513 operations that are skipped. */
16515 static void
16516 loc_descr_to_next_no_nop (dw_loc_descr_ref &loc,
16517 hash_set<dw_loc_descr_ref> &nops)
16519 while (loc->dw_loc_next != NULL && loc->dw_loc_opc == DW_OP_nop)
16521 nops.add (loc);
16522 loc = loc->dw_loc_next;
16526 /* Helper for loc_descr_without_nops: free the location description operation
16527 P. */
16529 bool
16530 free_loc_descr (const dw_loc_descr_ref &loc, void *data ATTRIBUTE_UNUSED)
16532 ggc_free (loc);
16533 return true;
16536 /* Remove all DW_OP_nop operations from LOC except, if it exists, the one that
16537 finishes LOC. */
16539 static void
16540 loc_descr_without_nops (dw_loc_descr_ref &loc)
16542 if (loc->dw_loc_opc == DW_OP_nop && loc->dw_loc_next == NULL)
16543 return;
16545 /* Set of all DW_OP_nop operations we remove. */
16546 hash_set<dw_loc_descr_ref> nops;
16548 /* First, strip all prefix NOP operations in order to keep the head of the
16549 operations list. */
16550 loc_descr_to_next_no_nop (loc, nops);
16552 for (dw_loc_descr_ref cur = loc; cur != NULL;)
16554 /* For control flow operations: strip "prefix" nops in destination
16555 labels. */
16556 if (cur->dw_loc_oprnd1.val_class == dw_val_class_loc)
16557 loc_descr_to_next_no_nop (cur->dw_loc_oprnd1.v.val_loc, nops);
16558 if (cur->dw_loc_oprnd2.val_class == dw_val_class_loc)
16559 loc_descr_to_next_no_nop (cur->dw_loc_oprnd2.v.val_loc, nops);
16561 /* Do the same for the operations that follow, then move to the next
16562 iteration. */
16563 if (cur->dw_loc_next != NULL)
16564 loc_descr_to_next_no_nop (cur->dw_loc_next, nops);
16565 cur = cur->dw_loc_next;
16568 nops.traverse<void *, free_loc_descr> (NULL);
16572 struct dwarf_procedure_info;
16574 /* Helper structure for location descriptions generation. */
16575 struct loc_descr_context
16577 /* The type that is implicitly referenced by DW_OP_push_object_address, or
16578 NULL_TREE if DW_OP_push_object_address in invalid for this location
16579 description. This is used when processing PLACEHOLDER_EXPR nodes. */
16580 tree context_type;
16581 /* The ..._DECL node that should be translated as a
16582 DW_OP_push_object_address operation. */
16583 tree base_decl;
16584 /* Information about the DWARF procedure we are currently generating. NULL if
16585 we are not generating a DWARF procedure. */
16586 struct dwarf_procedure_info *dpi;
16587 /* True if integral PLACEHOLDER_EXPR stands for the first argument passed
16588 by consumer. Used for DW_TAG_generic_subrange attributes. */
16589 bool placeholder_arg;
16590 /* True if PLACEHOLDER_EXPR has been seen. */
16591 bool placeholder_seen;
16594 /* DWARF procedures generation
16596 DWARF expressions (aka. location descriptions) are used to encode variable
16597 things such as sizes or offsets. Such computations can have redundant parts
16598 that can be factorized in order to reduce the size of the output debug
16599 information. This is the whole point of DWARF procedures.
16601 Thanks to stor-layout.c, size and offset expressions in GENERIC trees are
16602 already factorized into functions ("size functions") in order to handle very
16603 big and complex types. Such functions are quite simple: they have integral
16604 arguments, they return an integral result and their body contains only a
16605 return statement with arithmetic expressions. This is the only kind of
16606 function we are interested in translating into DWARF procedures, here.
16608 DWARF expressions and DWARF procedure are executed using a stack, so we have
16609 to define some calling convention for them to interact. Let's say that:
16611 - Before calling a DWARF procedure, DWARF expressions must push on the stack
16612 all arguments in reverse order (right-to-left) so that when the DWARF
16613 procedure execution starts, the first argument is the top of the stack.
16615 - Then, when returning, the DWARF procedure must have consumed all arguments
16616 on the stack, must have pushed the result and touched nothing else.
16618 - Each integral argument and the result are integral types can be hold in a
16619 single stack slot.
16621 - We call "frame offset" the number of stack slots that are "under DWARF
16622 procedure control": it includes the arguments slots, the temporaries and
16623 the result slot. Thus, it is equal to the number of arguments when the
16624 procedure execution starts and must be equal to one (the result) when it
16625 returns. */
16627 /* Helper structure used when generating operations for a DWARF procedure. */
16628 struct dwarf_procedure_info
16630 /* The FUNCTION_DECL node corresponding to the DWARF procedure that is
16631 currently translated. */
16632 tree fndecl;
16633 /* The number of arguments FNDECL takes. */
16634 unsigned args_count;
16637 /* Return a pointer to a newly created DIE node for a DWARF procedure. Add
16638 LOCATION as its DW_AT_location attribute. If FNDECL is not NULL_TREE,
16639 equate it to this DIE. */
16641 static dw_die_ref
16642 new_dwarf_proc_die (dw_loc_descr_ref location, tree fndecl,
16643 dw_die_ref parent_die)
16645 dw_die_ref dwarf_proc_die;
16647 if ((dwarf_version < 3 && dwarf_strict)
16648 || location == NULL)
16649 return NULL;
16651 dwarf_proc_die = new_die (DW_TAG_dwarf_procedure, parent_die, fndecl);
16652 if (fndecl)
16653 equate_decl_number_to_die (fndecl, dwarf_proc_die);
16654 add_AT_loc (dwarf_proc_die, DW_AT_location, location);
16655 return dwarf_proc_die;
16658 /* Return whether TYPE is a supported type as a DWARF procedure argument
16659 type or return type (we handle only scalar types and pointer types that
16660 aren't wider than the DWARF expression evaluation stack. */
16662 static bool
16663 is_handled_procedure_type (tree type)
16665 return ((INTEGRAL_TYPE_P (type)
16666 || TREE_CODE (type) == OFFSET_TYPE
16667 || TREE_CODE (type) == POINTER_TYPE)
16668 && int_size_in_bytes (type) <= DWARF2_ADDR_SIZE);
16671 /* Helper for resolve_args_picking: do the same but stop when coming across
16672 visited nodes. For each node we visit, register in FRAME_OFFSETS the frame
16673 offset *before* evaluating the corresponding operation. */
16675 static bool
16676 resolve_args_picking_1 (dw_loc_descr_ref loc, unsigned initial_frame_offset,
16677 struct dwarf_procedure_info *dpi,
16678 hash_map<dw_loc_descr_ref, unsigned> &frame_offsets)
16680 /* The "frame_offset" identifier is already used to name a macro... */
16681 unsigned frame_offset_ = initial_frame_offset;
16682 dw_loc_descr_ref l;
16684 for (l = loc; l != NULL;)
16686 bool existed;
16687 unsigned &l_frame_offset = frame_offsets.get_or_insert (l, &existed);
16689 /* If we already met this node, there is nothing to compute anymore. */
16690 if (existed)
16692 /* Make sure that the stack size is consistent wherever the execution
16693 flow comes from. */
16694 gcc_assert ((unsigned) l_frame_offset == frame_offset_);
16695 break;
16697 l_frame_offset = frame_offset_;
16699 /* If needed, relocate the picking offset with respect to the frame
16700 offset. */
16701 if (l->frame_offset_rel)
16703 unsigned HOST_WIDE_INT off;
16704 switch (l->dw_loc_opc)
16706 case DW_OP_pick:
16707 off = l->dw_loc_oprnd1.v.val_unsigned;
16708 break;
16709 case DW_OP_dup:
16710 off = 0;
16711 break;
16712 case DW_OP_over:
16713 off = 1;
16714 break;
16715 default:
16716 gcc_unreachable ();
16718 /* frame_offset_ is the size of the current stack frame, including
16719 incoming arguments. Besides, the arguments are pushed
16720 right-to-left. Thus, in order to access the Nth argument from
16721 this operation node, the picking has to skip temporaries *plus*
16722 one stack slot per argument (0 for the first one, 1 for the second
16723 one, etc.).
16725 The targetted argument number (N) is already set as the operand,
16726 and the number of temporaries can be computed with:
16727 frame_offsets_ - dpi->args_count */
16728 off += frame_offset_ - dpi->args_count;
16730 /* DW_OP_pick handles only offsets from 0 to 255 (inclusive)... */
16731 if (off > 255)
16732 return false;
16734 if (off == 0)
16736 l->dw_loc_opc = DW_OP_dup;
16737 l->dw_loc_oprnd1.v.val_unsigned = 0;
16739 else if (off == 1)
16741 l->dw_loc_opc = DW_OP_over;
16742 l->dw_loc_oprnd1.v.val_unsigned = 0;
16744 else
16746 l->dw_loc_opc = DW_OP_pick;
16747 l->dw_loc_oprnd1.v.val_unsigned = off;
16751 /* Update frame_offset according to the effect the current operation has
16752 on the stack. */
16753 switch (l->dw_loc_opc)
16755 case DW_OP_deref:
16756 case DW_OP_swap:
16757 case DW_OP_rot:
16758 case DW_OP_abs:
16759 case DW_OP_neg:
16760 case DW_OP_not:
16761 case DW_OP_plus_uconst:
16762 case DW_OP_skip:
16763 case DW_OP_reg0:
16764 case DW_OP_reg1:
16765 case DW_OP_reg2:
16766 case DW_OP_reg3:
16767 case DW_OP_reg4:
16768 case DW_OP_reg5:
16769 case DW_OP_reg6:
16770 case DW_OP_reg7:
16771 case DW_OP_reg8:
16772 case DW_OP_reg9:
16773 case DW_OP_reg10:
16774 case DW_OP_reg11:
16775 case DW_OP_reg12:
16776 case DW_OP_reg13:
16777 case DW_OP_reg14:
16778 case DW_OP_reg15:
16779 case DW_OP_reg16:
16780 case DW_OP_reg17:
16781 case DW_OP_reg18:
16782 case DW_OP_reg19:
16783 case DW_OP_reg20:
16784 case DW_OP_reg21:
16785 case DW_OP_reg22:
16786 case DW_OP_reg23:
16787 case DW_OP_reg24:
16788 case DW_OP_reg25:
16789 case DW_OP_reg26:
16790 case DW_OP_reg27:
16791 case DW_OP_reg28:
16792 case DW_OP_reg29:
16793 case DW_OP_reg30:
16794 case DW_OP_reg31:
16795 case DW_OP_bregx:
16796 case DW_OP_piece:
16797 case DW_OP_deref_size:
16798 case DW_OP_nop:
16799 case DW_OP_bit_piece:
16800 case DW_OP_implicit_value:
16801 case DW_OP_stack_value:
16802 break;
16804 case DW_OP_addr:
16805 case DW_OP_const1u:
16806 case DW_OP_const1s:
16807 case DW_OP_const2u:
16808 case DW_OP_const2s:
16809 case DW_OP_const4u:
16810 case DW_OP_const4s:
16811 case DW_OP_const8u:
16812 case DW_OP_const8s:
16813 case DW_OP_constu:
16814 case DW_OP_consts:
16815 case DW_OP_dup:
16816 case DW_OP_over:
16817 case DW_OP_pick:
16818 case DW_OP_lit0:
16819 case DW_OP_lit1:
16820 case DW_OP_lit2:
16821 case DW_OP_lit3:
16822 case DW_OP_lit4:
16823 case DW_OP_lit5:
16824 case DW_OP_lit6:
16825 case DW_OP_lit7:
16826 case DW_OP_lit8:
16827 case DW_OP_lit9:
16828 case DW_OP_lit10:
16829 case DW_OP_lit11:
16830 case DW_OP_lit12:
16831 case DW_OP_lit13:
16832 case DW_OP_lit14:
16833 case DW_OP_lit15:
16834 case DW_OP_lit16:
16835 case DW_OP_lit17:
16836 case DW_OP_lit18:
16837 case DW_OP_lit19:
16838 case DW_OP_lit20:
16839 case DW_OP_lit21:
16840 case DW_OP_lit22:
16841 case DW_OP_lit23:
16842 case DW_OP_lit24:
16843 case DW_OP_lit25:
16844 case DW_OP_lit26:
16845 case DW_OP_lit27:
16846 case DW_OP_lit28:
16847 case DW_OP_lit29:
16848 case DW_OP_lit30:
16849 case DW_OP_lit31:
16850 case DW_OP_breg0:
16851 case DW_OP_breg1:
16852 case DW_OP_breg2:
16853 case DW_OP_breg3:
16854 case DW_OP_breg4:
16855 case DW_OP_breg5:
16856 case DW_OP_breg6:
16857 case DW_OP_breg7:
16858 case DW_OP_breg8:
16859 case DW_OP_breg9:
16860 case DW_OP_breg10:
16861 case DW_OP_breg11:
16862 case DW_OP_breg12:
16863 case DW_OP_breg13:
16864 case DW_OP_breg14:
16865 case DW_OP_breg15:
16866 case DW_OP_breg16:
16867 case DW_OP_breg17:
16868 case DW_OP_breg18:
16869 case DW_OP_breg19:
16870 case DW_OP_breg20:
16871 case DW_OP_breg21:
16872 case DW_OP_breg22:
16873 case DW_OP_breg23:
16874 case DW_OP_breg24:
16875 case DW_OP_breg25:
16876 case DW_OP_breg26:
16877 case DW_OP_breg27:
16878 case DW_OP_breg28:
16879 case DW_OP_breg29:
16880 case DW_OP_breg30:
16881 case DW_OP_breg31:
16882 case DW_OP_fbreg:
16883 case DW_OP_push_object_address:
16884 case DW_OP_call_frame_cfa:
16885 case DW_OP_GNU_variable_value:
16886 ++frame_offset_;
16887 break;
16889 case DW_OP_drop:
16890 case DW_OP_xderef:
16891 case DW_OP_and:
16892 case DW_OP_div:
16893 case DW_OP_minus:
16894 case DW_OP_mod:
16895 case DW_OP_mul:
16896 case DW_OP_or:
16897 case DW_OP_plus:
16898 case DW_OP_shl:
16899 case DW_OP_shr:
16900 case DW_OP_shra:
16901 case DW_OP_xor:
16902 case DW_OP_bra:
16903 case DW_OP_eq:
16904 case DW_OP_ge:
16905 case DW_OP_gt:
16906 case DW_OP_le:
16907 case DW_OP_lt:
16908 case DW_OP_ne:
16909 case DW_OP_regx:
16910 case DW_OP_xderef_size:
16911 --frame_offset_;
16912 break;
16914 case DW_OP_call2:
16915 case DW_OP_call4:
16916 case DW_OP_call_ref:
16918 dw_die_ref dwarf_proc = l->dw_loc_oprnd1.v.val_die_ref.die;
16919 int *stack_usage = dwarf_proc_stack_usage_map->get (dwarf_proc);
16921 if (stack_usage == NULL)
16922 return false;
16923 frame_offset_ += *stack_usage;
16924 break;
16927 case DW_OP_implicit_pointer:
16928 case DW_OP_entry_value:
16929 case DW_OP_const_type:
16930 case DW_OP_regval_type:
16931 case DW_OP_deref_type:
16932 case DW_OP_convert:
16933 case DW_OP_reinterpret:
16934 case DW_OP_form_tls_address:
16935 case DW_OP_GNU_push_tls_address:
16936 case DW_OP_GNU_uninit:
16937 case DW_OP_GNU_encoded_addr:
16938 case DW_OP_GNU_implicit_pointer:
16939 case DW_OP_GNU_entry_value:
16940 case DW_OP_GNU_const_type:
16941 case DW_OP_GNU_regval_type:
16942 case DW_OP_GNU_deref_type:
16943 case DW_OP_GNU_convert:
16944 case DW_OP_GNU_reinterpret:
16945 case DW_OP_GNU_parameter_ref:
16946 /* loc_list_from_tree will probably not output these operations for
16947 size functions, so assume they will not appear here. */
16948 /* Fall through... */
16950 default:
16951 gcc_unreachable ();
16954 /* Now, follow the control flow (except subroutine calls). */
16955 switch (l->dw_loc_opc)
16957 case DW_OP_bra:
16958 if (!resolve_args_picking_1 (l->dw_loc_next, frame_offset_, dpi,
16959 frame_offsets))
16960 return false;
16961 /* Fall through. */
16963 case DW_OP_skip:
16964 l = l->dw_loc_oprnd1.v.val_loc;
16965 break;
16967 case DW_OP_stack_value:
16968 return true;
16970 default:
16971 l = l->dw_loc_next;
16972 break;
16976 return true;
16979 /* Make a DFS over operations reachable through LOC (i.e. follow branch
16980 operations) in order to resolve the operand of DW_OP_pick operations that
16981 target DWARF procedure arguments (DPI). INITIAL_FRAME_OFFSET is the frame
16982 offset *before* LOC is executed. Return if all relocations were
16983 successful. */
16985 static bool
16986 resolve_args_picking (dw_loc_descr_ref loc, unsigned initial_frame_offset,
16987 struct dwarf_procedure_info *dpi)
16989 /* Associate to all visited operations the frame offset *before* evaluating
16990 this operation. */
16991 hash_map<dw_loc_descr_ref, unsigned> frame_offsets;
16993 return resolve_args_picking_1 (loc, initial_frame_offset, dpi,
16994 frame_offsets);
16997 /* Try to generate a DWARF procedure that computes the same result as FNDECL.
16998 Return NULL if it is not possible. */
17000 static dw_die_ref
17001 function_to_dwarf_procedure (tree fndecl)
17003 struct loc_descr_context ctx;
17004 struct dwarf_procedure_info dpi;
17005 dw_die_ref dwarf_proc_die;
17006 tree tree_body = DECL_SAVED_TREE (fndecl);
17007 dw_loc_descr_ref loc_body, epilogue;
17009 tree cursor;
17010 unsigned i;
17012 /* Do not generate multiple DWARF procedures for the same function
17013 declaration. */
17014 dwarf_proc_die = lookup_decl_die (fndecl);
17015 if (dwarf_proc_die != NULL)
17016 return dwarf_proc_die;
17018 /* DWARF procedures are available starting with the DWARFv3 standard. */
17019 if (dwarf_version < 3 && dwarf_strict)
17020 return NULL;
17022 /* We handle only functions for which we still have a body, that return a
17023 supported type and that takes arguments with supported types. Note that
17024 there is no point translating functions that return nothing. */
17025 if (tree_body == NULL_TREE
17026 || DECL_RESULT (fndecl) == NULL_TREE
17027 || !is_handled_procedure_type (TREE_TYPE (DECL_RESULT (fndecl))))
17028 return NULL;
17030 for (cursor = DECL_ARGUMENTS (fndecl);
17031 cursor != NULL_TREE;
17032 cursor = TREE_CHAIN (cursor))
17033 if (!is_handled_procedure_type (TREE_TYPE (cursor)))
17034 return NULL;
17036 /* Match only "expr" in: RETURN_EXPR (MODIFY_EXPR (RESULT_DECL, expr)). */
17037 if (TREE_CODE (tree_body) != RETURN_EXPR)
17038 return NULL;
17039 tree_body = TREE_OPERAND (tree_body, 0);
17040 if (TREE_CODE (tree_body) != MODIFY_EXPR
17041 || TREE_OPERAND (tree_body, 0) != DECL_RESULT (fndecl))
17042 return NULL;
17043 tree_body = TREE_OPERAND (tree_body, 1);
17045 /* Try to translate the body expression itself. Note that this will probably
17046 cause an infinite recursion if its call graph has a cycle. This is very
17047 unlikely for size functions, however, so don't bother with such things at
17048 the moment. */
17049 ctx.context_type = NULL_TREE;
17050 ctx.base_decl = NULL_TREE;
17051 ctx.dpi = &dpi;
17052 ctx.placeholder_arg = false;
17053 ctx.placeholder_seen = false;
17054 dpi.fndecl = fndecl;
17055 dpi.args_count = list_length (DECL_ARGUMENTS (fndecl));
17056 loc_body = loc_descriptor_from_tree (tree_body, 0, &ctx);
17057 if (!loc_body)
17058 return NULL;
17060 /* After evaluating all operands in "loc_body", we should still have on the
17061 stack all arguments plus the desired function result (top of the stack).
17062 Generate code in order to keep only the result in our stack frame. */
17063 epilogue = NULL;
17064 for (i = 0; i < dpi.args_count; ++i)
17066 dw_loc_descr_ref op_couple = new_loc_descr (DW_OP_swap, 0, 0);
17067 op_couple->dw_loc_next = new_loc_descr (DW_OP_drop, 0, 0);
17068 op_couple->dw_loc_next->dw_loc_next = epilogue;
17069 epilogue = op_couple;
17071 add_loc_descr (&loc_body, epilogue);
17072 if (!resolve_args_picking (loc_body, dpi.args_count, &dpi))
17073 return NULL;
17075 /* Trailing nops from loc_descriptor_from_tree (if any) cannot be removed
17076 because they are considered useful. Now there is an epilogue, they are
17077 not anymore, so give it another try. */
17078 loc_descr_without_nops (loc_body);
17080 /* fndecl may be used both as a regular DW_TAG_subprogram DIE and as
17081 a DW_TAG_dwarf_procedure, so we may have a conflict, here. It's unlikely,
17082 though, given that size functions do not come from source, so they should
17083 not have a dedicated DW_TAG_subprogram DIE. */
17084 dwarf_proc_die
17085 = new_dwarf_proc_die (loc_body, fndecl,
17086 get_context_die (DECL_CONTEXT (fndecl)));
17088 /* The called DWARF procedure consumes one stack slot per argument and
17089 returns one stack slot. */
17090 dwarf_proc_stack_usage_map->put (dwarf_proc_die, 1 - dpi.args_count);
17092 return dwarf_proc_die;
17096 /* Generate Dwarf location list representing LOC.
17097 If WANT_ADDRESS is false, expression computing LOC will be computed
17098 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
17099 if WANT_ADDRESS is 2, expression computing address useable in location
17100 will be returned (i.e. DW_OP_reg can be used
17101 to refer to register values).
17103 CONTEXT provides information to customize the location descriptions
17104 generation. Its context_type field specifies what type is implicitly
17105 referenced by DW_OP_push_object_address. If it is NULL_TREE, this operation
17106 will not be generated.
17108 Its DPI field determines whether we are generating a DWARF expression for a
17109 DWARF procedure, so PARM_DECL references are processed specifically.
17111 If CONTEXT is NULL, the behavior is the same as if context_type, base_decl
17112 and dpi fields were null. */
17114 static dw_loc_list_ref
17115 loc_list_from_tree_1 (tree loc, int want_address,
17116 struct loc_descr_context *context)
17118 dw_loc_descr_ref ret = NULL, ret1 = NULL;
17119 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
17120 int have_address = 0;
17121 enum dwarf_location_atom op;
17123 /* ??? Most of the time we do not take proper care for sign/zero
17124 extending the values properly. Hopefully this won't be a real
17125 problem... */
17127 if (context != NULL
17128 && context->base_decl == loc
17129 && want_address == 0)
17131 if (dwarf_version >= 3 || !dwarf_strict)
17132 return new_loc_list (new_loc_descr (DW_OP_push_object_address, 0, 0),
17133 NULL, NULL, NULL);
17134 else
17135 return NULL;
17138 switch (TREE_CODE (loc))
17140 case ERROR_MARK:
17141 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
17142 return 0;
17144 case PLACEHOLDER_EXPR:
17145 /* This case involves extracting fields from an object to determine the
17146 position of other fields. It is supposed to appear only as the first
17147 operand of COMPONENT_REF nodes and to reference precisely the type
17148 that the context allows. */
17149 if (context != NULL
17150 && TREE_TYPE (loc) == context->context_type
17151 && want_address >= 1)
17153 if (dwarf_version >= 3 || !dwarf_strict)
17155 ret = new_loc_descr (DW_OP_push_object_address, 0, 0);
17156 have_address = 1;
17157 break;
17159 else
17160 return NULL;
17162 /* For DW_TAG_generic_subrange attributes, PLACEHOLDER_EXPR stands for
17163 the single argument passed by consumer. */
17164 else if (context != NULL
17165 && context->placeholder_arg
17166 && INTEGRAL_TYPE_P (TREE_TYPE (loc))
17167 && want_address == 0)
17169 ret = new_loc_descr (DW_OP_pick, 0, 0);
17170 ret->frame_offset_rel = 1;
17171 context->placeholder_seen = true;
17172 break;
17174 else
17175 expansion_failed (loc, NULL_RTX,
17176 "PLACEHOLDER_EXPR for an unexpected type");
17177 break;
17179 case CALL_EXPR:
17181 const int nargs = call_expr_nargs (loc);
17182 tree callee = get_callee_fndecl (loc);
17183 int i;
17184 dw_die_ref dwarf_proc;
17186 if (callee == NULL_TREE)
17187 goto call_expansion_failed;
17189 /* We handle only functions that return an integer. */
17190 if (!is_handled_procedure_type (TREE_TYPE (TREE_TYPE (callee))))
17191 goto call_expansion_failed;
17193 dwarf_proc = function_to_dwarf_procedure (callee);
17194 if (dwarf_proc == NULL)
17195 goto call_expansion_failed;
17197 /* Evaluate arguments right-to-left so that the first argument will
17198 be the top-most one on the stack. */
17199 for (i = nargs - 1; i >= 0; --i)
17201 dw_loc_descr_ref loc_descr
17202 = loc_descriptor_from_tree (CALL_EXPR_ARG (loc, i), 0,
17203 context);
17205 if (loc_descr == NULL)
17206 goto call_expansion_failed;
17208 add_loc_descr (&ret, loc_descr);
17211 ret1 = new_loc_descr (DW_OP_call4, 0, 0);
17212 ret1->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
17213 ret1->dw_loc_oprnd1.v.val_die_ref.die = dwarf_proc;
17214 ret1->dw_loc_oprnd1.v.val_die_ref.external = 0;
17215 add_loc_descr (&ret, ret1);
17216 break;
17218 call_expansion_failed:
17219 expansion_failed (loc, NULL_RTX, "CALL_EXPR");
17220 /* There are no opcodes for these operations. */
17221 return 0;
17224 case PREINCREMENT_EXPR:
17225 case PREDECREMENT_EXPR:
17226 case POSTINCREMENT_EXPR:
17227 case POSTDECREMENT_EXPR:
17228 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
17229 /* There are no opcodes for these operations. */
17230 return 0;
17232 case ADDR_EXPR:
17233 /* If we already want an address, see if there is INDIRECT_REF inside
17234 e.g. for &this->field. */
17235 if (want_address)
17237 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
17238 (loc, want_address == 2, context);
17239 if (list_ret)
17240 have_address = 1;
17241 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
17242 && (ret = cst_pool_loc_descr (loc)))
17243 have_address = 1;
17245 /* Otherwise, process the argument and look for the address. */
17246 if (!list_ret && !ret)
17247 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 1, context);
17248 else
17250 if (want_address)
17251 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
17252 return NULL;
17254 break;
17256 case VAR_DECL:
17257 if (DECL_THREAD_LOCAL_P (loc))
17259 rtx rtl;
17260 enum dwarf_location_atom tls_op;
17261 enum dtprel_bool dtprel = dtprel_false;
17263 if (targetm.have_tls)
17265 /* If this is not defined, we have no way to emit the
17266 data. */
17267 if (!targetm.asm_out.output_dwarf_dtprel)
17268 return 0;
17270 /* The way DW_OP_GNU_push_tls_address is specified, we
17271 can only look up addresses of objects in the current
17272 module. We used DW_OP_addr as first op, but that's
17273 wrong, because DW_OP_addr is relocated by the debug
17274 info consumer, while DW_OP_GNU_push_tls_address
17275 operand shouldn't be. */
17276 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
17277 return 0;
17278 dtprel = dtprel_true;
17279 /* We check for DWARF 5 here because gdb did not implement
17280 DW_OP_form_tls_address until after 7.12. */
17281 tls_op = (dwarf_version >= 5 ? DW_OP_form_tls_address
17282 : DW_OP_GNU_push_tls_address);
17284 else
17286 if (!targetm.emutls.debug_form_tls_address
17287 || !(dwarf_version >= 3 || !dwarf_strict))
17288 return 0;
17289 /* We stuffed the control variable into the DECL_VALUE_EXPR
17290 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
17291 no longer appear in gimple code. We used the control
17292 variable in specific so that we could pick it up here. */
17293 loc = DECL_VALUE_EXPR (loc);
17294 tls_op = DW_OP_form_tls_address;
17297 rtl = rtl_for_decl_location (loc);
17298 if (rtl == NULL_RTX)
17299 return 0;
17301 if (!MEM_P (rtl))
17302 return 0;
17303 rtl = XEXP (rtl, 0);
17304 if (! CONSTANT_P (rtl))
17305 return 0;
17307 ret = new_addr_loc_descr (rtl, dtprel);
17308 ret1 = new_loc_descr (tls_op, 0, 0);
17309 add_loc_descr (&ret, ret1);
17311 have_address = 1;
17312 break;
17314 /* FALLTHRU */
17316 case PARM_DECL:
17317 if (context != NULL && context->dpi != NULL
17318 && DECL_CONTEXT (loc) == context->dpi->fndecl)
17320 /* We are generating code for a DWARF procedure and we want to access
17321 one of its arguments: find the appropriate argument offset and let
17322 the resolve_args_picking pass compute the offset that complies
17323 with the stack frame size. */
17324 unsigned i = 0;
17325 tree cursor;
17327 for (cursor = DECL_ARGUMENTS (context->dpi->fndecl);
17328 cursor != NULL_TREE && cursor != loc;
17329 cursor = TREE_CHAIN (cursor), ++i)
17331 /* If we are translating a DWARF procedure, all referenced parameters
17332 must belong to the current function. */
17333 gcc_assert (cursor != NULL_TREE);
17335 ret = new_loc_descr (DW_OP_pick, i, 0);
17336 ret->frame_offset_rel = 1;
17337 break;
17339 /* FALLTHRU */
17341 case RESULT_DECL:
17342 if (DECL_HAS_VALUE_EXPR_P (loc))
17343 return loc_list_from_tree_1 (DECL_VALUE_EXPR (loc),
17344 want_address, context);
17345 /* FALLTHRU */
17347 case FUNCTION_DECL:
17349 rtx rtl;
17350 var_loc_list *loc_list = lookup_decl_loc (loc);
17352 if (loc_list && loc_list->first)
17354 list_ret = dw_loc_list (loc_list, loc, want_address);
17355 have_address = want_address != 0;
17356 break;
17358 rtl = rtl_for_decl_location (loc);
17359 if (rtl == NULL_RTX)
17361 if (TREE_CODE (loc) != FUNCTION_DECL
17362 && early_dwarf
17363 && current_function_decl
17364 && want_address != 1
17365 && (INTEGRAL_TYPE_P (TREE_TYPE (loc))
17366 || POINTER_TYPE_P (TREE_TYPE (loc)))
17367 && DECL_CONTEXT (loc) == current_function_decl
17368 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (loc)))
17369 <= DWARF2_ADDR_SIZE))
17371 dw_die_ref ref = lookup_decl_die (loc);
17372 ret = new_loc_descr (DW_OP_GNU_variable_value, 0, 0);
17373 if (ref)
17375 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
17376 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
17377 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
17379 else
17381 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
17382 ret->dw_loc_oprnd1.v.val_decl_ref = loc;
17384 break;
17386 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
17387 return 0;
17389 else if (CONST_INT_P (rtl))
17391 HOST_WIDE_INT val = INTVAL (rtl);
17392 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
17393 val &= GET_MODE_MASK (DECL_MODE (loc));
17394 ret = int_loc_descriptor (val);
17396 else if (GET_CODE (rtl) == CONST_STRING)
17398 expansion_failed (loc, NULL_RTX, "CONST_STRING");
17399 return 0;
17401 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
17402 ret = new_addr_loc_descr (rtl, dtprel_false);
17403 else
17405 machine_mode mode, mem_mode;
17407 /* Certain constructs can only be represented at top-level. */
17408 if (want_address == 2)
17410 ret = loc_descriptor (rtl, VOIDmode,
17411 VAR_INIT_STATUS_INITIALIZED);
17412 have_address = 1;
17414 else
17416 mode = GET_MODE (rtl);
17417 mem_mode = VOIDmode;
17418 if (MEM_P (rtl))
17420 mem_mode = mode;
17421 mode = get_address_mode (rtl);
17422 rtl = XEXP (rtl, 0);
17423 have_address = 1;
17425 ret = mem_loc_descriptor (rtl, mode, mem_mode,
17426 VAR_INIT_STATUS_INITIALIZED);
17428 if (!ret)
17429 expansion_failed (loc, rtl,
17430 "failed to produce loc descriptor for rtl");
17433 break;
17435 case MEM_REF:
17436 if (!integer_zerop (TREE_OPERAND (loc, 1)))
17438 have_address = 1;
17439 goto do_plus;
17441 /* Fallthru. */
17442 case INDIRECT_REF:
17443 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17444 have_address = 1;
17445 break;
17447 case TARGET_MEM_REF:
17448 case SSA_NAME:
17449 case DEBUG_EXPR_DECL:
17450 return NULL;
17452 case COMPOUND_EXPR:
17453 return loc_list_from_tree_1 (TREE_OPERAND (loc, 1), want_address,
17454 context);
17456 CASE_CONVERT:
17457 case VIEW_CONVERT_EXPR:
17458 case SAVE_EXPR:
17459 case MODIFY_EXPR:
17460 case NON_LVALUE_EXPR:
17461 return loc_list_from_tree_1 (TREE_OPERAND (loc, 0), want_address,
17462 context);
17464 case COMPONENT_REF:
17465 case BIT_FIELD_REF:
17466 case ARRAY_REF:
17467 case ARRAY_RANGE_REF:
17468 case REALPART_EXPR:
17469 case IMAGPART_EXPR:
17471 tree obj, offset;
17472 HOST_WIDE_INT bitsize, bitpos, bytepos;
17473 machine_mode mode;
17474 int unsignedp, reversep, volatilep = 0;
17476 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
17477 &unsignedp, &reversep, &volatilep);
17479 gcc_assert (obj != loc);
17481 list_ret = loc_list_from_tree_1 (obj,
17482 want_address == 2
17483 && !bitpos && !offset ? 2 : 1,
17484 context);
17485 /* TODO: We can extract value of the small expression via shifting even
17486 for nonzero bitpos. */
17487 if (list_ret == 0)
17488 return 0;
17489 if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
17491 expansion_failed (loc, NULL_RTX,
17492 "bitfield access");
17493 return 0;
17496 if (offset != NULL_TREE)
17498 /* Variable offset. */
17499 list_ret1 = loc_list_from_tree_1 (offset, 0, context);
17500 if (list_ret1 == 0)
17501 return 0;
17502 add_loc_list (&list_ret, list_ret1);
17503 if (!list_ret)
17504 return 0;
17505 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
17508 bytepos = bitpos / BITS_PER_UNIT;
17509 if (bytepos > 0)
17510 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
17511 else if (bytepos < 0)
17512 loc_list_plus_const (list_ret, bytepos);
17514 have_address = 1;
17515 break;
17518 case INTEGER_CST:
17519 if ((want_address || !tree_fits_shwi_p (loc))
17520 && (ret = cst_pool_loc_descr (loc)))
17521 have_address = 1;
17522 else if (want_address == 2
17523 && tree_fits_shwi_p (loc)
17524 && (ret = address_of_int_loc_descriptor
17525 (int_size_in_bytes (TREE_TYPE (loc)),
17526 tree_to_shwi (loc))))
17527 have_address = 1;
17528 else if (tree_fits_shwi_p (loc))
17529 ret = int_loc_descriptor (tree_to_shwi (loc));
17530 else if (tree_fits_uhwi_p (loc))
17531 ret = uint_loc_descriptor (tree_to_uhwi (loc));
17532 else
17534 expansion_failed (loc, NULL_RTX,
17535 "Integer operand is not host integer");
17536 return 0;
17538 break;
17540 case CONSTRUCTOR:
17541 case REAL_CST:
17542 case STRING_CST:
17543 case COMPLEX_CST:
17544 if ((ret = cst_pool_loc_descr (loc)))
17545 have_address = 1;
17546 else if (TREE_CODE (loc) == CONSTRUCTOR)
17548 tree type = TREE_TYPE (loc);
17549 unsigned HOST_WIDE_INT size = int_size_in_bytes (type);
17550 unsigned HOST_WIDE_INT offset = 0;
17551 unsigned HOST_WIDE_INT cnt;
17552 constructor_elt *ce;
17554 if (TREE_CODE (type) == RECORD_TYPE)
17556 /* This is very limited, but it's enough to output
17557 pointers to member functions, as long as the
17558 referenced function is defined in the current
17559 translation unit. */
17560 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (loc), cnt, ce)
17562 tree val = ce->value;
17564 tree field = ce->index;
17566 if (val)
17567 STRIP_NOPS (val);
17569 if (!field || DECL_BIT_FIELD (field))
17571 expansion_failed (loc, NULL_RTX,
17572 "bitfield in record type constructor");
17573 size = offset = (unsigned HOST_WIDE_INT)-1;
17574 ret = NULL;
17575 break;
17578 HOST_WIDE_INT fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
17579 unsigned HOST_WIDE_INT pos = int_byte_position (field);
17580 gcc_assert (pos + fieldsize <= size);
17581 if (pos < offset)
17583 expansion_failed (loc, NULL_RTX,
17584 "out-of-order fields in record constructor");
17585 size = offset = (unsigned HOST_WIDE_INT)-1;
17586 ret = NULL;
17587 break;
17589 if (pos > offset)
17591 ret1 = new_loc_descr (DW_OP_piece, pos - offset, 0);
17592 add_loc_descr (&ret, ret1);
17593 offset = pos;
17595 if (val && fieldsize != 0)
17597 ret1 = loc_descriptor_from_tree (val, want_address, context);
17598 if (!ret1)
17600 expansion_failed (loc, NULL_RTX,
17601 "unsupported expression in field");
17602 size = offset = (unsigned HOST_WIDE_INT)-1;
17603 ret = NULL;
17604 break;
17606 add_loc_descr (&ret, ret1);
17608 if (fieldsize)
17610 ret1 = new_loc_descr (DW_OP_piece, fieldsize, 0);
17611 add_loc_descr (&ret, ret1);
17612 offset = pos + fieldsize;
17616 if (offset != size)
17618 ret1 = new_loc_descr (DW_OP_piece, size - offset, 0);
17619 add_loc_descr (&ret, ret1);
17620 offset = size;
17623 have_address = !!want_address;
17625 else
17626 expansion_failed (loc, NULL_RTX,
17627 "constructor of non-record type");
17629 else
17630 /* We can construct small constants here using int_loc_descriptor. */
17631 expansion_failed (loc, NULL_RTX,
17632 "constructor or constant not in constant pool");
17633 break;
17635 case TRUTH_AND_EXPR:
17636 case TRUTH_ANDIF_EXPR:
17637 case BIT_AND_EXPR:
17638 op = DW_OP_and;
17639 goto do_binop;
17641 case TRUTH_XOR_EXPR:
17642 case BIT_XOR_EXPR:
17643 op = DW_OP_xor;
17644 goto do_binop;
17646 case TRUTH_OR_EXPR:
17647 case TRUTH_ORIF_EXPR:
17648 case BIT_IOR_EXPR:
17649 op = DW_OP_or;
17650 goto do_binop;
17652 case FLOOR_DIV_EXPR:
17653 case CEIL_DIV_EXPR:
17654 case ROUND_DIV_EXPR:
17655 case TRUNC_DIV_EXPR:
17656 case EXACT_DIV_EXPR:
17657 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
17658 return 0;
17659 op = DW_OP_div;
17660 goto do_binop;
17662 case MINUS_EXPR:
17663 op = DW_OP_minus;
17664 goto do_binop;
17666 case FLOOR_MOD_EXPR:
17667 case CEIL_MOD_EXPR:
17668 case ROUND_MOD_EXPR:
17669 case TRUNC_MOD_EXPR:
17670 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
17672 op = DW_OP_mod;
17673 goto do_binop;
17675 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17676 list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
17677 if (list_ret == 0 || list_ret1 == 0)
17678 return 0;
17680 add_loc_list (&list_ret, list_ret1);
17681 if (list_ret == 0)
17682 return 0;
17683 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
17684 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
17685 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
17686 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
17687 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
17688 break;
17690 case MULT_EXPR:
17691 op = DW_OP_mul;
17692 goto do_binop;
17694 case LSHIFT_EXPR:
17695 op = DW_OP_shl;
17696 goto do_binop;
17698 case RSHIFT_EXPR:
17699 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
17700 goto do_binop;
17702 case POINTER_PLUS_EXPR:
17703 case PLUS_EXPR:
17704 do_plus:
17705 if (tree_fits_shwi_p (TREE_OPERAND (loc, 1)))
17707 /* Big unsigned numbers can fit in HOST_WIDE_INT but it may be
17708 smarter to encode their opposite. The DW_OP_plus_uconst operation
17709 takes 1 + X bytes, X being the size of the ULEB128 addend. On the
17710 other hand, a "<push literal>; DW_OP_minus" pattern takes 1 + Y
17711 bytes, Y being the size of the operation that pushes the opposite
17712 of the addend. So let's choose the smallest representation. */
17713 const tree tree_addend = TREE_OPERAND (loc, 1);
17714 offset_int wi_addend;
17715 HOST_WIDE_INT shwi_addend;
17716 dw_loc_descr_ref loc_naddend;
17718 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17719 if (list_ret == 0)
17720 return 0;
17722 /* Try to get the literal to push. It is the opposite of the addend,
17723 so as we rely on wrapping during DWARF evaluation, first decode
17724 the literal as a "DWARF-sized" signed number. */
17725 wi_addend = wi::to_offset (tree_addend);
17726 wi_addend = wi::sext (wi_addend, DWARF2_ADDR_SIZE * 8);
17727 shwi_addend = wi_addend.to_shwi ();
17728 loc_naddend = (shwi_addend != INTTYPE_MINIMUM (HOST_WIDE_INT))
17729 ? int_loc_descriptor (-shwi_addend)
17730 : NULL;
17732 if (loc_naddend != NULL
17733 && ((unsigned) size_of_uleb128 (shwi_addend)
17734 > size_of_loc_descr (loc_naddend)))
17736 add_loc_descr_to_each (list_ret, loc_naddend);
17737 add_loc_descr_to_each (list_ret,
17738 new_loc_descr (DW_OP_minus, 0, 0));
17740 else
17742 for (dw_loc_descr_ref loc_cur = loc_naddend; loc_cur != NULL; )
17744 loc_naddend = loc_cur;
17745 loc_cur = loc_cur->dw_loc_next;
17746 ggc_free (loc_naddend);
17748 loc_list_plus_const (list_ret, wi_addend.to_shwi ());
17750 break;
17753 op = DW_OP_plus;
17754 goto do_binop;
17756 case LE_EXPR:
17757 op = DW_OP_le;
17758 goto do_comp_binop;
17760 case GE_EXPR:
17761 op = DW_OP_ge;
17762 goto do_comp_binop;
17764 case LT_EXPR:
17765 op = DW_OP_lt;
17766 goto do_comp_binop;
17768 case GT_EXPR:
17769 op = DW_OP_gt;
17770 goto do_comp_binop;
17772 do_comp_binop:
17773 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
17775 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
17776 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
17777 list_ret = loc_list_from_uint_comparison (list_ret, list_ret1,
17778 TREE_CODE (loc));
17779 break;
17781 else
17782 goto do_binop;
17784 case EQ_EXPR:
17785 op = DW_OP_eq;
17786 goto do_binop;
17788 case NE_EXPR:
17789 op = DW_OP_ne;
17790 goto do_binop;
17792 do_binop:
17793 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17794 list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
17795 if (list_ret == 0 || list_ret1 == 0)
17796 return 0;
17798 add_loc_list (&list_ret, list_ret1);
17799 if (list_ret == 0)
17800 return 0;
17801 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
17802 break;
17804 case TRUTH_NOT_EXPR:
17805 case BIT_NOT_EXPR:
17806 op = DW_OP_not;
17807 goto do_unop;
17809 case ABS_EXPR:
17810 op = DW_OP_abs;
17811 goto do_unop;
17813 case NEGATE_EXPR:
17814 op = DW_OP_neg;
17815 goto do_unop;
17817 do_unop:
17818 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17819 if (list_ret == 0)
17820 return 0;
17822 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
17823 break;
17825 case MIN_EXPR:
17826 case MAX_EXPR:
17828 const enum tree_code code =
17829 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
17831 loc = build3 (COND_EXPR, TREE_TYPE (loc),
17832 build2 (code, integer_type_node,
17833 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
17834 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
17837 /* fall through */
17839 case COND_EXPR:
17841 dw_loc_descr_ref lhs
17842 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0, context);
17843 dw_loc_list_ref rhs
17844 = loc_list_from_tree_1 (TREE_OPERAND (loc, 2), 0, context);
17845 dw_loc_descr_ref bra_node, jump_node, tmp;
17847 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17848 if (list_ret == 0 || lhs == 0 || rhs == 0)
17849 return 0;
17851 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
17852 add_loc_descr_to_each (list_ret, bra_node);
17854 add_loc_list (&list_ret, rhs);
17855 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
17856 add_loc_descr_to_each (list_ret, jump_node);
17858 add_loc_descr_to_each (list_ret, lhs);
17859 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
17860 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
17862 /* ??? Need a node to point the skip at. Use a nop. */
17863 tmp = new_loc_descr (DW_OP_nop, 0, 0);
17864 add_loc_descr_to_each (list_ret, tmp);
17865 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
17866 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
17868 break;
17870 case FIX_TRUNC_EXPR:
17871 return 0;
17873 default:
17874 /* Leave front-end specific codes as simply unknown. This comes
17875 up, for instance, with the C STMT_EXPR. */
17876 if ((unsigned int) TREE_CODE (loc)
17877 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
17879 expansion_failed (loc, NULL_RTX,
17880 "language specific tree node");
17881 return 0;
17884 /* Otherwise this is a generic code; we should just lists all of
17885 these explicitly. We forgot one. */
17886 if (flag_checking)
17887 gcc_unreachable ();
17889 /* In a release build, we want to degrade gracefully: better to
17890 generate incomplete debugging information than to crash. */
17891 return NULL;
17894 if (!ret && !list_ret)
17895 return 0;
17897 if (want_address == 2 && !have_address
17898 && (dwarf_version >= 4 || !dwarf_strict))
17900 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
17902 expansion_failed (loc, NULL_RTX,
17903 "DWARF address size mismatch");
17904 return 0;
17906 if (ret)
17907 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
17908 else
17909 add_loc_descr_to_each (list_ret,
17910 new_loc_descr (DW_OP_stack_value, 0, 0));
17911 have_address = 1;
17913 /* Show if we can't fill the request for an address. */
17914 if (want_address && !have_address)
17916 expansion_failed (loc, NULL_RTX,
17917 "Want address and only have value");
17918 return 0;
17921 gcc_assert (!ret || !list_ret);
17923 /* If we've got an address and don't want one, dereference. */
17924 if (!want_address && have_address)
17926 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
17928 if (size > DWARF2_ADDR_SIZE || size == -1)
17930 expansion_failed (loc, NULL_RTX,
17931 "DWARF address size mismatch");
17932 return 0;
17934 else if (size == DWARF2_ADDR_SIZE)
17935 op = DW_OP_deref;
17936 else
17937 op = DW_OP_deref_size;
17939 if (ret)
17940 add_loc_descr (&ret, new_loc_descr (op, size, 0));
17941 else
17942 add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
17944 if (ret)
17945 list_ret = new_loc_list (ret, NULL, NULL, NULL);
17947 return list_ret;
17950 /* Likewise, but strip useless DW_OP_nop operations in the resulting
17951 expressions. */
17953 static dw_loc_list_ref
17954 loc_list_from_tree (tree loc, int want_address,
17955 struct loc_descr_context *context)
17957 dw_loc_list_ref result = loc_list_from_tree_1 (loc, want_address, context);
17959 for (dw_loc_list_ref loc_cur = result;
17960 loc_cur != NULL; loc_cur = loc_cur->dw_loc_next)
17961 loc_descr_without_nops (loc_cur->expr);
17962 return result;
17965 /* Same as above but return only single location expression. */
17966 static dw_loc_descr_ref
17967 loc_descriptor_from_tree (tree loc, int want_address,
17968 struct loc_descr_context *context)
17970 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address, context);
17971 if (!ret)
17972 return NULL;
17973 if (ret->dw_loc_next)
17975 expansion_failed (loc, NULL_RTX,
17976 "Location list where only loc descriptor needed");
17977 return NULL;
17979 return ret->expr;
17982 /* Given a value, round it up to the lowest multiple of `boundary'
17983 which is not less than the value itself. */
17985 static inline HOST_WIDE_INT
17986 ceiling (HOST_WIDE_INT value, unsigned int boundary)
17988 return (((value + boundary - 1) / boundary) * boundary);
17991 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
17992 pointer to the declared type for the relevant field variable, or return
17993 `integer_type_node' if the given node turns out to be an
17994 ERROR_MARK node. */
17996 static inline tree
17997 field_type (const_tree decl)
17999 tree type;
18001 if (TREE_CODE (decl) == ERROR_MARK)
18002 return integer_type_node;
18004 type = DECL_BIT_FIELD_TYPE (decl);
18005 if (type == NULL_TREE)
18006 type = TREE_TYPE (decl);
18008 return type;
18011 /* Given a pointer to a tree node, return the alignment in bits for
18012 it, or else return BITS_PER_WORD if the node actually turns out to
18013 be an ERROR_MARK node. */
18015 static inline unsigned
18016 simple_type_align_in_bits (const_tree type)
18018 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
18021 static inline unsigned
18022 simple_decl_align_in_bits (const_tree decl)
18024 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
18027 /* Return the result of rounding T up to ALIGN. */
18029 static inline offset_int
18030 round_up_to_align (const offset_int &t, unsigned int align)
18032 return wi::udiv_trunc (t + align - 1, align) * align;
18035 /* Compute the size of TYPE in bytes. If possible, return NULL and store the
18036 size as an integer constant in CST_SIZE. Otherwise, if possible, return a
18037 DWARF expression that computes the size. Return NULL and set CST_SIZE to -1
18038 if we fail to return the size in one of these two forms. */
18040 static dw_loc_descr_ref
18041 type_byte_size (const_tree type, HOST_WIDE_INT *cst_size)
18043 tree tree_size;
18044 struct loc_descr_context ctx;
18046 /* Return a constant integer in priority, if possible. */
18047 *cst_size = int_size_in_bytes (type);
18048 if (*cst_size != -1)
18049 return NULL;
18051 ctx.context_type = const_cast<tree> (type);
18052 ctx.base_decl = NULL_TREE;
18053 ctx.dpi = NULL;
18054 ctx.placeholder_arg = false;
18055 ctx.placeholder_seen = false;
18057 type = TYPE_MAIN_VARIANT (type);
18058 tree_size = TYPE_SIZE_UNIT (type);
18059 return ((tree_size != NULL_TREE)
18060 ? loc_descriptor_from_tree (tree_size, 0, &ctx)
18061 : NULL);
18064 /* Helper structure for RECORD_TYPE processing. */
18065 struct vlr_context
18067 /* Root RECORD_TYPE. It is needed to generate data member location
18068 descriptions in variable-length records (VLR), but also to cope with
18069 variants, which are composed of nested structures multiplexed with
18070 QUAL_UNION_TYPE nodes. Each time such a structure is passed to a
18071 function processing a FIELD_DECL, it is required to be non null. */
18072 tree struct_type;
18073 /* When generating a variant part in a RECORD_TYPE (i.e. a nested
18074 QUAL_UNION_TYPE), this holds an expression that computes the offset for
18075 this variant part as part of the root record (in storage units). For
18076 regular records, it must be NULL_TREE. */
18077 tree variant_part_offset;
18080 /* Given a pointer to a FIELD_DECL, compute the byte offset of the lowest
18081 addressed byte of the "containing object" for the given FIELD_DECL. If
18082 possible, return a native constant through CST_OFFSET (in which case NULL is
18083 returned); otherwise return a DWARF expression that computes the offset.
18085 Set *CST_OFFSET to 0 and return NULL if we are unable to determine what
18086 that offset is, either because the argument turns out to be a pointer to an
18087 ERROR_MARK node, or because the offset expression is too complex for us.
18089 CTX is required: see the comment for VLR_CONTEXT. */
18091 static dw_loc_descr_ref
18092 field_byte_offset (const_tree decl, struct vlr_context *ctx,
18093 HOST_WIDE_INT *cst_offset)
18095 tree tree_result;
18096 dw_loc_list_ref loc_result;
18098 *cst_offset = 0;
18100 if (TREE_CODE (decl) == ERROR_MARK)
18101 return NULL;
18102 else
18103 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
18105 /* We cannot handle variable bit offsets at the moment, so abort if it's the
18106 case. */
18107 if (TREE_CODE (DECL_FIELD_BIT_OFFSET (decl)) != INTEGER_CST)
18108 return NULL;
18110 #ifdef PCC_BITFIELD_TYPE_MATTERS
18111 /* We used to handle only constant offsets in all cases. Now, we handle
18112 properly dynamic byte offsets only when PCC bitfield type doesn't
18113 matter. */
18114 if (PCC_BITFIELD_TYPE_MATTERS
18115 && TREE_CODE (DECL_FIELD_OFFSET (decl)) == INTEGER_CST)
18117 offset_int object_offset_in_bits;
18118 offset_int object_offset_in_bytes;
18119 offset_int bitpos_int;
18120 tree type;
18121 tree field_size_tree;
18122 offset_int deepest_bitpos;
18123 offset_int field_size_in_bits;
18124 unsigned int type_align_in_bits;
18125 unsigned int decl_align_in_bits;
18126 offset_int type_size_in_bits;
18128 bitpos_int = wi::to_offset (bit_position (decl));
18129 type = field_type (decl);
18130 type_size_in_bits = offset_int_type_size_in_bits (type);
18131 type_align_in_bits = simple_type_align_in_bits (type);
18133 field_size_tree = DECL_SIZE (decl);
18135 /* The size could be unspecified if there was an error, or for
18136 a flexible array member. */
18137 if (!field_size_tree)
18138 field_size_tree = bitsize_zero_node;
18140 /* If the size of the field is not constant, use the type size. */
18141 if (TREE_CODE (field_size_tree) == INTEGER_CST)
18142 field_size_in_bits = wi::to_offset (field_size_tree);
18143 else
18144 field_size_in_bits = type_size_in_bits;
18146 decl_align_in_bits = simple_decl_align_in_bits (decl);
18148 /* The GCC front-end doesn't make any attempt to keep track of the
18149 starting bit offset (relative to the start of the containing
18150 structure type) of the hypothetical "containing object" for a
18151 bit-field. Thus, when computing the byte offset value for the
18152 start of the "containing object" of a bit-field, we must deduce
18153 this information on our own. This can be rather tricky to do in
18154 some cases. For example, handling the following structure type
18155 definition when compiling for an i386/i486 target (which only
18156 aligns long long's to 32-bit boundaries) can be very tricky:
18158 struct S { int field1; long long field2:31; };
18160 Fortunately, there is a simple rule-of-thumb which can be used
18161 in such cases. When compiling for an i386/i486, GCC will
18162 allocate 8 bytes for the structure shown above. It decides to
18163 do this based upon one simple rule for bit-field allocation.
18164 GCC allocates each "containing object" for each bit-field at
18165 the first (i.e. lowest addressed) legitimate alignment boundary
18166 (based upon the required minimum alignment for the declared
18167 type of the field) which it can possibly use, subject to the
18168 condition that there is still enough available space remaining
18169 in the containing object (when allocated at the selected point)
18170 to fully accommodate all of the bits of the bit-field itself.
18172 This simple rule makes it obvious why GCC allocates 8 bytes for
18173 each object of the structure type shown above. When looking
18174 for a place to allocate the "containing object" for `field2',
18175 the compiler simply tries to allocate a 64-bit "containing
18176 object" at each successive 32-bit boundary (starting at zero)
18177 until it finds a place to allocate that 64- bit field such that
18178 at least 31 contiguous (and previously unallocated) bits remain
18179 within that selected 64 bit field. (As it turns out, for the
18180 example above, the compiler finds it is OK to allocate the
18181 "containing object" 64-bit field at bit-offset zero within the
18182 structure type.)
18184 Here we attempt to work backwards from the limited set of facts
18185 we're given, and we try to deduce from those facts, where GCC
18186 must have believed that the containing object started (within
18187 the structure type). The value we deduce is then used (by the
18188 callers of this routine) to generate DW_AT_location and
18189 DW_AT_bit_offset attributes for fields (both bit-fields and, in
18190 the case of DW_AT_location, regular fields as well). */
18192 /* Figure out the bit-distance from the start of the structure to
18193 the "deepest" bit of the bit-field. */
18194 deepest_bitpos = bitpos_int + field_size_in_bits;
18196 /* This is the tricky part. Use some fancy footwork to deduce
18197 where the lowest addressed bit of the containing object must
18198 be. */
18199 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
18201 /* Round up to type_align by default. This works best for
18202 bitfields. */
18203 object_offset_in_bits
18204 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
18206 if (wi::gtu_p (object_offset_in_bits, bitpos_int))
18208 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
18210 /* Round up to decl_align instead. */
18211 object_offset_in_bits
18212 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
18215 object_offset_in_bytes
18216 = wi::lrshift (object_offset_in_bits, LOG2_BITS_PER_UNIT);
18217 if (ctx->variant_part_offset == NULL_TREE)
18219 *cst_offset = object_offset_in_bytes.to_shwi ();
18220 return NULL;
18222 tree_result = wide_int_to_tree (sizetype, object_offset_in_bytes);
18224 else
18225 #endif /* PCC_BITFIELD_TYPE_MATTERS */
18226 tree_result = byte_position (decl);
18228 if (ctx->variant_part_offset != NULL_TREE)
18229 tree_result = fold_build2 (PLUS_EXPR, TREE_TYPE (tree_result),
18230 ctx->variant_part_offset, tree_result);
18232 /* If the byte offset is a constant, it's simplier to handle a native
18233 constant rather than a DWARF expression. */
18234 if (TREE_CODE (tree_result) == INTEGER_CST)
18236 *cst_offset = wi::to_offset (tree_result).to_shwi ();
18237 return NULL;
18239 struct loc_descr_context loc_ctx = {
18240 ctx->struct_type, /* context_type */
18241 NULL_TREE, /* base_decl */
18242 NULL, /* dpi */
18243 false, /* placeholder_arg */
18244 false /* placeholder_seen */
18246 loc_result = loc_list_from_tree (tree_result, 0, &loc_ctx);
18248 /* We want a DWARF expression: abort if we only have a location list with
18249 multiple elements. */
18250 if (!loc_result || !single_element_loc_list_p (loc_result))
18251 return NULL;
18252 else
18253 return loc_result->expr;
18256 /* The following routines define various Dwarf attributes and any data
18257 associated with them. */
18259 /* Add a location description attribute value to a DIE.
18261 This emits location attributes suitable for whole variables and
18262 whole parameters. Note that the location attributes for struct fields are
18263 generated by the routine `data_member_location_attribute' below. */
18265 static inline void
18266 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
18267 dw_loc_list_ref descr)
18269 if (descr == 0)
18270 return;
18271 if (single_element_loc_list_p (descr))
18272 add_AT_loc (die, attr_kind, descr->expr);
18273 else
18274 add_AT_loc_list (die, attr_kind, descr);
18277 /* Add DW_AT_accessibility attribute to DIE if needed. */
18279 static void
18280 add_accessibility_attribute (dw_die_ref die, tree decl)
18282 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
18283 children, otherwise the default is DW_ACCESS_public. In DWARF2
18284 the default has always been DW_ACCESS_public. */
18285 if (TREE_PROTECTED (decl))
18286 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
18287 else if (TREE_PRIVATE (decl))
18289 if (dwarf_version == 2
18290 || die->die_parent == NULL
18291 || die->die_parent->die_tag != DW_TAG_class_type)
18292 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
18294 else if (dwarf_version > 2
18295 && die->die_parent
18296 && die->die_parent->die_tag == DW_TAG_class_type)
18297 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
18300 /* Attach the specialized form of location attribute used for data members of
18301 struct and union types. In the special case of a FIELD_DECL node which
18302 represents a bit-field, the "offset" part of this special location
18303 descriptor must indicate the distance in bytes from the lowest-addressed
18304 byte of the containing struct or union type to the lowest-addressed byte of
18305 the "containing object" for the bit-field. (See the `field_byte_offset'
18306 function above).
18308 For any given bit-field, the "containing object" is a hypothetical object
18309 (of some integral or enum type) within which the given bit-field lives. The
18310 type of this hypothetical "containing object" is always the same as the
18311 declared type of the individual bit-field itself (for GCC anyway... the
18312 DWARF spec doesn't actually mandate this). Note that it is the size (in
18313 bytes) of the hypothetical "containing object" which will be given in the
18314 DW_AT_byte_size attribute for this bit-field. (See the
18315 `byte_size_attribute' function below.) It is also used when calculating the
18316 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
18317 function below.)
18319 CTX is required: see the comment for VLR_CONTEXT. */
18321 static void
18322 add_data_member_location_attribute (dw_die_ref die,
18323 tree decl,
18324 struct vlr_context *ctx)
18326 HOST_WIDE_INT offset;
18327 dw_loc_descr_ref loc_descr = 0;
18329 if (TREE_CODE (decl) == TREE_BINFO)
18331 /* We're working on the TAG_inheritance for a base class. */
18332 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
18334 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
18335 aren't at a fixed offset from all (sub)objects of the same
18336 type. We need to extract the appropriate offset from our
18337 vtable. The following dwarf expression means
18339 BaseAddr = ObAddr + *((*ObAddr) - Offset)
18341 This is specific to the V3 ABI, of course. */
18343 dw_loc_descr_ref tmp;
18345 /* Make a copy of the object address. */
18346 tmp = new_loc_descr (DW_OP_dup, 0, 0);
18347 add_loc_descr (&loc_descr, tmp);
18349 /* Extract the vtable address. */
18350 tmp = new_loc_descr (DW_OP_deref, 0, 0);
18351 add_loc_descr (&loc_descr, tmp);
18353 /* Calculate the address of the offset. */
18354 offset = tree_to_shwi (BINFO_VPTR_FIELD (decl));
18355 gcc_assert (offset < 0);
18357 tmp = int_loc_descriptor (-offset);
18358 add_loc_descr (&loc_descr, tmp);
18359 tmp = new_loc_descr (DW_OP_minus, 0, 0);
18360 add_loc_descr (&loc_descr, tmp);
18362 /* Extract the offset. */
18363 tmp = new_loc_descr (DW_OP_deref, 0, 0);
18364 add_loc_descr (&loc_descr, tmp);
18366 /* Add it to the object address. */
18367 tmp = new_loc_descr (DW_OP_plus, 0, 0);
18368 add_loc_descr (&loc_descr, tmp);
18370 else
18371 offset = tree_to_shwi (BINFO_OFFSET (decl));
18373 else
18375 loc_descr = field_byte_offset (decl, ctx, &offset);
18377 /* If loc_descr is available then we know the field offset is dynamic.
18378 However, GDB does not handle dynamic field offsets very well at the
18379 moment. */
18380 if (loc_descr != NULL && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL)
18382 loc_descr = NULL;
18383 offset = 0;
18386 /* Data member location evalutation starts with the base address on the
18387 stack. Compute the field offset and add it to this base address. */
18388 else if (loc_descr != NULL)
18389 add_loc_descr (&loc_descr, new_loc_descr (DW_OP_plus, 0, 0));
18392 if (! loc_descr)
18394 /* While DW_AT_data_bit_offset has been added already in DWARF4,
18395 e.g. GDB only added support to it in November 2016. For DWARF5
18396 we need newer debug info consumers anyway. We might change this
18397 to dwarf_version >= 4 once most consumers catched up. */
18398 if (dwarf_version >= 5
18399 && TREE_CODE (decl) == FIELD_DECL
18400 && DECL_BIT_FIELD_TYPE (decl))
18402 tree off = bit_position (decl);
18403 if (tree_fits_uhwi_p (off) && get_AT (die, DW_AT_bit_size))
18405 remove_AT (die, DW_AT_byte_size);
18406 remove_AT (die, DW_AT_bit_offset);
18407 add_AT_unsigned (die, DW_AT_data_bit_offset, tree_to_uhwi (off));
18408 return;
18411 if (dwarf_version > 2)
18413 /* Don't need to output a location expression, just the constant. */
18414 if (offset < 0)
18415 add_AT_int (die, DW_AT_data_member_location, offset);
18416 else
18417 add_AT_unsigned (die, DW_AT_data_member_location, offset);
18418 return;
18420 else
18422 enum dwarf_location_atom op;
18424 /* The DWARF2 standard says that we should assume that the structure
18425 address is already on the stack, so we can specify a structure
18426 field address by using DW_OP_plus_uconst. */
18427 op = DW_OP_plus_uconst;
18428 loc_descr = new_loc_descr (op, offset, 0);
18432 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
18435 /* Writes integer values to dw_vec_const array. */
18437 static void
18438 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
18440 while (size != 0)
18442 *dest++ = val & 0xff;
18443 val >>= 8;
18444 --size;
18448 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
18450 static HOST_WIDE_INT
18451 extract_int (const unsigned char *src, unsigned int size)
18453 HOST_WIDE_INT val = 0;
18455 src += size;
18456 while (size != 0)
18458 val <<= 8;
18459 val |= *--src & 0xff;
18460 --size;
18462 return val;
18465 /* Writes wide_int values to dw_vec_const array. */
18467 static void
18468 insert_wide_int (const wide_int &val, unsigned char *dest, int elt_size)
18470 int i;
18472 if (elt_size <= HOST_BITS_PER_WIDE_INT/BITS_PER_UNIT)
18474 insert_int ((HOST_WIDE_INT) val.elt (0), elt_size, dest);
18475 return;
18478 /* We'd have to extend this code to support odd sizes. */
18479 gcc_assert (elt_size % (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT) == 0);
18481 int n = elt_size / (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
18483 if (WORDS_BIG_ENDIAN)
18484 for (i = n - 1; i >= 0; i--)
18486 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
18487 dest += sizeof (HOST_WIDE_INT);
18489 else
18490 for (i = 0; i < n; i++)
18492 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
18493 dest += sizeof (HOST_WIDE_INT);
18497 /* Writes floating point values to dw_vec_const array. */
18499 static void
18500 insert_float (const_rtx rtl, unsigned char *array)
18502 long val[4];
18503 int i;
18505 real_to_target (val, CONST_DOUBLE_REAL_VALUE (rtl), GET_MODE (rtl));
18507 /* real_to_target puts 32-bit pieces in each long. Pack them. */
18508 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
18510 insert_int (val[i], 4, array);
18511 array += 4;
18515 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
18516 does not have a "location" either in memory or in a register. These
18517 things can arise in GNU C when a constant is passed as an actual parameter
18518 to an inlined function. They can also arise in C++ where declared
18519 constants do not necessarily get memory "homes". */
18521 static bool
18522 add_const_value_attribute (dw_die_ref die, rtx rtl)
18524 switch (GET_CODE (rtl))
18526 case CONST_INT:
18528 HOST_WIDE_INT val = INTVAL (rtl);
18530 if (val < 0)
18531 add_AT_int (die, DW_AT_const_value, val);
18532 else
18533 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
18535 return true;
18537 case CONST_WIDE_INT:
18539 wide_int w1 = rtx_mode_t (rtl, MAX_MODE_INT);
18540 unsigned int prec = MIN (wi::min_precision (w1, UNSIGNED),
18541 (unsigned int)CONST_WIDE_INT_NUNITS (rtl) * HOST_BITS_PER_WIDE_INT);
18542 wide_int w = wi::zext (w1, prec);
18543 add_AT_wide (die, DW_AT_const_value, w);
18545 return true;
18547 case CONST_DOUBLE:
18548 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
18549 floating-point constant. A CONST_DOUBLE is used whenever the
18550 constant requires more than one word in order to be adequately
18551 represented. */
18553 machine_mode mode = GET_MODE (rtl);
18555 if (TARGET_SUPPORTS_WIDE_INT == 0 && !SCALAR_FLOAT_MODE_P (mode))
18556 add_AT_double (die, DW_AT_const_value,
18557 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
18558 else
18560 unsigned int length = GET_MODE_SIZE (mode);
18561 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
18563 insert_float (rtl, array);
18564 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
18567 return true;
18569 case CONST_VECTOR:
18571 machine_mode mode = GET_MODE (rtl);
18572 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
18573 unsigned int length = CONST_VECTOR_NUNITS (rtl);
18574 unsigned char *array
18575 = ggc_vec_alloc<unsigned char> (length * elt_size);
18576 unsigned int i;
18577 unsigned char *p;
18578 machine_mode imode = GET_MODE_INNER (mode);
18580 switch (GET_MODE_CLASS (mode))
18582 case MODE_VECTOR_INT:
18583 for (i = 0, p = array; i < length; i++, p += elt_size)
18585 rtx elt = CONST_VECTOR_ELT (rtl, i);
18586 insert_wide_int (rtx_mode_t (elt, imode), p, elt_size);
18588 break;
18590 case MODE_VECTOR_FLOAT:
18591 for (i = 0, p = array; i < length; i++, p += elt_size)
18593 rtx elt = CONST_VECTOR_ELT (rtl, i);
18594 insert_float (elt, p);
18596 break;
18598 default:
18599 gcc_unreachable ();
18602 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
18604 return true;
18606 case CONST_STRING:
18607 if (dwarf_version >= 4 || !dwarf_strict)
18609 dw_loc_descr_ref loc_result;
18610 resolve_one_addr (&rtl);
18611 rtl_addr:
18612 loc_result = new_addr_loc_descr (rtl, dtprel_false);
18613 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
18614 add_AT_loc (die, DW_AT_location, loc_result);
18615 vec_safe_push (used_rtx_array, rtl);
18616 return true;
18618 return false;
18620 case CONST:
18621 if (CONSTANT_P (XEXP (rtl, 0)))
18622 return add_const_value_attribute (die, XEXP (rtl, 0));
18623 /* FALLTHROUGH */
18624 case SYMBOL_REF:
18625 if (!const_ok_for_output (rtl))
18626 return false;
18627 /* FALLTHROUGH */
18628 case LABEL_REF:
18629 if (dwarf_version >= 4 || !dwarf_strict)
18630 goto rtl_addr;
18631 return false;
18633 case PLUS:
18634 /* In cases where an inlined instance of an inline function is passed
18635 the address of an `auto' variable (which is local to the caller) we
18636 can get a situation where the DECL_RTL of the artificial local
18637 variable (for the inlining) which acts as a stand-in for the
18638 corresponding formal parameter (of the inline function) will look
18639 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
18640 exactly a compile-time constant expression, but it isn't the address
18641 of the (artificial) local variable either. Rather, it represents the
18642 *value* which the artificial local variable always has during its
18643 lifetime. We currently have no way to represent such quasi-constant
18644 values in Dwarf, so for now we just punt and generate nothing. */
18645 return false;
18647 case HIGH:
18648 case CONST_FIXED:
18649 return false;
18651 case MEM:
18652 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
18653 && MEM_READONLY_P (rtl)
18654 && GET_MODE (rtl) == BLKmode)
18656 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
18657 return true;
18659 return false;
18661 default:
18662 /* No other kinds of rtx should be possible here. */
18663 gcc_unreachable ();
18665 return false;
18668 /* Determine whether the evaluation of EXPR references any variables
18669 or functions which aren't otherwise used (and therefore may not be
18670 output). */
18671 static tree
18672 reference_to_unused (tree * tp, int * walk_subtrees,
18673 void * data ATTRIBUTE_UNUSED)
18675 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
18676 *walk_subtrees = 0;
18678 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
18679 && ! TREE_ASM_WRITTEN (*tp))
18680 return *tp;
18681 /* ??? The C++ FE emits debug information for using decls, so
18682 putting gcc_unreachable here falls over. See PR31899. For now
18683 be conservative. */
18684 else if (!symtab->global_info_ready && VAR_OR_FUNCTION_DECL_P (*tp))
18685 return *tp;
18686 else if (VAR_P (*tp))
18688 varpool_node *node = varpool_node::get (*tp);
18689 if (!node || !node->definition)
18690 return *tp;
18692 else if (TREE_CODE (*tp) == FUNCTION_DECL
18693 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
18695 /* The call graph machinery must have finished analyzing,
18696 optimizing and gimplifying the CU by now.
18697 So if *TP has no call graph node associated
18698 to it, it means *TP will not be emitted. */
18699 if (!cgraph_node::get (*tp))
18700 return *tp;
18702 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
18703 return *tp;
18705 return NULL_TREE;
18708 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
18709 for use in a later add_const_value_attribute call. */
18711 static rtx
18712 rtl_for_decl_init (tree init, tree type)
18714 rtx rtl = NULL_RTX;
18716 STRIP_NOPS (init);
18718 /* If a variable is initialized with a string constant without embedded
18719 zeros, build CONST_STRING. */
18720 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
18722 tree enttype = TREE_TYPE (type);
18723 tree domain = TYPE_DOMAIN (type);
18724 machine_mode mode = TYPE_MODE (enttype);
18726 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
18727 && domain
18728 && integer_zerop (TYPE_MIN_VALUE (domain))
18729 && compare_tree_int (TYPE_MAX_VALUE (domain),
18730 TREE_STRING_LENGTH (init) - 1) == 0
18731 && ((size_t) TREE_STRING_LENGTH (init)
18732 == strlen (TREE_STRING_POINTER (init)) + 1))
18734 rtl = gen_rtx_CONST_STRING (VOIDmode,
18735 ggc_strdup (TREE_STRING_POINTER (init)));
18736 rtl = gen_rtx_MEM (BLKmode, rtl);
18737 MEM_READONLY_P (rtl) = 1;
18740 /* Other aggregates, and complex values, could be represented using
18741 CONCAT: FIXME! */
18742 else if (AGGREGATE_TYPE_P (type)
18743 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
18744 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
18745 || TREE_CODE (type) == COMPLEX_TYPE)
18747 /* Vectors only work if their mode is supported by the target.
18748 FIXME: generic vectors ought to work too. */
18749 else if (TREE_CODE (type) == VECTOR_TYPE
18750 && !VECTOR_MODE_P (TYPE_MODE (type)))
18752 /* If the initializer is something that we know will expand into an
18753 immediate RTL constant, expand it now. We must be careful not to
18754 reference variables which won't be output. */
18755 else if (initializer_constant_valid_p (init, type)
18756 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
18758 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
18759 possible. */
18760 if (TREE_CODE (type) == VECTOR_TYPE)
18761 switch (TREE_CODE (init))
18763 case VECTOR_CST:
18764 break;
18765 case CONSTRUCTOR:
18766 if (TREE_CONSTANT (init))
18768 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
18769 bool constant_p = true;
18770 tree value;
18771 unsigned HOST_WIDE_INT ix;
18773 /* Even when ctor is constant, it might contain non-*_CST
18774 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
18775 belong into VECTOR_CST nodes. */
18776 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
18777 if (!CONSTANT_CLASS_P (value))
18779 constant_p = false;
18780 break;
18783 if (constant_p)
18785 init = build_vector_from_ctor (type, elts);
18786 break;
18789 /* FALLTHRU */
18791 default:
18792 return NULL;
18795 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
18797 /* If expand_expr returns a MEM, it wasn't immediate. */
18798 gcc_assert (!rtl || !MEM_P (rtl));
18801 return rtl;
18804 /* Generate RTL for the variable DECL to represent its location. */
18806 static rtx
18807 rtl_for_decl_location (tree decl)
18809 rtx rtl;
18811 /* Here we have to decide where we are going to say the parameter "lives"
18812 (as far as the debugger is concerned). We only have a couple of
18813 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
18815 DECL_RTL normally indicates where the parameter lives during most of the
18816 activation of the function. If optimization is enabled however, this
18817 could be either NULL or else a pseudo-reg. Both of those cases indicate
18818 that the parameter doesn't really live anywhere (as far as the code
18819 generation parts of GCC are concerned) during most of the function's
18820 activation. That will happen (for example) if the parameter is never
18821 referenced within the function.
18823 We could just generate a location descriptor here for all non-NULL
18824 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
18825 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
18826 where DECL_RTL is NULL or is a pseudo-reg.
18828 Note however that we can only get away with using DECL_INCOMING_RTL as
18829 a backup substitute for DECL_RTL in certain limited cases. In cases
18830 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
18831 we can be sure that the parameter was passed using the same type as it is
18832 declared to have within the function, and that its DECL_INCOMING_RTL
18833 points us to a place where a value of that type is passed.
18835 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
18836 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
18837 because in these cases DECL_INCOMING_RTL points us to a value of some
18838 type which is *different* from the type of the parameter itself. Thus,
18839 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
18840 such cases, the debugger would end up (for example) trying to fetch a
18841 `float' from a place which actually contains the first part of a
18842 `double'. That would lead to really incorrect and confusing
18843 output at debug-time.
18845 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
18846 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
18847 are a couple of exceptions however. On little-endian machines we can
18848 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
18849 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
18850 an integral type that is smaller than TREE_TYPE (decl). These cases arise
18851 when (on a little-endian machine) a non-prototyped function has a
18852 parameter declared to be of type `short' or `char'. In such cases,
18853 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
18854 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
18855 passed `int' value. If the debugger then uses that address to fetch
18856 a `short' or a `char' (on a little-endian machine) the result will be
18857 the correct data, so we allow for such exceptional cases below.
18859 Note that our goal here is to describe the place where the given formal
18860 parameter lives during most of the function's activation (i.e. between the
18861 end of the prologue and the start of the epilogue). We'll do that as best
18862 as we can. Note however that if the given formal parameter is modified
18863 sometime during the execution of the function, then a stack backtrace (at
18864 debug-time) will show the function as having been called with the *new*
18865 value rather than the value which was originally passed in. This happens
18866 rarely enough that it is not a major problem, but it *is* a problem, and
18867 I'd like to fix it.
18869 A future version of dwarf2out.c may generate two additional attributes for
18870 any given DW_TAG_formal_parameter DIE which will describe the "passed
18871 type" and the "passed location" for the given formal parameter in addition
18872 to the attributes we now generate to indicate the "declared type" and the
18873 "active location" for each parameter. This additional set of attributes
18874 could be used by debuggers for stack backtraces. Separately, note that
18875 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
18876 This happens (for example) for inlined-instances of inline function formal
18877 parameters which are never referenced. This really shouldn't be
18878 happening. All PARM_DECL nodes should get valid non-NULL
18879 DECL_INCOMING_RTL values. FIXME. */
18881 /* Use DECL_RTL as the "location" unless we find something better. */
18882 rtl = DECL_RTL_IF_SET (decl);
18884 /* When generating abstract instances, ignore everything except
18885 constants, symbols living in memory, and symbols living in
18886 fixed registers. */
18887 if (! reload_completed)
18889 if (rtl
18890 && (CONSTANT_P (rtl)
18891 || (MEM_P (rtl)
18892 && CONSTANT_P (XEXP (rtl, 0)))
18893 || (REG_P (rtl)
18894 && VAR_P (decl)
18895 && TREE_STATIC (decl))))
18897 rtl = targetm.delegitimize_address (rtl);
18898 return rtl;
18900 rtl = NULL_RTX;
18902 else if (TREE_CODE (decl) == PARM_DECL)
18904 if (rtl == NULL_RTX
18905 || is_pseudo_reg (rtl)
18906 || (MEM_P (rtl)
18907 && is_pseudo_reg (XEXP (rtl, 0))
18908 && DECL_INCOMING_RTL (decl)
18909 && MEM_P (DECL_INCOMING_RTL (decl))
18910 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
18912 tree declared_type = TREE_TYPE (decl);
18913 tree passed_type = DECL_ARG_TYPE (decl);
18914 machine_mode dmode = TYPE_MODE (declared_type);
18915 machine_mode pmode = TYPE_MODE (passed_type);
18917 /* This decl represents a formal parameter which was optimized out.
18918 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
18919 all cases where (rtl == NULL_RTX) just below. */
18920 if (dmode == pmode)
18921 rtl = DECL_INCOMING_RTL (decl);
18922 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
18923 && SCALAR_INT_MODE_P (dmode)
18924 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
18925 && DECL_INCOMING_RTL (decl))
18927 rtx inc = DECL_INCOMING_RTL (decl);
18928 if (REG_P (inc))
18929 rtl = inc;
18930 else if (MEM_P (inc))
18932 if (BYTES_BIG_ENDIAN)
18933 rtl = adjust_address_nv (inc, dmode,
18934 GET_MODE_SIZE (pmode)
18935 - GET_MODE_SIZE (dmode));
18936 else
18937 rtl = inc;
18942 /* If the parm was passed in registers, but lives on the stack, then
18943 make a big endian correction if the mode of the type of the
18944 parameter is not the same as the mode of the rtl. */
18945 /* ??? This is the same series of checks that are made in dbxout.c before
18946 we reach the big endian correction code there. It isn't clear if all
18947 of these checks are necessary here, but keeping them all is the safe
18948 thing to do. */
18949 else if (MEM_P (rtl)
18950 && XEXP (rtl, 0) != const0_rtx
18951 && ! CONSTANT_P (XEXP (rtl, 0))
18952 /* Not passed in memory. */
18953 && !MEM_P (DECL_INCOMING_RTL (decl))
18954 /* Not passed by invisible reference. */
18955 && (!REG_P (XEXP (rtl, 0))
18956 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
18957 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
18958 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
18959 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
18960 #endif
18962 /* Big endian correction check. */
18963 && BYTES_BIG_ENDIAN
18964 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
18965 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
18966 < UNITS_PER_WORD))
18968 machine_mode addr_mode = get_address_mode (rtl);
18969 int offset = (UNITS_PER_WORD
18970 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
18972 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
18973 plus_constant (addr_mode, XEXP (rtl, 0), offset));
18976 else if (VAR_P (decl)
18977 && rtl
18978 && MEM_P (rtl)
18979 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
18980 && BYTES_BIG_ENDIAN)
18982 machine_mode addr_mode = get_address_mode (rtl);
18983 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
18984 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
18986 /* If a variable is declared "register" yet is smaller than
18987 a register, then if we store the variable to memory, it
18988 looks like we're storing a register-sized value, when in
18989 fact we are not. We need to adjust the offset of the
18990 storage location to reflect the actual value's bytes,
18991 else gdb will not be able to display it. */
18992 if (rsize > dsize)
18993 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
18994 plus_constant (addr_mode, XEXP (rtl, 0),
18995 rsize - dsize));
18998 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
18999 and will have been substituted directly into all expressions that use it.
19000 C does not have such a concept, but C++ and other languages do. */
19001 if (!rtl && VAR_P (decl) && DECL_INITIAL (decl))
19002 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
19004 if (rtl)
19005 rtl = targetm.delegitimize_address (rtl);
19007 /* If we don't look past the constant pool, we risk emitting a
19008 reference to a constant pool entry that isn't referenced from
19009 code, and thus is not emitted. */
19010 if (rtl)
19011 rtl = avoid_constant_pool_reference (rtl);
19013 /* Try harder to get a rtl. If this symbol ends up not being emitted
19014 in the current CU, resolve_addr will remove the expression referencing
19015 it. */
19016 if (rtl == NULL_RTX
19017 && VAR_P (decl)
19018 && !DECL_EXTERNAL (decl)
19019 && TREE_STATIC (decl)
19020 && DECL_NAME (decl)
19021 && !DECL_HARD_REGISTER (decl)
19022 && DECL_MODE (decl) != VOIDmode)
19024 rtl = make_decl_rtl_for_debug (decl);
19025 if (!MEM_P (rtl)
19026 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
19027 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
19028 rtl = NULL_RTX;
19031 return rtl;
19034 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
19035 returned. If so, the decl for the COMMON block is returned, and the
19036 value is the offset into the common block for the symbol. */
19038 static tree
19039 fortran_common (tree decl, HOST_WIDE_INT *value)
19041 tree val_expr, cvar;
19042 machine_mode mode;
19043 HOST_WIDE_INT bitsize, bitpos;
19044 tree offset;
19045 int unsignedp, reversep, volatilep = 0;
19047 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
19048 it does not have a value (the offset into the common area), or if it
19049 is thread local (as opposed to global) then it isn't common, and shouldn't
19050 be handled as such. */
19051 if (!VAR_P (decl)
19052 || !TREE_STATIC (decl)
19053 || !DECL_HAS_VALUE_EXPR_P (decl)
19054 || !is_fortran ())
19055 return NULL_TREE;
19057 val_expr = DECL_VALUE_EXPR (decl);
19058 if (TREE_CODE (val_expr) != COMPONENT_REF)
19059 return NULL_TREE;
19061 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset, &mode,
19062 &unsignedp, &reversep, &volatilep);
19064 if (cvar == NULL_TREE
19065 || !VAR_P (cvar)
19066 || DECL_ARTIFICIAL (cvar)
19067 || !TREE_PUBLIC (cvar))
19068 return NULL_TREE;
19070 *value = 0;
19071 if (offset != NULL)
19073 if (!tree_fits_shwi_p (offset))
19074 return NULL_TREE;
19075 *value = tree_to_shwi (offset);
19077 if (bitpos != 0)
19078 *value += bitpos / BITS_PER_UNIT;
19080 return cvar;
19083 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
19084 data attribute for a variable or a parameter. We generate the
19085 DW_AT_const_value attribute only in those cases where the given variable
19086 or parameter does not have a true "location" either in memory or in a
19087 register. This can happen (for example) when a constant is passed as an
19088 actual argument in a call to an inline function. (It's possible that
19089 these things can crop up in other ways also.) Note that one type of
19090 constant value which can be passed into an inlined function is a constant
19091 pointer. This can happen for example if an actual argument in an inlined
19092 function call evaluates to a compile-time constant address.
19094 CACHE_P is true if it is worth caching the location list for DECL,
19095 so that future calls can reuse it rather than regenerate it from scratch.
19096 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
19097 since we will need to refer to them each time the function is inlined. */
19099 static bool
19100 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p)
19102 rtx rtl;
19103 dw_loc_list_ref list;
19104 var_loc_list *loc_list;
19105 cached_dw_loc_list *cache;
19107 if (early_dwarf)
19108 return false;
19110 if (TREE_CODE (decl) == ERROR_MARK)
19111 return false;
19113 if (get_AT (die, DW_AT_location)
19114 || get_AT (die, DW_AT_const_value))
19115 return true;
19117 gcc_assert (VAR_P (decl) || TREE_CODE (decl) == PARM_DECL
19118 || TREE_CODE (decl) == RESULT_DECL);
19120 /* Try to get some constant RTL for this decl, and use that as the value of
19121 the location. */
19123 rtl = rtl_for_decl_location (decl);
19124 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
19125 && add_const_value_attribute (die, rtl))
19126 return true;
19128 /* See if we have single element location list that is equivalent to
19129 a constant value. That way we are better to use add_const_value_attribute
19130 rather than expanding constant value equivalent. */
19131 loc_list = lookup_decl_loc (decl);
19132 if (loc_list
19133 && loc_list->first
19134 && loc_list->first->next == NULL
19135 && NOTE_P (loc_list->first->loc)
19136 && NOTE_VAR_LOCATION (loc_list->first->loc)
19137 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
19139 struct var_loc_node *node;
19141 node = loc_list->first;
19142 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
19143 if (GET_CODE (rtl) == EXPR_LIST)
19144 rtl = XEXP (rtl, 0);
19145 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
19146 && add_const_value_attribute (die, rtl))
19147 return true;
19149 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
19150 list several times. See if we've already cached the contents. */
19151 list = NULL;
19152 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
19153 cache_p = false;
19154 if (cache_p)
19156 cache = cached_dw_loc_list_table->find_with_hash (decl, DECL_UID (decl));
19157 if (cache)
19158 list = cache->loc_list;
19160 if (list == NULL)
19162 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2,
19163 NULL);
19164 /* It is usually worth caching this result if the decl is from
19165 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
19166 if (cache_p && list && list->dw_loc_next)
19168 cached_dw_loc_list **slot
19169 = cached_dw_loc_list_table->find_slot_with_hash (decl,
19170 DECL_UID (decl),
19171 INSERT);
19172 cache = ggc_cleared_alloc<cached_dw_loc_list> ();
19173 cache->decl_id = DECL_UID (decl);
19174 cache->loc_list = list;
19175 *slot = cache;
19178 if (list)
19180 add_AT_location_description (die, DW_AT_location, list);
19181 return true;
19183 /* None of that worked, so it must not really have a location;
19184 try adding a constant value attribute from the DECL_INITIAL. */
19185 return tree_add_const_value_attribute_for_decl (die, decl);
19188 /* Helper function for tree_add_const_value_attribute. Natively encode
19189 initializer INIT into an array. Return true if successful. */
19191 static bool
19192 native_encode_initializer (tree init, unsigned char *array, int size)
19194 tree type;
19196 if (init == NULL_TREE)
19197 return false;
19199 STRIP_NOPS (init);
19200 switch (TREE_CODE (init))
19202 case STRING_CST:
19203 type = TREE_TYPE (init);
19204 if (TREE_CODE (type) == ARRAY_TYPE)
19206 tree enttype = TREE_TYPE (type);
19207 machine_mode mode = TYPE_MODE (enttype);
19209 if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
19210 return false;
19211 if (int_size_in_bytes (type) != size)
19212 return false;
19213 if (size > TREE_STRING_LENGTH (init))
19215 memcpy (array, TREE_STRING_POINTER (init),
19216 TREE_STRING_LENGTH (init));
19217 memset (array + TREE_STRING_LENGTH (init),
19218 '\0', size - TREE_STRING_LENGTH (init));
19220 else
19221 memcpy (array, TREE_STRING_POINTER (init), size);
19222 return true;
19224 return false;
19225 case CONSTRUCTOR:
19226 type = TREE_TYPE (init);
19227 if (int_size_in_bytes (type) != size)
19228 return false;
19229 if (TREE_CODE (type) == ARRAY_TYPE)
19231 HOST_WIDE_INT min_index;
19232 unsigned HOST_WIDE_INT cnt;
19233 int curpos = 0, fieldsize;
19234 constructor_elt *ce;
19236 if (TYPE_DOMAIN (type) == NULL_TREE
19237 || !tree_fits_shwi_p (TYPE_MIN_VALUE (TYPE_DOMAIN (type))))
19238 return false;
19240 fieldsize = int_size_in_bytes (TREE_TYPE (type));
19241 if (fieldsize <= 0)
19242 return false;
19244 min_index = tree_to_shwi (TYPE_MIN_VALUE (TYPE_DOMAIN (type)));
19245 memset (array, '\0', size);
19246 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
19248 tree val = ce->value;
19249 tree index = ce->index;
19250 int pos = curpos;
19251 if (index && TREE_CODE (index) == RANGE_EXPR)
19252 pos = (tree_to_shwi (TREE_OPERAND (index, 0)) - min_index)
19253 * fieldsize;
19254 else if (index)
19255 pos = (tree_to_shwi (index) - min_index) * fieldsize;
19257 if (val)
19259 STRIP_NOPS (val);
19260 if (!native_encode_initializer (val, array + pos, fieldsize))
19261 return false;
19263 curpos = pos + fieldsize;
19264 if (index && TREE_CODE (index) == RANGE_EXPR)
19266 int count = tree_to_shwi (TREE_OPERAND (index, 1))
19267 - tree_to_shwi (TREE_OPERAND (index, 0));
19268 while (count-- > 0)
19270 if (val)
19271 memcpy (array + curpos, array + pos, fieldsize);
19272 curpos += fieldsize;
19275 gcc_assert (curpos <= size);
19277 return true;
19279 else if (TREE_CODE (type) == RECORD_TYPE
19280 || TREE_CODE (type) == UNION_TYPE)
19282 tree field = NULL_TREE;
19283 unsigned HOST_WIDE_INT cnt;
19284 constructor_elt *ce;
19286 if (int_size_in_bytes (type) != size)
19287 return false;
19289 if (TREE_CODE (type) == RECORD_TYPE)
19290 field = TYPE_FIELDS (type);
19292 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
19294 tree val = ce->value;
19295 int pos, fieldsize;
19297 if (ce->index != 0)
19298 field = ce->index;
19300 if (val)
19301 STRIP_NOPS (val);
19303 if (field == NULL_TREE || DECL_BIT_FIELD (field))
19304 return false;
19306 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
19307 && TYPE_DOMAIN (TREE_TYPE (field))
19308 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
19309 return false;
19310 else if (DECL_SIZE_UNIT (field) == NULL_TREE
19311 || !tree_fits_shwi_p (DECL_SIZE_UNIT (field)))
19312 return false;
19313 fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
19314 pos = int_byte_position (field);
19315 gcc_assert (pos + fieldsize <= size);
19316 if (val && fieldsize != 0
19317 && !native_encode_initializer (val, array + pos, fieldsize))
19318 return false;
19320 return true;
19322 return false;
19323 case VIEW_CONVERT_EXPR:
19324 case NON_LVALUE_EXPR:
19325 return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
19326 default:
19327 return native_encode_expr (init, array, size) == size;
19331 /* Attach a DW_AT_const_value attribute to DIE. The value of the
19332 attribute is the const value T. */
19334 static bool
19335 tree_add_const_value_attribute (dw_die_ref die, tree t)
19337 tree init;
19338 tree type = TREE_TYPE (t);
19339 rtx rtl;
19341 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
19342 return false;
19344 init = t;
19345 gcc_assert (!DECL_P (init));
19347 if (! early_dwarf)
19349 rtl = rtl_for_decl_init (init, type);
19350 if (rtl)
19351 return add_const_value_attribute (die, rtl);
19353 /* If the host and target are sane, try harder. */
19354 if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
19355 && initializer_constant_valid_p (init, type))
19357 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
19358 if (size > 0 && (int) size == size)
19360 unsigned char *array = ggc_cleared_vec_alloc<unsigned char> (size);
19362 if (native_encode_initializer (init, array, size))
19364 add_AT_vec (die, DW_AT_const_value, size, 1, array);
19365 return true;
19367 ggc_free (array);
19370 return false;
19373 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
19374 attribute is the const value of T, where T is an integral constant
19375 variable with static storage duration
19376 (so it can't be a PARM_DECL or a RESULT_DECL). */
19378 static bool
19379 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
19382 if (!decl
19383 || (!VAR_P (decl) && TREE_CODE (decl) != CONST_DECL)
19384 || (VAR_P (decl) && !TREE_STATIC (decl)))
19385 return false;
19387 if (TREE_READONLY (decl)
19388 && ! TREE_THIS_VOLATILE (decl)
19389 && DECL_INITIAL (decl))
19390 /* OK */;
19391 else
19392 return false;
19394 /* Don't add DW_AT_const_value if abstract origin already has one. */
19395 if (get_AT (var_die, DW_AT_const_value))
19396 return false;
19398 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
19401 /* Convert the CFI instructions for the current function into a
19402 location list. This is used for DW_AT_frame_base when we targeting
19403 a dwarf2 consumer that does not support the dwarf3
19404 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
19405 expressions. */
19407 static dw_loc_list_ref
19408 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
19410 int ix;
19411 dw_fde_ref fde;
19412 dw_loc_list_ref list, *list_tail;
19413 dw_cfi_ref cfi;
19414 dw_cfa_location last_cfa, next_cfa;
19415 const char *start_label, *last_label, *section;
19416 dw_cfa_location remember;
19418 fde = cfun->fde;
19419 gcc_assert (fde != NULL);
19421 section = secname_for_decl (current_function_decl);
19422 list_tail = &list;
19423 list = NULL;
19425 memset (&next_cfa, 0, sizeof (next_cfa));
19426 next_cfa.reg = INVALID_REGNUM;
19427 remember = next_cfa;
19429 start_label = fde->dw_fde_begin;
19431 /* ??? Bald assumption that the CIE opcode list does not contain
19432 advance opcodes. */
19433 FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
19434 lookup_cfa_1 (cfi, &next_cfa, &remember);
19436 last_cfa = next_cfa;
19437 last_label = start_label;
19439 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
19441 /* If the first partition contained no CFI adjustments, the
19442 CIE opcodes apply to the whole first partition. */
19443 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19444 fde->dw_fde_begin, fde->dw_fde_end, section);
19445 list_tail =&(*list_tail)->dw_loc_next;
19446 start_label = last_label = fde->dw_fde_second_begin;
19449 FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
19451 switch (cfi->dw_cfi_opc)
19453 case DW_CFA_set_loc:
19454 case DW_CFA_advance_loc1:
19455 case DW_CFA_advance_loc2:
19456 case DW_CFA_advance_loc4:
19457 if (!cfa_equal_p (&last_cfa, &next_cfa))
19459 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19460 start_label, last_label, section);
19462 list_tail = &(*list_tail)->dw_loc_next;
19463 last_cfa = next_cfa;
19464 start_label = last_label;
19466 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
19467 break;
19469 case DW_CFA_advance_loc:
19470 /* The encoding is complex enough that we should never emit this. */
19471 gcc_unreachable ();
19473 default:
19474 lookup_cfa_1 (cfi, &next_cfa, &remember);
19475 break;
19477 if (ix + 1 == fde->dw_fde_switch_cfi_index)
19479 if (!cfa_equal_p (&last_cfa, &next_cfa))
19481 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19482 start_label, last_label, section);
19484 list_tail = &(*list_tail)->dw_loc_next;
19485 last_cfa = next_cfa;
19486 start_label = last_label;
19488 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19489 start_label, fde->dw_fde_end, section);
19490 list_tail = &(*list_tail)->dw_loc_next;
19491 start_label = last_label = fde->dw_fde_second_begin;
19495 if (!cfa_equal_p (&last_cfa, &next_cfa))
19497 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19498 start_label, last_label, section);
19499 list_tail = &(*list_tail)->dw_loc_next;
19500 start_label = last_label;
19503 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
19504 start_label,
19505 fde->dw_fde_second_begin
19506 ? fde->dw_fde_second_end : fde->dw_fde_end,
19507 section);
19509 if (list && list->dw_loc_next)
19510 gen_llsym (list);
19512 return list;
19515 /* Compute a displacement from the "steady-state frame pointer" to the
19516 frame base (often the same as the CFA), and store it in
19517 frame_pointer_fb_offset. OFFSET is added to the displacement
19518 before the latter is negated. */
19520 static void
19521 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
19523 rtx reg, elim;
19525 #ifdef FRAME_POINTER_CFA_OFFSET
19526 reg = frame_pointer_rtx;
19527 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
19528 #else
19529 reg = arg_pointer_rtx;
19530 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
19531 #endif
19533 elim = (ira_use_lra_p
19534 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
19535 : eliminate_regs (reg, VOIDmode, NULL_RTX));
19536 if (GET_CODE (elim) == PLUS)
19538 offset += INTVAL (XEXP (elim, 1));
19539 elim = XEXP (elim, 0);
19542 frame_pointer_fb_offset = -offset;
19544 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
19545 in which to eliminate. This is because it's stack pointer isn't
19546 directly accessible as a register within the ISA. To work around
19547 this, assume that while we cannot provide a proper value for
19548 frame_pointer_fb_offset, we won't need one either. */
19549 frame_pointer_fb_offset_valid
19550 = ((SUPPORTS_STACK_ALIGNMENT
19551 && (elim == hard_frame_pointer_rtx
19552 || elim == stack_pointer_rtx))
19553 || elim == (frame_pointer_needed
19554 ? hard_frame_pointer_rtx
19555 : stack_pointer_rtx));
19558 /* Generate a DW_AT_name attribute given some string value to be included as
19559 the value of the attribute. */
19561 static void
19562 add_name_attribute (dw_die_ref die, const char *name_string)
19564 if (name_string != NULL && *name_string != 0)
19566 if (demangle_name_func)
19567 name_string = (*demangle_name_func) (name_string);
19569 add_AT_string (die, DW_AT_name, name_string);
19573 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
19574 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
19575 of TYPE accordingly.
19577 ??? This is a temporary measure until after we're able to generate
19578 regular DWARF for the complex Ada type system. */
19580 static void
19581 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
19582 dw_die_ref context_die)
19584 tree dtype;
19585 dw_die_ref dtype_die;
19587 if (!lang_hooks.types.descriptive_type)
19588 return;
19590 dtype = lang_hooks.types.descriptive_type (type);
19591 if (!dtype)
19592 return;
19594 dtype_die = lookup_type_die (dtype);
19595 if (!dtype_die)
19597 gen_type_die (dtype, context_die);
19598 dtype_die = lookup_type_die (dtype);
19599 gcc_assert (dtype_die);
19602 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
19605 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir. */
19607 static const char *
19608 comp_dir_string (void)
19610 const char *wd;
19611 char *wd1;
19612 static const char *cached_wd = NULL;
19614 if (cached_wd != NULL)
19615 return cached_wd;
19617 wd = get_src_pwd ();
19618 if (wd == NULL)
19619 return NULL;
19621 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
19623 int wdlen;
19625 wdlen = strlen (wd);
19626 wd1 = ggc_vec_alloc<char> (wdlen + 2);
19627 strcpy (wd1, wd);
19628 wd1 [wdlen] = DIR_SEPARATOR;
19629 wd1 [wdlen + 1] = 0;
19630 wd = wd1;
19633 cached_wd = remap_debug_filename (wd);
19634 return cached_wd;
19637 /* Generate a DW_AT_comp_dir attribute for DIE. */
19639 static void
19640 add_comp_dir_attribute (dw_die_ref die)
19642 const char * wd = comp_dir_string ();
19643 if (wd != NULL)
19644 add_AT_string (die, DW_AT_comp_dir, wd);
19647 /* Given a tree node VALUE describing a scalar attribute ATTR (i.e. a bound, a
19648 pointer computation, ...), output a representation for that bound according
19649 to the accepted FORMS (see enum dw_scalar_form) and add it to DIE. See
19650 loc_list_from_tree for the meaning of CONTEXT. */
19652 static void
19653 add_scalar_info (dw_die_ref die, enum dwarf_attribute attr, tree value,
19654 int forms, struct loc_descr_context *context)
19656 dw_die_ref context_die, decl_die;
19657 dw_loc_list_ref list;
19658 bool strip_conversions = true;
19659 bool placeholder_seen = false;
19661 while (strip_conversions)
19662 switch (TREE_CODE (value))
19664 case ERROR_MARK:
19665 case SAVE_EXPR:
19666 return;
19668 CASE_CONVERT:
19669 case VIEW_CONVERT_EXPR:
19670 value = TREE_OPERAND (value, 0);
19671 break;
19673 default:
19674 strip_conversions = false;
19675 break;
19678 /* If possible and permitted, output the attribute as a constant. */
19679 if ((forms & dw_scalar_form_constant) != 0
19680 && TREE_CODE (value) == INTEGER_CST)
19682 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (value));
19684 /* If HOST_WIDE_INT is big enough then represent the bound as
19685 a constant value. We need to choose a form based on
19686 whether the type is signed or unsigned. We cannot just
19687 call add_AT_unsigned if the value itself is positive
19688 (add_AT_unsigned might add the unsigned value encoded as
19689 DW_FORM_data[1248]). Some DWARF consumers will lookup the
19690 bounds type and then sign extend any unsigned values found
19691 for signed types. This is needed only for
19692 DW_AT_{lower,upper}_bound, since for most other attributes,
19693 consumers will treat DW_FORM_data[1248] as unsigned values,
19694 regardless of the underlying type. */
19695 if (prec <= HOST_BITS_PER_WIDE_INT
19696 || tree_fits_uhwi_p (value))
19698 if (TYPE_UNSIGNED (TREE_TYPE (value)))
19699 add_AT_unsigned (die, attr, TREE_INT_CST_LOW (value));
19700 else
19701 add_AT_int (die, attr, TREE_INT_CST_LOW (value));
19703 else
19704 /* Otherwise represent the bound as an unsigned value with
19705 the precision of its type. The precision and signedness
19706 of the type will be necessary to re-interpret it
19707 unambiguously. */
19708 add_AT_wide (die, attr, value);
19709 return;
19712 /* Otherwise, if it's possible and permitted too, output a reference to
19713 another DIE. */
19714 if ((forms & dw_scalar_form_reference) != 0)
19716 tree decl = NULL_TREE;
19718 /* Some type attributes reference an outer type. For instance, the upper
19719 bound of an array may reference an embedding record (this happens in
19720 Ada). */
19721 if (TREE_CODE (value) == COMPONENT_REF
19722 && TREE_CODE (TREE_OPERAND (value, 0)) == PLACEHOLDER_EXPR
19723 && TREE_CODE (TREE_OPERAND (value, 1)) == FIELD_DECL)
19724 decl = TREE_OPERAND (value, 1);
19726 else if (VAR_P (value)
19727 || TREE_CODE (value) == PARM_DECL
19728 || TREE_CODE (value) == RESULT_DECL)
19729 decl = value;
19731 if (decl != NULL_TREE)
19733 dw_die_ref decl_die = lookup_decl_die (decl);
19735 /* ??? Can this happen, or should the variable have been bound
19736 first? Probably it can, since I imagine that we try to create
19737 the types of parameters in the order in which they exist in
19738 the list, and won't have created a forward reference to a
19739 later parameter. */
19740 if (decl_die != NULL)
19742 add_AT_die_ref (die, attr, decl_die);
19743 return;
19748 /* Last chance: try to create a stack operation procedure to evaluate the
19749 value. Do nothing if even that is not possible or permitted. */
19750 if ((forms & dw_scalar_form_exprloc) == 0)
19751 return;
19753 list = loc_list_from_tree (value, 2, context);
19754 if (context && context->placeholder_arg)
19756 placeholder_seen = context->placeholder_seen;
19757 context->placeholder_seen = false;
19759 if (list == NULL || single_element_loc_list_p (list))
19761 /* If this attribute is not a reference nor constant, it is
19762 a DWARF expression rather than location description. For that
19763 loc_list_from_tree (value, 0, &context) is needed. */
19764 dw_loc_list_ref list2 = loc_list_from_tree (value, 0, context);
19765 if (list2 && single_element_loc_list_p (list2))
19767 if (placeholder_seen)
19769 struct dwarf_procedure_info dpi;
19770 dpi.fndecl = NULL_TREE;
19771 dpi.args_count = 1;
19772 if (!resolve_args_picking (list2->expr, 1, &dpi))
19773 return;
19775 add_AT_loc (die, attr, list2->expr);
19776 return;
19780 /* If that failed to give a single element location list, fall back to
19781 outputting this as a reference... still if permitted. */
19782 if (list == NULL
19783 || (forms & dw_scalar_form_reference) == 0
19784 || placeholder_seen)
19785 return;
19787 if (current_function_decl == 0)
19788 context_die = comp_unit_die ();
19789 else
19790 context_die = lookup_decl_die (current_function_decl);
19792 decl_die = new_die (DW_TAG_variable, context_die, value);
19793 add_AT_flag (decl_die, DW_AT_artificial, 1);
19794 add_type_attribute (decl_die, TREE_TYPE (value), TYPE_QUAL_CONST, false,
19795 context_die);
19796 add_AT_location_description (decl_die, DW_AT_location, list);
19797 add_AT_die_ref (die, attr, decl_die);
19800 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
19801 default. */
19803 static int
19804 lower_bound_default (void)
19806 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
19808 case DW_LANG_C:
19809 case DW_LANG_C89:
19810 case DW_LANG_C99:
19811 case DW_LANG_C11:
19812 case DW_LANG_C_plus_plus:
19813 case DW_LANG_C_plus_plus_11:
19814 case DW_LANG_C_plus_plus_14:
19815 case DW_LANG_ObjC:
19816 case DW_LANG_ObjC_plus_plus:
19817 case DW_LANG_Java:
19818 return 0;
19819 case DW_LANG_Fortran77:
19820 case DW_LANG_Fortran90:
19821 case DW_LANG_Fortran95:
19822 case DW_LANG_Fortran03:
19823 case DW_LANG_Fortran08:
19824 return 1;
19825 case DW_LANG_UPC:
19826 case DW_LANG_D:
19827 case DW_LANG_Python:
19828 return dwarf_version >= 4 ? 0 : -1;
19829 case DW_LANG_Ada95:
19830 case DW_LANG_Ada83:
19831 case DW_LANG_Cobol74:
19832 case DW_LANG_Cobol85:
19833 case DW_LANG_Pascal83:
19834 case DW_LANG_Modula2:
19835 case DW_LANG_PLI:
19836 return dwarf_version >= 4 ? 1 : -1;
19837 default:
19838 return -1;
19842 /* Given a tree node describing an array bound (either lower or upper) output
19843 a representation for that bound. */
19845 static void
19846 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr,
19847 tree bound, struct loc_descr_context *context)
19849 int dflt;
19851 while (1)
19852 switch (TREE_CODE (bound))
19854 /* Strip all conversions. */
19855 CASE_CONVERT:
19856 case VIEW_CONVERT_EXPR:
19857 bound = TREE_OPERAND (bound, 0);
19858 break;
19860 /* All fixed-bounds are represented by INTEGER_CST nodes. Lower bounds
19861 are even omitted when they are the default. */
19862 case INTEGER_CST:
19863 /* If the value for this bound is the default one, we can even omit the
19864 attribute. */
19865 if (bound_attr == DW_AT_lower_bound
19866 && tree_fits_shwi_p (bound)
19867 && (dflt = lower_bound_default ()) != -1
19868 && tree_to_shwi (bound) == dflt)
19869 return;
19871 /* FALLTHRU */
19873 default:
19874 /* Because of the complex interaction there can be with other GNAT
19875 encodings, GDB isn't ready yet to handle proper DWARF description
19876 for self-referencial subrange bounds: let GNAT encodings do the
19877 magic in such a case. */
19878 if (is_ada ()
19879 && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL
19880 && contains_placeholder_p (bound))
19881 return;
19883 add_scalar_info (subrange_die, bound_attr, bound,
19884 dw_scalar_form_constant
19885 | dw_scalar_form_exprloc
19886 | dw_scalar_form_reference,
19887 context);
19888 return;
19892 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
19893 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
19894 Note that the block of subscript information for an array type also
19895 includes information about the element type of the given array type.
19897 This function reuses previously set type and bound information if
19898 available. */
19900 static void
19901 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
19903 unsigned dimension_number;
19904 tree lower, upper;
19905 dw_die_ref child = type_die->die_child;
19907 for (dimension_number = 0;
19908 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
19909 type = TREE_TYPE (type), dimension_number++)
19911 tree domain = TYPE_DOMAIN (type);
19913 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
19914 break;
19916 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
19917 and (in GNU C only) variable bounds. Handle all three forms
19918 here. */
19920 /* Find and reuse a previously generated DW_TAG_subrange_type if
19921 available.
19923 For multi-dimensional arrays, as we iterate through the
19924 various dimensions in the enclosing for loop above, we also
19925 iterate through the DIE children and pick at each
19926 DW_TAG_subrange_type previously generated (if available).
19927 Each child DW_TAG_subrange_type DIE describes the range of
19928 the current dimension. At this point we should have as many
19929 DW_TAG_subrange_type's as we have dimensions in the
19930 array. */
19931 dw_die_ref subrange_die = NULL;
19932 if (child)
19933 while (1)
19935 child = child->die_sib;
19936 if (child->die_tag == DW_TAG_subrange_type)
19937 subrange_die = child;
19938 if (child == type_die->die_child)
19940 /* If we wrapped around, stop looking next time. */
19941 child = NULL;
19942 break;
19944 if (child->die_tag == DW_TAG_subrange_type)
19945 break;
19947 if (!subrange_die)
19948 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
19950 if (domain)
19952 /* We have an array type with specified bounds. */
19953 lower = TYPE_MIN_VALUE (domain);
19954 upper = TYPE_MAX_VALUE (domain);
19956 /* Define the index type. */
19957 if (TREE_TYPE (domain)
19958 && !get_AT (subrange_die, DW_AT_type))
19960 /* ??? This is probably an Ada unnamed subrange type. Ignore the
19961 TREE_TYPE field. We can't emit debug info for this
19962 because it is an unnamed integral type. */
19963 if (TREE_CODE (domain) == INTEGER_TYPE
19964 && TYPE_NAME (domain) == NULL_TREE
19965 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
19966 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
19968 else
19969 add_type_attribute (subrange_die, TREE_TYPE (domain),
19970 TYPE_UNQUALIFIED, false, type_die);
19973 /* ??? If upper is NULL, the array has unspecified length,
19974 but it does have a lower bound. This happens with Fortran
19975 dimension arr(N:*)
19976 Since the debugger is definitely going to need to know N
19977 to produce useful results, go ahead and output the lower
19978 bound solo, and hope the debugger can cope. */
19980 if (!get_AT (subrange_die, DW_AT_lower_bound))
19981 add_bound_info (subrange_die, DW_AT_lower_bound, lower, NULL);
19982 if (upper && !get_AT (subrange_die, DW_AT_upper_bound))
19983 add_bound_info (subrange_die, DW_AT_upper_bound, upper, NULL);
19986 /* Otherwise we have an array type with an unspecified length. The
19987 DWARF-2 spec does not say how to handle this; let's just leave out the
19988 bounds. */
19992 /* Add a DW_AT_byte_size attribute to DIE with TREE_NODE's size. */
19994 static void
19995 add_byte_size_attribute (dw_die_ref die, tree tree_node)
19997 dw_die_ref decl_die;
19998 HOST_WIDE_INT size;
19999 dw_loc_descr_ref size_expr = NULL;
20001 switch (TREE_CODE (tree_node))
20003 case ERROR_MARK:
20004 size = 0;
20005 break;
20006 case ENUMERAL_TYPE:
20007 case RECORD_TYPE:
20008 case UNION_TYPE:
20009 case QUAL_UNION_TYPE:
20010 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
20011 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
20013 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
20014 return;
20016 size_expr = type_byte_size (tree_node, &size);
20017 break;
20018 case FIELD_DECL:
20019 /* For a data member of a struct or union, the DW_AT_byte_size is
20020 generally given as the number of bytes normally allocated for an
20021 object of the *declared* type of the member itself. This is true
20022 even for bit-fields. */
20023 size = int_size_in_bytes (field_type (tree_node));
20024 break;
20025 default:
20026 gcc_unreachable ();
20029 /* Support for dynamically-sized objects was introduced by DWARFv3.
20030 At the moment, GDB does not handle variable byte sizes very well,
20031 though. */
20032 if ((dwarf_version >= 3 || !dwarf_strict)
20033 && gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL
20034 && size_expr != NULL)
20035 add_AT_loc (die, DW_AT_byte_size, size_expr);
20037 /* Note that `size' might be -1 when we get to this point. If it is, that
20038 indicates that the byte size of the entity in question is variable and
20039 that we could not generate a DWARF expression that computes it. */
20040 if (size >= 0)
20041 add_AT_unsigned (die, DW_AT_byte_size, size);
20044 /* Add a DW_AT_alignment attribute to DIE with TREE_NODE's non-default
20045 alignment. */
20047 static void
20048 add_alignment_attribute (dw_die_ref die, tree tree_node)
20050 if (dwarf_version < 5 && dwarf_strict)
20051 return;
20053 unsigned align;
20055 if (DECL_P (tree_node))
20057 if (!DECL_USER_ALIGN (tree_node))
20058 return;
20060 align = DECL_ALIGN_UNIT (tree_node);
20062 else if (TYPE_P (tree_node))
20064 if (!TYPE_USER_ALIGN (tree_node))
20065 return;
20067 align = TYPE_ALIGN_UNIT (tree_node);
20069 else
20070 gcc_unreachable ();
20072 add_AT_unsigned (die, DW_AT_alignment, align);
20075 /* For a FIELD_DECL node which represents a bit-field, output an attribute
20076 which specifies the distance in bits from the highest order bit of the
20077 "containing object" for the bit-field to the highest order bit of the
20078 bit-field itself.
20080 For any given bit-field, the "containing object" is a hypothetical object
20081 (of some integral or enum type) within which the given bit-field lives. The
20082 type of this hypothetical "containing object" is always the same as the
20083 declared type of the individual bit-field itself. The determination of the
20084 exact location of the "containing object" for a bit-field is rather
20085 complicated. It's handled by the `field_byte_offset' function (above).
20087 CTX is required: see the comment for VLR_CONTEXT.
20089 Note that it is the size (in bytes) of the hypothetical "containing object"
20090 which will be given in the DW_AT_byte_size attribute for this bit-field.
20091 (See `byte_size_attribute' above). */
20093 static inline void
20094 add_bit_offset_attribute (dw_die_ref die, tree decl, struct vlr_context *ctx)
20096 HOST_WIDE_INT object_offset_in_bytes;
20097 tree original_type = DECL_BIT_FIELD_TYPE (decl);
20098 HOST_WIDE_INT bitpos_int;
20099 HOST_WIDE_INT highest_order_object_bit_offset;
20100 HOST_WIDE_INT highest_order_field_bit_offset;
20101 HOST_WIDE_INT bit_offset;
20103 field_byte_offset (decl, ctx, &object_offset_in_bytes);
20105 /* Must be a field and a bit field. */
20106 gcc_assert (original_type && TREE_CODE (decl) == FIELD_DECL);
20108 /* We can't yet handle bit-fields whose offsets are variable, so if we
20109 encounter such things, just return without generating any attribute
20110 whatsoever. Likewise for variable or too large size. */
20111 if (! tree_fits_shwi_p (bit_position (decl))
20112 || ! tree_fits_uhwi_p (DECL_SIZE (decl)))
20113 return;
20115 bitpos_int = int_bit_position (decl);
20117 /* Note that the bit offset is always the distance (in bits) from the
20118 highest-order bit of the "containing object" to the highest-order bit of
20119 the bit-field itself. Since the "high-order end" of any object or field
20120 is different on big-endian and little-endian machines, the computation
20121 below must take account of these differences. */
20122 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
20123 highest_order_field_bit_offset = bitpos_int;
20125 if (! BYTES_BIG_ENDIAN)
20127 highest_order_field_bit_offset += tree_to_shwi (DECL_SIZE (decl));
20128 highest_order_object_bit_offset +=
20129 simple_type_size_in_bits (original_type);
20132 bit_offset
20133 = (! BYTES_BIG_ENDIAN
20134 ? highest_order_object_bit_offset - highest_order_field_bit_offset
20135 : highest_order_field_bit_offset - highest_order_object_bit_offset);
20137 if (bit_offset < 0)
20138 add_AT_int (die, DW_AT_bit_offset, bit_offset);
20139 else
20140 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
20143 /* For a FIELD_DECL node which represents a bit field, output an attribute
20144 which specifies the length in bits of the given field. */
20146 static inline void
20147 add_bit_size_attribute (dw_die_ref die, tree decl)
20149 /* Must be a field and a bit field. */
20150 gcc_assert (TREE_CODE (decl) == FIELD_DECL
20151 && DECL_BIT_FIELD_TYPE (decl));
20153 if (tree_fits_uhwi_p (DECL_SIZE (decl)))
20154 add_AT_unsigned (die, DW_AT_bit_size, tree_to_uhwi (DECL_SIZE (decl)));
20157 /* If the compiled language is ANSI C, then add a 'prototyped'
20158 attribute, if arg types are given for the parameters of a function. */
20160 static inline void
20161 add_prototyped_attribute (dw_die_ref die, tree func_type)
20163 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
20165 case DW_LANG_C:
20166 case DW_LANG_C89:
20167 case DW_LANG_C99:
20168 case DW_LANG_C11:
20169 case DW_LANG_ObjC:
20170 if (prototype_p (func_type))
20171 add_AT_flag (die, DW_AT_prototyped, 1);
20172 break;
20173 default:
20174 break;
20178 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
20179 by looking in the type declaration, the object declaration equate table or
20180 the block mapping. */
20182 static inline dw_die_ref
20183 add_abstract_origin_attribute (dw_die_ref die, tree origin)
20185 dw_die_ref origin_die = NULL;
20187 if (TREE_CODE (origin) != FUNCTION_DECL
20188 && TREE_CODE (origin) != BLOCK)
20190 /* We may have gotten separated from the block for the inlined
20191 function, if we're in an exception handler or some such; make
20192 sure that the abstract function has been written out.
20194 Doing this for nested functions is wrong, however; functions are
20195 distinct units, and our context might not even be inline. */
20196 tree fn = origin;
20198 if (TYPE_P (fn))
20199 fn = TYPE_STUB_DECL (fn);
20201 fn = decl_function_context (fn);
20202 if (fn)
20203 dwarf2out_abstract_function (fn);
20206 if (DECL_P (origin))
20207 origin_die = lookup_decl_die (origin);
20208 else if (TYPE_P (origin))
20209 origin_die = lookup_type_die (origin);
20210 else if (TREE_CODE (origin) == BLOCK)
20211 origin_die = BLOCK_DIE (origin);
20213 /* XXX: Functions that are never lowered don't always have correct block
20214 trees (in the case of java, they simply have no block tree, in some other
20215 languages). For these functions, there is nothing we can really do to
20216 output correct debug info for inlined functions in all cases. Rather
20217 than die, we'll just produce deficient debug info now, in that we will
20218 have variables without a proper abstract origin. In the future, when all
20219 functions are lowered, we should re-add a gcc_assert (origin_die)
20220 here. */
20222 if (origin_die)
20223 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
20224 return origin_die;
20227 /* We do not currently support the pure_virtual attribute. */
20229 static inline void
20230 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
20232 if (DECL_VINDEX (func_decl))
20234 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
20236 if (tree_fits_shwi_p (DECL_VINDEX (func_decl)))
20237 add_AT_loc (die, DW_AT_vtable_elem_location,
20238 new_loc_descr (DW_OP_constu,
20239 tree_to_shwi (DECL_VINDEX (func_decl)),
20240 0));
20242 /* GNU extension: Record what type this method came from originally. */
20243 if (debug_info_level > DINFO_LEVEL_TERSE
20244 && DECL_CONTEXT (func_decl))
20245 add_AT_die_ref (die, DW_AT_containing_type,
20246 lookup_type_die (DECL_CONTEXT (func_decl)));
20250 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
20251 given decl. This used to be a vendor extension until after DWARF 4
20252 standardized it. */
20254 static void
20255 add_linkage_attr (dw_die_ref die, tree decl)
20257 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
20259 /* Mimic what assemble_name_raw does with a leading '*'. */
20260 if (name[0] == '*')
20261 name = &name[1];
20263 if (dwarf_version >= 4)
20264 add_AT_string (die, DW_AT_linkage_name, name);
20265 else
20266 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
20269 /* Add source coordinate attributes for the given decl. */
20271 static void
20272 add_src_coords_attributes (dw_die_ref die, tree decl)
20274 expanded_location s;
20276 if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
20277 return;
20278 s = expand_location (DECL_SOURCE_LOCATION (decl));
20279 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
20280 add_AT_unsigned (die, DW_AT_decl_line, s.line);
20281 if (debug_column_info && s.column)
20282 add_AT_unsigned (die, DW_AT_decl_column, s.column);
20285 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
20287 static void
20288 add_linkage_name_raw (dw_die_ref die, tree decl)
20290 /* Defer until we have an assembler name set. */
20291 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
20293 limbo_die_node *asm_name;
20295 asm_name = ggc_cleared_alloc<limbo_die_node> ();
20296 asm_name->die = die;
20297 asm_name->created_for = decl;
20298 asm_name->next = deferred_asm_name;
20299 deferred_asm_name = asm_name;
20301 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
20302 add_linkage_attr (die, decl);
20305 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl if desired. */
20307 static void
20308 add_linkage_name (dw_die_ref die, tree decl)
20310 if (debug_info_level > DINFO_LEVEL_NONE
20311 && VAR_OR_FUNCTION_DECL_P (decl)
20312 && TREE_PUBLIC (decl)
20313 && !(VAR_P (decl) && DECL_REGISTER (decl))
20314 && die->die_tag != DW_TAG_member)
20315 add_linkage_name_raw (die, decl);
20318 /* Add a DW_AT_name attribute and source coordinate attribute for the
20319 given decl, but only if it actually has a name. */
20321 static void
20322 add_name_and_src_coords_attributes (dw_die_ref die, tree decl,
20323 bool no_linkage_name)
20325 tree decl_name;
20327 decl_name = DECL_NAME (decl);
20328 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
20330 const char *name = dwarf2_name (decl, 0);
20331 if (name)
20332 add_name_attribute (die, name);
20333 if (! DECL_ARTIFICIAL (decl))
20334 add_src_coords_attributes (die, decl);
20336 if (!no_linkage_name)
20337 add_linkage_name (die, decl);
20340 #ifdef VMS_DEBUGGING_INFO
20341 /* Get the function's name, as described by its RTL. This may be different
20342 from the DECL_NAME name used in the source file. */
20343 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
20345 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
20346 XEXP (DECL_RTL (decl), 0), false);
20347 vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
20349 #endif /* VMS_DEBUGGING_INFO */
20352 /* Add VALUE as a DW_AT_discr_value attribute to DIE. */
20354 static void
20355 add_discr_value (dw_die_ref die, dw_discr_value *value)
20357 dw_attr_node attr;
20359 attr.dw_attr = DW_AT_discr_value;
20360 attr.dw_attr_val.val_class = dw_val_class_discr_value;
20361 attr.dw_attr_val.val_entry = NULL;
20362 attr.dw_attr_val.v.val_discr_value.pos = value->pos;
20363 if (value->pos)
20364 attr.dw_attr_val.v.val_discr_value.v.uval = value->v.uval;
20365 else
20366 attr.dw_attr_val.v.val_discr_value.v.sval = value->v.sval;
20367 add_dwarf_attr (die, &attr);
20370 /* Add DISCR_LIST as a DW_AT_discr_list to DIE. */
20372 static void
20373 add_discr_list (dw_die_ref die, dw_discr_list_ref discr_list)
20375 dw_attr_node attr;
20377 attr.dw_attr = DW_AT_discr_list;
20378 attr.dw_attr_val.val_class = dw_val_class_discr_list;
20379 attr.dw_attr_val.val_entry = NULL;
20380 attr.dw_attr_val.v.val_discr_list = discr_list;
20381 add_dwarf_attr (die, &attr);
20384 static inline dw_discr_list_ref
20385 AT_discr_list (dw_attr_node *attr)
20387 return attr->dw_attr_val.v.val_discr_list;
20390 #ifdef VMS_DEBUGGING_INFO
20391 /* Output the debug main pointer die for VMS */
20393 void
20394 dwarf2out_vms_debug_main_pointer (void)
20396 char label[MAX_ARTIFICIAL_LABEL_BYTES];
20397 dw_die_ref die;
20399 /* Allocate the VMS debug main subprogram die. */
20400 die = ggc_cleared_alloc<die_node> ();
20401 die->die_tag = DW_TAG_subprogram;
20402 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
20403 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
20404 current_function_funcdef_no);
20405 add_AT_lbl_id (die, DW_AT_entry_pc, label);
20407 /* Make it the first child of comp_unit_die (). */
20408 die->die_parent = comp_unit_die ();
20409 if (comp_unit_die ()->die_child)
20411 die->die_sib = comp_unit_die ()->die_child->die_sib;
20412 comp_unit_die ()->die_child->die_sib = die;
20414 else
20416 die->die_sib = die;
20417 comp_unit_die ()->die_child = die;
20420 #endif /* VMS_DEBUGGING_INFO */
20422 /* Push a new declaration scope. */
20424 static void
20425 push_decl_scope (tree scope)
20427 vec_safe_push (decl_scope_table, scope);
20430 /* Pop a declaration scope. */
20432 static inline void
20433 pop_decl_scope (void)
20435 decl_scope_table->pop ();
20438 /* walk_tree helper function for uses_local_type, below. */
20440 static tree
20441 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
20443 if (!TYPE_P (*tp))
20444 *walk_subtrees = 0;
20445 else
20447 tree name = TYPE_NAME (*tp);
20448 if (name && DECL_P (name) && decl_function_context (name))
20449 return *tp;
20451 return NULL_TREE;
20454 /* If TYPE involves a function-local type (including a local typedef to a
20455 non-local type), returns that type; otherwise returns NULL_TREE. */
20457 static tree
20458 uses_local_type (tree type)
20460 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
20461 return used;
20464 /* Return the DIE for the scope that immediately contains this type.
20465 Non-named types that do not involve a function-local type get global
20466 scope. Named types nested in namespaces or other types get their
20467 containing scope. All other types (i.e. function-local named types) get
20468 the current active scope. */
20470 static dw_die_ref
20471 scope_die_for (tree t, dw_die_ref context_die)
20473 dw_die_ref scope_die = NULL;
20474 tree containing_scope;
20476 /* Non-types always go in the current scope. */
20477 gcc_assert (TYPE_P (t));
20479 /* Use the scope of the typedef, rather than the scope of the type
20480 it refers to. */
20481 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
20482 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
20483 else
20484 containing_scope = TYPE_CONTEXT (t);
20486 /* Use the containing namespace if there is one. */
20487 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
20489 if (context_die == lookup_decl_die (containing_scope))
20490 /* OK */;
20491 else if (debug_info_level > DINFO_LEVEL_TERSE)
20492 context_die = get_context_die (containing_scope);
20493 else
20494 containing_scope = NULL_TREE;
20497 /* Ignore function type "scopes" from the C frontend. They mean that
20498 a tagged type is local to a parmlist of a function declarator, but
20499 that isn't useful to DWARF. */
20500 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
20501 containing_scope = NULL_TREE;
20503 if (SCOPE_FILE_SCOPE_P (containing_scope))
20505 /* If T uses a local type keep it local as well, to avoid references
20506 to function-local DIEs from outside the function. */
20507 if (current_function_decl && uses_local_type (t))
20508 scope_die = context_die;
20509 else
20510 scope_die = comp_unit_die ();
20512 else if (TYPE_P (containing_scope))
20514 /* For types, we can just look up the appropriate DIE. */
20515 if (debug_info_level > DINFO_LEVEL_TERSE)
20516 scope_die = get_context_die (containing_scope);
20517 else
20519 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
20520 if (scope_die == NULL)
20521 scope_die = comp_unit_die ();
20524 else
20525 scope_die = context_die;
20527 return scope_die;
20530 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
20532 static inline int
20533 local_scope_p (dw_die_ref context_die)
20535 for (; context_die; context_die = context_die->die_parent)
20536 if (context_die->die_tag == DW_TAG_inlined_subroutine
20537 || context_die->die_tag == DW_TAG_subprogram)
20538 return 1;
20540 return 0;
20543 /* Returns nonzero if CONTEXT_DIE is a class. */
20545 static inline int
20546 class_scope_p (dw_die_ref context_die)
20548 return (context_die
20549 && (context_die->die_tag == DW_TAG_structure_type
20550 || context_die->die_tag == DW_TAG_class_type
20551 || context_die->die_tag == DW_TAG_interface_type
20552 || context_die->die_tag == DW_TAG_union_type));
20555 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
20556 whether or not to treat a DIE in this context as a declaration. */
20558 static inline int
20559 class_or_namespace_scope_p (dw_die_ref context_die)
20561 return (class_scope_p (context_die)
20562 || (context_die && context_die->die_tag == DW_TAG_namespace));
20565 /* Many forms of DIEs require a "type description" attribute. This
20566 routine locates the proper "type descriptor" die for the type given
20567 by 'type' plus any additional qualifiers given by 'cv_quals', and
20568 adds a DW_AT_type attribute below the given die. */
20570 static void
20571 add_type_attribute (dw_die_ref object_die, tree type, int cv_quals,
20572 bool reverse, dw_die_ref context_die)
20574 enum tree_code code = TREE_CODE (type);
20575 dw_die_ref type_die = NULL;
20577 /* ??? If this type is an unnamed subrange type of an integral, floating-point
20578 or fixed-point type, use the inner type. This is because we have no
20579 support for unnamed types in base_type_die. This can happen if this is
20580 an Ada subrange type. Correct solution is emit a subrange type die. */
20581 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
20582 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
20583 type = TREE_TYPE (type), code = TREE_CODE (type);
20585 if (code == ERROR_MARK
20586 /* Handle a special case. For functions whose return type is void, we
20587 generate *no* type attribute. (Note that no object may have type
20588 `void', so this only applies to function return types). */
20589 || code == VOID_TYPE)
20590 return;
20592 type_die = modified_type_die (type,
20593 cv_quals | TYPE_QUALS_NO_ADDR_SPACE (type),
20594 reverse,
20595 context_die);
20597 if (type_die != NULL)
20598 add_AT_die_ref (object_die, DW_AT_type, type_die);
20601 /* Given an object die, add the calling convention attribute for the
20602 function call type. */
20603 static void
20604 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
20606 enum dwarf_calling_convention value = DW_CC_normal;
20608 value = ((enum dwarf_calling_convention)
20609 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
20611 if (is_fortran ()
20612 && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
20614 /* DWARF 2 doesn't provide a way to identify a program's source-level
20615 entry point. DW_AT_calling_convention attributes are only meant
20616 to describe functions' calling conventions. However, lacking a
20617 better way to signal the Fortran main program, we used this for
20618 a long time, following existing custom. Now, DWARF 4 has
20619 DW_AT_main_subprogram, which we add below, but some tools still
20620 rely on the old way, which we thus keep. */
20621 value = DW_CC_program;
20623 if (dwarf_version >= 4 || !dwarf_strict)
20624 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
20627 /* Only add the attribute if the backend requests it, and
20628 is not DW_CC_normal. */
20629 if (value && (value != DW_CC_normal))
20630 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
20633 /* Given a tree pointer to a struct, class, union, or enum type node, return
20634 a pointer to the (string) tag name for the given type, or zero if the type
20635 was declared without a tag. */
20637 static const char *
20638 type_tag (const_tree type)
20640 const char *name = 0;
20642 if (TYPE_NAME (type) != 0)
20644 tree t = 0;
20646 /* Find the IDENTIFIER_NODE for the type name. */
20647 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
20648 && !TYPE_NAMELESS (type))
20649 t = TYPE_NAME (type);
20651 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
20652 a TYPE_DECL node, regardless of whether or not a `typedef' was
20653 involved. */
20654 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
20655 && ! DECL_IGNORED_P (TYPE_NAME (type)))
20657 /* We want to be extra verbose. Don't call dwarf_name if
20658 DECL_NAME isn't set. The default hook for decl_printable_name
20659 doesn't like that, and in this context it's correct to return
20660 0, instead of "<anonymous>" or the like. */
20661 if (DECL_NAME (TYPE_NAME (type))
20662 && !DECL_NAMELESS (TYPE_NAME (type)))
20663 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
20666 /* Now get the name as a string, or invent one. */
20667 if (!name && t != 0)
20668 name = IDENTIFIER_POINTER (t);
20671 return (name == 0 || *name == '\0') ? 0 : name;
20674 /* Return the type associated with a data member, make a special check
20675 for bit field types. */
20677 static inline tree
20678 member_declared_type (const_tree member)
20680 return (DECL_BIT_FIELD_TYPE (member)
20681 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
20684 /* Get the decl's label, as described by its RTL. This may be different
20685 from the DECL_NAME name used in the source file. */
20687 #if 0
20688 static const char *
20689 decl_start_label (tree decl)
20691 rtx x;
20692 const char *fnname;
20694 x = DECL_RTL (decl);
20695 gcc_assert (MEM_P (x));
20697 x = XEXP (x, 0);
20698 gcc_assert (GET_CODE (x) == SYMBOL_REF);
20700 fnname = XSTR (x, 0);
20701 return fnname;
20703 #endif
20705 /* For variable-length arrays that have been previously generated, but
20706 may be incomplete due to missing subscript info, fill the subscript
20707 info. Return TRUE if this is one of those cases. */
20708 static bool
20709 fill_variable_array_bounds (tree type)
20711 if (TREE_ASM_WRITTEN (type)
20712 && TREE_CODE (type) == ARRAY_TYPE
20713 && variably_modified_type_p (type, NULL))
20715 dw_die_ref array_die = lookup_type_die (type);
20716 if (!array_die)
20717 return false;
20718 add_subscript_info (array_die, type, !is_ada ());
20719 return true;
20721 return false;
20724 /* These routines generate the internal representation of the DIE's for
20725 the compilation unit. Debugging information is collected by walking
20726 the declaration trees passed in from dwarf2out_decl(). */
20728 static void
20729 gen_array_type_die (tree type, dw_die_ref context_die)
20731 dw_die_ref array_die;
20733 /* GNU compilers represent multidimensional array types as sequences of one
20734 dimensional array types whose element types are themselves array types.
20735 We sometimes squish that down to a single array_type DIE with multiple
20736 subscripts in the Dwarf debugging info. The draft Dwarf specification
20737 say that we are allowed to do this kind of compression in C, because
20738 there is no difference between an array of arrays and a multidimensional
20739 array. We don't do this for Ada to remain as close as possible to the
20740 actual representation, which is especially important against the language
20741 flexibilty wrt arrays of variable size. */
20743 bool collapse_nested_arrays = !is_ada ();
20745 if (fill_variable_array_bounds (type))
20746 return;
20748 dw_die_ref scope_die = scope_die_for (type, context_die);
20749 tree element_type;
20751 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
20752 DW_TAG_string_type doesn't have DW_AT_type attribute). */
20753 if (TYPE_STRING_FLAG (type)
20754 && TREE_CODE (type) == ARRAY_TYPE
20755 && is_fortran ()
20756 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
20758 HOST_WIDE_INT size;
20760 array_die = new_die (DW_TAG_string_type, scope_die, type);
20761 add_name_attribute (array_die, type_tag (type));
20762 equate_type_number_to_die (type, array_die);
20763 size = int_size_in_bytes (type);
20764 if (size >= 0)
20765 add_AT_unsigned (array_die, DW_AT_byte_size, size);
20766 else if (TYPE_DOMAIN (type) != NULL_TREE
20767 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE)
20769 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
20770 tree rszdecl = szdecl;
20772 size = int_size_in_bytes (TREE_TYPE (szdecl));
20773 if (!DECL_P (szdecl))
20775 if (TREE_CODE (szdecl) == INDIRECT_REF
20776 && DECL_P (TREE_OPERAND (szdecl, 0)))
20778 rszdecl = TREE_OPERAND (szdecl, 0);
20779 if (int_size_in_bytes (TREE_TYPE (rszdecl))
20780 != DWARF2_ADDR_SIZE)
20781 size = 0;
20783 else
20784 size = 0;
20786 if (size > 0)
20788 dw_loc_list_ref loc
20789 = loc_list_from_tree (rszdecl, szdecl == rszdecl ? 2 : 0,
20790 NULL);
20791 if (loc)
20793 add_AT_location_description (array_die, DW_AT_string_length,
20794 loc);
20795 if (size != DWARF2_ADDR_SIZE)
20796 add_AT_unsigned (array_die, dwarf_version >= 5
20797 ? DW_AT_string_length_byte_size
20798 : DW_AT_byte_size, size);
20802 return;
20805 array_die = new_die (DW_TAG_array_type, scope_die, type);
20806 add_name_attribute (array_die, type_tag (type));
20807 equate_type_number_to_die (type, array_die);
20809 if (TREE_CODE (type) == VECTOR_TYPE)
20810 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
20812 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
20813 if (is_fortran ()
20814 && TREE_CODE (type) == ARRAY_TYPE
20815 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
20816 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
20817 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
20819 #if 0
20820 /* We default the array ordering. SDB will probably do
20821 the right things even if DW_AT_ordering is not present. It's not even
20822 an issue until we start to get into multidimensional arrays anyway. If
20823 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
20824 then we'll have to put the DW_AT_ordering attribute back in. (But if
20825 and when we find out that we need to put these in, we will only do so
20826 for multidimensional arrays. */
20827 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
20828 #endif
20830 if (TREE_CODE (type) == VECTOR_TYPE)
20832 /* For VECTOR_TYPEs we use an array die with appropriate bounds. */
20833 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
20834 add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node, NULL);
20835 add_bound_info (subrange_die, DW_AT_upper_bound,
20836 size_int (TYPE_VECTOR_SUBPARTS (type) - 1), NULL);
20838 else
20839 add_subscript_info (array_die, type, collapse_nested_arrays);
20841 /* Add representation of the type of the elements of this array type and
20842 emit the corresponding DIE if we haven't done it already. */
20843 element_type = TREE_TYPE (type);
20844 if (collapse_nested_arrays)
20845 while (TREE_CODE (element_type) == ARRAY_TYPE)
20847 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
20848 break;
20849 element_type = TREE_TYPE (element_type);
20852 add_type_attribute (array_die, element_type, TYPE_UNQUALIFIED,
20853 TREE_CODE (type) == ARRAY_TYPE
20854 && TYPE_REVERSE_STORAGE_ORDER (type),
20855 context_die);
20857 add_gnat_descriptive_type_attribute (array_die, type, context_die);
20858 if (TYPE_ARTIFICIAL (type))
20859 add_AT_flag (array_die, DW_AT_artificial, 1);
20861 if (get_AT (array_die, DW_AT_name))
20862 add_pubtype (type, array_die);
20864 add_alignment_attribute (array_die, type);
20867 /* This routine generates DIE for array with hidden descriptor, details
20868 are filled into *info by a langhook. */
20870 static void
20871 gen_descr_array_type_die (tree type, struct array_descr_info *info,
20872 dw_die_ref context_die)
20874 const dw_die_ref scope_die = scope_die_for (type, context_die);
20875 const dw_die_ref array_die = new_die (DW_TAG_array_type, scope_die, type);
20876 struct loc_descr_context context = { type, info->base_decl, NULL,
20877 false, false };
20878 enum dwarf_tag subrange_tag = DW_TAG_subrange_type;
20879 int dim;
20881 add_name_attribute (array_die, type_tag (type));
20882 equate_type_number_to_die (type, array_die);
20884 if (info->ndimensions > 1)
20885 switch (info->ordering)
20887 case array_descr_ordering_row_major:
20888 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
20889 break;
20890 case array_descr_ordering_column_major:
20891 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
20892 break;
20893 default:
20894 break;
20897 if (dwarf_version >= 3 || !dwarf_strict)
20899 if (info->data_location)
20900 add_scalar_info (array_die, DW_AT_data_location, info->data_location,
20901 dw_scalar_form_exprloc, &context);
20902 if (info->associated)
20903 add_scalar_info (array_die, DW_AT_associated, info->associated,
20904 dw_scalar_form_constant
20905 | dw_scalar_form_exprloc
20906 | dw_scalar_form_reference, &context);
20907 if (info->allocated)
20908 add_scalar_info (array_die, DW_AT_allocated, info->allocated,
20909 dw_scalar_form_constant
20910 | dw_scalar_form_exprloc
20911 | dw_scalar_form_reference, &context);
20912 if (info->stride)
20914 const enum dwarf_attribute attr
20915 = (info->stride_in_bits) ? DW_AT_bit_stride : DW_AT_byte_stride;
20916 const int forms
20917 = (info->stride_in_bits)
20918 ? dw_scalar_form_constant
20919 : (dw_scalar_form_constant
20920 | dw_scalar_form_exprloc
20921 | dw_scalar_form_reference);
20923 add_scalar_info (array_die, attr, info->stride, forms, &context);
20926 if (dwarf_version >= 5)
20928 if (info->rank)
20930 add_scalar_info (array_die, DW_AT_rank, info->rank,
20931 dw_scalar_form_constant
20932 | dw_scalar_form_exprloc, &context);
20933 subrange_tag = DW_TAG_generic_subrange;
20934 context.placeholder_arg = true;
20938 add_gnat_descriptive_type_attribute (array_die, type, context_die);
20940 for (dim = 0; dim < info->ndimensions; dim++)
20942 dw_die_ref subrange_die = new_die (subrange_tag, array_die, NULL);
20944 if (info->dimen[dim].bounds_type)
20945 add_type_attribute (subrange_die,
20946 info->dimen[dim].bounds_type, TYPE_UNQUALIFIED,
20947 false, context_die);
20948 if (info->dimen[dim].lower_bound)
20949 add_bound_info (subrange_die, DW_AT_lower_bound,
20950 info->dimen[dim].lower_bound, &context);
20951 if (info->dimen[dim].upper_bound)
20952 add_bound_info (subrange_die, DW_AT_upper_bound,
20953 info->dimen[dim].upper_bound, &context);
20954 if ((dwarf_version >= 3 || !dwarf_strict) && info->dimen[dim].stride)
20955 add_scalar_info (subrange_die, DW_AT_byte_stride,
20956 info->dimen[dim].stride,
20957 dw_scalar_form_constant
20958 | dw_scalar_form_exprloc
20959 | dw_scalar_form_reference,
20960 &context);
20963 gen_type_die (info->element_type, context_die);
20964 add_type_attribute (array_die, info->element_type, TYPE_UNQUALIFIED,
20965 TREE_CODE (type) == ARRAY_TYPE
20966 && TYPE_REVERSE_STORAGE_ORDER (type),
20967 context_die);
20969 if (get_AT (array_die, DW_AT_name))
20970 add_pubtype (type, array_die);
20972 add_alignment_attribute (array_die, type);
20975 #if 0
20976 static void
20977 gen_entry_point_die (tree decl, dw_die_ref context_die)
20979 tree origin = decl_ultimate_origin (decl);
20980 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
20982 if (origin != NULL)
20983 add_abstract_origin_attribute (decl_die, origin);
20984 else
20986 add_name_and_src_coords_attributes (decl_die, decl);
20987 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
20988 TYPE_UNQUALIFIED, false, context_die);
20991 if (DECL_ABSTRACT_P (decl))
20992 equate_decl_number_to_die (decl, decl_die);
20993 else
20994 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
20996 #endif
20998 /* Walk through the list of incomplete types again, trying once more to
20999 emit full debugging info for them. */
21001 static void
21002 retry_incomplete_types (void)
21004 set_early_dwarf s;
21005 int i;
21007 for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
21008 if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
21009 gen_type_die ((*incomplete_types)[i], comp_unit_die ());
21010 vec_safe_truncate (incomplete_types, 0);
21013 /* Determine what tag to use for a record type. */
21015 static enum dwarf_tag
21016 record_type_tag (tree type)
21018 if (! lang_hooks.types.classify_record)
21019 return DW_TAG_structure_type;
21021 switch (lang_hooks.types.classify_record (type))
21023 case RECORD_IS_STRUCT:
21024 return DW_TAG_structure_type;
21026 case RECORD_IS_CLASS:
21027 return DW_TAG_class_type;
21029 case RECORD_IS_INTERFACE:
21030 if (dwarf_version >= 3 || !dwarf_strict)
21031 return DW_TAG_interface_type;
21032 return DW_TAG_structure_type;
21034 default:
21035 gcc_unreachable ();
21039 /* Generate a DIE to represent an enumeration type. Note that these DIEs
21040 include all of the information about the enumeration values also. Each
21041 enumerated type name/value is listed as a child of the enumerated type
21042 DIE. */
21044 static dw_die_ref
21045 gen_enumeration_type_die (tree type, dw_die_ref context_die)
21047 dw_die_ref type_die = lookup_type_die (type);
21049 if (type_die == NULL)
21051 type_die = new_die (DW_TAG_enumeration_type,
21052 scope_die_for (type, context_die), type);
21053 equate_type_number_to_die (type, type_die);
21054 add_name_attribute (type_die, type_tag (type));
21055 if (dwarf_version >= 4 || !dwarf_strict)
21057 if (ENUM_IS_SCOPED (type))
21058 add_AT_flag (type_die, DW_AT_enum_class, 1);
21059 if (ENUM_IS_OPAQUE (type))
21060 add_AT_flag (type_die, DW_AT_declaration, 1);
21062 if (!dwarf_strict)
21063 add_AT_unsigned (type_die, DW_AT_encoding,
21064 TYPE_UNSIGNED (type)
21065 ? DW_ATE_unsigned
21066 : DW_ATE_signed);
21068 else if (! TYPE_SIZE (type))
21069 return type_die;
21070 else
21071 remove_AT (type_die, DW_AT_declaration);
21073 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
21074 given enum type is incomplete, do not generate the DW_AT_byte_size
21075 attribute or the DW_AT_element_list attribute. */
21076 if (TYPE_SIZE (type))
21078 tree link;
21080 TREE_ASM_WRITTEN (type) = 1;
21081 add_byte_size_attribute (type_die, type);
21082 add_alignment_attribute (type_die, type);
21083 if (dwarf_version >= 3 || !dwarf_strict)
21085 tree underlying = lang_hooks.types.enum_underlying_base_type (type);
21086 add_type_attribute (type_die, underlying, TYPE_UNQUALIFIED, false,
21087 context_die);
21089 if (TYPE_STUB_DECL (type) != NULL_TREE)
21091 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
21092 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
21095 /* If the first reference to this type was as the return type of an
21096 inline function, then it may not have a parent. Fix this now. */
21097 if (type_die->die_parent == NULL)
21098 add_child_die (scope_die_for (type, context_die), type_die);
21100 for (link = TYPE_VALUES (type);
21101 link != NULL; link = TREE_CHAIN (link))
21103 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
21104 tree value = TREE_VALUE (link);
21106 add_name_attribute (enum_die,
21107 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
21109 if (TREE_CODE (value) == CONST_DECL)
21110 value = DECL_INITIAL (value);
21112 if (simple_type_size_in_bits (TREE_TYPE (value))
21113 <= HOST_BITS_PER_WIDE_INT || tree_fits_shwi_p (value))
21115 /* For constant forms created by add_AT_unsigned DWARF
21116 consumers (GDB, elfutils, etc.) always zero extend
21117 the value. Only when the actual value is negative
21118 do we need to use add_AT_int to generate a constant
21119 form that can represent negative values. */
21120 HOST_WIDE_INT val = TREE_INT_CST_LOW (value);
21121 if (TYPE_UNSIGNED (TREE_TYPE (value)) || val >= 0)
21122 add_AT_unsigned (enum_die, DW_AT_const_value,
21123 (unsigned HOST_WIDE_INT) val);
21124 else
21125 add_AT_int (enum_die, DW_AT_const_value, val);
21127 else
21128 /* Enumeration constants may be wider than HOST_WIDE_INT. Handle
21129 that here. TODO: This should be re-worked to use correct
21130 signed/unsigned double tags for all cases. */
21131 add_AT_wide (enum_die, DW_AT_const_value, value);
21134 add_gnat_descriptive_type_attribute (type_die, type, context_die);
21135 if (TYPE_ARTIFICIAL (type))
21136 add_AT_flag (type_die, DW_AT_artificial, 1);
21138 else
21139 add_AT_flag (type_die, DW_AT_declaration, 1);
21141 add_alignment_attribute (type_die, type);
21143 add_pubtype (type, type_die);
21145 return type_die;
21148 /* Generate a DIE to represent either a real live formal parameter decl or to
21149 represent just the type of some formal parameter position in some function
21150 type.
21152 Note that this routine is a bit unusual because its argument may be a
21153 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
21154 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
21155 node. If it's the former then this function is being called to output a
21156 DIE to represent a formal parameter object (or some inlining thereof). If
21157 it's the latter, then this function is only being called to output a
21158 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
21159 argument type of some subprogram type.
21160 If EMIT_NAME_P is true, name and source coordinate attributes
21161 are emitted. */
21163 static dw_die_ref
21164 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
21165 dw_die_ref context_die)
21167 tree node_or_origin = node ? node : origin;
21168 tree ultimate_origin;
21169 dw_die_ref parm_die = NULL;
21171 if (TREE_CODE_CLASS (TREE_CODE (node_or_origin)) == tcc_declaration)
21173 parm_die = lookup_decl_die (node);
21175 /* If the contexts differ, we may not be talking about the same
21176 thing. */
21177 if (parm_die && parm_die->die_parent != context_die)
21179 if (!DECL_ABSTRACT_P (node))
21181 /* This can happen when creating an inlined instance, in
21182 which case we need to create a new DIE that will get
21183 annotated with DW_AT_abstract_origin. */
21184 parm_die = NULL;
21186 else
21188 /* FIXME: Reuse DIE even with a differing context.
21190 This can happen when calling
21191 dwarf2out_abstract_function to build debug info for
21192 the abstract instance of a function for which we have
21193 already generated a DIE in
21194 dwarf2out_early_global_decl.
21196 Once we remove dwarf2out_abstract_function, we should
21197 have a call to gcc_unreachable here. */
21201 if (parm_die && parm_die->die_parent == NULL)
21203 /* Check that parm_die already has the right attributes that
21204 we would have added below. If any attributes are
21205 missing, fall through to add them. */
21206 if (! DECL_ABSTRACT_P (node_or_origin)
21207 && !get_AT (parm_die, DW_AT_location)
21208 && !get_AT (parm_die, DW_AT_const_value))
21209 /* We are missing location info, and are about to add it. */
21211 else
21213 add_child_die (context_die, parm_die);
21214 return parm_die;
21219 /* If we have a previously generated DIE, use it, unless this is an
21220 concrete instance (origin != NULL), in which case we need a new
21221 DIE with a corresponding DW_AT_abstract_origin. */
21222 bool reusing_die;
21223 if (parm_die && origin == NULL)
21224 reusing_die = true;
21225 else
21227 parm_die = new_die (DW_TAG_formal_parameter, context_die, node);
21228 reusing_die = false;
21231 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
21233 case tcc_declaration:
21234 ultimate_origin = decl_ultimate_origin (node_or_origin);
21235 if (node || ultimate_origin)
21236 origin = ultimate_origin;
21238 if (reusing_die)
21239 goto add_location;
21241 if (origin != NULL)
21242 add_abstract_origin_attribute (parm_die, origin);
21243 else if (emit_name_p)
21244 add_name_and_src_coords_attributes (parm_die, node);
21245 if (origin == NULL
21246 || (! DECL_ABSTRACT_P (node_or_origin)
21247 && variably_modified_type_p (TREE_TYPE (node_or_origin),
21248 decl_function_context
21249 (node_or_origin))))
21251 tree type = TREE_TYPE (node_or_origin);
21252 if (decl_by_reference_p (node_or_origin))
21253 add_type_attribute (parm_die, TREE_TYPE (type),
21254 TYPE_UNQUALIFIED,
21255 false, context_die);
21256 else
21257 add_type_attribute (parm_die, type,
21258 decl_quals (node_or_origin),
21259 false, context_die);
21261 if (origin == NULL && DECL_ARTIFICIAL (node))
21262 add_AT_flag (parm_die, DW_AT_artificial, 1);
21263 add_location:
21264 if (node && node != origin)
21265 equate_decl_number_to_die (node, parm_die);
21266 if (! DECL_ABSTRACT_P (node_or_origin))
21267 add_location_or_const_value_attribute (parm_die, node_or_origin,
21268 node == NULL);
21270 break;
21272 case tcc_type:
21273 /* We were called with some kind of a ..._TYPE node. */
21274 add_type_attribute (parm_die, node_or_origin, TYPE_UNQUALIFIED, false,
21275 context_die);
21276 break;
21278 default:
21279 gcc_unreachable ();
21282 return parm_die;
21285 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
21286 children DW_TAG_formal_parameter DIEs representing the arguments of the
21287 parameter pack.
21289 PARM_PACK must be a function parameter pack.
21290 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
21291 must point to the subsequent arguments of the function PACK_ARG belongs to.
21292 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
21293 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
21294 following the last one for which a DIE was generated. */
21296 static dw_die_ref
21297 gen_formal_parameter_pack_die (tree parm_pack,
21298 tree pack_arg,
21299 dw_die_ref subr_die,
21300 tree *next_arg)
21302 tree arg;
21303 dw_die_ref parm_pack_die;
21305 gcc_assert (parm_pack
21306 && lang_hooks.function_parameter_pack_p (parm_pack)
21307 && subr_die);
21309 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
21310 add_src_coords_attributes (parm_pack_die, parm_pack);
21312 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
21314 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
21315 parm_pack))
21316 break;
21317 gen_formal_parameter_die (arg, NULL,
21318 false /* Don't emit name attribute. */,
21319 parm_pack_die);
21321 if (next_arg)
21322 *next_arg = arg;
21323 return parm_pack_die;
21326 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
21327 at the end of an (ANSI prototyped) formal parameters list. */
21329 static void
21330 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
21332 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
21335 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
21336 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
21337 parameters as specified in some function type specification (except for
21338 those which appear as part of a function *definition*). */
21340 static void
21341 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
21343 tree link;
21344 tree formal_type = NULL;
21345 tree first_parm_type;
21346 tree arg;
21348 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
21350 arg = DECL_ARGUMENTS (function_or_method_type);
21351 function_or_method_type = TREE_TYPE (function_or_method_type);
21353 else
21354 arg = NULL_TREE;
21356 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
21358 /* Make our first pass over the list of formal parameter types and output a
21359 DW_TAG_formal_parameter DIE for each one. */
21360 for (link = first_parm_type; link; )
21362 dw_die_ref parm_die;
21364 formal_type = TREE_VALUE (link);
21365 if (formal_type == void_type_node)
21366 break;
21368 /* Output a (nameless) DIE to represent the formal parameter itself. */
21369 if (!POINTER_BOUNDS_TYPE_P (formal_type))
21371 parm_die = gen_formal_parameter_die (formal_type, NULL,
21372 true /* Emit name attribute. */,
21373 context_die);
21374 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
21375 && link == first_parm_type)
21377 add_AT_flag (parm_die, DW_AT_artificial, 1);
21378 if (dwarf_version >= 3 || !dwarf_strict)
21379 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
21381 else if (arg && DECL_ARTIFICIAL (arg))
21382 add_AT_flag (parm_die, DW_AT_artificial, 1);
21385 link = TREE_CHAIN (link);
21386 if (arg)
21387 arg = DECL_CHAIN (arg);
21390 /* If this function type has an ellipsis, add a
21391 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
21392 if (formal_type != void_type_node)
21393 gen_unspecified_parameters_die (function_or_method_type, context_die);
21395 /* Make our second (and final) pass over the list of formal parameter types
21396 and output DIEs to represent those types (as necessary). */
21397 for (link = TYPE_ARG_TYPES (function_or_method_type);
21398 link && TREE_VALUE (link);
21399 link = TREE_CHAIN (link))
21400 gen_type_die (TREE_VALUE (link), context_die);
21403 /* We want to generate the DIE for TYPE so that we can generate the
21404 die for MEMBER, which has been defined; we will need to refer back
21405 to the member declaration nested within TYPE. If we're trying to
21406 generate minimal debug info for TYPE, processing TYPE won't do the
21407 trick; we need to attach the member declaration by hand. */
21409 static void
21410 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
21412 gen_type_die (type, context_die);
21414 /* If we're trying to avoid duplicate debug info, we may not have
21415 emitted the member decl for this function. Emit it now. */
21416 if (TYPE_STUB_DECL (type)
21417 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
21418 && ! lookup_decl_die (member))
21420 dw_die_ref type_die;
21421 gcc_assert (!decl_ultimate_origin (member));
21423 push_decl_scope (type);
21424 type_die = lookup_type_die_strip_naming_typedef (type);
21425 if (TREE_CODE (member) == FUNCTION_DECL)
21426 gen_subprogram_die (member, type_die);
21427 else if (TREE_CODE (member) == FIELD_DECL)
21429 /* Ignore the nameless fields that are used to skip bits but handle
21430 C++ anonymous unions and structs. */
21431 if (DECL_NAME (member) != NULL_TREE
21432 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
21433 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
21435 struct vlr_context vlr_ctx = {
21436 DECL_CONTEXT (member), /* struct_type */
21437 NULL_TREE /* variant_part_offset */
21439 gen_type_die (member_declared_type (member), type_die);
21440 gen_field_die (member, &vlr_ctx, type_die);
21443 else
21444 gen_variable_die (member, NULL_TREE, type_die);
21446 pop_decl_scope ();
21450 /* Forward declare these functions, because they are mutually recursive
21451 with their set_block_* pairing functions. */
21452 static void set_decl_origin_self (tree);
21453 static void set_decl_abstract_flags (tree, vec<tree> &);
21455 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
21456 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
21457 that it points to the node itself, thus indicating that the node is its
21458 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
21459 the given node is NULL, recursively descend the decl/block tree which
21460 it is the root of, and for each other ..._DECL or BLOCK node contained
21461 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
21462 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
21463 values to point to themselves. */
21465 static void
21466 set_block_origin_self (tree stmt)
21468 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
21470 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
21473 tree local_decl;
21475 for (local_decl = BLOCK_VARS (stmt);
21476 local_decl != NULL_TREE;
21477 local_decl = DECL_CHAIN (local_decl))
21478 /* Do not recurse on nested functions since the inlining status
21479 of parent and child can be different as per the DWARF spec. */
21480 if (TREE_CODE (local_decl) != FUNCTION_DECL
21481 && !DECL_EXTERNAL (local_decl))
21482 set_decl_origin_self (local_decl);
21486 tree subblock;
21488 for (subblock = BLOCK_SUBBLOCKS (stmt);
21489 subblock != NULL_TREE;
21490 subblock = BLOCK_CHAIN (subblock))
21491 set_block_origin_self (subblock); /* Recurse. */
21496 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
21497 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
21498 node to so that it points to the node itself, thus indicating that the
21499 node represents its own (abstract) origin. Additionally, if the
21500 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
21501 the decl/block tree of which the given node is the root of, and for
21502 each other ..._DECL or BLOCK node contained therein whose
21503 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
21504 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
21505 point to themselves. */
21507 static void
21508 set_decl_origin_self (tree decl)
21510 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
21512 DECL_ABSTRACT_ORIGIN (decl) = decl;
21513 if (TREE_CODE (decl) == FUNCTION_DECL)
21515 tree arg;
21517 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
21518 DECL_ABSTRACT_ORIGIN (arg) = arg;
21519 if (DECL_INITIAL (decl) != NULL_TREE
21520 && DECL_INITIAL (decl) != error_mark_node)
21521 set_block_origin_self (DECL_INITIAL (decl));
21526 /* Given a pointer to some BLOCK node, set the BLOCK_ABSTRACT flag to 1
21527 and if it wasn't 1 before, push it to abstract_vec vector.
21528 For all local decls and all local sub-blocks (recursively) do it
21529 too. */
21531 static void
21532 set_block_abstract_flags (tree stmt, vec<tree> &abstract_vec)
21534 tree local_decl;
21535 tree subblock;
21536 unsigned int i;
21538 if (!BLOCK_ABSTRACT (stmt))
21540 abstract_vec.safe_push (stmt);
21541 BLOCK_ABSTRACT (stmt) = 1;
21544 for (local_decl = BLOCK_VARS (stmt);
21545 local_decl != NULL_TREE;
21546 local_decl = DECL_CHAIN (local_decl))
21547 if (! DECL_EXTERNAL (local_decl))
21548 set_decl_abstract_flags (local_decl, abstract_vec);
21550 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
21552 local_decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
21553 if ((VAR_P (local_decl) && !TREE_STATIC (local_decl))
21554 || TREE_CODE (local_decl) == PARM_DECL)
21555 set_decl_abstract_flags (local_decl, abstract_vec);
21558 for (subblock = BLOCK_SUBBLOCKS (stmt);
21559 subblock != NULL_TREE;
21560 subblock = BLOCK_CHAIN (subblock))
21561 set_block_abstract_flags (subblock, abstract_vec);
21564 /* Given a pointer to some ..._DECL node, set DECL_ABSTRACT_P flag on it
21565 to 1 and if it wasn't 1 before, push to abstract_vec vector.
21566 In the case where the decl is a FUNCTION_DECL also set the abstract
21567 flags for all of the parameters, local vars, local
21568 blocks and sub-blocks (recursively). */
21570 static void
21571 set_decl_abstract_flags (tree decl, vec<tree> &abstract_vec)
21573 if (!DECL_ABSTRACT_P (decl))
21575 abstract_vec.safe_push (decl);
21576 DECL_ABSTRACT_P (decl) = 1;
21579 if (TREE_CODE (decl) == FUNCTION_DECL)
21581 tree arg;
21583 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
21584 if (!DECL_ABSTRACT_P (arg))
21586 abstract_vec.safe_push (arg);
21587 DECL_ABSTRACT_P (arg) = 1;
21589 if (DECL_INITIAL (decl) != NULL_TREE
21590 && DECL_INITIAL (decl) != error_mark_node)
21591 set_block_abstract_flags (DECL_INITIAL (decl), abstract_vec);
21595 /* Generate the DWARF2 info for the "abstract" instance of a function which we
21596 may later generate inlined and/or out-of-line instances of.
21598 FIXME: In the early-dwarf world, this function, and most of the
21599 DECL_ABSTRACT code should be obsoleted. The early DIE _is_
21600 the abstract instance. All we would need to do is annotate
21601 the early DIE with the appropriate DW_AT_inline in late
21602 dwarf (perhaps in gen_inlined_subroutine_die).
21604 However, we can't do this yet, because LTO streaming of DIEs
21605 has not been implemented yet. */
21607 static void
21608 dwarf2out_abstract_function (tree decl)
21610 dw_die_ref old_die;
21611 tree save_fn;
21612 tree context;
21613 hash_table<decl_loc_hasher> *old_decl_loc_table;
21614 hash_table<dw_loc_list_hasher> *old_cached_dw_loc_list_table;
21615 int old_call_site_count, old_tail_call_site_count;
21616 struct call_arg_loc_node *old_call_arg_locations;
21618 /* Make sure we have the actual abstract inline, not a clone. */
21619 decl = DECL_ORIGIN (decl);
21621 old_die = lookup_decl_die (decl);
21622 if (old_die && get_AT (old_die, DW_AT_inline))
21623 /* We've already generated the abstract instance. */
21624 return;
21626 /* We can be called while recursively when seeing block defining inlined subroutine
21627 DIE. Be sure to not clobber the outer location table nor use it or we would
21628 get locations in abstract instantces. */
21629 old_decl_loc_table = decl_loc_table;
21630 decl_loc_table = NULL;
21631 old_cached_dw_loc_list_table = cached_dw_loc_list_table;
21632 cached_dw_loc_list_table = NULL;
21633 old_call_arg_locations = call_arg_locations;
21634 call_arg_locations = NULL;
21635 old_call_site_count = call_site_count;
21636 call_site_count = -1;
21637 old_tail_call_site_count = tail_call_site_count;
21638 tail_call_site_count = -1;
21640 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
21641 we don't get confused by DECL_ABSTRACT_P. */
21642 if (debug_info_level > DINFO_LEVEL_TERSE)
21644 context = decl_class_context (decl);
21645 if (context)
21646 gen_type_die_for_member
21647 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
21650 /* Pretend we've just finished compiling this function. */
21651 save_fn = current_function_decl;
21652 current_function_decl = decl;
21654 auto_vec<tree, 64> abstract_vec;
21655 set_decl_abstract_flags (decl, abstract_vec);
21656 dwarf2out_decl (decl);
21657 unsigned int i;
21658 tree t;
21659 FOR_EACH_VEC_ELT (abstract_vec, i, t)
21660 if (TREE_CODE (t) == BLOCK)
21661 BLOCK_ABSTRACT (t) = 0;
21662 else
21663 DECL_ABSTRACT_P (t) = 0;
21665 current_function_decl = save_fn;
21666 decl_loc_table = old_decl_loc_table;
21667 cached_dw_loc_list_table = old_cached_dw_loc_list_table;
21668 call_arg_locations = old_call_arg_locations;
21669 call_site_count = old_call_site_count;
21670 tail_call_site_count = old_tail_call_site_count;
21673 /* Helper function of premark_used_types() which gets called through
21674 htab_traverse.
21676 Marks the DIE of a given type in *SLOT as perennial, so it never gets
21677 marked as unused by prune_unused_types. */
21679 bool
21680 premark_used_types_helper (tree const &type, void *)
21682 dw_die_ref die;
21684 die = lookup_type_die (type);
21685 if (die != NULL)
21686 die->die_perennial_p = 1;
21687 return true;
21690 /* Helper function of premark_types_used_by_global_vars which gets called
21691 through htab_traverse.
21693 Marks the DIE of a given type in *SLOT as perennial, so it never gets
21694 marked as unused by prune_unused_types. The DIE of the type is marked
21695 only if the global variable using the type will actually be emitted. */
21698 premark_types_used_by_global_vars_helper (types_used_by_vars_entry **slot,
21699 void *)
21701 struct types_used_by_vars_entry *entry;
21702 dw_die_ref die;
21704 entry = (struct types_used_by_vars_entry *) *slot;
21705 gcc_assert (entry->type != NULL
21706 && entry->var_decl != NULL);
21707 die = lookup_type_die (entry->type);
21708 if (die)
21710 /* Ask cgraph if the global variable really is to be emitted.
21711 If yes, then we'll keep the DIE of ENTRY->TYPE. */
21712 varpool_node *node = varpool_node::get (entry->var_decl);
21713 if (node && node->definition)
21715 die->die_perennial_p = 1;
21716 /* Keep the parent DIEs as well. */
21717 while ((die = die->die_parent) && die->die_perennial_p == 0)
21718 die->die_perennial_p = 1;
21721 return 1;
21724 /* Mark all members of used_types_hash as perennial. */
21726 static void
21727 premark_used_types (struct function *fun)
21729 if (fun && fun->used_types_hash)
21730 fun->used_types_hash->traverse<void *, premark_used_types_helper> (NULL);
21733 /* Mark all members of types_used_by_vars_entry as perennial. */
21735 static void
21736 premark_types_used_by_global_vars (void)
21738 if (types_used_by_vars_hash)
21739 types_used_by_vars_hash
21740 ->traverse<void *, premark_types_used_by_global_vars_helper> (NULL);
21743 /* Generate a DW_TAG_call_site DIE in function DECL under SUBR_DIE
21744 for CA_LOC call arg loc node. */
21746 static dw_die_ref
21747 gen_call_site_die (tree decl, dw_die_ref subr_die,
21748 struct call_arg_loc_node *ca_loc)
21750 dw_die_ref stmt_die = NULL, die;
21751 tree block = ca_loc->block;
21753 while (block
21754 && block != DECL_INITIAL (decl)
21755 && TREE_CODE (block) == BLOCK)
21757 stmt_die = BLOCK_DIE (block);
21758 if (stmt_die)
21759 break;
21760 block = BLOCK_SUPERCONTEXT (block);
21762 if (stmt_die == NULL)
21763 stmt_die = subr_die;
21764 die = new_die (dwarf_TAG (DW_TAG_call_site), stmt_die, NULL_TREE);
21765 add_AT_lbl_id (die, dwarf_AT (DW_AT_call_return_pc), ca_loc->label);
21766 if (ca_loc->tail_call_p)
21767 add_AT_flag (die, dwarf_AT (DW_AT_call_tail_call), 1);
21768 if (ca_loc->symbol_ref)
21770 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
21771 if (tdie)
21772 add_AT_die_ref (die, dwarf_AT (DW_AT_call_origin), tdie);
21773 else
21774 add_AT_addr (die, dwarf_AT (DW_AT_call_origin), ca_loc->symbol_ref,
21775 false);
21777 return die;
21780 /* Generate a DIE to represent a declared function (either file-scope or
21781 block-local). */
21783 static void
21784 gen_subprogram_die (tree decl, dw_die_ref context_die)
21786 tree origin = decl_ultimate_origin (decl);
21787 dw_die_ref subr_die;
21788 dw_die_ref old_die = lookup_decl_die (decl);
21790 /* This function gets called multiple times for different stages of
21791 the debug process. For example, for func() in this code:
21793 namespace S
21795 void func() { ... }
21798 ...we get called 4 times. Twice in early debug and twice in
21799 late debug:
21801 Early debug
21802 -----------
21804 1. Once while generating func() within the namespace. This is
21805 the declaration. The declaration bit below is set, as the
21806 context is the namespace.
21808 A new DIE will be generated with DW_AT_declaration set.
21810 2. Once for func() itself. This is the specification. The
21811 declaration bit below is clear as the context is the CU.
21813 We will use the cached DIE from (1) to create a new DIE with
21814 DW_AT_specification pointing to the declaration in (1).
21816 Late debug via rest_of_handle_final()
21817 -------------------------------------
21819 3. Once generating func() within the namespace. This is also the
21820 declaration, as in (1), but this time we will early exit below
21821 as we have a cached DIE and a declaration needs no additional
21822 annotations (no locations), as the source declaration line
21823 info is enough.
21825 4. Once for func() itself. As in (2), this is the specification,
21826 but this time we will re-use the cached DIE, and just annotate
21827 it with the location information that should now be available.
21829 For something without namespaces, but with abstract instances, we
21830 are also called a multiple times:
21832 class Base
21834 public:
21835 Base (); // constructor declaration (1)
21838 Base::Base () { } // constructor specification (2)
21840 Early debug
21841 -----------
21843 1. Once for the Base() constructor by virtue of it being a
21844 member of the Base class. This is done via
21845 rest_of_type_compilation.
21847 This is a declaration, so a new DIE will be created with
21848 DW_AT_declaration.
21850 2. Once for the Base() constructor definition, but this time
21851 while generating the abstract instance of the base
21852 constructor (__base_ctor) which is being generated via early
21853 debug of reachable functions.
21855 Even though we have a cached version of the declaration (1),
21856 we will create a DW_AT_specification of the declaration DIE
21857 in (1).
21859 3. Once for the __base_ctor itself, but this time, we generate
21860 an DW_AT_abstract_origin version of the DW_AT_specification in
21861 (2).
21863 Late debug via rest_of_handle_final
21864 -----------------------------------
21866 4. One final time for the __base_ctor (which will have a cached
21867 DIE with DW_AT_abstract_origin created in (3). This time,
21868 we will just annotate the location information now
21869 available.
21871 int declaration = (current_function_decl != decl
21872 || class_or_namespace_scope_p (context_die));
21874 /* Now that the C++ front end lazily declares artificial member fns, we
21875 might need to retrofit the declaration into its class. */
21876 if (!declaration && !origin && !old_die
21877 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
21878 && !class_or_namespace_scope_p (context_die)
21879 && debug_info_level > DINFO_LEVEL_TERSE)
21880 old_die = force_decl_die (decl);
21882 /* An inlined instance, tag a new DIE with DW_AT_abstract_origin. */
21883 if (origin != NULL)
21885 gcc_assert (!declaration || local_scope_p (context_die));
21887 /* Fixup die_parent for the abstract instance of a nested
21888 inline function. */
21889 if (old_die && old_die->die_parent == NULL)
21890 add_child_die (context_die, old_die);
21892 if (old_die && get_AT_ref (old_die, DW_AT_abstract_origin))
21894 /* If we have a DW_AT_abstract_origin we have a working
21895 cached version. */
21896 subr_die = old_die;
21898 else
21900 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
21901 add_abstract_origin_attribute (subr_die, origin);
21902 /* This is where the actual code for a cloned function is.
21903 Let's emit linkage name attribute for it. This helps
21904 debuggers to e.g, set breakpoints into
21905 constructors/destructors when the user asks "break
21906 K::K". */
21907 add_linkage_name (subr_die, decl);
21910 /* A cached copy, possibly from early dwarf generation. Reuse as
21911 much as possible. */
21912 else if (old_die)
21914 /* A declaration that has been previously dumped needs no
21915 additional information. */
21916 if (declaration)
21917 return;
21919 if (!get_AT_flag (old_die, DW_AT_declaration)
21920 /* We can have a normal definition following an inline one in the
21921 case of redefinition of GNU C extern inlines.
21922 It seems reasonable to use AT_specification in this case. */
21923 && !get_AT (old_die, DW_AT_inline))
21925 /* Detect and ignore this case, where we are trying to output
21926 something we have already output. */
21927 if (get_AT (old_die, DW_AT_low_pc)
21928 || get_AT (old_die, DW_AT_ranges))
21929 return;
21931 /* If we have no location information, this must be a
21932 partially generated DIE from early dwarf generation.
21933 Fall through and generate it. */
21936 /* If the definition comes from the same place as the declaration,
21937 maybe use the old DIE. We always want the DIE for this function
21938 that has the *_pc attributes to be under comp_unit_die so the
21939 debugger can find it. We also need to do this for abstract
21940 instances of inlines, since the spec requires the out-of-line copy
21941 to have the same parent. For local class methods, this doesn't
21942 apply; we just use the old DIE. */
21943 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
21944 struct dwarf_file_data * file_index = lookup_filename (s.file);
21945 if ((is_cu_die (old_die->die_parent)
21946 /* This condition fixes the inconsistency/ICE with the
21947 following Fortran test (or some derivative thereof) while
21948 building libgfortran:
21950 module some_m
21951 contains
21952 logical function funky (FLAG)
21953 funky = .true.
21954 end function
21955 end module
21957 || (old_die->die_parent
21958 && old_die->die_parent->die_tag == DW_TAG_module)
21959 || context_die == NULL)
21960 && (DECL_ARTIFICIAL (decl)
21961 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
21962 && (get_AT_unsigned (old_die, DW_AT_decl_line)
21963 == (unsigned) s.line)
21964 && (!debug_column_info
21965 || s.column == 0
21966 || (get_AT_unsigned (old_die, DW_AT_decl_column)
21967 == (unsigned) s.column)))))
21969 subr_die = old_die;
21971 /* Clear out the declaration attribute, but leave the
21972 parameters so they can be augmented with location
21973 information later. Unless this was a declaration, in
21974 which case, wipe out the nameless parameters and recreate
21975 them further down. */
21976 if (remove_AT (subr_die, DW_AT_declaration))
21979 remove_AT (subr_die, DW_AT_object_pointer);
21980 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
21983 /* Make a specification pointing to the previously built
21984 declaration. */
21985 else
21987 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
21988 add_AT_specification (subr_die, old_die);
21989 add_pubname (decl, subr_die);
21990 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
21991 add_AT_file (subr_die, DW_AT_decl_file, file_index);
21992 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
21993 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
21994 if (debug_column_info
21995 && s.column
21996 && (get_AT_unsigned (old_die, DW_AT_decl_column)
21997 != (unsigned) s.column))
21998 add_AT_unsigned (subr_die, DW_AT_decl_column, s.column);
22000 /* If the prototype had an 'auto' or 'decltype(auto)' return type,
22001 emit the real type on the definition die. */
22002 if (is_cxx () && debug_info_level > DINFO_LEVEL_TERSE)
22004 dw_die_ref die = get_AT_ref (old_die, DW_AT_type);
22005 if (die == auto_die || die == decltype_auto_die)
22006 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
22007 TYPE_UNQUALIFIED, false, context_die);
22010 /* When we process the method declaration, we haven't seen
22011 the out-of-class defaulted definition yet, so we have to
22012 recheck now. */
22013 if ((dwarf_version >= 5 || ! dwarf_strict)
22014 && !get_AT (subr_die, DW_AT_defaulted))
22016 int defaulted
22017 = lang_hooks.decls.decl_dwarf_attribute (decl,
22018 DW_AT_defaulted);
22019 if (defaulted != -1)
22021 /* Other values must have been handled before. */
22022 gcc_assert (defaulted == DW_DEFAULTED_out_of_class);
22023 add_AT_unsigned (subr_die, DW_AT_defaulted, defaulted);
22028 /* Create a fresh DIE for anything else. */
22029 else
22031 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
22033 if (TREE_PUBLIC (decl))
22034 add_AT_flag (subr_die, DW_AT_external, 1);
22036 add_name_and_src_coords_attributes (subr_die, decl);
22037 add_pubname (decl, subr_die);
22038 if (debug_info_level > DINFO_LEVEL_TERSE)
22040 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
22041 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
22042 TYPE_UNQUALIFIED, false, context_die);
22045 add_pure_or_virtual_attribute (subr_die, decl);
22046 if (DECL_ARTIFICIAL (decl))
22047 add_AT_flag (subr_die, DW_AT_artificial, 1);
22049 if (TREE_THIS_VOLATILE (decl) && (dwarf_version >= 5 || !dwarf_strict))
22050 add_AT_flag (subr_die, DW_AT_noreturn, 1);
22052 add_alignment_attribute (subr_die, decl);
22054 add_accessibility_attribute (subr_die, decl);
22057 /* Unless we have an existing non-declaration DIE, equate the new
22058 DIE. */
22059 if (!old_die || is_declaration_die (old_die))
22060 equate_decl_number_to_die (decl, subr_die);
22062 if (declaration)
22064 if (!old_die || !get_AT (old_die, DW_AT_inline))
22066 add_AT_flag (subr_die, DW_AT_declaration, 1);
22068 /* If this is an explicit function declaration then generate
22069 a DW_AT_explicit attribute. */
22070 if ((dwarf_version >= 3 || !dwarf_strict)
22071 && lang_hooks.decls.decl_dwarf_attribute (decl,
22072 DW_AT_explicit) == 1)
22073 add_AT_flag (subr_die, DW_AT_explicit, 1);
22075 /* If this is a C++11 deleted special function member then generate
22076 a DW_AT_deleted attribute. */
22077 if ((dwarf_version >= 5 || !dwarf_strict)
22078 && lang_hooks.decls.decl_dwarf_attribute (decl,
22079 DW_AT_deleted) == 1)
22080 add_AT_flag (subr_die, DW_AT_deleted, 1);
22082 /* If this is a C++11 defaulted special function member then
22083 generate a DW_AT_defaulted attribute. */
22084 if (dwarf_version >= 5 || !dwarf_strict)
22086 int defaulted
22087 = lang_hooks.decls.decl_dwarf_attribute (decl,
22088 DW_AT_defaulted);
22089 if (defaulted != -1)
22090 add_AT_unsigned (subr_die, DW_AT_defaulted, defaulted);
22093 /* If this is a C++11 non-static member function with & ref-qualifier
22094 then generate a DW_AT_reference attribute. */
22095 if ((dwarf_version >= 5 || !dwarf_strict)
22096 && lang_hooks.decls.decl_dwarf_attribute (decl,
22097 DW_AT_reference) == 1)
22098 add_AT_flag (subr_die, DW_AT_reference, 1);
22100 /* If this is a C++11 non-static member function with &&
22101 ref-qualifier then generate a DW_AT_reference attribute. */
22102 if ((dwarf_version >= 5 || !dwarf_strict)
22103 && lang_hooks.decls.decl_dwarf_attribute (decl,
22104 DW_AT_rvalue_reference)
22105 == 1)
22106 add_AT_flag (subr_die, DW_AT_rvalue_reference, 1);
22109 /* Tag abstract instances with DW_AT_inline. */
22110 else if (DECL_ABSTRACT_P (decl))
22112 if (DECL_DECLARED_INLINE_P (decl))
22114 if (cgraph_function_possibly_inlined_p (decl))
22115 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
22116 else
22117 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
22119 else
22121 if (cgraph_function_possibly_inlined_p (decl))
22122 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
22123 else
22124 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
22127 if (DECL_DECLARED_INLINE_P (decl)
22128 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
22129 add_AT_flag (subr_die, DW_AT_artificial, 1);
22131 /* For non DECL_EXTERNALs, if range information is available, fill
22132 the DIE with it. */
22133 else if (!DECL_EXTERNAL (decl) && !early_dwarf)
22135 HOST_WIDE_INT cfa_fb_offset;
22137 struct function *fun = DECL_STRUCT_FUNCTION (decl);
22139 if (!flag_reorder_blocks_and_partition)
22141 dw_fde_ref fde = fun->fde;
22142 if (fde->dw_fde_begin)
22144 /* We have already generated the labels. */
22145 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
22146 fde->dw_fde_end, false);
22148 else
22150 /* Create start/end labels and add the range. */
22151 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
22152 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
22153 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
22154 current_function_funcdef_no);
22155 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
22156 current_function_funcdef_no);
22157 add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
22158 false);
22161 #if VMS_DEBUGGING_INFO
22162 /* HP OpenVMS Industry Standard 64: DWARF Extensions
22163 Section 2.3 Prologue and Epilogue Attributes:
22164 When a breakpoint is set on entry to a function, it is generally
22165 desirable for execution to be suspended, not on the very first
22166 instruction of the function, but rather at a point after the
22167 function's frame has been set up, after any language defined local
22168 declaration processing has been completed, and before execution of
22169 the first statement of the function begins. Debuggers generally
22170 cannot properly determine where this point is. Similarly for a
22171 breakpoint set on exit from a function. The prologue and epilogue
22172 attributes allow a compiler to communicate the location(s) to use. */
22175 if (fde->dw_fde_vms_end_prologue)
22176 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
22177 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
22179 if (fde->dw_fde_vms_begin_epilogue)
22180 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
22181 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
22183 #endif
22186 else
22188 /* Generate pubnames entries for the split function code ranges. */
22189 dw_fde_ref fde = fun->fde;
22191 if (fde->dw_fde_second_begin)
22193 if (dwarf_version >= 3 || !dwarf_strict)
22195 /* We should use ranges for non-contiguous code section
22196 addresses. Use the actual code range for the initial
22197 section, since the HOT/COLD labels might precede an
22198 alignment offset. */
22199 bool range_list_added = false;
22200 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
22201 fde->dw_fde_end, &range_list_added,
22202 false);
22203 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
22204 fde->dw_fde_second_end,
22205 &range_list_added, false);
22206 if (range_list_added)
22207 add_ranges (NULL);
22209 else
22211 /* There is no real support in DW2 for this .. so we make
22212 a work-around. First, emit the pub name for the segment
22213 containing the function label. Then make and emit a
22214 simplified subprogram DIE for the second segment with the
22215 name pre-fixed by __hot/cold_sect_of_. We use the same
22216 linkage name for the second die so that gdb will find both
22217 sections when given "b foo". */
22218 const char *name = NULL;
22219 tree decl_name = DECL_NAME (decl);
22220 dw_die_ref seg_die;
22222 /* Do the 'primary' section. */
22223 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
22224 fde->dw_fde_end, false);
22226 /* Build a minimal DIE for the secondary section. */
22227 seg_die = new_die (DW_TAG_subprogram,
22228 subr_die->die_parent, decl);
22230 if (TREE_PUBLIC (decl))
22231 add_AT_flag (seg_die, DW_AT_external, 1);
22233 if (decl_name != NULL
22234 && IDENTIFIER_POINTER (decl_name) != NULL)
22236 name = dwarf2_name (decl, 1);
22237 if (! DECL_ARTIFICIAL (decl))
22238 add_src_coords_attributes (seg_die, decl);
22240 add_linkage_name (seg_die, decl);
22242 gcc_assert (name != NULL);
22243 add_pure_or_virtual_attribute (seg_die, decl);
22244 if (DECL_ARTIFICIAL (decl))
22245 add_AT_flag (seg_die, DW_AT_artificial, 1);
22247 name = concat ("__second_sect_of_", name, NULL);
22248 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
22249 fde->dw_fde_second_end, false);
22250 add_name_attribute (seg_die, name);
22251 if (want_pubnames ())
22252 add_pubname_string (name, seg_die);
22255 else
22256 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
22257 false);
22260 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
22262 /* We define the "frame base" as the function's CFA. This is more
22263 convenient for several reasons: (1) It's stable across the prologue
22264 and epilogue, which makes it better than just a frame pointer,
22265 (2) With dwarf3, there exists a one-byte encoding that allows us
22266 to reference the .debug_frame data by proxy, but failing that,
22267 (3) We can at least reuse the code inspection and interpretation
22268 code that determines the CFA position at various points in the
22269 function. */
22270 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
22272 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
22273 add_AT_loc (subr_die, DW_AT_frame_base, op);
22275 else
22277 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
22278 if (list->dw_loc_next)
22279 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
22280 else
22281 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
22284 /* Compute a displacement from the "steady-state frame pointer" to
22285 the CFA. The former is what all stack slots and argument slots
22286 will reference in the rtl; the latter is what we've told the
22287 debugger about. We'll need to adjust all frame_base references
22288 by this displacement. */
22289 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
22291 if (fun->static_chain_decl)
22293 /* DWARF requires here a location expression that computes the
22294 address of the enclosing subprogram's frame base. The machinery
22295 in tree-nested.c is supposed to store this specific address in the
22296 last field of the FRAME record. */
22297 const tree frame_type
22298 = TREE_TYPE (TREE_TYPE (fun->static_chain_decl));
22299 const tree fb_decl = tree_last (TYPE_FIELDS (frame_type));
22301 tree fb_expr
22302 = build1 (INDIRECT_REF, frame_type, fun->static_chain_decl);
22303 fb_expr = build3 (COMPONENT_REF, TREE_TYPE (fb_decl),
22304 fb_expr, fb_decl, NULL_TREE);
22306 add_AT_location_description (subr_die, DW_AT_static_link,
22307 loc_list_from_tree (fb_expr, 0, NULL));
22310 resolve_variable_values ();
22313 /* Generate child dies for template paramaters. */
22314 if (early_dwarf && debug_info_level > DINFO_LEVEL_TERSE)
22315 gen_generic_params_dies (decl);
22317 /* Now output descriptions of the arguments for this function. This gets
22318 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
22319 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
22320 `...' at the end of the formal parameter list. In order to find out if
22321 there was a trailing ellipsis or not, we must instead look at the type
22322 associated with the FUNCTION_DECL. This will be a node of type
22323 FUNCTION_TYPE. If the chain of type nodes hanging off of this
22324 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
22325 an ellipsis at the end. */
22327 /* In the case where we are describing a mere function declaration, all we
22328 need to do here (and all we *can* do here) is to describe the *types* of
22329 its formal parameters. */
22330 if (debug_info_level <= DINFO_LEVEL_TERSE)
22332 else if (declaration)
22333 gen_formal_types_die (decl, subr_die);
22334 else
22336 /* Generate DIEs to represent all known formal parameters. */
22337 tree parm = DECL_ARGUMENTS (decl);
22338 tree generic_decl = early_dwarf
22339 ? lang_hooks.decls.get_generic_function_decl (decl) : NULL;
22340 tree generic_decl_parm = generic_decl
22341 ? DECL_ARGUMENTS (generic_decl)
22342 : NULL;
22344 /* Now we want to walk the list of parameters of the function and
22345 emit their relevant DIEs.
22347 We consider the case of DECL being an instance of a generic function
22348 as well as it being a normal function.
22350 If DECL is an instance of a generic function we walk the
22351 parameters of the generic function declaration _and_ the parameters of
22352 DECL itself. This is useful because we want to emit specific DIEs for
22353 function parameter packs and those are declared as part of the
22354 generic function declaration. In that particular case,
22355 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
22356 That DIE has children DIEs representing the set of arguments
22357 of the pack. Note that the set of pack arguments can be empty.
22358 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
22359 children DIE.
22361 Otherwise, we just consider the parameters of DECL. */
22362 while (generic_decl_parm || parm)
22364 if (generic_decl_parm
22365 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
22366 gen_formal_parameter_pack_die (generic_decl_parm,
22367 parm, subr_die,
22368 &parm);
22369 else if (parm && !POINTER_BOUNDS_P (parm))
22371 dw_die_ref parm_die = gen_decl_die (parm, NULL, NULL, subr_die);
22373 if (parm == DECL_ARGUMENTS (decl)
22374 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
22375 && parm_die
22376 && (dwarf_version >= 3 || !dwarf_strict))
22377 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
22379 parm = DECL_CHAIN (parm);
22381 else if (parm)
22382 parm = DECL_CHAIN (parm);
22384 if (generic_decl_parm)
22385 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
22388 /* Decide whether we need an unspecified_parameters DIE at the end.
22389 There are 2 more cases to do this for: 1) the ansi ... declaration -
22390 this is detectable when the end of the arg list is not a
22391 void_type_node 2) an unprototyped function declaration (not a
22392 definition). This just means that we have no info about the
22393 parameters at all. */
22394 if (early_dwarf)
22396 if (prototype_p (TREE_TYPE (decl)))
22398 /* This is the prototyped case, check for.... */
22399 if (stdarg_p (TREE_TYPE (decl)))
22400 gen_unspecified_parameters_die (decl, subr_die);
22402 else if (DECL_INITIAL (decl) == NULL_TREE)
22403 gen_unspecified_parameters_die (decl, subr_die);
22407 if (subr_die != old_die)
22408 /* Add the calling convention attribute if requested. */
22409 add_calling_convention_attribute (subr_die, decl);
22411 /* Output Dwarf info for all of the stuff within the body of the function
22412 (if it has one - it may be just a declaration).
22414 OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
22415 a function. This BLOCK actually represents the outermost binding contour
22416 for the function, i.e. the contour in which the function's formal
22417 parameters and labels get declared. Curiously, it appears that the front
22418 end doesn't actually put the PARM_DECL nodes for the current function onto
22419 the BLOCK_VARS list for this outer scope, but are strung off of the
22420 DECL_ARGUMENTS list for the function instead.
22422 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
22423 the LABEL_DECL nodes for the function however, and we output DWARF info
22424 for those in decls_for_scope. Just within the `outer_scope' there will be
22425 a BLOCK node representing the function's outermost pair of curly braces,
22426 and any blocks used for the base and member initializers of a C++
22427 constructor function. */
22428 tree outer_scope = DECL_INITIAL (decl);
22429 if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
22431 int call_site_note_count = 0;
22432 int tail_call_site_note_count = 0;
22434 /* Emit a DW_TAG_variable DIE for a named return value. */
22435 if (DECL_NAME (DECL_RESULT (decl)))
22436 gen_decl_die (DECL_RESULT (decl), NULL, NULL, subr_die);
22438 /* The first time through decls_for_scope we will generate the
22439 DIEs for the locals. The second time, we fill in the
22440 location info. */
22441 decls_for_scope (outer_scope, subr_die);
22443 if (call_arg_locations && (!dwarf_strict || dwarf_version >= 5))
22445 struct call_arg_loc_node *ca_loc;
22446 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
22448 dw_die_ref die = NULL;
22449 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
22450 rtx arg, next_arg;
22452 for (arg = (ca_loc->call_arg_loc_note != NULL_RTX
22453 ? NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note)
22454 : NULL_RTX);
22455 arg; arg = next_arg)
22457 dw_loc_descr_ref reg, val;
22458 machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
22459 dw_die_ref cdie, tdie = NULL;
22461 next_arg = XEXP (arg, 1);
22462 if (REG_P (XEXP (XEXP (arg, 0), 0))
22463 && next_arg
22464 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
22465 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
22466 && REGNO (XEXP (XEXP (arg, 0), 0))
22467 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
22468 next_arg = XEXP (next_arg, 1);
22469 if (mode == VOIDmode)
22471 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
22472 if (mode == VOIDmode)
22473 mode = GET_MODE (XEXP (arg, 0));
22475 if (mode == VOIDmode || mode == BLKmode)
22476 continue;
22477 /* Get dynamic information about call target only if we
22478 have no static information: we cannot generate both
22479 DW_AT_call_origin and DW_AT_call_target
22480 attributes. */
22481 if (ca_loc->symbol_ref == NULL_RTX)
22483 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
22485 tloc = XEXP (XEXP (arg, 0), 1);
22486 continue;
22488 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
22489 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
22491 tlocc = XEXP (XEXP (arg, 0), 1);
22492 continue;
22495 reg = NULL;
22496 if (REG_P (XEXP (XEXP (arg, 0), 0)))
22497 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
22498 VAR_INIT_STATUS_INITIALIZED);
22499 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
22501 rtx mem = XEXP (XEXP (arg, 0), 0);
22502 reg = mem_loc_descriptor (XEXP (mem, 0),
22503 get_address_mode (mem),
22504 GET_MODE (mem),
22505 VAR_INIT_STATUS_INITIALIZED);
22507 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
22508 == DEBUG_PARAMETER_REF)
22510 tree tdecl
22511 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
22512 tdie = lookup_decl_die (tdecl);
22513 if (tdie == NULL)
22514 continue;
22516 else
22517 continue;
22518 if (reg == NULL
22519 && GET_CODE (XEXP (XEXP (arg, 0), 0))
22520 != DEBUG_PARAMETER_REF)
22521 continue;
22522 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
22523 VOIDmode,
22524 VAR_INIT_STATUS_INITIALIZED);
22525 if (val == NULL)
22526 continue;
22527 if (die == NULL)
22528 die = gen_call_site_die (decl, subr_die, ca_loc);
22529 cdie = new_die (dwarf_TAG (DW_TAG_call_site_parameter), die,
22530 NULL_TREE);
22531 if (reg != NULL)
22532 add_AT_loc (cdie, DW_AT_location, reg);
22533 else if (tdie != NULL)
22534 add_AT_die_ref (cdie, dwarf_AT (DW_AT_call_parameter),
22535 tdie);
22536 add_AT_loc (cdie, dwarf_AT (DW_AT_call_value), val);
22537 if (next_arg != XEXP (arg, 1))
22539 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
22540 if (mode == VOIDmode)
22541 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
22542 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
22543 0), 1),
22544 mode, VOIDmode,
22545 VAR_INIT_STATUS_INITIALIZED);
22546 if (val != NULL)
22547 add_AT_loc (cdie, dwarf_AT (DW_AT_call_data_value),
22548 val);
22551 if (die == NULL
22552 && (ca_loc->symbol_ref || tloc))
22553 die = gen_call_site_die (decl, subr_die, ca_loc);
22554 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
22556 dw_loc_descr_ref tval = NULL;
22558 if (tloc != NULL_RTX)
22559 tval = mem_loc_descriptor (tloc,
22560 GET_MODE (tloc) == VOIDmode
22561 ? Pmode : GET_MODE (tloc),
22562 VOIDmode,
22563 VAR_INIT_STATUS_INITIALIZED);
22564 if (tval)
22565 add_AT_loc (die, dwarf_AT (DW_AT_call_target), tval);
22566 else if (tlocc != NULL_RTX)
22568 tval = mem_loc_descriptor (tlocc,
22569 GET_MODE (tlocc) == VOIDmode
22570 ? Pmode : GET_MODE (tlocc),
22571 VOIDmode,
22572 VAR_INIT_STATUS_INITIALIZED);
22573 if (tval)
22574 add_AT_loc (die,
22575 dwarf_AT (DW_AT_call_target_clobbered),
22576 tval);
22579 if (die != NULL)
22581 call_site_note_count++;
22582 if (ca_loc->tail_call_p)
22583 tail_call_site_note_count++;
22587 call_arg_locations = NULL;
22588 call_arg_loc_last = NULL;
22589 if (tail_call_site_count >= 0
22590 && tail_call_site_count == tail_call_site_note_count
22591 && (!dwarf_strict || dwarf_version >= 5))
22593 if (call_site_count >= 0
22594 && call_site_count == call_site_note_count)
22595 add_AT_flag (subr_die, dwarf_AT (DW_AT_call_all_calls), 1);
22596 else
22597 add_AT_flag (subr_die, dwarf_AT (DW_AT_call_all_tail_calls), 1);
22599 call_site_count = -1;
22600 tail_call_site_count = -1;
22603 /* Mark used types after we have created DIEs for the functions scopes. */
22604 premark_used_types (DECL_STRUCT_FUNCTION (decl));
22607 /* Returns a hash value for X (which really is a die_struct). */
22609 hashval_t
22610 block_die_hasher::hash (die_struct *d)
22612 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
22615 /* Return nonzero if decl_id and die_parent of die_struct X is the same
22616 as decl_id and die_parent of die_struct Y. */
22618 bool
22619 block_die_hasher::equal (die_struct *x, die_struct *y)
22621 return x->decl_id == y->decl_id && x->die_parent == y->die_parent;
22624 /* Return TRUE if DECL, which may have been previously generated as
22625 OLD_DIE, is a candidate for a DW_AT_specification. DECLARATION is
22626 true if decl (or its origin) is either an extern declaration or a
22627 class/namespace scoped declaration.
22629 The declare_in_namespace support causes us to get two DIEs for one
22630 variable, both of which are declarations. We want to avoid
22631 considering one to be a specification, so we must test for
22632 DECLARATION and DW_AT_declaration. */
22633 static inline bool
22634 decl_will_get_specification_p (dw_die_ref old_die, tree decl, bool declaration)
22636 return (old_die && TREE_STATIC (decl) && !declaration
22637 && get_AT_flag (old_die, DW_AT_declaration) == 1);
22640 /* Return true if DECL is a local static. */
22642 static inline bool
22643 local_function_static (tree decl)
22645 gcc_assert (VAR_P (decl));
22646 return TREE_STATIC (decl)
22647 && DECL_CONTEXT (decl)
22648 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL;
22651 /* Generate a DIE to represent a declared data object.
22652 Either DECL or ORIGIN must be non-null. */
22654 static void
22655 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
22657 HOST_WIDE_INT off = 0;
22658 tree com_decl;
22659 tree decl_or_origin = decl ? decl : origin;
22660 tree ultimate_origin;
22661 dw_die_ref var_die;
22662 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
22663 bool declaration = (DECL_EXTERNAL (decl_or_origin)
22664 || class_or_namespace_scope_p (context_die));
22665 bool specialization_p = false;
22666 bool no_linkage_name = false;
22668 /* While C++ inline static data members have definitions inside of the
22669 class, force the first DIE to be a declaration, then let gen_member_die
22670 reparent it to the class context and call gen_variable_die again
22671 to create the outside of the class DIE for the definition. */
22672 if (!declaration
22673 && old_die == NULL
22674 && decl
22675 && DECL_CONTEXT (decl)
22676 && TYPE_P (DECL_CONTEXT (decl))
22677 && lang_hooks.decls.decl_dwarf_attribute (decl, DW_AT_inline) != -1)
22679 declaration = true;
22680 if (dwarf_version < 5)
22681 no_linkage_name = true;
22684 ultimate_origin = decl_ultimate_origin (decl_or_origin);
22685 if (decl || ultimate_origin)
22686 origin = ultimate_origin;
22687 com_decl = fortran_common (decl_or_origin, &off);
22689 /* Symbol in common gets emitted as a child of the common block, in the form
22690 of a data member. */
22691 if (com_decl)
22693 dw_die_ref com_die;
22694 dw_loc_list_ref loc = NULL;
22695 die_node com_die_arg;
22697 var_die = lookup_decl_die (decl_or_origin);
22698 if (var_die)
22700 if (! early_dwarf && get_AT (var_die, DW_AT_location) == NULL)
22702 loc = loc_list_from_tree (com_decl, off ? 1 : 2, NULL);
22703 if (loc)
22705 if (off)
22707 /* Optimize the common case. */
22708 if (single_element_loc_list_p (loc)
22709 && loc->expr->dw_loc_opc == DW_OP_addr
22710 && loc->expr->dw_loc_next == NULL
22711 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
22712 == SYMBOL_REF)
22714 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
22715 loc->expr->dw_loc_oprnd1.v.val_addr
22716 = plus_constant (GET_MODE (x), x , off);
22718 else
22719 loc_list_plus_const (loc, off);
22721 add_AT_location_description (var_die, DW_AT_location, loc);
22722 remove_AT (var_die, DW_AT_declaration);
22725 return;
22728 if (common_block_die_table == NULL)
22729 common_block_die_table = hash_table<block_die_hasher>::create_ggc (10);
22731 com_die_arg.decl_id = DECL_UID (com_decl);
22732 com_die_arg.die_parent = context_die;
22733 com_die = common_block_die_table->find (&com_die_arg);
22734 if (! early_dwarf)
22735 loc = loc_list_from_tree (com_decl, 2, NULL);
22736 if (com_die == NULL)
22738 const char *cnam
22739 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
22740 die_node **slot;
22742 com_die = new_die (DW_TAG_common_block, context_die, decl);
22743 add_name_and_src_coords_attributes (com_die, com_decl);
22744 if (loc)
22746 add_AT_location_description (com_die, DW_AT_location, loc);
22747 /* Avoid sharing the same loc descriptor between
22748 DW_TAG_common_block and DW_TAG_variable. */
22749 loc = loc_list_from_tree (com_decl, 2, NULL);
22751 else if (DECL_EXTERNAL (decl_or_origin))
22752 add_AT_flag (com_die, DW_AT_declaration, 1);
22753 if (want_pubnames ())
22754 add_pubname_string (cnam, com_die); /* ??? needed? */
22755 com_die->decl_id = DECL_UID (com_decl);
22756 slot = common_block_die_table->find_slot (com_die, INSERT);
22757 *slot = com_die;
22759 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
22761 add_AT_location_description (com_die, DW_AT_location, loc);
22762 loc = loc_list_from_tree (com_decl, 2, NULL);
22763 remove_AT (com_die, DW_AT_declaration);
22765 var_die = new_die (DW_TAG_variable, com_die, decl);
22766 add_name_and_src_coords_attributes (var_die, decl_or_origin);
22767 add_type_attribute (var_die, TREE_TYPE (decl_or_origin),
22768 decl_quals (decl_or_origin), false,
22769 context_die);
22770 add_alignment_attribute (var_die, decl);
22771 add_AT_flag (var_die, DW_AT_external, 1);
22772 if (loc)
22774 if (off)
22776 /* Optimize the common case. */
22777 if (single_element_loc_list_p (loc)
22778 && loc->expr->dw_loc_opc == DW_OP_addr
22779 && loc->expr->dw_loc_next == NULL
22780 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
22782 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
22783 loc->expr->dw_loc_oprnd1.v.val_addr
22784 = plus_constant (GET_MODE (x), x, off);
22786 else
22787 loc_list_plus_const (loc, off);
22789 add_AT_location_description (var_die, DW_AT_location, loc);
22791 else if (DECL_EXTERNAL (decl_or_origin))
22792 add_AT_flag (var_die, DW_AT_declaration, 1);
22793 if (decl)
22794 equate_decl_number_to_die (decl, var_die);
22795 return;
22798 if (old_die)
22800 if (declaration)
22802 /* A declaration that has been previously dumped, needs no
22803 further annotations, since it doesn't need location on
22804 the second pass. */
22805 return;
22807 else if (decl_will_get_specification_p (old_die, decl, declaration)
22808 && !get_AT (old_die, DW_AT_specification))
22810 /* Fall-thru so we can make a new variable die along with a
22811 DW_AT_specification. */
22813 else if (origin && old_die->die_parent != context_die)
22815 /* If we will be creating an inlined instance, we need a
22816 new DIE that will get annotated with
22817 DW_AT_abstract_origin. Clear things so we can get a
22818 new DIE. */
22819 gcc_assert (!DECL_ABSTRACT_P (decl));
22820 old_die = NULL;
22822 else
22824 /* If a DIE was dumped early, it still needs location info.
22825 Skip to where we fill the location bits. */
22826 var_die = old_die;
22827 goto gen_variable_die_location;
22831 /* For static data members, the declaration in the class is supposed
22832 to have DW_TAG_member tag in DWARF{3,4} and we emit it for compatibility
22833 also in DWARF2; the specification should still be DW_TAG_variable
22834 referencing the DW_TAG_member DIE. */
22835 if (declaration && class_scope_p (context_die) && dwarf_version < 5)
22836 var_die = new_die (DW_TAG_member, context_die, decl);
22837 else
22838 var_die = new_die (DW_TAG_variable, context_die, decl);
22840 if (origin != NULL)
22841 add_abstract_origin_attribute (var_die, origin);
22843 /* Loop unrolling can create multiple blocks that refer to the same
22844 static variable, so we must test for the DW_AT_declaration flag.
22846 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
22847 copy decls and set the DECL_ABSTRACT_P flag on them instead of
22848 sharing them.
22850 ??? Duplicated blocks have been rewritten to use .debug_ranges. */
22851 else if (decl_will_get_specification_p (old_die, decl, declaration))
22853 /* This is a definition of a C++ class level static. */
22854 add_AT_specification (var_die, old_die);
22855 specialization_p = true;
22856 if (DECL_NAME (decl))
22858 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
22859 struct dwarf_file_data * file_index = lookup_filename (s.file);
22861 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
22862 add_AT_file (var_die, DW_AT_decl_file, file_index);
22864 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
22865 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
22867 if (debug_column_info
22868 && s.column
22869 && (get_AT_unsigned (old_die, DW_AT_decl_column)
22870 != (unsigned) s.column))
22871 add_AT_unsigned (var_die, DW_AT_decl_column, s.column);
22873 if (old_die->die_tag == DW_TAG_member)
22874 add_linkage_name (var_die, decl);
22877 else
22878 add_name_and_src_coords_attributes (var_die, decl, no_linkage_name);
22880 if ((origin == NULL && !specialization_p)
22881 || (origin != NULL
22882 && !DECL_ABSTRACT_P (decl_or_origin)
22883 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
22884 decl_function_context
22885 (decl_or_origin))))
22887 tree type = TREE_TYPE (decl_or_origin);
22889 if (decl_by_reference_p (decl_or_origin))
22890 add_type_attribute (var_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
22891 context_die);
22892 else
22893 add_type_attribute (var_die, type, decl_quals (decl_or_origin), false,
22894 context_die);
22897 if (origin == NULL && !specialization_p)
22899 if (TREE_PUBLIC (decl))
22900 add_AT_flag (var_die, DW_AT_external, 1);
22902 if (DECL_ARTIFICIAL (decl))
22903 add_AT_flag (var_die, DW_AT_artificial, 1);
22905 add_alignment_attribute (var_die, decl);
22907 add_accessibility_attribute (var_die, decl);
22910 if (declaration)
22911 add_AT_flag (var_die, DW_AT_declaration, 1);
22913 if (decl && (DECL_ABSTRACT_P (decl)
22914 || !old_die || is_declaration_die (old_die)))
22915 equate_decl_number_to_die (decl, var_die);
22917 gen_variable_die_location:
22918 if (! declaration
22919 && (! DECL_ABSTRACT_P (decl_or_origin)
22920 /* Local static vars are shared between all clones/inlines,
22921 so emit DW_AT_location on the abstract DIE if DECL_RTL is
22922 already set. */
22923 || (VAR_P (decl_or_origin)
22924 && TREE_STATIC (decl_or_origin)
22925 && DECL_RTL_SET_P (decl_or_origin))))
22927 if (early_dwarf)
22928 add_pubname (decl_or_origin, var_die);
22929 else
22930 add_location_or_const_value_attribute (var_die, decl_or_origin,
22931 decl == NULL);
22933 else
22934 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
22936 if ((dwarf_version >= 4 || !dwarf_strict)
22937 && lang_hooks.decls.decl_dwarf_attribute (decl_or_origin,
22938 DW_AT_const_expr) == 1
22939 && !get_AT (var_die, DW_AT_const_expr)
22940 && !specialization_p)
22941 add_AT_flag (var_die, DW_AT_const_expr, 1);
22943 if (!dwarf_strict)
22945 int inl = lang_hooks.decls.decl_dwarf_attribute (decl_or_origin,
22946 DW_AT_inline);
22947 if (inl != -1
22948 && !get_AT (var_die, DW_AT_inline)
22949 && !specialization_p)
22950 add_AT_unsigned (var_die, DW_AT_inline, inl);
22954 /* Generate a DIE to represent a named constant. */
22956 static void
22957 gen_const_die (tree decl, dw_die_ref context_die)
22959 dw_die_ref const_die;
22960 tree type = TREE_TYPE (decl);
22962 const_die = lookup_decl_die (decl);
22963 if (const_die)
22964 return;
22966 const_die = new_die (DW_TAG_constant, context_die, decl);
22967 equate_decl_number_to_die (decl, const_die);
22968 add_name_and_src_coords_attributes (const_die, decl);
22969 add_type_attribute (const_die, type, TYPE_QUAL_CONST, false, context_die);
22970 if (TREE_PUBLIC (decl))
22971 add_AT_flag (const_die, DW_AT_external, 1);
22972 if (DECL_ARTIFICIAL (decl))
22973 add_AT_flag (const_die, DW_AT_artificial, 1);
22974 tree_add_const_value_attribute_for_decl (const_die, decl);
22977 /* Generate a DIE to represent a label identifier. */
22979 static void
22980 gen_label_die (tree decl, dw_die_ref context_die)
22982 tree origin = decl_ultimate_origin (decl);
22983 dw_die_ref lbl_die = lookup_decl_die (decl);
22984 rtx insn;
22985 char label[MAX_ARTIFICIAL_LABEL_BYTES];
22987 if (!lbl_die)
22989 lbl_die = new_die (DW_TAG_label, context_die, decl);
22990 equate_decl_number_to_die (decl, lbl_die);
22992 if (origin != NULL)
22993 add_abstract_origin_attribute (lbl_die, origin);
22994 else
22995 add_name_and_src_coords_attributes (lbl_die, decl);
22998 if (DECL_ABSTRACT_P (decl))
22999 equate_decl_number_to_die (decl, lbl_die);
23000 else if (! early_dwarf)
23002 insn = DECL_RTL_IF_SET (decl);
23004 /* Deleted labels are programmer specified labels which have been
23005 eliminated because of various optimizations. We still emit them
23006 here so that it is possible to put breakpoints on them. */
23007 if (insn
23008 && (LABEL_P (insn)
23009 || ((NOTE_P (insn)
23010 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
23012 /* When optimization is enabled (via -O) some parts of the compiler
23013 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
23014 represent source-level labels which were explicitly declared by
23015 the user. This really shouldn't be happening though, so catch
23016 it if it ever does happen. */
23017 gcc_assert (!as_a<rtx_insn *> (insn)->deleted ());
23019 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
23020 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
23022 else if (insn
23023 && NOTE_P (insn)
23024 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
23025 && CODE_LABEL_NUMBER (insn) != -1)
23027 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
23028 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
23033 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
23034 attributes to the DIE for a block STMT, to describe where the inlined
23035 function was called from. This is similar to add_src_coords_attributes. */
23037 static inline void
23038 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
23040 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
23042 if (dwarf_version >= 3 || !dwarf_strict)
23044 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
23045 add_AT_unsigned (die, DW_AT_call_line, s.line);
23046 if (debug_column_info && s.column)
23047 add_AT_unsigned (die, DW_AT_call_column, s.column);
23052 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
23053 Add low_pc and high_pc attributes to the DIE for a block STMT. */
23055 static inline void
23056 add_high_low_attributes (tree stmt, dw_die_ref die)
23058 char label[MAX_ARTIFICIAL_LABEL_BYTES];
23060 if (BLOCK_FRAGMENT_CHAIN (stmt)
23061 && (dwarf_version >= 3 || !dwarf_strict))
23063 tree chain, superblock = NULL_TREE;
23064 dw_die_ref pdie;
23065 dw_attr_node *attr = NULL;
23067 if (inlined_function_outer_scope_p (stmt))
23069 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
23070 BLOCK_NUMBER (stmt));
23071 add_AT_lbl_id (die, DW_AT_entry_pc, label);
23074 /* Optimize duplicate .debug_ranges lists or even tails of
23075 lists. If this BLOCK has same ranges as its supercontext,
23076 lookup DW_AT_ranges attribute in the supercontext (and
23077 recursively so), verify that the ranges_table contains the
23078 right values and use it instead of adding a new .debug_range. */
23079 for (chain = stmt, pdie = die;
23080 BLOCK_SAME_RANGE (chain);
23081 chain = BLOCK_SUPERCONTEXT (chain))
23083 dw_attr_node *new_attr;
23085 pdie = pdie->die_parent;
23086 if (pdie == NULL)
23087 break;
23088 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
23089 break;
23090 new_attr = get_AT (pdie, DW_AT_ranges);
23091 if (new_attr == NULL
23092 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
23093 break;
23094 attr = new_attr;
23095 superblock = BLOCK_SUPERCONTEXT (chain);
23097 if (attr != NULL
23098 && ((*ranges_table)[attr->dw_attr_val.v.val_offset].num
23099 == BLOCK_NUMBER (superblock))
23100 && BLOCK_FRAGMENT_CHAIN (superblock))
23102 unsigned long off = attr->dw_attr_val.v.val_offset;
23103 unsigned long supercnt = 0, thiscnt = 0;
23104 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
23105 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
23107 ++supercnt;
23108 gcc_checking_assert ((*ranges_table)[off + supercnt].num
23109 == BLOCK_NUMBER (chain));
23111 gcc_checking_assert ((*ranges_table)[off + supercnt + 1].num == 0);
23112 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
23113 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
23114 ++thiscnt;
23115 gcc_assert (supercnt >= thiscnt);
23116 add_AT_range_list (die, DW_AT_ranges, off + supercnt - thiscnt,
23117 false);
23118 note_rnglist_head (off + supercnt - thiscnt);
23119 return;
23122 unsigned int offset = add_ranges (stmt, true);
23123 add_AT_range_list (die, DW_AT_ranges, offset, false);
23124 note_rnglist_head (offset);
23126 bool prev_in_cold = BLOCK_IN_COLD_SECTION_P (stmt);
23127 chain = BLOCK_FRAGMENT_CHAIN (stmt);
23130 add_ranges (chain, prev_in_cold != BLOCK_IN_COLD_SECTION_P (chain));
23131 prev_in_cold = BLOCK_IN_COLD_SECTION_P (chain);
23132 chain = BLOCK_FRAGMENT_CHAIN (chain);
23134 while (chain);
23135 add_ranges (NULL);
23137 else
23139 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
23140 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
23141 BLOCK_NUMBER (stmt));
23142 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
23143 BLOCK_NUMBER (stmt));
23144 add_AT_low_high_pc (die, label, label_high, false);
23148 /* Generate a DIE for a lexical block. */
23150 static void
23151 gen_lexical_block_die (tree stmt, dw_die_ref context_die)
23153 dw_die_ref old_die = BLOCK_DIE (stmt);
23154 dw_die_ref stmt_die = NULL;
23155 if (!old_die)
23157 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
23158 BLOCK_DIE (stmt) = stmt_die;
23161 if (BLOCK_ABSTRACT (stmt))
23163 if (old_die)
23165 /* This must have been generated early and it won't even
23166 need location information since it's a DW_AT_inline
23167 function. */
23168 if (flag_checking)
23169 for (dw_die_ref c = context_die; c; c = c->die_parent)
23170 if (c->die_tag == DW_TAG_inlined_subroutine
23171 || c->die_tag == DW_TAG_subprogram)
23173 gcc_assert (get_AT (c, DW_AT_inline));
23174 break;
23176 return;
23179 else if (BLOCK_ABSTRACT_ORIGIN (stmt))
23181 /* If this is an inlined instance, create a new lexical die for
23182 anything below to attach DW_AT_abstract_origin to. */
23183 if (old_die)
23185 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
23186 BLOCK_DIE (stmt) = stmt_die;
23187 old_die = NULL;
23190 tree origin = block_ultimate_origin (stmt);
23191 if (origin != NULL_TREE && origin != stmt)
23192 add_abstract_origin_attribute (stmt_die, origin);
23195 if (old_die)
23196 stmt_die = old_die;
23198 /* A non abstract block whose blocks have already been reordered
23199 should have the instruction range for this block. If so, set the
23200 high/low attributes. */
23201 if (!early_dwarf && !BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
23203 gcc_assert (stmt_die);
23204 add_high_low_attributes (stmt, stmt_die);
23207 decls_for_scope (stmt, stmt_die);
23210 /* Generate a DIE for an inlined subprogram. */
23212 static void
23213 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die)
23215 tree decl;
23217 /* The instance of function that is effectively being inlined shall not
23218 be abstract. */
23219 gcc_assert (! BLOCK_ABSTRACT (stmt));
23221 decl = block_ultimate_origin (stmt);
23223 /* Make sure any inlined functions are known to be inlineable. */
23224 gcc_checking_assert (DECL_ABSTRACT_P (decl)
23225 || cgraph_function_possibly_inlined_p (decl));
23227 /* Emit info for the abstract instance first, if we haven't yet. We
23228 must emit this even if the block is abstract, otherwise when we
23229 emit the block below (or elsewhere), we may end up trying to emit
23230 a die whose origin die hasn't been emitted, and crashing. */
23231 dwarf2out_abstract_function (decl);
23233 if (! BLOCK_ABSTRACT (stmt))
23235 dw_die_ref subr_die
23236 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
23238 if (call_arg_locations)
23239 BLOCK_DIE (stmt) = subr_die;
23240 add_abstract_origin_attribute (subr_die, decl);
23241 if (TREE_ASM_WRITTEN (stmt))
23242 add_high_low_attributes (stmt, subr_die);
23243 add_call_src_coords_attributes (stmt, subr_die);
23245 decls_for_scope (stmt, subr_die);
23249 /* Generate a DIE for a field in a record, or structure. CTX is required: see
23250 the comment for VLR_CONTEXT. */
23252 static void
23253 gen_field_die (tree decl, struct vlr_context *ctx, dw_die_ref context_die)
23255 dw_die_ref decl_die;
23257 if (TREE_TYPE (decl) == error_mark_node)
23258 return;
23260 decl_die = new_die (DW_TAG_member, context_die, decl);
23261 add_name_and_src_coords_attributes (decl_die, decl);
23262 add_type_attribute (decl_die, member_declared_type (decl), decl_quals (decl),
23263 TYPE_REVERSE_STORAGE_ORDER (DECL_FIELD_CONTEXT (decl)),
23264 context_die);
23266 if (DECL_BIT_FIELD_TYPE (decl))
23268 add_byte_size_attribute (decl_die, decl);
23269 add_bit_size_attribute (decl_die, decl);
23270 add_bit_offset_attribute (decl_die, decl, ctx);
23273 add_alignment_attribute (decl_die, decl);
23275 /* If we have a variant part offset, then we are supposed to process a member
23276 of a QUAL_UNION_TYPE, which is how we represent variant parts in
23277 trees. */
23278 gcc_assert (ctx->variant_part_offset == NULL_TREE
23279 || TREE_CODE (DECL_FIELD_CONTEXT (decl)) != QUAL_UNION_TYPE);
23280 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
23281 add_data_member_location_attribute (decl_die, decl, ctx);
23283 if (DECL_ARTIFICIAL (decl))
23284 add_AT_flag (decl_die, DW_AT_artificial, 1);
23286 add_accessibility_attribute (decl_die, decl);
23288 /* Equate decl number to die, so that we can look up this decl later on. */
23289 equate_decl_number_to_die (decl, decl_die);
23292 #if 0
23293 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
23294 Use modified_type_die instead.
23295 We keep this code here just in case these types of DIEs may be needed to
23296 represent certain things in other languages (e.g. Pascal) someday. */
23298 static void
23299 gen_pointer_type_die (tree type, dw_die_ref context_die)
23301 dw_die_ref ptr_die
23302 = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
23304 equate_type_number_to_die (type, ptr_die);
23305 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
23306 context_die);
23307 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
23310 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
23311 Use modified_type_die instead.
23312 We keep this code here just in case these types of DIEs may be needed to
23313 represent certain things in other languages (e.g. Pascal) someday. */
23315 static void
23316 gen_reference_type_die (tree type, dw_die_ref context_die)
23318 dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
23320 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
23321 ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
23322 else
23323 ref_die = new_die (DW_TAG_reference_type, scope_die, type);
23325 equate_type_number_to_die (type, ref_die);
23326 add_type_attribute (ref_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
23327 context_die);
23328 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
23330 #endif
23332 /* Generate a DIE for a pointer to a member type. TYPE can be an
23333 OFFSET_TYPE, for a pointer to data member, or a RECORD_TYPE, for a
23334 pointer to member function. */
23336 static void
23337 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
23339 if (lookup_type_die (type))
23340 return;
23342 dw_die_ref ptr_die = new_die (DW_TAG_ptr_to_member_type,
23343 scope_die_for (type, context_die), type);
23345 equate_type_number_to_die (type, ptr_die);
23346 add_AT_die_ref (ptr_die, DW_AT_containing_type,
23347 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
23348 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
23349 context_die);
23350 add_alignment_attribute (ptr_die, type);
23352 if (TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE
23353 && TREE_CODE (TREE_TYPE (type)) != METHOD_TYPE)
23355 dw_loc_descr_ref op = new_loc_descr (DW_OP_plus, 0, 0);
23356 add_AT_loc (ptr_die, DW_AT_use_location, op);
23360 static char *producer_string;
23362 /* Return a heap allocated producer string including command line options
23363 if -grecord-gcc-switches. */
23365 static char *
23366 gen_producer_string (void)
23368 size_t j;
23369 auto_vec<const char *> switches;
23370 const char *language_string = lang_hooks.name;
23371 char *producer, *tail;
23372 const char *p;
23373 size_t len = dwarf_record_gcc_switches ? 0 : 3;
23374 size_t plen = strlen (language_string) + 1 + strlen (version_string);
23376 for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
23377 switch (save_decoded_options[j].opt_index)
23379 case OPT_o:
23380 case OPT_d:
23381 case OPT_dumpbase:
23382 case OPT_dumpdir:
23383 case OPT_auxbase:
23384 case OPT_auxbase_strip:
23385 case OPT_quiet:
23386 case OPT_version:
23387 case OPT_v:
23388 case OPT_w:
23389 case OPT_L:
23390 case OPT_D:
23391 case OPT_I:
23392 case OPT_U:
23393 case OPT_SPECIAL_unknown:
23394 case OPT_SPECIAL_ignore:
23395 case OPT_SPECIAL_program_name:
23396 case OPT_SPECIAL_input_file:
23397 case OPT_grecord_gcc_switches:
23398 case OPT_gno_record_gcc_switches:
23399 case OPT__output_pch_:
23400 case OPT_fdiagnostics_show_location_:
23401 case OPT_fdiagnostics_show_option:
23402 case OPT_fdiagnostics_show_caret:
23403 case OPT_fdiagnostics_color_:
23404 case OPT_fverbose_asm:
23405 case OPT____:
23406 case OPT__sysroot_:
23407 case OPT_nostdinc:
23408 case OPT_nostdinc__:
23409 case OPT_fpreprocessed:
23410 case OPT_fltrans_output_list_:
23411 case OPT_fresolution_:
23412 case OPT_fdebug_prefix_map_:
23413 /* Ignore these. */
23414 continue;
23415 default:
23416 if (cl_options[save_decoded_options[j].opt_index].flags
23417 & CL_NO_DWARF_RECORD)
23418 continue;
23419 gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
23420 == '-');
23421 switch (save_decoded_options[j].canonical_option[0][1])
23423 case 'M':
23424 case 'i':
23425 case 'W':
23426 continue;
23427 case 'f':
23428 if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
23429 "dump", 4) == 0)
23430 continue;
23431 break;
23432 default:
23433 break;
23435 switches.safe_push (save_decoded_options[j].orig_option_with_args_text);
23436 len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
23437 break;
23440 producer = XNEWVEC (char, plen + 1 + len + 1);
23441 tail = producer;
23442 sprintf (tail, "%s %s", language_string, version_string);
23443 tail += plen;
23445 FOR_EACH_VEC_ELT (switches, j, p)
23447 len = strlen (p);
23448 *tail = ' ';
23449 memcpy (tail + 1, p, len);
23450 tail += len + 1;
23453 *tail = '\0';
23454 return producer;
23457 /* Given a C and/or C++ language/version string return the "highest".
23458 C++ is assumed to be "higher" than C in this case. Used for merging
23459 LTO translation unit languages. */
23460 static const char *
23461 highest_c_language (const char *lang1, const char *lang2)
23463 if (strcmp ("GNU C++14", lang1) == 0 || strcmp ("GNU C++14", lang2) == 0)
23464 return "GNU C++14";
23465 if (strcmp ("GNU C++11", lang1) == 0 || strcmp ("GNU C++11", lang2) == 0)
23466 return "GNU C++11";
23467 if (strcmp ("GNU C++98", lang1) == 0 || strcmp ("GNU C++98", lang2) == 0)
23468 return "GNU C++98";
23470 if (strcmp ("GNU C11", lang1) == 0 || strcmp ("GNU C11", lang2) == 0)
23471 return "GNU C11";
23472 if (strcmp ("GNU C99", lang1) == 0 || strcmp ("GNU C99", lang2) == 0)
23473 return "GNU C99";
23474 if (strcmp ("GNU C89", lang1) == 0 || strcmp ("GNU C89", lang2) == 0)
23475 return "GNU C89";
23477 gcc_unreachable ();
23481 /* Generate the DIE for the compilation unit. */
23483 static dw_die_ref
23484 gen_compile_unit_die (const char *filename)
23486 dw_die_ref die;
23487 const char *language_string = lang_hooks.name;
23488 int language;
23490 die = new_die (DW_TAG_compile_unit, NULL, NULL);
23492 if (filename)
23494 add_name_attribute (die, filename);
23495 /* Don't add cwd for <built-in>. */
23496 if (filename[0] != '<')
23497 add_comp_dir_attribute (die);
23500 add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
23502 /* If our producer is LTO try to figure out a common language to use
23503 from the global list of translation units. */
23504 if (strcmp (language_string, "GNU GIMPLE") == 0)
23506 unsigned i;
23507 tree t;
23508 const char *common_lang = NULL;
23510 FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
23512 if (!TRANSLATION_UNIT_LANGUAGE (t))
23513 continue;
23514 if (!common_lang)
23515 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
23516 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
23518 else if (strncmp (common_lang, "GNU C", 5) == 0
23519 && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
23520 /* Mixing C and C++ is ok, use C++ in that case. */
23521 common_lang = highest_c_language (common_lang,
23522 TRANSLATION_UNIT_LANGUAGE (t));
23523 else
23525 /* Fall back to C. */
23526 common_lang = NULL;
23527 break;
23531 if (common_lang)
23532 language_string = common_lang;
23535 language = DW_LANG_C;
23536 if (strncmp (language_string, "GNU C", 5) == 0
23537 && ISDIGIT (language_string[5]))
23539 language = DW_LANG_C89;
23540 if (dwarf_version >= 3 || !dwarf_strict)
23542 if (strcmp (language_string, "GNU C89") != 0)
23543 language = DW_LANG_C99;
23545 if (dwarf_version >= 5 /* || !dwarf_strict */)
23546 if (strcmp (language_string, "GNU C11") == 0)
23547 language = DW_LANG_C11;
23550 else if (strncmp (language_string, "GNU C++", 7) == 0)
23552 language = DW_LANG_C_plus_plus;
23553 if (dwarf_version >= 5 /* || !dwarf_strict */)
23555 if (strcmp (language_string, "GNU C++11") == 0)
23556 language = DW_LANG_C_plus_plus_11;
23557 else if (strcmp (language_string, "GNU C++14") == 0)
23558 language = DW_LANG_C_plus_plus_14;
23561 else if (strcmp (language_string, "GNU F77") == 0)
23562 language = DW_LANG_Fortran77;
23563 else if (strcmp (language_string, "GNU Pascal") == 0)
23564 language = DW_LANG_Pascal83;
23565 else if (dwarf_version >= 3 || !dwarf_strict)
23567 if (strcmp (language_string, "GNU Ada") == 0)
23568 language = DW_LANG_Ada95;
23569 else if (strncmp (language_string, "GNU Fortran", 11) == 0)
23571 language = DW_LANG_Fortran95;
23572 if (dwarf_version >= 5 /* || !dwarf_strict */)
23574 if (strcmp (language_string, "GNU Fortran2003") == 0)
23575 language = DW_LANG_Fortran03;
23576 else if (strcmp (language_string, "GNU Fortran2008") == 0)
23577 language = DW_LANG_Fortran08;
23580 else if (strcmp (language_string, "GNU Java") == 0)
23581 language = DW_LANG_Java;
23582 else if (strcmp (language_string, "GNU Objective-C") == 0)
23583 language = DW_LANG_ObjC;
23584 else if (strcmp (language_string, "GNU Objective-C++") == 0)
23585 language = DW_LANG_ObjC_plus_plus;
23586 else if (dwarf_version >= 5 || !dwarf_strict)
23588 if (strcmp (language_string, "GNU Go") == 0)
23589 language = DW_LANG_Go;
23592 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
23593 else if (strncmp (language_string, "GNU Fortran", 11) == 0)
23594 language = DW_LANG_Fortran90;
23596 add_AT_unsigned (die, DW_AT_language, language);
23598 switch (language)
23600 case DW_LANG_Fortran77:
23601 case DW_LANG_Fortran90:
23602 case DW_LANG_Fortran95:
23603 case DW_LANG_Fortran03:
23604 case DW_LANG_Fortran08:
23605 /* Fortran has case insensitive identifiers and the front-end
23606 lowercases everything. */
23607 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
23608 break;
23609 default:
23610 /* The default DW_ID_case_sensitive doesn't need to be specified. */
23611 break;
23613 return die;
23616 /* Generate the DIE for a base class. */
23618 static void
23619 gen_inheritance_die (tree binfo, tree access, tree type,
23620 dw_die_ref context_die)
23622 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
23623 struct vlr_context ctx = { type, NULL };
23625 add_type_attribute (die, BINFO_TYPE (binfo), TYPE_UNQUALIFIED, false,
23626 context_die);
23627 add_data_member_location_attribute (die, binfo, &ctx);
23629 if (BINFO_VIRTUAL_P (binfo))
23630 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
23632 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
23633 children, otherwise the default is DW_ACCESS_public. In DWARF2
23634 the default has always been DW_ACCESS_private. */
23635 if (access == access_public_node)
23637 if (dwarf_version == 2
23638 || context_die->die_tag == DW_TAG_class_type)
23639 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
23641 else if (access == access_protected_node)
23642 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
23643 else if (dwarf_version > 2
23644 && context_die->die_tag != DW_TAG_class_type)
23645 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
23648 /* Return whether DECL is a FIELD_DECL that represents the variant part of a
23649 structure. */
23650 static bool
23651 is_variant_part (tree decl)
23653 return (TREE_CODE (decl) == FIELD_DECL
23654 && TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE);
23657 /* Check that OPERAND is a reference to a field in STRUCT_TYPE. If it is,
23658 return the FIELD_DECL. Return NULL_TREE otherwise. */
23660 static tree
23661 analyze_discr_in_predicate (tree operand, tree struct_type)
23663 bool continue_stripping = true;
23664 while (continue_stripping)
23665 switch (TREE_CODE (operand))
23667 CASE_CONVERT:
23668 operand = TREE_OPERAND (operand, 0);
23669 break;
23670 default:
23671 continue_stripping = false;
23672 break;
23675 /* Match field access to members of struct_type only. */
23676 if (TREE_CODE (operand) == COMPONENT_REF
23677 && TREE_CODE (TREE_OPERAND (operand, 0)) == PLACEHOLDER_EXPR
23678 && TREE_TYPE (TREE_OPERAND (operand, 0)) == struct_type
23679 && TREE_CODE (TREE_OPERAND (operand, 1)) == FIELD_DECL)
23680 return TREE_OPERAND (operand, 1);
23681 else
23682 return NULL_TREE;
23685 /* Check that SRC is a constant integer that can be represented as a native
23686 integer constant (either signed or unsigned). If so, store it into DEST and
23687 return true. Return false otherwise. */
23689 static bool
23690 get_discr_value (tree src, dw_discr_value *dest)
23692 bool is_unsigned = TYPE_UNSIGNED (TREE_TYPE (src));
23694 if (TREE_CODE (src) != INTEGER_CST
23695 || !(is_unsigned ? tree_fits_uhwi_p (src) : tree_fits_shwi_p (src)))
23696 return false;
23698 dest->pos = is_unsigned;
23699 if (is_unsigned)
23700 dest->v.uval = tree_to_uhwi (src);
23701 else
23702 dest->v.sval = tree_to_shwi (src);
23704 return true;
23707 /* Try to extract synthetic properties out of VARIANT_PART_DECL, which is a
23708 FIELD_DECL in STRUCT_TYPE that represents a variant part. If unsuccessful,
23709 store NULL_TREE in DISCR_DECL. Otherwise:
23711 - store the discriminant field in STRUCT_TYPE that controls the variant
23712 part to *DISCR_DECL
23714 - put in *DISCR_LISTS_P an array where for each variant, the item
23715 represents the corresponding matching list of discriminant values.
23717 - put in *DISCR_LISTS_LENGTH the number of variants, which is the size of
23718 the above array.
23720 Note that when the array is allocated (i.e. when the analysis is
23721 successful), it is up to the caller to free the array. */
23723 static void
23724 analyze_variants_discr (tree variant_part_decl,
23725 tree struct_type,
23726 tree *discr_decl,
23727 dw_discr_list_ref **discr_lists_p,
23728 unsigned *discr_lists_length)
23730 tree variant_part_type = TREE_TYPE (variant_part_decl);
23731 tree variant;
23732 dw_discr_list_ref *discr_lists;
23733 unsigned i;
23735 /* Compute how many variants there are in this variant part. */
23736 *discr_lists_length = 0;
23737 for (variant = TYPE_FIELDS (variant_part_type);
23738 variant != NULL_TREE;
23739 variant = DECL_CHAIN (variant))
23740 ++*discr_lists_length;
23742 *discr_decl = NULL_TREE;
23743 *discr_lists_p
23744 = (dw_discr_list_ref *) xcalloc (*discr_lists_length,
23745 sizeof (**discr_lists_p));
23746 discr_lists = *discr_lists_p;
23748 /* And then analyze all variants to extract discriminant information for all
23749 of them. This analysis is conservative: as soon as we detect something we
23750 do not support, abort everything and pretend we found nothing. */
23751 for (variant = TYPE_FIELDS (variant_part_type), i = 0;
23752 variant != NULL_TREE;
23753 variant = DECL_CHAIN (variant), ++i)
23755 tree match_expr = DECL_QUALIFIER (variant);
23757 /* Now, try to analyze the predicate and deduce a discriminant for
23758 it. */
23759 if (match_expr == boolean_true_node)
23760 /* Typically happens for the default variant: it matches all cases that
23761 previous variants rejected. Don't output any matching value for
23762 this one. */
23763 continue;
23765 /* The following loop tries to iterate over each discriminant
23766 possibility: single values or ranges. */
23767 while (match_expr != NULL_TREE)
23769 tree next_round_match_expr;
23770 tree candidate_discr = NULL_TREE;
23771 dw_discr_list_ref new_node = NULL;
23773 /* Possibilities are matched one after the other by nested
23774 TRUTH_ORIF_EXPR expressions. Process the current possibility and
23775 continue with the rest at next iteration. */
23776 if (TREE_CODE (match_expr) == TRUTH_ORIF_EXPR)
23778 next_round_match_expr = TREE_OPERAND (match_expr, 0);
23779 match_expr = TREE_OPERAND (match_expr, 1);
23781 else
23782 next_round_match_expr = NULL_TREE;
23784 if (match_expr == boolean_false_node)
23785 /* This sub-expression matches nothing: just wait for the next
23786 one. */
23789 else if (TREE_CODE (match_expr) == EQ_EXPR)
23791 /* We are matching: <discr_field> == <integer_cst>
23792 This sub-expression matches a single value. */
23793 tree integer_cst = TREE_OPERAND (match_expr, 1);
23795 candidate_discr
23796 = analyze_discr_in_predicate (TREE_OPERAND (match_expr, 0),
23797 struct_type);
23799 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
23800 if (!get_discr_value (integer_cst,
23801 &new_node->dw_discr_lower_bound))
23802 goto abort;
23803 new_node->dw_discr_range = false;
23806 else if (TREE_CODE (match_expr) == TRUTH_ANDIF_EXPR)
23808 /* We are matching:
23809 <discr_field> > <integer_cst>
23810 && <discr_field> < <integer_cst>.
23811 This sub-expression matches the range of values between the
23812 two matched integer constants. Note that comparisons can be
23813 inclusive or exclusive. */
23814 tree candidate_discr_1, candidate_discr_2;
23815 tree lower_cst, upper_cst;
23816 bool lower_cst_included, upper_cst_included;
23817 tree lower_op = TREE_OPERAND (match_expr, 0);
23818 tree upper_op = TREE_OPERAND (match_expr, 1);
23820 /* When the comparison is exclusive, the integer constant is not
23821 the discriminant range bound we are looking for: we will have
23822 to increment or decrement it. */
23823 if (TREE_CODE (lower_op) == GE_EXPR)
23824 lower_cst_included = true;
23825 else if (TREE_CODE (lower_op) == GT_EXPR)
23826 lower_cst_included = false;
23827 else
23828 goto abort;
23830 if (TREE_CODE (upper_op) == LE_EXPR)
23831 upper_cst_included = true;
23832 else if (TREE_CODE (upper_op) == LT_EXPR)
23833 upper_cst_included = false;
23834 else
23835 goto abort;
23837 /* Extract the discriminant from the first operand and check it
23838 is consistant with the same analysis in the second
23839 operand. */
23840 candidate_discr_1
23841 = analyze_discr_in_predicate (TREE_OPERAND (lower_op, 0),
23842 struct_type);
23843 candidate_discr_2
23844 = analyze_discr_in_predicate (TREE_OPERAND (upper_op, 0),
23845 struct_type);
23846 if (candidate_discr_1 == candidate_discr_2)
23847 candidate_discr = candidate_discr_1;
23848 else
23849 goto abort;
23851 /* Extract bounds from both. */
23852 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
23853 lower_cst = TREE_OPERAND (lower_op, 1);
23854 upper_cst = TREE_OPERAND (upper_op, 1);
23856 if (!lower_cst_included)
23857 lower_cst
23858 = fold_build2 (PLUS_EXPR, TREE_TYPE (lower_cst), lower_cst,
23859 build_int_cst (TREE_TYPE (lower_cst), 1));
23860 if (!upper_cst_included)
23861 upper_cst
23862 = fold_build2 (MINUS_EXPR, TREE_TYPE (upper_cst), upper_cst,
23863 build_int_cst (TREE_TYPE (upper_cst), 1));
23865 if (!get_discr_value (lower_cst,
23866 &new_node->dw_discr_lower_bound)
23867 || !get_discr_value (upper_cst,
23868 &new_node->dw_discr_upper_bound))
23869 goto abort;
23871 new_node->dw_discr_range = true;
23874 else
23875 /* Unsupported sub-expression: we cannot determine the set of
23876 matching discriminant values. Abort everything. */
23877 goto abort;
23879 /* If the discriminant info is not consistant with what we saw so
23880 far, consider the analysis failed and abort everything. */
23881 if (candidate_discr == NULL_TREE
23882 || (*discr_decl != NULL_TREE && candidate_discr != *discr_decl))
23883 goto abort;
23884 else
23885 *discr_decl = candidate_discr;
23887 if (new_node != NULL)
23889 new_node->dw_discr_next = discr_lists[i];
23890 discr_lists[i] = new_node;
23892 match_expr = next_round_match_expr;
23896 /* If we reach this point, we could match everything we were interested
23897 in. */
23898 return;
23900 abort:
23901 /* Clean all data structure and return no result. */
23902 free (*discr_lists_p);
23903 *discr_lists_p = NULL;
23904 *discr_decl = NULL_TREE;
23907 /* Generate a DIE to represent VARIANT_PART_DECL, a variant part that is part
23908 of STRUCT_TYPE, a record type. This new DIE is emitted as the next child
23909 under CONTEXT_DIE.
23911 Variant parts are supposed to be implemented as a FIELD_DECL whose type is a
23912 QUAL_UNION_TYPE: this is the VARIANT_PART_DECL parameter. The members for
23913 this type, which are record types, represent the available variants and each
23914 has a DECL_QUALIFIER attribute. The discriminant and the discriminant
23915 values are inferred from these attributes.
23917 In trees, the offsets for the fields inside these sub-records are relative
23918 to the variant part itself, whereas the corresponding DIEs should have
23919 offset attributes that are relative to the embedding record base address.
23920 This is why the caller must provide a VARIANT_PART_OFFSET expression: it
23921 must be an expression that computes the offset of the variant part to
23922 describe in DWARF. */
23924 static void
23925 gen_variant_part (tree variant_part_decl, struct vlr_context *vlr_ctx,
23926 dw_die_ref context_die)
23928 const tree variant_part_type = TREE_TYPE (variant_part_decl);
23929 tree variant_part_offset = vlr_ctx->variant_part_offset;
23930 struct loc_descr_context ctx = {
23931 vlr_ctx->struct_type, /* context_type */
23932 NULL_TREE, /* base_decl */
23933 NULL, /* dpi */
23934 false, /* placeholder_arg */
23935 false /* placeholder_seen */
23938 /* The FIELD_DECL node in STRUCT_TYPE that acts as the discriminant, or
23939 NULL_TREE if there is no such field. */
23940 tree discr_decl = NULL_TREE;
23941 dw_discr_list_ref *discr_lists;
23942 unsigned discr_lists_length = 0;
23943 unsigned i;
23945 dw_die_ref dwarf_proc_die = NULL;
23946 dw_die_ref variant_part_die
23947 = new_die (DW_TAG_variant_part, context_die, variant_part_type);
23949 equate_decl_number_to_die (variant_part_decl, variant_part_die);
23951 analyze_variants_discr (variant_part_decl, vlr_ctx->struct_type,
23952 &discr_decl, &discr_lists, &discr_lists_length);
23954 if (discr_decl != NULL_TREE)
23956 dw_die_ref discr_die = lookup_decl_die (discr_decl);
23958 if (discr_die)
23959 add_AT_die_ref (variant_part_die, DW_AT_discr, discr_die);
23960 else
23961 /* We have no DIE for the discriminant, so just discard all
23962 discrimimant information in the output. */
23963 discr_decl = NULL_TREE;
23966 /* If the offset for this variant part is more complex than a constant,
23967 create a DWARF procedure for it so that we will not have to generate DWARF
23968 expressions for it for each member. */
23969 if (TREE_CODE (variant_part_offset) != INTEGER_CST
23970 && (dwarf_version >= 3 || !dwarf_strict))
23972 const tree dwarf_proc_fndecl
23973 = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL, NULL_TREE,
23974 build_function_type (TREE_TYPE (variant_part_offset),
23975 NULL_TREE));
23976 const tree dwarf_proc_call = build_call_expr (dwarf_proc_fndecl, 0);
23977 const dw_loc_descr_ref dwarf_proc_body
23978 = loc_descriptor_from_tree (variant_part_offset, 0, &ctx);
23980 dwarf_proc_die = new_dwarf_proc_die (dwarf_proc_body,
23981 dwarf_proc_fndecl, context_die);
23982 if (dwarf_proc_die != NULL)
23983 variant_part_offset = dwarf_proc_call;
23986 /* Output DIEs for all variants. */
23987 i = 0;
23988 for (tree variant = TYPE_FIELDS (variant_part_type);
23989 variant != NULL_TREE;
23990 variant = DECL_CHAIN (variant), ++i)
23992 tree variant_type = TREE_TYPE (variant);
23993 dw_die_ref variant_die;
23995 /* All variants (i.e. members of a variant part) are supposed to be
23996 encoded as structures. Sub-variant parts are QUAL_UNION_TYPE fields
23997 under these records. */
23998 gcc_assert (TREE_CODE (variant_type) == RECORD_TYPE);
24000 variant_die = new_die (DW_TAG_variant, variant_part_die, variant_type);
24001 equate_decl_number_to_die (variant, variant_die);
24003 /* Output discriminant values this variant matches, if any. */
24004 if (discr_decl == NULL || discr_lists[i] == NULL)
24005 /* In the case we have discriminant information at all, this is
24006 probably the default variant: as the standard says, don't
24007 output any discriminant value/list attribute. */
24009 else if (discr_lists[i]->dw_discr_next == NULL
24010 && !discr_lists[i]->dw_discr_range)
24011 /* If there is only one accepted value, don't bother outputting a
24012 list. */
24013 add_discr_value (variant_die, &discr_lists[i]->dw_discr_lower_bound);
24014 else
24015 add_discr_list (variant_die, discr_lists[i]);
24017 for (tree member = TYPE_FIELDS (variant_type);
24018 member != NULL_TREE;
24019 member = DECL_CHAIN (member))
24021 struct vlr_context vlr_sub_ctx = {
24022 vlr_ctx->struct_type, /* struct_type */
24023 NULL /* variant_part_offset */
24025 if (is_variant_part (member))
24027 /* All offsets for fields inside variant parts are relative to
24028 the top-level embedding RECORD_TYPE's base address. On the
24029 other hand, offsets in GCC's types are relative to the
24030 nested-most variant part. So we have to sum offsets each time
24031 we recurse. */
24033 vlr_sub_ctx.variant_part_offset
24034 = fold_build2 (PLUS_EXPR, TREE_TYPE (variant_part_offset),
24035 variant_part_offset, byte_position (member));
24036 gen_variant_part (member, &vlr_sub_ctx, variant_die);
24038 else
24040 vlr_sub_ctx.variant_part_offset = variant_part_offset;
24041 gen_decl_die (member, NULL, &vlr_sub_ctx, variant_die);
24046 free (discr_lists);
24049 /* Generate a DIE for a class member. */
24051 static void
24052 gen_member_die (tree type, dw_die_ref context_die)
24054 tree member;
24055 tree binfo = TYPE_BINFO (type);
24056 dw_die_ref child;
24058 /* If this is not an incomplete type, output descriptions of each of its
24059 members. Note that as we output the DIEs necessary to represent the
24060 members of this record or union type, we will also be trying to output
24061 DIEs to represent the *types* of those members. However the `type'
24062 function (above) will specifically avoid generating type DIEs for member
24063 types *within* the list of member DIEs for this (containing) type except
24064 for those types (of members) which are explicitly marked as also being
24065 members of this (containing) type themselves. The g++ front- end can
24066 force any given type to be treated as a member of some other (containing)
24067 type by setting the TYPE_CONTEXT of the given (member) type to point to
24068 the TREE node representing the appropriate (containing) type. */
24070 /* First output info about the base classes. */
24071 if (binfo)
24073 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
24074 int i;
24075 tree base;
24077 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
24078 gen_inheritance_die (base,
24079 (accesses ? (*accesses)[i] : access_public_node),
24080 type,
24081 context_die);
24084 /* Now output info about the data members and type members. */
24085 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
24087 struct vlr_context vlr_ctx = { type, NULL_TREE };
24088 bool static_inline_p
24089 = (TREE_STATIC (member)
24090 && (lang_hooks.decls.decl_dwarf_attribute (member, DW_AT_inline)
24091 != -1));
24093 /* If we thought we were generating minimal debug info for TYPE
24094 and then changed our minds, some of the member declarations
24095 may have already been defined. Don't define them again, but
24096 do put them in the right order. */
24098 child = lookup_decl_die (member);
24099 if (child)
24101 /* Handle inline static data members, which only have in-class
24102 declarations. */
24103 dw_die_ref ref = NULL;
24104 if (child->die_tag == DW_TAG_variable
24105 && child->die_parent == comp_unit_die ())
24107 ref = get_AT_ref (child, DW_AT_specification);
24108 /* For C++17 inline static data members followed by redundant
24109 out of class redeclaration, we might get here with
24110 child being the DIE created for the out of class
24111 redeclaration and with its DW_AT_specification being
24112 the DIE created for in-class definition. We want to
24113 reparent the latter, and don't want to create another
24114 DIE with DW_AT_specification in that case, because
24115 we already have one. */
24116 if (ref
24117 && static_inline_p
24118 && ref->die_tag == DW_TAG_variable
24119 && ref->die_parent == comp_unit_die ()
24120 && get_AT (ref, DW_AT_specification) == NULL)
24122 child = ref;
24123 ref = NULL;
24124 static_inline_p = false;
24127 if (child->die_tag == DW_TAG_variable
24128 && child->die_parent == comp_unit_die ()
24129 && ref == NULL)
24131 reparent_child (child, context_die);
24132 if (dwarf_version < 5)
24133 child->die_tag = DW_TAG_member;
24135 else
24136 splice_child_die (context_die, child);
24139 /* Do not generate standard DWARF for variant parts if we are generating
24140 the corresponding GNAT encodings: DIEs generated for both would
24141 conflict in our mappings. */
24142 else if (is_variant_part (member)
24143 && gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL)
24145 vlr_ctx.variant_part_offset = byte_position (member);
24146 gen_variant_part (member, &vlr_ctx, context_die);
24148 else
24150 vlr_ctx.variant_part_offset = NULL_TREE;
24151 gen_decl_die (member, NULL, &vlr_ctx, context_die);
24154 /* For C++ inline static data members emit immediately a DW_TAG_variable
24155 DIE that will refer to that DW_TAG_member/DW_TAG_variable through
24156 DW_AT_specification. */
24157 if (static_inline_p)
24159 int old_extern = DECL_EXTERNAL (member);
24160 DECL_EXTERNAL (member) = 0;
24161 gen_decl_die (member, NULL, NULL, comp_unit_die ());
24162 DECL_EXTERNAL (member) = old_extern;
24166 /* We do not keep type methods in type variants. */
24167 gcc_assert (TYPE_MAIN_VARIANT (type) == type);
24168 /* Now output info about the function members (if any). */
24169 if (TYPE_METHODS (type) != error_mark_node)
24170 for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
24172 /* Don't include clones in the member list. */
24173 if (DECL_ABSTRACT_ORIGIN (member))
24174 continue;
24175 /* Nor constructors for anonymous classes. */
24176 if (DECL_ARTIFICIAL (member)
24177 && dwarf2_name (member, 0) == NULL)
24178 continue;
24180 child = lookup_decl_die (member);
24181 if (child)
24182 splice_child_die (context_die, child);
24183 else
24184 gen_decl_die (member, NULL, NULL, context_die);
24188 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
24189 is set, we pretend that the type was never defined, so we only get the
24190 member DIEs needed by later specification DIEs. */
24192 static void
24193 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
24194 enum debug_info_usage usage)
24196 if (TREE_ASM_WRITTEN (type))
24198 /* Fill in the bound of variable-length fields in late dwarf if
24199 still incomplete. */
24200 if (!early_dwarf && variably_modified_type_p (type, NULL))
24201 for (tree member = TYPE_FIELDS (type);
24202 member;
24203 member = DECL_CHAIN (member))
24204 fill_variable_array_bounds (TREE_TYPE (member));
24205 return;
24208 dw_die_ref type_die = lookup_type_die (type);
24209 dw_die_ref scope_die = 0;
24210 int nested = 0;
24211 int complete = (TYPE_SIZE (type)
24212 && (! TYPE_STUB_DECL (type)
24213 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
24214 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
24215 complete = complete && should_emit_struct_debug (type, usage);
24217 if (type_die && ! complete)
24218 return;
24220 if (TYPE_CONTEXT (type) != NULL_TREE
24221 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
24222 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
24223 nested = 1;
24225 scope_die = scope_die_for (type, context_die);
24227 /* Generate child dies for template paramaters. */
24228 if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
24229 schedule_generic_params_dies_gen (type);
24231 if (! type_die || (nested && is_cu_die (scope_die)))
24232 /* First occurrence of type or toplevel definition of nested class. */
24234 dw_die_ref old_die = type_die;
24236 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
24237 ? record_type_tag (type) : DW_TAG_union_type,
24238 scope_die, type);
24239 equate_type_number_to_die (type, type_die);
24240 if (old_die)
24241 add_AT_specification (type_die, old_die);
24242 else
24243 add_name_attribute (type_die, type_tag (type));
24245 else
24246 remove_AT (type_die, DW_AT_declaration);
24248 /* If this type has been completed, then give it a byte_size attribute and
24249 then give a list of members. */
24250 if (complete && !ns_decl)
24252 /* Prevent infinite recursion in cases where the type of some member of
24253 this type is expressed in terms of this type itself. */
24254 TREE_ASM_WRITTEN (type) = 1;
24255 add_byte_size_attribute (type_die, type);
24256 add_alignment_attribute (type_die, type);
24257 if (TYPE_STUB_DECL (type) != NULL_TREE)
24259 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
24260 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
24263 /* If the first reference to this type was as the return type of an
24264 inline function, then it may not have a parent. Fix this now. */
24265 if (type_die->die_parent == NULL)
24266 add_child_die (scope_die, type_die);
24268 push_decl_scope (type);
24269 gen_member_die (type, type_die);
24270 pop_decl_scope ();
24272 add_gnat_descriptive_type_attribute (type_die, type, context_die);
24273 if (TYPE_ARTIFICIAL (type))
24274 add_AT_flag (type_die, DW_AT_artificial, 1);
24276 /* GNU extension: Record what type our vtable lives in. */
24277 if (TYPE_VFIELD (type))
24279 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
24281 gen_type_die (vtype, context_die);
24282 add_AT_die_ref (type_die, DW_AT_containing_type,
24283 lookup_type_die (vtype));
24286 else
24288 add_AT_flag (type_die, DW_AT_declaration, 1);
24290 /* We don't need to do this for function-local types. */
24291 if (TYPE_STUB_DECL (type)
24292 && ! decl_function_context (TYPE_STUB_DECL (type)))
24293 vec_safe_push (incomplete_types, type);
24296 if (get_AT (type_die, DW_AT_name))
24297 add_pubtype (type, type_die);
24300 /* Generate a DIE for a subroutine _type_. */
24302 static void
24303 gen_subroutine_type_die (tree type, dw_die_ref context_die)
24305 tree return_type = TREE_TYPE (type);
24306 dw_die_ref subr_die
24307 = new_die (DW_TAG_subroutine_type,
24308 scope_die_for (type, context_die), type);
24310 equate_type_number_to_die (type, subr_die);
24311 add_prototyped_attribute (subr_die, type);
24312 add_type_attribute (subr_die, return_type, TYPE_UNQUALIFIED, false,
24313 context_die);
24314 add_alignment_attribute (subr_die, type);
24315 gen_formal_types_die (type, subr_die);
24317 if (get_AT (subr_die, DW_AT_name))
24318 add_pubtype (type, subr_die);
24319 if ((dwarf_version >= 5 || !dwarf_strict)
24320 && lang_hooks.types.type_dwarf_attribute (type, DW_AT_reference) != -1)
24321 add_AT_flag (subr_die, DW_AT_reference, 1);
24322 if ((dwarf_version >= 5 || !dwarf_strict)
24323 && lang_hooks.types.type_dwarf_attribute (type,
24324 DW_AT_rvalue_reference) != -1)
24325 add_AT_flag (subr_die, DW_AT_rvalue_reference, 1);
24328 /* Generate a DIE for a type definition. */
24330 static void
24331 gen_typedef_die (tree decl, dw_die_ref context_die)
24333 dw_die_ref type_die;
24334 tree origin;
24336 if (TREE_ASM_WRITTEN (decl))
24338 if (DECL_ORIGINAL_TYPE (decl))
24339 fill_variable_array_bounds (DECL_ORIGINAL_TYPE (decl));
24340 return;
24343 TREE_ASM_WRITTEN (decl) = 1;
24344 type_die = new_die (DW_TAG_typedef, context_die, decl);
24345 origin = decl_ultimate_origin (decl);
24346 if (origin != NULL)
24347 add_abstract_origin_attribute (type_die, origin);
24348 else
24350 tree type = TREE_TYPE (decl);
24352 if (type == error_mark_node)
24353 return;
24355 add_name_and_src_coords_attributes (type_die, decl);
24356 if (DECL_ORIGINAL_TYPE (decl))
24358 type = DECL_ORIGINAL_TYPE (decl);
24360 if (type == error_mark_node)
24361 return;
24363 gcc_assert (type != TREE_TYPE (decl));
24364 equate_type_number_to_die (TREE_TYPE (decl), type_die);
24366 else
24368 if (is_naming_typedef_decl (TYPE_NAME (type)))
24370 /* Here, we are in the case of decl being a typedef naming
24371 an anonymous type, e.g:
24372 typedef struct {...} foo;
24373 In that case TREE_TYPE (decl) is not a typedef variant
24374 type and TYPE_NAME of the anonymous type is set to the
24375 TYPE_DECL of the typedef. This construct is emitted by
24376 the C++ FE.
24378 TYPE is the anonymous struct named by the typedef
24379 DECL. As we need the DW_AT_type attribute of the
24380 DW_TAG_typedef to point to the DIE of TYPE, let's
24381 generate that DIE right away. add_type_attribute
24382 called below will then pick (via lookup_type_die) that
24383 anonymous struct DIE. */
24384 if (!TREE_ASM_WRITTEN (type))
24385 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
24387 /* This is a GNU Extension. We are adding a
24388 DW_AT_linkage_name attribute to the DIE of the
24389 anonymous struct TYPE. The value of that attribute
24390 is the name of the typedef decl naming the anonymous
24391 struct. This greatly eases the work of consumers of
24392 this debug info. */
24393 add_linkage_name_raw (lookup_type_die (type), decl);
24397 add_type_attribute (type_die, type, decl_quals (decl), false,
24398 context_die);
24400 if (is_naming_typedef_decl (decl))
24401 /* We want that all subsequent calls to lookup_type_die with
24402 TYPE in argument yield the DW_TAG_typedef we have just
24403 created. */
24404 equate_type_number_to_die (type, type_die);
24406 type = TREE_TYPE (decl);
24408 add_alignment_attribute (type_die, type);
24410 add_accessibility_attribute (type_die, decl);
24413 if (DECL_ABSTRACT_P (decl))
24414 equate_decl_number_to_die (decl, type_die);
24416 if (get_AT (type_die, DW_AT_name))
24417 add_pubtype (decl, type_die);
24420 /* Generate a DIE for a struct, class, enum or union type. */
24422 static void
24423 gen_tagged_type_die (tree type,
24424 dw_die_ref context_die,
24425 enum debug_info_usage usage)
24427 int need_pop;
24429 if (type == NULL_TREE
24430 || !is_tagged_type (type))
24431 return;
24433 if (TREE_ASM_WRITTEN (type))
24434 need_pop = 0;
24435 /* If this is a nested type whose containing class hasn't been written
24436 out yet, writing it out will cover this one, too. This does not apply
24437 to instantiations of member class templates; they need to be added to
24438 the containing class as they are generated. FIXME: This hurts the
24439 idea of combining type decls from multiple TUs, since we can't predict
24440 what set of template instantiations we'll get. */
24441 else if (TYPE_CONTEXT (type)
24442 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
24443 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
24445 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
24447 if (TREE_ASM_WRITTEN (type))
24448 return;
24450 /* If that failed, attach ourselves to the stub. */
24451 push_decl_scope (TYPE_CONTEXT (type));
24452 context_die = lookup_type_die (TYPE_CONTEXT (type));
24453 need_pop = 1;
24455 else if (TYPE_CONTEXT (type) != NULL_TREE
24456 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
24458 /* If this type is local to a function that hasn't been written
24459 out yet, use a NULL context for now; it will be fixed up in
24460 decls_for_scope. */
24461 context_die = lookup_decl_die (TYPE_CONTEXT (type));
24462 /* A declaration DIE doesn't count; nested types need to go in the
24463 specification. */
24464 if (context_die && is_declaration_die (context_die))
24465 context_die = NULL;
24466 need_pop = 0;
24468 else
24470 context_die = declare_in_namespace (type, context_die);
24471 need_pop = 0;
24474 if (TREE_CODE (type) == ENUMERAL_TYPE)
24476 /* This might have been written out by the call to
24477 declare_in_namespace. */
24478 if (!TREE_ASM_WRITTEN (type))
24479 gen_enumeration_type_die (type, context_die);
24481 else
24482 gen_struct_or_union_type_die (type, context_die, usage);
24484 if (need_pop)
24485 pop_decl_scope ();
24487 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
24488 it up if it is ever completed. gen_*_type_die will set it for us
24489 when appropriate. */
24492 /* Generate a type description DIE. */
24494 static void
24495 gen_type_die_with_usage (tree type, dw_die_ref context_die,
24496 enum debug_info_usage usage)
24498 struct array_descr_info info;
24500 if (type == NULL_TREE || type == error_mark_node)
24501 return;
24503 if (flag_checking && type)
24504 verify_type (type);
24506 if (TYPE_NAME (type) != NULL_TREE
24507 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
24508 && is_redundant_typedef (TYPE_NAME (type))
24509 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
24510 /* The DECL of this type is a typedef we don't want to emit debug
24511 info for but we want debug info for its underlying typedef.
24512 This can happen for e.g, the injected-class-name of a C++
24513 type. */
24514 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
24516 /* If TYPE is a typedef type variant, let's generate debug info
24517 for the parent typedef which TYPE is a type of. */
24518 if (typedef_variant_p (type))
24520 if (TREE_ASM_WRITTEN (type))
24521 return;
24523 /* Prevent broken recursion; we can't hand off to the same type. */
24524 gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
24526 /* Give typedefs the right scope. */
24527 context_die = scope_die_for (type, context_die);
24529 TREE_ASM_WRITTEN (type) = 1;
24531 gen_decl_die (TYPE_NAME (type), NULL, NULL, context_die);
24532 return;
24535 /* If type is an anonymous tagged type named by a typedef, let's
24536 generate debug info for the typedef. */
24537 if (is_naming_typedef_decl (TYPE_NAME (type)))
24539 /* Use the DIE of the containing namespace as the parent DIE of
24540 the type description DIE we want to generate. */
24541 if (DECL_CONTEXT (TYPE_NAME (type))
24542 && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
24543 context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
24545 gen_decl_die (TYPE_NAME (type), NULL, NULL, context_die);
24546 return;
24549 if (lang_hooks.types.get_debug_type)
24551 tree debug_type = lang_hooks.types.get_debug_type (type);
24553 if (debug_type != NULL_TREE && debug_type != type)
24555 gen_type_die_with_usage (debug_type, context_die, usage);
24556 return;
24560 /* We are going to output a DIE to represent the unqualified version
24561 of this type (i.e. without any const or volatile qualifiers) so
24562 get the main variant (i.e. the unqualified version) of this type
24563 now. (Vectors and arrays are special because the debugging info is in the
24564 cloned type itself. Similarly function/method types can contain extra
24565 ref-qualification). */
24566 if (TREE_CODE (type) == FUNCTION_TYPE
24567 || TREE_CODE (type) == METHOD_TYPE)
24569 /* For function/method types, can't use type_main_variant here,
24570 because that can have different ref-qualifiers for C++,
24571 but try to canonicalize. */
24572 tree main = TYPE_MAIN_VARIANT (type);
24573 for (tree t = main; t; t = TYPE_NEXT_VARIANT (t))
24575 if (check_base_type (t, main) && check_lang_type (t, type))
24577 type = t;
24578 break;
24582 else if (TREE_CODE (type) != VECTOR_TYPE
24583 && TREE_CODE (type) != ARRAY_TYPE)
24584 type = type_main_variant (type);
24586 /* If this is an array type with hidden descriptor, handle it first. */
24587 if (!TREE_ASM_WRITTEN (type)
24588 && lang_hooks.types.get_array_descr_info)
24590 memset (&info, 0, sizeof (info));
24591 if (lang_hooks.types.get_array_descr_info (type, &info))
24593 /* Fortran sometimes emits array types with no dimension. */
24594 gcc_assert (info.ndimensions >= 0
24595 && (info.ndimensions
24596 <= DWARF2OUT_ARRAY_DESCR_INFO_MAX_DIMEN));
24597 gen_descr_array_type_die (type, &info, context_die);
24598 TREE_ASM_WRITTEN (type) = 1;
24599 return;
24603 if (TREE_ASM_WRITTEN (type))
24605 /* Variable-length types may be incomplete even if
24606 TREE_ASM_WRITTEN. For such types, fall through to
24607 gen_array_type_die() and possibly fill in
24608 DW_AT_{upper,lower}_bound attributes. */
24609 if ((TREE_CODE (type) != ARRAY_TYPE
24610 && TREE_CODE (type) != RECORD_TYPE
24611 && TREE_CODE (type) != UNION_TYPE
24612 && TREE_CODE (type) != QUAL_UNION_TYPE)
24613 || !variably_modified_type_p (type, NULL))
24614 return;
24617 switch (TREE_CODE (type))
24619 case ERROR_MARK:
24620 break;
24622 case POINTER_TYPE:
24623 case REFERENCE_TYPE:
24624 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
24625 ensures that the gen_type_die recursion will terminate even if the
24626 type is recursive. Recursive types are possible in Ada. */
24627 /* ??? We could perhaps do this for all types before the switch
24628 statement. */
24629 TREE_ASM_WRITTEN (type) = 1;
24631 /* For these types, all that is required is that we output a DIE (or a
24632 set of DIEs) to represent the "basis" type. */
24633 gen_type_die_with_usage (TREE_TYPE (type), context_die,
24634 DINFO_USAGE_IND_USE);
24635 break;
24637 case OFFSET_TYPE:
24638 /* This code is used for C++ pointer-to-data-member types.
24639 Output a description of the relevant class type. */
24640 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
24641 DINFO_USAGE_IND_USE);
24643 /* Output a description of the type of the object pointed to. */
24644 gen_type_die_with_usage (TREE_TYPE (type), context_die,
24645 DINFO_USAGE_IND_USE);
24647 /* Now output a DIE to represent this pointer-to-data-member type
24648 itself. */
24649 gen_ptr_to_mbr_type_die (type, context_die);
24650 break;
24652 case FUNCTION_TYPE:
24653 /* Force out return type (in case it wasn't forced out already). */
24654 gen_type_die_with_usage (TREE_TYPE (type), context_die,
24655 DINFO_USAGE_DIR_USE);
24656 gen_subroutine_type_die (type, context_die);
24657 break;
24659 case METHOD_TYPE:
24660 /* Force out return type (in case it wasn't forced out already). */
24661 gen_type_die_with_usage (TREE_TYPE (type), context_die,
24662 DINFO_USAGE_DIR_USE);
24663 gen_subroutine_type_die (type, context_die);
24664 break;
24666 case ARRAY_TYPE:
24667 case VECTOR_TYPE:
24668 gen_array_type_die (type, context_die);
24669 break;
24671 case ENUMERAL_TYPE:
24672 case RECORD_TYPE:
24673 case UNION_TYPE:
24674 case QUAL_UNION_TYPE:
24675 gen_tagged_type_die (type, context_die, usage);
24676 return;
24678 case VOID_TYPE:
24679 case INTEGER_TYPE:
24680 case REAL_TYPE:
24681 case FIXED_POINT_TYPE:
24682 case COMPLEX_TYPE:
24683 case BOOLEAN_TYPE:
24684 case POINTER_BOUNDS_TYPE:
24685 /* No DIEs needed for fundamental types. */
24686 break;
24688 case NULLPTR_TYPE:
24689 case LANG_TYPE:
24690 /* Just use DW_TAG_unspecified_type. */
24692 dw_die_ref type_die = lookup_type_die (type);
24693 if (type_die == NULL)
24695 tree name = TYPE_IDENTIFIER (type);
24696 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (),
24697 type);
24698 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
24699 equate_type_number_to_die (type, type_die);
24702 break;
24704 default:
24705 if (is_cxx_auto (type))
24707 tree name = TYPE_IDENTIFIER (type);
24708 dw_die_ref *die = (name == get_identifier ("auto")
24709 ? &auto_die : &decltype_auto_die);
24710 if (!*die)
24712 *die = new_die (DW_TAG_unspecified_type,
24713 comp_unit_die (), NULL_TREE);
24714 add_name_attribute (*die, IDENTIFIER_POINTER (name));
24716 equate_type_number_to_die (type, *die);
24717 break;
24719 gcc_unreachable ();
24722 TREE_ASM_WRITTEN (type) = 1;
24725 static void
24726 gen_type_die (tree type, dw_die_ref context_die)
24728 if (type != error_mark_node)
24730 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
24731 if (flag_checking)
24733 dw_die_ref die = lookup_type_die (type);
24734 if (die)
24735 check_die (die);
24740 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
24741 things which are local to the given block. */
24743 static void
24744 gen_block_die (tree stmt, dw_die_ref context_die)
24746 int must_output_die = 0;
24747 bool inlined_func;
24749 /* Ignore blocks that are NULL. */
24750 if (stmt == NULL_TREE)
24751 return;
24753 inlined_func = inlined_function_outer_scope_p (stmt);
24755 /* If the block is one fragment of a non-contiguous block, do not
24756 process the variables, since they will have been done by the
24757 origin block. Do process subblocks. */
24758 if (BLOCK_FRAGMENT_ORIGIN (stmt))
24760 tree sub;
24762 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
24763 gen_block_die (sub, context_die);
24765 return;
24768 /* Determine if we need to output any Dwarf DIEs at all to represent this
24769 block. */
24770 if (inlined_func)
24771 /* The outer scopes for inlinings *must* always be represented. We
24772 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
24773 must_output_die = 1;
24774 else
24776 /* Determine if this block directly contains any "significant"
24777 local declarations which we will need to output DIEs for. */
24778 if (debug_info_level > DINFO_LEVEL_TERSE)
24779 /* We are not in terse mode so *any* local declaration counts
24780 as being a "significant" one. */
24781 must_output_die = ((BLOCK_VARS (stmt) != NULL
24782 || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
24783 && (TREE_USED (stmt)
24784 || TREE_ASM_WRITTEN (stmt)
24785 || BLOCK_ABSTRACT (stmt)));
24786 else if ((TREE_USED (stmt)
24787 || TREE_ASM_WRITTEN (stmt)
24788 || BLOCK_ABSTRACT (stmt))
24789 && !dwarf2out_ignore_block (stmt))
24790 must_output_die = 1;
24793 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
24794 DIE for any block which contains no significant local declarations at
24795 all. Rather, in such cases we just call `decls_for_scope' so that any
24796 needed Dwarf info for any sub-blocks will get properly generated. Note
24797 that in terse mode, our definition of what constitutes a "significant"
24798 local declaration gets restricted to include only inlined function
24799 instances and local (nested) function definitions. */
24800 if (must_output_die)
24802 if (inlined_func)
24804 /* If STMT block is abstract, that means we have been called
24805 indirectly from dwarf2out_abstract_function.
24806 That function rightfully marks the descendent blocks (of
24807 the abstract function it is dealing with) as being abstract,
24808 precisely to prevent us from emitting any
24809 DW_TAG_inlined_subroutine DIE as a descendent
24810 of an abstract function instance. So in that case, we should
24811 not call gen_inlined_subroutine_die.
24813 Later though, when cgraph asks dwarf2out to emit info
24814 for the concrete instance of the function decl into which
24815 the concrete instance of STMT got inlined, the later will lead
24816 to the generation of a DW_TAG_inlined_subroutine DIE. */
24817 if (! BLOCK_ABSTRACT (stmt))
24818 gen_inlined_subroutine_die (stmt, context_die);
24820 else
24821 gen_lexical_block_die (stmt, context_die);
24823 else
24824 decls_for_scope (stmt, context_die);
24827 /* Process variable DECL (or variable with origin ORIGIN) within
24828 block STMT and add it to CONTEXT_DIE. */
24829 static void
24830 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
24832 dw_die_ref die;
24833 tree decl_or_origin = decl ? decl : origin;
24835 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
24836 die = lookup_decl_die (decl_or_origin);
24837 else if (TREE_CODE (decl_or_origin) == TYPE_DECL)
24839 if (TYPE_DECL_IS_STUB (decl_or_origin))
24840 die = lookup_type_die (TREE_TYPE (decl_or_origin));
24841 else
24842 die = lookup_decl_die (decl_or_origin);
24843 /* Avoid re-creating the DIE late if it was optimized as unused early. */
24844 if (! die && ! early_dwarf)
24845 return;
24847 else
24848 die = NULL;
24850 if (die != NULL && die->die_parent == NULL)
24851 add_child_die (context_die, die);
24852 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
24854 if (early_dwarf)
24855 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
24856 stmt, context_die);
24858 else
24859 gen_decl_die (decl, origin, NULL, context_die);
24862 /* Generate all of the decls declared within a given scope and (recursively)
24863 all of its sub-blocks. */
24865 static void
24866 decls_for_scope (tree stmt, dw_die_ref context_die)
24868 tree decl;
24869 unsigned int i;
24870 tree subblocks;
24872 /* Ignore NULL blocks. */
24873 if (stmt == NULL_TREE)
24874 return;
24876 /* Output the DIEs to represent all of the data objects and typedefs
24877 declared directly within this block but not within any nested
24878 sub-blocks. Also, nested function and tag DIEs have been
24879 generated with a parent of NULL; fix that up now. We don't
24880 have to do this if we're at -g1. */
24881 if (debug_info_level > DINFO_LEVEL_TERSE)
24883 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
24884 process_scope_var (stmt, decl, NULL_TREE, context_die);
24885 /* BLOCK_NONLOCALIZED_VARs simply generate DIE stubs with abstract
24886 origin - avoid doing this twice as we have no good way to see
24887 if we've done it once already. */
24888 if (! early_dwarf)
24889 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
24891 decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
24892 if (TREE_CODE (decl) == FUNCTION_DECL)
24893 process_scope_var (stmt, decl, NULL_TREE, context_die);
24894 else
24895 process_scope_var (stmt, NULL_TREE, decl, context_die);
24899 /* Even if we're at -g1, we need to process the subblocks in order to get
24900 inlined call information. */
24902 /* Output the DIEs to represent all sub-blocks (and the items declared
24903 therein) of this block. */
24904 for (subblocks = BLOCK_SUBBLOCKS (stmt);
24905 subblocks != NULL;
24906 subblocks = BLOCK_CHAIN (subblocks))
24907 gen_block_die (subblocks, context_die);
24910 /* Is this a typedef we can avoid emitting? */
24912 bool
24913 is_redundant_typedef (const_tree decl)
24915 if (TYPE_DECL_IS_STUB (decl))
24916 return true;
24918 if (DECL_ARTIFICIAL (decl)
24919 && DECL_CONTEXT (decl)
24920 && is_tagged_type (DECL_CONTEXT (decl))
24921 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
24922 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
24923 /* Also ignore the artificial member typedef for the class name. */
24924 return true;
24926 return false;
24929 /* Return TRUE if TYPE is a typedef that names a type for linkage
24930 purposes. This kind of typedefs is produced by the C++ FE for
24931 constructs like:
24933 typedef struct {...} foo;
24935 In that case, there is no typedef variant type produced for foo.
24936 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
24937 struct type. */
24939 static bool
24940 is_naming_typedef_decl (const_tree decl)
24942 if (decl == NULL_TREE
24943 || TREE_CODE (decl) != TYPE_DECL
24944 || DECL_NAMELESS (decl)
24945 || !is_tagged_type (TREE_TYPE (decl))
24946 || DECL_IS_BUILTIN (decl)
24947 || is_redundant_typedef (decl)
24948 /* It looks like Ada produces TYPE_DECLs that are very similar
24949 to C++ naming typedefs but that have different
24950 semantics. Let's be specific to c++ for now. */
24951 || !is_cxx (decl))
24952 return FALSE;
24954 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
24955 && TYPE_NAME (TREE_TYPE (decl)) == decl
24956 && (TYPE_STUB_DECL (TREE_TYPE (decl))
24957 != TYPE_NAME (TREE_TYPE (decl))));
24960 /* Looks up the DIE for a context. */
24962 static inline dw_die_ref
24963 lookup_context_die (tree context)
24965 if (context)
24967 /* Find die that represents this context. */
24968 if (TYPE_P (context))
24970 context = TYPE_MAIN_VARIANT (context);
24971 dw_die_ref ctx = lookup_type_die (context);
24972 if (!ctx)
24973 return NULL;
24974 return strip_naming_typedef (context, ctx);
24976 else
24977 return lookup_decl_die (context);
24979 return comp_unit_die ();
24982 /* Returns the DIE for a context. */
24984 static inline dw_die_ref
24985 get_context_die (tree context)
24987 if (context)
24989 /* Find die that represents this context. */
24990 if (TYPE_P (context))
24992 context = TYPE_MAIN_VARIANT (context);
24993 return strip_naming_typedef (context, force_type_die (context));
24995 else
24996 return force_decl_die (context);
24998 return comp_unit_die ();
25001 /* Returns the DIE for decl. A DIE will always be returned. */
25003 static dw_die_ref
25004 force_decl_die (tree decl)
25006 dw_die_ref decl_die;
25007 unsigned saved_external_flag;
25008 tree save_fn = NULL_TREE;
25009 decl_die = lookup_decl_die (decl);
25010 if (!decl_die)
25012 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
25014 decl_die = lookup_decl_die (decl);
25015 if (decl_die)
25016 return decl_die;
25018 switch (TREE_CODE (decl))
25020 case FUNCTION_DECL:
25021 /* Clear current_function_decl, so that gen_subprogram_die thinks
25022 that this is a declaration. At this point, we just want to force
25023 declaration die. */
25024 save_fn = current_function_decl;
25025 current_function_decl = NULL_TREE;
25026 gen_subprogram_die (decl, context_die);
25027 current_function_decl = save_fn;
25028 break;
25030 case VAR_DECL:
25031 /* Set external flag to force declaration die. Restore it after
25032 gen_decl_die() call. */
25033 saved_external_flag = DECL_EXTERNAL (decl);
25034 DECL_EXTERNAL (decl) = 1;
25035 gen_decl_die (decl, NULL, NULL, context_die);
25036 DECL_EXTERNAL (decl) = saved_external_flag;
25037 break;
25039 case NAMESPACE_DECL:
25040 if (dwarf_version >= 3 || !dwarf_strict)
25041 dwarf2out_decl (decl);
25042 else
25043 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
25044 decl_die = comp_unit_die ();
25045 break;
25047 case TRANSLATION_UNIT_DECL:
25048 decl_die = comp_unit_die ();
25049 break;
25051 default:
25052 gcc_unreachable ();
25055 /* We should be able to find the DIE now. */
25056 if (!decl_die)
25057 decl_die = lookup_decl_die (decl);
25058 gcc_assert (decl_die);
25061 return decl_die;
25064 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
25065 always returned. */
25067 static dw_die_ref
25068 force_type_die (tree type)
25070 dw_die_ref type_die;
25072 type_die = lookup_type_die (type);
25073 if (!type_die)
25075 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
25077 type_die = modified_type_die (type, TYPE_QUALS_NO_ADDR_SPACE (type),
25078 false, context_die);
25079 gcc_assert (type_die);
25081 return type_die;
25084 /* Force out any required namespaces to be able to output DECL,
25085 and return the new context_die for it, if it's changed. */
25087 static dw_die_ref
25088 setup_namespace_context (tree thing, dw_die_ref context_die)
25090 tree context = (DECL_P (thing)
25091 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
25092 if (context && TREE_CODE (context) == NAMESPACE_DECL)
25093 /* Force out the namespace. */
25094 context_die = force_decl_die (context);
25096 return context_die;
25099 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
25100 type) within its namespace, if appropriate.
25102 For compatibility with older debuggers, namespace DIEs only contain
25103 declarations; all definitions are emitted at CU scope, with
25104 DW_AT_specification pointing to the declaration (like with class
25105 members). */
25107 static dw_die_ref
25108 declare_in_namespace (tree thing, dw_die_ref context_die)
25110 dw_die_ref ns_context;
25112 if (debug_info_level <= DINFO_LEVEL_TERSE)
25113 return context_die;
25115 /* External declarations in the local scope only need to be emitted
25116 once, not once in the namespace and once in the scope.
25118 This avoids declaring the `extern' below in the
25119 namespace DIE as well as in the innermost scope:
25121 namespace S
25123 int i=5;
25124 int foo()
25126 int i=8;
25127 extern int i;
25128 return i;
25132 if (DECL_P (thing) && DECL_EXTERNAL (thing) && local_scope_p (context_die))
25133 return context_die;
25135 /* If this decl is from an inlined function, then don't try to emit it in its
25136 namespace, as we will get confused. It would have already been emitted
25137 when the abstract instance of the inline function was emitted anyways. */
25138 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
25139 return context_die;
25141 ns_context = setup_namespace_context (thing, context_die);
25143 if (ns_context != context_die)
25145 if (is_fortran ())
25146 return ns_context;
25147 if (DECL_P (thing))
25148 gen_decl_die (thing, NULL, NULL, ns_context);
25149 else
25150 gen_type_die (thing, ns_context);
25152 return context_die;
25155 /* Generate a DIE for a namespace or namespace alias. */
25157 static void
25158 gen_namespace_die (tree decl, dw_die_ref context_die)
25160 dw_die_ref namespace_die;
25162 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
25163 they are an alias of. */
25164 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
25166 /* Output a real namespace or module. */
25167 context_die = setup_namespace_context (decl, comp_unit_die ());
25168 namespace_die = new_die (is_fortran ()
25169 ? DW_TAG_module : DW_TAG_namespace,
25170 context_die, decl);
25171 /* For Fortran modules defined in different CU don't add src coords. */
25172 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
25174 const char *name = dwarf2_name (decl, 0);
25175 if (name)
25176 add_name_attribute (namespace_die, name);
25178 else
25179 add_name_and_src_coords_attributes (namespace_die, decl);
25180 if (DECL_EXTERNAL (decl))
25181 add_AT_flag (namespace_die, DW_AT_declaration, 1);
25182 equate_decl_number_to_die (decl, namespace_die);
25184 else
25186 /* Output a namespace alias. */
25188 /* Force out the namespace we are an alias of, if necessary. */
25189 dw_die_ref origin_die
25190 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
25192 if (DECL_FILE_SCOPE_P (decl)
25193 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
25194 context_die = setup_namespace_context (decl, comp_unit_die ());
25195 /* Now create the namespace alias DIE. */
25196 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
25197 add_name_and_src_coords_attributes (namespace_die, decl);
25198 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
25199 equate_decl_number_to_die (decl, namespace_die);
25201 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
25202 if (want_pubnames ())
25203 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
25206 /* Generate Dwarf debug information for a decl described by DECL.
25207 The return value is currently only meaningful for PARM_DECLs,
25208 for all other decls it returns NULL.
25210 If DECL is a FIELD_DECL, CTX is required: see the comment for VLR_CONTEXT.
25211 It can be NULL otherwise. */
25213 static dw_die_ref
25214 gen_decl_die (tree decl, tree origin, struct vlr_context *ctx,
25215 dw_die_ref context_die)
25217 tree decl_or_origin = decl ? decl : origin;
25218 tree class_origin = NULL, ultimate_origin;
25220 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
25221 return NULL;
25223 /* Ignore pointer bounds decls. */
25224 if (DECL_P (decl_or_origin)
25225 && TREE_TYPE (decl_or_origin)
25226 && POINTER_BOUNDS_P (decl_or_origin))
25227 return NULL;
25229 switch (TREE_CODE (decl_or_origin))
25231 case ERROR_MARK:
25232 break;
25234 case CONST_DECL:
25235 if (!is_fortran () && !is_ada ())
25237 /* The individual enumerators of an enum type get output when we output
25238 the Dwarf representation of the relevant enum type itself. */
25239 break;
25242 /* Emit its type. */
25243 gen_type_die (TREE_TYPE (decl), context_die);
25245 /* And its containing namespace. */
25246 context_die = declare_in_namespace (decl, context_die);
25248 gen_const_die (decl, context_die);
25249 break;
25251 case FUNCTION_DECL:
25252 /* Don't output any DIEs to represent mere function declarations,
25253 unless they are class members or explicit block externs. */
25254 if (DECL_INITIAL (decl_or_origin) == NULL_TREE
25255 && DECL_FILE_SCOPE_P (decl_or_origin)
25256 && (current_function_decl == NULL_TREE
25257 || DECL_ARTIFICIAL (decl_or_origin)))
25258 break;
25260 #if 0
25261 /* FIXME */
25262 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
25263 on local redeclarations of global functions. That seems broken. */
25264 if (current_function_decl != decl)
25265 /* This is only a declaration. */;
25266 #endif
25268 /* If we're emitting a clone, emit info for the abstract instance. */
25269 if (origin || DECL_ORIGIN (decl) != decl)
25270 dwarf2out_abstract_function (origin
25271 ? DECL_ORIGIN (origin)
25272 : DECL_ABSTRACT_ORIGIN (decl));
25274 /* If we're emitting an out-of-line copy of an inline function,
25275 emit info for the abstract instance and set up to refer to it. */
25276 else if (cgraph_function_possibly_inlined_p (decl)
25277 && ! DECL_ABSTRACT_P (decl)
25278 && ! class_or_namespace_scope_p (context_die)
25279 /* dwarf2out_abstract_function won't emit a die if this is just
25280 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
25281 that case, because that works only if we have a die. */
25282 && DECL_INITIAL (decl) != NULL_TREE)
25284 dwarf2out_abstract_function (decl);
25285 set_decl_origin_self (decl);
25288 /* Otherwise we're emitting the primary DIE for this decl. */
25289 else if (debug_info_level > DINFO_LEVEL_TERSE)
25291 /* Before we describe the FUNCTION_DECL itself, make sure that we
25292 have its containing type. */
25293 if (!origin)
25294 origin = decl_class_context (decl);
25295 if (origin != NULL_TREE)
25296 gen_type_die (origin, context_die);
25298 /* And its return type. */
25299 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
25301 /* And its virtual context. */
25302 if (DECL_VINDEX (decl) != NULL_TREE)
25303 gen_type_die (DECL_CONTEXT (decl), context_die);
25305 /* Make sure we have a member DIE for decl. */
25306 if (origin != NULL_TREE)
25307 gen_type_die_for_member (origin, decl, context_die);
25309 /* And its containing namespace. */
25310 context_die = declare_in_namespace (decl, context_die);
25313 /* Now output a DIE to represent the function itself. */
25314 if (decl)
25315 gen_subprogram_die (decl, context_die);
25316 break;
25318 case TYPE_DECL:
25319 /* If we are in terse mode, don't generate any DIEs to represent any
25320 actual typedefs. */
25321 if (debug_info_level <= DINFO_LEVEL_TERSE)
25322 break;
25324 /* In the special case of a TYPE_DECL node representing the declaration
25325 of some type tag, if the given TYPE_DECL is marked as having been
25326 instantiated from some other (original) TYPE_DECL node (e.g. one which
25327 was generated within the original definition of an inline function) we
25328 used to generate a special (abbreviated) DW_TAG_structure_type,
25329 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
25330 should be actually referencing those DIEs, as variable DIEs with that
25331 type would be emitted already in the abstract origin, so it was always
25332 removed during unused type prunning. Don't add anything in this
25333 case. */
25334 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
25335 break;
25337 if (is_redundant_typedef (decl))
25338 gen_type_die (TREE_TYPE (decl), context_die);
25339 else
25340 /* Output a DIE to represent the typedef itself. */
25341 gen_typedef_die (decl, context_die);
25342 break;
25344 case LABEL_DECL:
25345 if (debug_info_level >= DINFO_LEVEL_NORMAL)
25346 gen_label_die (decl, context_die);
25347 break;
25349 case VAR_DECL:
25350 case RESULT_DECL:
25351 /* If we are in terse mode, don't generate any DIEs to represent any
25352 variable declarations or definitions. */
25353 if (debug_info_level <= DINFO_LEVEL_TERSE)
25354 break;
25356 /* Output any DIEs that are needed to specify the type of this data
25357 object. */
25358 if (decl_by_reference_p (decl_or_origin))
25359 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
25360 else
25361 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
25363 /* And its containing type. */
25364 class_origin = decl_class_context (decl_or_origin);
25365 if (class_origin != NULL_TREE)
25366 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
25368 /* And its containing namespace. */
25369 context_die = declare_in_namespace (decl_or_origin, context_die);
25371 /* Now output the DIE to represent the data object itself. This gets
25372 complicated because of the possibility that the VAR_DECL really
25373 represents an inlined instance of a formal parameter for an inline
25374 function. */
25375 ultimate_origin = decl_ultimate_origin (decl_or_origin);
25376 if (ultimate_origin != NULL_TREE
25377 && TREE_CODE (ultimate_origin) == PARM_DECL)
25378 gen_formal_parameter_die (decl, origin,
25379 true /* Emit name attribute. */,
25380 context_die);
25381 else
25382 gen_variable_die (decl, origin, context_die);
25383 break;
25385 case FIELD_DECL:
25386 gcc_assert (ctx != NULL && ctx->struct_type != NULL);
25387 /* Ignore the nameless fields that are used to skip bits but handle C++
25388 anonymous unions and structs. */
25389 if (DECL_NAME (decl) != NULL_TREE
25390 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
25391 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
25393 gen_type_die (member_declared_type (decl), context_die);
25394 gen_field_die (decl, ctx, context_die);
25396 break;
25398 case PARM_DECL:
25399 if (DECL_BY_REFERENCE (decl_or_origin))
25400 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
25401 else
25402 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
25403 return gen_formal_parameter_die (decl, origin,
25404 true /* Emit name attribute. */,
25405 context_die);
25407 case NAMESPACE_DECL:
25408 if (dwarf_version >= 3 || !dwarf_strict)
25409 gen_namespace_die (decl, context_die);
25410 break;
25412 case IMPORTED_DECL:
25413 dwarf2out_imported_module_or_decl_1 (decl, DECL_NAME (decl),
25414 DECL_CONTEXT (decl), context_die);
25415 break;
25417 case NAMELIST_DECL:
25418 gen_namelist_decl (DECL_NAME (decl), context_die,
25419 NAMELIST_DECL_ASSOCIATED_DECL (decl));
25420 break;
25422 default:
25423 /* Probably some frontend-internal decl. Assume we don't care. */
25424 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
25425 break;
25428 return NULL;
25431 /* Output initial debug information for global DECL. Called at the
25432 end of the parsing process.
25434 This is the initial debug generation process. As such, the DIEs
25435 generated may be incomplete. A later debug generation pass
25436 (dwarf2out_late_global_decl) will augment the information generated
25437 in this pass (e.g., with complete location info). */
25439 static void
25440 dwarf2out_early_global_decl (tree decl)
25442 set_early_dwarf s;
25444 /* gen_decl_die() will set DECL_ABSTRACT because
25445 cgraph_function_possibly_inlined_p() returns true. This is in
25446 turn will cause DW_AT_inline attributes to be set.
25448 This happens because at early dwarf generation, there is no
25449 cgraph information, causing cgraph_function_possibly_inlined_p()
25450 to return true. Trick cgraph_function_possibly_inlined_p()
25451 while we generate dwarf early. */
25452 bool save = symtab->global_info_ready;
25453 symtab->global_info_ready = true;
25455 /* We don't handle TYPE_DECLs. If required, they'll be reached via
25456 other DECLs and they can point to template types or other things
25457 that dwarf2out can't handle when done via dwarf2out_decl. */
25458 if (TREE_CODE (decl) != TYPE_DECL
25459 && TREE_CODE (decl) != PARM_DECL)
25461 tree save_fndecl = current_function_decl;
25462 if (TREE_CODE (decl) == FUNCTION_DECL)
25464 /* No cfun means the symbol has no body, so there's nothing
25465 to emit. */
25466 if (!DECL_STRUCT_FUNCTION (decl))
25467 goto early_decl_exit;
25469 /* For nested functions, make sure we have DIEs for the parents first
25470 so that all nested DIEs are generated at the proper scope in the
25471 first shot. */
25472 tree context = decl_function_context (decl);
25473 if (context != NULL && lookup_decl_die (context) == NULL)
25475 current_function_decl = context;
25476 dwarf2out_decl (context);
25479 current_function_decl = decl;
25481 dwarf2out_decl (decl);
25482 if (TREE_CODE (decl) == FUNCTION_DECL)
25483 current_function_decl = save_fndecl;
25485 early_decl_exit:
25486 symtab->global_info_ready = save;
25489 /* Output debug information for global decl DECL. Called from
25490 toplev.c after compilation proper has finished. */
25492 static void
25493 dwarf2out_late_global_decl (tree decl)
25495 /* Fill-in any location information we were unable to determine
25496 on the first pass. */
25497 if (VAR_P (decl) && !POINTER_BOUNDS_P (decl))
25499 dw_die_ref die = lookup_decl_die (decl);
25501 /* We have to generate early debug late for LTO. */
25502 if (! die && in_lto_p)
25504 dwarf2out_decl (decl);
25505 die = lookup_decl_die (decl);
25508 if (die)
25510 /* We get called via the symtab code invoking late_global_decl
25511 for symbols that are optimized out. Do not add locations
25512 for those. */
25513 varpool_node *node = varpool_node::get (decl);
25514 if (! node || ! node->definition)
25515 tree_add_const_value_attribute_for_decl (die, decl);
25516 else
25517 add_location_or_const_value_attribute (die, decl, false);
25522 /* Output debug information for type decl DECL. Called from toplev.c
25523 and from language front ends (to record built-in types). */
25524 static void
25525 dwarf2out_type_decl (tree decl, int local)
25527 if (!local)
25529 set_early_dwarf s;
25530 dwarf2out_decl (decl);
25534 /* Output debug information for imported module or decl DECL.
25535 NAME is non-NULL name in the lexical block if the decl has been renamed.
25536 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
25537 that DECL belongs to.
25538 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
25539 static void
25540 dwarf2out_imported_module_or_decl_1 (tree decl,
25541 tree name,
25542 tree lexical_block,
25543 dw_die_ref lexical_block_die)
25545 expanded_location xloc;
25546 dw_die_ref imported_die = NULL;
25547 dw_die_ref at_import_die;
25549 if (TREE_CODE (decl) == IMPORTED_DECL)
25551 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
25552 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
25553 gcc_assert (decl);
25555 else
25556 xloc = expand_location (input_location);
25558 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
25560 at_import_die = force_type_die (TREE_TYPE (decl));
25561 /* For namespace N { typedef void T; } using N::T; base_type_die
25562 returns NULL, but DW_TAG_imported_declaration requires
25563 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
25564 if (!at_import_die)
25566 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
25567 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
25568 at_import_die = lookup_type_die (TREE_TYPE (decl));
25569 gcc_assert (at_import_die);
25572 else
25574 at_import_die = lookup_decl_die (decl);
25575 if (!at_import_die)
25577 /* If we're trying to avoid duplicate debug info, we may not have
25578 emitted the member decl for this field. Emit it now. */
25579 if (TREE_CODE (decl) == FIELD_DECL)
25581 tree type = DECL_CONTEXT (decl);
25583 if (TYPE_CONTEXT (type)
25584 && TYPE_P (TYPE_CONTEXT (type))
25585 && !should_emit_struct_debug (TYPE_CONTEXT (type),
25586 DINFO_USAGE_DIR_USE))
25587 return;
25588 gen_type_die_for_member (type, decl,
25589 get_context_die (TYPE_CONTEXT (type)));
25591 if (TREE_CODE (decl) == NAMELIST_DECL)
25592 at_import_die = gen_namelist_decl (DECL_NAME (decl),
25593 get_context_die (DECL_CONTEXT (decl)),
25594 NULL_TREE);
25595 else
25596 at_import_die = force_decl_die (decl);
25600 if (TREE_CODE (decl) == NAMESPACE_DECL)
25602 if (dwarf_version >= 3 || !dwarf_strict)
25603 imported_die = new_die (DW_TAG_imported_module,
25604 lexical_block_die,
25605 lexical_block);
25606 else
25607 return;
25609 else
25610 imported_die = new_die (DW_TAG_imported_declaration,
25611 lexical_block_die,
25612 lexical_block);
25614 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
25615 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
25616 if (debug_column_info && xloc.column)
25617 add_AT_unsigned (imported_die, DW_AT_decl_column, xloc.column);
25618 if (name)
25619 add_AT_string (imported_die, DW_AT_name,
25620 IDENTIFIER_POINTER (name));
25621 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
25624 /* Output debug information for imported module or decl DECL.
25625 NAME is non-NULL name in context if the decl has been renamed.
25626 CHILD is true if decl is one of the renamed decls as part of
25627 importing whole module. */
25629 static void
25630 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
25631 bool child)
25633 /* dw_die_ref at_import_die; */
25634 dw_die_ref scope_die;
25636 if (debug_info_level <= DINFO_LEVEL_TERSE)
25637 return;
25639 gcc_assert (decl);
25641 set_early_dwarf s;
25643 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
25644 We need decl DIE for reference and scope die. First, get DIE for the decl
25645 itself. */
25647 /* Get the scope die for decl context. Use comp_unit_die for global module
25648 or decl. If die is not found for non globals, force new die. */
25649 if (context
25650 && TYPE_P (context)
25651 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
25652 return;
25654 scope_die = get_context_die (context);
25656 if (child)
25658 /* DW_TAG_imported_module was introduced in the DWARFv3 specification, so
25659 there is nothing we can do, here. */
25660 if (dwarf_version < 3 && dwarf_strict)
25661 return;
25663 gcc_assert (scope_die->die_child);
25664 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
25665 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
25666 scope_die = scope_die->die_child;
25669 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
25670 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
25673 /* Output debug information for namelists. */
25675 static dw_die_ref
25676 gen_namelist_decl (tree name, dw_die_ref scope_die, tree item_decls)
25678 dw_die_ref nml_die, nml_item_die, nml_item_ref_die;
25679 tree value;
25680 unsigned i;
25682 if (debug_info_level <= DINFO_LEVEL_TERSE)
25683 return NULL;
25685 gcc_assert (scope_die != NULL);
25686 nml_die = new_die (DW_TAG_namelist, scope_die, NULL);
25687 add_AT_string (nml_die, DW_AT_name, IDENTIFIER_POINTER (name));
25689 /* If there are no item_decls, we have a nondefining namelist, e.g.
25690 with USE association; hence, set DW_AT_declaration. */
25691 if (item_decls == NULL_TREE)
25693 add_AT_flag (nml_die, DW_AT_declaration, 1);
25694 return nml_die;
25697 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (item_decls), i, value)
25699 nml_item_ref_die = lookup_decl_die (value);
25700 if (!nml_item_ref_die)
25701 nml_item_ref_die = force_decl_die (value);
25703 nml_item_die = new_die (DW_TAG_namelist_item, nml_die, NULL);
25704 add_AT_die_ref (nml_item_die, DW_AT_namelist_items, nml_item_ref_die);
25706 return nml_die;
25710 /* Write the debugging output for DECL and return the DIE. */
25712 static void
25713 dwarf2out_decl (tree decl)
25715 dw_die_ref context_die = comp_unit_die ();
25717 switch (TREE_CODE (decl))
25719 case ERROR_MARK:
25720 return;
25722 case FUNCTION_DECL:
25723 /* What we would really like to do here is to filter out all mere
25724 file-scope declarations of file-scope functions which are never
25725 referenced later within this translation unit (and keep all of ones
25726 that *are* referenced later on) but we aren't clairvoyant, so we have
25727 no idea which functions will be referenced in the future (i.e. later
25728 on within the current translation unit). So here we just ignore all
25729 file-scope function declarations which are not also definitions. If
25730 and when the debugger needs to know something about these functions,
25731 it will have to hunt around and find the DWARF information associated
25732 with the definition of the function.
25734 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
25735 nodes represent definitions and which ones represent mere
25736 declarations. We have to check DECL_INITIAL instead. That's because
25737 the C front-end supports some weird semantics for "extern inline"
25738 function definitions. These can get inlined within the current
25739 translation unit (and thus, we need to generate Dwarf info for their
25740 abstract instances so that the Dwarf info for the concrete inlined
25741 instances can have something to refer to) but the compiler never
25742 generates any out-of-lines instances of such things (despite the fact
25743 that they *are* definitions).
25745 The important point is that the C front-end marks these "extern
25746 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
25747 them anyway. Note that the C++ front-end also plays some similar games
25748 for inline function definitions appearing within include files which
25749 also contain `#pragma interface' pragmas.
25751 If we are called from dwarf2out_abstract_function output a DIE
25752 anyway. We can end up here this way with early inlining and LTO
25753 where the inlined function is output in a different LTRANS unit
25754 or not at all. */
25755 if (DECL_INITIAL (decl) == NULL_TREE
25756 && ! DECL_ABSTRACT_P (decl))
25757 return;
25759 /* If we're a nested function, initially use a parent of NULL; if we're
25760 a plain function, this will be fixed up in decls_for_scope. If
25761 we're a method, it will be ignored, since we already have a DIE. */
25762 if (decl_function_context (decl)
25763 /* But if we're in terse mode, we don't care about scope. */
25764 && debug_info_level > DINFO_LEVEL_TERSE)
25765 context_die = NULL;
25766 break;
25768 case VAR_DECL:
25769 /* For local statics lookup proper context die. */
25770 if (local_function_static (decl))
25771 context_die = lookup_decl_die (DECL_CONTEXT (decl));
25773 /* If we are in terse mode, don't generate any DIEs to represent any
25774 variable declarations or definitions. */
25775 if (debug_info_level <= DINFO_LEVEL_TERSE)
25776 return;
25777 break;
25779 case CONST_DECL:
25780 if (debug_info_level <= DINFO_LEVEL_TERSE)
25781 return;
25782 if (!is_fortran () && !is_ada ())
25783 return;
25784 if (TREE_STATIC (decl) && decl_function_context (decl))
25785 context_die = lookup_decl_die (DECL_CONTEXT (decl));
25786 break;
25788 case NAMESPACE_DECL:
25789 case IMPORTED_DECL:
25790 if (debug_info_level <= DINFO_LEVEL_TERSE)
25791 return;
25792 if (lookup_decl_die (decl) != NULL)
25793 return;
25794 break;
25796 case TYPE_DECL:
25797 /* Don't emit stubs for types unless they are needed by other DIEs. */
25798 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
25799 return;
25801 /* Don't bother trying to generate any DIEs to represent any of the
25802 normal built-in types for the language we are compiling. */
25803 if (DECL_IS_BUILTIN (decl))
25804 return;
25806 /* If we are in terse mode, don't generate any DIEs for types. */
25807 if (debug_info_level <= DINFO_LEVEL_TERSE)
25808 return;
25810 /* If we're a function-scope tag, initially use a parent of NULL;
25811 this will be fixed up in decls_for_scope. */
25812 if (decl_function_context (decl))
25813 context_die = NULL;
25815 break;
25817 case NAMELIST_DECL:
25818 break;
25820 default:
25821 return;
25824 gen_decl_die (decl, NULL, NULL, context_die);
25826 if (flag_checking)
25828 dw_die_ref die = lookup_decl_die (decl);
25829 if (die)
25830 check_die (die);
25834 /* Write the debugging output for DECL. */
25836 static void
25837 dwarf2out_function_decl (tree decl)
25839 dwarf2out_decl (decl);
25840 call_arg_locations = NULL;
25841 call_arg_loc_last = NULL;
25842 call_site_count = -1;
25843 tail_call_site_count = -1;
25844 decl_loc_table->empty ();
25845 cached_dw_loc_list_table->empty ();
25848 /* Output a marker (i.e. a label) for the beginning of the generated code for
25849 a lexical block. */
25851 static void
25852 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
25853 unsigned int blocknum)
25855 switch_to_section (current_function_section ());
25856 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
25859 /* Output a marker (i.e. a label) for the end of the generated code for a
25860 lexical block. */
25862 static void
25863 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
25865 switch_to_section (current_function_section ());
25866 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
25869 /* Returns nonzero if it is appropriate not to emit any debugging
25870 information for BLOCK, because it doesn't contain any instructions.
25872 Don't allow this for blocks with nested functions or local classes
25873 as we would end up with orphans, and in the presence of scheduling
25874 we may end up calling them anyway. */
25876 static bool
25877 dwarf2out_ignore_block (const_tree block)
25879 tree decl;
25880 unsigned int i;
25882 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
25883 if (TREE_CODE (decl) == FUNCTION_DECL
25884 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
25885 return 0;
25886 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
25888 decl = BLOCK_NONLOCALIZED_VAR (block, i);
25889 if (TREE_CODE (decl) == FUNCTION_DECL
25890 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
25891 return 0;
25894 return 1;
25897 /* Hash table routines for file_hash. */
25899 bool
25900 dwarf_file_hasher::equal (dwarf_file_data *p1, const char *p2)
25902 return filename_cmp (p1->filename, p2) == 0;
25905 hashval_t
25906 dwarf_file_hasher::hash (dwarf_file_data *p)
25908 return htab_hash_string (p->filename);
25911 /* Lookup FILE_NAME (in the list of filenames that we know about here in
25912 dwarf2out.c) and return its "index". The index of each (known) filename is
25913 just a unique number which is associated with only that one filename. We
25914 need such numbers for the sake of generating labels (in the .debug_sfnames
25915 section) and references to those files numbers (in the .debug_srcinfo
25916 and .debug_macinfo sections). If the filename given as an argument is not
25917 found in our current list, add it to the list and assign it the next
25918 available unique index number. */
25920 static struct dwarf_file_data *
25921 lookup_filename (const char *file_name)
25923 struct dwarf_file_data * created;
25925 if (!file_name)
25926 return NULL;
25928 dwarf_file_data **slot
25929 = file_table->find_slot_with_hash (file_name, htab_hash_string (file_name),
25930 INSERT);
25931 if (*slot)
25932 return *slot;
25934 created = ggc_alloc<dwarf_file_data> ();
25935 created->filename = file_name;
25936 created->emitted_number = 0;
25937 *slot = created;
25938 return created;
25941 /* If the assembler will construct the file table, then translate the compiler
25942 internal file table number into the assembler file table number, and emit
25943 a .file directive if we haven't already emitted one yet. The file table
25944 numbers are different because we prune debug info for unused variables and
25945 types, which may include filenames. */
25947 static int
25948 maybe_emit_file (struct dwarf_file_data * fd)
25950 if (! fd->emitted_number)
25952 if (last_emitted_file)
25953 fd->emitted_number = last_emitted_file->emitted_number + 1;
25954 else
25955 fd->emitted_number = 1;
25956 last_emitted_file = fd;
25958 if (DWARF2_ASM_LINE_DEBUG_INFO)
25960 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
25961 output_quoted_string (asm_out_file,
25962 remap_debug_filename (fd->filename));
25963 fputc ('\n', asm_out_file);
25967 return fd->emitted_number;
25970 /* Schedule generation of a DW_AT_const_value attribute to DIE.
25971 That generation should happen after function debug info has been
25972 generated. The value of the attribute is the constant value of ARG. */
25974 static void
25975 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
25977 die_arg_entry entry;
25979 if (!die || !arg)
25980 return;
25982 gcc_assert (early_dwarf);
25984 if (!tmpl_value_parm_die_table)
25985 vec_alloc (tmpl_value_parm_die_table, 32);
25987 entry.die = die;
25988 entry.arg = arg;
25989 vec_safe_push (tmpl_value_parm_die_table, entry);
25992 /* Return TRUE if T is an instance of generic type, FALSE
25993 otherwise. */
25995 static bool
25996 generic_type_p (tree t)
25998 if (t == NULL_TREE || !TYPE_P (t))
25999 return false;
26000 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
26003 /* Schedule the generation of the generic parameter dies for the
26004 instance of generic type T. The proper generation itself is later
26005 done by gen_scheduled_generic_parms_dies. */
26007 static void
26008 schedule_generic_params_dies_gen (tree t)
26010 if (!generic_type_p (t))
26011 return;
26013 gcc_assert (early_dwarf);
26015 if (!generic_type_instances)
26016 vec_alloc (generic_type_instances, 256);
26018 vec_safe_push (generic_type_instances, t);
26021 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
26022 by append_entry_to_tmpl_value_parm_die_table. This function must
26023 be called after function DIEs have been generated. */
26025 static void
26026 gen_remaining_tmpl_value_param_die_attribute (void)
26028 if (tmpl_value_parm_die_table)
26030 unsigned i, j;
26031 die_arg_entry *e;
26033 /* We do this in two phases - first get the cases we can
26034 handle during early-finish, preserving those we cannot
26035 (containing symbolic constants where we don't yet know
26036 whether we are going to output the referenced symbols).
26037 For those we try again at late-finish. */
26038 j = 0;
26039 FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
26041 if (!tree_add_const_value_attribute (e->die, e->arg))
26043 dw_loc_descr_ref loc = NULL;
26044 if (! early_dwarf
26045 && (dwarf_version >= 5 || !dwarf_strict))
26046 loc = loc_descriptor_from_tree (e->arg, 2, NULL);
26047 if (loc)
26048 add_AT_loc (e->die, DW_AT_location, loc);
26049 else
26050 (*tmpl_value_parm_die_table)[j++] = *e;
26053 tmpl_value_parm_die_table->truncate (j);
26057 /* Generate generic parameters DIEs for instances of generic types
26058 that have been previously scheduled by
26059 schedule_generic_params_dies_gen. This function must be called
26060 after all the types of the CU have been laid out. */
26062 static void
26063 gen_scheduled_generic_parms_dies (void)
26065 unsigned i;
26066 tree t;
26068 if (!generic_type_instances)
26069 return;
26071 FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
26072 if (COMPLETE_TYPE_P (t))
26073 gen_generic_params_dies (t);
26075 generic_type_instances = NULL;
26079 /* Replace DW_AT_name for the decl with name. */
26081 static void
26082 dwarf2out_set_name (tree decl, tree name)
26084 dw_die_ref die;
26085 dw_attr_node *attr;
26086 const char *dname;
26088 die = TYPE_SYMTAB_DIE (decl);
26089 if (!die)
26090 return;
26092 dname = dwarf2_name (name, 0);
26093 if (!dname)
26094 return;
26096 attr = get_AT (die, DW_AT_name);
26097 if (attr)
26099 struct indirect_string_node *node;
26101 node = find_AT_string (dname);
26102 /* replace the string. */
26103 attr->dw_attr_val.v.val_str = node;
26106 else
26107 add_name_attribute (die, dname);
26110 /* True if before or during processing of the first function being emitted. */
26111 static bool in_first_function_p = true;
26112 /* True if loc_note during dwarf2out_var_location call might still be
26113 before first real instruction at address equal to .Ltext0. */
26114 static bool maybe_at_text_label_p = true;
26115 /* One above highest N where .LVLN label might be equal to .Ltext0 label. */
26116 static unsigned int first_loclabel_num_not_at_text_label;
26118 /* Called by the final INSN scan whenever we see a var location. We
26119 use it to drop labels in the right places, and throw the location in
26120 our lookup table. */
26122 static void
26123 dwarf2out_var_location (rtx_insn *loc_note)
26125 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
26126 struct var_loc_node *newloc;
26127 rtx_insn *next_real, *next_note;
26128 rtx_insn *call_insn = NULL;
26129 static const char *last_label;
26130 static const char *last_postcall_label;
26131 static bool last_in_cold_section_p;
26132 static rtx_insn *expected_next_loc_note;
26133 tree decl;
26134 bool var_loc_p;
26136 if (!NOTE_P (loc_note))
26138 if (CALL_P (loc_note))
26140 call_site_count++;
26141 if (SIBLING_CALL_P (loc_note))
26142 tail_call_site_count++;
26143 if (optimize == 0 && !flag_var_tracking)
26145 /* When the var-tracking pass is not running, there is no note
26146 for indirect calls whose target is compile-time known. In this
26147 case, process such calls specifically so that we generate call
26148 sites for them anyway. */
26149 rtx x = PATTERN (loc_note);
26150 if (GET_CODE (x) == PARALLEL)
26151 x = XVECEXP (x, 0, 0);
26152 if (GET_CODE (x) == SET)
26153 x = SET_SRC (x);
26154 if (GET_CODE (x) == CALL)
26155 x = XEXP (x, 0);
26156 if (!MEM_P (x)
26157 || GET_CODE (XEXP (x, 0)) != SYMBOL_REF
26158 || !SYMBOL_REF_DECL (XEXP (x, 0))
26159 || (TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0)))
26160 != FUNCTION_DECL))
26162 call_insn = loc_note;
26163 loc_note = NULL;
26164 var_loc_p = false;
26166 next_real = next_real_insn (call_insn);
26167 next_note = NULL;
26168 cached_next_real_insn = NULL;
26169 goto create_label;
26173 return;
26176 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
26177 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
26178 return;
26180 /* Optimize processing a large consecutive sequence of location
26181 notes so we don't spend too much time in next_real_insn. If the
26182 next insn is another location note, remember the next_real_insn
26183 calculation for next time. */
26184 next_real = cached_next_real_insn;
26185 if (next_real)
26187 if (expected_next_loc_note != loc_note)
26188 next_real = NULL;
26191 next_note = NEXT_INSN (loc_note);
26192 if (! next_note
26193 || next_note->deleted ()
26194 || ! NOTE_P (next_note)
26195 || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
26196 && NOTE_KIND (next_note) != NOTE_INSN_CALL_ARG_LOCATION))
26197 next_note = NULL;
26199 if (! next_real)
26200 next_real = next_real_insn (loc_note);
26202 if (next_note)
26204 expected_next_loc_note = next_note;
26205 cached_next_real_insn = next_real;
26207 else
26208 cached_next_real_insn = NULL;
26210 /* If there are no instructions which would be affected by this note,
26211 don't do anything. */
26212 if (var_loc_p
26213 && next_real == NULL_RTX
26214 && !NOTE_DURING_CALL_P (loc_note))
26215 return;
26217 create_label:
26219 if (next_real == NULL_RTX)
26220 next_real = get_last_insn ();
26222 /* If there were any real insns between note we processed last time
26223 and this note (or if it is the first note), clear
26224 last_{,postcall_}label so that they are not reused this time. */
26225 if (last_var_location_insn == NULL_RTX
26226 || last_var_location_insn != next_real
26227 || last_in_cold_section_p != in_cold_section_p)
26229 last_label = NULL;
26230 last_postcall_label = NULL;
26233 if (var_loc_p)
26235 decl = NOTE_VAR_LOCATION_DECL (loc_note);
26236 newloc = add_var_loc_to_decl (decl, loc_note,
26237 NOTE_DURING_CALL_P (loc_note)
26238 ? last_postcall_label : last_label);
26239 if (newloc == NULL)
26240 return;
26242 else
26244 decl = NULL_TREE;
26245 newloc = NULL;
26248 /* If there were no real insns between note we processed last time
26249 and this note, use the label we emitted last time. Otherwise
26250 create a new label and emit it. */
26251 if (last_label == NULL)
26253 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
26254 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
26255 loclabel_num++;
26256 last_label = ggc_strdup (loclabel);
26257 /* See if loclabel might be equal to .Ltext0. If yes,
26258 bump first_loclabel_num_not_at_text_label. */
26259 if (!have_multiple_function_sections
26260 && in_first_function_p
26261 && maybe_at_text_label_p)
26263 static rtx_insn *last_start;
26264 rtx_insn *insn;
26265 for (insn = loc_note; insn; insn = previous_insn (insn))
26266 if (insn == last_start)
26267 break;
26268 else if (!NONDEBUG_INSN_P (insn))
26269 continue;
26270 else
26272 rtx body = PATTERN (insn);
26273 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
26274 continue;
26275 /* Inline asm could occupy zero bytes. */
26276 else if (GET_CODE (body) == ASM_INPUT
26277 || asm_noperands (body) >= 0)
26278 continue;
26279 #ifdef HAVE_attr_length
26280 else if (get_attr_min_length (insn) == 0)
26281 continue;
26282 #endif
26283 else
26285 /* Assume insn has non-zero length. */
26286 maybe_at_text_label_p = false;
26287 break;
26290 if (maybe_at_text_label_p)
26292 last_start = loc_note;
26293 first_loclabel_num_not_at_text_label = loclabel_num;
26298 gcc_assert ((loc_note == NULL_RTX && call_insn != NULL_RTX)
26299 || (loc_note != NULL_RTX && call_insn == NULL_RTX));
26301 if (!var_loc_p)
26303 struct call_arg_loc_node *ca_loc
26304 = ggc_cleared_alloc<call_arg_loc_node> ();
26305 rtx_insn *prev
26306 = loc_note != NULL_RTX ? prev_real_insn (loc_note) : call_insn;
26308 ca_loc->call_arg_loc_note = loc_note;
26309 ca_loc->next = NULL;
26310 ca_loc->label = last_label;
26311 gcc_assert (prev
26312 && (CALL_P (prev)
26313 || (NONJUMP_INSN_P (prev)
26314 && GET_CODE (PATTERN (prev)) == SEQUENCE
26315 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
26316 if (!CALL_P (prev))
26317 prev = as_a <rtx_sequence *> (PATTERN (prev))->insn (0);
26318 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
26320 /* Look for a SYMBOL_REF in the "prev" instruction. */
26321 rtx x = get_call_rtx_from (PATTERN (prev));
26322 if (x)
26324 /* Try to get the call symbol, if any. */
26325 if (MEM_P (XEXP (x, 0)))
26326 x = XEXP (x, 0);
26327 /* First, look for a memory access to a symbol_ref. */
26328 if (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
26329 && SYMBOL_REF_DECL (XEXP (x, 0))
26330 && TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0))) == FUNCTION_DECL)
26331 ca_loc->symbol_ref = XEXP (x, 0);
26332 /* Otherwise, look at a compile-time known user-level function
26333 declaration. */
26334 else if (MEM_P (x)
26335 && MEM_EXPR (x)
26336 && TREE_CODE (MEM_EXPR (x)) == FUNCTION_DECL)
26337 ca_loc->symbol_ref = XEXP (DECL_RTL (MEM_EXPR (x)), 0);
26340 ca_loc->block = insn_scope (prev);
26341 if (call_arg_locations)
26342 call_arg_loc_last->next = ca_loc;
26343 else
26344 call_arg_locations = ca_loc;
26345 call_arg_loc_last = ca_loc;
26347 else if (loc_note != NULL_RTX && !NOTE_DURING_CALL_P (loc_note))
26348 newloc->label = last_label;
26349 else
26351 if (!last_postcall_label)
26353 sprintf (loclabel, "%s-1", last_label);
26354 last_postcall_label = ggc_strdup (loclabel);
26356 newloc->label = last_postcall_label;
26359 last_var_location_insn = next_real;
26360 last_in_cold_section_p = in_cold_section_p;
26363 /* Called from finalize_size_functions for size functions so that their body
26364 can be encoded in the debug info to describe the layout of variable-length
26365 structures. */
26367 static void
26368 dwarf2out_size_function (tree decl)
26370 function_to_dwarf_procedure (decl);
26373 /* Note in one location list that text section has changed. */
26376 var_location_switch_text_section_1 (var_loc_list **slot, void *)
26378 var_loc_list *list = *slot;
26379 if (list->first)
26380 list->last_before_switch
26381 = list->last->next ? list->last->next : list->last;
26382 return 1;
26385 /* Note in all location lists that text section has changed. */
26387 static void
26388 var_location_switch_text_section (void)
26390 if (decl_loc_table == NULL)
26391 return;
26393 decl_loc_table->traverse<void *, var_location_switch_text_section_1> (NULL);
26396 /* Create a new line number table. */
26398 static dw_line_info_table *
26399 new_line_info_table (void)
26401 dw_line_info_table *table;
26403 table = ggc_cleared_alloc<dw_line_info_table> ();
26404 table->file_num = 1;
26405 table->line_num = 1;
26406 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
26408 return table;
26411 /* Lookup the "current" table into which we emit line info, so
26412 that we don't have to do it for every source line. */
26414 static void
26415 set_cur_line_info_table (section *sec)
26417 dw_line_info_table *table;
26419 if (sec == text_section)
26420 table = text_section_line_info;
26421 else if (sec == cold_text_section)
26423 table = cold_text_section_line_info;
26424 if (!table)
26426 cold_text_section_line_info = table = new_line_info_table ();
26427 table->end_label = cold_end_label;
26430 else
26432 const char *end_label;
26434 if (flag_reorder_blocks_and_partition)
26436 if (in_cold_section_p)
26437 end_label = crtl->subsections.cold_section_end_label;
26438 else
26439 end_label = crtl->subsections.hot_section_end_label;
26441 else
26443 char label[MAX_ARTIFICIAL_LABEL_BYTES];
26444 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
26445 current_function_funcdef_no);
26446 end_label = ggc_strdup (label);
26449 table = new_line_info_table ();
26450 table->end_label = end_label;
26452 vec_safe_push (separate_line_info, table);
26455 if (DWARF2_ASM_LINE_DEBUG_INFO)
26456 table->is_stmt = (cur_line_info_table
26457 ? cur_line_info_table->is_stmt
26458 : DWARF_LINE_DEFAULT_IS_STMT_START);
26459 cur_line_info_table = table;
26463 /* We need to reset the locations at the beginning of each
26464 function. We can't do this in the end_function hook, because the
26465 declarations that use the locations won't have been output when
26466 that hook is called. Also compute have_multiple_function_sections here. */
26468 static void
26469 dwarf2out_begin_function (tree fun)
26471 section *sec = function_section (fun);
26473 if (sec != text_section)
26474 have_multiple_function_sections = true;
26476 if (flag_reorder_blocks_and_partition && !cold_text_section)
26478 gcc_assert (current_function_decl == fun);
26479 cold_text_section = unlikely_text_section ();
26480 switch_to_section (cold_text_section);
26481 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
26482 switch_to_section (sec);
26485 dwarf2out_note_section_used ();
26486 call_site_count = 0;
26487 tail_call_site_count = 0;
26489 set_cur_line_info_table (sec);
26492 /* Helper function of dwarf2out_end_function, called only after emitting
26493 the very first function into assembly. Check if some .debug_loc range
26494 might end with a .LVL* label that could be equal to .Ltext0.
26495 In that case we must force using absolute addresses in .debug_loc ranges,
26496 because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
26497 .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
26498 list terminator.
26499 Set have_multiple_function_sections to true in that case and
26500 terminate htab traversal. */
26503 find_empty_loc_ranges_at_text_label (var_loc_list **slot, int)
26505 var_loc_list *entry = *slot;
26506 struct var_loc_node *node;
26508 node = entry->first;
26509 if (node && node->next && node->next->label)
26511 unsigned int i;
26512 const char *label = node->next->label;
26513 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
26515 for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
26517 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
26518 if (strcmp (label, loclabel) == 0)
26520 have_multiple_function_sections = true;
26521 return 0;
26525 return 1;
26528 /* Hook called after emitting a function into assembly.
26529 This does something only for the very first function emitted. */
26531 static void
26532 dwarf2out_end_function (unsigned int)
26534 if (in_first_function_p
26535 && !have_multiple_function_sections
26536 && first_loclabel_num_not_at_text_label
26537 && decl_loc_table)
26538 decl_loc_table->traverse<int, find_empty_loc_ranges_at_text_label> (0);
26539 in_first_function_p = false;
26540 maybe_at_text_label_p = false;
26543 /* Temporary holder for dwarf2out_register_main_translation_unit. Used to let
26544 front-ends register a translation unit even before dwarf2out_init is
26545 called. */
26546 static tree main_translation_unit = NULL_TREE;
26548 /* Hook called by front-ends after they built their main translation unit.
26549 Associate comp_unit_die to UNIT. */
26551 static void
26552 dwarf2out_register_main_translation_unit (tree unit)
26554 gcc_assert (TREE_CODE (unit) == TRANSLATION_UNIT_DECL
26555 && main_translation_unit == NULL_TREE);
26556 main_translation_unit = unit;
26557 /* If dwarf2out_init has not been called yet, it will perform the association
26558 itself looking at main_translation_unit. */
26559 if (decl_die_table != NULL)
26560 equate_decl_number_to_die (unit, comp_unit_die ());
26563 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
26565 static void
26566 push_dw_line_info_entry (dw_line_info_table *table,
26567 enum dw_line_info_opcode opcode, unsigned int val)
26569 dw_line_info_entry e;
26570 e.opcode = opcode;
26571 e.val = val;
26572 vec_safe_push (table->entries, e);
26575 /* Output a label to mark the beginning of a source code line entry
26576 and record information relating to this source line, in
26577 'line_info_table' for later output of the .debug_line section. */
26578 /* ??? The discriminator parameter ought to be unsigned. */
26580 static void
26581 dwarf2out_source_line (unsigned int line, unsigned int column,
26582 const char *filename,
26583 int discriminator, bool is_stmt)
26585 unsigned int file_num;
26586 dw_line_info_table *table;
26588 if (debug_info_level < DINFO_LEVEL_TERSE || line == 0)
26589 return;
26591 /* The discriminator column was added in dwarf4. Simplify the below
26592 by simply removing it if we're not supposed to output it. */
26593 if (dwarf_version < 4 && dwarf_strict)
26594 discriminator = 0;
26596 if (!debug_column_info)
26597 column = 0;
26599 table = cur_line_info_table;
26600 file_num = maybe_emit_file (lookup_filename (filename));
26602 /* ??? TODO: Elide duplicate line number entries. Traditionally,
26603 the debugger has used the second (possibly duplicate) line number
26604 at the beginning of the function to mark the end of the prologue.
26605 We could eliminate any other duplicates within the function. For
26606 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
26607 that second line number entry. */
26608 /* Recall that this end-of-prologue indication is *not* the same thing
26609 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
26610 to which the hook corresponds, follows the last insn that was
26611 emitted by gen_prologue. What we need is to precede the first insn
26612 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
26613 insn that corresponds to something the user wrote. These may be
26614 very different locations once scheduling is enabled. */
26616 if (0 && file_num == table->file_num
26617 && line == table->line_num
26618 && column == table->column_num
26619 && discriminator == table->discrim_num
26620 && is_stmt == table->is_stmt)
26621 return;
26623 switch_to_section (current_function_section ());
26625 /* If requested, emit something human-readable. */
26626 if (flag_debug_asm)
26628 if (debug_column_info)
26629 fprintf (asm_out_file, "\t%s %s:%d:%d\n", ASM_COMMENT_START,
26630 filename, line, column);
26631 else
26632 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
26633 filename, line);
26636 if (DWARF2_ASM_LINE_DEBUG_INFO)
26638 /* Emit the .loc directive understood by GNU as. */
26639 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
26640 file_num, line, is_stmt, discriminator */
26641 fputs ("\t.loc ", asm_out_file);
26642 fprint_ul (asm_out_file, file_num);
26643 putc (' ', asm_out_file);
26644 fprint_ul (asm_out_file, line);
26645 putc (' ', asm_out_file);
26646 if (debug_column_info)
26647 fprint_ul (asm_out_file, column);
26648 else
26649 putc ('0', asm_out_file);
26651 if (is_stmt != table->is_stmt)
26653 fputs (" is_stmt ", asm_out_file);
26654 putc (is_stmt ? '1' : '0', asm_out_file);
26656 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
26658 gcc_assert (discriminator > 0);
26659 fputs (" discriminator ", asm_out_file);
26660 fprint_ul (asm_out_file, (unsigned long) discriminator);
26662 putc ('\n', asm_out_file);
26664 else
26666 unsigned int label_num = ++line_info_label_num;
26668 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
26670 push_dw_line_info_entry (table, LI_set_address, label_num);
26671 if (file_num != table->file_num)
26672 push_dw_line_info_entry (table, LI_set_file, file_num);
26673 if (discriminator != table->discrim_num)
26674 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
26675 if (is_stmt != table->is_stmt)
26676 push_dw_line_info_entry (table, LI_negate_stmt, 0);
26677 push_dw_line_info_entry (table, LI_set_line, line);
26678 if (debug_column_info)
26679 push_dw_line_info_entry (table, LI_set_column, column);
26682 table->file_num = file_num;
26683 table->line_num = line;
26684 table->column_num = column;
26685 table->discrim_num = discriminator;
26686 table->is_stmt = is_stmt;
26687 table->in_use = true;
26690 /* Record the beginning of a new source file. */
26692 static void
26693 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
26695 if (flag_eliminate_dwarf2_dups)
26697 /* Record the beginning of the file for break_out_includes. */
26698 dw_die_ref bincl_die;
26700 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
26701 add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
26704 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
26706 macinfo_entry e;
26707 e.code = DW_MACINFO_start_file;
26708 e.lineno = lineno;
26709 e.info = ggc_strdup (filename);
26710 vec_safe_push (macinfo_table, e);
26714 /* Record the end of a source file. */
26716 static void
26717 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
26719 if (flag_eliminate_dwarf2_dups)
26720 /* Record the end of the file for break_out_includes. */
26721 new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
26723 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
26725 macinfo_entry e;
26726 e.code = DW_MACINFO_end_file;
26727 e.lineno = lineno;
26728 e.info = NULL;
26729 vec_safe_push (macinfo_table, e);
26733 /* Called from debug_define in toplev.c. The `buffer' parameter contains
26734 the tail part of the directive line, i.e. the part which is past the
26735 initial whitespace, #, whitespace, directive-name, whitespace part. */
26737 static void
26738 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
26739 const char *buffer ATTRIBUTE_UNUSED)
26741 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
26743 macinfo_entry e;
26744 /* Insert a dummy first entry to be able to optimize the whole
26745 predefined macro block using DW_MACRO_import. */
26746 if (macinfo_table->is_empty () && lineno <= 1)
26748 e.code = 0;
26749 e.lineno = 0;
26750 e.info = NULL;
26751 vec_safe_push (macinfo_table, e);
26753 e.code = DW_MACINFO_define;
26754 e.lineno = lineno;
26755 e.info = ggc_strdup (buffer);
26756 vec_safe_push (macinfo_table, e);
26760 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
26761 the tail part of the directive line, i.e. the part which is past the
26762 initial whitespace, #, whitespace, directive-name, whitespace part. */
26764 static void
26765 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
26766 const char *buffer ATTRIBUTE_UNUSED)
26768 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
26770 macinfo_entry e;
26771 /* Insert a dummy first entry to be able to optimize the whole
26772 predefined macro block using DW_MACRO_import. */
26773 if (macinfo_table->is_empty () && lineno <= 1)
26775 e.code = 0;
26776 e.lineno = 0;
26777 e.info = NULL;
26778 vec_safe_push (macinfo_table, e);
26780 e.code = DW_MACINFO_undef;
26781 e.lineno = lineno;
26782 e.info = ggc_strdup (buffer);
26783 vec_safe_push (macinfo_table, e);
26787 /* Helpers to manipulate hash table of CUs. */
26789 struct macinfo_entry_hasher : nofree_ptr_hash <macinfo_entry>
26791 static inline hashval_t hash (const macinfo_entry *);
26792 static inline bool equal (const macinfo_entry *, const macinfo_entry *);
26795 inline hashval_t
26796 macinfo_entry_hasher::hash (const macinfo_entry *entry)
26798 return htab_hash_string (entry->info);
26801 inline bool
26802 macinfo_entry_hasher::equal (const macinfo_entry *entry1,
26803 const macinfo_entry *entry2)
26805 return !strcmp (entry1->info, entry2->info);
26808 typedef hash_table<macinfo_entry_hasher> macinfo_hash_type;
26810 /* Output a single .debug_macinfo entry. */
26812 static void
26813 output_macinfo_op (macinfo_entry *ref)
26815 int file_num;
26816 size_t len;
26817 struct indirect_string_node *node;
26818 char label[MAX_ARTIFICIAL_LABEL_BYTES];
26819 struct dwarf_file_data *fd;
26821 switch (ref->code)
26823 case DW_MACINFO_start_file:
26824 fd = lookup_filename (ref->info);
26825 file_num = maybe_emit_file (fd);
26826 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
26827 dw2_asm_output_data_uleb128 (ref->lineno,
26828 "Included from line number %lu",
26829 (unsigned long) ref->lineno);
26830 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
26831 break;
26832 case DW_MACINFO_end_file:
26833 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
26834 break;
26835 case DW_MACINFO_define:
26836 case DW_MACINFO_undef:
26837 len = strlen (ref->info) + 1;
26838 if (!dwarf_strict
26839 && len > DWARF_OFFSET_SIZE
26840 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
26841 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
26843 ref->code = ref->code == DW_MACINFO_define
26844 ? DW_MACRO_define_strp : DW_MACRO_undef_strp;
26845 output_macinfo_op (ref);
26846 return;
26848 dw2_asm_output_data (1, ref->code,
26849 ref->code == DW_MACINFO_define
26850 ? "Define macro" : "Undefine macro");
26851 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
26852 (unsigned long) ref->lineno);
26853 dw2_asm_output_nstring (ref->info, -1, "The macro");
26854 break;
26855 case DW_MACRO_define_strp:
26856 case DW_MACRO_undef_strp:
26857 node = find_AT_string (ref->info);
26858 gcc_assert (node
26859 && (node->form == DW_FORM_strp
26860 || node->form == DW_FORM_GNU_str_index));
26861 dw2_asm_output_data (1, ref->code,
26862 ref->code == DW_MACRO_define_strp
26863 ? "Define macro strp"
26864 : "Undefine macro strp");
26865 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
26866 (unsigned long) ref->lineno);
26867 if (node->form == DW_FORM_strp)
26868 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
26869 debug_str_section, "The macro: \"%s\"",
26870 ref->info);
26871 else
26872 dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
26873 ref->info);
26874 break;
26875 case DW_MACRO_import:
26876 dw2_asm_output_data (1, ref->code, "Import");
26877 ASM_GENERATE_INTERNAL_LABEL (label,
26878 DEBUG_MACRO_SECTION_LABEL, ref->lineno);
26879 dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
26880 break;
26881 default:
26882 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
26883 ASM_COMMENT_START, (unsigned long) ref->code);
26884 break;
26888 /* Attempt to make a sequence of define/undef macinfo ops shareable with
26889 other compilation unit .debug_macinfo sections. IDX is the first
26890 index of a define/undef, return the number of ops that should be
26891 emitted in a comdat .debug_macinfo section and emit
26892 a DW_MACRO_import entry referencing it.
26893 If the define/undef entry should be emitted normally, return 0. */
26895 static unsigned
26896 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
26897 macinfo_hash_type **macinfo_htab)
26899 macinfo_entry *first, *second, *cur, *inc;
26900 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
26901 unsigned char checksum[16];
26902 struct md5_ctx ctx;
26903 char *grp_name, *tail;
26904 const char *base;
26905 unsigned int i, count, encoded_filename_len, linebuf_len;
26906 macinfo_entry **slot;
26908 first = &(*macinfo_table)[idx];
26909 second = &(*macinfo_table)[idx + 1];
26911 /* Optimize only if there are at least two consecutive define/undef ops,
26912 and either all of them are before first DW_MACINFO_start_file
26913 with lineno {0,1} (i.e. predefined macro block), or all of them are
26914 in some included header file. */
26915 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
26916 return 0;
26917 if (vec_safe_is_empty (files))
26919 if (first->lineno > 1 || second->lineno > 1)
26920 return 0;
26922 else if (first->lineno == 0)
26923 return 0;
26925 /* Find the last define/undef entry that can be grouped together
26926 with first and at the same time compute md5 checksum of their
26927 codes, linenumbers and strings. */
26928 md5_init_ctx (&ctx);
26929 for (i = idx; macinfo_table->iterate (i, &cur); i++)
26930 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
26931 break;
26932 else if (vec_safe_is_empty (files) && cur->lineno > 1)
26933 break;
26934 else
26936 unsigned char code = cur->code;
26937 md5_process_bytes (&code, 1, &ctx);
26938 checksum_uleb128 (cur->lineno, &ctx);
26939 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
26941 md5_finish_ctx (&ctx, checksum);
26942 count = i - idx;
26944 /* From the containing include filename (if any) pick up just
26945 usable characters from its basename. */
26946 if (vec_safe_is_empty (files))
26947 base = "";
26948 else
26949 base = lbasename (files->last ().info);
26950 for (encoded_filename_len = 0, i = 0; base[i]; i++)
26951 if (ISIDNUM (base[i]) || base[i] == '.')
26952 encoded_filename_len++;
26953 /* Count . at the end. */
26954 if (encoded_filename_len)
26955 encoded_filename_len++;
26957 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
26958 linebuf_len = strlen (linebuf);
26960 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
26961 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
26962 + 16 * 2 + 1);
26963 memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
26964 tail = grp_name + 4;
26965 if (encoded_filename_len)
26967 for (i = 0; base[i]; i++)
26968 if (ISIDNUM (base[i]) || base[i] == '.')
26969 *tail++ = base[i];
26970 *tail++ = '.';
26972 memcpy (tail, linebuf, linebuf_len);
26973 tail += linebuf_len;
26974 *tail++ = '.';
26975 for (i = 0; i < 16; i++)
26976 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
26978 /* Construct a macinfo_entry for DW_MACRO_import
26979 in the empty vector entry before the first define/undef. */
26980 inc = &(*macinfo_table)[idx - 1];
26981 inc->code = DW_MACRO_import;
26982 inc->lineno = 0;
26983 inc->info = ggc_strdup (grp_name);
26984 if (!*macinfo_htab)
26985 *macinfo_htab = new macinfo_hash_type (10);
26986 /* Avoid emitting duplicates. */
26987 slot = (*macinfo_htab)->find_slot (inc, INSERT);
26988 if (*slot != NULL)
26990 inc->code = 0;
26991 inc->info = NULL;
26992 /* If such an entry has been used before, just emit
26993 a DW_MACRO_import op. */
26994 inc = *slot;
26995 output_macinfo_op (inc);
26996 /* And clear all macinfo_entry in the range to avoid emitting them
26997 in the second pass. */
26998 for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
27000 cur->code = 0;
27001 cur->info = NULL;
27004 else
27006 *slot = inc;
27007 inc->lineno = (*macinfo_htab)->elements ();
27008 output_macinfo_op (inc);
27010 return count;
27013 /* Save any strings needed by the macinfo table in the debug str
27014 table. All strings must be collected into the table by the time
27015 index_string is called. */
27017 static void
27018 save_macinfo_strings (void)
27020 unsigned len;
27021 unsigned i;
27022 macinfo_entry *ref;
27024 for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
27026 switch (ref->code)
27028 /* Match the logic in output_macinfo_op to decide on
27029 indirect strings. */
27030 case DW_MACINFO_define:
27031 case DW_MACINFO_undef:
27032 len = strlen (ref->info) + 1;
27033 if (!dwarf_strict
27034 && len > DWARF_OFFSET_SIZE
27035 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
27036 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
27037 set_indirect_string (find_AT_string (ref->info));
27038 break;
27039 case DW_MACRO_define_strp:
27040 case DW_MACRO_undef_strp:
27041 set_indirect_string (find_AT_string (ref->info));
27042 break;
27043 default:
27044 break;
27049 /* Output macinfo section(s). */
27051 static void
27052 output_macinfo (void)
27054 unsigned i;
27055 unsigned long length = vec_safe_length (macinfo_table);
27056 macinfo_entry *ref;
27057 vec<macinfo_entry, va_gc> *files = NULL;
27058 macinfo_hash_type *macinfo_htab = NULL;
27060 if (! length)
27061 return;
27063 /* output_macinfo* uses these interchangeably. */
27064 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_define
27065 && (int) DW_MACINFO_undef == (int) DW_MACRO_undef
27066 && (int) DW_MACINFO_start_file == (int) DW_MACRO_start_file
27067 && (int) DW_MACINFO_end_file == (int) DW_MACRO_end_file);
27069 /* For .debug_macro emit the section header. */
27070 if (!dwarf_strict || dwarf_version >= 5)
27072 dw2_asm_output_data (2, dwarf_version >= 5 ? 5 : 4,
27073 "DWARF macro version number");
27074 if (DWARF_OFFSET_SIZE == 8)
27075 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
27076 else
27077 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
27078 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
27079 (!dwarf_split_debug_info ? debug_line_section_label
27080 : debug_skeleton_line_section_label),
27081 debug_line_section, NULL);
27084 /* In the first loop, it emits the primary .debug_macinfo section
27085 and after each emitted op the macinfo_entry is cleared.
27086 If a longer range of define/undef ops can be optimized using
27087 DW_MACRO_import, the DW_MACRO_import op is emitted and kept in
27088 the vector before the first define/undef in the range and the
27089 whole range of define/undef ops is not emitted and kept. */
27090 for (i = 0; macinfo_table->iterate (i, &ref); i++)
27092 switch (ref->code)
27094 case DW_MACINFO_start_file:
27095 vec_safe_push (files, *ref);
27096 break;
27097 case DW_MACINFO_end_file:
27098 if (!vec_safe_is_empty (files))
27099 files->pop ();
27100 break;
27101 case DW_MACINFO_define:
27102 case DW_MACINFO_undef:
27103 if ((!dwarf_strict || dwarf_version >= 5)
27104 && HAVE_COMDAT_GROUP
27105 && vec_safe_length (files) != 1
27106 && i > 0
27107 && i + 1 < length
27108 && (*macinfo_table)[i - 1].code == 0)
27110 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
27111 if (count)
27113 i += count - 1;
27114 continue;
27117 break;
27118 case 0:
27119 /* A dummy entry may be inserted at the beginning to be able
27120 to optimize the whole block of predefined macros. */
27121 if (i == 0)
27122 continue;
27123 default:
27124 break;
27126 output_macinfo_op (ref);
27127 ref->info = NULL;
27128 ref->code = 0;
27131 if (!macinfo_htab)
27132 return;
27134 delete macinfo_htab;
27135 macinfo_htab = NULL;
27137 /* If any DW_MACRO_import were used, on those DW_MACRO_import entries
27138 terminate the current chain and switch to a new comdat .debug_macinfo
27139 section and emit the define/undef entries within it. */
27140 for (i = 0; macinfo_table->iterate (i, &ref); i++)
27141 switch (ref->code)
27143 case 0:
27144 continue;
27145 case DW_MACRO_import:
27147 char label[MAX_ARTIFICIAL_LABEL_BYTES];
27148 tree comdat_key = get_identifier (ref->info);
27149 /* Terminate the previous .debug_macinfo section. */
27150 dw2_asm_output_data (1, 0, "End compilation unit");
27151 targetm.asm_out.named_section (debug_macinfo_section_name,
27152 SECTION_DEBUG
27153 | SECTION_LINKONCE,
27154 comdat_key);
27155 ASM_GENERATE_INTERNAL_LABEL (label,
27156 DEBUG_MACRO_SECTION_LABEL,
27157 ref->lineno);
27158 ASM_OUTPUT_LABEL (asm_out_file, label);
27159 ref->code = 0;
27160 ref->info = NULL;
27161 dw2_asm_output_data (2, dwarf_version >= 5 ? 5 : 4,
27162 "DWARF macro version number");
27163 if (DWARF_OFFSET_SIZE == 8)
27164 dw2_asm_output_data (1, 1, "Flags: 64-bit");
27165 else
27166 dw2_asm_output_data (1, 0, "Flags: 32-bit");
27168 break;
27169 case DW_MACINFO_define:
27170 case DW_MACINFO_undef:
27171 output_macinfo_op (ref);
27172 ref->code = 0;
27173 ref->info = NULL;
27174 break;
27175 default:
27176 gcc_unreachable ();
27180 /* Initialize the various sections and labels for dwarf output. */
27182 static void
27183 init_sections_and_labels (void)
27185 if (!dwarf_split_debug_info)
27187 debug_info_section = get_section (DEBUG_INFO_SECTION,
27188 SECTION_DEBUG, NULL);
27189 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
27190 SECTION_DEBUG, NULL);
27191 debug_loc_section = get_section (dwarf_version >= 5
27192 ? DEBUG_LOCLISTS_SECTION
27193 : DEBUG_LOC_SECTION,
27194 SECTION_DEBUG, NULL);
27195 debug_macinfo_section_name
27196 = (dwarf_strict && dwarf_version < 5)
27197 ? DEBUG_MACINFO_SECTION : DEBUG_MACRO_SECTION;
27198 debug_macinfo_section = get_section (debug_macinfo_section_name,
27199 SECTION_DEBUG, NULL);
27201 else
27203 debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
27204 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
27205 debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
27206 SECTION_DEBUG | SECTION_EXCLUDE,
27207 NULL);
27208 debug_addr_section = get_section (DEBUG_ADDR_SECTION,
27209 SECTION_DEBUG, NULL);
27210 debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
27211 SECTION_DEBUG, NULL);
27212 debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
27213 SECTION_DEBUG, NULL);
27214 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
27215 DEBUG_SKELETON_ABBREV_SECTION_LABEL, 0);
27217 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections stay in
27218 the main .o, but the skeleton_line goes into the split off dwo. */
27219 debug_skeleton_line_section
27220 = get_section (DEBUG_DWO_LINE_SECTION,
27221 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
27222 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
27223 DEBUG_SKELETON_LINE_SECTION_LABEL, 0);
27224 debug_str_offsets_section = get_section (DEBUG_DWO_STR_OFFSETS_SECTION,
27225 SECTION_DEBUG | SECTION_EXCLUDE,
27226 NULL);
27227 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
27228 DEBUG_SKELETON_INFO_SECTION_LABEL, 0);
27229 debug_loc_section = get_section (dwarf_version >= 5
27230 ? DEBUG_DWO_LOCLISTS_SECTION
27231 : DEBUG_DWO_LOC_SECTION,
27232 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
27233 debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
27234 DEBUG_STR_DWO_SECTION_FLAGS, NULL);
27235 debug_macinfo_section_name
27236 = (dwarf_strict && dwarf_version < 5)
27237 ? DEBUG_DWO_MACINFO_SECTION : DEBUG_DWO_MACRO_SECTION;
27238 debug_macinfo_section = get_section (debug_macinfo_section_name,
27239 SECTION_DEBUG | SECTION_EXCLUDE,
27240 NULL);
27242 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
27243 SECTION_DEBUG, NULL);
27244 debug_line_section = get_section (DEBUG_LINE_SECTION,
27245 SECTION_DEBUG, NULL);
27246 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
27247 SECTION_DEBUG, NULL);
27248 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
27249 SECTION_DEBUG, NULL);
27250 debug_str_section = get_section (DEBUG_STR_SECTION,
27251 DEBUG_STR_SECTION_FLAGS, NULL);
27252 if (!dwarf_split_debug_info && !DWARF2_ASM_LINE_DEBUG_INFO)
27253 debug_line_str_section = get_section (DEBUG_LINE_STR_SECTION,
27254 DEBUG_STR_SECTION_FLAGS, NULL);
27256 debug_ranges_section = get_section (dwarf_version >= 5
27257 ? DEBUG_RNGLISTS_SECTION
27258 : DEBUG_RANGES_SECTION,
27259 SECTION_DEBUG, NULL);
27260 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
27261 SECTION_DEBUG, NULL);
27263 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
27264 DEBUG_ABBREV_SECTION_LABEL, 0);
27265 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
27266 DEBUG_INFO_SECTION_LABEL, 0);
27267 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
27268 DEBUG_LINE_SECTION_LABEL, 0);
27269 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
27270 DEBUG_RANGES_SECTION_LABEL, 0);
27271 if (dwarf_version >= 5 && dwarf_split_debug_info)
27272 ASM_GENERATE_INTERNAL_LABEL (ranges_base_label,
27273 DEBUG_RANGES_SECTION_LABEL, 1);
27274 ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
27275 DEBUG_ADDR_SECTION_LABEL, 0);
27276 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
27277 (dwarf_strict && dwarf_version < 5)
27278 ? DEBUG_MACINFO_SECTION_LABEL
27279 : DEBUG_MACRO_SECTION_LABEL, 0);
27280 ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL, 0);
27283 /* Set up for Dwarf output at the start of compilation. */
27285 static void
27286 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
27288 /* This option is currently broken, see (PR53118 and PR46102). */
27289 if (flag_eliminate_dwarf2_dups
27290 && strstr (lang_hooks.name, "C++"))
27292 warning (0, "-feliminate-dwarf2-dups is broken for C++, ignoring");
27293 flag_eliminate_dwarf2_dups = 0;
27296 /* Allocate the file_table. */
27297 file_table = hash_table<dwarf_file_hasher>::create_ggc (50);
27299 #ifndef DWARF2_LINENO_DEBUGGING_INFO
27300 /* Allocate the decl_die_table. */
27301 decl_die_table = hash_table<decl_die_hasher>::create_ggc (10);
27303 /* Allocate the decl_loc_table. */
27304 decl_loc_table = hash_table<decl_loc_hasher>::create_ggc (10);
27306 /* Allocate the cached_dw_loc_list_table. */
27307 cached_dw_loc_list_table = hash_table<dw_loc_list_hasher>::create_ggc (10);
27309 /* Allocate the initial hunk of the decl_scope_table. */
27310 vec_alloc (decl_scope_table, 256);
27312 /* Allocate the initial hunk of the abbrev_die_table. */
27313 vec_alloc (abbrev_die_table, 256);
27314 /* Zero-th entry is allocated, but unused. */
27315 abbrev_die_table->quick_push (NULL);
27317 /* Allocate the dwarf_proc_stack_usage_map. */
27318 dwarf_proc_stack_usage_map = new hash_map<dw_die_ref, int>;
27320 /* Allocate the pubtypes and pubnames vectors. */
27321 vec_alloc (pubname_table, 32);
27322 vec_alloc (pubtype_table, 32);
27324 vec_alloc (incomplete_types, 64);
27326 vec_alloc (used_rtx_array, 32);
27328 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
27329 vec_alloc (macinfo_table, 64);
27330 #endif
27332 /* If front-ends already registered a main translation unit but we were not
27333 ready to perform the association, do this now. */
27334 if (main_translation_unit != NULL_TREE)
27335 equate_decl_number_to_die (main_translation_unit, comp_unit_die ());
27338 /* Called before compile () starts outputtting functions, variables
27339 and toplevel asms into assembly. */
27341 static void
27342 dwarf2out_assembly_start (void)
27344 #ifndef DWARF2_LINENO_DEBUGGING_INFO
27345 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
27346 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
27347 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
27348 COLD_TEXT_SECTION_LABEL, 0);
27349 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
27351 switch_to_section (text_section);
27352 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
27353 #endif
27355 /* Make sure the line number table for .text always exists. */
27356 text_section_line_info = new_line_info_table ();
27357 text_section_line_info->end_label = text_end_label;
27359 #ifdef DWARF2_LINENO_DEBUGGING_INFO
27360 cur_line_info_table = text_section_line_info;
27361 #endif
27363 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
27364 && dwarf2out_do_cfi_asm ()
27365 && (!(flag_unwind_tables || flag_exceptions)
27366 || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
27367 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
27370 /* A helper function for dwarf2out_finish called through
27371 htab_traverse. Assign a string its index. All strings must be
27372 collected into the table by the time index_string is called,
27373 because the indexing code relies on htab_traverse to traverse nodes
27374 in the same order for each run. */
27377 index_string (indirect_string_node **h, unsigned int *index)
27379 indirect_string_node *node = *h;
27381 find_string_form (node);
27382 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
27384 gcc_assert (node->index == NO_INDEX_ASSIGNED);
27385 node->index = *index;
27386 *index += 1;
27388 return 1;
27391 /* A helper function for output_indirect_strings called through
27392 htab_traverse. Output the offset to a string and update the
27393 current offset. */
27396 output_index_string_offset (indirect_string_node **h, unsigned int *offset)
27398 indirect_string_node *node = *h;
27400 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
27402 /* Assert that this node has been assigned an index. */
27403 gcc_assert (node->index != NO_INDEX_ASSIGNED
27404 && node->index != NOT_INDEXED);
27405 dw2_asm_output_data (DWARF_OFFSET_SIZE, *offset,
27406 "indexed string 0x%x: %s", node->index, node->str);
27407 *offset += strlen (node->str) + 1;
27409 return 1;
27412 /* A helper function for dwarf2out_finish called through
27413 htab_traverse. Output the indexed string. */
27416 output_index_string (indirect_string_node **h, unsigned int *cur_idx)
27418 struct indirect_string_node *node = *h;
27420 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
27422 /* Assert that the strings are output in the same order as their
27423 indexes were assigned. */
27424 gcc_assert (*cur_idx == node->index);
27425 assemble_string (node->str, strlen (node->str) + 1);
27426 *cur_idx += 1;
27428 return 1;
27431 /* A helper function for dwarf2out_finish called through
27432 htab_traverse. Emit one queued .debug_str string. */
27435 output_indirect_string (indirect_string_node **h, enum dwarf_form form)
27437 struct indirect_string_node *node = *h;
27439 node->form = find_string_form (node);
27440 if (node->form == form && node->refcount > 0)
27442 ASM_OUTPUT_LABEL (asm_out_file, node->label);
27443 assemble_string (node->str, strlen (node->str) + 1);
27446 return 1;
27449 /* Output the indexed string table. */
27451 static void
27452 output_indirect_strings (void)
27454 switch_to_section (debug_str_section);
27455 if (!dwarf_split_debug_info)
27456 debug_str_hash->traverse<enum dwarf_form,
27457 output_indirect_string> (DW_FORM_strp);
27458 else
27460 unsigned int offset = 0;
27461 unsigned int cur_idx = 0;
27463 skeleton_debug_str_hash->traverse<enum dwarf_form,
27464 output_indirect_string> (DW_FORM_strp);
27466 switch_to_section (debug_str_offsets_section);
27467 debug_str_hash->traverse_noresize
27468 <unsigned int *, output_index_string_offset> (&offset);
27469 switch_to_section (debug_str_dwo_section);
27470 debug_str_hash->traverse_noresize<unsigned int *, output_index_string>
27471 (&cur_idx);
27475 /* Callback for htab_traverse to assign an index to an entry in the
27476 table, and to write that entry to the .debug_addr section. */
27479 output_addr_table_entry (addr_table_entry **slot, unsigned int *cur_index)
27481 addr_table_entry *entry = *slot;
27483 if (entry->refcount == 0)
27485 gcc_assert (entry->index == NO_INDEX_ASSIGNED
27486 || entry->index == NOT_INDEXED);
27487 return 1;
27490 gcc_assert (entry->index == *cur_index);
27491 (*cur_index)++;
27493 switch (entry->kind)
27495 case ate_kind_rtx:
27496 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
27497 "0x%x", entry->index);
27498 break;
27499 case ate_kind_rtx_dtprel:
27500 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
27501 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
27502 DWARF2_ADDR_SIZE,
27503 entry->addr.rtl);
27504 fputc ('\n', asm_out_file);
27505 break;
27506 case ate_kind_label:
27507 dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
27508 "0x%x", entry->index);
27509 break;
27510 default:
27511 gcc_unreachable ();
27513 return 1;
27516 /* Produce the .debug_addr section. */
27518 static void
27519 output_addr_table (void)
27521 unsigned int index = 0;
27522 if (addr_index_table == NULL || addr_index_table->size () == 0)
27523 return;
27525 switch_to_section (debug_addr_section);
27526 addr_index_table
27527 ->traverse_noresize<unsigned int *, output_addr_table_entry> (&index);
27530 #if ENABLE_ASSERT_CHECKING
27531 /* Verify that all marks are clear. */
27533 static void
27534 verify_marks_clear (dw_die_ref die)
27536 dw_die_ref c;
27538 gcc_assert (! die->die_mark);
27539 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
27541 #endif /* ENABLE_ASSERT_CHECKING */
27543 /* Clear the marks for a die and its children.
27544 Be cool if the mark isn't set. */
27546 static void
27547 prune_unmark_dies (dw_die_ref die)
27549 dw_die_ref c;
27551 if (die->die_mark)
27552 die->die_mark = 0;
27553 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
27556 /* Given LOC that is referenced by a DIE we're marking as used, find all
27557 referenced DWARF procedures it references and mark them as used. */
27559 static void
27560 prune_unused_types_walk_loc_descr (dw_loc_descr_ref loc)
27562 for (; loc != NULL; loc = loc->dw_loc_next)
27563 switch (loc->dw_loc_opc)
27565 case DW_OP_implicit_pointer:
27566 case DW_OP_convert:
27567 case DW_OP_reinterpret:
27568 case DW_OP_GNU_implicit_pointer:
27569 case DW_OP_GNU_convert:
27570 case DW_OP_GNU_reinterpret:
27571 if (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref)
27572 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
27573 break;
27574 case DW_OP_GNU_variable_value:
27575 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
27577 dw_die_ref ref
27578 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
27579 if (ref == NULL)
27580 break;
27581 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
27582 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
27583 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
27585 /* FALLTHRU */
27586 case DW_OP_call2:
27587 case DW_OP_call4:
27588 case DW_OP_call_ref:
27589 case DW_OP_const_type:
27590 case DW_OP_GNU_const_type:
27591 case DW_OP_GNU_parameter_ref:
27592 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref);
27593 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
27594 break;
27595 case DW_OP_regval_type:
27596 case DW_OP_deref_type:
27597 case DW_OP_GNU_regval_type:
27598 case DW_OP_GNU_deref_type:
27599 gcc_assert (loc->dw_loc_oprnd2.val_class == dw_val_class_die_ref);
27600 prune_unused_types_mark (loc->dw_loc_oprnd2.v.val_die_ref.die, 1);
27601 break;
27602 case DW_OP_entry_value:
27603 case DW_OP_GNU_entry_value:
27604 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_loc);
27605 prune_unused_types_walk_loc_descr (loc->dw_loc_oprnd1.v.val_loc);
27606 break;
27607 default:
27608 break;
27612 /* Given DIE that we're marking as used, find any other dies
27613 it references as attributes and mark them as used. */
27615 static void
27616 prune_unused_types_walk_attribs (dw_die_ref die)
27618 dw_attr_node *a;
27619 unsigned ix;
27621 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
27623 switch (AT_class (a))
27625 /* Make sure DWARF procedures referenced by location descriptions will
27626 get emitted. */
27627 case dw_val_class_loc:
27628 prune_unused_types_walk_loc_descr (AT_loc (a));
27629 break;
27630 case dw_val_class_loc_list:
27631 for (dw_loc_list_ref list = AT_loc_list (a);
27632 list != NULL;
27633 list = list->dw_loc_next)
27634 prune_unused_types_walk_loc_descr (list->expr);
27635 break;
27637 case dw_val_class_die_ref:
27638 /* A reference to another DIE.
27639 Make sure that it will get emitted.
27640 If it was broken out into a comdat group, don't follow it. */
27641 if (! AT_ref (a)->comdat_type_p
27642 || a->dw_attr == DW_AT_specification)
27643 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
27644 break;
27646 case dw_val_class_str:
27647 /* Set the string's refcount to 0 so that prune_unused_types_mark
27648 accounts properly for it. */
27649 a->dw_attr_val.v.val_str->refcount = 0;
27650 break;
27652 default:
27653 break;
27658 /* Mark the generic parameters and arguments children DIEs of DIE. */
27660 static void
27661 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
27663 dw_die_ref c;
27665 if (die == NULL || die->die_child == NULL)
27666 return;
27667 c = die->die_child;
27670 if (is_template_parameter (c))
27671 prune_unused_types_mark (c, 1);
27672 c = c->die_sib;
27673 } while (c && c != die->die_child);
27676 /* Mark DIE as being used. If DOKIDS is true, then walk down
27677 to DIE's children. */
27679 static void
27680 prune_unused_types_mark (dw_die_ref die, int dokids)
27682 dw_die_ref c;
27684 if (die->die_mark == 0)
27686 /* We haven't done this node yet. Mark it as used. */
27687 die->die_mark = 1;
27688 /* If this is the DIE of a generic type instantiation,
27689 mark the children DIEs that describe its generic parms and
27690 args. */
27691 prune_unused_types_mark_generic_parms_dies (die);
27693 /* We also have to mark its parents as used.
27694 (But we don't want to mark our parent's kids due to this,
27695 unless it is a class.) */
27696 if (die->die_parent)
27697 prune_unused_types_mark (die->die_parent,
27698 class_scope_p (die->die_parent));
27700 /* Mark any referenced nodes. */
27701 prune_unused_types_walk_attribs (die);
27703 /* If this node is a specification,
27704 also mark the definition, if it exists. */
27705 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
27706 prune_unused_types_mark (die->die_definition, 1);
27709 if (dokids && die->die_mark != 2)
27711 /* We need to walk the children, but haven't done so yet.
27712 Remember that we've walked the kids. */
27713 die->die_mark = 2;
27715 /* If this is an array type, we need to make sure our
27716 kids get marked, even if they're types. If we're
27717 breaking out types into comdat sections, do this
27718 for all type definitions. */
27719 if (die->die_tag == DW_TAG_array_type
27720 || (use_debug_types
27721 && is_type_die (die) && ! is_declaration_die (die)))
27722 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
27723 else
27724 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
27728 /* For local classes, look if any static member functions were emitted
27729 and if so, mark them. */
27731 static void
27732 prune_unused_types_walk_local_classes (dw_die_ref die)
27734 dw_die_ref c;
27736 if (die->die_mark == 2)
27737 return;
27739 switch (die->die_tag)
27741 case DW_TAG_structure_type:
27742 case DW_TAG_union_type:
27743 case DW_TAG_class_type:
27744 break;
27746 case DW_TAG_subprogram:
27747 if (!get_AT_flag (die, DW_AT_declaration)
27748 || die->die_definition != NULL)
27749 prune_unused_types_mark (die, 1);
27750 return;
27752 default:
27753 return;
27756 /* Mark children. */
27757 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
27760 /* Walk the tree DIE and mark types that we actually use. */
27762 static void
27763 prune_unused_types_walk (dw_die_ref die)
27765 dw_die_ref c;
27767 /* Don't do anything if this node is already marked and
27768 children have been marked as well. */
27769 if (die->die_mark == 2)
27770 return;
27772 switch (die->die_tag)
27774 case DW_TAG_structure_type:
27775 case DW_TAG_union_type:
27776 case DW_TAG_class_type:
27777 if (die->die_perennial_p)
27778 break;
27780 for (c = die->die_parent; c; c = c->die_parent)
27781 if (c->die_tag == DW_TAG_subprogram)
27782 break;
27784 /* Finding used static member functions inside of classes
27785 is needed just for local classes, because for other classes
27786 static member function DIEs with DW_AT_specification
27787 are emitted outside of the DW_TAG_*_type. If we ever change
27788 it, we'd need to call this even for non-local classes. */
27789 if (c)
27790 prune_unused_types_walk_local_classes (die);
27792 /* It's a type node --- don't mark it. */
27793 return;
27795 case DW_TAG_const_type:
27796 case DW_TAG_packed_type:
27797 case DW_TAG_pointer_type:
27798 case DW_TAG_reference_type:
27799 case DW_TAG_rvalue_reference_type:
27800 case DW_TAG_volatile_type:
27801 case DW_TAG_typedef:
27802 case DW_TAG_array_type:
27803 case DW_TAG_interface_type:
27804 case DW_TAG_friend:
27805 case DW_TAG_enumeration_type:
27806 case DW_TAG_subroutine_type:
27807 case DW_TAG_string_type:
27808 case DW_TAG_set_type:
27809 case DW_TAG_subrange_type:
27810 case DW_TAG_ptr_to_member_type:
27811 case DW_TAG_file_type:
27812 /* Type nodes are useful only when other DIEs reference them --- don't
27813 mark them. */
27814 /* FALLTHROUGH */
27816 case DW_TAG_dwarf_procedure:
27817 /* Likewise for DWARF procedures. */
27819 if (die->die_perennial_p)
27820 break;
27822 return;
27824 default:
27825 /* Mark everything else. */
27826 break;
27829 if (die->die_mark == 0)
27831 die->die_mark = 1;
27833 /* Now, mark any dies referenced from here. */
27834 prune_unused_types_walk_attribs (die);
27837 die->die_mark = 2;
27839 /* Mark children. */
27840 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
27843 /* Increment the string counts on strings referred to from DIE's
27844 attributes. */
27846 static void
27847 prune_unused_types_update_strings (dw_die_ref die)
27849 dw_attr_node *a;
27850 unsigned ix;
27852 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
27853 if (AT_class (a) == dw_val_class_str)
27855 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
27856 s->refcount++;
27857 /* Avoid unnecessarily putting strings that are used less than
27858 twice in the hash table. */
27859 if (s->refcount
27860 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
27862 indirect_string_node **slot
27863 = debug_str_hash->find_slot_with_hash (s->str,
27864 htab_hash_string (s->str),
27865 INSERT);
27866 gcc_assert (*slot == NULL);
27867 *slot = s;
27872 /* Mark DIE and its children as removed. */
27874 static void
27875 mark_removed (dw_die_ref die)
27877 dw_die_ref c;
27878 die->removed = true;
27879 FOR_EACH_CHILD (die, c, mark_removed (c));
27882 /* Remove from the tree DIE any dies that aren't marked. */
27884 static void
27885 prune_unused_types_prune (dw_die_ref die)
27887 dw_die_ref c;
27889 gcc_assert (die->die_mark);
27890 prune_unused_types_update_strings (die);
27892 if (! die->die_child)
27893 return;
27895 c = die->die_child;
27896 do {
27897 dw_die_ref prev = c, next;
27898 for (c = c->die_sib; ! c->die_mark; c = next)
27899 if (c == die->die_child)
27901 /* No marked children between 'prev' and the end of the list. */
27902 if (prev == c)
27903 /* No marked children at all. */
27904 die->die_child = NULL;
27905 else
27907 prev->die_sib = c->die_sib;
27908 die->die_child = prev;
27910 c->die_sib = NULL;
27911 mark_removed (c);
27912 return;
27914 else
27916 next = c->die_sib;
27917 c->die_sib = NULL;
27918 mark_removed (c);
27921 if (c != prev->die_sib)
27922 prev->die_sib = c;
27923 prune_unused_types_prune (c);
27924 } while (c != die->die_child);
27927 /* Remove dies representing declarations that we never use. */
27929 static void
27930 prune_unused_types (void)
27932 unsigned int i;
27933 limbo_die_node *node;
27934 comdat_type_node *ctnode;
27935 pubname_entry *pub;
27936 dw_die_ref base_type;
27938 #if ENABLE_ASSERT_CHECKING
27939 /* All the marks should already be clear. */
27940 verify_marks_clear (comp_unit_die ());
27941 for (node = limbo_die_list; node; node = node->next)
27942 verify_marks_clear (node->die);
27943 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
27944 verify_marks_clear (ctnode->root_die);
27945 #endif /* ENABLE_ASSERT_CHECKING */
27947 /* Mark types that are used in global variables. */
27948 premark_types_used_by_global_vars ();
27950 /* Set the mark on nodes that are actually used. */
27951 prune_unused_types_walk (comp_unit_die ());
27952 for (node = limbo_die_list; node; node = node->next)
27953 prune_unused_types_walk (node->die);
27954 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
27956 prune_unused_types_walk (ctnode->root_die);
27957 prune_unused_types_mark (ctnode->type_die, 1);
27960 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
27961 are unusual in that they are pubnames that are the children of pubtypes.
27962 They should only be marked via their parent DW_TAG_enumeration_type die,
27963 not as roots in themselves. */
27964 FOR_EACH_VEC_ELT (*pubname_table, i, pub)
27965 if (pub->die->die_tag != DW_TAG_enumerator)
27966 prune_unused_types_mark (pub->die, 1);
27967 for (i = 0; base_types.iterate (i, &base_type); i++)
27968 prune_unused_types_mark (base_type, 1);
27970 /* For -fvar-tracking-assignments, also set the mark on nodes that could be
27971 referenced by DW_TAG_call_site DW_AT_call_origin (i.e. direct call
27972 callees). */
27973 cgraph_node *cnode;
27974 FOR_EACH_FUNCTION (cnode)
27975 if (cnode->referred_to_p (false))
27977 dw_die_ref die = lookup_decl_die (cnode->decl);
27978 if (die == NULL || die->die_mark)
27979 continue;
27980 for (cgraph_edge *e = cnode->callers; e; e = e->next_caller)
27981 if (e->caller != cnode
27982 && opt_for_fn (e->caller->decl, flag_var_tracking_assignments))
27984 prune_unused_types_mark (die, 1);
27985 break;
27989 if (debug_str_hash)
27990 debug_str_hash->empty ();
27991 if (skeleton_debug_str_hash)
27992 skeleton_debug_str_hash->empty ();
27993 prune_unused_types_prune (comp_unit_die ());
27994 for (limbo_die_node **pnode = &limbo_die_list; *pnode; )
27996 node = *pnode;
27997 if (!node->die->die_mark)
27998 *pnode = node->next;
27999 else
28001 prune_unused_types_prune (node->die);
28002 pnode = &node->next;
28005 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
28006 prune_unused_types_prune (ctnode->root_die);
28008 /* Leave the marks clear. */
28009 prune_unmark_dies (comp_unit_die ());
28010 for (node = limbo_die_list; node; node = node->next)
28011 prune_unmark_dies (node->die);
28012 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
28013 prune_unmark_dies (ctnode->root_die);
28016 /* Helpers to manipulate hash table of comdat type units. */
28018 struct comdat_type_hasher : nofree_ptr_hash <comdat_type_node>
28020 static inline hashval_t hash (const comdat_type_node *);
28021 static inline bool equal (const comdat_type_node *, const comdat_type_node *);
28024 inline hashval_t
28025 comdat_type_hasher::hash (const comdat_type_node *type_node)
28027 hashval_t h;
28028 memcpy (&h, type_node->signature, sizeof (h));
28029 return h;
28032 inline bool
28033 comdat_type_hasher::equal (const comdat_type_node *type_node_1,
28034 const comdat_type_node *type_node_2)
28036 return (! memcmp (type_node_1->signature, type_node_2->signature,
28037 DWARF_TYPE_SIGNATURE_SIZE));
28040 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
28041 to the location it would have been added, should we know its
28042 DECL_ASSEMBLER_NAME when we added other attributes. This will
28043 probably improve compactness of debug info, removing equivalent
28044 abbrevs, and hide any differences caused by deferring the
28045 computation of the assembler name, triggered by e.g. PCH. */
28047 static inline void
28048 move_linkage_attr (dw_die_ref die)
28050 unsigned ix = vec_safe_length (die->die_attr);
28051 dw_attr_node linkage = (*die->die_attr)[ix - 1];
28053 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
28054 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
28056 while (--ix > 0)
28058 dw_attr_node *prev = &(*die->die_attr)[ix - 1];
28060 if (prev->dw_attr == DW_AT_decl_line
28061 || prev->dw_attr == DW_AT_decl_column
28062 || prev->dw_attr == DW_AT_name)
28063 break;
28066 if (ix != vec_safe_length (die->die_attr) - 1)
28068 die->die_attr->pop ();
28069 die->die_attr->quick_insert (ix, linkage);
28073 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
28074 referenced from typed stack ops and count how often they are used. */
28076 static void
28077 mark_base_types (dw_loc_descr_ref loc)
28079 dw_die_ref base_type = NULL;
28081 for (; loc; loc = loc->dw_loc_next)
28083 switch (loc->dw_loc_opc)
28085 case DW_OP_regval_type:
28086 case DW_OP_deref_type:
28087 case DW_OP_GNU_regval_type:
28088 case DW_OP_GNU_deref_type:
28089 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
28090 break;
28091 case DW_OP_convert:
28092 case DW_OP_reinterpret:
28093 case DW_OP_GNU_convert:
28094 case DW_OP_GNU_reinterpret:
28095 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
28096 continue;
28097 /* FALLTHRU */
28098 case DW_OP_const_type:
28099 case DW_OP_GNU_const_type:
28100 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
28101 break;
28102 case DW_OP_entry_value:
28103 case DW_OP_GNU_entry_value:
28104 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
28105 continue;
28106 default:
28107 continue;
28109 gcc_assert (base_type->die_parent == comp_unit_die ());
28110 if (base_type->die_mark)
28111 base_type->die_mark++;
28112 else
28114 base_types.safe_push (base_type);
28115 base_type->die_mark = 1;
28120 /* Comparison function for sorting marked base types. */
28122 static int
28123 base_type_cmp (const void *x, const void *y)
28125 dw_die_ref dx = *(const dw_die_ref *) x;
28126 dw_die_ref dy = *(const dw_die_ref *) y;
28127 unsigned int byte_size1, byte_size2;
28128 unsigned int encoding1, encoding2;
28129 unsigned int align1, align2;
28130 if (dx->die_mark > dy->die_mark)
28131 return -1;
28132 if (dx->die_mark < dy->die_mark)
28133 return 1;
28134 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
28135 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
28136 if (byte_size1 < byte_size2)
28137 return 1;
28138 if (byte_size1 > byte_size2)
28139 return -1;
28140 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
28141 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
28142 if (encoding1 < encoding2)
28143 return 1;
28144 if (encoding1 > encoding2)
28145 return -1;
28146 align1 = get_AT_unsigned (dx, DW_AT_alignment);
28147 align2 = get_AT_unsigned (dy, DW_AT_alignment);
28148 if (align1 < align2)
28149 return 1;
28150 if (align1 > align2)
28151 return -1;
28152 return 0;
28155 /* Move base types marked by mark_base_types as early as possible
28156 in the CU, sorted by decreasing usage count both to make the
28157 uleb128 references as small as possible and to make sure they
28158 will have die_offset already computed by calc_die_sizes when
28159 sizes of typed stack loc ops is computed. */
28161 static void
28162 move_marked_base_types (void)
28164 unsigned int i;
28165 dw_die_ref base_type, die, c;
28167 if (base_types.is_empty ())
28168 return;
28170 /* Sort by decreasing usage count, they will be added again in that
28171 order later on. */
28172 base_types.qsort (base_type_cmp);
28173 die = comp_unit_die ();
28174 c = die->die_child;
28177 dw_die_ref prev = c;
28178 c = c->die_sib;
28179 while (c->die_mark)
28181 remove_child_with_prev (c, prev);
28182 /* As base types got marked, there must be at least
28183 one node other than DW_TAG_base_type. */
28184 gcc_assert (die->die_child != NULL);
28185 c = prev->die_sib;
28188 while (c != die->die_child);
28189 gcc_assert (die->die_child);
28190 c = die->die_child;
28191 for (i = 0; base_types.iterate (i, &base_type); i++)
28193 base_type->die_mark = 0;
28194 base_type->die_sib = c->die_sib;
28195 c->die_sib = base_type;
28196 c = base_type;
28200 /* Helper function for resolve_addr, attempt to resolve
28201 one CONST_STRING, return true if successful. Similarly verify that
28202 SYMBOL_REFs refer to variables emitted in the current CU. */
28204 static bool
28205 resolve_one_addr (rtx *addr)
28207 rtx rtl = *addr;
28209 if (GET_CODE (rtl) == CONST_STRING)
28211 size_t len = strlen (XSTR (rtl, 0)) + 1;
28212 tree t = build_string (len, XSTR (rtl, 0));
28213 tree tlen = size_int (len - 1);
28214 TREE_TYPE (t)
28215 = build_array_type (char_type_node, build_index_type (tlen));
28216 rtl = lookup_constant_def (t);
28217 if (!rtl || !MEM_P (rtl))
28218 return false;
28219 rtl = XEXP (rtl, 0);
28220 if (GET_CODE (rtl) == SYMBOL_REF
28221 && SYMBOL_REF_DECL (rtl)
28222 && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
28223 return false;
28224 vec_safe_push (used_rtx_array, rtl);
28225 *addr = rtl;
28226 return true;
28229 if (GET_CODE (rtl) == SYMBOL_REF
28230 && SYMBOL_REF_DECL (rtl))
28232 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
28234 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
28235 return false;
28237 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
28238 return false;
28241 if (GET_CODE (rtl) == CONST)
28243 subrtx_ptr_iterator::array_type array;
28244 FOR_EACH_SUBRTX_PTR (iter, array, &XEXP (rtl, 0), ALL)
28245 if (!resolve_one_addr (*iter))
28246 return false;
28249 return true;
28252 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
28253 if possible, and create DW_TAG_dwarf_procedure that can be referenced
28254 from DW_OP_implicit_pointer if the string hasn't been seen yet. */
28256 static rtx
28257 string_cst_pool_decl (tree t)
28259 rtx rtl = output_constant_def (t, 1);
28260 unsigned char *array;
28261 dw_loc_descr_ref l;
28262 tree decl;
28263 size_t len;
28264 dw_die_ref ref;
28266 if (!rtl || !MEM_P (rtl))
28267 return NULL_RTX;
28268 rtl = XEXP (rtl, 0);
28269 if (GET_CODE (rtl) != SYMBOL_REF
28270 || SYMBOL_REF_DECL (rtl) == NULL_TREE)
28271 return NULL_RTX;
28273 decl = SYMBOL_REF_DECL (rtl);
28274 if (!lookup_decl_die (decl))
28276 len = TREE_STRING_LENGTH (t);
28277 vec_safe_push (used_rtx_array, rtl);
28278 ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
28279 array = ggc_vec_alloc<unsigned char> (len);
28280 memcpy (array, TREE_STRING_POINTER (t), len);
28281 l = new_loc_descr (DW_OP_implicit_value, len, 0);
28282 l->dw_loc_oprnd2.val_class = dw_val_class_vec;
28283 l->dw_loc_oprnd2.v.val_vec.length = len;
28284 l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
28285 l->dw_loc_oprnd2.v.val_vec.array = array;
28286 add_AT_loc (ref, DW_AT_location, l);
28287 equate_decl_number_to_die (decl, ref);
28289 return rtl;
28292 /* Helper function of resolve_addr_in_expr. LOC is
28293 a DW_OP_addr followed by DW_OP_stack_value, either at the start
28294 of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
28295 resolved. Replace it (both DW_OP_addr and DW_OP_stack_value)
28296 with DW_OP_implicit_pointer if possible
28297 and return true, if unsuccessful, return false. */
28299 static bool
28300 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
28302 rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
28303 HOST_WIDE_INT offset = 0;
28304 dw_die_ref ref = NULL;
28305 tree decl;
28307 if (GET_CODE (rtl) == CONST
28308 && GET_CODE (XEXP (rtl, 0)) == PLUS
28309 && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
28311 offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
28312 rtl = XEXP (XEXP (rtl, 0), 0);
28314 if (GET_CODE (rtl) == CONST_STRING)
28316 size_t len = strlen (XSTR (rtl, 0)) + 1;
28317 tree t = build_string (len, XSTR (rtl, 0));
28318 tree tlen = size_int (len - 1);
28320 TREE_TYPE (t)
28321 = build_array_type (char_type_node, build_index_type (tlen));
28322 rtl = string_cst_pool_decl (t);
28323 if (!rtl)
28324 return false;
28326 if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
28328 decl = SYMBOL_REF_DECL (rtl);
28329 if (VAR_P (decl) && !DECL_EXTERNAL (decl))
28331 ref = lookup_decl_die (decl);
28332 if (ref && (get_AT (ref, DW_AT_location)
28333 || get_AT (ref, DW_AT_const_value)))
28335 loc->dw_loc_opc = dwarf_OP (DW_OP_implicit_pointer);
28336 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
28337 loc->dw_loc_oprnd1.val_entry = NULL;
28338 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
28339 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
28340 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
28341 loc->dw_loc_oprnd2.v.val_int = offset;
28342 return true;
28346 return false;
28349 /* Helper function for resolve_addr, handle one location
28350 expression, return false if at least one CONST_STRING or SYMBOL_REF in
28351 the location list couldn't be resolved. */
28353 static bool
28354 resolve_addr_in_expr (dw_attr_node *a, dw_loc_descr_ref loc)
28356 dw_loc_descr_ref keep = NULL;
28357 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
28358 switch (loc->dw_loc_opc)
28360 case DW_OP_addr:
28361 if (!resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
28363 if ((prev == NULL
28364 || prev->dw_loc_opc == DW_OP_piece
28365 || prev->dw_loc_opc == DW_OP_bit_piece)
28366 && loc->dw_loc_next
28367 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
28368 && (!dwarf_strict || dwarf_version >= 5)
28369 && optimize_one_addr_into_implicit_ptr (loc))
28370 break;
28371 return false;
28373 break;
28374 case DW_OP_GNU_addr_index:
28375 case DW_OP_GNU_const_index:
28376 if (loc->dw_loc_opc == DW_OP_GNU_addr_index
28377 || (loc->dw_loc_opc == DW_OP_GNU_const_index && loc->dtprel))
28379 rtx rtl = loc->dw_loc_oprnd1.val_entry->addr.rtl;
28380 if (!resolve_one_addr (&rtl))
28381 return false;
28382 remove_addr_table_entry (loc->dw_loc_oprnd1.val_entry);
28383 loc->dw_loc_oprnd1.val_entry
28384 = add_addr_table_entry (rtl, ate_kind_rtx);
28386 break;
28387 case DW_OP_const4u:
28388 case DW_OP_const8u:
28389 if (loc->dtprel
28390 && !resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
28391 return false;
28392 break;
28393 case DW_OP_plus_uconst:
28394 if (size_of_loc_descr (loc)
28395 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
28397 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
28399 dw_loc_descr_ref repl
28400 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
28401 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
28402 add_loc_descr (&repl, loc->dw_loc_next);
28403 *loc = *repl;
28405 break;
28406 case DW_OP_implicit_value:
28407 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
28408 && !resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr))
28409 return false;
28410 break;
28411 case DW_OP_implicit_pointer:
28412 case DW_OP_GNU_implicit_pointer:
28413 case DW_OP_GNU_parameter_ref:
28414 case DW_OP_GNU_variable_value:
28415 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
28417 dw_die_ref ref
28418 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
28419 if (ref == NULL)
28420 return false;
28421 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
28422 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
28423 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
28425 if (loc->dw_loc_opc == DW_OP_GNU_variable_value)
28427 if (prev == NULL
28428 && loc->dw_loc_next == NULL
28429 && AT_class (a) == dw_val_class_loc)
28430 switch (a->dw_attr)
28432 /* Following attributes allow both exprloc and reference,
28433 so if the whole expression is DW_OP_GNU_variable_value
28434 alone we could transform it into reference. */
28435 case DW_AT_byte_size:
28436 case DW_AT_bit_size:
28437 case DW_AT_lower_bound:
28438 case DW_AT_upper_bound:
28439 case DW_AT_bit_stride:
28440 case DW_AT_count:
28441 case DW_AT_allocated:
28442 case DW_AT_associated:
28443 case DW_AT_byte_stride:
28444 a->dw_attr_val.val_class = dw_val_class_die_ref;
28445 a->dw_attr_val.val_entry = NULL;
28446 a->dw_attr_val.v.val_die_ref.die
28447 = loc->dw_loc_oprnd1.v.val_die_ref.die;
28448 a->dw_attr_val.v.val_die_ref.external = 0;
28449 return true;
28450 default:
28451 break;
28453 if (dwarf_strict)
28454 return false;
28456 break;
28457 case DW_OP_const_type:
28458 case DW_OP_regval_type:
28459 case DW_OP_deref_type:
28460 case DW_OP_convert:
28461 case DW_OP_reinterpret:
28462 case DW_OP_GNU_const_type:
28463 case DW_OP_GNU_regval_type:
28464 case DW_OP_GNU_deref_type:
28465 case DW_OP_GNU_convert:
28466 case DW_OP_GNU_reinterpret:
28467 while (loc->dw_loc_next
28468 && (loc->dw_loc_next->dw_loc_opc == DW_OP_convert
28469 || loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert))
28471 dw_die_ref base1, base2;
28472 unsigned enc1, enc2, size1, size2;
28473 if (loc->dw_loc_opc == DW_OP_regval_type
28474 || loc->dw_loc_opc == DW_OP_deref_type
28475 || loc->dw_loc_opc == DW_OP_GNU_regval_type
28476 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
28477 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
28478 else if (loc->dw_loc_oprnd1.val_class
28479 == dw_val_class_unsigned_const)
28480 break;
28481 else
28482 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
28483 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
28484 == dw_val_class_unsigned_const)
28485 break;
28486 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
28487 gcc_assert (base1->die_tag == DW_TAG_base_type
28488 && base2->die_tag == DW_TAG_base_type);
28489 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
28490 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
28491 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
28492 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
28493 if (size1 == size2
28494 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
28495 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
28496 && loc != keep)
28497 || enc1 == enc2))
28499 /* Optimize away next DW_OP_convert after
28500 adjusting LOC's base type die reference. */
28501 if (loc->dw_loc_opc == DW_OP_regval_type
28502 || loc->dw_loc_opc == DW_OP_deref_type
28503 || loc->dw_loc_opc == DW_OP_GNU_regval_type
28504 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
28505 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
28506 else
28507 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
28508 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
28509 continue;
28511 /* Don't change integer DW_OP_convert after e.g. floating
28512 point typed stack entry. */
28513 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
28514 keep = loc->dw_loc_next;
28515 break;
28517 break;
28518 default:
28519 break;
28521 return true;
28524 /* Helper function of resolve_addr. DIE had DW_AT_location of
28525 DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
28526 and DW_OP_addr couldn't be resolved. resolve_addr has already
28527 removed the DW_AT_location attribute. This function attempts to
28528 add a new DW_AT_location attribute with DW_OP_implicit_pointer
28529 to it or DW_AT_const_value attribute, if possible. */
28531 static void
28532 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
28534 if (!VAR_P (decl)
28535 || lookup_decl_die (decl) != die
28536 || DECL_EXTERNAL (decl)
28537 || !TREE_STATIC (decl)
28538 || DECL_INITIAL (decl) == NULL_TREE
28539 || DECL_P (DECL_INITIAL (decl))
28540 || get_AT (die, DW_AT_const_value))
28541 return;
28543 tree init = DECL_INITIAL (decl);
28544 HOST_WIDE_INT offset = 0;
28545 /* For variables that have been optimized away and thus
28546 don't have a memory location, see if we can emit
28547 DW_AT_const_value instead. */
28548 if (tree_add_const_value_attribute (die, init))
28549 return;
28550 if (dwarf_strict && dwarf_version < 5)
28551 return;
28552 /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
28553 and ADDR_EXPR refers to a decl that has DW_AT_location or
28554 DW_AT_const_value (but isn't addressable, otherwise
28555 resolving the original DW_OP_addr wouldn't fail), see if
28556 we can add DW_OP_implicit_pointer. */
28557 STRIP_NOPS (init);
28558 if (TREE_CODE (init) == POINTER_PLUS_EXPR
28559 && tree_fits_shwi_p (TREE_OPERAND (init, 1)))
28561 offset = tree_to_shwi (TREE_OPERAND (init, 1));
28562 init = TREE_OPERAND (init, 0);
28563 STRIP_NOPS (init);
28565 if (TREE_CODE (init) != ADDR_EXPR)
28566 return;
28567 if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
28568 && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
28569 || (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL
28570 && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
28571 && TREE_OPERAND (init, 0) != decl))
28573 dw_die_ref ref;
28574 dw_loc_descr_ref l;
28576 if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
28578 rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
28579 if (!rtl)
28580 return;
28581 decl = SYMBOL_REF_DECL (rtl);
28583 else
28584 decl = TREE_OPERAND (init, 0);
28585 ref = lookup_decl_die (decl);
28586 if (ref == NULL
28587 || (!get_AT (ref, DW_AT_location)
28588 && !get_AT (ref, DW_AT_const_value)))
28589 return;
28590 l = new_loc_descr (dwarf_OP (DW_OP_implicit_pointer), 0, offset);
28591 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
28592 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
28593 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
28594 add_AT_loc (die, DW_AT_location, l);
28598 /* Return NULL if l is a DWARF expression, or first op that is not
28599 valid DWARF expression. */
28601 static dw_loc_descr_ref
28602 non_dwarf_expression (dw_loc_descr_ref l)
28604 while (l)
28606 if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
28607 return l;
28608 switch (l->dw_loc_opc)
28610 case DW_OP_regx:
28611 case DW_OP_implicit_value:
28612 case DW_OP_stack_value:
28613 case DW_OP_implicit_pointer:
28614 case DW_OP_GNU_implicit_pointer:
28615 case DW_OP_GNU_parameter_ref:
28616 case DW_OP_piece:
28617 case DW_OP_bit_piece:
28618 return l;
28619 default:
28620 break;
28622 l = l->dw_loc_next;
28624 return NULL;
28627 /* Return adjusted copy of EXPR:
28628 If it is empty DWARF expression, return it.
28629 If it is valid non-empty DWARF expression,
28630 return copy of EXPR with DW_OP_deref appended to it.
28631 If it is DWARF expression followed by DW_OP_reg{N,x}, return
28632 copy of the DWARF expression with DW_OP_breg{N,x} <0> appended.
28633 If it is DWARF expression followed by DW_OP_stack_value, return
28634 copy of the DWARF expression without anything appended.
28635 Otherwise, return NULL. */
28637 static dw_loc_descr_ref
28638 copy_deref_exprloc (dw_loc_descr_ref expr)
28640 dw_loc_descr_ref tail = NULL;
28642 if (expr == NULL)
28643 return NULL;
28645 dw_loc_descr_ref l = non_dwarf_expression (expr);
28646 if (l && l->dw_loc_next)
28647 return NULL;
28649 if (l)
28651 if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
28652 tail = new_loc_descr ((enum dwarf_location_atom)
28653 (DW_OP_breg0 + (l->dw_loc_opc - DW_OP_reg0)),
28654 0, 0);
28655 else
28656 switch (l->dw_loc_opc)
28658 case DW_OP_regx:
28659 tail = new_loc_descr (DW_OP_bregx,
28660 l->dw_loc_oprnd1.v.val_unsigned, 0);
28661 break;
28662 case DW_OP_stack_value:
28663 break;
28664 default:
28665 return NULL;
28668 else
28669 tail = new_loc_descr (DW_OP_deref, 0, 0);
28671 dw_loc_descr_ref ret = NULL, *p = &ret;
28672 while (expr != l)
28674 *p = new_loc_descr (expr->dw_loc_opc, 0, 0);
28675 (*p)->dw_loc_oprnd1 = expr->dw_loc_oprnd1;
28676 (*p)->dw_loc_oprnd2 = expr->dw_loc_oprnd2;
28677 p = &(*p)->dw_loc_next;
28678 expr = expr->dw_loc_next;
28680 *p = tail;
28681 return ret;
28684 /* For DW_AT_string_length attribute with DW_OP_GNU_variable_value
28685 reference to a variable or argument, adjust it if needed and return:
28686 -1 if the DW_AT_string_length attribute and DW_AT_{string_length_,}byte_size
28687 attribute if present should be removed
28688 0 keep the attribute perhaps with minor modifications, no need to rescan
28689 1 if the attribute has been successfully adjusted. */
28691 static int
28692 optimize_string_length (dw_attr_node *a)
28694 dw_loc_descr_ref l = AT_loc (a), lv;
28695 dw_die_ref die;
28696 if (l->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
28698 tree decl = l->dw_loc_oprnd1.v.val_decl_ref;
28699 die = lookup_decl_die (decl);
28700 if (die)
28702 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
28703 l->dw_loc_oprnd1.v.val_die_ref.die = die;
28704 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
28706 else
28707 return -1;
28709 else
28710 die = l->dw_loc_oprnd1.v.val_die_ref.die;
28712 /* DWARF5 allows reference class, so we can then reference the DIE.
28713 Only do this for DW_OP_GNU_variable_value DW_OP_stack_value. */
28714 if (l->dw_loc_next != NULL && dwarf_version >= 5)
28716 a->dw_attr_val.val_class = dw_val_class_die_ref;
28717 a->dw_attr_val.val_entry = NULL;
28718 a->dw_attr_val.v.val_die_ref.die = die;
28719 a->dw_attr_val.v.val_die_ref.external = 0;
28720 return 0;
28723 dw_attr_node *av = get_AT (die, DW_AT_location);
28724 dw_loc_list_ref d;
28725 bool non_dwarf_expr = false;
28727 if (av == NULL)
28728 return dwarf_strict ? -1 : 0;
28729 switch (AT_class (av))
28731 case dw_val_class_loc_list:
28732 for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
28733 if (d->expr && non_dwarf_expression (d->expr))
28734 non_dwarf_expr = true;
28735 break;
28736 case dw_val_class_loc:
28737 lv = AT_loc (av);
28738 if (lv == NULL)
28739 return dwarf_strict ? -1 : 0;
28740 if (non_dwarf_expression (lv))
28741 non_dwarf_expr = true;
28742 break;
28743 default:
28744 return dwarf_strict ? -1 : 0;
28747 /* If it is safe to transform DW_OP_GNU_variable_value DW_OP_stack_value
28748 into DW_OP_call4 or DW_OP_GNU_variable_value into
28749 DW_OP_call4 DW_OP_deref, do so. */
28750 if (!non_dwarf_expr
28751 && (l->dw_loc_next != NULL || AT_class (av) == dw_val_class_loc))
28753 l->dw_loc_opc = DW_OP_call4;
28754 if (l->dw_loc_next)
28755 l->dw_loc_next = NULL;
28756 else
28757 l->dw_loc_next = new_loc_descr (DW_OP_deref, 0, 0);
28758 return 0;
28761 /* For DW_OP_GNU_variable_value DW_OP_stack_value, we can just
28762 copy over the DW_AT_location attribute from die to a. */
28763 if (l->dw_loc_next != NULL)
28765 a->dw_attr_val = av->dw_attr_val;
28766 return 1;
28769 dw_loc_list_ref list, *p;
28770 switch (AT_class (av))
28772 case dw_val_class_loc_list:
28773 p = &list;
28774 list = NULL;
28775 for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
28777 lv = copy_deref_exprloc (d->expr);
28778 if (lv)
28780 *p = new_loc_list (lv, d->begin, d->end, d->section);
28781 p = &(*p)->dw_loc_next;
28783 else if (!dwarf_strict && d->expr)
28784 return 0;
28786 if (list == NULL)
28787 return dwarf_strict ? -1 : 0;
28788 a->dw_attr_val.val_class = dw_val_class_loc_list;
28789 gen_llsym (list);
28790 *AT_loc_list_ptr (a) = list;
28791 return 1;
28792 case dw_val_class_loc:
28793 lv = copy_deref_exprloc (AT_loc (av));
28794 if (lv == NULL)
28795 return dwarf_strict ? -1 : 0;
28796 a->dw_attr_val.v.val_loc = lv;
28797 return 1;
28798 default:
28799 gcc_unreachable ();
28803 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
28804 an address in .rodata section if the string literal is emitted there,
28805 or remove the containing location list or replace DW_AT_const_value
28806 with DW_AT_location and empty location expression, if it isn't found
28807 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
28808 to something that has been emitted in the current CU. */
28810 static void
28811 resolve_addr (dw_die_ref die)
28813 dw_die_ref c;
28814 dw_attr_node *a;
28815 dw_loc_list_ref *curr, *start, loc;
28816 unsigned ix;
28817 bool remove_AT_byte_size = false;
28819 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
28820 switch (AT_class (a))
28822 case dw_val_class_loc_list:
28823 start = curr = AT_loc_list_ptr (a);
28824 loc = *curr;
28825 gcc_assert (loc);
28826 /* The same list can be referenced more than once. See if we have
28827 already recorded the result from a previous pass. */
28828 if (loc->replaced)
28829 *curr = loc->dw_loc_next;
28830 else if (!loc->resolved_addr)
28832 /* As things stand, we do not expect or allow one die to
28833 reference a suffix of another die's location list chain.
28834 References must be identical or completely separate.
28835 There is therefore no need to cache the result of this
28836 pass on any list other than the first; doing so
28837 would lead to unnecessary writes. */
28838 while (*curr)
28840 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
28841 if (!resolve_addr_in_expr (a, (*curr)->expr))
28843 dw_loc_list_ref next = (*curr)->dw_loc_next;
28844 dw_loc_descr_ref l = (*curr)->expr;
28846 if (next && (*curr)->ll_symbol)
28848 gcc_assert (!next->ll_symbol);
28849 next->ll_symbol = (*curr)->ll_symbol;
28851 if (dwarf_split_debug_info)
28852 remove_loc_list_addr_table_entries (l);
28853 *curr = next;
28855 else
28857 mark_base_types ((*curr)->expr);
28858 curr = &(*curr)->dw_loc_next;
28861 if (loc == *start)
28862 loc->resolved_addr = 1;
28863 else
28865 loc->replaced = 1;
28866 loc->dw_loc_next = *start;
28869 if (!*start)
28871 remove_AT (die, a->dw_attr);
28872 ix--;
28874 break;
28875 case dw_val_class_loc:
28877 dw_loc_descr_ref l = AT_loc (a);
28878 /* DW_OP_GNU_variable_value DW_OP_stack_value or
28879 DW_OP_GNU_variable_value in DW_AT_string_length can be converted
28880 into DW_OP_call4 or DW_OP_call4 DW_OP_deref, which is standard
28881 DWARF4 unlike DW_OP_GNU_variable_value. Or for DWARF5
28882 DW_OP_GNU_variable_value DW_OP_stack_value can be replaced
28883 with DW_FORM_ref referencing the same DIE as
28884 DW_OP_GNU_variable_value used to reference. */
28885 if (a->dw_attr == DW_AT_string_length
28886 && l
28887 && l->dw_loc_opc == DW_OP_GNU_variable_value
28888 && (l->dw_loc_next == NULL
28889 || (l->dw_loc_next->dw_loc_next == NULL
28890 && l->dw_loc_next->dw_loc_opc == DW_OP_stack_value)))
28892 switch (optimize_string_length (a))
28894 case -1:
28895 remove_AT (die, a->dw_attr);
28896 ix--;
28897 /* If we drop DW_AT_string_length, we need to drop also
28898 DW_AT_{string_length_,}byte_size. */
28899 remove_AT_byte_size = true;
28900 continue;
28901 default:
28902 break;
28903 case 1:
28904 /* Even if we keep the optimized DW_AT_string_length,
28905 it might have changed AT_class, so process it again. */
28906 ix--;
28907 continue;
28910 /* For -gdwarf-2 don't attempt to optimize
28911 DW_AT_data_member_location containing
28912 DW_OP_plus_uconst - older consumers might
28913 rely on it being that op instead of a more complex,
28914 but shorter, location description. */
28915 if ((dwarf_version > 2
28916 || a->dw_attr != DW_AT_data_member_location
28917 || l == NULL
28918 || l->dw_loc_opc != DW_OP_plus_uconst
28919 || l->dw_loc_next != NULL)
28920 && !resolve_addr_in_expr (a, l))
28922 if (dwarf_split_debug_info)
28923 remove_loc_list_addr_table_entries (l);
28924 if (l != NULL
28925 && l->dw_loc_next == NULL
28926 && l->dw_loc_opc == DW_OP_addr
28927 && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
28928 && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
28929 && a->dw_attr == DW_AT_location)
28931 tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
28932 remove_AT (die, a->dw_attr);
28933 ix--;
28934 optimize_location_into_implicit_ptr (die, decl);
28935 break;
28937 if (a->dw_attr == DW_AT_string_length)
28938 /* If we drop DW_AT_string_length, we need to drop also
28939 DW_AT_{string_length_,}byte_size. */
28940 remove_AT_byte_size = true;
28941 remove_AT (die, a->dw_attr);
28942 ix--;
28944 else
28945 mark_base_types (l);
28947 break;
28948 case dw_val_class_addr:
28949 if (a->dw_attr == DW_AT_const_value
28950 && !resolve_one_addr (&a->dw_attr_val.v.val_addr))
28952 if (AT_index (a) != NOT_INDEXED)
28953 remove_addr_table_entry (a->dw_attr_val.val_entry);
28954 remove_AT (die, a->dw_attr);
28955 ix--;
28957 if ((die->die_tag == DW_TAG_call_site
28958 && a->dw_attr == DW_AT_call_origin)
28959 || (die->die_tag == DW_TAG_GNU_call_site
28960 && a->dw_attr == DW_AT_abstract_origin))
28962 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
28963 dw_die_ref tdie = lookup_decl_die (tdecl);
28964 dw_die_ref cdie;
28965 if (tdie == NULL
28966 && DECL_EXTERNAL (tdecl)
28967 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE
28968 && (cdie = lookup_context_die (DECL_CONTEXT (tdecl))))
28970 dw_die_ref pdie = cdie;
28971 /* Make sure we don't add these DIEs into type units.
28972 We could emit skeleton DIEs for context (namespaces,
28973 outer structs/classes) and a skeleton DIE for the
28974 innermost context with DW_AT_signature pointing to the
28975 type unit. See PR78835. */
28976 while (pdie && pdie->die_tag != DW_TAG_type_unit)
28977 pdie = pdie->die_parent;
28978 if (pdie == NULL)
28980 /* Creating a full DIE for tdecl is overly expensive and
28981 at this point even wrong when in the LTO phase
28982 as it can end up generating new type DIEs we didn't
28983 output and thus optimize_external_refs will crash. */
28984 tdie = new_die (DW_TAG_subprogram, cdie, NULL_TREE);
28985 add_AT_flag (tdie, DW_AT_external, 1);
28986 add_AT_flag (tdie, DW_AT_declaration, 1);
28987 add_linkage_attr (tdie, tdecl);
28988 add_name_and_src_coords_attributes (tdie, tdecl);
28989 equate_decl_number_to_die (tdecl, tdie);
28992 if (tdie)
28994 a->dw_attr_val.val_class = dw_val_class_die_ref;
28995 a->dw_attr_val.v.val_die_ref.die = tdie;
28996 a->dw_attr_val.v.val_die_ref.external = 0;
28998 else
29000 if (AT_index (a) != NOT_INDEXED)
29001 remove_addr_table_entry (a->dw_attr_val.val_entry);
29002 remove_AT (die, a->dw_attr);
29003 ix--;
29006 break;
29007 default:
29008 break;
29011 if (remove_AT_byte_size)
29012 remove_AT (die, dwarf_version >= 5
29013 ? DW_AT_string_length_byte_size
29014 : DW_AT_byte_size);
29016 FOR_EACH_CHILD (die, c, resolve_addr (c));
29019 /* Helper routines for optimize_location_lists.
29020 This pass tries to share identical local lists in .debug_loc
29021 section. */
29023 /* Iteratively hash operands of LOC opcode into HSTATE. */
29025 static void
29026 hash_loc_operands (dw_loc_descr_ref loc, inchash::hash &hstate)
29028 dw_val_ref val1 = &loc->dw_loc_oprnd1;
29029 dw_val_ref val2 = &loc->dw_loc_oprnd2;
29031 switch (loc->dw_loc_opc)
29033 case DW_OP_const4u:
29034 case DW_OP_const8u:
29035 if (loc->dtprel)
29036 goto hash_addr;
29037 /* FALLTHRU */
29038 case DW_OP_const1u:
29039 case DW_OP_const1s:
29040 case DW_OP_const2u:
29041 case DW_OP_const2s:
29042 case DW_OP_const4s:
29043 case DW_OP_const8s:
29044 case DW_OP_constu:
29045 case DW_OP_consts:
29046 case DW_OP_pick:
29047 case DW_OP_plus_uconst:
29048 case DW_OP_breg0:
29049 case DW_OP_breg1:
29050 case DW_OP_breg2:
29051 case DW_OP_breg3:
29052 case DW_OP_breg4:
29053 case DW_OP_breg5:
29054 case DW_OP_breg6:
29055 case DW_OP_breg7:
29056 case DW_OP_breg8:
29057 case DW_OP_breg9:
29058 case DW_OP_breg10:
29059 case DW_OP_breg11:
29060 case DW_OP_breg12:
29061 case DW_OP_breg13:
29062 case DW_OP_breg14:
29063 case DW_OP_breg15:
29064 case DW_OP_breg16:
29065 case DW_OP_breg17:
29066 case DW_OP_breg18:
29067 case DW_OP_breg19:
29068 case DW_OP_breg20:
29069 case DW_OP_breg21:
29070 case DW_OP_breg22:
29071 case DW_OP_breg23:
29072 case DW_OP_breg24:
29073 case DW_OP_breg25:
29074 case DW_OP_breg26:
29075 case DW_OP_breg27:
29076 case DW_OP_breg28:
29077 case DW_OP_breg29:
29078 case DW_OP_breg30:
29079 case DW_OP_breg31:
29080 case DW_OP_regx:
29081 case DW_OP_fbreg:
29082 case DW_OP_piece:
29083 case DW_OP_deref_size:
29084 case DW_OP_xderef_size:
29085 hstate.add_object (val1->v.val_int);
29086 break;
29087 case DW_OP_skip:
29088 case DW_OP_bra:
29090 int offset;
29092 gcc_assert (val1->val_class == dw_val_class_loc);
29093 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
29094 hstate.add_object (offset);
29096 break;
29097 case DW_OP_implicit_value:
29098 hstate.add_object (val1->v.val_unsigned);
29099 switch (val2->val_class)
29101 case dw_val_class_const:
29102 hstate.add_object (val2->v.val_int);
29103 break;
29104 case dw_val_class_vec:
29106 unsigned int elt_size = val2->v.val_vec.elt_size;
29107 unsigned int len = val2->v.val_vec.length;
29109 hstate.add_int (elt_size);
29110 hstate.add_int (len);
29111 hstate.add (val2->v.val_vec.array, len * elt_size);
29113 break;
29114 case dw_val_class_const_double:
29115 hstate.add_object (val2->v.val_double.low);
29116 hstate.add_object (val2->v.val_double.high);
29117 break;
29118 case dw_val_class_wide_int:
29119 hstate.add (val2->v.val_wide->get_val (),
29120 get_full_len (*val2->v.val_wide)
29121 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
29122 break;
29123 case dw_val_class_addr:
29124 inchash::add_rtx (val2->v.val_addr, hstate);
29125 break;
29126 default:
29127 gcc_unreachable ();
29129 break;
29130 case DW_OP_bregx:
29131 case DW_OP_bit_piece:
29132 hstate.add_object (val1->v.val_int);
29133 hstate.add_object (val2->v.val_int);
29134 break;
29135 case DW_OP_addr:
29136 hash_addr:
29137 if (loc->dtprel)
29139 unsigned char dtprel = 0xd1;
29140 hstate.add_object (dtprel);
29142 inchash::add_rtx (val1->v.val_addr, hstate);
29143 break;
29144 case DW_OP_GNU_addr_index:
29145 case DW_OP_GNU_const_index:
29147 if (loc->dtprel)
29149 unsigned char dtprel = 0xd1;
29150 hstate.add_object (dtprel);
29152 inchash::add_rtx (val1->val_entry->addr.rtl, hstate);
29154 break;
29155 case DW_OP_implicit_pointer:
29156 case DW_OP_GNU_implicit_pointer:
29157 hstate.add_int (val2->v.val_int);
29158 break;
29159 case DW_OP_entry_value:
29160 case DW_OP_GNU_entry_value:
29161 hstate.add_object (val1->v.val_loc);
29162 break;
29163 case DW_OP_regval_type:
29164 case DW_OP_deref_type:
29165 case DW_OP_GNU_regval_type:
29166 case DW_OP_GNU_deref_type:
29168 unsigned int byte_size
29169 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
29170 unsigned int encoding
29171 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
29172 hstate.add_object (val1->v.val_int);
29173 hstate.add_object (byte_size);
29174 hstate.add_object (encoding);
29176 break;
29177 case DW_OP_convert:
29178 case DW_OP_reinterpret:
29179 case DW_OP_GNU_convert:
29180 case DW_OP_GNU_reinterpret:
29181 if (val1->val_class == dw_val_class_unsigned_const)
29183 hstate.add_object (val1->v.val_unsigned);
29184 break;
29186 /* FALLTHRU */
29187 case DW_OP_const_type:
29188 case DW_OP_GNU_const_type:
29190 unsigned int byte_size
29191 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
29192 unsigned int encoding
29193 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
29194 hstate.add_object (byte_size);
29195 hstate.add_object (encoding);
29196 if (loc->dw_loc_opc != DW_OP_const_type
29197 && loc->dw_loc_opc != DW_OP_GNU_const_type)
29198 break;
29199 hstate.add_object (val2->val_class);
29200 switch (val2->val_class)
29202 case dw_val_class_const:
29203 hstate.add_object (val2->v.val_int);
29204 break;
29205 case dw_val_class_vec:
29207 unsigned int elt_size = val2->v.val_vec.elt_size;
29208 unsigned int len = val2->v.val_vec.length;
29210 hstate.add_object (elt_size);
29211 hstate.add_object (len);
29212 hstate.add (val2->v.val_vec.array, len * elt_size);
29214 break;
29215 case dw_val_class_const_double:
29216 hstate.add_object (val2->v.val_double.low);
29217 hstate.add_object (val2->v.val_double.high);
29218 break;
29219 case dw_val_class_wide_int:
29220 hstate.add (val2->v.val_wide->get_val (),
29221 get_full_len (*val2->v.val_wide)
29222 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
29223 break;
29224 default:
29225 gcc_unreachable ();
29228 break;
29230 default:
29231 /* Other codes have no operands. */
29232 break;
29236 /* Iteratively hash the whole DWARF location expression LOC into HSTATE. */
29238 static inline void
29239 hash_locs (dw_loc_descr_ref loc, inchash::hash &hstate)
29241 dw_loc_descr_ref l;
29242 bool sizes_computed = false;
29243 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
29244 size_of_locs (loc);
29246 for (l = loc; l != NULL; l = l->dw_loc_next)
29248 enum dwarf_location_atom opc = l->dw_loc_opc;
29249 hstate.add_object (opc);
29250 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
29252 size_of_locs (loc);
29253 sizes_computed = true;
29255 hash_loc_operands (l, hstate);
29259 /* Compute hash of the whole location list LIST_HEAD. */
29261 static inline void
29262 hash_loc_list (dw_loc_list_ref list_head)
29264 dw_loc_list_ref curr = list_head;
29265 inchash::hash hstate;
29267 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
29269 hstate.add (curr->begin, strlen (curr->begin) + 1);
29270 hstate.add (curr->end, strlen (curr->end) + 1);
29271 if (curr->section)
29272 hstate.add (curr->section, strlen (curr->section) + 1);
29273 hash_locs (curr->expr, hstate);
29275 list_head->hash = hstate.end ();
29278 /* Return true if X and Y opcodes have the same operands. */
29280 static inline bool
29281 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
29283 dw_val_ref valx1 = &x->dw_loc_oprnd1;
29284 dw_val_ref valx2 = &x->dw_loc_oprnd2;
29285 dw_val_ref valy1 = &y->dw_loc_oprnd1;
29286 dw_val_ref valy2 = &y->dw_loc_oprnd2;
29288 switch (x->dw_loc_opc)
29290 case DW_OP_const4u:
29291 case DW_OP_const8u:
29292 if (x->dtprel)
29293 goto hash_addr;
29294 /* FALLTHRU */
29295 case DW_OP_const1u:
29296 case DW_OP_const1s:
29297 case DW_OP_const2u:
29298 case DW_OP_const2s:
29299 case DW_OP_const4s:
29300 case DW_OP_const8s:
29301 case DW_OP_constu:
29302 case DW_OP_consts:
29303 case DW_OP_pick:
29304 case DW_OP_plus_uconst:
29305 case DW_OP_breg0:
29306 case DW_OP_breg1:
29307 case DW_OP_breg2:
29308 case DW_OP_breg3:
29309 case DW_OP_breg4:
29310 case DW_OP_breg5:
29311 case DW_OP_breg6:
29312 case DW_OP_breg7:
29313 case DW_OP_breg8:
29314 case DW_OP_breg9:
29315 case DW_OP_breg10:
29316 case DW_OP_breg11:
29317 case DW_OP_breg12:
29318 case DW_OP_breg13:
29319 case DW_OP_breg14:
29320 case DW_OP_breg15:
29321 case DW_OP_breg16:
29322 case DW_OP_breg17:
29323 case DW_OP_breg18:
29324 case DW_OP_breg19:
29325 case DW_OP_breg20:
29326 case DW_OP_breg21:
29327 case DW_OP_breg22:
29328 case DW_OP_breg23:
29329 case DW_OP_breg24:
29330 case DW_OP_breg25:
29331 case DW_OP_breg26:
29332 case DW_OP_breg27:
29333 case DW_OP_breg28:
29334 case DW_OP_breg29:
29335 case DW_OP_breg30:
29336 case DW_OP_breg31:
29337 case DW_OP_regx:
29338 case DW_OP_fbreg:
29339 case DW_OP_piece:
29340 case DW_OP_deref_size:
29341 case DW_OP_xderef_size:
29342 return valx1->v.val_int == valy1->v.val_int;
29343 case DW_OP_skip:
29344 case DW_OP_bra:
29345 /* If splitting debug info, the use of DW_OP_GNU_addr_index
29346 can cause irrelevant differences in dw_loc_addr. */
29347 gcc_assert (valx1->val_class == dw_val_class_loc
29348 && valy1->val_class == dw_val_class_loc
29349 && (dwarf_split_debug_info
29350 || x->dw_loc_addr == y->dw_loc_addr));
29351 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
29352 case DW_OP_implicit_value:
29353 if (valx1->v.val_unsigned != valy1->v.val_unsigned
29354 || valx2->val_class != valy2->val_class)
29355 return false;
29356 switch (valx2->val_class)
29358 case dw_val_class_const:
29359 return valx2->v.val_int == valy2->v.val_int;
29360 case dw_val_class_vec:
29361 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
29362 && valx2->v.val_vec.length == valy2->v.val_vec.length
29363 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
29364 valx2->v.val_vec.elt_size
29365 * valx2->v.val_vec.length) == 0;
29366 case dw_val_class_const_double:
29367 return valx2->v.val_double.low == valy2->v.val_double.low
29368 && valx2->v.val_double.high == valy2->v.val_double.high;
29369 case dw_val_class_wide_int:
29370 return *valx2->v.val_wide == *valy2->v.val_wide;
29371 case dw_val_class_addr:
29372 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
29373 default:
29374 gcc_unreachable ();
29376 case DW_OP_bregx:
29377 case DW_OP_bit_piece:
29378 return valx1->v.val_int == valy1->v.val_int
29379 && valx2->v.val_int == valy2->v.val_int;
29380 case DW_OP_addr:
29381 hash_addr:
29382 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
29383 case DW_OP_GNU_addr_index:
29384 case DW_OP_GNU_const_index:
29386 rtx ax1 = valx1->val_entry->addr.rtl;
29387 rtx ay1 = valy1->val_entry->addr.rtl;
29388 return rtx_equal_p (ax1, ay1);
29390 case DW_OP_implicit_pointer:
29391 case DW_OP_GNU_implicit_pointer:
29392 return valx1->val_class == dw_val_class_die_ref
29393 && valx1->val_class == valy1->val_class
29394 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
29395 && valx2->v.val_int == valy2->v.val_int;
29396 case DW_OP_entry_value:
29397 case DW_OP_GNU_entry_value:
29398 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
29399 case DW_OP_const_type:
29400 case DW_OP_GNU_const_type:
29401 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
29402 || valx2->val_class != valy2->val_class)
29403 return false;
29404 switch (valx2->val_class)
29406 case dw_val_class_const:
29407 return valx2->v.val_int == valy2->v.val_int;
29408 case dw_val_class_vec:
29409 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
29410 && valx2->v.val_vec.length == valy2->v.val_vec.length
29411 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
29412 valx2->v.val_vec.elt_size
29413 * valx2->v.val_vec.length) == 0;
29414 case dw_val_class_const_double:
29415 return valx2->v.val_double.low == valy2->v.val_double.low
29416 && valx2->v.val_double.high == valy2->v.val_double.high;
29417 case dw_val_class_wide_int:
29418 return *valx2->v.val_wide == *valy2->v.val_wide;
29419 default:
29420 gcc_unreachable ();
29422 case DW_OP_regval_type:
29423 case DW_OP_deref_type:
29424 case DW_OP_GNU_regval_type:
29425 case DW_OP_GNU_deref_type:
29426 return valx1->v.val_int == valy1->v.val_int
29427 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
29428 case DW_OP_convert:
29429 case DW_OP_reinterpret:
29430 case DW_OP_GNU_convert:
29431 case DW_OP_GNU_reinterpret:
29432 if (valx1->val_class != valy1->val_class)
29433 return false;
29434 if (valx1->val_class == dw_val_class_unsigned_const)
29435 return valx1->v.val_unsigned == valy1->v.val_unsigned;
29436 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
29437 case DW_OP_GNU_parameter_ref:
29438 return valx1->val_class == dw_val_class_die_ref
29439 && valx1->val_class == valy1->val_class
29440 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
29441 default:
29442 /* Other codes have no operands. */
29443 return true;
29447 /* Return true if DWARF location expressions X and Y are the same. */
29449 static inline bool
29450 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
29452 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
29453 if (x->dw_loc_opc != y->dw_loc_opc
29454 || x->dtprel != y->dtprel
29455 || !compare_loc_operands (x, y))
29456 break;
29457 return x == NULL && y == NULL;
29460 /* Hashtable helpers. */
29462 struct loc_list_hasher : nofree_ptr_hash <dw_loc_list_struct>
29464 static inline hashval_t hash (const dw_loc_list_struct *);
29465 static inline bool equal (const dw_loc_list_struct *,
29466 const dw_loc_list_struct *);
29469 /* Return precomputed hash of location list X. */
29471 inline hashval_t
29472 loc_list_hasher::hash (const dw_loc_list_struct *x)
29474 return x->hash;
29477 /* Return true if location lists A and B are the same. */
29479 inline bool
29480 loc_list_hasher::equal (const dw_loc_list_struct *a,
29481 const dw_loc_list_struct *b)
29483 if (a == b)
29484 return 1;
29485 if (a->hash != b->hash)
29486 return 0;
29487 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
29488 if (strcmp (a->begin, b->begin) != 0
29489 || strcmp (a->end, b->end) != 0
29490 || (a->section == NULL) != (b->section == NULL)
29491 || (a->section && strcmp (a->section, b->section) != 0)
29492 || !compare_locs (a->expr, b->expr))
29493 break;
29494 return a == NULL && b == NULL;
29497 typedef hash_table<loc_list_hasher> loc_list_hash_type;
29500 /* Recursively optimize location lists referenced from DIE
29501 children and share them whenever possible. */
29503 static void
29504 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type *htab)
29506 dw_die_ref c;
29507 dw_attr_node *a;
29508 unsigned ix;
29509 dw_loc_list_struct **slot;
29511 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
29512 if (AT_class (a) == dw_val_class_loc_list)
29514 dw_loc_list_ref list = AT_loc_list (a);
29515 /* TODO: perform some optimizations here, before hashing
29516 it and storing into the hash table. */
29517 hash_loc_list (list);
29518 slot = htab->find_slot_with_hash (list, list->hash, INSERT);
29519 if (*slot == NULL)
29520 *slot = list;
29521 else
29522 a->dw_attr_val.v.val_loc_list = *slot;
29525 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
29529 /* Recursively assign each location list a unique index into the debug_addr
29530 section. */
29532 static void
29533 index_location_lists (dw_die_ref die)
29535 dw_die_ref c;
29536 dw_attr_node *a;
29537 unsigned ix;
29539 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
29540 if (AT_class (a) == dw_val_class_loc_list)
29542 dw_loc_list_ref list = AT_loc_list (a);
29543 dw_loc_list_ref curr;
29544 for (curr = list; curr != NULL; curr = curr->dw_loc_next)
29546 /* Don't index an entry that has already been indexed
29547 or won't be output. */
29548 if (curr->begin_entry != NULL
29549 || (strcmp (curr->begin, curr->end) == 0 && !curr->force))
29550 continue;
29552 curr->begin_entry
29553 = add_addr_table_entry (xstrdup (curr->begin), ate_kind_label);
29557 FOR_EACH_CHILD (die, c, index_location_lists (c));
29560 /* Optimize location lists referenced from DIE
29561 children and share them whenever possible. */
29563 static void
29564 optimize_location_lists (dw_die_ref die)
29566 loc_list_hash_type htab (500);
29567 optimize_location_lists_1 (die, &htab);
29570 /* Traverse the limbo die list, and add parent/child links. The only
29571 dies without parents that should be here are concrete instances of
29572 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
29573 For concrete instances, we can get the parent die from the abstract
29574 instance. */
29576 static void
29577 flush_limbo_die_list (void)
29579 limbo_die_node *node;
29581 /* get_context_die calls force_decl_die, which can put new DIEs on the
29582 limbo list in LTO mode when nested functions are put in a different
29583 partition than that of their parent function. */
29584 while ((node = limbo_die_list))
29586 dw_die_ref die = node->die;
29587 limbo_die_list = node->next;
29589 if (die->die_parent == NULL)
29591 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
29593 if (origin && origin->die_parent)
29594 add_child_die (origin->die_parent, die);
29595 else if (is_cu_die (die))
29597 else if (seen_error ())
29598 /* It's OK to be confused by errors in the input. */
29599 add_child_die (comp_unit_die (), die);
29600 else
29602 /* In certain situations, the lexical block containing a
29603 nested function can be optimized away, which results
29604 in the nested function die being orphaned. Likewise
29605 with the return type of that nested function. Force
29606 this to be a child of the containing function.
29608 It may happen that even the containing function got fully
29609 inlined and optimized out. In that case we are lost and
29610 assign the empty child. This should not be big issue as
29611 the function is likely unreachable too. */
29612 gcc_assert (node->created_for);
29614 if (DECL_P (node->created_for))
29615 origin = get_context_die (DECL_CONTEXT (node->created_for));
29616 else if (TYPE_P (node->created_for))
29617 origin = scope_die_for (node->created_for, comp_unit_die ());
29618 else
29619 origin = comp_unit_die ();
29621 add_child_die (origin, die);
29627 /* Output stuff that dwarf requires at the end of every file,
29628 and generate the DWARF-2 debugging info. */
29630 static void
29631 dwarf2out_finish (const char *)
29633 comdat_type_node *ctnode;
29634 dw_die_ref main_comp_unit_die;
29635 unsigned char checksum[16];
29637 /* Flush out any latecomers to the limbo party. */
29638 flush_limbo_die_list ();
29640 if (flag_checking)
29642 verify_die (comp_unit_die ());
29643 for (limbo_die_node *node = cu_die_list; node; node = node->next)
29644 verify_die (node->die);
29647 /* We shouldn't have any symbols with delayed asm names for
29648 DIEs generated after early finish. */
29649 gcc_assert (deferred_asm_name == NULL);
29651 gen_remaining_tmpl_value_param_die_attribute ();
29653 #if ENABLE_ASSERT_CHECKING
29655 dw_die_ref die = comp_unit_die (), c;
29656 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
29658 #endif
29659 resolve_addr (comp_unit_die ());
29660 move_marked_base_types ();
29662 /* Initialize sections and labels used for actual assembler output. */
29663 init_sections_and_labels ();
29665 /* Traverse the DIE's and add sibling attributes to those DIE's that
29666 have children. */
29667 add_sibling_attributes (comp_unit_die ());
29668 limbo_die_node *node;
29669 for (node = cu_die_list; node; node = node->next)
29670 add_sibling_attributes (node->die);
29671 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
29672 add_sibling_attributes (ctnode->root_die);
29674 /* When splitting DWARF info, we put some attributes in the
29675 skeleton compile_unit DIE that remains in the .o, while
29676 most attributes go in the DWO compile_unit_die. */
29677 if (dwarf_split_debug_info)
29679 limbo_die_node *cu;
29680 main_comp_unit_die = gen_compile_unit_die (NULL);
29681 if (dwarf_version >= 5)
29682 main_comp_unit_die->die_tag = DW_TAG_skeleton_unit;
29683 cu = limbo_die_list;
29684 gcc_assert (cu->die == main_comp_unit_die);
29685 limbo_die_list = limbo_die_list->next;
29686 cu->next = cu_die_list;
29687 cu_die_list = cu;
29689 else
29690 main_comp_unit_die = comp_unit_die ();
29692 /* Output a terminator label for the .text section. */
29693 switch_to_section (text_section);
29694 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
29695 if (cold_text_section)
29697 switch_to_section (cold_text_section);
29698 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
29701 /* We can only use the low/high_pc attributes if all of the code was
29702 in .text. */
29703 if (!have_multiple_function_sections
29704 || (dwarf_version < 3 && dwarf_strict))
29706 /* Don't add if the CU has no associated code. */
29707 if (text_section_used)
29708 add_AT_low_high_pc (main_comp_unit_die, text_section_label,
29709 text_end_label, true);
29711 else
29713 unsigned fde_idx;
29714 dw_fde_ref fde;
29715 bool range_list_added = false;
29717 if (text_section_used)
29718 add_ranges_by_labels (main_comp_unit_die, text_section_label,
29719 text_end_label, &range_list_added, true);
29720 if (cold_text_section_used)
29721 add_ranges_by_labels (main_comp_unit_die, cold_text_section_label,
29722 cold_end_label, &range_list_added, true);
29724 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
29726 if (DECL_IGNORED_P (fde->decl))
29727 continue;
29728 if (!fde->in_std_section)
29729 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
29730 fde->dw_fde_end, &range_list_added,
29731 true);
29732 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
29733 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
29734 fde->dw_fde_second_end, &range_list_added,
29735 true);
29738 if (range_list_added)
29740 /* We need to give .debug_loc and .debug_ranges an appropriate
29741 "base address". Use zero so that these addresses become
29742 absolute. Historically, we've emitted the unexpected
29743 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
29744 Emit both to give time for other tools to adapt. */
29745 add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
29746 if (! dwarf_strict && dwarf_version < 4)
29747 add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
29749 add_ranges (NULL);
29753 if (debug_info_level >= DINFO_LEVEL_TERSE)
29754 add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
29755 debug_line_section_label);
29757 if (have_macinfo)
29758 add_AT_macptr (comp_unit_die (),
29759 dwarf_version >= 5 ? DW_AT_macros
29760 : dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros,
29761 macinfo_section_label);
29763 if (dwarf_split_debug_info)
29765 if (have_location_lists)
29767 if (dwarf_version >= 5)
29768 add_AT_loclistsptr (comp_unit_die (), DW_AT_loclists_base,
29769 loc_section_label);
29770 /* optimize_location_lists calculates the size of the lists,
29771 so index them first, and assign indices to the entries.
29772 Although optimize_location_lists will remove entries from
29773 the table, it only does so for duplicates, and therefore
29774 only reduces ref_counts to 1. */
29775 index_location_lists (comp_unit_die ());
29778 if (addr_index_table != NULL)
29780 unsigned int index = 0;
29781 addr_index_table
29782 ->traverse_noresize<unsigned int *, index_addr_table_entry>
29783 (&index);
29787 loc_list_idx = 0;
29788 if (have_location_lists)
29790 optimize_location_lists (comp_unit_die ());
29791 /* And finally assign indexes to the entries for -gsplit-dwarf. */
29792 if (dwarf_version >= 5 && dwarf_split_debug_info)
29793 assign_location_list_indexes (comp_unit_die ());
29796 save_macinfo_strings ();
29798 if (dwarf_split_debug_info)
29800 unsigned int index = 0;
29802 /* Add attributes common to skeleton compile_units and
29803 type_units. Because these attributes include strings, it
29804 must be done before freezing the string table. Top-level
29805 skeleton die attrs are added when the skeleton type unit is
29806 created, so ensure it is created by this point. */
29807 add_top_level_skeleton_die_attrs (main_comp_unit_die);
29808 debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
29811 /* Output all of the compilation units. We put the main one last so that
29812 the offsets are available to output_pubnames. */
29813 for (node = cu_die_list; node; node = node->next)
29814 output_comp_unit (node->die, 0, NULL);
29816 hash_table<comdat_type_hasher> comdat_type_table (100);
29817 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
29819 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
29821 /* Don't output duplicate types. */
29822 if (*slot != HTAB_EMPTY_ENTRY)
29823 continue;
29825 /* Add a pointer to the line table for the main compilation unit
29826 so that the debugger can make sense of DW_AT_decl_file
29827 attributes. */
29828 if (debug_info_level >= DINFO_LEVEL_TERSE)
29829 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
29830 (!dwarf_split_debug_info
29831 ? debug_line_section_label
29832 : debug_skeleton_line_section_label));
29834 output_comdat_type_unit (ctnode);
29835 *slot = ctnode;
29838 /* The AT_pubnames attribute needs to go in all skeleton dies, including
29839 both the main_cu and all skeleton TUs. Making this call unconditional
29840 would end up either adding a second copy of the AT_pubnames attribute, or
29841 requiring a special case in add_top_level_skeleton_die_attrs. */
29842 if (!dwarf_split_debug_info)
29843 add_AT_pubnames (comp_unit_die ());
29845 if (dwarf_split_debug_info)
29847 int mark;
29848 struct md5_ctx ctx;
29850 if (dwarf_version >= 5 && !vec_safe_is_empty (ranges_table))
29851 index_rnglists ();
29853 /* Compute a checksum of the comp_unit to use as the dwo_id. */
29854 md5_init_ctx (&ctx);
29855 mark = 0;
29856 die_checksum (comp_unit_die (), &ctx, &mark);
29857 unmark_all_dies (comp_unit_die ());
29858 md5_finish_ctx (&ctx, checksum);
29860 if (dwarf_version < 5)
29862 /* Use the first 8 bytes of the checksum as the dwo_id,
29863 and add it to both comp-unit DIEs. */
29864 add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
29865 add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
29868 /* Add the base offset of the ranges table to the skeleton
29869 comp-unit DIE. */
29870 if (!vec_safe_is_empty (ranges_table))
29872 if (dwarf_version >= 5)
29873 add_AT_lineptr (main_comp_unit_die, DW_AT_rnglists_base,
29874 ranges_base_label);
29875 else
29876 add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
29877 ranges_section_label);
29880 switch_to_section (debug_addr_section);
29881 ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
29882 output_addr_table ();
29885 /* Output the main compilation unit if non-empty or if .debug_macinfo
29886 or .debug_macro will be emitted. */
29887 output_comp_unit (comp_unit_die (), have_macinfo,
29888 dwarf_split_debug_info ? checksum : NULL);
29890 if (dwarf_split_debug_info && info_section_emitted)
29891 output_skeleton_debug_sections (main_comp_unit_die, checksum);
29893 /* Output the abbreviation table. */
29894 if (vec_safe_length (abbrev_die_table) != 1)
29896 switch_to_section (debug_abbrev_section);
29897 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
29898 output_abbrev_section ();
29901 /* Output location list section if necessary. */
29902 if (have_location_lists)
29904 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
29905 char l2[MAX_ARTIFICIAL_LABEL_BYTES];
29906 /* Output the location lists info. */
29907 switch_to_section (debug_loc_section);
29908 if (dwarf_version >= 5)
29910 ASM_GENERATE_INTERNAL_LABEL (l1, DEBUG_LOC_SECTION_LABEL, 1);
29911 ASM_GENERATE_INTERNAL_LABEL (l2, DEBUG_LOC_SECTION_LABEL, 2);
29912 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
29913 dw2_asm_output_data (4, 0xffffffff,
29914 "Initial length escape value indicating "
29915 "64-bit DWARF extension");
29916 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
29917 "Length of Location Lists");
29918 ASM_OUTPUT_LABEL (asm_out_file, l1);
29919 dw2_asm_output_data (2, dwarf_version, "DWARF Version");
29920 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
29921 dw2_asm_output_data (1, 0, "Segment Size");
29922 dw2_asm_output_data (4, dwarf_split_debug_info ? loc_list_idx : 0,
29923 "Offset Entry Count");
29925 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
29926 if (dwarf_version >= 5 && dwarf_split_debug_info)
29928 unsigned int save_loc_list_idx = loc_list_idx;
29929 loc_list_idx = 0;
29930 output_loclists_offsets (comp_unit_die ());
29931 gcc_assert (save_loc_list_idx == loc_list_idx);
29933 output_location_lists (comp_unit_die ());
29934 if (dwarf_version >= 5)
29935 ASM_OUTPUT_LABEL (asm_out_file, l2);
29938 output_pubtables ();
29940 /* Output the address range information if a CU (.debug_info section)
29941 was emitted. We output an empty table even if we had no functions
29942 to put in it. This because the consumer has no way to tell the
29943 difference between an empty table that we omitted and failure to
29944 generate a table that would have contained data. */
29945 if (info_section_emitted)
29947 switch_to_section (debug_aranges_section);
29948 output_aranges ();
29951 /* Output ranges section if necessary. */
29952 if (!vec_safe_is_empty (ranges_table))
29954 if (dwarf_version >= 5)
29955 output_rnglists ();
29956 else
29957 output_ranges ();
29960 /* Have to end the macro section. */
29961 if (have_macinfo)
29963 switch_to_section (debug_macinfo_section);
29964 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
29965 output_macinfo ();
29966 dw2_asm_output_data (1, 0, "End compilation unit");
29969 /* Output the source line correspondence table. We must do this
29970 even if there is no line information. Otherwise, on an empty
29971 translation unit, we will generate a present, but empty,
29972 .debug_info section. IRIX 6.5 `nm' will then complain when
29973 examining the file. This is done late so that any filenames
29974 used by the debug_info section are marked as 'used'. */
29975 switch_to_section (debug_line_section);
29976 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
29977 if (! DWARF2_ASM_LINE_DEBUG_INFO)
29978 output_line_info (false);
29980 if (dwarf_split_debug_info && info_section_emitted)
29982 switch_to_section (debug_skeleton_line_section);
29983 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
29984 output_line_info (true);
29987 /* If we emitted any indirect strings, output the string table too. */
29988 if (debug_str_hash || skeleton_debug_str_hash)
29989 output_indirect_strings ();
29990 if (debug_line_str_hash)
29992 switch_to_section (debug_line_str_section);
29993 const enum dwarf_form form = DW_FORM_line_strp;
29994 debug_line_str_hash->traverse<enum dwarf_form,
29995 output_indirect_string> (form);
29999 /* Returns a hash value for X (which really is a variable_value_struct). */
30001 inline hashval_t
30002 variable_value_hasher::hash (variable_value_struct *x)
30004 return (hashval_t) x->decl_id;
30007 /* Return nonzero if decl_id of variable_value_struct X is the same as
30008 UID of decl Y. */
30010 inline bool
30011 variable_value_hasher::equal (variable_value_struct *x, tree y)
30013 return x->decl_id == DECL_UID (y);
30016 /* Helper function for resolve_variable_value, handle
30017 DW_OP_GNU_variable_value in one location expression.
30018 Return true if exprloc has been changed into loclist. */
30020 static bool
30021 resolve_variable_value_in_expr (dw_attr_node *a, dw_loc_descr_ref loc)
30023 dw_loc_descr_ref next;
30024 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = next)
30026 next = loc->dw_loc_next;
30027 if (loc->dw_loc_opc != DW_OP_GNU_variable_value
30028 || loc->dw_loc_oprnd1.val_class != dw_val_class_decl_ref)
30029 continue;
30031 tree decl = loc->dw_loc_oprnd1.v.val_decl_ref;
30032 if (DECL_CONTEXT (decl) != current_function_decl)
30033 continue;
30035 dw_die_ref ref = lookup_decl_die (decl);
30036 if (ref)
30038 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30039 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
30040 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
30041 continue;
30043 dw_loc_list_ref l = loc_list_from_tree (decl, 0, NULL);
30044 if (l == NULL)
30045 continue;
30046 if (l->dw_loc_next)
30048 if (AT_class (a) != dw_val_class_loc)
30049 continue;
30050 switch (a->dw_attr)
30052 /* Following attributes allow both exprloc and loclist
30053 classes, so we can change them into a loclist. */
30054 case DW_AT_location:
30055 case DW_AT_string_length:
30056 case DW_AT_return_addr:
30057 case DW_AT_data_member_location:
30058 case DW_AT_frame_base:
30059 case DW_AT_segment:
30060 case DW_AT_static_link:
30061 case DW_AT_use_location:
30062 case DW_AT_vtable_elem_location:
30063 if (prev)
30065 prev->dw_loc_next = NULL;
30066 prepend_loc_descr_to_each (l, AT_loc (a));
30068 if (next)
30069 add_loc_descr_to_each (l, next);
30070 a->dw_attr_val.val_class = dw_val_class_loc_list;
30071 a->dw_attr_val.val_entry = NULL;
30072 a->dw_attr_val.v.val_loc_list = l;
30073 have_location_lists = true;
30074 return true;
30075 /* Following attributes allow both exprloc and reference,
30076 so if the whole expression is DW_OP_GNU_variable_value alone
30077 we could transform it into reference. */
30078 case DW_AT_byte_size:
30079 case DW_AT_bit_size:
30080 case DW_AT_lower_bound:
30081 case DW_AT_upper_bound:
30082 case DW_AT_bit_stride:
30083 case DW_AT_count:
30084 case DW_AT_allocated:
30085 case DW_AT_associated:
30086 case DW_AT_byte_stride:
30087 if (prev == NULL && next == NULL)
30088 break;
30089 /* FALLTHRU */
30090 default:
30091 if (dwarf_strict)
30092 continue;
30093 break;
30095 /* Create DW_TAG_variable that we can refer to. */
30096 ref = gen_decl_die (decl, NULL_TREE, NULL,
30097 lookup_decl_die (current_function_decl));
30098 if (ref)
30100 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30101 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
30102 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
30104 continue;
30106 if (prev)
30108 prev->dw_loc_next = l->expr;
30109 add_loc_descr (&prev->dw_loc_next, next);
30110 free_loc_descr (loc, NULL);
30111 next = prev->dw_loc_next;
30113 else
30115 memcpy (loc, l->expr, sizeof (dw_loc_descr_node));
30116 add_loc_descr (&loc, next);
30117 next = loc;
30119 loc = prev;
30121 return false;
30124 /* Attempt to resolve DW_OP_GNU_variable_value using loc_list_from_tree. */
30126 static void
30127 resolve_variable_value (dw_die_ref die)
30129 dw_attr_node *a;
30130 dw_loc_list_ref loc;
30131 unsigned ix;
30133 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
30134 switch (AT_class (a))
30136 case dw_val_class_loc:
30137 if (!resolve_variable_value_in_expr (a, AT_loc (a)))
30138 break;
30139 /* FALLTHRU */
30140 case dw_val_class_loc_list:
30141 loc = AT_loc_list (a);
30142 gcc_assert (loc);
30143 for (; loc; loc = loc->dw_loc_next)
30144 resolve_variable_value_in_expr (a, loc->expr);
30145 break;
30146 default:
30147 break;
30151 /* Attempt to optimize DW_OP_GNU_variable_value refering to
30152 temporaries in the current function. */
30154 static void
30155 resolve_variable_values (void)
30157 if (!variable_value_hash || !current_function_decl)
30158 return;
30160 struct variable_value_struct *node
30161 = variable_value_hash->find_with_hash (current_function_decl,
30162 DECL_UID (current_function_decl));
30164 if (node == NULL)
30165 return;
30167 unsigned int i;
30168 dw_die_ref die;
30169 FOR_EACH_VEC_SAFE_ELT (node->dies, i, die)
30170 resolve_variable_value (die);
30173 /* Helper function for note_variable_value, handle one location
30174 expression. */
30176 static void
30177 note_variable_value_in_expr (dw_die_ref die, dw_loc_descr_ref loc)
30179 for (; loc; loc = loc->dw_loc_next)
30180 if (loc->dw_loc_opc == DW_OP_GNU_variable_value
30181 && loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
30183 tree decl = loc->dw_loc_oprnd1.v.val_decl_ref;
30184 dw_die_ref ref = lookup_decl_die (decl);
30185 if (ref)
30187 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30188 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
30189 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
30191 if (VAR_P (decl)
30192 && DECL_CONTEXT (decl)
30193 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL
30194 && lookup_decl_die (DECL_CONTEXT (decl)))
30196 if (!variable_value_hash)
30197 variable_value_hash
30198 = hash_table<variable_value_hasher>::create_ggc (10);
30200 tree fndecl = DECL_CONTEXT (decl);
30201 struct variable_value_struct *node;
30202 struct variable_value_struct **slot
30203 = variable_value_hash->find_slot_with_hash (fndecl,
30204 DECL_UID (fndecl),
30205 INSERT);
30206 if (*slot == NULL)
30208 node = ggc_cleared_alloc<variable_value_struct> ();
30209 node->decl_id = DECL_UID (fndecl);
30210 *slot = node;
30212 else
30213 node = *slot;
30215 vec_safe_push (node->dies, die);
30220 /* Walk the tree DIE and note DIEs with DW_OP_GNU_variable_value still
30221 with dw_val_class_decl_ref operand. */
30223 static void
30224 note_variable_value (dw_die_ref die)
30226 dw_die_ref c;
30227 dw_attr_node *a;
30228 dw_loc_list_ref loc;
30229 unsigned ix;
30231 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
30232 switch (AT_class (a))
30234 case dw_val_class_loc_list:
30235 loc = AT_loc_list (a);
30236 gcc_assert (loc);
30237 if (!loc->noted_variable_value)
30239 loc->noted_variable_value = 1;
30240 for (; loc; loc = loc->dw_loc_next)
30241 note_variable_value_in_expr (die, loc->expr);
30243 break;
30244 case dw_val_class_loc:
30245 note_variable_value_in_expr (die, AT_loc (a));
30246 break;
30247 default:
30248 break;
30251 /* Mark children. */
30252 FOR_EACH_CHILD (die, c, note_variable_value (c));
30255 /* Perform any cleanups needed after the early debug generation pass
30256 has run. */
30258 static void
30259 dwarf2out_early_finish (const char *filename)
30261 set_early_dwarf s;
30263 /* PCH might result in DW_AT_producer string being restored from the
30264 header compilation, so always fill it with empty string initially
30265 and overwrite only here. */
30266 dw_attr_node *producer = get_AT (comp_unit_die (), DW_AT_producer);
30267 producer_string = gen_producer_string ();
30268 producer->dw_attr_val.v.val_str->refcount--;
30269 producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
30271 /* Add the name for the main input file now. We delayed this from
30272 dwarf2out_init to avoid complications with PCH. */
30273 add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
30274 add_comp_dir_attribute (comp_unit_die ());
30276 /* When emitting DWARF5 .debug_line_str, move DW_AT_name and
30277 DW_AT_comp_dir into .debug_line_str section. */
30278 if (!DWARF2_ASM_LINE_DEBUG_INFO
30279 && dwarf_version >= 5
30280 && DWARF5_USE_DEBUG_LINE_STR)
30282 for (int i = 0; i < 2; i++)
30284 dw_attr_node *a = get_AT (comp_unit_die (),
30285 i ? DW_AT_comp_dir : DW_AT_name);
30286 if (a == NULL
30287 || AT_class (a) != dw_val_class_str
30288 || strlen (AT_string (a)) + 1 <= DWARF_OFFSET_SIZE)
30289 continue;
30291 if (! debug_line_str_hash)
30292 debug_line_str_hash
30293 = hash_table<indirect_string_hasher>::create_ggc (10);
30295 struct indirect_string_node *node
30296 = find_AT_string_in_table (AT_string (a), debug_line_str_hash);
30297 set_indirect_string (node);
30298 node->form = DW_FORM_line_strp;
30299 a->dw_attr_val.v.val_str->refcount--;
30300 a->dw_attr_val.v.val_str = node;
30304 /* With LTO early dwarf was really finished at compile-time, so make
30305 sure to adjust the phase after annotating the LTRANS CU DIE. */
30306 if (in_lto_p)
30308 early_dwarf_finished = true;
30309 return;
30312 /* Walk through the list of incomplete types again, trying once more to
30313 emit full debugging info for them. */
30314 retry_incomplete_types ();
30316 /* The point here is to flush out the limbo list so that it is empty
30317 and we don't need to stream it for LTO. */
30318 flush_limbo_die_list ();
30320 gen_scheduled_generic_parms_dies ();
30321 gen_remaining_tmpl_value_param_die_attribute ();
30323 /* Add DW_AT_linkage_name for all deferred DIEs. */
30324 for (limbo_die_node *node = deferred_asm_name; node; node = node->next)
30326 tree decl = node->created_for;
30327 if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
30328 /* A missing DECL_ASSEMBLER_NAME can be a constant DIE that
30329 ended up in deferred_asm_name before we knew it was
30330 constant and never written to disk. */
30331 && DECL_ASSEMBLER_NAME (decl))
30333 add_linkage_attr (node->die, decl);
30334 move_linkage_attr (node->die);
30337 deferred_asm_name = NULL;
30339 if (flag_eliminate_unused_debug_types)
30340 prune_unused_types ();
30342 /* Generate separate COMDAT sections for type DIEs. */
30343 if (use_debug_types)
30345 break_out_comdat_types (comp_unit_die ());
30347 /* Each new type_unit DIE was added to the limbo die list when created.
30348 Since these have all been added to comdat_type_list, clear the
30349 limbo die list. */
30350 limbo_die_list = NULL;
30352 /* For each new comdat type unit, copy declarations for incomplete
30353 types to make the new unit self-contained (i.e., no direct
30354 references to the main compile unit). */
30355 for (comdat_type_node *ctnode = comdat_type_list;
30356 ctnode != NULL; ctnode = ctnode->next)
30357 copy_decls_for_unworthy_types (ctnode->root_die);
30358 copy_decls_for_unworthy_types (comp_unit_die ());
30360 /* In the process of copying declarations from one unit to another,
30361 we may have left some declarations behind that are no longer
30362 referenced. Prune them. */
30363 prune_unused_types ();
30366 /* Generate separate CUs for each of the include files we've seen.
30367 They will go into limbo_die_list and from there to cu_die_list. */
30368 if (flag_eliminate_dwarf2_dups)
30370 gcc_assert (limbo_die_list == NULL);
30371 break_out_includes (comp_unit_die ());
30372 limbo_die_node *cu;
30373 while ((cu = limbo_die_list))
30375 limbo_die_list = cu->next;
30376 cu->next = cu_die_list;
30377 cu_die_list = cu;
30381 /* Traverse the DIE's and note DIEs with DW_OP_GNU_variable_value still
30382 with dw_val_class_decl_ref operand. */
30383 note_variable_value (comp_unit_die ());
30384 for (limbo_die_node *node = cu_die_list; node; node = node->next)
30385 note_variable_value (node->die);
30386 for (comdat_type_node *ctnode = comdat_type_list; ctnode != NULL;
30387 ctnode = ctnode->next)
30388 note_variable_value (ctnode->root_die);
30389 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
30390 note_variable_value (node->die);
30392 /* The early debug phase is now finished. */
30393 early_dwarf_finished = true;
30396 /* Reset all state within dwarf2out.c so that we can rerun the compiler
30397 within the same process. For use by toplev::finalize. */
30399 void
30400 dwarf2out_c_finalize (void)
30402 last_var_location_insn = NULL;
30403 cached_next_real_insn = NULL;
30404 used_rtx_array = NULL;
30405 incomplete_types = NULL;
30406 decl_scope_table = NULL;
30407 debug_info_section = NULL;
30408 debug_skeleton_info_section = NULL;
30409 debug_abbrev_section = NULL;
30410 debug_skeleton_abbrev_section = NULL;
30411 debug_aranges_section = NULL;
30412 debug_addr_section = NULL;
30413 debug_macinfo_section = NULL;
30414 debug_line_section = NULL;
30415 debug_skeleton_line_section = NULL;
30416 debug_loc_section = NULL;
30417 debug_pubnames_section = NULL;
30418 debug_pubtypes_section = NULL;
30419 debug_str_section = NULL;
30420 debug_line_str_section = NULL;
30421 debug_str_dwo_section = NULL;
30422 debug_str_offsets_section = NULL;
30423 debug_ranges_section = NULL;
30424 debug_frame_section = NULL;
30425 fde_vec = NULL;
30426 debug_str_hash = NULL;
30427 debug_line_str_hash = NULL;
30428 skeleton_debug_str_hash = NULL;
30429 dw2_string_counter = 0;
30430 have_multiple_function_sections = false;
30431 text_section_used = false;
30432 cold_text_section_used = false;
30433 cold_text_section = NULL;
30434 current_unit_personality = NULL;
30436 early_dwarf = false;
30437 early_dwarf_finished = false;
30439 next_die_offset = 0;
30440 single_comp_unit_die = NULL;
30441 comdat_type_list = NULL;
30442 limbo_die_list = NULL;
30443 file_table = NULL;
30444 decl_die_table = NULL;
30445 common_block_die_table = NULL;
30446 decl_loc_table = NULL;
30447 call_arg_locations = NULL;
30448 call_arg_loc_last = NULL;
30449 call_site_count = -1;
30450 tail_call_site_count = -1;
30451 cached_dw_loc_list_table = NULL;
30452 abbrev_die_table = NULL;
30453 delete dwarf_proc_stack_usage_map;
30454 dwarf_proc_stack_usage_map = NULL;
30455 line_info_label_num = 0;
30456 cur_line_info_table = NULL;
30457 text_section_line_info = NULL;
30458 cold_text_section_line_info = NULL;
30459 separate_line_info = NULL;
30460 info_section_emitted = false;
30461 pubname_table = NULL;
30462 pubtype_table = NULL;
30463 macinfo_table = NULL;
30464 ranges_table = NULL;
30465 ranges_by_label = NULL;
30466 rnglist_idx = 0;
30467 have_location_lists = false;
30468 loclabel_num = 0;
30469 poc_label_num = 0;
30470 last_emitted_file = NULL;
30471 label_num = 0;
30472 tmpl_value_parm_die_table = NULL;
30473 generic_type_instances = NULL;
30474 frame_pointer_fb_offset = 0;
30475 frame_pointer_fb_offset_valid = false;
30476 base_types.release ();
30477 XDELETEVEC (producer_string);
30478 producer_string = NULL;
30481 #include "gt-dwarf2out.h"