[testsuite] Require shared effective target for some lto.exp tests
[official-gcc.git] / gcc / dwarf2out.c
blob46565705506dc97389487fa51dfe5a75ebec1ed9
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, const char *, int, bool);
97 static rtx_insn *last_var_location_insn;
98 static rtx_insn *cached_next_real_insn;
99 static void dwarf2out_decl (tree);
101 #ifndef XCOFF_DEBUGGING_INFO
102 #define XCOFF_DEBUGGING_INFO 0
103 #endif
105 #ifndef HAVE_XCOFF_DWARF_EXTRAS
106 #define HAVE_XCOFF_DWARF_EXTRAS 0
107 #endif
109 #ifdef VMS_DEBUGGING_INFO
110 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
112 /* Define this macro to be a nonzero value if the directory specifications
113 which are output in the debug info should end with a separator. */
114 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
115 /* Define this macro to evaluate to a nonzero value if GCC should refrain
116 from generating indirect strings in DWARF2 debug information, for instance
117 if your target is stuck with an old version of GDB that is unable to
118 process them properly or uses VMS Debug. */
119 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
120 #else
121 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
122 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
123 #endif
125 /* ??? Poison these here until it can be done generically. They've been
126 totally replaced in this file; make sure it stays that way. */
127 #undef DWARF2_UNWIND_INFO
128 #undef DWARF2_FRAME_INFO
129 #if (GCC_VERSION >= 3000)
130 #pragma GCC poison DWARF2_UNWIND_INFO DWARF2_FRAME_INFO
131 #endif
133 /* The size of the target's pointer type. */
134 #ifndef PTR_SIZE
135 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
136 #endif
138 /* Array of RTXes referenced by the debugging information, which therefore
139 must be kept around forever. */
140 static GTY(()) vec<rtx, va_gc> *used_rtx_array;
142 /* A pointer to the base of a list of incomplete types which might be
143 completed at some later time. incomplete_types_list needs to be a
144 vec<tree, va_gc> *because we want to tell the garbage collector about
145 it. */
146 static GTY(()) vec<tree, va_gc> *incomplete_types;
148 /* A pointer to the base of a table of references to declaration
149 scopes. This table is a display which tracks the nesting
150 of declaration scopes at the current scope and containing
151 scopes. This table is used to find the proper place to
152 define type declaration DIE's. */
153 static GTY(()) vec<tree, va_gc> *decl_scope_table;
155 /* Pointers to various DWARF2 sections. */
156 static GTY(()) section *debug_info_section;
157 static GTY(()) section *debug_skeleton_info_section;
158 static GTY(()) section *debug_abbrev_section;
159 static GTY(()) section *debug_skeleton_abbrev_section;
160 static GTY(()) section *debug_aranges_section;
161 static GTY(()) section *debug_addr_section;
162 static GTY(()) section *debug_macinfo_section;
163 static const char *debug_macinfo_section_name;
164 static GTY(()) section *debug_line_section;
165 static GTY(()) section *debug_skeleton_line_section;
166 static GTY(()) section *debug_loc_section;
167 static GTY(()) section *debug_pubnames_section;
168 static GTY(()) section *debug_pubtypes_section;
169 static GTY(()) section *debug_str_section;
170 static GTY(()) section *debug_line_str_section;
171 static GTY(()) section *debug_str_dwo_section;
172 static GTY(()) section *debug_str_offsets_section;
173 static GTY(()) section *debug_ranges_section;
174 static GTY(()) section *debug_frame_section;
176 /* Maximum size (in bytes) of an artificially generated label. */
177 #define MAX_ARTIFICIAL_LABEL_BYTES 30
179 /* According to the (draft) DWARF 3 specification, the initial length
180 should either be 4 or 12 bytes. When it's 12 bytes, the first 4
181 bytes are 0xffffffff, followed by the length stored in the next 8
182 bytes.
184 However, the SGI/MIPS ABI uses an initial length which is equal to
185 DWARF_OFFSET_SIZE. It is defined (elsewhere) accordingly. */
187 #ifndef DWARF_INITIAL_LENGTH_SIZE
188 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
189 #endif
191 /* Round SIZE up to the nearest BOUNDARY. */
192 #define DWARF_ROUND(SIZE,BOUNDARY) \
193 ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
195 /* CIE identifier. */
196 #if HOST_BITS_PER_WIDE_INT >= 64
197 #define DWARF_CIE_ID \
198 (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
199 #else
200 #define DWARF_CIE_ID DW_CIE_ID
201 #endif
204 /* A vector for a table that contains frame description
205 information for each routine. */
206 #define NOT_INDEXED (-1U)
207 #define NO_INDEX_ASSIGNED (-2U)
209 static GTY(()) vec<dw_fde_ref, va_gc> *fde_vec;
211 struct GTY((for_user)) indirect_string_node {
212 const char *str;
213 unsigned int refcount;
214 enum dwarf_form form;
215 char *label;
216 unsigned int index;
219 struct indirect_string_hasher : ggc_ptr_hash<indirect_string_node>
221 typedef const char *compare_type;
223 static hashval_t hash (indirect_string_node *);
224 static bool equal (indirect_string_node *, const char *);
227 static GTY (()) hash_table<indirect_string_hasher> *debug_str_hash;
229 static GTY (()) hash_table<indirect_string_hasher> *debug_line_str_hash;
231 /* With split_debug_info, both the comp_dir and dwo_name go in the
232 main object file, rather than the dwo, similar to the force_direct
233 parameter elsewhere but with additional complications:
235 1) The string is needed in both the main object file and the dwo.
236 That is, the comp_dir and dwo_name will appear in both places.
238 2) Strings can use four forms: DW_FORM_string, DW_FORM_strp,
239 DW_FORM_line_strp or DW_FORM_GNU_str_index.
241 3) GCC chooses the form to use late, depending on the size and
242 reference count.
244 Rather than forcing the all debug string handling functions and
245 callers to deal with these complications, simply use a separate,
246 special-cased string table for any attribute that should go in the
247 main object file. This limits the complexity to just the places
248 that need it. */
250 static GTY (()) hash_table<indirect_string_hasher> *skeleton_debug_str_hash;
252 static GTY(()) int dw2_string_counter;
254 /* True if the compilation unit places functions in more than one section. */
255 static GTY(()) bool have_multiple_function_sections = false;
257 /* Whether the default text and cold text sections have been used at all. */
259 static GTY(()) bool text_section_used = false;
260 static GTY(()) bool cold_text_section_used = false;
262 /* The default cold text section. */
263 static GTY(()) section *cold_text_section;
265 /* The DIE for C++14 'auto' in a function return type. */
266 static GTY(()) dw_die_ref auto_die;
268 /* The DIE for C++14 'decltype(auto)' in a function return type. */
269 static GTY(()) dw_die_ref decltype_auto_die;
271 /* Forward declarations for functions defined in this file. */
273 static void output_call_frame_info (int);
274 static void dwarf2out_note_section_used (void);
276 /* Personality decl of current unit. Used only when assembler does not support
277 personality CFI. */
278 static GTY(()) rtx current_unit_personality;
280 /* .debug_rnglists next index. */
281 static unsigned int rnglist_idx;
283 /* Data and reference forms for relocatable data. */
284 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
285 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
287 #ifndef DEBUG_FRAME_SECTION
288 #define DEBUG_FRAME_SECTION ".debug_frame"
289 #endif
291 #ifndef FUNC_BEGIN_LABEL
292 #define FUNC_BEGIN_LABEL "LFB"
293 #endif
295 #ifndef FUNC_END_LABEL
296 #define FUNC_END_LABEL "LFE"
297 #endif
299 #ifndef PROLOGUE_END_LABEL
300 #define PROLOGUE_END_LABEL "LPE"
301 #endif
303 #ifndef EPILOGUE_BEGIN_LABEL
304 #define EPILOGUE_BEGIN_LABEL "LEB"
305 #endif
307 #ifndef FRAME_BEGIN_LABEL
308 #define FRAME_BEGIN_LABEL "Lframe"
309 #endif
310 #define CIE_AFTER_SIZE_LABEL "LSCIE"
311 #define CIE_END_LABEL "LECIE"
312 #define FDE_LABEL "LSFDE"
313 #define FDE_AFTER_SIZE_LABEL "LASFDE"
314 #define FDE_END_LABEL "LEFDE"
315 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
316 #define LINE_NUMBER_END_LABEL "LELT"
317 #define LN_PROLOG_AS_LABEL "LASLTP"
318 #define LN_PROLOG_END_LABEL "LELTP"
319 #define DIE_LABEL_PREFIX "DW"
321 /* Match the base name of a file to the base name of a compilation unit. */
323 static int
324 matches_main_base (const char *path)
326 /* Cache the last query. */
327 static const char *last_path = NULL;
328 static int last_match = 0;
329 if (path != last_path)
331 const char *base;
332 int length = base_of_path (path, &base);
333 last_path = path;
334 last_match = (length == main_input_baselength
335 && memcmp (base, main_input_basename, length) == 0);
337 return last_match;
340 #ifdef DEBUG_DEBUG_STRUCT
342 static int
343 dump_struct_debug (tree type, enum debug_info_usage usage,
344 enum debug_struct_file criterion, int generic,
345 int matches, int result)
347 /* Find the type name. */
348 tree type_decl = TYPE_STUB_DECL (type);
349 tree t = type_decl;
350 const char *name = 0;
351 if (TREE_CODE (t) == TYPE_DECL)
352 t = DECL_NAME (t);
353 if (t)
354 name = IDENTIFIER_POINTER (t);
356 fprintf (stderr, " struct %d %s %s %s %s %d %p %s\n",
357 criterion,
358 DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
359 matches ? "bas" : "hdr",
360 generic ? "gen" : "ord",
361 usage == DINFO_USAGE_DFN ? ";" :
362 usage == DINFO_USAGE_DIR_USE ? "." : "*",
363 result,
364 (void*) type_decl, name);
365 return result;
367 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
368 dump_struct_debug (type, usage, criterion, generic, matches, result)
370 #else
372 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
373 (result)
375 #endif
377 /* Get the number of HOST_WIDE_INTs needed to represent the precision
378 of the number. Some constants have a large uniform precision, so
379 we get the precision needed for the actual value of the number. */
381 static unsigned int
382 get_full_len (const wide_int &op)
384 int prec = wi::min_precision (op, UNSIGNED);
385 return ((prec + HOST_BITS_PER_WIDE_INT - 1)
386 / HOST_BITS_PER_WIDE_INT);
389 static bool
390 should_emit_struct_debug (tree type, enum debug_info_usage usage)
392 enum debug_struct_file criterion;
393 tree type_decl;
394 bool generic = lang_hooks.types.generic_p (type);
396 if (generic)
397 criterion = debug_struct_generic[usage];
398 else
399 criterion = debug_struct_ordinary[usage];
401 if (criterion == DINFO_STRUCT_FILE_NONE)
402 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
403 if (criterion == DINFO_STRUCT_FILE_ANY)
404 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
406 type_decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type));
408 if (type_decl != NULL)
410 if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
411 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
413 if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
414 return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
417 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
420 /* Switch [BACK] to eh_frame_section. If we don't have an eh_frame_section,
421 switch to the data section instead, and write out a synthetic start label
422 for collect2 the first time around. */
424 static void
425 switch_to_eh_frame_section (bool back ATTRIBUTE_UNUSED)
427 if (eh_frame_section == 0)
429 int flags;
431 if (EH_TABLES_CAN_BE_READ_ONLY)
433 int fde_encoding;
434 int per_encoding;
435 int lsda_encoding;
437 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
438 /*global=*/0);
439 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
440 /*global=*/1);
441 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
442 /*global=*/0);
443 flags = ((! flag_pic
444 || ((fde_encoding & 0x70) != DW_EH_PE_absptr
445 && (fde_encoding & 0x70) != DW_EH_PE_aligned
446 && (per_encoding & 0x70) != DW_EH_PE_absptr
447 && (per_encoding & 0x70) != DW_EH_PE_aligned
448 && (lsda_encoding & 0x70) != DW_EH_PE_absptr
449 && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
450 ? 0 : SECTION_WRITE);
452 else
453 flags = SECTION_WRITE;
455 #ifdef EH_FRAME_SECTION_NAME
456 eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
457 #else
458 eh_frame_section = ((flags == SECTION_WRITE)
459 ? data_section : readonly_data_section);
460 #endif /* EH_FRAME_SECTION_NAME */
463 switch_to_section (eh_frame_section);
465 #ifdef EH_FRAME_THROUGH_COLLECT2
466 /* We have no special eh_frame section. Emit special labels to guide
467 collect2. */
468 if (!back)
470 tree label = get_file_function_name ("F");
471 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
472 targetm.asm_out.globalize_label (asm_out_file,
473 IDENTIFIER_POINTER (label));
474 ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
476 #endif
479 /* Switch [BACK] to the eh or debug frame table section, depending on
480 FOR_EH. */
482 static void
483 switch_to_frame_table_section (int for_eh, bool back)
485 if (for_eh)
486 switch_to_eh_frame_section (back);
487 else
489 if (!debug_frame_section)
490 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
491 SECTION_DEBUG, NULL);
492 switch_to_section (debug_frame_section);
496 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used. */
498 enum dw_cfi_oprnd_type
499 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
501 switch (cfi)
503 case DW_CFA_nop:
504 case DW_CFA_GNU_window_save:
505 case DW_CFA_remember_state:
506 case DW_CFA_restore_state:
507 return dw_cfi_oprnd_unused;
509 case DW_CFA_set_loc:
510 case DW_CFA_advance_loc1:
511 case DW_CFA_advance_loc2:
512 case DW_CFA_advance_loc4:
513 case DW_CFA_MIPS_advance_loc8:
514 return dw_cfi_oprnd_addr;
516 case DW_CFA_offset:
517 case DW_CFA_offset_extended:
518 case DW_CFA_def_cfa:
519 case DW_CFA_offset_extended_sf:
520 case DW_CFA_def_cfa_sf:
521 case DW_CFA_restore:
522 case DW_CFA_restore_extended:
523 case DW_CFA_undefined:
524 case DW_CFA_same_value:
525 case DW_CFA_def_cfa_register:
526 case DW_CFA_register:
527 case DW_CFA_expression:
528 case DW_CFA_val_expression:
529 return dw_cfi_oprnd_reg_num;
531 case DW_CFA_def_cfa_offset:
532 case DW_CFA_GNU_args_size:
533 case DW_CFA_def_cfa_offset_sf:
534 return dw_cfi_oprnd_offset;
536 case DW_CFA_def_cfa_expression:
537 return dw_cfi_oprnd_loc;
539 default:
540 gcc_unreachable ();
544 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used. */
546 enum dw_cfi_oprnd_type
547 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
549 switch (cfi)
551 case DW_CFA_def_cfa:
552 case DW_CFA_def_cfa_sf:
553 case DW_CFA_offset:
554 case DW_CFA_offset_extended_sf:
555 case DW_CFA_offset_extended:
556 return dw_cfi_oprnd_offset;
558 case DW_CFA_register:
559 return dw_cfi_oprnd_reg_num;
561 case DW_CFA_expression:
562 case DW_CFA_val_expression:
563 return dw_cfi_oprnd_loc;
565 default:
566 return dw_cfi_oprnd_unused;
570 /* Output one FDE. */
572 static void
573 output_fde (dw_fde_ref fde, bool for_eh, bool second,
574 char *section_start_label, int fde_encoding, char *augmentation,
575 bool any_lsda_needed, int lsda_encoding)
577 const char *begin, *end;
578 static unsigned int j;
579 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
581 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
582 /* empty */ 0);
583 targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
584 for_eh + j);
585 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
586 ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
587 if (!XCOFF_DEBUGGING_INFO || for_eh)
589 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
590 dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
591 " indicating 64-bit DWARF extension");
592 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
593 "FDE Length");
595 ASM_OUTPUT_LABEL (asm_out_file, l1);
597 if (for_eh)
598 dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
599 else
600 dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
601 debug_frame_section, "FDE CIE offset");
603 begin = second ? fde->dw_fde_second_begin : fde->dw_fde_begin;
604 end = second ? fde->dw_fde_second_end : fde->dw_fde_end;
606 if (for_eh)
608 rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
609 SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
610 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
611 "FDE initial location");
612 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
613 end, begin, "FDE address range");
615 else
617 dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
618 dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
621 if (augmentation[0])
623 if (any_lsda_needed)
625 int size = size_of_encoded_value (lsda_encoding);
627 if (lsda_encoding == DW_EH_PE_aligned)
629 int offset = ( 4 /* Length */
630 + 4 /* CIE offset */
631 + 2 * size_of_encoded_value (fde_encoding)
632 + 1 /* Augmentation size */ );
633 int pad = -offset & (PTR_SIZE - 1);
635 size += pad;
636 gcc_assert (size_of_uleb128 (size) == 1);
639 dw2_asm_output_data_uleb128 (size, "Augmentation size");
641 if (fde->uses_eh_lsda)
643 ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
644 fde->funcdef_number);
645 dw2_asm_output_encoded_addr_rtx (lsda_encoding,
646 gen_rtx_SYMBOL_REF (Pmode, l1),
647 false,
648 "Language Specific Data Area");
650 else
652 if (lsda_encoding == DW_EH_PE_aligned)
653 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
654 dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
655 "Language Specific Data Area (none)");
658 else
659 dw2_asm_output_data_uleb128 (0, "Augmentation size");
662 /* Loop through the Call Frame Instructions associated with this FDE. */
663 fde->dw_fde_current_label = begin;
665 size_t from, until, i;
667 from = 0;
668 until = vec_safe_length (fde->dw_fde_cfi);
670 if (fde->dw_fde_second_begin == NULL)
672 else if (!second)
673 until = fde->dw_fde_switch_cfi_index;
674 else
675 from = fde->dw_fde_switch_cfi_index;
677 for (i = from; i < until; i++)
678 output_cfi ((*fde->dw_fde_cfi)[i], fde, for_eh);
681 /* If we are to emit a ref/link from function bodies to their frame tables,
682 do it now. This is typically performed to make sure that tables
683 associated with functions are dragged with them and not discarded in
684 garbage collecting links. We need to do this on a per function basis to
685 cope with -ffunction-sections. */
687 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
688 /* Switch to the function section, emit the ref to the tables, and
689 switch *back* into the table section. */
690 switch_to_section (function_section (fde->decl));
691 ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
692 switch_to_frame_table_section (for_eh, true);
693 #endif
695 /* Pad the FDE out to an address sized boundary. */
696 ASM_OUTPUT_ALIGN (asm_out_file,
697 floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
698 ASM_OUTPUT_LABEL (asm_out_file, l2);
700 j += 2;
703 /* Return true if frame description entry FDE is needed for EH. */
705 static bool
706 fde_needed_for_eh_p (dw_fde_ref fde)
708 if (flag_asynchronous_unwind_tables)
709 return true;
711 if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
712 return true;
714 if (fde->uses_eh_lsda)
715 return true;
717 /* If exceptions are enabled, we have collected nothrow info. */
718 if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
719 return false;
721 return true;
724 /* Output the call frame information used to record information
725 that relates to calculating the frame pointer, and records the
726 location of saved registers. */
728 static void
729 output_call_frame_info (int for_eh)
731 unsigned int i;
732 dw_fde_ref fde;
733 dw_cfi_ref cfi;
734 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
735 char section_start_label[MAX_ARTIFICIAL_LABEL_BYTES];
736 bool any_lsda_needed = false;
737 char augmentation[6];
738 int augmentation_size;
739 int fde_encoding = DW_EH_PE_absptr;
740 int per_encoding = DW_EH_PE_absptr;
741 int lsda_encoding = DW_EH_PE_absptr;
742 int return_reg;
743 rtx personality = NULL;
744 int dw_cie_version;
746 /* Don't emit a CIE if there won't be any FDEs. */
747 if (!fde_vec)
748 return;
750 /* Nothing to do if the assembler's doing it all. */
751 if (dwarf2out_do_cfi_asm ())
752 return;
754 /* If we don't have any functions we'll want to unwind out of, don't emit
755 any EH unwind information. If we make FDEs linkonce, we may have to
756 emit an empty label for an FDE that wouldn't otherwise be emitted. We
757 want to avoid having an FDE kept around when the function it refers to
758 is discarded. Example where this matters: a primary function template
759 in C++ requires EH information, an explicit specialization doesn't. */
760 if (for_eh)
762 bool any_eh_needed = false;
764 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
766 if (fde->uses_eh_lsda)
767 any_eh_needed = any_lsda_needed = true;
768 else if (fde_needed_for_eh_p (fde))
769 any_eh_needed = true;
770 else if (TARGET_USES_WEAK_UNWIND_INFO)
771 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, 1, 1);
774 if (!any_eh_needed)
775 return;
778 /* We're going to be generating comments, so turn on app. */
779 if (flag_debug_asm)
780 app_enable ();
782 /* Switch to the proper frame section, first time. */
783 switch_to_frame_table_section (for_eh, false);
785 ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
786 ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
788 /* Output the CIE. */
789 ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
790 ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
791 if (!XCOFF_DEBUGGING_INFO || for_eh)
793 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
794 dw2_asm_output_data (4, 0xffffffff,
795 "Initial length escape value indicating 64-bit DWARF extension");
796 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
797 "Length of Common Information Entry");
799 ASM_OUTPUT_LABEL (asm_out_file, l1);
801 /* Now that the CIE pointer is PC-relative for EH,
802 use 0 to identify the CIE. */
803 dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
804 (for_eh ? 0 : DWARF_CIE_ID),
805 "CIE Identifier Tag");
807 /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
808 use CIE version 1, unless that would produce incorrect results
809 due to overflowing the return register column. */
810 return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
811 dw_cie_version = 1;
812 if (return_reg >= 256 || dwarf_version > 2)
813 dw_cie_version = 3;
814 dw2_asm_output_data (1, dw_cie_version, "CIE Version");
816 augmentation[0] = 0;
817 augmentation_size = 0;
819 personality = current_unit_personality;
820 if (for_eh)
822 char *p;
824 /* Augmentation:
825 z Indicates that a uleb128 is present to size the
826 augmentation section.
827 L Indicates the encoding (and thus presence) of
828 an LSDA pointer in the FDE augmentation.
829 R Indicates a non-default pointer encoding for
830 FDE code pointers.
831 P Indicates the presence of an encoding + language
832 personality routine in the CIE augmentation. */
834 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
835 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
836 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
838 p = augmentation + 1;
839 if (personality)
841 *p++ = 'P';
842 augmentation_size += 1 + size_of_encoded_value (per_encoding);
843 assemble_external_libcall (personality);
845 if (any_lsda_needed)
847 *p++ = 'L';
848 augmentation_size += 1;
850 if (fde_encoding != DW_EH_PE_absptr)
852 *p++ = 'R';
853 augmentation_size += 1;
855 if (p > augmentation + 1)
857 augmentation[0] = 'z';
858 *p = '\0';
861 /* Ug. Some platforms can't do unaligned dynamic relocations at all. */
862 if (personality && per_encoding == DW_EH_PE_aligned)
864 int offset = ( 4 /* Length */
865 + 4 /* CIE Id */
866 + 1 /* CIE version */
867 + strlen (augmentation) + 1 /* Augmentation */
868 + size_of_uleb128 (1) /* Code alignment */
869 + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
870 + 1 /* RA column */
871 + 1 /* Augmentation size */
872 + 1 /* Personality encoding */ );
873 int pad = -offset & (PTR_SIZE - 1);
875 augmentation_size += pad;
877 /* Augmentations should be small, so there's scarce need to
878 iterate for a solution. Die if we exceed one uleb128 byte. */
879 gcc_assert (size_of_uleb128 (augmentation_size) == 1);
883 dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
884 if (dw_cie_version >= 4)
886 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
887 dw2_asm_output_data (1, 0, "CIE Segment Size");
889 dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
890 dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
891 "CIE Data Alignment Factor");
893 if (dw_cie_version == 1)
894 dw2_asm_output_data (1, return_reg, "CIE RA Column");
895 else
896 dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
898 if (augmentation[0])
900 dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
901 if (personality)
903 dw2_asm_output_data (1, per_encoding, "Personality (%s)",
904 eh_data_format_name (per_encoding));
905 dw2_asm_output_encoded_addr_rtx (per_encoding,
906 personality,
907 true, NULL);
910 if (any_lsda_needed)
911 dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
912 eh_data_format_name (lsda_encoding));
914 if (fde_encoding != DW_EH_PE_absptr)
915 dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
916 eh_data_format_name (fde_encoding));
919 FOR_EACH_VEC_ELT (*cie_cfi_vec, i, cfi)
920 output_cfi (cfi, NULL, for_eh);
922 /* Pad the CIE out to an address sized boundary. */
923 ASM_OUTPUT_ALIGN (asm_out_file,
924 floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
925 ASM_OUTPUT_LABEL (asm_out_file, l2);
927 /* Loop through all of the FDE's. */
928 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
930 unsigned int k;
932 /* Don't emit EH unwind info for leaf functions that don't need it. */
933 if (for_eh && !fde_needed_for_eh_p (fde))
934 continue;
936 for (k = 0; k < (fde->dw_fde_second_begin ? 2 : 1); k++)
937 output_fde (fde, for_eh, k, section_start_label, fde_encoding,
938 augmentation, any_lsda_needed, lsda_encoding);
941 if (for_eh && targetm.terminate_dw2_eh_frame_info)
942 dw2_asm_output_data (4, 0, "End of Table");
944 /* Turn off app to make assembly quicker. */
945 if (flag_debug_asm)
946 app_disable ();
949 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed. */
951 static void
952 dwarf2out_do_cfi_startproc (bool second)
954 int enc;
955 rtx ref;
956 rtx personality = get_personality_function (current_function_decl);
958 fprintf (asm_out_file, "\t.cfi_startproc\n");
960 if (personality)
962 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
963 ref = personality;
965 /* ??? The GAS support isn't entirely consistent. We have to
966 handle indirect support ourselves, but PC-relative is done
967 in the assembler. Further, the assembler can't handle any
968 of the weirder relocation types. */
969 if (enc & DW_EH_PE_indirect)
970 ref = dw2_force_const_mem (ref, true);
972 fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
973 output_addr_const (asm_out_file, ref);
974 fputc ('\n', asm_out_file);
977 if (crtl->uses_eh_lsda)
979 char lab[MAX_ARTIFICIAL_LABEL_BYTES];
981 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
982 ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
983 current_function_funcdef_no);
984 ref = gen_rtx_SYMBOL_REF (Pmode, lab);
985 SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
987 if (enc & DW_EH_PE_indirect)
988 ref = dw2_force_const_mem (ref, true);
990 fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
991 output_addr_const (asm_out_file, ref);
992 fputc ('\n', asm_out_file);
996 /* Allocate CURRENT_FDE. Immediately initialize all we can, noting that
997 this allocation may be done before pass_final. */
999 dw_fde_ref
1000 dwarf2out_alloc_current_fde (void)
1002 dw_fde_ref fde;
1004 fde = ggc_cleared_alloc<dw_fde_node> ();
1005 fde->decl = current_function_decl;
1006 fde->funcdef_number = current_function_funcdef_no;
1007 fde->fde_index = vec_safe_length (fde_vec);
1008 fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
1009 fde->uses_eh_lsda = crtl->uses_eh_lsda;
1010 fde->nothrow = crtl->nothrow;
1011 fde->drap_reg = INVALID_REGNUM;
1012 fde->vdrap_reg = INVALID_REGNUM;
1014 /* Record the FDE associated with this function. */
1015 cfun->fde = fde;
1016 vec_safe_push (fde_vec, fde);
1018 return fde;
1021 /* Output a marker (i.e. a label) for the beginning of a function, before
1022 the prologue. */
1024 void
1025 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
1026 const char *file ATTRIBUTE_UNUSED)
1028 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1029 char * dup_label;
1030 dw_fde_ref fde;
1031 section *fnsec;
1032 bool do_frame;
1034 current_function_func_begin_label = NULL;
1036 do_frame = dwarf2out_do_frame ();
1038 /* ??? current_function_func_begin_label is also used by except.c for
1039 call-site information. We must emit this label if it might be used. */
1040 if (!do_frame
1041 && (!flag_exceptions
1042 || targetm_common.except_unwind_info (&global_options) == UI_SJLJ))
1043 return;
1045 fnsec = function_section (current_function_decl);
1046 switch_to_section (fnsec);
1047 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
1048 current_function_funcdef_no);
1049 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
1050 current_function_funcdef_no);
1051 dup_label = xstrdup (label);
1052 current_function_func_begin_label = dup_label;
1054 /* We can elide the fde allocation if we're not emitting debug info. */
1055 if (!do_frame)
1056 return;
1058 /* Cater to the various TARGET_ASM_OUTPUT_MI_THUNK implementations that
1059 emit insns as rtx but bypass the bulk of rest_of_compilation, which
1060 would include pass_dwarf2_frame. If we've not created the FDE yet,
1061 do so now. */
1062 fde = cfun->fde;
1063 if (fde == NULL)
1064 fde = dwarf2out_alloc_current_fde ();
1066 /* Initialize the bits of CURRENT_FDE that were not available earlier. */
1067 fde->dw_fde_begin = dup_label;
1068 fde->dw_fde_current_label = dup_label;
1069 fde->in_std_section = (fnsec == text_section
1070 || (cold_text_section && fnsec == cold_text_section));
1072 /* We only want to output line number information for the genuine dwarf2
1073 prologue case, not the eh frame case. */
1074 #ifdef DWARF2_DEBUGGING_INFO
1075 if (file)
1076 dwarf2out_source_line (line, file, 0, true);
1077 #endif
1079 if (dwarf2out_do_cfi_asm ())
1080 dwarf2out_do_cfi_startproc (false);
1081 else
1083 rtx personality = get_personality_function (current_function_decl);
1084 if (!current_unit_personality)
1085 current_unit_personality = personality;
1087 /* We cannot keep a current personality per function as without CFI
1088 asm, at the point where we emit the CFI data, there is no current
1089 function anymore. */
1090 if (personality && current_unit_personality != personality)
1091 sorry ("multiple EH personalities are supported only with assemblers "
1092 "supporting .cfi_personality directive");
1096 /* Output a marker (i.e. a label) for the end of the generated code
1097 for a function prologue. This gets called *after* the prologue code has
1098 been generated. */
1100 void
1101 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
1102 const char *file ATTRIBUTE_UNUSED)
1104 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1106 /* Output a label to mark the endpoint of the code generated for this
1107 function. */
1108 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
1109 current_function_funcdef_no);
1110 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
1111 current_function_funcdef_no);
1112 cfun->fde->dw_fde_vms_end_prologue = xstrdup (label);
1115 /* Output a marker (i.e. a label) for the beginning of the generated code
1116 for a function epilogue. This gets called *before* the prologue code has
1117 been generated. */
1119 void
1120 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1121 const char *file ATTRIBUTE_UNUSED)
1123 dw_fde_ref fde = cfun->fde;
1124 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1126 if (fde->dw_fde_vms_begin_epilogue)
1127 return;
1129 /* Output a label to mark the endpoint of the code generated for this
1130 function. */
1131 ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
1132 current_function_funcdef_no);
1133 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
1134 current_function_funcdef_no);
1135 fde->dw_fde_vms_begin_epilogue = xstrdup (label);
1138 /* Output a marker (i.e. a label) for the absolute end of the generated code
1139 for a function definition. This gets called *after* the epilogue code has
1140 been generated. */
1142 void
1143 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1144 const char *file ATTRIBUTE_UNUSED)
1146 dw_fde_ref fde;
1147 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1149 last_var_location_insn = NULL;
1150 cached_next_real_insn = NULL;
1152 if (dwarf2out_do_cfi_asm ())
1153 fprintf (asm_out_file, "\t.cfi_endproc\n");
1155 /* Output a label to mark the endpoint of the code generated for this
1156 function. */
1157 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
1158 current_function_funcdef_no);
1159 ASM_OUTPUT_LABEL (asm_out_file, label);
1160 fde = cfun->fde;
1161 gcc_assert (fde != NULL);
1162 if (fde->dw_fde_second_begin == NULL)
1163 fde->dw_fde_end = xstrdup (label);
1166 void
1167 dwarf2out_frame_finish (void)
1169 /* Output call frame information. */
1170 if (targetm.debug_unwind_info () == UI_DWARF2)
1171 output_call_frame_info (0);
1173 /* Output another copy for the unwinder. */
1174 if ((flag_unwind_tables || flag_exceptions)
1175 && targetm_common.except_unwind_info (&global_options) == UI_DWARF2)
1176 output_call_frame_info (1);
1179 /* Note that the current function section is being used for code. */
1181 static void
1182 dwarf2out_note_section_used (void)
1184 section *sec = current_function_section ();
1185 if (sec == text_section)
1186 text_section_used = true;
1187 else if (sec == cold_text_section)
1188 cold_text_section_used = true;
1191 static void var_location_switch_text_section (void);
1192 static void set_cur_line_info_table (section *);
1194 void
1195 dwarf2out_switch_text_section (void)
1197 section *sect;
1198 dw_fde_ref fde = cfun->fde;
1200 gcc_assert (cfun && fde && fde->dw_fde_second_begin == NULL);
1202 if (!in_cold_section_p)
1204 fde->dw_fde_end = crtl->subsections.cold_section_end_label;
1205 fde->dw_fde_second_begin = crtl->subsections.hot_section_label;
1206 fde->dw_fde_second_end = crtl->subsections.hot_section_end_label;
1208 else
1210 fde->dw_fde_end = crtl->subsections.hot_section_end_label;
1211 fde->dw_fde_second_begin = crtl->subsections.cold_section_label;
1212 fde->dw_fde_second_end = crtl->subsections.cold_section_end_label;
1214 have_multiple_function_sections = true;
1216 /* There is no need to mark used sections when not debugging. */
1217 if (cold_text_section != NULL)
1218 dwarf2out_note_section_used ();
1220 if (dwarf2out_do_cfi_asm ())
1221 fprintf (asm_out_file, "\t.cfi_endproc\n");
1223 /* Now do the real section switch. */
1224 sect = current_function_section ();
1225 switch_to_section (sect);
1227 fde->second_in_std_section
1228 = (sect == text_section
1229 || (cold_text_section && sect == cold_text_section));
1231 if (dwarf2out_do_cfi_asm ())
1232 dwarf2out_do_cfi_startproc (true);
1234 var_location_switch_text_section ();
1236 if (cold_text_section != NULL)
1237 set_cur_line_info_table (sect);
1240 /* And now, the subset of the debugging information support code necessary
1241 for emitting location expressions. */
1243 /* Data about a single source file. */
1244 struct GTY((for_user)) dwarf_file_data {
1245 const char * filename;
1246 int emitted_number;
1249 /* Describe an entry into the .debug_addr section. */
1251 enum ate_kind {
1252 ate_kind_rtx,
1253 ate_kind_rtx_dtprel,
1254 ate_kind_label
1257 struct GTY((for_user)) addr_table_entry {
1258 enum ate_kind kind;
1259 unsigned int refcount;
1260 unsigned int index;
1261 union addr_table_entry_struct_union
1263 rtx GTY ((tag ("0"))) rtl;
1264 char * GTY ((tag ("1"))) label;
1266 GTY ((desc ("%1.kind"))) addr;
1269 /* Location lists are ranges + location descriptions for that range,
1270 so you can track variables that are in different places over
1271 their entire life. */
1272 typedef struct GTY(()) dw_loc_list_struct {
1273 dw_loc_list_ref dw_loc_next;
1274 const char *begin; /* Label and addr_entry for start of range */
1275 addr_table_entry *begin_entry;
1276 const char *end; /* Label for end of range */
1277 char *ll_symbol; /* Label for beginning of location list.
1278 Only on head of list */
1279 const char *section; /* Section this loclist is relative to */
1280 dw_loc_descr_ref expr;
1281 hashval_t hash;
1282 /* True if all addresses in this and subsequent lists are known to be
1283 resolved. */
1284 bool resolved_addr;
1285 /* True if this list has been replaced by dw_loc_next. */
1286 bool replaced;
1287 /* True if it has been emitted into .debug_loc* / .debug_loclists*
1288 section. */
1289 unsigned char emitted : 1;
1290 /* True if hash field is index rather than hash value. */
1291 unsigned char num_assigned : 1;
1292 /* True if .debug_loclists.dwo offset has been emitted for it already. */
1293 unsigned char offset_emitted : 1;
1294 /* True if the range should be emitted even if begin and end
1295 are the same. */
1296 bool force;
1297 } dw_loc_list_node;
1299 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
1300 static dw_loc_descr_ref uint_loc_descriptor (unsigned HOST_WIDE_INT);
1302 /* Convert a DWARF stack opcode into its string name. */
1304 static const char *
1305 dwarf_stack_op_name (unsigned int op)
1307 const char *name = get_DW_OP_name (op);
1309 if (name != NULL)
1310 return name;
1312 return "OP_<unknown>";
1315 /* Return a pointer to a newly allocated location description. Location
1316 descriptions are simple expression terms that can be strung
1317 together to form more complicated location (address) descriptions. */
1319 static inline dw_loc_descr_ref
1320 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
1321 unsigned HOST_WIDE_INT oprnd2)
1323 dw_loc_descr_ref descr = ggc_cleared_alloc<dw_loc_descr_node> ();
1325 descr->dw_loc_opc = op;
1326 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
1327 descr->dw_loc_oprnd1.val_entry = NULL;
1328 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
1329 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
1330 descr->dw_loc_oprnd2.val_entry = NULL;
1331 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
1333 return descr;
1336 /* Return a pointer to a newly allocated location description for
1337 REG and OFFSET. */
1339 static inline dw_loc_descr_ref
1340 new_reg_loc_descr (unsigned int reg, unsigned HOST_WIDE_INT offset)
1342 if (reg <= 31)
1343 return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
1344 offset, 0);
1345 else
1346 return new_loc_descr (DW_OP_bregx, reg, offset);
1349 /* Add a location description term to a location description expression. */
1351 static inline void
1352 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
1354 dw_loc_descr_ref *d;
1356 /* Find the end of the chain. */
1357 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
1360 *d = descr;
1363 /* Compare two location operands for exact equality. */
1365 static bool
1366 dw_val_equal_p (dw_val_node *a, dw_val_node *b)
1368 if (a->val_class != b->val_class)
1369 return false;
1370 switch (a->val_class)
1372 case dw_val_class_none:
1373 return true;
1374 case dw_val_class_addr:
1375 return rtx_equal_p (a->v.val_addr, b->v.val_addr);
1377 case dw_val_class_offset:
1378 case dw_val_class_unsigned_const:
1379 case dw_val_class_const:
1380 case dw_val_class_unsigned_const_implicit:
1381 case dw_val_class_const_implicit:
1382 case dw_val_class_range_list:
1383 /* These are all HOST_WIDE_INT, signed or unsigned. */
1384 return a->v.val_unsigned == b->v.val_unsigned;
1386 case dw_val_class_loc:
1387 return a->v.val_loc == b->v.val_loc;
1388 case dw_val_class_loc_list:
1389 return a->v.val_loc_list == b->v.val_loc_list;
1390 case dw_val_class_die_ref:
1391 return a->v.val_die_ref.die == b->v.val_die_ref.die;
1392 case dw_val_class_fde_ref:
1393 return a->v.val_fde_index == b->v.val_fde_index;
1394 case dw_val_class_lbl_id:
1395 case dw_val_class_lineptr:
1396 case dw_val_class_macptr:
1397 case dw_val_class_loclistsptr:
1398 case dw_val_class_high_pc:
1399 return strcmp (a->v.val_lbl_id, b->v.val_lbl_id) == 0;
1400 case dw_val_class_str:
1401 return a->v.val_str == b->v.val_str;
1402 case dw_val_class_flag:
1403 return a->v.val_flag == b->v.val_flag;
1404 case dw_val_class_file:
1405 case dw_val_class_file_implicit:
1406 return a->v.val_file == b->v.val_file;
1407 case dw_val_class_decl_ref:
1408 return a->v.val_decl_ref == b->v.val_decl_ref;
1410 case dw_val_class_const_double:
1411 return (a->v.val_double.high == b->v.val_double.high
1412 && a->v.val_double.low == b->v.val_double.low);
1414 case dw_val_class_wide_int:
1415 return *a->v.val_wide == *b->v.val_wide;
1417 case dw_val_class_vec:
1419 size_t a_len = a->v.val_vec.elt_size * a->v.val_vec.length;
1420 size_t b_len = b->v.val_vec.elt_size * b->v.val_vec.length;
1422 return (a_len == b_len
1423 && !memcmp (a->v.val_vec.array, b->v.val_vec.array, a_len));
1426 case dw_val_class_data8:
1427 return memcmp (a->v.val_data8, b->v.val_data8, 8) == 0;
1429 case dw_val_class_vms_delta:
1430 return (!strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1)
1431 && !strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1));
1433 case dw_val_class_discr_value:
1434 return (a->v.val_discr_value.pos == b->v.val_discr_value.pos
1435 && a->v.val_discr_value.v.uval == b->v.val_discr_value.v.uval);
1436 case dw_val_class_discr_list:
1437 /* It makes no sense comparing two discriminant value lists. */
1438 return false;
1440 gcc_unreachable ();
1443 /* Compare two location atoms for exact equality. */
1445 static bool
1446 loc_descr_equal_p_1 (dw_loc_descr_ref a, dw_loc_descr_ref b)
1448 if (a->dw_loc_opc != b->dw_loc_opc)
1449 return false;
1451 /* ??? This is only ever set for DW_OP_constNu, for N equal to the
1452 address size, but since we always allocate cleared storage it
1453 should be zero for other types of locations. */
1454 if (a->dtprel != b->dtprel)
1455 return false;
1457 return (dw_val_equal_p (&a->dw_loc_oprnd1, &b->dw_loc_oprnd1)
1458 && dw_val_equal_p (&a->dw_loc_oprnd2, &b->dw_loc_oprnd2));
1461 /* Compare two complete location expressions for exact equality. */
1463 bool
1464 loc_descr_equal_p (dw_loc_descr_ref a, dw_loc_descr_ref b)
1466 while (1)
1468 if (a == b)
1469 return true;
1470 if (a == NULL || b == NULL)
1471 return false;
1472 if (!loc_descr_equal_p_1 (a, b))
1473 return false;
1475 a = a->dw_loc_next;
1476 b = b->dw_loc_next;
1481 /* Add a constant OFFSET to a location expression. */
1483 static void
1484 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
1486 dw_loc_descr_ref loc;
1487 HOST_WIDE_INT *p;
1489 gcc_assert (*list_head != NULL);
1491 if (!offset)
1492 return;
1494 /* Find the end of the chain. */
1495 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
1498 p = NULL;
1499 if (loc->dw_loc_opc == DW_OP_fbreg
1500 || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
1501 p = &loc->dw_loc_oprnd1.v.val_int;
1502 else if (loc->dw_loc_opc == DW_OP_bregx)
1503 p = &loc->dw_loc_oprnd2.v.val_int;
1505 /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
1506 offset. Don't optimize if an signed integer overflow would happen. */
1507 if (p != NULL
1508 && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
1509 || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
1510 *p += offset;
1512 else if (offset > 0)
1513 loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
1515 else
1517 loc->dw_loc_next
1518 = uint_loc_descriptor (-(unsigned HOST_WIDE_INT) offset);
1519 add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
1523 /* Add a constant OFFSET to a location list. */
1525 static void
1526 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
1528 dw_loc_list_ref d;
1529 for (d = list_head; d != NULL; d = d->dw_loc_next)
1530 loc_descr_plus_const (&d->expr, offset);
1533 #define DWARF_REF_SIZE \
1534 (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
1536 /* The number of bits that can be encoded by largest DW_FORM_dataN.
1537 In DWARF4 and earlier it is DW_FORM_data8 with 64 bits, in DWARF5
1538 DW_FORM_data16 with 128 bits. */
1539 #define DWARF_LARGEST_DATA_FORM_BITS \
1540 (dwarf_version >= 5 ? 128 : 64)
1542 /* Utility inline function for construction of ops that were GNU extension
1543 before DWARF 5. */
1544 static inline enum dwarf_location_atom
1545 dwarf_OP (enum dwarf_location_atom op)
1547 switch (op)
1549 case DW_OP_implicit_pointer:
1550 if (dwarf_version < 5)
1551 return DW_OP_GNU_implicit_pointer;
1552 break;
1554 case DW_OP_entry_value:
1555 if (dwarf_version < 5)
1556 return DW_OP_GNU_entry_value;
1557 break;
1559 case DW_OP_const_type:
1560 if (dwarf_version < 5)
1561 return DW_OP_GNU_const_type;
1562 break;
1564 case DW_OP_regval_type:
1565 if (dwarf_version < 5)
1566 return DW_OP_GNU_regval_type;
1567 break;
1569 case DW_OP_deref_type:
1570 if (dwarf_version < 5)
1571 return DW_OP_GNU_deref_type;
1572 break;
1574 case DW_OP_convert:
1575 if (dwarf_version < 5)
1576 return DW_OP_GNU_convert;
1577 break;
1579 case DW_OP_reinterpret:
1580 if (dwarf_version < 5)
1581 return DW_OP_GNU_reinterpret;
1582 break;
1584 default:
1585 break;
1587 return op;
1590 /* Similarly for attributes. */
1591 static inline enum dwarf_attribute
1592 dwarf_AT (enum dwarf_attribute at)
1594 switch (at)
1596 case DW_AT_call_return_pc:
1597 if (dwarf_version < 5)
1598 return DW_AT_low_pc;
1599 break;
1601 case DW_AT_call_tail_call:
1602 if (dwarf_version < 5)
1603 return DW_AT_GNU_tail_call;
1604 break;
1606 case DW_AT_call_origin:
1607 if (dwarf_version < 5)
1608 return DW_AT_abstract_origin;
1609 break;
1611 case DW_AT_call_target:
1612 if (dwarf_version < 5)
1613 return DW_AT_GNU_call_site_target;
1614 break;
1616 case DW_AT_call_target_clobbered:
1617 if (dwarf_version < 5)
1618 return DW_AT_GNU_call_site_target_clobbered;
1619 break;
1621 case DW_AT_call_parameter:
1622 if (dwarf_version < 5)
1623 return DW_AT_abstract_origin;
1624 break;
1626 case DW_AT_call_value:
1627 if (dwarf_version < 5)
1628 return DW_AT_GNU_call_site_value;
1629 break;
1631 case DW_AT_call_data_value:
1632 if (dwarf_version < 5)
1633 return DW_AT_GNU_call_site_data_value;
1634 break;
1636 case DW_AT_call_all_calls:
1637 if (dwarf_version < 5)
1638 return DW_AT_GNU_all_call_sites;
1639 break;
1641 case DW_AT_call_all_tail_calls:
1642 if (dwarf_version < 5)
1643 return DW_AT_GNU_all_tail_call_sites;
1644 break;
1646 case DW_AT_dwo_name:
1647 if (dwarf_version < 5)
1648 return DW_AT_GNU_dwo_name;
1649 break;
1651 default:
1652 break;
1654 return at;
1657 /* And similarly for tags. */
1658 static inline enum dwarf_tag
1659 dwarf_TAG (enum dwarf_tag tag)
1661 switch (tag)
1663 case DW_TAG_call_site:
1664 if (dwarf_version < 5)
1665 return DW_TAG_GNU_call_site;
1666 break;
1668 case DW_TAG_call_site_parameter:
1669 if (dwarf_version < 5)
1670 return DW_TAG_GNU_call_site_parameter;
1671 break;
1673 default:
1674 break;
1676 return tag;
1679 static unsigned long int get_base_type_offset (dw_die_ref);
1681 /* Return the size of a location descriptor. */
1683 static unsigned long
1684 size_of_loc_descr (dw_loc_descr_ref loc)
1686 unsigned long size = 1;
1688 switch (loc->dw_loc_opc)
1690 case DW_OP_addr:
1691 size += DWARF2_ADDR_SIZE;
1692 break;
1693 case DW_OP_GNU_addr_index:
1694 case DW_OP_GNU_const_index:
1695 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1696 size += size_of_uleb128 (loc->dw_loc_oprnd1.val_entry->index);
1697 break;
1698 case DW_OP_const1u:
1699 case DW_OP_const1s:
1700 size += 1;
1701 break;
1702 case DW_OP_const2u:
1703 case DW_OP_const2s:
1704 size += 2;
1705 break;
1706 case DW_OP_const4u:
1707 case DW_OP_const4s:
1708 size += 4;
1709 break;
1710 case DW_OP_const8u:
1711 case DW_OP_const8s:
1712 size += 8;
1713 break;
1714 case DW_OP_constu:
1715 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1716 break;
1717 case DW_OP_consts:
1718 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1719 break;
1720 case DW_OP_pick:
1721 size += 1;
1722 break;
1723 case DW_OP_plus_uconst:
1724 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1725 break;
1726 case DW_OP_skip:
1727 case DW_OP_bra:
1728 size += 2;
1729 break;
1730 case DW_OP_breg0:
1731 case DW_OP_breg1:
1732 case DW_OP_breg2:
1733 case DW_OP_breg3:
1734 case DW_OP_breg4:
1735 case DW_OP_breg5:
1736 case DW_OP_breg6:
1737 case DW_OP_breg7:
1738 case DW_OP_breg8:
1739 case DW_OP_breg9:
1740 case DW_OP_breg10:
1741 case DW_OP_breg11:
1742 case DW_OP_breg12:
1743 case DW_OP_breg13:
1744 case DW_OP_breg14:
1745 case DW_OP_breg15:
1746 case DW_OP_breg16:
1747 case DW_OP_breg17:
1748 case DW_OP_breg18:
1749 case DW_OP_breg19:
1750 case DW_OP_breg20:
1751 case DW_OP_breg21:
1752 case DW_OP_breg22:
1753 case DW_OP_breg23:
1754 case DW_OP_breg24:
1755 case DW_OP_breg25:
1756 case DW_OP_breg26:
1757 case DW_OP_breg27:
1758 case DW_OP_breg28:
1759 case DW_OP_breg29:
1760 case DW_OP_breg30:
1761 case DW_OP_breg31:
1762 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1763 break;
1764 case DW_OP_regx:
1765 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1766 break;
1767 case DW_OP_fbreg:
1768 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1769 break;
1770 case DW_OP_bregx:
1771 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1772 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1773 break;
1774 case DW_OP_piece:
1775 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1776 break;
1777 case DW_OP_bit_piece:
1778 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1779 size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
1780 break;
1781 case DW_OP_deref_size:
1782 case DW_OP_xderef_size:
1783 size += 1;
1784 break;
1785 case DW_OP_call2:
1786 size += 2;
1787 break;
1788 case DW_OP_call4:
1789 size += 4;
1790 break;
1791 case DW_OP_call_ref:
1792 size += DWARF_REF_SIZE;
1793 break;
1794 case DW_OP_implicit_value:
1795 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1796 + loc->dw_loc_oprnd1.v.val_unsigned;
1797 break;
1798 case DW_OP_implicit_pointer:
1799 case DW_OP_GNU_implicit_pointer:
1800 size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1801 break;
1802 case DW_OP_entry_value:
1803 case DW_OP_GNU_entry_value:
1805 unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
1806 size += size_of_uleb128 (op_size) + op_size;
1807 break;
1809 case DW_OP_const_type:
1810 case DW_OP_GNU_const_type:
1812 unsigned long o
1813 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1814 size += size_of_uleb128 (o) + 1;
1815 switch (loc->dw_loc_oprnd2.val_class)
1817 case dw_val_class_vec:
1818 size += loc->dw_loc_oprnd2.v.val_vec.length
1819 * loc->dw_loc_oprnd2.v.val_vec.elt_size;
1820 break;
1821 case dw_val_class_const:
1822 size += HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
1823 break;
1824 case dw_val_class_const_double:
1825 size += HOST_BITS_PER_DOUBLE_INT / BITS_PER_UNIT;
1826 break;
1827 case dw_val_class_wide_int:
1828 size += (get_full_len (*loc->dw_loc_oprnd2.v.val_wide)
1829 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
1830 break;
1831 default:
1832 gcc_unreachable ();
1834 break;
1836 case DW_OP_regval_type:
1837 case DW_OP_GNU_regval_type:
1839 unsigned long o
1840 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1841 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1842 + size_of_uleb128 (o);
1844 break;
1845 case DW_OP_deref_type:
1846 case DW_OP_GNU_deref_type:
1848 unsigned long o
1849 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1850 size += 1 + size_of_uleb128 (o);
1852 break;
1853 case DW_OP_convert:
1854 case DW_OP_reinterpret:
1855 case DW_OP_GNU_convert:
1856 case DW_OP_GNU_reinterpret:
1857 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
1858 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1859 else
1861 unsigned long o
1862 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1863 size += size_of_uleb128 (o);
1865 break;
1866 case DW_OP_GNU_parameter_ref:
1867 size += 4;
1868 break;
1869 default:
1870 break;
1873 return size;
1876 /* Return the size of a series of location descriptors. */
1878 unsigned long
1879 size_of_locs (dw_loc_descr_ref loc)
1881 dw_loc_descr_ref l;
1882 unsigned long size;
1884 /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
1885 field, to avoid writing to a PCH file. */
1886 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1888 if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
1889 break;
1890 size += size_of_loc_descr (l);
1892 if (! l)
1893 return size;
1895 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1897 l->dw_loc_addr = size;
1898 size += size_of_loc_descr (l);
1901 return size;
1904 /* Return the size of the value in a DW_AT_discr_value attribute. */
1906 static int
1907 size_of_discr_value (dw_discr_value *discr_value)
1909 if (discr_value->pos)
1910 return size_of_uleb128 (discr_value->v.uval);
1911 else
1912 return size_of_sleb128 (discr_value->v.sval);
1915 /* Return the size of the value in a DW_AT_discr_list attribute. */
1917 static int
1918 size_of_discr_list (dw_discr_list_ref discr_list)
1920 int size = 0;
1922 for (dw_discr_list_ref list = discr_list;
1923 list != NULL;
1924 list = list->dw_discr_next)
1926 /* One byte for the discriminant value descriptor, and then one or two
1927 LEB128 numbers, depending on whether it's a single case label or a
1928 range label. */
1929 size += 1;
1930 size += size_of_discr_value (&list->dw_discr_lower_bound);
1931 if (list->dw_discr_range != 0)
1932 size += size_of_discr_value (&list->dw_discr_upper_bound);
1934 return size;
1937 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
1938 static void get_ref_die_offset_label (char *, dw_die_ref);
1939 static unsigned long int get_ref_die_offset (dw_die_ref);
1941 /* Output location description stack opcode's operands (if any).
1942 The for_eh_or_skip parameter controls whether register numbers are
1943 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
1944 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
1945 info). This should be suppressed for the cases that have not been converted
1946 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
1948 static void
1949 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
1951 dw_val_ref val1 = &loc->dw_loc_oprnd1;
1952 dw_val_ref val2 = &loc->dw_loc_oprnd2;
1954 switch (loc->dw_loc_opc)
1956 #ifdef DWARF2_DEBUGGING_INFO
1957 case DW_OP_const2u:
1958 case DW_OP_const2s:
1959 dw2_asm_output_data (2, val1->v.val_int, NULL);
1960 break;
1961 case DW_OP_const4u:
1962 if (loc->dtprel)
1964 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1965 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
1966 val1->v.val_addr);
1967 fputc ('\n', asm_out_file);
1968 break;
1970 /* FALLTHRU */
1971 case DW_OP_const4s:
1972 dw2_asm_output_data (4, val1->v.val_int, NULL);
1973 break;
1974 case DW_OP_const8u:
1975 if (loc->dtprel)
1977 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1978 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
1979 val1->v.val_addr);
1980 fputc ('\n', asm_out_file);
1981 break;
1983 /* FALLTHRU */
1984 case DW_OP_const8s:
1985 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
1986 dw2_asm_output_data (8, val1->v.val_int, NULL);
1987 break;
1988 case DW_OP_skip:
1989 case DW_OP_bra:
1991 int offset;
1993 gcc_assert (val1->val_class == dw_val_class_loc);
1994 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
1996 dw2_asm_output_data (2, offset, NULL);
1998 break;
1999 case DW_OP_implicit_value:
2000 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2001 switch (val2->val_class)
2003 case dw_val_class_const:
2004 dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
2005 break;
2006 case dw_val_class_vec:
2008 unsigned int elt_size = val2->v.val_vec.elt_size;
2009 unsigned int len = val2->v.val_vec.length;
2010 unsigned int i;
2011 unsigned char *p;
2013 if (elt_size > sizeof (HOST_WIDE_INT))
2015 elt_size /= 2;
2016 len *= 2;
2018 for (i = 0, p = val2->v.val_vec.array;
2019 i < len;
2020 i++, p += elt_size)
2021 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2022 "fp or vector constant word %u", i);
2024 break;
2025 case dw_val_class_const_double:
2027 unsigned HOST_WIDE_INT first, second;
2029 if (WORDS_BIG_ENDIAN)
2031 first = val2->v.val_double.high;
2032 second = val2->v.val_double.low;
2034 else
2036 first = val2->v.val_double.low;
2037 second = val2->v.val_double.high;
2039 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2040 first, NULL);
2041 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2042 second, NULL);
2044 break;
2045 case dw_val_class_wide_int:
2047 int i;
2048 int len = get_full_len (*val2->v.val_wide);
2049 if (WORDS_BIG_ENDIAN)
2050 for (i = len - 1; i >= 0; --i)
2051 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2052 val2->v.val_wide->elt (i), NULL);
2053 else
2054 for (i = 0; i < len; ++i)
2055 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2056 val2->v.val_wide->elt (i), NULL);
2058 break;
2059 case dw_val_class_addr:
2060 gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
2061 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
2062 break;
2063 default:
2064 gcc_unreachable ();
2066 break;
2067 #else
2068 case DW_OP_const2u:
2069 case DW_OP_const2s:
2070 case DW_OP_const4u:
2071 case DW_OP_const4s:
2072 case DW_OP_const8u:
2073 case DW_OP_const8s:
2074 case DW_OP_skip:
2075 case DW_OP_bra:
2076 case DW_OP_implicit_value:
2077 /* We currently don't make any attempt to make sure these are
2078 aligned properly like we do for the main unwind info, so
2079 don't support emitting things larger than a byte if we're
2080 only doing unwinding. */
2081 gcc_unreachable ();
2082 #endif
2083 case DW_OP_const1u:
2084 case DW_OP_const1s:
2085 dw2_asm_output_data (1, val1->v.val_int, NULL);
2086 break;
2087 case DW_OP_constu:
2088 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2089 break;
2090 case DW_OP_consts:
2091 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2092 break;
2093 case DW_OP_pick:
2094 dw2_asm_output_data (1, val1->v.val_int, NULL);
2095 break;
2096 case DW_OP_plus_uconst:
2097 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2098 break;
2099 case DW_OP_breg0:
2100 case DW_OP_breg1:
2101 case DW_OP_breg2:
2102 case DW_OP_breg3:
2103 case DW_OP_breg4:
2104 case DW_OP_breg5:
2105 case DW_OP_breg6:
2106 case DW_OP_breg7:
2107 case DW_OP_breg8:
2108 case DW_OP_breg9:
2109 case DW_OP_breg10:
2110 case DW_OP_breg11:
2111 case DW_OP_breg12:
2112 case DW_OP_breg13:
2113 case DW_OP_breg14:
2114 case DW_OP_breg15:
2115 case DW_OP_breg16:
2116 case DW_OP_breg17:
2117 case DW_OP_breg18:
2118 case DW_OP_breg19:
2119 case DW_OP_breg20:
2120 case DW_OP_breg21:
2121 case DW_OP_breg22:
2122 case DW_OP_breg23:
2123 case DW_OP_breg24:
2124 case DW_OP_breg25:
2125 case DW_OP_breg26:
2126 case DW_OP_breg27:
2127 case DW_OP_breg28:
2128 case DW_OP_breg29:
2129 case DW_OP_breg30:
2130 case DW_OP_breg31:
2131 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2132 break;
2133 case DW_OP_regx:
2135 unsigned r = val1->v.val_unsigned;
2136 if (for_eh_or_skip >= 0)
2137 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2138 gcc_assert (size_of_uleb128 (r)
2139 == size_of_uleb128 (val1->v.val_unsigned));
2140 dw2_asm_output_data_uleb128 (r, NULL);
2142 break;
2143 case DW_OP_fbreg:
2144 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2145 break;
2146 case DW_OP_bregx:
2148 unsigned r = val1->v.val_unsigned;
2149 if (for_eh_or_skip >= 0)
2150 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2151 gcc_assert (size_of_uleb128 (r)
2152 == size_of_uleb128 (val1->v.val_unsigned));
2153 dw2_asm_output_data_uleb128 (r, NULL);
2154 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2156 break;
2157 case DW_OP_piece:
2158 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2159 break;
2160 case DW_OP_bit_piece:
2161 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2162 dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
2163 break;
2164 case DW_OP_deref_size:
2165 case DW_OP_xderef_size:
2166 dw2_asm_output_data (1, val1->v.val_int, NULL);
2167 break;
2169 case DW_OP_addr:
2170 if (loc->dtprel)
2172 if (targetm.asm_out.output_dwarf_dtprel)
2174 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
2175 DWARF2_ADDR_SIZE,
2176 val1->v.val_addr);
2177 fputc ('\n', asm_out_file);
2179 else
2180 gcc_unreachable ();
2182 else
2184 #ifdef DWARF2_DEBUGGING_INFO
2185 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
2186 #else
2187 gcc_unreachable ();
2188 #endif
2190 break;
2192 case DW_OP_GNU_addr_index:
2193 case DW_OP_GNU_const_index:
2194 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
2195 dw2_asm_output_data_uleb128 (loc->dw_loc_oprnd1.val_entry->index,
2196 "(index into .debug_addr)");
2197 break;
2199 case DW_OP_call2:
2200 case DW_OP_call4:
2202 unsigned long die_offset
2203 = get_ref_die_offset (val1->v.val_die_ref.die);
2204 /* Make sure the offset has been computed and that we can encode it as
2205 an operand. */
2206 gcc_assert (die_offset > 0
2207 && die_offset <= (loc->dw_loc_opc == DW_OP_call2
2208 ? 0xffff
2209 : 0xffffffff));
2210 dw2_asm_output_data ((loc->dw_loc_opc == DW_OP_call2) ? 2 : 4,
2211 die_offset, NULL);
2213 break;
2215 case DW_OP_implicit_pointer:
2216 case DW_OP_GNU_implicit_pointer:
2218 char label[MAX_ARTIFICIAL_LABEL_BYTES
2219 + HOST_BITS_PER_WIDE_INT / 2 + 2];
2220 gcc_assert (val1->val_class == dw_val_class_die_ref);
2221 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2222 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2223 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2225 break;
2227 case DW_OP_entry_value:
2228 case DW_OP_GNU_entry_value:
2229 dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
2230 output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
2231 break;
2233 case DW_OP_const_type:
2234 case DW_OP_GNU_const_type:
2236 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
2237 gcc_assert (o);
2238 dw2_asm_output_data_uleb128 (o, NULL);
2239 switch (val2->val_class)
2241 case dw_val_class_const:
2242 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2243 dw2_asm_output_data (1, l, NULL);
2244 dw2_asm_output_data (l, val2->v.val_int, NULL);
2245 break;
2246 case dw_val_class_vec:
2248 unsigned int elt_size = val2->v.val_vec.elt_size;
2249 unsigned int len = val2->v.val_vec.length;
2250 unsigned int i;
2251 unsigned char *p;
2253 l = len * elt_size;
2254 dw2_asm_output_data (1, l, NULL);
2255 if (elt_size > sizeof (HOST_WIDE_INT))
2257 elt_size /= 2;
2258 len *= 2;
2260 for (i = 0, p = val2->v.val_vec.array;
2261 i < len;
2262 i++, p += elt_size)
2263 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2264 "fp or vector constant word %u", i);
2266 break;
2267 case dw_val_class_const_double:
2269 unsigned HOST_WIDE_INT first, second;
2270 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2272 dw2_asm_output_data (1, 2 * l, NULL);
2273 if (WORDS_BIG_ENDIAN)
2275 first = val2->v.val_double.high;
2276 second = val2->v.val_double.low;
2278 else
2280 first = val2->v.val_double.low;
2281 second = val2->v.val_double.high;
2283 dw2_asm_output_data (l, first, NULL);
2284 dw2_asm_output_data (l, second, NULL);
2286 break;
2287 case dw_val_class_wide_int:
2289 int i;
2290 int len = get_full_len (*val2->v.val_wide);
2291 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2293 dw2_asm_output_data (1, len * l, NULL);
2294 if (WORDS_BIG_ENDIAN)
2295 for (i = len - 1; i >= 0; --i)
2296 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2297 else
2298 for (i = 0; i < len; ++i)
2299 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2301 break;
2302 default:
2303 gcc_unreachable ();
2306 break;
2307 case DW_OP_regval_type:
2308 case DW_OP_GNU_regval_type:
2310 unsigned r = val1->v.val_unsigned;
2311 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2312 gcc_assert (o);
2313 if (for_eh_or_skip >= 0)
2315 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2316 gcc_assert (size_of_uleb128 (r)
2317 == size_of_uleb128 (val1->v.val_unsigned));
2319 dw2_asm_output_data_uleb128 (r, NULL);
2320 dw2_asm_output_data_uleb128 (o, NULL);
2322 break;
2323 case DW_OP_deref_type:
2324 case DW_OP_GNU_deref_type:
2326 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2327 gcc_assert (o);
2328 dw2_asm_output_data (1, val1->v.val_int, NULL);
2329 dw2_asm_output_data_uleb128 (o, NULL);
2331 break;
2332 case DW_OP_convert:
2333 case DW_OP_reinterpret:
2334 case DW_OP_GNU_convert:
2335 case DW_OP_GNU_reinterpret:
2336 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2337 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2338 else
2340 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
2341 gcc_assert (o);
2342 dw2_asm_output_data_uleb128 (o, NULL);
2344 break;
2346 case DW_OP_GNU_parameter_ref:
2348 unsigned long o;
2349 gcc_assert (val1->val_class == dw_val_class_die_ref);
2350 o = get_ref_die_offset (val1->v.val_die_ref.die);
2351 dw2_asm_output_data (4, o, NULL);
2353 break;
2355 default:
2356 /* Other codes have no operands. */
2357 break;
2361 /* Output a sequence of location operations.
2362 The for_eh_or_skip parameter controls whether register numbers are
2363 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2364 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2365 info). This should be suppressed for the cases that have not been converted
2366 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
2368 void
2369 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
2371 for (; loc != NULL; loc = loc->dw_loc_next)
2373 enum dwarf_location_atom opc = loc->dw_loc_opc;
2374 /* Output the opcode. */
2375 if (for_eh_or_skip >= 0
2376 && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2378 unsigned r = (opc - DW_OP_breg0);
2379 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2380 gcc_assert (r <= 31);
2381 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2383 else if (for_eh_or_skip >= 0
2384 && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2386 unsigned r = (opc - DW_OP_reg0);
2387 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2388 gcc_assert (r <= 31);
2389 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2392 dw2_asm_output_data (1, opc,
2393 "%s", dwarf_stack_op_name (opc));
2395 /* Output the operand(s) (if any). */
2396 output_loc_operands (loc, for_eh_or_skip);
2400 /* Output location description stack opcode's operands (if any).
2401 The output is single bytes on a line, suitable for .cfi_escape. */
2403 static void
2404 output_loc_operands_raw (dw_loc_descr_ref loc)
2406 dw_val_ref val1 = &loc->dw_loc_oprnd1;
2407 dw_val_ref val2 = &loc->dw_loc_oprnd2;
2409 switch (loc->dw_loc_opc)
2411 case DW_OP_addr:
2412 case DW_OP_GNU_addr_index:
2413 case DW_OP_GNU_const_index:
2414 case DW_OP_implicit_value:
2415 /* We cannot output addresses in .cfi_escape, only bytes. */
2416 gcc_unreachable ();
2418 case DW_OP_const1u:
2419 case DW_OP_const1s:
2420 case DW_OP_pick:
2421 case DW_OP_deref_size:
2422 case DW_OP_xderef_size:
2423 fputc (',', asm_out_file);
2424 dw2_asm_output_data_raw (1, val1->v.val_int);
2425 break;
2427 case DW_OP_const2u:
2428 case DW_OP_const2s:
2429 fputc (',', asm_out_file);
2430 dw2_asm_output_data_raw (2, val1->v.val_int);
2431 break;
2433 case DW_OP_const4u:
2434 case DW_OP_const4s:
2435 fputc (',', asm_out_file);
2436 dw2_asm_output_data_raw (4, val1->v.val_int);
2437 break;
2439 case DW_OP_const8u:
2440 case DW_OP_const8s:
2441 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2442 fputc (',', asm_out_file);
2443 dw2_asm_output_data_raw (8, val1->v.val_int);
2444 break;
2446 case DW_OP_skip:
2447 case DW_OP_bra:
2449 int offset;
2451 gcc_assert (val1->val_class == dw_val_class_loc);
2452 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2454 fputc (',', asm_out_file);
2455 dw2_asm_output_data_raw (2, offset);
2457 break;
2459 case DW_OP_regx:
2461 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2462 gcc_assert (size_of_uleb128 (r)
2463 == size_of_uleb128 (val1->v.val_unsigned));
2464 fputc (',', asm_out_file);
2465 dw2_asm_output_data_uleb128_raw (r);
2467 break;
2469 case DW_OP_constu:
2470 case DW_OP_plus_uconst:
2471 case DW_OP_piece:
2472 fputc (',', asm_out_file);
2473 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2474 break;
2476 case DW_OP_bit_piece:
2477 fputc (',', asm_out_file);
2478 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2479 dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
2480 break;
2482 case DW_OP_consts:
2483 case DW_OP_breg0:
2484 case DW_OP_breg1:
2485 case DW_OP_breg2:
2486 case DW_OP_breg3:
2487 case DW_OP_breg4:
2488 case DW_OP_breg5:
2489 case DW_OP_breg6:
2490 case DW_OP_breg7:
2491 case DW_OP_breg8:
2492 case DW_OP_breg9:
2493 case DW_OP_breg10:
2494 case DW_OP_breg11:
2495 case DW_OP_breg12:
2496 case DW_OP_breg13:
2497 case DW_OP_breg14:
2498 case DW_OP_breg15:
2499 case DW_OP_breg16:
2500 case DW_OP_breg17:
2501 case DW_OP_breg18:
2502 case DW_OP_breg19:
2503 case DW_OP_breg20:
2504 case DW_OP_breg21:
2505 case DW_OP_breg22:
2506 case DW_OP_breg23:
2507 case DW_OP_breg24:
2508 case DW_OP_breg25:
2509 case DW_OP_breg26:
2510 case DW_OP_breg27:
2511 case DW_OP_breg28:
2512 case DW_OP_breg29:
2513 case DW_OP_breg30:
2514 case DW_OP_breg31:
2515 case DW_OP_fbreg:
2516 fputc (',', asm_out_file);
2517 dw2_asm_output_data_sleb128_raw (val1->v.val_int);
2518 break;
2520 case DW_OP_bregx:
2522 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2523 gcc_assert (size_of_uleb128 (r)
2524 == size_of_uleb128 (val1->v.val_unsigned));
2525 fputc (',', asm_out_file);
2526 dw2_asm_output_data_uleb128_raw (r);
2527 fputc (',', asm_out_file);
2528 dw2_asm_output_data_sleb128_raw (val2->v.val_int);
2530 break;
2532 case DW_OP_implicit_pointer:
2533 case DW_OP_entry_value:
2534 case DW_OP_const_type:
2535 case DW_OP_regval_type:
2536 case DW_OP_deref_type:
2537 case DW_OP_convert:
2538 case DW_OP_reinterpret:
2539 case DW_OP_GNU_implicit_pointer:
2540 case DW_OP_GNU_entry_value:
2541 case DW_OP_GNU_const_type:
2542 case DW_OP_GNU_regval_type:
2543 case DW_OP_GNU_deref_type:
2544 case DW_OP_GNU_convert:
2545 case DW_OP_GNU_reinterpret:
2546 case DW_OP_GNU_parameter_ref:
2547 gcc_unreachable ();
2548 break;
2550 default:
2551 /* Other codes have no operands. */
2552 break;
2556 void
2557 output_loc_sequence_raw (dw_loc_descr_ref loc)
2559 while (1)
2561 enum dwarf_location_atom opc = loc->dw_loc_opc;
2562 /* Output the opcode. */
2563 if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2565 unsigned r = (opc - DW_OP_breg0);
2566 r = DWARF2_FRAME_REG_OUT (r, 1);
2567 gcc_assert (r <= 31);
2568 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2570 else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2572 unsigned r = (opc - DW_OP_reg0);
2573 r = DWARF2_FRAME_REG_OUT (r, 1);
2574 gcc_assert (r <= 31);
2575 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2577 /* Output the opcode. */
2578 fprintf (asm_out_file, "%#x", opc);
2579 output_loc_operands_raw (loc);
2581 if (!loc->dw_loc_next)
2582 break;
2583 loc = loc->dw_loc_next;
2585 fputc (',', asm_out_file);
2589 /* This function builds a dwarf location descriptor sequence from a
2590 dw_cfa_location, adding the given OFFSET to the result of the
2591 expression. */
2593 struct dw_loc_descr_node *
2594 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
2596 struct dw_loc_descr_node *head, *tmp;
2598 offset += cfa->offset;
2600 if (cfa->indirect)
2602 head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
2603 head->dw_loc_oprnd1.val_class = dw_val_class_const;
2604 head->dw_loc_oprnd1.val_entry = NULL;
2605 tmp = new_loc_descr (DW_OP_deref, 0, 0);
2606 add_loc_descr (&head, tmp);
2607 if (offset != 0)
2609 tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
2610 add_loc_descr (&head, tmp);
2613 else
2614 head = new_reg_loc_descr (cfa->reg, offset);
2616 return head;
2619 /* This function builds a dwarf location descriptor sequence for
2620 the address at OFFSET from the CFA when stack is aligned to
2621 ALIGNMENT byte. */
2623 struct dw_loc_descr_node *
2624 build_cfa_aligned_loc (dw_cfa_location *cfa,
2625 HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
2627 struct dw_loc_descr_node *head;
2628 unsigned int dwarf_fp
2629 = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
2631 /* When CFA is defined as FP+OFFSET, emulate stack alignment. */
2632 if (cfa->reg == HARD_FRAME_POINTER_REGNUM && cfa->indirect == 0)
2634 head = new_reg_loc_descr (dwarf_fp, 0);
2635 add_loc_descr (&head, int_loc_descriptor (alignment));
2636 add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
2637 loc_descr_plus_const (&head, offset);
2639 else
2640 head = new_reg_loc_descr (dwarf_fp, offset);
2641 return head;
2644 /* And now, the support for symbolic debugging information. */
2646 /* .debug_str support. */
2648 static void dwarf2out_init (const char *);
2649 static void dwarf2out_finish (const char *);
2650 static void dwarf2out_early_finish (const char *);
2651 static void dwarf2out_assembly_start (void);
2652 static void dwarf2out_define (unsigned int, const char *);
2653 static void dwarf2out_undef (unsigned int, const char *);
2654 static void dwarf2out_start_source_file (unsigned, const char *);
2655 static void dwarf2out_end_source_file (unsigned);
2656 static void dwarf2out_function_decl (tree);
2657 static void dwarf2out_begin_block (unsigned, unsigned);
2658 static void dwarf2out_end_block (unsigned, unsigned);
2659 static bool dwarf2out_ignore_block (const_tree);
2660 static void dwarf2out_early_global_decl (tree);
2661 static void dwarf2out_late_global_decl (tree);
2662 static void dwarf2out_type_decl (tree, int);
2663 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
2664 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
2665 dw_die_ref);
2666 static void dwarf2out_abstract_function (tree);
2667 static void dwarf2out_var_location (rtx_insn *);
2668 static void dwarf2out_size_function (tree);
2669 static void dwarf2out_begin_function (tree);
2670 static void dwarf2out_end_function (unsigned int);
2671 static void dwarf2out_register_main_translation_unit (tree unit);
2672 static void dwarf2out_set_name (tree, tree);
2674 /* The debug hooks structure. */
2676 const struct gcc_debug_hooks dwarf2_debug_hooks =
2678 dwarf2out_init,
2679 dwarf2out_finish,
2680 dwarf2out_early_finish,
2681 dwarf2out_assembly_start,
2682 dwarf2out_define,
2683 dwarf2out_undef,
2684 dwarf2out_start_source_file,
2685 dwarf2out_end_source_file,
2686 dwarf2out_begin_block,
2687 dwarf2out_end_block,
2688 dwarf2out_ignore_block,
2689 dwarf2out_source_line,
2690 dwarf2out_begin_prologue,
2691 #if VMS_DEBUGGING_INFO
2692 dwarf2out_vms_end_prologue,
2693 dwarf2out_vms_begin_epilogue,
2694 #else
2695 debug_nothing_int_charstar,
2696 debug_nothing_int_charstar,
2697 #endif
2698 dwarf2out_end_epilogue,
2699 dwarf2out_begin_function,
2700 dwarf2out_end_function, /* end_function */
2701 dwarf2out_register_main_translation_unit,
2702 dwarf2out_function_decl, /* function_decl */
2703 dwarf2out_early_global_decl,
2704 dwarf2out_late_global_decl,
2705 dwarf2out_type_decl, /* type_decl */
2706 dwarf2out_imported_module_or_decl,
2707 debug_nothing_tree, /* deferred_inline_function */
2708 /* The DWARF 2 backend tries to reduce debugging bloat by not
2709 emitting the abstract description of inline functions until
2710 something tries to reference them. */
2711 dwarf2out_abstract_function, /* outlining_inline_function */
2712 debug_nothing_rtx_code_label, /* label */
2713 debug_nothing_int, /* handle_pch */
2714 dwarf2out_var_location,
2715 dwarf2out_size_function, /* size_function */
2716 dwarf2out_switch_text_section,
2717 dwarf2out_set_name,
2718 1, /* start_end_main_source_file */
2719 TYPE_SYMTAB_IS_DIE /* tree_type_symtab_field */
2722 const struct gcc_debug_hooks dwarf2_lineno_debug_hooks =
2724 dwarf2out_init,
2725 debug_nothing_charstar,
2726 debug_nothing_charstar,
2727 dwarf2out_assembly_start,
2728 debug_nothing_int_charstar,
2729 debug_nothing_int_charstar,
2730 debug_nothing_int_charstar,
2731 debug_nothing_int,
2732 debug_nothing_int_int, /* begin_block */
2733 debug_nothing_int_int, /* end_block */
2734 debug_true_const_tree, /* ignore_block */
2735 dwarf2out_source_line, /* source_line */
2736 debug_nothing_int_charstar, /* begin_prologue */
2737 debug_nothing_int_charstar, /* end_prologue */
2738 debug_nothing_int_charstar, /* begin_epilogue */
2739 debug_nothing_int_charstar, /* end_epilogue */
2740 debug_nothing_tree, /* begin_function */
2741 debug_nothing_int, /* end_function */
2742 debug_nothing_tree, /* register_main_translation_unit */
2743 debug_nothing_tree, /* function_decl */
2744 debug_nothing_tree, /* early_global_decl */
2745 debug_nothing_tree, /* late_global_decl */
2746 debug_nothing_tree_int, /* type_decl */
2747 debug_nothing_tree_tree_tree_bool, /* imported_module_or_decl */
2748 debug_nothing_tree, /* deferred_inline_function */
2749 debug_nothing_tree, /* outlining_inline_function */
2750 debug_nothing_rtx_code_label, /* label */
2751 debug_nothing_int, /* handle_pch */
2752 debug_nothing_rtx_insn, /* var_location */
2753 debug_nothing_tree, /* size_function */
2754 debug_nothing_void, /* switch_text_section */
2755 debug_nothing_tree_tree, /* set_name */
2756 0, /* start_end_main_source_file */
2757 TYPE_SYMTAB_IS_ADDRESS /* tree_type_symtab_field */
2760 /* NOTE: In the comments in this file, many references are made to
2761 "Debugging Information Entries". This term is abbreviated as `DIE'
2762 throughout the remainder of this file. */
2764 /* An internal representation of the DWARF output is built, and then
2765 walked to generate the DWARF debugging info. The walk of the internal
2766 representation is done after the entire program has been compiled.
2767 The types below are used to describe the internal representation. */
2769 /* Whether to put type DIEs into their own section .debug_types instead
2770 of making them part of the .debug_info section. Only supported for
2771 Dwarf V4 or higher and the user didn't disable them through
2772 -fno-debug-types-section. It is more efficient to put them in a
2773 separate comdat sections since the linker will then be able to
2774 remove duplicates. But not all tools support .debug_types sections
2775 yet. For Dwarf V5 or higher .debug_types doesn't exist any more,
2776 it is DW_UT_type unit type in .debug_info section. */
2778 #define use_debug_types (dwarf_version >= 4 && flag_debug_types_section)
2780 /* Various DIE's use offsets relative to the beginning of the
2781 .debug_info section to refer to each other. */
2783 typedef long int dw_offset;
2785 struct comdat_type_node;
2787 /* The entries in the line_info table more-or-less mirror the opcodes
2788 that are used in the real dwarf line table. Arrays of these entries
2789 are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
2790 supported. */
2792 enum dw_line_info_opcode {
2793 /* Emit DW_LNE_set_address; the operand is the label index. */
2794 LI_set_address,
2796 /* Emit a row to the matrix with the given line. This may be done
2797 via any combination of DW_LNS_copy, DW_LNS_advance_line, and
2798 special opcodes. */
2799 LI_set_line,
2801 /* Emit a DW_LNS_set_file. */
2802 LI_set_file,
2804 /* Emit a DW_LNS_set_column. */
2805 LI_set_column,
2807 /* Emit a DW_LNS_negate_stmt; the operand is ignored. */
2808 LI_negate_stmt,
2810 /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored. */
2811 LI_set_prologue_end,
2812 LI_set_epilogue_begin,
2814 /* Emit a DW_LNE_set_discriminator. */
2815 LI_set_discriminator
2818 typedef struct GTY(()) dw_line_info_struct {
2819 enum dw_line_info_opcode opcode;
2820 unsigned int val;
2821 } dw_line_info_entry;
2824 struct GTY(()) dw_line_info_table {
2825 /* The label that marks the end of this section. */
2826 const char *end_label;
2828 /* The values for the last row of the matrix, as collected in the table.
2829 These are used to minimize the changes to the next row. */
2830 unsigned int file_num;
2831 unsigned int line_num;
2832 unsigned int column_num;
2833 int discrim_num;
2834 bool is_stmt;
2835 bool in_use;
2837 vec<dw_line_info_entry, va_gc> *entries;
2841 /* Each DIE attribute has a field specifying the attribute kind,
2842 a link to the next attribute in the chain, and an attribute value.
2843 Attributes are typically linked below the DIE they modify. */
2845 typedef struct GTY(()) dw_attr_struct {
2846 enum dwarf_attribute dw_attr;
2847 dw_val_node dw_attr_val;
2849 dw_attr_node;
2852 /* The Debugging Information Entry (DIE) structure. DIEs form a tree.
2853 The children of each node form a circular list linked by
2854 die_sib. die_child points to the node *before* the "first" child node. */
2856 typedef struct GTY((chain_circular ("%h.die_sib"), for_user)) die_struct {
2857 union die_symbol_or_type_node
2859 const char * GTY ((tag ("0"))) die_symbol;
2860 comdat_type_node *GTY ((tag ("1"))) die_type_node;
2862 GTY ((desc ("%0.comdat_type_p"))) die_id;
2863 vec<dw_attr_node, va_gc> *die_attr;
2864 dw_die_ref die_parent;
2865 dw_die_ref die_child;
2866 dw_die_ref die_sib;
2867 dw_die_ref die_definition; /* ref from a specification to its definition */
2868 dw_offset die_offset;
2869 unsigned long die_abbrev;
2870 int die_mark;
2871 unsigned int decl_id;
2872 enum dwarf_tag die_tag;
2873 /* Die is used and must not be pruned as unused. */
2874 BOOL_BITFIELD die_perennial_p : 1;
2875 BOOL_BITFIELD comdat_type_p : 1; /* DIE has a type signature */
2876 /* Whether this DIE was removed from the DIE tree, for example via
2877 prune_unused_types. We don't consider those present from the
2878 DIE lookup routines. */
2879 BOOL_BITFIELD removed : 1;
2880 /* Lots of spare bits. */
2882 die_node;
2884 /* Set to TRUE while dwarf2out_early_global_decl is running. */
2885 static bool early_dwarf;
2886 static bool early_dwarf_finished;
2887 struct set_early_dwarf {
2888 bool saved;
2889 set_early_dwarf () : saved(early_dwarf)
2891 gcc_assert (! early_dwarf_finished);
2892 early_dwarf = true;
2894 ~set_early_dwarf () { early_dwarf = saved; }
2897 /* Evaluate 'expr' while 'c' is set to each child of DIE in order. */
2898 #define FOR_EACH_CHILD(die, c, expr) do { \
2899 c = die->die_child; \
2900 if (c) do { \
2901 c = c->die_sib; \
2902 expr; \
2903 } while (c != die->die_child); \
2904 } while (0)
2906 /* The pubname structure */
2908 typedef struct GTY(()) pubname_struct {
2909 dw_die_ref die;
2910 const char *name;
2912 pubname_entry;
2915 struct GTY(()) dw_ranges {
2916 const char *label;
2917 /* If this is positive, it's a block number, otherwise it's a
2918 bitwise-negated index into dw_ranges_by_label. */
2919 int num;
2920 /* Index for the range list for DW_FORM_rnglistx. */
2921 unsigned int idx : 31;
2922 /* True if this range might be possibly in a different section
2923 from previous entry. */
2924 unsigned int maybe_new_sec : 1;
2927 /* A structure to hold a macinfo entry. */
2929 typedef struct GTY(()) macinfo_struct {
2930 unsigned char code;
2931 unsigned HOST_WIDE_INT lineno;
2932 const char *info;
2934 macinfo_entry;
2937 struct GTY(()) dw_ranges_by_label {
2938 const char *begin;
2939 const char *end;
2942 /* The comdat type node structure. */
2943 struct GTY(()) comdat_type_node
2945 dw_die_ref root_die;
2946 dw_die_ref type_die;
2947 dw_die_ref skeleton_die;
2948 char signature[DWARF_TYPE_SIGNATURE_SIZE];
2949 comdat_type_node *next;
2952 /* A list of DIEs for which we can't determine ancestry (parent_die
2953 field) just yet. Later in dwarf2out_finish we will fill in the
2954 missing bits. */
2955 typedef struct GTY(()) limbo_die_struct {
2956 dw_die_ref die;
2957 /* The tree for which this DIE was created. We use this to
2958 determine ancestry later. */
2959 tree created_for;
2960 struct limbo_die_struct *next;
2962 limbo_die_node;
2964 typedef struct skeleton_chain_struct
2966 dw_die_ref old_die;
2967 dw_die_ref new_die;
2968 struct skeleton_chain_struct *parent;
2970 skeleton_chain_node;
2972 /* Define a macro which returns nonzero for a TYPE_DECL which was
2973 implicitly generated for a type.
2975 Note that, unlike the C front-end (which generates a NULL named
2976 TYPE_DECL node for each complete tagged type, each array type,
2977 and each function type node created) the C++ front-end generates
2978 a _named_ TYPE_DECL node for each tagged type node created.
2979 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
2980 generate a DW_TAG_typedef DIE for them. Likewise with the Ada
2981 front-end, but for each type, tagged or not. */
2983 #define TYPE_DECL_IS_STUB(decl) \
2984 (DECL_NAME (decl) == NULL_TREE \
2985 || (DECL_ARTIFICIAL (decl) \
2986 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
2987 /* This is necessary for stub decls that \
2988 appear in nested inline functions. */ \
2989 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
2990 && (decl_ultimate_origin (decl) \
2991 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
2993 /* Information concerning the compilation unit's programming
2994 language, and compiler version. */
2996 /* Fixed size portion of the DWARF compilation unit header. */
2997 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
2998 (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE \
2999 + (dwarf_version >= 5 ? 4 : 3))
3001 /* Fixed size portion of the DWARF comdat type unit header. */
3002 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
3003 (DWARF_COMPILE_UNIT_HEADER_SIZE \
3004 + DWARF_TYPE_SIGNATURE_SIZE + DWARF_OFFSET_SIZE)
3006 /* Fixed size portion of the DWARF skeleton compilation unit header. */
3007 #define DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE \
3008 (DWARF_COMPILE_UNIT_HEADER_SIZE + (dwarf_version >= 5 ? 8 : 0))
3010 /* Fixed size portion of public names info. */
3011 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
3013 /* Fixed size portion of the address range info. */
3014 #define DWARF_ARANGES_HEADER_SIZE \
3015 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
3016 DWARF2_ADDR_SIZE * 2) \
3017 - DWARF_INITIAL_LENGTH_SIZE)
3019 /* Size of padding portion in the address range info. It must be
3020 aligned to twice the pointer size. */
3021 #define DWARF_ARANGES_PAD_SIZE \
3022 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
3023 DWARF2_ADDR_SIZE * 2) \
3024 - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
3026 /* Use assembler line directives if available. */
3027 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
3028 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
3029 #define DWARF2_ASM_LINE_DEBUG_INFO 1
3030 #else
3031 #define DWARF2_ASM_LINE_DEBUG_INFO 0
3032 #endif
3033 #endif
3035 /* Minimum line offset in a special line info. opcode.
3036 This value was chosen to give a reasonable range of values. */
3037 #define DWARF_LINE_BASE -10
3039 /* First special line opcode - leave room for the standard opcodes. */
3040 #define DWARF_LINE_OPCODE_BASE ((int)DW_LNS_set_isa + 1)
3042 /* Range of line offsets in a special line info. opcode. */
3043 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
3045 /* Flag that indicates the initial value of the is_stmt_start flag.
3046 In the present implementation, we do not mark any lines as
3047 the beginning of a source statement, because that information
3048 is not made available by the GCC front-end. */
3049 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
3051 /* Maximum number of operations per instruction bundle. */
3052 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
3053 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
3054 #endif
3056 /* This location is used by calc_die_sizes() to keep track
3057 the offset of each DIE within the .debug_info section. */
3058 static unsigned long next_die_offset;
3060 /* Record the root of the DIE's built for the current compilation unit. */
3061 static GTY(()) dw_die_ref single_comp_unit_die;
3063 /* A list of type DIEs that have been separated into comdat sections. */
3064 static GTY(()) comdat_type_node *comdat_type_list;
3066 /* A list of CU DIEs that have been separated. */
3067 static GTY(()) limbo_die_node *cu_die_list;
3069 /* A list of DIEs with a NULL parent waiting to be relocated. */
3070 static GTY(()) limbo_die_node *limbo_die_list;
3072 /* A list of DIEs for which we may have to generate
3073 DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set. */
3074 static GTY(()) limbo_die_node *deferred_asm_name;
3076 struct dwarf_file_hasher : ggc_ptr_hash<dwarf_file_data>
3078 typedef const char *compare_type;
3080 static hashval_t hash (dwarf_file_data *);
3081 static bool equal (dwarf_file_data *, const char *);
3084 /* Filenames referenced by this compilation unit. */
3085 static GTY(()) hash_table<dwarf_file_hasher> *file_table;
3087 struct decl_die_hasher : ggc_ptr_hash<die_node>
3089 typedef tree compare_type;
3091 static hashval_t hash (die_node *);
3092 static bool equal (die_node *, tree);
3094 /* A hash table of references to DIE's that describe declarations.
3095 The key is a DECL_UID() which is a unique number identifying each decl. */
3096 static GTY (()) hash_table<decl_die_hasher> *decl_die_table;
3098 struct block_die_hasher : ggc_ptr_hash<die_struct>
3100 static hashval_t hash (die_struct *);
3101 static bool equal (die_struct *, die_struct *);
3104 /* A hash table of references to DIE's that describe COMMON blocks.
3105 The key is DECL_UID() ^ die_parent. */
3106 static GTY (()) hash_table<block_die_hasher> *common_block_die_table;
3108 typedef struct GTY(()) die_arg_entry_struct {
3109 dw_die_ref die;
3110 tree arg;
3111 } die_arg_entry;
3114 /* Node of the variable location list. */
3115 struct GTY ((chain_next ("%h.next"))) var_loc_node {
3116 /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
3117 EXPR_LIST chain. For small bitsizes, bitsize is encoded
3118 in mode of the EXPR_LIST node and first EXPR_LIST operand
3119 is either NOTE_INSN_VAR_LOCATION for a piece with a known
3120 location or NULL for padding. For larger bitsizes,
3121 mode is 0 and first operand is a CONCAT with bitsize
3122 as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
3123 NULL as second operand. */
3124 rtx GTY (()) loc;
3125 const char * GTY (()) label;
3126 struct var_loc_node * GTY (()) next;
3129 /* Variable location list. */
3130 struct GTY ((for_user)) var_loc_list_def {
3131 struct var_loc_node * GTY (()) first;
3133 /* Pointer to the last but one or last element of the
3134 chained list. If the list is empty, both first and
3135 last are NULL, if the list contains just one node
3136 or the last node certainly is not redundant, it points
3137 to the last node, otherwise points to the last but one.
3138 Do not mark it for GC because it is marked through the chain. */
3139 struct var_loc_node * GTY ((skip ("%h"))) last;
3141 /* Pointer to the last element before section switch,
3142 if NULL, either sections weren't switched or first
3143 is after section switch. */
3144 struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
3146 /* DECL_UID of the variable decl. */
3147 unsigned int decl_id;
3149 typedef struct var_loc_list_def var_loc_list;
3151 /* Call argument location list. */
3152 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
3153 rtx GTY (()) call_arg_loc_note;
3154 const char * GTY (()) label;
3155 tree GTY (()) block;
3156 bool tail_call_p;
3157 rtx GTY (()) symbol_ref;
3158 struct call_arg_loc_node * GTY (()) next;
3162 struct decl_loc_hasher : ggc_ptr_hash<var_loc_list>
3164 typedef const_tree compare_type;
3166 static hashval_t hash (var_loc_list *);
3167 static bool equal (var_loc_list *, const_tree);
3170 /* Table of decl location linked lists. */
3171 static GTY (()) hash_table<decl_loc_hasher> *decl_loc_table;
3173 /* Head and tail of call_arg_loc chain. */
3174 static GTY (()) struct call_arg_loc_node *call_arg_locations;
3175 static struct call_arg_loc_node *call_arg_loc_last;
3177 /* Number of call sites in the current function. */
3178 static int call_site_count = -1;
3179 /* Number of tail call sites in the current function. */
3180 static int tail_call_site_count = -1;
3182 /* A cached location list. */
3183 struct GTY ((for_user)) cached_dw_loc_list_def {
3184 /* The DECL_UID of the decl that this entry describes. */
3185 unsigned int decl_id;
3187 /* The cached location list. */
3188 dw_loc_list_ref loc_list;
3190 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
3192 struct dw_loc_list_hasher : ggc_ptr_hash<cached_dw_loc_list>
3195 typedef const_tree compare_type;
3197 static hashval_t hash (cached_dw_loc_list *);
3198 static bool equal (cached_dw_loc_list *, const_tree);
3201 /* Table of cached location lists. */
3202 static GTY (()) hash_table<dw_loc_list_hasher> *cached_dw_loc_list_table;
3204 /* A vector of references to DIE's that are uniquely identified by their tag,
3205 presence/absence of children DIE's, and list of attribute/value pairs. */
3206 static GTY(()) vec<dw_die_ref, va_gc> *abbrev_die_table;
3208 /* A hash map to remember the stack usage for DWARF procedures. The value
3209 stored is the stack size difference between before the DWARF procedure
3210 invokation and after it returned. In other words, for a DWARF procedure
3211 that consumes N stack slots and that pushes M ones, this stores M - N. */
3212 static hash_map<dw_die_ref, int> *dwarf_proc_stack_usage_map;
3214 /* A global counter for generating labels for line number data. */
3215 static unsigned int line_info_label_num;
3217 /* The current table to which we should emit line number information
3218 for the current function. This will be set up at the beginning of
3219 assembly for the function. */
3220 static GTY(()) dw_line_info_table *cur_line_info_table;
3222 /* The two default tables of line number info. */
3223 static GTY(()) dw_line_info_table *text_section_line_info;
3224 static GTY(()) dw_line_info_table *cold_text_section_line_info;
3226 /* The set of all non-default tables of line number info. */
3227 static GTY(()) vec<dw_line_info_table *, va_gc> *separate_line_info;
3229 /* A flag to tell pubnames/types export if there is an info section to
3230 refer to. */
3231 static bool info_section_emitted;
3233 /* A pointer to the base of a table that contains a list of publicly
3234 accessible names. */
3235 static GTY (()) vec<pubname_entry, va_gc> *pubname_table;
3237 /* A pointer to the base of a table that contains a list of publicly
3238 accessible types. */
3239 static GTY (()) vec<pubname_entry, va_gc> *pubtype_table;
3241 /* A pointer to the base of a table that contains a list of macro
3242 defines/undefines (and file start/end markers). */
3243 static GTY (()) vec<macinfo_entry, va_gc> *macinfo_table;
3245 /* True if .debug_macinfo or .debug_macros section is going to be
3246 emitted. */
3247 #define have_macinfo \
3248 ((!XCOFF_DEBUGGING_INFO || HAVE_XCOFF_DWARF_EXTRAS) \
3249 && debug_info_level >= DINFO_LEVEL_VERBOSE \
3250 && !macinfo_table->is_empty ())
3252 /* Vector of dies for which we should generate .debug_ranges info. */
3253 static GTY (()) vec<dw_ranges, va_gc> *ranges_table;
3255 /* Vector of pairs of labels referenced in ranges_table. */
3256 static GTY (()) vec<dw_ranges_by_label, va_gc> *ranges_by_label;
3258 /* Whether we have location lists that need outputting */
3259 static GTY(()) bool have_location_lists;
3261 /* Unique label counter. */
3262 static GTY(()) unsigned int loclabel_num;
3264 /* Unique label counter for point-of-call tables. */
3265 static GTY(()) unsigned int poc_label_num;
3267 /* The last file entry emitted by maybe_emit_file(). */
3268 static GTY(()) struct dwarf_file_data * last_emitted_file;
3270 /* Number of internal labels generated by gen_internal_sym(). */
3271 static GTY(()) int label_num;
3273 static GTY(()) vec<die_arg_entry, va_gc> *tmpl_value_parm_die_table;
3275 /* Instances of generic types for which we need to generate debug
3276 info that describe their generic parameters and arguments. That
3277 generation needs to happen once all types are properly laid out so
3278 we do it at the end of compilation. */
3279 static GTY(()) vec<tree, va_gc> *generic_type_instances;
3281 /* Offset from the "steady-state frame pointer" to the frame base,
3282 within the current function. */
3283 static HOST_WIDE_INT frame_pointer_fb_offset;
3284 static bool frame_pointer_fb_offset_valid;
3286 static vec<dw_die_ref> base_types;
3288 /* Pointer to vector of DW_TAG_string_type DIEs that need finalization
3289 once all arguments are parsed. */
3290 static vec<dw_die_ref> *string_types;
3292 /* Flags to represent a set of attribute classes for attributes that represent
3293 a scalar value (bounds, pointers, ...). */
3294 enum dw_scalar_form
3296 dw_scalar_form_constant = 0x01,
3297 dw_scalar_form_exprloc = 0x02,
3298 dw_scalar_form_reference = 0x04
3301 /* Forward declarations for functions defined in this file. */
3303 static int is_pseudo_reg (const_rtx);
3304 static tree type_main_variant (tree);
3305 static int is_tagged_type (const_tree);
3306 static const char *dwarf_tag_name (unsigned);
3307 static const char *dwarf_attr_name (unsigned);
3308 static const char *dwarf_form_name (unsigned);
3309 static tree decl_ultimate_origin (const_tree);
3310 static tree decl_class_context (tree);
3311 static void add_dwarf_attr (dw_die_ref, dw_attr_node *);
3312 static inline enum dw_val_class AT_class (dw_attr_node *);
3313 static inline unsigned int AT_index (dw_attr_node *);
3314 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
3315 static inline unsigned AT_flag (dw_attr_node *);
3316 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
3317 static inline HOST_WIDE_INT AT_int (dw_attr_node *);
3318 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
3319 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_node *);
3320 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
3321 HOST_WIDE_INT, unsigned HOST_WIDE_INT);
3322 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
3323 unsigned int, unsigned char *);
3324 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
3325 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
3326 static inline const char *AT_string (dw_attr_node *);
3327 static enum dwarf_form AT_string_form (dw_attr_node *);
3328 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
3329 static void add_AT_specification (dw_die_ref, dw_die_ref);
3330 static inline dw_die_ref AT_ref (dw_attr_node *);
3331 static inline int AT_ref_external (dw_attr_node *);
3332 static inline void set_AT_ref_external (dw_attr_node *, int);
3333 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
3334 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
3335 static inline dw_loc_descr_ref AT_loc (dw_attr_node *);
3336 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
3337 dw_loc_list_ref);
3338 static inline dw_loc_list_ref AT_loc_list (dw_attr_node *);
3339 static addr_table_entry *add_addr_table_entry (void *, enum ate_kind);
3340 static void remove_addr_table_entry (addr_table_entry *);
3341 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx, bool);
3342 static inline rtx AT_addr (dw_attr_node *);
3343 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
3344 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
3345 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
3346 static void add_AT_loclistsptr (dw_die_ref, enum dwarf_attribute,
3347 const char *);
3348 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
3349 unsigned HOST_WIDE_INT);
3350 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
3351 unsigned long, bool);
3352 static inline const char *AT_lbl (dw_attr_node *);
3353 static dw_attr_node *get_AT (dw_die_ref, enum dwarf_attribute);
3354 static const char *get_AT_low_pc (dw_die_ref);
3355 static const char *get_AT_hi_pc (dw_die_ref);
3356 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
3357 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
3358 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
3359 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
3360 static bool is_cxx (void);
3361 static bool is_cxx (const_tree);
3362 static bool is_fortran (void);
3363 static bool is_ada (void);
3364 static bool remove_AT (dw_die_ref, enum dwarf_attribute);
3365 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
3366 static void add_child_die (dw_die_ref, dw_die_ref);
3367 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
3368 static dw_die_ref lookup_type_die (tree);
3369 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
3370 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
3371 static void equate_type_number_to_die (tree, dw_die_ref);
3372 static dw_die_ref lookup_decl_die (tree);
3373 static var_loc_list *lookup_decl_loc (const_tree);
3374 static void equate_decl_number_to_die (tree, dw_die_ref);
3375 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
3376 static void print_spaces (FILE *);
3377 static void print_die (dw_die_ref, FILE *);
3378 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
3379 static dw_die_ref pop_compile_unit (dw_die_ref);
3380 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3381 static void attr_checksum (dw_attr_node *, struct md5_ctx *, int *);
3382 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3383 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
3384 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
3385 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
3386 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_node *,
3387 struct md5_ctx *, int *);
3388 struct checksum_attributes;
3389 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
3390 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
3391 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
3392 static void generate_type_signature (dw_die_ref, comdat_type_node *);
3393 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3394 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
3395 static int same_attr_p (dw_attr_node *, dw_attr_node *, int *);
3396 static int same_die_p (dw_die_ref, dw_die_ref, int *);
3397 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
3398 static void compute_section_prefix (dw_die_ref);
3399 static int is_type_die (dw_die_ref);
3400 static int is_comdat_die (dw_die_ref);
3401 static int is_symbol_die (dw_die_ref);
3402 static inline bool is_template_instantiation (dw_die_ref);
3403 static void assign_symbol_names (dw_die_ref);
3404 static void break_out_includes (dw_die_ref);
3405 static int is_declaration_die (dw_die_ref);
3406 static int should_move_die_to_comdat (dw_die_ref);
3407 static dw_die_ref clone_as_declaration (dw_die_ref);
3408 static dw_die_ref clone_die (dw_die_ref);
3409 static dw_die_ref clone_tree (dw_die_ref);
3410 static dw_die_ref copy_declaration_context (dw_die_ref, dw_die_ref);
3411 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
3412 static void generate_skeleton_bottom_up (skeleton_chain_node *);
3413 static dw_die_ref generate_skeleton (dw_die_ref);
3414 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
3415 dw_die_ref,
3416 dw_die_ref);
3417 static void break_out_comdat_types (dw_die_ref);
3418 static void copy_decls_for_unworthy_types (dw_die_ref);
3420 static void add_sibling_attributes (dw_die_ref);
3421 static void output_location_lists (dw_die_ref);
3422 static int constant_size (unsigned HOST_WIDE_INT);
3423 static unsigned long size_of_die (dw_die_ref);
3424 static void calc_die_sizes (dw_die_ref);
3425 static void calc_base_type_die_sizes (void);
3426 static void mark_dies (dw_die_ref);
3427 static void unmark_dies (dw_die_ref);
3428 static void unmark_all_dies (dw_die_ref);
3429 static unsigned long size_of_pubnames (vec<pubname_entry, va_gc> *);
3430 static unsigned long size_of_aranges (void);
3431 static enum dwarf_form value_format (dw_attr_node *);
3432 static void output_value_format (dw_attr_node *);
3433 static void output_abbrev_section (void);
3434 static void output_die_abbrevs (unsigned long, dw_die_ref);
3435 static void output_die_symbol (dw_die_ref);
3436 static void output_die (dw_die_ref);
3437 static void output_compilation_unit_header (enum dwarf_unit_type);
3438 static void output_comp_unit (dw_die_ref, int, const unsigned char *);
3439 static void output_comdat_type_unit (comdat_type_node *);
3440 static const char *dwarf2_name (tree, int);
3441 static void add_pubname (tree, dw_die_ref);
3442 static void add_enumerator_pubname (const char *, dw_die_ref);
3443 static void add_pubname_string (const char *, dw_die_ref);
3444 static void add_pubtype (tree, dw_die_ref);
3445 static void output_pubnames (vec<pubname_entry, va_gc> *);
3446 static void output_aranges (void);
3447 static unsigned int add_ranges (const_tree, bool = false);
3448 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3449 bool *, bool);
3450 static void output_ranges (void);
3451 static dw_line_info_table *new_line_info_table (void);
3452 static void output_line_info (bool);
3453 static void output_file_names (void);
3454 static dw_die_ref base_type_die (tree, bool);
3455 static int is_base_type (tree);
3456 static dw_die_ref subrange_type_die (tree, tree, tree, tree, dw_die_ref);
3457 static int decl_quals (const_tree);
3458 static dw_die_ref modified_type_die (tree, int, bool, dw_die_ref);
3459 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3460 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3461 static int type_is_enum (const_tree);
3462 static unsigned int dbx_reg_number (const_rtx);
3463 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3464 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3465 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3466 enum var_init_status);
3467 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3468 enum var_init_status);
3469 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
3470 enum var_init_status);
3471 static int is_based_loc (const_rtx);
3472 static bool resolve_one_addr (rtx *);
3473 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3474 enum var_init_status);
3475 static dw_loc_descr_ref loc_descriptor (rtx, machine_mode mode,
3476 enum var_init_status);
3477 struct loc_descr_context;
3478 static void add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref);
3479 static void add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list);
3480 static dw_loc_list_ref loc_list_from_tree (tree, int,
3481 struct loc_descr_context *);
3482 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int,
3483 struct loc_descr_context *);
3484 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
3485 static tree field_type (const_tree);
3486 static unsigned int simple_type_align_in_bits (const_tree);
3487 static unsigned int simple_decl_align_in_bits (const_tree);
3488 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3489 struct vlr_context;
3490 static dw_loc_descr_ref field_byte_offset (const_tree, struct vlr_context *,
3491 HOST_WIDE_INT *);
3492 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3493 dw_loc_list_ref);
3494 static void add_data_member_location_attribute (dw_die_ref, tree,
3495 struct vlr_context *);
3496 static bool add_const_value_attribute (dw_die_ref, rtx);
3497 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3498 static void insert_wide_int (const wide_int &, unsigned char *, int);
3499 static void insert_float (const_rtx, unsigned char *);
3500 static rtx rtl_for_decl_location (tree);
3501 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool);
3502 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3503 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3504 static void add_name_attribute (dw_die_ref, const char *);
3505 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3506 static void add_comp_dir_attribute (dw_die_ref);
3507 static void add_scalar_info (dw_die_ref, enum dwarf_attribute, tree, int,
3508 struct loc_descr_context *);
3509 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree,
3510 struct loc_descr_context *);
3511 static void add_subscript_info (dw_die_ref, tree, bool);
3512 static void add_byte_size_attribute (dw_die_ref, tree);
3513 static inline void add_bit_offset_attribute (dw_die_ref, tree,
3514 struct vlr_context *);
3515 static void add_bit_size_attribute (dw_die_ref, tree);
3516 static void add_prototyped_attribute (dw_die_ref, tree);
3517 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
3518 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3519 static void add_src_coords_attributes (dw_die_ref, tree);
3520 static void add_name_and_src_coords_attributes (dw_die_ref, tree, bool = false);
3521 static void add_discr_value (dw_die_ref, dw_discr_value *);
3522 static void add_discr_list (dw_die_ref, dw_discr_list_ref);
3523 static inline dw_discr_list_ref AT_discr_list (dw_attr_node *);
3524 static void push_decl_scope (tree);
3525 static void pop_decl_scope (void);
3526 static dw_die_ref scope_die_for (tree, dw_die_ref);
3527 static inline int local_scope_p (dw_die_ref);
3528 static inline int class_scope_p (dw_die_ref);
3529 static inline int class_or_namespace_scope_p (dw_die_ref);
3530 static void add_type_attribute (dw_die_ref, tree, int, bool, dw_die_ref);
3531 static void add_calling_convention_attribute (dw_die_ref, tree);
3532 static const char *type_tag (const_tree);
3533 static tree member_declared_type (const_tree);
3534 #if 0
3535 static const char *decl_start_label (tree);
3536 #endif
3537 static void gen_array_type_die (tree, dw_die_ref);
3538 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3539 #if 0
3540 static void gen_entry_point_die (tree, dw_die_ref);
3541 #endif
3542 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3543 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3544 static dw_die_ref gen_formal_parameter_pack_die (tree, tree, dw_die_ref, tree*);
3545 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3546 static void gen_formal_types_die (tree, dw_die_ref);
3547 static void gen_subprogram_die (tree, dw_die_ref);
3548 static void gen_variable_die (tree, tree, dw_die_ref);
3549 static void gen_const_die (tree, dw_die_ref);
3550 static void gen_label_die (tree, dw_die_ref);
3551 static void gen_lexical_block_die (tree, dw_die_ref);
3552 static void gen_inlined_subroutine_die (tree, dw_die_ref);
3553 static void gen_field_die (tree, struct vlr_context *, dw_die_ref);
3554 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3555 static dw_die_ref gen_compile_unit_die (const char *);
3556 static void gen_inheritance_die (tree, tree, tree, dw_die_ref);
3557 static void gen_member_die (tree, dw_die_ref);
3558 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3559 enum debug_info_usage);
3560 static void gen_subroutine_type_die (tree, dw_die_ref);
3561 static void gen_typedef_die (tree, dw_die_ref);
3562 static void gen_type_die (tree, dw_die_ref);
3563 static void gen_block_die (tree, dw_die_ref);
3564 static void decls_for_scope (tree, dw_die_ref);
3565 static bool is_naming_typedef_decl (const_tree);
3566 static inline dw_die_ref get_context_die (tree);
3567 static void gen_namespace_die (tree, dw_die_ref);
3568 static dw_die_ref gen_namelist_decl (tree, dw_die_ref, tree);
3569 static dw_die_ref gen_decl_die (tree, tree, struct vlr_context *, dw_die_ref);
3570 static dw_die_ref force_decl_die (tree);
3571 static dw_die_ref force_type_die (tree);
3572 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3573 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3574 static struct dwarf_file_data * lookup_filename (const char *);
3575 static void retry_incomplete_types (void);
3576 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3577 static void gen_generic_params_dies (tree);
3578 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
3579 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
3580 static void splice_child_die (dw_die_ref, dw_die_ref);
3581 static int file_info_cmp (const void *, const void *);
3582 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
3583 const char *, const char *);
3584 static void output_loc_list (dw_loc_list_ref);
3585 static char *gen_internal_sym (const char *);
3586 static bool want_pubnames (void);
3588 static void prune_unmark_dies (dw_die_ref);
3589 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3590 static void prune_unused_types_mark (dw_die_ref, int);
3591 static void prune_unused_types_walk (dw_die_ref);
3592 static void prune_unused_types_walk_attribs (dw_die_ref);
3593 static void prune_unused_types_prune (dw_die_ref);
3594 static void prune_unused_types (void);
3595 static int maybe_emit_file (struct dwarf_file_data *fd);
3596 static inline const char *AT_vms_delta1 (dw_attr_node *);
3597 static inline const char *AT_vms_delta2 (dw_attr_node *);
3598 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
3599 const char *, const char *);
3600 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
3601 static void gen_remaining_tmpl_value_param_die_attribute (void);
3602 static bool generic_type_p (tree);
3603 static void schedule_generic_params_dies_gen (tree t);
3604 static void gen_scheduled_generic_parms_dies (void);
3606 static const char *comp_dir_string (void);
3608 static void hash_loc_operands (dw_loc_descr_ref, inchash::hash &);
3610 /* enum for tracking thread-local variables whose address is really an offset
3611 relative to the TLS pointer, which will need link-time relocation, but will
3612 not need relocation by the DWARF consumer. */
3614 enum dtprel_bool
3616 dtprel_false = 0,
3617 dtprel_true = 1
3620 /* Return the operator to use for an address of a variable. For dtprel_true, we
3621 use DW_OP_const*. For regular variables, which need both link-time
3622 relocation and consumer-level relocation (e.g., to account for shared objects
3623 loaded at a random address), we use DW_OP_addr*. */
3625 static inline enum dwarf_location_atom
3626 dw_addr_op (enum dtprel_bool dtprel)
3628 if (dtprel == dtprel_true)
3629 return (dwarf_split_debug_info ? DW_OP_GNU_const_index
3630 : (DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u));
3631 else
3632 return dwarf_split_debug_info ? DW_OP_GNU_addr_index : DW_OP_addr;
3635 /* Return a pointer to a newly allocated address location description. If
3636 dwarf_split_debug_info is true, then record the address with the appropriate
3637 relocation. */
3638 static inline dw_loc_descr_ref
3639 new_addr_loc_descr (rtx addr, enum dtprel_bool dtprel)
3641 dw_loc_descr_ref ref = new_loc_descr (dw_addr_op (dtprel), 0, 0);
3643 ref->dw_loc_oprnd1.val_class = dw_val_class_addr;
3644 ref->dw_loc_oprnd1.v.val_addr = addr;
3645 ref->dtprel = dtprel;
3646 if (dwarf_split_debug_info)
3647 ref->dw_loc_oprnd1.val_entry
3648 = add_addr_table_entry (addr,
3649 dtprel ? ate_kind_rtx_dtprel : ate_kind_rtx);
3650 else
3651 ref->dw_loc_oprnd1.val_entry = NULL;
3653 return ref;
3656 /* Section names used to hold DWARF debugging information. */
3658 #ifndef DEBUG_INFO_SECTION
3659 #define DEBUG_INFO_SECTION ".debug_info"
3660 #endif
3661 #ifndef DEBUG_DWO_INFO_SECTION
3662 #define DEBUG_DWO_INFO_SECTION ".debug_info.dwo"
3663 #endif
3664 #ifndef DEBUG_ABBREV_SECTION
3665 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
3666 #endif
3667 #ifndef DEBUG_DWO_ABBREV_SECTION
3668 #define DEBUG_DWO_ABBREV_SECTION ".debug_abbrev.dwo"
3669 #endif
3670 #ifndef DEBUG_ARANGES_SECTION
3671 #define DEBUG_ARANGES_SECTION ".debug_aranges"
3672 #endif
3673 #ifndef DEBUG_ADDR_SECTION
3674 #define DEBUG_ADDR_SECTION ".debug_addr"
3675 #endif
3676 #ifndef DEBUG_MACINFO_SECTION
3677 #define DEBUG_MACINFO_SECTION ".debug_macinfo"
3678 #endif
3679 #ifndef DEBUG_DWO_MACINFO_SECTION
3680 #define DEBUG_DWO_MACINFO_SECTION ".debug_macinfo.dwo"
3681 #endif
3682 #ifndef DEBUG_DWO_MACRO_SECTION
3683 #define DEBUG_DWO_MACRO_SECTION ".debug_macro.dwo"
3684 #endif
3685 #ifndef DEBUG_MACRO_SECTION
3686 #define DEBUG_MACRO_SECTION ".debug_macro"
3687 #endif
3688 #ifndef DEBUG_LINE_SECTION
3689 #define DEBUG_LINE_SECTION ".debug_line"
3690 #endif
3691 #ifndef DEBUG_DWO_LINE_SECTION
3692 #define DEBUG_DWO_LINE_SECTION ".debug_line.dwo"
3693 #endif
3694 #ifndef DEBUG_LOC_SECTION
3695 #define DEBUG_LOC_SECTION ".debug_loc"
3696 #endif
3697 #ifndef DEBUG_DWO_LOC_SECTION
3698 #define DEBUG_DWO_LOC_SECTION ".debug_loc.dwo"
3699 #endif
3700 #ifndef DEBUG_LOCLISTS_SECTION
3701 #define DEBUG_LOCLISTS_SECTION ".debug_loclists"
3702 #endif
3703 #ifndef DEBUG_DWO_LOCLISTS_SECTION
3704 #define DEBUG_DWO_LOCLISTS_SECTION ".debug_loclists.dwo"
3705 #endif
3706 #ifndef DEBUG_PUBNAMES_SECTION
3707 #define DEBUG_PUBNAMES_SECTION \
3708 ((debug_generate_pub_sections == 2) \
3709 ? ".debug_gnu_pubnames" : ".debug_pubnames")
3710 #endif
3711 #ifndef DEBUG_PUBTYPES_SECTION
3712 #define DEBUG_PUBTYPES_SECTION \
3713 ((debug_generate_pub_sections == 2) \
3714 ? ".debug_gnu_pubtypes" : ".debug_pubtypes")
3715 #endif
3716 #ifndef DEBUG_STR_OFFSETS_SECTION
3717 #define DEBUG_STR_OFFSETS_SECTION ".debug_str_offsets"
3718 #endif
3719 #ifndef DEBUG_DWO_STR_OFFSETS_SECTION
3720 #define DEBUG_DWO_STR_OFFSETS_SECTION ".debug_str_offsets.dwo"
3721 #endif
3722 #ifndef DEBUG_STR_DWO_SECTION
3723 #define DEBUG_STR_DWO_SECTION ".debug_str.dwo"
3724 #endif
3725 #ifndef DEBUG_STR_SECTION
3726 #define DEBUG_STR_SECTION ".debug_str"
3727 #endif
3728 #ifndef DEBUG_RANGES_SECTION
3729 #define DEBUG_RANGES_SECTION ".debug_ranges"
3730 #endif
3731 #ifndef DEBUG_RNGLISTS_SECTION
3732 #define DEBUG_RNGLISTS_SECTION ".debug_rnglists"
3733 #endif
3734 #ifndef DEBUG_LINE_STR_SECTION
3735 #define DEBUG_LINE_STR_SECTION ".debug_line_str"
3736 #endif
3738 /* Standard ELF section names for compiled code and data. */
3739 #ifndef TEXT_SECTION_NAME
3740 #define TEXT_SECTION_NAME ".text"
3741 #endif
3743 /* Section flags for .debug_str section. */
3744 #define DEBUG_STR_SECTION_FLAGS \
3745 (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings \
3746 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
3747 : SECTION_DEBUG)
3749 /* Section flags for .debug_str.dwo section. */
3750 #define DEBUG_STR_DWO_SECTION_FLAGS (SECTION_DEBUG | SECTION_EXCLUDE)
3752 /* Labels we insert at beginning sections we can reference instead of
3753 the section names themselves. */
3755 #ifndef TEXT_SECTION_LABEL
3756 #define TEXT_SECTION_LABEL "Ltext"
3757 #endif
3758 #ifndef COLD_TEXT_SECTION_LABEL
3759 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
3760 #endif
3761 #ifndef DEBUG_LINE_SECTION_LABEL
3762 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
3763 #endif
3764 #ifndef DEBUG_SKELETON_LINE_SECTION_LABEL
3765 #define DEBUG_SKELETON_LINE_SECTION_LABEL "Lskeleton_debug_line"
3766 #endif
3767 #ifndef DEBUG_INFO_SECTION_LABEL
3768 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
3769 #endif
3770 #ifndef DEBUG_SKELETON_INFO_SECTION_LABEL
3771 #define DEBUG_SKELETON_INFO_SECTION_LABEL "Lskeleton_debug_info"
3772 #endif
3773 #ifndef DEBUG_ABBREV_SECTION_LABEL
3774 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
3775 #endif
3776 #ifndef DEBUG_SKELETON_ABBREV_SECTION_LABEL
3777 #define DEBUG_SKELETON_ABBREV_SECTION_LABEL "Lskeleton_debug_abbrev"
3778 #endif
3779 #ifndef DEBUG_ADDR_SECTION_LABEL
3780 #define DEBUG_ADDR_SECTION_LABEL "Ldebug_addr"
3781 #endif
3782 #ifndef DEBUG_LOC_SECTION_LABEL
3783 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
3784 #endif
3785 #ifndef DEBUG_RANGES_SECTION_LABEL
3786 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
3787 #endif
3788 #ifndef DEBUG_MACINFO_SECTION_LABEL
3789 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
3790 #endif
3791 #ifndef DEBUG_MACRO_SECTION_LABEL
3792 #define DEBUG_MACRO_SECTION_LABEL "Ldebug_macro"
3793 #endif
3794 #define SKELETON_COMP_DIE_ABBREV 1
3795 #define SKELETON_TYPE_DIE_ABBREV 2
3797 /* Definitions of defaults for formats and names of various special
3798 (artificial) labels which may be generated within this file (when the -g
3799 options is used and DWARF2_DEBUGGING_INFO is in effect.
3800 If necessary, these may be overridden from within the tm.h file, but
3801 typically, overriding these defaults is unnecessary. */
3803 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3804 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3805 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3806 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3807 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3808 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3809 static char debug_skeleton_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3810 static char debug_skeleton_abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3811 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3812 static char debug_addr_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3813 static char debug_skeleton_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3814 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3815 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3816 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3817 static char ranges_base_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3819 #ifndef TEXT_END_LABEL
3820 #define TEXT_END_LABEL "Letext"
3821 #endif
3822 #ifndef COLD_END_LABEL
3823 #define COLD_END_LABEL "Letext_cold"
3824 #endif
3825 #ifndef BLOCK_BEGIN_LABEL
3826 #define BLOCK_BEGIN_LABEL "LBB"
3827 #endif
3828 #ifndef BLOCK_END_LABEL
3829 #define BLOCK_END_LABEL "LBE"
3830 #endif
3831 #ifndef LINE_CODE_LABEL
3832 #define LINE_CODE_LABEL "LM"
3833 #endif
3836 /* Return the root of the DIE's built for the current compilation unit. */
3837 static dw_die_ref
3838 comp_unit_die (void)
3840 if (!single_comp_unit_die)
3841 single_comp_unit_die = gen_compile_unit_die (NULL);
3842 return single_comp_unit_die;
3845 /* We allow a language front-end to designate a function that is to be
3846 called to "demangle" any name before it is put into a DIE. */
3848 static const char *(*demangle_name_func) (const char *);
3850 void
3851 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
3853 demangle_name_func = func;
3856 /* Test if rtl node points to a pseudo register. */
3858 static inline int
3859 is_pseudo_reg (const_rtx rtl)
3861 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
3862 || (GET_CODE (rtl) == SUBREG
3863 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
3866 /* Return a reference to a type, with its const and volatile qualifiers
3867 removed. */
3869 static inline tree
3870 type_main_variant (tree type)
3872 type = TYPE_MAIN_VARIANT (type);
3874 /* ??? There really should be only one main variant among any group of
3875 variants of a given type (and all of the MAIN_VARIANT values for all
3876 members of the group should point to that one type) but sometimes the C
3877 front-end messes this up for array types, so we work around that bug
3878 here. */
3879 if (TREE_CODE (type) == ARRAY_TYPE)
3880 while (type != TYPE_MAIN_VARIANT (type))
3881 type = TYPE_MAIN_VARIANT (type);
3883 return type;
3886 /* Return nonzero if the given type node represents a tagged type. */
3888 static inline int
3889 is_tagged_type (const_tree type)
3891 enum tree_code code = TREE_CODE (type);
3893 return (code == RECORD_TYPE || code == UNION_TYPE
3894 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
3897 /* Set label to debug_info_section_label + die_offset of a DIE reference. */
3899 static void
3900 get_ref_die_offset_label (char *label, dw_die_ref ref)
3902 sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
3905 /* Return die_offset of a DIE reference to a base type. */
3907 static unsigned long int
3908 get_base_type_offset (dw_die_ref ref)
3910 if (ref->die_offset)
3911 return ref->die_offset;
3912 if (comp_unit_die ()->die_abbrev)
3914 calc_base_type_die_sizes ();
3915 gcc_assert (ref->die_offset);
3917 return ref->die_offset;
3920 /* Return die_offset of a DIE reference other than base type. */
3922 static unsigned long int
3923 get_ref_die_offset (dw_die_ref ref)
3925 gcc_assert (ref->die_offset);
3926 return ref->die_offset;
3929 /* Convert a DIE tag into its string name. */
3931 static const char *
3932 dwarf_tag_name (unsigned int tag)
3934 const char *name = get_DW_TAG_name (tag);
3936 if (name != NULL)
3937 return name;
3939 return "DW_TAG_<unknown>";
3942 /* Convert a DWARF attribute code into its string name. */
3944 static const char *
3945 dwarf_attr_name (unsigned int attr)
3947 const char *name;
3949 switch (attr)
3951 #if VMS_DEBUGGING_INFO
3952 case DW_AT_HP_prologue:
3953 return "DW_AT_HP_prologue";
3954 #else
3955 case DW_AT_MIPS_loop_unroll_factor:
3956 return "DW_AT_MIPS_loop_unroll_factor";
3957 #endif
3959 #if VMS_DEBUGGING_INFO
3960 case DW_AT_HP_epilogue:
3961 return "DW_AT_HP_epilogue";
3962 #else
3963 case DW_AT_MIPS_stride:
3964 return "DW_AT_MIPS_stride";
3965 #endif
3968 name = get_DW_AT_name (attr);
3970 if (name != NULL)
3971 return name;
3973 return "DW_AT_<unknown>";
3976 /* Convert a DWARF value form code into its string name. */
3978 static const char *
3979 dwarf_form_name (unsigned int form)
3981 const char *name = get_DW_FORM_name (form);
3983 if (name != NULL)
3984 return name;
3986 return "DW_FORM_<unknown>";
3989 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
3990 instance of an inlined instance of a decl which is local to an inline
3991 function, so we have to trace all of the way back through the origin chain
3992 to find out what sort of node actually served as the original seed for the
3993 given block. */
3995 static tree
3996 decl_ultimate_origin (const_tree decl)
3998 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
3999 return NULL_TREE;
4001 /* DECL_ABSTRACT_ORIGIN can point to itself; ignore that if
4002 we're trying to output the abstract instance of this function. */
4003 if (DECL_ABSTRACT_P (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
4004 return NULL_TREE;
4006 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
4007 most distant ancestor, this should never happen. */
4008 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
4010 return DECL_ABSTRACT_ORIGIN (decl);
4013 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
4014 of a virtual function may refer to a base class, so we check the 'this'
4015 parameter. */
4017 static tree
4018 decl_class_context (tree decl)
4020 tree context = NULL_TREE;
4022 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
4023 context = DECL_CONTEXT (decl);
4024 else
4025 context = TYPE_MAIN_VARIANT
4026 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
4028 if (context && !TYPE_P (context))
4029 context = NULL_TREE;
4031 return context;
4034 /* Add an attribute/value pair to a DIE. */
4036 static inline void
4037 add_dwarf_attr (dw_die_ref die, dw_attr_node *attr)
4039 /* Maybe this should be an assert? */
4040 if (die == NULL)
4041 return;
4043 vec_safe_reserve (die->die_attr, 1);
4044 vec_safe_push (die->die_attr, *attr);
4047 static inline enum dw_val_class
4048 AT_class (dw_attr_node *a)
4050 return a->dw_attr_val.val_class;
4053 /* Return the index for any attribute that will be referenced with a
4054 DW_FORM_GNU_addr_index or DW_FORM_GNU_str_index. String indices
4055 are stored in dw_attr_val.v.val_str for reference counting
4056 pruning. */
4058 static inline unsigned int
4059 AT_index (dw_attr_node *a)
4061 if (AT_class (a) == dw_val_class_str)
4062 return a->dw_attr_val.v.val_str->index;
4063 else if (a->dw_attr_val.val_entry != NULL)
4064 return a->dw_attr_val.val_entry->index;
4065 return NOT_INDEXED;
4068 /* Add a flag value attribute to a DIE. */
4070 static inline void
4071 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
4073 dw_attr_node attr;
4075 attr.dw_attr = attr_kind;
4076 attr.dw_attr_val.val_class = dw_val_class_flag;
4077 attr.dw_attr_val.val_entry = NULL;
4078 attr.dw_attr_val.v.val_flag = flag;
4079 add_dwarf_attr (die, &attr);
4082 static inline unsigned
4083 AT_flag (dw_attr_node *a)
4085 gcc_assert (a && AT_class (a) == dw_val_class_flag);
4086 return a->dw_attr_val.v.val_flag;
4089 /* Add a signed integer attribute value to a DIE. */
4091 static inline void
4092 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
4094 dw_attr_node attr;
4096 attr.dw_attr = attr_kind;
4097 attr.dw_attr_val.val_class = dw_val_class_const;
4098 attr.dw_attr_val.val_entry = NULL;
4099 attr.dw_attr_val.v.val_int = int_val;
4100 add_dwarf_attr (die, &attr);
4103 static inline HOST_WIDE_INT
4104 AT_int (dw_attr_node *a)
4106 gcc_assert (a && (AT_class (a) == dw_val_class_const
4107 || AT_class (a) == dw_val_class_const_implicit));
4108 return a->dw_attr_val.v.val_int;
4111 /* Add an unsigned integer attribute value to a DIE. */
4113 static inline void
4114 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
4115 unsigned HOST_WIDE_INT unsigned_val)
4117 dw_attr_node attr;
4119 attr.dw_attr = attr_kind;
4120 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
4121 attr.dw_attr_val.val_entry = NULL;
4122 attr.dw_attr_val.v.val_unsigned = unsigned_val;
4123 add_dwarf_attr (die, &attr);
4126 static inline unsigned HOST_WIDE_INT
4127 AT_unsigned (dw_attr_node *a)
4129 gcc_assert (a && (AT_class (a) == dw_val_class_unsigned_const
4130 || AT_class (a) == dw_val_class_unsigned_const_implicit));
4131 return a->dw_attr_val.v.val_unsigned;
4134 /* Add an unsigned wide integer attribute value to a DIE. */
4136 static inline void
4137 add_AT_wide (dw_die_ref die, enum dwarf_attribute attr_kind,
4138 const wide_int& w)
4140 dw_attr_node attr;
4142 attr.dw_attr = attr_kind;
4143 attr.dw_attr_val.val_class = dw_val_class_wide_int;
4144 attr.dw_attr_val.val_entry = NULL;
4145 attr.dw_attr_val.v.val_wide = ggc_alloc<wide_int> ();
4146 *attr.dw_attr_val.v.val_wide = w;
4147 add_dwarf_attr (die, &attr);
4150 /* Add an unsigned double integer attribute value to a DIE. */
4152 static inline void
4153 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
4154 HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
4156 dw_attr_node attr;
4158 attr.dw_attr = attr_kind;
4159 attr.dw_attr_val.val_class = dw_val_class_const_double;
4160 attr.dw_attr_val.val_entry = NULL;
4161 attr.dw_attr_val.v.val_double.high = high;
4162 attr.dw_attr_val.v.val_double.low = low;
4163 add_dwarf_attr (die, &attr);
4166 /* Add a floating point attribute value to a DIE and return it. */
4168 static inline void
4169 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
4170 unsigned int length, unsigned int elt_size, unsigned char *array)
4172 dw_attr_node attr;
4174 attr.dw_attr = attr_kind;
4175 attr.dw_attr_val.val_class = dw_val_class_vec;
4176 attr.dw_attr_val.val_entry = NULL;
4177 attr.dw_attr_val.v.val_vec.length = length;
4178 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
4179 attr.dw_attr_val.v.val_vec.array = array;
4180 add_dwarf_attr (die, &attr);
4183 /* Add an 8-byte data attribute value to a DIE. */
4185 static inline void
4186 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
4187 unsigned char data8[8])
4189 dw_attr_node attr;
4191 attr.dw_attr = attr_kind;
4192 attr.dw_attr_val.val_class = dw_val_class_data8;
4193 attr.dw_attr_val.val_entry = NULL;
4194 memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
4195 add_dwarf_attr (die, &attr);
4198 /* Add DW_AT_low_pc and DW_AT_high_pc to a DIE. When using
4199 dwarf_split_debug_info, address attributes in dies destined for the
4200 final executable have force_direct set to avoid using indexed
4201 references. */
4203 static inline void
4204 add_AT_low_high_pc (dw_die_ref die, const char *lbl_low, const char *lbl_high,
4205 bool force_direct)
4207 dw_attr_node attr;
4208 char * lbl_id;
4210 lbl_id = xstrdup (lbl_low);
4211 attr.dw_attr = DW_AT_low_pc;
4212 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4213 attr.dw_attr_val.v.val_lbl_id = lbl_id;
4214 if (dwarf_split_debug_info && !force_direct)
4215 attr.dw_attr_val.val_entry
4216 = add_addr_table_entry (lbl_id, ate_kind_label);
4217 else
4218 attr.dw_attr_val.val_entry = NULL;
4219 add_dwarf_attr (die, &attr);
4221 attr.dw_attr = DW_AT_high_pc;
4222 if (dwarf_version < 4)
4223 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4224 else
4225 attr.dw_attr_val.val_class = dw_val_class_high_pc;
4226 lbl_id = xstrdup (lbl_high);
4227 attr.dw_attr_val.v.val_lbl_id = lbl_id;
4228 if (attr.dw_attr_val.val_class == dw_val_class_lbl_id
4229 && dwarf_split_debug_info && !force_direct)
4230 attr.dw_attr_val.val_entry
4231 = add_addr_table_entry (lbl_id, ate_kind_label);
4232 else
4233 attr.dw_attr_val.val_entry = NULL;
4234 add_dwarf_attr (die, &attr);
4237 /* Hash and equality functions for debug_str_hash. */
4239 hashval_t
4240 indirect_string_hasher::hash (indirect_string_node *x)
4242 return htab_hash_string (x->str);
4245 bool
4246 indirect_string_hasher::equal (indirect_string_node *x1, const char *x2)
4248 return strcmp (x1->str, x2) == 0;
4251 /* Add STR to the given string hash table. */
4253 static struct indirect_string_node *
4254 find_AT_string_in_table (const char *str,
4255 hash_table<indirect_string_hasher> *table)
4257 struct indirect_string_node *node;
4259 indirect_string_node **slot
4260 = table->find_slot_with_hash (str, htab_hash_string (str), INSERT);
4261 if (*slot == NULL)
4263 node = ggc_cleared_alloc<indirect_string_node> ();
4264 node->str = ggc_strdup (str);
4265 *slot = node;
4267 else
4268 node = *slot;
4270 node->refcount++;
4271 return node;
4274 /* Add STR to the indirect string hash table. */
4276 static struct indirect_string_node *
4277 find_AT_string (const char *str)
4279 if (! debug_str_hash)
4280 debug_str_hash = hash_table<indirect_string_hasher>::create_ggc (10);
4282 return find_AT_string_in_table (str, debug_str_hash);
4285 /* Add a string attribute value to a DIE. */
4287 static inline void
4288 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
4290 dw_attr_node attr;
4291 struct indirect_string_node *node;
4293 node = find_AT_string (str);
4295 attr.dw_attr = attr_kind;
4296 attr.dw_attr_val.val_class = dw_val_class_str;
4297 attr.dw_attr_val.val_entry = NULL;
4298 attr.dw_attr_val.v.val_str = node;
4299 add_dwarf_attr (die, &attr);
4302 static inline const char *
4303 AT_string (dw_attr_node *a)
4305 gcc_assert (a && AT_class (a) == dw_val_class_str);
4306 return a->dw_attr_val.v.val_str->str;
4309 /* Call this function directly to bypass AT_string_form's logic to put
4310 the string inline in the die. */
4312 static void
4313 set_indirect_string (struct indirect_string_node *node)
4315 char label[MAX_ARTIFICIAL_LABEL_BYTES];
4316 /* Already indirect is a no op. */
4317 if (node->form == DW_FORM_strp
4318 || node->form == DW_FORM_line_strp
4319 || node->form == DW_FORM_GNU_str_index)
4321 gcc_assert (node->label);
4322 return;
4324 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
4325 ++dw2_string_counter;
4326 node->label = xstrdup (label);
4328 if (!dwarf_split_debug_info)
4330 node->form = DW_FORM_strp;
4331 node->index = NOT_INDEXED;
4333 else
4335 node->form = DW_FORM_GNU_str_index;
4336 node->index = NO_INDEX_ASSIGNED;
4340 /* Find out whether a string should be output inline in DIE
4341 or out-of-line in .debug_str section. */
4343 static enum dwarf_form
4344 find_string_form (struct indirect_string_node *node)
4346 unsigned int len;
4348 if (node->form)
4349 return node->form;
4351 len = strlen (node->str) + 1;
4353 /* If the string is shorter or equal to the size of the reference, it is
4354 always better to put it inline. */
4355 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
4356 return node->form = DW_FORM_string;
4358 /* If we cannot expect the linker to merge strings in .debug_str
4359 section, only put it into .debug_str if it is worth even in this
4360 single module. */
4361 if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
4362 || ((debug_str_section->common.flags & SECTION_MERGE) == 0
4363 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
4364 return node->form = DW_FORM_string;
4366 set_indirect_string (node);
4368 return node->form;
4371 /* Find out whether the string referenced from the attribute should be
4372 output inline in DIE or out-of-line in .debug_str section. */
4374 static enum dwarf_form
4375 AT_string_form (dw_attr_node *a)
4377 gcc_assert (a && AT_class (a) == dw_val_class_str);
4378 return find_string_form (a->dw_attr_val.v.val_str);
4381 /* Add a DIE reference attribute value to a DIE. */
4383 static inline void
4384 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
4386 dw_attr_node attr;
4387 gcc_checking_assert (targ_die != NULL);
4389 /* With LTO we can end up trying to reference something we didn't create
4390 a DIE for. Avoid crashing later on a NULL referenced DIE. */
4391 if (targ_die == NULL)
4392 return;
4394 attr.dw_attr = attr_kind;
4395 attr.dw_attr_val.val_class = dw_val_class_die_ref;
4396 attr.dw_attr_val.val_entry = NULL;
4397 attr.dw_attr_val.v.val_die_ref.die = targ_die;
4398 attr.dw_attr_val.v.val_die_ref.external = 0;
4399 add_dwarf_attr (die, &attr);
4402 /* Change DIE reference REF to point to NEW_DIE instead. */
4404 static inline void
4405 change_AT_die_ref (dw_attr_node *ref, dw_die_ref new_die)
4407 gcc_assert (ref->dw_attr_val.val_class == dw_val_class_die_ref);
4408 ref->dw_attr_val.v.val_die_ref.die = new_die;
4409 ref->dw_attr_val.v.val_die_ref.external = 0;
4412 /* Add an AT_specification attribute to a DIE, and also make the back
4413 pointer from the specification to the definition. */
4415 static inline void
4416 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4418 add_AT_die_ref (die, DW_AT_specification, targ_die);
4419 gcc_assert (!targ_die->die_definition);
4420 targ_die->die_definition = die;
4423 static inline dw_die_ref
4424 AT_ref (dw_attr_node *a)
4426 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4427 return a->dw_attr_val.v.val_die_ref.die;
4430 static inline int
4431 AT_ref_external (dw_attr_node *a)
4433 if (a && AT_class (a) == dw_val_class_die_ref)
4434 return a->dw_attr_val.v.val_die_ref.external;
4436 return 0;
4439 static inline void
4440 set_AT_ref_external (dw_attr_node *a, int i)
4442 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4443 a->dw_attr_val.v.val_die_ref.external = i;
4446 /* Add an FDE reference attribute value to a DIE. */
4448 static inline void
4449 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
4451 dw_attr_node attr;
4453 attr.dw_attr = attr_kind;
4454 attr.dw_attr_val.val_class = dw_val_class_fde_ref;
4455 attr.dw_attr_val.val_entry = NULL;
4456 attr.dw_attr_val.v.val_fde_index = targ_fde;
4457 add_dwarf_attr (die, &attr);
4460 /* Add a location description attribute value to a DIE. */
4462 static inline void
4463 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4465 dw_attr_node attr;
4467 attr.dw_attr = attr_kind;
4468 attr.dw_attr_val.val_class = dw_val_class_loc;
4469 attr.dw_attr_val.val_entry = NULL;
4470 attr.dw_attr_val.v.val_loc = loc;
4471 add_dwarf_attr (die, &attr);
4474 static inline dw_loc_descr_ref
4475 AT_loc (dw_attr_node *a)
4477 gcc_assert (a && AT_class (a) == dw_val_class_loc);
4478 return a->dw_attr_val.v.val_loc;
4481 static inline void
4482 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4484 dw_attr_node attr;
4486 if (XCOFF_DEBUGGING_INFO && !HAVE_XCOFF_DWARF_EXTRAS)
4487 return;
4489 attr.dw_attr = attr_kind;
4490 attr.dw_attr_val.val_class = dw_val_class_loc_list;
4491 attr.dw_attr_val.val_entry = NULL;
4492 attr.dw_attr_val.v.val_loc_list = loc_list;
4493 add_dwarf_attr (die, &attr);
4494 have_location_lists = true;
4497 static inline dw_loc_list_ref
4498 AT_loc_list (dw_attr_node *a)
4500 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4501 return a->dw_attr_val.v.val_loc_list;
4504 static inline dw_loc_list_ref *
4505 AT_loc_list_ptr (dw_attr_node *a)
4507 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4508 return &a->dw_attr_val.v.val_loc_list;
4511 struct addr_hasher : ggc_ptr_hash<addr_table_entry>
4513 static hashval_t hash (addr_table_entry *);
4514 static bool equal (addr_table_entry *, addr_table_entry *);
4517 /* Table of entries into the .debug_addr section. */
4519 static GTY (()) hash_table<addr_hasher> *addr_index_table;
4521 /* Hash an address_table_entry. */
4523 hashval_t
4524 addr_hasher::hash (addr_table_entry *a)
4526 inchash::hash hstate;
4527 switch (a->kind)
4529 case ate_kind_rtx:
4530 hstate.add_int (0);
4531 break;
4532 case ate_kind_rtx_dtprel:
4533 hstate.add_int (1);
4534 break;
4535 case ate_kind_label:
4536 return htab_hash_string (a->addr.label);
4537 default:
4538 gcc_unreachable ();
4540 inchash::add_rtx (a->addr.rtl, hstate);
4541 return hstate.end ();
4544 /* Determine equality for two address_table_entries. */
4546 bool
4547 addr_hasher::equal (addr_table_entry *a1, addr_table_entry *a2)
4549 if (a1->kind != a2->kind)
4550 return 0;
4551 switch (a1->kind)
4553 case ate_kind_rtx:
4554 case ate_kind_rtx_dtprel:
4555 return rtx_equal_p (a1->addr.rtl, a2->addr.rtl);
4556 case ate_kind_label:
4557 return strcmp (a1->addr.label, a2->addr.label) == 0;
4558 default:
4559 gcc_unreachable ();
4563 /* Initialize an addr_table_entry. */
4565 void
4566 init_addr_table_entry (addr_table_entry *e, enum ate_kind kind, void *addr)
4568 e->kind = kind;
4569 switch (kind)
4571 case ate_kind_rtx:
4572 case ate_kind_rtx_dtprel:
4573 e->addr.rtl = (rtx) addr;
4574 break;
4575 case ate_kind_label:
4576 e->addr.label = (char *) addr;
4577 break;
4579 e->refcount = 0;
4580 e->index = NO_INDEX_ASSIGNED;
4583 /* Add attr to the address table entry to the table. Defer setting an
4584 index until output time. */
4586 static addr_table_entry *
4587 add_addr_table_entry (void *addr, enum ate_kind kind)
4589 addr_table_entry *node;
4590 addr_table_entry finder;
4592 gcc_assert (dwarf_split_debug_info);
4593 if (! addr_index_table)
4594 addr_index_table = hash_table<addr_hasher>::create_ggc (10);
4595 init_addr_table_entry (&finder, kind, addr);
4596 addr_table_entry **slot = addr_index_table->find_slot (&finder, INSERT);
4598 if (*slot == HTAB_EMPTY_ENTRY)
4600 node = ggc_cleared_alloc<addr_table_entry> ();
4601 init_addr_table_entry (node, kind, addr);
4602 *slot = node;
4604 else
4605 node = *slot;
4607 node->refcount++;
4608 return node;
4611 /* Remove an entry from the addr table by decrementing its refcount.
4612 Strictly, decrementing the refcount would be enough, but the
4613 assertion that the entry is actually in the table has found
4614 bugs. */
4616 static void
4617 remove_addr_table_entry (addr_table_entry *entry)
4619 gcc_assert (dwarf_split_debug_info && addr_index_table);
4620 /* After an index is assigned, the table is frozen. */
4621 gcc_assert (entry->refcount > 0 && entry->index == NO_INDEX_ASSIGNED);
4622 entry->refcount--;
4625 /* Given a location list, remove all addresses it refers to from the
4626 address_table. */
4628 static void
4629 remove_loc_list_addr_table_entries (dw_loc_descr_ref descr)
4631 for (; descr; descr = descr->dw_loc_next)
4632 if (descr->dw_loc_oprnd1.val_entry != NULL)
4634 gcc_assert (descr->dw_loc_oprnd1.val_entry->index == NO_INDEX_ASSIGNED);
4635 remove_addr_table_entry (descr->dw_loc_oprnd1.val_entry);
4639 /* A helper function for dwarf2out_finish called through
4640 htab_traverse. Assign an addr_table_entry its index. All entries
4641 must be collected into the table when this function is called,
4642 because the indexing code relies on htab_traverse to traverse nodes
4643 in the same order for each run. */
4646 index_addr_table_entry (addr_table_entry **h, unsigned int *index)
4648 addr_table_entry *node = *h;
4650 /* Don't index unreferenced nodes. */
4651 if (node->refcount == 0)
4652 return 1;
4654 gcc_assert (node->index == NO_INDEX_ASSIGNED);
4655 node->index = *index;
4656 *index += 1;
4658 return 1;
4661 /* Add an address constant attribute value to a DIE. When using
4662 dwarf_split_debug_info, address attributes in dies destined for the
4663 final executable should be direct references--setting the parameter
4664 force_direct ensures this behavior. */
4666 static inline void
4667 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr,
4668 bool force_direct)
4670 dw_attr_node attr;
4672 attr.dw_attr = attr_kind;
4673 attr.dw_attr_val.val_class = dw_val_class_addr;
4674 attr.dw_attr_val.v.val_addr = addr;
4675 if (dwarf_split_debug_info && !force_direct)
4676 attr.dw_attr_val.val_entry = add_addr_table_entry (addr, ate_kind_rtx);
4677 else
4678 attr.dw_attr_val.val_entry = NULL;
4679 add_dwarf_attr (die, &attr);
4682 /* Get the RTX from to an address DIE attribute. */
4684 static inline rtx
4685 AT_addr (dw_attr_node *a)
4687 gcc_assert (a && AT_class (a) == dw_val_class_addr);
4688 return a->dw_attr_val.v.val_addr;
4691 /* Add a file attribute value to a DIE. */
4693 static inline void
4694 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
4695 struct dwarf_file_data *fd)
4697 dw_attr_node attr;
4699 attr.dw_attr = attr_kind;
4700 attr.dw_attr_val.val_class = dw_val_class_file;
4701 attr.dw_attr_val.val_entry = NULL;
4702 attr.dw_attr_val.v.val_file = fd;
4703 add_dwarf_attr (die, &attr);
4706 /* Get the dwarf_file_data from a file DIE attribute. */
4708 static inline struct dwarf_file_data *
4709 AT_file (dw_attr_node *a)
4711 gcc_assert (a && (AT_class (a) == dw_val_class_file
4712 || AT_class (a) == dw_val_class_file_implicit));
4713 return a->dw_attr_val.v.val_file;
4716 /* Add a vms delta attribute value to a DIE. */
4718 static inline void
4719 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
4720 const char *lbl1, const char *lbl2)
4722 dw_attr_node attr;
4724 attr.dw_attr = attr_kind;
4725 attr.dw_attr_val.val_class = dw_val_class_vms_delta;
4726 attr.dw_attr_val.val_entry = NULL;
4727 attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
4728 attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
4729 add_dwarf_attr (die, &attr);
4732 /* Add a label identifier attribute value to a DIE. */
4734 static inline void
4735 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind,
4736 const char *lbl_id)
4738 dw_attr_node attr;
4740 attr.dw_attr = attr_kind;
4741 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4742 attr.dw_attr_val.val_entry = NULL;
4743 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
4744 if (dwarf_split_debug_info)
4745 attr.dw_attr_val.val_entry
4746 = add_addr_table_entry (attr.dw_attr_val.v.val_lbl_id,
4747 ate_kind_label);
4748 add_dwarf_attr (die, &attr);
4751 /* Add a section offset attribute value to a DIE, an offset into the
4752 debug_line section. */
4754 static inline void
4755 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4756 const char *label)
4758 dw_attr_node attr;
4760 attr.dw_attr = attr_kind;
4761 attr.dw_attr_val.val_class = dw_val_class_lineptr;
4762 attr.dw_attr_val.val_entry = NULL;
4763 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4764 add_dwarf_attr (die, &attr);
4767 /* Add a section offset attribute value to a DIE, an offset into the
4768 debug_loclists section. */
4770 static inline void
4771 add_AT_loclistsptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4772 const char *label)
4774 dw_attr_node attr;
4776 attr.dw_attr = attr_kind;
4777 attr.dw_attr_val.val_class = dw_val_class_loclistsptr;
4778 attr.dw_attr_val.val_entry = NULL;
4779 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4780 add_dwarf_attr (die, &attr);
4783 /* Add a section offset attribute value to a DIE, an offset into the
4784 debug_macinfo section. */
4786 static inline void
4787 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4788 const char *label)
4790 dw_attr_node attr;
4792 attr.dw_attr = attr_kind;
4793 attr.dw_attr_val.val_class = dw_val_class_macptr;
4794 attr.dw_attr_val.val_entry = NULL;
4795 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4796 add_dwarf_attr (die, &attr);
4799 /* Add an offset attribute value to a DIE. */
4801 static inline void
4802 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
4803 unsigned HOST_WIDE_INT offset)
4805 dw_attr_node attr;
4807 attr.dw_attr = attr_kind;
4808 attr.dw_attr_val.val_class = dw_val_class_offset;
4809 attr.dw_attr_val.val_entry = NULL;
4810 attr.dw_attr_val.v.val_offset = offset;
4811 add_dwarf_attr (die, &attr);
4814 /* Add a range_list attribute value to a DIE. When using
4815 dwarf_split_debug_info, address attributes in dies destined for the
4816 final executable should be direct references--setting the parameter
4817 force_direct ensures this behavior. */
4819 #define UNRELOCATED_OFFSET ((addr_table_entry *) 1)
4820 #define RELOCATED_OFFSET (NULL)
4822 static void
4823 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
4824 long unsigned int offset, bool force_direct)
4826 dw_attr_node attr;
4828 attr.dw_attr = attr_kind;
4829 attr.dw_attr_val.val_class = dw_val_class_range_list;
4830 /* For the range_list attribute, use val_entry to store whether the
4831 offset should follow split-debug-info or normal semantics. This
4832 value is read in output_range_list_offset. */
4833 if (dwarf_split_debug_info && !force_direct)
4834 attr.dw_attr_val.val_entry = UNRELOCATED_OFFSET;
4835 else
4836 attr.dw_attr_val.val_entry = RELOCATED_OFFSET;
4837 attr.dw_attr_val.v.val_offset = offset;
4838 add_dwarf_attr (die, &attr);
4841 /* Return the start label of a delta attribute. */
4843 static inline const char *
4844 AT_vms_delta1 (dw_attr_node *a)
4846 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4847 return a->dw_attr_val.v.val_vms_delta.lbl1;
4850 /* Return the end label of a delta attribute. */
4852 static inline const char *
4853 AT_vms_delta2 (dw_attr_node *a)
4855 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4856 return a->dw_attr_val.v.val_vms_delta.lbl2;
4859 static inline const char *
4860 AT_lbl (dw_attr_node *a)
4862 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
4863 || AT_class (a) == dw_val_class_lineptr
4864 || AT_class (a) == dw_val_class_macptr
4865 || AT_class (a) == dw_val_class_loclistsptr
4866 || AT_class (a) == dw_val_class_high_pc));
4867 return a->dw_attr_val.v.val_lbl_id;
4870 /* Get the attribute of type attr_kind. */
4872 static dw_attr_node *
4873 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4875 dw_attr_node *a;
4876 unsigned ix;
4877 dw_die_ref spec = NULL;
4879 if (! die)
4880 return NULL;
4882 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4883 if (a->dw_attr == attr_kind)
4884 return a;
4885 else if (a->dw_attr == DW_AT_specification
4886 || a->dw_attr == DW_AT_abstract_origin)
4887 spec = AT_ref (a);
4889 if (spec)
4890 return get_AT (spec, attr_kind);
4892 return NULL;
4895 /* Returns the parent of the declaration of DIE. */
4897 static dw_die_ref
4898 get_die_parent (dw_die_ref die)
4900 dw_die_ref t;
4902 if (!die)
4903 return NULL;
4905 if ((t = get_AT_ref (die, DW_AT_abstract_origin))
4906 || (t = get_AT_ref (die, DW_AT_specification)))
4907 die = t;
4909 return die->die_parent;
4912 /* Return the "low pc" attribute value, typically associated with a subprogram
4913 DIE. Return null if the "low pc" attribute is either not present, or if it
4914 cannot be represented as an assembler label identifier. */
4916 static inline const char *
4917 get_AT_low_pc (dw_die_ref die)
4919 dw_attr_node *a = get_AT (die, DW_AT_low_pc);
4921 return a ? AT_lbl (a) : NULL;
4924 /* Return the "high pc" attribute value, typically associated with a subprogram
4925 DIE. Return null if the "high pc" attribute is either not present, or if it
4926 cannot be represented as an assembler label identifier. */
4928 static inline const char *
4929 get_AT_hi_pc (dw_die_ref die)
4931 dw_attr_node *a = get_AT (die, DW_AT_high_pc);
4933 return a ? AT_lbl (a) : NULL;
4936 /* Return the value of the string attribute designated by ATTR_KIND, or
4937 NULL if it is not present. */
4939 static inline const char *
4940 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
4942 dw_attr_node *a = get_AT (die, attr_kind);
4944 return a ? AT_string (a) : NULL;
4947 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
4948 if it is not present. */
4950 static inline int
4951 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
4953 dw_attr_node *a = get_AT (die, attr_kind);
4955 return a ? AT_flag (a) : 0;
4958 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
4959 if it is not present. */
4961 static inline unsigned
4962 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
4964 dw_attr_node *a = get_AT (die, attr_kind);
4966 return a ? AT_unsigned (a) : 0;
4969 static inline dw_die_ref
4970 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
4972 dw_attr_node *a = get_AT (die, attr_kind);
4974 return a ? AT_ref (a) : NULL;
4977 static inline struct dwarf_file_data *
4978 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
4980 dw_attr_node *a = get_AT (die, attr_kind);
4982 return a ? AT_file (a) : NULL;
4985 /* Return TRUE if the language is C++. */
4987 static inline bool
4988 is_cxx (void)
4990 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4992 return (lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus
4993 || lang == DW_LANG_C_plus_plus_11 || lang == DW_LANG_C_plus_plus_14);
4996 /* Return TRUE if DECL was created by the C++ frontend. */
4998 static bool
4999 is_cxx (const_tree decl)
5001 if (in_lto_p)
5003 const_tree context = decl;
5004 while (context && TREE_CODE (context) != TRANSLATION_UNIT_DECL)
5006 if (TREE_CODE (context) == BLOCK)
5007 context = BLOCK_SUPERCONTEXT (context);
5008 else
5009 context = get_containing_scope (context);
5011 if (context && TRANSLATION_UNIT_LANGUAGE (context))
5012 return strncmp (TRANSLATION_UNIT_LANGUAGE (context), "GNU C++", 7) == 0;
5014 return is_cxx ();
5017 /* Return TRUE if the language is Java. */
5019 static inline bool
5020 is_java (void)
5022 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5024 return lang == DW_LANG_Java;
5027 /* Return TRUE if the language is Fortran. */
5029 static inline bool
5030 is_fortran (void)
5032 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5034 return (lang == DW_LANG_Fortran77
5035 || lang == DW_LANG_Fortran90
5036 || lang == DW_LANG_Fortran95
5037 || lang == DW_LANG_Fortran03
5038 || lang == DW_LANG_Fortran08);
5041 /* Return TRUE if the language is Ada. */
5043 static inline bool
5044 is_ada (void)
5046 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5048 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
5051 /* Remove the specified attribute if present. Return TRUE if removal
5052 was successful. */
5054 static bool
5055 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5057 dw_attr_node *a;
5058 unsigned ix;
5060 if (! die)
5061 return false;
5063 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5064 if (a->dw_attr == attr_kind)
5066 if (AT_class (a) == dw_val_class_str)
5067 if (a->dw_attr_val.v.val_str->refcount)
5068 a->dw_attr_val.v.val_str->refcount--;
5070 /* vec::ordered_remove should help reduce the number of abbrevs
5071 that are needed. */
5072 die->die_attr->ordered_remove (ix);
5073 return true;
5075 return false;
5078 /* Remove CHILD from its parent. PREV must have the property that
5079 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
5081 static void
5082 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
5084 gcc_assert (child->die_parent == prev->die_parent);
5085 gcc_assert (prev->die_sib == child);
5086 if (prev == child)
5088 gcc_assert (child->die_parent->die_child == child);
5089 prev = NULL;
5091 else
5092 prev->die_sib = child->die_sib;
5093 if (child->die_parent->die_child == child)
5094 child->die_parent->die_child = prev;
5095 child->die_sib = NULL;
5098 /* Replace OLD_CHILD with NEW_CHILD. PREV must have the property that
5099 PREV->DIE_SIB == OLD_CHILD. Does not alter OLD_CHILD. */
5101 static void
5102 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
5104 dw_die_ref parent = old_child->die_parent;
5106 gcc_assert (parent == prev->die_parent);
5107 gcc_assert (prev->die_sib == old_child);
5109 new_child->die_parent = parent;
5110 if (prev == old_child)
5112 gcc_assert (parent->die_child == old_child);
5113 new_child->die_sib = new_child;
5115 else
5117 prev->die_sib = new_child;
5118 new_child->die_sib = old_child->die_sib;
5120 if (old_child->die_parent->die_child == old_child)
5121 old_child->die_parent->die_child = new_child;
5122 old_child->die_sib = NULL;
5125 /* Move all children from OLD_PARENT to NEW_PARENT. */
5127 static void
5128 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
5130 dw_die_ref c;
5131 new_parent->die_child = old_parent->die_child;
5132 old_parent->die_child = NULL;
5133 FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
5136 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
5137 matches TAG. */
5139 static void
5140 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
5142 dw_die_ref c;
5144 c = die->die_child;
5145 if (c) do {
5146 dw_die_ref prev = c;
5147 c = c->die_sib;
5148 while (c->die_tag == tag)
5150 remove_child_with_prev (c, prev);
5151 c->die_parent = NULL;
5152 /* Might have removed every child. */
5153 if (die->die_child == NULL)
5154 return;
5155 c = prev->die_sib;
5157 } while (c != die->die_child);
5160 /* Add a CHILD_DIE as the last child of DIE. */
5162 static void
5163 add_child_die (dw_die_ref die, dw_die_ref child_die)
5165 /* FIXME this should probably be an assert. */
5166 if (! die || ! child_die)
5167 return;
5168 gcc_assert (die != child_die);
5170 child_die->die_parent = die;
5171 if (die->die_child)
5173 child_die->die_sib = die->die_child->die_sib;
5174 die->die_child->die_sib = child_die;
5176 else
5177 child_die->die_sib = child_die;
5178 die->die_child = child_die;
5181 /* Like add_child_die, but put CHILD_DIE after AFTER_DIE. */
5183 static void
5184 add_child_die_after (dw_die_ref die, dw_die_ref child_die,
5185 dw_die_ref after_die)
5187 gcc_assert (die
5188 && child_die
5189 && after_die
5190 && die->die_child
5191 && die != child_die);
5193 child_die->die_parent = die;
5194 child_die->die_sib = after_die->die_sib;
5195 after_die->die_sib = child_die;
5196 if (die->die_child == after_die)
5197 die->die_child = child_die;
5200 /* Unassociate CHILD from its parent, and make its parent be
5201 NEW_PARENT. */
5203 static void
5204 reparent_child (dw_die_ref child, dw_die_ref new_parent)
5206 for (dw_die_ref p = child->die_parent->die_child; ; p = p->die_sib)
5207 if (p->die_sib == child)
5209 remove_child_with_prev (child, p);
5210 break;
5212 add_child_die (new_parent, child);
5215 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
5216 is the specification, to the end of PARENT's list of children.
5217 This is done by removing and re-adding it. */
5219 static void
5220 splice_child_die (dw_die_ref parent, dw_die_ref child)
5222 /* We want the declaration DIE from inside the class, not the
5223 specification DIE at toplevel. */
5224 if (child->die_parent != parent)
5226 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
5228 if (tmp)
5229 child = tmp;
5232 gcc_assert (child->die_parent == parent
5233 || (child->die_parent
5234 == get_AT_ref (parent, DW_AT_specification)));
5236 reparent_child (child, parent);
5239 /* Create and return a new die with a parent of PARENT_DIE. If
5240 PARENT_DIE is NULL, the new DIE is placed in limbo and an
5241 associated tree T must be supplied to determine parenthood
5242 later. */
5244 static inline dw_die_ref
5245 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
5247 dw_die_ref die = ggc_cleared_alloc<die_node> ();
5249 die->die_tag = tag_value;
5251 if (parent_die != NULL)
5252 add_child_die (parent_die, die);
5253 else
5255 limbo_die_node *limbo_node;
5257 /* No DIEs created after early dwarf should end up in limbo,
5258 because the limbo list should not persist past LTO
5259 streaming. */
5260 if (tag_value != DW_TAG_compile_unit
5261 /* These are allowed because they're generated while
5262 breaking out COMDAT units late. */
5263 && tag_value != DW_TAG_type_unit
5264 && tag_value != DW_TAG_skeleton_unit
5265 && !early_dwarf
5266 /* Allow nested functions to live in limbo because they will
5267 only temporarily live there, as decls_for_scope will fix
5268 them up. */
5269 && (TREE_CODE (t) != FUNCTION_DECL
5270 || !decl_function_context (t))
5271 /* Same as nested functions above but for types. Types that
5272 are local to a function will be fixed in
5273 decls_for_scope. */
5274 && (!RECORD_OR_UNION_TYPE_P (t)
5275 || !TYPE_CONTEXT (t)
5276 || TREE_CODE (TYPE_CONTEXT (t)) != FUNCTION_DECL)
5277 /* FIXME debug-early: Allow late limbo DIE creation for LTO,
5278 especially in the ltrans stage, but once we implement LTO
5279 dwarf streaming, we should remove this exception. */
5280 && !in_lto_p)
5282 fprintf (stderr, "symbol ended up in limbo too late:");
5283 debug_generic_stmt (t);
5284 gcc_unreachable ();
5287 limbo_node = ggc_cleared_alloc<limbo_die_node> ();
5288 limbo_node->die = die;
5289 limbo_node->created_for = t;
5290 limbo_node->next = limbo_die_list;
5291 limbo_die_list = limbo_node;
5294 return die;
5297 /* Return the DIE associated with the given type specifier. */
5299 static inline dw_die_ref
5300 lookup_type_die (tree type)
5302 dw_die_ref die = TYPE_SYMTAB_DIE (type);
5303 if (die && die->removed)
5305 TYPE_SYMTAB_DIE (type) = NULL;
5306 return NULL;
5308 return die;
5311 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
5312 anonymous type named by the typedef TYPE_DIE, return the DIE of the
5313 anonymous type instead the one of the naming typedef. */
5315 static inline dw_die_ref
5316 strip_naming_typedef (tree type, dw_die_ref type_die)
5318 if (type
5319 && TREE_CODE (type) == RECORD_TYPE
5320 && type_die
5321 && type_die->die_tag == DW_TAG_typedef
5322 && is_naming_typedef_decl (TYPE_NAME (type)))
5323 type_die = get_AT_ref (type_die, DW_AT_type);
5324 return type_die;
5327 /* Like lookup_type_die, but if type is an anonymous type named by a
5328 typedef[1], return the DIE of the anonymous type instead the one of
5329 the naming typedef. This is because in gen_typedef_die, we did
5330 equate the anonymous struct named by the typedef with the DIE of
5331 the naming typedef. So by default, lookup_type_die on an anonymous
5332 struct yields the DIE of the naming typedef.
5334 [1]: Read the comment of is_naming_typedef_decl to learn about what
5335 a naming typedef is. */
5337 static inline dw_die_ref
5338 lookup_type_die_strip_naming_typedef (tree type)
5340 dw_die_ref die = lookup_type_die (type);
5341 return strip_naming_typedef (type, die);
5344 /* Equate a DIE to a given type specifier. */
5346 static inline void
5347 equate_type_number_to_die (tree type, dw_die_ref type_die)
5349 TYPE_SYMTAB_DIE (type) = type_die;
5352 /* Returns a hash value for X (which really is a die_struct). */
5354 inline hashval_t
5355 decl_die_hasher::hash (die_node *x)
5357 return (hashval_t) x->decl_id;
5360 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
5362 inline bool
5363 decl_die_hasher::equal (die_node *x, tree y)
5365 return (x->decl_id == DECL_UID (y));
5368 /* Return the DIE associated with a given declaration. */
5370 static inline dw_die_ref
5371 lookup_decl_die (tree decl)
5373 dw_die_ref *die = decl_die_table->find_slot_with_hash (decl, DECL_UID (decl),
5374 NO_INSERT);
5375 if (!die)
5376 return NULL;
5377 if ((*die)->removed)
5379 decl_die_table->clear_slot (die);
5380 return NULL;
5382 return *die;
5385 /* Returns a hash value for X (which really is a var_loc_list). */
5387 inline hashval_t
5388 decl_loc_hasher::hash (var_loc_list *x)
5390 return (hashval_t) x->decl_id;
5393 /* Return nonzero if decl_id of var_loc_list X is the same as
5394 UID of decl *Y. */
5396 inline bool
5397 decl_loc_hasher::equal (var_loc_list *x, const_tree y)
5399 return (x->decl_id == DECL_UID (y));
5402 /* Return the var_loc list associated with a given declaration. */
5404 static inline var_loc_list *
5405 lookup_decl_loc (const_tree decl)
5407 if (!decl_loc_table)
5408 return NULL;
5409 return decl_loc_table->find_with_hash (decl, DECL_UID (decl));
5412 /* Returns a hash value for X (which really is a cached_dw_loc_list_list). */
5414 inline hashval_t
5415 dw_loc_list_hasher::hash (cached_dw_loc_list *x)
5417 return (hashval_t) x->decl_id;
5420 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
5421 UID of decl *Y. */
5423 inline bool
5424 dw_loc_list_hasher::equal (cached_dw_loc_list *x, const_tree y)
5426 return (x->decl_id == DECL_UID (y));
5429 /* Equate a DIE to a particular declaration. */
5431 static void
5432 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
5434 unsigned int decl_id = DECL_UID (decl);
5436 *decl_die_table->find_slot_with_hash (decl, decl_id, INSERT) = decl_die;
5437 decl_die->decl_id = decl_id;
5440 /* Return how many bits covers PIECE EXPR_LIST. */
5442 static HOST_WIDE_INT
5443 decl_piece_bitsize (rtx piece)
5445 int ret = (int) GET_MODE (piece);
5446 if (ret)
5447 return ret;
5448 gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
5449 && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
5450 return INTVAL (XEXP (XEXP (piece, 0), 0));
5453 /* Return pointer to the location of location note in PIECE EXPR_LIST. */
5455 static rtx *
5456 decl_piece_varloc_ptr (rtx piece)
5458 if ((int) GET_MODE (piece))
5459 return &XEXP (piece, 0);
5460 else
5461 return &XEXP (XEXP (piece, 0), 1);
5464 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
5465 Next is the chain of following piece nodes. */
5467 static rtx_expr_list *
5468 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
5470 if (bitsize > 0 && bitsize <= (int) MAX_MACHINE_MODE)
5471 return alloc_EXPR_LIST (bitsize, loc_note, next);
5472 else
5473 return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
5474 GEN_INT (bitsize),
5475 loc_note), next);
5478 /* Return rtx that should be stored into loc field for
5479 LOC_NOTE and BITPOS/BITSIZE. */
5481 static rtx
5482 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
5483 HOST_WIDE_INT bitsize)
5485 if (bitsize != -1)
5487 loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
5488 if (bitpos != 0)
5489 loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
5491 return loc_note;
5494 /* This function either modifies location piece list *DEST in
5495 place (if SRC and INNER is NULL), or copies location piece list
5496 *SRC to *DEST while modifying it. Location BITPOS is modified
5497 to contain LOC_NOTE, any pieces overlapping it are removed resp.
5498 not copied and if needed some padding around it is added.
5499 When modifying in place, DEST should point to EXPR_LIST where
5500 earlier pieces cover PIECE_BITPOS bits, when copying SRC points
5501 to the start of the whole list and INNER points to the EXPR_LIST
5502 where earlier pieces cover PIECE_BITPOS bits. */
5504 static void
5505 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
5506 HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
5507 HOST_WIDE_INT bitsize, rtx loc_note)
5509 HOST_WIDE_INT diff;
5510 bool copy = inner != NULL;
5512 if (copy)
5514 /* First copy all nodes preceding the current bitpos. */
5515 while (src != inner)
5517 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5518 decl_piece_bitsize (*src), NULL_RTX);
5519 dest = &XEXP (*dest, 1);
5520 src = &XEXP (*src, 1);
5523 /* Add padding if needed. */
5524 if (bitpos != piece_bitpos)
5526 *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
5527 copy ? NULL_RTX : *dest);
5528 dest = &XEXP (*dest, 1);
5530 else if (*dest && decl_piece_bitsize (*dest) == bitsize)
5532 gcc_assert (!copy);
5533 /* A piece with correct bitpos and bitsize already exist,
5534 just update the location for it and return. */
5535 *decl_piece_varloc_ptr (*dest) = loc_note;
5536 return;
5538 /* Add the piece that changed. */
5539 *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
5540 dest = &XEXP (*dest, 1);
5541 /* Skip over pieces that overlap it. */
5542 diff = bitpos - piece_bitpos + bitsize;
5543 if (!copy)
5544 src = dest;
5545 while (diff > 0 && *src)
5547 rtx piece = *src;
5548 diff -= decl_piece_bitsize (piece);
5549 if (copy)
5550 src = &XEXP (piece, 1);
5551 else
5553 *src = XEXP (piece, 1);
5554 free_EXPR_LIST_node (piece);
5557 /* Add padding if needed. */
5558 if (diff < 0 && *src)
5560 if (!copy)
5561 dest = src;
5562 *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
5563 dest = &XEXP (*dest, 1);
5565 if (!copy)
5566 return;
5567 /* Finally copy all nodes following it. */
5568 while (*src)
5570 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5571 decl_piece_bitsize (*src), NULL_RTX);
5572 dest = &XEXP (*dest, 1);
5573 src = &XEXP (*src, 1);
5577 /* Add a variable location node to the linked list for DECL. */
5579 static struct var_loc_node *
5580 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
5582 unsigned int decl_id;
5583 var_loc_list *temp;
5584 struct var_loc_node *loc = NULL;
5585 HOST_WIDE_INT bitsize = -1, bitpos = -1;
5587 if (VAR_P (decl) && DECL_HAS_DEBUG_EXPR_P (decl))
5589 tree realdecl = DECL_DEBUG_EXPR (decl);
5590 if (handled_component_p (realdecl)
5591 || (TREE_CODE (realdecl) == MEM_REF
5592 && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR))
5594 HOST_WIDE_INT maxsize;
5595 bool reverse;
5596 tree innerdecl
5597 = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize,
5598 &reverse);
5599 if (!DECL_P (innerdecl)
5600 || DECL_IGNORED_P (innerdecl)
5601 || TREE_STATIC (innerdecl)
5602 || bitsize <= 0
5603 || bitpos + bitsize > 256
5604 || bitsize != maxsize)
5605 return NULL;
5606 decl = innerdecl;
5610 decl_id = DECL_UID (decl);
5611 var_loc_list **slot
5612 = decl_loc_table->find_slot_with_hash (decl, decl_id, INSERT);
5613 if (*slot == NULL)
5615 temp = ggc_cleared_alloc<var_loc_list> ();
5616 temp->decl_id = decl_id;
5617 *slot = temp;
5619 else
5620 temp = *slot;
5622 /* For PARM_DECLs try to keep around the original incoming value,
5623 even if that means we'll emit a zero-range .debug_loc entry. */
5624 if (temp->last
5625 && temp->first == temp->last
5626 && TREE_CODE (decl) == PARM_DECL
5627 && NOTE_P (temp->first->loc)
5628 && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
5629 && DECL_INCOMING_RTL (decl)
5630 && NOTE_VAR_LOCATION_LOC (temp->first->loc)
5631 && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
5632 == GET_CODE (DECL_INCOMING_RTL (decl))
5633 && prev_real_insn (as_a<rtx_insn *> (temp->first->loc)) == NULL_RTX
5634 && (bitsize != -1
5635 || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
5636 NOTE_VAR_LOCATION_LOC (loc_note))
5637 || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
5638 != NOTE_VAR_LOCATION_STATUS (loc_note))))
5640 loc = ggc_cleared_alloc<var_loc_node> ();
5641 temp->first->next = loc;
5642 temp->last = loc;
5643 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5645 else if (temp->last)
5647 struct var_loc_node *last = temp->last, *unused = NULL;
5648 rtx *piece_loc = NULL, last_loc_note;
5649 HOST_WIDE_INT piece_bitpos = 0;
5650 if (last->next)
5652 last = last->next;
5653 gcc_assert (last->next == NULL);
5655 if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
5657 piece_loc = &last->loc;
5660 HOST_WIDE_INT cur_bitsize = decl_piece_bitsize (*piece_loc);
5661 if (piece_bitpos + cur_bitsize > bitpos)
5662 break;
5663 piece_bitpos += cur_bitsize;
5664 piece_loc = &XEXP (*piece_loc, 1);
5666 while (*piece_loc);
5668 /* TEMP->LAST here is either pointer to the last but one or
5669 last element in the chained list, LAST is pointer to the
5670 last element. */
5671 if (label && strcmp (last->label, label) == 0)
5673 /* For SRA optimized variables if there weren't any real
5674 insns since last note, just modify the last node. */
5675 if (piece_loc != NULL)
5677 adjust_piece_list (piece_loc, NULL, NULL,
5678 bitpos, piece_bitpos, bitsize, loc_note);
5679 return NULL;
5681 /* If the last note doesn't cover any instructions, remove it. */
5682 if (temp->last != last)
5684 temp->last->next = NULL;
5685 unused = last;
5686 last = temp->last;
5687 gcc_assert (strcmp (last->label, label) != 0);
5689 else
5691 gcc_assert (temp->first == temp->last
5692 || (temp->first->next == temp->last
5693 && TREE_CODE (decl) == PARM_DECL));
5694 memset (temp->last, '\0', sizeof (*temp->last));
5695 temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
5696 return temp->last;
5699 if (bitsize == -1 && NOTE_P (last->loc))
5700 last_loc_note = last->loc;
5701 else if (piece_loc != NULL
5702 && *piece_loc != NULL_RTX
5703 && piece_bitpos == bitpos
5704 && decl_piece_bitsize (*piece_loc) == bitsize)
5705 last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
5706 else
5707 last_loc_note = NULL_RTX;
5708 /* If the current location is the same as the end of the list,
5709 and either both or neither of the locations is uninitialized,
5710 we have nothing to do. */
5711 if (last_loc_note == NULL_RTX
5712 || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
5713 NOTE_VAR_LOCATION_LOC (loc_note)))
5714 || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5715 != NOTE_VAR_LOCATION_STATUS (loc_note))
5716 && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5717 == VAR_INIT_STATUS_UNINITIALIZED)
5718 || (NOTE_VAR_LOCATION_STATUS (loc_note)
5719 == VAR_INIT_STATUS_UNINITIALIZED))))
5721 /* Add LOC to the end of list and update LAST. If the last
5722 element of the list has been removed above, reuse its
5723 memory for the new node, otherwise allocate a new one. */
5724 if (unused)
5726 loc = unused;
5727 memset (loc, '\0', sizeof (*loc));
5729 else
5730 loc = ggc_cleared_alloc<var_loc_node> ();
5731 if (bitsize == -1 || piece_loc == NULL)
5732 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5733 else
5734 adjust_piece_list (&loc->loc, &last->loc, piece_loc,
5735 bitpos, piece_bitpos, bitsize, loc_note);
5736 last->next = loc;
5737 /* Ensure TEMP->LAST will point either to the new last but one
5738 element of the chain, or to the last element in it. */
5739 if (last != temp->last)
5740 temp->last = last;
5742 else if (unused)
5743 ggc_free (unused);
5745 else
5747 loc = ggc_cleared_alloc<var_loc_node> ();
5748 temp->first = loc;
5749 temp->last = loc;
5750 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5752 return loc;
5755 /* Keep track of the number of spaces used to indent the
5756 output of the debugging routines that print the structure of
5757 the DIE internal representation. */
5758 static int print_indent;
5760 /* Indent the line the number of spaces given by print_indent. */
5762 static inline void
5763 print_spaces (FILE *outfile)
5765 fprintf (outfile, "%*s", print_indent, "");
5768 /* Print a type signature in hex. */
5770 static inline void
5771 print_signature (FILE *outfile, char *sig)
5773 int i;
5775 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
5776 fprintf (outfile, "%02x", sig[i] & 0xff);
5779 static inline void
5780 print_discr_value (FILE *outfile, dw_discr_value *discr_value)
5782 if (discr_value->pos)
5783 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, discr_value->v.sval);
5784 else
5785 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, discr_value->v.uval);
5788 static void print_loc_descr (dw_loc_descr_ref, FILE *);
5790 /* Print the value associated to the VAL DWARF value node to OUTFILE. If
5791 RECURSE, output location descriptor operations. */
5793 static void
5794 print_dw_val (dw_val_node *val, bool recurse, FILE *outfile)
5796 switch (val->val_class)
5798 case dw_val_class_addr:
5799 fprintf (outfile, "address");
5800 break;
5801 case dw_val_class_offset:
5802 fprintf (outfile, "offset");
5803 break;
5804 case dw_val_class_loc:
5805 fprintf (outfile, "location descriptor");
5806 if (val->v.val_loc == NULL)
5807 fprintf (outfile, " -> <null>\n");
5808 else if (recurse)
5810 fprintf (outfile, ":\n");
5811 print_indent += 4;
5812 print_loc_descr (val->v.val_loc, outfile);
5813 print_indent -= 4;
5815 else
5816 fprintf (outfile, " (%p)\n", (void *) val->v.val_loc);
5817 break;
5818 case dw_val_class_loc_list:
5819 fprintf (outfile, "location list -> label:%s",
5820 val->v.val_loc_list->ll_symbol);
5821 break;
5822 case dw_val_class_range_list:
5823 fprintf (outfile, "range list");
5824 break;
5825 case dw_val_class_const:
5826 case dw_val_class_const_implicit:
5827 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, val->v.val_int);
5828 break;
5829 case dw_val_class_unsigned_const:
5830 case dw_val_class_unsigned_const_implicit:
5831 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, val->v.val_unsigned);
5832 break;
5833 case dw_val_class_const_double:
5834 fprintf (outfile, "constant (" HOST_WIDE_INT_PRINT_DEC","\
5835 HOST_WIDE_INT_PRINT_UNSIGNED")",
5836 val->v.val_double.high,
5837 val->v.val_double.low);
5838 break;
5839 case dw_val_class_wide_int:
5841 int i = val->v.val_wide->get_len ();
5842 fprintf (outfile, "constant (");
5843 gcc_assert (i > 0);
5844 if (val->v.val_wide->elt (i - 1) == 0)
5845 fprintf (outfile, "0x");
5846 fprintf (outfile, HOST_WIDE_INT_PRINT_HEX,
5847 val->v.val_wide->elt (--i));
5848 while (--i >= 0)
5849 fprintf (outfile, HOST_WIDE_INT_PRINT_PADDED_HEX,
5850 val->v.val_wide->elt (i));
5851 fprintf (outfile, ")");
5852 break;
5854 case dw_val_class_vec:
5855 fprintf (outfile, "floating-point or vector constant");
5856 break;
5857 case dw_val_class_flag:
5858 fprintf (outfile, "%u", val->v.val_flag);
5859 break;
5860 case dw_val_class_die_ref:
5861 if (val->v.val_die_ref.die != NULL)
5863 dw_die_ref die = val->v.val_die_ref.die;
5865 if (die->comdat_type_p)
5867 fprintf (outfile, "die -> signature: ");
5868 print_signature (outfile,
5869 die->die_id.die_type_node->signature);
5871 else if (die->die_id.die_symbol)
5872 fprintf (outfile, "die -> label: %s", die->die_id.die_symbol);
5873 else
5874 fprintf (outfile, "die -> %ld", die->die_offset);
5875 fprintf (outfile, " (%p)", (void *) die);
5877 else
5878 fprintf (outfile, "die -> <null>");
5879 break;
5880 case dw_val_class_vms_delta:
5881 fprintf (outfile, "delta: @slotcount(%s-%s)",
5882 val->v.val_vms_delta.lbl2, val->v.val_vms_delta.lbl1);
5883 break;
5884 case dw_val_class_lbl_id:
5885 case dw_val_class_lineptr:
5886 case dw_val_class_macptr:
5887 case dw_val_class_loclistsptr:
5888 case dw_val_class_high_pc:
5889 fprintf (outfile, "label: %s", val->v.val_lbl_id);
5890 break;
5891 case dw_val_class_str:
5892 if (val->v.val_str->str != NULL)
5893 fprintf (outfile, "\"%s\"", val->v.val_str->str);
5894 else
5895 fprintf (outfile, "<null>");
5896 break;
5897 case dw_val_class_file:
5898 case dw_val_class_file_implicit:
5899 fprintf (outfile, "\"%s\" (%d)", val->v.val_file->filename,
5900 val->v.val_file->emitted_number);
5901 break;
5902 case dw_val_class_data8:
5904 int i;
5906 for (i = 0; i < 8; i++)
5907 fprintf (outfile, "%02x", val->v.val_data8[i]);
5908 break;
5910 case dw_val_class_discr_value:
5911 print_discr_value (outfile, &val->v.val_discr_value);
5912 break;
5913 case dw_val_class_discr_list:
5914 for (dw_discr_list_ref node = val->v.val_discr_list;
5915 node != NULL;
5916 node = node->dw_discr_next)
5918 if (node->dw_discr_range)
5920 fprintf (outfile, " .. ");
5921 print_discr_value (outfile, &node->dw_discr_lower_bound);
5922 print_discr_value (outfile, &node->dw_discr_upper_bound);
5924 else
5925 print_discr_value (outfile, &node->dw_discr_lower_bound);
5927 if (node->dw_discr_next != NULL)
5928 fprintf (outfile, " | ");
5930 default:
5931 break;
5935 /* Likewise, for a DIE attribute. */
5937 static void
5938 print_attribute (dw_attr_node *a, bool recurse, FILE *outfile)
5940 print_dw_val (&a->dw_attr_val, recurse, outfile);
5944 /* Print the list of operands in the LOC location description to OUTFILE. This
5945 routine is a debugging aid only. */
5947 static void
5948 print_loc_descr (dw_loc_descr_ref loc, FILE *outfile)
5950 dw_loc_descr_ref l = loc;
5952 if (loc == NULL)
5954 print_spaces (outfile);
5955 fprintf (outfile, "<null>\n");
5956 return;
5959 for (l = loc; l != NULL; l = l->dw_loc_next)
5961 print_spaces (outfile);
5962 fprintf (outfile, "(%p) %s",
5963 (void *) l,
5964 dwarf_stack_op_name (l->dw_loc_opc));
5965 if (l->dw_loc_oprnd1.val_class != dw_val_class_none)
5967 fprintf (outfile, " ");
5968 print_dw_val (&l->dw_loc_oprnd1, false, outfile);
5970 if (l->dw_loc_oprnd2.val_class != dw_val_class_none)
5972 fprintf (outfile, ", ");
5973 print_dw_val (&l->dw_loc_oprnd2, false, outfile);
5975 fprintf (outfile, "\n");
5979 /* Print the information associated with a given DIE, and its children.
5980 This routine is a debugging aid only. */
5982 static void
5983 print_die (dw_die_ref die, FILE *outfile)
5985 dw_attr_node *a;
5986 dw_die_ref c;
5987 unsigned ix;
5989 print_spaces (outfile);
5990 fprintf (outfile, "DIE %4ld: %s (%p)\n",
5991 die->die_offset, dwarf_tag_name (die->die_tag),
5992 (void*) die);
5993 print_spaces (outfile);
5994 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
5995 fprintf (outfile, " offset: %ld", die->die_offset);
5996 fprintf (outfile, " mark: %d\n", die->die_mark);
5998 if (die->comdat_type_p)
6000 print_spaces (outfile);
6001 fprintf (outfile, " signature: ");
6002 print_signature (outfile, die->die_id.die_type_node->signature);
6003 fprintf (outfile, "\n");
6006 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6008 print_spaces (outfile);
6009 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
6011 print_attribute (a, true, outfile);
6012 fprintf (outfile, "\n");
6015 if (die->die_child != NULL)
6017 print_indent += 4;
6018 FOR_EACH_CHILD (die, c, print_die (c, outfile));
6019 print_indent -= 4;
6021 if (print_indent == 0)
6022 fprintf (outfile, "\n");
6025 /* Print the list of operations in the LOC location description. */
6027 DEBUG_FUNCTION void
6028 debug_dwarf_loc_descr (dw_loc_descr_ref loc)
6030 print_loc_descr (loc, stderr);
6033 /* Print the information collected for a given DIE. */
6035 DEBUG_FUNCTION void
6036 debug_dwarf_die (dw_die_ref die)
6038 print_die (die, stderr);
6041 DEBUG_FUNCTION void
6042 debug (die_struct &ref)
6044 print_die (&ref, stderr);
6047 DEBUG_FUNCTION void
6048 debug (die_struct *ptr)
6050 if (ptr)
6051 debug (*ptr);
6052 else
6053 fprintf (stderr, "<nil>\n");
6057 /* Print all DWARF information collected for the compilation unit.
6058 This routine is a debugging aid only. */
6060 DEBUG_FUNCTION void
6061 debug_dwarf (void)
6063 print_indent = 0;
6064 print_die (comp_unit_die (), stderr);
6067 /* Verify the DIE tree structure. */
6069 DEBUG_FUNCTION void
6070 verify_die (dw_die_ref die)
6072 gcc_assert (!die->die_mark);
6073 if (die->die_parent == NULL
6074 && die->die_sib == NULL)
6075 return;
6076 /* Verify the die_sib list is cyclic. */
6077 dw_die_ref x = die;
6080 x->die_mark = 1;
6081 x = x->die_sib;
6083 while (x && !x->die_mark);
6084 gcc_assert (x == die);
6085 x = die;
6088 /* Verify all dies have the same parent. */
6089 gcc_assert (x->die_parent == die->die_parent);
6090 if (x->die_child)
6092 /* Verify the child has the proper parent and recurse. */
6093 gcc_assert (x->die_child->die_parent == x);
6094 verify_die (x->die_child);
6096 x->die_mark = 0;
6097 x = x->die_sib;
6099 while (x && x->die_mark);
6102 /* Sanity checks on DIEs. */
6104 static void
6105 check_die (dw_die_ref die)
6107 unsigned ix;
6108 dw_attr_node *a;
6109 bool inline_found = false;
6110 int n_location = 0, n_low_pc = 0, n_high_pc = 0, n_artificial = 0;
6111 int n_decl_line = 0, n_decl_file = 0;
6112 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6114 switch (a->dw_attr)
6116 case DW_AT_inline:
6117 if (a->dw_attr_val.v.val_unsigned)
6118 inline_found = true;
6119 break;
6120 case DW_AT_location:
6121 ++n_location;
6122 break;
6123 case DW_AT_low_pc:
6124 ++n_low_pc;
6125 break;
6126 case DW_AT_high_pc:
6127 ++n_high_pc;
6128 break;
6129 case DW_AT_artificial:
6130 ++n_artificial;
6131 break;
6132 case DW_AT_decl_line:
6133 ++n_decl_line;
6134 break;
6135 case DW_AT_decl_file:
6136 ++n_decl_file;
6137 break;
6138 default:
6139 break;
6142 if (n_location > 1 || n_low_pc > 1 || n_high_pc > 1 || n_artificial > 1
6143 || n_decl_line > 1 || n_decl_file > 1)
6145 fprintf (stderr, "Duplicate attributes in DIE:\n");
6146 debug_dwarf_die (die);
6147 gcc_unreachable ();
6149 if (inline_found)
6151 /* A debugging information entry that is a member of an abstract
6152 instance tree [that has DW_AT_inline] should not contain any
6153 attributes which describe aspects of the subroutine which vary
6154 between distinct inlined expansions or distinct out-of-line
6155 expansions. */
6156 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6157 gcc_assert (a->dw_attr != DW_AT_low_pc
6158 && a->dw_attr != DW_AT_high_pc
6159 && a->dw_attr != DW_AT_location
6160 && a->dw_attr != DW_AT_frame_base
6161 && a->dw_attr != DW_AT_call_all_calls
6162 && a->dw_attr != DW_AT_GNU_all_call_sites);
6166 /* Start a new compilation unit DIE for an include file. OLD_UNIT is the CU
6167 for the enclosing include file, if any. BINCL_DIE is the DW_TAG_GNU_BINCL
6168 DIE that marks the start of the DIEs for this include file. */
6170 static dw_die_ref
6171 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
6173 const char *filename = get_AT_string (bincl_die, DW_AT_name);
6174 dw_die_ref new_unit = gen_compile_unit_die (filename);
6176 new_unit->die_sib = old_unit;
6177 return new_unit;
6180 /* Close an include-file CU and reopen the enclosing one. */
6182 static dw_die_ref
6183 pop_compile_unit (dw_die_ref old_unit)
6185 dw_die_ref new_unit = old_unit->die_sib;
6187 old_unit->die_sib = NULL;
6188 return new_unit;
6191 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
6192 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
6193 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
6195 /* Calculate the checksum of a location expression. */
6197 static inline void
6198 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
6200 int tem;
6201 inchash::hash hstate;
6202 hashval_t hash;
6204 tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
6205 CHECKSUM (tem);
6206 hash_loc_operands (loc, hstate);
6207 hash = hstate.end();
6208 CHECKSUM (hash);
6211 /* Calculate the checksum of an attribute. */
6213 static void
6214 attr_checksum (dw_attr_node *at, struct md5_ctx *ctx, int *mark)
6216 dw_loc_descr_ref loc;
6217 rtx r;
6219 CHECKSUM (at->dw_attr);
6221 /* We don't care that this was compiled with a different compiler
6222 snapshot; if the output is the same, that's what matters. */
6223 if (at->dw_attr == DW_AT_producer)
6224 return;
6226 switch (AT_class (at))
6228 case dw_val_class_const:
6229 case dw_val_class_const_implicit:
6230 CHECKSUM (at->dw_attr_val.v.val_int);
6231 break;
6232 case dw_val_class_unsigned_const:
6233 case dw_val_class_unsigned_const_implicit:
6234 CHECKSUM (at->dw_attr_val.v.val_unsigned);
6235 break;
6236 case dw_val_class_const_double:
6237 CHECKSUM (at->dw_attr_val.v.val_double);
6238 break;
6239 case dw_val_class_wide_int:
6240 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
6241 get_full_len (*at->dw_attr_val.v.val_wide)
6242 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
6243 break;
6244 case dw_val_class_vec:
6245 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
6246 (at->dw_attr_val.v.val_vec.length
6247 * at->dw_attr_val.v.val_vec.elt_size));
6248 break;
6249 case dw_val_class_flag:
6250 CHECKSUM (at->dw_attr_val.v.val_flag);
6251 break;
6252 case dw_val_class_str:
6253 CHECKSUM_STRING (AT_string (at));
6254 break;
6256 case dw_val_class_addr:
6257 r = AT_addr (at);
6258 gcc_assert (GET_CODE (r) == SYMBOL_REF);
6259 CHECKSUM_STRING (XSTR (r, 0));
6260 break;
6262 case dw_val_class_offset:
6263 CHECKSUM (at->dw_attr_val.v.val_offset);
6264 break;
6266 case dw_val_class_loc:
6267 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6268 loc_checksum (loc, ctx);
6269 break;
6271 case dw_val_class_die_ref:
6272 die_checksum (AT_ref (at), ctx, mark);
6273 break;
6275 case dw_val_class_fde_ref:
6276 case dw_val_class_vms_delta:
6277 case dw_val_class_lbl_id:
6278 case dw_val_class_lineptr:
6279 case dw_val_class_macptr:
6280 case dw_val_class_loclistsptr:
6281 case dw_val_class_high_pc:
6282 break;
6284 case dw_val_class_file:
6285 case dw_val_class_file_implicit:
6286 CHECKSUM_STRING (AT_file (at)->filename);
6287 break;
6289 case dw_val_class_data8:
6290 CHECKSUM (at->dw_attr_val.v.val_data8);
6291 break;
6293 default:
6294 break;
6298 /* Calculate the checksum of a DIE. */
6300 static void
6301 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6303 dw_die_ref c;
6304 dw_attr_node *a;
6305 unsigned ix;
6307 /* To avoid infinite recursion. */
6308 if (die->die_mark)
6310 CHECKSUM (die->die_mark);
6311 return;
6313 die->die_mark = ++(*mark);
6315 CHECKSUM (die->die_tag);
6317 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6318 attr_checksum (a, ctx, mark);
6320 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
6323 #undef CHECKSUM
6324 #undef CHECKSUM_BLOCK
6325 #undef CHECKSUM_STRING
6327 /* For DWARF-4 types, include the trailing NULL when checksumming strings. */
6328 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
6329 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
6330 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
6331 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
6332 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
6333 #define CHECKSUM_ATTR(FOO) \
6334 if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
6336 /* Calculate the checksum of a number in signed LEB128 format. */
6338 static void
6339 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
6341 unsigned char byte;
6342 bool more;
6344 while (1)
6346 byte = (value & 0x7f);
6347 value >>= 7;
6348 more = !((value == 0 && (byte & 0x40) == 0)
6349 || (value == -1 && (byte & 0x40) != 0));
6350 if (more)
6351 byte |= 0x80;
6352 CHECKSUM (byte);
6353 if (!more)
6354 break;
6358 /* Calculate the checksum of a number in unsigned LEB128 format. */
6360 static void
6361 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
6363 while (1)
6365 unsigned char byte = (value & 0x7f);
6366 value >>= 7;
6367 if (value != 0)
6368 /* More bytes to follow. */
6369 byte |= 0x80;
6370 CHECKSUM (byte);
6371 if (value == 0)
6372 break;
6376 /* Checksum the context of the DIE. This adds the names of any
6377 surrounding namespaces or structures to the checksum. */
6379 static void
6380 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
6382 const char *name;
6383 dw_die_ref spec;
6384 int tag = die->die_tag;
6386 if (tag != DW_TAG_namespace
6387 && tag != DW_TAG_structure_type
6388 && tag != DW_TAG_class_type)
6389 return;
6391 name = get_AT_string (die, DW_AT_name);
6393 spec = get_AT_ref (die, DW_AT_specification);
6394 if (spec != NULL)
6395 die = spec;
6397 if (die->die_parent != NULL)
6398 checksum_die_context (die->die_parent, ctx);
6400 CHECKSUM_ULEB128 ('C');
6401 CHECKSUM_ULEB128 (tag);
6402 if (name != NULL)
6403 CHECKSUM_STRING (name);
6406 /* Calculate the checksum of a location expression. */
6408 static inline void
6409 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
6411 /* Special case for lone DW_OP_plus_uconst: checksum as if the location
6412 were emitted as a DW_FORM_sdata instead of a location expression. */
6413 if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
6415 CHECKSUM_ULEB128 (DW_FORM_sdata);
6416 CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
6417 return;
6420 /* Otherwise, just checksum the raw location expression. */
6421 while (loc != NULL)
6423 inchash::hash hstate;
6424 hashval_t hash;
6426 CHECKSUM_ULEB128 (loc->dtprel);
6427 CHECKSUM_ULEB128 (loc->dw_loc_opc);
6428 hash_loc_operands (loc, hstate);
6429 hash = hstate.end ();
6430 CHECKSUM (hash);
6431 loc = loc->dw_loc_next;
6435 /* Calculate the checksum of an attribute. */
6437 static void
6438 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_node *at,
6439 struct md5_ctx *ctx, int *mark)
6441 dw_loc_descr_ref loc;
6442 rtx r;
6444 if (AT_class (at) == dw_val_class_die_ref)
6446 dw_die_ref target_die = AT_ref (at);
6448 /* For pointer and reference types, we checksum only the (qualified)
6449 name of the target type (if there is a name). For friend entries,
6450 we checksum only the (qualified) name of the target type or function.
6451 This allows the checksum to remain the same whether the target type
6452 is complete or not. */
6453 if ((at->dw_attr == DW_AT_type
6454 && (tag == DW_TAG_pointer_type
6455 || tag == DW_TAG_reference_type
6456 || tag == DW_TAG_rvalue_reference_type
6457 || tag == DW_TAG_ptr_to_member_type))
6458 || (at->dw_attr == DW_AT_friend
6459 && tag == DW_TAG_friend))
6461 dw_attr_node *name_attr = get_AT (target_die, DW_AT_name);
6463 if (name_attr != NULL)
6465 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
6467 if (decl == NULL)
6468 decl = target_die;
6469 CHECKSUM_ULEB128 ('N');
6470 CHECKSUM_ULEB128 (at->dw_attr);
6471 if (decl->die_parent != NULL)
6472 checksum_die_context (decl->die_parent, ctx);
6473 CHECKSUM_ULEB128 ('E');
6474 CHECKSUM_STRING (AT_string (name_attr));
6475 return;
6479 /* For all other references to another DIE, we check to see if the
6480 target DIE has already been visited. If it has, we emit a
6481 backward reference; if not, we descend recursively. */
6482 if (target_die->die_mark > 0)
6484 CHECKSUM_ULEB128 ('R');
6485 CHECKSUM_ULEB128 (at->dw_attr);
6486 CHECKSUM_ULEB128 (target_die->die_mark);
6488 else
6490 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
6492 if (decl == NULL)
6493 decl = target_die;
6494 target_die->die_mark = ++(*mark);
6495 CHECKSUM_ULEB128 ('T');
6496 CHECKSUM_ULEB128 (at->dw_attr);
6497 if (decl->die_parent != NULL)
6498 checksum_die_context (decl->die_parent, ctx);
6499 die_checksum_ordered (target_die, ctx, mark);
6501 return;
6504 CHECKSUM_ULEB128 ('A');
6505 CHECKSUM_ULEB128 (at->dw_attr);
6507 switch (AT_class (at))
6509 case dw_val_class_const:
6510 case dw_val_class_const_implicit:
6511 CHECKSUM_ULEB128 (DW_FORM_sdata);
6512 CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
6513 break;
6515 case dw_val_class_unsigned_const:
6516 case dw_val_class_unsigned_const_implicit:
6517 CHECKSUM_ULEB128 (DW_FORM_sdata);
6518 CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
6519 break;
6521 case dw_val_class_const_double:
6522 CHECKSUM_ULEB128 (DW_FORM_block);
6523 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
6524 CHECKSUM (at->dw_attr_val.v.val_double);
6525 break;
6527 case dw_val_class_wide_int:
6528 CHECKSUM_ULEB128 (DW_FORM_block);
6529 CHECKSUM_ULEB128 (get_full_len (*at->dw_attr_val.v.val_wide)
6530 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
6531 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
6532 get_full_len (*at->dw_attr_val.v.val_wide)
6533 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
6534 break;
6536 case dw_val_class_vec:
6537 CHECKSUM_ULEB128 (DW_FORM_block);
6538 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_vec.length
6539 * at->dw_attr_val.v.val_vec.elt_size);
6540 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
6541 (at->dw_attr_val.v.val_vec.length
6542 * at->dw_attr_val.v.val_vec.elt_size));
6543 break;
6545 case dw_val_class_flag:
6546 CHECKSUM_ULEB128 (DW_FORM_flag);
6547 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
6548 break;
6550 case dw_val_class_str:
6551 CHECKSUM_ULEB128 (DW_FORM_string);
6552 CHECKSUM_STRING (AT_string (at));
6553 break;
6555 case dw_val_class_addr:
6556 r = AT_addr (at);
6557 gcc_assert (GET_CODE (r) == SYMBOL_REF);
6558 CHECKSUM_ULEB128 (DW_FORM_string);
6559 CHECKSUM_STRING (XSTR (r, 0));
6560 break;
6562 case dw_val_class_offset:
6563 CHECKSUM_ULEB128 (DW_FORM_sdata);
6564 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
6565 break;
6567 case dw_val_class_loc:
6568 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6569 loc_checksum_ordered (loc, ctx);
6570 break;
6572 case dw_val_class_fde_ref:
6573 case dw_val_class_lbl_id:
6574 case dw_val_class_lineptr:
6575 case dw_val_class_macptr:
6576 case dw_val_class_loclistsptr:
6577 case dw_val_class_high_pc:
6578 break;
6580 case dw_val_class_file:
6581 case dw_val_class_file_implicit:
6582 CHECKSUM_ULEB128 (DW_FORM_string);
6583 CHECKSUM_STRING (AT_file (at)->filename);
6584 break;
6586 case dw_val_class_data8:
6587 CHECKSUM (at->dw_attr_val.v.val_data8);
6588 break;
6590 default:
6591 break;
6595 struct checksum_attributes
6597 dw_attr_node *at_name;
6598 dw_attr_node *at_type;
6599 dw_attr_node *at_friend;
6600 dw_attr_node *at_accessibility;
6601 dw_attr_node *at_address_class;
6602 dw_attr_node *at_allocated;
6603 dw_attr_node *at_artificial;
6604 dw_attr_node *at_associated;
6605 dw_attr_node *at_binary_scale;
6606 dw_attr_node *at_bit_offset;
6607 dw_attr_node *at_bit_size;
6608 dw_attr_node *at_bit_stride;
6609 dw_attr_node *at_byte_size;
6610 dw_attr_node *at_byte_stride;
6611 dw_attr_node *at_const_value;
6612 dw_attr_node *at_containing_type;
6613 dw_attr_node *at_count;
6614 dw_attr_node *at_data_location;
6615 dw_attr_node *at_data_member_location;
6616 dw_attr_node *at_decimal_scale;
6617 dw_attr_node *at_decimal_sign;
6618 dw_attr_node *at_default_value;
6619 dw_attr_node *at_digit_count;
6620 dw_attr_node *at_discr;
6621 dw_attr_node *at_discr_list;
6622 dw_attr_node *at_discr_value;
6623 dw_attr_node *at_encoding;
6624 dw_attr_node *at_endianity;
6625 dw_attr_node *at_explicit;
6626 dw_attr_node *at_is_optional;
6627 dw_attr_node *at_location;
6628 dw_attr_node *at_lower_bound;
6629 dw_attr_node *at_mutable;
6630 dw_attr_node *at_ordering;
6631 dw_attr_node *at_picture_string;
6632 dw_attr_node *at_prototyped;
6633 dw_attr_node *at_small;
6634 dw_attr_node *at_segment;
6635 dw_attr_node *at_string_length;
6636 dw_attr_node *at_string_length_bit_size;
6637 dw_attr_node *at_string_length_byte_size;
6638 dw_attr_node *at_threads_scaled;
6639 dw_attr_node *at_upper_bound;
6640 dw_attr_node *at_use_location;
6641 dw_attr_node *at_use_UTF8;
6642 dw_attr_node *at_variable_parameter;
6643 dw_attr_node *at_virtuality;
6644 dw_attr_node *at_visibility;
6645 dw_attr_node *at_vtable_elem_location;
6648 /* Collect the attributes that we will want to use for the checksum. */
6650 static void
6651 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
6653 dw_attr_node *a;
6654 unsigned ix;
6656 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6658 switch (a->dw_attr)
6660 case DW_AT_name:
6661 attrs->at_name = a;
6662 break;
6663 case DW_AT_type:
6664 attrs->at_type = a;
6665 break;
6666 case DW_AT_friend:
6667 attrs->at_friend = a;
6668 break;
6669 case DW_AT_accessibility:
6670 attrs->at_accessibility = a;
6671 break;
6672 case DW_AT_address_class:
6673 attrs->at_address_class = a;
6674 break;
6675 case DW_AT_allocated:
6676 attrs->at_allocated = a;
6677 break;
6678 case DW_AT_artificial:
6679 attrs->at_artificial = a;
6680 break;
6681 case DW_AT_associated:
6682 attrs->at_associated = a;
6683 break;
6684 case DW_AT_binary_scale:
6685 attrs->at_binary_scale = a;
6686 break;
6687 case DW_AT_bit_offset:
6688 attrs->at_bit_offset = a;
6689 break;
6690 case DW_AT_bit_size:
6691 attrs->at_bit_size = a;
6692 break;
6693 case DW_AT_bit_stride:
6694 attrs->at_bit_stride = a;
6695 break;
6696 case DW_AT_byte_size:
6697 attrs->at_byte_size = a;
6698 break;
6699 case DW_AT_byte_stride:
6700 attrs->at_byte_stride = a;
6701 break;
6702 case DW_AT_const_value:
6703 attrs->at_const_value = a;
6704 break;
6705 case DW_AT_containing_type:
6706 attrs->at_containing_type = a;
6707 break;
6708 case DW_AT_count:
6709 attrs->at_count = a;
6710 break;
6711 case DW_AT_data_location:
6712 attrs->at_data_location = a;
6713 break;
6714 case DW_AT_data_member_location:
6715 attrs->at_data_member_location = a;
6716 break;
6717 case DW_AT_decimal_scale:
6718 attrs->at_decimal_scale = a;
6719 break;
6720 case DW_AT_decimal_sign:
6721 attrs->at_decimal_sign = a;
6722 break;
6723 case DW_AT_default_value:
6724 attrs->at_default_value = a;
6725 break;
6726 case DW_AT_digit_count:
6727 attrs->at_digit_count = a;
6728 break;
6729 case DW_AT_discr:
6730 attrs->at_discr = a;
6731 break;
6732 case DW_AT_discr_list:
6733 attrs->at_discr_list = a;
6734 break;
6735 case DW_AT_discr_value:
6736 attrs->at_discr_value = a;
6737 break;
6738 case DW_AT_encoding:
6739 attrs->at_encoding = a;
6740 break;
6741 case DW_AT_endianity:
6742 attrs->at_endianity = a;
6743 break;
6744 case DW_AT_explicit:
6745 attrs->at_explicit = a;
6746 break;
6747 case DW_AT_is_optional:
6748 attrs->at_is_optional = a;
6749 break;
6750 case DW_AT_location:
6751 attrs->at_location = a;
6752 break;
6753 case DW_AT_lower_bound:
6754 attrs->at_lower_bound = a;
6755 break;
6756 case DW_AT_mutable:
6757 attrs->at_mutable = a;
6758 break;
6759 case DW_AT_ordering:
6760 attrs->at_ordering = a;
6761 break;
6762 case DW_AT_picture_string:
6763 attrs->at_picture_string = a;
6764 break;
6765 case DW_AT_prototyped:
6766 attrs->at_prototyped = a;
6767 break;
6768 case DW_AT_small:
6769 attrs->at_small = a;
6770 break;
6771 case DW_AT_segment:
6772 attrs->at_segment = a;
6773 break;
6774 case DW_AT_string_length:
6775 attrs->at_string_length = a;
6776 break;
6777 case DW_AT_string_length_bit_size:
6778 attrs->at_string_length_bit_size = a;
6779 break;
6780 case DW_AT_string_length_byte_size:
6781 attrs->at_string_length_byte_size = a;
6782 break;
6783 case DW_AT_threads_scaled:
6784 attrs->at_threads_scaled = a;
6785 break;
6786 case DW_AT_upper_bound:
6787 attrs->at_upper_bound = a;
6788 break;
6789 case DW_AT_use_location:
6790 attrs->at_use_location = a;
6791 break;
6792 case DW_AT_use_UTF8:
6793 attrs->at_use_UTF8 = a;
6794 break;
6795 case DW_AT_variable_parameter:
6796 attrs->at_variable_parameter = a;
6797 break;
6798 case DW_AT_virtuality:
6799 attrs->at_virtuality = a;
6800 break;
6801 case DW_AT_visibility:
6802 attrs->at_visibility = a;
6803 break;
6804 case DW_AT_vtable_elem_location:
6805 attrs->at_vtable_elem_location = a;
6806 break;
6807 default:
6808 break;
6813 /* Calculate the checksum of a DIE, using an ordered subset of attributes. */
6815 static void
6816 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6818 dw_die_ref c;
6819 dw_die_ref decl;
6820 struct checksum_attributes attrs;
6822 CHECKSUM_ULEB128 ('D');
6823 CHECKSUM_ULEB128 (die->die_tag);
6825 memset (&attrs, 0, sizeof (attrs));
6827 decl = get_AT_ref (die, DW_AT_specification);
6828 if (decl != NULL)
6829 collect_checksum_attributes (&attrs, decl);
6830 collect_checksum_attributes (&attrs, die);
6832 CHECKSUM_ATTR (attrs.at_name);
6833 CHECKSUM_ATTR (attrs.at_accessibility);
6834 CHECKSUM_ATTR (attrs.at_address_class);
6835 CHECKSUM_ATTR (attrs.at_allocated);
6836 CHECKSUM_ATTR (attrs.at_artificial);
6837 CHECKSUM_ATTR (attrs.at_associated);
6838 CHECKSUM_ATTR (attrs.at_binary_scale);
6839 CHECKSUM_ATTR (attrs.at_bit_offset);
6840 CHECKSUM_ATTR (attrs.at_bit_size);
6841 CHECKSUM_ATTR (attrs.at_bit_stride);
6842 CHECKSUM_ATTR (attrs.at_byte_size);
6843 CHECKSUM_ATTR (attrs.at_byte_stride);
6844 CHECKSUM_ATTR (attrs.at_const_value);
6845 CHECKSUM_ATTR (attrs.at_containing_type);
6846 CHECKSUM_ATTR (attrs.at_count);
6847 CHECKSUM_ATTR (attrs.at_data_location);
6848 CHECKSUM_ATTR (attrs.at_data_member_location);
6849 CHECKSUM_ATTR (attrs.at_decimal_scale);
6850 CHECKSUM_ATTR (attrs.at_decimal_sign);
6851 CHECKSUM_ATTR (attrs.at_default_value);
6852 CHECKSUM_ATTR (attrs.at_digit_count);
6853 CHECKSUM_ATTR (attrs.at_discr);
6854 CHECKSUM_ATTR (attrs.at_discr_list);
6855 CHECKSUM_ATTR (attrs.at_discr_value);
6856 CHECKSUM_ATTR (attrs.at_encoding);
6857 CHECKSUM_ATTR (attrs.at_endianity);
6858 CHECKSUM_ATTR (attrs.at_explicit);
6859 CHECKSUM_ATTR (attrs.at_is_optional);
6860 CHECKSUM_ATTR (attrs.at_location);
6861 CHECKSUM_ATTR (attrs.at_lower_bound);
6862 CHECKSUM_ATTR (attrs.at_mutable);
6863 CHECKSUM_ATTR (attrs.at_ordering);
6864 CHECKSUM_ATTR (attrs.at_picture_string);
6865 CHECKSUM_ATTR (attrs.at_prototyped);
6866 CHECKSUM_ATTR (attrs.at_small);
6867 CHECKSUM_ATTR (attrs.at_segment);
6868 CHECKSUM_ATTR (attrs.at_string_length);
6869 CHECKSUM_ATTR (attrs.at_string_length_bit_size);
6870 CHECKSUM_ATTR (attrs.at_string_length_byte_size);
6871 CHECKSUM_ATTR (attrs.at_threads_scaled);
6872 CHECKSUM_ATTR (attrs.at_upper_bound);
6873 CHECKSUM_ATTR (attrs.at_use_location);
6874 CHECKSUM_ATTR (attrs.at_use_UTF8);
6875 CHECKSUM_ATTR (attrs.at_variable_parameter);
6876 CHECKSUM_ATTR (attrs.at_virtuality);
6877 CHECKSUM_ATTR (attrs.at_visibility);
6878 CHECKSUM_ATTR (attrs.at_vtable_elem_location);
6879 CHECKSUM_ATTR (attrs.at_type);
6880 CHECKSUM_ATTR (attrs.at_friend);
6882 /* Checksum the child DIEs. */
6883 c = die->die_child;
6884 if (c) do {
6885 dw_attr_node *name_attr;
6887 c = c->die_sib;
6888 name_attr = get_AT (c, DW_AT_name);
6889 if (is_template_instantiation (c))
6891 /* Ignore instantiations of member type and function templates. */
6893 else if (name_attr != NULL
6894 && (is_type_die (c) || c->die_tag == DW_TAG_subprogram))
6896 /* Use a shallow checksum for named nested types and member
6897 functions. */
6898 CHECKSUM_ULEB128 ('S');
6899 CHECKSUM_ULEB128 (c->die_tag);
6900 CHECKSUM_STRING (AT_string (name_attr));
6902 else
6904 /* Use a deep checksum for other children. */
6905 /* Mark this DIE so it gets processed when unmarking. */
6906 if (c->die_mark == 0)
6907 c->die_mark = -1;
6908 die_checksum_ordered (c, ctx, mark);
6910 } while (c != die->die_child);
6912 CHECKSUM_ULEB128 (0);
6915 /* Add a type name and tag to a hash. */
6916 static void
6917 die_odr_checksum (int tag, const char *name, md5_ctx *ctx)
6919 CHECKSUM_ULEB128 (tag);
6920 CHECKSUM_STRING (name);
6923 #undef CHECKSUM
6924 #undef CHECKSUM_STRING
6925 #undef CHECKSUM_ATTR
6926 #undef CHECKSUM_LEB128
6927 #undef CHECKSUM_ULEB128
6929 /* Generate the type signature for DIE. This is computed by generating an
6930 MD5 checksum over the DIE's tag, its relevant attributes, and its
6931 children. Attributes that are references to other DIEs are processed
6932 by recursion, using the MARK field to prevent infinite recursion.
6933 If the DIE is nested inside a namespace or another type, we also
6934 need to include that context in the signature. The lower 64 bits
6935 of the resulting MD5 checksum comprise the signature. */
6937 static void
6938 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
6940 int mark;
6941 const char *name;
6942 unsigned char checksum[16];
6943 struct md5_ctx ctx;
6944 dw_die_ref decl;
6945 dw_die_ref parent;
6947 name = get_AT_string (die, DW_AT_name);
6948 decl = get_AT_ref (die, DW_AT_specification);
6949 parent = get_die_parent (die);
6951 /* First, compute a signature for just the type name (and its surrounding
6952 context, if any. This is stored in the type unit DIE for link-time
6953 ODR (one-definition rule) checking. */
6955 if (is_cxx () && name != NULL)
6957 md5_init_ctx (&ctx);
6959 /* Checksum the names of surrounding namespaces and structures. */
6960 if (parent != NULL)
6961 checksum_die_context (parent, &ctx);
6963 /* Checksum the current DIE. */
6964 die_odr_checksum (die->die_tag, name, &ctx);
6965 md5_finish_ctx (&ctx, checksum);
6967 add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
6970 /* Next, compute the complete type signature. */
6972 md5_init_ctx (&ctx);
6973 mark = 1;
6974 die->die_mark = mark;
6976 /* Checksum the names of surrounding namespaces and structures. */
6977 if (parent != NULL)
6978 checksum_die_context (parent, &ctx);
6980 /* Checksum the DIE and its children. */
6981 die_checksum_ordered (die, &ctx, &mark);
6982 unmark_all_dies (die);
6983 md5_finish_ctx (&ctx, checksum);
6985 /* Store the signature in the type node and link the type DIE and the
6986 type node together. */
6987 memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
6988 DWARF_TYPE_SIGNATURE_SIZE);
6989 die->comdat_type_p = true;
6990 die->die_id.die_type_node = type_node;
6991 type_node->type_die = die;
6993 /* If the DIE is a specification, link its declaration to the type node
6994 as well. */
6995 if (decl != NULL)
6997 decl->comdat_type_p = true;
6998 decl->die_id.die_type_node = type_node;
7002 /* Do the location expressions look same? */
7003 static inline int
7004 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
7006 return loc1->dw_loc_opc == loc2->dw_loc_opc
7007 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
7008 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
7011 /* Do the values look the same? */
7012 static int
7013 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
7015 dw_loc_descr_ref loc1, loc2;
7016 rtx r1, r2;
7018 if (v1->val_class != v2->val_class)
7019 return 0;
7021 switch (v1->val_class)
7023 case dw_val_class_const:
7024 case dw_val_class_const_implicit:
7025 return v1->v.val_int == v2->v.val_int;
7026 case dw_val_class_unsigned_const:
7027 case dw_val_class_unsigned_const_implicit:
7028 return v1->v.val_unsigned == v2->v.val_unsigned;
7029 case dw_val_class_const_double:
7030 return v1->v.val_double.high == v2->v.val_double.high
7031 && v1->v.val_double.low == v2->v.val_double.low;
7032 case dw_val_class_wide_int:
7033 return *v1->v.val_wide == *v2->v.val_wide;
7034 case dw_val_class_vec:
7035 if (v1->v.val_vec.length != v2->v.val_vec.length
7036 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
7037 return 0;
7038 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
7039 v1->v.val_vec.length * v1->v.val_vec.elt_size))
7040 return 0;
7041 return 1;
7042 case dw_val_class_flag:
7043 return v1->v.val_flag == v2->v.val_flag;
7044 case dw_val_class_str:
7045 return !strcmp (v1->v.val_str->str, v2->v.val_str->str);
7047 case dw_val_class_addr:
7048 r1 = v1->v.val_addr;
7049 r2 = v2->v.val_addr;
7050 if (GET_CODE (r1) != GET_CODE (r2))
7051 return 0;
7052 return !rtx_equal_p (r1, r2);
7054 case dw_val_class_offset:
7055 return v1->v.val_offset == v2->v.val_offset;
7057 case dw_val_class_loc:
7058 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
7059 loc1 && loc2;
7060 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
7061 if (!same_loc_p (loc1, loc2, mark))
7062 return 0;
7063 return !loc1 && !loc2;
7065 case dw_val_class_die_ref:
7066 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
7068 case dw_val_class_fde_ref:
7069 case dw_val_class_vms_delta:
7070 case dw_val_class_lbl_id:
7071 case dw_val_class_lineptr:
7072 case dw_val_class_macptr:
7073 case dw_val_class_loclistsptr:
7074 case dw_val_class_high_pc:
7075 return 1;
7077 case dw_val_class_file:
7078 case dw_val_class_file_implicit:
7079 return v1->v.val_file == v2->v.val_file;
7081 case dw_val_class_data8:
7082 return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
7084 default:
7085 return 1;
7089 /* Do the attributes look the same? */
7091 static int
7092 same_attr_p (dw_attr_node *at1, dw_attr_node *at2, int *mark)
7094 if (at1->dw_attr != at2->dw_attr)
7095 return 0;
7097 /* We don't care that this was compiled with a different compiler
7098 snapshot; if the output is the same, that's what matters. */
7099 if (at1->dw_attr == DW_AT_producer)
7100 return 1;
7102 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
7105 /* Do the dies look the same? */
7107 static int
7108 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
7110 dw_die_ref c1, c2;
7111 dw_attr_node *a1;
7112 unsigned ix;
7114 /* To avoid infinite recursion. */
7115 if (die1->die_mark)
7116 return die1->die_mark == die2->die_mark;
7117 die1->die_mark = die2->die_mark = ++(*mark);
7119 if (die1->die_tag != die2->die_tag)
7120 return 0;
7122 if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
7123 return 0;
7125 FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
7126 if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
7127 return 0;
7129 c1 = die1->die_child;
7130 c2 = die2->die_child;
7131 if (! c1)
7133 if (c2)
7134 return 0;
7136 else
7137 for (;;)
7139 if (!same_die_p (c1, c2, mark))
7140 return 0;
7141 c1 = c1->die_sib;
7142 c2 = c2->die_sib;
7143 if (c1 == die1->die_child)
7145 if (c2 == die2->die_child)
7146 break;
7147 else
7148 return 0;
7152 return 1;
7155 /* Do the dies look the same? Wrapper around same_die_p. */
7157 static int
7158 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
7160 int mark = 0;
7161 int ret = same_die_p (die1, die2, &mark);
7163 unmark_all_dies (die1);
7164 unmark_all_dies (die2);
7166 return ret;
7169 /* The prefix to attach to symbols on DIEs in the current comdat debug
7170 info section. */
7171 static const char *comdat_symbol_id;
7173 /* The index of the current symbol within the current comdat CU. */
7174 static unsigned int comdat_symbol_number;
7176 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
7177 children, and set comdat_symbol_id accordingly. */
7179 static void
7180 compute_section_prefix (dw_die_ref unit_die)
7182 const char *die_name = get_AT_string (unit_die, DW_AT_name);
7183 const char *base = die_name ? lbasename (die_name) : "anonymous";
7184 char *name = XALLOCAVEC (char, strlen (base) + 64);
7185 char *p;
7186 int i, mark;
7187 unsigned char checksum[16];
7188 struct md5_ctx ctx;
7190 /* Compute the checksum of the DIE, then append part of it as hex digits to
7191 the name filename of the unit. */
7193 md5_init_ctx (&ctx);
7194 mark = 0;
7195 die_checksum (unit_die, &ctx, &mark);
7196 unmark_all_dies (unit_die);
7197 md5_finish_ctx (&ctx, checksum);
7199 sprintf (name, "%s.", base);
7200 clean_symbol_name (name);
7202 p = name + strlen (name);
7203 for (i = 0; i < 4; i++)
7205 sprintf (p, "%.2x", checksum[i]);
7206 p += 2;
7209 comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
7210 comdat_symbol_number = 0;
7213 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
7215 static int
7216 is_type_die (dw_die_ref die)
7218 switch (die->die_tag)
7220 case DW_TAG_array_type:
7221 case DW_TAG_class_type:
7222 case DW_TAG_interface_type:
7223 case DW_TAG_enumeration_type:
7224 case DW_TAG_pointer_type:
7225 case DW_TAG_reference_type:
7226 case DW_TAG_rvalue_reference_type:
7227 case DW_TAG_string_type:
7228 case DW_TAG_structure_type:
7229 case DW_TAG_subroutine_type:
7230 case DW_TAG_union_type:
7231 case DW_TAG_ptr_to_member_type:
7232 case DW_TAG_set_type:
7233 case DW_TAG_subrange_type:
7234 case DW_TAG_base_type:
7235 case DW_TAG_const_type:
7236 case DW_TAG_file_type:
7237 case DW_TAG_packed_type:
7238 case DW_TAG_volatile_type:
7239 case DW_TAG_typedef:
7240 return 1;
7241 default:
7242 return 0;
7246 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
7247 Basically, we want to choose the bits that are likely to be shared between
7248 compilations (types) and leave out the bits that are specific to individual
7249 compilations (functions). */
7251 static int
7252 is_comdat_die (dw_die_ref c)
7254 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
7255 we do for stabs. The advantage is a greater likelihood of sharing between
7256 objects that don't include headers in the same order (and therefore would
7257 put the base types in a different comdat). jason 8/28/00 */
7259 if (c->die_tag == DW_TAG_base_type)
7260 return 0;
7262 if (c->die_tag == DW_TAG_pointer_type
7263 || c->die_tag == DW_TAG_reference_type
7264 || c->die_tag == DW_TAG_rvalue_reference_type
7265 || c->die_tag == DW_TAG_const_type
7266 || c->die_tag == DW_TAG_volatile_type)
7268 dw_die_ref t = get_AT_ref (c, DW_AT_type);
7270 return t ? is_comdat_die (t) : 0;
7273 return is_type_die (c);
7276 /* Returns 1 iff C is the sort of DIE that might be referred to from another
7277 compilation unit. */
7279 static int
7280 is_symbol_die (dw_die_ref c)
7282 return (is_type_die (c)
7283 || is_declaration_die (c)
7284 || c->die_tag == DW_TAG_namespace
7285 || c->die_tag == DW_TAG_module);
7288 /* Returns true iff C is a compile-unit DIE. */
7290 static inline bool
7291 is_cu_die (dw_die_ref c)
7293 return c && (c->die_tag == DW_TAG_compile_unit
7294 || c->die_tag == DW_TAG_skeleton_unit);
7297 /* Returns true iff C is a unit DIE of some sort. */
7299 static inline bool
7300 is_unit_die (dw_die_ref c)
7302 return c && (c->die_tag == DW_TAG_compile_unit
7303 || c->die_tag == DW_TAG_partial_unit
7304 || c->die_tag == DW_TAG_type_unit
7305 || c->die_tag == DW_TAG_skeleton_unit);
7308 /* Returns true iff C is a namespace DIE. */
7310 static inline bool
7311 is_namespace_die (dw_die_ref c)
7313 return c && c->die_tag == DW_TAG_namespace;
7316 /* Returns true iff C is a class or structure DIE. */
7318 static inline bool
7319 is_class_die (dw_die_ref c)
7321 return c && (c->die_tag == DW_TAG_class_type
7322 || c->die_tag == DW_TAG_structure_type);
7325 /* Return non-zero if this DIE is a template parameter. */
7327 static inline bool
7328 is_template_parameter (dw_die_ref die)
7330 switch (die->die_tag)
7332 case DW_TAG_template_type_param:
7333 case DW_TAG_template_value_param:
7334 case DW_TAG_GNU_template_template_param:
7335 case DW_TAG_GNU_template_parameter_pack:
7336 return true;
7337 default:
7338 return false;
7342 /* Return non-zero if this DIE represents a template instantiation. */
7344 static inline bool
7345 is_template_instantiation (dw_die_ref die)
7347 dw_die_ref c;
7349 if (!is_type_die (die) && die->die_tag != DW_TAG_subprogram)
7350 return false;
7351 FOR_EACH_CHILD (die, c, if (is_template_parameter (c)) return true);
7352 return false;
7355 static char *
7356 gen_internal_sym (const char *prefix)
7358 char buf[MAX_ARTIFICIAL_LABEL_BYTES];
7360 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
7361 return xstrdup (buf);
7364 /* Assign symbols to all worthy DIEs under DIE. */
7366 static void
7367 assign_symbol_names (dw_die_ref die)
7369 dw_die_ref c;
7371 if (is_symbol_die (die) && !die->comdat_type_p)
7373 if (comdat_symbol_id)
7375 char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
7377 sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
7378 comdat_symbol_id, comdat_symbol_number++);
7379 die->die_id.die_symbol = xstrdup (p);
7381 else
7382 die->die_id.die_symbol = gen_internal_sym ("LDIE");
7385 FOR_EACH_CHILD (die, c, assign_symbol_names (c));
7388 struct cu_hash_table_entry
7390 dw_die_ref cu;
7391 unsigned min_comdat_num, max_comdat_num;
7392 struct cu_hash_table_entry *next;
7395 /* Helpers to manipulate hash table of CUs. */
7397 struct cu_hash_table_entry_hasher : pointer_hash <cu_hash_table_entry>
7399 typedef die_struct *compare_type;
7400 static inline hashval_t hash (const cu_hash_table_entry *);
7401 static inline bool equal (const cu_hash_table_entry *, const die_struct *);
7402 static inline void remove (cu_hash_table_entry *);
7405 inline hashval_t
7406 cu_hash_table_entry_hasher::hash (const cu_hash_table_entry *entry)
7408 return htab_hash_string (entry->cu->die_id.die_symbol);
7411 inline bool
7412 cu_hash_table_entry_hasher::equal (const cu_hash_table_entry *entry1,
7413 const die_struct *entry2)
7415 return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
7418 inline void
7419 cu_hash_table_entry_hasher::remove (cu_hash_table_entry *entry)
7421 struct cu_hash_table_entry *next;
7423 while (entry)
7425 next = entry->next;
7426 free (entry);
7427 entry = next;
7431 typedef hash_table<cu_hash_table_entry_hasher> cu_hash_type;
7433 /* Check whether we have already seen this CU and set up SYM_NUM
7434 accordingly. */
7435 static int
7436 check_duplicate_cu (dw_die_ref cu, cu_hash_type *htable, unsigned int *sym_num)
7438 struct cu_hash_table_entry dummy;
7439 struct cu_hash_table_entry **slot, *entry, *last = &dummy;
7441 dummy.max_comdat_num = 0;
7443 slot = htable->find_slot_with_hash (cu,
7444 htab_hash_string (cu->die_id.die_symbol),
7445 INSERT);
7446 entry = *slot;
7448 for (; entry; last = entry, entry = entry->next)
7450 if (same_die_p_wrap (cu, entry->cu))
7451 break;
7454 if (entry)
7456 *sym_num = entry->min_comdat_num;
7457 return 1;
7460 entry = XCNEW (struct cu_hash_table_entry);
7461 entry->cu = cu;
7462 entry->min_comdat_num = *sym_num = last->max_comdat_num;
7463 entry->next = *slot;
7464 *slot = entry;
7466 return 0;
7469 /* Record SYM_NUM to record of CU in HTABLE. */
7470 static void
7471 record_comdat_symbol_number (dw_die_ref cu, cu_hash_type *htable,
7472 unsigned int sym_num)
7474 struct cu_hash_table_entry **slot, *entry;
7476 slot = htable->find_slot_with_hash (cu,
7477 htab_hash_string (cu->die_id.die_symbol),
7478 NO_INSERT);
7479 entry = *slot;
7481 entry->max_comdat_num = sym_num;
7484 /* Traverse the DIE (which is always comp_unit_die), and set up
7485 additional compilation units for each of the include files we see
7486 bracketed by BINCL/EINCL. */
7488 static void
7489 break_out_includes (dw_die_ref die)
7491 dw_die_ref c;
7492 dw_die_ref unit = NULL;
7493 limbo_die_node *node, **pnode;
7495 c = die->die_child;
7496 if (c) do {
7497 dw_die_ref prev = c;
7498 c = c->die_sib;
7499 while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
7500 || (unit && is_comdat_die (c)))
7502 dw_die_ref next = c->die_sib;
7504 /* This DIE is for a secondary CU; remove it from the main one. */
7505 remove_child_with_prev (c, prev);
7507 if (c->die_tag == DW_TAG_GNU_BINCL)
7508 unit = push_new_compile_unit (unit, c);
7509 else if (c->die_tag == DW_TAG_GNU_EINCL)
7510 unit = pop_compile_unit (unit);
7511 else
7512 add_child_die (unit, c);
7513 c = next;
7514 if (c == die->die_child)
7515 break;
7517 } while (c != die->die_child);
7519 #if 0
7520 /* We can only use this in debugging, since the frontend doesn't check
7521 to make sure that we leave every include file we enter. */
7522 gcc_assert (!unit);
7523 #endif
7525 assign_symbol_names (die);
7526 cu_hash_type cu_hash_table (10);
7527 for (node = limbo_die_list, pnode = &limbo_die_list;
7528 node;
7529 node = node->next)
7531 int is_dupl;
7533 compute_section_prefix (node->die);
7534 is_dupl = check_duplicate_cu (node->die, &cu_hash_table,
7535 &comdat_symbol_number);
7536 assign_symbol_names (node->die);
7537 if (is_dupl)
7538 *pnode = node->next;
7539 else
7541 pnode = &node->next;
7542 record_comdat_symbol_number (node->die, &cu_hash_table,
7543 comdat_symbol_number);
7548 /* Return non-zero if this DIE is a declaration. */
7550 static int
7551 is_declaration_die (dw_die_ref die)
7553 dw_attr_node *a;
7554 unsigned ix;
7556 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7557 if (a->dw_attr == DW_AT_declaration)
7558 return 1;
7560 return 0;
7563 /* Return non-zero if this DIE is nested inside a subprogram. */
7565 static int
7566 is_nested_in_subprogram (dw_die_ref die)
7568 dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
7570 if (decl == NULL)
7571 decl = die;
7572 return local_scope_p (decl);
7575 /* Return non-zero if this DIE contains a defining declaration of a
7576 subprogram. */
7578 static int
7579 contains_subprogram_definition (dw_die_ref die)
7581 dw_die_ref c;
7583 if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
7584 return 1;
7585 FOR_EACH_CHILD (die, c, if (contains_subprogram_definition (c)) return 1);
7586 return 0;
7589 /* Return non-zero if this is a type DIE that should be moved to a
7590 COMDAT .debug_types section or .debug_info section with DW_UT_*type
7591 unit type. */
7593 static int
7594 should_move_die_to_comdat (dw_die_ref die)
7596 switch (die->die_tag)
7598 case DW_TAG_class_type:
7599 case DW_TAG_structure_type:
7600 case DW_TAG_enumeration_type:
7601 case DW_TAG_union_type:
7602 /* Don't move declarations, inlined instances, types nested in a
7603 subprogram, or types that contain subprogram definitions. */
7604 if (is_declaration_die (die)
7605 || get_AT (die, DW_AT_abstract_origin)
7606 || is_nested_in_subprogram (die)
7607 || contains_subprogram_definition (die))
7608 return 0;
7609 return 1;
7610 case DW_TAG_array_type:
7611 case DW_TAG_interface_type:
7612 case DW_TAG_pointer_type:
7613 case DW_TAG_reference_type:
7614 case DW_TAG_rvalue_reference_type:
7615 case DW_TAG_string_type:
7616 case DW_TAG_subroutine_type:
7617 case DW_TAG_ptr_to_member_type:
7618 case DW_TAG_set_type:
7619 case DW_TAG_subrange_type:
7620 case DW_TAG_base_type:
7621 case DW_TAG_const_type:
7622 case DW_TAG_file_type:
7623 case DW_TAG_packed_type:
7624 case DW_TAG_volatile_type:
7625 case DW_TAG_typedef:
7626 default:
7627 return 0;
7631 /* Make a clone of DIE. */
7633 static dw_die_ref
7634 clone_die (dw_die_ref die)
7636 dw_die_ref clone;
7637 dw_attr_node *a;
7638 unsigned ix;
7640 clone = ggc_cleared_alloc<die_node> ();
7641 clone->die_tag = die->die_tag;
7643 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7644 add_dwarf_attr (clone, a);
7646 return clone;
7649 /* Make a clone of the tree rooted at DIE. */
7651 static dw_die_ref
7652 clone_tree (dw_die_ref die)
7654 dw_die_ref c;
7655 dw_die_ref clone = clone_die (die);
7657 FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree (c)));
7659 return clone;
7662 /* Make a clone of DIE as a declaration. */
7664 static dw_die_ref
7665 clone_as_declaration (dw_die_ref die)
7667 dw_die_ref clone;
7668 dw_die_ref decl;
7669 dw_attr_node *a;
7670 unsigned ix;
7672 /* If the DIE is already a declaration, just clone it. */
7673 if (is_declaration_die (die))
7674 return clone_die (die);
7676 /* If the DIE is a specification, just clone its declaration DIE. */
7677 decl = get_AT_ref (die, DW_AT_specification);
7678 if (decl != NULL)
7680 clone = clone_die (decl);
7681 if (die->comdat_type_p)
7682 add_AT_die_ref (clone, DW_AT_signature, die);
7683 return clone;
7686 clone = ggc_cleared_alloc<die_node> ();
7687 clone->die_tag = die->die_tag;
7689 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7691 /* We don't want to copy over all attributes.
7692 For example we don't want DW_AT_byte_size because otherwise we will no
7693 longer have a declaration and GDB will treat it as a definition. */
7695 switch (a->dw_attr)
7697 case DW_AT_abstract_origin:
7698 case DW_AT_artificial:
7699 case DW_AT_containing_type:
7700 case DW_AT_external:
7701 case DW_AT_name:
7702 case DW_AT_type:
7703 case DW_AT_virtuality:
7704 case DW_AT_linkage_name:
7705 case DW_AT_MIPS_linkage_name:
7706 add_dwarf_attr (clone, a);
7707 break;
7708 case DW_AT_byte_size:
7709 default:
7710 break;
7714 if (die->comdat_type_p)
7715 add_AT_die_ref (clone, DW_AT_signature, die);
7717 add_AT_flag (clone, DW_AT_declaration, 1);
7718 return clone;
7722 /* Structure to map a DIE in one CU to its copy in a comdat type unit. */
7724 struct decl_table_entry
7726 dw_die_ref orig;
7727 dw_die_ref copy;
7730 /* Helpers to manipulate hash table of copied declarations. */
7732 /* Hashtable helpers. */
7734 struct decl_table_entry_hasher : free_ptr_hash <decl_table_entry>
7736 typedef die_struct *compare_type;
7737 static inline hashval_t hash (const decl_table_entry *);
7738 static inline bool equal (const decl_table_entry *, const die_struct *);
7741 inline hashval_t
7742 decl_table_entry_hasher::hash (const decl_table_entry *entry)
7744 return htab_hash_pointer (entry->orig);
7747 inline bool
7748 decl_table_entry_hasher::equal (const decl_table_entry *entry1,
7749 const die_struct *entry2)
7751 return entry1->orig == entry2;
7754 typedef hash_table<decl_table_entry_hasher> decl_hash_type;
7756 /* Copy DIE and its ancestors, up to, but not including, the compile unit
7757 or type unit entry, to a new tree. Adds the new tree to UNIT and returns
7758 a pointer to the copy of DIE. If DECL_TABLE is provided, it is used
7759 to check if the ancestor has already been copied into UNIT. */
7761 static dw_die_ref
7762 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die,
7763 decl_hash_type *decl_table)
7765 dw_die_ref parent = die->die_parent;
7766 dw_die_ref new_parent = unit;
7767 dw_die_ref copy;
7768 decl_table_entry **slot = NULL;
7769 struct decl_table_entry *entry = NULL;
7771 if (decl_table)
7773 /* Check if the entry has already been copied to UNIT. */
7774 slot = decl_table->find_slot_with_hash (die, htab_hash_pointer (die),
7775 INSERT);
7776 if (*slot != HTAB_EMPTY_ENTRY)
7778 entry = *slot;
7779 return entry->copy;
7782 /* Record in DECL_TABLE that DIE has been copied to UNIT. */
7783 entry = XCNEW (struct decl_table_entry);
7784 entry->orig = die;
7785 entry->copy = NULL;
7786 *slot = entry;
7789 if (parent != NULL)
7791 dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
7792 if (spec != NULL)
7793 parent = spec;
7794 if (!is_unit_die (parent))
7795 new_parent = copy_ancestor_tree (unit, parent, decl_table);
7798 copy = clone_as_declaration (die);
7799 add_child_die (new_parent, copy);
7801 if (decl_table)
7803 /* Record the pointer to the copy. */
7804 entry->copy = copy;
7807 return copy;
7809 /* Copy the declaration context to the new type unit DIE. This includes
7810 any surrounding namespace or type declarations. If the DIE has an
7811 AT_specification attribute, it also includes attributes and children
7812 attached to the specification, and returns a pointer to the original
7813 parent of the declaration DIE. Returns NULL otherwise. */
7815 static dw_die_ref
7816 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
7818 dw_die_ref decl;
7819 dw_die_ref new_decl;
7820 dw_die_ref orig_parent = NULL;
7822 decl = get_AT_ref (die, DW_AT_specification);
7823 if (decl == NULL)
7824 decl = die;
7825 else
7827 unsigned ix;
7828 dw_die_ref c;
7829 dw_attr_node *a;
7831 /* The original DIE will be changed to a declaration, and must
7832 be moved to be a child of the original declaration DIE. */
7833 orig_parent = decl->die_parent;
7835 /* Copy the type node pointer from the new DIE to the original
7836 declaration DIE so we can forward references later. */
7837 decl->comdat_type_p = true;
7838 decl->die_id.die_type_node = die->die_id.die_type_node;
7840 remove_AT (die, DW_AT_specification);
7842 FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
7844 if (a->dw_attr != DW_AT_name
7845 && a->dw_attr != DW_AT_declaration
7846 && a->dw_attr != DW_AT_external)
7847 add_dwarf_attr (die, a);
7850 FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree (c)));
7853 if (decl->die_parent != NULL
7854 && !is_unit_die (decl->die_parent))
7856 new_decl = copy_ancestor_tree (unit, decl, NULL);
7857 if (new_decl != NULL)
7859 remove_AT (new_decl, DW_AT_signature);
7860 add_AT_specification (die, new_decl);
7864 return orig_parent;
7867 /* Generate the skeleton ancestor tree for the given NODE, then clone
7868 the DIE and add the clone into the tree. */
7870 static void
7871 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
7873 if (node->new_die != NULL)
7874 return;
7876 node->new_die = clone_as_declaration (node->old_die);
7878 if (node->parent != NULL)
7880 generate_skeleton_ancestor_tree (node->parent);
7881 add_child_die (node->parent->new_die, node->new_die);
7885 /* Generate a skeleton tree of DIEs containing any declarations that are
7886 found in the original tree. We traverse the tree looking for declaration
7887 DIEs, and construct the skeleton from the bottom up whenever we find one. */
7889 static void
7890 generate_skeleton_bottom_up (skeleton_chain_node *parent)
7892 skeleton_chain_node node;
7893 dw_die_ref c;
7894 dw_die_ref first;
7895 dw_die_ref prev = NULL;
7896 dw_die_ref next = NULL;
7898 node.parent = parent;
7900 first = c = parent->old_die->die_child;
7901 if (c)
7902 next = c->die_sib;
7903 if (c) do {
7904 if (prev == NULL || prev->die_sib == c)
7905 prev = c;
7906 c = next;
7907 next = (c == first ? NULL : c->die_sib);
7908 node.old_die = c;
7909 node.new_die = NULL;
7910 if (is_declaration_die (c))
7912 if (is_template_instantiation (c))
7914 /* Instantiated templates do not need to be cloned into the
7915 type unit. Just move the DIE and its children back to
7916 the skeleton tree (in the main CU). */
7917 remove_child_with_prev (c, prev);
7918 add_child_die (parent->new_die, c);
7919 c = prev;
7921 else
7923 /* Clone the existing DIE, move the original to the skeleton
7924 tree (which is in the main CU), and put the clone, with
7925 all the original's children, where the original came from
7926 (which is about to be moved to the type unit). */
7927 dw_die_ref clone = clone_die (c);
7928 move_all_children (c, clone);
7930 /* If the original has a DW_AT_object_pointer attribute,
7931 it would now point to a child DIE just moved to the
7932 cloned tree, so we need to remove that attribute from
7933 the original. */
7934 remove_AT (c, DW_AT_object_pointer);
7936 replace_child (c, clone, prev);
7937 generate_skeleton_ancestor_tree (parent);
7938 add_child_die (parent->new_die, c);
7939 node.new_die = c;
7940 c = clone;
7943 generate_skeleton_bottom_up (&node);
7944 } while (next != NULL);
7947 /* Wrapper function for generate_skeleton_bottom_up. */
7949 static dw_die_ref
7950 generate_skeleton (dw_die_ref die)
7952 skeleton_chain_node node;
7954 node.old_die = die;
7955 node.new_die = NULL;
7956 node.parent = NULL;
7958 /* If this type definition is nested inside another type,
7959 and is not an instantiation of a template, always leave
7960 at least a declaration in its place. */
7961 if (die->die_parent != NULL
7962 && is_type_die (die->die_parent)
7963 && !is_template_instantiation (die))
7964 node.new_die = clone_as_declaration (die);
7966 generate_skeleton_bottom_up (&node);
7967 return node.new_die;
7970 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
7971 declaration. The original DIE is moved to a new compile unit so that
7972 existing references to it follow it to the new location. If any of the
7973 original DIE's descendants is a declaration, we need to replace the
7974 original DIE with a skeleton tree and move the declarations back into the
7975 skeleton tree. */
7977 static dw_die_ref
7978 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
7979 dw_die_ref prev)
7981 dw_die_ref skeleton, orig_parent;
7983 /* Copy the declaration context to the type unit DIE. If the returned
7984 ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
7985 that DIE. */
7986 orig_parent = copy_declaration_context (unit, child);
7988 skeleton = generate_skeleton (child);
7989 if (skeleton == NULL)
7990 remove_child_with_prev (child, prev);
7991 else
7993 skeleton->comdat_type_p = true;
7994 skeleton->die_id.die_type_node = child->die_id.die_type_node;
7996 /* If the original DIE was a specification, we need to put
7997 the skeleton under the parent DIE of the declaration.
7998 This leaves the original declaration in the tree, but
7999 it will be pruned later since there are no longer any
8000 references to it. */
8001 if (orig_parent != NULL)
8003 remove_child_with_prev (child, prev);
8004 add_child_die (orig_parent, skeleton);
8006 else
8007 replace_child (child, skeleton, prev);
8010 return skeleton;
8013 static void
8014 copy_dwarf_procs_ref_in_attrs (dw_die_ref die,
8015 comdat_type_node *type_node,
8016 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs);
8018 /* Helper for copy_dwarf_procs_ref_in_dies. Make a copy of the DIE DWARF
8019 procedure, put it under TYPE_NODE and return the copy. Continue looking for
8020 DWARF procedure references in the DW_AT_location attribute. */
8022 static dw_die_ref
8023 copy_dwarf_procedure (dw_die_ref die,
8024 comdat_type_node *type_node,
8025 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8027 gcc_assert (die->die_tag == DW_TAG_dwarf_procedure);
8029 /* DWARF procedures are not supposed to have children... */
8030 gcc_assert (die->die_child == NULL);
8032 /* ... and they are supposed to have only one attribute: DW_AT_location. */
8033 gcc_assert (vec_safe_length (die->die_attr) == 1
8034 && ((*die->die_attr)[0].dw_attr == DW_AT_location));
8036 /* Do not copy more than once DWARF procedures. */
8037 bool existed;
8038 dw_die_ref &die_copy = copied_dwarf_procs.get_or_insert (die, &existed);
8039 if (existed)
8040 return die_copy;
8042 die_copy = clone_die (die);
8043 add_child_die (type_node->root_die, die_copy);
8044 copy_dwarf_procs_ref_in_attrs (die_copy, type_node, copied_dwarf_procs);
8045 return die_copy;
8048 /* Helper for copy_dwarf_procs_ref_in_dies. Look for references to DWARF
8049 procedures in DIE's attributes. */
8051 static void
8052 copy_dwarf_procs_ref_in_attrs (dw_die_ref die,
8053 comdat_type_node *type_node,
8054 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8056 dw_attr_node *a;
8057 unsigned i;
8059 FOR_EACH_VEC_SAFE_ELT (die->die_attr, i, a)
8061 dw_loc_descr_ref loc;
8063 if (a->dw_attr_val.val_class != dw_val_class_loc)
8064 continue;
8066 for (loc = a->dw_attr_val.v.val_loc; loc != NULL; loc = loc->dw_loc_next)
8068 switch (loc->dw_loc_opc)
8070 case DW_OP_call2:
8071 case DW_OP_call4:
8072 case DW_OP_call_ref:
8073 gcc_assert (loc->dw_loc_oprnd1.val_class
8074 == dw_val_class_die_ref);
8075 loc->dw_loc_oprnd1.v.val_die_ref.die
8076 = copy_dwarf_procedure (loc->dw_loc_oprnd1.v.val_die_ref.die,
8077 type_node,
8078 copied_dwarf_procs);
8080 default:
8081 break;
8087 /* Copy DWARF procedures that are referenced by the DIE tree to TREE_NODE and
8088 rewrite references to point to the copies.
8090 References are looked for in DIE's attributes and recursively in all its
8091 children attributes that are location descriptions. COPIED_DWARF_PROCS is a
8092 mapping from old DWARF procedures to their copy. It is used not to copy
8093 twice the same DWARF procedure under TYPE_NODE. */
8095 static void
8096 copy_dwarf_procs_ref_in_dies (dw_die_ref die,
8097 comdat_type_node *type_node,
8098 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8100 dw_die_ref c;
8102 copy_dwarf_procs_ref_in_attrs (die, type_node, copied_dwarf_procs);
8103 FOR_EACH_CHILD (die, c, copy_dwarf_procs_ref_in_dies (c,
8104 type_node,
8105 copied_dwarf_procs));
8108 /* Traverse the DIE and set up additional .debug_types or .debug_info
8109 DW_UT_*type sections for each type worthy of being placed in a COMDAT
8110 section. */
8112 static void
8113 break_out_comdat_types (dw_die_ref die)
8115 dw_die_ref c;
8116 dw_die_ref first;
8117 dw_die_ref prev = NULL;
8118 dw_die_ref next = NULL;
8119 dw_die_ref unit = NULL;
8121 first = c = die->die_child;
8122 if (c)
8123 next = c->die_sib;
8124 if (c) do {
8125 if (prev == NULL || prev->die_sib == c)
8126 prev = c;
8127 c = next;
8128 next = (c == first ? NULL : c->die_sib);
8129 if (should_move_die_to_comdat (c))
8131 dw_die_ref replacement;
8132 comdat_type_node *type_node;
8134 /* Break out nested types into their own type units. */
8135 break_out_comdat_types (c);
8137 /* Create a new type unit DIE as the root for the new tree, and
8138 add it to the list of comdat types. */
8139 unit = new_die (DW_TAG_type_unit, NULL, NULL);
8140 add_AT_unsigned (unit, DW_AT_language,
8141 get_AT_unsigned (comp_unit_die (), DW_AT_language));
8142 type_node = ggc_cleared_alloc<comdat_type_node> ();
8143 type_node->root_die = unit;
8144 type_node->next = comdat_type_list;
8145 comdat_type_list = type_node;
8147 /* Generate the type signature. */
8148 generate_type_signature (c, type_node);
8150 /* Copy the declaration context, attributes, and children of the
8151 declaration into the new type unit DIE, then remove this DIE
8152 from the main CU (or replace it with a skeleton if necessary). */
8153 replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
8154 type_node->skeleton_die = replacement;
8156 /* Add the DIE to the new compunit. */
8157 add_child_die (unit, c);
8159 /* Types can reference DWARF procedures for type size or data location
8160 expressions. Calls in DWARF expressions cannot target procedures
8161 that are not in the same section. So we must copy DWARF procedures
8162 along with this type and then rewrite references to them. */
8163 hash_map<dw_die_ref, dw_die_ref> copied_dwarf_procs;
8164 copy_dwarf_procs_ref_in_dies (c, type_node, copied_dwarf_procs);
8166 if (replacement != NULL)
8167 c = replacement;
8169 else if (c->die_tag == DW_TAG_namespace
8170 || c->die_tag == DW_TAG_class_type
8171 || c->die_tag == DW_TAG_structure_type
8172 || c->die_tag == DW_TAG_union_type)
8174 /* Look for nested types that can be broken out. */
8175 break_out_comdat_types (c);
8177 } while (next != NULL);
8180 /* Like clone_tree, but copy DW_TAG_subprogram DIEs as declarations.
8181 Enter all the cloned children into the hash table decl_table. */
8183 static dw_die_ref
8184 clone_tree_partial (dw_die_ref die, decl_hash_type *decl_table)
8186 dw_die_ref c;
8187 dw_die_ref clone;
8188 struct decl_table_entry *entry;
8189 decl_table_entry **slot;
8191 if (die->die_tag == DW_TAG_subprogram)
8192 clone = clone_as_declaration (die);
8193 else
8194 clone = clone_die (die);
8196 slot = decl_table->find_slot_with_hash (die,
8197 htab_hash_pointer (die), INSERT);
8199 /* Assert that DIE isn't in the hash table yet. If it would be there
8200 before, the ancestors would be necessarily there as well, therefore
8201 clone_tree_partial wouldn't be called. */
8202 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
8204 entry = XCNEW (struct decl_table_entry);
8205 entry->orig = die;
8206 entry->copy = clone;
8207 *slot = entry;
8209 if (die->die_tag != DW_TAG_subprogram)
8210 FOR_EACH_CHILD (die, c,
8211 add_child_die (clone, clone_tree_partial (c, decl_table)));
8213 return clone;
8216 /* Walk the DIE and its children, looking for references to incomplete
8217 or trivial types that are unmarked (i.e., that are not in the current
8218 type_unit). */
8220 static void
8221 copy_decls_walk (dw_die_ref unit, dw_die_ref die, decl_hash_type *decl_table)
8223 dw_die_ref c;
8224 dw_attr_node *a;
8225 unsigned ix;
8227 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8229 if (AT_class (a) == dw_val_class_die_ref)
8231 dw_die_ref targ = AT_ref (a);
8232 decl_table_entry **slot;
8233 struct decl_table_entry *entry;
8235 if (targ->die_mark != 0 || targ->comdat_type_p)
8236 continue;
8238 slot = decl_table->find_slot_with_hash (targ,
8239 htab_hash_pointer (targ),
8240 INSERT);
8242 if (*slot != HTAB_EMPTY_ENTRY)
8244 /* TARG has already been copied, so we just need to
8245 modify the reference to point to the copy. */
8246 entry = *slot;
8247 a->dw_attr_val.v.val_die_ref.die = entry->copy;
8249 else
8251 dw_die_ref parent = unit;
8252 dw_die_ref copy = clone_die (targ);
8254 /* Record in DECL_TABLE that TARG has been copied.
8255 Need to do this now, before the recursive call,
8256 because DECL_TABLE may be expanded and SLOT
8257 would no longer be a valid pointer. */
8258 entry = XCNEW (struct decl_table_entry);
8259 entry->orig = targ;
8260 entry->copy = copy;
8261 *slot = entry;
8263 /* If TARG is not a declaration DIE, we need to copy its
8264 children. */
8265 if (!is_declaration_die (targ))
8267 FOR_EACH_CHILD (
8268 targ, c,
8269 add_child_die (copy,
8270 clone_tree_partial (c, decl_table)));
8273 /* Make sure the cloned tree is marked as part of the
8274 type unit. */
8275 mark_dies (copy);
8277 /* If TARG has surrounding context, copy its ancestor tree
8278 into the new type unit. */
8279 if (targ->die_parent != NULL
8280 && !is_unit_die (targ->die_parent))
8281 parent = copy_ancestor_tree (unit, targ->die_parent,
8282 decl_table);
8284 add_child_die (parent, copy);
8285 a->dw_attr_val.v.val_die_ref.die = copy;
8287 /* Make sure the newly-copied DIE is walked. If it was
8288 installed in a previously-added context, it won't
8289 get visited otherwise. */
8290 if (parent != unit)
8292 /* Find the highest point of the newly-added tree,
8293 mark each node along the way, and walk from there. */
8294 parent->die_mark = 1;
8295 while (parent->die_parent
8296 && parent->die_parent->die_mark == 0)
8298 parent = parent->die_parent;
8299 parent->die_mark = 1;
8301 copy_decls_walk (unit, parent, decl_table);
8307 FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
8310 /* Copy declarations for "unworthy" types into the new comdat section.
8311 Incomplete types, modified types, and certain other types aren't broken
8312 out into comdat sections of their own, so they don't have a signature,
8313 and we need to copy the declaration into the same section so that we
8314 don't have an external reference. */
8316 static void
8317 copy_decls_for_unworthy_types (dw_die_ref unit)
8319 mark_dies (unit);
8320 decl_hash_type decl_table (10);
8321 copy_decls_walk (unit, unit, &decl_table);
8322 unmark_dies (unit);
8325 /* Traverse the DIE and add a sibling attribute if it may have the
8326 effect of speeding up access to siblings. To save some space,
8327 avoid generating sibling attributes for DIE's without children. */
8329 static void
8330 add_sibling_attributes (dw_die_ref die)
8332 dw_die_ref c;
8334 if (! die->die_child)
8335 return;
8337 if (die->die_parent && die != die->die_parent->die_child)
8338 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
8340 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
8343 /* Output all location lists for the DIE and its children. */
8345 static void
8346 output_location_lists (dw_die_ref die)
8348 dw_die_ref c;
8349 dw_attr_node *a;
8350 unsigned ix;
8352 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8353 if (AT_class (a) == dw_val_class_loc_list)
8354 output_loc_list (AT_loc_list (a));
8356 FOR_EACH_CHILD (die, c, output_location_lists (c));
8359 /* During assign_location_list_indexes and output_loclists_offset the
8360 current index, after it the number of assigned indexes (i.e. how
8361 large the .debug_loclists* offset table should be). */
8362 static unsigned int loc_list_idx;
8364 /* Output all location list offsets for the DIE and its children. */
8366 static void
8367 output_loclists_offsets (dw_die_ref die)
8369 dw_die_ref c;
8370 dw_attr_node *a;
8371 unsigned ix;
8373 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8374 if (AT_class (a) == dw_val_class_loc_list)
8376 dw_loc_list_ref l = AT_loc_list (a);
8377 if (l->offset_emitted)
8378 continue;
8379 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l->ll_symbol,
8380 loc_section_label, NULL);
8381 gcc_assert (l->hash == loc_list_idx);
8382 loc_list_idx++;
8383 l->offset_emitted = true;
8386 FOR_EACH_CHILD (die, c, output_loclists_offsets (c));
8389 /* Recursively set indexes of location lists. */
8391 static void
8392 assign_location_list_indexes (dw_die_ref die)
8394 dw_die_ref c;
8395 dw_attr_node *a;
8396 unsigned ix;
8398 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8399 if (AT_class (a) == dw_val_class_loc_list)
8401 dw_loc_list_ref list = AT_loc_list (a);
8402 if (!list->num_assigned)
8404 list->num_assigned = true;
8405 list->hash = loc_list_idx++;
8409 FOR_EACH_CHILD (die, c, assign_location_list_indexes (c));
8412 /* We want to limit the number of external references, because they are
8413 larger than local references: a relocation takes multiple words, and
8414 even a sig8 reference is always eight bytes, whereas a local reference
8415 can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
8416 So if we encounter multiple external references to the same type DIE, we
8417 make a local typedef stub for it and redirect all references there.
8419 This is the element of the hash table for keeping track of these
8420 references. */
8422 struct external_ref
8424 dw_die_ref type;
8425 dw_die_ref stub;
8426 unsigned n_refs;
8429 /* Hashtable helpers. */
8431 struct external_ref_hasher : free_ptr_hash <external_ref>
8433 static inline hashval_t hash (const external_ref *);
8434 static inline bool equal (const external_ref *, const external_ref *);
8437 inline hashval_t
8438 external_ref_hasher::hash (const external_ref *r)
8440 dw_die_ref die = r->type;
8441 hashval_t h = 0;
8443 /* We can't use the address of the DIE for hashing, because
8444 that will make the order of the stub DIEs non-deterministic. */
8445 if (! die->comdat_type_p)
8446 /* We have a symbol; use it to compute a hash. */
8447 h = htab_hash_string (die->die_id.die_symbol);
8448 else
8450 /* We have a type signature; use a subset of the bits as the hash.
8451 The 8-byte signature is at least as large as hashval_t. */
8452 comdat_type_node *type_node = die->die_id.die_type_node;
8453 memcpy (&h, type_node->signature, sizeof (h));
8455 return h;
8458 inline bool
8459 external_ref_hasher::equal (const external_ref *r1, const external_ref *r2)
8461 return r1->type == r2->type;
8464 typedef hash_table<external_ref_hasher> external_ref_hash_type;
8466 /* Return a pointer to the external_ref for references to DIE. */
8468 static struct external_ref *
8469 lookup_external_ref (external_ref_hash_type *map, dw_die_ref die)
8471 struct external_ref ref, *ref_p;
8472 external_ref **slot;
8474 ref.type = die;
8475 slot = map->find_slot (&ref, INSERT);
8476 if (*slot != HTAB_EMPTY_ENTRY)
8477 return *slot;
8479 ref_p = XCNEW (struct external_ref);
8480 ref_p->type = die;
8481 *slot = ref_p;
8482 return ref_p;
8485 /* Subroutine of optimize_external_refs, below.
8487 If we see a type skeleton, record it as our stub. If we see external
8488 references, remember how many we've seen. */
8490 static void
8491 optimize_external_refs_1 (dw_die_ref die, external_ref_hash_type *map)
8493 dw_die_ref c;
8494 dw_attr_node *a;
8495 unsigned ix;
8496 struct external_ref *ref_p;
8498 if (is_type_die (die)
8499 && (c = get_AT_ref (die, DW_AT_signature)))
8501 /* This is a local skeleton; use it for local references. */
8502 ref_p = lookup_external_ref (map, c);
8503 ref_p->stub = die;
8506 /* Scan the DIE references, and remember any that refer to DIEs from
8507 other CUs (i.e. those which are not marked). */
8508 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8509 if (AT_class (a) == dw_val_class_die_ref
8510 && (c = AT_ref (a))->die_mark == 0
8511 && is_type_die (c))
8513 ref_p = lookup_external_ref (map, c);
8514 ref_p->n_refs++;
8517 FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
8520 /* htab_traverse callback function for optimize_external_refs, below. SLOT
8521 points to an external_ref, DATA is the CU we're processing. If we don't
8522 already have a local stub, and we have multiple refs, build a stub. */
8525 dwarf2_build_local_stub (external_ref **slot, dw_die_ref data)
8527 struct external_ref *ref_p = *slot;
8529 if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
8531 /* We have multiple references to this type, so build a small stub.
8532 Both of these forms are a bit dodgy from the perspective of the
8533 DWARF standard, since technically they should have names. */
8534 dw_die_ref cu = data;
8535 dw_die_ref type = ref_p->type;
8536 dw_die_ref stub = NULL;
8538 if (type->comdat_type_p)
8540 /* If we refer to this type via sig8, use AT_signature. */
8541 stub = new_die (type->die_tag, cu, NULL_TREE);
8542 add_AT_die_ref (stub, DW_AT_signature, type);
8544 else
8546 /* Otherwise, use a typedef with no name. */
8547 stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
8548 add_AT_die_ref (stub, DW_AT_type, type);
8551 stub->die_mark++;
8552 ref_p->stub = stub;
8554 return 1;
8557 /* DIE is a unit; look through all the DIE references to see if there are
8558 any external references to types, and if so, create local stubs for
8559 them which will be applied in build_abbrev_table. This is useful because
8560 references to local DIEs are smaller. */
8562 static external_ref_hash_type *
8563 optimize_external_refs (dw_die_ref die)
8565 external_ref_hash_type *map = new external_ref_hash_type (10);
8566 optimize_external_refs_1 (die, map);
8567 map->traverse <dw_die_ref, dwarf2_build_local_stub> (die);
8568 return map;
8571 /* The following 3 variables are temporaries that are computed only during the
8572 build_abbrev_table call and used and released during the following
8573 optimize_abbrev_table call. */
8575 /* First abbrev_id that can be optimized based on usage. */
8576 static unsigned int abbrev_opt_start;
8578 /* Maximum abbrev_id of a base type plus one (we can't optimize DIEs with
8579 abbrev_id smaller than this, because they must be already sized
8580 during build_abbrev_table). */
8581 static unsigned int abbrev_opt_base_type_end;
8583 /* Vector of usage counts during build_abbrev_table. Indexed by
8584 abbrev_id - abbrev_opt_start. */
8585 static vec<unsigned int> abbrev_usage_count;
8587 /* Vector of all DIEs added with die_abbrev >= abbrev_opt_start. */
8588 static vec<dw_die_ref> sorted_abbrev_dies;
8590 /* The format of each DIE (and its attribute value pairs) is encoded in an
8591 abbreviation table. This routine builds the abbreviation table and assigns
8592 a unique abbreviation id for each abbreviation entry. The children of each
8593 die are visited recursively. */
8595 static void
8596 build_abbrev_table (dw_die_ref die, external_ref_hash_type *extern_map)
8598 unsigned int abbrev_id = 0;
8599 dw_die_ref c;
8600 dw_attr_node *a;
8601 unsigned ix;
8602 dw_die_ref abbrev;
8604 /* Scan the DIE references, and replace any that refer to
8605 DIEs from other CUs (i.e. those which are not marked) with
8606 the local stubs we built in optimize_external_refs. */
8607 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8608 if (AT_class (a) == dw_val_class_die_ref
8609 && (c = AT_ref (a))->die_mark == 0)
8611 struct external_ref *ref_p;
8612 gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
8614 ref_p = lookup_external_ref (extern_map, c);
8615 if (ref_p->stub && ref_p->stub != die)
8616 change_AT_die_ref (a, ref_p->stub);
8617 else
8618 /* We aren't changing this reference, so mark it external. */
8619 set_AT_ref_external (a, 1);
8622 FOR_EACH_VEC_SAFE_ELT (abbrev_die_table, abbrev_id, abbrev)
8624 dw_attr_node *die_a, *abbrev_a;
8625 unsigned ix;
8626 bool ok = true;
8628 if (abbrev_id == 0)
8629 continue;
8630 if (abbrev->die_tag != die->die_tag)
8631 continue;
8632 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
8633 continue;
8635 if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
8636 continue;
8638 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
8640 abbrev_a = &(*abbrev->die_attr)[ix];
8641 if ((abbrev_a->dw_attr != die_a->dw_attr)
8642 || (value_format (abbrev_a) != value_format (die_a)))
8644 ok = false;
8645 break;
8648 if (ok)
8649 break;
8652 if (abbrev_id >= vec_safe_length (abbrev_die_table))
8654 vec_safe_push (abbrev_die_table, die);
8655 if (abbrev_opt_start)
8656 abbrev_usage_count.safe_push (0);
8658 if (abbrev_opt_start && abbrev_id >= abbrev_opt_start)
8660 abbrev_usage_count[abbrev_id - abbrev_opt_start]++;
8661 sorted_abbrev_dies.safe_push (die);
8664 die->die_abbrev = abbrev_id;
8665 FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
8668 /* Callback function for sorted_abbrev_dies vector sorting. We sort
8669 by die_abbrev's usage count, from the most commonly used
8670 abbreviation to the least. */
8672 static int
8673 die_abbrev_cmp (const void *p1, const void *p2)
8675 dw_die_ref die1 = *(const dw_die_ref *) p1;
8676 dw_die_ref die2 = *(const dw_die_ref *) p2;
8678 gcc_checking_assert (die1->die_abbrev >= abbrev_opt_start);
8679 gcc_checking_assert (die2->die_abbrev >= abbrev_opt_start);
8681 if (die1->die_abbrev >= abbrev_opt_base_type_end
8682 && die2->die_abbrev >= abbrev_opt_base_type_end)
8684 if (abbrev_usage_count[die1->die_abbrev - abbrev_opt_start]
8685 > abbrev_usage_count[die2->die_abbrev - abbrev_opt_start])
8686 return -1;
8687 if (abbrev_usage_count[die1->die_abbrev - abbrev_opt_start]
8688 < abbrev_usage_count[die2->die_abbrev - abbrev_opt_start])
8689 return 1;
8692 /* Stabilize the sort. */
8693 if (die1->die_abbrev < die2->die_abbrev)
8694 return -1;
8695 if (die1->die_abbrev > die2->die_abbrev)
8696 return 1;
8698 return 0;
8701 /* Convert dw_val_class_const and dw_val_class_unsigned_const class attributes
8702 of DIEs in between sorted_abbrev_dies[first_id] and abbrev_dies[end_id - 1]
8703 into dw_val_class_const_implicit or
8704 dw_val_class_unsigned_const_implicit. */
8706 static void
8707 optimize_implicit_const (unsigned int first_id, unsigned int end,
8708 vec<bool> &implicit_consts)
8710 /* It never makes sense if there is just one DIE using the abbreviation. */
8711 if (end < first_id + 2)
8712 return;
8714 dw_attr_node *a;
8715 unsigned ix, i;
8716 dw_die_ref die = sorted_abbrev_dies[first_id];
8717 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8718 if (implicit_consts[ix])
8720 enum dw_val_class new_class = dw_val_class_none;
8721 switch (AT_class (a))
8723 case dw_val_class_unsigned_const:
8724 if ((HOST_WIDE_INT) AT_unsigned (a) < 0)
8725 continue;
8727 /* The .debug_abbrev section will grow by
8728 size_of_sleb128 (AT_unsigned (a)) and we avoid the constants
8729 in all the DIEs using that abbreviation. */
8730 if (constant_size (AT_unsigned (a)) * (end - first_id)
8731 <= (unsigned) size_of_sleb128 (AT_unsigned (a)))
8732 continue;
8734 new_class = dw_val_class_unsigned_const_implicit;
8735 break;
8737 case dw_val_class_const:
8738 new_class = dw_val_class_const_implicit;
8739 break;
8741 case dw_val_class_file:
8742 new_class = dw_val_class_file_implicit;
8743 break;
8745 default:
8746 continue;
8748 for (i = first_id; i < end; i++)
8749 (*sorted_abbrev_dies[i]->die_attr)[ix].dw_attr_val.val_class
8750 = new_class;
8754 /* Attempt to optimize abbreviation table from abbrev_opt_start
8755 abbreviation above. */
8757 static void
8758 optimize_abbrev_table (void)
8760 if (abbrev_opt_start
8761 && vec_safe_length (abbrev_die_table) > abbrev_opt_start
8762 && (dwarf_version >= 5 || vec_safe_length (abbrev_die_table) > 127))
8764 auto_vec<bool, 32> implicit_consts;
8765 sorted_abbrev_dies.qsort (die_abbrev_cmp);
8767 unsigned int abbrev_id = abbrev_opt_start - 1;
8768 unsigned int first_id = ~0U;
8769 unsigned int last_abbrev_id = 0;
8770 unsigned int i;
8771 dw_die_ref die;
8772 if (abbrev_opt_base_type_end > abbrev_opt_start)
8773 abbrev_id = abbrev_opt_base_type_end - 1;
8774 /* Reassign abbreviation ids from abbrev_opt_start above, so that
8775 most commonly used abbreviations come first. */
8776 FOR_EACH_VEC_ELT (sorted_abbrev_dies, i, die)
8778 dw_attr_node *a;
8779 unsigned ix;
8781 /* If calc_base_type_die_sizes has been called, the CU and
8782 base types after it can't be optimized, because we've already
8783 calculated their DIE offsets. We've sorted them first. */
8784 if (die->die_abbrev < abbrev_opt_base_type_end)
8785 continue;
8786 if (die->die_abbrev != last_abbrev_id)
8788 last_abbrev_id = die->die_abbrev;
8789 if (dwarf_version >= 5 && first_id != ~0U)
8790 optimize_implicit_const (first_id, i, implicit_consts);
8791 abbrev_id++;
8792 (*abbrev_die_table)[abbrev_id] = die;
8793 if (dwarf_version >= 5)
8795 first_id = i;
8796 implicit_consts.truncate (0);
8798 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8799 switch (AT_class (a))
8801 case dw_val_class_const:
8802 case dw_val_class_unsigned_const:
8803 case dw_val_class_file:
8804 implicit_consts.safe_push (true);
8805 break;
8806 default:
8807 implicit_consts.safe_push (false);
8808 break;
8812 else if (dwarf_version >= 5)
8814 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8815 if (!implicit_consts[ix])
8816 continue;
8817 else
8819 dw_attr_node *other_a
8820 = &(*(*abbrev_die_table)[abbrev_id]->die_attr)[ix];
8821 if (!dw_val_equal_p (&a->dw_attr_val,
8822 &other_a->dw_attr_val))
8823 implicit_consts[ix] = false;
8826 die->die_abbrev = abbrev_id;
8828 gcc_assert (abbrev_id == vec_safe_length (abbrev_die_table) - 1);
8829 if (dwarf_version >= 5 && first_id != ~0U)
8830 optimize_implicit_const (first_id, i, implicit_consts);
8833 abbrev_opt_start = 0;
8834 abbrev_opt_base_type_end = 0;
8835 abbrev_usage_count.release ();
8836 sorted_abbrev_dies.release ();
8839 /* Return the power-of-two number of bytes necessary to represent VALUE. */
8841 static int
8842 constant_size (unsigned HOST_WIDE_INT value)
8844 int log;
8846 if (value == 0)
8847 log = 0;
8848 else
8849 log = floor_log2 (value);
8851 log = log / 8;
8852 log = 1 << (floor_log2 (log) + 1);
8854 return log;
8857 /* Return the size of a DIE as it is represented in the
8858 .debug_info section. */
8860 static unsigned long
8861 size_of_die (dw_die_ref die)
8863 unsigned long size = 0;
8864 dw_attr_node *a;
8865 unsigned ix;
8866 enum dwarf_form form;
8868 size += size_of_uleb128 (die->die_abbrev);
8869 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8871 switch (AT_class (a))
8873 case dw_val_class_addr:
8874 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8876 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8877 size += size_of_uleb128 (AT_index (a));
8879 else
8880 size += DWARF2_ADDR_SIZE;
8881 break;
8882 case dw_val_class_offset:
8883 size += DWARF_OFFSET_SIZE;
8884 break;
8885 case dw_val_class_loc:
8887 unsigned long lsize = size_of_locs (AT_loc (a));
8889 /* Block length. */
8890 if (dwarf_version >= 4)
8891 size += size_of_uleb128 (lsize);
8892 else
8893 size += constant_size (lsize);
8894 size += lsize;
8896 break;
8897 case dw_val_class_loc_list:
8898 if (dwarf_split_debug_info && dwarf_version >= 5)
8900 gcc_assert (AT_loc_list (a)->num_assigned);
8901 size += size_of_uleb128 (AT_loc_list (a)->hash);
8903 else
8904 size += DWARF_OFFSET_SIZE;
8905 break;
8906 case dw_val_class_range_list:
8907 if (value_format (a) == DW_FORM_rnglistx)
8909 gcc_assert (rnglist_idx);
8910 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
8911 size += size_of_uleb128 (r->idx);
8913 else
8914 size += DWARF_OFFSET_SIZE;
8915 break;
8916 case dw_val_class_const:
8917 size += size_of_sleb128 (AT_int (a));
8918 break;
8919 case dw_val_class_unsigned_const:
8921 int csize = constant_size (AT_unsigned (a));
8922 if (dwarf_version == 3
8923 && a->dw_attr == DW_AT_data_member_location
8924 && csize >= 4)
8925 size += size_of_uleb128 (AT_unsigned (a));
8926 else
8927 size += csize;
8929 break;
8930 case dw_val_class_const_implicit:
8931 case dw_val_class_unsigned_const_implicit:
8932 case dw_val_class_file_implicit:
8933 /* These occupy no size in the DIE, just an extra sleb128 in
8934 .debug_abbrev. */
8935 break;
8936 case dw_val_class_const_double:
8937 size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
8938 if (HOST_BITS_PER_WIDE_INT >= DWARF_LARGEST_DATA_FORM_BITS)
8939 size++; /* block */
8940 break;
8941 case dw_val_class_wide_int:
8942 size += (get_full_len (*a->dw_attr_val.v.val_wide)
8943 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
8944 if (get_full_len (*a->dw_attr_val.v.val_wide)
8945 * HOST_BITS_PER_WIDE_INT > DWARF_LARGEST_DATA_FORM_BITS)
8946 size++; /* block */
8947 break;
8948 case dw_val_class_vec:
8949 size += constant_size (a->dw_attr_val.v.val_vec.length
8950 * a->dw_attr_val.v.val_vec.elt_size)
8951 + a->dw_attr_val.v.val_vec.length
8952 * a->dw_attr_val.v.val_vec.elt_size; /* block */
8953 break;
8954 case dw_val_class_flag:
8955 if (dwarf_version >= 4)
8956 /* Currently all add_AT_flag calls pass in 1 as last argument,
8957 so DW_FORM_flag_present can be used. If that ever changes,
8958 we'll need to use DW_FORM_flag and have some optimization
8959 in build_abbrev_table that will change those to
8960 DW_FORM_flag_present if it is set to 1 in all DIEs using
8961 the same abbrev entry. */
8962 gcc_assert (a->dw_attr_val.v.val_flag == 1);
8963 else
8964 size += 1;
8965 break;
8966 case dw_val_class_die_ref:
8967 if (AT_ref_external (a))
8969 /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
8970 we use DW_FORM_ref_addr. In DWARF2, DW_FORM_ref_addr
8971 is sized by target address length, whereas in DWARF3
8972 it's always sized as an offset. */
8973 if (use_debug_types)
8974 size += DWARF_TYPE_SIGNATURE_SIZE;
8975 else if (dwarf_version == 2)
8976 size += DWARF2_ADDR_SIZE;
8977 else
8978 size += DWARF_OFFSET_SIZE;
8980 else
8981 size += DWARF_OFFSET_SIZE;
8982 break;
8983 case dw_val_class_fde_ref:
8984 size += DWARF_OFFSET_SIZE;
8985 break;
8986 case dw_val_class_lbl_id:
8987 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8989 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8990 size += size_of_uleb128 (AT_index (a));
8992 else
8993 size += DWARF2_ADDR_SIZE;
8994 break;
8995 case dw_val_class_lineptr:
8996 case dw_val_class_macptr:
8997 case dw_val_class_loclistsptr:
8998 size += DWARF_OFFSET_SIZE;
8999 break;
9000 case dw_val_class_str:
9001 form = AT_string_form (a);
9002 if (form == DW_FORM_strp || form == DW_FORM_line_strp)
9003 size += DWARF_OFFSET_SIZE;
9004 else if (form == DW_FORM_GNU_str_index)
9005 size += size_of_uleb128 (AT_index (a));
9006 else
9007 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
9008 break;
9009 case dw_val_class_file:
9010 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
9011 break;
9012 case dw_val_class_data8:
9013 size += 8;
9014 break;
9015 case dw_val_class_vms_delta:
9016 size += DWARF_OFFSET_SIZE;
9017 break;
9018 case dw_val_class_high_pc:
9019 size += DWARF2_ADDR_SIZE;
9020 break;
9021 case dw_val_class_discr_value:
9022 size += size_of_discr_value (&a->dw_attr_val.v.val_discr_value);
9023 break;
9024 case dw_val_class_discr_list:
9026 unsigned block_size = size_of_discr_list (AT_discr_list (a));
9028 /* This is a block, so we have the block length and then its
9029 data. */
9030 size += constant_size (block_size) + block_size;
9032 break;
9033 default:
9034 gcc_unreachable ();
9038 return size;
9041 /* Size the debugging information associated with a given DIE. Visits the
9042 DIE's children recursively. Updates the global variable next_die_offset, on
9043 each time through. Uses the current value of next_die_offset to update the
9044 die_offset field in each DIE. */
9046 static void
9047 calc_die_sizes (dw_die_ref die)
9049 dw_die_ref c;
9051 gcc_assert (die->die_offset == 0
9052 || (unsigned long int) die->die_offset == next_die_offset);
9053 die->die_offset = next_die_offset;
9054 next_die_offset += size_of_die (die);
9056 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
9058 if (die->die_child != NULL)
9059 /* Count the null byte used to terminate sibling lists. */
9060 next_die_offset += 1;
9063 /* Size just the base type children at the start of the CU.
9064 This is needed because build_abbrev needs to size locs
9065 and sizing of type based stack ops needs to know die_offset
9066 values for the base types. */
9068 static void
9069 calc_base_type_die_sizes (void)
9071 unsigned long die_offset = (dwarf_split_debug_info
9072 ? DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
9073 : DWARF_COMPILE_UNIT_HEADER_SIZE);
9074 unsigned int i;
9075 dw_die_ref base_type;
9076 #if ENABLE_ASSERT_CHECKING
9077 dw_die_ref prev = comp_unit_die ()->die_child;
9078 #endif
9080 die_offset += size_of_die (comp_unit_die ());
9081 for (i = 0; base_types.iterate (i, &base_type); i++)
9083 #if ENABLE_ASSERT_CHECKING
9084 gcc_assert (base_type->die_offset == 0
9085 && prev->die_sib == base_type
9086 && base_type->die_child == NULL
9087 && base_type->die_abbrev);
9088 prev = base_type;
9089 #endif
9090 if (abbrev_opt_start
9091 && base_type->die_abbrev >= abbrev_opt_base_type_end)
9092 abbrev_opt_base_type_end = base_type->die_abbrev + 1;
9093 base_type->die_offset = die_offset;
9094 die_offset += size_of_die (base_type);
9098 /* Set the marks for a die and its children. We do this so
9099 that we know whether or not a reference needs to use FORM_ref_addr; only
9100 DIEs in the same CU will be marked. We used to clear out the offset
9101 and use that as the flag, but ran into ordering problems. */
9103 static void
9104 mark_dies (dw_die_ref die)
9106 dw_die_ref c;
9108 gcc_assert (!die->die_mark);
9110 die->die_mark = 1;
9111 FOR_EACH_CHILD (die, c, mark_dies (c));
9114 /* Clear the marks for a die and its children. */
9116 static void
9117 unmark_dies (dw_die_ref die)
9119 dw_die_ref c;
9121 if (! use_debug_types)
9122 gcc_assert (die->die_mark);
9124 die->die_mark = 0;
9125 FOR_EACH_CHILD (die, c, unmark_dies (c));
9128 /* Clear the marks for a die, its children and referred dies. */
9130 static void
9131 unmark_all_dies (dw_die_ref die)
9133 dw_die_ref c;
9134 dw_attr_node *a;
9135 unsigned ix;
9137 if (!die->die_mark)
9138 return;
9139 die->die_mark = 0;
9141 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
9143 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9144 if (AT_class (a) == dw_val_class_die_ref)
9145 unmark_all_dies (AT_ref (a));
9148 /* Calculate if the entry should appear in the final output file. It may be
9149 from a pruned a type. */
9151 static bool
9152 include_pubname_in_output (vec<pubname_entry, va_gc> *table, pubname_entry *p)
9154 /* By limiting gnu pubnames to definitions only, gold can generate a
9155 gdb index without entries for declarations, which don't include
9156 enough information to be useful. */
9157 if (debug_generate_pub_sections == 2 && is_declaration_die (p->die))
9158 return false;
9160 if (table == pubname_table)
9162 /* Enumerator names are part of the pubname table, but the
9163 parent DW_TAG_enumeration_type die may have been pruned.
9164 Don't output them if that is the case. */
9165 if (p->die->die_tag == DW_TAG_enumerator &&
9166 (p->die->die_parent == NULL
9167 || !p->die->die_parent->die_perennial_p))
9168 return false;
9170 /* Everything else in the pubname table is included. */
9171 return true;
9174 /* The pubtypes table shouldn't include types that have been
9175 pruned. */
9176 return (p->die->die_offset != 0
9177 || !flag_eliminate_unused_debug_types);
9180 /* Return the size of the .debug_pubnames or .debug_pubtypes table
9181 generated for the compilation unit. */
9183 static unsigned long
9184 size_of_pubnames (vec<pubname_entry, va_gc> *names)
9186 unsigned long size;
9187 unsigned i;
9188 pubname_entry *p;
9189 int space_for_flags = (debug_generate_pub_sections == 2) ? 1 : 0;
9191 size = DWARF_PUBNAMES_HEADER_SIZE;
9192 FOR_EACH_VEC_ELT (*names, i, p)
9193 if (include_pubname_in_output (names, p))
9194 size += strlen (p->name) + DWARF_OFFSET_SIZE + 1 + space_for_flags;
9196 size += DWARF_OFFSET_SIZE;
9197 return size;
9200 /* Return the size of the information in the .debug_aranges section. */
9202 static unsigned long
9203 size_of_aranges (void)
9205 unsigned long size;
9207 size = DWARF_ARANGES_HEADER_SIZE;
9209 /* Count the address/length pair for this compilation unit. */
9210 if (text_section_used)
9211 size += 2 * DWARF2_ADDR_SIZE;
9212 if (cold_text_section_used)
9213 size += 2 * DWARF2_ADDR_SIZE;
9214 if (have_multiple_function_sections)
9216 unsigned fde_idx;
9217 dw_fde_ref fde;
9219 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
9221 if (DECL_IGNORED_P (fde->decl))
9222 continue;
9223 if (!fde->in_std_section)
9224 size += 2 * DWARF2_ADDR_SIZE;
9225 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
9226 size += 2 * DWARF2_ADDR_SIZE;
9230 /* Count the two zero words used to terminated the address range table. */
9231 size += 2 * DWARF2_ADDR_SIZE;
9232 return size;
9235 /* Select the encoding of an attribute value. */
9237 static enum dwarf_form
9238 value_format (dw_attr_node *a)
9240 switch (AT_class (a))
9242 case dw_val_class_addr:
9243 /* Only very few attributes allow DW_FORM_addr. */
9244 switch (a->dw_attr)
9246 case DW_AT_low_pc:
9247 case DW_AT_high_pc:
9248 case DW_AT_entry_pc:
9249 case DW_AT_trampoline:
9250 return (AT_index (a) == NOT_INDEXED
9251 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
9252 default:
9253 break;
9255 switch (DWARF2_ADDR_SIZE)
9257 case 1:
9258 return DW_FORM_data1;
9259 case 2:
9260 return DW_FORM_data2;
9261 case 4:
9262 return DW_FORM_data4;
9263 case 8:
9264 return DW_FORM_data8;
9265 default:
9266 gcc_unreachable ();
9268 case dw_val_class_loc_list:
9269 if (dwarf_split_debug_info
9270 && dwarf_version >= 5
9271 && AT_loc_list (a)->num_assigned)
9272 return DW_FORM_loclistx;
9273 /* FALLTHRU */
9274 case dw_val_class_range_list:
9275 /* For range lists in DWARF 5, use DW_FORM_rnglistx from .debug_info.dwo
9276 but in .debug_info use DW_FORM_sec_offset, which is shorter if we
9277 care about sizes of .debug* sections in shared libraries and
9278 executables and don't take into account relocations that affect just
9279 relocatable objects - for DW_FORM_rnglistx we'd have to emit offset
9280 table in the .debug_rnglists section. */
9281 if (dwarf_split_debug_info
9282 && dwarf_version >= 5
9283 && AT_class (a) == dw_val_class_range_list
9284 && rnglist_idx
9285 && a->dw_attr_val.val_entry != RELOCATED_OFFSET)
9286 return DW_FORM_rnglistx;
9287 if (dwarf_version >= 4)
9288 return DW_FORM_sec_offset;
9289 /* FALLTHRU */
9290 case dw_val_class_vms_delta:
9291 case dw_val_class_offset:
9292 switch (DWARF_OFFSET_SIZE)
9294 case 4:
9295 return DW_FORM_data4;
9296 case 8:
9297 return DW_FORM_data8;
9298 default:
9299 gcc_unreachable ();
9301 case dw_val_class_loc:
9302 if (dwarf_version >= 4)
9303 return DW_FORM_exprloc;
9304 switch (constant_size (size_of_locs (AT_loc (a))))
9306 case 1:
9307 return DW_FORM_block1;
9308 case 2:
9309 return DW_FORM_block2;
9310 case 4:
9311 return DW_FORM_block4;
9312 default:
9313 gcc_unreachable ();
9315 case dw_val_class_const:
9316 return DW_FORM_sdata;
9317 case dw_val_class_unsigned_const:
9318 switch (constant_size (AT_unsigned (a)))
9320 case 1:
9321 return DW_FORM_data1;
9322 case 2:
9323 return DW_FORM_data2;
9324 case 4:
9325 /* In DWARF3 DW_AT_data_member_location with
9326 DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
9327 constant, so we need to use DW_FORM_udata if we need
9328 a large constant. */
9329 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
9330 return DW_FORM_udata;
9331 return DW_FORM_data4;
9332 case 8:
9333 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
9334 return DW_FORM_udata;
9335 return DW_FORM_data8;
9336 default:
9337 gcc_unreachable ();
9339 case dw_val_class_const_implicit:
9340 case dw_val_class_unsigned_const_implicit:
9341 case dw_val_class_file_implicit:
9342 return DW_FORM_implicit_const;
9343 case dw_val_class_const_double:
9344 switch (HOST_BITS_PER_WIDE_INT)
9346 case 8:
9347 return DW_FORM_data2;
9348 case 16:
9349 return DW_FORM_data4;
9350 case 32:
9351 return DW_FORM_data8;
9352 case 64:
9353 if (dwarf_version >= 5)
9354 return DW_FORM_data16;
9355 /* FALLTHRU */
9356 default:
9357 return DW_FORM_block1;
9359 case dw_val_class_wide_int:
9360 switch (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT)
9362 case 8:
9363 return DW_FORM_data1;
9364 case 16:
9365 return DW_FORM_data2;
9366 case 32:
9367 return DW_FORM_data4;
9368 case 64:
9369 return DW_FORM_data8;
9370 case 128:
9371 if (dwarf_version >= 5)
9372 return DW_FORM_data16;
9373 /* FALLTHRU */
9374 default:
9375 return DW_FORM_block1;
9377 case dw_val_class_vec:
9378 switch (constant_size (a->dw_attr_val.v.val_vec.length
9379 * a->dw_attr_val.v.val_vec.elt_size))
9381 case 1:
9382 return DW_FORM_block1;
9383 case 2:
9384 return DW_FORM_block2;
9385 case 4:
9386 return DW_FORM_block4;
9387 default:
9388 gcc_unreachable ();
9390 case dw_val_class_flag:
9391 if (dwarf_version >= 4)
9393 /* Currently all add_AT_flag calls pass in 1 as last argument,
9394 so DW_FORM_flag_present can be used. If that ever changes,
9395 we'll need to use DW_FORM_flag and have some optimization
9396 in build_abbrev_table that will change those to
9397 DW_FORM_flag_present if it is set to 1 in all DIEs using
9398 the same abbrev entry. */
9399 gcc_assert (a->dw_attr_val.v.val_flag == 1);
9400 return DW_FORM_flag_present;
9402 return DW_FORM_flag;
9403 case dw_val_class_die_ref:
9404 if (AT_ref_external (a))
9405 return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
9406 else
9407 return DW_FORM_ref;
9408 case dw_val_class_fde_ref:
9409 return DW_FORM_data;
9410 case dw_val_class_lbl_id:
9411 return (AT_index (a) == NOT_INDEXED
9412 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
9413 case dw_val_class_lineptr:
9414 case dw_val_class_macptr:
9415 case dw_val_class_loclistsptr:
9416 return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
9417 case dw_val_class_str:
9418 return AT_string_form (a);
9419 case dw_val_class_file:
9420 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
9422 case 1:
9423 return DW_FORM_data1;
9424 case 2:
9425 return DW_FORM_data2;
9426 case 4:
9427 return DW_FORM_data4;
9428 default:
9429 gcc_unreachable ();
9432 case dw_val_class_data8:
9433 return DW_FORM_data8;
9435 case dw_val_class_high_pc:
9436 switch (DWARF2_ADDR_SIZE)
9438 case 1:
9439 return DW_FORM_data1;
9440 case 2:
9441 return DW_FORM_data2;
9442 case 4:
9443 return DW_FORM_data4;
9444 case 8:
9445 return DW_FORM_data8;
9446 default:
9447 gcc_unreachable ();
9450 case dw_val_class_discr_value:
9451 return (a->dw_attr_val.v.val_discr_value.pos
9452 ? DW_FORM_udata
9453 : DW_FORM_sdata);
9454 case dw_val_class_discr_list:
9455 switch (constant_size (size_of_discr_list (AT_discr_list (a))))
9457 case 1:
9458 return DW_FORM_block1;
9459 case 2:
9460 return DW_FORM_block2;
9461 case 4:
9462 return DW_FORM_block4;
9463 default:
9464 gcc_unreachable ();
9467 default:
9468 gcc_unreachable ();
9472 /* Output the encoding of an attribute value. */
9474 static void
9475 output_value_format (dw_attr_node *a)
9477 enum dwarf_form form = value_format (a);
9479 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
9482 /* Given a die and id, produce the appropriate abbreviations. */
9484 static void
9485 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
9487 unsigned ix;
9488 dw_attr_node *a_attr;
9490 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
9491 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
9492 dwarf_tag_name (abbrev->die_tag));
9494 if (abbrev->die_child != NULL)
9495 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
9496 else
9497 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
9499 for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
9501 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
9502 dwarf_attr_name (a_attr->dw_attr));
9503 output_value_format (a_attr);
9504 if (value_format (a_attr) == DW_FORM_implicit_const)
9506 if (AT_class (a_attr) == dw_val_class_file_implicit)
9508 int f = maybe_emit_file (a_attr->dw_attr_val.v.val_file);
9509 const char *filename = a_attr->dw_attr_val.v.val_file->filename;
9510 dw2_asm_output_data_sleb128 (f, "(%s)", filename);
9512 else
9513 dw2_asm_output_data_sleb128 (a_attr->dw_attr_val.v.val_int, NULL);
9517 dw2_asm_output_data (1, 0, NULL);
9518 dw2_asm_output_data (1, 0, NULL);
9522 /* Output the .debug_abbrev section which defines the DIE abbreviation
9523 table. */
9525 static void
9526 output_abbrev_section (void)
9528 unsigned int abbrev_id;
9529 dw_die_ref abbrev;
9531 FOR_EACH_VEC_SAFE_ELT (abbrev_die_table, abbrev_id, abbrev)
9532 if (abbrev_id != 0)
9533 output_die_abbrevs (abbrev_id, abbrev);
9535 /* Terminate the table. */
9536 dw2_asm_output_data (1, 0, NULL);
9539 /* Output a symbol we can use to refer to this DIE from another CU. */
9541 static inline void
9542 output_die_symbol (dw_die_ref die)
9544 const char *sym = die->die_id.die_symbol;
9546 gcc_assert (!die->comdat_type_p);
9548 if (sym == 0)
9549 return;
9551 if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
9552 /* We make these global, not weak; if the target doesn't support
9553 .linkonce, it doesn't support combining the sections, so debugging
9554 will break. */
9555 targetm.asm_out.globalize_label (asm_out_file, sym);
9557 ASM_OUTPUT_LABEL (asm_out_file, sym);
9560 /* Return a new location list, given the begin and end range, and the
9561 expression. */
9563 static inline dw_loc_list_ref
9564 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
9565 const char *section)
9567 dw_loc_list_ref retlist = ggc_cleared_alloc<dw_loc_list_node> ();
9569 retlist->begin = begin;
9570 retlist->begin_entry = NULL;
9571 retlist->end = end;
9572 retlist->expr = expr;
9573 retlist->section = section;
9575 return retlist;
9578 /* Generate a new internal symbol for this location list node, if it
9579 hasn't got one yet. */
9581 static inline void
9582 gen_llsym (dw_loc_list_ref list)
9584 gcc_assert (!list->ll_symbol);
9585 list->ll_symbol = gen_internal_sym ("LLST");
9588 /* Output the location list given to us. */
9590 static void
9591 output_loc_list (dw_loc_list_ref list_head)
9593 if (list_head->emitted)
9594 return;
9595 list_head->emitted = true;
9597 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
9599 dw_loc_list_ref curr = list_head;
9600 const char *last_section = NULL;
9601 const char *base_label = NULL;
9603 /* Walk the location list, and output each range + expression. */
9604 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
9606 unsigned long size;
9607 /* Don't output an entry that starts and ends at the same address. */
9608 if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
9609 continue;
9610 size = size_of_locs (curr->expr);
9611 /* If the expression is too large, drop it on the floor. We could
9612 perhaps put it into DW_TAG_dwarf_procedure and refer to that
9613 in the expression, but >= 64KB expressions for a single value
9614 in a single range are unlikely very useful. */
9615 if (dwarf_version < 5 && size > 0xffff)
9616 continue;
9617 if (dwarf_version >= 5)
9619 if (dwarf_split_debug_info)
9621 /* For -gsplit-dwarf, emit DW_LLE_starx_length, which has
9622 uleb128 index into .debug_addr and uleb128 length. */
9623 dw2_asm_output_data (1, DW_LLE_startx_length,
9624 "DW_LLE_startx_length (%s)",
9625 list_head->ll_symbol);
9626 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
9627 "Location list range start index "
9628 "(%s)", curr->begin);
9629 /* FIXME: This will ICE ifndef HAVE_AS_LEB128.
9630 For that case we probably need to emit DW_LLE_startx_endx,
9631 but we'd need 2 .debug_addr entries rather than just one. */
9632 dw2_asm_output_delta_uleb128 (curr->end, curr->begin,
9633 "Location list length (%s)",
9634 list_head->ll_symbol);
9636 else if (!have_multiple_function_sections && HAVE_AS_LEB128)
9638 /* If all code is in .text section, the base address is
9639 already provided by the CU attributes. Use
9640 DW_LLE_offset_pair where both addresses are uleb128 encoded
9641 offsets against that base. */
9642 dw2_asm_output_data (1, DW_LLE_offset_pair,
9643 "DW_LLE_offset_pair (%s)",
9644 list_head->ll_symbol);
9645 dw2_asm_output_delta_uleb128 (curr->begin, curr->section,
9646 "Location list begin address (%s)",
9647 list_head->ll_symbol);
9648 dw2_asm_output_delta_uleb128 (curr->end, curr->section,
9649 "Location list end address (%s)",
9650 list_head->ll_symbol);
9652 else if (HAVE_AS_LEB128)
9654 /* Otherwise, find out how many consecutive entries could share
9655 the same base entry. If just one, emit DW_LLE_start_length,
9656 otherwise emit DW_LLE_base_address for the base address
9657 followed by a series of DW_LLE_offset_pair. */
9658 if (last_section == NULL || curr->section != last_section)
9660 dw_loc_list_ref curr2;
9661 for (curr2 = curr->dw_loc_next; curr2 != NULL;
9662 curr2 = curr2->dw_loc_next)
9664 if (strcmp (curr2->begin, curr2->end) == 0
9665 && !curr2->force)
9666 continue;
9667 break;
9669 if (curr2 == NULL || curr->section != curr2->section)
9670 last_section = NULL;
9671 else
9673 last_section = curr->section;
9674 base_label = curr->begin;
9675 dw2_asm_output_data (1, DW_LLE_base_address,
9676 "DW_LLE_base_address (%s)",
9677 list_head->ll_symbol);
9678 dw2_asm_output_addr (DWARF2_ADDR_SIZE, base_label,
9679 "Base address (%s)",
9680 list_head->ll_symbol);
9683 /* Only one entry with the same base address. Use
9684 DW_LLE_start_length with absolute address and uleb128
9685 length. */
9686 if (last_section == NULL)
9688 dw2_asm_output_data (1, DW_LLE_start_length,
9689 "DW_LLE_start_length (%s)",
9690 list_head->ll_symbol);
9691 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
9692 "Location list begin address (%s)",
9693 list_head->ll_symbol);
9694 dw2_asm_output_delta_uleb128 (curr->end, curr->begin,
9695 "Location list length "
9696 "(%s)", list_head->ll_symbol);
9698 /* Otherwise emit DW_LLE_offset_pair, relative to above emitted
9699 DW_LLE_base_address. */
9700 else
9702 dw2_asm_output_data (1, DW_LLE_offset_pair,
9703 "DW_LLE_offset_pair (%s)",
9704 list_head->ll_symbol);
9705 dw2_asm_output_delta_uleb128 (curr->begin, base_label,
9706 "Location list begin address "
9707 "(%s)", list_head->ll_symbol);
9708 dw2_asm_output_delta_uleb128 (curr->end, base_label,
9709 "Location list end address "
9710 "(%s)", list_head->ll_symbol);
9713 /* The assembler does not support .uleb128 directive. Emit
9714 DW_LLE_start_end with a pair of absolute addresses. */
9715 else
9717 dw2_asm_output_data (1, DW_LLE_start_end,
9718 "DW_LLE_start_end (%s)",
9719 list_head->ll_symbol);
9720 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
9721 "Location list begin address (%s)",
9722 list_head->ll_symbol);
9723 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
9724 "Location list end address (%s)",
9725 list_head->ll_symbol);
9728 else if (dwarf_split_debug_info)
9730 /* For -gsplit-dwarf -gdwarf-{2,3,4} emit index into .debug_addr
9731 and 4 byte length. */
9732 dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
9733 "Location list start/length entry (%s)",
9734 list_head->ll_symbol);
9735 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
9736 "Location list range start index (%s)",
9737 curr->begin);
9738 /* The length field is 4 bytes. If we ever need to support
9739 an 8-byte length, we can add a new DW_LLE code or fall back
9740 to DW_LLE_GNU_start_end_entry. */
9741 dw2_asm_output_delta (4, curr->end, curr->begin,
9742 "Location list range length (%s)",
9743 list_head->ll_symbol);
9745 else if (!have_multiple_function_sections)
9747 /* Pair of relative addresses against start of text section. */
9748 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
9749 "Location list begin address (%s)",
9750 list_head->ll_symbol);
9751 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
9752 "Location list end address (%s)",
9753 list_head->ll_symbol);
9755 else
9757 /* Pair of absolute addresses. */
9758 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
9759 "Location list begin address (%s)",
9760 list_head->ll_symbol);
9761 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
9762 "Location list end address (%s)",
9763 list_head->ll_symbol);
9766 /* Output the block length for this list of location operations. */
9767 if (dwarf_version >= 5)
9768 dw2_asm_output_data_uleb128 (size, "Location expression size");
9769 else
9771 gcc_assert (size <= 0xffff);
9772 dw2_asm_output_data (2, size, "Location expression size");
9775 output_loc_sequence (curr->expr, -1);
9778 /* And finally list termination. */
9779 if (dwarf_version >= 5)
9780 dw2_asm_output_data (1, DW_LLE_end_of_list,
9781 "DW_LLE_end_of_list (%s)", list_head->ll_symbol);
9782 else if (dwarf_split_debug_info)
9783 dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
9784 "Location list terminator (%s)",
9785 list_head->ll_symbol);
9786 else
9788 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
9789 "Location list terminator begin (%s)",
9790 list_head->ll_symbol);
9791 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
9792 "Location list terminator end (%s)",
9793 list_head->ll_symbol);
9797 /* Output a range_list offset into the .debug_ranges or .debug_rnglists
9798 section. Emit a relocated reference if val_entry is NULL, otherwise,
9799 emit an indirect reference. */
9801 static void
9802 output_range_list_offset (dw_attr_node *a)
9804 const char *name = dwarf_attr_name (a->dw_attr);
9806 if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
9808 if (dwarf_version >= 5)
9810 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
9811 dw2_asm_output_offset (DWARF_OFFSET_SIZE, r->label,
9812 debug_ranges_section, "%s", name);
9814 else
9816 char *p = strchr (ranges_section_label, '\0');
9817 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
9818 a->dw_attr_val.v.val_offset * 2 * DWARF2_ADDR_SIZE);
9819 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
9820 debug_ranges_section, "%s", name);
9821 *p = '\0';
9824 else if (dwarf_version >= 5)
9826 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
9827 gcc_assert (rnglist_idx);
9828 dw2_asm_output_data_uleb128 (r->idx, "%s", name);
9830 else
9831 dw2_asm_output_data (DWARF_OFFSET_SIZE,
9832 a->dw_attr_val.v.val_offset * 2 * DWARF2_ADDR_SIZE,
9833 "%s (offset from %s)", name, ranges_section_label);
9836 /* Output the offset into the debug_loc section. */
9838 static void
9839 output_loc_list_offset (dw_attr_node *a)
9841 char *sym = AT_loc_list (a)->ll_symbol;
9843 gcc_assert (sym);
9844 if (!dwarf_split_debug_info)
9845 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
9846 "%s", dwarf_attr_name (a->dw_attr));
9847 else if (dwarf_version >= 5)
9849 gcc_assert (AT_loc_list (a)->num_assigned);
9850 dw2_asm_output_data_uleb128 (AT_loc_list (a)->hash, "%s (%s)",
9851 dwarf_attr_name (a->dw_attr),
9852 sym);
9854 else
9855 dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
9856 "%s", dwarf_attr_name (a->dw_attr));
9859 /* Output an attribute's index or value appropriately. */
9861 static void
9862 output_attr_index_or_value (dw_attr_node *a)
9864 const char *name = dwarf_attr_name (a->dw_attr);
9866 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
9868 dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
9869 return;
9871 switch (AT_class (a))
9873 case dw_val_class_addr:
9874 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
9875 break;
9876 case dw_val_class_high_pc:
9877 case dw_val_class_lbl_id:
9878 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
9879 break;
9880 default:
9881 gcc_unreachable ();
9885 /* Output a type signature. */
9887 static inline void
9888 output_signature (const char *sig, const char *name)
9890 int i;
9892 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9893 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
9896 /* Output a discriminant value. */
9898 static inline void
9899 output_discr_value (dw_discr_value *discr_value, const char *name)
9901 if (discr_value->pos)
9902 dw2_asm_output_data_uleb128 (discr_value->v.uval, "%s", name);
9903 else
9904 dw2_asm_output_data_sleb128 (discr_value->v.sval, "%s", name);
9907 /* Output the DIE and its attributes. Called recursively to generate
9908 the definitions of each child DIE. */
9910 static void
9911 output_die (dw_die_ref die)
9913 dw_attr_node *a;
9914 dw_die_ref c;
9915 unsigned long size;
9916 unsigned ix;
9918 /* If someone in another CU might refer to us, set up a symbol for
9919 them to point to. */
9920 if (! die->comdat_type_p && die->die_id.die_symbol)
9921 output_die_symbol (die);
9923 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
9924 (unsigned long)die->die_offset,
9925 dwarf_tag_name (die->die_tag));
9927 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9929 const char *name = dwarf_attr_name (a->dw_attr);
9931 switch (AT_class (a))
9933 case dw_val_class_addr:
9934 output_attr_index_or_value (a);
9935 break;
9937 case dw_val_class_offset:
9938 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
9939 "%s", name);
9940 break;
9942 case dw_val_class_range_list:
9943 output_range_list_offset (a);
9944 break;
9946 case dw_val_class_loc:
9947 size = size_of_locs (AT_loc (a));
9949 /* Output the block length for this list of location operations. */
9950 if (dwarf_version >= 4)
9951 dw2_asm_output_data_uleb128 (size, "%s", name);
9952 else
9953 dw2_asm_output_data (constant_size (size), size, "%s", name);
9955 output_loc_sequence (AT_loc (a), -1);
9956 break;
9958 case dw_val_class_const:
9959 /* ??? It would be slightly more efficient to use a scheme like is
9960 used for unsigned constants below, but gdb 4.x does not sign
9961 extend. Gdb 5.x does sign extend. */
9962 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
9963 break;
9965 case dw_val_class_unsigned_const:
9967 int csize = constant_size (AT_unsigned (a));
9968 if (dwarf_version == 3
9969 && a->dw_attr == DW_AT_data_member_location
9970 && csize >= 4)
9971 dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
9972 else
9973 dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
9975 break;
9977 case dw_val_class_const_implicit:
9978 if (flag_debug_asm)
9979 fprintf (asm_out_file, "\t\t\t%s %s ("
9980 HOST_WIDE_INT_PRINT_DEC ")\n",
9981 ASM_COMMENT_START, name, AT_int (a));
9982 break;
9984 case dw_val_class_unsigned_const_implicit:
9985 if (flag_debug_asm)
9986 fprintf (asm_out_file, "\t\t\t%s %s ("
9987 HOST_WIDE_INT_PRINT_HEX ")\n",
9988 ASM_COMMENT_START, name, AT_unsigned (a));
9989 break;
9991 case dw_val_class_const_double:
9993 unsigned HOST_WIDE_INT first, second;
9995 if (HOST_BITS_PER_WIDE_INT >= DWARF_LARGEST_DATA_FORM_BITS)
9996 dw2_asm_output_data (1,
9997 HOST_BITS_PER_DOUBLE_INT
9998 / HOST_BITS_PER_CHAR,
9999 NULL);
10001 if (WORDS_BIG_ENDIAN)
10003 first = a->dw_attr_val.v.val_double.high;
10004 second = a->dw_attr_val.v.val_double.low;
10006 else
10008 first = a->dw_attr_val.v.val_double.low;
10009 second = a->dw_attr_val.v.val_double.high;
10012 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
10013 first, "%s", name);
10014 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
10015 second, NULL);
10017 break;
10019 case dw_val_class_wide_int:
10021 int i;
10022 int len = get_full_len (*a->dw_attr_val.v.val_wide);
10023 int l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
10024 if (len * HOST_BITS_PER_WIDE_INT > DWARF_LARGEST_DATA_FORM_BITS)
10025 dw2_asm_output_data (1, get_full_len (*a->dw_attr_val.v.val_wide)
10026 * l, NULL);
10028 if (WORDS_BIG_ENDIAN)
10029 for (i = len - 1; i >= 0; --i)
10031 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
10032 "%s", name);
10033 name = "";
10035 else
10036 for (i = 0; i < len; ++i)
10038 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
10039 "%s", name);
10040 name = "";
10043 break;
10045 case dw_val_class_vec:
10047 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
10048 unsigned int len = a->dw_attr_val.v.val_vec.length;
10049 unsigned int i;
10050 unsigned char *p;
10052 dw2_asm_output_data (constant_size (len * elt_size),
10053 len * elt_size, "%s", name);
10054 if (elt_size > sizeof (HOST_WIDE_INT))
10056 elt_size /= 2;
10057 len *= 2;
10059 for (i = 0, p = a->dw_attr_val.v.val_vec.array;
10060 i < len;
10061 i++, p += elt_size)
10062 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
10063 "fp or vector constant word %u", i);
10064 break;
10067 case dw_val_class_flag:
10068 if (dwarf_version >= 4)
10070 /* Currently all add_AT_flag calls pass in 1 as last argument,
10071 so DW_FORM_flag_present can be used. If that ever changes,
10072 we'll need to use DW_FORM_flag and have some optimization
10073 in build_abbrev_table that will change those to
10074 DW_FORM_flag_present if it is set to 1 in all DIEs using
10075 the same abbrev entry. */
10076 gcc_assert (AT_flag (a) == 1);
10077 if (flag_debug_asm)
10078 fprintf (asm_out_file, "\t\t\t%s %s\n",
10079 ASM_COMMENT_START, name);
10080 break;
10082 dw2_asm_output_data (1, AT_flag (a), "%s", name);
10083 break;
10085 case dw_val_class_loc_list:
10086 output_loc_list_offset (a);
10087 break;
10089 case dw_val_class_die_ref:
10090 if (AT_ref_external (a))
10092 if (AT_ref (a)->comdat_type_p)
10094 comdat_type_node *type_node
10095 = AT_ref (a)->die_id.die_type_node;
10097 gcc_assert (type_node);
10098 output_signature (type_node->signature, name);
10100 else
10102 const char *sym = AT_ref (a)->die_id.die_symbol;
10103 int size;
10105 gcc_assert (sym);
10106 /* In DWARF2, DW_FORM_ref_addr is sized by target address
10107 length, whereas in DWARF3 it's always sized as an
10108 offset. */
10109 if (dwarf_version == 2)
10110 size = DWARF2_ADDR_SIZE;
10111 else
10112 size = DWARF_OFFSET_SIZE;
10113 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
10114 name);
10117 else
10119 gcc_assert (AT_ref (a)->die_offset);
10120 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
10121 "%s", name);
10123 break;
10125 case dw_val_class_fde_ref:
10127 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
10129 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
10130 a->dw_attr_val.v.val_fde_index * 2);
10131 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
10132 "%s", name);
10134 break;
10136 case dw_val_class_vms_delta:
10137 #ifdef ASM_OUTPUT_DWARF_VMS_DELTA
10138 dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
10139 AT_vms_delta2 (a), AT_vms_delta1 (a),
10140 "%s", name);
10141 #else
10142 dw2_asm_output_delta (DWARF_OFFSET_SIZE,
10143 AT_vms_delta2 (a), AT_vms_delta1 (a),
10144 "%s", name);
10145 #endif
10146 break;
10148 case dw_val_class_lbl_id:
10149 output_attr_index_or_value (a);
10150 break;
10152 case dw_val_class_lineptr:
10153 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
10154 debug_line_section, "%s", name);
10155 break;
10157 case dw_val_class_macptr:
10158 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
10159 debug_macinfo_section, "%s", name);
10160 break;
10162 case dw_val_class_loclistsptr:
10163 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
10164 debug_loc_section, "%s", name);
10165 break;
10167 case dw_val_class_str:
10168 if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
10169 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
10170 a->dw_attr_val.v.val_str->label,
10171 debug_str_section,
10172 "%s: \"%s\"", name, AT_string (a));
10173 else if (a->dw_attr_val.v.val_str->form == DW_FORM_line_strp)
10174 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
10175 a->dw_attr_val.v.val_str->label,
10176 debug_line_str_section,
10177 "%s: \"%s\"", name, AT_string (a));
10178 else if (a->dw_attr_val.v.val_str->form == DW_FORM_GNU_str_index)
10179 dw2_asm_output_data_uleb128 (AT_index (a),
10180 "%s: \"%s\"", name, AT_string (a));
10181 else
10182 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
10183 break;
10185 case dw_val_class_file:
10187 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
10189 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
10190 a->dw_attr_val.v.val_file->filename);
10191 break;
10194 case dw_val_class_file_implicit:
10195 if (flag_debug_asm)
10196 fprintf (asm_out_file, "\t\t\t%s %s (%d, %s)\n",
10197 ASM_COMMENT_START, name,
10198 maybe_emit_file (a->dw_attr_val.v.val_file),
10199 a->dw_attr_val.v.val_file->filename);
10200 break;
10202 case dw_val_class_data8:
10204 int i;
10206 for (i = 0; i < 8; i++)
10207 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
10208 i == 0 ? "%s" : NULL, name);
10209 break;
10212 case dw_val_class_high_pc:
10213 dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
10214 get_AT_low_pc (die), "DW_AT_high_pc");
10215 break;
10217 case dw_val_class_discr_value:
10218 output_discr_value (&a->dw_attr_val.v.val_discr_value, name);
10219 break;
10221 case dw_val_class_discr_list:
10223 dw_discr_list_ref list = AT_discr_list (a);
10224 const int size = size_of_discr_list (list);
10226 /* This is a block, so output its length first. */
10227 dw2_asm_output_data (constant_size (size), size,
10228 "%s: block size", name);
10230 for (; list != NULL; list = list->dw_discr_next)
10232 /* One byte for the discriminant value descriptor, and then as
10233 many LEB128 numbers as required. */
10234 if (list->dw_discr_range)
10235 dw2_asm_output_data (1, DW_DSC_range,
10236 "%s: DW_DSC_range", name);
10237 else
10238 dw2_asm_output_data (1, DW_DSC_label,
10239 "%s: DW_DSC_label", name);
10241 output_discr_value (&list->dw_discr_lower_bound, name);
10242 if (list->dw_discr_range)
10243 output_discr_value (&list->dw_discr_upper_bound, name);
10245 break;
10248 default:
10249 gcc_unreachable ();
10253 FOR_EACH_CHILD (die, c, output_die (c));
10255 /* Add null byte to terminate sibling list. */
10256 if (die->die_child != NULL)
10257 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
10258 (unsigned long) die->die_offset);
10261 /* Output the compilation unit that appears at the beginning of the
10262 .debug_info section, and precedes the DIE descriptions. */
10264 static void
10265 output_compilation_unit_header (enum dwarf_unit_type ut)
10267 if (!XCOFF_DEBUGGING_INFO)
10269 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10270 dw2_asm_output_data (4, 0xffffffff,
10271 "Initial length escape value indicating 64-bit DWARF extension");
10272 dw2_asm_output_data (DWARF_OFFSET_SIZE,
10273 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
10274 "Length of Compilation Unit Info");
10277 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
10278 if (dwarf_version >= 5)
10280 const char *name;
10281 switch (ut)
10283 case DW_UT_compile: name = "DW_UT_compile"; break;
10284 case DW_UT_type: name = "DW_UT_type"; break;
10285 case DW_UT_split_compile: name = "DW_UT_split_compile"; break;
10286 case DW_UT_split_type: name = "DW_UT_split_type"; break;
10287 default: gcc_unreachable ();
10289 dw2_asm_output_data (1, ut, "%s", name);
10290 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10292 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
10293 debug_abbrev_section,
10294 "Offset Into Abbrev. Section");
10295 if (dwarf_version < 5)
10296 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10299 /* Output the compilation unit DIE and its children. */
10301 static void
10302 output_comp_unit (dw_die_ref die, int output_if_empty,
10303 const unsigned char *dwo_id)
10305 const char *secname, *oldsym;
10306 char *tmp;
10308 /* Unless we are outputting main CU, we may throw away empty ones. */
10309 if (!output_if_empty && die->die_child == NULL)
10310 return;
10312 /* Even if there are no children of this DIE, we must output the information
10313 about the compilation unit. Otherwise, on an empty translation unit, we
10314 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
10315 will then complain when examining the file. First mark all the DIEs in
10316 this CU so we know which get local refs. */
10317 mark_dies (die);
10319 external_ref_hash_type *extern_map = optimize_external_refs (die);
10321 /* For now, optimize only the main CU, in order to optimize the rest
10322 we'd need to see all of them earlier. Leave the rest for post-linking
10323 tools like DWZ. */
10324 if (die == comp_unit_die ())
10325 abbrev_opt_start = vec_safe_length (abbrev_die_table);
10327 build_abbrev_table (die, extern_map);
10329 optimize_abbrev_table ();
10331 delete extern_map;
10333 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
10334 next_die_offset = (dwo_id
10335 ? DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
10336 : DWARF_COMPILE_UNIT_HEADER_SIZE);
10337 calc_die_sizes (die);
10339 oldsym = die->die_id.die_symbol;
10340 if (oldsym)
10342 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
10344 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
10345 secname = tmp;
10346 die->die_id.die_symbol = NULL;
10347 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
10349 else
10351 switch_to_section (debug_info_section);
10352 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
10353 info_section_emitted = true;
10356 /* Output debugging information. */
10357 output_compilation_unit_header (dwo_id
10358 ? DW_UT_split_compile : DW_UT_compile);
10359 if (dwarf_version >= 5)
10361 if (dwo_id != NULL)
10362 for (int i = 0; i < 8; i++)
10363 dw2_asm_output_data (1, dwo_id[i], i == 0 ? "DWO id" : NULL);
10365 output_die (die);
10367 /* Leave the marks on the main CU, so we can check them in
10368 output_pubnames. */
10369 if (oldsym)
10371 unmark_dies (die);
10372 die->die_id.die_symbol = oldsym;
10376 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
10377 and .debug_pubtypes. This is configured per-target, but can be
10378 overridden by the -gpubnames or -gno-pubnames options. */
10380 static inline bool
10381 want_pubnames (void)
10383 if (debug_info_level <= DINFO_LEVEL_TERSE)
10384 return false;
10385 if (debug_generate_pub_sections != -1)
10386 return debug_generate_pub_sections;
10387 return targetm.want_debug_pub_sections;
10390 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes. */
10392 static void
10393 add_AT_pubnames (dw_die_ref die)
10395 if (want_pubnames ())
10396 add_AT_flag (die, DW_AT_GNU_pubnames, 1);
10399 /* Add a string attribute value to a skeleton DIE. */
10401 static inline void
10402 add_skeleton_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
10403 const char *str)
10405 dw_attr_node attr;
10406 struct indirect_string_node *node;
10408 if (! skeleton_debug_str_hash)
10409 skeleton_debug_str_hash
10410 = hash_table<indirect_string_hasher>::create_ggc (10);
10412 node = find_AT_string_in_table (str, skeleton_debug_str_hash);
10413 find_string_form (node);
10414 if (node->form == DW_FORM_GNU_str_index)
10415 node->form = DW_FORM_strp;
10417 attr.dw_attr = attr_kind;
10418 attr.dw_attr_val.val_class = dw_val_class_str;
10419 attr.dw_attr_val.val_entry = NULL;
10420 attr.dw_attr_val.v.val_str = node;
10421 add_dwarf_attr (die, &attr);
10424 /* Helper function to generate top-level dies for skeleton debug_info and
10425 debug_types. */
10427 static void
10428 add_top_level_skeleton_die_attrs (dw_die_ref die)
10430 const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
10431 const char *comp_dir = comp_dir_string ();
10433 add_skeleton_AT_string (die, dwarf_AT (DW_AT_dwo_name), dwo_file_name);
10434 if (comp_dir != NULL)
10435 add_skeleton_AT_string (die, DW_AT_comp_dir, comp_dir);
10436 add_AT_pubnames (die);
10437 add_AT_lineptr (die, DW_AT_GNU_addr_base, debug_addr_section_label);
10440 /* Output skeleton debug sections that point to the dwo file. */
10442 static void
10443 output_skeleton_debug_sections (dw_die_ref comp_unit,
10444 const unsigned char *dwo_id)
10446 /* These attributes will be found in the full debug_info section. */
10447 remove_AT (comp_unit, DW_AT_producer);
10448 remove_AT (comp_unit, DW_AT_language);
10450 switch_to_section (debug_skeleton_info_section);
10451 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
10453 /* Produce the skeleton compilation-unit header. This one differs enough from
10454 a normal CU header that it's better not to call output_compilation_unit
10455 header. */
10456 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10457 dw2_asm_output_data (4, 0xffffffff,
10458 "Initial length escape value indicating 64-bit "
10459 "DWARF extension");
10461 dw2_asm_output_data (DWARF_OFFSET_SIZE,
10462 DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
10463 - DWARF_INITIAL_LENGTH_SIZE
10464 + size_of_die (comp_unit),
10465 "Length of Compilation Unit Info");
10466 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
10467 if (dwarf_version >= 5)
10469 dw2_asm_output_data (1, DW_UT_skeleton, "DW_UT_skeleton");
10470 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10472 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_abbrev_section_label,
10473 debug_skeleton_abbrev_section,
10474 "Offset Into Abbrev. Section");
10475 if (dwarf_version < 5)
10476 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10477 else
10478 for (int i = 0; i < 8; i++)
10479 dw2_asm_output_data (1, dwo_id[i], i == 0 ? "DWO id" : NULL);
10481 comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
10482 output_die (comp_unit);
10484 /* Build the skeleton debug_abbrev section. */
10485 switch_to_section (debug_skeleton_abbrev_section);
10486 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
10488 output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
10490 dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
10493 /* Output a comdat type unit DIE and its children. */
10495 static void
10496 output_comdat_type_unit (comdat_type_node *node)
10498 const char *secname;
10499 char *tmp;
10500 int i;
10501 #if defined (OBJECT_FORMAT_ELF)
10502 tree comdat_key;
10503 #endif
10505 /* First mark all the DIEs in this CU so we know which get local refs. */
10506 mark_dies (node->root_die);
10508 external_ref_hash_type *extern_map = optimize_external_refs (node->root_die);
10510 build_abbrev_table (node->root_die, extern_map);
10512 delete extern_map;
10513 extern_map = NULL;
10515 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
10516 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
10517 calc_die_sizes (node->root_die);
10519 #if defined (OBJECT_FORMAT_ELF)
10520 if (dwarf_version >= 5)
10522 if (!dwarf_split_debug_info)
10523 secname = ".debug_info";
10524 else
10525 secname = ".debug_info.dwo";
10527 else if (!dwarf_split_debug_info)
10528 secname = ".debug_types";
10529 else
10530 secname = ".debug_types.dwo";
10532 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
10533 sprintf (tmp, dwarf_version >= 5 ? "wi." : "wt.");
10534 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
10535 sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
10536 comdat_key = get_identifier (tmp);
10537 targetm.asm_out.named_section (secname,
10538 SECTION_DEBUG | SECTION_LINKONCE,
10539 comdat_key);
10540 #else
10541 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
10542 sprintf (tmp, (dwarf_version >= 5
10543 ? ".gnu.linkonce.wi." : ".gnu.linkonce.wt."));
10544 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
10545 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
10546 secname = tmp;
10547 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
10548 #endif
10550 /* Output debugging information. */
10551 output_compilation_unit_header (dwarf_split_debug_info
10552 ? DW_UT_split_type : DW_UT_type);
10553 output_signature (node->signature, "Type Signature");
10554 dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
10555 "Offset to Type DIE");
10556 output_die (node->root_die);
10558 unmark_dies (node->root_die);
10561 /* Return the DWARF2/3 pubname associated with a decl. */
10563 static const char *
10564 dwarf2_name (tree decl, int scope)
10566 if (DECL_NAMELESS (decl))
10567 return NULL;
10568 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
10571 /* Add a new entry to .debug_pubnames if appropriate. */
10573 static void
10574 add_pubname_string (const char *str, dw_die_ref die)
10576 pubname_entry e;
10578 e.die = die;
10579 e.name = xstrdup (str);
10580 vec_safe_push (pubname_table, e);
10583 static void
10584 add_pubname (tree decl, dw_die_ref die)
10586 if (!want_pubnames ())
10587 return;
10589 /* Don't add items to the table when we expect that the consumer will have
10590 just read the enclosing die. For example, if the consumer is looking at a
10591 class_member, it will either be inside the class already, or will have just
10592 looked up the class to find the member. Either way, searching the class is
10593 faster than searching the index. */
10594 if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
10595 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
10597 const char *name = dwarf2_name (decl, 1);
10599 if (name)
10600 add_pubname_string (name, die);
10604 /* Add an enumerator to the pubnames section. */
10606 static void
10607 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
10609 pubname_entry e;
10611 gcc_assert (scope_name);
10612 e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
10613 e.die = die;
10614 vec_safe_push (pubname_table, e);
10617 /* Add a new entry to .debug_pubtypes if appropriate. */
10619 static void
10620 add_pubtype (tree decl, dw_die_ref die)
10622 pubname_entry e;
10624 if (!want_pubnames ())
10625 return;
10627 if ((TREE_PUBLIC (decl)
10628 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
10629 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
10631 tree scope = NULL;
10632 const char *scope_name = "";
10633 const char *sep = is_cxx () ? "::" : ".";
10634 const char *name;
10636 scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
10637 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
10639 scope_name = lang_hooks.dwarf_name (scope, 1);
10640 if (scope_name != NULL && scope_name[0] != '\0')
10641 scope_name = concat (scope_name, sep, NULL);
10642 else
10643 scope_name = "";
10646 if (TYPE_P (decl))
10647 name = type_tag (decl);
10648 else
10649 name = lang_hooks.dwarf_name (decl, 1);
10651 /* If we don't have a name for the type, there's no point in adding
10652 it to the table. */
10653 if (name != NULL && name[0] != '\0')
10655 e.die = die;
10656 e.name = concat (scope_name, name, NULL);
10657 vec_safe_push (pubtype_table, e);
10660 /* Although it might be more consistent to add the pubinfo for the
10661 enumerators as their dies are created, they should only be added if the
10662 enum type meets the criteria above. So rather than re-check the parent
10663 enum type whenever an enumerator die is created, just output them all
10664 here. This isn't protected by the name conditional because anonymous
10665 enums don't have names. */
10666 if (die->die_tag == DW_TAG_enumeration_type)
10668 dw_die_ref c;
10670 FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
10675 /* Output a single entry in the pubnames table. */
10677 static void
10678 output_pubname (dw_offset die_offset, pubname_entry *entry)
10680 dw_die_ref die = entry->die;
10681 int is_static = get_AT_flag (die, DW_AT_external) ? 0 : 1;
10683 dw2_asm_output_data (DWARF_OFFSET_SIZE, die_offset, "DIE offset");
10685 if (debug_generate_pub_sections == 2)
10687 /* This logic follows gdb's method for determining the value of the flag
10688 byte. */
10689 uint32_t flags = GDB_INDEX_SYMBOL_KIND_NONE;
10690 switch (die->die_tag)
10692 case DW_TAG_typedef:
10693 case DW_TAG_base_type:
10694 case DW_TAG_subrange_type:
10695 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
10696 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
10697 break;
10698 case DW_TAG_enumerator:
10699 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
10700 GDB_INDEX_SYMBOL_KIND_VARIABLE);
10701 if (!is_cxx () && !is_java ())
10702 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
10703 break;
10704 case DW_TAG_subprogram:
10705 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
10706 GDB_INDEX_SYMBOL_KIND_FUNCTION);
10707 if (!is_ada ())
10708 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
10709 break;
10710 case DW_TAG_constant:
10711 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
10712 GDB_INDEX_SYMBOL_KIND_VARIABLE);
10713 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
10714 break;
10715 case DW_TAG_variable:
10716 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
10717 GDB_INDEX_SYMBOL_KIND_VARIABLE);
10718 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
10719 break;
10720 case DW_TAG_namespace:
10721 case DW_TAG_imported_declaration:
10722 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
10723 break;
10724 case DW_TAG_class_type:
10725 case DW_TAG_interface_type:
10726 case DW_TAG_structure_type:
10727 case DW_TAG_union_type:
10728 case DW_TAG_enumeration_type:
10729 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
10730 if (!is_cxx () && !is_java ())
10731 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
10732 break;
10733 default:
10734 /* An unusual tag. Leave the flag-byte empty. */
10735 break;
10737 dw2_asm_output_data (1, flags >> GDB_INDEX_CU_BITSIZE,
10738 "GDB-index flags");
10741 dw2_asm_output_nstring (entry->name, -1, "external name");
10745 /* Output the public names table used to speed up access to externally
10746 visible names; or the public types table used to find type definitions. */
10748 static void
10749 output_pubnames (vec<pubname_entry, va_gc> *names)
10751 unsigned i;
10752 unsigned long pubnames_length = size_of_pubnames (names);
10753 pubname_entry *pub;
10755 if (!XCOFF_DEBUGGING_INFO)
10757 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10758 dw2_asm_output_data (4, 0xffffffff,
10759 "Initial length escape value indicating 64-bit DWARF extension");
10760 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
10761 "Pub Info Length");
10764 /* Version number for pubnames/pubtypes is independent of dwarf version. */
10765 dw2_asm_output_data (2, 2, "DWARF Version");
10767 if (dwarf_split_debug_info)
10768 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
10769 debug_skeleton_info_section,
10770 "Offset of Compilation Unit Info");
10771 else
10772 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
10773 debug_info_section,
10774 "Offset of Compilation Unit Info");
10775 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
10776 "Compilation Unit Length");
10778 FOR_EACH_VEC_ELT (*names, i, pub)
10780 if (include_pubname_in_output (names, pub))
10782 dw_offset die_offset = pub->die->die_offset;
10784 /* We shouldn't see pubnames for DIEs outside of the main CU. */
10785 if (names == pubname_table && pub->die->die_tag != DW_TAG_enumerator)
10786 gcc_assert (pub->die->die_mark);
10788 /* If we're putting types in their own .debug_types sections,
10789 the .debug_pubtypes table will still point to the compile
10790 unit (not the type unit), so we want to use the offset of
10791 the skeleton DIE (if there is one). */
10792 if (pub->die->comdat_type_p && names == pubtype_table)
10794 comdat_type_node *type_node = pub->die->die_id.die_type_node;
10796 if (type_node != NULL)
10797 die_offset = (type_node->skeleton_die != NULL
10798 ? type_node->skeleton_die->die_offset
10799 : comp_unit_die ()->die_offset);
10802 output_pubname (die_offset, pub);
10806 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
10809 /* Output public names and types tables if necessary. */
10811 static void
10812 output_pubtables (void)
10814 if (!want_pubnames () || !info_section_emitted)
10815 return;
10817 switch_to_section (debug_pubnames_section);
10818 output_pubnames (pubname_table);
10819 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
10820 It shouldn't hurt to emit it always, since pure DWARF2 consumers
10821 simply won't look for the section. */
10822 switch_to_section (debug_pubtypes_section);
10823 output_pubnames (pubtype_table);
10827 /* Output the information that goes into the .debug_aranges table.
10828 Namely, define the beginning and ending address range of the
10829 text section generated for this compilation unit. */
10831 static void
10832 output_aranges (void)
10834 unsigned i;
10835 unsigned long aranges_length = size_of_aranges ();
10837 if (!XCOFF_DEBUGGING_INFO)
10839 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10840 dw2_asm_output_data (4, 0xffffffff,
10841 "Initial length escape value indicating 64-bit DWARF extension");
10842 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
10843 "Length of Address Ranges Info");
10846 /* Version number for aranges is still 2, even up to DWARF5. */
10847 dw2_asm_output_data (2, 2, "DWARF Version");
10848 if (dwarf_split_debug_info)
10849 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
10850 debug_skeleton_info_section,
10851 "Offset of Compilation Unit Info");
10852 else
10853 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
10854 debug_info_section,
10855 "Offset of Compilation Unit Info");
10856 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
10857 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
10859 /* We need to align to twice the pointer size here. */
10860 if (DWARF_ARANGES_PAD_SIZE)
10862 /* Pad using a 2 byte words so that padding is correct for any
10863 pointer size. */
10864 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
10865 2 * DWARF2_ADDR_SIZE);
10866 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
10867 dw2_asm_output_data (2, 0, NULL);
10870 /* It is necessary not to output these entries if the sections were
10871 not used; if the sections were not used, the length will be 0 and
10872 the address may end up as 0 if the section is discarded by ld
10873 --gc-sections, leaving an invalid (0, 0) entry that can be
10874 confused with the terminator. */
10875 if (text_section_used)
10877 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
10878 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
10879 text_section_label, "Length");
10881 if (cold_text_section_used)
10883 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
10884 "Address");
10885 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
10886 cold_text_section_label, "Length");
10889 if (have_multiple_function_sections)
10891 unsigned fde_idx;
10892 dw_fde_ref fde;
10894 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
10896 if (DECL_IGNORED_P (fde->decl))
10897 continue;
10898 if (!fde->in_std_section)
10900 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
10901 "Address");
10902 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
10903 fde->dw_fde_begin, "Length");
10905 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
10907 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
10908 "Address");
10909 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
10910 fde->dw_fde_second_begin, "Length");
10915 /* Output the terminator words. */
10916 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
10917 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
10920 /* Add a new entry to .debug_ranges. Return its index into
10921 ranges_table vector. */
10923 static unsigned int
10924 add_ranges_num (int num, bool maybe_new_sec)
10926 dw_ranges r = { NULL, num, 0, maybe_new_sec };
10927 vec_safe_push (ranges_table, r);
10928 return vec_safe_length (ranges_table) - 1;
10931 /* Add a new entry to .debug_ranges corresponding to a block, or a
10932 range terminator if BLOCK is NULL. MAYBE_NEW_SEC is true if
10933 this entry might be in a different section from previous range. */
10935 static unsigned int
10936 add_ranges (const_tree block, bool maybe_new_sec)
10938 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0, maybe_new_sec);
10941 /* Note that (*rnglist_table)[offset] is either a head of a rnglist
10942 chain, or middle entry of a chain that will be directly referred to. */
10944 static void
10945 note_rnglist_head (unsigned int offset)
10947 if (dwarf_version < 5 || (*ranges_table)[offset].label)
10948 return;
10949 (*ranges_table)[offset].label = gen_internal_sym ("LLRL");
10952 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
10953 When using dwarf_split_debug_info, address attributes in dies destined
10954 for the final executable should be direct references--setting the
10955 parameter force_direct ensures this behavior. */
10957 static void
10958 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
10959 bool *added, bool force_direct)
10961 unsigned int in_use = vec_safe_length (ranges_by_label);
10962 unsigned int offset;
10963 dw_ranges_by_label rbl = { begin, end };
10964 vec_safe_push (ranges_by_label, rbl);
10965 offset = add_ranges_num (-(int)in_use - 1, true);
10966 if (!*added)
10968 add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
10969 *added = true;
10970 note_rnglist_head (offset);
10974 /* Emit .debug_ranges section. */
10976 static void
10977 output_ranges (void)
10979 unsigned i;
10980 static const char *const start_fmt = "Offset %#x";
10981 const char *fmt = start_fmt;
10982 dw_ranges *r;
10984 switch_to_section (debug_ranges_section);
10985 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
10986 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
10988 int block_num = r->num;
10990 if (block_num > 0)
10992 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
10993 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
10995 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
10996 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
10998 /* If all code is in the text section, then the compilation
10999 unit base address defaults to DW_AT_low_pc, which is the
11000 base of the text section. */
11001 if (!have_multiple_function_sections)
11003 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
11004 text_section_label,
11005 fmt, i * 2 * DWARF2_ADDR_SIZE);
11006 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
11007 text_section_label, NULL);
11010 /* Otherwise, the compilation unit base address is zero,
11011 which allows us to use absolute addresses, and not worry
11012 about whether the target supports cross-section
11013 arithmetic. */
11014 else
11016 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11017 fmt, i * 2 * DWARF2_ADDR_SIZE);
11018 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
11021 fmt = NULL;
11024 /* Negative block_num stands for an index into ranges_by_label. */
11025 else if (block_num < 0)
11027 int lab_idx = - block_num - 1;
11029 if (!have_multiple_function_sections)
11031 gcc_unreachable ();
11032 #if 0
11033 /* If we ever use add_ranges_by_labels () for a single
11034 function section, all we have to do is to take out
11035 the #if 0 above. */
11036 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11037 (*ranges_by_label)[lab_idx].begin,
11038 text_section_label,
11039 fmt, i * 2 * DWARF2_ADDR_SIZE);
11040 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11041 (*ranges_by_label)[lab_idx].end,
11042 text_section_label, NULL);
11043 #endif
11045 else
11047 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11048 (*ranges_by_label)[lab_idx].begin,
11049 fmt, i * 2 * DWARF2_ADDR_SIZE);
11050 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11051 (*ranges_by_label)[lab_idx].end,
11052 NULL);
11055 else
11057 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11058 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11059 fmt = start_fmt;
11064 /* Non-zero if .debug_line_str should be used for .debug_line section
11065 strings or strings that are likely shareable with those. */
11066 #define DWARF5_USE_DEBUG_LINE_STR \
11067 (!DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET \
11068 && (DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) != 0 \
11069 /* FIXME: there is no .debug_line_str.dwo section, \
11070 for -gsplit-dwarf we should use DW_FORM_strx instead. */ \
11071 && !dwarf_split_debug_info)
11073 /* Assign .debug_rnglists indexes. */
11075 static void
11076 index_rnglists (void)
11078 unsigned i;
11079 dw_ranges *r;
11081 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11082 if (r->label)
11083 r->idx = rnglist_idx++;
11086 /* Emit .debug_rnglists section. */
11088 static void
11089 output_rnglists (void)
11091 unsigned i;
11092 dw_ranges *r;
11093 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
11094 char l2[MAX_ARTIFICIAL_LABEL_BYTES];
11095 char basebuf[MAX_ARTIFICIAL_LABEL_BYTES];
11097 switch_to_section (debug_ranges_section);
11098 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
11099 ASM_GENERATE_INTERNAL_LABEL (l1, DEBUG_RANGES_SECTION_LABEL, 2);
11100 ASM_GENERATE_INTERNAL_LABEL (l2, DEBUG_RANGES_SECTION_LABEL, 3);
11101 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11102 dw2_asm_output_data (4, 0xffffffff,
11103 "Initial length escape value indicating "
11104 "64-bit DWARF extension");
11105 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
11106 "Length of Range Lists");
11107 ASM_OUTPUT_LABEL (asm_out_file, l1);
11108 dw2_asm_output_data (2, dwarf_version, "DWARF Version");
11109 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
11110 dw2_asm_output_data (1, 0, "Segment Size");
11111 /* Emit the offset table only for -gsplit-dwarf. If we don't care
11112 about relocation sizes and primarily care about the size of .debug*
11113 sections in linked shared libraries and executables, then
11114 the offset table plus corresponding DW_FORM_rnglistx uleb128 indexes
11115 into it are usually larger than just DW_FORM_sec_offset offsets
11116 into the .debug_rnglists section. */
11117 dw2_asm_output_data (4, dwarf_split_debug_info ? rnglist_idx : 0,
11118 "Offset Entry Count");
11119 if (dwarf_split_debug_info)
11121 ASM_OUTPUT_LABEL (asm_out_file, ranges_base_label);
11122 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11123 if (r->label)
11124 dw2_asm_output_delta (DWARF_OFFSET_SIZE, r->label,
11125 ranges_base_label, NULL);
11128 const char *lab = "";
11129 unsigned int len = vec_safe_length (ranges_table);
11130 const char *base = NULL;
11131 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11133 int block_num = r->num;
11135 if (r->label)
11137 ASM_OUTPUT_LABEL (asm_out_file, r->label);
11138 lab = r->label;
11140 if (HAVE_AS_LEB128 && (r->label || r->maybe_new_sec))
11141 base = NULL;
11142 if (block_num > 0)
11144 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
11145 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
11147 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
11148 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
11150 if (HAVE_AS_LEB128)
11152 /* If all code is in the text section, then the compilation
11153 unit base address defaults to DW_AT_low_pc, which is the
11154 base of the text section. */
11155 if (!have_multiple_function_sections)
11157 dw2_asm_output_data (1, DW_RLE_offset_pair,
11158 "DW_RLE_offset_pair (%s)", lab);
11159 dw2_asm_output_delta_uleb128 (blabel, text_section_label,
11160 "Range begin address (%s)", lab);
11161 dw2_asm_output_delta_uleb128 (elabel, text_section_label,
11162 "Range end address (%s)", lab);
11163 continue;
11165 if (base == NULL)
11167 dw_ranges *r2 = NULL;
11168 if (i < len - 1)
11169 r2 = &(*ranges_table)[i + 1];
11170 if (r2
11171 && r2->num != 0
11172 && r2->label == NULL
11173 && !r2->maybe_new_sec)
11175 dw2_asm_output_data (1, DW_RLE_base_address,
11176 "DW_RLE_base_address (%s)", lab);
11177 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11178 "Base address (%s)", lab);
11179 strcpy (basebuf, blabel);
11180 base = basebuf;
11183 if (base)
11185 dw2_asm_output_data (1, DW_RLE_offset_pair,
11186 "DW_RLE_offset_pair (%s)", lab);
11187 dw2_asm_output_delta_uleb128 (blabel, base,
11188 "Range begin address (%s)", lab);
11189 dw2_asm_output_delta_uleb128 (elabel, base,
11190 "Range end address (%s)", lab);
11191 continue;
11193 dw2_asm_output_data (1, DW_RLE_start_length,
11194 "DW_RLE_start_length (%s)", lab);
11195 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11196 "Range begin address (%s)", lab);
11197 dw2_asm_output_delta_uleb128 (elabel, blabel,
11198 "Range length (%s)", lab);
11200 else
11202 dw2_asm_output_data (1, DW_RLE_start_end,
11203 "DW_RLE_start_end (%s)", lab);
11204 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11205 "Range begin address (%s)", lab);
11206 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel,
11207 "Range end address (%s)", lab);
11211 /* Negative block_num stands for an index into ranges_by_label. */
11212 else if (block_num < 0)
11214 int lab_idx = - block_num - 1;
11215 const char *blabel = (*ranges_by_label)[lab_idx].begin;
11216 const char *elabel = (*ranges_by_label)[lab_idx].end;
11218 if (!have_multiple_function_sections)
11219 gcc_unreachable ();
11220 if (HAVE_AS_LEB128)
11222 dw2_asm_output_data (1, DW_RLE_start_length,
11223 "DW_RLE_start_length (%s)", lab);
11224 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11225 "Range begin address (%s)", lab);
11226 dw2_asm_output_delta_uleb128 (elabel, blabel,
11227 "Range length (%s)", lab);
11229 else
11231 dw2_asm_output_data (1, DW_RLE_start_end,
11232 "DW_RLE_start_end (%s)", lab);
11233 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11234 "Range begin address (%s)", lab);
11235 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel,
11236 "Range end address (%s)", lab);
11239 else
11240 dw2_asm_output_data (1, DW_RLE_end_of_list,
11241 "DW_RLE_end_of_list (%s)", lab);
11243 ASM_OUTPUT_LABEL (asm_out_file, l2);
11246 /* Data structure containing information about input files. */
11247 struct file_info
11249 const char *path; /* Complete file name. */
11250 const char *fname; /* File name part. */
11251 int length; /* Length of entire string. */
11252 struct dwarf_file_data * file_idx; /* Index in input file table. */
11253 int dir_idx; /* Index in directory table. */
11256 /* Data structure containing information about directories with source
11257 files. */
11258 struct dir_info
11260 const char *path; /* Path including directory name. */
11261 int length; /* Path length. */
11262 int prefix; /* Index of directory entry which is a prefix. */
11263 int count; /* Number of files in this directory. */
11264 int dir_idx; /* Index of directory used as base. */
11267 /* Callback function for file_info comparison. We sort by looking at
11268 the directories in the path. */
11270 static int
11271 file_info_cmp (const void *p1, const void *p2)
11273 const struct file_info *const s1 = (const struct file_info *) p1;
11274 const struct file_info *const s2 = (const struct file_info *) p2;
11275 const unsigned char *cp1;
11276 const unsigned char *cp2;
11278 /* Take care of file names without directories. We need to make sure that
11279 we return consistent values to qsort since some will get confused if
11280 we return the same value when identical operands are passed in opposite
11281 orders. So if neither has a directory, return 0 and otherwise return
11282 1 or -1 depending on which one has the directory. */
11283 if ((s1->path == s1->fname || s2->path == s2->fname))
11284 return (s2->path == s2->fname) - (s1->path == s1->fname);
11286 cp1 = (const unsigned char *) s1->path;
11287 cp2 = (const unsigned char *) s2->path;
11289 while (1)
11291 ++cp1;
11292 ++cp2;
11293 /* Reached the end of the first path? If so, handle like above. */
11294 if ((cp1 == (const unsigned char *) s1->fname)
11295 || (cp2 == (const unsigned char *) s2->fname))
11296 return ((cp2 == (const unsigned char *) s2->fname)
11297 - (cp1 == (const unsigned char *) s1->fname));
11299 /* Character of current path component the same? */
11300 else if (*cp1 != *cp2)
11301 return *cp1 - *cp2;
11305 struct file_name_acquire_data
11307 struct file_info *files;
11308 int used_files;
11309 int max_files;
11312 /* Traversal function for the hash table. */
11315 file_name_acquire (dwarf_file_data **slot, file_name_acquire_data *fnad)
11317 struct dwarf_file_data *d = *slot;
11318 struct file_info *fi;
11319 const char *f;
11321 gcc_assert (fnad->max_files >= d->emitted_number);
11323 if (! d->emitted_number)
11324 return 1;
11326 gcc_assert (fnad->max_files != fnad->used_files);
11328 fi = fnad->files + fnad->used_files++;
11330 /* Skip all leading "./". */
11331 f = d->filename;
11332 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
11333 f += 2;
11335 /* Create a new array entry. */
11336 fi->path = f;
11337 fi->length = strlen (f);
11338 fi->file_idx = d;
11340 /* Search for the file name part. */
11341 f = strrchr (f, DIR_SEPARATOR);
11342 #if defined (DIR_SEPARATOR_2)
11344 char *g = strrchr (fi->path, DIR_SEPARATOR_2);
11346 if (g != NULL)
11348 if (f == NULL || f < g)
11349 f = g;
11352 #endif
11354 fi->fname = f == NULL ? fi->path : f + 1;
11355 return 1;
11358 /* Helper function for output_file_names. Emit a FORM encoded
11359 string STR, with assembly comment start ENTRY_KIND and
11360 index IDX */
11362 static void
11363 output_line_string (enum dwarf_form form, const char *str,
11364 const char *entry_kind, unsigned int idx)
11366 switch (form)
11368 case DW_FORM_string:
11369 dw2_asm_output_nstring (str, -1, "%s: %#x", entry_kind, idx);
11370 break;
11371 case DW_FORM_line_strp:
11372 if (!debug_line_str_hash)
11373 debug_line_str_hash
11374 = hash_table<indirect_string_hasher>::create_ggc (10);
11376 struct indirect_string_node *node;
11377 node = find_AT_string_in_table (str, debug_line_str_hash);
11378 set_indirect_string (node);
11379 node->form = form;
11380 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
11381 debug_line_str_section, "%s: %#x: \"%s\"",
11382 entry_kind, 0, node->str);
11383 break;
11384 default:
11385 gcc_unreachable ();
11389 /* Output the directory table and the file name table. We try to minimize
11390 the total amount of memory needed. A heuristic is used to avoid large
11391 slowdowns with many input files. */
11393 static void
11394 output_file_names (void)
11396 struct file_name_acquire_data fnad;
11397 int numfiles;
11398 struct file_info *files;
11399 struct dir_info *dirs;
11400 int *saved;
11401 int *savehere;
11402 int *backmap;
11403 int ndirs;
11404 int idx_offset;
11405 int i;
11407 if (!last_emitted_file)
11409 if (dwarf_version >= 5)
11411 dw2_asm_output_data (1, 0, "Directory entry format count");
11412 dw2_asm_output_data_uleb128 (0, "Directories count");
11413 dw2_asm_output_data (1, 0, "File name entry format count");
11414 dw2_asm_output_data_uleb128 (0, "File names count");
11416 else
11418 dw2_asm_output_data (1, 0, "End directory table");
11419 dw2_asm_output_data (1, 0, "End file name table");
11421 return;
11424 numfiles = last_emitted_file->emitted_number;
11426 /* Allocate the various arrays we need. */
11427 files = XALLOCAVEC (struct file_info, numfiles);
11428 dirs = XALLOCAVEC (struct dir_info, numfiles);
11430 fnad.files = files;
11431 fnad.used_files = 0;
11432 fnad.max_files = numfiles;
11433 file_table->traverse<file_name_acquire_data *, file_name_acquire> (&fnad);
11434 gcc_assert (fnad.used_files == fnad.max_files);
11436 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
11438 /* Find all the different directories used. */
11439 dirs[0].path = files[0].path;
11440 dirs[0].length = files[0].fname - files[0].path;
11441 dirs[0].prefix = -1;
11442 dirs[0].count = 1;
11443 dirs[0].dir_idx = 0;
11444 files[0].dir_idx = 0;
11445 ndirs = 1;
11447 for (i = 1; i < numfiles; i++)
11448 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
11449 && memcmp (dirs[ndirs - 1].path, files[i].path,
11450 dirs[ndirs - 1].length) == 0)
11452 /* Same directory as last entry. */
11453 files[i].dir_idx = ndirs - 1;
11454 ++dirs[ndirs - 1].count;
11456 else
11458 int j;
11460 /* This is a new directory. */
11461 dirs[ndirs].path = files[i].path;
11462 dirs[ndirs].length = files[i].fname - files[i].path;
11463 dirs[ndirs].count = 1;
11464 dirs[ndirs].dir_idx = ndirs;
11465 files[i].dir_idx = ndirs;
11467 /* Search for a prefix. */
11468 dirs[ndirs].prefix = -1;
11469 for (j = 0; j < ndirs; j++)
11470 if (dirs[j].length < dirs[ndirs].length
11471 && dirs[j].length > 1
11472 && (dirs[ndirs].prefix == -1
11473 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
11474 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
11475 dirs[ndirs].prefix = j;
11477 ++ndirs;
11480 /* Now to the actual work. We have to find a subset of the directories which
11481 allow expressing the file name using references to the directory table
11482 with the least amount of characters. We do not do an exhaustive search
11483 where we would have to check out every combination of every single
11484 possible prefix. Instead we use a heuristic which provides nearly optimal
11485 results in most cases and never is much off. */
11486 saved = XALLOCAVEC (int, ndirs);
11487 savehere = XALLOCAVEC (int, ndirs);
11489 memset (saved, '\0', ndirs * sizeof (saved[0]));
11490 for (i = 0; i < ndirs; i++)
11492 int j;
11493 int total;
11495 /* We can always save some space for the current directory. But this
11496 does not mean it will be enough to justify adding the directory. */
11497 savehere[i] = dirs[i].length;
11498 total = (savehere[i] - saved[i]) * dirs[i].count;
11500 for (j = i + 1; j < ndirs; j++)
11502 savehere[j] = 0;
11503 if (saved[j] < dirs[i].length)
11505 /* Determine whether the dirs[i] path is a prefix of the
11506 dirs[j] path. */
11507 int k;
11509 k = dirs[j].prefix;
11510 while (k != -1 && k != (int) i)
11511 k = dirs[k].prefix;
11513 if (k == (int) i)
11515 /* Yes it is. We can possibly save some memory by
11516 writing the filenames in dirs[j] relative to
11517 dirs[i]. */
11518 savehere[j] = dirs[i].length;
11519 total += (savehere[j] - saved[j]) * dirs[j].count;
11524 /* Check whether we can save enough to justify adding the dirs[i]
11525 directory. */
11526 if (total > dirs[i].length + 1)
11528 /* It's worthwhile adding. */
11529 for (j = i; j < ndirs; j++)
11530 if (savehere[j] > 0)
11532 /* Remember how much we saved for this directory so far. */
11533 saved[j] = savehere[j];
11535 /* Remember the prefix directory. */
11536 dirs[j].dir_idx = i;
11541 /* Emit the directory name table. */
11542 idx_offset = dirs[0].length > 0 ? 1 : 0;
11543 enum dwarf_form str_form = DW_FORM_string;
11544 enum dwarf_form idx_form = DW_FORM_udata;
11545 if (dwarf_version >= 5)
11547 const char *comp_dir = comp_dir_string ();
11548 if (comp_dir == NULL)
11549 comp_dir = "";
11550 dw2_asm_output_data (1, 1, "Directory entry format count");
11551 if (DWARF5_USE_DEBUG_LINE_STR)
11552 str_form = DW_FORM_line_strp;
11553 dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
11554 dw2_asm_output_data_uleb128 (str_form, "%s",
11555 get_DW_FORM_name (str_form));
11556 dw2_asm_output_data_uleb128 (ndirs + idx_offset, "Directories count");
11557 if (str_form == DW_FORM_string)
11559 dw2_asm_output_nstring (comp_dir, -1, "Directory Entry: %#x", 0);
11560 for (i = 1 - idx_offset; i < ndirs; i++)
11561 dw2_asm_output_nstring (dirs[i].path,
11562 dirs[i].length
11563 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
11564 "Directory Entry: %#x", i + idx_offset);
11566 else
11568 output_line_string (str_form, comp_dir, "Directory Entry", 0);
11569 for (i = 1 - idx_offset; i < ndirs; i++)
11571 const char *str
11572 = ggc_alloc_string (dirs[i].path,
11573 dirs[i].length
11574 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR);
11575 output_line_string (str_form, str, "Directory Entry",
11576 (unsigned) i + idx_offset);
11580 else
11582 for (i = 1 - idx_offset; i < ndirs; i++)
11583 dw2_asm_output_nstring (dirs[i].path,
11584 dirs[i].length
11585 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
11586 "Directory Entry: %#x", i + idx_offset);
11588 dw2_asm_output_data (1, 0, "End directory table");
11591 /* We have to emit them in the order of emitted_number since that's
11592 used in the debug info generation. To do this efficiently we
11593 generate a back-mapping of the indices first. */
11594 backmap = XALLOCAVEC (int, numfiles);
11595 for (i = 0; i < numfiles; i++)
11596 backmap[files[i].file_idx->emitted_number - 1] = i;
11598 if (dwarf_version >= 5)
11600 const char *filename0 = get_AT_string (comp_unit_die (), DW_AT_name);
11601 if (filename0 == NULL)
11602 filename0 = "";
11603 /* DW_LNCT_directory_index can use DW_FORM_udata, DW_FORM_data1 and
11604 DW_FORM_data2. Choose one based on the number of directories
11605 and how much space would they occupy in each encoding.
11606 If we have at most 256 directories, all indexes fit into
11607 a single byte, so DW_FORM_data1 is most compact (if there
11608 are at most 128 directories, DW_FORM_udata would be as
11609 compact as that, but not shorter and slower to decode). */
11610 if (ndirs + idx_offset <= 256)
11611 idx_form = DW_FORM_data1;
11612 /* If there are more than 65536 directories, we have to use
11613 DW_FORM_udata, DW_FORM_data2 can't refer to them.
11614 Otherwise, compute what space would occupy if all the indexes
11615 used DW_FORM_udata - sum - and compare that to how large would
11616 be DW_FORM_data2 encoding, and pick the more efficient one. */
11617 else if (ndirs + idx_offset <= 65536)
11619 unsigned HOST_WIDE_INT sum = 1;
11620 for (i = 0; i < numfiles; i++)
11622 int file_idx = backmap[i];
11623 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
11624 sum += size_of_uleb128 (dir_idx);
11626 if (sum >= HOST_WIDE_INT_UC (2) * (numfiles + 1))
11627 idx_form = DW_FORM_data2;
11629 #ifdef VMS_DEBUGGING_INFO
11630 dw2_asm_output_data (1, 4, "File name entry format count");
11631 #else
11632 dw2_asm_output_data (1, 2, "File name entry format count");
11633 #endif
11634 dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
11635 dw2_asm_output_data_uleb128 (str_form, "%s",
11636 get_DW_FORM_name (str_form));
11637 dw2_asm_output_data_uleb128 (DW_LNCT_directory_index,
11638 "DW_LNCT_directory_index");
11639 dw2_asm_output_data_uleb128 (idx_form, "%s",
11640 get_DW_FORM_name (idx_form));
11641 #ifdef VMS_DEBUGGING_INFO
11642 dw2_asm_output_data_uleb128 (DW_LNCT_timestamp, "DW_LNCT_timestamp");
11643 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
11644 dw2_asm_output_data_uleb128 (DW_LNCT_size, "DW_LNCT_size");
11645 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
11646 #endif
11647 dw2_asm_output_data_uleb128 (numfiles + 1, "File names count");
11649 output_line_string (str_form, filename0, "File Entry", 0);
11651 /* Include directory index. */
11652 if (dwarf_version >= 5 && idx_form != DW_FORM_udata)
11653 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
11654 0, NULL);
11655 else
11656 dw2_asm_output_data_uleb128 (0, NULL);
11658 #ifdef VMS_DEBUGGING_INFO
11659 dw2_asm_output_data_uleb128 (0, NULL);
11660 dw2_asm_output_data_uleb128 (0, NULL);
11661 #endif
11664 /* Now write all the file names. */
11665 for (i = 0; i < numfiles; i++)
11667 int file_idx = backmap[i];
11668 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
11670 #ifdef VMS_DEBUGGING_INFO
11671 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
11673 /* Setting these fields can lead to debugger miscomparisons,
11674 but VMS Debug requires them to be set correctly. */
11676 int ver;
11677 long long cdt;
11678 long siz;
11679 int maxfilelen = (strlen (files[file_idx].path)
11680 + dirs[dir_idx].length
11681 + MAX_VMS_VERSION_LEN + 1);
11682 char *filebuf = XALLOCAVEC (char, maxfilelen);
11684 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
11685 snprintf (filebuf, maxfilelen, "%s;%d",
11686 files[file_idx].path + dirs[dir_idx].length, ver);
11688 output_line_string (str_form, filebuf, "File Entry", (unsigned) i + 1);
11690 /* Include directory index. */
11691 if (dwarf_version >= 5 && idx_form != DW_FORM_udata)
11692 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
11693 dir_idx + idx_offset, NULL);
11694 else
11695 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
11697 /* Modification time. */
11698 dw2_asm_output_data_uleb128 ((vms_file_stats_name (files[file_idx].path,
11699 &cdt, 0, 0, 0) == 0)
11700 ? cdt : 0, NULL);
11702 /* File length in bytes. */
11703 dw2_asm_output_data_uleb128 ((vms_file_stats_name (files[file_idx].path,
11704 0, &siz, 0, 0) == 0)
11705 ? siz : 0, NULL);
11706 #else
11707 output_line_string (str_form,
11708 files[file_idx].path + dirs[dir_idx].length,
11709 "File Entry", (unsigned) i + 1);
11711 /* Include directory index. */
11712 if (dwarf_version >= 5 && idx_form != DW_FORM_udata)
11713 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
11714 dir_idx + idx_offset, NULL);
11715 else
11716 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
11718 if (dwarf_version >= 5)
11719 continue;
11721 /* Modification time. */
11722 dw2_asm_output_data_uleb128 (0, NULL);
11724 /* File length in bytes. */
11725 dw2_asm_output_data_uleb128 (0, NULL);
11726 #endif /* VMS_DEBUGGING_INFO */
11729 if (dwarf_version < 5)
11730 dw2_asm_output_data (1, 0, "End file name table");
11734 /* Output one line number table into the .debug_line section. */
11736 static void
11737 output_one_line_info_table (dw_line_info_table *table)
11739 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
11740 unsigned int current_line = 1;
11741 bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
11742 dw_line_info_entry *ent;
11743 size_t i;
11745 FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
11747 switch (ent->opcode)
11749 case LI_set_address:
11750 /* ??? Unfortunately, we have little choice here currently, and
11751 must always use the most general form. GCC does not know the
11752 address delta itself, so we can't use DW_LNS_advance_pc. Many
11753 ports do have length attributes which will give an upper bound
11754 on the address range. We could perhaps use length attributes
11755 to determine when it is safe to use DW_LNS_fixed_advance_pc. */
11756 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
11758 /* This can handle any delta. This takes
11759 4+DWARF2_ADDR_SIZE bytes. */
11760 dw2_asm_output_data (1, 0, "set address %s", line_label);
11761 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
11762 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
11763 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
11764 break;
11766 case LI_set_line:
11767 if (ent->val == current_line)
11769 /* We still need to start a new row, so output a copy insn. */
11770 dw2_asm_output_data (1, DW_LNS_copy,
11771 "copy line %u", current_line);
11773 else
11775 int line_offset = ent->val - current_line;
11776 int line_delta = line_offset - DWARF_LINE_BASE;
11778 current_line = ent->val;
11779 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
11781 /* This can handle deltas from -10 to 234, using the current
11782 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
11783 This takes 1 byte. */
11784 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
11785 "line %u", current_line);
11787 else
11789 /* This can handle any delta. This takes at least 4 bytes,
11790 depending on the value being encoded. */
11791 dw2_asm_output_data (1, DW_LNS_advance_line,
11792 "advance to line %u", current_line);
11793 dw2_asm_output_data_sleb128 (line_offset, NULL);
11794 dw2_asm_output_data (1, DW_LNS_copy, NULL);
11797 break;
11799 case LI_set_file:
11800 dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
11801 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
11802 break;
11804 case LI_set_column:
11805 dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
11806 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
11807 break;
11809 case LI_negate_stmt:
11810 current_is_stmt = !current_is_stmt;
11811 dw2_asm_output_data (1, DW_LNS_negate_stmt,
11812 "is_stmt %d", current_is_stmt);
11813 break;
11815 case LI_set_prologue_end:
11816 dw2_asm_output_data (1, DW_LNS_set_prologue_end,
11817 "set prologue end");
11818 break;
11820 case LI_set_epilogue_begin:
11821 dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
11822 "set epilogue begin");
11823 break;
11825 case LI_set_discriminator:
11826 dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
11827 dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
11828 dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
11829 dw2_asm_output_data_uleb128 (ent->val, NULL);
11830 break;
11834 /* Emit debug info for the address of the end of the table. */
11835 dw2_asm_output_data (1, 0, "set address %s", table->end_label);
11836 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
11837 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
11838 dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
11840 dw2_asm_output_data (1, 0, "end sequence");
11841 dw2_asm_output_data_uleb128 (1, NULL);
11842 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
11845 /* Output the source line number correspondence information. This
11846 information goes into the .debug_line section. */
11848 static void
11849 output_line_info (bool prologue_only)
11851 static unsigned int generation;
11852 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
11853 char p1[MAX_ARTIFICIAL_LABEL_BYTES], p2[MAX_ARTIFICIAL_LABEL_BYTES];
11854 bool saw_one = false;
11855 int opc;
11857 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, generation);
11858 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, generation);
11859 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, generation);
11860 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, generation++);
11862 if (!XCOFF_DEBUGGING_INFO)
11864 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11865 dw2_asm_output_data (4, 0xffffffff,
11866 "Initial length escape value indicating 64-bit DWARF extension");
11867 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
11868 "Length of Source Line Info");
11871 ASM_OUTPUT_LABEL (asm_out_file, l1);
11873 dw2_asm_output_data (2, dwarf_version, "DWARF Version");
11874 if (dwarf_version >= 5)
11876 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
11877 dw2_asm_output_data (1, 0, "Segment Size");
11879 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
11880 ASM_OUTPUT_LABEL (asm_out_file, p1);
11882 /* Define the architecture-dependent minimum instruction length (in bytes).
11883 In this implementation of DWARF, this field is used for information
11884 purposes only. Since GCC generates assembly language, we have no
11885 a priori knowledge of how many instruction bytes are generated for each
11886 source line, and therefore can use only the DW_LNE_set_address and
11887 DW_LNS_fixed_advance_pc line information commands. Accordingly, we fix
11888 this as '1', which is "correct enough" for all architectures,
11889 and don't let the target override. */
11890 dw2_asm_output_data (1, 1, "Minimum Instruction Length");
11892 if (dwarf_version >= 4)
11893 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
11894 "Maximum Operations Per Instruction");
11895 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
11896 "Default is_stmt_start flag");
11897 dw2_asm_output_data (1, DWARF_LINE_BASE,
11898 "Line Base Value (Special Opcodes)");
11899 dw2_asm_output_data (1, DWARF_LINE_RANGE,
11900 "Line Range Value (Special Opcodes)");
11901 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
11902 "Special Opcode Base");
11904 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
11906 int n_op_args;
11907 switch (opc)
11909 case DW_LNS_advance_pc:
11910 case DW_LNS_advance_line:
11911 case DW_LNS_set_file:
11912 case DW_LNS_set_column:
11913 case DW_LNS_fixed_advance_pc:
11914 case DW_LNS_set_isa:
11915 n_op_args = 1;
11916 break;
11917 default:
11918 n_op_args = 0;
11919 break;
11922 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
11923 opc, n_op_args);
11926 /* Write out the information about the files we use. */
11927 output_file_names ();
11928 ASM_OUTPUT_LABEL (asm_out_file, p2);
11929 if (prologue_only)
11931 /* Output the marker for the end of the line number info. */
11932 ASM_OUTPUT_LABEL (asm_out_file, l2);
11933 return;
11936 if (separate_line_info)
11938 dw_line_info_table *table;
11939 size_t i;
11941 FOR_EACH_VEC_ELT (*separate_line_info, i, table)
11942 if (table->in_use)
11944 output_one_line_info_table (table);
11945 saw_one = true;
11948 if (cold_text_section_line_info && cold_text_section_line_info->in_use)
11950 output_one_line_info_table (cold_text_section_line_info);
11951 saw_one = true;
11954 /* ??? Some Darwin linkers crash on a .debug_line section with no
11955 sequences. Further, merely a DW_LNE_end_sequence entry is not
11956 sufficient -- the address column must also be initialized.
11957 Make sure to output at least one set_address/end_sequence pair,
11958 choosing .text since that section is always present. */
11959 if (text_section_line_info->in_use || !saw_one)
11960 output_one_line_info_table (text_section_line_info);
11962 /* Output the marker for the end of the line number info. */
11963 ASM_OUTPUT_LABEL (asm_out_file, l2);
11966 /* Return true if DW_AT_endianity should be emitted according to REVERSE. */
11968 static inline bool
11969 need_endianity_attribute_p (bool reverse)
11971 return reverse && (dwarf_version >= 3 || !dwarf_strict);
11974 /* Given a pointer to a tree node for some base type, return a pointer to
11975 a DIE that describes the given type. REVERSE is true if the type is
11976 to be interpreted in the reverse storage order wrt the target order.
11978 This routine must only be called for GCC type nodes that correspond to
11979 Dwarf base (fundamental) types. */
11981 static dw_die_ref
11982 base_type_die (tree type, bool reverse)
11984 dw_die_ref base_type_result;
11985 enum dwarf_type encoding;
11986 bool fpt_used = false;
11987 struct fixed_point_type_info fpt_info;
11988 tree type_bias = NULL_TREE;
11990 if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
11991 return 0;
11993 /* If this is a subtype that should not be emitted as a subrange type,
11994 use the base type. See subrange_type_for_debug_p. */
11995 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
11996 type = TREE_TYPE (type);
11998 switch (TREE_CODE (type))
12000 case INTEGER_TYPE:
12001 if ((dwarf_version >= 4 || !dwarf_strict)
12002 && TYPE_NAME (type)
12003 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12004 && DECL_IS_BUILTIN (TYPE_NAME (type))
12005 && DECL_NAME (TYPE_NAME (type)))
12007 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
12008 if (strcmp (name, "char16_t") == 0
12009 || strcmp (name, "char32_t") == 0)
12011 encoding = DW_ATE_UTF;
12012 break;
12015 if ((dwarf_version >= 3 || !dwarf_strict)
12016 && lang_hooks.types.get_fixed_point_type_info)
12018 memset (&fpt_info, 0, sizeof (fpt_info));
12019 if (lang_hooks.types.get_fixed_point_type_info (type, &fpt_info))
12021 fpt_used = true;
12022 encoding = ((TYPE_UNSIGNED (type))
12023 ? DW_ATE_unsigned_fixed
12024 : DW_ATE_signed_fixed);
12025 break;
12028 if (TYPE_STRING_FLAG (type))
12030 if (TYPE_UNSIGNED (type))
12031 encoding = DW_ATE_unsigned_char;
12032 else
12033 encoding = DW_ATE_signed_char;
12035 else if (TYPE_UNSIGNED (type))
12036 encoding = DW_ATE_unsigned;
12037 else
12038 encoding = DW_ATE_signed;
12040 if (!dwarf_strict
12041 && lang_hooks.types.get_type_bias)
12042 type_bias = lang_hooks.types.get_type_bias (type);
12043 break;
12045 case REAL_TYPE:
12046 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
12048 if (dwarf_version >= 3 || !dwarf_strict)
12049 encoding = DW_ATE_decimal_float;
12050 else
12051 encoding = DW_ATE_lo_user;
12053 else
12054 encoding = DW_ATE_float;
12055 break;
12057 case FIXED_POINT_TYPE:
12058 if (!(dwarf_version >= 3 || !dwarf_strict))
12059 encoding = DW_ATE_lo_user;
12060 else if (TYPE_UNSIGNED (type))
12061 encoding = DW_ATE_unsigned_fixed;
12062 else
12063 encoding = DW_ATE_signed_fixed;
12064 break;
12066 /* Dwarf2 doesn't know anything about complex ints, so use
12067 a user defined type for it. */
12068 case COMPLEX_TYPE:
12069 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
12070 encoding = DW_ATE_complex_float;
12071 else
12072 encoding = DW_ATE_lo_user;
12073 break;
12075 case BOOLEAN_TYPE:
12076 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
12077 encoding = DW_ATE_boolean;
12078 break;
12080 default:
12081 /* No other TREE_CODEs are Dwarf fundamental types. */
12082 gcc_unreachable ();
12085 base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
12087 add_AT_unsigned (base_type_result, DW_AT_byte_size,
12088 int_size_in_bytes (type));
12089 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
12091 if (need_endianity_attribute_p (reverse))
12092 add_AT_unsigned (base_type_result, DW_AT_endianity,
12093 BYTES_BIG_ENDIAN ? DW_END_little : DW_END_big);
12095 if (fpt_used)
12097 switch (fpt_info.scale_factor_kind)
12099 case fixed_point_scale_factor_binary:
12100 add_AT_int (base_type_result, DW_AT_binary_scale,
12101 fpt_info.scale_factor.binary);
12102 break;
12104 case fixed_point_scale_factor_decimal:
12105 add_AT_int (base_type_result, DW_AT_decimal_scale,
12106 fpt_info.scale_factor.decimal);
12107 break;
12109 case fixed_point_scale_factor_arbitrary:
12110 /* Arbitrary scale factors cannot be described in standard DWARF,
12111 yet. */
12112 if (!dwarf_strict)
12114 /* Describe the scale factor as a rational constant. */
12115 const dw_die_ref scale_factor
12116 = new_die (DW_TAG_constant, comp_unit_die (), type);
12118 add_AT_unsigned (scale_factor, DW_AT_GNU_numerator,
12119 fpt_info.scale_factor.arbitrary.numerator);
12120 add_AT_int (scale_factor, DW_AT_GNU_denominator,
12121 fpt_info.scale_factor.arbitrary.denominator);
12123 add_AT_die_ref (base_type_result, DW_AT_small, scale_factor);
12125 break;
12127 default:
12128 gcc_unreachable ();
12132 if (type_bias)
12133 add_scalar_info (base_type_result, DW_AT_GNU_bias, type_bias,
12134 dw_scalar_form_constant
12135 | dw_scalar_form_exprloc
12136 | dw_scalar_form_reference,
12137 NULL);
12139 add_pubtype (type, base_type_result);
12141 return base_type_result;
12144 /* A C++ function with deduced return type can have a TEMPLATE_TYPE_PARM
12145 named 'auto' in its type: return true for it, false otherwise. */
12147 static inline bool
12148 is_cxx_auto (tree type)
12150 if (is_cxx ())
12152 tree name = TYPE_IDENTIFIER (type);
12153 if (name == get_identifier ("auto")
12154 || name == get_identifier ("decltype(auto)"))
12155 return true;
12157 return false;
12160 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
12161 given input type is a Dwarf "fundamental" type. Otherwise return null. */
12163 static inline int
12164 is_base_type (tree type)
12166 switch (TREE_CODE (type))
12168 case ERROR_MARK:
12169 case VOID_TYPE:
12170 case INTEGER_TYPE:
12171 case REAL_TYPE:
12172 case FIXED_POINT_TYPE:
12173 case COMPLEX_TYPE:
12174 case BOOLEAN_TYPE:
12175 case POINTER_BOUNDS_TYPE:
12176 return 1;
12178 case ARRAY_TYPE:
12179 case RECORD_TYPE:
12180 case UNION_TYPE:
12181 case QUAL_UNION_TYPE:
12182 case ENUMERAL_TYPE:
12183 case FUNCTION_TYPE:
12184 case METHOD_TYPE:
12185 case POINTER_TYPE:
12186 case REFERENCE_TYPE:
12187 case NULLPTR_TYPE:
12188 case OFFSET_TYPE:
12189 case LANG_TYPE:
12190 case VECTOR_TYPE:
12191 return 0;
12193 default:
12194 if (is_cxx_auto (type))
12195 return 0;
12196 gcc_unreachable ();
12199 return 0;
12202 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
12203 node, return the size in bits for the type if it is a constant, or else
12204 return the alignment for the type if the type's size is not constant, or
12205 else return BITS_PER_WORD if the type actually turns out to be an
12206 ERROR_MARK node. */
12208 static inline unsigned HOST_WIDE_INT
12209 simple_type_size_in_bits (const_tree type)
12211 if (TREE_CODE (type) == ERROR_MARK)
12212 return BITS_PER_WORD;
12213 else if (TYPE_SIZE (type) == NULL_TREE)
12214 return 0;
12215 else if (tree_fits_uhwi_p (TYPE_SIZE (type)))
12216 return tree_to_uhwi (TYPE_SIZE (type));
12217 else
12218 return TYPE_ALIGN (type);
12221 /* Similarly, but return an offset_int instead of UHWI. */
12223 static inline offset_int
12224 offset_int_type_size_in_bits (const_tree type)
12226 if (TREE_CODE (type) == ERROR_MARK)
12227 return BITS_PER_WORD;
12228 else if (TYPE_SIZE (type) == NULL_TREE)
12229 return 0;
12230 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
12231 return wi::to_offset (TYPE_SIZE (type));
12232 else
12233 return TYPE_ALIGN (type);
12236 /* Given a pointer to a tree node for a subrange type, return a pointer
12237 to a DIE that describes the given type. */
12239 static dw_die_ref
12240 subrange_type_die (tree type, tree low, tree high, tree bias,
12241 dw_die_ref context_die)
12243 dw_die_ref subrange_die;
12244 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
12246 if (context_die == NULL)
12247 context_die = comp_unit_die ();
12249 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
12251 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
12253 /* The size of the subrange type and its base type do not match,
12254 so we need to generate a size attribute for the subrange type. */
12255 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
12258 if (low)
12259 add_bound_info (subrange_die, DW_AT_lower_bound, low, NULL);
12260 if (high)
12261 add_bound_info (subrange_die, DW_AT_upper_bound, high, NULL);
12262 if (bias && !dwarf_strict)
12263 add_scalar_info (subrange_die, DW_AT_GNU_bias, bias,
12264 dw_scalar_form_constant
12265 | dw_scalar_form_exprloc
12266 | dw_scalar_form_reference,
12267 NULL);
12269 return subrange_die;
12272 /* Returns the (const and/or volatile) cv_qualifiers associated with
12273 the decl node. This will normally be augmented with the
12274 cv_qualifiers of the underlying type in add_type_attribute. */
12276 static int
12277 decl_quals (const_tree decl)
12279 return ((TREE_READONLY (decl)
12280 /* The C++ front-end correctly marks reference-typed
12281 variables as readonly, but from a language (and debug
12282 info) standpoint they are not const-qualified. */
12283 && TREE_CODE (TREE_TYPE (decl)) != REFERENCE_TYPE
12284 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED)
12285 | (TREE_THIS_VOLATILE (decl)
12286 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED));
12289 /* Determine the TYPE whose qualifiers match the largest strict subset
12290 of the given TYPE_QUALS, and return its qualifiers. Ignore all
12291 qualifiers outside QUAL_MASK. */
12293 static int
12294 get_nearest_type_subqualifiers (tree type, int type_quals, int qual_mask)
12296 tree t;
12297 int best_rank = 0, best_qual = 0, max_rank;
12299 type_quals &= qual_mask;
12300 max_rank = popcount_hwi (type_quals) - 1;
12302 for (t = TYPE_MAIN_VARIANT (type); t && best_rank < max_rank;
12303 t = TYPE_NEXT_VARIANT (t))
12305 int q = TYPE_QUALS (t) & qual_mask;
12307 if ((q & type_quals) == q && q != type_quals
12308 && check_base_type (t, type))
12310 int rank = popcount_hwi (q);
12312 if (rank > best_rank)
12314 best_rank = rank;
12315 best_qual = q;
12320 return best_qual;
12323 struct dwarf_qual_info_t { int q; enum dwarf_tag t; };
12324 static const dwarf_qual_info_t dwarf_qual_info[] =
12326 { TYPE_QUAL_CONST, DW_TAG_const_type },
12327 { TYPE_QUAL_VOLATILE, DW_TAG_volatile_type },
12328 { TYPE_QUAL_RESTRICT, DW_TAG_restrict_type },
12329 { TYPE_QUAL_ATOMIC, DW_TAG_atomic_type }
12331 static const unsigned int dwarf_qual_info_size
12332 = sizeof (dwarf_qual_info) / sizeof (dwarf_qual_info[0]);
12334 /* If DIE is a qualified DIE of some base DIE with the same parent,
12335 return the base DIE, otherwise return NULL. Set MASK to the
12336 qualifiers added compared to the returned DIE. */
12338 static dw_die_ref
12339 qualified_die_p (dw_die_ref die, int *mask, unsigned int depth)
12341 unsigned int i;
12342 for (i = 0; i < dwarf_qual_info_size; i++)
12343 if (die->die_tag == dwarf_qual_info[i].t)
12344 break;
12345 if (i == dwarf_qual_info_size)
12346 return NULL;
12347 if (vec_safe_length (die->die_attr) != 1)
12348 return NULL;
12349 dw_die_ref type = get_AT_ref (die, DW_AT_type);
12350 if (type == NULL || type->die_parent != die->die_parent)
12351 return NULL;
12352 *mask |= dwarf_qual_info[i].q;
12353 if (depth)
12355 dw_die_ref ret = qualified_die_p (type, mask, depth - 1);
12356 if (ret)
12357 return ret;
12359 return type;
12362 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
12363 entry that chains the modifiers specified by CV_QUALS in front of the
12364 given type. REVERSE is true if the type is to be interpreted in the
12365 reverse storage order wrt the target order. */
12367 static dw_die_ref
12368 modified_type_die (tree type, int cv_quals, bool reverse,
12369 dw_die_ref context_die)
12371 enum tree_code code = TREE_CODE (type);
12372 dw_die_ref mod_type_die;
12373 dw_die_ref sub_die = NULL;
12374 tree item_type = NULL;
12375 tree qualified_type;
12376 tree name, low, high;
12377 dw_die_ref mod_scope;
12378 /* Only these cv-qualifiers are currently handled. */
12379 const int cv_qual_mask = (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE
12380 | TYPE_QUAL_RESTRICT | TYPE_QUAL_ATOMIC);
12382 if (code == ERROR_MARK)
12383 return NULL;
12385 if (lang_hooks.types.get_debug_type)
12387 tree debug_type = lang_hooks.types.get_debug_type (type);
12389 if (debug_type != NULL_TREE && debug_type != type)
12390 return modified_type_die (debug_type, cv_quals, reverse, context_die);
12393 cv_quals &= cv_qual_mask;
12395 /* Don't emit DW_TAG_restrict_type for DWARFv2, since it is a type
12396 tag modifier (and not an attribute) old consumers won't be able
12397 to handle it. */
12398 if (dwarf_version < 3)
12399 cv_quals &= ~TYPE_QUAL_RESTRICT;
12401 /* Likewise for DW_TAG_atomic_type for DWARFv5. */
12402 if (dwarf_version < 5)
12403 cv_quals &= ~TYPE_QUAL_ATOMIC;
12405 /* See if we already have the appropriately qualified variant of
12406 this type. */
12407 qualified_type = get_qualified_type (type, cv_quals);
12409 if (qualified_type == sizetype
12410 && TYPE_NAME (qualified_type)
12411 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
12413 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
12415 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
12416 && TYPE_PRECISION (t)
12417 == TYPE_PRECISION (qualified_type)
12418 && TYPE_UNSIGNED (t)
12419 == TYPE_UNSIGNED (qualified_type));
12420 qualified_type = t;
12423 /* If we do, then we can just use its DIE, if it exists. */
12424 if (qualified_type)
12426 mod_type_die = lookup_type_die (qualified_type);
12428 /* DW_AT_endianity doesn't come from a qualifier on the type. */
12429 if (mod_type_die
12430 && (!need_endianity_attribute_p (reverse)
12431 || !is_base_type (type)
12432 || get_AT_unsigned (mod_type_die, DW_AT_endianity)))
12433 return mod_type_die;
12436 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
12438 /* Handle C typedef types. */
12439 if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
12440 && !DECL_ARTIFICIAL (name))
12442 tree dtype = TREE_TYPE (name);
12444 if (qualified_type == dtype)
12446 /* For a named type, use the typedef. */
12447 gen_type_die (qualified_type, context_die);
12448 return lookup_type_die (qualified_type);
12450 else
12452 int dquals = TYPE_QUALS_NO_ADDR_SPACE (dtype);
12453 dquals &= cv_qual_mask;
12454 if ((dquals & ~cv_quals) != TYPE_UNQUALIFIED
12455 || (cv_quals == dquals && DECL_ORIGINAL_TYPE (name) != type))
12456 /* cv-unqualified version of named type. Just use
12457 the unnamed type to which it refers. */
12458 return modified_type_die (DECL_ORIGINAL_TYPE (name), cv_quals,
12459 reverse, context_die);
12460 /* Else cv-qualified version of named type; fall through. */
12464 mod_scope = scope_die_for (type, context_die);
12466 if (cv_quals)
12468 int sub_quals = 0, first_quals = 0;
12469 unsigned i;
12470 dw_die_ref first = NULL, last = NULL;
12472 /* Determine a lesser qualified type that most closely matches
12473 this one. Then generate DW_TAG_* entries for the remaining
12474 qualifiers. */
12475 sub_quals = get_nearest_type_subqualifiers (type, cv_quals,
12476 cv_qual_mask);
12477 if (sub_quals && use_debug_types)
12479 bool needed = false;
12480 /* If emitting type units, make sure the order of qualifiers
12481 is canonical. Thus, start from unqualified type if
12482 an earlier qualifier is missing in sub_quals, but some later
12483 one is present there. */
12484 for (i = 0; i < dwarf_qual_info_size; i++)
12485 if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
12486 needed = true;
12487 else if (needed && (dwarf_qual_info[i].q & cv_quals))
12489 sub_quals = 0;
12490 break;
12493 mod_type_die = modified_type_die (type, sub_quals, reverse, context_die);
12494 if (mod_scope && mod_type_die && mod_type_die->die_parent == mod_scope)
12496 /* As not all intermediate qualified DIEs have corresponding
12497 tree types, ensure that qualified DIEs in the same scope
12498 as their DW_AT_type are emitted after their DW_AT_type,
12499 only with other qualified DIEs for the same type possibly
12500 in between them. Determine the range of such qualified
12501 DIEs now (first being the base type, last being corresponding
12502 last qualified DIE for it). */
12503 unsigned int count = 0;
12504 first = qualified_die_p (mod_type_die, &first_quals,
12505 dwarf_qual_info_size);
12506 if (first == NULL)
12507 first = mod_type_die;
12508 gcc_assert ((first_quals & ~sub_quals) == 0);
12509 for (count = 0, last = first;
12510 count < (1U << dwarf_qual_info_size);
12511 count++, last = last->die_sib)
12513 int quals = 0;
12514 if (last == mod_scope->die_child)
12515 break;
12516 if (qualified_die_p (last->die_sib, &quals, dwarf_qual_info_size)
12517 != first)
12518 break;
12522 for (i = 0; i < dwarf_qual_info_size; i++)
12523 if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
12525 dw_die_ref d;
12526 if (first && first != last)
12528 for (d = first->die_sib; ; d = d->die_sib)
12530 int quals = 0;
12531 qualified_die_p (d, &quals, dwarf_qual_info_size);
12532 if (quals == (first_quals | dwarf_qual_info[i].q))
12533 break;
12534 if (d == last)
12536 d = NULL;
12537 break;
12540 if (d)
12542 mod_type_die = d;
12543 continue;
12546 if (first)
12548 d = ggc_cleared_alloc<die_node> ();
12549 d->die_tag = dwarf_qual_info[i].t;
12550 add_child_die_after (mod_scope, d, last);
12551 last = d;
12553 else
12554 d = new_die (dwarf_qual_info[i].t, mod_scope, type);
12555 if (mod_type_die)
12556 add_AT_die_ref (d, DW_AT_type, mod_type_die);
12557 mod_type_die = d;
12558 first_quals |= dwarf_qual_info[i].q;
12561 else if (code == POINTER_TYPE || code == REFERENCE_TYPE)
12563 dwarf_tag tag = DW_TAG_pointer_type;
12564 if (code == REFERENCE_TYPE)
12566 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
12567 tag = DW_TAG_rvalue_reference_type;
12568 else
12569 tag = DW_TAG_reference_type;
12571 mod_type_die = new_die (tag, mod_scope, type);
12573 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
12574 simple_type_size_in_bits (type) / BITS_PER_UNIT);
12575 item_type = TREE_TYPE (type);
12577 addr_space_t as = TYPE_ADDR_SPACE (item_type);
12578 if (!ADDR_SPACE_GENERIC_P (as))
12580 int action = targetm.addr_space.debug (as);
12581 if (action >= 0)
12583 /* Positive values indicate an address_class. */
12584 add_AT_unsigned (mod_type_die, DW_AT_address_class, action);
12586 else
12588 /* Negative values indicate an (inverted) segment base reg. */
12589 dw_loc_descr_ref d
12590 = one_reg_loc_descriptor (~action, VAR_INIT_STATUS_INITIALIZED);
12591 add_AT_loc (mod_type_die, DW_AT_segment, d);
12595 else if (code == INTEGER_TYPE
12596 && TREE_TYPE (type) != NULL_TREE
12597 && subrange_type_for_debug_p (type, &low, &high))
12599 tree bias = NULL_TREE;
12600 if (lang_hooks.types.get_type_bias)
12601 bias = lang_hooks.types.get_type_bias (type);
12602 mod_type_die = subrange_type_die (type, low, high, bias, context_die);
12603 item_type = TREE_TYPE (type);
12605 else if (is_base_type (type))
12606 mod_type_die = base_type_die (type, reverse);
12607 else
12609 gen_type_die (type, context_die);
12611 /* We have to get the type_main_variant here (and pass that to the
12612 `lookup_type_die' routine) because the ..._TYPE node we have
12613 might simply be a *copy* of some original type node (where the
12614 copy was created to help us keep track of typedef names) and
12615 that copy might have a different TYPE_UID from the original
12616 ..._TYPE node. */
12617 if (TREE_CODE (type) == FUNCTION_TYPE
12618 || TREE_CODE (type) == METHOD_TYPE)
12620 /* For function/method types, can't just use type_main_variant here,
12621 because that can have different ref-qualifiers for C++,
12622 but try to canonicalize. */
12623 tree main = TYPE_MAIN_VARIANT (type);
12624 for (tree t = main; t; t = TYPE_NEXT_VARIANT (t))
12625 if (check_base_type (t, main) && check_lang_type (t, type))
12626 return lookup_type_die (t);
12627 return lookup_type_die (type);
12629 else if (TREE_CODE (type) != VECTOR_TYPE
12630 && TREE_CODE (type) != ARRAY_TYPE)
12631 return lookup_type_die (type_main_variant (type));
12632 else
12633 /* Vectors have the debugging information in the type,
12634 not the main variant. */
12635 return lookup_type_die (type);
12638 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
12639 don't output a DW_TAG_typedef, since there isn't one in the
12640 user's program; just attach a DW_AT_name to the type.
12641 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
12642 if the base type already has the same name. */
12643 if (name
12644 && ((TREE_CODE (name) != TYPE_DECL
12645 && (qualified_type == TYPE_MAIN_VARIANT (type)
12646 || (cv_quals == TYPE_UNQUALIFIED)))
12647 || (TREE_CODE (name) == TYPE_DECL
12648 && TREE_TYPE (name) == qualified_type
12649 && DECL_NAME (name))))
12651 if (TREE_CODE (name) == TYPE_DECL)
12652 /* Could just call add_name_and_src_coords_attributes here,
12653 but since this is a builtin type it doesn't have any
12654 useful source coordinates anyway. */
12655 name = DECL_NAME (name);
12656 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
12658 /* This probably indicates a bug. */
12659 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
12661 name = TYPE_IDENTIFIER (type);
12662 add_name_attribute (mod_type_die,
12663 name ? IDENTIFIER_POINTER (name) : "__unknown__");
12666 if (qualified_type)
12667 equate_type_number_to_die (qualified_type, mod_type_die);
12669 if (item_type)
12670 /* We must do this after the equate_type_number_to_die call, in case
12671 this is a recursive type. This ensures that the modified_type_die
12672 recursion will terminate even if the type is recursive. Recursive
12673 types are possible in Ada. */
12674 sub_die = modified_type_die (item_type,
12675 TYPE_QUALS_NO_ADDR_SPACE (item_type),
12676 reverse,
12677 context_die);
12679 if (sub_die != NULL)
12680 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
12682 add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
12683 if (TYPE_ARTIFICIAL (type))
12684 add_AT_flag (mod_type_die, DW_AT_artificial, 1);
12686 return mod_type_die;
12689 /* Generate DIEs for the generic parameters of T.
12690 T must be either a generic type or a generic function.
12691 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
12693 static void
12694 gen_generic_params_dies (tree t)
12696 tree parms, args;
12697 int parms_num, i;
12698 dw_die_ref die = NULL;
12699 int non_default;
12701 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
12702 return;
12704 if (TYPE_P (t))
12705 die = lookup_type_die (t);
12706 else if (DECL_P (t))
12707 die = lookup_decl_die (t);
12709 gcc_assert (die);
12711 parms = lang_hooks.get_innermost_generic_parms (t);
12712 if (!parms)
12713 /* T has no generic parameter. It means T is neither a generic type
12714 or function. End of story. */
12715 return;
12717 parms_num = TREE_VEC_LENGTH (parms);
12718 args = lang_hooks.get_innermost_generic_args (t);
12719 if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
12720 non_default = int_cst_value (TREE_CHAIN (args));
12721 else
12722 non_default = TREE_VEC_LENGTH (args);
12723 for (i = 0; i < parms_num; i++)
12725 tree parm, arg, arg_pack_elems;
12726 dw_die_ref parm_die;
12728 parm = TREE_VEC_ELT (parms, i);
12729 arg = TREE_VEC_ELT (args, i);
12730 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
12731 gcc_assert (parm && TREE_VALUE (parm) && arg);
12733 if (parm && TREE_VALUE (parm) && arg)
12735 /* If PARM represents a template parameter pack,
12736 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
12737 by DW_TAG_template_*_parameter DIEs for the argument
12738 pack elements of ARG. Note that ARG would then be
12739 an argument pack. */
12740 if (arg_pack_elems)
12741 parm_die = template_parameter_pack_die (TREE_VALUE (parm),
12742 arg_pack_elems,
12743 die);
12744 else
12745 parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
12746 true /* emit name */, die);
12747 if (i >= non_default)
12748 add_AT_flag (parm_die, DW_AT_default_value, 1);
12753 /* Create and return a DIE for PARM which should be
12754 the representation of a generic type parameter.
12755 For instance, in the C++ front end, PARM would be a template parameter.
12756 ARG is the argument to PARM.
12757 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
12758 name of the PARM.
12759 PARENT_DIE is the parent DIE which the new created DIE should be added to,
12760 as a child node. */
12762 static dw_die_ref
12763 generic_parameter_die (tree parm, tree arg,
12764 bool emit_name_p,
12765 dw_die_ref parent_die)
12767 dw_die_ref tmpl_die = NULL;
12768 const char *name = NULL;
12770 if (!parm || !DECL_NAME (parm) || !arg)
12771 return NULL;
12773 /* We support non-type generic parameters and arguments,
12774 type generic parameters and arguments, as well as
12775 generic generic parameters (a.k.a. template template parameters in C++)
12776 and arguments. */
12777 if (TREE_CODE (parm) == PARM_DECL)
12778 /* PARM is a nontype generic parameter */
12779 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
12780 else if (TREE_CODE (parm) == TYPE_DECL)
12781 /* PARM is a type generic parameter. */
12782 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
12783 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
12784 /* PARM is a generic generic parameter.
12785 Its DIE is a GNU extension. It shall have a
12786 DW_AT_name attribute to represent the name of the template template
12787 parameter, and a DW_AT_GNU_template_name attribute to represent the
12788 name of the template template argument. */
12789 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
12790 parent_die, parm);
12791 else
12792 gcc_unreachable ();
12794 if (tmpl_die)
12796 tree tmpl_type;
12798 /* If PARM is a generic parameter pack, it means we are
12799 emitting debug info for a template argument pack element.
12800 In other terms, ARG is a template argument pack element.
12801 In that case, we don't emit any DW_AT_name attribute for
12802 the die. */
12803 if (emit_name_p)
12805 name = IDENTIFIER_POINTER (DECL_NAME (parm));
12806 gcc_assert (name);
12807 add_AT_string (tmpl_die, DW_AT_name, name);
12810 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
12812 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
12813 TMPL_DIE should have a child DW_AT_type attribute that is set
12814 to the type of the argument to PARM, which is ARG.
12815 If PARM is a type generic parameter, TMPL_DIE should have a
12816 child DW_AT_type that is set to ARG. */
12817 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
12818 add_type_attribute (tmpl_die, tmpl_type,
12819 (TREE_THIS_VOLATILE (tmpl_type)
12820 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED),
12821 false, parent_die);
12823 else
12825 /* So TMPL_DIE is a DIE representing a
12826 a generic generic template parameter, a.k.a template template
12827 parameter in C++ and arg is a template. */
12829 /* The DW_AT_GNU_template_name attribute of the DIE must be set
12830 to the name of the argument. */
12831 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
12832 if (name)
12833 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
12836 if (TREE_CODE (parm) == PARM_DECL)
12837 /* So PARM is a non-type generic parameter.
12838 DWARF3 5.6.8 says we must set a DW_AT_const_value child
12839 attribute of TMPL_DIE which value represents the value
12840 of ARG.
12841 We must be careful here:
12842 The value of ARG might reference some function decls.
12843 We might currently be emitting debug info for a generic
12844 type and types are emitted before function decls, we don't
12845 know if the function decls referenced by ARG will actually be
12846 emitted after cgraph computations.
12847 So must defer the generation of the DW_AT_const_value to
12848 after cgraph is ready. */
12849 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
12852 return tmpl_die;
12855 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
12856 PARM_PACK must be a template parameter pack. The returned DIE
12857 will be child DIE of PARENT_DIE. */
12859 static dw_die_ref
12860 template_parameter_pack_die (tree parm_pack,
12861 tree parm_pack_args,
12862 dw_die_ref parent_die)
12864 dw_die_ref die;
12865 int j;
12867 gcc_assert (parent_die && parm_pack);
12869 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
12870 add_name_and_src_coords_attributes (die, parm_pack);
12871 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
12872 generic_parameter_die (parm_pack,
12873 TREE_VEC_ELT (parm_pack_args, j),
12874 false /* Don't emit DW_AT_name */,
12875 die);
12876 return die;
12879 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
12880 an enumerated type. */
12882 static inline int
12883 type_is_enum (const_tree type)
12885 return TREE_CODE (type) == ENUMERAL_TYPE;
12888 /* Return the DBX register number described by a given RTL node. */
12890 static unsigned int
12891 dbx_reg_number (const_rtx rtl)
12893 unsigned regno = REGNO (rtl);
12895 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
12897 #ifdef LEAF_REG_REMAP
12898 if (crtl->uses_only_leaf_regs)
12900 int leaf_reg = LEAF_REG_REMAP (regno);
12901 if (leaf_reg != -1)
12902 regno = (unsigned) leaf_reg;
12904 #endif
12906 regno = DBX_REGISTER_NUMBER (regno);
12907 gcc_assert (regno != INVALID_REGNUM);
12908 return regno;
12911 /* Optionally add a DW_OP_piece term to a location description expression.
12912 DW_OP_piece is only added if the location description expression already
12913 doesn't end with DW_OP_piece. */
12915 static void
12916 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
12918 dw_loc_descr_ref loc;
12920 if (*list_head != NULL)
12922 /* Find the end of the chain. */
12923 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
12926 if (loc->dw_loc_opc != DW_OP_piece)
12927 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
12931 /* Return a location descriptor that designates a machine register or
12932 zero if there is none. */
12934 static dw_loc_descr_ref
12935 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
12937 rtx regs;
12939 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
12940 return 0;
12942 /* We only use "frame base" when we're sure we're talking about the
12943 post-prologue local stack frame. We do this by *not* running
12944 register elimination until this point, and recognizing the special
12945 argument pointer and soft frame pointer rtx's.
12946 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
12947 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
12948 && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
12950 dw_loc_descr_ref result = NULL;
12952 if (dwarf_version >= 4 || !dwarf_strict)
12954 result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
12955 initialized);
12956 if (result)
12957 add_loc_descr (&result,
12958 new_loc_descr (DW_OP_stack_value, 0, 0));
12960 return result;
12963 regs = targetm.dwarf_register_span (rtl);
12965 if (REG_NREGS (rtl) > 1 || regs)
12966 return multiple_reg_loc_descriptor (rtl, regs, initialized);
12967 else
12969 unsigned int dbx_regnum = dbx_reg_number (rtl);
12970 if (dbx_regnum == IGNORED_DWARF_REGNUM)
12971 return 0;
12972 return one_reg_loc_descriptor (dbx_regnum, initialized);
12976 /* Return a location descriptor that designates a machine register for
12977 a given hard register number. */
12979 static dw_loc_descr_ref
12980 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
12982 dw_loc_descr_ref reg_loc_descr;
12984 if (regno <= 31)
12985 reg_loc_descr
12986 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
12987 else
12988 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
12990 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
12991 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12993 return reg_loc_descr;
12996 /* Given an RTL of a register, return a location descriptor that
12997 designates a value that spans more than one register. */
12999 static dw_loc_descr_ref
13000 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
13001 enum var_init_status initialized)
13003 int size, i;
13004 dw_loc_descr_ref loc_result = NULL;
13006 /* Simple, contiguous registers. */
13007 if (regs == NULL_RTX)
13009 unsigned reg = REGNO (rtl);
13010 int nregs;
13012 #ifdef LEAF_REG_REMAP
13013 if (crtl->uses_only_leaf_regs)
13015 int leaf_reg = LEAF_REG_REMAP (reg);
13016 if (leaf_reg != -1)
13017 reg = (unsigned) leaf_reg;
13019 #endif
13021 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
13022 nregs = REG_NREGS (rtl);
13024 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
13026 loc_result = NULL;
13027 while (nregs--)
13029 dw_loc_descr_ref t;
13031 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
13032 VAR_INIT_STATUS_INITIALIZED);
13033 add_loc_descr (&loc_result, t);
13034 add_loc_descr_op_piece (&loc_result, size);
13035 ++reg;
13037 return loc_result;
13040 /* Now onto stupid register sets in non contiguous locations. */
13042 gcc_assert (GET_CODE (regs) == PARALLEL);
13044 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
13045 loc_result = NULL;
13047 for (i = 0; i < XVECLEN (regs, 0); ++i)
13049 dw_loc_descr_ref t;
13051 t = one_reg_loc_descriptor (dbx_reg_number (XVECEXP (regs, 0, i)),
13052 VAR_INIT_STATUS_INITIALIZED);
13053 add_loc_descr (&loc_result, t);
13054 add_loc_descr_op_piece (&loc_result, size);
13057 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13058 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13059 return loc_result;
13062 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
13064 /* Return a location descriptor that designates a constant i,
13065 as a compound operation from constant (i >> shift), constant shift
13066 and DW_OP_shl. */
13068 static dw_loc_descr_ref
13069 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
13071 dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
13072 add_loc_descr (&ret, int_loc_descriptor (shift));
13073 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
13074 return ret;
13077 /* Return a location descriptor that designates a constant. */
13079 static dw_loc_descr_ref
13080 int_loc_descriptor (HOST_WIDE_INT i)
13082 enum dwarf_location_atom op;
13084 /* Pick the smallest representation of a constant, rather than just
13085 defaulting to the LEB encoding. */
13086 if (i >= 0)
13088 int clz = clz_hwi (i);
13089 int ctz = ctz_hwi (i);
13090 if (i <= 31)
13091 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
13092 else if (i <= 0xff)
13093 op = DW_OP_const1u;
13094 else if (i <= 0xffff)
13095 op = DW_OP_const2u;
13096 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
13097 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
13098 /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
13099 DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
13100 while DW_OP_const4u is 5 bytes. */
13101 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
13102 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13103 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
13104 /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
13105 while DW_OP_const4u is 5 bytes. */
13106 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
13108 else if (DWARF2_ADDR_SIZE == 4 && i > 0x7fffffff
13109 && size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i)
13110 <= 4)
13112 /* As i >= 2**31, the double cast above will yield a negative number.
13113 Since wrapping is defined in DWARF expressions we can output big
13114 positive integers as small negative ones, regardless of the size
13115 of host wide ints.
13117 Here, since the evaluator will handle 32-bit values and since i >=
13118 2**31, we know it's going to be interpreted as a negative literal:
13119 store it this way if we can do better than 5 bytes this way. */
13120 return int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i);
13122 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
13123 op = DW_OP_const4u;
13125 /* Past this point, i >= 0x100000000 and thus DW_OP_constu will take at
13126 least 6 bytes: see if we can do better before falling back to it. */
13127 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13128 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
13129 /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes. */
13130 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
13131 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
13132 && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
13133 >= HOST_BITS_PER_WIDE_INT)
13134 /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
13135 DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes. */
13136 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
13137 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
13138 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
13139 && size_of_uleb128 (i) > 6)
13140 /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes. */
13141 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
13142 else
13143 op = DW_OP_constu;
13145 else
13147 if (i >= -0x80)
13148 op = DW_OP_const1s;
13149 else if (i >= -0x8000)
13150 op = DW_OP_const2s;
13151 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
13153 if (size_of_int_loc_descriptor (i) < 5)
13155 dw_loc_descr_ref ret = int_loc_descriptor (-i);
13156 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
13157 return ret;
13159 op = DW_OP_const4s;
13161 else
13163 if (size_of_int_loc_descriptor (i)
13164 < (unsigned long) 1 + size_of_sleb128 (i))
13166 dw_loc_descr_ref ret = int_loc_descriptor (-i);
13167 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
13168 return ret;
13170 op = DW_OP_consts;
13174 return new_loc_descr (op, i, 0);
13177 /* Likewise, for unsigned constants. */
13179 static dw_loc_descr_ref
13180 uint_loc_descriptor (unsigned HOST_WIDE_INT i)
13182 const unsigned HOST_WIDE_INT max_int = INTTYPE_MAXIMUM (HOST_WIDE_INT);
13183 const unsigned HOST_WIDE_INT max_uint
13184 = INTTYPE_MAXIMUM (unsigned HOST_WIDE_INT);
13186 /* If possible, use the clever signed constants handling. */
13187 if (i <= max_int)
13188 return int_loc_descriptor ((HOST_WIDE_INT) i);
13190 /* Here, we are left with positive numbers that cannot be represented as
13191 HOST_WIDE_INT, i.e.:
13192 max (HOST_WIDE_INT) < i <= max (unsigned HOST_WIDE_INT)
13194 Using DW_OP_const4/8/./u operation to encode them consumes a lot of bytes
13195 whereas may be better to output a negative integer: thanks to integer
13196 wrapping, we know that:
13197 x = x - 2 ** DWARF2_ADDR_SIZE
13198 = x - 2 * (max (HOST_WIDE_INT) + 1)
13199 So numbers close to max (unsigned HOST_WIDE_INT) could be represented as
13200 small negative integers. Let's try that in cases it will clearly improve
13201 the encoding: there is no gain turning DW_OP_const4u into
13202 DW_OP_const4s. */
13203 if (DWARF2_ADDR_SIZE * 8 == HOST_BITS_PER_WIDE_INT
13204 && ((DWARF2_ADDR_SIZE == 4 && i > max_uint - 0x8000)
13205 || (DWARF2_ADDR_SIZE == 8 && i > max_uint - 0x80000000)))
13207 const unsigned HOST_WIDE_INT first_shift = i - max_int - 1;
13209 /* Now, -1 < first_shift <= max (HOST_WIDE_INT)
13210 i.e. 0 <= first_shift <= max (HOST_WIDE_INT). */
13211 const HOST_WIDE_INT second_shift
13212 = (HOST_WIDE_INT) first_shift - (HOST_WIDE_INT) max_int - 1;
13214 /* So we finally have:
13215 -max (HOST_WIDE_INT) - 1 <= second_shift <= -1.
13216 i.e. min (HOST_WIDE_INT) <= second_shift < 0. */
13217 return int_loc_descriptor (second_shift);
13220 /* Last chance: fallback to a simple constant operation. */
13221 return new_loc_descr
13222 ((HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
13223 ? DW_OP_const4u
13224 : DW_OP_const8u,
13225 i, 0);
13228 /* Generate and return a location description that computes the unsigned
13229 comparison of the two stack top entries (a OP b where b is the top-most
13230 entry and a is the second one). The KIND of comparison can be LT_EXPR,
13231 LE_EXPR, GT_EXPR or GE_EXPR. */
13233 static dw_loc_descr_ref
13234 uint_comparison_loc_list (enum tree_code kind)
13236 enum dwarf_location_atom op, flip_op;
13237 dw_loc_descr_ref ret, bra_node, jmp_node, tmp;
13239 switch (kind)
13241 case LT_EXPR:
13242 op = DW_OP_lt;
13243 break;
13244 case LE_EXPR:
13245 op = DW_OP_le;
13246 break;
13247 case GT_EXPR:
13248 op = DW_OP_gt;
13249 break;
13250 case GE_EXPR:
13251 op = DW_OP_ge;
13252 break;
13253 default:
13254 gcc_unreachable ();
13257 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
13258 jmp_node = new_loc_descr (DW_OP_skip, 0, 0);
13260 /* Until DWARFv4, operations all work on signed integers. It is nevertheless
13261 possible to perform unsigned comparisons: we just have to distinguish
13262 three cases:
13264 1. when a and b have the same sign (as signed integers); then we should
13265 return: a OP(signed) b;
13267 2. when a is a negative signed integer while b is a positive one, then a
13268 is a greater unsigned integer than b; likewise when a and b's roles
13269 are flipped.
13271 So first, compare the sign of the two operands. */
13272 ret = new_loc_descr (DW_OP_over, 0, 0);
13273 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
13274 add_loc_descr (&ret, new_loc_descr (DW_OP_xor, 0, 0));
13275 /* If they have different signs (i.e. they have different sign bits), then
13276 the stack top value has now the sign bit set and thus it's smaller than
13277 zero. */
13278 add_loc_descr (&ret, new_loc_descr (DW_OP_lit0, 0, 0));
13279 add_loc_descr (&ret, new_loc_descr (DW_OP_lt, 0, 0));
13280 add_loc_descr (&ret, bra_node);
13282 /* We are in case 1. At this point, we know both operands have the same
13283 sign, to it's safe to use the built-in signed comparison. */
13284 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
13285 add_loc_descr (&ret, jmp_node);
13287 /* We are in case 2. Here, we know both operands do not have the same sign,
13288 so we have to flip the signed comparison. */
13289 flip_op = (kind == LT_EXPR || kind == LE_EXPR) ? DW_OP_gt : DW_OP_lt;
13290 tmp = new_loc_descr (flip_op, 0, 0);
13291 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13292 bra_node->dw_loc_oprnd1.v.val_loc = tmp;
13293 add_loc_descr (&ret, tmp);
13295 /* This dummy operation is necessary to make the two branches join. */
13296 tmp = new_loc_descr (DW_OP_nop, 0, 0);
13297 jmp_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13298 jmp_node->dw_loc_oprnd1.v.val_loc = tmp;
13299 add_loc_descr (&ret, tmp);
13301 return ret;
13304 /* Likewise, but takes the location description lists (might be destructive on
13305 them). Return NULL if either is NULL or if concatenation fails. */
13307 static dw_loc_list_ref
13308 loc_list_from_uint_comparison (dw_loc_list_ref left, dw_loc_list_ref right,
13309 enum tree_code kind)
13311 if (left == NULL || right == NULL)
13312 return NULL;
13314 add_loc_list (&left, right);
13315 if (left == NULL)
13316 return NULL;
13318 add_loc_descr_to_each (left, uint_comparison_loc_list (kind));
13319 return left;
13322 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
13323 without actually allocating it. */
13325 static unsigned long
13326 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
13328 return size_of_int_loc_descriptor (i >> shift)
13329 + size_of_int_loc_descriptor (shift)
13330 + 1;
13333 /* Return size_of_locs (int_loc_descriptor (i)) without
13334 actually allocating it. */
13336 static unsigned long
13337 size_of_int_loc_descriptor (HOST_WIDE_INT i)
13339 unsigned long s;
13341 if (i >= 0)
13343 int clz, ctz;
13344 if (i <= 31)
13345 return 1;
13346 else if (i <= 0xff)
13347 return 2;
13348 else if (i <= 0xffff)
13349 return 3;
13350 clz = clz_hwi (i);
13351 ctz = ctz_hwi (i);
13352 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
13353 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
13354 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13355 - clz - 5);
13356 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13357 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
13358 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13359 - clz - 8);
13360 else if (DWARF2_ADDR_SIZE == 4 && i > 0x7fffffff
13361 && size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i)
13362 <= 4)
13363 return size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i);
13364 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
13365 return 5;
13366 s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
13367 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13368 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
13369 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13370 - clz - 8);
13371 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
13372 && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
13373 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13374 - clz - 16);
13375 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
13376 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
13377 && s > 6)
13378 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13379 - clz - 32);
13380 else
13381 return 1 + s;
13383 else
13385 if (i >= -0x80)
13386 return 2;
13387 else if (i >= -0x8000)
13388 return 3;
13389 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
13391 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
13393 s = size_of_int_loc_descriptor (-i) + 1;
13394 if (s < 5)
13395 return s;
13397 return 5;
13399 else
13401 unsigned long r = 1 + size_of_sleb128 (i);
13402 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
13404 s = size_of_int_loc_descriptor (-i) + 1;
13405 if (s < r)
13406 return s;
13408 return r;
13413 /* Return loc description representing "address" of integer value.
13414 This can appear only as toplevel expression. */
13416 static dw_loc_descr_ref
13417 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
13419 int litsize;
13420 dw_loc_descr_ref loc_result = NULL;
13422 if (!(dwarf_version >= 4 || !dwarf_strict))
13423 return NULL;
13425 litsize = size_of_int_loc_descriptor (i);
13426 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
13427 is more compact. For DW_OP_stack_value we need:
13428 litsize + 1 (DW_OP_stack_value)
13429 and for DW_OP_implicit_value:
13430 1 (DW_OP_implicit_value) + 1 (length) + size. */
13431 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
13433 loc_result = int_loc_descriptor (i);
13434 add_loc_descr (&loc_result,
13435 new_loc_descr (DW_OP_stack_value, 0, 0));
13436 return loc_result;
13439 loc_result = new_loc_descr (DW_OP_implicit_value,
13440 size, 0);
13441 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
13442 loc_result->dw_loc_oprnd2.v.val_int = i;
13443 return loc_result;
13446 /* Return a location descriptor that designates a base+offset location. */
13448 static dw_loc_descr_ref
13449 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
13450 enum var_init_status initialized)
13452 unsigned int regno;
13453 dw_loc_descr_ref result;
13454 dw_fde_ref fde = cfun->fde;
13456 /* We only use "frame base" when we're sure we're talking about the
13457 post-prologue local stack frame. We do this by *not* running
13458 register elimination until this point, and recognizing the special
13459 argument pointer and soft frame pointer rtx's. */
13460 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
13462 rtx elim = (ira_use_lra_p
13463 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
13464 : eliminate_regs (reg, VOIDmode, NULL_RTX));
13466 if (elim != reg)
13468 if (GET_CODE (elim) == PLUS)
13470 offset += INTVAL (XEXP (elim, 1));
13471 elim = XEXP (elim, 0);
13473 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
13474 && (elim == hard_frame_pointer_rtx
13475 || elim == stack_pointer_rtx))
13476 || elim == (frame_pointer_needed
13477 ? hard_frame_pointer_rtx
13478 : stack_pointer_rtx));
13480 /* If drap register is used to align stack, use frame
13481 pointer + offset to access stack variables. If stack
13482 is aligned without drap, use stack pointer + offset to
13483 access stack variables. */
13484 if (crtl->stack_realign_tried
13485 && reg == frame_pointer_rtx)
13487 int base_reg
13488 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
13489 ? HARD_FRAME_POINTER_REGNUM
13490 : REGNO (elim));
13491 return new_reg_loc_descr (base_reg, offset);
13494 gcc_assert (frame_pointer_fb_offset_valid);
13495 offset += frame_pointer_fb_offset;
13496 return new_loc_descr (DW_OP_fbreg, offset, 0);
13500 regno = REGNO (reg);
13501 #ifdef LEAF_REG_REMAP
13502 if (crtl->uses_only_leaf_regs)
13504 int leaf_reg = LEAF_REG_REMAP (regno);
13505 if (leaf_reg != -1)
13506 regno = (unsigned) leaf_reg;
13508 #endif
13509 regno = DWARF_FRAME_REGNUM (regno);
13511 if (!optimize && fde
13512 && (fde->drap_reg == regno || fde->vdrap_reg == regno))
13514 /* Use cfa+offset to represent the location of arguments passed
13515 on the stack when drap is used to align stack.
13516 Only do this when not optimizing, for optimized code var-tracking
13517 is supposed to track where the arguments live and the register
13518 used as vdrap or drap in some spot might be used for something
13519 else in other part of the routine. */
13520 return new_loc_descr (DW_OP_fbreg, offset, 0);
13523 if (regno <= 31)
13524 result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
13525 offset, 0);
13526 else
13527 result = new_loc_descr (DW_OP_bregx, regno, offset);
13529 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13530 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13532 return result;
13535 /* Return true if this RTL expression describes a base+offset calculation. */
13537 static inline int
13538 is_based_loc (const_rtx rtl)
13540 return (GET_CODE (rtl) == PLUS
13541 && ((REG_P (XEXP (rtl, 0))
13542 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
13543 && CONST_INT_P (XEXP (rtl, 1)))));
13546 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
13547 failed. */
13549 static dw_loc_descr_ref
13550 tls_mem_loc_descriptor (rtx mem)
13552 tree base;
13553 dw_loc_descr_ref loc_result;
13555 if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
13556 return NULL;
13558 base = get_base_address (MEM_EXPR (mem));
13559 if (base == NULL
13560 || !VAR_P (base)
13561 || !DECL_THREAD_LOCAL_P (base))
13562 return NULL;
13564 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1, NULL);
13565 if (loc_result == NULL)
13566 return NULL;
13568 if (MEM_OFFSET (mem))
13569 loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
13571 return loc_result;
13574 /* Output debug info about reason why we failed to expand expression as dwarf
13575 expression. */
13577 static void
13578 expansion_failed (tree expr, rtx rtl, char const *reason)
13580 if (dump_file && (dump_flags & TDF_DETAILS))
13582 fprintf (dump_file, "Failed to expand as dwarf: ");
13583 if (expr)
13584 print_generic_expr (dump_file, expr, dump_flags);
13585 if (rtl)
13587 fprintf (dump_file, "\n");
13588 print_rtl (dump_file, rtl);
13590 fprintf (dump_file, "\nReason: %s\n", reason);
13594 /* Helper function for const_ok_for_output. */
13596 static bool
13597 const_ok_for_output_1 (rtx rtl)
13599 if (GET_CODE (rtl) == UNSPEC)
13601 /* If delegitimize_address couldn't do anything with the UNSPEC, assume
13602 we can't express it in the debug info. */
13603 /* Don't complain about TLS UNSPECs, those are just too hard to
13604 delegitimize. Note this could be a non-decl SYMBOL_REF such as
13605 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
13606 rather than DECL_THREAD_LOCAL_P is not just an optimization. */
13607 if (flag_checking
13608 && (XVECLEN (rtl, 0) == 0
13609 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
13610 || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE))
13611 inform (current_function_decl
13612 ? DECL_SOURCE_LOCATION (current_function_decl)
13613 : UNKNOWN_LOCATION,
13614 #if NUM_UNSPEC_VALUES > 0
13615 "non-delegitimized UNSPEC %s (%d) found in variable location",
13616 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
13617 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
13618 XINT (rtl, 1));
13619 #else
13620 "non-delegitimized UNSPEC %d found in variable location",
13621 XINT (rtl, 1));
13622 #endif
13623 expansion_failed (NULL_TREE, rtl,
13624 "UNSPEC hasn't been delegitimized.\n");
13625 return false;
13628 if (targetm.const_not_ok_for_debug_p (rtl))
13630 expansion_failed (NULL_TREE, rtl,
13631 "Expression rejected for debug by the backend.\n");
13632 return false;
13635 /* FIXME: Refer to PR60655. It is possible for simplification
13636 of rtl expressions in var tracking to produce such expressions.
13637 We should really identify / validate expressions
13638 enclosed in CONST that can be handled by assemblers on various
13639 targets and only handle legitimate cases here. */
13640 if (GET_CODE (rtl) != SYMBOL_REF)
13642 if (GET_CODE (rtl) == NOT)
13643 return false;
13644 return true;
13647 if (CONSTANT_POOL_ADDRESS_P (rtl))
13649 bool marked;
13650 get_pool_constant_mark (rtl, &marked);
13651 /* If all references to this pool constant were optimized away,
13652 it was not output and thus we can't represent it. */
13653 if (!marked)
13655 expansion_failed (NULL_TREE, rtl,
13656 "Constant was removed from constant pool.\n");
13657 return false;
13661 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
13662 return false;
13664 /* Avoid references to external symbols in debug info, on several targets
13665 the linker might even refuse to link when linking a shared library,
13666 and in many other cases the relocations for .debug_info/.debug_loc are
13667 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
13668 to be defined within the same shared library or executable are fine. */
13669 if (SYMBOL_REF_EXTERNAL_P (rtl))
13671 tree decl = SYMBOL_REF_DECL (rtl);
13673 if (decl == NULL || !targetm.binds_local_p (decl))
13675 expansion_failed (NULL_TREE, rtl,
13676 "Symbol not defined in current TU.\n");
13677 return false;
13681 return true;
13684 /* Return true if constant RTL can be emitted in DW_OP_addr or
13685 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
13686 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
13688 static bool
13689 const_ok_for_output (rtx rtl)
13691 if (GET_CODE (rtl) == SYMBOL_REF)
13692 return const_ok_for_output_1 (rtl);
13694 if (GET_CODE (rtl) == CONST)
13696 subrtx_var_iterator::array_type array;
13697 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 0), ALL)
13698 if (!const_ok_for_output_1 (*iter))
13699 return false;
13700 return true;
13703 return true;
13706 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
13707 if possible, NULL otherwise. */
13709 static dw_die_ref
13710 base_type_for_mode (machine_mode mode, bool unsignedp)
13712 dw_die_ref type_die;
13713 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
13715 if (type == NULL)
13716 return NULL;
13717 switch (TREE_CODE (type))
13719 case INTEGER_TYPE:
13720 case REAL_TYPE:
13721 break;
13722 default:
13723 return NULL;
13725 type_die = lookup_type_die (type);
13726 if (!type_die)
13727 type_die = modified_type_die (type, TYPE_UNQUALIFIED, false,
13728 comp_unit_die ());
13729 if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
13730 return NULL;
13731 return type_die;
13734 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
13735 type matching MODE, or, if MODE is narrower than or as wide as
13736 DWARF2_ADDR_SIZE, untyped. Return NULL if the conversion is not
13737 possible. */
13739 static dw_loc_descr_ref
13740 convert_descriptor_to_mode (machine_mode mode, dw_loc_descr_ref op)
13742 machine_mode outer_mode = mode;
13743 dw_die_ref type_die;
13744 dw_loc_descr_ref cvt;
13746 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
13748 add_loc_descr (&op, new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0));
13749 return op;
13751 type_die = base_type_for_mode (outer_mode, 1);
13752 if (type_die == NULL)
13753 return NULL;
13754 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
13755 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13756 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13757 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13758 add_loc_descr (&op, cvt);
13759 return op;
13762 /* Return location descriptor for comparison OP with operands OP0 and OP1. */
13764 static dw_loc_descr_ref
13765 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
13766 dw_loc_descr_ref op1)
13768 dw_loc_descr_ref ret = op0;
13769 add_loc_descr (&ret, op1);
13770 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
13771 if (STORE_FLAG_VALUE != 1)
13773 add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
13774 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
13776 return ret;
13779 /* Return location descriptor for signed comparison OP RTL. */
13781 static dw_loc_descr_ref
13782 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
13783 machine_mode mem_mode)
13785 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
13786 dw_loc_descr_ref op0, op1;
13787 int shift;
13789 if (op_mode == VOIDmode)
13790 op_mode = GET_MODE (XEXP (rtl, 1));
13791 if (op_mode == VOIDmode)
13792 return NULL;
13794 if (dwarf_strict
13795 && dwarf_version < 5
13796 && (!SCALAR_INT_MODE_P (op_mode)
13797 || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
13798 return NULL;
13800 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
13801 VAR_INIT_STATUS_INITIALIZED);
13802 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
13803 VAR_INIT_STATUS_INITIALIZED);
13805 if (op0 == NULL || op1 == NULL)
13806 return NULL;
13808 if (!SCALAR_INT_MODE_P (op_mode)
13809 || GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
13810 return compare_loc_descriptor (op, op0, op1);
13812 if (GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
13814 dw_die_ref type_die = base_type_for_mode (op_mode, 0);
13815 dw_loc_descr_ref cvt;
13817 if (type_die == NULL)
13818 return NULL;
13819 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
13820 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13821 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13822 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13823 add_loc_descr (&op0, cvt);
13824 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
13825 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13826 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13827 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13828 add_loc_descr (&op1, cvt);
13829 return compare_loc_descriptor (op, op0, op1);
13832 shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
13833 /* For eq/ne, if the operands are known to be zero-extended,
13834 there is no need to do the fancy shifting up. */
13835 if (op == DW_OP_eq || op == DW_OP_ne)
13837 dw_loc_descr_ref last0, last1;
13838 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
13840 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
13842 /* deref_size zero extends, and for constants we can check
13843 whether they are zero extended or not. */
13844 if (((last0->dw_loc_opc == DW_OP_deref_size
13845 && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
13846 || (CONST_INT_P (XEXP (rtl, 0))
13847 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
13848 == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
13849 && ((last1->dw_loc_opc == DW_OP_deref_size
13850 && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
13851 || (CONST_INT_P (XEXP (rtl, 1))
13852 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
13853 == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
13854 return compare_loc_descriptor (op, op0, op1);
13856 /* EQ/NE comparison against constant in narrower type than
13857 DWARF2_ADDR_SIZE can be performed either as
13858 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
13859 DW_OP_{eq,ne}
13861 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
13862 DW_OP_{eq,ne}. Pick whatever is shorter. */
13863 if (CONST_INT_P (XEXP (rtl, 1))
13864 && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
13865 && (size_of_int_loc_descriptor (shift) + 1
13866 + size_of_int_loc_descriptor (UINTVAL (XEXP (rtl, 1)) << shift)
13867 >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
13868 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
13869 & GET_MODE_MASK (op_mode))))
13871 add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
13872 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
13873 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
13874 & GET_MODE_MASK (op_mode));
13875 return compare_loc_descriptor (op, op0, op1);
13878 add_loc_descr (&op0, int_loc_descriptor (shift));
13879 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
13880 if (CONST_INT_P (XEXP (rtl, 1)))
13881 op1 = int_loc_descriptor (UINTVAL (XEXP (rtl, 1)) << shift);
13882 else
13884 add_loc_descr (&op1, int_loc_descriptor (shift));
13885 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
13887 return compare_loc_descriptor (op, op0, op1);
13890 /* Return location descriptor for unsigned comparison OP RTL. */
13892 static dw_loc_descr_ref
13893 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
13894 machine_mode mem_mode)
13896 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
13897 dw_loc_descr_ref op0, op1;
13899 if (op_mode == VOIDmode)
13900 op_mode = GET_MODE (XEXP (rtl, 1));
13901 if (op_mode == VOIDmode)
13902 return NULL;
13903 if (!SCALAR_INT_MODE_P (op_mode))
13904 return NULL;
13906 if (dwarf_strict
13907 && dwarf_version < 5
13908 && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
13909 return NULL;
13911 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
13912 VAR_INIT_STATUS_INITIALIZED);
13913 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
13914 VAR_INIT_STATUS_INITIALIZED);
13916 if (op0 == NULL || op1 == NULL)
13917 return NULL;
13919 if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
13921 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
13922 dw_loc_descr_ref last0, last1;
13923 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
13925 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
13927 if (CONST_INT_P (XEXP (rtl, 0)))
13928 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
13929 /* deref_size zero extends, so no need to mask it again. */
13930 else if (last0->dw_loc_opc != DW_OP_deref_size
13931 || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
13933 add_loc_descr (&op0, int_loc_descriptor (mask));
13934 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
13936 if (CONST_INT_P (XEXP (rtl, 1)))
13937 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
13938 /* deref_size zero extends, so no need to mask it again. */
13939 else if (last1->dw_loc_opc != DW_OP_deref_size
13940 || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
13942 add_loc_descr (&op1, int_loc_descriptor (mask));
13943 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
13946 else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
13948 HOST_WIDE_INT bias = 1;
13949 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
13950 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
13951 if (CONST_INT_P (XEXP (rtl, 1)))
13952 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
13953 + INTVAL (XEXP (rtl, 1)));
13954 else
13955 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
13956 bias, 0));
13958 return compare_loc_descriptor (op, op0, op1);
13961 /* Return location descriptor for {U,S}{MIN,MAX}. */
13963 static dw_loc_descr_ref
13964 minmax_loc_descriptor (rtx rtl, machine_mode mode,
13965 machine_mode mem_mode)
13967 enum dwarf_location_atom op;
13968 dw_loc_descr_ref op0, op1, ret;
13969 dw_loc_descr_ref bra_node, drop_node;
13971 if (dwarf_strict
13972 && dwarf_version < 5
13973 && (!SCALAR_INT_MODE_P (mode)
13974 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
13975 return NULL;
13977 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13978 VAR_INIT_STATUS_INITIALIZED);
13979 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13980 VAR_INIT_STATUS_INITIALIZED);
13982 if (op0 == NULL || op1 == NULL)
13983 return NULL;
13985 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
13986 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
13987 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
13988 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
13990 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
13992 HOST_WIDE_INT mask = GET_MODE_MASK (mode);
13993 add_loc_descr (&op0, int_loc_descriptor (mask));
13994 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
13995 add_loc_descr (&op1, int_loc_descriptor (mask));
13996 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
13998 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
14000 HOST_WIDE_INT bias = 1;
14001 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
14002 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14003 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14006 else if (!SCALAR_INT_MODE_P (mode)
14007 && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
14009 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode)) * BITS_PER_UNIT;
14010 add_loc_descr (&op0, int_loc_descriptor (shift));
14011 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
14012 add_loc_descr (&op1, int_loc_descriptor (shift));
14013 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
14015 else if (SCALAR_INT_MODE_P (mode)
14016 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
14018 dw_die_ref type_die = base_type_for_mode (mode, 0);
14019 dw_loc_descr_ref cvt;
14020 if (type_die == NULL)
14021 return NULL;
14022 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14023 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14024 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14025 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14026 add_loc_descr (&op0, cvt);
14027 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14028 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14029 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14030 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14031 add_loc_descr (&op1, cvt);
14034 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
14035 op = DW_OP_lt;
14036 else
14037 op = DW_OP_gt;
14038 ret = op0;
14039 add_loc_descr (&ret, op1);
14040 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
14041 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14042 add_loc_descr (&ret, bra_node);
14043 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14044 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
14045 add_loc_descr (&ret, drop_node);
14046 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14047 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
14048 if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
14049 && SCALAR_INT_MODE_P (mode)
14050 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
14051 ret = convert_descriptor_to_mode (mode, ret);
14052 return ret;
14055 /* Helper function for mem_loc_descriptor. Perform OP binary op,
14056 but after converting arguments to type_die, afterwards
14057 convert back to unsigned. */
14059 static dw_loc_descr_ref
14060 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
14061 machine_mode mode, machine_mode mem_mode)
14063 dw_loc_descr_ref cvt, op0, op1;
14065 if (type_die == NULL)
14066 return NULL;
14067 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14068 VAR_INIT_STATUS_INITIALIZED);
14069 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14070 VAR_INIT_STATUS_INITIALIZED);
14071 if (op0 == NULL || op1 == NULL)
14072 return NULL;
14073 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14074 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14075 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14076 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14077 add_loc_descr (&op0, cvt);
14078 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14079 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14080 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14081 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14082 add_loc_descr (&op1, cvt);
14083 add_loc_descr (&op0, op1);
14084 add_loc_descr (&op0, new_loc_descr (op, 0, 0));
14085 return convert_descriptor_to_mode (mode, op0);
14088 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
14089 const0 is DW_OP_lit0 or corresponding typed constant,
14090 const1 is DW_OP_lit1 or corresponding typed constant
14091 and constMSB is constant with just the MSB bit set
14092 for the mode):
14093 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
14094 L1: const0 DW_OP_swap
14095 L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
14096 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
14097 L3: DW_OP_drop
14098 L4: DW_OP_nop
14100 CTZ is similar:
14101 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
14102 L1: const0 DW_OP_swap
14103 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
14104 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
14105 L3: DW_OP_drop
14106 L4: DW_OP_nop
14108 FFS is similar:
14109 DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
14110 L1: const1 DW_OP_swap
14111 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
14112 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
14113 L3: DW_OP_drop
14114 L4: DW_OP_nop */
14116 static dw_loc_descr_ref
14117 clz_loc_descriptor (rtx rtl, machine_mode mode,
14118 machine_mode mem_mode)
14120 dw_loc_descr_ref op0, ret, tmp;
14121 HOST_WIDE_INT valv;
14122 dw_loc_descr_ref l1jump, l1label;
14123 dw_loc_descr_ref l2jump, l2label;
14124 dw_loc_descr_ref l3jump, l3label;
14125 dw_loc_descr_ref l4jump, l4label;
14126 rtx msb;
14128 if (!SCALAR_INT_MODE_P (mode)
14129 || GET_MODE (XEXP (rtl, 0)) != mode)
14130 return NULL;
14132 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14133 VAR_INIT_STATUS_INITIALIZED);
14134 if (op0 == NULL)
14135 return NULL;
14136 ret = op0;
14137 if (GET_CODE (rtl) == CLZ)
14139 if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
14140 valv = GET_MODE_BITSIZE (mode);
14142 else if (GET_CODE (rtl) == FFS)
14143 valv = 0;
14144 else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
14145 valv = GET_MODE_BITSIZE (mode);
14146 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
14147 l1jump = new_loc_descr (DW_OP_bra, 0, 0);
14148 add_loc_descr (&ret, l1jump);
14149 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
14150 tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
14151 VAR_INIT_STATUS_INITIALIZED);
14152 if (tmp == NULL)
14153 return NULL;
14154 add_loc_descr (&ret, tmp);
14155 l4jump = new_loc_descr (DW_OP_skip, 0, 0);
14156 add_loc_descr (&ret, l4jump);
14157 l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
14158 ? const1_rtx : const0_rtx,
14159 mode, mem_mode,
14160 VAR_INIT_STATUS_INITIALIZED);
14161 if (l1label == NULL)
14162 return NULL;
14163 add_loc_descr (&ret, l1label);
14164 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14165 l2label = new_loc_descr (DW_OP_dup, 0, 0);
14166 add_loc_descr (&ret, l2label);
14167 if (GET_CODE (rtl) != CLZ)
14168 msb = const1_rtx;
14169 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
14170 msb = GEN_INT (HOST_WIDE_INT_1U
14171 << (GET_MODE_BITSIZE (mode) - 1));
14172 else
14173 msb = immed_wide_int_const
14174 (wi::set_bit_in_zero (GET_MODE_PRECISION (mode) - 1,
14175 GET_MODE_PRECISION (mode)), mode);
14176 if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
14177 tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
14178 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
14179 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
14180 else
14181 tmp = mem_loc_descriptor (msb, mode, mem_mode,
14182 VAR_INIT_STATUS_INITIALIZED);
14183 if (tmp == NULL)
14184 return NULL;
14185 add_loc_descr (&ret, tmp);
14186 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
14187 l3jump = new_loc_descr (DW_OP_bra, 0, 0);
14188 add_loc_descr (&ret, l3jump);
14189 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
14190 VAR_INIT_STATUS_INITIALIZED);
14191 if (tmp == NULL)
14192 return NULL;
14193 add_loc_descr (&ret, tmp);
14194 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
14195 ? DW_OP_shl : DW_OP_shr, 0, 0));
14196 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14197 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
14198 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14199 l2jump = new_loc_descr (DW_OP_skip, 0, 0);
14200 add_loc_descr (&ret, l2jump);
14201 l3label = new_loc_descr (DW_OP_drop, 0, 0);
14202 add_loc_descr (&ret, l3label);
14203 l4label = new_loc_descr (DW_OP_nop, 0, 0);
14204 add_loc_descr (&ret, l4label);
14205 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14206 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
14207 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14208 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
14209 l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14210 l3jump->dw_loc_oprnd1.v.val_loc = l3label;
14211 l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14212 l4jump->dw_loc_oprnd1.v.val_loc = l4label;
14213 return ret;
14216 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
14217 const1 is DW_OP_lit1 or corresponding typed constant):
14218 const0 DW_OP_swap
14219 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
14220 DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
14221 L2: DW_OP_drop
14223 PARITY is similar:
14224 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
14225 DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
14226 L2: DW_OP_drop */
14228 static dw_loc_descr_ref
14229 popcount_loc_descriptor (rtx rtl, machine_mode mode,
14230 machine_mode mem_mode)
14232 dw_loc_descr_ref op0, ret, tmp;
14233 dw_loc_descr_ref l1jump, l1label;
14234 dw_loc_descr_ref l2jump, l2label;
14236 if (!SCALAR_INT_MODE_P (mode)
14237 || GET_MODE (XEXP (rtl, 0)) != mode)
14238 return NULL;
14240 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14241 VAR_INIT_STATUS_INITIALIZED);
14242 if (op0 == NULL)
14243 return NULL;
14244 ret = op0;
14245 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
14246 VAR_INIT_STATUS_INITIALIZED);
14247 if (tmp == NULL)
14248 return NULL;
14249 add_loc_descr (&ret, tmp);
14250 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14251 l1label = new_loc_descr (DW_OP_dup, 0, 0);
14252 add_loc_descr (&ret, l1label);
14253 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
14254 add_loc_descr (&ret, l2jump);
14255 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
14256 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
14257 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
14258 VAR_INIT_STATUS_INITIALIZED);
14259 if (tmp == NULL)
14260 return NULL;
14261 add_loc_descr (&ret, tmp);
14262 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
14263 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
14264 ? DW_OP_plus : DW_OP_xor, 0, 0));
14265 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14266 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
14267 VAR_INIT_STATUS_INITIALIZED);
14268 add_loc_descr (&ret, tmp);
14269 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
14270 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
14271 add_loc_descr (&ret, l1jump);
14272 l2label = new_loc_descr (DW_OP_drop, 0, 0);
14273 add_loc_descr (&ret, l2label);
14274 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14275 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
14276 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14277 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
14278 return ret;
14281 /* BSWAP (constS is initial shift count, either 56 or 24):
14282 constS const0
14283 L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
14284 const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
14285 DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
14286 DW_OP_minus DW_OP_swap DW_OP_skip <L1>
14287 L2: DW_OP_drop DW_OP_swap DW_OP_drop */
14289 static dw_loc_descr_ref
14290 bswap_loc_descriptor (rtx rtl, machine_mode mode,
14291 machine_mode mem_mode)
14293 dw_loc_descr_ref op0, ret, tmp;
14294 dw_loc_descr_ref l1jump, l1label;
14295 dw_loc_descr_ref l2jump, l2label;
14297 if (!SCALAR_INT_MODE_P (mode)
14298 || BITS_PER_UNIT != 8
14299 || (GET_MODE_BITSIZE (mode) != 32
14300 && GET_MODE_BITSIZE (mode) != 64))
14301 return NULL;
14303 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14304 VAR_INIT_STATUS_INITIALIZED);
14305 if (op0 == NULL)
14306 return NULL;
14308 ret = op0;
14309 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
14310 mode, mem_mode,
14311 VAR_INIT_STATUS_INITIALIZED);
14312 if (tmp == NULL)
14313 return NULL;
14314 add_loc_descr (&ret, tmp);
14315 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
14316 VAR_INIT_STATUS_INITIALIZED);
14317 if (tmp == NULL)
14318 return NULL;
14319 add_loc_descr (&ret, tmp);
14320 l1label = new_loc_descr (DW_OP_pick, 2, 0);
14321 add_loc_descr (&ret, l1label);
14322 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
14323 mode, mem_mode,
14324 VAR_INIT_STATUS_INITIALIZED);
14325 add_loc_descr (&ret, tmp);
14326 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
14327 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
14328 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
14329 tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
14330 VAR_INIT_STATUS_INITIALIZED);
14331 if (tmp == NULL)
14332 return NULL;
14333 add_loc_descr (&ret, tmp);
14334 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
14335 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
14336 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
14337 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
14338 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14339 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
14340 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
14341 VAR_INIT_STATUS_INITIALIZED);
14342 add_loc_descr (&ret, tmp);
14343 add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
14344 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
14345 add_loc_descr (&ret, l2jump);
14346 tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
14347 VAR_INIT_STATUS_INITIALIZED);
14348 add_loc_descr (&ret, tmp);
14349 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
14350 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14351 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
14352 add_loc_descr (&ret, l1jump);
14353 l2label = new_loc_descr (DW_OP_drop, 0, 0);
14354 add_loc_descr (&ret, l2label);
14355 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14356 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
14357 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14358 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
14359 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14360 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
14361 return ret;
14364 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
14365 DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
14366 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
14367 DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
14369 ROTATERT is similar:
14370 DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
14371 DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
14372 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or */
14374 static dw_loc_descr_ref
14375 rotate_loc_descriptor (rtx rtl, machine_mode mode,
14376 machine_mode mem_mode)
14378 rtx rtlop1 = XEXP (rtl, 1);
14379 dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
14380 int i;
14382 if (!SCALAR_INT_MODE_P (mode))
14383 return NULL;
14385 if (GET_MODE (rtlop1) != VOIDmode
14386 && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
14387 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
14388 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14389 VAR_INIT_STATUS_INITIALIZED);
14390 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
14391 VAR_INIT_STATUS_INITIALIZED);
14392 if (op0 == NULL || op1 == NULL)
14393 return NULL;
14394 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
14395 for (i = 0; i < 2; i++)
14397 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
14398 mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
14399 mode, mem_mode,
14400 VAR_INIT_STATUS_INITIALIZED);
14401 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
14402 mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
14403 ? DW_OP_const4u
14404 : HOST_BITS_PER_WIDE_INT == 64
14405 ? DW_OP_const8u : DW_OP_constu,
14406 GET_MODE_MASK (mode), 0);
14407 else
14408 mask[i] = NULL;
14409 if (mask[i] == NULL)
14410 return NULL;
14411 add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
14413 ret = op0;
14414 add_loc_descr (&ret, op1);
14415 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
14416 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
14417 if (GET_CODE (rtl) == ROTATERT)
14419 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
14420 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
14421 GET_MODE_BITSIZE (mode), 0));
14423 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
14424 if (mask[0] != NULL)
14425 add_loc_descr (&ret, mask[0]);
14426 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
14427 if (mask[1] != NULL)
14429 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14430 add_loc_descr (&ret, mask[1]);
14431 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14433 if (GET_CODE (rtl) == ROTATE)
14435 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
14436 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
14437 GET_MODE_BITSIZE (mode), 0));
14439 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
14440 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
14441 return ret;
14444 /* Helper function for mem_loc_descriptor. Return DW_OP_GNU_parameter_ref
14445 for DEBUG_PARAMETER_REF RTL. */
14447 static dw_loc_descr_ref
14448 parameter_ref_descriptor (rtx rtl)
14450 dw_loc_descr_ref ret;
14451 dw_die_ref ref;
14453 if (dwarf_strict)
14454 return NULL;
14455 gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
14456 ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
14457 ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
14458 if (ref)
14460 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14461 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
14462 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
14464 else
14466 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
14467 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
14469 return ret;
14472 /* The following routine converts the RTL for a variable or parameter
14473 (resident in memory) into an equivalent Dwarf representation of a
14474 mechanism for getting the address of that same variable onto the top of a
14475 hypothetical "address evaluation" stack.
14477 When creating memory location descriptors, we are effectively transforming
14478 the RTL for a memory-resident object into its Dwarf postfix expression
14479 equivalent. This routine recursively descends an RTL tree, turning
14480 it into Dwarf postfix code as it goes.
14482 MODE is the mode that should be assumed for the rtl if it is VOIDmode.
14484 MEM_MODE is the mode of the memory reference, needed to handle some
14485 autoincrement addressing modes.
14487 Return 0 if we can't represent the location. */
14489 dw_loc_descr_ref
14490 mem_loc_descriptor (rtx rtl, machine_mode mode,
14491 machine_mode mem_mode,
14492 enum var_init_status initialized)
14494 dw_loc_descr_ref mem_loc_result = NULL;
14495 enum dwarf_location_atom op;
14496 dw_loc_descr_ref op0, op1;
14497 rtx inner = NULL_RTX;
14499 if (mode == VOIDmode)
14500 mode = GET_MODE (rtl);
14502 /* Note that for a dynamically sized array, the location we will generate a
14503 description of here will be the lowest numbered location which is
14504 actually within the array. That's *not* necessarily the same as the
14505 zeroth element of the array. */
14507 rtl = targetm.delegitimize_address (rtl);
14509 if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
14510 return NULL;
14512 switch (GET_CODE (rtl))
14514 case POST_INC:
14515 case POST_DEC:
14516 case POST_MODIFY:
14517 return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
14519 case SUBREG:
14520 /* The case of a subreg may arise when we have a local (register)
14521 variable or a formal (register) parameter which doesn't quite fill
14522 up an entire register. For now, just assume that it is
14523 legitimate to make the Dwarf info refer to the whole register which
14524 contains the given subreg. */
14525 if (!subreg_lowpart_p (rtl))
14526 break;
14527 inner = SUBREG_REG (rtl);
14528 /* FALLTHRU */
14529 case TRUNCATE:
14530 if (inner == NULL_RTX)
14531 inner = XEXP (rtl, 0);
14532 if (SCALAR_INT_MODE_P (mode)
14533 && SCALAR_INT_MODE_P (GET_MODE (inner))
14534 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
14535 #ifdef POINTERS_EXTEND_UNSIGNED
14536 || (mode == Pmode && mem_mode != VOIDmode)
14537 #endif
14539 && GET_MODE_SIZE (GET_MODE (inner)) <= DWARF2_ADDR_SIZE)
14541 mem_loc_result = mem_loc_descriptor (inner,
14542 GET_MODE (inner),
14543 mem_mode, initialized);
14544 break;
14546 if (dwarf_strict && dwarf_version < 5)
14547 break;
14548 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (inner)))
14549 break;
14550 if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (inner))
14551 && (!SCALAR_INT_MODE_P (mode)
14552 || !SCALAR_INT_MODE_P (GET_MODE (inner))))
14553 break;
14554 else
14556 dw_die_ref type_die;
14557 dw_loc_descr_ref cvt;
14559 mem_loc_result = mem_loc_descriptor (inner,
14560 GET_MODE (inner),
14561 mem_mode, initialized);
14562 if (mem_loc_result == NULL)
14563 break;
14564 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
14565 if (type_die == NULL)
14567 mem_loc_result = NULL;
14568 break;
14570 if (GET_MODE_SIZE (mode)
14571 != GET_MODE_SIZE (GET_MODE (inner)))
14572 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14573 else
14574 cvt = new_loc_descr (dwarf_OP (DW_OP_reinterpret), 0, 0);
14575 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14576 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14577 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14578 add_loc_descr (&mem_loc_result, cvt);
14579 if (SCALAR_INT_MODE_P (mode)
14580 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
14582 /* Convert it to untyped afterwards. */
14583 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14584 add_loc_descr (&mem_loc_result, cvt);
14587 break;
14589 case REG:
14590 if (! SCALAR_INT_MODE_P (mode)
14591 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
14592 && rtl != arg_pointer_rtx
14593 && rtl != frame_pointer_rtx
14594 #ifdef POINTERS_EXTEND_UNSIGNED
14595 && (mode != Pmode || mem_mode == VOIDmode)
14596 #endif
14599 dw_die_ref type_die;
14600 unsigned int dbx_regnum;
14602 if (dwarf_strict && dwarf_version < 5)
14603 break;
14604 if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
14605 break;
14606 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
14607 if (type_die == NULL)
14608 break;
14610 dbx_regnum = dbx_reg_number (rtl);
14611 if (dbx_regnum == IGNORED_DWARF_REGNUM)
14612 break;
14613 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_regval_type),
14614 dbx_regnum, 0);
14615 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
14616 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
14617 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
14618 break;
14620 /* Whenever a register number forms a part of the description of the
14621 method for calculating the (dynamic) address of a memory resident
14622 object, DWARF rules require the register number be referred to as
14623 a "base register". This distinction is not based in any way upon
14624 what category of register the hardware believes the given register
14625 belongs to. This is strictly DWARF terminology we're dealing with
14626 here. Note that in cases where the location of a memory-resident
14627 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
14628 OP_CONST (0)) the actual DWARF location descriptor that we generate
14629 may just be OP_BASEREG (basereg). This may look deceptively like
14630 the object in question was allocated to a register (rather than in
14631 memory) so DWARF consumers need to be aware of the subtle
14632 distinction between OP_REG and OP_BASEREG. */
14633 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
14634 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
14635 else if (stack_realign_drap
14636 && crtl->drap_reg
14637 && crtl->args.internal_arg_pointer == rtl
14638 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
14640 /* If RTL is internal_arg_pointer, which has been optimized
14641 out, use DRAP instead. */
14642 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
14643 VAR_INIT_STATUS_INITIALIZED);
14645 break;
14647 case SIGN_EXTEND:
14648 case ZERO_EXTEND:
14649 if (!SCALAR_INT_MODE_P (mode))
14650 break;
14651 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
14652 mem_mode, VAR_INIT_STATUS_INITIALIZED);
14653 if (op0 == 0)
14654 break;
14655 else if (GET_CODE (rtl) == ZERO_EXTEND
14656 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
14657 && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
14658 < HOST_BITS_PER_WIDE_INT
14659 /* If DW_OP_const{1,2,4}u won't be used, it is shorter
14660 to expand zero extend as two shifts instead of
14661 masking. */
14662 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= 4)
14664 machine_mode imode = GET_MODE (XEXP (rtl, 0));
14665 mem_loc_result = op0;
14666 add_loc_descr (&mem_loc_result,
14667 int_loc_descriptor (GET_MODE_MASK (imode)));
14668 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
14670 else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
14672 int shift = DWARF2_ADDR_SIZE
14673 - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
14674 shift *= BITS_PER_UNIT;
14675 if (GET_CODE (rtl) == SIGN_EXTEND)
14676 op = DW_OP_shra;
14677 else
14678 op = DW_OP_shr;
14679 mem_loc_result = op0;
14680 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
14681 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
14682 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
14683 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14685 else if (!dwarf_strict || dwarf_version >= 5)
14687 dw_die_ref type_die1, type_die2;
14688 dw_loc_descr_ref cvt;
14690 type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
14691 GET_CODE (rtl) == ZERO_EXTEND);
14692 if (type_die1 == NULL)
14693 break;
14694 type_die2 = base_type_for_mode (mode, 1);
14695 if (type_die2 == NULL)
14696 break;
14697 mem_loc_result = op0;
14698 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14699 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14700 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
14701 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14702 add_loc_descr (&mem_loc_result, cvt);
14703 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14704 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14705 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
14706 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14707 add_loc_descr (&mem_loc_result, cvt);
14709 break;
14711 case MEM:
14713 rtx new_rtl = avoid_constant_pool_reference (rtl);
14714 if (new_rtl != rtl)
14716 mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
14717 initialized);
14718 if (mem_loc_result != NULL)
14719 return mem_loc_result;
14722 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
14723 get_address_mode (rtl), mode,
14724 VAR_INIT_STATUS_INITIALIZED);
14725 if (mem_loc_result == NULL)
14726 mem_loc_result = tls_mem_loc_descriptor (rtl);
14727 if (mem_loc_result != NULL)
14729 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
14730 || !SCALAR_INT_MODE_P(mode))
14732 dw_die_ref type_die;
14733 dw_loc_descr_ref deref;
14735 if (dwarf_strict && dwarf_version < 5)
14736 return NULL;
14737 type_die
14738 = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
14739 if (type_die == NULL)
14740 return NULL;
14741 deref = new_loc_descr (dwarf_OP (DW_OP_deref_type),
14742 GET_MODE_SIZE (mode), 0);
14743 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
14744 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
14745 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
14746 add_loc_descr (&mem_loc_result, deref);
14748 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
14749 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
14750 else
14751 add_loc_descr (&mem_loc_result,
14752 new_loc_descr (DW_OP_deref_size,
14753 GET_MODE_SIZE (mode), 0));
14755 break;
14757 case LO_SUM:
14758 return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
14760 case LABEL_REF:
14761 /* Some ports can transform a symbol ref into a label ref, because
14762 the symbol ref is too far away and has to be dumped into a constant
14763 pool. */
14764 case CONST:
14765 case SYMBOL_REF:
14766 if (!SCALAR_INT_MODE_P (mode)
14767 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
14768 #ifdef POINTERS_EXTEND_UNSIGNED
14769 && (mode != Pmode || mem_mode == VOIDmode)
14770 #endif
14772 break;
14773 if (GET_CODE (rtl) == SYMBOL_REF
14774 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
14776 dw_loc_descr_ref temp;
14778 /* If this is not defined, we have no way to emit the data. */
14779 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
14780 break;
14782 temp = new_addr_loc_descr (rtl, dtprel_true);
14784 /* We check for DWARF 5 here because gdb did not implement
14785 DW_OP_form_tls_address until after 7.12. */
14786 mem_loc_result = new_loc_descr ((dwarf_version >= 5
14787 ? DW_OP_form_tls_address
14788 : DW_OP_GNU_push_tls_address),
14789 0, 0);
14790 add_loc_descr (&mem_loc_result, temp);
14792 break;
14795 if (!const_ok_for_output (rtl))
14797 if (GET_CODE (rtl) == CONST)
14798 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14799 initialized);
14800 break;
14803 symref:
14804 mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
14805 vec_safe_push (used_rtx_array, rtl);
14806 break;
14808 case CONCAT:
14809 case CONCATN:
14810 case VAR_LOCATION:
14811 case DEBUG_IMPLICIT_PTR:
14812 expansion_failed (NULL_TREE, rtl,
14813 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
14814 return 0;
14816 case ENTRY_VALUE:
14817 if (dwarf_strict && dwarf_version < 5)
14818 return NULL;
14819 if (REG_P (ENTRY_VALUE_EXP (rtl)))
14821 if (!SCALAR_INT_MODE_P (mode)
14822 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
14823 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
14824 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14825 else
14827 unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
14828 if (dbx_regnum == IGNORED_DWARF_REGNUM)
14829 return NULL;
14830 op0 = one_reg_loc_descriptor (dbx_regnum,
14831 VAR_INIT_STATUS_INITIALIZED);
14834 else if (MEM_P (ENTRY_VALUE_EXP (rtl))
14835 && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
14837 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
14838 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14839 if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
14840 return NULL;
14842 else
14843 gcc_unreachable ();
14844 if (op0 == NULL)
14845 return NULL;
14846 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_entry_value), 0, 0);
14847 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
14848 mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
14849 break;
14851 case DEBUG_PARAMETER_REF:
14852 mem_loc_result = parameter_ref_descriptor (rtl);
14853 break;
14855 case PRE_MODIFY:
14856 /* Extract the PLUS expression nested inside and fall into
14857 PLUS code below. */
14858 rtl = XEXP (rtl, 1);
14859 goto plus;
14861 case PRE_INC:
14862 case PRE_DEC:
14863 /* Turn these into a PLUS expression and fall into the PLUS code
14864 below. */
14865 rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
14866 gen_int_mode (GET_CODE (rtl) == PRE_INC
14867 ? GET_MODE_UNIT_SIZE (mem_mode)
14868 : -GET_MODE_UNIT_SIZE (mem_mode),
14869 mode));
14871 /* fall through */
14873 case PLUS:
14874 plus:
14875 if (is_based_loc (rtl)
14876 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
14877 || XEXP (rtl, 0) == arg_pointer_rtx
14878 || XEXP (rtl, 0) == frame_pointer_rtx)
14879 && SCALAR_INT_MODE_P (mode))
14880 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
14881 INTVAL (XEXP (rtl, 1)),
14882 VAR_INIT_STATUS_INITIALIZED);
14883 else
14885 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14886 VAR_INIT_STATUS_INITIALIZED);
14887 if (mem_loc_result == 0)
14888 break;
14890 if (CONST_INT_P (XEXP (rtl, 1))
14891 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
14892 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
14893 else
14895 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14896 VAR_INIT_STATUS_INITIALIZED);
14897 if (op1 == 0)
14898 return NULL;
14899 add_loc_descr (&mem_loc_result, op1);
14900 add_loc_descr (&mem_loc_result,
14901 new_loc_descr (DW_OP_plus, 0, 0));
14904 break;
14906 /* If a pseudo-reg is optimized away, it is possible for it to
14907 be replaced with a MEM containing a multiply or shift. */
14908 case MINUS:
14909 op = DW_OP_minus;
14910 goto do_binop;
14912 case MULT:
14913 op = DW_OP_mul;
14914 goto do_binop;
14916 case DIV:
14917 if ((!dwarf_strict || dwarf_version >= 5)
14918 && SCALAR_INT_MODE_P (mode)
14919 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
14921 mem_loc_result = typed_binop (DW_OP_div, rtl,
14922 base_type_for_mode (mode, 0),
14923 mode, mem_mode);
14924 break;
14926 op = DW_OP_div;
14927 goto do_binop;
14929 case UMOD:
14930 op = DW_OP_mod;
14931 goto do_binop;
14933 case ASHIFT:
14934 op = DW_OP_shl;
14935 goto do_shift;
14937 case ASHIFTRT:
14938 op = DW_OP_shra;
14939 goto do_shift;
14941 case LSHIFTRT:
14942 op = DW_OP_shr;
14943 goto do_shift;
14945 do_shift:
14946 if (!SCALAR_INT_MODE_P (mode))
14947 break;
14948 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14949 VAR_INIT_STATUS_INITIALIZED);
14951 rtx rtlop1 = XEXP (rtl, 1);
14952 if (GET_MODE (rtlop1) != VOIDmode
14953 && GET_MODE_BITSIZE (GET_MODE (rtlop1))
14954 < GET_MODE_BITSIZE (mode))
14955 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
14956 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
14957 VAR_INIT_STATUS_INITIALIZED);
14960 if (op0 == 0 || op1 == 0)
14961 break;
14963 mem_loc_result = op0;
14964 add_loc_descr (&mem_loc_result, op1);
14965 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14966 break;
14968 case AND:
14969 op = DW_OP_and;
14970 goto do_binop;
14972 case IOR:
14973 op = DW_OP_or;
14974 goto do_binop;
14976 case XOR:
14977 op = DW_OP_xor;
14978 goto do_binop;
14980 do_binop:
14981 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14982 VAR_INIT_STATUS_INITIALIZED);
14983 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14984 VAR_INIT_STATUS_INITIALIZED);
14986 if (op0 == 0 || op1 == 0)
14987 break;
14989 mem_loc_result = op0;
14990 add_loc_descr (&mem_loc_result, op1);
14991 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14992 break;
14994 case MOD:
14995 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
14996 && (!dwarf_strict || dwarf_version >= 5))
14998 mem_loc_result = typed_binop (DW_OP_mod, rtl,
14999 base_type_for_mode (mode, 0),
15000 mode, mem_mode);
15001 break;
15004 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15005 VAR_INIT_STATUS_INITIALIZED);
15006 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15007 VAR_INIT_STATUS_INITIALIZED);
15009 if (op0 == 0 || op1 == 0)
15010 break;
15012 mem_loc_result = op0;
15013 add_loc_descr (&mem_loc_result, op1);
15014 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
15015 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
15016 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
15017 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
15018 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
15019 break;
15021 case UDIV:
15022 if ((!dwarf_strict || dwarf_version >= 5)
15023 && SCALAR_INT_MODE_P (mode))
15025 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
15027 op = DW_OP_div;
15028 goto do_binop;
15030 mem_loc_result = typed_binop (DW_OP_div, rtl,
15031 base_type_for_mode (mode, 1),
15032 mode, mem_mode);
15034 break;
15036 case NOT:
15037 op = DW_OP_not;
15038 goto do_unop;
15040 case ABS:
15041 op = DW_OP_abs;
15042 goto do_unop;
15044 case NEG:
15045 op = DW_OP_neg;
15046 goto do_unop;
15048 do_unop:
15049 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15050 VAR_INIT_STATUS_INITIALIZED);
15052 if (op0 == 0)
15053 break;
15055 mem_loc_result = op0;
15056 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15057 break;
15059 case CONST_INT:
15060 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
15061 #ifdef POINTERS_EXTEND_UNSIGNED
15062 || (mode == Pmode
15063 && mem_mode != VOIDmode
15064 && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
15065 #endif
15068 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
15069 break;
15071 if ((!dwarf_strict || dwarf_version >= 5)
15072 && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
15073 || GET_MODE_BITSIZE (mode) == HOST_BITS_PER_DOUBLE_INT))
15075 dw_die_ref type_die = base_type_for_mode (mode, 1);
15076 machine_mode amode;
15077 if (type_die == NULL)
15078 return NULL;
15079 amode = mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT,
15080 MODE_INT, 0);
15081 if (INTVAL (rtl) >= 0
15082 && amode != BLKmode
15083 && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
15084 /* const DW_OP_convert <XXX> vs.
15085 DW_OP_const_type <XXX, 1, const>. */
15086 && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
15087 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (mode))
15089 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
15090 op0 = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15091 op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15092 op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15093 op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
15094 add_loc_descr (&mem_loc_result, op0);
15095 return mem_loc_result;
15097 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0,
15098 INTVAL (rtl));
15099 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15100 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15101 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
15102 if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
15103 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
15104 else
15106 mem_loc_result->dw_loc_oprnd2.val_class
15107 = dw_val_class_const_double;
15108 mem_loc_result->dw_loc_oprnd2.v.val_double
15109 = double_int::from_shwi (INTVAL (rtl));
15112 break;
15114 case CONST_DOUBLE:
15115 if (!dwarf_strict || dwarf_version >= 5)
15117 dw_die_ref type_die;
15119 /* Note that if TARGET_SUPPORTS_WIDE_INT == 0, a
15120 CONST_DOUBLE rtx could represent either a large integer
15121 or a floating-point constant. If TARGET_SUPPORTS_WIDE_INT != 0,
15122 the value is always a floating point constant.
15124 When it is an integer, a CONST_DOUBLE is used whenever
15125 the constant requires 2 HWIs to be adequately represented.
15126 We output CONST_DOUBLEs as blocks. */
15127 if (mode == VOIDmode
15128 || (GET_MODE (rtl) == VOIDmode
15129 && GET_MODE_BITSIZE (mode) != HOST_BITS_PER_DOUBLE_INT))
15130 break;
15131 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
15132 if (type_die == NULL)
15133 return NULL;
15134 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0, 0);
15135 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15136 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15137 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
15138 #if TARGET_SUPPORTS_WIDE_INT == 0
15139 if (!SCALAR_FLOAT_MODE_P (mode))
15141 mem_loc_result->dw_loc_oprnd2.val_class
15142 = dw_val_class_const_double;
15143 mem_loc_result->dw_loc_oprnd2.v.val_double
15144 = rtx_to_double_int (rtl);
15146 else
15147 #endif
15149 unsigned int length = GET_MODE_SIZE (mode);
15150 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
15152 insert_float (rtl, array);
15153 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
15154 mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
15155 mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
15156 mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
15159 break;
15161 case CONST_WIDE_INT:
15162 if (!dwarf_strict || dwarf_version >= 5)
15164 dw_die_ref type_die;
15166 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
15167 if (type_die == NULL)
15168 return NULL;
15169 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0, 0);
15170 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15171 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15172 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
15173 mem_loc_result->dw_loc_oprnd2.val_class
15174 = dw_val_class_wide_int;
15175 mem_loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
15176 *mem_loc_result->dw_loc_oprnd2.v.val_wide = rtx_mode_t (rtl, mode);
15178 break;
15180 case EQ:
15181 mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
15182 break;
15184 case GE:
15185 mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
15186 break;
15188 case GT:
15189 mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
15190 break;
15192 case LE:
15193 mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
15194 break;
15196 case LT:
15197 mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
15198 break;
15200 case NE:
15201 mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
15202 break;
15204 case GEU:
15205 mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
15206 break;
15208 case GTU:
15209 mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
15210 break;
15212 case LEU:
15213 mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
15214 break;
15216 case LTU:
15217 mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
15218 break;
15220 case UMIN:
15221 case UMAX:
15222 if (!SCALAR_INT_MODE_P (mode))
15223 break;
15224 /* FALLTHRU */
15225 case SMIN:
15226 case SMAX:
15227 mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
15228 break;
15230 case ZERO_EXTRACT:
15231 case SIGN_EXTRACT:
15232 if (CONST_INT_P (XEXP (rtl, 1))
15233 && CONST_INT_P (XEXP (rtl, 2))
15234 && ((unsigned) INTVAL (XEXP (rtl, 1))
15235 + (unsigned) INTVAL (XEXP (rtl, 2))
15236 <= GET_MODE_BITSIZE (mode))
15237 && SCALAR_INT_MODE_P (mode)
15238 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
15239 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
15241 int shift, size;
15242 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
15243 mem_mode, VAR_INIT_STATUS_INITIALIZED);
15244 if (op0 == 0)
15245 break;
15246 if (GET_CODE (rtl) == SIGN_EXTRACT)
15247 op = DW_OP_shra;
15248 else
15249 op = DW_OP_shr;
15250 mem_loc_result = op0;
15251 size = INTVAL (XEXP (rtl, 1));
15252 shift = INTVAL (XEXP (rtl, 2));
15253 if (BITS_BIG_ENDIAN)
15254 shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
15255 - shift - size;
15256 if (shift + size != (int) DWARF2_ADDR_SIZE)
15258 add_loc_descr (&mem_loc_result,
15259 int_loc_descriptor (DWARF2_ADDR_SIZE
15260 - shift - size));
15261 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
15263 if (size != (int) DWARF2_ADDR_SIZE)
15265 add_loc_descr (&mem_loc_result,
15266 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
15267 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15270 break;
15272 case IF_THEN_ELSE:
15274 dw_loc_descr_ref op2, bra_node, drop_node;
15275 op0 = mem_loc_descriptor (XEXP (rtl, 0),
15276 GET_MODE (XEXP (rtl, 0)) == VOIDmode
15277 ? word_mode : GET_MODE (XEXP (rtl, 0)),
15278 mem_mode, VAR_INIT_STATUS_INITIALIZED);
15279 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15280 VAR_INIT_STATUS_INITIALIZED);
15281 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
15282 VAR_INIT_STATUS_INITIALIZED);
15283 if (op0 == NULL || op1 == NULL || op2 == NULL)
15284 break;
15286 mem_loc_result = op1;
15287 add_loc_descr (&mem_loc_result, op2);
15288 add_loc_descr (&mem_loc_result, op0);
15289 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
15290 add_loc_descr (&mem_loc_result, bra_node);
15291 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
15292 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
15293 add_loc_descr (&mem_loc_result, drop_node);
15294 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15295 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
15297 break;
15299 case FLOAT_EXTEND:
15300 case FLOAT_TRUNCATE:
15301 case FLOAT:
15302 case UNSIGNED_FLOAT:
15303 case FIX:
15304 case UNSIGNED_FIX:
15305 if (!dwarf_strict || dwarf_version >= 5)
15307 dw_die_ref type_die;
15308 dw_loc_descr_ref cvt;
15310 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
15311 mem_mode, VAR_INIT_STATUS_INITIALIZED);
15312 if (op0 == NULL)
15313 break;
15314 if (SCALAR_INT_MODE_P (GET_MODE (XEXP (rtl, 0)))
15315 && (GET_CODE (rtl) == FLOAT
15316 || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
15317 <= DWARF2_ADDR_SIZE))
15319 type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
15320 GET_CODE (rtl) == UNSIGNED_FLOAT);
15321 if (type_die == NULL)
15322 break;
15323 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15324 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15325 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15326 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15327 add_loc_descr (&op0, cvt);
15329 type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
15330 if (type_die == NULL)
15331 break;
15332 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15333 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15334 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15335 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15336 add_loc_descr (&op0, cvt);
15337 if (SCALAR_INT_MODE_P (mode)
15338 && (GET_CODE (rtl) == FIX
15339 || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
15341 op0 = convert_descriptor_to_mode (mode, op0);
15342 if (op0 == NULL)
15343 break;
15345 mem_loc_result = op0;
15347 break;
15349 case CLZ:
15350 case CTZ:
15351 case FFS:
15352 mem_loc_result = clz_loc_descriptor (rtl, mode, mem_mode);
15353 break;
15355 case POPCOUNT:
15356 case PARITY:
15357 mem_loc_result = popcount_loc_descriptor (rtl, mode, mem_mode);
15358 break;
15360 case BSWAP:
15361 mem_loc_result = bswap_loc_descriptor (rtl, mode, mem_mode);
15362 break;
15364 case ROTATE:
15365 case ROTATERT:
15366 mem_loc_result = rotate_loc_descriptor (rtl, mode, mem_mode);
15367 break;
15369 case COMPARE:
15370 /* In theory, we could implement the above. */
15371 /* DWARF cannot represent the unsigned compare operations
15372 natively. */
15373 case SS_MULT:
15374 case US_MULT:
15375 case SS_DIV:
15376 case US_DIV:
15377 case SS_PLUS:
15378 case US_PLUS:
15379 case SS_MINUS:
15380 case US_MINUS:
15381 case SS_NEG:
15382 case US_NEG:
15383 case SS_ABS:
15384 case SS_ASHIFT:
15385 case US_ASHIFT:
15386 case SS_TRUNCATE:
15387 case US_TRUNCATE:
15388 case UNORDERED:
15389 case ORDERED:
15390 case UNEQ:
15391 case UNGE:
15392 case UNGT:
15393 case UNLE:
15394 case UNLT:
15395 case LTGT:
15396 case FRACT_CONVERT:
15397 case UNSIGNED_FRACT_CONVERT:
15398 case SAT_FRACT:
15399 case UNSIGNED_SAT_FRACT:
15400 case SQRT:
15401 case ASM_OPERANDS:
15402 case VEC_MERGE:
15403 case VEC_SELECT:
15404 case VEC_CONCAT:
15405 case VEC_DUPLICATE:
15406 case UNSPEC:
15407 case HIGH:
15408 case FMA:
15409 case STRICT_LOW_PART:
15410 case CONST_VECTOR:
15411 case CONST_FIXED:
15412 case CLRSB:
15413 case CLOBBER:
15414 /* If delegitimize_address couldn't do anything with the UNSPEC, we
15415 can't express it in the debug info. This can happen e.g. with some
15416 TLS UNSPECs. */
15417 break;
15419 case CONST_STRING:
15420 resolve_one_addr (&rtl);
15421 goto symref;
15423 /* RTL sequences inside PARALLEL record a series of DWARF operations for
15424 the expression. An UNSPEC rtx represents a raw DWARF operation,
15425 new_loc_descr is called for it to build the operation directly.
15426 Otherwise mem_loc_descriptor is called recursively. */
15427 case PARALLEL:
15429 int index = 0;
15430 dw_loc_descr_ref exp_result = NULL;
15432 for (; index < XVECLEN (rtl, 0); index++)
15434 rtx elem = XVECEXP (rtl, 0, index);
15435 if (GET_CODE (elem) == UNSPEC)
15437 /* Each DWARF operation UNSPEC contain two operands, if
15438 one operand is not used for the operation, const0_rtx is
15439 passed. */
15440 gcc_assert (XVECLEN (elem, 0) == 2);
15442 HOST_WIDE_INT dw_op = XINT (elem, 1);
15443 HOST_WIDE_INT oprnd1 = INTVAL (XVECEXP (elem, 0, 0));
15444 HOST_WIDE_INT oprnd2 = INTVAL (XVECEXP (elem, 0, 1));
15445 exp_result
15446 = new_loc_descr ((enum dwarf_location_atom) dw_op, oprnd1,
15447 oprnd2);
15449 else
15450 exp_result
15451 = mem_loc_descriptor (elem, mode, mem_mode,
15452 VAR_INIT_STATUS_INITIALIZED);
15454 if (!mem_loc_result)
15455 mem_loc_result = exp_result;
15456 else
15457 add_loc_descr (&mem_loc_result, exp_result);
15460 break;
15463 default:
15464 if (flag_checking)
15466 print_rtl (stderr, rtl);
15467 gcc_unreachable ();
15469 break;
15472 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
15473 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
15475 return mem_loc_result;
15478 /* Return a descriptor that describes the concatenation of two locations.
15479 This is typically a complex variable. */
15481 static dw_loc_descr_ref
15482 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
15484 dw_loc_descr_ref cc_loc_result = NULL;
15485 dw_loc_descr_ref x0_ref
15486 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15487 dw_loc_descr_ref x1_ref
15488 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15490 if (x0_ref == 0 || x1_ref == 0)
15491 return 0;
15493 cc_loc_result = x0_ref;
15494 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
15496 add_loc_descr (&cc_loc_result, x1_ref);
15497 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
15499 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
15500 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
15502 return cc_loc_result;
15505 /* Return a descriptor that describes the concatenation of N
15506 locations. */
15508 static dw_loc_descr_ref
15509 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
15511 unsigned int i;
15512 dw_loc_descr_ref cc_loc_result = NULL;
15513 unsigned int n = XVECLEN (concatn, 0);
15515 for (i = 0; i < n; ++i)
15517 dw_loc_descr_ref ref;
15518 rtx x = XVECEXP (concatn, 0, i);
15520 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15521 if (ref == NULL)
15522 return NULL;
15524 add_loc_descr (&cc_loc_result, ref);
15525 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
15528 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
15529 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
15531 return cc_loc_result;
15534 /* Helper function for loc_descriptor. Return DW_OP_implicit_pointer
15535 for DEBUG_IMPLICIT_PTR RTL. */
15537 static dw_loc_descr_ref
15538 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
15540 dw_loc_descr_ref ret;
15541 dw_die_ref ref;
15543 if (dwarf_strict && dwarf_version < 5)
15544 return NULL;
15545 gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
15546 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
15547 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
15548 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
15549 ret = new_loc_descr (dwarf_OP (DW_OP_implicit_pointer), 0, offset);
15550 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
15551 if (ref)
15553 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15554 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
15555 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
15557 else
15559 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
15560 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
15562 return ret;
15565 /* Output a proper Dwarf location descriptor for a variable or parameter
15566 which is either allocated in a register or in a memory location. For a
15567 register, we just generate an OP_REG and the register number. For a
15568 memory location we provide a Dwarf postfix expression describing how to
15569 generate the (dynamic) address of the object onto the address stack.
15571 MODE is mode of the decl if this loc_descriptor is going to be used in
15572 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
15573 allowed, VOIDmode otherwise.
15575 If we don't know how to describe it, return 0. */
15577 static dw_loc_descr_ref
15578 loc_descriptor (rtx rtl, machine_mode mode,
15579 enum var_init_status initialized)
15581 dw_loc_descr_ref loc_result = NULL;
15583 switch (GET_CODE (rtl))
15585 case SUBREG:
15586 /* The case of a subreg may arise when we have a local (register)
15587 variable or a formal (register) parameter which doesn't quite fill
15588 up an entire register. For now, just assume that it is
15589 legitimate to make the Dwarf info refer to the whole register which
15590 contains the given subreg. */
15591 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
15592 loc_result = loc_descriptor (SUBREG_REG (rtl),
15593 GET_MODE (SUBREG_REG (rtl)), initialized);
15594 else
15595 goto do_default;
15596 break;
15598 case REG:
15599 loc_result = reg_loc_descriptor (rtl, initialized);
15600 break;
15602 case MEM:
15603 loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
15604 GET_MODE (rtl), initialized);
15605 if (loc_result == NULL)
15606 loc_result = tls_mem_loc_descriptor (rtl);
15607 if (loc_result == NULL)
15609 rtx new_rtl = avoid_constant_pool_reference (rtl);
15610 if (new_rtl != rtl)
15611 loc_result = loc_descriptor (new_rtl, mode, initialized);
15613 break;
15615 case CONCAT:
15616 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
15617 initialized);
15618 break;
15620 case CONCATN:
15621 loc_result = concatn_loc_descriptor (rtl, initialized);
15622 break;
15624 case VAR_LOCATION:
15625 /* Single part. */
15626 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
15628 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
15629 if (GET_CODE (loc) == EXPR_LIST)
15630 loc = XEXP (loc, 0);
15631 loc_result = loc_descriptor (loc, mode, initialized);
15632 break;
15635 rtl = XEXP (rtl, 1);
15636 /* FALLTHRU */
15638 case PARALLEL:
15640 rtvec par_elems = XVEC (rtl, 0);
15641 int num_elem = GET_NUM_ELEM (par_elems);
15642 machine_mode mode;
15643 int i;
15645 /* Create the first one, so we have something to add to. */
15646 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
15647 VOIDmode, initialized);
15648 if (loc_result == NULL)
15649 return NULL;
15650 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
15651 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
15652 for (i = 1; i < num_elem; i++)
15654 dw_loc_descr_ref temp;
15656 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
15657 VOIDmode, initialized);
15658 if (temp == NULL)
15659 return NULL;
15660 add_loc_descr (&loc_result, temp);
15661 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
15662 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
15665 break;
15667 case CONST_INT:
15668 if (mode != VOIDmode && mode != BLKmode)
15669 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
15670 INTVAL (rtl));
15671 break;
15673 case CONST_DOUBLE:
15674 if (mode == VOIDmode)
15675 mode = GET_MODE (rtl);
15677 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
15679 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
15681 /* Note that a CONST_DOUBLE rtx could represent either an integer
15682 or a floating-point constant. A CONST_DOUBLE is used whenever
15683 the constant requires more than one word in order to be
15684 adequately represented. We output CONST_DOUBLEs as blocks. */
15685 loc_result = new_loc_descr (DW_OP_implicit_value,
15686 GET_MODE_SIZE (mode), 0);
15687 #if TARGET_SUPPORTS_WIDE_INT == 0
15688 if (!SCALAR_FLOAT_MODE_P (mode))
15690 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
15691 loc_result->dw_loc_oprnd2.v.val_double
15692 = rtx_to_double_int (rtl);
15694 else
15695 #endif
15697 unsigned int length = GET_MODE_SIZE (mode);
15698 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
15700 insert_float (rtl, array);
15701 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
15702 loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
15703 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
15704 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
15707 break;
15709 case CONST_WIDE_INT:
15710 if (mode == VOIDmode)
15711 mode = GET_MODE (rtl);
15713 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
15715 loc_result = new_loc_descr (DW_OP_implicit_value,
15716 GET_MODE_SIZE (mode), 0);
15717 loc_result->dw_loc_oprnd2.val_class = dw_val_class_wide_int;
15718 loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
15719 *loc_result->dw_loc_oprnd2.v.val_wide = rtx_mode_t (rtl, mode);
15721 break;
15723 case CONST_VECTOR:
15724 if (mode == VOIDmode)
15725 mode = GET_MODE (rtl);
15727 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
15729 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
15730 unsigned int length = CONST_VECTOR_NUNITS (rtl);
15731 unsigned char *array
15732 = ggc_vec_alloc<unsigned char> (length * elt_size);
15733 unsigned int i;
15734 unsigned char *p;
15735 machine_mode imode = GET_MODE_INNER (mode);
15737 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
15738 switch (GET_MODE_CLASS (mode))
15740 case MODE_VECTOR_INT:
15741 for (i = 0, p = array; i < length; i++, p += elt_size)
15743 rtx elt = CONST_VECTOR_ELT (rtl, i);
15744 insert_wide_int (rtx_mode_t (elt, imode), p, elt_size);
15746 break;
15748 case MODE_VECTOR_FLOAT:
15749 for (i = 0, p = array; i < length; i++, p += elt_size)
15751 rtx elt = CONST_VECTOR_ELT (rtl, i);
15752 insert_float (elt, p);
15754 break;
15756 default:
15757 gcc_unreachable ();
15760 loc_result = new_loc_descr (DW_OP_implicit_value,
15761 length * elt_size, 0);
15762 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
15763 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
15764 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
15765 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
15767 break;
15769 case CONST:
15770 if (mode == VOIDmode
15771 || CONST_SCALAR_INT_P (XEXP (rtl, 0))
15772 || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
15773 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
15775 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
15776 break;
15778 /* FALLTHROUGH */
15779 case SYMBOL_REF:
15780 if (!const_ok_for_output (rtl))
15781 break;
15782 /* FALLTHROUGH */
15783 case LABEL_REF:
15784 if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
15785 && (dwarf_version >= 4 || !dwarf_strict))
15787 loc_result = new_addr_loc_descr (rtl, dtprel_false);
15788 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
15789 vec_safe_push (used_rtx_array, rtl);
15791 break;
15793 case DEBUG_IMPLICIT_PTR:
15794 loc_result = implicit_ptr_descriptor (rtl, 0);
15795 break;
15797 case PLUS:
15798 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
15799 && CONST_INT_P (XEXP (rtl, 1)))
15801 loc_result
15802 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
15803 break;
15805 /* FALLTHRU */
15806 do_default:
15807 default:
15808 if ((SCALAR_INT_MODE_P (mode)
15809 && GET_MODE (rtl) == mode
15810 && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
15811 && dwarf_version >= 4)
15812 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
15814 /* Value expression. */
15815 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
15816 if (loc_result)
15817 add_loc_descr (&loc_result,
15818 new_loc_descr (DW_OP_stack_value, 0, 0));
15820 break;
15823 return loc_result;
15826 /* We need to figure out what section we should use as the base for the
15827 address ranges where a given location is valid.
15828 1. If this particular DECL has a section associated with it, use that.
15829 2. If this function has a section associated with it, use that.
15830 3. Otherwise, use the text section.
15831 XXX: If you split a variable across multiple sections, we won't notice. */
15833 static const char *
15834 secname_for_decl (const_tree decl)
15836 const char *secname;
15838 if (VAR_OR_FUNCTION_DECL_P (decl)
15839 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl) || TREE_STATIC (decl))
15840 && DECL_SECTION_NAME (decl))
15841 secname = DECL_SECTION_NAME (decl);
15842 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
15843 secname = DECL_SECTION_NAME (current_function_decl);
15844 else if (cfun && in_cold_section_p)
15845 secname = crtl->subsections.cold_section_label;
15846 else
15847 secname = text_section_label;
15849 return secname;
15852 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
15854 static bool
15855 decl_by_reference_p (tree decl)
15857 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
15858 || VAR_P (decl))
15859 && DECL_BY_REFERENCE (decl));
15862 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
15863 for VARLOC. */
15865 static dw_loc_descr_ref
15866 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
15867 enum var_init_status initialized)
15869 int have_address = 0;
15870 dw_loc_descr_ref descr;
15871 machine_mode mode;
15873 if (want_address != 2)
15875 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
15876 /* Single part. */
15877 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
15879 varloc = PAT_VAR_LOCATION_LOC (varloc);
15880 if (GET_CODE (varloc) == EXPR_LIST)
15881 varloc = XEXP (varloc, 0);
15882 mode = GET_MODE (varloc);
15883 if (MEM_P (varloc))
15885 rtx addr = XEXP (varloc, 0);
15886 descr = mem_loc_descriptor (addr, get_address_mode (varloc),
15887 mode, initialized);
15888 if (descr)
15889 have_address = 1;
15890 else
15892 rtx x = avoid_constant_pool_reference (varloc);
15893 if (x != varloc)
15894 descr = mem_loc_descriptor (x, mode, VOIDmode,
15895 initialized);
15898 else
15899 descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
15901 else
15902 return 0;
15904 else
15906 if (GET_CODE (varloc) == VAR_LOCATION)
15907 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
15908 else
15909 mode = DECL_MODE (loc);
15910 descr = loc_descriptor (varloc, mode, initialized);
15911 have_address = 1;
15914 if (!descr)
15915 return 0;
15917 if (want_address == 2 && !have_address
15918 && (dwarf_version >= 4 || !dwarf_strict))
15920 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
15922 expansion_failed (loc, NULL_RTX,
15923 "DWARF address size mismatch");
15924 return 0;
15926 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
15927 have_address = 1;
15929 /* Show if we can't fill the request for an address. */
15930 if (want_address && !have_address)
15932 expansion_failed (loc, NULL_RTX,
15933 "Want address and only have value");
15934 return 0;
15937 /* If we've got an address and don't want one, dereference. */
15938 if (!want_address && have_address)
15940 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
15941 enum dwarf_location_atom op;
15943 if (size > DWARF2_ADDR_SIZE || size == -1)
15945 expansion_failed (loc, NULL_RTX,
15946 "DWARF address size mismatch");
15947 return 0;
15949 else if (size == DWARF2_ADDR_SIZE)
15950 op = DW_OP_deref;
15951 else
15952 op = DW_OP_deref_size;
15954 add_loc_descr (&descr, new_loc_descr (op, size, 0));
15957 return descr;
15960 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
15961 if it is not possible. */
15963 static dw_loc_descr_ref
15964 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
15966 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
15967 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
15968 else if (dwarf_version >= 3 || !dwarf_strict)
15969 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
15970 else
15971 return NULL;
15974 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
15975 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
15977 static dw_loc_descr_ref
15978 dw_sra_loc_expr (tree decl, rtx loc)
15980 rtx p;
15981 unsigned HOST_WIDE_INT padsize = 0;
15982 dw_loc_descr_ref descr, *descr_tail;
15983 unsigned HOST_WIDE_INT decl_size;
15984 rtx varloc;
15985 enum var_init_status initialized;
15987 if (DECL_SIZE (decl) == NULL
15988 || !tree_fits_uhwi_p (DECL_SIZE (decl)))
15989 return NULL;
15991 decl_size = tree_to_uhwi (DECL_SIZE (decl));
15992 descr = NULL;
15993 descr_tail = &descr;
15995 for (p = loc; p; p = XEXP (p, 1))
15997 unsigned HOST_WIDE_INT bitsize = decl_piece_bitsize (p);
15998 rtx loc_note = *decl_piece_varloc_ptr (p);
15999 dw_loc_descr_ref cur_descr;
16000 dw_loc_descr_ref *tail, last = NULL;
16001 unsigned HOST_WIDE_INT opsize = 0;
16003 if (loc_note == NULL_RTX
16004 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
16006 padsize += bitsize;
16007 continue;
16009 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
16010 varloc = NOTE_VAR_LOCATION (loc_note);
16011 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
16012 if (cur_descr == NULL)
16014 padsize += bitsize;
16015 continue;
16018 /* Check that cur_descr either doesn't use
16019 DW_OP_*piece operations, or their sum is equal
16020 to bitsize. Otherwise we can't embed it. */
16021 for (tail = &cur_descr; *tail != NULL;
16022 tail = &(*tail)->dw_loc_next)
16023 if ((*tail)->dw_loc_opc == DW_OP_piece)
16025 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
16026 * BITS_PER_UNIT;
16027 last = *tail;
16029 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
16031 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
16032 last = *tail;
16035 if (last != NULL && opsize != bitsize)
16037 padsize += bitsize;
16038 /* Discard the current piece of the descriptor and release any
16039 addr_table entries it uses. */
16040 remove_loc_list_addr_table_entries (cur_descr);
16041 continue;
16044 /* If there is a hole, add DW_OP_*piece after empty DWARF
16045 expression, which means that those bits are optimized out. */
16046 if (padsize)
16048 if (padsize > decl_size)
16050 remove_loc_list_addr_table_entries (cur_descr);
16051 goto discard_descr;
16053 decl_size -= padsize;
16054 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
16055 if (*descr_tail == NULL)
16057 remove_loc_list_addr_table_entries (cur_descr);
16058 goto discard_descr;
16060 descr_tail = &(*descr_tail)->dw_loc_next;
16061 padsize = 0;
16063 *descr_tail = cur_descr;
16064 descr_tail = tail;
16065 if (bitsize > decl_size)
16066 goto discard_descr;
16067 decl_size -= bitsize;
16068 if (last == NULL)
16070 HOST_WIDE_INT offset = 0;
16071 if (GET_CODE (varloc) == VAR_LOCATION
16072 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
16074 varloc = PAT_VAR_LOCATION_LOC (varloc);
16075 if (GET_CODE (varloc) == EXPR_LIST)
16076 varloc = XEXP (varloc, 0);
16080 if (GET_CODE (varloc) == CONST
16081 || GET_CODE (varloc) == SIGN_EXTEND
16082 || GET_CODE (varloc) == ZERO_EXTEND)
16083 varloc = XEXP (varloc, 0);
16084 else if (GET_CODE (varloc) == SUBREG)
16085 varloc = SUBREG_REG (varloc);
16086 else
16087 break;
16089 while (1);
16090 /* DW_OP_bit_size offset should be zero for register
16091 or implicit location descriptions and empty location
16092 descriptions, but for memory addresses needs big endian
16093 adjustment. */
16094 if (MEM_P (varloc))
16096 unsigned HOST_WIDE_INT memsize
16097 = MEM_SIZE (varloc) * BITS_PER_UNIT;
16098 if (memsize != bitsize)
16100 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
16101 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
16102 goto discard_descr;
16103 if (memsize < bitsize)
16104 goto discard_descr;
16105 if (BITS_BIG_ENDIAN)
16106 offset = memsize - bitsize;
16110 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
16111 if (*descr_tail == NULL)
16112 goto discard_descr;
16113 descr_tail = &(*descr_tail)->dw_loc_next;
16117 /* If there were any non-empty expressions, add padding till the end of
16118 the decl. */
16119 if (descr != NULL && decl_size != 0)
16121 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
16122 if (*descr_tail == NULL)
16123 goto discard_descr;
16125 return descr;
16127 discard_descr:
16128 /* Discard the descriptor and release any addr_table entries it uses. */
16129 remove_loc_list_addr_table_entries (descr);
16130 return NULL;
16133 /* Return the dwarf representation of the location list LOC_LIST of
16134 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
16135 function. */
16137 static dw_loc_list_ref
16138 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
16140 const char *endname, *secname;
16141 rtx varloc;
16142 enum var_init_status initialized;
16143 struct var_loc_node *node;
16144 dw_loc_descr_ref descr;
16145 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
16146 dw_loc_list_ref list = NULL;
16147 dw_loc_list_ref *listp = &list;
16149 /* Now that we know what section we are using for a base,
16150 actually construct the list of locations.
16151 The first location information is what is passed to the
16152 function that creates the location list, and the remaining
16153 locations just get added on to that list.
16154 Note that we only know the start address for a location
16155 (IE location changes), so to build the range, we use
16156 the range [current location start, next location start].
16157 This means we have to special case the last node, and generate
16158 a range of [last location start, end of function label]. */
16160 secname = secname_for_decl (decl);
16162 for (node = loc_list->first; node; node = node->next)
16163 if (GET_CODE (node->loc) == EXPR_LIST
16164 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
16166 if (GET_CODE (node->loc) == EXPR_LIST)
16168 /* This requires DW_OP_{,bit_}piece, which is not usable
16169 inside DWARF expressions. */
16170 if (want_address != 2)
16171 continue;
16172 descr = dw_sra_loc_expr (decl, node->loc);
16173 if (descr == NULL)
16174 continue;
16176 else
16178 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
16179 varloc = NOTE_VAR_LOCATION (node->loc);
16180 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
16182 if (descr)
16184 bool range_across_switch = false;
16185 /* If section switch happens in between node->label
16186 and node->next->label (or end of function) and
16187 we can't emit it as a single entry list,
16188 emit two ranges, first one ending at the end
16189 of first partition and second one starting at the
16190 beginning of second partition. */
16191 if (node == loc_list->last_before_switch
16192 && (node != loc_list->first || loc_list->first->next)
16193 && current_function_decl)
16195 endname = cfun->fde->dw_fde_end;
16196 range_across_switch = true;
16198 /* The variable has a location between NODE->LABEL and
16199 NODE->NEXT->LABEL. */
16200 else if (node->next)
16201 endname = node->next->label;
16202 /* If the variable has a location at the last label
16203 it keeps its location until the end of function. */
16204 else if (!current_function_decl)
16205 endname = text_end_label;
16206 else
16208 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
16209 current_function_funcdef_no);
16210 endname = ggc_strdup (label_id);
16213 *listp = new_loc_list (descr, node->label, endname, secname);
16214 if (TREE_CODE (decl) == PARM_DECL
16215 && node == loc_list->first
16216 && NOTE_P (node->loc)
16217 && strcmp (node->label, endname) == 0)
16218 (*listp)->force = true;
16219 listp = &(*listp)->dw_loc_next;
16221 if (range_across_switch)
16223 if (GET_CODE (node->loc) == EXPR_LIST)
16224 descr = dw_sra_loc_expr (decl, node->loc);
16225 else
16227 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
16228 varloc = NOTE_VAR_LOCATION (node->loc);
16229 descr = dw_loc_list_1 (decl, varloc, want_address,
16230 initialized);
16232 gcc_assert (descr);
16233 /* The variable has a location between NODE->LABEL and
16234 NODE->NEXT->LABEL. */
16235 if (node->next)
16236 endname = node->next->label;
16237 else
16238 endname = cfun->fde->dw_fde_second_end;
16239 *listp = new_loc_list (descr,
16240 cfun->fde->dw_fde_second_begin,
16241 endname, secname);
16242 listp = &(*listp)->dw_loc_next;
16247 /* Try to avoid the overhead of a location list emitting a location
16248 expression instead, but only if we didn't have more than one
16249 location entry in the first place. If some entries were not
16250 representable, we don't want to pretend a single entry that was
16251 applies to the entire scope in which the variable is
16252 available. */
16253 if (list && loc_list->first->next)
16254 gen_llsym (list);
16256 return list;
16259 /* Return if the loc_list has only single element and thus can be represented
16260 as location description. */
16262 static bool
16263 single_element_loc_list_p (dw_loc_list_ref list)
16265 gcc_assert (!list->dw_loc_next || list->ll_symbol);
16266 return !list->ll_symbol;
16269 /* To each location in list LIST add loc descr REF. */
16271 static void
16272 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
16274 dw_loc_descr_ref copy;
16275 add_loc_descr (&list->expr, ref);
16276 list = list->dw_loc_next;
16277 while (list)
16279 copy = ggc_alloc<dw_loc_descr_node> ();
16280 memcpy (copy, ref, sizeof (dw_loc_descr_node));
16281 add_loc_descr (&list->expr, copy);
16282 while (copy->dw_loc_next)
16284 dw_loc_descr_ref new_copy = ggc_alloc<dw_loc_descr_node> ();
16285 memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
16286 copy->dw_loc_next = new_copy;
16287 copy = new_copy;
16289 list = list->dw_loc_next;
16293 /* Given two lists RET and LIST
16294 produce location list that is result of adding expression in LIST
16295 to expression in RET on each position in program.
16296 Might be destructive on both RET and LIST.
16298 TODO: We handle only simple cases of RET or LIST having at most one
16299 element. General case would inolve sorting the lists in program order
16300 and merging them that will need some additional work.
16301 Adding that will improve quality of debug info especially for SRA-ed
16302 structures. */
16304 static void
16305 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
16307 if (!list)
16308 return;
16309 if (!*ret)
16311 *ret = list;
16312 return;
16314 if (!list->dw_loc_next)
16316 add_loc_descr_to_each (*ret, list->expr);
16317 return;
16319 if (!(*ret)->dw_loc_next)
16321 add_loc_descr_to_each (list, (*ret)->expr);
16322 *ret = list;
16323 return;
16325 expansion_failed (NULL_TREE, NULL_RTX,
16326 "Don't know how to merge two non-trivial"
16327 " location lists.\n");
16328 *ret = NULL;
16329 return;
16332 /* LOC is constant expression. Try a luck, look it up in constant
16333 pool and return its loc_descr of its address. */
16335 static dw_loc_descr_ref
16336 cst_pool_loc_descr (tree loc)
16338 /* Get an RTL for this, if something has been emitted. */
16339 rtx rtl = lookup_constant_def (loc);
16341 if (!rtl || !MEM_P (rtl))
16343 gcc_assert (!rtl);
16344 return 0;
16346 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
16348 /* TODO: We might get more coverage if we was actually delaying expansion
16349 of all expressions till end of compilation when constant pools are fully
16350 populated. */
16351 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
16353 expansion_failed (loc, NULL_RTX,
16354 "CST value in contant pool but not marked.");
16355 return 0;
16357 return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
16358 GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
16361 /* Return dw_loc_list representing address of addr_expr LOC
16362 by looking for inner INDIRECT_REF expression and turning
16363 it into simple arithmetics.
16365 See loc_list_from_tree for the meaning of CONTEXT. */
16367 static dw_loc_list_ref
16368 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev,
16369 loc_descr_context *context)
16371 tree obj, offset;
16372 HOST_WIDE_INT bitsize, bitpos, bytepos;
16373 machine_mode mode;
16374 int unsignedp, reversep, volatilep = 0;
16375 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
16377 obj = get_inner_reference (TREE_OPERAND (loc, 0),
16378 &bitsize, &bitpos, &offset, &mode,
16379 &unsignedp, &reversep, &volatilep);
16380 STRIP_NOPS (obj);
16381 if (bitpos % BITS_PER_UNIT)
16383 expansion_failed (loc, NULL_RTX, "bitfield access");
16384 return 0;
16386 if (!INDIRECT_REF_P (obj))
16388 expansion_failed (obj,
16389 NULL_RTX, "no indirect ref in inner refrence");
16390 return 0;
16392 if (!offset && !bitpos)
16393 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1,
16394 context);
16395 else if (toplev
16396 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
16397 && (dwarf_version >= 4 || !dwarf_strict))
16399 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0, context);
16400 if (!list_ret)
16401 return 0;
16402 if (offset)
16404 /* Variable offset. */
16405 list_ret1 = loc_list_from_tree (offset, 0, context);
16406 if (list_ret1 == 0)
16407 return 0;
16408 add_loc_list (&list_ret, list_ret1);
16409 if (!list_ret)
16410 return 0;
16411 add_loc_descr_to_each (list_ret,
16412 new_loc_descr (DW_OP_plus, 0, 0));
16414 bytepos = bitpos / BITS_PER_UNIT;
16415 if (bytepos > 0)
16416 add_loc_descr_to_each (list_ret,
16417 new_loc_descr (DW_OP_plus_uconst,
16418 bytepos, 0));
16419 else if (bytepos < 0)
16420 loc_list_plus_const (list_ret, bytepos);
16421 add_loc_descr_to_each (list_ret,
16422 new_loc_descr (DW_OP_stack_value, 0, 0));
16424 return list_ret;
16427 /* Set LOC to the next operation that is not a DW_OP_nop operation. In the case
16428 all operations from LOC are nops, move to the last one. Insert in NOPS all
16429 operations that are skipped. */
16431 static void
16432 loc_descr_to_next_no_nop (dw_loc_descr_ref &loc,
16433 hash_set<dw_loc_descr_ref> &nops)
16435 while (loc->dw_loc_next != NULL && loc->dw_loc_opc == DW_OP_nop)
16437 nops.add (loc);
16438 loc = loc->dw_loc_next;
16442 /* Helper for loc_descr_without_nops: free the location description operation
16443 P. */
16445 bool
16446 free_loc_descr (const dw_loc_descr_ref &loc, void *data ATTRIBUTE_UNUSED)
16448 ggc_free (loc);
16449 return true;
16452 /* Remove all DW_OP_nop operations from LOC except, if it exists, the one that
16453 finishes LOC. */
16455 static void
16456 loc_descr_without_nops (dw_loc_descr_ref &loc)
16458 if (loc->dw_loc_opc == DW_OP_nop && loc->dw_loc_next == NULL)
16459 return;
16461 /* Set of all DW_OP_nop operations we remove. */
16462 hash_set<dw_loc_descr_ref> nops;
16464 /* First, strip all prefix NOP operations in order to keep the head of the
16465 operations list. */
16466 loc_descr_to_next_no_nop (loc, nops);
16468 for (dw_loc_descr_ref cur = loc; cur != NULL;)
16470 /* For control flow operations: strip "prefix" nops in destination
16471 labels. */
16472 if (cur->dw_loc_oprnd1.val_class == dw_val_class_loc)
16473 loc_descr_to_next_no_nop (cur->dw_loc_oprnd1.v.val_loc, nops);
16474 if (cur->dw_loc_oprnd2.val_class == dw_val_class_loc)
16475 loc_descr_to_next_no_nop (cur->dw_loc_oprnd2.v.val_loc, nops);
16477 /* Do the same for the operations that follow, then move to the next
16478 iteration. */
16479 if (cur->dw_loc_next != NULL)
16480 loc_descr_to_next_no_nop (cur->dw_loc_next, nops);
16481 cur = cur->dw_loc_next;
16484 nops.traverse<void *, free_loc_descr> (NULL);
16488 struct dwarf_procedure_info;
16490 /* Helper structure for location descriptions generation. */
16491 struct loc_descr_context
16493 /* The type that is implicitly referenced by DW_OP_push_object_address, or
16494 NULL_TREE if DW_OP_push_object_address in invalid for this location
16495 description. This is used when processing PLACEHOLDER_EXPR nodes. */
16496 tree context_type;
16497 /* The ..._DECL node that should be translated as a
16498 DW_OP_push_object_address operation. */
16499 tree base_decl;
16500 /* Information about the DWARF procedure we are currently generating. NULL if
16501 we are not generating a DWARF procedure. */
16502 struct dwarf_procedure_info *dpi;
16503 /* True if integral PLACEHOLDER_EXPR stands for the first argument passed
16504 by consumer. Used for DW_TAG_generic_subrange attributes. */
16505 bool placeholder_arg;
16506 /* True if PLACEHOLDER_EXPR has been seen. */
16507 bool placeholder_seen;
16510 /* DWARF procedures generation
16512 DWARF expressions (aka. location descriptions) are used to encode variable
16513 things such as sizes or offsets. Such computations can have redundant parts
16514 that can be factorized in order to reduce the size of the output debug
16515 information. This is the whole point of DWARF procedures.
16517 Thanks to stor-layout.c, size and offset expressions in GENERIC trees are
16518 already factorized into functions ("size functions") in order to handle very
16519 big and complex types. Such functions are quite simple: they have integral
16520 arguments, they return an integral result and their body contains only a
16521 return statement with arithmetic expressions. This is the only kind of
16522 function we are interested in translating into DWARF procedures, here.
16524 DWARF expressions and DWARF procedure are executed using a stack, so we have
16525 to define some calling convention for them to interact. Let's say that:
16527 - Before calling a DWARF procedure, DWARF expressions must push on the stack
16528 all arguments in reverse order (right-to-left) so that when the DWARF
16529 procedure execution starts, the first argument is the top of the stack.
16531 - Then, when returning, the DWARF procedure must have consumed all arguments
16532 on the stack, must have pushed the result and touched nothing else.
16534 - Each integral argument and the result are integral types can be hold in a
16535 single stack slot.
16537 - We call "frame offset" the number of stack slots that are "under DWARF
16538 procedure control": it includes the arguments slots, the temporaries and
16539 the result slot. Thus, it is equal to the number of arguments when the
16540 procedure execution starts and must be equal to one (the result) when it
16541 returns. */
16543 /* Helper structure used when generating operations for a DWARF procedure. */
16544 struct dwarf_procedure_info
16546 /* The FUNCTION_DECL node corresponding to the DWARF procedure that is
16547 currently translated. */
16548 tree fndecl;
16549 /* The number of arguments FNDECL takes. */
16550 unsigned args_count;
16553 /* Return a pointer to a newly created DIE node for a DWARF procedure. Add
16554 LOCATION as its DW_AT_location attribute. If FNDECL is not NULL_TREE,
16555 equate it to this DIE. */
16557 static dw_die_ref
16558 new_dwarf_proc_die (dw_loc_descr_ref location, tree fndecl,
16559 dw_die_ref parent_die)
16561 dw_die_ref dwarf_proc_die;
16563 if ((dwarf_version < 3 && dwarf_strict)
16564 || location == NULL)
16565 return NULL;
16567 dwarf_proc_die = new_die (DW_TAG_dwarf_procedure, parent_die, fndecl);
16568 if (fndecl)
16569 equate_decl_number_to_die (fndecl, dwarf_proc_die);
16570 add_AT_loc (dwarf_proc_die, DW_AT_location, location);
16571 return dwarf_proc_die;
16574 /* Return whether TYPE is a supported type as a DWARF procedure argument
16575 type or return type (we handle only scalar types and pointer types that
16576 aren't wider than the DWARF expression evaluation stack. */
16578 static bool
16579 is_handled_procedure_type (tree type)
16581 return ((INTEGRAL_TYPE_P (type)
16582 || TREE_CODE (type) == OFFSET_TYPE
16583 || TREE_CODE (type) == POINTER_TYPE)
16584 && int_size_in_bytes (type) <= DWARF2_ADDR_SIZE);
16587 /* Helper for resolve_args_picking: do the same but stop when coming across
16588 visited nodes. For each node we visit, register in FRAME_OFFSETS the frame
16589 offset *before* evaluating the corresponding operation. */
16591 static bool
16592 resolve_args_picking_1 (dw_loc_descr_ref loc, unsigned initial_frame_offset,
16593 struct dwarf_procedure_info *dpi,
16594 hash_map<dw_loc_descr_ref, unsigned> &frame_offsets)
16596 /* The "frame_offset" identifier is already used to name a macro... */
16597 unsigned frame_offset_ = initial_frame_offset;
16598 dw_loc_descr_ref l;
16600 for (l = loc; l != NULL;)
16602 bool existed;
16603 unsigned &l_frame_offset = frame_offsets.get_or_insert (l, &existed);
16605 /* If we already met this node, there is nothing to compute anymore. */
16606 if (existed)
16608 /* Make sure that the stack size is consistent wherever the execution
16609 flow comes from. */
16610 gcc_assert ((unsigned) l_frame_offset == frame_offset_);
16611 break;
16613 l_frame_offset = frame_offset_;
16615 /* If needed, relocate the picking offset with respect to the frame
16616 offset. */
16617 if (l->frame_offset_rel)
16619 unsigned HOST_WIDE_INT off;
16620 switch (l->dw_loc_opc)
16622 case DW_OP_pick:
16623 off = l->dw_loc_oprnd1.v.val_unsigned;
16624 break;
16625 case DW_OP_dup:
16626 off = 0;
16627 break;
16628 case DW_OP_over:
16629 off = 1;
16630 break;
16631 default:
16632 gcc_unreachable ();
16634 /* frame_offset_ is the size of the current stack frame, including
16635 incoming arguments. Besides, the arguments are pushed
16636 right-to-left. Thus, in order to access the Nth argument from
16637 this operation node, the picking has to skip temporaries *plus*
16638 one stack slot per argument (0 for the first one, 1 for the second
16639 one, etc.).
16641 The targetted argument number (N) is already set as the operand,
16642 and the number of temporaries can be computed with:
16643 frame_offsets_ - dpi->args_count */
16644 off += frame_offset_ - dpi->args_count;
16646 /* DW_OP_pick handles only offsets from 0 to 255 (inclusive)... */
16647 if (off > 255)
16648 return false;
16650 if (off == 0)
16652 l->dw_loc_opc = DW_OP_dup;
16653 l->dw_loc_oprnd1.v.val_unsigned = 0;
16655 else if (off == 1)
16657 l->dw_loc_opc = DW_OP_over;
16658 l->dw_loc_oprnd1.v.val_unsigned = 0;
16660 else
16662 l->dw_loc_opc = DW_OP_pick;
16663 l->dw_loc_oprnd1.v.val_unsigned = off;
16667 /* Update frame_offset according to the effect the current operation has
16668 on the stack. */
16669 switch (l->dw_loc_opc)
16671 case DW_OP_deref:
16672 case DW_OP_swap:
16673 case DW_OP_rot:
16674 case DW_OP_abs:
16675 case DW_OP_neg:
16676 case DW_OP_not:
16677 case DW_OP_plus_uconst:
16678 case DW_OP_skip:
16679 case DW_OP_reg0:
16680 case DW_OP_reg1:
16681 case DW_OP_reg2:
16682 case DW_OP_reg3:
16683 case DW_OP_reg4:
16684 case DW_OP_reg5:
16685 case DW_OP_reg6:
16686 case DW_OP_reg7:
16687 case DW_OP_reg8:
16688 case DW_OP_reg9:
16689 case DW_OP_reg10:
16690 case DW_OP_reg11:
16691 case DW_OP_reg12:
16692 case DW_OP_reg13:
16693 case DW_OP_reg14:
16694 case DW_OP_reg15:
16695 case DW_OP_reg16:
16696 case DW_OP_reg17:
16697 case DW_OP_reg18:
16698 case DW_OP_reg19:
16699 case DW_OP_reg20:
16700 case DW_OP_reg21:
16701 case DW_OP_reg22:
16702 case DW_OP_reg23:
16703 case DW_OP_reg24:
16704 case DW_OP_reg25:
16705 case DW_OP_reg26:
16706 case DW_OP_reg27:
16707 case DW_OP_reg28:
16708 case DW_OP_reg29:
16709 case DW_OP_reg30:
16710 case DW_OP_reg31:
16711 case DW_OP_bregx:
16712 case DW_OP_piece:
16713 case DW_OP_deref_size:
16714 case DW_OP_nop:
16715 case DW_OP_bit_piece:
16716 case DW_OP_implicit_value:
16717 case DW_OP_stack_value:
16718 break;
16720 case DW_OP_addr:
16721 case DW_OP_const1u:
16722 case DW_OP_const1s:
16723 case DW_OP_const2u:
16724 case DW_OP_const2s:
16725 case DW_OP_const4u:
16726 case DW_OP_const4s:
16727 case DW_OP_const8u:
16728 case DW_OP_const8s:
16729 case DW_OP_constu:
16730 case DW_OP_consts:
16731 case DW_OP_dup:
16732 case DW_OP_over:
16733 case DW_OP_pick:
16734 case DW_OP_lit0:
16735 case DW_OP_lit1:
16736 case DW_OP_lit2:
16737 case DW_OP_lit3:
16738 case DW_OP_lit4:
16739 case DW_OP_lit5:
16740 case DW_OP_lit6:
16741 case DW_OP_lit7:
16742 case DW_OP_lit8:
16743 case DW_OP_lit9:
16744 case DW_OP_lit10:
16745 case DW_OP_lit11:
16746 case DW_OP_lit12:
16747 case DW_OP_lit13:
16748 case DW_OP_lit14:
16749 case DW_OP_lit15:
16750 case DW_OP_lit16:
16751 case DW_OP_lit17:
16752 case DW_OP_lit18:
16753 case DW_OP_lit19:
16754 case DW_OP_lit20:
16755 case DW_OP_lit21:
16756 case DW_OP_lit22:
16757 case DW_OP_lit23:
16758 case DW_OP_lit24:
16759 case DW_OP_lit25:
16760 case DW_OP_lit26:
16761 case DW_OP_lit27:
16762 case DW_OP_lit28:
16763 case DW_OP_lit29:
16764 case DW_OP_lit30:
16765 case DW_OP_lit31:
16766 case DW_OP_breg0:
16767 case DW_OP_breg1:
16768 case DW_OP_breg2:
16769 case DW_OP_breg3:
16770 case DW_OP_breg4:
16771 case DW_OP_breg5:
16772 case DW_OP_breg6:
16773 case DW_OP_breg7:
16774 case DW_OP_breg8:
16775 case DW_OP_breg9:
16776 case DW_OP_breg10:
16777 case DW_OP_breg11:
16778 case DW_OP_breg12:
16779 case DW_OP_breg13:
16780 case DW_OP_breg14:
16781 case DW_OP_breg15:
16782 case DW_OP_breg16:
16783 case DW_OP_breg17:
16784 case DW_OP_breg18:
16785 case DW_OP_breg19:
16786 case DW_OP_breg20:
16787 case DW_OP_breg21:
16788 case DW_OP_breg22:
16789 case DW_OP_breg23:
16790 case DW_OP_breg24:
16791 case DW_OP_breg25:
16792 case DW_OP_breg26:
16793 case DW_OP_breg27:
16794 case DW_OP_breg28:
16795 case DW_OP_breg29:
16796 case DW_OP_breg30:
16797 case DW_OP_breg31:
16798 case DW_OP_fbreg:
16799 case DW_OP_push_object_address:
16800 case DW_OP_call_frame_cfa:
16801 ++frame_offset_;
16802 break;
16804 case DW_OP_drop:
16805 case DW_OP_xderef:
16806 case DW_OP_and:
16807 case DW_OP_div:
16808 case DW_OP_minus:
16809 case DW_OP_mod:
16810 case DW_OP_mul:
16811 case DW_OP_or:
16812 case DW_OP_plus:
16813 case DW_OP_shl:
16814 case DW_OP_shr:
16815 case DW_OP_shra:
16816 case DW_OP_xor:
16817 case DW_OP_bra:
16818 case DW_OP_eq:
16819 case DW_OP_ge:
16820 case DW_OP_gt:
16821 case DW_OP_le:
16822 case DW_OP_lt:
16823 case DW_OP_ne:
16824 case DW_OP_regx:
16825 case DW_OP_xderef_size:
16826 --frame_offset_;
16827 break;
16829 case DW_OP_call2:
16830 case DW_OP_call4:
16831 case DW_OP_call_ref:
16833 dw_die_ref dwarf_proc = l->dw_loc_oprnd1.v.val_die_ref.die;
16834 int *stack_usage = dwarf_proc_stack_usage_map->get (dwarf_proc);
16836 if (stack_usage == NULL)
16837 return false;
16838 frame_offset_ += *stack_usage;
16839 break;
16842 case DW_OP_implicit_pointer:
16843 case DW_OP_entry_value:
16844 case DW_OP_const_type:
16845 case DW_OP_regval_type:
16846 case DW_OP_deref_type:
16847 case DW_OP_convert:
16848 case DW_OP_reinterpret:
16849 case DW_OP_form_tls_address:
16850 case DW_OP_GNU_push_tls_address:
16851 case DW_OP_GNU_uninit:
16852 case DW_OP_GNU_encoded_addr:
16853 case DW_OP_GNU_implicit_pointer:
16854 case DW_OP_GNU_entry_value:
16855 case DW_OP_GNU_const_type:
16856 case DW_OP_GNU_regval_type:
16857 case DW_OP_GNU_deref_type:
16858 case DW_OP_GNU_convert:
16859 case DW_OP_GNU_reinterpret:
16860 case DW_OP_GNU_parameter_ref:
16861 /* loc_list_from_tree will probably not output these operations for
16862 size functions, so assume they will not appear here. */
16863 /* Fall through... */
16865 default:
16866 gcc_unreachable ();
16869 /* Now, follow the control flow (except subroutine calls). */
16870 switch (l->dw_loc_opc)
16872 case DW_OP_bra:
16873 if (!resolve_args_picking_1 (l->dw_loc_next, frame_offset_, dpi,
16874 frame_offsets))
16875 return false;
16876 /* Fall through. */
16878 case DW_OP_skip:
16879 l = l->dw_loc_oprnd1.v.val_loc;
16880 break;
16882 case DW_OP_stack_value:
16883 return true;
16885 default:
16886 l = l->dw_loc_next;
16887 break;
16891 return true;
16894 /* Make a DFS over operations reachable through LOC (i.e. follow branch
16895 operations) in order to resolve the operand of DW_OP_pick operations that
16896 target DWARF procedure arguments (DPI). INITIAL_FRAME_OFFSET is the frame
16897 offset *before* LOC is executed. Return if all relocations were
16898 successful. */
16900 static bool
16901 resolve_args_picking (dw_loc_descr_ref loc, unsigned initial_frame_offset,
16902 struct dwarf_procedure_info *dpi)
16904 /* Associate to all visited operations the frame offset *before* evaluating
16905 this operation. */
16906 hash_map<dw_loc_descr_ref, unsigned> frame_offsets;
16908 return resolve_args_picking_1 (loc, initial_frame_offset, dpi,
16909 frame_offsets);
16912 /* Try to generate a DWARF procedure that computes the same result as FNDECL.
16913 Return NULL if it is not possible. */
16915 static dw_die_ref
16916 function_to_dwarf_procedure (tree fndecl)
16918 struct loc_descr_context ctx;
16919 struct dwarf_procedure_info dpi;
16920 dw_die_ref dwarf_proc_die;
16921 tree tree_body = DECL_SAVED_TREE (fndecl);
16922 dw_loc_descr_ref loc_body, epilogue;
16924 tree cursor;
16925 unsigned i;
16927 /* Do not generate multiple DWARF procedures for the same function
16928 declaration. */
16929 dwarf_proc_die = lookup_decl_die (fndecl);
16930 if (dwarf_proc_die != NULL)
16931 return dwarf_proc_die;
16933 /* DWARF procedures are available starting with the DWARFv3 standard. */
16934 if (dwarf_version < 3 && dwarf_strict)
16935 return NULL;
16937 /* We handle only functions for which we still have a body, that return a
16938 supported type and that takes arguments with supported types. Note that
16939 there is no point translating functions that return nothing. */
16940 if (tree_body == NULL_TREE
16941 || DECL_RESULT (fndecl) == NULL_TREE
16942 || !is_handled_procedure_type (TREE_TYPE (DECL_RESULT (fndecl))))
16943 return NULL;
16945 for (cursor = DECL_ARGUMENTS (fndecl);
16946 cursor != NULL_TREE;
16947 cursor = TREE_CHAIN (cursor))
16948 if (!is_handled_procedure_type (TREE_TYPE (cursor)))
16949 return NULL;
16951 /* Match only "expr" in: RETURN_EXPR (MODIFY_EXPR (RESULT_DECL, expr)). */
16952 if (TREE_CODE (tree_body) != RETURN_EXPR)
16953 return NULL;
16954 tree_body = TREE_OPERAND (tree_body, 0);
16955 if (TREE_CODE (tree_body) != MODIFY_EXPR
16956 || TREE_OPERAND (tree_body, 0) != DECL_RESULT (fndecl))
16957 return NULL;
16958 tree_body = TREE_OPERAND (tree_body, 1);
16960 /* Try to translate the body expression itself. Note that this will probably
16961 cause an infinite recursion if its call graph has a cycle. This is very
16962 unlikely for size functions, however, so don't bother with such things at
16963 the moment. */
16964 ctx.context_type = NULL_TREE;
16965 ctx.base_decl = NULL_TREE;
16966 ctx.dpi = &dpi;
16967 ctx.placeholder_arg = false;
16968 ctx.placeholder_seen = false;
16969 dpi.fndecl = fndecl;
16970 dpi.args_count = list_length (DECL_ARGUMENTS (fndecl));
16971 loc_body = loc_descriptor_from_tree (tree_body, 0, &ctx);
16972 if (!loc_body)
16973 return NULL;
16975 /* After evaluating all operands in "loc_body", we should still have on the
16976 stack all arguments plus the desired function result (top of the stack).
16977 Generate code in order to keep only the result in our stack frame. */
16978 epilogue = NULL;
16979 for (i = 0; i < dpi.args_count; ++i)
16981 dw_loc_descr_ref op_couple = new_loc_descr (DW_OP_swap, 0, 0);
16982 op_couple->dw_loc_next = new_loc_descr (DW_OP_drop, 0, 0);
16983 op_couple->dw_loc_next->dw_loc_next = epilogue;
16984 epilogue = op_couple;
16986 add_loc_descr (&loc_body, epilogue);
16987 if (!resolve_args_picking (loc_body, dpi.args_count, &dpi))
16988 return NULL;
16990 /* Trailing nops from loc_descriptor_from_tree (if any) cannot be removed
16991 because they are considered useful. Now there is an epilogue, they are
16992 not anymore, so give it another try. */
16993 loc_descr_without_nops (loc_body);
16995 /* fndecl may be used both as a regular DW_TAG_subprogram DIE and as
16996 a DW_TAG_dwarf_procedure, so we may have a conflict, here. It's unlikely,
16997 though, given that size functions do not come from source, so they should
16998 not have a dedicated DW_TAG_subprogram DIE. */
16999 dwarf_proc_die
17000 = new_dwarf_proc_die (loc_body, fndecl,
17001 get_context_die (DECL_CONTEXT (fndecl)));
17003 /* The called DWARF procedure consumes one stack slot per argument and
17004 returns one stack slot. */
17005 dwarf_proc_stack_usage_map->put (dwarf_proc_die, 1 - dpi.args_count);
17007 return dwarf_proc_die;
17011 /* Generate Dwarf location list representing LOC.
17012 If WANT_ADDRESS is false, expression computing LOC will be computed
17013 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
17014 if WANT_ADDRESS is 2, expression computing address useable in location
17015 will be returned (i.e. DW_OP_reg can be used
17016 to refer to register values).
17018 CONTEXT provides information to customize the location descriptions
17019 generation. Its context_type field specifies what type is implicitly
17020 referenced by DW_OP_push_object_address. If it is NULL_TREE, this operation
17021 will not be generated.
17023 Its DPI field determines whether we are generating a DWARF expression for a
17024 DWARF procedure, so PARM_DECL references are processed specifically.
17026 If CONTEXT is NULL, the behavior is the same as if context_type, base_decl
17027 and dpi fields were null. */
17029 static dw_loc_list_ref
17030 loc_list_from_tree_1 (tree loc, int want_address,
17031 struct loc_descr_context *context)
17033 dw_loc_descr_ref ret = NULL, ret1 = NULL;
17034 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
17035 int have_address = 0;
17036 enum dwarf_location_atom op;
17038 /* ??? Most of the time we do not take proper care for sign/zero
17039 extending the values properly. Hopefully this won't be a real
17040 problem... */
17042 if (context != NULL
17043 && context->base_decl == loc
17044 && want_address == 0)
17046 if (dwarf_version >= 3 || !dwarf_strict)
17047 return new_loc_list (new_loc_descr (DW_OP_push_object_address, 0, 0),
17048 NULL, NULL, NULL);
17049 else
17050 return NULL;
17053 switch (TREE_CODE (loc))
17055 case ERROR_MARK:
17056 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
17057 return 0;
17059 case PLACEHOLDER_EXPR:
17060 /* This case involves extracting fields from an object to determine the
17061 position of other fields. It is supposed to appear only as the first
17062 operand of COMPONENT_REF nodes and to reference precisely the type
17063 that the context allows. */
17064 if (context != NULL
17065 && TREE_TYPE (loc) == context->context_type
17066 && want_address >= 1)
17068 if (dwarf_version >= 3 || !dwarf_strict)
17070 ret = new_loc_descr (DW_OP_push_object_address, 0, 0);
17071 have_address = 1;
17072 break;
17074 else
17075 return NULL;
17077 /* For DW_TAG_generic_subrange attributes, PLACEHOLDER_EXPR stands for
17078 the single argument passed by consumer. */
17079 else if (context != NULL
17080 && context->placeholder_arg
17081 && INTEGRAL_TYPE_P (TREE_TYPE (loc))
17082 && want_address == 0)
17084 ret = new_loc_descr (DW_OP_pick, 0, 0);
17085 ret->frame_offset_rel = 1;
17086 context->placeholder_seen = true;
17087 break;
17089 else
17090 expansion_failed (loc, NULL_RTX,
17091 "PLACEHOLDER_EXPR for an unexpected type");
17092 break;
17094 case CALL_EXPR:
17096 const int nargs = call_expr_nargs (loc);
17097 tree callee = get_callee_fndecl (loc);
17098 int i;
17099 dw_die_ref dwarf_proc;
17101 if (callee == NULL_TREE)
17102 goto call_expansion_failed;
17104 /* We handle only functions that return an integer. */
17105 if (!is_handled_procedure_type (TREE_TYPE (TREE_TYPE (callee))))
17106 goto call_expansion_failed;
17108 dwarf_proc = function_to_dwarf_procedure (callee);
17109 if (dwarf_proc == NULL)
17110 goto call_expansion_failed;
17112 /* Evaluate arguments right-to-left so that the first argument will
17113 be the top-most one on the stack. */
17114 for (i = nargs - 1; i >= 0; --i)
17116 dw_loc_descr_ref loc_descr
17117 = loc_descriptor_from_tree (CALL_EXPR_ARG (loc, i), 0,
17118 context);
17120 if (loc_descr == NULL)
17121 goto call_expansion_failed;
17123 add_loc_descr (&ret, loc_descr);
17126 ret1 = new_loc_descr (DW_OP_call4, 0, 0);
17127 ret1->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
17128 ret1->dw_loc_oprnd1.v.val_die_ref.die = dwarf_proc;
17129 ret1->dw_loc_oprnd1.v.val_die_ref.external = 0;
17130 add_loc_descr (&ret, ret1);
17131 break;
17133 call_expansion_failed:
17134 expansion_failed (loc, NULL_RTX, "CALL_EXPR");
17135 /* There are no opcodes for these operations. */
17136 return 0;
17139 case PREINCREMENT_EXPR:
17140 case PREDECREMENT_EXPR:
17141 case POSTINCREMENT_EXPR:
17142 case POSTDECREMENT_EXPR:
17143 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
17144 /* There are no opcodes for these operations. */
17145 return 0;
17147 case ADDR_EXPR:
17148 /* If we already want an address, see if there is INDIRECT_REF inside
17149 e.g. for &this->field. */
17150 if (want_address)
17152 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
17153 (loc, want_address == 2, context);
17154 if (list_ret)
17155 have_address = 1;
17156 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
17157 && (ret = cst_pool_loc_descr (loc)))
17158 have_address = 1;
17160 /* Otherwise, process the argument and look for the address. */
17161 if (!list_ret && !ret)
17162 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 1, context);
17163 else
17165 if (want_address)
17166 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
17167 return NULL;
17169 break;
17171 case VAR_DECL:
17172 if (DECL_THREAD_LOCAL_P (loc))
17174 rtx rtl;
17175 enum dwarf_location_atom tls_op;
17176 enum dtprel_bool dtprel = dtprel_false;
17178 if (targetm.have_tls)
17180 /* If this is not defined, we have no way to emit the
17181 data. */
17182 if (!targetm.asm_out.output_dwarf_dtprel)
17183 return 0;
17185 /* The way DW_OP_GNU_push_tls_address is specified, we
17186 can only look up addresses of objects in the current
17187 module. We used DW_OP_addr as first op, but that's
17188 wrong, because DW_OP_addr is relocated by the debug
17189 info consumer, while DW_OP_GNU_push_tls_address
17190 operand shouldn't be. */
17191 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
17192 return 0;
17193 dtprel = dtprel_true;
17194 /* We check for DWARF 5 here because gdb did not implement
17195 DW_OP_form_tls_address until after 7.12. */
17196 tls_op = (dwarf_version >= 5 ? DW_OP_form_tls_address
17197 : DW_OP_GNU_push_tls_address);
17199 else
17201 if (!targetm.emutls.debug_form_tls_address
17202 || !(dwarf_version >= 3 || !dwarf_strict))
17203 return 0;
17204 /* We stuffed the control variable into the DECL_VALUE_EXPR
17205 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
17206 no longer appear in gimple code. We used the control
17207 variable in specific so that we could pick it up here. */
17208 loc = DECL_VALUE_EXPR (loc);
17209 tls_op = DW_OP_form_tls_address;
17212 rtl = rtl_for_decl_location (loc);
17213 if (rtl == NULL_RTX)
17214 return 0;
17216 if (!MEM_P (rtl))
17217 return 0;
17218 rtl = XEXP (rtl, 0);
17219 if (! CONSTANT_P (rtl))
17220 return 0;
17222 ret = new_addr_loc_descr (rtl, dtprel);
17223 ret1 = new_loc_descr (tls_op, 0, 0);
17224 add_loc_descr (&ret, ret1);
17226 have_address = 1;
17227 break;
17229 /* FALLTHRU */
17231 case PARM_DECL:
17232 if (context != NULL && context->dpi != NULL
17233 && DECL_CONTEXT (loc) == context->dpi->fndecl)
17235 /* We are generating code for a DWARF procedure and we want to access
17236 one of its arguments: find the appropriate argument offset and let
17237 the resolve_args_picking pass compute the offset that complies
17238 with the stack frame size. */
17239 unsigned i = 0;
17240 tree cursor;
17242 for (cursor = DECL_ARGUMENTS (context->dpi->fndecl);
17243 cursor != NULL_TREE && cursor != loc;
17244 cursor = TREE_CHAIN (cursor), ++i)
17246 /* If we are translating a DWARF procedure, all referenced parameters
17247 must belong to the current function. */
17248 gcc_assert (cursor != NULL_TREE);
17250 ret = new_loc_descr (DW_OP_pick, i, 0);
17251 ret->frame_offset_rel = 1;
17252 break;
17254 /* FALLTHRU */
17256 case RESULT_DECL:
17257 if (DECL_HAS_VALUE_EXPR_P (loc))
17258 return loc_list_from_tree_1 (DECL_VALUE_EXPR (loc),
17259 want_address, context);
17260 /* FALLTHRU */
17262 case FUNCTION_DECL:
17264 rtx rtl;
17265 var_loc_list *loc_list = lookup_decl_loc (loc);
17267 if (loc_list && loc_list->first)
17269 list_ret = dw_loc_list (loc_list, loc, want_address);
17270 have_address = want_address != 0;
17271 break;
17273 rtl = rtl_for_decl_location (loc);
17274 if (rtl == NULL_RTX)
17276 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
17277 return 0;
17279 else if (CONST_INT_P (rtl))
17281 HOST_WIDE_INT val = INTVAL (rtl);
17282 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
17283 val &= GET_MODE_MASK (DECL_MODE (loc));
17284 ret = int_loc_descriptor (val);
17286 else if (GET_CODE (rtl) == CONST_STRING)
17288 expansion_failed (loc, NULL_RTX, "CONST_STRING");
17289 return 0;
17291 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
17292 ret = new_addr_loc_descr (rtl, dtprel_false);
17293 else
17295 machine_mode mode, mem_mode;
17297 /* Certain constructs can only be represented at top-level. */
17298 if (want_address == 2)
17300 ret = loc_descriptor (rtl, VOIDmode,
17301 VAR_INIT_STATUS_INITIALIZED);
17302 have_address = 1;
17304 else
17306 mode = GET_MODE (rtl);
17307 mem_mode = VOIDmode;
17308 if (MEM_P (rtl))
17310 mem_mode = mode;
17311 mode = get_address_mode (rtl);
17312 rtl = XEXP (rtl, 0);
17313 have_address = 1;
17315 ret = mem_loc_descriptor (rtl, mode, mem_mode,
17316 VAR_INIT_STATUS_INITIALIZED);
17318 if (!ret)
17319 expansion_failed (loc, rtl,
17320 "failed to produce loc descriptor for rtl");
17323 break;
17325 case MEM_REF:
17326 if (!integer_zerop (TREE_OPERAND (loc, 1)))
17328 have_address = 1;
17329 goto do_plus;
17331 /* Fallthru. */
17332 case INDIRECT_REF:
17333 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17334 have_address = 1;
17335 break;
17337 case TARGET_MEM_REF:
17338 case SSA_NAME:
17339 case DEBUG_EXPR_DECL:
17340 return NULL;
17342 case COMPOUND_EXPR:
17343 return loc_list_from_tree_1 (TREE_OPERAND (loc, 1), want_address,
17344 context);
17346 CASE_CONVERT:
17347 case VIEW_CONVERT_EXPR:
17348 case SAVE_EXPR:
17349 case MODIFY_EXPR:
17350 case NON_LVALUE_EXPR:
17351 return loc_list_from_tree_1 (TREE_OPERAND (loc, 0), want_address,
17352 context);
17354 case COMPONENT_REF:
17355 case BIT_FIELD_REF:
17356 case ARRAY_REF:
17357 case ARRAY_RANGE_REF:
17358 case REALPART_EXPR:
17359 case IMAGPART_EXPR:
17361 tree obj, offset;
17362 HOST_WIDE_INT bitsize, bitpos, bytepos;
17363 machine_mode mode;
17364 int unsignedp, reversep, volatilep = 0;
17366 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
17367 &unsignedp, &reversep, &volatilep);
17369 gcc_assert (obj != loc);
17371 list_ret = loc_list_from_tree_1 (obj,
17372 want_address == 2
17373 && !bitpos && !offset ? 2 : 1,
17374 context);
17375 /* TODO: We can extract value of the small expression via shifting even
17376 for nonzero bitpos. */
17377 if (list_ret == 0)
17378 return 0;
17379 if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
17381 expansion_failed (loc, NULL_RTX,
17382 "bitfield access");
17383 return 0;
17386 if (offset != NULL_TREE)
17388 /* Variable offset. */
17389 list_ret1 = loc_list_from_tree_1 (offset, 0, context);
17390 if (list_ret1 == 0)
17391 return 0;
17392 add_loc_list (&list_ret, list_ret1);
17393 if (!list_ret)
17394 return 0;
17395 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
17398 bytepos = bitpos / BITS_PER_UNIT;
17399 if (bytepos > 0)
17400 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
17401 else if (bytepos < 0)
17402 loc_list_plus_const (list_ret, bytepos);
17404 have_address = 1;
17405 break;
17408 case INTEGER_CST:
17409 if ((want_address || !tree_fits_shwi_p (loc))
17410 && (ret = cst_pool_loc_descr (loc)))
17411 have_address = 1;
17412 else if (want_address == 2
17413 && tree_fits_shwi_p (loc)
17414 && (ret = address_of_int_loc_descriptor
17415 (int_size_in_bytes (TREE_TYPE (loc)),
17416 tree_to_shwi (loc))))
17417 have_address = 1;
17418 else if (tree_fits_shwi_p (loc))
17419 ret = int_loc_descriptor (tree_to_shwi (loc));
17420 else if (tree_fits_uhwi_p (loc))
17421 ret = uint_loc_descriptor (tree_to_uhwi (loc));
17422 else
17424 expansion_failed (loc, NULL_RTX,
17425 "Integer operand is not host integer");
17426 return 0;
17428 break;
17430 case CONSTRUCTOR:
17431 case REAL_CST:
17432 case STRING_CST:
17433 case COMPLEX_CST:
17434 if ((ret = cst_pool_loc_descr (loc)))
17435 have_address = 1;
17436 else if (TREE_CODE (loc) == CONSTRUCTOR)
17438 tree type = TREE_TYPE (loc);
17439 unsigned HOST_WIDE_INT size = int_size_in_bytes (type);
17440 unsigned HOST_WIDE_INT offset = 0;
17441 unsigned HOST_WIDE_INT cnt;
17442 constructor_elt *ce;
17444 if (TREE_CODE (type) == RECORD_TYPE)
17446 /* This is very limited, but it's enough to output
17447 pointers to member functions, as long as the
17448 referenced function is defined in the current
17449 translation unit. */
17450 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (loc), cnt, ce)
17452 tree val = ce->value;
17454 tree field = ce->index;
17456 if (val)
17457 STRIP_NOPS (val);
17459 if (!field || DECL_BIT_FIELD (field))
17461 expansion_failed (loc, NULL_RTX,
17462 "bitfield in record type constructor");
17463 size = offset = (unsigned HOST_WIDE_INT)-1;
17464 ret = NULL;
17465 break;
17468 HOST_WIDE_INT fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
17469 unsigned HOST_WIDE_INT pos = int_byte_position (field);
17470 gcc_assert (pos + fieldsize <= size);
17471 if (pos < offset)
17473 expansion_failed (loc, NULL_RTX,
17474 "out-of-order fields in record constructor");
17475 size = offset = (unsigned HOST_WIDE_INT)-1;
17476 ret = NULL;
17477 break;
17479 if (pos > offset)
17481 ret1 = new_loc_descr (DW_OP_piece, pos - offset, 0);
17482 add_loc_descr (&ret, ret1);
17483 offset = pos;
17485 if (val && fieldsize != 0)
17487 ret1 = loc_descriptor_from_tree (val, want_address, context);
17488 if (!ret1)
17490 expansion_failed (loc, NULL_RTX,
17491 "unsupported expression in field");
17492 size = offset = (unsigned HOST_WIDE_INT)-1;
17493 ret = NULL;
17494 break;
17496 add_loc_descr (&ret, ret1);
17498 if (fieldsize)
17500 ret1 = new_loc_descr (DW_OP_piece, fieldsize, 0);
17501 add_loc_descr (&ret, ret1);
17502 offset = pos + fieldsize;
17506 if (offset != size)
17508 ret1 = new_loc_descr (DW_OP_piece, size - offset, 0);
17509 add_loc_descr (&ret, ret1);
17510 offset = size;
17513 have_address = !!want_address;
17515 else
17516 expansion_failed (loc, NULL_RTX,
17517 "constructor of non-record type");
17519 else
17520 /* We can construct small constants here using int_loc_descriptor. */
17521 expansion_failed (loc, NULL_RTX,
17522 "constructor or constant not in constant pool");
17523 break;
17525 case TRUTH_AND_EXPR:
17526 case TRUTH_ANDIF_EXPR:
17527 case BIT_AND_EXPR:
17528 op = DW_OP_and;
17529 goto do_binop;
17531 case TRUTH_XOR_EXPR:
17532 case BIT_XOR_EXPR:
17533 op = DW_OP_xor;
17534 goto do_binop;
17536 case TRUTH_OR_EXPR:
17537 case TRUTH_ORIF_EXPR:
17538 case BIT_IOR_EXPR:
17539 op = DW_OP_or;
17540 goto do_binop;
17542 case FLOOR_DIV_EXPR:
17543 case CEIL_DIV_EXPR:
17544 case ROUND_DIV_EXPR:
17545 case TRUNC_DIV_EXPR:
17546 case EXACT_DIV_EXPR:
17547 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
17548 return 0;
17549 op = DW_OP_div;
17550 goto do_binop;
17552 case MINUS_EXPR:
17553 op = DW_OP_minus;
17554 goto do_binop;
17556 case FLOOR_MOD_EXPR:
17557 case CEIL_MOD_EXPR:
17558 case ROUND_MOD_EXPR:
17559 case TRUNC_MOD_EXPR:
17560 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
17562 op = DW_OP_mod;
17563 goto do_binop;
17565 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17566 list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
17567 if (list_ret == 0 || list_ret1 == 0)
17568 return 0;
17570 add_loc_list (&list_ret, list_ret1);
17571 if (list_ret == 0)
17572 return 0;
17573 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
17574 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
17575 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
17576 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
17577 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
17578 break;
17580 case MULT_EXPR:
17581 op = DW_OP_mul;
17582 goto do_binop;
17584 case LSHIFT_EXPR:
17585 op = DW_OP_shl;
17586 goto do_binop;
17588 case RSHIFT_EXPR:
17589 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
17590 goto do_binop;
17592 case POINTER_PLUS_EXPR:
17593 case PLUS_EXPR:
17594 do_plus:
17595 if (tree_fits_shwi_p (TREE_OPERAND (loc, 1)))
17597 /* Big unsigned numbers can fit in HOST_WIDE_INT but it may be
17598 smarter to encode their opposite. The DW_OP_plus_uconst operation
17599 takes 1 + X bytes, X being the size of the ULEB128 addend. On the
17600 other hand, a "<push literal>; DW_OP_minus" pattern takes 1 + Y
17601 bytes, Y being the size of the operation that pushes the opposite
17602 of the addend. So let's choose the smallest representation. */
17603 const tree tree_addend = TREE_OPERAND (loc, 1);
17604 offset_int wi_addend;
17605 HOST_WIDE_INT shwi_addend;
17606 dw_loc_descr_ref loc_naddend;
17608 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17609 if (list_ret == 0)
17610 return 0;
17612 /* Try to get the literal to push. It is the opposite of the addend,
17613 so as we rely on wrapping during DWARF evaluation, first decode
17614 the literal as a "DWARF-sized" signed number. */
17615 wi_addend = wi::to_offset (tree_addend);
17616 wi_addend = wi::sext (wi_addend, DWARF2_ADDR_SIZE * 8);
17617 shwi_addend = wi_addend.to_shwi ();
17618 loc_naddend = (shwi_addend != INTTYPE_MINIMUM (HOST_WIDE_INT))
17619 ? int_loc_descriptor (-shwi_addend)
17620 : NULL;
17622 if (loc_naddend != NULL
17623 && ((unsigned) size_of_uleb128 (shwi_addend)
17624 > size_of_loc_descr (loc_naddend)))
17626 add_loc_descr_to_each (list_ret, loc_naddend);
17627 add_loc_descr_to_each (list_ret,
17628 new_loc_descr (DW_OP_minus, 0, 0));
17630 else
17632 for (dw_loc_descr_ref loc_cur = loc_naddend; loc_cur != NULL; )
17634 loc_naddend = loc_cur;
17635 loc_cur = loc_cur->dw_loc_next;
17636 ggc_free (loc_naddend);
17638 loc_list_plus_const (list_ret, wi_addend.to_shwi ());
17640 break;
17643 op = DW_OP_plus;
17644 goto do_binop;
17646 case LE_EXPR:
17647 op = DW_OP_le;
17648 goto do_comp_binop;
17650 case GE_EXPR:
17651 op = DW_OP_ge;
17652 goto do_comp_binop;
17654 case LT_EXPR:
17655 op = DW_OP_lt;
17656 goto do_comp_binop;
17658 case GT_EXPR:
17659 op = DW_OP_gt;
17660 goto do_comp_binop;
17662 do_comp_binop:
17663 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
17665 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
17666 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
17667 list_ret = loc_list_from_uint_comparison (list_ret, list_ret1,
17668 TREE_CODE (loc));
17669 break;
17671 else
17672 goto do_binop;
17674 case EQ_EXPR:
17675 op = DW_OP_eq;
17676 goto do_binop;
17678 case NE_EXPR:
17679 op = DW_OP_ne;
17680 goto do_binop;
17682 do_binop:
17683 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17684 list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
17685 if (list_ret == 0 || list_ret1 == 0)
17686 return 0;
17688 add_loc_list (&list_ret, list_ret1);
17689 if (list_ret == 0)
17690 return 0;
17691 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
17692 break;
17694 case TRUTH_NOT_EXPR:
17695 case BIT_NOT_EXPR:
17696 op = DW_OP_not;
17697 goto do_unop;
17699 case ABS_EXPR:
17700 op = DW_OP_abs;
17701 goto do_unop;
17703 case NEGATE_EXPR:
17704 op = DW_OP_neg;
17705 goto do_unop;
17707 do_unop:
17708 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17709 if (list_ret == 0)
17710 return 0;
17712 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
17713 break;
17715 case MIN_EXPR:
17716 case MAX_EXPR:
17718 const enum tree_code code =
17719 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
17721 loc = build3 (COND_EXPR, TREE_TYPE (loc),
17722 build2 (code, integer_type_node,
17723 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
17724 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
17727 /* fall through */
17729 case COND_EXPR:
17731 dw_loc_descr_ref lhs
17732 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0, context);
17733 dw_loc_list_ref rhs
17734 = loc_list_from_tree_1 (TREE_OPERAND (loc, 2), 0, context);
17735 dw_loc_descr_ref bra_node, jump_node, tmp;
17737 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17738 if (list_ret == 0 || lhs == 0 || rhs == 0)
17739 return 0;
17741 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
17742 add_loc_descr_to_each (list_ret, bra_node);
17744 add_loc_list (&list_ret, rhs);
17745 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
17746 add_loc_descr_to_each (list_ret, jump_node);
17748 add_loc_descr_to_each (list_ret, lhs);
17749 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
17750 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
17752 /* ??? Need a node to point the skip at. Use a nop. */
17753 tmp = new_loc_descr (DW_OP_nop, 0, 0);
17754 add_loc_descr_to_each (list_ret, tmp);
17755 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
17756 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
17758 break;
17760 case FIX_TRUNC_EXPR:
17761 return 0;
17763 default:
17764 /* Leave front-end specific codes as simply unknown. This comes
17765 up, for instance, with the C STMT_EXPR. */
17766 if ((unsigned int) TREE_CODE (loc)
17767 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
17769 expansion_failed (loc, NULL_RTX,
17770 "language specific tree node");
17771 return 0;
17774 /* Otherwise this is a generic code; we should just lists all of
17775 these explicitly. We forgot one. */
17776 if (flag_checking)
17777 gcc_unreachable ();
17779 /* In a release build, we want to degrade gracefully: better to
17780 generate incomplete debugging information than to crash. */
17781 return NULL;
17784 if (!ret && !list_ret)
17785 return 0;
17787 if (want_address == 2 && !have_address
17788 && (dwarf_version >= 4 || !dwarf_strict))
17790 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
17792 expansion_failed (loc, NULL_RTX,
17793 "DWARF address size mismatch");
17794 return 0;
17796 if (ret)
17797 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
17798 else
17799 add_loc_descr_to_each (list_ret,
17800 new_loc_descr (DW_OP_stack_value, 0, 0));
17801 have_address = 1;
17803 /* Show if we can't fill the request for an address. */
17804 if (want_address && !have_address)
17806 expansion_failed (loc, NULL_RTX,
17807 "Want address and only have value");
17808 return 0;
17811 gcc_assert (!ret || !list_ret);
17813 /* If we've got an address and don't want one, dereference. */
17814 if (!want_address && have_address)
17816 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
17818 if (size > DWARF2_ADDR_SIZE || size == -1)
17820 expansion_failed (loc, NULL_RTX,
17821 "DWARF address size mismatch");
17822 return 0;
17824 else if (size == DWARF2_ADDR_SIZE)
17825 op = DW_OP_deref;
17826 else
17827 op = DW_OP_deref_size;
17829 if (ret)
17830 add_loc_descr (&ret, new_loc_descr (op, size, 0));
17831 else
17832 add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
17834 if (ret)
17835 list_ret = new_loc_list (ret, NULL, NULL, NULL);
17837 return list_ret;
17840 /* Likewise, but strip useless DW_OP_nop operations in the resulting
17841 expressions. */
17843 static dw_loc_list_ref
17844 loc_list_from_tree (tree loc, int want_address,
17845 struct loc_descr_context *context)
17847 dw_loc_list_ref result = loc_list_from_tree_1 (loc, want_address, context);
17849 for (dw_loc_list_ref loc_cur = result;
17850 loc_cur != NULL; loc_cur =
17851 loc_cur->dw_loc_next)
17852 loc_descr_without_nops (loc_cur->expr);
17853 return result;
17856 /* Same as above but return only single location expression. */
17857 static dw_loc_descr_ref
17858 loc_descriptor_from_tree (tree loc, int want_address,
17859 struct loc_descr_context *context)
17861 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address, context);
17862 if (!ret)
17863 return NULL;
17864 if (ret->dw_loc_next)
17866 expansion_failed (loc, NULL_RTX,
17867 "Location list where only loc descriptor needed");
17868 return NULL;
17870 return ret->expr;
17873 /* Given a value, round it up to the lowest multiple of `boundary'
17874 which is not less than the value itself. */
17876 static inline HOST_WIDE_INT
17877 ceiling (HOST_WIDE_INT value, unsigned int boundary)
17879 return (((value + boundary - 1) / boundary) * boundary);
17882 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
17883 pointer to the declared type for the relevant field variable, or return
17884 `integer_type_node' if the given node turns out to be an
17885 ERROR_MARK node. */
17887 static inline tree
17888 field_type (const_tree decl)
17890 tree type;
17892 if (TREE_CODE (decl) == ERROR_MARK)
17893 return integer_type_node;
17895 type = DECL_BIT_FIELD_TYPE (decl);
17896 if (type == NULL_TREE)
17897 type = TREE_TYPE (decl);
17899 return type;
17902 /* Given a pointer to a tree node, return the alignment in bits for
17903 it, or else return BITS_PER_WORD if the node actually turns out to
17904 be an ERROR_MARK node. */
17906 static inline unsigned
17907 simple_type_align_in_bits (const_tree type)
17909 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
17912 static inline unsigned
17913 simple_decl_align_in_bits (const_tree decl)
17915 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
17918 /* Return the result of rounding T up to ALIGN. */
17920 static inline offset_int
17921 round_up_to_align (const offset_int &t, unsigned int align)
17923 return wi::udiv_trunc (t + align - 1, align) * align;
17926 /* Compute the size of TYPE in bytes. If possible, return NULL and store the
17927 size as an integer constant in CST_SIZE. Otherwise, if possible, return a
17928 DWARF expression that computes the size. Return NULL and set CST_SIZE to -1
17929 if we fail to return the size in one of these two forms. */
17931 static dw_loc_descr_ref
17932 type_byte_size (const_tree type, HOST_WIDE_INT *cst_size)
17934 tree tree_size;
17935 struct loc_descr_context ctx;
17937 /* Return a constant integer in priority, if possible. */
17938 *cst_size = int_size_in_bytes (type);
17939 if (*cst_size != -1)
17940 return NULL;
17942 ctx.context_type = const_cast<tree> (type);
17943 ctx.base_decl = NULL_TREE;
17944 ctx.dpi = NULL;
17945 ctx.placeholder_arg = false;
17946 ctx.placeholder_seen = false;
17948 type = TYPE_MAIN_VARIANT (type);
17949 tree_size = TYPE_SIZE_UNIT (type);
17950 return ((tree_size != NULL_TREE)
17951 ? loc_descriptor_from_tree (tree_size, 0, &ctx)
17952 : NULL);
17955 /* Helper structure for RECORD_TYPE processing. */
17956 struct vlr_context
17958 /* Root RECORD_TYPE. It is needed to generate data member location
17959 descriptions in variable-length records (VLR), but also to cope with
17960 variants, which are composed of nested structures multiplexed with
17961 QUAL_UNION_TYPE nodes. Each time such a structure is passed to a
17962 function processing a FIELD_DECL, it is required to be non null. */
17963 tree struct_type;
17964 /* When generating a variant part in a RECORD_TYPE (i.e. a nested
17965 QUAL_UNION_TYPE), this holds an expression that computes the offset for
17966 this variant part as part of the root record (in storage units). For
17967 regular records, it must be NULL_TREE. */
17968 tree variant_part_offset;
17971 /* Given a pointer to a FIELD_DECL, compute the byte offset of the lowest
17972 addressed byte of the "containing object" for the given FIELD_DECL. If
17973 possible, return a native constant through CST_OFFSET (in which case NULL is
17974 returned); otherwise return a DWARF expression that computes the offset.
17976 Set *CST_OFFSET to 0 and return NULL if we are unable to determine what
17977 that offset is, either because the argument turns out to be a pointer to an
17978 ERROR_MARK node, or because the offset expression is too complex for us.
17980 CTX is required: see the comment for VLR_CONTEXT. */
17982 static dw_loc_descr_ref
17983 field_byte_offset (const_tree decl, struct vlr_context *ctx,
17984 HOST_WIDE_INT *cst_offset)
17986 tree tree_result;
17987 dw_loc_list_ref loc_result;
17989 *cst_offset = 0;
17991 if (TREE_CODE (decl) == ERROR_MARK)
17992 return NULL;
17993 else
17994 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
17996 /* We cannot handle variable bit offsets at the moment, so abort if it's the
17997 case. */
17998 if (TREE_CODE (DECL_FIELD_BIT_OFFSET (decl)) != INTEGER_CST)
17999 return NULL;
18001 #ifdef PCC_BITFIELD_TYPE_MATTERS
18002 /* We used to handle only constant offsets in all cases. Now, we handle
18003 properly dynamic byte offsets only when PCC bitfield type doesn't
18004 matter. */
18005 if (PCC_BITFIELD_TYPE_MATTERS
18006 && TREE_CODE (DECL_FIELD_OFFSET (decl)) == INTEGER_CST)
18008 offset_int object_offset_in_bits;
18009 offset_int object_offset_in_bytes;
18010 offset_int bitpos_int;
18011 tree type;
18012 tree field_size_tree;
18013 offset_int deepest_bitpos;
18014 offset_int field_size_in_bits;
18015 unsigned int type_align_in_bits;
18016 unsigned int decl_align_in_bits;
18017 offset_int type_size_in_bits;
18019 bitpos_int = wi::to_offset (bit_position (decl));
18020 type = field_type (decl);
18021 type_size_in_bits = offset_int_type_size_in_bits (type);
18022 type_align_in_bits = simple_type_align_in_bits (type);
18024 field_size_tree = DECL_SIZE (decl);
18026 /* The size could be unspecified if there was an error, or for
18027 a flexible array member. */
18028 if (!field_size_tree)
18029 field_size_tree = bitsize_zero_node;
18031 /* If the size of the field is not constant, use the type size. */
18032 if (TREE_CODE (field_size_tree) == INTEGER_CST)
18033 field_size_in_bits = wi::to_offset (field_size_tree);
18034 else
18035 field_size_in_bits = type_size_in_bits;
18037 decl_align_in_bits = simple_decl_align_in_bits (decl);
18039 /* The GCC front-end doesn't make any attempt to keep track of the
18040 starting bit offset (relative to the start of the containing
18041 structure type) of the hypothetical "containing object" for a
18042 bit-field. Thus, when computing the byte offset value for the
18043 start of the "containing object" of a bit-field, we must deduce
18044 this information on our own. This can be rather tricky to do in
18045 some cases. For example, handling the following structure type
18046 definition when compiling for an i386/i486 target (which only
18047 aligns long long's to 32-bit boundaries) can be very tricky:
18049 struct S { int field1; long long field2:31; };
18051 Fortunately, there is a simple rule-of-thumb which can be used
18052 in such cases. When compiling for an i386/i486, GCC will
18053 allocate 8 bytes for the structure shown above. It decides to
18054 do this based upon one simple rule for bit-field allocation.
18055 GCC allocates each "containing object" for each bit-field at
18056 the first (i.e. lowest addressed) legitimate alignment boundary
18057 (based upon the required minimum alignment for the declared
18058 type of the field) which it can possibly use, subject to the
18059 condition that there is still enough available space remaining
18060 in the containing object (when allocated at the selected point)
18061 to fully accommodate all of the bits of the bit-field itself.
18063 This simple rule makes it obvious why GCC allocates 8 bytes for
18064 each object of the structure type shown above. When looking
18065 for a place to allocate the "containing object" for `field2',
18066 the compiler simply tries to allocate a 64-bit "containing
18067 object" at each successive 32-bit boundary (starting at zero)
18068 until it finds a place to allocate that 64- bit field such that
18069 at least 31 contiguous (and previously unallocated) bits remain
18070 within that selected 64 bit field. (As it turns out, for the
18071 example above, the compiler finds it is OK to allocate the
18072 "containing object" 64-bit field at bit-offset zero within the
18073 structure type.)
18075 Here we attempt to work backwards from the limited set of facts
18076 we're given, and we try to deduce from those facts, where GCC
18077 must have believed that the containing object started (within
18078 the structure type). The value we deduce is then used (by the
18079 callers of this routine) to generate DW_AT_location and
18080 DW_AT_bit_offset attributes for fields (both bit-fields and, in
18081 the case of DW_AT_location, regular fields as well). */
18083 /* Figure out the bit-distance from the start of the structure to
18084 the "deepest" bit of the bit-field. */
18085 deepest_bitpos = bitpos_int + field_size_in_bits;
18087 /* This is the tricky part. Use some fancy footwork to deduce
18088 where the lowest addressed bit of the containing object must
18089 be. */
18090 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
18092 /* Round up to type_align by default. This works best for
18093 bitfields. */
18094 object_offset_in_bits
18095 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
18097 if (wi::gtu_p (object_offset_in_bits, bitpos_int))
18099 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
18101 /* Round up to decl_align instead. */
18102 object_offset_in_bits
18103 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
18106 object_offset_in_bytes
18107 = wi::lrshift (object_offset_in_bits, LOG2_BITS_PER_UNIT);
18108 if (ctx->variant_part_offset == NULL_TREE)
18110 *cst_offset = object_offset_in_bytes.to_shwi ();
18111 return NULL;
18113 tree_result = wide_int_to_tree (sizetype, object_offset_in_bytes);
18115 else
18116 #endif /* PCC_BITFIELD_TYPE_MATTERS */
18117 tree_result = byte_position (decl);
18119 if (ctx->variant_part_offset != NULL_TREE)
18120 tree_result = fold_build2 (PLUS_EXPR, TREE_TYPE (tree_result),
18121 ctx->variant_part_offset, tree_result);
18123 /* If the byte offset is a constant, it's simplier to handle a native
18124 constant rather than a DWARF expression. */
18125 if (TREE_CODE (tree_result) == INTEGER_CST)
18127 *cst_offset = wi::to_offset (tree_result).to_shwi ();
18128 return NULL;
18130 struct loc_descr_context loc_ctx = {
18131 ctx->struct_type, /* context_type */
18132 NULL_TREE, /* base_decl */
18133 NULL, /* dpi */
18134 false, /* placeholder_arg */
18135 false /* placeholder_seen */
18137 loc_result = loc_list_from_tree (tree_result, 0, &loc_ctx);
18139 /* We want a DWARF expression: abort if we only have a location list with
18140 multiple elements. */
18141 if (!loc_result || !single_element_loc_list_p (loc_result))
18142 return NULL;
18143 else
18144 return loc_result->expr;
18147 /* The following routines define various Dwarf attributes and any data
18148 associated with them. */
18150 /* Add a location description attribute value to a DIE.
18152 This emits location attributes suitable for whole variables and
18153 whole parameters. Note that the location attributes for struct fields are
18154 generated by the routine `data_member_location_attribute' below. */
18156 static inline void
18157 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
18158 dw_loc_list_ref descr)
18160 if (descr == 0)
18161 return;
18162 if (single_element_loc_list_p (descr))
18163 add_AT_loc (die, attr_kind, descr->expr);
18164 else
18165 add_AT_loc_list (die, attr_kind, descr);
18168 /* Add DW_AT_accessibility attribute to DIE if needed. */
18170 static void
18171 add_accessibility_attribute (dw_die_ref die, tree decl)
18173 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
18174 children, otherwise the default is DW_ACCESS_public. In DWARF2
18175 the default has always been DW_ACCESS_public. */
18176 if (TREE_PROTECTED (decl))
18177 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
18178 else if (TREE_PRIVATE (decl))
18180 if (dwarf_version == 2
18181 || die->die_parent == NULL
18182 || die->die_parent->die_tag != DW_TAG_class_type)
18183 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
18185 else if (dwarf_version > 2
18186 && die->die_parent
18187 && die->die_parent->die_tag == DW_TAG_class_type)
18188 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
18191 /* Attach the specialized form of location attribute used for data members of
18192 struct and union types. In the special case of a FIELD_DECL node which
18193 represents a bit-field, the "offset" part of this special location
18194 descriptor must indicate the distance in bytes from the lowest-addressed
18195 byte of the containing struct or union type to the lowest-addressed byte of
18196 the "containing object" for the bit-field. (See the `field_byte_offset'
18197 function above).
18199 For any given bit-field, the "containing object" is a hypothetical object
18200 (of some integral or enum type) within which the given bit-field lives. The
18201 type of this hypothetical "containing object" is always the same as the
18202 declared type of the individual bit-field itself (for GCC anyway... the
18203 DWARF spec doesn't actually mandate this). Note that it is the size (in
18204 bytes) of the hypothetical "containing object" which will be given in the
18205 DW_AT_byte_size attribute for this bit-field. (See the
18206 `byte_size_attribute' function below.) It is also used when calculating the
18207 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
18208 function below.)
18210 CTX is required: see the comment for VLR_CONTEXT. */
18212 static void
18213 add_data_member_location_attribute (dw_die_ref die,
18214 tree decl,
18215 struct vlr_context *ctx)
18217 HOST_WIDE_INT offset;
18218 dw_loc_descr_ref loc_descr = 0;
18220 if (TREE_CODE (decl) == TREE_BINFO)
18222 /* We're working on the TAG_inheritance for a base class. */
18223 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
18225 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
18226 aren't at a fixed offset from all (sub)objects of the same
18227 type. We need to extract the appropriate offset from our
18228 vtable. The following dwarf expression means
18230 BaseAddr = ObAddr + *((*ObAddr) - Offset)
18232 This is specific to the V3 ABI, of course. */
18234 dw_loc_descr_ref tmp;
18236 /* Make a copy of the object address. */
18237 tmp = new_loc_descr (DW_OP_dup, 0, 0);
18238 add_loc_descr (&loc_descr, tmp);
18240 /* Extract the vtable address. */
18241 tmp = new_loc_descr (DW_OP_deref, 0, 0);
18242 add_loc_descr (&loc_descr, tmp);
18244 /* Calculate the address of the offset. */
18245 offset = tree_to_shwi (BINFO_VPTR_FIELD (decl));
18246 gcc_assert (offset < 0);
18248 tmp = int_loc_descriptor (-offset);
18249 add_loc_descr (&loc_descr, tmp);
18250 tmp = new_loc_descr (DW_OP_minus, 0, 0);
18251 add_loc_descr (&loc_descr, tmp);
18253 /* Extract the offset. */
18254 tmp = new_loc_descr (DW_OP_deref, 0, 0);
18255 add_loc_descr (&loc_descr, tmp);
18257 /* Add it to the object address. */
18258 tmp = new_loc_descr (DW_OP_plus, 0, 0);
18259 add_loc_descr (&loc_descr, tmp);
18261 else
18262 offset = tree_to_shwi (BINFO_OFFSET (decl));
18264 else
18266 loc_descr = field_byte_offset (decl, ctx, &offset);
18268 /* If loc_descr is available then we know the field offset is dynamic.
18269 However, GDB does not handle dynamic field offsets very well at the
18270 moment. */
18271 if (loc_descr != NULL && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL)
18273 loc_descr = NULL;
18274 offset = 0;
18277 /* Data member location evalutation starts with the base address on the
18278 stack. Compute the field offset and add it to this base address. */
18279 else if (loc_descr != NULL)
18280 add_loc_descr (&loc_descr, new_loc_descr (DW_OP_plus, 0, 0));
18283 if (! loc_descr)
18285 /* While DW_AT_data_bit_offset has been added already in DWARF4,
18286 e.g. GDB only added support to it in November 2016. For DWARF5
18287 we need newer debug info consumers anyway. We might change this
18288 to dwarf_version >= 4 once most consumers catched up. */
18289 if (dwarf_version >= 5
18290 && TREE_CODE (decl) == FIELD_DECL
18291 && DECL_BIT_FIELD_TYPE (decl))
18293 tree off = bit_position (decl);
18294 if (tree_fits_uhwi_p (off) && get_AT (die, DW_AT_bit_size))
18296 remove_AT (die, DW_AT_byte_size);
18297 remove_AT (die, DW_AT_bit_offset);
18298 add_AT_unsigned (die, DW_AT_data_bit_offset, tree_to_uhwi (off));
18299 return;
18302 if (dwarf_version > 2)
18304 /* Don't need to output a location expression, just the constant. */
18305 if (offset < 0)
18306 add_AT_int (die, DW_AT_data_member_location, offset);
18307 else
18308 add_AT_unsigned (die, DW_AT_data_member_location, offset);
18309 return;
18311 else
18313 enum dwarf_location_atom op;
18315 /* The DWARF2 standard says that we should assume that the structure
18316 address is already on the stack, so we can specify a structure
18317 field address by using DW_OP_plus_uconst. */
18318 op = DW_OP_plus_uconst;
18319 loc_descr = new_loc_descr (op, offset, 0);
18323 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
18326 /* Writes integer values to dw_vec_const array. */
18328 static void
18329 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
18331 while (size != 0)
18333 *dest++ = val & 0xff;
18334 val >>= 8;
18335 --size;
18339 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
18341 static HOST_WIDE_INT
18342 extract_int (const unsigned char *src, unsigned int size)
18344 HOST_WIDE_INT val = 0;
18346 src += size;
18347 while (size != 0)
18349 val <<= 8;
18350 val |= *--src & 0xff;
18351 --size;
18353 return val;
18356 /* Writes wide_int values to dw_vec_const array. */
18358 static void
18359 insert_wide_int (const wide_int &val, unsigned char *dest, int elt_size)
18361 int i;
18363 if (elt_size <= HOST_BITS_PER_WIDE_INT/BITS_PER_UNIT)
18365 insert_int ((HOST_WIDE_INT) val.elt (0), elt_size, dest);
18366 return;
18369 /* We'd have to extend this code to support odd sizes. */
18370 gcc_assert (elt_size % (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT) == 0);
18372 int n = elt_size / (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
18374 if (WORDS_BIG_ENDIAN)
18375 for (i = n - 1; i >= 0; i--)
18377 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
18378 dest += sizeof (HOST_WIDE_INT);
18380 else
18381 for (i = 0; i < n; i++)
18383 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
18384 dest += sizeof (HOST_WIDE_INT);
18388 /* Writes floating point values to dw_vec_const array. */
18390 static void
18391 insert_float (const_rtx rtl, unsigned char *array)
18393 long val[4];
18394 int i;
18396 real_to_target (val, CONST_DOUBLE_REAL_VALUE (rtl), GET_MODE (rtl));
18398 /* real_to_target puts 32-bit pieces in each long. Pack them. */
18399 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
18401 insert_int (val[i], 4, array);
18402 array += 4;
18406 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
18407 does not have a "location" either in memory or in a register. These
18408 things can arise in GNU C when a constant is passed as an actual parameter
18409 to an inlined function. They can also arise in C++ where declared
18410 constants do not necessarily get memory "homes". */
18412 static bool
18413 add_const_value_attribute (dw_die_ref die, rtx rtl)
18415 switch (GET_CODE (rtl))
18417 case CONST_INT:
18419 HOST_WIDE_INT val = INTVAL (rtl);
18421 if (val < 0)
18422 add_AT_int (die, DW_AT_const_value, val);
18423 else
18424 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
18426 return true;
18428 case CONST_WIDE_INT:
18430 wide_int w1 = rtx_mode_t (rtl, MAX_MODE_INT);
18431 unsigned int prec = MIN (wi::min_precision (w1, UNSIGNED),
18432 (unsigned int)CONST_WIDE_INT_NUNITS (rtl) * HOST_BITS_PER_WIDE_INT);
18433 wide_int w = wi::zext (w1, prec);
18434 add_AT_wide (die, DW_AT_const_value, w);
18436 return true;
18438 case CONST_DOUBLE:
18439 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
18440 floating-point constant. A CONST_DOUBLE is used whenever the
18441 constant requires more than one word in order to be adequately
18442 represented. */
18444 machine_mode mode = GET_MODE (rtl);
18446 if (TARGET_SUPPORTS_WIDE_INT == 0 && !SCALAR_FLOAT_MODE_P (mode))
18447 add_AT_double (die, DW_AT_const_value,
18448 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
18449 else
18451 unsigned int length = GET_MODE_SIZE (mode);
18452 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
18454 insert_float (rtl, array);
18455 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
18458 return true;
18460 case CONST_VECTOR:
18462 machine_mode mode = GET_MODE (rtl);
18463 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
18464 unsigned int length = CONST_VECTOR_NUNITS (rtl);
18465 unsigned char *array
18466 = ggc_vec_alloc<unsigned char> (length * elt_size);
18467 unsigned int i;
18468 unsigned char *p;
18469 machine_mode imode = GET_MODE_INNER (mode);
18471 switch (GET_MODE_CLASS (mode))
18473 case MODE_VECTOR_INT:
18474 for (i = 0, p = array; i < length; i++, p += elt_size)
18476 rtx elt = CONST_VECTOR_ELT (rtl, i);
18477 insert_wide_int (rtx_mode_t (elt, imode), p, elt_size);
18479 break;
18481 case MODE_VECTOR_FLOAT:
18482 for (i = 0, p = array; i < length; i++, p += elt_size)
18484 rtx elt = CONST_VECTOR_ELT (rtl, i);
18485 insert_float (elt, p);
18487 break;
18489 default:
18490 gcc_unreachable ();
18493 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
18495 return true;
18497 case CONST_STRING:
18498 if (dwarf_version >= 4 || !dwarf_strict)
18500 dw_loc_descr_ref loc_result;
18501 resolve_one_addr (&rtl);
18502 rtl_addr:
18503 loc_result = new_addr_loc_descr (rtl, dtprel_false);
18504 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
18505 add_AT_loc (die, DW_AT_location, loc_result);
18506 vec_safe_push (used_rtx_array, rtl);
18507 return true;
18509 return false;
18511 case CONST:
18512 if (CONSTANT_P (XEXP (rtl, 0)))
18513 return add_const_value_attribute (die, XEXP (rtl, 0));
18514 /* FALLTHROUGH */
18515 case SYMBOL_REF:
18516 if (!const_ok_for_output (rtl))
18517 return false;
18518 /* FALLTHROUGH */
18519 case LABEL_REF:
18520 if (dwarf_version >= 4 || !dwarf_strict)
18521 goto rtl_addr;
18522 return false;
18524 case PLUS:
18525 /* In cases where an inlined instance of an inline function is passed
18526 the address of an `auto' variable (which is local to the caller) we
18527 can get a situation where the DECL_RTL of the artificial local
18528 variable (for the inlining) which acts as a stand-in for the
18529 corresponding formal parameter (of the inline function) will look
18530 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
18531 exactly a compile-time constant expression, but it isn't the address
18532 of the (artificial) local variable either. Rather, it represents the
18533 *value* which the artificial local variable always has during its
18534 lifetime. We currently have no way to represent such quasi-constant
18535 values in Dwarf, so for now we just punt and generate nothing. */
18536 return false;
18538 case HIGH:
18539 case CONST_FIXED:
18540 return false;
18542 case MEM:
18543 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
18544 && MEM_READONLY_P (rtl)
18545 && GET_MODE (rtl) == BLKmode)
18547 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
18548 return true;
18550 return false;
18552 default:
18553 /* No other kinds of rtx should be possible here. */
18554 gcc_unreachable ();
18556 return false;
18559 /* Determine whether the evaluation of EXPR references any variables
18560 or functions which aren't otherwise used (and therefore may not be
18561 output). */
18562 static tree
18563 reference_to_unused (tree * tp, int * walk_subtrees,
18564 void * data ATTRIBUTE_UNUSED)
18566 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
18567 *walk_subtrees = 0;
18569 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
18570 && ! TREE_ASM_WRITTEN (*tp))
18571 return *tp;
18572 /* ??? The C++ FE emits debug information for using decls, so
18573 putting gcc_unreachable here falls over. See PR31899. For now
18574 be conservative. */
18575 else if (!symtab->global_info_ready && VAR_OR_FUNCTION_DECL_P (*tp))
18576 return *tp;
18577 else if (VAR_P (*tp))
18579 varpool_node *node = varpool_node::get (*tp);
18580 if (!node || !node->definition)
18581 return *tp;
18583 else if (TREE_CODE (*tp) == FUNCTION_DECL
18584 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
18586 /* The call graph machinery must have finished analyzing,
18587 optimizing and gimplifying the CU by now.
18588 So if *TP has no call graph node associated
18589 to it, it means *TP will not be emitted. */
18590 if (!cgraph_node::get (*tp))
18591 return *tp;
18593 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
18594 return *tp;
18596 return NULL_TREE;
18599 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
18600 for use in a later add_const_value_attribute call. */
18602 static rtx
18603 rtl_for_decl_init (tree init, tree type)
18605 rtx rtl = NULL_RTX;
18607 STRIP_NOPS (init);
18609 /* If a variable is initialized with a string constant without embedded
18610 zeros, build CONST_STRING. */
18611 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
18613 tree enttype = TREE_TYPE (type);
18614 tree domain = TYPE_DOMAIN (type);
18615 machine_mode mode = TYPE_MODE (enttype);
18617 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
18618 && domain
18619 && integer_zerop (TYPE_MIN_VALUE (domain))
18620 && compare_tree_int (TYPE_MAX_VALUE (domain),
18621 TREE_STRING_LENGTH (init) - 1) == 0
18622 && ((size_t) TREE_STRING_LENGTH (init)
18623 == strlen (TREE_STRING_POINTER (init)) + 1))
18625 rtl = gen_rtx_CONST_STRING (VOIDmode,
18626 ggc_strdup (TREE_STRING_POINTER (init)));
18627 rtl = gen_rtx_MEM (BLKmode, rtl);
18628 MEM_READONLY_P (rtl) = 1;
18631 /* Other aggregates, and complex values, could be represented using
18632 CONCAT: FIXME! */
18633 else if (AGGREGATE_TYPE_P (type)
18634 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
18635 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
18636 || TREE_CODE (type) == COMPLEX_TYPE)
18638 /* Vectors only work if their mode is supported by the target.
18639 FIXME: generic vectors ought to work too. */
18640 else if (TREE_CODE (type) == VECTOR_TYPE
18641 && !VECTOR_MODE_P (TYPE_MODE (type)))
18643 /* If the initializer is something that we know will expand into an
18644 immediate RTL constant, expand it now. We must be careful not to
18645 reference variables which won't be output. */
18646 else if (initializer_constant_valid_p (init, type)
18647 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
18649 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
18650 possible. */
18651 if (TREE_CODE (type) == VECTOR_TYPE)
18652 switch (TREE_CODE (init))
18654 case VECTOR_CST:
18655 break;
18656 case CONSTRUCTOR:
18657 if (TREE_CONSTANT (init))
18659 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
18660 bool constant_p = true;
18661 tree value;
18662 unsigned HOST_WIDE_INT ix;
18664 /* Even when ctor is constant, it might contain non-*_CST
18665 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
18666 belong into VECTOR_CST nodes. */
18667 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
18668 if (!CONSTANT_CLASS_P (value))
18670 constant_p = false;
18671 break;
18674 if (constant_p)
18676 init = build_vector_from_ctor (type, elts);
18677 break;
18680 /* FALLTHRU */
18682 default:
18683 return NULL;
18686 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
18688 /* If expand_expr returns a MEM, it wasn't immediate. */
18689 gcc_assert (!rtl || !MEM_P (rtl));
18692 return rtl;
18695 /* Generate RTL for the variable DECL to represent its location. */
18697 static rtx
18698 rtl_for_decl_location (tree decl)
18700 rtx rtl;
18702 /* Here we have to decide where we are going to say the parameter "lives"
18703 (as far as the debugger is concerned). We only have a couple of
18704 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
18706 DECL_RTL normally indicates where the parameter lives during most of the
18707 activation of the function. If optimization is enabled however, this
18708 could be either NULL or else a pseudo-reg. Both of those cases indicate
18709 that the parameter doesn't really live anywhere (as far as the code
18710 generation parts of GCC are concerned) during most of the function's
18711 activation. That will happen (for example) if the parameter is never
18712 referenced within the function.
18714 We could just generate a location descriptor here for all non-NULL
18715 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
18716 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
18717 where DECL_RTL is NULL or is a pseudo-reg.
18719 Note however that we can only get away with using DECL_INCOMING_RTL as
18720 a backup substitute for DECL_RTL in certain limited cases. In cases
18721 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
18722 we can be sure that the parameter was passed using the same type as it is
18723 declared to have within the function, and that its DECL_INCOMING_RTL
18724 points us to a place where a value of that type is passed.
18726 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
18727 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
18728 because in these cases DECL_INCOMING_RTL points us to a value of some
18729 type which is *different* from the type of the parameter itself. Thus,
18730 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
18731 such cases, the debugger would end up (for example) trying to fetch a
18732 `float' from a place which actually contains the first part of a
18733 `double'. That would lead to really incorrect and confusing
18734 output at debug-time.
18736 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
18737 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
18738 are a couple of exceptions however. On little-endian machines we can
18739 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
18740 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
18741 an integral type that is smaller than TREE_TYPE (decl). These cases arise
18742 when (on a little-endian machine) a non-prototyped function has a
18743 parameter declared to be of type `short' or `char'. In such cases,
18744 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
18745 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
18746 passed `int' value. If the debugger then uses that address to fetch
18747 a `short' or a `char' (on a little-endian machine) the result will be
18748 the correct data, so we allow for such exceptional cases below.
18750 Note that our goal here is to describe the place where the given formal
18751 parameter lives during most of the function's activation (i.e. between the
18752 end of the prologue and the start of the epilogue). We'll do that as best
18753 as we can. Note however that if the given formal parameter is modified
18754 sometime during the execution of the function, then a stack backtrace (at
18755 debug-time) will show the function as having been called with the *new*
18756 value rather than the value which was originally passed in. This happens
18757 rarely enough that it is not a major problem, but it *is* a problem, and
18758 I'd like to fix it.
18760 A future version of dwarf2out.c may generate two additional attributes for
18761 any given DW_TAG_formal_parameter DIE which will describe the "passed
18762 type" and the "passed location" for the given formal parameter in addition
18763 to the attributes we now generate to indicate the "declared type" and the
18764 "active location" for each parameter. This additional set of attributes
18765 could be used by debuggers for stack backtraces. Separately, note that
18766 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
18767 This happens (for example) for inlined-instances of inline function formal
18768 parameters which are never referenced. This really shouldn't be
18769 happening. All PARM_DECL nodes should get valid non-NULL
18770 DECL_INCOMING_RTL values. FIXME. */
18772 /* Use DECL_RTL as the "location" unless we find something better. */
18773 rtl = DECL_RTL_IF_SET (decl);
18775 /* When generating abstract instances, ignore everything except
18776 constants, symbols living in memory, and symbols living in
18777 fixed registers. */
18778 if (! reload_completed)
18780 if (rtl
18781 && (CONSTANT_P (rtl)
18782 || (MEM_P (rtl)
18783 && CONSTANT_P (XEXP (rtl, 0)))
18784 || (REG_P (rtl)
18785 && VAR_P (decl)
18786 && TREE_STATIC (decl))))
18788 rtl = targetm.delegitimize_address (rtl);
18789 return rtl;
18791 rtl = NULL_RTX;
18793 else if (TREE_CODE (decl) == PARM_DECL)
18795 if (rtl == NULL_RTX
18796 || is_pseudo_reg (rtl)
18797 || (MEM_P (rtl)
18798 && is_pseudo_reg (XEXP (rtl, 0))
18799 && DECL_INCOMING_RTL (decl)
18800 && MEM_P (DECL_INCOMING_RTL (decl))
18801 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
18803 tree declared_type = TREE_TYPE (decl);
18804 tree passed_type = DECL_ARG_TYPE (decl);
18805 machine_mode dmode = TYPE_MODE (declared_type);
18806 machine_mode pmode = TYPE_MODE (passed_type);
18808 /* This decl represents a formal parameter which was optimized out.
18809 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
18810 all cases where (rtl == NULL_RTX) just below. */
18811 if (dmode == pmode)
18812 rtl = DECL_INCOMING_RTL (decl);
18813 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
18814 && SCALAR_INT_MODE_P (dmode)
18815 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
18816 && DECL_INCOMING_RTL (decl))
18818 rtx inc = DECL_INCOMING_RTL (decl);
18819 if (REG_P (inc))
18820 rtl = inc;
18821 else if (MEM_P (inc))
18823 if (BYTES_BIG_ENDIAN)
18824 rtl = adjust_address_nv (inc, dmode,
18825 GET_MODE_SIZE (pmode)
18826 - GET_MODE_SIZE (dmode));
18827 else
18828 rtl = inc;
18833 /* If the parm was passed in registers, but lives on the stack, then
18834 make a big endian correction if the mode of the type of the
18835 parameter is not the same as the mode of the rtl. */
18836 /* ??? This is the same series of checks that are made in dbxout.c before
18837 we reach the big endian correction code there. It isn't clear if all
18838 of these checks are necessary here, but keeping them all is the safe
18839 thing to do. */
18840 else if (MEM_P (rtl)
18841 && XEXP (rtl, 0) != const0_rtx
18842 && ! CONSTANT_P (XEXP (rtl, 0))
18843 /* Not passed in memory. */
18844 && !MEM_P (DECL_INCOMING_RTL (decl))
18845 /* Not passed by invisible reference. */
18846 && (!REG_P (XEXP (rtl, 0))
18847 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
18848 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
18849 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
18850 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
18851 #endif
18853 /* Big endian correction check. */
18854 && BYTES_BIG_ENDIAN
18855 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
18856 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
18857 < UNITS_PER_WORD))
18859 machine_mode addr_mode = get_address_mode (rtl);
18860 int offset = (UNITS_PER_WORD
18861 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
18863 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
18864 plus_constant (addr_mode, XEXP (rtl, 0), offset));
18867 else if (VAR_P (decl)
18868 && rtl
18869 && MEM_P (rtl)
18870 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
18871 && BYTES_BIG_ENDIAN)
18873 machine_mode addr_mode = get_address_mode (rtl);
18874 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
18875 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
18877 /* If a variable is declared "register" yet is smaller than
18878 a register, then if we store the variable to memory, it
18879 looks like we're storing a register-sized value, when in
18880 fact we are not. We need to adjust the offset of the
18881 storage location to reflect the actual value's bytes,
18882 else gdb will not be able to display it. */
18883 if (rsize > dsize)
18884 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
18885 plus_constant (addr_mode, XEXP (rtl, 0),
18886 rsize - dsize));
18889 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
18890 and will have been substituted directly into all expressions that use it.
18891 C does not have such a concept, but C++ and other languages do. */
18892 if (!rtl && VAR_P (decl) && DECL_INITIAL (decl))
18893 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
18895 if (rtl)
18896 rtl = targetm.delegitimize_address (rtl);
18898 /* If we don't look past the constant pool, we risk emitting a
18899 reference to a constant pool entry that isn't referenced from
18900 code, and thus is not emitted. */
18901 if (rtl)
18902 rtl = avoid_constant_pool_reference (rtl);
18904 /* Try harder to get a rtl. If this symbol ends up not being emitted
18905 in the current CU, resolve_addr will remove the expression referencing
18906 it. */
18907 if (rtl == NULL_RTX
18908 && VAR_P (decl)
18909 && !DECL_EXTERNAL (decl)
18910 && TREE_STATIC (decl)
18911 && DECL_NAME (decl)
18912 && !DECL_HARD_REGISTER (decl)
18913 && DECL_MODE (decl) != VOIDmode)
18915 rtl = make_decl_rtl_for_debug (decl);
18916 if (!MEM_P (rtl)
18917 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
18918 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
18919 rtl = NULL_RTX;
18922 return rtl;
18925 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
18926 returned. If so, the decl for the COMMON block is returned, and the
18927 value is the offset into the common block for the symbol. */
18929 static tree
18930 fortran_common (tree decl, HOST_WIDE_INT *value)
18932 tree val_expr, cvar;
18933 machine_mode mode;
18934 HOST_WIDE_INT bitsize, bitpos;
18935 tree offset;
18936 int unsignedp, reversep, volatilep = 0;
18938 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
18939 it does not have a value (the offset into the common area), or if it
18940 is thread local (as opposed to global) then it isn't common, and shouldn't
18941 be handled as such. */
18942 if (!VAR_P (decl)
18943 || !TREE_STATIC (decl)
18944 || !DECL_HAS_VALUE_EXPR_P (decl)
18945 || !is_fortran ())
18946 return NULL_TREE;
18948 val_expr = DECL_VALUE_EXPR (decl);
18949 if (TREE_CODE (val_expr) != COMPONENT_REF)
18950 return NULL_TREE;
18952 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset, &mode,
18953 &unsignedp, &reversep, &volatilep);
18955 if (cvar == NULL_TREE
18956 || !VAR_P (cvar)
18957 || DECL_ARTIFICIAL (cvar)
18958 || !TREE_PUBLIC (cvar))
18959 return NULL_TREE;
18961 *value = 0;
18962 if (offset != NULL)
18964 if (!tree_fits_shwi_p (offset))
18965 return NULL_TREE;
18966 *value = tree_to_shwi (offset);
18968 if (bitpos != 0)
18969 *value += bitpos / BITS_PER_UNIT;
18971 return cvar;
18974 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
18975 data attribute for a variable or a parameter. We generate the
18976 DW_AT_const_value attribute only in those cases where the given variable
18977 or parameter does not have a true "location" either in memory or in a
18978 register. This can happen (for example) when a constant is passed as an
18979 actual argument in a call to an inline function. (It's possible that
18980 these things can crop up in other ways also.) Note that one type of
18981 constant value which can be passed into an inlined function is a constant
18982 pointer. This can happen for example if an actual argument in an inlined
18983 function call evaluates to a compile-time constant address.
18985 CACHE_P is true if it is worth caching the location list for DECL,
18986 so that future calls can reuse it rather than regenerate it from scratch.
18987 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
18988 since we will need to refer to them each time the function is inlined. */
18990 static bool
18991 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p)
18993 rtx rtl;
18994 dw_loc_list_ref list;
18995 var_loc_list *loc_list;
18996 cached_dw_loc_list *cache;
18998 if (early_dwarf)
18999 return false;
19001 if (TREE_CODE (decl) == ERROR_MARK)
19002 return false;
19004 if (get_AT (die, DW_AT_location)
19005 || get_AT (die, DW_AT_const_value))
19006 return true;
19008 gcc_assert (VAR_P (decl) || TREE_CODE (decl) == PARM_DECL
19009 || TREE_CODE (decl) == RESULT_DECL);
19011 /* Try to get some constant RTL for this decl, and use that as the value of
19012 the location. */
19014 rtl = rtl_for_decl_location (decl);
19015 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
19016 && add_const_value_attribute (die, rtl))
19017 return true;
19019 /* See if we have single element location list that is equivalent to
19020 a constant value. That way we are better to use add_const_value_attribute
19021 rather than expanding constant value equivalent. */
19022 loc_list = lookup_decl_loc (decl);
19023 if (loc_list
19024 && loc_list->first
19025 && loc_list->first->next == NULL
19026 && NOTE_P (loc_list->first->loc)
19027 && NOTE_VAR_LOCATION (loc_list->first->loc)
19028 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
19030 struct var_loc_node *node;
19032 node = loc_list->first;
19033 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
19034 if (GET_CODE (rtl) == EXPR_LIST)
19035 rtl = XEXP (rtl, 0);
19036 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
19037 && add_const_value_attribute (die, rtl))
19038 return true;
19040 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
19041 list several times. See if we've already cached the contents. */
19042 list = NULL;
19043 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
19044 cache_p = false;
19045 if (cache_p)
19047 cache = cached_dw_loc_list_table->find_with_hash (decl, DECL_UID (decl));
19048 if (cache)
19049 list = cache->loc_list;
19051 if (list == NULL)
19053 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2,
19054 NULL);
19055 /* It is usually worth caching this result if the decl is from
19056 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
19057 if (cache_p && list && list->dw_loc_next)
19059 cached_dw_loc_list **slot
19060 = cached_dw_loc_list_table->find_slot_with_hash (decl,
19061 DECL_UID (decl),
19062 INSERT);
19063 cache = ggc_cleared_alloc<cached_dw_loc_list> ();
19064 cache->decl_id = DECL_UID (decl);
19065 cache->loc_list = list;
19066 *slot = cache;
19069 if (list)
19071 add_AT_location_description (die, DW_AT_location, list);
19072 return true;
19074 /* None of that worked, so it must not really have a location;
19075 try adding a constant value attribute from the DECL_INITIAL. */
19076 return tree_add_const_value_attribute_for_decl (die, decl);
19079 /* Helper function for tree_add_const_value_attribute. Natively encode
19080 initializer INIT into an array. Return true if successful. */
19082 static bool
19083 native_encode_initializer (tree init, unsigned char *array, int size)
19085 tree type;
19087 if (init == NULL_TREE)
19088 return false;
19090 STRIP_NOPS (init);
19091 switch (TREE_CODE (init))
19093 case STRING_CST:
19094 type = TREE_TYPE (init);
19095 if (TREE_CODE (type) == ARRAY_TYPE)
19097 tree enttype = TREE_TYPE (type);
19098 machine_mode mode = TYPE_MODE (enttype);
19100 if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
19101 return false;
19102 if (int_size_in_bytes (type) != size)
19103 return false;
19104 if (size > TREE_STRING_LENGTH (init))
19106 memcpy (array, TREE_STRING_POINTER (init),
19107 TREE_STRING_LENGTH (init));
19108 memset (array + TREE_STRING_LENGTH (init),
19109 '\0', size - TREE_STRING_LENGTH (init));
19111 else
19112 memcpy (array, TREE_STRING_POINTER (init), size);
19113 return true;
19115 return false;
19116 case CONSTRUCTOR:
19117 type = TREE_TYPE (init);
19118 if (int_size_in_bytes (type) != size)
19119 return false;
19120 if (TREE_CODE (type) == ARRAY_TYPE)
19122 HOST_WIDE_INT min_index;
19123 unsigned HOST_WIDE_INT cnt;
19124 int curpos = 0, fieldsize;
19125 constructor_elt *ce;
19127 if (TYPE_DOMAIN (type) == NULL_TREE
19128 || !tree_fits_shwi_p (TYPE_MIN_VALUE (TYPE_DOMAIN (type))))
19129 return false;
19131 fieldsize = int_size_in_bytes (TREE_TYPE (type));
19132 if (fieldsize <= 0)
19133 return false;
19135 min_index = tree_to_shwi (TYPE_MIN_VALUE (TYPE_DOMAIN (type)));
19136 memset (array, '\0', size);
19137 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
19139 tree val = ce->value;
19140 tree index = ce->index;
19141 int pos = curpos;
19142 if (index && TREE_CODE (index) == RANGE_EXPR)
19143 pos = (tree_to_shwi (TREE_OPERAND (index, 0)) - min_index)
19144 * fieldsize;
19145 else if (index)
19146 pos = (tree_to_shwi (index) - min_index) * fieldsize;
19148 if (val)
19150 STRIP_NOPS (val);
19151 if (!native_encode_initializer (val, array + pos, fieldsize))
19152 return false;
19154 curpos = pos + fieldsize;
19155 if (index && TREE_CODE (index) == RANGE_EXPR)
19157 int count = tree_to_shwi (TREE_OPERAND (index, 1))
19158 - tree_to_shwi (TREE_OPERAND (index, 0));
19159 while (count-- > 0)
19161 if (val)
19162 memcpy (array + curpos, array + pos, fieldsize);
19163 curpos += fieldsize;
19166 gcc_assert (curpos <= size);
19168 return true;
19170 else if (TREE_CODE (type) == RECORD_TYPE
19171 || TREE_CODE (type) == UNION_TYPE)
19173 tree field = NULL_TREE;
19174 unsigned HOST_WIDE_INT cnt;
19175 constructor_elt *ce;
19177 if (int_size_in_bytes (type) != size)
19178 return false;
19180 if (TREE_CODE (type) == RECORD_TYPE)
19181 field = TYPE_FIELDS (type);
19183 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
19185 tree val = ce->value;
19186 int pos, fieldsize;
19188 if (ce->index != 0)
19189 field = ce->index;
19191 if (val)
19192 STRIP_NOPS (val);
19194 if (field == NULL_TREE || DECL_BIT_FIELD (field))
19195 return false;
19197 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
19198 && TYPE_DOMAIN (TREE_TYPE (field))
19199 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
19200 return false;
19201 else if (DECL_SIZE_UNIT (field) == NULL_TREE
19202 || !tree_fits_shwi_p (DECL_SIZE_UNIT (field)))
19203 return false;
19204 fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
19205 pos = int_byte_position (field);
19206 gcc_assert (pos + fieldsize <= size);
19207 if (val && fieldsize != 0
19208 && !native_encode_initializer (val, array + pos, fieldsize))
19209 return false;
19211 return true;
19213 return false;
19214 case VIEW_CONVERT_EXPR:
19215 case NON_LVALUE_EXPR:
19216 return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
19217 default:
19218 return native_encode_expr (init, array, size) == size;
19222 /* Attach a DW_AT_const_value attribute to DIE. The value of the
19223 attribute is the const value T. */
19225 static bool
19226 tree_add_const_value_attribute (dw_die_ref die, tree t)
19228 tree init;
19229 tree type = TREE_TYPE (t);
19230 rtx rtl;
19232 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
19233 return false;
19235 init = t;
19236 gcc_assert (!DECL_P (init));
19238 if (! early_dwarf)
19240 rtl = rtl_for_decl_init (init, type);
19241 if (rtl)
19242 return add_const_value_attribute (die, rtl);
19244 /* If the host and target are sane, try harder. */
19245 if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
19246 && initializer_constant_valid_p (init, type))
19248 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
19249 if (size > 0 && (int) size == size)
19251 unsigned char *array = ggc_cleared_vec_alloc<unsigned char> (size);
19253 if (native_encode_initializer (init, array, size))
19255 add_AT_vec (die, DW_AT_const_value, size, 1, array);
19256 return true;
19258 ggc_free (array);
19261 return false;
19264 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
19265 attribute is the const value of T, where T is an integral constant
19266 variable with static storage duration
19267 (so it can't be a PARM_DECL or a RESULT_DECL). */
19269 static bool
19270 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
19273 if (!decl
19274 || (!VAR_P (decl) && TREE_CODE (decl) != CONST_DECL)
19275 || (VAR_P (decl) && !TREE_STATIC (decl)))
19276 return false;
19278 if (TREE_READONLY (decl)
19279 && ! TREE_THIS_VOLATILE (decl)
19280 && DECL_INITIAL (decl))
19281 /* OK */;
19282 else
19283 return false;
19285 /* Don't add DW_AT_const_value if abstract origin already has one. */
19286 if (get_AT (var_die, DW_AT_const_value))
19287 return false;
19289 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
19292 /* Convert the CFI instructions for the current function into a
19293 location list. This is used for DW_AT_frame_base when we targeting
19294 a dwarf2 consumer that does not support the dwarf3
19295 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
19296 expressions. */
19298 static dw_loc_list_ref
19299 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
19301 int ix;
19302 dw_fde_ref fde;
19303 dw_loc_list_ref list, *list_tail;
19304 dw_cfi_ref cfi;
19305 dw_cfa_location last_cfa, next_cfa;
19306 const char *start_label, *last_label, *section;
19307 dw_cfa_location remember;
19309 fde = cfun->fde;
19310 gcc_assert (fde != NULL);
19312 section = secname_for_decl (current_function_decl);
19313 list_tail = &list;
19314 list = NULL;
19316 memset (&next_cfa, 0, sizeof (next_cfa));
19317 next_cfa.reg = INVALID_REGNUM;
19318 remember = next_cfa;
19320 start_label = fde->dw_fde_begin;
19322 /* ??? Bald assumption that the CIE opcode list does not contain
19323 advance opcodes. */
19324 FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
19325 lookup_cfa_1 (cfi, &next_cfa, &remember);
19327 last_cfa = next_cfa;
19328 last_label = start_label;
19330 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
19332 /* If the first partition contained no CFI adjustments, the
19333 CIE opcodes apply to the whole first partition. */
19334 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19335 fde->dw_fde_begin, fde->dw_fde_end, section);
19336 list_tail =&(*list_tail)->dw_loc_next;
19337 start_label = last_label = fde->dw_fde_second_begin;
19340 FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
19342 switch (cfi->dw_cfi_opc)
19344 case DW_CFA_set_loc:
19345 case DW_CFA_advance_loc1:
19346 case DW_CFA_advance_loc2:
19347 case DW_CFA_advance_loc4:
19348 if (!cfa_equal_p (&last_cfa, &next_cfa))
19350 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19351 start_label, last_label, section);
19353 list_tail = &(*list_tail)->dw_loc_next;
19354 last_cfa = next_cfa;
19355 start_label = last_label;
19357 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
19358 break;
19360 case DW_CFA_advance_loc:
19361 /* The encoding is complex enough that we should never emit this. */
19362 gcc_unreachable ();
19364 default:
19365 lookup_cfa_1 (cfi, &next_cfa, &remember);
19366 break;
19368 if (ix + 1 == fde->dw_fde_switch_cfi_index)
19370 if (!cfa_equal_p (&last_cfa, &next_cfa))
19372 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19373 start_label, last_label, section);
19375 list_tail = &(*list_tail)->dw_loc_next;
19376 last_cfa = next_cfa;
19377 start_label = last_label;
19379 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19380 start_label, fde->dw_fde_end, section);
19381 list_tail = &(*list_tail)->dw_loc_next;
19382 start_label = last_label = fde->dw_fde_second_begin;
19386 if (!cfa_equal_p (&last_cfa, &next_cfa))
19388 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19389 start_label, last_label, section);
19390 list_tail = &(*list_tail)->dw_loc_next;
19391 start_label = last_label;
19394 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
19395 start_label,
19396 fde->dw_fde_second_begin
19397 ? fde->dw_fde_second_end : fde->dw_fde_end,
19398 section);
19400 if (list && list->dw_loc_next)
19401 gen_llsym (list);
19403 return list;
19406 /* Compute a displacement from the "steady-state frame pointer" to the
19407 frame base (often the same as the CFA), and store it in
19408 frame_pointer_fb_offset. OFFSET is added to the displacement
19409 before the latter is negated. */
19411 static void
19412 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
19414 rtx reg, elim;
19416 #ifdef FRAME_POINTER_CFA_OFFSET
19417 reg = frame_pointer_rtx;
19418 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
19419 #else
19420 reg = arg_pointer_rtx;
19421 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
19422 #endif
19424 elim = (ira_use_lra_p
19425 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
19426 : eliminate_regs (reg, VOIDmode, NULL_RTX));
19427 if (GET_CODE (elim) == PLUS)
19429 offset += INTVAL (XEXP (elim, 1));
19430 elim = XEXP (elim, 0);
19433 frame_pointer_fb_offset = -offset;
19435 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
19436 in which to eliminate. This is because it's stack pointer isn't
19437 directly accessible as a register within the ISA. To work around
19438 this, assume that while we cannot provide a proper value for
19439 frame_pointer_fb_offset, we won't need one either. */
19440 frame_pointer_fb_offset_valid
19441 = ((SUPPORTS_STACK_ALIGNMENT
19442 && (elim == hard_frame_pointer_rtx
19443 || elim == stack_pointer_rtx))
19444 || elim == (frame_pointer_needed
19445 ? hard_frame_pointer_rtx
19446 : stack_pointer_rtx));
19449 /* Generate a DW_AT_name attribute given some string value to be included as
19450 the value of the attribute. */
19452 static void
19453 add_name_attribute (dw_die_ref die, const char *name_string)
19455 if (name_string != NULL && *name_string != 0)
19457 if (demangle_name_func)
19458 name_string = (*demangle_name_func) (name_string);
19460 add_AT_string (die, DW_AT_name, name_string);
19464 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
19465 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
19466 of TYPE accordingly.
19468 ??? This is a temporary measure until after we're able to generate
19469 regular DWARF for the complex Ada type system. */
19471 static void
19472 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
19473 dw_die_ref context_die)
19475 tree dtype;
19476 dw_die_ref dtype_die;
19478 if (!lang_hooks.types.descriptive_type)
19479 return;
19481 dtype = lang_hooks.types.descriptive_type (type);
19482 if (!dtype)
19483 return;
19485 dtype_die = lookup_type_die (dtype);
19486 if (!dtype_die)
19488 gen_type_die (dtype, context_die);
19489 dtype_die = lookup_type_die (dtype);
19490 gcc_assert (dtype_die);
19493 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
19496 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir. */
19498 static const char *
19499 comp_dir_string (void)
19501 const char *wd;
19502 char *wd1;
19503 static const char *cached_wd = NULL;
19505 if (cached_wd != NULL)
19506 return cached_wd;
19508 wd = get_src_pwd ();
19509 if (wd == NULL)
19510 return NULL;
19512 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
19514 int wdlen;
19516 wdlen = strlen (wd);
19517 wd1 = ggc_vec_alloc<char> (wdlen + 2);
19518 strcpy (wd1, wd);
19519 wd1 [wdlen] = DIR_SEPARATOR;
19520 wd1 [wdlen + 1] = 0;
19521 wd = wd1;
19524 cached_wd = remap_debug_filename (wd);
19525 return cached_wd;
19528 /* Generate a DW_AT_comp_dir attribute for DIE. */
19530 static void
19531 add_comp_dir_attribute (dw_die_ref die)
19533 const char * wd = comp_dir_string ();
19534 if (wd != NULL)
19535 add_AT_string (die, DW_AT_comp_dir, wd);
19538 /* Given a tree node VALUE describing a scalar attribute ATTR (i.e. a bound, a
19539 pointer computation, ...), output a representation for that bound according
19540 to the accepted FORMS (see enum dw_scalar_form) and add it to DIE. See
19541 loc_list_from_tree for the meaning of CONTEXT. */
19543 static void
19544 add_scalar_info (dw_die_ref die, enum dwarf_attribute attr, tree value,
19545 int forms, struct loc_descr_context *context)
19547 dw_die_ref context_die, decl_die;
19548 dw_loc_list_ref list;
19549 bool strip_conversions = true;
19550 bool placeholder_seen = false;
19552 while (strip_conversions)
19553 switch (TREE_CODE (value))
19555 case ERROR_MARK:
19556 case SAVE_EXPR:
19557 return;
19559 CASE_CONVERT:
19560 case VIEW_CONVERT_EXPR:
19561 value = TREE_OPERAND (value, 0);
19562 break;
19564 default:
19565 strip_conversions = false;
19566 break;
19569 /* If possible and permitted, output the attribute as a constant. */
19570 if ((forms & dw_scalar_form_constant) != 0
19571 && TREE_CODE (value) == INTEGER_CST)
19573 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (value));
19575 /* If HOST_WIDE_INT is big enough then represent the bound as
19576 a constant value. We need to choose a form based on
19577 whether the type is signed or unsigned. We cannot just
19578 call add_AT_unsigned if the value itself is positive
19579 (add_AT_unsigned might add the unsigned value encoded as
19580 DW_FORM_data[1248]). Some DWARF consumers will lookup the
19581 bounds type and then sign extend any unsigned values found
19582 for signed types. This is needed only for
19583 DW_AT_{lower,upper}_bound, since for most other attributes,
19584 consumers will treat DW_FORM_data[1248] as unsigned values,
19585 regardless of the underlying type. */
19586 if (prec <= HOST_BITS_PER_WIDE_INT
19587 || tree_fits_uhwi_p (value))
19589 if (TYPE_UNSIGNED (TREE_TYPE (value)))
19590 add_AT_unsigned (die, attr, TREE_INT_CST_LOW (value));
19591 else
19592 add_AT_int (die, attr, TREE_INT_CST_LOW (value));
19594 else
19595 /* Otherwise represent the bound as an unsigned value with
19596 the precision of its type. The precision and signedness
19597 of the type will be necessary to re-interpret it
19598 unambiguously. */
19599 add_AT_wide (die, attr, value);
19600 return;
19603 /* Otherwise, if it's possible and permitted too, output a reference to
19604 another DIE. */
19605 if ((forms & dw_scalar_form_reference) != 0)
19607 tree decl = NULL_TREE;
19609 /* Some type attributes reference an outer type. For instance, the upper
19610 bound of an array may reference an embedding record (this happens in
19611 Ada). */
19612 if (TREE_CODE (value) == COMPONENT_REF
19613 && TREE_CODE (TREE_OPERAND (value, 0)) == PLACEHOLDER_EXPR
19614 && TREE_CODE (TREE_OPERAND (value, 1)) == FIELD_DECL)
19615 decl = TREE_OPERAND (value, 1);
19617 else if (VAR_P (value)
19618 || TREE_CODE (value) == PARM_DECL
19619 || TREE_CODE (value) == RESULT_DECL)
19620 decl = value;
19622 if (decl != NULL_TREE)
19624 dw_die_ref decl_die = lookup_decl_die (decl);
19626 /* ??? Can this happen, or should the variable have been bound
19627 first? Probably it can, since I imagine that we try to create
19628 the types of parameters in the order in which they exist in
19629 the list, and won't have created a forward reference to a
19630 later parameter. */
19631 if (decl_die != NULL)
19633 add_AT_die_ref (die, attr, decl_die);
19634 return;
19639 /* Last chance: try to create a stack operation procedure to evaluate the
19640 value. Do nothing if even that is not possible or permitted. */
19641 if ((forms & dw_scalar_form_exprloc) == 0)
19642 return;
19644 list = loc_list_from_tree (value, 2, context);
19645 if (context && context->placeholder_arg)
19647 placeholder_seen = context->placeholder_seen;
19648 context->placeholder_seen = false;
19650 if (list == NULL || single_element_loc_list_p (list))
19652 /* If this attribute is not a reference nor constant, it is
19653 a DWARF expression rather than location description. For that
19654 loc_list_from_tree (value, 0, &context) is needed. */
19655 dw_loc_list_ref list2 = loc_list_from_tree (value, 0, context);
19656 if (list2 && single_element_loc_list_p (list2))
19658 if (placeholder_seen)
19660 struct dwarf_procedure_info dpi;
19661 dpi.fndecl = NULL_TREE;
19662 dpi.args_count = 1;
19663 if (!resolve_args_picking (list2->expr, 1, &dpi))
19664 return;
19666 add_AT_loc (die, attr, list2->expr);
19667 return;
19671 /* If that failed to give a single element location list, fall back to
19672 outputting this as a reference... still if permitted. */
19673 if (list == NULL
19674 || (forms & dw_scalar_form_reference) == 0
19675 || placeholder_seen)
19676 return;
19678 if (current_function_decl == 0)
19679 context_die = comp_unit_die ();
19680 else
19681 context_die = lookup_decl_die (current_function_decl);
19683 decl_die = new_die (DW_TAG_variable, context_die, value);
19684 add_AT_flag (decl_die, DW_AT_artificial, 1);
19685 add_type_attribute (decl_die, TREE_TYPE (value), TYPE_QUAL_CONST, false,
19686 context_die);
19687 add_AT_location_description (decl_die, DW_AT_location, list);
19688 add_AT_die_ref (die, attr, decl_die);
19691 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
19692 default. */
19694 static int
19695 lower_bound_default (void)
19697 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
19699 case DW_LANG_C:
19700 case DW_LANG_C89:
19701 case DW_LANG_C99:
19702 case DW_LANG_C11:
19703 case DW_LANG_C_plus_plus:
19704 case DW_LANG_C_plus_plus_11:
19705 case DW_LANG_C_plus_plus_14:
19706 case DW_LANG_ObjC:
19707 case DW_LANG_ObjC_plus_plus:
19708 case DW_LANG_Java:
19709 return 0;
19710 case DW_LANG_Fortran77:
19711 case DW_LANG_Fortran90:
19712 case DW_LANG_Fortran95:
19713 case DW_LANG_Fortran03:
19714 case DW_LANG_Fortran08:
19715 return 1;
19716 case DW_LANG_UPC:
19717 case DW_LANG_D:
19718 case DW_LANG_Python:
19719 return dwarf_version >= 4 ? 0 : -1;
19720 case DW_LANG_Ada95:
19721 case DW_LANG_Ada83:
19722 case DW_LANG_Cobol74:
19723 case DW_LANG_Cobol85:
19724 case DW_LANG_Pascal83:
19725 case DW_LANG_Modula2:
19726 case DW_LANG_PLI:
19727 return dwarf_version >= 4 ? 1 : -1;
19728 default:
19729 return -1;
19733 /* Given a tree node describing an array bound (either lower or upper) output
19734 a representation for that bound. */
19736 static void
19737 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr,
19738 tree bound, struct loc_descr_context *context)
19740 int dflt;
19742 while (1)
19743 switch (TREE_CODE (bound))
19745 /* Strip all conversions. */
19746 CASE_CONVERT:
19747 case VIEW_CONVERT_EXPR:
19748 bound = TREE_OPERAND (bound, 0);
19749 break;
19751 /* All fixed-bounds are represented by INTEGER_CST nodes. Lower bounds
19752 are even omitted when they are the default. */
19753 case INTEGER_CST:
19754 /* If the value for this bound is the default one, we can even omit the
19755 attribute. */
19756 if (bound_attr == DW_AT_lower_bound
19757 && tree_fits_shwi_p (bound)
19758 && (dflt = lower_bound_default ()) != -1
19759 && tree_to_shwi (bound) == dflt)
19760 return;
19762 /* FALLTHRU */
19764 default:
19765 /* Because of the complex interaction there can be with other GNAT
19766 encodings, GDB isn't ready yet to handle proper DWARF description
19767 for self-referencial subrange bounds: let GNAT encodings do the
19768 magic in such a case. */
19769 if (is_ada ()
19770 && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL
19771 && contains_placeholder_p (bound))
19772 return;
19774 add_scalar_info (subrange_die, bound_attr, bound,
19775 dw_scalar_form_constant
19776 | dw_scalar_form_exprloc
19777 | dw_scalar_form_reference,
19778 context);
19779 return;
19783 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
19784 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
19785 Note that the block of subscript information for an array type also
19786 includes information about the element type of the given array type.
19788 This function reuses previously set type and bound information if
19789 available. */
19791 static void
19792 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
19794 unsigned dimension_number;
19795 tree lower, upper;
19796 dw_die_ref child = type_die->die_child;
19798 for (dimension_number = 0;
19799 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
19800 type = TREE_TYPE (type), dimension_number++)
19802 tree domain = TYPE_DOMAIN (type);
19804 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
19805 break;
19807 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
19808 and (in GNU C only) variable bounds. Handle all three forms
19809 here. */
19811 /* Find and reuse a previously generated DW_TAG_subrange_type if
19812 available.
19814 For multi-dimensional arrays, as we iterate through the
19815 various dimensions in the enclosing for loop above, we also
19816 iterate through the DIE children and pick at each
19817 DW_TAG_subrange_type previously generated (if available).
19818 Each child DW_TAG_subrange_type DIE describes the range of
19819 the current dimension. At this point we should have as many
19820 DW_TAG_subrange_type's as we have dimensions in the
19821 array. */
19822 dw_die_ref subrange_die = NULL;
19823 if (child)
19824 while (1)
19826 child = child->die_sib;
19827 if (child->die_tag == DW_TAG_subrange_type)
19828 subrange_die = child;
19829 if (child == type_die->die_child)
19831 /* If we wrapped around, stop looking next time. */
19832 child = NULL;
19833 break;
19835 if (child->die_tag == DW_TAG_subrange_type)
19836 break;
19838 if (!subrange_die)
19839 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
19841 if (domain)
19843 /* We have an array type with specified bounds. */
19844 lower = TYPE_MIN_VALUE (domain);
19845 upper = TYPE_MAX_VALUE (domain);
19847 /* Define the index type. */
19848 if (TREE_TYPE (domain)
19849 && !get_AT (subrange_die, DW_AT_type))
19851 /* ??? This is probably an Ada unnamed subrange type. Ignore the
19852 TREE_TYPE field. We can't emit debug info for this
19853 because it is an unnamed integral type. */
19854 if (TREE_CODE (domain) == INTEGER_TYPE
19855 && TYPE_NAME (domain) == NULL_TREE
19856 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
19857 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
19859 else
19860 add_type_attribute (subrange_die, TREE_TYPE (domain),
19861 TYPE_UNQUALIFIED, false, type_die);
19864 /* ??? If upper is NULL, the array has unspecified length,
19865 but it does have a lower bound. This happens with Fortran
19866 dimension arr(N:*)
19867 Since the debugger is definitely going to need to know N
19868 to produce useful results, go ahead and output the lower
19869 bound solo, and hope the debugger can cope. */
19871 if (!get_AT (subrange_die, DW_AT_lower_bound))
19872 add_bound_info (subrange_die, DW_AT_lower_bound, lower, NULL);
19873 if (upper && !get_AT (subrange_die, DW_AT_upper_bound))
19874 add_bound_info (subrange_die, DW_AT_upper_bound, upper, NULL);
19877 /* Otherwise we have an array type with an unspecified length. The
19878 DWARF-2 spec does not say how to handle this; let's just leave out the
19879 bounds. */
19883 /* Add a DW_AT_byte_size attribute to DIE with TREE_NODE's size. */
19885 static void
19886 add_byte_size_attribute (dw_die_ref die, tree tree_node)
19888 dw_die_ref decl_die;
19889 HOST_WIDE_INT size;
19890 dw_loc_descr_ref size_expr = NULL;
19892 switch (TREE_CODE (tree_node))
19894 case ERROR_MARK:
19895 size = 0;
19896 break;
19897 case ENUMERAL_TYPE:
19898 case RECORD_TYPE:
19899 case UNION_TYPE:
19900 case QUAL_UNION_TYPE:
19901 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
19902 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
19904 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
19905 return;
19907 size_expr = type_byte_size (tree_node, &size);
19908 break;
19909 case FIELD_DECL:
19910 /* For a data member of a struct or union, the DW_AT_byte_size is
19911 generally given as the number of bytes normally allocated for an
19912 object of the *declared* type of the member itself. This is true
19913 even for bit-fields. */
19914 size = int_size_in_bytes (field_type (tree_node));
19915 break;
19916 default:
19917 gcc_unreachable ();
19920 /* Support for dynamically-sized objects was introduced by DWARFv3.
19921 At the moment, GDB does not handle variable byte sizes very well,
19922 though. */
19923 if ((dwarf_version >= 3 || !dwarf_strict)
19924 && gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL
19925 && size_expr != NULL)
19926 add_AT_loc (die, DW_AT_byte_size, size_expr);
19928 /* Note that `size' might be -1 when we get to this point. If it is, that
19929 indicates that the byte size of the entity in question is variable and
19930 that we could not generate a DWARF expression that computes it. */
19931 if (size >= 0)
19932 add_AT_unsigned (die, DW_AT_byte_size, size);
19935 /* For a FIELD_DECL node which represents a bit-field, output an attribute
19936 which specifies the distance in bits from the highest order bit of the
19937 "containing object" for the bit-field to the highest order bit of the
19938 bit-field itself.
19940 For any given bit-field, the "containing object" is a hypothetical object
19941 (of some integral or enum type) within which the given bit-field lives. The
19942 type of this hypothetical "containing object" is always the same as the
19943 declared type of the individual bit-field itself. The determination of the
19944 exact location of the "containing object" for a bit-field is rather
19945 complicated. It's handled by the `field_byte_offset' function (above).
19947 CTX is required: see the comment for VLR_CONTEXT.
19949 Note that it is the size (in bytes) of the hypothetical "containing object"
19950 which will be given in the DW_AT_byte_size attribute for this bit-field.
19951 (See `byte_size_attribute' above). */
19953 static inline void
19954 add_bit_offset_attribute (dw_die_ref die, tree decl, struct vlr_context *ctx)
19956 HOST_WIDE_INT object_offset_in_bytes;
19957 tree original_type = DECL_BIT_FIELD_TYPE (decl);
19958 HOST_WIDE_INT bitpos_int;
19959 HOST_WIDE_INT highest_order_object_bit_offset;
19960 HOST_WIDE_INT highest_order_field_bit_offset;
19961 HOST_WIDE_INT bit_offset;
19963 field_byte_offset (decl, ctx, &object_offset_in_bytes);
19965 /* Must be a field and a bit field. */
19966 gcc_assert (original_type && TREE_CODE (decl) == FIELD_DECL);
19968 /* We can't yet handle bit-fields whose offsets are variable, so if we
19969 encounter such things, just return without generating any attribute
19970 whatsoever. Likewise for variable or too large size. */
19971 if (! tree_fits_shwi_p (bit_position (decl))
19972 || ! tree_fits_uhwi_p (DECL_SIZE (decl)))
19973 return;
19975 bitpos_int = int_bit_position (decl);
19977 /* Note that the bit offset is always the distance (in bits) from the
19978 highest-order bit of the "containing object" to the highest-order bit of
19979 the bit-field itself. Since the "high-order end" of any object or field
19980 is different on big-endian and little-endian machines, the computation
19981 below must take account of these differences. */
19982 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
19983 highest_order_field_bit_offset = bitpos_int;
19985 if (! BYTES_BIG_ENDIAN)
19987 highest_order_field_bit_offset += tree_to_shwi (DECL_SIZE (decl));
19988 highest_order_object_bit_offset +=
19989 simple_type_size_in_bits (original_type);
19992 bit_offset
19993 = (! BYTES_BIG_ENDIAN
19994 ? highest_order_object_bit_offset - highest_order_field_bit_offset
19995 : highest_order_field_bit_offset - highest_order_object_bit_offset);
19997 if (bit_offset < 0)
19998 add_AT_int (die, DW_AT_bit_offset, bit_offset);
19999 else
20000 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
20003 /* For a FIELD_DECL node which represents a bit field, output an attribute
20004 which specifies the length in bits of the given field. */
20006 static inline void
20007 add_bit_size_attribute (dw_die_ref die, tree decl)
20009 /* Must be a field and a bit field. */
20010 gcc_assert (TREE_CODE (decl) == FIELD_DECL
20011 && DECL_BIT_FIELD_TYPE (decl));
20013 if (tree_fits_uhwi_p (DECL_SIZE (decl)))
20014 add_AT_unsigned (die, DW_AT_bit_size, tree_to_uhwi (DECL_SIZE (decl)));
20017 /* If the compiled language is ANSI C, then add a 'prototyped'
20018 attribute, if arg types are given for the parameters of a function. */
20020 static inline void
20021 add_prototyped_attribute (dw_die_ref die, tree func_type)
20023 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
20025 case DW_LANG_C:
20026 case DW_LANG_C89:
20027 case DW_LANG_C99:
20028 case DW_LANG_C11:
20029 case DW_LANG_ObjC:
20030 if (prototype_p (func_type))
20031 add_AT_flag (die, DW_AT_prototyped, 1);
20032 break;
20033 default:
20034 break;
20038 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
20039 by looking in the type declaration, the object declaration equate table or
20040 the block mapping. */
20042 static inline dw_die_ref
20043 add_abstract_origin_attribute (dw_die_ref die, tree origin)
20045 dw_die_ref origin_die = NULL;
20047 if (TREE_CODE (origin) != FUNCTION_DECL
20048 && TREE_CODE (origin) != BLOCK)
20050 /* We may have gotten separated from the block for the inlined
20051 function, if we're in an exception handler or some such; make
20052 sure that the abstract function has been written out.
20054 Doing this for nested functions is wrong, however; functions are
20055 distinct units, and our context might not even be inline. */
20056 tree fn = origin;
20058 if (TYPE_P (fn))
20059 fn = TYPE_STUB_DECL (fn);
20061 fn = decl_function_context (fn);
20062 if (fn)
20063 dwarf2out_abstract_function (fn);
20066 if (DECL_P (origin))
20067 origin_die = lookup_decl_die (origin);
20068 else if (TYPE_P (origin))
20069 origin_die = lookup_type_die (origin);
20070 else if (TREE_CODE (origin) == BLOCK)
20071 origin_die = BLOCK_DIE (origin);
20073 /* XXX: Functions that are never lowered don't always have correct block
20074 trees (in the case of java, they simply have no block tree, in some other
20075 languages). For these functions, there is nothing we can really do to
20076 output correct debug info for inlined functions in all cases. Rather
20077 than die, we'll just produce deficient debug info now, in that we will
20078 have variables without a proper abstract origin. In the future, when all
20079 functions are lowered, we should re-add a gcc_assert (origin_die)
20080 here. */
20082 if (origin_die)
20083 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
20084 return origin_die;
20087 /* We do not currently support the pure_virtual attribute. */
20089 static inline void
20090 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
20092 if (DECL_VINDEX (func_decl))
20094 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
20096 if (tree_fits_shwi_p (DECL_VINDEX (func_decl)))
20097 add_AT_loc (die, DW_AT_vtable_elem_location,
20098 new_loc_descr (DW_OP_constu,
20099 tree_to_shwi (DECL_VINDEX (func_decl)),
20100 0));
20102 /* GNU extension: Record what type this method came from originally. */
20103 if (debug_info_level > DINFO_LEVEL_TERSE
20104 && DECL_CONTEXT (func_decl))
20105 add_AT_die_ref (die, DW_AT_containing_type,
20106 lookup_type_die (DECL_CONTEXT (func_decl)));
20110 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
20111 given decl. This used to be a vendor extension until after DWARF 4
20112 standardized it. */
20114 static void
20115 add_linkage_attr (dw_die_ref die, tree decl)
20117 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
20119 /* Mimic what assemble_name_raw does with a leading '*'. */
20120 if (name[0] == '*')
20121 name = &name[1];
20123 if (dwarf_version >= 4)
20124 add_AT_string (die, DW_AT_linkage_name, name);
20125 else
20126 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
20129 /* Add source coordinate attributes for the given decl. */
20131 static void
20132 add_src_coords_attributes (dw_die_ref die, tree decl)
20134 expanded_location s;
20136 if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
20137 return;
20138 s = expand_location (DECL_SOURCE_LOCATION (decl));
20139 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
20140 add_AT_unsigned (die, DW_AT_decl_line, s.line);
20143 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
20145 static void
20146 add_linkage_name_raw (dw_die_ref die, tree decl)
20148 /* Defer until we have an assembler name set. */
20149 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
20151 limbo_die_node *asm_name;
20153 asm_name = ggc_cleared_alloc<limbo_die_node> ();
20154 asm_name->die = die;
20155 asm_name->created_for = decl;
20156 asm_name->next = deferred_asm_name;
20157 deferred_asm_name = asm_name;
20159 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
20160 add_linkage_attr (die, decl);
20163 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl if desired. */
20165 static void
20166 add_linkage_name (dw_die_ref die, tree decl)
20168 if (debug_info_level > DINFO_LEVEL_NONE
20169 && VAR_OR_FUNCTION_DECL_P (decl)
20170 && TREE_PUBLIC (decl)
20171 && !(VAR_P (decl) && DECL_REGISTER (decl))
20172 && die->die_tag != DW_TAG_member)
20173 add_linkage_name_raw (die, decl);
20176 /* Add a DW_AT_name attribute and source coordinate attribute for the
20177 given decl, but only if it actually has a name. */
20179 static void
20180 add_name_and_src_coords_attributes (dw_die_ref die, tree decl,
20181 bool no_linkage_name)
20183 tree decl_name;
20185 decl_name = DECL_NAME (decl);
20186 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
20188 const char *name = dwarf2_name (decl, 0);
20189 if (name)
20190 add_name_attribute (die, name);
20191 if (! DECL_ARTIFICIAL (decl))
20192 add_src_coords_attributes (die, decl);
20194 if (!no_linkage_name)
20195 add_linkage_name (die, decl);
20198 #ifdef VMS_DEBUGGING_INFO
20199 /* Get the function's name, as described by its RTL. This may be different
20200 from the DECL_NAME name used in the source file. */
20201 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
20203 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
20204 XEXP (DECL_RTL (decl), 0), false);
20205 vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
20207 #endif /* VMS_DEBUGGING_INFO */
20210 /* Add VALUE as a DW_AT_discr_value attribute to DIE. */
20212 static void
20213 add_discr_value (dw_die_ref die, dw_discr_value *value)
20215 dw_attr_node attr;
20217 attr.dw_attr = DW_AT_discr_value;
20218 attr.dw_attr_val.val_class = dw_val_class_discr_value;
20219 attr.dw_attr_val.val_entry = NULL;
20220 attr.dw_attr_val.v.val_discr_value.pos = value->pos;
20221 if (value->pos)
20222 attr.dw_attr_val.v.val_discr_value.v.uval = value->v.uval;
20223 else
20224 attr.dw_attr_val.v.val_discr_value.v.sval = value->v.sval;
20225 add_dwarf_attr (die, &attr);
20228 /* Add DISCR_LIST as a DW_AT_discr_list to DIE. */
20230 static void
20231 add_discr_list (dw_die_ref die, dw_discr_list_ref discr_list)
20233 dw_attr_node attr;
20235 attr.dw_attr = DW_AT_discr_list;
20236 attr.dw_attr_val.val_class = dw_val_class_discr_list;
20237 attr.dw_attr_val.val_entry = NULL;
20238 attr.dw_attr_val.v.val_discr_list = discr_list;
20239 add_dwarf_attr (die, &attr);
20242 static inline dw_discr_list_ref
20243 AT_discr_list (dw_attr_node *attr)
20245 return attr->dw_attr_val.v.val_discr_list;
20248 #ifdef VMS_DEBUGGING_INFO
20249 /* Output the debug main pointer die for VMS */
20251 void
20252 dwarf2out_vms_debug_main_pointer (void)
20254 char label[MAX_ARTIFICIAL_LABEL_BYTES];
20255 dw_die_ref die;
20257 /* Allocate the VMS debug main subprogram die. */
20258 die = ggc_cleared_alloc<die_node> ();
20259 die->die_tag = DW_TAG_subprogram;
20260 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
20261 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
20262 current_function_funcdef_no);
20263 add_AT_lbl_id (die, DW_AT_entry_pc, label);
20265 /* Make it the first child of comp_unit_die (). */
20266 die->die_parent = comp_unit_die ();
20267 if (comp_unit_die ()->die_child)
20269 die->die_sib = comp_unit_die ()->die_child->die_sib;
20270 comp_unit_die ()->die_child->die_sib = die;
20272 else
20274 die->die_sib = die;
20275 comp_unit_die ()->die_child = die;
20278 #endif /* VMS_DEBUGGING_INFO */
20280 /* Push a new declaration scope. */
20282 static void
20283 push_decl_scope (tree scope)
20285 vec_safe_push (decl_scope_table, scope);
20288 /* Pop a declaration scope. */
20290 static inline void
20291 pop_decl_scope (void)
20293 decl_scope_table->pop ();
20296 /* walk_tree helper function for uses_local_type, below. */
20298 static tree
20299 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
20301 if (!TYPE_P (*tp))
20302 *walk_subtrees = 0;
20303 else
20305 tree name = TYPE_NAME (*tp);
20306 if (name && DECL_P (name) && decl_function_context (name))
20307 return *tp;
20309 return NULL_TREE;
20312 /* If TYPE involves a function-local type (including a local typedef to a
20313 non-local type), returns that type; otherwise returns NULL_TREE. */
20315 static tree
20316 uses_local_type (tree type)
20318 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
20319 return used;
20322 /* Return the DIE for the scope that immediately contains this type.
20323 Non-named types that do not involve a function-local type get global
20324 scope. Named types nested in namespaces or other types get their
20325 containing scope. All other types (i.e. function-local named types) get
20326 the current active scope. */
20328 static dw_die_ref
20329 scope_die_for (tree t, dw_die_ref context_die)
20331 dw_die_ref scope_die = NULL;
20332 tree containing_scope;
20334 /* Non-types always go in the current scope. */
20335 gcc_assert (TYPE_P (t));
20337 /* Use the scope of the typedef, rather than the scope of the type
20338 it refers to. */
20339 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
20340 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
20341 else
20342 containing_scope = TYPE_CONTEXT (t);
20344 /* Use the containing namespace if there is one. */
20345 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
20347 if (context_die == lookup_decl_die (containing_scope))
20348 /* OK */;
20349 else if (debug_info_level > DINFO_LEVEL_TERSE)
20350 context_die = get_context_die (containing_scope);
20351 else
20352 containing_scope = NULL_TREE;
20355 /* Ignore function type "scopes" from the C frontend. They mean that
20356 a tagged type is local to a parmlist of a function declarator, but
20357 that isn't useful to DWARF. */
20358 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
20359 containing_scope = NULL_TREE;
20361 if (SCOPE_FILE_SCOPE_P (containing_scope))
20363 /* If T uses a local type keep it local as well, to avoid references
20364 to function-local DIEs from outside the function. */
20365 if (current_function_decl && uses_local_type (t))
20366 scope_die = context_die;
20367 else
20368 scope_die = comp_unit_die ();
20370 else if (TYPE_P (containing_scope))
20372 /* For types, we can just look up the appropriate DIE. */
20373 if (debug_info_level > DINFO_LEVEL_TERSE)
20374 scope_die = get_context_die (containing_scope);
20375 else
20377 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
20378 if (scope_die == NULL)
20379 scope_die = comp_unit_die ();
20382 else
20383 scope_die = context_die;
20385 return scope_die;
20388 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
20390 static inline int
20391 local_scope_p (dw_die_ref context_die)
20393 for (; context_die; context_die = context_die->die_parent)
20394 if (context_die->die_tag == DW_TAG_inlined_subroutine
20395 || context_die->die_tag == DW_TAG_subprogram)
20396 return 1;
20398 return 0;
20401 /* Returns nonzero if CONTEXT_DIE is a class. */
20403 static inline int
20404 class_scope_p (dw_die_ref context_die)
20406 return (context_die
20407 && (context_die->die_tag == DW_TAG_structure_type
20408 || context_die->die_tag == DW_TAG_class_type
20409 || context_die->die_tag == DW_TAG_interface_type
20410 || context_die->die_tag == DW_TAG_union_type));
20413 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
20414 whether or not to treat a DIE in this context as a declaration. */
20416 static inline int
20417 class_or_namespace_scope_p (dw_die_ref context_die)
20419 return (class_scope_p (context_die)
20420 || (context_die && context_die->die_tag == DW_TAG_namespace));
20423 /* Many forms of DIEs require a "type description" attribute. This
20424 routine locates the proper "type descriptor" die for the type given
20425 by 'type' plus any additional qualifiers given by 'cv_quals', and
20426 adds a DW_AT_type attribute below the given die. */
20428 static void
20429 add_type_attribute (dw_die_ref object_die, tree type, int cv_quals,
20430 bool reverse, dw_die_ref context_die)
20432 enum tree_code code = TREE_CODE (type);
20433 dw_die_ref type_die = NULL;
20435 /* ??? If this type is an unnamed subrange type of an integral, floating-point
20436 or fixed-point type, use the inner type. This is because we have no
20437 support for unnamed types in base_type_die. This can happen if this is
20438 an Ada subrange type. Correct solution is emit a subrange type die. */
20439 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
20440 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
20441 type = TREE_TYPE (type), code = TREE_CODE (type);
20443 if (code == ERROR_MARK
20444 /* Handle a special case. For functions whose return type is void, we
20445 generate *no* type attribute. (Note that no object may have type
20446 `void', so this only applies to function return types). */
20447 || code == VOID_TYPE)
20448 return;
20450 type_die = modified_type_die (type,
20451 cv_quals | TYPE_QUALS_NO_ADDR_SPACE (type),
20452 reverse,
20453 context_die);
20455 if (type_die != NULL)
20456 add_AT_die_ref (object_die, DW_AT_type, type_die);
20459 /* Given an object die, add the calling convention attribute for the
20460 function call type. */
20461 static void
20462 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
20464 enum dwarf_calling_convention value = DW_CC_normal;
20466 value = ((enum dwarf_calling_convention)
20467 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
20469 if (is_fortran ()
20470 && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
20472 /* DWARF 2 doesn't provide a way to identify a program's source-level
20473 entry point. DW_AT_calling_convention attributes are only meant
20474 to describe functions' calling conventions. However, lacking a
20475 better way to signal the Fortran main program, we used this for
20476 a long time, following existing custom. Now, DWARF 4 has
20477 DW_AT_main_subprogram, which we add below, but some tools still
20478 rely on the old way, which we thus keep. */
20479 value = DW_CC_program;
20481 if (dwarf_version >= 4 || !dwarf_strict)
20482 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
20485 /* Only add the attribute if the backend requests it, and
20486 is not DW_CC_normal. */
20487 if (value && (value != DW_CC_normal))
20488 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
20491 /* Given a tree pointer to a struct, class, union, or enum type node, return
20492 a pointer to the (string) tag name for the given type, or zero if the type
20493 was declared without a tag. */
20495 static const char *
20496 type_tag (const_tree type)
20498 const char *name = 0;
20500 if (TYPE_NAME (type) != 0)
20502 tree t = 0;
20504 /* Find the IDENTIFIER_NODE for the type name. */
20505 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
20506 && !TYPE_NAMELESS (type))
20507 t = TYPE_NAME (type);
20509 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
20510 a TYPE_DECL node, regardless of whether or not a `typedef' was
20511 involved. */
20512 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
20513 && ! DECL_IGNORED_P (TYPE_NAME (type)))
20515 /* We want to be extra verbose. Don't call dwarf_name if
20516 DECL_NAME isn't set. The default hook for decl_printable_name
20517 doesn't like that, and in this context it's correct to return
20518 0, instead of "<anonymous>" or the like. */
20519 if (DECL_NAME (TYPE_NAME (type))
20520 && !DECL_NAMELESS (TYPE_NAME (type)))
20521 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
20524 /* Now get the name as a string, or invent one. */
20525 if (!name && t != 0)
20526 name = IDENTIFIER_POINTER (t);
20529 return (name == 0 || *name == '\0') ? 0 : name;
20532 /* Return the type associated with a data member, make a special check
20533 for bit field types. */
20535 static inline tree
20536 member_declared_type (const_tree member)
20538 return (DECL_BIT_FIELD_TYPE (member)
20539 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
20542 /* Get the decl's label, as described by its RTL. This may be different
20543 from the DECL_NAME name used in the source file. */
20545 #if 0
20546 static const char *
20547 decl_start_label (tree decl)
20549 rtx x;
20550 const char *fnname;
20552 x = DECL_RTL (decl);
20553 gcc_assert (MEM_P (x));
20555 x = XEXP (x, 0);
20556 gcc_assert (GET_CODE (x) == SYMBOL_REF);
20558 fnname = XSTR (x, 0);
20559 return fnname;
20561 #endif
20563 /* For variable-length arrays that have been previously generated, but
20564 may be incomplete due to missing subscript info, fill the subscript
20565 info. Return TRUE if this is one of those cases. */
20566 static bool
20567 fill_variable_array_bounds (tree type)
20569 if (TREE_ASM_WRITTEN (type)
20570 && TREE_CODE (type) == ARRAY_TYPE
20571 && variably_modified_type_p (type, NULL))
20573 dw_die_ref array_die = lookup_type_die (type);
20574 if (!array_die)
20575 return false;
20576 add_subscript_info (array_die, type, !is_ada ());
20577 return true;
20579 return false;
20582 /* These routines generate the internal representation of the DIE's for
20583 the compilation unit. Debugging information is collected by walking
20584 the declaration trees passed in from dwarf2out_decl(). */
20586 static void
20587 gen_array_type_die (tree type, dw_die_ref context_die)
20589 dw_die_ref array_die;
20591 /* GNU compilers represent multidimensional array types as sequences of one
20592 dimensional array types whose element types are themselves array types.
20593 We sometimes squish that down to a single array_type DIE with multiple
20594 subscripts in the Dwarf debugging info. The draft Dwarf specification
20595 say that we are allowed to do this kind of compression in C, because
20596 there is no difference between an array of arrays and a multidimensional
20597 array. We don't do this for Ada to remain as close as possible to the
20598 actual representation, which is especially important against the language
20599 flexibilty wrt arrays of variable size. */
20601 bool collapse_nested_arrays = !is_ada ();
20603 if (fill_variable_array_bounds (type))
20604 return;
20606 dw_die_ref scope_die = scope_die_for (type, context_die);
20607 tree element_type;
20609 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
20610 DW_TAG_string_type doesn't have DW_AT_type attribute). */
20611 if (TYPE_STRING_FLAG (type)
20612 && TREE_CODE (type) == ARRAY_TYPE
20613 && is_fortran ()
20614 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
20616 HOST_WIDE_INT size;
20618 array_die = new_die (DW_TAG_string_type, scope_die, type);
20619 add_name_attribute (array_die, type_tag (type));
20620 equate_type_number_to_die (type, array_die);
20621 size = int_size_in_bytes (type);
20622 if (size >= 0)
20623 add_AT_unsigned (array_die, DW_AT_byte_size, size);
20624 else if (TYPE_DOMAIN (type) != NULL_TREE
20625 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE)
20627 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
20628 tree rszdecl = szdecl;
20629 HOST_WIDE_INT rsize = 0;
20631 size = int_size_in_bytes (TREE_TYPE (szdecl));
20632 if (!DECL_P (szdecl))
20634 if (TREE_CODE (szdecl) == INDIRECT_REF
20635 && DECL_P (TREE_OPERAND (szdecl, 0)))
20637 rszdecl = TREE_OPERAND (szdecl, 0);
20638 rsize = int_size_in_bytes (TREE_TYPE (rszdecl));
20639 if (rsize <= 0)
20640 size = 0;
20642 else
20643 size = 0;
20645 if (size > 0)
20647 dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2, NULL);
20648 if (loc == NULL
20649 && early_dwarf
20650 && current_function_decl
20651 && DECL_CONTEXT (rszdecl) == current_function_decl)
20653 dw_die_ref ref = lookup_decl_die (rszdecl);
20654 dw_loc_descr_ref l = NULL;
20655 if (ref)
20657 l = new_loc_descr (DW_OP_call4, 0, 0);
20658 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
20659 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
20660 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
20662 else if (TREE_CODE (rszdecl) == PARM_DECL
20663 && string_types)
20665 l = new_loc_descr (DW_OP_call4, 0, 0);
20666 l->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
20667 l->dw_loc_oprnd1.v.val_decl_ref = rszdecl;
20668 string_types->safe_push (array_die);
20670 if (l && rszdecl != szdecl)
20672 if (rsize == DWARF2_ADDR_SIZE)
20673 add_loc_descr (&l, new_loc_descr (DW_OP_deref,
20674 0, 0));
20675 else
20676 add_loc_descr (&l, new_loc_descr (DW_OP_deref_size,
20677 rsize, 0));
20679 if (l)
20680 loc = new_loc_list (l, NULL, NULL, NULL);
20682 if (loc)
20684 add_AT_location_description (array_die, DW_AT_string_length,
20685 loc);
20686 if (size != DWARF2_ADDR_SIZE)
20687 add_AT_unsigned (array_die, dwarf_version >= 5
20688 ? DW_AT_string_length_byte_size
20689 : DW_AT_byte_size, size);
20693 return;
20696 array_die = new_die (DW_TAG_array_type, scope_die, type);
20697 add_name_attribute (array_die, type_tag (type));
20698 equate_type_number_to_die (type, array_die);
20700 if (TREE_CODE (type) == VECTOR_TYPE)
20701 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
20703 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
20704 if (is_fortran ()
20705 && TREE_CODE (type) == ARRAY_TYPE
20706 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
20707 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
20708 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
20710 #if 0
20711 /* We default the array ordering. SDB will probably do
20712 the right things even if DW_AT_ordering is not present. It's not even
20713 an issue until we start to get into multidimensional arrays anyway. If
20714 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
20715 then we'll have to put the DW_AT_ordering attribute back in. (But if
20716 and when we find out that we need to put these in, we will only do so
20717 for multidimensional arrays. */
20718 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
20719 #endif
20721 if (TREE_CODE (type) == VECTOR_TYPE)
20723 /* For VECTOR_TYPEs we use an array die with appropriate bounds. */
20724 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
20725 add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node, NULL);
20726 add_bound_info (subrange_die, DW_AT_upper_bound,
20727 size_int (TYPE_VECTOR_SUBPARTS (type) - 1), NULL);
20729 else
20730 add_subscript_info (array_die, type, collapse_nested_arrays);
20732 /* Add representation of the type of the elements of this array type and
20733 emit the corresponding DIE if we haven't done it already. */
20734 element_type = TREE_TYPE (type);
20735 if (collapse_nested_arrays)
20736 while (TREE_CODE (element_type) == ARRAY_TYPE)
20738 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
20739 break;
20740 element_type = TREE_TYPE (element_type);
20743 add_type_attribute (array_die, element_type, TYPE_UNQUALIFIED,
20744 TREE_CODE (type) == ARRAY_TYPE
20745 && TYPE_REVERSE_STORAGE_ORDER (type),
20746 context_die);
20748 add_gnat_descriptive_type_attribute (array_die, type, context_die);
20749 if (TYPE_ARTIFICIAL (type))
20750 add_AT_flag (array_die, DW_AT_artificial, 1);
20752 if (get_AT (array_die, DW_AT_name))
20753 add_pubtype (type, array_die);
20756 /* After all arguments are created, adjust any DW_TAG_string_type
20757 DIEs DW_AT_string_length attributes. */
20759 static void
20760 adjust_string_types (void)
20762 dw_die_ref array_die;
20763 unsigned int i;
20764 FOR_EACH_VEC_ELT (*string_types, i, array_die)
20766 dw_attr_node *a = get_AT (array_die, DW_AT_string_length);
20767 if (a == NULL)
20768 continue;
20769 dw_loc_descr_ref loc = AT_loc (a);
20770 gcc_assert (loc->dw_loc_opc == DW_OP_call4
20771 && loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref);
20772 dw_die_ref ref = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
20773 if (ref)
20775 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
20776 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
20777 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
20779 else
20781 remove_AT (array_die, DW_AT_string_length);
20782 remove_AT (array_die, dwarf_version >= 5
20783 ? DW_AT_string_length_byte_size
20784 : DW_AT_byte_size);
20789 /* This routine generates DIE for array with hidden descriptor, details
20790 are filled into *info by a langhook. */
20792 static void
20793 gen_descr_array_type_die (tree type, struct array_descr_info *info,
20794 dw_die_ref context_die)
20796 const dw_die_ref scope_die = scope_die_for (type, context_die);
20797 const dw_die_ref array_die = new_die (DW_TAG_array_type, scope_die, type);
20798 struct loc_descr_context context = { type, info->base_decl, NULL,
20799 false, false };
20800 enum dwarf_tag subrange_tag = DW_TAG_subrange_type;
20801 int dim;
20803 add_name_attribute (array_die, type_tag (type));
20804 equate_type_number_to_die (type, array_die);
20806 if (info->ndimensions > 1)
20807 switch (info->ordering)
20809 case array_descr_ordering_row_major:
20810 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
20811 break;
20812 case array_descr_ordering_column_major:
20813 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
20814 break;
20815 default:
20816 break;
20819 if (dwarf_version >= 3 || !dwarf_strict)
20821 if (info->data_location)
20822 add_scalar_info (array_die, DW_AT_data_location, info->data_location,
20823 dw_scalar_form_exprloc, &context);
20824 if (info->associated)
20825 add_scalar_info (array_die, DW_AT_associated, info->associated,
20826 dw_scalar_form_constant
20827 | dw_scalar_form_exprloc
20828 | dw_scalar_form_reference, &context);
20829 if (info->allocated)
20830 add_scalar_info (array_die, DW_AT_allocated, info->allocated,
20831 dw_scalar_form_constant
20832 | dw_scalar_form_exprloc
20833 | dw_scalar_form_reference, &context);
20834 if (info->stride)
20836 const enum dwarf_attribute attr
20837 = (info->stride_in_bits) ? DW_AT_bit_stride : DW_AT_byte_stride;
20838 const int forms
20839 = (info->stride_in_bits)
20840 ? dw_scalar_form_constant
20841 : (dw_scalar_form_constant
20842 | dw_scalar_form_exprloc
20843 | dw_scalar_form_reference);
20845 add_scalar_info (array_die, attr, info->stride, forms, &context);
20848 if (dwarf_version >= 5)
20850 if (info->rank)
20852 add_scalar_info (array_die, DW_AT_rank, info->rank,
20853 dw_scalar_form_constant
20854 | dw_scalar_form_exprloc, &context);
20855 subrange_tag = DW_TAG_generic_subrange;
20856 context.placeholder_arg = true;
20860 add_gnat_descriptive_type_attribute (array_die, type, context_die);
20862 for (dim = 0; dim < info->ndimensions; dim++)
20864 dw_die_ref subrange_die = new_die (subrange_tag, array_die, NULL);
20866 if (info->dimen[dim].bounds_type)
20867 add_type_attribute (subrange_die,
20868 info->dimen[dim].bounds_type, TYPE_UNQUALIFIED,
20869 false, context_die);
20870 if (info->dimen[dim].lower_bound)
20871 add_bound_info (subrange_die, DW_AT_lower_bound,
20872 info->dimen[dim].lower_bound, &context);
20873 if (info->dimen[dim].upper_bound)
20874 add_bound_info (subrange_die, DW_AT_upper_bound,
20875 info->dimen[dim].upper_bound, &context);
20876 if ((dwarf_version >= 3 || !dwarf_strict) && info->dimen[dim].stride)
20877 add_scalar_info (subrange_die, DW_AT_byte_stride,
20878 info->dimen[dim].stride,
20879 dw_scalar_form_constant
20880 | dw_scalar_form_exprloc
20881 | dw_scalar_form_reference,
20882 &context);
20885 gen_type_die (info->element_type, context_die);
20886 add_type_attribute (array_die, info->element_type, TYPE_UNQUALIFIED,
20887 TREE_CODE (type) == ARRAY_TYPE
20888 && TYPE_REVERSE_STORAGE_ORDER (type),
20889 context_die);
20891 if (get_AT (array_die, DW_AT_name))
20892 add_pubtype (type, array_die);
20895 #if 0
20896 static void
20897 gen_entry_point_die (tree decl, dw_die_ref context_die)
20899 tree origin = decl_ultimate_origin (decl);
20900 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
20902 if (origin != NULL)
20903 add_abstract_origin_attribute (decl_die, origin);
20904 else
20906 add_name_and_src_coords_attributes (decl_die, decl);
20907 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
20908 TYPE_UNQUALIFIED, false, context_die);
20911 if (DECL_ABSTRACT_P (decl))
20912 equate_decl_number_to_die (decl, decl_die);
20913 else
20914 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
20916 #endif
20918 /* Walk through the list of incomplete types again, trying once more to
20919 emit full debugging info for them. */
20921 static void
20922 retry_incomplete_types (void)
20924 set_early_dwarf s;
20925 int i;
20927 for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
20928 if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
20929 gen_type_die ((*incomplete_types)[i], comp_unit_die ());
20930 vec_safe_truncate (incomplete_types, 0);
20933 /* Determine what tag to use for a record type. */
20935 static enum dwarf_tag
20936 record_type_tag (tree type)
20938 if (! lang_hooks.types.classify_record)
20939 return DW_TAG_structure_type;
20941 switch (lang_hooks.types.classify_record (type))
20943 case RECORD_IS_STRUCT:
20944 return DW_TAG_structure_type;
20946 case RECORD_IS_CLASS:
20947 return DW_TAG_class_type;
20949 case RECORD_IS_INTERFACE:
20950 if (dwarf_version >= 3 || !dwarf_strict)
20951 return DW_TAG_interface_type;
20952 return DW_TAG_structure_type;
20954 default:
20955 gcc_unreachable ();
20959 /* Generate a DIE to represent an enumeration type. Note that these DIEs
20960 include all of the information about the enumeration values also. Each
20961 enumerated type name/value is listed as a child of the enumerated type
20962 DIE. */
20964 static dw_die_ref
20965 gen_enumeration_type_die (tree type, dw_die_ref context_die)
20967 dw_die_ref type_die = lookup_type_die (type);
20969 if (type_die == NULL)
20971 type_die = new_die (DW_TAG_enumeration_type,
20972 scope_die_for (type, context_die), type);
20973 equate_type_number_to_die (type, type_die);
20974 add_name_attribute (type_die, type_tag (type));
20975 if (dwarf_version >= 4 || !dwarf_strict)
20977 if (ENUM_IS_SCOPED (type))
20978 add_AT_flag (type_die, DW_AT_enum_class, 1);
20979 if (ENUM_IS_OPAQUE (type))
20980 add_AT_flag (type_die, DW_AT_declaration, 1);
20982 if (!dwarf_strict)
20983 add_AT_unsigned (type_die, DW_AT_encoding,
20984 TYPE_UNSIGNED (type)
20985 ? DW_ATE_unsigned
20986 : DW_ATE_signed);
20988 else if (! TYPE_SIZE (type))
20989 return type_die;
20990 else
20991 remove_AT (type_die, DW_AT_declaration);
20993 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
20994 given enum type is incomplete, do not generate the DW_AT_byte_size
20995 attribute or the DW_AT_element_list attribute. */
20996 if (TYPE_SIZE (type))
20998 tree link;
21000 TREE_ASM_WRITTEN (type) = 1;
21001 add_byte_size_attribute (type_die, type);
21002 if (dwarf_version >= 3 || !dwarf_strict)
21004 tree underlying = lang_hooks.types.enum_underlying_base_type (type);
21005 add_type_attribute (type_die, underlying, TYPE_UNQUALIFIED, false,
21006 context_die);
21008 if (TYPE_STUB_DECL (type) != NULL_TREE)
21010 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
21011 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
21014 /* If the first reference to this type was as the return type of an
21015 inline function, then it may not have a parent. Fix this now. */
21016 if (type_die->die_parent == NULL)
21017 add_child_die (scope_die_for (type, context_die), type_die);
21019 for (link = TYPE_VALUES (type);
21020 link != NULL; link = TREE_CHAIN (link))
21022 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
21023 tree value = TREE_VALUE (link);
21025 add_name_attribute (enum_die,
21026 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
21028 if (TREE_CODE (value) == CONST_DECL)
21029 value = DECL_INITIAL (value);
21031 if (simple_type_size_in_bits (TREE_TYPE (value))
21032 <= HOST_BITS_PER_WIDE_INT || tree_fits_shwi_p (value))
21034 /* For constant forms created by add_AT_unsigned DWARF
21035 consumers (GDB, elfutils, etc.) always zero extend
21036 the value. Only when the actual value is negative
21037 do we need to use add_AT_int to generate a constant
21038 form that can represent negative values. */
21039 HOST_WIDE_INT val = TREE_INT_CST_LOW (value);
21040 if (TYPE_UNSIGNED (TREE_TYPE (value)) || val >= 0)
21041 add_AT_unsigned (enum_die, DW_AT_const_value,
21042 (unsigned HOST_WIDE_INT) val);
21043 else
21044 add_AT_int (enum_die, DW_AT_const_value, val);
21046 else
21047 /* Enumeration constants may be wider than HOST_WIDE_INT. Handle
21048 that here. TODO: This should be re-worked to use correct
21049 signed/unsigned double tags for all cases. */
21050 add_AT_wide (enum_die, DW_AT_const_value, value);
21053 add_gnat_descriptive_type_attribute (type_die, type, context_die);
21054 if (TYPE_ARTIFICIAL (type))
21055 add_AT_flag (type_die, DW_AT_artificial, 1);
21057 else
21058 add_AT_flag (type_die, DW_AT_declaration, 1);
21060 add_pubtype (type, type_die);
21062 return type_die;
21065 /* Generate a DIE to represent either a real live formal parameter decl or to
21066 represent just the type of some formal parameter position in some function
21067 type.
21069 Note that this routine is a bit unusual because its argument may be a
21070 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
21071 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
21072 node. If it's the former then this function is being called to output a
21073 DIE to represent a formal parameter object (or some inlining thereof). If
21074 it's the latter, then this function is only being called to output a
21075 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
21076 argument type of some subprogram type.
21077 If EMIT_NAME_P is true, name and source coordinate attributes
21078 are emitted. */
21080 static dw_die_ref
21081 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
21082 dw_die_ref context_die)
21084 tree node_or_origin = node ? node : origin;
21085 tree ultimate_origin;
21086 dw_die_ref parm_die = NULL;
21088 if (TREE_CODE_CLASS (TREE_CODE (node_or_origin)) == tcc_declaration)
21090 parm_die = lookup_decl_die (node);
21092 /* If the contexts differ, we may not be talking about the same
21093 thing. */
21094 if (parm_die && parm_die->die_parent != context_die)
21096 if (!DECL_ABSTRACT_P (node))
21098 /* This can happen when creating an inlined instance, in
21099 which case we need to create a new DIE that will get
21100 annotated with DW_AT_abstract_origin. */
21101 parm_die = NULL;
21103 else
21105 /* FIXME: Reuse DIE even with a differing context.
21107 This can happen when calling
21108 dwarf2out_abstract_function to build debug info for
21109 the abstract instance of a function for which we have
21110 already generated a DIE in
21111 dwarf2out_early_global_decl.
21113 Once we remove dwarf2out_abstract_function, we should
21114 have a call to gcc_unreachable here. */
21118 if (parm_die && parm_die->die_parent == NULL)
21120 /* Check that parm_die already has the right attributes that
21121 we would have added below. If any attributes are
21122 missing, fall through to add them. */
21123 if (! DECL_ABSTRACT_P (node_or_origin)
21124 && !get_AT (parm_die, DW_AT_location)
21125 && !get_AT (parm_die, DW_AT_const_value))
21126 /* We are missing location info, and are about to add it. */
21128 else
21130 add_child_die (context_die, parm_die);
21131 return parm_die;
21136 /* If we have a previously generated DIE, use it, unless this is an
21137 concrete instance (origin != NULL), in which case we need a new
21138 DIE with a corresponding DW_AT_abstract_origin. */
21139 bool reusing_die;
21140 if (parm_die && origin == NULL)
21141 reusing_die = true;
21142 else
21144 parm_die = new_die (DW_TAG_formal_parameter, context_die, node);
21145 reusing_die = false;
21148 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
21150 case tcc_declaration:
21151 ultimate_origin = decl_ultimate_origin (node_or_origin);
21152 if (node || ultimate_origin)
21153 origin = ultimate_origin;
21155 if (reusing_die)
21156 goto add_location;
21158 if (origin != NULL)
21159 add_abstract_origin_attribute (parm_die, origin);
21160 else if (emit_name_p)
21161 add_name_and_src_coords_attributes (parm_die, node);
21162 if (origin == NULL
21163 || (! DECL_ABSTRACT_P (node_or_origin)
21164 && variably_modified_type_p (TREE_TYPE (node_or_origin),
21165 decl_function_context
21166 (node_or_origin))))
21168 tree type = TREE_TYPE (node_or_origin);
21169 if (decl_by_reference_p (node_or_origin))
21170 add_type_attribute (parm_die, TREE_TYPE (type),
21171 TYPE_UNQUALIFIED,
21172 false, context_die);
21173 else
21174 add_type_attribute (parm_die, type,
21175 decl_quals (node_or_origin),
21176 false, context_die);
21178 if (origin == NULL && DECL_ARTIFICIAL (node))
21179 add_AT_flag (parm_die, DW_AT_artificial, 1);
21180 add_location:
21181 if (node && node != origin)
21182 equate_decl_number_to_die (node, parm_die);
21183 if (! DECL_ABSTRACT_P (node_or_origin))
21184 add_location_or_const_value_attribute (parm_die, node_or_origin,
21185 node == NULL);
21187 break;
21189 case tcc_type:
21190 /* We were called with some kind of a ..._TYPE node. */
21191 add_type_attribute (parm_die, node_or_origin, TYPE_UNQUALIFIED, false,
21192 context_die);
21193 break;
21195 default:
21196 gcc_unreachable ();
21199 return parm_die;
21202 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
21203 children DW_TAG_formal_parameter DIEs representing the arguments of the
21204 parameter pack.
21206 PARM_PACK must be a function parameter pack.
21207 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
21208 must point to the subsequent arguments of the function PACK_ARG belongs to.
21209 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
21210 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
21211 following the last one for which a DIE was generated. */
21213 static dw_die_ref
21214 gen_formal_parameter_pack_die (tree parm_pack,
21215 tree pack_arg,
21216 dw_die_ref subr_die,
21217 tree *next_arg)
21219 tree arg;
21220 dw_die_ref parm_pack_die;
21222 gcc_assert (parm_pack
21223 && lang_hooks.function_parameter_pack_p (parm_pack)
21224 && subr_die);
21226 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
21227 add_src_coords_attributes (parm_pack_die, parm_pack);
21229 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
21231 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
21232 parm_pack))
21233 break;
21234 gen_formal_parameter_die (arg, NULL,
21235 false /* Don't emit name attribute. */,
21236 parm_pack_die);
21238 if (next_arg)
21239 *next_arg = arg;
21240 return parm_pack_die;
21243 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
21244 at the end of an (ANSI prototyped) formal parameters list. */
21246 static void
21247 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
21249 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
21252 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
21253 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
21254 parameters as specified in some function type specification (except for
21255 those which appear as part of a function *definition*). */
21257 static void
21258 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
21260 tree link;
21261 tree formal_type = NULL;
21262 tree first_parm_type;
21263 tree arg;
21265 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
21267 arg = DECL_ARGUMENTS (function_or_method_type);
21268 function_or_method_type = TREE_TYPE (function_or_method_type);
21270 else
21271 arg = NULL_TREE;
21273 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
21275 /* Make our first pass over the list of formal parameter types and output a
21276 DW_TAG_formal_parameter DIE for each one. */
21277 for (link = first_parm_type; link; )
21279 dw_die_ref parm_die;
21281 formal_type = TREE_VALUE (link);
21282 if (formal_type == void_type_node)
21283 break;
21285 /* Output a (nameless) DIE to represent the formal parameter itself. */
21286 if (!POINTER_BOUNDS_TYPE_P (formal_type))
21288 parm_die = gen_formal_parameter_die (formal_type, NULL,
21289 true /* Emit name attribute. */,
21290 context_die);
21291 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
21292 && link == first_parm_type)
21294 add_AT_flag (parm_die, DW_AT_artificial, 1);
21295 if (dwarf_version >= 3 || !dwarf_strict)
21296 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
21298 else if (arg && DECL_ARTIFICIAL (arg))
21299 add_AT_flag (parm_die, DW_AT_artificial, 1);
21302 link = TREE_CHAIN (link);
21303 if (arg)
21304 arg = DECL_CHAIN (arg);
21307 /* If this function type has an ellipsis, add a
21308 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
21309 if (formal_type != void_type_node)
21310 gen_unspecified_parameters_die (function_or_method_type, context_die);
21312 /* Make our second (and final) pass over the list of formal parameter types
21313 and output DIEs to represent those types (as necessary). */
21314 for (link = TYPE_ARG_TYPES (function_or_method_type);
21315 link && TREE_VALUE (link);
21316 link = TREE_CHAIN (link))
21317 gen_type_die (TREE_VALUE (link), context_die);
21320 /* We want to generate the DIE for TYPE so that we can generate the
21321 die for MEMBER, which has been defined; we will need to refer back
21322 to the member declaration nested within TYPE. If we're trying to
21323 generate minimal debug info for TYPE, processing TYPE won't do the
21324 trick; we need to attach the member declaration by hand. */
21326 static void
21327 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
21329 gen_type_die (type, context_die);
21331 /* If we're trying to avoid duplicate debug info, we may not have
21332 emitted the member decl for this function. Emit it now. */
21333 if (TYPE_STUB_DECL (type)
21334 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
21335 && ! lookup_decl_die (member))
21337 dw_die_ref type_die;
21338 gcc_assert (!decl_ultimate_origin (member));
21340 push_decl_scope (type);
21341 type_die = lookup_type_die_strip_naming_typedef (type);
21342 if (TREE_CODE (member) == FUNCTION_DECL)
21343 gen_subprogram_die (member, type_die);
21344 else if (TREE_CODE (member) == FIELD_DECL)
21346 /* Ignore the nameless fields that are used to skip bits but handle
21347 C++ anonymous unions and structs. */
21348 if (DECL_NAME (member) != NULL_TREE
21349 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
21350 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
21352 struct vlr_context vlr_ctx = {
21353 DECL_CONTEXT (member), /* struct_type */
21354 NULL_TREE /* variant_part_offset */
21356 gen_type_die (member_declared_type (member), type_die);
21357 gen_field_die (member, &vlr_ctx, type_die);
21360 else
21361 gen_variable_die (member, NULL_TREE, type_die);
21363 pop_decl_scope ();
21367 /* Forward declare these functions, because they are mutually recursive
21368 with their set_block_* pairing functions. */
21369 static void set_decl_origin_self (tree);
21370 static void set_decl_abstract_flags (tree, vec<tree> &);
21372 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
21373 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
21374 that it points to the node itself, thus indicating that the node is its
21375 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
21376 the given node is NULL, recursively descend the decl/block tree which
21377 it is the root of, and for each other ..._DECL or BLOCK node contained
21378 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
21379 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
21380 values to point to themselves. */
21382 static void
21383 set_block_origin_self (tree stmt)
21385 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
21387 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
21390 tree local_decl;
21392 for (local_decl = BLOCK_VARS (stmt);
21393 local_decl != NULL_TREE;
21394 local_decl = DECL_CHAIN (local_decl))
21395 /* Do not recurse on nested functions since the inlining status
21396 of parent and child can be different as per the DWARF spec. */
21397 if (TREE_CODE (local_decl) != FUNCTION_DECL
21398 && !DECL_EXTERNAL (local_decl))
21399 set_decl_origin_self (local_decl);
21403 tree subblock;
21405 for (subblock = BLOCK_SUBBLOCKS (stmt);
21406 subblock != NULL_TREE;
21407 subblock = BLOCK_CHAIN (subblock))
21408 set_block_origin_self (subblock); /* Recurse. */
21413 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
21414 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
21415 node to so that it points to the node itself, thus indicating that the
21416 node represents its own (abstract) origin. Additionally, if the
21417 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
21418 the decl/block tree of which the given node is the root of, and for
21419 each other ..._DECL or BLOCK node contained therein whose
21420 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
21421 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
21422 point to themselves. */
21424 static void
21425 set_decl_origin_self (tree decl)
21427 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
21429 DECL_ABSTRACT_ORIGIN (decl) = decl;
21430 if (TREE_CODE (decl) == FUNCTION_DECL)
21432 tree arg;
21434 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
21435 DECL_ABSTRACT_ORIGIN (arg) = arg;
21436 if (DECL_INITIAL (decl) != NULL_TREE
21437 && DECL_INITIAL (decl) != error_mark_node)
21438 set_block_origin_self (DECL_INITIAL (decl));
21443 /* Given a pointer to some BLOCK node, set the BLOCK_ABSTRACT flag to 1
21444 and if it wasn't 1 before, push it to abstract_vec vector.
21445 For all local decls and all local sub-blocks (recursively) do it
21446 too. */
21448 static void
21449 set_block_abstract_flags (tree stmt, vec<tree> &abstract_vec)
21451 tree local_decl;
21452 tree subblock;
21453 unsigned int i;
21455 if (!BLOCK_ABSTRACT (stmt))
21457 abstract_vec.safe_push (stmt);
21458 BLOCK_ABSTRACT (stmt) = 1;
21461 for (local_decl = BLOCK_VARS (stmt);
21462 local_decl != NULL_TREE;
21463 local_decl = DECL_CHAIN (local_decl))
21464 if (! DECL_EXTERNAL (local_decl))
21465 set_decl_abstract_flags (local_decl, abstract_vec);
21467 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
21469 local_decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
21470 if ((VAR_P (local_decl) && !TREE_STATIC (local_decl))
21471 || TREE_CODE (local_decl) == PARM_DECL)
21472 set_decl_abstract_flags (local_decl, abstract_vec);
21475 for (subblock = BLOCK_SUBBLOCKS (stmt);
21476 subblock != NULL_TREE;
21477 subblock = BLOCK_CHAIN (subblock))
21478 set_block_abstract_flags (subblock, abstract_vec);
21481 /* Given a pointer to some ..._DECL node, set DECL_ABSTRACT_P flag on it
21482 to 1 and if it wasn't 1 before, push to abstract_vec vector.
21483 In the case where the decl is a FUNCTION_DECL also set the abstract
21484 flags for all of the parameters, local vars, local
21485 blocks and sub-blocks (recursively). */
21487 static void
21488 set_decl_abstract_flags (tree decl, vec<tree> &abstract_vec)
21490 if (!DECL_ABSTRACT_P (decl))
21492 abstract_vec.safe_push (decl);
21493 DECL_ABSTRACT_P (decl) = 1;
21496 if (TREE_CODE (decl) == FUNCTION_DECL)
21498 tree arg;
21500 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
21501 if (!DECL_ABSTRACT_P (arg))
21503 abstract_vec.safe_push (arg);
21504 DECL_ABSTRACT_P (arg) = 1;
21506 if (DECL_INITIAL (decl) != NULL_TREE
21507 && DECL_INITIAL (decl) != error_mark_node)
21508 set_block_abstract_flags (DECL_INITIAL (decl), abstract_vec);
21512 /* Generate the DWARF2 info for the "abstract" instance of a function which we
21513 may later generate inlined and/or out-of-line instances of.
21515 FIXME: In the early-dwarf world, this function, and most of the
21516 DECL_ABSTRACT code should be obsoleted. The early DIE _is_
21517 the abstract instance. All we would need to do is annotate
21518 the early DIE with the appropriate DW_AT_inline in late
21519 dwarf (perhaps in gen_inlined_subroutine_die).
21521 However, we can't do this yet, because LTO streaming of DIEs
21522 has not been implemented yet. */
21524 static void
21525 dwarf2out_abstract_function (tree decl)
21527 dw_die_ref old_die;
21528 tree save_fn;
21529 tree context;
21530 hash_table<decl_loc_hasher> *old_decl_loc_table;
21531 hash_table<dw_loc_list_hasher> *old_cached_dw_loc_list_table;
21532 int old_call_site_count, old_tail_call_site_count;
21533 struct call_arg_loc_node *old_call_arg_locations;
21535 /* Make sure we have the actual abstract inline, not a clone. */
21536 decl = DECL_ORIGIN (decl);
21538 old_die = lookup_decl_die (decl);
21539 if (old_die && get_AT (old_die, DW_AT_inline))
21540 /* We've already generated the abstract instance. */
21541 return;
21543 /* We can be called while recursively when seeing block defining inlined subroutine
21544 DIE. Be sure to not clobber the outer location table nor use it or we would
21545 get locations in abstract instantces. */
21546 old_decl_loc_table = decl_loc_table;
21547 decl_loc_table = NULL;
21548 old_cached_dw_loc_list_table = cached_dw_loc_list_table;
21549 cached_dw_loc_list_table = NULL;
21550 old_call_arg_locations = call_arg_locations;
21551 call_arg_locations = NULL;
21552 old_call_site_count = call_site_count;
21553 call_site_count = -1;
21554 old_tail_call_site_count = tail_call_site_count;
21555 tail_call_site_count = -1;
21557 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
21558 we don't get confused by DECL_ABSTRACT_P. */
21559 if (debug_info_level > DINFO_LEVEL_TERSE)
21561 context = decl_class_context (decl);
21562 if (context)
21563 gen_type_die_for_member
21564 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
21567 /* Pretend we've just finished compiling this function. */
21568 save_fn = current_function_decl;
21569 current_function_decl = decl;
21571 auto_vec<tree, 64> abstract_vec;
21572 set_decl_abstract_flags (decl, abstract_vec);
21573 dwarf2out_decl (decl);
21574 unsigned int i;
21575 tree t;
21576 FOR_EACH_VEC_ELT (abstract_vec, i, t)
21577 if (TREE_CODE (t) == BLOCK)
21578 BLOCK_ABSTRACT (t) = 0;
21579 else
21580 DECL_ABSTRACT_P (t) = 0;
21582 current_function_decl = save_fn;
21583 decl_loc_table = old_decl_loc_table;
21584 cached_dw_loc_list_table = old_cached_dw_loc_list_table;
21585 call_arg_locations = old_call_arg_locations;
21586 call_site_count = old_call_site_count;
21587 tail_call_site_count = old_tail_call_site_count;
21590 /* Helper function of premark_used_types() which gets called through
21591 htab_traverse.
21593 Marks the DIE of a given type in *SLOT as perennial, so it never gets
21594 marked as unused by prune_unused_types. */
21596 bool
21597 premark_used_types_helper (tree const &type, void *)
21599 dw_die_ref die;
21601 die = lookup_type_die (type);
21602 if (die != NULL)
21603 die->die_perennial_p = 1;
21604 return true;
21607 /* Helper function of premark_types_used_by_global_vars which gets called
21608 through htab_traverse.
21610 Marks the DIE of a given type in *SLOT as perennial, so it never gets
21611 marked as unused by prune_unused_types. The DIE of the type is marked
21612 only if the global variable using the type will actually be emitted. */
21615 premark_types_used_by_global_vars_helper (types_used_by_vars_entry **slot,
21616 void *)
21618 struct types_used_by_vars_entry *entry;
21619 dw_die_ref die;
21621 entry = (struct types_used_by_vars_entry *) *slot;
21622 gcc_assert (entry->type != NULL
21623 && entry->var_decl != NULL);
21624 die = lookup_type_die (entry->type);
21625 if (die)
21627 /* Ask cgraph if the global variable really is to be emitted.
21628 If yes, then we'll keep the DIE of ENTRY->TYPE. */
21629 varpool_node *node = varpool_node::get (entry->var_decl);
21630 if (node && node->definition)
21632 die->die_perennial_p = 1;
21633 /* Keep the parent DIEs as well. */
21634 while ((die = die->die_parent) && die->die_perennial_p == 0)
21635 die->die_perennial_p = 1;
21638 return 1;
21641 /* Mark all members of used_types_hash as perennial. */
21643 static void
21644 premark_used_types (struct function *fun)
21646 if (fun && fun->used_types_hash)
21647 fun->used_types_hash->traverse<void *, premark_used_types_helper> (NULL);
21650 /* Mark all members of types_used_by_vars_entry as perennial. */
21652 static void
21653 premark_types_used_by_global_vars (void)
21655 if (types_used_by_vars_hash)
21656 types_used_by_vars_hash
21657 ->traverse<void *, premark_types_used_by_global_vars_helper> (NULL);
21660 /* Generate a DW_TAG_call_site DIE in function DECL under SUBR_DIE
21661 for CA_LOC call arg loc node. */
21663 static dw_die_ref
21664 gen_call_site_die (tree decl, dw_die_ref subr_die,
21665 struct call_arg_loc_node *ca_loc)
21667 dw_die_ref stmt_die = NULL, die;
21668 tree block = ca_loc->block;
21670 while (block
21671 && block != DECL_INITIAL (decl)
21672 && TREE_CODE (block) == BLOCK)
21674 stmt_die = BLOCK_DIE (block);
21675 if (stmt_die)
21676 break;
21677 block = BLOCK_SUPERCONTEXT (block);
21679 if (stmt_die == NULL)
21680 stmt_die = subr_die;
21681 die = new_die (dwarf_TAG (DW_TAG_call_site), stmt_die, NULL_TREE);
21682 add_AT_lbl_id (die, dwarf_AT (DW_AT_call_return_pc), ca_loc->label);
21683 if (ca_loc->tail_call_p)
21684 add_AT_flag (die, dwarf_AT (DW_AT_call_tail_call), 1);
21685 if (ca_loc->symbol_ref)
21687 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
21688 if (tdie)
21689 add_AT_die_ref (die, dwarf_AT (DW_AT_call_origin), tdie);
21690 else
21691 add_AT_addr (die, dwarf_AT (DW_AT_call_origin), ca_loc->symbol_ref,
21692 false);
21694 return die;
21697 /* Generate a DIE to represent a declared function (either file-scope or
21698 block-local). */
21700 static void
21701 gen_subprogram_die (tree decl, dw_die_ref context_die)
21703 tree origin = decl_ultimate_origin (decl);
21704 dw_die_ref subr_die;
21705 dw_die_ref old_die = lookup_decl_die (decl);
21707 /* This function gets called multiple times for different stages of
21708 the debug process. For example, for func() in this code:
21710 namespace S
21712 void func() { ... }
21715 ...we get called 4 times. Twice in early debug and twice in
21716 late debug:
21718 Early debug
21719 -----------
21721 1. Once while generating func() within the namespace. This is
21722 the declaration. The declaration bit below is set, as the
21723 context is the namespace.
21725 A new DIE will be generated with DW_AT_declaration set.
21727 2. Once for func() itself. This is the specification. The
21728 declaration bit below is clear as the context is the CU.
21730 We will use the cached DIE from (1) to create a new DIE with
21731 DW_AT_specification pointing to the declaration in (1).
21733 Late debug via rest_of_handle_final()
21734 -------------------------------------
21736 3. Once generating func() within the namespace. This is also the
21737 declaration, as in (1), but this time we will early exit below
21738 as we have a cached DIE and a declaration needs no additional
21739 annotations (no locations), as the source declaration line
21740 info is enough.
21742 4. Once for func() itself. As in (2), this is the specification,
21743 but this time we will re-use the cached DIE, and just annotate
21744 it with the location information that should now be available.
21746 For something without namespaces, but with abstract instances, we
21747 are also called a multiple times:
21749 class Base
21751 public:
21752 Base (); // constructor declaration (1)
21755 Base::Base () { } // constructor specification (2)
21757 Early debug
21758 -----------
21760 1. Once for the Base() constructor by virtue of it being a
21761 member of the Base class. This is done via
21762 rest_of_type_compilation.
21764 This is a declaration, so a new DIE will be created with
21765 DW_AT_declaration.
21767 2. Once for the Base() constructor definition, but this time
21768 while generating the abstract instance of the base
21769 constructor (__base_ctor) which is being generated via early
21770 debug of reachable functions.
21772 Even though we have a cached version of the declaration (1),
21773 we will create a DW_AT_specification of the declaration DIE
21774 in (1).
21776 3. Once for the __base_ctor itself, but this time, we generate
21777 an DW_AT_abstract_origin version of the DW_AT_specification in
21778 (2).
21780 Late debug via rest_of_handle_final
21781 -----------------------------------
21783 4. One final time for the __base_ctor (which will have a cached
21784 DIE with DW_AT_abstract_origin created in (3). This time,
21785 we will just annotate the location information now
21786 available.
21788 int declaration = (current_function_decl != decl
21789 || class_or_namespace_scope_p (context_die));
21791 /* Now that the C++ front end lazily declares artificial member fns, we
21792 might need to retrofit the declaration into its class. */
21793 if (!declaration && !origin && !old_die
21794 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
21795 && !class_or_namespace_scope_p (context_die)
21796 && debug_info_level > DINFO_LEVEL_TERSE)
21797 old_die = force_decl_die (decl);
21799 /* An inlined instance, tag a new DIE with DW_AT_abstract_origin. */
21800 if (origin != NULL)
21802 gcc_assert (!declaration || local_scope_p (context_die));
21804 /* Fixup die_parent for the abstract instance of a nested
21805 inline function. */
21806 if (old_die && old_die->die_parent == NULL)
21807 add_child_die (context_die, old_die);
21809 if (old_die && get_AT_ref (old_die, DW_AT_abstract_origin))
21811 /* If we have a DW_AT_abstract_origin we have a working
21812 cached version. */
21813 subr_die = old_die;
21815 else
21817 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
21818 add_abstract_origin_attribute (subr_die, origin);
21819 /* This is where the actual code for a cloned function is.
21820 Let's emit linkage name attribute for it. This helps
21821 debuggers to e.g, set breakpoints into
21822 constructors/destructors when the user asks "break
21823 K::K". */
21824 add_linkage_name (subr_die, decl);
21827 /* A cached copy, possibly from early dwarf generation. Reuse as
21828 much as possible. */
21829 else if (old_die)
21831 /* A declaration that has been previously dumped needs no
21832 additional information. */
21833 if (declaration)
21834 return;
21836 if (!get_AT_flag (old_die, DW_AT_declaration)
21837 /* We can have a normal definition following an inline one in the
21838 case of redefinition of GNU C extern inlines.
21839 It seems reasonable to use AT_specification in this case. */
21840 && !get_AT (old_die, DW_AT_inline))
21842 /* Detect and ignore this case, where we are trying to output
21843 something we have already output. */
21844 if (get_AT (old_die, DW_AT_low_pc)
21845 || get_AT (old_die, DW_AT_ranges))
21846 return;
21848 /* If we have no location information, this must be a
21849 partially generated DIE from early dwarf generation.
21850 Fall through and generate it. */
21853 /* If the definition comes from the same place as the declaration,
21854 maybe use the old DIE. We always want the DIE for this function
21855 that has the *_pc attributes to be under comp_unit_die so the
21856 debugger can find it. We also need to do this for abstract
21857 instances of inlines, since the spec requires the out-of-line copy
21858 to have the same parent. For local class methods, this doesn't
21859 apply; we just use the old DIE. */
21860 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
21861 struct dwarf_file_data * file_index = lookup_filename (s.file);
21862 if ((is_cu_die (old_die->die_parent)
21863 /* This condition fixes the inconsistency/ICE with the
21864 following Fortran test (or some derivative thereof) while
21865 building libgfortran:
21867 module some_m
21868 contains
21869 logical function funky (FLAG)
21870 funky = .true.
21871 end function
21872 end module
21874 || (old_die->die_parent
21875 && old_die->die_parent->die_tag == DW_TAG_module)
21876 || context_die == NULL)
21877 && (DECL_ARTIFICIAL (decl)
21878 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
21879 && (get_AT_unsigned (old_die, DW_AT_decl_line)
21880 == (unsigned) s.line))))
21882 subr_die = old_die;
21884 /* Clear out the declaration attribute, but leave the
21885 parameters so they can be augmented with location
21886 information later. Unless this was a declaration, in
21887 which case, wipe out the nameless parameters and recreate
21888 them further down. */
21889 if (remove_AT (subr_die, DW_AT_declaration))
21892 remove_AT (subr_die, DW_AT_object_pointer);
21893 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
21896 /* Make a specification pointing to the previously built
21897 declaration. */
21898 else
21900 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
21901 add_AT_specification (subr_die, old_die);
21902 add_pubname (decl, subr_die);
21903 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
21904 add_AT_file (subr_die, DW_AT_decl_file, file_index);
21905 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
21906 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
21908 /* If the prototype had an 'auto' or 'decltype(auto)' return type,
21909 emit the real type on the definition die. */
21910 if (is_cxx() && debug_info_level > DINFO_LEVEL_TERSE)
21912 dw_die_ref die = get_AT_ref (old_die, DW_AT_type);
21913 if (die == auto_die || die == decltype_auto_die)
21914 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
21915 TYPE_UNQUALIFIED, false, context_die);
21918 /* When we process the method declaration, we haven't seen
21919 the out-of-class defaulted definition yet, so we have to
21920 recheck now. */
21921 if ((dwarf_version >= 5 || ! dwarf_strict)
21922 && !get_AT (subr_die, DW_AT_defaulted))
21924 int defaulted
21925 = lang_hooks.decls.decl_dwarf_attribute (decl,
21926 DW_AT_defaulted);
21927 if (defaulted != -1)
21929 /* Other values must have been handled before. */
21930 gcc_assert (defaulted == DW_DEFAULTED_out_of_class);
21931 add_AT_unsigned (subr_die, DW_AT_defaulted, defaulted);
21936 /* Create a fresh DIE for anything else. */
21937 else
21939 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
21941 if (TREE_PUBLIC (decl))
21942 add_AT_flag (subr_die, DW_AT_external, 1);
21944 add_name_and_src_coords_attributes (subr_die, decl);
21945 add_pubname (decl, subr_die);
21946 if (debug_info_level > DINFO_LEVEL_TERSE)
21948 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
21949 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
21950 TYPE_UNQUALIFIED, false, context_die);
21953 add_pure_or_virtual_attribute (subr_die, decl);
21954 if (DECL_ARTIFICIAL (decl))
21955 add_AT_flag (subr_die, DW_AT_artificial, 1);
21957 if (TREE_THIS_VOLATILE (decl) && (dwarf_version >= 5 || !dwarf_strict))
21958 add_AT_flag (subr_die, DW_AT_noreturn, 1);
21960 add_accessibility_attribute (subr_die, decl);
21963 /* Unless we have an existing non-declaration DIE, equate the new
21964 DIE. */
21965 if (!old_die || is_declaration_die (old_die))
21966 equate_decl_number_to_die (decl, subr_die);
21968 if (declaration)
21970 if (!old_die || !get_AT (old_die, DW_AT_inline))
21972 add_AT_flag (subr_die, DW_AT_declaration, 1);
21974 /* If this is an explicit function declaration then generate
21975 a DW_AT_explicit attribute. */
21976 if ((dwarf_version >= 3 || !dwarf_strict)
21977 && lang_hooks.decls.decl_dwarf_attribute (decl,
21978 DW_AT_explicit) == 1)
21979 add_AT_flag (subr_die, DW_AT_explicit, 1);
21981 /* If this is a C++11 deleted special function member then generate
21982 a DW_AT_deleted attribute. */
21983 if ((dwarf_version >= 5 || !dwarf_strict)
21984 && lang_hooks.decls.decl_dwarf_attribute (decl,
21985 DW_AT_deleted) == 1)
21986 add_AT_flag (subr_die, DW_AT_deleted, 1);
21988 /* If this is a C++11 defaulted special function member then
21989 generate a DW_AT_defaulted attribute. */
21990 if (dwarf_version >= 5 || !dwarf_strict)
21992 int defaulted
21993 = lang_hooks.decls.decl_dwarf_attribute (decl,
21994 DW_AT_defaulted);
21995 if (defaulted != -1)
21996 add_AT_unsigned (subr_die, DW_AT_defaulted, defaulted);
21999 /* If this is a C++11 non-static member function with & ref-qualifier
22000 then generate a DW_AT_reference attribute. */
22001 if ((dwarf_version >= 5 || !dwarf_strict)
22002 && lang_hooks.decls.decl_dwarf_attribute (decl,
22003 DW_AT_reference) == 1)
22004 add_AT_flag (subr_die, DW_AT_reference, 1);
22006 /* If this is a C++11 non-static member function with &&
22007 ref-qualifier then generate a DW_AT_reference attribute. */
22008 if ((dwarf_version >= 5 || !dwarf_strict)
22009 && lang_hooks.decls.decl_dwarf_attribute (decl,
22010 DW_AT_rvalue_reference)
22011 == 1)
22012 add_AT_flag (subr_die, DW_AT_rvalue_reference, 1);
22015 /* Tag abstract instances with DW_AT_inline. */
22016 else if (DECL_ABSTRACT_P (decl))
22018 if (DECL_DECLARED_INLINE_P (decl))
22020 if (cgraph_function_possibly_inlined_p (decl))
22021 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
22022 else
22023 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
22025 else
22027 if (cgraph_function_possibly_inlined_p (decl))
22028 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
22029 else
22030 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
22033 if (DECL_DECLARED_INLINE_P (decl)
22034 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
22035 add_AT_flag (subr_die, DW_AT_artificial, 1);
22037 /* For non DECL_EXTERNALs, if range information is available, fill
22038 the DIE with it. */
22039 else if (!DECL_EXTERNAL (decl) && !early_dwarf)
22041 HOST_WIDE_INT cfa_fb_offset;
22043 struct function *fun = DECL_STRUCT_FUNCTION (decl);
22045 if (!flag_reorder_blocks_and_partition)
22047 dw_fde_ref fde = fun->fde;
22048 if (fde->dw_fde_begin)
22050 /* We have already generated the labels. */
22051 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
22052 fde->dw_fde_end, false);
22054 else
22056 /* Create start/end labels and add the range. */
22057 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
22058 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
22059 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
22060 current_function_funcdef_no);
22061 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
22062 current_function_funcdef_no);
22063 add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
22064 false);
22067 #if VMS_DEBUGGING_INFO
22068 /* HP OpenVMS Industry Standard 64: DWARF Extensions
22069 Section 2.3 Prologue and Epilogue Attributes:
22070 When a breakpoint is set on entry to a function, it is generally
22071 desirable for execution to be suspended, not on the very first
22072 instruction of the function, but rather at a point after the
22073 function's frame has been set up, after any language defined local
22074 declaration processing has been completed, and before execution of
22075 the first statement of the function begins. Debuggers generally
22076 cannot properly determine where this point is. Similarly for a
22077 breakpoint set on exit from a function. The prologue and epilogue
22078 attributes allow a compiler to communicate the location(s) to use. */
22081 if (fde->dw_fde_vms_end_prologue)
22082 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
22083 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
22085 if (fde->dw_fde_vms_begin_epilogue)
22086 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
22087 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
22089 #endif
22092 else
22094 /* Generate pubnames entries for the split function code ranges. */
22095 dw_fde_ref fde = fun->fde;
22097 if (fde->dw_fde_second_begin)
22099 if (dwarf_version >= 3 || !dwarf_strict)
22101 /* We should use ranges for non-contiguous code section
22102 addresses. Use the actual code range for the initial
22103 section, since the HOT/COLD labels might precede an
22104 alignment offset. */
22105 bool range_list_added = false;
22106 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
22107 fde->dw_fde_end, &range_list_added,
22108 false);
22109 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
22110 fde->dw_fde_second_end,
22111 &range_list_added, false);
22112 if (range_list_added)
22113 add_ranges (NULL);
22115 else
22117 /* There is no real support in DW2 for this .. so we make
22118 a work-around. First, emit the pub name for the segment
22119 containing the function label. Then make and emit a
22120 simplified subprogram DIE for the second segment with the
22121 name pre-fixed by __hot/cold_sect_of_. We use the same
22122 linkage name for the second die so that gdb will find both
22123 sections when given "b foo". */
22124 const char *name = NULL;
22125 tree decl_name = DECL_NAME (decl);
22126 dw_die_ref seg_die;
22128 /* Do the 'primary' section. */
22129 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
22130 fde->dw_fde_end, false);
22132 /* Build a minimal DIE for the secondary section. */
22133 seg_die = new_die (DW_TAG_subprogram,
22134 subr_die->die_parent, decl);
22136 if (TREE_PUBLIC (decl))
22137 add_AT_flag (seg_die, DW_AT_external, 1);
22139 if (decl_name != NULL
22140 && IDENTIFIER_POINTER (decl_name) != NULL)
22142 name = dwarf2_name (decl, 1);
22143 if (! DECL_ARTIFICIAL (decl))
22144 add_src_coords_attributes (seg_die, decl);
22146 add_linkage_name (seg_die, decl);
22148 gcc_assert (name != NULL);
22149 add_pure_or_virtual_attribute (seg_die, decl);
22150 if (DECL_ARTIFICIAL (decl))
22151 add_AT_flag (seg_die, DW_AT_artificial, 1);
22153 name = concat ("__second_sect_of_", name, NULL);
22154 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
22155 fde->dw_fde_second_end, false);
22156 add_name_attribute (seg_die, name);
22157 if (want_pubnames ())
22158 add_pubname_string (name, seg_die);
22161 else
22162 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
22163 false);
22166 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
22168 /* We define the "frame base" as the function's CFA. This is more
22169 convenient for several reasons: (1) It's stable across the prologue
22170 and epilogue, which makes it better than just a frame pointer,
22171 (2) With dwarf3, there exists a one-byte encoding that allows us
22172 to reference the .debug_frame data by proxy, but failing that,
22173 (3) We can at least reuse the code inspection and interpretation
22174 code that determines the CFA position at various points in the
22175 function. */
22176 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
22178 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
22179 add_AT_loc (subr_die, DW_AT_frame_base, op);
22181 else
22183 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
22184 if (list->dw_loc_next)
22185 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
22186 else
22187 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
22190 /* Compute a displacement from the "steady-state frame pointer" to
22191 the CFA. The former is what all stack slots and argument slots
22192 will reference in the rtl; the latter is what we've told the
22193 debugger about. We'll need to adjust all frame_base references
22194 by this displacement. */
22195 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
22197 if (fun->static_chain_decl)
22199 /* DWARF requires here a location expression that computes the
22200 address of the enclosing subprogram's frame base. The machinery
22201 in tree-nested.c is supposed to store this specific address in the
22202 last field of the FRAME record. */
22203 const tree frame_type
22204 = TREE_TYPE (TREE_TYPE (fun->static_chain_decl));
22205 const tree fb_decl = tree_last (TYPE_FIELDS (frame_type));
22207 tree fb_expr
22208 = build1 (INDIRECT_REF, frame_type, fun->static_chain_decl);
22209 fb_expr = build3 (COMPONENT_REF, TREE_TYPE (fb_decl),
22210 fb_expr, fb_decl, NULL_TREE);
22212 add_AT_location_description (subr_die, DW_AT_static_link,
22213 loc_list_from_tree (fb_expr, 0, NULL));
22217 /* Generate child dies for template paramaters. */
22218 if (early_dwarf && debug_info_level > DINFO_LEVEL_TERSE)
22219 gen_generic_params_dies (decl);
22221 /* Now output descriptions of the arguments for this function. This gets
22222 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
22223 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
22224 `...' at the end of the formal parameter list. In order to find out if
22225 there was a trailing ellipsis or not, we must instead look at the type
22226 associated with the FUNCTION_DECL. This will be a node of type
22227 FUNCTION_TYPE. If the chain of type nodes hanging off of this
22228 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
22229 an ellipsis at the end. */
22231 /* In the case where we are describing a mere function declaration, all we
22232 need to do here (and all we *can* do here) is to describe the *types* of
22233 its formal parameters. */
22234 if (debug_info_level <= DINFO_LEVEL_TERSE)
22236 else if (declaration)
22237 gen_formal_types_die (decl, subr_die);
22238 else
22240 /* Generate DIEs to represent all known formal parameters. */
22241 tree parm = DECL_ARGUMENTS (decl);
22242 tree generic_decl = early_dwarf
22243 ? lang_hooks.decls.get_generic_function_decl (decl) : NULL;
22244 tree generic_decl_parm = generic_decl
22245 ? DECL_ARGUMENTS (generic_decl)
22246 : NULL;
22247 auto_vec<dw_die_ref> string_types_vec;
22248 if (string_types == NULL)
22249 string_types = &string_types_vec;
22251 /* Now we want to walk the list of parameters of the function and
22252 emit their relevant DIEs.
22254 We consider the case of DECL being an instance of a generic function
22255 as well as it being a normal function.
22257 If DECL is an instance of a generic function we walk the
22258 parameters of the generic function declaration _and_ the parameters of
22259 DECL itself. This is useful because we want to emit specific DIEs for
22260 function parameter packs and those are declared as part of the
22261 generic function declaration. In that particular case,
22262 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
22263 That DIE has children DIEs representing the set of arguments
22264 of the pack. Note that the set of pack arguments can be empty.
22265 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
22266 children DIE.
22268 Otherwise, we just consider the parameters of DECL. */
22269 while (generic_decl_parm || parm)
22271 if (generic_decl_parm
22272 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
22273 gen_formal_parameter_pack_die (generic_decl_parm,
22274 parm, subr_die,
22275 &parm);
22276 else if (parm && !POINTER_BOUNDS_P (parm))
22278 dw_die_ref parm_die = gen_decl_die (parm, NULL, NULL, subr_die);
22280 if (parm == DECL_ARGUMENTS (decl)
22281 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
22282 && parm_die
22283 && (dwarf_version >= 3 || !dwarf_strict))
22284 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
22286 parm = DECL_CHAIN (parm);
22288 else if (parm)
22289 parm = DECL_CHAIN (parm);
22291 if (generic_decl_parm)
22292 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
22295 /* Decide whether we need an unspecified_parameters DIE at the end.
22296 There are 2 more cases to do this for: 1) the ansi ... declaration -
22297 this is detectable when the end of the arg list is not a
22298 void_type_node 2) an unprototyped function declaration (not a
22299 definition). This just means that we have no info about the
22300 parameters at all. */
22301 if (early_dwarf)
22303 if (prototype_p (TREE_TYPE (decl)))
22305 /* This is the prototyped case, check for.... */
22306 if (stdarg_p (TREE_TYPE (decl)))
22307 gen_unspecified_parameters_die (decl, subr_die);
22309 else if (DECL_INITIAL (decl) == NULL_TREE)
22310 gen_unspecified_parameters_die (decl, subr_die);
22313 /* Adjust DW_TAG_string_type DIEs if needed, now that all arguments
22314 have DIEs. */
22315 if (string_types == &string_types_vec)
22317 adjust_string_types ();
22318 string_types = NULL;
22322 if (subr_die != old_die)
22323 /* Add the calling convention attribute if requested. */
22324 add_calling_convention_attribute (subr_die, decl);
22326 /* Output Dwarf info for all of the stuff within the body of the function
22327 (if it has one - it may be just a declaration).
22329 OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
22330 a function. This BLOCK actually represents the outermost binding contour
22331 for the function, i.e. the contour in which the function's formal
22332 parameters and labels get declared. Curiously, it appears that the front
22333 end doesn't actually put the PARM_DECL nodes for the current function onto
22334 the BLOCK_VARS list for this outer scope, but are strung off of the
22335 DECL_ARGUMENTS list for the function instead.
22337 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
22338 the LABEL_DECL nodes for the function however, and we output DWARF info
22339 for those in decls_for_scope. Just within the `outer_scope' there will be
22340 a BLOCK node representing the function's outermost pair of curly braces,
22341 and any blocks used for the base and member initializers of a C++
22342 constructor function. */
22343 tree outer_scope = DECL_INITIAL (decl);
22344 if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
22346 int call_site_note_count = 0;
22347 int tail_call_site_note_count = 0;
22349 /* Emit a DW_TAG_variable DIE for a named return value. */
22350 if (DECL_NAME (DECL_RESULT (decl)))
22351 gen_decl_die (DECL_RESULT (decl), NULL, NULL, subr_die);
22353 /* The first time through decls_for_scope we will generate the
22354 DIEs for the locals. The second time, we fill in the
22355 location info. */
22356 decls_for_scope (outer_scope, subr_die);
22358 if (call_arg_locations && (!dwarf_strict || dwarf_version >= 5))
22360 struct call_arg_loc_node *ca_loc;
22361 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
22363 dw_die_ref die = NULL;
22364 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
22365 rtx arg, next_arg;
22367 for (arg = (ca_loc->call_arg_loc_note != NULL_RTX
22368 ? NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note)
22369 : NULL_RTX);
22370 arg; arg = next_arg)
22372 dw_loc_descr_ref reg, val;
22373 machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
22374 dw_die_ref cdie, tdie = NULL;
22376 next_arg = XEXP (arg, 1);
22377 if (REG_P (XEXP (XEXP (arg, 0), 0))
22378 && next_arg
22379 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
22380 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
22381 && REGNO (XEXP (XEXP (arg, 0), 0))
22382 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
22383 next_arg = XEXP (next_arg, 1);
22384 if (mode == VOIDmode)
22386 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
22387 if (mode == VOIDmode)
22388 mode = GET_MODE (XEXP (arg, 0));
22390 if (mode == VOIDmode || mode == BLKmode)
22391 continue;
22392 /* Get dynamic information about call target only if we
22393 have no static information: we cannot generate both
22394 DW_AT_call_origin and DW_AT_call_target
22395 attributes. */
22396 if (ca_loc->symbol_ref == NULL_RTX)
22398 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
22400 tloc = XEXP (XEXP (arg, 0), 1);
22401 continue;
22403 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
22404 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
22406 tlocc = XEXP (XEXP (arg, 0), 1);
22407 continue;
22410 reg = NULL;
22411 if (REG_P (XEXP (XEXP (arg, 0), 0)))
22412 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
22413 VAR_INIT_STATUS_INITIALIZED);
22414 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
22416 rtx mem = XEXP (XEXP (arg, 0), 0);
22417 reg = mem_loc_descriptor (XEXP (mem, 0),
22418 get_address_mode (mem),
22419 GET_MODE (mem),
22420 VAR_INIT_STATUS_INITIALIZED);
22422 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
22423 == DEBUG_PARAMETER_REF)
22425 tree tdecl
22426 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
22427 tdie = lookup_decl_die (tdecl);
22428 if (tdie == NULL)
22429 continue;
22431 else
22432 continue;
22433 if (reg == NULL
22434 && GET_CODE (XEXP (XEXP (arg, 0), 0))
22435 != DEBUG_PARAMETER_REF)
22436 continue;
22437 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
22438 VOIDmode,
22439 VAR_INIT_STATUS_INITIALIZED);
22440 if (val == NULL)
22441 continue;
22442 if (die == NULL)
22443 die = gen_call_site_die (decl, subr_die, ca_loc);
22444 cdie = new_die (dwarf_TAG (DW_TAG_call_site_parameter), die,
22445 NULL_TREE);
22446 if (reg != NULL)
22447 add_AT_loc (cdie, DW_AT_location, reg);
22448 else if (tdie != NULL)
22449 add_AT_die_ref (cdie, dwarf_AT (DW_AT_call_parameter),
22450 tdie);
22451 add_AT_loc (cdie, dwarf_AT (DW_AT_call_value), val);
22452 if (next_arg != XEXP (arg, 1))
22454 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
22455 if (mode == VOIDmode)
22456 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
22457 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
22458 0), 1),
22459 mode, VOIDmode,
22460 VAR_INIT_STATUS_INITIALIZED);
22461 if (val != NULL)
22462 add_AT_loc (cdie, dwarf_AT (DW_AT_call_data_value),
22463 val);
22466 if (die == NULL
22467 && (ca_loc->symbol_ref || tloc))
22468 die = gen_call_site_die (decl, subr_die, ca_loc);
22469 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
22471 dw_loc_descr_ref tval = NULL;
22473 if (tloc != NULL_RTX)
22474 tval = mem_loc_descriptor (tloc,
22475 GET_MODE (tloc) == VOIDmode
22476 ? Pmode : GET_MODE (tloc),
22477 VOIDmode,
22478 VAR_INIT_STATUS_INITIALIZED);
22479 if (tval)
22480 add_AT_loc (die, dwarf_AT (DW_AT_call_target), tval);
22481 else if (tlocc != NULL_RTX)
22483 tval = mem_loc_descriptor (tlocc,
22484 GET_MODE (tlocc) == VOIDmode
22485 ? Pmode : GET_MODE (tlocc),
22486 VOIDmode,
22487 VAR_INIT_STATUS_INITIALIZED);
22488 if (tval)
22489 add_AT_loc (die,
22490 dwarf_AT (DW_AT_call_target_clobbered),
22491 tval);
22494 if (die != NULL)
22496 call_site_note_count++;
22497 if (ca_loc->tail_call_p)
22498 tail_call_site_note_count++;
22502 call_arg_locations = NULL;
22503 call_arg_loc_last = NULL;
22504 if (tail_call_site_count >= 0
22505 && tail_call_site_count == tail_call_site_note_count
22506 && (!dwarf_strict || dwarf_version >= 5))
22508 if (call_site_count >= 0
22509 && call_site_count == call_site_note_count)
22510 add_AT_flag (subr_die, dwarf_AT (DW_AT_call_all_calls), 1);
22511 else
22512 add_AT_flag (subr_die, dwarf_AT (DW_AT_call_all_tail_calls), 1);
22514 call_site_count = -1;
22515 tail_call_site_count = -1;
22518 /* Mark used types after we have created DIEs for the functions scopes. */
22519 premark_used_types (DECL_STRUCT_FUNCTION (decl));
22522 /* Returns a hash value for X (which really is a die_struct). */
22524 hashval_t
22525 block_die_hasher::hash (die_struct *d)
22527 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
22530 /* Return nonzero if decl_id and die_parent of die_struct X is the same
22531 as decl_id and die_parent of die_struct Y. */
22533 bool
22534 block_die_hasher::equal (die_struct *x, die_struct *y)
22536 return x->decl_id == y->decl_id && x->die_parent == y->die_parent;
22539 /* Return TRUE if DECL, which may have been previously generated as
22540 OLD_DIE, is a candidate for a DW_AT_specification. DECLARATION is
22541 true if decl (or its origin) is either an extern declaration or a
22542 class/namespace scoped declaration.
22544 The declare_in_namespace support causes us to get two DIEs for one
22545 variable, both of which are declarations. We want to avoid
22546 considering one to be a specification, so we must test for
22547 DECLARATION and DW_AT_declaration. */
22548 static inline bool
22549 decl_will_get_specification_p (dw_die_ref old_die, tree decl, bool declaration)
22551 return (old_die && TREE_STATIC (decl) && !declaration
22552 && get_AT_flag (old_die, DW_AT_declaration) == 1);
22555 /* Return true if DECL is a local static. */
22557 static inline bool
22558 local_function_static (tree decl)
22560 gcc_assert (VAR_P (decl));
22561 return TREE_STATIC (decl)
22562 && DECL_CONTEXT (decl)
22563 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL;
22566 /* Generate a DIE to represent a declared data object.
22567 Either DECL or ORIGIN must be non-null. */
22569 static void
22570 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
22572 HOST_WIDE_INT off = 0;
22573 tree com_decl;
22574 tree decl_or_origin = decl ? decl : origin;
22575 tree ultimate_origin;
22576 dw_die_ref var_die;
22577 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
22578 bool declaration = (DECL_EXTERNAL (decl_or_origin)
22579 || class_or_namespace_scope_p (context_die));
22580 bool specialization_p = false;
22581 bool no_linkage_name = false;
22583 /* While C++ inline static data members have definitions inside of the
22584 class, force the first DIE to be a declaration, then let gen_member_die
22585 reparent it to the class context and call gen_variable_die again
22586 to create the outside of the class DIE for the definition. */
22587 if (!declaration
22588 && old_die == NULL
22589 && decl
22590 && DECL_CONTEXT (decl)
22591 && TYPE_P (DECL_CONTEXT (decl))
22592 && lang_hooks.decls.decl_dwarf_attribute (decl, DW_AT_inline) != -1)
22594 declaration = true;
22595 no_linkage_name = true;
22598 ultimate_origin = decl_ultimate_origin (decl_or_origin);
22599 if (decl || ultimate_origin)
22600 origin = ultimate_origin;
22601 com_decl = fortran_common (decl_or_origin, &off);
22603 /* Symbol in common gets emitted as a child of the common block, in the form
22604 of a data member. */
22605 if (com_decl)
22607 dw_die_ref com_die;
22608 dw_loc_list_ref loc = NULL;
22609 die_node com_die_arg;
22611 var_die = lookup_decl_die (decl_or_origin);
22612 if (var_die)
22614 if (! early_dwarf && get_AT (var_die, DW_AT_location) == NULL)
22616 loc = loc_list_from_tree (com_decl, off ? 1 : 2, NULL);
22617 if (loc)
22619 if (off)
22621 /* Optimize the common case. */
22622 if (single_element_loc_list_p (loc)
22623 && loc->expr->dw_loc_opc == DW_OP_addr
22624 && loc->expr->dw_loc_next == NULL
22625 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
22626 == SYMBOL_REF)
22628 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
22629 loc->expr->dw_loc_oprnd1.v.val_addr
22630 = plus_constant (GET_MODE (x), x , off);
22632 else
22633 loc_list_plus_const (loc, off);
22635 add_AT_location_description (var_die, DW_AT_location, loc);
22636 remove_AT (var_die, DW_AT_declaration);
22639 return;
22642 if (common_block_die_table == NULL)
22643 common_block_die_table = hash_table<block_die_hasher>::create_ggc (10);
22645 com_die_arg.decl_id = DECL_UID (com_decl);
22646 com_die_arg.die_parent = context_die;
22647 com_die = common_block_die_table->find (&com_die_arg);
22648 if (! early_dwarf)
22649 loc = loc_list_from_tree (com_decl, 2, NULL);
22650 if (com_die == NULL)
22652 const char *cnam
22653 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
22654 die_node **slot;
22656 com_die = new_die (DW_TAG_common_block, context_die, decl);
22657 add_name_and_src_coords_attributes (com_die, com_decl);
22658 if (loc)
22660 add_AT_location_description (com_die, DW_AT_location, loc);
22661 /* Avoid sharing the same loc descriptor between
22662 DW_TAG_common_block and DW_TAG_variable. */
22663 loc = loc_list_from_tree (com_decl, 2, NULL);
22665 else if (DECL_EXTERNAL (decl_or_origin))
22666 add_AT_flag (com_die, DW_AT_declaration, 1);
22667 if (want_pubnames ())
22668 add_pubname_string (cnam, com_die); /* ??? needed? */
22669 com_die->decl_id = DECL_UID (com_decl);
22670 slot = common_block_die_table->find_slot (com_die, INSERT);
22671 *slot = com_die;
22673 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
22675 add_AT_location_description (com_die, DW_AT_location, loc);
22676 loc = loc_list_from_tree (com_decl, 2, NULL);
22677 remove_AT (com_die, DW_AT_declaration);
22679 var_die = new_die (DW_TAG_variable, com_die, decl);
22680 add_name_and_src_coords_attributes (var_die, decl_or_origin);
22681 add_type_attribute (var_die, TREE_TYPE (decl_or_origin),
22682 decl_quals (decl_or_origin), false,
22683 context_die);
22684 add_AT_flag (var_die, DW_AT_external, 1);
22685 if (loc)
22687 if (off)
22689 /* Optimize the common case. */
22690 if (single_element_loc_list_p (loc)
22691 && loc->expr->dw_loc_opc == DW_OP_addr
22692 && loc->expr->dw_loc_next == NULL
22693 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
22695 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
22696 loc->expr->dw_loc_oprnd1.v.val_addr
22697 = plus_constant (GET_MODE (x), x, off);
22699 else
22700 loc_list_plus_const (loc, off);
22702 add_AT_location_description (var_die, DW_AT_location, loc);
22704 else if (DECL_EXTERNAL (decl_or_origin))
22705 add_AT_flag (var_die, DW_AT_declaration, 1);
22706 if (decl)
22707 equate_decl_number_to_die (decl, var_die);
22708 return;
22711 if (old_die)
22713 if (declaration)
22715 /* A declaration that has been previously dumped, needs no
22716 further annotations, since it doesn't need location on
22717 the second pass. */
22718 return;
22720 else if (decl_will_get_specification_p (old_die, decl, declaration)
22721 && !get_AT (old_die, DW_AT_specification))
22723 /* Fall-thru so we can make a new variable die along with a
22724 DW_AT_specification. */
22726 else if (origin && old_die->die_parent != context_die)
22728 /* If we will be creating an inlined instance, we need a
22729 new DIE that will get annotated with
22730 DW_AT_abstract_origin. Clear things so we can get a
22731 new DIE. */
22732 gcc_assert (!DECL_ABSTRACT_P (decl));
22733 old_die = NULL;
22735 else
22737 /* If a DIE was dumped early, it still needs location info.
22738 Skip to where we fill the location bits. */
22739 var_die = old_die;
22740 goto gen_variable_die_location;
22744 /* For static data members, the declaration in the class is supposed
22745 to have DW_TAG_member tag; the specification should still be
22746 DW_TAG_variable referencing the DW_TAG_member DIE. */
22747 if (declaration && class_scope_p (context_die))
22748 var_die = new_die (DW_TAG_member, context_die, decl);
22749 else
22750 var_die = new_die (DW_TAG_variable, context_die, decl);
22752 if (origin != NULL)
22753 add_abstract_origin_attribute (var_die, origin);
22755 /* Loop unrolling can create multiple blocks that refer to the same
22756 static variable, so we must test for the DW_AT_declaration flag.
22758 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
22759 copy decls and set the DECL_ABSTRACT_P flag on them instead of
22760 sharing them.
22762 ??? Duplicated blocks have been rewritten to use .debug_ranges. */
22763 else if (decl_will_get_specification_p (old_die, decl, declaration))
22765 /* This is a definition of a C++ class level static. */
22766 add_AT_specification (var_die, old_die);
22767 specialization_p = true;
22768 if (DECL_NAME (decl))
22770 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
22771 struct dwarf_file_data * file_index = lookup_filename (s.file);
22773 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
22774 add_AT_file (var_die, DW_AT_decl_file, file_index);
22776 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
22777 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
22779 if (old_die->die_tag == DW_TAG_member)
22780 add_linkage_name (var_die, decl);
22783 else
22784 add_name_and_src_coords_attributes (var_die, decl, no_linkage_name);
22786 if ((origin == NULL && !specialization_p)
22787 || (origin != NULL
22788 && !DECL_ABSTRACT_P (decl_or_origin)
22789 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
22790 decl_function_context
22791 (decl_or_origin))))
22793 tree type = TREE_TYPE (decl_or_origin);
22795 if (decl_by_reference_p (decl_or_origin))
22796 add_type_attribute (var_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
22797 context_die);
22798 else
22799 add_type_attribute (var_die, type, decl_quals (decl_or_origin), false,
22800 context_die);
22803 if (origin == NULL && !specialization_p)
22805 if (TREE_PUBLIC (decl))
22806 add_AT_flag (var_die, DW_AT_external, 1);
22808 if (DECL_ARTIFICIAL (decl))
22809 add_AT_flag (var_die, DW_AT_artificial, 1);
22811 add_accessibility_attribute (var_die, decl);
22814 if (declaration)
22815 add_AT_flag (var_die, DW_AT_declaration, 1);
22817 if (decl && (DECL_ABSTRACT_P (decl)
22818 || !old_die || is_declaration_die (old_die)))
22819 equate_decl_number_to_die (decl, var_die);
22821 gen_variable_die_location:
22822 if (! declaration
22823 && (! DECL_ABSTRACT_P (decl_or_origin)
22824 /* Local static vars are shared between all clones/inlines,
22825 so emit DW_AT_location on the abstract DIE if DECL_RTL is
22826 already set. */
22827 || (VAR_P (decl_or_origin)
22828 && TREE_STATIC (decl_or_origin)
22829 && DECL_RTL_SET_P (decl_or_origin))))
22831 if (early_dwarf)
22832 add_pubname (decl_or_origin, var_die);
22833 else
22834 add_location_or_const_value_attribute (var_die, decl_or_origin,
22835 decl == NULL);
22837 else
22838 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
22840 if ((dwarf_version >= 4 || !dwarf_strict)
22841 && lang_hooks.decls.decl_dwarf_attribute (decl_or_origin,
22842 DW_AT_const_expr) == 1
22843 && !get_AT (var_die, DW_AT_const_expr)
22844 && !specialization_p)
22845 add_AT_flag (var_die, DW_AT_const_expr, 1);
22847 if (!dwarf_strict)
22849 int inl = lang_hooks.decls.decl_dwarf_attribute (decl_or_origin,
22850 DW_AT_inline);
22851 if (inl != -1
22852 && !get_AT (var_die, DW_AT_inline)
22853 && !specialization_p)
22854 add_AT_unsigned (var_die, DW_AT_inline, inl);
22858 /* Generate a DIE to represent a named constant. */
22860 static void
22861 gen_const_die (tree decl, dw_die_ref context_die)
22863 dw_die_ref const_die;
22864 tree type = TREE_TYPE (decl);
22866 const_die = lookup_decl_die (decl);
22867 if (const_die)
22868 return;
22870 const_die = new_die (DW_TAG_constant, context_die, decl);
22871 equate_decl_number_to_die (decl, const_die);
22872 add_name_and_src_coords_attributes (const_die, decl);
22873 add_type_attribute (const_die, type, TYPE_QUAL_CONST, false, context_die);
22874 if (TREE_PUBLIC (decl))
22875 add_AT_flag (const_die, DW_AT_external, 1);
22876 if (DECL_ARTIFICIAL (decl))
22877 add_AT_flag (const_die, DW_AT_artificial, 1);
22878 tree_add_const_value_attribute_for_decl (const_die, decl);
22881 /* Generate a DIE to represent a label identifier. */
22883 static void
22884 gen_label_die (tree decl, dw_die_ref context_die)
22886 tree origin = decl_ultimate_origin (decl);
22887 dw_die_ref lbl_die = lookup_decl_die (decl);
22888 rtx insn;
22889 char label[MAX_ARTIFICIAL_LABEL_BYTES];
22891 if (!lbl_die)
22893 lbl_die = new_die (DW_TAG_label, context_die, decl);
22894 equate_decl_number_to_die (decl, lbl_die);
22896 if (origin != NULL)
22897 add_abstract_origin_attribute (lbl_die, origin);
22898 else
22899 add_name_and_src_coords_attributes (lbl_die, decl);
22902 if (DECL_ABSTRACT_P (decl))
22903 equate_decl_number_to_die (decl, lbl_die);
22904 else if (! early_dwarf)
22906 insn = DECL_RTL_IF_SET (decl);
22908 /* Deleted labels are programmer specified labels which have been
22909 eliminated because of various optimizations. We still emit them
22910 here so that it is possible to put breakpoints on them. */
22911 if (insn
22912 && (LABEL_P (insn)
22913 || ((NOTE_P (insn)
22914 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
22916 /* When optimization is enabled (via -O) some parts of the compiler
22917 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
22918 represent source-level labels which were explicitly declared by
22919 the user. This really shouldn't be happening though, so catch
22920 it if it ever does happen. */
22921 gcc_assert (!as_a<rtx_insn *> (insn)->deleted ());
22923 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
22924 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
22926 else if (insn
22927 && NOTE_P (insn)
22928 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
22929 && CODE_LABEL_NUMBER (insn) != -1)
22931 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
22932 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
22937 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
22938 attributes to the DIE for a block STMT, to describe where the inlined
22939 function was called from. This is similar to add_src_coords_attributes. */
22941 static inline void
22942 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
22944 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
22946 if (dwarf_version >= 3 || !dwarf_strict)
22948 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
22949 add_AT_unsigned (die, DW_AT_call_line, s.line);
22954 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
22955 Add low_pc and high_pc attributes to the DIE for a block STMT. */
22957 static inline void
22958 add_high_low_attributes (tree stmt, dw_die_ref die)
22960 char label[MAX_ARTIFICIAL_LABEL_BYTES];
22962 if (BLOCK_FRAGMENT_CHAIN (stmt)
22963 && (dwarf_version >= 3 || !dwarf_strict))
22965 tree chain, superblock = NULL_TREE;
22966 dw_die_ref pdie;
22967 dw_attr_node *attr = NULL;
22969 if (inlined_function_outer_scope_p (stmt))
22971 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
22972 BLOCK_NUMBER (stmt));
22973 add_AT_lbl_id (die, DW_AT_entry_pc, label);
22976 /* Optimize duplicate .debug_ranges lists or even tails of
22977 lists. If this BLOCK has same ranges as its supercontext,
22978 lookup DW_AT_ranges attribute in the supercontext (and
22979 recursively so), verify that the ranges_table contains the
22980 right values and use it instead of adding a new .debug_range. */
22981 for (chain = stmt, pdie = die;
22982 BLOCK_SAME_RANGE (chain);
22983 chain = BLOCK_SUPERCONTEXT (chain))
22985 dw_attr_node *new_attr;
22987 pdie = pdie->die_parent;
22988 if (pdie == NULL)
22989 break;
22990 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
22991 break;
22992 new_attr = get_AT (pdie, DW_AT_ranges);
22993 if (new_attr == NULL
22994 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
22995 break;
22996 attr = new_attr;
22997 superblock = BLOCK_SUPERCONTEXT (chain);
22999 if (attr != NULL
23000 && ((*ranges_table)[attr->dw_attr_val.v.val_offset].num
23001 == BLOCK_NUMBER (superblock))
23002 && BLOCK_FRAGMENT_CHAIN (superblock))
23004 unsigned long off = attr->dw_attr_val.v.val_offset;
23005 unsigned long supercnt = 0, thiscnt = 0;
23006 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
23007 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
23009 ++supercnt;
23010 gcc_checking_assert ((*ranges_table)[off + supercnt].num
23011 == BLOCK_NUMBER (chain));
23013 gcc_checking_assert ((*ranges_table)[off + supercnt + 1].num == 0);
23014 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
23015 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
23016 ++thiscnt;
23017 gcc_assert (supercnt >= thiscnt);
23018 add_AT_range_list (die, DW_AT_ranges, off + supercnt - thiscnt,
23019 false);
23020 note_rnglist_head (off + supercnt - thiscnt);
23021 return;
23024 unsigned int offset = add_ranges (stmt, true);
23025 add_AT_range_list (die, DW_AT_ranges, offset, false);
23026 note_rnglist_head (offset);
23028 bool prev_in_cold = BLOCK_IN_COLD_SECTION_P (stmt);
23029 chain = BLOCK_FRAGMENT_CHAIN (stmt);
23032 add_ranges (chain, prev_in_cold != BLOCK_IN_COLD_SECTION_P (chain));
23033 prev_in_cold = BLOCK_IN_COLD_SECTION_P (chain);
23034 chain = BLOCK_FRAGMENT_CHAIN (chain);
23036 while (chain);
23037 add_ranges (NULL);
23039 else
23041 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
23042 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
23043 BLOCK_NUMBER (stmt));
23044 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
23045 BLOCK_NUMBER (stmt));
23046 add_AT_low_high_pc (die, label, label_high, false);
23050 /* Generate a DIE for a lexical block. */
23052 static void
23053 gen_lexical_block_die (tree stmt, dw_die_ref context_die)
23055 dw_die_ref old_die = BLOCK_DIE (stmt);
23056 dw_die_ref stmt_die = NULL;
23057 if (!old_die)
23059 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
23060 BLOCK_DIE (stmt) = stmt_die;
23063 if (BLOCK_ABSTRACT (stmt))
23065 if (old_die)
23067 /* This must have been generated early and it won't even
23068 need location information since it's a DW_AT_inline
23069 function. */
23070 if (flag_checking)
23071 for (dw_die_ref c = context_die; c; c = c->die_parent)
23072 if (c->die_tag == DW_TAG_inlined_subroutine
23073 || c->die_tag == DW_TAG_subprogram)
23075 gcc_assert (get_AT (c, DW_AT_inline));
23076 break;
23078 return;
23081 else if (BLOCK_ABSTRACT_ORIGIN (stmt))
23083 /* If this is an inlined instance, create a new lexical die for
23084 anything below to attach DW_AT_abstract_origin to. */
23085 if (old_die)
23087 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
23088 BLOCK_DIE (stmt) = stmt_die;
23089 old_die = NULL;
23092 tree origin = block_ultimate_origin (stmt);
23093 if (origin != NULL_TREE && origin != stmt)
23094 add_abstract_origin_attribute (stmt_die, origin);
23097 if (old_die)
23098 stmt_die = old_die;
23100 /* A non abstract block whose blocks have already been reordered
23101 should have the instruction range for this block. If so, set the
23102 high/low attributes. */
23103 if (!early_dwarf && !BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
23105 gcc_assert (stmt_die);
23106 add_high_low_attributes (stmt, stmt_die);
23109 decls_for_scope (stmt, stmt_die);
23112 /* Generate a DIE for an inlined subprogram. */
23114 static void
23115 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die)
23117 tree decl;
23119 /* The instance of function that is effectively being inlined shall not
23120 be abstract. */
23121 gcc_assert (! BLOCK_ABSTRACT (stmt));
23123 decl = block_ultimate_origin (stmt);
23125 /* Make sure any inlined functions are known to be inlineable. */
23126 gcc_checking_assert (DECL_ABSTRACT_P (decl)
23127 || cgraph_function_possibly_inlined_p (decl));
23129 /* Emit info for the abstract instance first, if we haven't yet. We
23130 must emit this even if the block is abstract, otherwise when we
23131 emit the block below (or elsewhere), we may end up trying to emit
23132 a die whose origin die hasn't been emitted, and crashing. */
23133 dwarf2out_abstract_function (decl);
23135 if (! BLOCK_ABSTRACT (stmt))
23137 dw_die_ref subr_die
23138 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
23140 if (call_arg_locations)
23141 BLOCK_DIE (stmt) = subr_die;
23142 add_abstract_origin_attribute (subr_die, decl);
23143 if (TREE_ASM_WRITTEN (stmt))
23144 add_high_low_attributes (stmt, subr_die);
23145 add_call_src_coords_attributes (stmt, subr_die);
23147 decls_for_scope (stmt, subr_die);
23151 /* Generate a DIE for a field in a record, or structure. CTX is required: see
23152 the comment for VLR_CONTEXT. */
23154 static void
23155 gen_field_die (tree decl, struct vlr_context *ctx, dw_die_ref context_die)
23157 dw_die_ref decl_die;
23159 if (TREE_TYPE (decl) == error_mark_node)
23160 return;
23162 decl_die = new_die (DW_TAG_member, context_die, decl);
23163 add_name_and_src_coords_attributes (decl_die, decl);
23164 add_type_attribute (decl_die, member_declared_type (decl), decl_quals (decl),
23165 TYPE_REVERSE_STORAGE_ORDER (DECL_FIELD_CONTEXT (decl)),
23166 context_die);
23168 if (DECL_BIT_FIELD_TYPE (decl))
23170 add_byte_size_attribute (decl_die, decl);
23171 add_bit_size_attribute (decl_die, decl);
23172 add_bit_offset_attribute (decl_die, decl, ctx);
23175 /* If we have a variant part offset, then we are supposed to process a member
23176 of a QUAL_UNION_TYPE, which is how we represent variant parts in
23177 trees. */
23178 gcc_assert (ctx->variant_part_offset == NULL_TREE
23179 || TREE_CODE (DECL_FIELD_CONTEXT (decl)) != QUAL_UNION_TYPE);
23180 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
23181 add_data_member_location_attribute (decl_die, decl, ctx);
23183 if (DECL_ARTIFICIAL (decl))
23184 add_AT_flag (decl_die, DW_AT_artificial, 1);
23186 add_accessibility_attribute (decl_die, decl);
23188 /* Equate decl number to die, so that we can look up this decl later on. */
23189 equate_decl_number_to_die (decl, decl_die);
23192 #if 0
23193 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
23194 Use modified_type_die instead.
23195 We keep this code here just in case these types of DIEs may be needed to
23196 represent certain things in other languages (e.g. Pascal) someday. */
23198 static void
23199 gen_pointer_type_die (tree type, dw_die_ref context_die)
23201 dw_die_ref ptr_die
23202 = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
23204 equate_type_number_to_die (type, ptr_die);
23205 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
23206 context_die);
23207 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
23210 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
23211 Use modified_type_die instead.
23212 We keep this code here just in case these types of DIEs may be needed to
23213 represent certain things in other languages (e.g. Pascal) someday. */
23215 static void
23216 gen_reference_type_die (tree type, dw_die_ref context_die)
23218 dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
23220 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
23221 ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
23222 else
23223 ref_die = new_die (DW_TAG_reference_type, scope_die, type);
23225 equate_type_number_to_die (type, ref_die);
23226 add_type_attribute (ref_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
23227 context_die);
23228 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
23230 #endif
23232 /* Generate a DIE for a pointer to a member type. TYPE can be an
23233 OFFSET_TYPE, for a pointer to data member, or a RECORD_TYPE, for a
23234 pointer to member function. */
23236 static void
23237 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
23239 if (lookup_type_die (type))
23240 return;
23242 dw_die_ref ptr_die = new_die (DW_TAG_ptr_to_member_type,
23243 scope_die_for (type, context_die), type);
23245 equate_type_number_to_die (type, ptr_die);
23246 add_AT_die_ref (ptr_die, DW_AT_containing_type,
23247 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
23248 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
23249 context_die);
23251 if (TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE
23252 && TREE_CODE (TREE_TYPE (type)) != METHOD_TYPE)
23254 dw_loc_descr_ref op = new_loc_descr (DW_OP_plus, 0, 0);
23255 add_AT_loc (ptr_die, DW_AT_use_location, op);
23259 static char *producer_string;
23261 /* Return a heap allocated producer string including command line options
23262 if -grecord-gcc-switches. */
23264 static char *
23265 gen_producer_string (void)
23267 size_t j;
23268 auto_vec<const char *> switches;
23269 const char *language_string = lang_hooks.name;
23270 char *producer, *tail;
23271 const char *p;
23272 size_t len = dwarf_record_gcc_switches ? 0 : 3;
23273 size_t plen = strlen (language_string) + 1 + strlen (version_string);
23275 for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
23276 switch (save_decoded_options[j].opt_index)
23278 case OPT_o:
23279 case OPT_d:
23280 case OPT_dumpbase:
23281 case OPT_dumpdir:
23282 case OPT_auxbase:
23283 case OPT_auxbase_strip:
23284 case OPT_quiet:
23285 case OPT_version:
23286 case OPT_v:
23287 case OPT_w:
23288 case OPT_L:
23289 case OPT_D:
23290 case OPT_I:
23291 case OPT_U:
23292 case OPT_SPECIAL_unknown:
23293 case OPT_SPECIAL_ignore:
23294 case OPT_SPECIAL_program_name:
23295 case OPT_SPECIAL_input_file:
23296 case OPT_grecord_gcc_switches:
23297 case OPT_gno_record_gcc_switches:
23298 case OPT__output_pch_:
23299 case OPT_fdiagnostics_show_location_:
23300 case OPT_fdiagnostics_show_option:
23301 case OPT_fdiagnostics_show_caret:
23302 case OPT_fdiagnostics_color_:
23303 case OPT_fverbose_asm:
23304 case OPT____:
23305 case OPT__sysroot_:
23306 case OPT_nostdinc:
23307 case OPT_nostdinc__:
23308 case OPT_fpreprocessed:
23309 case OPT_fltrans_output_list_:
23310 case OPT_fresolution_:
23311 case OPT_fdebug_prefix_map_:
23312 /* Ignore these. */
23313 continue;
23314 default:
23315 if (cl_options[save_decoded_options[j].opt_index].flags
23316 & CL_NO_DWARF_RECORD)
23317 continue;
23318 gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
23319 == '-');
23320 switch (save_decoded_options[j].canonical_option[0][1])
23322 case 'M':
23323 case 'i':
23324 case 'W':
23325 continue;
23326 case 'f':
23327 if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
23328 "dump", 4) == 0)
23329 continue;
23330 break;
23331 default:
23332 break;
23334 switches.safe_push (save_decoded_options[j].orig_option_with_args_text);
23335 len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
23336 break;
23339 producer = XNEWVEC (char, plen + 1 + len + 1);
23340 tail = producer;
23341 sprintf (tail, "%s %s", language_string, version_string);
23342 tail += plen;
23344 FOR_EACH_VEC_ELT (switches, j, p)
23346 len = strlen (p);
23347 *tail = ' ';
23348 memcpy (tail + 1, p, len);
23349 tail += len + 1;
23352 *tail = '\0';
23353 return producer;
23356 /* Given a C and/or C++ language/version string return the "highest".
23357 C++ is assumed to be "higher" than C in this case. Used for merging
23358 LTO translation unit languages. */
23359 static const char *
23360 highest_c_language (const char *lang1, const char *lang2)
23362 if (strcmp ("GNU C++14", lang1) == 0 || strcmp ("GNU C++14", lang2) == 0)
23363 return "GNU C++14";
23364 if (strcmp ("GNU C++11", lang1) == 0 || strcmp ("GNU C++11", lang2) == 0)
23365 return "GNU C++11";
23366 if (strcmp ("GNU C++98", lang1) == 0 || strcmp ("GNU C++98", lang2) == 0)
23367 return "GNU C++98";
23369 if (strcmp ("GNU C11", lang1) == 0 || strcmp ("GNU C11", lang2) == 0)
23370 return "GNU C11";
23371 if (strcmp ("GNU C99", lang1) == 0 || strcmp ("GNU C99", lang2) == 0)
23372 return "GNU C99";
23373 if (strcmp ("GNU C89", lang1) == 0 || strcmp ("GNU C89", lang2) == 0)
23374 return "GNU C89";
23376 gcc_unreachable ();
23380 /* Generate the DIE for the compilation unit. */
23382 static dw_die_ref
23383 gen_compile_unit_die (const char *filename)
23385 dw_die_ref die;
23386 const char *language_string = lang_hooks.name;
23387 int language;
23389 die = new_die (DW_TAG_compile_unit, NULL, NULL);
23391 if (filename)
23393 add_name_attribute (die, filename);
23394 /* Don't add cwd for <built-in>. */
23395 if (filename[0] != '<')
23396 add_comp_dir_attribute (die);
23399 add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
23401 /* If our producer is LTO try to figure out a common language to use
23402 from the global list of translation units. */
23403 if (strcmp (language_string, "GNU GIMPLE") == 0)
23405 unsigned i;
23406 tree t;
23407 const char *common_lang = NULL;
23409 FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
23411 if (!TRANSLATION_UNIT_LANGUAGE (t))
23412 continue;
23413 if (!common_lang)
23414 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
23415 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
23417 else if (strncmp (common_lang, "GNU C", 5) == 0
23418 && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
23419 /* Mixing C and C++ is ok, use C++ in that case. */
23420 common_lang = highest_c_language (common_lang,
23421 TRANSLATION_UNIT_LANGUAGE (t));
23422 else
23424 /* Fall back to C. */
23425 common_lang = NULL;
23426 break;
23430 if (common_lang)
23431 language_string = common_lang;
23434 language = DW_LANG_C;
23435 if (strncmp (language_string, "GNU C", 5) == 0
23436 && ISDIGIT (language_string[5]))
23438 language = DW_LANG_C89;
23439 if (dwarf_version >= 3 || !dwarf_strict)
23441 if (strcmp (language_string, "GNU C89") != 0)
23442 language = DW_LANG_C99;
23444 if (dwarf_version >= 5 /* || !dwarf_strict */)
23445 if (strcmp (language_string, "GNU C11") == 0)
23446 language = DW_LANG_C11;
23449 else if (strncmp (language_string, "GNU C++", 7) == 0)
23451 language = DW_LANG_C_plus_plus;
23452 if (dwarf_version >= 5 /* || !dwarf_strict */)
23454 if (strcmp (language_string, "GNU C++11") == 0)
23455 language = DW_LANG_C_plus_plus_11;
23456 else if (strcmp (language_string, "GNU C++14") == 0)
23457 language = DW_LANG_C_plus_plus_14;
23460 else if (strcmp (language_string, "GNU F77") == 0)
23461 language = DW_LANG_Fortran77;
23462 else if (strcmp (language_string, "GNU Pascal") == 0)
23463 language = DW_LANG_Pascal83;
23464 else if (dwarf_version >= 3 || !dwarf_strict)
23466 if (strcmp (language_string, "GNU Ada") == 0)
23467 language = DW_LANG_Ada95;
23468 else if (strncmp (language_string, "GNU Fortran", 11) == 0)
23470 language = DW_LANG_Fortran95;
23471 if (dwarf_version >= 5 /* || !dwarf_strict */)
23473 if (strcmp (language_string, "GNU Fortran2003") == 0)
23474 language = DW_LANG_Fortran03;
23475 else if (strcmp (language_string, "GNU Fortran2008") == 0)
23476 language = DW_LANG_Fortran08;
23479 else if (strcmp (language_string, "GNU Java") == 0)
23480 language = DW_LANG_Java;
23481 else if (strcmp (language_string, "GNU Objective-C") == 0)
23482 language = DW_LANG_ObjC;
23483 else if (strcmp (language_string, "GNU Objective-C++") == 0)
23484 language = DW_LANG_ObjC_plus_plus;
23485 else if (dwarf_version >= 5 || !dwarf_strict)
23487 if (strcmp (language_string, "GNU Go") == 0)
23488 language = DW_LANG_Go;
23491 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
23492 else if (strncmp (language_string, "GNU Fortran", 11) == 0)
23493 language = DW_LANG_Fortran90;
23495 add_AT_unsigned (die, DW_AT_language, language);
23497 switch (language)
23499 case DW_LANG_Fortran77:
23500 case DW_LANG_Fortran90:
23501 case DW_LANG_Fortran95:
23502 case DW_LANG_Fortran03:
23503 case DW_LANG_Fortran08:
23504 /* Fortran has case insensitive identifiers and the front-end
23505 lowercases everything. */
23506 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
23507 break;
23508 default:
23509 /* The default DW_ID_case_sensitive doesn't need to be specified. */
23510 break;
23512 return die;
23515 /* Generate the DIE for a base class. */
23517 static void
23518 gen_inheritance_die (tree binfo, tree access, tree type,
23519 dw_die_ref context_die)
23521 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
23522 struct vlr_context ctx = { type, NULL };
23524 add_type_attribute (die, BINFO_TYPE (binfo), TYPE_UNQUALIFIED, false,
23525 context_die);
23526 add_data_member_location_attribute (die, binfo, &ctx);
23528 if (BINFO_VIRTUAL_P (binfo))
23529 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
23531 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
23532 children, otherwise the default is DW_ACCESS_public. In DWARF2
23533 the default has always been DW_ACCESS_private. */
23534 if (access == access_public_node)
23536 if (dwarf_version == 2
23537 || context_die->die_tag == DW_TAG_class_type)
23538 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
23540 else if (access == access_protected_node)
23541 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
23542 else if (dwarf_version > 2
23543 && context_die->die_tag != DW_TAG_class_type)
23544 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
23547 /* Return whether DECL is a FIELD_DECL that represents the variant part of a
23548 structure. */
23549 static bool
23550 is_variant_part (tree decl)
23552 return (TREE_CODE (decl) == FIELD_DECL
23553 && TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE);
23556 /* Check that OPERAND is a reference to a field in STRUCT_TYPE. If it is,
23557 return the FIELD_DECL. Return NULL_TREE otherwise. */
23559 static tree
23560 analyze_discr_in_predicate (tree operand, tree struct_type)
23562 bool continue_stripping = true;
23563 while (continue_stripping)
23564 switch (TREE_CODE (operand))
23566 CASE_CONVERT:
23567 operand = TREE_OPERAND (operand, 0);
23568 break;
23569 default:
23570 continue_stripping = false;
23571 break;
23574 /* Match field access to members of struct_type only. */
23575 if (TREE_CODE (operand) == COMPONENT_REF
23576 && TREE_CODE (TREE_OPERAND (operand, 0)) == PLACEHOLDER_EXPR
23577 && TREE_TYPE (TREE_OPERAND (operand, 0)) == struct_type
23578 && TREE_CODE (TREE_OPERAND (operand, 1)) == FIELD_DECL)
23579 return TREE_OPERAND (operand, 1);
23580 else
23581 return NULL_TREE;
23584 /* Check that SRC is a constant integer that can be represented as a native
23585 integer constant (either signed or unsigned). If so, store it into DEST and
23586 return true. Return false otherwise. */
23588 static bool
23589 get_discr_value (tree src, dw_discr_value *dest)
23591 bool is_unsigned = TYPE_UNSIGNED (TREE_TYPE (src));
23593 if (TREE_CODE (src) != INTEGER_CST
23594 || !(is_unsigned ? tree_fits_uhwi_p (src) : tree_fits_shwi_p (src)))
23595 return false;
23597 dest->pos = is_unsigned;
23598 if (is_unsigned)
23599 dest->v.uval = tree_to_uhwi (src);
23600 else
23601 dest->v.sval = tree_to_shwi (src);
23603 return true;
23606 /* Try to extract synthetic properties out of VARIANT_PART_DECL, which is a
23607 FIELD_DECL in STRUCT_TYPE that represents a variant part. If unsuccessful,
23608 store NULL_TREE in DISCR_DECL. Otherwise:
23610 - store the discriminant field in STRUCT_TYPE that controls the variant
23611 part to *DISCR_DECL
23613 - put in *DISCR_LISTS_P an array where for each variant, the item
23614 represents the corresponding matching list of discriminant values.
23616 - put in *DISCR_LISTS_LENGTH the number of variants, which is the size of
23617 the above array.
23619 Note that when the array is allocated (i.e. when the analysis is
23620 successful), it is up to the caller to free the array. */
23622 static void
23623 analyze_variants_discr (tree variant_part_decl,
23624 tree struct_type,
23625 tree *discr_decl,
23626 dw_discr_list_ref **discr_lists_p,
23627 unsigned *discr_lists_length)
23629 tree variant_part_type = TREE_TYPE (variant_part_decl);
23630 tree variant;
23631 dw_discr_list_ref *discr_lists;
23632 unsigned i;
23634 /* Compute how many variants there are in this variant part. */
23635 *discr_lists_length = 0;
23636 for (variant = TYPE_FIELDS (variant_part_type);
23637 variant != NULL_TREE;
23638 variant = DECL_CHAIN (variant))
23639 ++*discr_lists_length;
23641 *discr_decl = NULL_TREE;
23642 *discr_lists_p
23643 = (dw_discr_list_ref *) xcalloc (*discr_lists_length,
23644 sizeof (**discr_lists_p));
23645 discr_lists = *discr_lists_p;
23647 /* And then analyze all variants to extract discriminant information for all
23648 of them. This analysis is conservative: as soon as we detect something we
23649 do not support, abort everything and pretend we found nothing. */
23650 for (variant = TYPE_FIELDS (variant_part_type), i = 0;
23651 variant != NULL_TREE;
23652 variant = DECL_CHAIN (variant), ++i)
23654 tree match_expr = DECL_QUALIFIER (variant);
23656 /* Now, try to analyze the predicate and deduce a discriminant for
23657 it. */
23658 if (match_expr == boolean_true_node)
23659 /* Typically happens for the default variant: it matches all cases that
23660 previous variants rejected. Don't output any matching value for
23661 this one. */
23662 continue;
23664 /* The following loop tries to iterate over each discriminant
23665 possibility: single values or ranges. */
23666 while (match_expr != NULL_TREE)
23668 tree next_round_match_expr;
23669 tree candidate_discr = NULL_TREE;
23670 dw_discr_list_ref new_node = NULL;
23672 /* Possibilities are matched one after the other by nested
23673 TRUTH_ORIF_EXPR expressions. Process the current possibility and
23674 continue with the rest at next iteration. */
23675 if (TREE_CODE (match_expr) == TRUTH_ORIF_EXPR)
23677 next_round_match_expr = TREE_OPERAND (match_expr, 0);
23678 match_expr = TREE_OPERAND (match_expr, 1);
23680 else
23681 next_round_match_expr = NULL_TREE;
23683 if (match_expr == boolean_false_node)
23684 /* This sub-expression matches nothing: just wait for the next
23685 one. */
23688 else if (TREE_CODE (match_expr) == EQ_EXPR)
23690 /* We are matching: <discr_field> == <integer_cst>
23691 This sub-expression matches a single value. */
23692 tree integer_cst = TREE_OPERAND (match_expr, 1);
23694 candidate_discr
23695 = analyze_discr_in_predicate (TREE_OPERAND (match_expr, 0),
23696 struct_type);
23698 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
23699 if (!get_discr_value (integer_cst,
23700 &new_node->dw_discr_lower_bound))
23701 goto abort;
23702 new_node->dw_discr_range = false;
23705 else if (TREE_CODE (match_expr) == TRUTH_ANDIF_EXPR)
23707 /* We are matching:
23708 <discr_field> > <integer_cst>
23709 && <discr_field> < <integer_cst>.
23710 This sub-expression matches the range of values between the
23711 two matched integer constants. Note that comparisons can be
23712 inclusive or exclusive. */
23713 tree candidate_discr_1, candidate_discr_2;
23714 tree lower_cst, upper_cst;
23715 bool lower_cst_included, upper_cst_included;
23716 tree lower_op = TREE_OPERAND (match_expr, 0);
23717 tree upper_op = TREE_OPERAND (match_expr, 1);
23719 /* When the comparison is exclusive, the integer constant is not
23720 the discriminant range bound we are looking for: we will have
23721 to increment or decrement it. */
23722 if (TREE_CODE (lower_op) == GE_EXPR)
23723 lower_cst_included = true;
23724 else if (TREE_CODE (lower_op) == GT_EXPR)
23725 lower_cst_included = false;
23726 else
23727 goto abort;
23729 if (TREE_CODE (upper_op) == LE_EXPR)
23730 upper_cst_included = true;
23731 else if (TREE_CODE (upper_op) == LT_EXPR)
23732 upper_cst_included = false;
23733 else
23734 goto abort;
23736 /* Extract the discriminant from the first operand and check it
23737 is consistant with the same analysis in the second
23738 operand. */
23739 candidate_discr_1
23740 = analyze_discr_in_predicate (TREE_OPERAND (lower_op, 0),
23741 struct_type);
23742 candidate_discr_2
23743 = analyze_discr_in_predicate (TREE_OPERAND (upper_op, 0),
23744 struct_type);
23745 if (candidate_discr_1 == candidate_discr_2)
23746 candidate_discr = candidate_discr_1;
23747 else
23748 goto abort;
23750 /* Extract bounds from both. */
23751 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
23752 lower_cst = TREE_OPERAND (lower_op, 1);
23753 upper_cst = TREE_OPERAND (upper_op, 1);
23755 if (!lower_cst_included)
23756 lower_cst
23757 = fold_build2 (PLUS_EXPR, TREE_TYPE (lower_cst), lower_cst,
23758 build_int_cst (TREE_TYPE (lower_cst), 1));
23759 if (!upper_cst_included)
23760 upper_cst
23761 = fold_build2 (MINUS_EXPR, TREE_TYPE (upper_cst), upper_cst,
23762 build_int_cst (TREE_TYPE (upper_cst), 1));
23764 if (!get_discr_value (lower_cst,
23765 &new_node->dw_discr_lower_bound)
23766 || !get_discr_value (upper_cst,
23767 &new_node->dw_discr_upper_bound))
23768 goto abort;
23770 new_node->dw_discr_range = true;
23773 else
23774 /* Unsupported sub-expression: we cannot determine the set of
23775 matching discriminant values. Abort everything. */
23776 goto abort;
23778 /* If the discriminant info is not consistant with what we saw so
23779 far, consider the analysis failed and abort everything. */
23780 if (candidate_discr == NULL_TREE
23781 || (*discr_decl != NULL_TREE && candidate_discr != *discr_decl))
23782 goto abort;
23783 else
23784 *discr_decl = candidate_discr;
23786 if (new_node != NULL)
23788 new_node->dw_discr_next = discr_lists[i];
23789 discr_lists[i] = new_node;
23791 match_expr = next_round_match_expr;
23795 /* If we reach this point, we could match everything we were interested
23796 in. */
23797 return;
23799 abort:
23800 /* Clean all data structure and return no result. */
23801 free (*discr_lists_p);
23802 *discr_lists_p = NULL;
23803 *discr_decl = NULL_TREE;
23806 /* Generate a DIE to represent VARIANT_PART_DECL, a variant part that is part
23807 of STRUCT_TYPE, a record type. This new DIE is emitted as the next child
23808 under CONTEXT_DIE.
23810 Variant parts are supposed to be implemented as a FIELD_DECL whose type is a
23811 QUAL_UNION_TYPE: this is the VARIANT_PART_DECL parameter. The members for
23812 this type, which are record types, represent the available variants and each
23813 has a DECL_QUALIFIER attribute. The discriminant and the discriminant
23814 values are inferred from these attributes.
23816 In trees, the offsets for the fields inside these sub-records are relative
23817 to the variant part itself, whereas the corresponding DIEs should have
23818 offset attributes that are relative to the embedding record base address.
23819 This is why the caller must provide a VARIANT_PART_OFFSET expression: it
23820 must be an expression that computes the offset of the variant part to
23821 describe in DWARF. */
23823 static void
23824 gen_variant_part (tree variant_part_decl, struct vlr_context *vlr_ctx,
23825 dw_die_ref context_die)
23827 const tree variant_part_type = TREE_TYPE (variant_part_decl);
23828 tree variant_part_offset = vlr_ctx->variant_part_offset;
23829 struct loc_descr_context ctx = {
23830 vlr_ctx->struct_type, /* context_type */
23831 NULL_TREE, /* base_decl */
23832 NULL, /* dpi */
23833 false, /* placeholder_arg */
23834 false /* placeholder_seen */
23837 /* The FIELD_DECL node in STRUCT_TYPE that acts as the discriminant, or
23838 NULL_TREE if there is no such field. */
23839 tree discr_decl = NULL_TREE;
23840 dw_discr_list_ref *discr_lists;
23841 unsigned discr_lists_length = 0;
23842 unsigned i;
23844 dw_die_ref dwarf_proc_die = NULL;
23845 dw_die_ref variant_part_die
23846 = new_die (DW_TAG_variant_part, context_die, variant_part_type);
23848 equate_decl_number_to_die (variant_part_decl, variant_part_die);
23850 analyze_variants_discr (variant_part_decl, vlr_ctx->struct_type,
23851 &discr_decl, &discr_lists, &discr_lists_length);
23853 if (discr_decl != NULL_TREE)
23855 dw_die_ref discr_die = lookup_decl_die (discr_decl);
23857 if (discr_die)
23858 add_AT_die_ref (variant_part_die, DW_AT_discr, discr_die);
23859 else
23860 /* We have no DIE for the discriminant, so just discard all
23861 discrimimant information in the output. */
23862 discr_decl = NULL_TREE;
23865 /* If the offset for this variant part is more complex than a constant,
23866 create a DWARF procedure for it so that we will not have to generate DWARF
23867 expressions for it for each member. */
23868 if (TREE_CODE (variant_part_offset) != INTEGER_CST
23869 && (dwarf_version >= 3 || !dwarf_strict))
23871 const tree dwarf_proc_fndecl
23872 = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL, NULL_TREE,
23873 build_function_type (TREE_TYPE (variant_part_offset),
23874 NULL_TREE));
23875 const tree dwarf_proc_call = build_call_expr (dwarf_proc_fndecl, 0);
23876 const dw_loc_descr_ref dwarf_proc_body
23877 = loc_descriptor_from_tree (variant_part_offset, 0, &ctx);
23879 dwarf_proc_die = new_dwarf_proc_die (dwarf_proc_body,
23880 dwarf_proc_fndecl, context_die);
23881 if (dwarf_proc_die != NULL)
23882 variant_part_offset = dwarf_proc_call;
23885 /* Output DIEs for all variants. */
23886 i = 0;
23887 for (tree variant = TYPE_FIELDS (variant_part_type);
23888 variant != NULL_TREE;
23889 variant = DECL_CHAIN (variant), ++i)
23891 tree variant_type = TREE_TYPE (variant);
23892 dw_die_ref variant_die;
23894 /* All variants (i.e. members of a variant part) are supposed to be
23895 encoded as structures. Sub-variant parts are QUAL_UNION_TYPE fields
23896 under these records. */
23897 gcc_assert (TREE_CODE (variant_type) == RECORD_TYPE);
23899 variant_die = new_die (DW_TAG_variant, variant_part_die, variant_type);
23900 equate_decl_number_to_die (variant, variant_die);
23902 /* Output discriminant values this variant matches, if any. */
23903 if (discr_decl == NULL || discr_lists[i] == NULL)
23904 /* In the case we have discriminant information at all, this is
23905 probably the default variant: as the standard says, don't
23906 output any discriminant value/list attribute. */
23908 else if (discr_lists[i]->dw_discr_next == NULL
23909 && !discr_lists[i]->dw_discr_range)
23910 /* If there is only one accepted value, don't bother outputting a
23911 list. */
23912 add_discr_value (variant_die, &discr_lists[i]->dw_discr_lower_bound);
23913 else
23914 add_discr_list (variant_die, discr_lists[i]);
23916 for (tree member = TYPE_FIELDS (variant_type);
23917 member != NULL_TREE;
23918 member = DECL_CHAIN (member))
23920 struct vlr_context vlr_sub_ctx = {
23921 vlr_ctx->struct_type, /* struct_type */
23922 NULL /* variant_part_offset */
23924 if (is_variant_part (member))
23926 /* All offsets for fields inside variant parts are relative to
23927 the top-level embedding RECORD_TYPE's base address. On the
23928 other hand, offsets in GCC's types are relative to the
23929 nested-most variant part. So we have to sum offsets each time
23930 we recurse. */
23932 vlr_sub_ctx.variant_part_offset
23933 = fold_build2 (PLUS_EXPR, TREE_TYPE (variant_part_offset),
23934 variant_part_offset, byte_position (member));
23935 gen_variant_part (member, &vlr_sub_ctx, variant_die);
23937 else
23939 vlr_sub_ctx.variant_part_offset = variant_part_offset;
23940 gen_decl_die (member, NULL, &vlr_sub_ctx, variant_die);
23945 free (discr_lists);
23948 /* Generate a DIE for a class member. */
23950 static void
23951 gen_member_die (tree type, dw_die_ref context_die)
23953 tree member;
23954 tree binfo = TYPE_BINFO (type);
23955 dw_die_ref child;
23957 /* If this is not an incomplete type, output descriptions of each of its
23958 members. Note that as we output the DIEs necessary to represent the
23959 members of this record or union type, we will also be trying to output
23960 DIEs to represent the *types* of those members. However the `type'
23961 function (above) will specifically avoid generating type DIEs for member
23962 types *within* the list of member DIEs for this (containing) type except
23963 for those types (of members) which are explicitly marked as also being
23964 members of this (containing) type themselves. The g++ front- end can
23965 force any given type to be treated as a member of some other (containing)
23966 type by setting the TYPE_CONTEXT of the given (member) type to point to
23967 the TREE node representing the appropriate (containing) type. */
23969 /* First output info about the base classes. */
23970 if (binfo)
23972 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
23973 int i;
23974 tree base;
23976 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
23977 gen_inheritance_die (base,
23978 (accesses ? (*accesses)[i] : access_public_node),
23979 type,
23980 context_die);
23983 /* Now output info about the data members and type members. */
23984 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
23986 struct vlr_context vlr_ctx = { type, NULL_TREE };
23988 /* If we thought we were generating minimal debug info for TYPE
23989 and then changed our minds, some of the member declarations
23990 may have already been defined. Don't define them again, but
23991 do put them in the right order. */
23993 child = lookup_decl_die (member);
23994 if (child)
23996 /* Handle inline static data members, which only have in-class
23997 declarations. */
23998 if (child->die_tag == DW_TAG_variable
23999 && child->die_parent == comp_unit_die ()
24000 && get_AT (child, DW_AT_specification) == NULL)
24002 reparent_child (child, context_die);
24003 child->die_tag = DW_TAG_member;
24005 else
24006 splice_child_die (context_die, child);
24009 /* Do not generate standard DWARF for variant parts if we are generating
24010 the corresponding GNAT encodings: DIEs generated for both would
24011 conflict in our mappings. */
24012 else if (is_variant_part (member)
24013 && gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL)
24015 vlr_ctx.variant_part_offset = byte_position (member);
24016 gen_variant_part (member, &vlr_ctx, context_die);
24018 else
24020 vlr_ctx.variant_part_offset = NULL_TREE;
24021 gen_decl_die (member, NULL, &vlr_ctx, context_die);
24024 /* For C++ inline static data members emit immediately a DW_TAG_variable
24025 DIE that will refer to that DW_TAG_member through
24026 DW_AT_specification. */
24027 if (TREE_STATIC (member)
24028 && (lang_hooks.decls.decl_dwarf_attribute (member, DW_AT_inline)
24029 != -1))
24031 int old_extern = DECL_EXTERNAL (member);
24032 DECL_EXTERNAL (member) = 0;
24033 gen_decl_die (member, NULL, NULL, comp_unit_die ());
24034 DECL_EXTERNAL (member) = old_extern;
24038 /* We do not keep type methods in type variants. */
24039 gcc_assert (TYPE_MAIN_VARIANT (type) == type);
24040 /* Now output info about the function members (if any). */
24041 if (TYPE_METHODS (type) != error_mark_node)
24042 for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
24044 /* Don't include clones in the member list. */
24045 if (DECL_ABSTRACT_ORIGIN (member))
24046 continue;
24047 /* Nor constructors for anonymous classes. */
24048 if (DECL_ARTIFICIAL (member)
24049 && dwarf2_name (member, 0) == NULL)
24050 continue;
24052 child = lookup_decl_die (member);
24053 if (child)
24054 splice_child_die (context_die, child);
24055 else
24056 gen_decl_die (member, NULL, NULL, context_die);
24060 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
24061 is set, we pretend that the type was never defined, so we only get the
24062 member DIEs needed by later specification DIEs. */
24064 static void
24065 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
24066 enum debug_info_usage usage)
24068 if (TREE_ASM_WRITTEN (type))
24070 /* Fill in the bound of variable-length fields in late dwarf if
24071 still incomplete. */
24072 if (!early_dwarf && variably_modified_type_p (type, NULL))
24073 for (tree member = TYPE_FIELDS (type);
24074 member;
24075 member = DECL_CHAIN (member))
24076 fill_variable_array_bounds (TREE_TYPE (member));
24077 return;
24080 dw_die_ref type_die = lookup_type_die (type);
24081 dw_die_ref scope_die = 0;
24082 int nested = 0;
24083 int complete = (TYPE_SIZE (type)
24084 && (! TYPE_STUB_DECL (type)
24085 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
24086 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
24087 complete = complete && should_emit_struct_debug (type, usage);
24089 if (type_die && ! complete)
24090 return;
24092 if (TYPE_CONTEXT (type) != NULL_TREE
24093 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
24094 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
24095 nested = 1;
24097 scope_die = scope_die_for (type, context_die);
24099 /* Generate child dies for template paramaters. */
24100 if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
24101 schedule_generic_params_dies_gen (type);
24103 if (! type_die || (nested && is_cu_die (scope_die)))
24104 /* First occurrence of type or toplevel definition of nested class. */
24106 dw_die_ref old_die = type_die;
24108 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
24109 ? record_type_tag (type) : DW_TAG_union_type,
24110 scope_die, type);
24111 equate_type_number_to_die (type, type_die);
24112 if (old_die)
24113 add_AT_specification (type_die, old_die);
24114 else
24115 add_name_attribute (type_die, type_tag (type));
24117 else
24118 remove_AT (type_die, DW_AT_declaration);
24120 /* If this type has been completed, then give it a byte_size attribute and
24121 then give a list of members. */
24122 if (complete && !ns_decl)
24124 /* Prevent infinite recursion in cases where the type of some member of
24125 this type is expressed in terms of this type itself. */
24126 TREE_ASM_WRITTEN (type) = 1;
24127 add_byte_size_attribute (type_die, type);
24128 if (TYPE_STUB_DECL (type) != NULL_TREE)
24130 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
24131 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
24134 /* If the first reference to this type was as the return type of an
24135 inline function, then it may not have a parent. Fix this now. */
24136 if (type_die->die_parent == NULL)
24137 add_child_die (scope_die, type_die);
24139 push_decl_scope (type);
24140 gen_member_die (type, type_die);
24141 pop_decl_scope ();
24143 add_gnat_descriptive_type_attribute (type_die, type, context_die);
24144 if (TYPE_ARTIFICIAL (type))
24145 add_AT_flag (type_die, DW_AT_artificial, 1);
24147 /* GNU extension: Record what type our vtable lives in. */
24148 if (TYPE_VFIELD (type))
24150 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
24152 gen_type_die (vtype, context_die);
24153 add_AT_die_ref (type_die, DW_AT_containing_type,
24154 lookup_type_die (vtype));
24157 else
24159 add_AT_flag (type_die, DW_AT_declaration, 1);
24161 /* We don't need to do this for function-local types. */
24162 if (TYPE_STUB_DECL (type)
24163 && ! decl_function_context (TYPE_STUB_DECL (type)))
24164 vec_safe_push (incomplete_types, type);
24167 if (get_AT (type_die, DW_AT_name))
24168 add_pubtype (type, type_die);
24171 /* Generate a DIE for a subroutine _type_. */
24173 static void
24174 gen_subroutine_type_die (tree type, dw_die_ref context_die)
24176 tree return_type = TREE_TYPE (type);
24177 dw_die_ref subr_die
24178 = new_die (DW_TAG_subroutine_type,
24179 scope_die_for (type, context_die), type);
24181 equate_type_number_to_die (type, subr_die);
24182 add_prototyped_attribute (subr_die, type);
24183 add_type_attribute (subr_die, return_type, TYPE_UNQUALIFIED, false,
24184 context_die);
24185 gen_formal_types_die (type, subr_die);
24187 if (get_AT (subr_die, DW_AT_name))
24188 add_pubtype (type, subr_die);
24189 if ((dwarf_version >= 5 || !dwarf_strict)
24190 && lang_hooks.types.type_dwarf_attribute (type, DW_AT_reference) != -1)
24191 add_AT_flag (subr_die, DW_AT_reference, 1);
24192 if ((dwarf_version >= 5 || !dwarf_strict)
24193 && lang_hooks.types.type_dwarf_attribute (type,
24194 DW_AT_rvalue_reference) != -1)
24195 add_AT_flag (subr_die, DW_AT_rvalue_reference, 1);
24198 /* Generate a DIE for a type definition. */
24200 static void
24201 gen_typedef_die (tree decl, dw_die_ref context_die)
24203 dw_die_ref type_die;
24204 tree origin;
24206 if (TREE_ASM_WRITTEN (decl))
24208 if (DECL_ORIGINAL_TYPE (decl))
24209 fill_variable_array_bounds (DECL_ORIGINAL_TYPE (decl));
24210 return;
24213 TREE_ASM_WRITTEN (decl) = 1;
24214 type_die = new_die (DW_TAG_typedef, context_die, decl);
24215 origin = decl_ultimate_origin (decl);
24216 if (origin != NULL)
24217 add_abstract_origin_attribute (type_die, origin);
24218 else
24220 tree type;
24222 add_name_and_src_coords_attributes (type_die, decl);
24223 if (DECL_ORIGINAL_TYPE (decl))
24225 type = DECL_ORIGINAL_TYPE (decl);
24227 if (type == error_mark_node)
24228 return;
24230 gcc_assert (type != TREE_TYPE (decl));
24231 equate_type_number_to_die (TREE_TYPE (decl), type_die);
24233 else
24235 type = TREE_TYPE (decl);
24237 if (type == error_mark_node)
24238 return;
24240 if (is_naming_typedef_decl (TYPE_NAME (type)))
24242 /* Here, we are in the case of decl being a typedef naming
24243 an anonymous type, e.g:
24244 typedef struct {...} foo;
24245 In that case TREE_TYPE (decl) is not a typedef variant
24246 type and TYPE_NAME of the anonymous type is set to the
24247 TYPE_DECL of the typedef. This construct is emitted by
24248 the C++ FE.
24250 TYPE is the anonymous struct named by the typedef
24251 DECL. As we need the DW_AT_type attribute of the
24252 DW_TAG_typedef to point to the DIE of TYPE, let's
24253 generate that DIE right away. add_type_attribute
24254 called below will then pick (via lookup_type_die) that
24255 anonymous struct DIE. */
24256 if (!TREE_ASM_WRITTEN (type))
24257 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
24259 /* This is a GNU Extension. We are adding a
24260 DW_AT_linkage_name attribute to the DIE of the
24261 anonymous struct TYPE. The value of that attribute
24262 is the name of the typedef decl naming the anonymous
24263 struct. This greatly eases the work of consumers of
24264 this debug info. */
24265 add_linkage_name_raw (lookup_type_die (type), decl);
24269 add_type_attribute (type_die, type, decl_quals (decl), false,
24270 context_die);
24272 if (is_naming_typedef_decl (decl))
24273 /* We want that all subsequent calls to lookup_type_die with
24274 TYPE in argument yield the DW_TAG_typedef we have just
24275 created. */
24276 equate_type_number_to_die (type, type_die);
24278 add_accessibility_attribute (type_die, decl);
24281 if (DECL_ABSTRACT_P (decl))
24282 equate_decl_number_to_die (decl, type_die);
24284 if (get_AT (type_die, DW_AT_name))
24285 add_pubtype (decl, type_die);
24288 /* Generate a DIE for a struct, class, enum or union type. */
24290 static void
24291 gen_tagged_type_die (tree type,
24292 dw_die_ref context_die,
24293 enum debug_info_usage usage)
24295 int need_pop;
24297 if (type == NULL_TREE
24298 || !is_tagged_type (type))
24299 return;
24301 if (TREE_ASM_WRITTEN (type))
24302 need_pop = 0;
24303 /* If this is a nested type whose containing class hasn't been written
24304 out yet, writing it out will cover this one, too. This does not apply
24305 to instantiations of member class templates; they need to be added to
24306 the containing class as they are generated. FIXME: This hurts the
24307 idea of combining type decls from multiple TUs, since we can't predict
24308 what set of template instantiations we'll get. */
24309 else if (TYPE_CONTEXT (type)
24310 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
24311 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
24313 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
24315 if (TREE_ASM_WRITTEN (type))
24316 return;
24318 /* If that failed, attach ourselves to the stub. */
24319 push_decl_scope (TYPE_CONTEXT (type));
24320 context_die = lookup_type_die (TYPE_CONTEXT (type));
24321 need_pop = 1;
24323 else if (TYPE_CONTEXT (type) != NULL_TREE
24324 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
24326 /* If this type is local to a function that hasn't been written
24327 out yet, use a NULL context for now; it will be fixed up in
24328 decls_for_scope. */
24329 context_die = lookup_decl_die (TYPE_CONTEXT (type));
24330 /* A declaration DIE doesn't count; nested types need to go in the
24331 specification. */
24332 if (context_die && is_declaration_die (context_die))
24333 context_die = NULL;
24334 need_pop = 0;
24336 else
24338 context_die = declare_in_namespace (type, context_die);
24339 need_pop = 0;
24342 if (TREE_CODE (type) == ENUMERAL_TYPE)
24344 /* This might have been written out by the call to
24345 declare_in_namespace. */
24346 if (!TREE_ASM_WRITTEN (type))
24347 gen_enumeration_type_die (type, context_die);
24349 else
24350 gen_struct_or_union_type_die (type, context_die, usage);
24352 if (need_pop)
24353 pop_decl_scope ();
24355 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
24356 it up if it is ever completed. gen_*_type_die will set it for us
24357 when appropriate. */
24360 /* Generate a type description DIE. */
24362 static void
24363 gen_type_die_with_usage (tree type, dw_die_ref context_die,
24364 enum debug_info_usage usage)
24366 struct array_descr_info info;
24368 if (type == NULL_TREE || type == error_mark_node)
24369 return;
24371 if (flag_checking && type)
24372 verify_type (type);
24374 if (TYPE_NAME (type) != NULL_TREE
24375 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
24376 && is_redundant_typedef (TYPE_NAME (type))
24377 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
24378 /* The DECL of this type is a typedef we don't want to emit debug
24379 info for but we want debug info for its underlying typedef.
24380 This can happen for e.g, the injected-class-name of a C++
24381 type. */
24382 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
24384 /* If TYPE is a typedef type variant, let's generate debug info
24385 for the parent typedef which TYPE is a type of. */
24386 if (typedef_variant_p (type))
24388 if (TREE_ASM_WRITTEN (type))
24389 return;
24391 /* Prevent broken recursion; we can't hand off to the same type. */
24392 gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
24394 /* Give typedefs the right scope. */
24395 context_die = scope_die_for (type, context_die);
24397 TREE_ASM_WRITTEN (type) = 1;
24399 gen_decl_die (TYPE_NAME (type), NULL, NULL, context_die);
24400 return;
24403 /* If type is an anonymous tagged type named by a typedef, let's
24404 generate debug info for the typedef. */
24405 if (is_naming_typedef_decl (TYPE_NAME (type)))
24407 /* Use the DIE of the containing namespace as the parent DIE of
24408 the type description DIE we want to generate. */
24409 if (DECL_CONTEXT (TYPE_NAME (type))
24410 && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
24411 context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
24413 gen_decl_die (TYPE_NAME (type), NULL, NULL, context_die);
24414 return;
24417 if (lang_hooks.types.get_debug_type)
24419 tree debug_type = lang_hooks.types.get_debug_type (type);
24421 if (debug_type != NULL_TREE && debug_type != type)
24423 gen_type_die_with_usage (debug_type, context_die, usage);
24424 return;
24428 /* We are going to output a DIE to represent the unqualified version
24429 of this type (i.e. without any const or volatile qualifiers) so
24430 get the main variant (i.e. the unqualified version) of this type
24431 now. (Vectors and arrays are special because the debugging info is in the
24432 cloned type itself. Similarly function/method types can contain extra
24433 ref-qualification). */
24434 if (TREE_CODE (type) == FUNCTION_TYPE
24435 || TREE_CODE (type) == METHOD_TYPE)
24437 /* For function/method types, can't use type_main_variant here,
24438 because that can have different ref-qualifiers for C++,
24439 but try to canonicalize. */
24440 tree main = TYPE_MAIN_VARIANT (type);
24441 for (tree t = main; t; t = TYPE_NEXT_VARIANT (t))
24442 if (check_base_type (t, main) && check_lang_type (t, type))
24443 type = t;
24445 else if (TREE_CODE (type) != VECTOR_TYPE
24446 && TREE_CODE (type) != ARRAY_TYPE)
24447 type = type_main_variant (type);
24449 /* If this is an array type with hidden descriptor, handle it first. */
24450 if (!TREE_ASM_WRITTEN (type)
24451 && lang_hooks.types.get_array_descr_info)
24453 memset (&info, 0, sizeof (info));
24454 if (lang_hooks.types.get_array_descr_info (type, &info))
24456 /* Fortran sometimes emits array types with no dimension. */
24457 gcc_assert (info.ndimensions >= 0
24458 && (info.ndimensions
24459 <= DWARF2OUT_ARRAY_DESCR_INFO_MAX_DIMEN));
24460 gen_descr_array_type_die (type, &info, context_die);
24461 TREE_ASM_WRITTEN (type) = 1;
24462 return;
24466 if (TREE_ASM_WRITTEN (type))
24468 /* Variable-length types may be incomplete even if
24469 TREE_ASM_WRITTEN. For such types, fall through to
24470 gen_array_type_die() and possibly fill in
24471 DW_AT_{upper,lower}_bound attributes. */
24472 if ((TREE_CODE (type) != ARRAY_TYPE
24473 && TREE_CODE (type) != RECORD_TYPE
24474 && TREE_CODE (type) != UNION_TYPE
24475 && TREE_CODE (type) != QUAL_UNION_TYPE)
24476 || !variably_modified_type_p (type, NULL))
24477 return;
24480 switch (TREE_CODE (type))
24482 case ERROR_MARK:
24483 break;
24485 case POINTER_TYPE:
24486 case REFERENCE_TYPE:
24487 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
24488 ensures that the gen_type_die recursion will terminate even if the
24489 type is recursive. Recursive types are possible in Ada. */
24490 /* ??? We could perhaps do this for all types before the switch
24491 statement. */
24492 TREE_ASM_WRITTEN (type) = 1;
24494 /* For these types, all that is required is that we output a DIE (or a
24495 set of DIEs) to represent the "basis" type. */
24496 gen_type_die_with_usage (TREE_TYPE (type), context_die,
24497 DINFO_USAGE_IND_USE);
24498 break;
24500 case OFFSET_TYPE:
24501 /* This code is used for C++ pointer-to-data-member types.
24502 Output a description of the relevant class type. */
24503 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
24504 DINFO_USAGE_IND_USE);
24506 /* Output a description of the type of the object pointed to. */
24507 gen_type_die_with_usage (TREE_TYPE (type), context_die,
24508 DINFO_USAGE_IND_USE);
24510 /* Now output a DIE to represent this pointer-to-data-member type
24511 itself. */
24512 gen_ptr_to_mbr_type_die (type, context_die);
24513 break;
24515 case FUNCTION_TYPE:
24516 /* Force out return type (in case it wasn't forced out already). */
24517 gen_type_die_with_usage (TREE_TYPE (type), context_die,
24518 DINFO_USAGE_DIR_USE);
24519 gen_subroutine_type_die (type, context_die);
24520 break;
24522 case METHOD_TYPE:
24523 /* Force out return type (in case it wasn't forced out already). */
24524 gen_type_die_with_usage (TREE_TYPE (type), context_die,
24525 DINFO_USAGE_DIR_USE);
24526 gen_subroutine_type_die (type, context_die);
24527 break;
24529 case ARRAY_TYPE:
24530 case VECTOR_TYPE:
24531 gen_array_type_die (type, context_die);
24532 break;
24534 case ENUMERAL_TYPE:
24535 case RECORD_TYPE:
24536 case UNION_TYPE:
24537 case QUAL_UNION_TYPE:
24538 gen_tagged_type_die (type, context_die, usage);
24539 return;
24541 case VOID_TYPE:
24542 case INTEGER_TYPE:
24543 case REAL_TYPE:
24544 case FIXED_POINT_TYPE:
24545 case COMPLEX_TYPE:
24546 case BOOLEAN_TYPE:
24547 case POINTER_BOUNDS_TYPE:
24548 /* No DIEs needed for fundamental types. */
24549 break;
24551 case NULLPTR_TYPE:
24552 case LANG_TYPE:
24553 /* Just use DW_TAG_unspecified_type. */
24555 dw_die_ref type_die = lookup_type_die (type);
24556 if (type_die == NULL)
24558 tree name = TYPE_IDENTIFIER (type);
24559 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (),
24560 type);
24561 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
24562 equate_type_number_to_die (type, type_die);
24565 break;
24567 default:
24568 if (is_cxx_auto (type))
24570 tree name = TYPE_IDENTIFIER (type);
24571 dw_die_ref *die = (name == get_identifier ("auto")
24572 ? &auto_die : &decltype_auto_die);
24573 if (!*die)
24575 *die = new_die (DW_TAG_unspecified_type,
24576 comp_unit_die (), NULL_TREE);
24577 add_name_attribute (*die, IDENTIFIER_POINTER (name));
24579 equate_type_number_to_die (type, *die);
24580 break;
24582 gcc_unreachable ();
24585 TREE_ASM_WRITTEN (type) = 1;
24588 static void
24589 gen_type_die (tree type, dw_die_ref context_die)
24591 if (type != error_mark_node)
24593 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
24594 if (flag_checking)
24596 dw_die_ref die = lookup_type_die (type);
24597 if (die)
24598 check_die (die);
24603 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
24604 things which are local to the given block. */
24606 static void
24607 gen_block_die (tree stmt, dw_die_ref context_die)
24609 int must_output_die = 0;
24610 bool inlined_func;
24612 /* Ignore blocks that are NULL. */
24613 if (stmt == NULL_TREE)
24614 return;
24616 inlined_func = inlined_function_outer_scope_p (stmt);
24618 /* If the block is one fragment of a non-contiguous block, do not
24619 process the variables, since they will have been done by the
24620 origin block. Do process subblocks. */
24621 if (BLOCK_FRAGMENT_ORIGIN (stmt))
24623 tree sub;
24625 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
24626 gen_block_die (sub, context_die);
24628 return;
24631 /* Determine if we need to output any Dwarf DIEs at all to represent this
24632 block. */
24633 if (inlined_func)
24634 /* The outer scopes for inlinings *must* always be represented. We
24635 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
24636 must_output_die = 1;
24637 else
24639 /* Determine if this block directly contains any "significant"
24640 local declarations which we will need to output DIEs for. */
24641 if (debug_info_level > DINFO_LEVEL_TERSE)
24642 /* We are not in terse mode so *any* local declaration counts
24643 as being a "significant" one. */
24644 must_output_die = ((BLOCK_VARS (stmt) != NULL
24645 || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
24646 && (TREE_USED (stmt)
24647 || TREE_ASM_WRITTEN (stmt)
24648 || BLOCK_ABSTRACT (stmt)));
24649 else if ((TREE_USED (stmt)
24650 || TREE_ASM_WRITTEN (stmt)
24651 || BLOCK_ABSTRACT (stmt))
24652 && !dwarf2out_ignore_block (stmt))
24653 must_output_die = 1;
24656 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
24657 DIE for any block which contains no significant local declarations at
24658 all. Rather, in such cases we just call `decls_for_scope' so that any
24659 needed Dwarf info for any sub-blocks will get properly generated. Note
24660 that in terse mode, our definition of what constitutes a "significant"
24661 local declaration gets restricted to include only inlined function
24662 instances and local (nested) function definitions. */
24663 if (must_output_die)
24665 if (inlined_func)
24667 /* If STMT block is abstract, that means we have been called
24668 indirectly from dwarf2out_abstract_function.
24669 That function rightfully marks the descendent blocks (of
24670 the abstract function it is dealing with) as being abstract,
24671 precisely to prevent us from emitting any
24672 DW_TAG_inlined_subroutine DIE as a descendent
24673 of an abstract function instance. So in that case, we should
24674 not call gen_inlined_subroutine_die.
24676 Later though, when cgraph asks dwarf2out to emit info
24677 for the concrete instance of the function decl into which
24678 the concrete instance of STMT got inlined, the later will lead
24679 to the generation of a DW_TAG_inlined_subroutine DIE. */
24680 if (! BLOCK_ABSTRACT (stmt))
24681 gen_inlined_subroutine_die (stmt, context_die);
24683 else
24684 gen_lexical_block_die (stmt, context_die);
24686 else
24687 decls_for_scope (stmt, context_die);
24690 /* Process variable DECL (or variable with origin ORIGIN) within
24691 block STMT and add it to CONTEXT_DIE. */
24692 static void
24693 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
24695 dw_die_ref die;
24696 tree decl_or_origin = decl ? decl : origin;
24698 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
24699 die = lookup_decl_die (decl_or_origin);
24700 else if (TREE_CODE (decl_or_origin) == TYPE_DECL)
24702 if (TYPE_DECL_IS_STUB (decl_or_origin))
24703 die = lookup_type_die (TREE_TYPE (decl_or_origin));
24704 else
24705 die = lookup_decl_die (decl_or_origin);
24706 /* Avoid re-creating the DIE late if it was optimized as unused early. */
24707 if (! die && ! early_dwarf)
24708 return;
24710 else
24711 die = NULL;
24713 if (die != NULL && die->die_parent == NULL)
24714 add_child_die (context_die, die);
24715 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
24717 if (early_dwarf)
24718 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
24719 stmt, context_die);
24721 else
24722 gen_decl_die (decl, origin, NULL, context_die);
24725 /* Generate all of the decls declared within a given scope and (recursively)
24726 all of its sub-blocks. */
24728 static void
24729 decls_for_scope (tree stmt, dw_die_ref context_die)
24731 tree decl;
24732 unsigned int i;
24733 tree subblocks;
24735 /* Ignore NULL blocks. */
24736 if (stmt == NULL_TREE)
24737 return;
24739 /* Output the DIEs to represent all of the data objects and typedefs
24740 declared directly within this block but not within any nested
24741 sub-blocks. Also, nested function and tag DIEs have been
24742 generated with a parent of NULL; fix that up now. We don't
24743 have to do this if we're at -g1. */
24744 if (debug_info_level > DINFO_LEVEL_TERSE)
24746 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
24747 process_scope_var (stmt, decl, NULL_TREE, context_die);
24748 /* BLOCK_NONLOCALIZED_VARs simply generate DIE stubs with abstract
24749 origin - avoid doing this twice as we have no good way to see
24750 if we've done it once already. */
24751 if (! early_dwarf)
24752 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
24753 process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
24754 context_die);
24757 /* Even if we're at -g1, we need to process the subblocks in order to get
24758 inlined call information. */
24760 /* Output the DIEs to represent all sub-blocks (and the items declared
24761 therein) of this block. */
24762 for (subblocks = BLOCK_SUBBLOCKS (stmt);
24763 subblocks != NULL;
24764 subblocks = BLOCK_CHAIN (subblocks))
24765 gen_block_die (subblocks, context_die);
24768 /* Is this a typedef we can avoid emitting? */
24770 bool
24771 is_redundant_typedef (const_tree decl)
24773 if (TYPE_DECL_IS_STUB (decl))
24774 return true;
24776 if (DECL_ARTIFICIAL (decl)
24777 && DECL_CONTEXT (decl)
24778 && is_tagged_type (DECL_CONTEXT (decl))
24779 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
24780 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
24781 /* Also ignore the artificial member typedef for the class name. */
24782 return true;
24784 return false;
24787 /* Return TRUE if TYPE is a typedef that names a type for linkage
24788 purposes. This kind of typedefs is produced by the C++ FE for
24789 constructs like:
24791 typedef struct {...} foo;
24793 In that case, there is no typedef variant type produced for foo.
24794 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
24795 struct type. */
24797 static bool
24798 is_naming_typedef_decl (const_tree decl)
24800 if (decl == NULL_TREE
24801 || TREE_CODE (decl) != TYPE_DECL
24802 || DECL_NAMELESS (decl)
24803 || !is_tagged_type (TREE_TYPE (decl))
24804 || DECL_IS_BUILTIN (decl)
24805 || is_redundant_typedef (decl)
24806 /* It looks like Ada produces TYPE_DECLs that are very similar
24807 to C++ naming typedefs but that have different
24808 semantics. Let's be specific to c++ for now. */
24809 || !is_cxx (decl))
24810 return FALSE;
24812 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
24813 && TYPE_NAME (TREE_TYPE (decl)) == decl
24814 && (TYPE_STUB_DECL (TREE_TYPE (decl))
24815 != TYPE_NAME (TREE_TYPE (decl))));
24818 /* Looks up the DIE for a context. */
24820 static inline dw_die_ref
24821 lookup_context_die (tree context)
24823 if (context)
24825 /* Find die that represents this context. */
24826 if (TYPE_P (context))
24828 context = TYPE_MAIN_VARIANT (context);
24829 dw_die_ref ctx = lookup_type_die (context);
24830 if (!ctx)
24831 return NULL;
24832 return strip_naming_typedef (context, ctx);
24834 else
24835 return lookup_decl_die (context);
24837 return comp_unit_die ();
24840 /* Returns the DIE for a context. */
24842 static inline dw_die_ref
24843 get_context_die (tree context)
24845 if (context)
24847 /* Find die that represents this context. */
24848 if (TYPE_P (context))
24850 context = TYPE_MAIN_VARIANT (context);
24851 return strip_naming_typedef (context, force_type_die (context));
24853 else
24854 return force_decl_die (context);
24856 return comp_unit_die ();
24859 /* Returns the DIE for decl. A DIE will always be returned. */
24861 static dw_die_ref
24862 force_decl_die (tree decl)
24864 dw_die_ref decl_die;
24865 unsigned saved_external_flag;
24866 tree save_fn = NULL_TREE;
24867 decl_die = lookup_decl_die (decl);
24868 if (!decl_die)
24870 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
24872 decl_die = lookup_decl_die (decl);
24873 if (decl_die)
24874 return decl_die;
24876 switch (TREE_CODE (decl))
24878 case FUNCTION_DECL:
24879 /* Clear current_function_decl, so that gen_subprogram_die thinks
24880 that this is a declaration. At this point, we just want to force
24881 declaration die. */
24882 save_fn = current_function_decl;
24883 current_function_decl = NULL_TREE;
24884 gen_subprogram_die (decl, context_die);
24885 current_function_decl = save_fn;
24886 break;
24888 case VAR_DECL:
24889 /* Set external flag to force declaration die. Restore it after
24890 gen_decl_die() call. */
24891 saved_external_flag = DECL_EXTERNAL (decl);
24892 DECL_EXTERNAL (decl) = 1;
24893 gen_decl_die (decl, NULL, NULL, context_die);
24894 DECL_EXTERNAL (decl) = saved_external_flag;
24895 break;
24897 case NAMESPACE_DECL:
24898 if (dwarf_version >= 3 || !dwarf_strict)
24899 dwarf2out_decl (decl);
24900 else
24901 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
24902 decl_die = comp_unit_die ();
24903 break;
24905 case TRANSLATION_UNIT_DECL:
24906 decl_die = comp_unit_die ();
24907 break;
24909 default:
24910 gcc_unreachable ();
24913 /* We should be able to find the DIE now. */
24914 if (!decl_die)
24915 decl_die = lookup_decl_die (decl);
24916 gcc_assert (decl_die);
24919 return decl_die;
24922 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
24923 always returned. */
24925 static dw_die_ref
24926 force_type_die (tree type)
24928 dw_die_ref type_die;
24930 type_die = lookup_type_die (type);
24931 if (!type_die)
24933 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
24935 type_die = modified_type_die (type, TYPE_QUALS_NO_ADDR_SPACE (type),
24936 false, context_die);
24937 gcc_assert (type_die);
24939 return type_die;
24942 /* Force out any required namespaces to be able to output DECL,
24943 and return the new context_die for it, if it's changed. */
24945 static dw_die_ref
24946 setup_namespace_context (tree thing, dw_die_ref context_die)
24948 tree context = (DECL_P (thing)
24949 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
24950 if (context && TREE_CODE (context) == NAMESPACE_DECL)
24951 /* Force out the namespace. */
24952 context_die = force_decl_die (context);
24954 return context_die;
24957 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
24958 type) within its namespace, if appropriate.
24960 For compatibility with older debuggers, namespace DIEs only contain
24961 declarations; all definitions are emitted at CU scope, with
24962 DW_AT_specification pointing to the declaration (like with class
24963 members). */
24965 static dw_die_ref
24966 declare_in_namespace (tree thing, dw_die_ref context_die)
24968 dw_die_ref ns_context;
24970 if (debug_info_level <= DINFO_LEVEL_TERSE)
24971 return context_die;
24973 /* External declarations in the local scope only need to be emitted
24974 once, not once in the namespace and once in the scope.
24976 This avoids declaring the `extern' below in the
24977 namespace DIE as well as in the innermost scope:
24979 namespace S
24981 int i=5;
24982 int foo()
24984 int i=8;
24985 extern int i;
24986 return i;
24990 if (DECL_P (thing) && DECL_EXTERNAL (thing) && local_scope_p (context_die))
24991 return context_die;
24993 /* If this decl is from an inlined function, then don't try to emit it in its
24994 namespace, as we will get confused. It would have already been emitted
24995 when the abstract instance of the inline function was emitted anyways. */
24996 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
24997 return context_die;
24999 ns_context = setup_namespace_context (thing, context_die);
25001 if (ns_context != context_die)
25003 if (is_fortran ())
25004 return ns_context;
25005 if (DECL_P (thing))
25006 gen_decl_die (thing, NULL, NULL, ns_context);
25007 else
25008 gen_type_die (thing, ns_context);
25010 return context_die;
25013 /* Generate a DIE for a namespace or namespace alias. */
25015 static void
25016 gen_namespace_die (tree decl, dw_die_ref context_die)
25018 dw_die_ref namespace_die;
25020 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
25021 they are an alias of. */
25022 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
25024 /* Output a real namespace or module. */
25025 context_die = setup_namespace_context (decl, comp_unit_die ());
25026 namespace_die = new_die (is_fortran ()
25027 ? DW_TAG_module : DW_TAG_namespace,
25028 context_die, decl);
25029 /* For Fortran modules defined in different CU don't add src coords. */
25030 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
25032 const char *name = dwarf2_name (decl, 0);
25033 if (name)
25034 add_name_attribute (namespace_die, name);
25036 else
25037 add_name_and_src_coords_attributes (namespace_die, decl);
25038 if (DECL_EXTERNAL (decl))
25039 add_AT_flag (namespace_die, DW_AT_declaration, 1);
25040 equate_decl_number_to_die (decl, namespace_die);
25042 else
25044 /* Output a namespace alias. */
25046 /* Force out the namespace we are an alias of, if necessary. */
25047 dw_die_ref origin_die
25048 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
25050 if (DECL_FILE_SCOPE_P (decl)
25051 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
25052 context_die = setup_namespace_context (decl, comp_unit_die ());
25053 /* Now create the namespace alias DIE. */
25054 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
25055 add_name_and_src_coords_attributes (namespace_die, decl);
25056 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
25057 equate_decl_number_to_die (decl, namespace_die);
25059 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
25060 if (want_pubnames ())
25061 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
25064 /* Generate Dwarf debug information for a decl described by DECL.
25065 The return value is currently only meaningful for PARM_DECLs,
25066 for all other decls it returns NULL.
25068 If DECL is a FIELD_DECL, CTX is required: see the comment for VLR_CONTEXT.
25069 It can be NULL otherwise. */
25071 static dw_die_ref
25072 gen_decl_die (tree decl, tree origin, struct vlr_context *ctx,
25073 dw_die_ref context_die)
25075 tree decl_or_origin = decl ? decl : origin;
25076 tree class_origin = NULL, ultimate_origin;
25078 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
25079 return NULL;
25081 /* Ignore pointer bounds decls. */
25082 if (DECL_P (decl_or_origin)
25083 && TREE_TYPE (decl_or_origin)
25084 && POINTER_BOUNDS_P (decl_or_origin))
25085 return NULL;
25087 switch (TREE_CODE (decl_or_origin))
25089 case ERROR_MARK:
25090 break;
25092 case CONST_DECL:
25093 if (!is_fortran () && !is_ada ())
25095 /* The individual enumerators of an enum type get output when we output
25096 the Dwarf representation of the relevant enum type itself. */
25097 break;
25100 /* Emit its type. */
25101 gen_type_die (TREE_TYPE (decl), context_die);
25103 /* And its containing namespace. */
25104 context_die = declare_in_namespace (decl, context_die);
25106 gen_const_die (decl, context_die);
25107 break;
25109 case FUNCTION_DECL:
25110 /* Don't output any DIEs to represent mere function declarations,
25111 unless they are class members or explicit block externs. */
25112 if (DECL_INITIAL (decl_or_origin) == NULL_TREE
25113 && DECL_FILE_SCOPE_P (decl_or_origin)
25114 && (current_function_decl == NULL_TREE
25115 || DECL_ARTIFICIAL (decl_or_origin)))
25116 break;
25118 #if 0
25119 /* FIXME */
25120 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
25121 on local redeclarations of global functions. That seems broken. */
25122 if (current_function_decl != decl)
25123 /* This is only a declaration. */;
25124 #endif
25126 /* If we're emitting a clone, emit info for the abstract instance. */
25127 if (origin || DECL_ORIGIN (decl) != decl)
25128 dwarf2out_abstract_function (origin
25129 ? DECL_ORIGIN (origin)
25130 : DECL_ABSTRACT_ORIGIN (decl));
25132 /* If we're emitting an out-of-line copy of an inline function,
25133 emit info for the abstract instance and set up to refer to it. */
25134 else if (cgraph_function_possibly_inlined_p (decl)
25135 && ! DECL_ABSTRACT_P (decl)
25136 && ! class_or_namespace_scope_p (context_die)
25137 /* dwarf2out_abstract_function won't emit a die if this is just
25138 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
25139 that case, because that works only if we have a die. */
25140 && DECL_INITIAL (decl) != NULL_TREE)
25142 dwarf2out_abstract_function (decl);
25143 set_decl_origin_self (decl);
25146 /* Otherwise we're emitting the primary DIE for this decl. */
25147 else if (debug_info_level > DINFO_LEVEL_TERSE)
25149 /* Before we describe the FUNCTION_DECL itself, make sure that we
25150 have its containing type. */
25151 if (!origin)
25152 origin = decl_class_context (decl);
25153 if (origin != NULL_TREE)
25154 gen_type_die (origin, context_die);
25156 /* And its return type. */
25157 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
25159 /* And its virtual context. */
25160 if (DECL_VINDEX (decl) != NULL_TREE)
25161 gen_type_die (DECL_CONTEXT (decl), context_die);
25163 /* Make sure we have a member DIE for decl. */
25164 if (origin != NULL_TREE)
25165 gen_type_die_for_member (origin, decl, context_die);
25167 /* And its containing namespace. */
25168 context_die = declare_in_namespace (decl, context_die);
25171 /* Now output a DIE to represent the function itself. */
25172 if (decl)
25173 gen_subprogram_die (decl, context_die);
25174 break;
25176 case TYPE_DECL:
25177 /* If we are in terse mode, don't generate any DIEs to represent any
25178 actual typedefs. */
25179 if (debug_info_level <= DINFO_LEVEL_TERSE)
25180 break;
25182 /* In the special case of a TYPE_DECL node representing the declaration
25183 of some type tag, if the given TYPE_DECL is marked as having been
25184 instantiated from some other (original) TYPE_DECL node (e.g. one which
25185 was generated within the original definition of an inline function) we
25186 used to generate a special (abbreviated) DW_TAG_structure_type,
25187 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
25188 should be actually referencing those DIEs, as variable DIEs with that
25189 type would be emitted already in the abstract origin, so it was always
25190 removed during unused type prunning. Don't add anything in this
25191 case. */
25192 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
25193 break;
25195 if (is_redundant_typedef (decl))
25196 gen_type_die (TREE_TYPE (decl), context_die);
25197 else
25198 /* Output a DIE to represent the typedef itself. */
25199 gen_typedef_die (decl, context_die);
25200 break;
25202 case LABEL_DECL:
25203 if (debug_info_level >= DINFO_LEVEL_NORMAL)
25204 gen_label_die (decl, context_die);
25205 break;
25207 case VAR_DECL:
25208 case RESULT_DECL:
25209 /* If we are in terse mode, don't generate any DIEs to represent any
25210 variable declarations or definitions. */
25211 if (debug_info_level <= DINFO_LEVEL_TERSE)
25212 break;
25214 /* Output any DIEs that are needed to specify the type of this data
25215 object. */
25216 if (decl_by_reference_p (decl_or_origin))
25217 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
25218 else
25219 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
25221 /* And its containing type. */
25222 class_origin = decl_class_context (decl_or_origin);
25223 if (class_origin != NULL_TREE)
25224 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
25226 /* And its containing namespace. */
25227 context_die = declare_in_namespace (decl_or_origin, context_die);
25229 /* Now output the DIE to represent the data object itself. This gets
25230 complicated because of the possibility that the VAR_DECL really
25231 represents an inlined instance of a formal parameter for an inline
25232 function. */
25233 ultimate_origin = decl_ultimate_origin (decl_or_origin);
25234 if (ultimate_origin != NULL_TREE
25235 && TREE_CODE (ultimate_origin) == PARM_DECL)
25236 gen_formal_parameter_die (decl, origin,
25237 true /* Emit name attribute. */,
25238 context_die);
25239 else
25240 gen_variable_die (decl, origin, context_die);
25241 break;
25243 case FIELD_DECL:
25244 gcc_assert (ctx != NULL && ctx->struct_type != NULL);
25245 /* Ignore the nameless fields that are used to skip bits but handle C++
25246 anonymous unions and structs. */
25247 if (DECL_NAME (decl) != NULL_TREE
25248 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
25249 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
25251 gen_type_die (member_declared_type (decl), context_die);
25252 gen_field_die (decl, ctx, context_die);
25254 break;
25256 case PARM_DECL:
25257 if (DECL_BY_REFERENCE (decl_or_origin))
25258 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
25259 else
25260 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
25261 return gen_formal_parameter_die (decl, origin,
25262 true /* Emit name attribute. */,
25263 context_die);
25265 case NAMESPACE_DECL:
25266 if (dwarf_version >= 3 || !dwarf_strict)
25267 gen_namespace_die (decl, context_die);
25268 break;
25270 case IMPORTED_DECL:
25271 dwarf2out_imported_module_or_decl_1 (decl, DECL_NAME (decl),
25272 DECL_CONTEXT (decl), context_die);
25273 break;
25275 case NAMELIST_DECL:
25276 gen_namelist_decl (DECL_NAME (decl), context_die,
25277 NAMELIST_DECL_ASSOCIATED_DECL (decl));
25278 break;
25280 default:
25281 /* Probably some frontend-internal decl. Assume we don't care. */
25282 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
25283 break;
25286 return NULL;
25289 /* Output initial debug information for global DECL. Called at the
25290 end of the parsing process.
25292 This is the initial debug generation process. As such, the DIEs
25293 generated may be incomplete. A later debug generation pass
25294 (dwarf2out_late_global_decl) will augment the information generated
25295 in this pass (e.g., with complete location info). */
25297 static void
25298 dwarf2out_early_global_decl (tree decl)
25300 set_early_dwarf s;
25302 /* gen_decl_die() will set DECL_ABSTRACT because
25303 cgraph_function_possibly_inlined_p() returns true. This is in
25304 turn will cause DW_AT_inline attributes to be set.
25306 This happens because at early dwarf generation, there is no
25307 cgraph information, causing cgraph_function_possibly_inlined_p()
25308 to return true. Trick cgraph_function_possibly_inlined_p()
25309 while we generate dwarf early. */
25310 bool save = symtab->global_info_ready;
25311 symtab->global_info_ready = true;
25313 /* We don't handle TYPE_DECLs. If required, they'll be reached via
25314 other DECLs and they can point to template types or other things
25315 that dwarf2out can't handle when done via dwarf2out_decl. */
25316 if (TREE_CODE (decl) != TYPE_DECL
25317 && TREE_CODE (decl) != PARM_DECL)
25319 tree save_fndecl = current_function_decl;
25320 if (TREE_CODE (decl) == FUNCTION_DECL)
25322 /* No cfun means the symbol has no body, so there's nothing
25323 to emit. */
25324 if (!DECL_STRUCT_FUNCTION (decl))
25325 goto early_decl_exit;
25327 /* For nested functions, make sure we have DIEs for the parents first
25328 so that all nested DIEs are generated at the proper scope in the
25329 first shot. */
25330 tree context = decl_function_context (decl);
25331 if (context != NULL && lookup_decl_die (context) == NULL)
25333 current_function_decl = context;
25334 dwarf2out_decl (context);
25337 current_function_decl = decl;
25339 dwarf2out_decl (decl);
25340 if (TREE_CODE (decl) == FUNCTION_DECL)
25341 current_function_decl = save_fndecl;
25343 early_decl_exit:
25344 symtab->global_info_ready = save;
25347 /* Output debug information for global decl DECL. Called from
25348 toplev.c after compilation proper has finished. */
25350 static void
25351 dwarf2out_late_global_decl (tree decl)
25353 /* Fill-in any location information we were unable to determine
25354 on the first pass. */
25355 if (VAR_P (decl) && !POINTER_BOUNDS_P (decl))
25357 dw_die_ref die = lookup_decl_die (decl);
25359 /* We have to generate early debug late for LTO. */
25360 if (! die && in_lto_p)
25362 dwarf2out_decl (decl);
25363 die = lookup_decl_die (decl);
25366 if (die)
25368 /* We get called via the symtab code invoking late_global_decl
25369 for symbols that are optimized out. Do not add locations
25370 for those. */
25371 varpool_node *node = varpool_node::get (decl);
25372 if (! node || ! node->definition)
25373 tree_add_const_value_attribute_for_decl (die, decl);
25374 else
25375 add_location_or_const_value_attribute (die, decl, false);
25380 /* Output debug information for type decl DECL. Called from toplev.c
25381 and from language front ends (to record built-in types). */
25382 static void
25383 dwarf2out_type_decl (tree decl, int local)
25385 if (!local)
25387 set_early_dwarf s;
25388 dwarf2out_decl (decl);
25392 /* Output debug information for imported module or decl DECL.
25393 NAME is non-NULL name in the lexical block if the decl has been renamed.
25394 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
25395 that DECL belongs to.
25396 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
25397 static void
25398 dwarf2out_imported_module_or_decl_1 (tree decl,
25399 tree name,
25400 tree lexical_block,
25401 dw_die_ref lexical_block_die)
25403 expanded_location xloc;
25404 dw_die_ref imported_die = NULL;
25405 dw_die_ref at_import_die;
25407 if (TREE_CODE (decl) == IMPORTED_DECL)
25409 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
25410 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
25411 gcc_assert (decl);
25413 else
25414 xloc = expand_location (input_location);
25416 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
25418 at_import_die = force_type_die (TREE_TYPE (decl));
25419 /* For namespace N { typedef void T; } using N::T; base_type_die
25420 returns NULL, but DW_TAG_imported_declaration requires
25421 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
25422 if (!at_import_die)
25424 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
25425 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
25426 at_import_die = lookup_type_die (TREE_TYPE (decl));
25427 gcc_assert (at_import_die);
25430 else
25432 at_import_die = lookup_decl_die (decl);
25433 if (!at_import_die)
25435 /* If we're trying to avoid duplicate debug info, we may not have
25436 emitted the member decl for this field. Emit it now. */
25437 if (TREE_CODE (decl) == FIELD_DECL)
25439 tree type = DECL_CONTEXT (decl);
25441 if (TYPE_CONTEXT (type)
25442 && TYPE_P (TYPE_CONTEXT (type))
25443 && !should_emit_struct_debug (TYPE_CONTEXT (type),
25444 DINFO_USAGE_DIR_USE))
25445 return;
25446 gen_type_die_for_member (type, decl,
25447 get_context_die (TYPE_CONTEXT (type)));
25449 if (TREE_CODE (decl) == NAMELIST_DECL)
25450 at_import_die = gen_namelist_decl (DECL_NAME (decl),
25451 get_context_die (DECL_CONTEXT (decl)),
25452 NULL_TREE);
25453 else
25454 at_import_die = force_decl_die (decl);
25458 if (TREE_CODE (decl) == NAMESPACE_DECL)
25460 if (dwarf_version >= 3 || !dwarf_strict)
25461 imported_die = new_die (DW_TAG_imported_module,
25462 lexical_block_die,
25463 lexical_block);
25464 else
25465 return;
25467 else
25468 imported_die = new_die (DW_TAG_imported_declaration,
25469 lexical_block_die,
25470 lexical_block);
25472 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
25473 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
25474 if (name)
25475 add_AT_string (imported_die, DW_AT_name,
25476 IDENTIFIER_POINTER (name));
25477 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
25480 /* Output debug information for imported module or decl DECL.
25481 NAME is non-NULL name in context if the decl has been renamed.
25482 CHILD is true if decl is one of the renamed decls as part of
25483 importing whole module. */
25485 static void
25486 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
25487 bool child)
25489 /* dw_die_ref at_import_die; */
25490 dw_die_ref scope_die;
25492 if (debug_info_level <= DINFO_LEVEL_TERSE)
25493 return;
25495 gcc_assert (decl);
25497 set_early_dwarf s;
25499 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
25500 We need decl DIE for reference and scope die. First, get DIE for the decl
25501 itself. */
25503 /* Get the scope die for decl context. Use comp_unit_die for global module
25504 or decl. If die is not found for non globals, force new die. */
25505 if (context
25506 && TYPE_P (context)
25507 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
25508 return;
25510 scope_die = get_context_die (context);
25512 if (child)
25514 /* DW_TAG_imported_module was introduced in the DWARFv3 specification, so
25515 there is nothing we can do, here. */
25516 if (dwarf_version < 3 && dwarf_strict)
25517 return;
25519 gcc_assert (scope_die->die_child);
25520 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
25521 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
25522 scope_die = scope_die->die_child;
25525 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
25526 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
25529 /* Output debug information for namelists. */
25531 static dw_die_ref
25532 gen_namelist_decl (tree name, dw_die_ref scope_die, tree item_decls)
25534 dw_die_ref nml_die, nml_item_die, nml_item_ref_die;
25535 tree value;
25536 unsigned i;
25538 if (debug_info_level <= DINFO_LEVEL_TERSE)
25539 return NULL;
25541 gcc_assert (scope_die != NULL);
25542 nml_die = new_die (DW_TAG_namelist, scope_die, NULL);
25543 add_AT_string (nml_die, DW_AT_name, IDENTIFIER_POINTER (name));
25545 /* If there are no item_decls, we have a nondefining namelist, e.g.
25546 with USE association; hence, set DW_AT_declaration. */
25547 if (item_decls == NULL_TREE)
25549 add_AT_flag (nml_die, DW_AT_declaration, 1);
25550 return nml_die;
25553 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (item_decls), i, value)
25555 nml_item_ref_die = lookup_decl_die (value);
25556 if (!nml_item_ref_die)
25557 nml_item_ref_die = force_decl_die (value);
25559 nml_item_die = new_die (DW_TAG_namelist_item, nml_die, NULL);
25560 add_AT_die_ref (nml_item_die, DW_AT_namelist_items, nml_item_ref_die);
25562 return nml_die;
25566 /* Write the debugging output for DECL and return the DIE. */
25568 static void
25569 dwarf2out_decl (tree decl)
25571 dw_die_ref context_die = comp_unit_die ();
25573 switch (TREE_CODE (decl))
25575 case ERROR_MARK:
25576 return;
25578 case FUNCTION_DECL:
25579 /* What we would really like to do here is to filter out all mere
25580 file-scope declarations of file-scope functions which are never
25581 referenced later within this translation unit (and keep all of ones
25582 that *are* referenced later on) but we aren't clairvoyant, so we have
25583 no idea which functions will be referenced in the future (i.e. later
25584 on within the current translation unit). So here we just ignore all
25585 file-scope function declarations which are not also definitions. If
25586 and when the debugger needs to know something about these functions,
25587 it will have to hunt around and find the DWARF information associated
25588 with the definition of the function.
25590 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
25591 nodes represent definitions and which ones represent mere
25592 declarations. We have to check DECL_INITIAL instead. That's because
25593 the C front-end supports some weird semantics for "extern inline"
25594 function definitions. These can get inlined within the current
25595 translation unit (and thus, we need to generate Dwarf info for their
25596 abstract instances so that the Dwarf info for the concrete inlined
25597 instances can have something to refer to) but the compiler never
25598 generates any out-of-lines instances of such things (despite the fact
25599 that they *are* definitions).
25601 The important point is that the C front-end marks these "extern
25602 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
25603 them anyway. Note that the C++ front-end also plays some similar games
25604 for inline function definitions appearing within include files which
25605 also contain `#pragma interface' pragmas.
25607 If we are called from dwarf2out_abstract_function output a DIE
25608 anyway. We can end up here this way with early inlining and LTO
25609 where the inlined function is output in a different LTRANS unit
25610 or not at all. */
25611 if (DECL_INITIAL (decl) == NULL_TREE
25612 && ! DECL_ABSTRACT_P (decl))
25613 return;
25615 /* If we're a nested function, initially use a parent of NULL; if we're
25616 a plain function, this will be fixed up in decls_for_scope. If
25617 we're a method, it will be ignored, since we already have a DIE. */
25618 if (decl_function_context (decl)
25619 /* But if we're in terse mode, we don't care about scope. */
25620 && debug_info_level > DINFO_LEVEL_TERSE)
25621 context_die = NULL;
25622 break;
25624 case VAR_DECL:
25625 /* For local statics lookup proper context die. */
25626 if (local_function_static (decl))
25627 context_die = lookup_decl_die (DECL_CONTEXT (decl));
25629 /* If we are in terse mode, don't generate any DIEs to represent any
25630 variable declarations or definitions. */
25631 if (debug_info_level <= DINFO_LEVEL_TERSE)
25632 return;
25633 break;
25635 case CONST_DECL:
25636 if (debug_info_level <= DINFO_LEVEL_TERSE)
25637 return;
25638 if (!is_fortran () && !is_ada ())
25639 return;
25640 if (TREE_STATIC (decl) && decl_function_context (decl))
25641 context_die = lookup_decl_die (DECL_CONTEXT (decl));
25642 break;
25644 case NAMESPACE_DECL:
25645 case IMPORTED_DECL:
25646 if (debug_info_level <= DINFO_LEVEL_TERSE)
25647 return;
25648 if (lookup_decl_die (decl) != NULL)
25649 return;
25650 break;
25652 case TYPE_DECL:
25653 /* Don't emit stubs for types unless they are needed by other DIEs. */
25654 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
25655 return;
25657 /* Don't bother trying to generate any DIEs to represent any of the
25658 normal built-in types for the language we are compiling. */
25659 if (DECL_IS_BUILTIN (decl))
25660 return;
25662 /* If we are in terse mode, don't generate any DIEs for types. */
25663 if (debug_info_level <= DINFO_LEVEL_TERSE)
25664 return;
25666 /* If we're a function-scope tag, initially use a parent of NULL;
25667 this will be fixed up in decls_for_scope. */
25668 if (decl_function_context (decl))
25669 context_die = NULL;
25671 break;
25673 case NAMELIST_DECL:
25674 break;
25676 default:
25677 return;
25680 gen_decl_die (decl, NULL, NULL, context_die);
25682 if (flag_checking)
25684 dw_die_ref die = lookup_decl_die (decl);
25685 if (die)
25686 check_die (die);
25690 /* Write the debugging output for DECL. */
25692 static void
25693 dwarf2out_function_decl (tree decl)
25695 dwarf2out_decl (decl);
25696 call_arg_locations = NULL;
25697 call_arg_loc_last = NULL;
25698 call_site_count = -1;
25699 tail_call_site_count = -1;
25700 decl_loc_table->empty ();
25701 cached_dw_loc_list_table->empty ();
25704 /* Output a marker (i.e. a label) for the beginning of the generated code for
25705 a lexical block. */
25707 static void
25708 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
25709 unsigned int blocknum)
25711 switch_to_section (current_function_section ());
25712 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
25715 /* Output a marker (i.e. a label) for the end of the generated code for a
25716 lexical block. */
25718 static void
25719 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
25721 switch_to_section (current_function_section ());
25722 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
25725 /* Returns nonzero if it is appropriate not to emit any debugging
25726 information for BLOCK, because it doesn't contain any instructions.
25728 Don't allow this for blocks with nested functions or local classes
25729 as we would end up with orphans, and in the presence of scheduling
25730 we may end up calling them anyway. */
25732 static bool
25733 dwarf2out_ignore_block (const_tree block)
25735 tree decl;
25736 unsigned int i;
25738 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
25739 if (TREE_CODE (decl) == FUNCTION_DECL
25740 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
25741 return 0;
25742 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
25744 decl = BLOCK_NONLOCALIZED_VAR (block, i);
25745 if (TREE_CODE (decl) == FUNCTION_DECL
25746 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
25747 return 0;
25750 return 1;
25753 /* Hash table routines for file_hash. */
25755 bool
25756 dwarf_file_hasher::equal (dwarf_file_data *p1, const char *p2)
25758 return filename_cmp (p1->filename, p2) == 0;
25761 hashval_t
25762 dwarf_file_hasher::hash (dwarf_file_data *p)
25764 return htab_hash_string (p->filename);
25767 /* Lookup FILE_NAME (in the list of filenames that we know about here in
25768 dwarf2out.c) and return its "index". The index of each (known) filename is
25769 just a unique number which is associated with only that one filename. We
25770 need such numbers for the sake of generating labels (in the .debug_sfnames
25771 section) and references to those files numbers (in the .debug_srcinfo
25772 and .debug_macinfo sections). If the filename given as an argument is not
25773 found in our current list, add it to the list and assign it the next
25774 available unique index number. */
25776 static struct dwarf_file_data *
25777 lookup_filename (const char *file_name)
25779 struct dwarf_file_data * created;
25781 if (!file_name)
25782 return NULL;
25784 dwarf_file_data **slot
25785 = file_table->find_slot_with_hash (file_name, htab_hash_string (file_name),
25786 INSERT);
25787 if (*slot)
25788 return *slot;
25790 created = ggc_alloc<dwarf_file_data> ();
25791 created->filename = file_name;
25792 created->emitted_number = 0;
25793 *slot = created;
25794 return created;
25797 /* If the assembler will construct the file table, then translate the compiler
25798 internal file table number into the assembler file table number, and emit
25799 a .file directive if we haven't already emitted one yet. The file table
25800 numbers are different because we prune debug info for unused variables and
25801 types, which may include filenames. */
25803 static int
25804 maybe_emit_file (struct dwarf_file_data * fd)
25806 if (! fd->emitted_number)
25808 if (last_emitted_file)
25809 fd->emitted_number = last_emitted_file->emitted_number + 1;
25810 else
25811 fd->emitted_number = 1;
25812 last_emitted_file = fd;
25814 if (DWARF2_ASM_LINE_DEBUG_INFO)
25816 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
25817 output_quoted_string (asm_out_file,
25818 remap_debug_filename (fd->filename));
25819 fputc ('\n', asm_out_file);
25823 return fd->emitted_number;
25826 /* Schedule generation of a DW_AT_const_value attribute to DIE.
25827 That generation should happen after function debug info has been
25828 generated. The value of the attribute is the constant value of ARG. */
25830 static void
25831 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
25833 die_arg_entry entry;
25835 if (!die || !arg)
25836 return;
25838 gcc_assert (early_dwarf);
25840 if (!tmpl_value_parm_die_table)
25841 vec_alloc (tmpl_value_parm_die_table, 32);
25843 entry.die = die;
25844 entry.arg = arg;
25845 vec_safe_push (tmpl_value_parm_die_table, entry);
25848 /* Return TRUE if T is an instance of generic type, FALSE
25849 otherwise. */
25851 static bool
25852 generic_type_p (tree t)
25854 if (t == NULL_TREE || !TYPE_P (t))
25855 return false;
25856 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
25859 /* Schedule the generation of the generic parameter dies for the
25860 instance of generic type T. The proper generation itself is later
25861 done by gen_scheduled_generic_parms_dies. */
25863 static void
25864 schedule_generic_params_dies_gen (tree t)
25866 if (!generic_type_p (t))
25867 return;
25869 gcc_assert (early_dwarf);
25871 if (!generic_type_instances)
25872 vec_alloc (generic_type_instances, 256);
25874 vec_safe_push (generic_type_instances, t);
25877 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
25878 by append_entry_to_tmpl_value_parm_die_table. This function must
25879 be called after function DIEs have been generated. */
25881 static void
25882 gen_remaining_tmpl_value_param_die_attribute (void)
25884 if (tmpl_value_parm_die_table)
25886 unsigned i, j;
25887 die_arg_entry *e;
25889 /* We do this in two phases - first get the cases we can
25890 handle during early-finish, preserving those we cannot
25891 (containing symbolic constants where we don't yet know
25892 whether we are going to output the referenced symbols).
25893 For those we try again at late-finish. */
25894 j = 0;
25895 FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
25897 if (!tree_add_const_value_attribute (e->die, e->arg))
25899 dw_loc_descr_ref loc = NULL;
25900 if (! early_dwarf
25901 && (dwarf_version >= 5 || !dwarf_strict))
25902 loc = loc_descriptor_from_tree (e->arg, 2, NULL);
25903 if (loc)
25904 add_AT_loc (e->die, DW_AT_location, loc);
25905 else
25906 (*tmpl_value_parm_die_table)[j++] = *e;
25909 tmpl_value_parm_die_table->truncate (j);
25913 /* Generate generic parameters DIEs for instances of generic types
25914 that have been previously scheduled by
25915 schedule_generic_params_dies_gen. This function must be called
25916 after all the types of the CU have been laid out. */
25918 static void
25919 gen_scheduled_generic_parms_dies (void)
25921 unsigned i;
25922 tree t;
25924 if (!generic_type_instances)
25925 return;
25927 FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
25928 if (COMPLETE_TYPE_P (t))
25929 gen_generic_params_dies (t);
25931 generic_type_instances = NULL;
25935 /* Replace DW_AT_name for the decl with name. */
25937 static void
25938 dwarf2out_set_name (tree decl, tree name)
25940 dw_die_ref die;
25941 dw_attr_node *attr;
25942 const char *dname;
25944 die = TYPE_SYMTAB_DIE (decl);
25945 if (!die)
25946 return;
25948 dname = dwarf2_name (name, 0);
25949 if (!dname)
25950 return;
25952 attr = get_AT (die, DW_AT_name);
25953 if (attr)
25955 struct indirect_string_node *node;
25957 node = find_AT_string (dname);
25958 /* replace the string. */
25959 attr->dw_attr_val.v.val_str = node;
25962 else
25963 add_name_attribute (die, dname);
25966 /* True if before or during processing of the first function being emitted. */
25967 static bool in_first_function_p = true;
25968 /* True if loc_note during dwarf2out_var_location call might still be
25969 before first real instruction at address equal to .Ltext0. */
25970 static bool maybe_at_text_label_p = true;
25971 /* One above highest N where .LVLN label might be equal to .Ltext0 label. */
25972 static unsigned int first_loclabel_num_not_at_text_label;
25974 /* Called by the final INSN scan whenever we see a var location. We
25975 use it to drop labels in the right places, and throw the location in
25976 our lookup table. */
25978 static void
25979 dwarf2out_var_location (rtx_insn *loc_note)
25981 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
25982 struct var_loc_node *newloc;
25983 rtx_insn *next_real, *next_note;
25984 rtx_insn *call_insn = NULL;
25985 static const char *last_label;
25986 static const char *last_postcall_label;
25987 static bool last_in_cold_section_p;
25988 static rtx_insn *expected_next_loc_note;
25989 tree decl;
25990 bool var_loc_p;
25992 if (!NOTE_P (loc_note))
25994 if (CALL_P (loc_note))
25996 call_site_count++;
25997 if (SIBLING_CALL_P (loc_note))
25998 tail_call_site_count++;
25999 if (optimize == 0 && !flag_var_tracking)
26001 /* When the var-tracking pass is not running, there is no note
26002 for indirect calls whose target is compile-time known. In this
26003 case, process such calls specifically so that we generate call
26004 sites for them anyway. */
26005 rtx x = PATTERN (loc_note);
26006 if (GET_CODE (x) == PARALLEL)
26007 x = XVECEXP (x, 0, 0);
26008 if (GET_CODE (x) == SET)
26009 x = SET_SRC (x);
26010 if (GET_CODE (x) == CALL)
26011 x = XEXP (x, 0);
26012 if (!MEM_P (x)
26013 || GET_CODE (XEXP (x, 0)) != SYMBOL_REF
26014 || !SYMBOL_REF_DECL (XEXP (x, 0))
26015 || (TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0)))
26016 != FUNCTION_DECL))
26018 call_insn = loc_note;
26019 loc_note = NULL;
26020 var_loc_p = false;
26022 next_real = next_real_insn (call_insn);
26023 next_note = NULL;
26024 cached_next_real_insn = NULL;
26025 goto create_label;
26029 return;
26032 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
26033 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
26034 return;
26036 /* Optimize processing a large consecutive sequence of location
26037 notes so we don't spend too much time in next_real_insn. If the
26038 next insn is another location note, remember the next_real_insn
26039 calculation for next time. */
26040 next_real = cached_next_real_insn;
26041 if (next_real)
26043 if (expected_next_loc_note != loc_note)
26044 next_real = NULL;
26047 next_note = NEXT_INSN (loc_note);
26048 if (! next_note
26049 || next_note->deleted ()
26050 || ! NOTE_P (next_note)
26051 || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
26052 && NOTE_KIND (next_note) != NOTE_INSN_CALL_ARG_LOCATION))
26053 next_note = NULL;
26055 if (! next_real)
26056 next_real = next_real_insn (loc_note);
26058 if (next_note)
26060 expected_next_loc_note = next_note;
26061 cached_next_real_insn = next_real;
26063 else
26064 cached_next_real_insn = NULL;
26066 /* If there are no instructions which would be affected by this note,
26067 don't do anything. */
26068 if (var_loc_p
26069 && next_real == NULL_RTX
26070 && !NOTE_DURING_CALL_P (loc_note))
26071 return;
26073 create_label:
26075 if (next_real == NULL_RTX)
26076 next_real = get_last_insn ();
26078 /* If there were any real insns between note we processed last time
26079 and this note (or if it is the first note), clear
26080 last_{,postcall_}label so that they are not reused this time. */
26081 if (last_var_location_insn == NULL_RTX
26082 || last_var_location_insn != next_real
26083 || last_in_cold_section_p != in_cold_section_p)
26085 last_label = NULL;
26086 last_postcall_label = NULL;
26089 if (var_loc_p)
26091 decl = NOTE_VAR_LOCATION_DECL (loc_note);
26092 newloc = add_var_loc_to_decl (decl, loc_note,
26093 NOTE_DURING_CALL_P (loc_note)
26094 ? last_postcall_label : last_label);
26095 if (newloc == NULL)
26096 return;
26098 else
26100 decl = NULL_TREE;
26101 newloc = NULL;
26104 /* If there were no real insns between note we processed last time
26105 and this note, use the label we emitted last time. Otherwise
26106 create a new label and emit it. */
26107 if (last_label == NULL)
26109 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
26110 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
26111 loclabel_num++;
26112 last_label = ggc_strdup (loclabel);
26113 /* See if loclabel might be equal to .Ltext0. If yes,
26114 bump first_loclabel_num_not_at_text_label. */
26115 if (!have_multiple_function_sections
26116 && in_first_function_p
26117 && maybe_at_text_label_p)
26119 static rtx_insn *last_start;
26120 rtx_insn *insn;
26121 for (insn = loc_note; insn; insn = previous_insn (insn))
26122 if (insn == last_start)
26123 break;
26124 else if (!NONDEBUG_INSN_P (insn))
26125 continue;
26126 else
26128 rtx body = PATTERN (insn);
26129 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
26130 continue;
26131 /* Inline asm could occupy zero bytes. */
26132 else if (GET_CODE (body) == ASM_INPUT
26133 || asm_noperands (body) >= 0)
26134 continue;
26135 #ifdef HAVE_attr_length
26136 else if (get_attr_min_length (insn) == 0)
26137 continue;
26138 #endif
26139 else
26141 /* Assume insn has non-zero length. */
26142 maybe_at_text_label_p = false;
26143 break;
26146 if (maybe_at_text_label_p)
26148 last_start = loc_note;
26149 first_loclabel_num_not_at_text_label = loclabel_num;
26154 gcc_assert ((loc_note == NULL_RTX && call_insn != NULL_RTX)
26155 || (loc_note != NULL_RTX && call_insn == NULL_RTX));
26157 if (!var_loc_p)
26159 struct call_arg_loc_node *ca_loc
26160 = ggc_cleared_alloc<call_arg_loc_node> ();
26161 rtx_insn *prev
26162 = loc_note != NULL_RTX ? prev_real_insn (loc_note) : call_insn;
26164 ca_loc->call_arg_loc_note = loc_note;
26165 ca_loc->next = NULL;
26166 ca_loc->label = last_label;
26167 gcc_assert (prev
26168 && (CALL_P (prev)
26169 || (NONJUMP_INSN_P (prev)
26170 && GET_CODE (PATTERN (prev)) == SEQUENCE
26171 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
26172 if (!CALL_P (prev))
26173 prev = as_a <rtx_sequence *> (PATTERN (prev))->insn (0);
26174 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
26176 /* Look for a SYMBOL_REF in the "prev" instruction. */
26177 rtx x = get_call_rtx_from (PATTERN (prev));
26178 if (x)
26180 /* Try to get the call symbol, if any. */
26181 if (MEM_P (XEXP (x, 0)))
26182 x = XEXP (x, 0);
26183 /* First, look for a memory access to a symbol_ref. */
26184 if (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
26185 && SYMBOL_REF_DECL (XEXP (x, 0))
26186 && TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0))) == FUNCTION_DECL)
26187 ca_loc->symbol_ref = XEXP (x, 0);
26188 /* Otherwise, look at a compile-time known user-level function
26189 declaration. */
26190 else if (MEM_P (x)
26191 && MEM_EXPR (x)
26192 && TREE_CODE (MEM_EXPR (x)) == FUNCTION_DECL)
26193 ca_loc->symbol_ref = XEXP (DECL_RTL (MEM_EXPR (x)), 0);
26196 ca_loc->block = insn_scope (prev);
26197 if (call_arg_locations)
26198 call_arg_loc_last->next = ca_loc;
26199 else
26200 call_arg_locations = ca_loc;
26201 call_arg_loc_last = ca_loc;
26203 else if (loc_note != NULL_RTX && !NOTE_DURING_CALL_P (loc_note))
26204 newloc->label = last_label;
26205 else
26207 if (!last_postcall_label)
26209 sprintf (loclabel, "%s-1", last_label);
26210 last_postcall_label = ggc_strdup (loclabel);
26212 newloc->label = last_postcall_label;
26215 last_var_location_insn = next_real;
26216 last_in_cold_section_p = in_cold_section_p;
26219 /* Called from finalize_size_functions for size functions so that their body
26220 can be encoded in the debug info to describe the layout of variable-length
26221 structures. */
26223 static void
26224 dwarf2out_size_function (tree decl)
26226 function_to_dwarf_procedure (decl);
26229 /* Note in one location list that text section has changed. */
26232 var_location_switch_text_section_1 (var_loc_list **slot, void *)
26234 var_loc_list *list = *slot;
26235 if (list->first)
26236 list->last_before_switch
26237 = list->last->next ? list->last->next : list->last;
26238 return 1;
26241 /* Note in all location lists that text section has changed. */
26243 static void
26244 var_location_switch_text_section (void)
26246 if (decl_loc_table == NULL)
26247 return;
26249 decl_loc_table->traverse<void *, var_location_switch_text_section_1> (NULL);
26252 /* Create a new line number table. */
26254 static dw_line_info_table *
26255 new_line_info_table (void)
26257 dw_line_info_table *table;
26259 table = ggc_cleared_alloc<dw_line_info_table> ();
26260 table->file_num = 1;
26261 table->line_num = 1;
26262 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
26264 return table;
26267 /* Lookup the "current" table into which we emit line info, so
26268 that we don't have to do it for every source line. */
26270 static void
26271 set_cur_line_info_table (section *sec)
26273 dw_line_info_table *table;
26275 if (sec == text_section)
26276 table = text_section_line_info;
26277 else if (sec == cold_text_section)
26279 table = cold_text_section_line_info;
26280 if (!table)
26282 cold_text_section_line_info = table = new_line_info_table ();
26283 table->end_label = cold_end_label;
26286 else
26288 const char *end_label;
26290 if (flag_reorder_blocks_and_partition)
26292 if (in_cold_section_p)
26293 end_label = crtl->subsections.cold_section_end_label;
26294 else
26295 end_label = crtl->subsections.hot_section_end_label;
26297 else
26299 char label[MAX_ARTIFICIAL_LABEL_BYTES];
26300 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
26301 current_function_funcdef_no);
26302 end_label = ggc_strdup (label);
26305 table = new_line_info_table ();
26306 table->end_label = end_label;
26308 vec_safe_push (separate_line_info, table);
26311 if (DWARF2_ASM_LINE_DEBUG_INFO)
26312 table->is_stmt = (cur_line_info_table
26313 ? cur_line_info_table->is_stmt
26314 : DWARF_LINE_DEFAULT_IS_STMT_START);
26315 cur_line_info_table = table;
26319 /* We need to reset the locations at the beginning of each
26320 function. We can't do this in the end_function hook, because the
26321 declarations that use the locations won't have been output when
26322 that hook is called. Also compute have_multiple_function_sections here. */
26324 static void
26325 dwarf2out_begin_function (tree fun)
26327 section *sec = function_section (fun);
26329 if (sec != text_section)
26330 have_multiple_function_sections = true;
26332 if (flag_reorder_blocks_and_partition && !cold_text_section)
26334 gcc_assert (current_function_decl == fun);
26335 cold_text_section = unlikely_text_section ();
26336 switch_to_section (cold_text_section);
26337 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
26338 switch_to_section (sec);
26341 dwarf2out_note_section_used ();
26342 call_site_count = 0;
26343 tail_call_site_count = 0;
26345 set_cur_line_info_table (sec);
26348 /* Helper function of dwarf2out_end_function, called only after emitting
26349 the very first function into assembly. Check if some .debug_loc range
26350 might end with a .LVL* label that could be equal to .Ltext0.
26351 In that case we must force using absolute addresses in .debug_loc ranges,
26352 because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
26353 .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
26354 list terminator.
26355 Set have_multiple_function_sections to true in that case and
26356 terminate htab traversal. */
26359 find_empty_loc_ranges_at_text_label (var_loc_list **slot, int)
26361 var_loc_list *entry = *slot;
26362 struct var_loc_node *node;
26364 node = entry->first;
26365 if (node && node->next && node->next->label)
26367 unsigned int i;
26368 const char *label = node->next->label;
26369 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
26371 for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
26373 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
26374 if (strcmp (label, loclabel) == 0)
26376 have_multiple_function_sections = true;
26377 return 0;
26381 return 1;
26384 /* Hook called after emitting a function into assembly.
26385 This does something only for the very first function emitted. */
26387 static void
26388 dwarf2out_end_function (unsigned int)
26390 if (in_first_function_p
26391 && !have_multiple_function_sections
26392 && first_loclabel_num_not_at_text_label
26393 && decl_loc_table)
26394 decl_loc_table->traverse<int, find_empty_loc_ranges_at_text_label> (0);
26395 in_first_function_p = false;
26396 maybe_at_text_label_p = false;
26399 /* Temporary holder for dwarf2out_register_main_translation_unit. Used to let
26400 front-ends register a translation unit even before dwarf2out_init is
26401 called. */
26402 static tree main_translation_unit = NULL_TREE;
26404 /* Hook called by front-ends after they built their main translation unit.
26405 Associate comp_unit_die to UNIT. */
26407 static void
26408 dwarf2out_register_main_translation_unit (tree unit)
26410 gcc_assert (TREE_CODE (unit) == TRANSLATION_UNIT_DECL
26411 && main_translation_unit == NULL_TREE);
26412 main_translation_unit = unit;
26413 /* If dwarf2out_init has not been called yet, it will perform the association
26414 itself looking at main_translation_unit. */
26415 if (decl_die_table != NULL)
26416 equate_decl_number_to_die (unit, comp_unit_die ());
26419 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
26421 static void
26422 push_dw_line_info_entry (dw_line_info_table *table,
26423 enum dw_line_info_opcode opcode, unsigned int val)
26425 dw_line_info_entry e;
26426 e.opcode = opcode;
26427 e.val = val;
26428 vec_safe_push (table->entries, e);
26431 /* Output a label to mark the beginning of a source code line entry
26432 and record information relating to this source line, in
26433 'line_info_table' for later output of the .debug_line section. */
26434 /* ??? The discriminator parameter ought to be unsigned. */
26436 static void
26437 dwarf2out_source_line (unsigned int line, const char *filename,
26438 int discriminator, bool is_stmt)
26440 unsigned int file_num;
26441 dw_line_info_table *table;
26443 if (debug_info_level < DINFO_LEVEL_TERSE || line == 0)
26444 return;
26446 /* The discriminator column was added in dwarf4. Simplify the below
26447 by simply removing it if we're not supposed to output it. */
26448 if (dwarf_version < 4 && dwarf_strict)
26449 discriminator = 0;
26451 table = cur_line_info_table;
26452 file_num = maybe_emit_file (lookup_filename (filename));
26454 /* ??? TODO: Elide duplicate line number entries. Traditionally,
26455 the debugger has used the second (possibly duplicate) line number
26456 at the beginning of the function to mark the end of the prologue.
26457 We could eliminate any other duplicates within the function. For
26458 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
26459 that second line number entry. */
26460 /* Recall that this end-of-prologue indication is *not* the same thing
26461 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
26462 to which the hook corresponds, follows the last insn that was
26463 emitted by gen_prologue. What we need is to precede the first insn
26464 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
26465 insn that corresponds to something the user wrote. These may be
26466 very different locations once scheduling is enabled. */
26468 if (0 && file_num == table->file_num
26469 && line == table->line_num
26470 && discriminator == table->discrim_num
26471 && is_stmt == table->is_stmt)
26472 return;
26474 switch_to_section (current_function_section ());
26476 /* If requested, emit something human-readable. */
26477 if (flag_debug_asm)
26478 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START, filename, line);
26480 if (DWARF2_ASM_LINE_DEBUG_INFO)
26482 /* Emit the .loc directive understood by GNU as. */
26483 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
26484 file_num, line, is_stmt, discriminator */
26485 fputs ("\t.loc ", asm_out_file);
26486 fprint_ul (asm_out_file, file_num);
26487 putc (' ', asm_out_file);
26488 fprint_ul (asm_out_file, line);
26489 putc (' ', asm_out_file);
26490 putc ('0', asm_out_file);
26492 if (is_stmt != table->is_stmt)
26494 fputs (" is_stmt ", asm_out_file);
26495 putc (is_stmt ? '1' : '0', asm_out_file);
26497 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
26499 gcc_assert (discriminator > 0);
26500 fputs (" discriminator ", asm_out_file);
26501 fprint_ul (asm_out_file, (unsigned long) discriminator);
26503 putc ('\n', asm_out_file);
26505 else
26507 unsigned int label_num = ++line_info_label_num;
26509 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
26511 push_dw_line_info_entry (table, LI_set_address, label_num);
26512 if (file_num != table->file_num)
26513 push_dw_line_info_entry (table, LI_set_file, file_num);
26514 if (discriminator != table->discrim_num)
26515 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
26516 if (is_stmt != table->is_stmt)
26517 push_dw_line_info_entry (table, LI_negate_stmt, 0);
26518 push_dw_line_info_entry (table, LI_set_line, line);
26521 table->file_num = file_num;
26522 table->line_num = line;
26523 table->discrim_num = discriminator;
26524 table->is_stmt = is_stmt;
26525 table->in_use = true;
26528 /* Record the beginning of a new source file. */
26530 static void
26531 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
26533 if (flag_eliminate_dwarf2_dups)
26535 /* Record the beginning of the file for break_out_includes. */
26536 dw_die_ref bincl_die;
26538 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
26539 add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
26542 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
26544 macinfo_entry e;
26545 e.code = DW_MACINFO_start_file;
26546 e.lineno = lineno;
26547 e.info = ggc_strdup (filename);
26548 vec_safe_push (macinfo_table, e);
26552 /* Record the end of a source file. */
26554 static void
26555 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
26557 if (flag_eliminate_dwarf2_dups)
26558 /* Record the end of the file for break_out_includes. */
26559 new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
26561 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
26563 macinfo_entry e;
26564 e.code = DW_MACINFO_end_file;
26565 e.lineno = lineno;
26566 e.info = NULL;
26567 vec_safe_push (macinfo_table, e);
26571 /* Called from debug_define in toplev.c. The `buffer' parameter contains
26572 the tail part of the directive line, i.e. the part which is past the
26573 initial whitespace, #, whitespace, directive-name, whitespace part. */
26575 static void
26576 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
26577 const char *buffer ATTRIBUTE_UNUSED)
26579 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
26581 macinfo_entry e;
26582 /* Insert a dummy first entry to be able to optimize the whole
26583 predefined macro block using DW_MACRO_import. */
26584 if (macinfo_table->is_empty () && lineno <= 1)
26586 e.code = 0;
26587 e.lineno = 0;
26588 e.info = NULL;
26589 vec_safe_push (macinfo_table, e);
26591 e.code = DW_MACINFO_define;
26592 e.lineno = lineno;
26593 e.info = ggc_strdup (buffer);
26594 vec_safe_push (macinfo_table, e);
26598 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
26599 the tail part of the directive line, i.e. the part which is past the
26600 initial whitespace, #, whitespace, directive-name, whitespace part. */
26602 static void
26603 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
26604 const char *buffer ATTRIBUTE_UNUSED)
26606 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
26608 macinfo_entry e;
26609 /* Insert a dummy first entry to be able to optimize the whole
26610 predefined macro block using DW_MACRO_import. */
26611 if (macinfo_table->is_empty () && lineno <= 1)
26613 e.code = 0;
26614 e.lineno = 0;
26615 e.info = NULL;
26616 vec_safe_push (macinfo_table, e);
26618 e.code = DW_MACINFO_undef;
26619 e.lineno = lineno;
26620 e.info = ggc_strdup (buffer);
26621 vec_safe_push (macinfo_table, e);
26625 /* Helpers to manipulate hash table of CUs. */
26627 struct macinfo_entry_hasher : nofree_ptr_hash <macinfo_entry>
26629 static inline hashval_t hash (const macinfo_entry *);
26630 static inline bool equal (const macinfo_entry *, const macinfo_entry *);
26633 inline hashval_t
26634 macinfo_entry_hasher::hash (const macinfo_entry *entry)
26636 return htab_hash_string (entry->info);
26639 inline bool
26640 macinfo_entry_hasher::equal (const macinfo_entry *entry1,
26641 const macinfo_entry *entry2)
26643 return !strcmp (entry1->info, entry2->info);
26646 typedef hash_table<macinfo_entry_hasher> macinfo_hash_type;
26648 /* Output a single .debug_macinfo entry. */
26650 static void
26651 output_macinfo_op (macinfo_entry *ref)
26653 int file_num;
26654 size_t len;
26655 struct indirect_string_node *node;
26656 char label[MAX_ARTIFICIAL_LABEL_BYTES];
26657 struct dwarf_file_data *fd;
26659 switch (ref->code)
26661 case DW_MACINFO_start_file:
26662 fd = lookup_filename (ref->info);
26663 file_num = maybe_emit_file (fd);
26664 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
26665 dw2_asm_output_data_uleb128 (ref->lineno,
26666 "Included from line number %lu",
26667 (unsigned long) ref->lineno);
26668 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
26669 break;
26670 case DW_MACINFO_end_file:
26671 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
26672 break;
26673 case DW_MACINFO_define:
26674 case DW_MACINFO_undef:
26675 len = strlen (ref->info) + 1;
26676 if (!dwarf_strict
26677 && len > DWARF_OFFSET_SIZE
26678 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
26679 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
26681 ref->code = ref->code == DW_MACINFO_define
26682 ? DW_MACRO_define_strp : DW_MACRO_undef_strp;
26683 output_macinfo_op (ref);
26684 return;
26686 dw2_asm_output_data (1, ref->code,
26687 ref->code == DW_MACINFO_define
26688 ? "Define macro" : "Undefine macro");
26689 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
26690 (unsigned long) ref->lineno);
26691 dw2_asm_output_nstring (ref->info, -1, "The macro");
26692 break;
26693 case DW_MACRO_define_strp:
26694 case DW_MACRO_undef_strp:
26695 node = find_AT_string (ref->info);
26696 gcc_assert (node
26697 && (node->form == DW_FORM_strp
26698 || node->form == DW_FORM_GNU_str_index));
26699 dw2_asm_output_data (1, ref->code,
26700 ref->code == DW_MACRO_define_strp
26701 ? "Define macro strp"
26702 : "Undefine macro strp");
26703 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
26704 (unsigned long) ref->lineno);
26705 if (node->form == DW_FORM_strp)
26706 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
26707 debug_str_section, "The macro: \"%s\"",
26708 ref->info);
26709 else
26710 dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
26711 ref->info);
26712 break;
26713 case DW_MACRO_import:
26714 dw2_asm_output_data (1, ref->code, "Import");
26715 ASM_GENERATE_INTERNAL_LABEL (label,
26716 DEBUG_MACRO_SECTION_LABEL, ref->lineno);
26717 dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
26718 break;
26719 default:
26720 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
26721 ASM_COMMENT_START, (unsigned long) ref->code);
26722 break;
26726 /* Attempt to make a sequence of define/undef macinfo ops shareable with
26727 other compilation unit .debug_macinfo sections. IDX is the first
26728 index of a define/undef, return the number of ops that should be
26729 emitted in a comdat .debug_macinfo section and emit
26730 a DW_MACRO_import entry referencing it.
26731 If the define/undef entry should be emitted normally, return 0. */
26733 static unsigned
26734 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
26735 macinfo_hash_type **macinfo_htab)
26737 macinfo_entry *first, *second, *cur, *inc;
26738 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
26739 unsigned char checksum[16];
26740 struct md5_ctx ctx;
26741 char *grp_name, *tail;
26742 const char *base;
26743 unsigned int i, count, encoded_filename_len, linebuf_len;
26744 macinfo_entry **slot;
26746 first = &(*macinfo_table)[idx];
26747 second = &(*macinfo_table)[idx + 1];
26749 /* Optimize only if there are at least two consecutive define/undef ops,
26750 and either all of them are before first DW_MACINFO_start_file
26751 with lineno {0,1} (i.e. predefined macro block), or all of them are
26752 in some included header file. */
26753 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
26754 return 0;
26755 if (vec_safe_is_empty (files))
26757 if (first->lineno > 1 || second->lineno > 1)
26758 return 0;
26760 else if (first->lineno == 0)
26761 return 0;
26763 /* Find the last define/undef entry that can be grouped together
26764 with first and at the same time compute md5 checksum of their
26765 codes, linenumbers and strings. */
26766 md5_init_ctx (&ctx);
26767 for (i = idx; macinfo_table->iterate (i, &cur); i++)
26768 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
26769 break;
26770 else if (vec_safe_is_empty (files) && cur->lineno > 1)
26771 break;
26772 else
26774 unsigned char code = cur->code;
26775 md5_process_bytes (&code, 1, &ctx);
26776 checksum_uleb128 (cur->lineno, &ctx);
26777 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
26779 md5_finish_ctx (&ctx, checksum);
26780 count = i - idx;
26782 /* From the containing include filename (if any) pick up just
26783 usable characters from its basename. */
26784 if (vec_safe_is_empty (files))
26785 base = "";
26786 else
26787 base = lbasename (files->last ().info);
26788 for (encoded_filename_len = 0, i = 0; base[i]; i++)
26789 if (ISIDNUM (base[i]) || base[i] == '.')
26790 encoded_filename_len++;
26791 /* Count . at the end. */
26792 if (encoded_filename_len)
26793 encoded_filename_len++;
26795 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
26796 linebuf_len = strlen (linebuf);
26798 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
26799 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
26800 + 16 * 2 + 1);
26801 memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
26802 tail = grp_name + 4;
26803 if (encoded_filename_len)
26805 for (i = 0; base[i]; i++)
26806 if (ISIDNUM (base[i]) || base[i] == '.')
26807 *tail++ = base[i];
26808 *tail++ = '.';
26810 memcpy (tail, linebuf, linebuf_len);
26811 tail += linebuf_len;
26812 *tail++ = '.';
26813 for (i = 0; i < 16; i++)
26814 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
26816 /* Construct a macinfo_entry for DW_MACRO_import
26817 in the empty vector entry before the first define/undef. */
26818 inc = &(*macinfo_table)[idx - 1];
26819 inc->code = DW_MACRO_import;
26820 inc->lineno = 0;
26821 inc->info = ggc_strdup (grp_name);
26822 if (!*macinfo_htab)
26823 *macinfo_htab = new macinfo_hash_type (10);
26824 /* Avoid emitting duplicates. */
26825 slot = (*macinfo_htab)->find_slot (inc, INSERT);
26826 if (*slot != NULL)
26828 inc->code = 0;
26829 inc->info = NULL;
26830 /* If such an entry has been used before, just emit
26831 a DW_MACRO_import op. */
26832 inc = *slot;
26833 output_macinfo_op (inc);
26834 /* And clear all macinfo_entry in the range to avoid emitting them
26835 in the second pass. */
26836 for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
26838 cur->code = 0;
26839 cur->info = NULL;
26842 else
26844 *slot = inc;
26845 inc->lineno = (*macinfo_htab)->elements ();
26846 output_macinfo_op (inc);
26848 return count;
26851 /* Save any strings needed by the macinfo table in the debug str
26852 table. All strings must be collected into the table by the time
26853 index_string is called. */
26855 static void
26856 save_macinfo_strings (void)
26858 unsigned len;
26859 unsigned i;
26860 macinfo_entry *ref;
26862 for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
26864 switch (ref->code)
26866 /* Match the logic in output_macinfo_op to decide on
26867 indirect strings. */
26868 case DW_MACINFO_define:
26869 case DW_MACINFO_undef:
26870 len = strlen (ref->info) + 1;
26871 if (!dwarf_strict
26872 && len > DWARF_OFFSET_SIZE
26873 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
26874 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
26875 set_indirect_string (find_AT_string (ref->info));
26876 break;
26877 case DW_MACRO_define_strp:
26878 case DW_MACRO_undef_strp:
26879 set_indirect_string (find_AT_string (ref->info));
26880 break;
26881 default:
26882 break;
26887 /* Output macinfo section(s). */
26889 static void
26890 output_macinfo (void)
26892 unsigned i;
26893 unsigned long length = vec_safe_length (macinfo_table);
26894 macinfo_entry *ref;
26895 vec<macinfo_entry, va_gc> *files = NULL;
26896 macinfo_hash_type *macinfo_htab = NULL;
26898 if (! length)
26899 return;
26901 /* output_macinfo* uses these interchangeably. */
26902 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_define
26903 && (int) DW_MACINFO_undef == (int) DW_MACRO_undef
26904 && (int) DW_MACINFO_start_file == (int) DW_MACRO_start_file
26905 && (int) DW_MACINFO_end_file == (int) DW_MACRO_end_file);
26907 /* For .debug_macro emit the section header. */
26908 if (!dwarf_strict || dwarf_version >= 5)
26910 dw2_asm_output_data (2, dwarf_version >= 5 ? 5 : 4,
26911 "DWARF macro version number");
26912 if (DWARF_OFFSET_SIZE == 8)
26913 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
26914 else
26915 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
26916 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
26917 (!dwarf_split_debug_info ? debug_line_section_label
26918 : debug_skeleton_line_section_label),
26919 debug_line_section, NULL);
26922 /* In the first loop, it emits the primary .debug_macinfo section
26923 and after each emitted op the macinfo_entry is cleared.
26924 If a longer range of define/undef ops can be optimized using
26925 DW_MACRO_import, the DW_MACRO_import op is emitted and kept in
26926 the vector before the first define/undef in the range and the
26927 whole range of define/undef ops is not emitted and kept. */
26928 for (i = 0; macinfo_table->iterate (i, &ref); i++)
26930 switch (ref->code)
26932 case DW_MACINFO_start_file:
26933 vec_safe_push (files, *ref);
26934 break;
26935 case DW_MACINFO_end_file:
26936 if (!vec_safe_is_empty (files))
26937 files->pop ();
26938 break;
26939 case DW_MACINFO_define:
26940 case DW_MACINFO_undef:
26941 if ((!dwarf_strict || dwarf_version >= 5)
26942 && HAVE_COMDAT_GROUP
26943 && vec_safe_length (files) != 1
26944 && i > 0
26945 && i + 1 < length
26946 && (*macinfo_table)[i - 1].code == 0)
26948 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
26949 if (count)
26951 i += count - 1;
26952 continue;
26955 break;
26956 case 0:
26957 /* A dummy entry may be inserted at the beginning to be able
26958 to optimize the whole block of predefined macros. */
26959 if (i == 0)
26960 continue;
26961 default:
26962 break;
26964 output_macinfo_op (ref);
26965 ref->info = NULL;
26966 ref->code = 0;
26969 if (!macinfo_htab)
26970 return;
26972 delete macinfo_htab;
26973 macinfo_htab = NULL;
26975 /* If any DW_MACRO_import were used, on those DW_MACRO_import entries
26976 terminate the current chain and switch to a new comdat .debug_macinfo
26977 section and emit the define/undef entries within it. */
26978 for (i = 0; macinfo_table->iterate (i, &ref); i++)
26979 switch (ref->code)
26981 case 0:
26982 continue;
26983 case DW_MACRO_import:
26985 char label[MAX_ARTIFICIAL_LABEL_BYTES];
26986 tree comdat_key = get_identifier (ref->info);
26987 /* Terminate the previous .debug_macinfo section. */
26988 dw2_asm_output_data (1, 0, "End compilation unit");
26989 targetm.asm_out.named_section (debug_macinfo_section_name,
26990 SECTION_DEBUG
26991 | SECTION_LINKONCE,
26992 comdat_key);
26993 ASM_GENERATE_INTERNAL_LABEL (label,
26994 DEBUG_MACRO_SECTION_LABEL,
26995 ref->lineno);
26996 ASM_OUTPUT_LABEL (asm_out_file, label);
26997 ref->code = 0;
26998 ref->info = NULL;
26999 dw2_asm_output_data (2, dwarf_version >= 5 ? 5 : 4,
27000 "DWARF macro version number");
27001 if (DWARF_OFFSET_SIZE == 8)
27002 dw2_asm_output_data (1, 1, "Flags: 64-bit");
27003 else
27004 dw2_asm_output_data (1, 0, "Flags: 32-bit");
27006 break;
27007 case DW_MACINFO_define:
27008 case DW_MACINFO_undef:
27009 output_macinfo_op (ref);
27010 ref->code = 0;
27011 ref->info = NULL;
27012 break;
27013 default:
27014 gcc_unreachable ();
27018 /* Initialize the various sections and labels for dwarf output. */
27020 static void
27021 init_sections_and_labels (void)
27023 if (!dwarf_split_debug_info)
27025 debug_info_section = get_section (DEBUG_INFO_SECTION,
27026 SECTION_DEBUG, NULL);
27027 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
27028 SECTION_DEBUG, NULL);
27029 debug_loc_section = get_section (dwarf_version >= 5
27030 ? DEBUG_LOCLISTS_SECTION
27031 : DEBUG_LOC_SECTION,
27032 SECTION_DEBUG, NULL);
27033 debug_macinfo_section_name
27034 = (dwarf_strict && dwarf_version < 5)
27035 ? DEBUG_MACINFO_SECTION : DEBUG_MACRO_SECTION;
27036 debug_macinfo_section = get_section (debug_macinfo_section_name,
27037 SECTION_DEBUG, NULL);
27039 else
27041 debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
27042 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
27043 debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
27044 SECTION_DEBUG | SECTION_EXCLUDE,
27045 NULL);
27046 debug_addr_section = get_section (DEBUG_ADDR_SECTION,
27047 SECTION_DEBUG, NULL);
27048 debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
27049 SECTION_DEBUG, NULL);
27050 debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
27051 SECTION_DEBUG, NULL);
27052 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
27053 DEBUG_SKELETON_ABBREV_SECTION_LABEL, 0);
27055 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections stay in
27056 the main .o, but the skeleton_line goes into the split off dwo. */
27057 debug_skeleton_line_section
27058 = get_section (DEBUG_DWO_LINE_SECTION,
27059 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
27060 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
27061 DEBUG_SKELETON_LINE_SECTION_LABEL, 0);
27062 debug_str_offsets_section = get_section (DEBUG_DWO_STR_OFFSETS_SECTION,
27063 SECTION_DEBUG | SECTION_EXCLUDE,
27064 NULL);
27065 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
27066 DEBUG_SKELETON_INFO_SECTION_LABEL, 0);
27067 debug_loc_section = get_section (dwarf_version >= 5
27068 ? DEBUG_DWO_LOCLISTS_SECTION
27069 : DEBUG_DWO_LOC_SECTION,
27070 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
27071 debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
27072 DEBUG_STR_DWO_SECTION_FLAGS, NULL);
27073 debug_macinfo_section_name
27074 = (dwarf_strict && dwarf_version < 5)
27075 ? DEBUG_DWO_MACINFO_SECTION : DEBUG_DWO_MACRO_SECTION;
27076 debug_macinfo_section = get_section (debug_macinfo_section_name,
27077 SECTION_DEBUG | SECTION_EXCLUDE,
27078 NULL);
27080 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
27081 SECTION_DEBUG, NULL);
27082 debug_line_section = get_section (DEBUG_LINE_SECTION,
27083 SECTION_DEBUG, NULL);
27084 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
27085 SECTION_DEBUG, NULL);
27086 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
27087 SECTION_DEBUG, NULL);
27088 debug_str_section = get_section (DEBUG_STR_SECTION,
27089 DEBUG_STR_SECTION_FLAGS, NULL);
27090 if (!dwarf_split_debug_info && !DWARF2_ASM_LINE_DEBUG_INFO)
27091 debug_line_str_section = get_section (DEBUG_LINE_STR_SECTION,
27092 DEBUG_STR_SECTION_FLAGS, NULL);
27094 debug_ranges_section = get_section (dwarf_version >= 5
27095 ? DEBUG_RNGLISTS_SECTION
27096 : DEBUG_RANGES_SECTION,
27097 SECTION_DEBUG, NULL);
27098 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
27099 SECTION_DEBUG, NULL);
27101 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
27102 DEBUG_ABBREV_SECTION_LABEL, 0);
27103 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
27104 DEBUG_INFO_SECTION_LABEL, 0);
27105 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
27106 DEBUG_LINE_SECTION_LABEL, 0);
27107 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
27108 DEBUG_RANGES_SECTION_LABEL, 0);
27109 if (dwarf_version >= 5 && dwarf_split_debug_info)
27110 ASM_GENERATE_INTERNAL_LABEL (ranges_base_label,
27111 DEBUG_RANGES_SECTION_LABEL, 1);
27112 ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
27113 DEBUG_ADDR_SECTION_LABEL, 0);
27114 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
27115 (dwarf_strict && dwarf_version < 5)
27116 ? DEBUG_MACINFO_SECTION_LABEL
27117 : DEBUG_MACRO_SECTION_LABEL, 0);
27118 ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL, 0);
27121 /* Set up for Dwarf output at the start of compilation. */
27123 static void
27124 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
27126 /* This option is currently broken, see (PR53118 and PR46102). */
27127 if (flag_eliminate_dwarf2_dups
27128 && strstr (lang_hooks.name, "C++"))
27130 warning (0, "-feliminate-dwarf2-dups is broken for C++, ignoring");
27131 flag_eliminate_dwarf2_dups = 0;
27134 /* Allocate the file_table. */
27135 file_table = hash_table<dwarf_file_hasher>::create_ggc (50);
27137 #ifndef DWARF2_LINENO_DEBUGGING_INFO
27138 /* Allocate the decl_die_table. */
27139 decl_die_table = hash_table<decl_die_hasher>::create_ggc (10);
27141 /* Allocate the decl_loc_table. */
27142 decl_loc_table = hash_table<decl_loc_hasher>::create_ggc (10);
27144 /* Allocate the cached_dw_loc_list_table. */
27145 cached_dw_loc_list_table = hash_table<dw_loc_list_hasher>::create_ggc (10);
27147 /* Allocate the initial hunk of the decl_scope_table. */
27148 vec_alloc (decl_scope_table, 256);
27150 /* Allocate the initial hunk of the abbrev_die_table. */
27151 vec_alloc (abbrev_die_table, 256);
27152 /* Zero-th entry is allocated, but unused. */
27153 abbrev_die_table->quick_push (NULL);
27155 /* Allocate the dwarf_proc_stack_usage_map. */
27156 dwarf_proc_stack_usage_map = new hash_map<dw_die_ref, int>;
27158 /* Allocate the pubtypes and pubnames vectors. */
27159 vec_alloc (pubname_table, 32);
27160 vec_alloc (pubtype_table, 32);
27162 vec_alloc (incomplete_types, 64);
27164 vec_alloc (used_rtx_array, 32);
27166 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
27167 vec_alloc (macinfo_table, 64);
27168 #endif
27170 /* If front-ends already registered a main translation unit but we were not
27171 ready to perform the association, do this now. */
27172 if (main_translation_unit != NULL_TREE)
27173 equate_decl_number_to_die (main_translation_unit, comp_unit_die ());
27176 /* Called before compile () starts outputtting functions, variables
27177 and toplevel asms into assembly. */
27179 static void
27180 dwarf2out_assembly_start (void)
27182 #ifndef DWARF2_LINENO_DEBUGGING_INFO
27183 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
27184 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
27185 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
27186 COLD_TEXT_SECTION_LABEL, 0);
27187 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
27189 switch_to_section (text_section);
27190 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
27191 #endif
27193 /* Make sure the line number table for .text always exists. */
27194 text_section_line_info = new_line_info_table ();
27195 text_section_line_info->end_label = text_end_label;
27197 #ifdef DWARF2_LINENO_DEBUGGING_INFO
27198 cur_line_info_table = text_section_line_info;
27199 #endif
27201 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
27202 && dwarf2out_do_cfi_asm ()
27203 && (!(flag_unwind_tables || flag_exceptions)
27204 || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
27205 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
27208 /* A helper function for dwarf2out_finish called through
27209 htab_traverse. Assign a string its index. All strings must be
27210 collected into the table by the time index_string is called,
27211 because the indexing code relies on htab_traverse to traverse nodes
27212 in the same order for each run. */
27215 index_string (indirect_string_node **h, unsigned int *index)
27217 indirect_string_node *node = *h;
27219 find_string_form (node);
27220 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
27222 gcc_assert (node->index == NO_INDEX_ASSIGNED);
27223 node->index = *index;
27224 *index += 1;
27226 return 1;
27229 /* A helper function for output_indirect_strings called through
27230 htab_traverse. Output the offset to a string and update the
27231 current offset. */
27234 output_index_string_offset (indirect_string_node **h, unsigned int *offset)
27236 indirect_string_node *node = *h;
27238 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
27240 /* Assert that this node has been assigned an index. */
27241 gcc_assert (node->index != NO_INDEX_ASSIGNED
27242 && node->index != NOT_INDEXED);
27243 dw2_asm_output_data (DWARF_OFFSET_SIZE, *offset,
27244 "indexed string 0x%x: %s", node->index, node->str);
27245 *offset += strlen (node->str) + 1;
27247 return 1;
27250 /* A helper function for dwarf2out_finish called through
27251 htab_traverse. Output the indexed string. */
27254 output_index_string (indirect_string_node **h, unsigned int *cur_idx)
27256 struct indirect_string_node *node = *h;
27258 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
27260 /* Assert that the strings are output in the same order as their
27261 indexes were assigned. */
27262 gcc_assert (*cur_idx == node->index);
27263 assemble_string (node->str, strlen (node->str) + 1);
27264 *cur_idx += 1;
27266 return 1;
27269 /* A helper function for dwarf2out_finish called through
27270 htab_traverse. Emit one queued .debug_str string. */
27273 output_indirect_string (indirect_string_node **h, enum dwarf_form form)
27275 struct indirect_string_node *node = *h;
27277 node->form = find_string_form (node);
27278 if (node->form == form && node->refcount > 0)
27280 ASM_OUTPUT_LABEL (asm_out_file, node->label);
27281 assemble_string (node->str, strlen (node->str) + 1);
27284 return 1;
27287 /* Output the indexed string table. */
27289 static void
27290 output_indirect_strings (void)
27292 switch_to_section (debug_str_section);
27293 if (!dwarf_split_debug_info)
27294 debug_str_hash->traverse<enum dwarf_form,
27295 output_indirect_string> (DW_FORM_strp);
27296 else
27298 unsigned int offset = 0;
27299 unsigned int cur_idx = 0;
27301 skeleton_debug_str_hash->traverse<enum dwarf_form,
27302 output_indirect_string> (DW_FORM_strp);
27304 switch_to_section (debug_str_offsets_section);
27305 debug_str_hash->traverse_noresize
27306 <unsigned int *, output_index_string_offset> (&offset);
27307 switch_to_section (debug_str_dwo_section);
27308 debug_str_hash->traverse_noresize<unsigned int *, output_index_string>
27309 (&cur_idx);
27313 /* Callback for htab_traverse to assign an index to an entry in the
27314 table, and to write that entry to the .debug_addr section. */
27317 output_addr_table_entry (addr_table_entry **slot, unsigned int *cur_index)
27319 addr_table_entry *entry = *slot;
27321 if (entry->refcount == 0)
27323 gcc_assert (entry->index == NO_INDEX_ASSIGNED
27324 || entry->index == NOT_INDEXED);
27325 return 1;
27328 gcc_assert (entry->index == *cur_index);
27329 (*cur_index)++;
27331 switch (entry->kind)
27333 case ate_kind_rtx:
27334 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
27335 "0x%x", entry->index);
27336 break;
27337 case ate_kind_rtx_dtprel:
27338 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
27339 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
27340 DWARF2_ADDR_SIZE,
27341 entry->addr.rtl);
27342 fputc ('\n', asm_out_file);
27343 break;
27344 case ate_kind_label:
27345 dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
27346 "0x%x", entry->index);
27347 break;
27348 default:
27349 gcc_unreachable ();
27351 return 1;
27354 /* Produce the .debug_addr section. */
27356 static void
27357 output_addr_table (void)
27359 unsigned int index = 0;
27360 if (addr_index_table == NULL || addr_index_table->size () == 0)
27361 return;
27363 switch_to_section (debug_addr_section);
27364 addr_index_table
27365 ->traverse_noresize<unsigned int *, output_addr_table_entry> (&index);
27368 #if ENABLE_ASSERT_CHECKING
27369 /* Verify that all marks are clear. */
27371 static void
27372 verify_marks_clear (dw_die_ref die)
27374 dw_die_ref c;
27376 gcc_assert (! die->die_mark);
27377 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
27379 #endif /* ENABLE_ASSERT_CHECKING */
27381 /* Clear the marks for a die and its children.
27382 Be cool if the mark isn't set. */
27384 static void
27385 prune_unmark_dies (dw_die_ref die)
27387 dw_die_ref c;
27389 if (die->die_mark)
27390 die->die_mark = 0;
27391 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
27394 /* Given LOC that is referenced by a DIE we're marking as used, find all
27395 referenced DWARF procedures it references and mark them as used. */
27397 static void
27398 prune_unused_types_walk_loc_descr (dw_loc_descr_ref loc)
27400 for (; loc != NULL; loc = loc->dw_loc_next)
27401 switch (loc->dw_loc_opc)
27403 case DW_OP_implicit_pointer:
27404 case DW_OP_convert:
27405 case DW_OP_reinterpret:
27406 case DW_OP_GNU_implicit_pointer:
27407 case DW_OP_GNU_convert:
27408 case DW_OP_GNU_reinterpret:
27409 if (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref)
27410 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
27411 break;
27412 case DW_OP_call2:
27413 case DW_OP_call4:
27414 case DW_OP_call_ref:
27415 case DW_OP_const_type:
27416 case DW_OP_GNU_const_type:
27417 case DW_OP_GNU_parameter_ref:
27418 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref);
27419 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
27420 break;
27421 case DW_OP_regval_type:
27422 case DW_OP_deref_type:
27423 case DW_OP_GNU_regval_type:
27424 case DW_OP_GNU_deref_type:
27425 gcc_assert (loc->dw_loc_oprnd2.val_class == dw_val_class_die_ref);
27426 prune_unused_types_mark (loc->dw_loc_oprnd2.v.val_die_ref.die, 1);
27427 break;
27428 case DW_OP_entry_value:
27429 case DW_OP_GNU_entry_value:
27430 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_loc);
27431 prune_unused_types_walk_loc_descr (loc->dw_loc_oprnd1.v.val_loc);
27432 break;
27433 default:
27434 break;
27438 /* Given DIE that we're marking as used, find any other dies
27439 it references as attributes and mark them as used. */
27441 static void
27442 prune_unused_types_walk_attribs (dw_die_ref die)
27444 dw_attr_node *a;
27445 unsigned ix;
27447 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
27449 switch (AT_class (a))
27451 /* Make sure DWARF procedures referenced by location descriptions will
27452 get emitted. */
27453 case dw_val_class_loc:
27454 prune_unused_types_walk_loc_descr (AT_loc (a));
27455 break;
27456 case dw_val_class_loc_list:
27457 for (dw_loc_list_ref list = AT_loc_list (a);
27458 list != NULL;
27459 list = list->dw_loc_next)
27460 prune_unused_types_walk_loc_descr (list->expr);
27461 break;
27463 case dw_val_class_die_ref:
27464 /* A reference to another DIE.
27465 Make sure that it will get emitted.
27466 If it was broken out into a comdat group, don't follow it. */
27467 if (! AT_ref (a)->comdat_type_p
27468 || a->dw_attr == DW_AT_specification)
27469 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
27470 break;
27472 case dw_val_class_str:
27473 /* Set the string's refcount to 0 so that prune_unused_types_mark
27474 accounts properly for it. */
27475 a->dw_attr_val.v.val_str->refcount = 0;
27476 break;
27478 default:
27479 break;
27484 /* Mark the generic parameters and arguments children DIEs of DIE. */
27486 static void
27487 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
27489 dw_die_ref c;
27491 if (die == NULL || die->die_child == NULL)
27492 return;
27493 c = die->die_child;
27496 if (is_template_parameter (c))
27497 prune_unused_types_mark (c, 1);
27498 c = c->die_sib;
27499 } while (c && c != die->die_child);
27502 /* Mark DIE as being used. If DOKIDS is true, then walk down
27503 to DIE's children. */
27505 static void
27506 prune_unused_types_mark (dw_die_ref die, int dokids)
27508 dw_die_ref c;
27510 if (die->die_mark == 0)
27512 /* We haven't done this node yet. Mark it as used. */
27513 die->die_mark = 1;
27514 /* If this is the DIE of a generic type instantiation,
27515 mark the children DIEs that describe its generic parms and
27516 args. */
27517 prune_unused_types_mark_generic_parms_dies (die);
27519 /* We also have to mark its parents as used.
27520 (But we don't want to mark our parent's kids due to this,
27521 unless it is a class.) */
27522 if (die->die_parent)
27523 prune_unused_types_mark (die->die_parent,
27524 class_scope_p (die->die_parent));
27526 /* Mark any referenced nodes. */
27527 prune_unused_types_walk_attribs (die);
27529 /* If this node is a specification,
27530 also mark the definition, if it exists. */
27531 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
27532 prune_unused_types_mark (die->die_definition, 1);
27535 if (dokids && die->die_mark != 2)
27537 /* We need to walk the children, but haven't done so yet.
27538 Remember that we've walked the kids. */
27539 die->die_mark = 2;
27541 /* If this is an array type, we need to make sure our
27542 kids get marked, even if they're types. If we're
27543 breaking out types into comdat sections, do this
27544 for all type definitions. */
27545 if (die->die_tag == DW_TAG_array_type
27546 || (use_debug_types
27547 && is_type_die (die) && ! is_declaration_die (die)))
27548 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
27549 else
27550 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
27554 /* For local classes, look if any static member functions were emitted
27555 and if so, mark them. */
27557 static void
27558 prune_unused_types_walk_local_classes (dw_die_ref die)
27560 dw_die_ref c;
27562 if (die->die_mark == 2)
27563 return;
27565 switch (die->die_tag)
27567 case DW_TAG_structure_type:
27568 case DW_TAG_union_type:
27569 case DW_TAG_class_type:
27570 break;
27572 case DW_TAG_subprogram:
27573 if (!get_AT_flag (die, DW_AT_declaration)
27574 || die->die_definition != NULL)
27575 prune_unused_types_mark (die, 1);
27576 return;
27578 default:
27579 return;
27582 /* Mark children. */
27583 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
27586 /* Walk the tree DIE and mark types that we actually use. */
27588 static void
27589 prune_unused_types_walk (dw_die_ref die)
27591 dw_die_ref c;
27593 /* Don't do anything if this node is already marked and
27594 children have been marked as well. */
27595 if (die->die_mark == 2)
27596 return;
27598 switch (die->die_tag)
27600 case DW_TAG_structure_type:
27601 case DW_TAG_union_type:
27602 case DW_TAG_class_type:
27603 if (die->die_perennial_p)
27604 break;
27606 for (c = die->die_parent; c; c = c->die_parent)
27607 if (c->die_tag == DW_TAG_subprogram)
27608 break;
27610 /* Finding used static member functions inside of classes
27611 is needed just for local classes, because for other classes
27612 static member function DIEs with DW_AT_specification
27613 are emitted outside of the DW_TAG_*_type. If we ever change
27614 it, we'd need to call this even for non-local classes. */
27615 if (c)
27616 prune_unused_types_walk_local_classes (die);
27618 /* It's a type node --- don't mark it. */
27619 return;
27621 case DW_TAG_const_type:
27622 case DW_TAG_packed_type:
27623 case DW_TAG_pointer_type:
27624 case DW_TAG_reference_type:
27625 case DW_TAG_rvalue_reference_type:
27626 case DW_TAG_volatile_type:
27627 case DW_TAG_typedef:
27628 case DW_TAG_array_type:
27629 case DW_TAG_interface_type:
27630 case DW_TAG_friend:
27631 case DW_TAG_enumeration_type:
27632 case DW_TAG_subroutine_type:
27633 case DW_TAG_string_type:
27634 case DW_TAG_set_type:
27635 case DW_TAG_subrange_type:
27636 case DW_TAG_ptr_to_member_type:
27637 case DW_TAG_file_type:
27638 /* Type nodes are useful only when other DIEs reference them --- don't
27639 mark them. */
27640 /* FALLTHROUGH */
27642 case DW_TAG_dwarf_procedure:
27643 /* Likewise for DWARF procedures. */
27645 if (die->die_perennial_p)
27646 break;
27648 return;
27650 default:
27651 /* Mark everything else. */
27652 break;
27655 if (die->die_mark == 0)
27657 die->die_mark = 1;
27659 /* Now, mark any dies referenced from here. */
27660 prune_unused_types_walk_attribs (die);
27663 die->die_mark = 2;
27665 /* Mark children. */
27666 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
27669 /* Increment the string counts on strings referred to from DIE's
27670 attributes. */
27672 static void
27673 prune_unused_types_update_strings (dw_die_ref die)
27675 dw_attr_node *a;
27676 unsigned ix;
27678 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
27679 if (AT_class (a) == dw_val_class_str)
27681 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
27682 s->refcount++;
27683 /* Avoid unnecessarily putting strings that are used less than
27684 twice in the hash table. */
27685 if (s->refcount
27686 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
27688 indirect_string_node **slot
27689 = debug_str_hash->find_slot_with_hash (s->str,
27690 htab_hash_string (s->str),
27691 INSERT);
27692 gcc_assert (*slot == NULL);
27693 *slot = s;
27698 /* Mark DIE and its children as removed. */
27700 static void
27701 mark_removed (dw_die_ref die)
27703 dw_die_ref c;
27704 die->removed = true;
27705 FOR_EACH_CHILD (die, c, mark_removed (c));
27708 /* Remove from the tree DIE any dies that aren't marked. */
27710 static void
27711 prune_unused_types_prune (dw_die_ref die)
27713 dw_die_ref c;
27715 gcc_assert (die->die_mark);
27716 prune_unused_types_update_strings (die);
27718 if (! die->die_child)
27719 return;
27721 c = die->die_child;
27722 do {
27723 dw_die_ref prev = c, next;
27724 for (c = c->die_sib; ! c->die_mark; c = next)
27725 if (c == die->die_child)
27727 /* No marked children between 'prev' and the end of the list. */
27728 if (prev == c)
27729 /* No marked children at all. */
27730 die->die_child = NULL;
27731 else
27733 prev->die_sib = c->die_sib;
27734 die->die_child = prev;
27736 c->die_sib = NULL;
27737 mark_removed (c);
27738 return;
27740 else
27742 next = c->die_sib;
27743 c->die_sib = NULL;
27744 mark_removed (c);
27747 if (c != prev->die_sib)
27748 prev->die_sib = c;
27749 prune_unused_types_prune (c);
27750 } while (c != die->die_child);
27753 /* Remove dies representing declarations that we never use. */
27755 static void
27756 prune_unused_types (void)
27758 unsigned int i;
27759 limbo_die_node *node;
27760 comdat_type_node *ctnode;
27761 pubname_entry *pub;
27762 dw_die_ref base_type;
27764 #if ENABLE_ASSERT_CHECKING
27765 /* All the marks should already be clear. */
27766 verify_marks_clear (comp_unit_die ());
27767 for (node = limbo_die_list; node; node = node->next)
27768 verify_marks_clear (node->die);
27769 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
27770 verify_marks_clear (ctnode->root_die);
27771 #endif /* ENABLE_ASSERT_CHECKING */
27773 /* Mark types that are used in global variables. */
27774 premark_types_used_by_global_vars ();
27776 /* Set the mark on nodes that are actually used. */
27777 prune_unused_types_walk (comp_unit_die ());
27778 for (node = limbo_die_list; node; node = node->next)
27779 prune_unused_types_walk (node->die);
27780 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
27782 prune_unused_types_walk (ctnode->root_die);
27783 prune_unused_types_mark (ctnode->type_die, 1);
27786 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
27787 are unusual in that they are pubnames that are the children of pubtypes.
27788 They should only be marked via their parent DW_TAG_enumeration_type die,
27789 not as roots in themselves. */
27790 FOR_EACH_VEC_ELT (*pubname_table, i, pub)
27791 if (pub->die->die_tag != DW_TAG_enumerator)
27792 prune_unused_types_mark (pub->die, 1);
27793 for (i = 0; base_types.iterate (i, &base_type); i++)
27794 prune_unused_types_mark (base_type, 1);
27796 if (debug_str_hash)
27797 debug_str_hash->empty ();
27798 if (skeleton_debug_str_hash)
27799 skeleton_debug_str_hash->empty ();
27800 prune_unused_types_prune (comp_unit_die ());
27801 for (limbo_die_node **pnode = &limbo_die_list; *pnode; )
27803 node = *pnode;
27804 if (!node->die->die_mark)
27805 *pnode = node->next;
27806 else
27808 prune_unused_types_prune (node->die);
27809 pnode = &node->next;
27812 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
27813 prune_unused_types_prune (ctnode->root_die);
27815 /* Leave the marks clear. */
27816 prune_unmark_dies (comp_unit_die ());
27817 for (node = limbo_die_list; node; node = node->next)
27818 prune_unmark_dies (node->die);
27819 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
27820 prune_unmark_dies (ctnode->root_die);
27823 /* Helpers to manipulate hash table of comdat type units. */
27825 struct comdat_type_hasher : nofree_ptr_hash <comdat_type_node>
27827 static inline hashval_t hash (const comdat_type_node *);
27828 static inline bool equal (const comdat_type_node *, const comdat_type_node *);
27831 inline hashval_t
27832 comdat_type_hasher::hash (const comdat_type_node *type_node)
27834 hashval_t h;
27835 memcpy (&h, type_node->signature, sizeof (h));
27836 return h;
27839 inline bool
27840 comdat_type_hasher::equal (const comdat_type_node *type_node_1,
27841 const comdat_type_node *type_node_2)
27843 return (! memcmp (type_node_1->signature, type_node_2->signature,
27844 DWARF_TYPE_SIGNATURE_SIZE));
27847 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
27848 to the location it would have been added, should we know its
27849 DECL_ASSEMBLER_NAME when we added other attributes. This will
27850 probably improve compactness of debug info, removing equivalent
27851 abbrevs, and hide any differences caused by deferring the
27852 computation of the assembler name, triggered by e.g. PCH. */
27854 static inline void
27855 move_linkage_attr (dw_die_ref die)
27857 unsigned ix = vec_safe_length (die->die_attr);
27858 dw_attr_node linkage = (*die->die_attr)[ix - 1];
27860 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
27861 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
27863 while (--ix > 0)
27865 dw_attr_node *prev = &(*die->die_attr)[ix - 1];
27867 if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
27868 break;
27871 if (ix != vec_safe_length (die->die_attr) - 1)
27873 die->die_attr->pop ();
27874 die->die_attr->quick_insert (ix, linkage);
27878 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
27879 referenced from typed stack ops and count how often they are used. */
27881 static void
27882 mark_base_types (dw_loc_descr_ref loc)
27884 dw_die_ref base_type = NULL;
27886 for (; loc; loc = loc->dw_loc_next)
27888 switch (loc->dw_loc_opc)
27890 case DW_OP_regval_type:
27891 case DW_OP_deref_type:
27892 case DW_OP_GNU_regval_type:
27893 case DW_OP_GNU_deref_type:
27894 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
27895 break;
27896 case DW_OP_convert:
27897 case DW_OP_reinterpret:
27898 case DW_OP_GNU_convert:
27899 case DW_OP_GNU_reinterpret:
27900 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
27901 continue;
27902 /* FALLTHRU */
27903 case DW_OP_const_type:
27904 case DW_OP_GNU_const_type:
27905 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
27906 break;
27907 case DW_OP_entry_value:
27908 case DW_OP_GNU_entry_value:
27909 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
27910 continue;
27911 default:
27912 continue;
27914 gcc_assert (base_type->die_parent == comp_unit_die ());
27915 if (base_type->die_mark)
27916 base_type->die_mark++;
27917 else
27919 base_types.safe_push (base_type);
27920 base_type->die_mark = 1;
27925 /* Comparison function for sorting marked base types. */
27927 static int
27928 base_type_cmp (const void *x, const void *y)
27930 dw_die_ref dx = *(const dw_die_ref *) x;
27931 dw_die_ref dy = *(const dw_die_ref *) y;
27932 unsigned int byte_size1, byte_size2;
27933 unsigned int encoding1, encoding2;
27934 if (dx->die_mark > dy->die_mark)
27935 return -1;
27936 if (dx->die_mark < dy->die_mark)
27937 return 1;
27938 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
27939 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
27940 if (byte_size1 < byte_size2)
27941 return 1;
27942 if (byte_size1 > byte_size2)
27943 return -1;
27944 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
27945 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
27946 if (encoding1 < encoding2)
27947 return 1;
27948 if (encoding1 > encoding2)
27949 return -1;
27950 return 0;
27953 /* Move base types marked by mark_base_types as early as possible
27954 in the CU, sorted by decreasing usage count both to make the
27955 uleb128 references as small as possible and to make sure they
27956 will have die_offset already computed by calc_die_sizes when
27957 sizes of typed stack loc ops is computed. */
27959 static void
27960 move_marked_base_types (void)
27962 unsigned int i;
27963 dw_die_ref base_type, die, c;
27965 if (base_types.is_empty ())
27966 return;
27968 /* Sort by decreasing usage count, they will be added again in that
27969 order later on. */
27970 base_types.qsort (base_type_cmp);
27971 die = comp_unit_die ();
27972 c = die->die_child;
27975 dw_die_ref prev = c;
27976 c = c->die_sib;
27977 while (c->die_mark)
27979 remove_child_with_prev (c, prev);
27980 /* As base types got marked, there must be at least
27981 one node other than DW_TAG_base_type. */
27982 gcc_assert (die->die_child != NULL);
27983 c = prev->die_sib;
27986 while (c != die->die_child);
27987 gcc_assert (die->die_child);
27988 c = die->die_child;
27989 for (i = 0; base_types.iterate (i, &base_type); i++)
27991 base_type->die_mark = 0;
27992 base_type->die_sib = c->die_sib;
27993 c->die_sib = base_type;
27994 c = base_type;
27998 /* Helper function for resolve_addr, attempt to resolve
27999 one CONST_STRING, return true if successful. Similarly verify that
28000 SYMBOL_REFs refer to variables emitted in the current CU. */
28002 static bool
28003 resolve_one_addr (rtx *addr)
28005 rtx rtl = *addr;
28007 if (GET_CODE (rtl) == CONST_STRING)
28009 size_t len = strlen (XSTR (rtl, 0)) + 1;
28010 tree t = build_string (len, XSTR (rtl, 0));
28011 tree tlen = size_int (len - 1);
28012 TREE_TYPE (t)
28013 = build_array_type (char_type_node, build_index_type (tlen));
28014 rtl = lookup_constant_def (t);
28015 if (!rtl || !MEM_P (rtl))
28016 return false;
28017 rtl = XEXP (rtl, 0);
28018 if (GET_CODE (rtl) == SYMBOL_REF
28019 && SYMBOL_REF_DECL (rtl)
28020 && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
28021 return false;
28022 vec_safe_push (used_rtx_array, rtl);
28023 *addr = rtl;
28024 return true;
28027 if (GET_CODE (rtl) == SYMBOL_REF
28028 && SYMBOL_REF_DECL (rtl))
28030 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
28032 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
28033 return false;
28035 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
28036 return false;
28039 if (GET_CODE (rtl) == CONST)
28041 subrtx_ptr_iterator::array_type array;
28042 FOR_EACH_SUBRTX_PTR (iter, array, &XEXP (rtl, 0), ALL)
28043 if (!resolve_one_addr (*iter))
28044 return false;
28047 return true;
28050 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
28051 if possible, and create DW_TAG_dwarf_procedure that can be referenced
28052 from DW_OP_implicit_pointer if the string hasn't been seen yet. */
28054 static rtx
28055 string_cst_pool_decl (tree t)
28057 rtx rtl = output_constant_def (t, 1);
28058 unsigned char *array;
28059 dw_loc_descr_ref l;
28060 tree decl;
28061 size_t len;
28062 dw_die_ref ref;
28064 if (!rtl || !MEM_P (rtl))
28065 return NULL_RTX;
28066 rtl = XEXP (rtl, 0);
28067 if (GET_CODE (rtl) != SYMBOL_REF
28068 || SYMBOL_REF_DECL (rtl) == NULL_TREE)
28069 return NULL_RTX;
28071 decl = SYMBOL_REF_DECL (rtl);
28072 if (!lookup_decl_die (decl))
28074 len = TREE_STRING_LENGTH (t);
28075 vec_safe_push (used_rtx_array, rtl);
28076 ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
28077 array = ggc_vec_alloc<unsigned char> (len);
28078 memcpy (array, TREE_STRING_POINTER (t), len);
28079 l = new_loc_descr (DW_OP_implicit_value, len, 0);
28080 l->dw_loc_oprnd2.val_class = dw_val_class_vec;
28081 l->dw_loc_oprnd2.v.val_vec.length = len;
28082 l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
28083 l->dw_loc_oprnd2.v.val_vec.array = array;
28084 add_AT_loc (ref, DW_AT_location, l);
28085 equate_decl_number_to_die (decl, ref);
28087 return rtl;
28090 /* Helper function of resolve_addr_in_expr. LOC is
28091 a DW_OP_addr followed by DW_OP_stack_value, either at the start
28092 of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
28093 resolved. Replace it (both DW_OP_addr and DW_OP_stack_value)
28094 with DW_OP_implicit_pointer if possible
28095 and return true, if unsuccessful, return false. */
28097 static bool
28098 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
28100 rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
28101 HOST_WIDE_INT offset = 0;
28102 dw_die_ref ref = NULL;
28103 tree decl;
28105 if (GET_CODE (rtl) == CONST
28106 && GET_CODE (XEXP (rtl, 0)) == PLUS
28107 && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
28109 offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
28110 rtl = XEXP (XEXP (rtl, 0), 0);
28112 if (GET_CODE (rtl) == CONST_STRING)
28114 size_t len = strlen (XSTR (rtl, 0)) + 1;
28115 tree t = build_string (len, XSTR (rtl, 0));
28116 tree tlen = size_int (len - 1);
28118 TREE_TYPE (t)
28119 = build_array_type (char_type_node, build_index_type (tlen));
28120 rtl = string_cst_pool_decl (t);
28121 if (!rtl)
28122 return false;
28124 if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
28126 decl = SYMBOL_REF_DECL (rtl);
28127 if (VAR_P (decl) && !DECL_EXTERNAL (decl))
28129 ref = lookup_decl_die (decl);
28130 if (ref && (get_AT (ref, DW_AT_location)
28131 || get_AT (ref, DW_AT_const_value)))
28133 loc->dw_loc_opc = dwarf_OP (DW_OP_implicit_pointer);
28134 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
28135 loc->dw_loc_oprnd1.val_entry = NULL;
28136 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
28137 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
28138 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
28139 loc->dw_loc_oprnd2.v.val_int = offset;
28140 return true;
28144 return false;
28147 /* Helper function for resolve_addr, handle one location
28148 expression, return false if at least one CONST_STRING or SYMBOL_REF in
28149 the location list couldn't be resolved. */
28151 static bool
28152 resolve_addr_in_expr (dw_loc_descr_ref loc)
28154 dw_loc_descr_ref keep = NULL;
28155 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
28156 switch (loc->dw_loc_opc)
28158 case DW_OP_addr:
28159 if (!resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
28161 if ((prev == NULL
28162 || prev->dw_loc_opc == DW_OP_piece
28163 || prev->dw_loc_opc == DW_OP_bit_piece)
28164 && loc->dw_loc_next
28165 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
28166 && (!dwarf_strict || dwarf_version >= 5)
28167 && optimize_one_addr_into_implicit_ptr (loc))
28168 break;
28169 return false;
28171 break;
28172 case DW_OP_GNU_addr_index:
28173 case DW_OP_GNU_const_index:
28174 if (loc->dw_loc_opc == DW_OP_GNU_addr_index
28175 || (loc->dw_loc_opc == DW_OP_GNU_const_index && loc->dtprel))
28177 rtx rtl = loc->dw_loc_oprnd1.val_entry->addr.rtl;
28178 if (!resolve_one_addr (&rtl))
28179 return false;
28180 remove_addr_table_entry (loc->dw_loc_oprnd1.val_entry);
28181 loc->dw_loc_oprnd1.val_entry
28182 = add_addr_table_entry (rtl, ate_kind_rtx);
28184 break;
28185 case DW_OP_const4u:
28186 case DW_OP_const8u:
28187 if (loc->dtprel
28188 && !resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
28189 return false;
28190 break;
28191 case DW_OP_plus_uconst:
28192 if (size_of_loc_descr (loc)
28193 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
28195 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
28197 dw_loc_descr_ref repl
28198 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
28199 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
28200 add_loc_descr (&repl, loc->dw_loc_next);
28201 *loc = *repl;
28203 break;
28204 case DW_OP_implicit_value:
28205 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
28206 && !resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr))
28207 return false;
28208 break;
28209 case DW_OP_implicit_pointer:
28210 case DW_OP_GNU_implicit_pointer:
28211 case DW_OP_GNU_parameter_ref:
28212 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
28214 dw_die_ref ref
28215 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
28216 if (ref == NULL)
28217 return false;
28218 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
28219 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
28220 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
28222 break;
28223 case DW_OP_const_type:
28224 case DW_OP_regval_type:
28225 case DW_OP_deref_type:
28226 case DW_OP_convert:
28227 case DW_OP_reinterpret:
28228 case DW_OP_GNU_const_type:
28229 case DW_OP_GNU_regval_type:
28230 case DW_OP_GNU_deref_type:
28231 case DW_OP_GNU_convert:
28232 case DW_OP_GNU_reinterpret:
28233 while (loc->dw_loc_next
28234 && (loc->dw_loc_next->dw_loc_opc == DW_OP_convert
28235 || loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert))
28237 dw_die_ref base1, base2;
28238 unsigned enc1, enc2, size1, size2;
28239 if (loc->dw_loc_opc == DW_OP_regval_type
28240 || loc->dw_loc_opc == DW_OP_deref_type
28241 || loc->dw_loc_opc == DW_OP_GNU_regval_type
28242 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
28243 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
28244 else if (loc->dw_loc_oprnd1.val_class
28245 == dw_val_class_unsigned_const)
28246 break;
28247 else
28248 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
28249 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
28250 == dw_val_class_unsigned_const)
28251 break;
28252 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
28253 gcc_assert (base1->die_tag == DW_TAG_base_type
28254 && base2->die_tag == DW_TAG_base_type);
28255 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
28256 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
28257 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
28258 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
28259 if (size1 == size2
28260 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
28261 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
28262 && loc != keep)
28263 || enc1 == enc2))
28265 /* Optimize away next DW_OP_convert after
28266 adjusting LOC's base type die reference. */
28267 if (loc->dw_loc_opc == DW_OP_regval_type
28268 || loc->dw_loc_opc == DW_OP_deref_type
28269 || loc->dw_loc_opc == DW_OP_GNU_regval_type
28270 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
28271 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
28272 else
28273 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
28274 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
28275 continue;
28277 /* Don't change integer DW_OP_convert after e.g. floating
28278 point typed stack entry. */
28279 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
28280 keep = loc->dw_loc_next;
28281 break;
28283 break;
28284 default:
28285 break;
28287 return true;
28290 /* Helper function of resolve_addr. DIE had DW_AT_location of
28291 DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
28292 and DW_OP_addr couldn't be resolved. resolve_addr has already
28293 removed the DW_AT_location attribute. This function attempts to
28294 add a new DW_AT_location attribute with DW_OP_implicit_pointer
28295 to it or DW_AT_const_value attribute, if possible. */
28297 static void
28298 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
28300 if (!VAR_P (decl)
28301 || lookup_decl_die (decl) != die
28302 || DECL_EXTERNAL (decl)
28303 || !TREE_STATIC (decl)
28304 || DECL_INITIAL (decl) == NULL_TREE
28305 || DECL_P (DECL_INITIAL (decl))
28306 || get_AT (die, DW_AT_const_value))
28307 return;
28309 tree init = DECL_INITIAL (decl);
28310 HOST_WIDE_INT offset = 0;
28311 /* For variables that have been optimized away and thus
28312 don't have a memory location, see if we can emit
28313 DW_AT_const_value instead. */
28314 if (tree_add_const_value_attribute (die, init))
28315 return;
28316 if (dwarf_strict && dwarf_version < 5)
28317 return;
28318 /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
28319 and ADDR_EXPR refers to a decl that has DW_AT_location or
28320 DW_AT_const_value (but isn't addressable, otherwise
28321 resolving the original DW_OP_addr wouldn't fail), see if
28322 we can add DW_OP_implicit_pointer. */
28323 STRIP_NOPS (init);
28324 if (TREE_CODE (init) == POINTER_PLUS_EXPR
28325 && tree_fits_shwi_p (TREE_OPERAND (init, 1)))
28327 offset = tree_to_shwi (TREE_OPERAND (init, 1));
28328 init = TREE_OPERAND (init, 0);
28329 STRIP_NOPS (init);
28331 if (TREE_CODE (init) != ADDR_EXPR)
28332 return;
28333 if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
28334 && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
28335 || (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL
28336 && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
28337 && TREE_OPERAND (init, 0) != decl))
28339 dw_die_ref ref;
28340 dw_loc_descr_ref l;
28342 if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
28344 rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
28345 if (!rtl)
28346 return;
28347 decl = SYMBOL_REF_DECL (rtl);
28349 else
28350 decl = TREE_OPERAND (init, 0);
28351 ref = lookup_decl_die (decl);
28352 if (ref == NULL
28353 || (!get_AT (ref, DW_AT_location)
28354 && !get_AT (ref, DW_AT_const_value)))
28355 return;
28356 l = new_loc_descr (dwarf_OP (DW_OP_implicit_pointer), 0, offset);
28357 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
28358 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
28359 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
28360 add_AT_loc (die, DW_AT_location, l);
28364 /* Return NULL if l is a DWARF expression, or first op that is not
28365 valid DWARF expression. */
28367 static dw_loc_descr_ref
28368 non_dwarf_expression (dw_loc_descr_ref l)
28370 while (l)
28372 if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
28373 return l;
28374 switch (l->dw_loc_opc)
28376 case DW_OP_regx:
28377 case DW_OP_implicit_value:
28378 case DW_OP_stack_value:
28379 case DW_OP_implicit_pointer:
28380 case DW_OP_GNU_implicit_pointer:
28381 case DW_OP_GNU_parameter_ref:
28382 case DW_OP_piece:
28383 case DW_OP_bit_piece:
28384 return l;
28385 default:
28386 break;
28388 l = l->dw_loc_next;
28390 return NULL;
28393 /* Return adjusted copy of EXPR:
28394 If it is empty DWARF expression, return it.
28395 If it is valid non-empty DWARF expression,
28396 return copy of EXPR with copy of DEREF appended to it.
28397 If it is DWARF expression followed by DW_OP_reg{N,x}, return
28398 copy of the DWARF expression with DW_OP_breg{N,x} <0> appended
28399 and no DEREF.
28400 If it is DWARF expression followed by DW_OP_stack_value, return
28401 copy of the DWARF expression without anything appended.
28402 Otherwise, return NULL. */
28404 static dw_loc_descr_ref
28405 copy_deref_exprloc (dw_loc_descr_ref expr, dw_loc_descr_ref deref)
28408 if (expr == NULL)
28409 return NULL;
28411 dw_loc_descr_ref l = non_dwarf_expression (expr);
28412 if (l && l->dw_loc_next)
28413 return NULL;
28415 if (l)
28417 if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
28418 deref = new_loc_descr ((enum dwarf_location_atom)
28419 (DW_OP_breg0 + (l->dw_loc_opc - DW_OP_reg0)),
28420 0, 0);
28421 else
28422 switch (l->dw_loc_opc)
28424 case DW_OP_regx:
28425 deref = new_loc_descr (DW_OP_bregx,
28426 l->dw_loc_oprnd1.v.val_unsigned, 0);
28427 break;
28428 case DW_OP_stack_value:
28429 deref = NULL;
28430 break;
28431 default:
28432 return NULL;
28435 else
28436 deref = new_loc_descr (deref->dw_loc_opc,
28437 deref->dw_loc_oprnd1.v.val_int, 0);
28439 dw_loc_descr_ref ret = NULL, *p = &ret;
28440 while (expr != l)
28442 *p = new_loc_descr (expr->dw_loc_opc, 0, 0);
28443 (*p)->dw_loc_oprnd1 = expr->dw_loc_oprnd1;
28444 (*p)->dw_loc_oprnd2 = expr->dw_loc_oprnd2;
28445 p = &(*p)->dw_loc_next;
28446 expr = expr->dw_loc_next;
28448 *p = deref;
28449 return ret;
28452 /* For DW_AT_string_length attribute with DW_OP_call4 reference to a variable
28453 or argument, adjust it if needed and return:
28454 -1 if the DW_AT_string_length attribute and DW_AT_{string_length_,}byte_size
28455 attribute if present should be removed
28456 0 keep the attribute as is if the referenced var or argument has
28457 only DWARF expression that covers all ranges
28458 1 if the attribute has been successfully adjusted. */
28460 static int
28461 optimize_string_length (dw_attr_node *a)
28463 dw_loc_descr_ref l = AT_loc (a), lv;
28464 dw_die_ref die = l->dw_loc_oprnd1.v.val_die_ref.die;
28465 dw_attr_node *av = get_AT (die, DW_AT_location);
28466 dw_loc_list_ref d;
28467 bool non_dwarf_expr = false;
28469 if (av == NULL)
28470 return -1;
28471 switch (AT_class (av))
28473 case dw_val_class_loc_list:
28474 for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
28475 if (d->expr && non_dwarf_expression (d->expr))
28476 non_dwarf_expr = true;
28477 break;
28478 case dw_val_class_loc:
28479 lv = AT_loc (av);
28480 if (lv == NULL)
28481 return -1;
28482 if (non_dwarf_expression (lv))
28483 non_dwarf_expr = true;
28484 break;
28485 default:
28486 return -1;
28489 /* If it is safe to keep DW_OP_call4 in, keep it. */
28490 if (!non_dwarf_expr
28491 && (l->dw_loc_next == NULL || AT_class (av) == dw_val_class_loc))
28492 return 0;
28494 /* If not dereferencing the DW_OP_call4 afterwards, we can just
28495 copy over the DW_AT_location attribute from die to a. */
28496 if (l->dw_loc_next == NULL)
28498 a->dw_attr_val = av->dw_attr_val;
28499 return 1;
28502 dw_loc_list_ref list, *p;
28503 switch (AT_class (av))
28505 case dw_val_class_loc_list:
28506 p = &list;
28507 list = NULL;
28508 for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
28510 lv = copy_deref_exprloc (d->expr, l->dw_loc_next);
28511 if (lv)
28513 *p = new_loc_list (lv, d->begin, d->end, d->section);
28514 p = &(*p)->dw_loc_next;
28517 if (list == NULL)
28518 return -1;
28519 a->dw_attr_val.val_class = dw_val_class_loc_list;
28520 gen_llsym (list);
28521 *AT_loc_list_ptr (a) = list;
28522 return 1;
28523 case dw_val_class_loc:
28524 lv = copy_deref_exprloc (AT_loc (av), l->dw_loc_next);
28525 if (lv == NULL)
28526 return -1;
28527 a->dw_attr_val.v.val_loc = lv;
28528 return 1;
28529 default:
28530 gcc_unreachable ();
28534 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
28535 an address in .rodata section if the string literal is emitted there,
28536 or remove the containing location list or replace DW_AT_const_value
28537 with DW_AT_location and empty location expression, if it isn't found
28538 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
28539 to something that has been emitted in the current CU. */
28541 static void
28542 resolve_addr (dw_die_ref die)
28544 dw_die_ref c;
28545 dw_attr_node *a;
28546 dw_loc_list_ref *curr, *start, loc;
28547 unsigned ix;
28548 bool remove_AT_byte_size = false;
28550 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
28551 switch (AT_class (a))
28553 case dw_val_class_loc_list:
28554 start = curr = AT_loc_list_ptr (a);
28555 loc = *curr;
28556 gcc_assert (loc);
28557 /* The same list can be referenced more than once. See if we have
28558 already recorded the result from a previous pass. */
28559 if (loc->replaced)
28560 *curr = loc->dw_loc_next;
28561 else if (!loc->resolved_addr)
28563 /* As things stand, we do not expect or allow one die to
28564 reference a suffix of another die's location list chain.
28565 References must be identical or completely separate.
28566 There is therefore no need to cache the result of this
28567 pass on any list other than the first; doing so
28568 would lead to unnecessary writes. */
28569 while (*curr)
28571 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
28572 if (!resolve_addr_in_expr ((*curr)->expr))
28574 dw_loc_list_ref next = (*curr)->dw_loc_next;
28575 dw_loc_descr_ref l = (*curr)->expr;
28577 if (next && (*curr)->ll_symbol)
28579 gcc_assert (!next->ll_symbol);
28580 next->ll_symbol = (*curr)->ll_symbol;
28582 if (dwarf_split_debug_info)
28583 remove_loc_list_addr_table_entries (l);
28584 *curr = next;
28586 else
28588 mark_base_types ((*curr)->expr);
28589 curr = &(*curr)->dw_loc_next;
28592 if (loc == *start)
28593 loc->resolved_addr = 1;
28594 else
28596 loc->replaced = 1;
28597 loc->dw_loc_next = *start;
28600 if (!*start)
28602 remove_AT (die, a->dw_attr);
28603 ix--;
28605 break;
28606 case dw_val_class_loc:
28608 dw_loc_descr_ref l = AT_loc (a);
28609 /* Using DW_OP_call4 or DW_OP_call4 DW_OP_deref in
28610 DW_AT_string_length is only a rough approximation; unfortunately
28611 DW_AT_string_length can't be a reference to a DIE. DW_OP_call4
28612 needs a DWARF expression, while DW_AT_location of the referenced
28613 variable or argument might be any location description. */
28614 if (a->dw_attr == DW_AT_string_length
28615 && l
28616 && l->dw_loc_opc == DW_OP_call4
28617 && l->dw_loc_oprnd1.val_class == dw_val_class_die_ref
28618 && (l->dw_loc_next == NULL
28619 || (l->dw_loc_next->dw_loc_next == NULL
28620 && (l->dw_loc_next->dw_loc_opc == DW_OP_deref
28621 || l->dw_loc_next->dw_loc_opc != DW_OP_deref_size))))
28623 switch (optimize_string_length (a))
28625 case -1:
28626 remove_AT (die, a->dw_attr);
28627 ix--;
28628 /* If we drop DW_AT_string_length, we need to drop also
28629 DW_AT_{string_length_,}byte_size. */
28630 remove_AT_byte_size = true;
28631 continue;
28632 default:
28633 break;
28634 case 1:
28635 /* Even if we keep the optimized DW_AT_string_length,
28636 it might have changed AT_class, so process it again. */
28637 ix--;
28638 continue;
28641 /* For -gdwarf-2 don't attempt to optimize
28642 DW_AT_data_member_location containing
28643 DW_OP_plus_uconst - older consumers might
28644 rely on it being that op instead of a more complex,
28645 but shorter, location description. */
28646 if ((dwarf_version > 2
28647 || a->dw_attr != DW_AT_data_member_location
28648 || l == NULL
28649 || l->dw_loc_opc != DW_OP_plus_uconst
28650 || l->dw_loc_next != NULL)
28651 && !resolve_addr_in_expr (l))
28653 if (dwarf_split_debug_info)
28654 remove_loc_list_addr_table_entries (l);
28655 if (l != NULL
28656 && l->dw_loc_next == NULL
28657 && l->dw_loc_opc == DW_OP_addr
28658 && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
28659 && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
28660 && a->dw_attr == DW_AT_location)
28662 tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
28663 remove_AT (die, a->dw_attr);
28664 ix--;
28665 optimize_location_into_implicit_ptr (die, decl);
28666 break;
28668 remove_AT (die, a->dw_attr);
28669 ix--;
28671 else
28672 mark_base_types (l);
28674 break;
28675 case dw_val_class_addr:
28676 if (a->dw_attr == DW_AT_const_value
28677 && !resolve_one_addr (&a->dw_attr_val.v.val_addr))
28679 if (AT_index (a) != NOT_INDEXED)
28680 remove_addr_table_entry (a->dw_attr_val.val_entry);
28681 remove_AT (die, a->dw_attr);
28682 ix--;
28684 if ((die->die_tag == DW_TAG_call_site
28685 && a->dw_attr == DW_AT_call_origin)
28686 || (die->die_tag == DW_TAG_GNU_call_site
28687 && a->dw_attr == DW_AT_abstract_origin))
28689 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
28690 dw_die_ref tdie = lookup_decl_die (tdecl);
28691 dw_die_ref cdie;
28692 if (tdie == NULL
28693 && DECL_EXTERNAL (tdecl)
28694 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE
28695 && (cdie = lookup_context_die (DECL_CONTEXT (tdecl))))
28697 /* Creating a full DIE for tdecl is overly expensive and
28698 at this point even wrong when in the LTO phase
28699 as it can end up generating new type DIEs we didn't
28700 output and thus optimize_external_refs will crash. */
28701 tdie = new_die (DW_TAG_subprogram, cdie, NULL_TREE);
28702 add_AT_flag (tdie, DW_AT_external, 1);
28703 add_AT_flag (tdie, DW_AT_declaration, 1);
28704 add_linkage_attr (tdie, tdecl);
28705 add_name_and_src_coords_attributes (tdie, tdecl);
28706 equate_decl_number_to_die (tdecl, tdie);
28708 if (tdie)
28710 a->dw_attr_val.val_class = dw_val_class_die_ref;
28711 a->dw_attr_val.v.val_die_ref.die = tdie;
28712 a->dw_attr_val.v.val_die_ref.external = 0;
28714 else
28716 if (AT_index (a) != NOT_INDEXED)
28717 remove_addr_table_entry (a->dw_attr_val.val_entry);
28718 remove_AT (die, a->dw_attr);
28719 ix--;
28722 break;
28723 default:
28724 break;
28727 if (remove_AT_byte_size)
28728 remove_AT (die, dwarf_version >= 5
28729 ? DW_AT_string_length_byte_size
28730 : DW_AT_byte_size);
28732 FOR_EACH_CHILD (die, c, resolve_addr (c));
28735 /* Helper routines for optimize_location_lists.
28736 This pass tries to share identical local lists in .debug_loc
28737 section. */
28739 /* Iteratively hash operands of LOC opcode into HSTATE. */
28741 static void
28742 hash_loc_operands (dw_loc_descr_ref loc, inchash::hash &hstate)
28744 dw_val_ref val1 = &loc->dw_loc_oprnd1;
28745 dw_val_ref val2 = &loc->dw_loc_oprnd2;
28747 switch (loc->dw_loc_opc)
28749 case DW_OP_const4u:
28750 case DW_OP_const8u:
28751 if (loc->dtprel)
28752 goto hash_addr;
28753 /* FALLTHRU */
28754 case DW_OP_const1u:
28755 case DW_OP_const1s:
28756 case DW_OP_const2u:
28757 case DW_OP_const2s:
28758 case DW_OP_const4s:
28759 case DW_OP_const8s:
28760 case DW_OP_constu:
28761 case DW_OP_consts:
28762 case DW_OP_pick:
28763 case DW_OP_plus_uconst:
28764 case DW_OP_breg0:
28765 case DW_OP_breg1:
28766 case DW_OP_breg2:
28767 case DW_OP_breg3:
28768 case DW_OP_breg4:
28769 case DW_OP_breg5:
28770 case DW_OP_breg6:
28771 case DW_OP_breg7:
28772 case DW_OP_breg8:
28773 case DW_OP_breg9:
28774 case DW_OP_breg10:
28775 case DW_OP_breg11:
28776 case DW_OP_breg12:
28777 case DW_OP_breg13:
28778 case DW_OP_breg14:
28779 case DW_OP_breg15:
28780 case DW_OP_breg16:
28781 case DW_OP_breg17:
28782 case DW_OP_breg18:
28783 case DW_OP_breg19:
28784 case DW_OP_breg20:
28785 case DW_OP_breg21:
28786 case DW_OP_breg22:
28787 case DW_OP_breg23:
28788 case DW_OP_breg24:
28789 case DW_OP_breg25:
28790 case DW_OP_breg26:
28791 case DW_OP_breg27:
28792 case DW_OP_breg28:
28793 case DW_OP_breg29:
28794 case DW_OP_breg30:
28795 case DW_OP_breg31:
28796 case DW_OP_regx:
28797 case DW_OP_fbreg:
28798 case DW_OP_piece:
28799 case DW_OP_deref_size:
28800 case DW_OP_xderef_size:
28801 hstate.add_object (val1->v.val_int);
28802 break;
28803 case DW_OP_skip:
28804 case DW_OP_bra:
28806 int offset;
28808 gcc_assert (val1->val_class == dw_val_class_loc);
28809 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
28810 hstate.add_object (offset);
28812 break;
28813 case DW_OP_implicit_value:
28814 hstate.add_object (val1->v.val_unsigned);
28815 switch (val2->val_class)
28817 case dw_val_class_const:
28818 hstate.add_object (val2->v.val_int);
28819 break;
28820 case dw_val_class_vec:
28822 unsigned int elt_size = val2->v.val_vec.elt_size;
28823 unsigned int len = val2->v.val_vec.length;
28825 hstate.add_int (elt_size);
28826 hstate.add_int (len);
28827 hstate.add (val2->v.val_vec.array, len * elt_size);
28829 break;
28830 case dw_val_class_const_double:
28831 hstate.add_object (val2->v.val_double.low);
28832 hstate.add_object (val2->v.val_double.high);
28833 break;
28834 case dw_val_class_wide_int:
28835 hstate.add (val2->v.val_wide->get_val (),
28836 get_full_len (*val2->v.val_wide)
28837 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
28838 break;
28839 case dw_val_class_addr:
28840 inchash::add_rtx (val2->v.val_addr, hstate);
28841 break;
28842 default:
28843 gcc_unreachable ();
28845 break;
28846 case DW_OP_bregx:
28847 case DW_OP_bit_piece:
28848 hstate.add_object (val1->v.val_int);
28849 hstate.add_object (val2->v.val_int);
28850 break;
28851 case DW_OP_addr:
28852 hash_addr:
28853 if (loc->dtprel)
28855 unsigned char dtprel = 0xd1;
28856 hstate.add_object (dtprel);
28858 inchash::add_rtx (val1->v.val_addr, hstate);
28859 break;
28860 case DW_OP_GNU_addr_index:
28861 case DW_OP_GNU_const_index:
28863 if (loc->dtprel)
28865 unsigned char dtprel = 0xd1;
28866 hstate.add_object (dtprel);
28868 inchash::add_rtx (val1->val_entry->addr.rtl, hstate);
28870 break;
28871 case DW_OP_implicit_pointer:
28872 case DW_OP_GNU_implicit_pointer:
28873 hstate.add_int (val2->v.val_int);
28874 break;
28875 case DW_OP_entry_value:
28876 case DW_OP_GNU_entry_value:
28877 hstate.add_object (val1->v.val_loc);
28878 break;
28879 case DW_OP_regval_type:
28880 case DW_OP_deref_type:
28881 case DW_OP_GNU_regval_type:
28882 case DW_OP_GNU_deref_type:
28884 unsigned int byte_size
28885 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
28886 unsigned int encoding
28887 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
28888 hstate.add_object (val1->v.val_int);
28889 hstate.add_object (byte_size);
28890 hstate.add_object (encoding);
28892 break;
28893 case DW_OP_convert:
28894 case DW_OP_reinterpret:
28895 case DW_OP_GNU_convert:
28896 case DW_OP_GNU_reinterpret:
28897 if (val1->val_class == dw_val_class_unsigned_const)
28899 hstate.add_object (val1->v.val_unsigned);
28900 break;
28902 /* FALLTHRU */
28903 case DW_OP_const_type:
28904 case DW_OP_GNU_const_type:
28906 unsigned int byte_size
28907 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
28908 unsigned int encoding
28909 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
28910 hstate.add_object (byte_size);
28911 hstate.add_object (encoding);
28912 if (loc->dw_loc_opc != DW_OP_const_type
28913 && loc->dw_loc_opc != DW_OP_GNU_const_type)
28914 break;
28915 hstate.add_object (val2->val_class);
28916 switch (val2->val_class)
28918 case dw_val_class_const:
28919 hstate.add_object (val2->v.val_int);
28920 break;
28921 case dw_val_class_vec:
28923 unsigned int elt_size = val2->v.val_vec.elt_size;
28924 unsigned int len = val2->v.val_vec.length;
28926 hstate.add_object (elt_size);
28927 hstate.add_object (len);
28928 hstate.add (val2->v.val_vec.array, len * elt_size);
28930 break;
28931 case dw_val_class_const_double:
28932 hstate.add_object (val2->v.val_double.low);
28933 hstate.add_object (val2->v.val_double.high);
28934 break;
28935 case dw_val_class_wide_int:
28936 hstate.add (val2->v.val_wide->get_val (),
28937 get_full_len (*val2->v.val_wide)
28938 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
28939 break;
28940 default:
28941 gcc_unreachable ();
28944 break;
28946 default:
28947 /* Other codes have no operands. */
28948 break;
28952 /* Iteratively hash the whole DWARF location expression LOC into HSTATE. */
28954 static inline void
28955 hash_locs (dw_loc_descr_ref loc, inchash::hash &hstate)
28957 dw_loc_descr_ref l;
28958 bool sizes_computed = false;
28959 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
28960 size_of_locs (loc);
28962 for (l = loc; l != NULL; l = l->dw_loc_next)
28964 enum dwarf_location_atom opc = l->dw_loc_opc;
28965 hstate.add_object (opc);
28966 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
28968 size_of_locs (loc);
28969 sizes_computed = true;
28971 hash_loc_operands (l, hstate);
28975 /* Compute hash of the whole location list LIST_HEAD. */
28977 static inline void
28978 hash_loc_list (dw_loc_list_ref list_head)
28980 dw_loc_list_ref curr = list_head;
28981 inchash::hash hstate;
28983 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
28985 hstate.add (curr->begin, strlen (curr->begin) + 1);
28986 hstate.add (curr->end, strlen (curr->end) + 1);
28987 if (curr->section)
28988 hstate.add (curr->section, strlen (curr->section) + 1);
28989 hash_locs (curr->expr, hstate);
28991 list_head->hash = hstate.end ();
28994 /* Return true if X and Y opcodes have the same operands. */
28996 static inline bool
28997 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
28999 dw_val_ref valx1 = &x->dw_loc_oprnd1;
29000 dw_val_ref valx2 = &x->dw_loc_oprnd2;
29001 dw_val_ref valy1 = &y->dw_loc_oprnd1;
29002 dw_val_ref valy2 = &y->dw_loc_oprnd2;
29004 switch (x->dw_loc_opc)
29006 case DW_OP_const4u:
29007 case DW_OP_const8u:
29008 if (x->dtprel)
29009 goto hash_addr;
29010 /* FALLTHRU */
29011 case DW_OP_const1u:
29012 case DW_OP_const1s:
29013 case DW_OP_const2u:
29014 case DW_OP_const2s:
29015 case DW_OP_const4s:
29016 case DW_OP_const8s:
29017 case DW_OP_constu:
29018 case DW_OP_consts:
29019 case DW_OP_pick:
29020 case DW_OP_plus_uconst:
29021 case DW_OP_breg0:
29022 case DW_OP_breg1:
29023 case DW_OP_breg2:
29024 case DW_OP_breg3:
29025 case DW_OP_breg4:
29026 case DW_OP_breg5:
29027 case DW_OP_breg6:
29028 case DW_OP_breg7:
29029 case DW_OP_breg8:
29030 case DW_OP_breg9:
29031 case DW_OP_breg10:
29032 case DW_OP_breg11:
29033 case DW_OP_breg12:
29034 case DW_OP_breg13:
29035 case DW_OP_breg14:
29036 case DW_OP_breg15:
29037 case DW_OP_breg16:
29038 case DW_OP_breg17:
29039 case DW_OP_breg18:
29040 case DW_OP_breg19:
29041 case DW_OP_breg20:
29042 case DW_OP_breg21:
29043 case DW_OP_breg22:
29044 case DW_OP_breg23:
29045 case DW_OP_breg24:
29046 case DW_OP_breg25:
29047 case DW_OP_breg26:
29048 case DW_OP_breg27:
29049 case DW_OP_breg28:
29050 case DW_OP_breg29:
29051 case DW_OP_breg30:
29052 case DW_OP_breg31:
29053 case DW_OP_regx:
29054 case DW_OP_fbreg:
29055 case DW_OP_piece:
29056 case DW_OP_deref_size:
29057 case DW_OP_xderef_size:
29058 return valx1->v.val_int == valy1->v.val_int;
29059 case DW_OP_skip:
29060 case DW_OP_bra:
29061 /* If splitting debug info, the use of DW_OP_GNU_addr_index
29062 can cause irrelevant differences in dw_loc_addr. */
29063 gcc_assert (valx1->val_class == dw_val_class_loc
29064 && valy1->val_class == dw_val_class_loc
29065 && (dwarf_split_debug_info
29066 || x->dw_loc_addr == y->dw_loc_addr));
29067 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
29068 case DW_OP_implicit_value:
29069 if (valx1->v.val_unsigned != valy1->v.val_unsigned
29070 || valx2->val_class != valy2->val_class)
29071 return false;
29072 switch (valx2->val_class)
29074 case dw_val_class_const:
29075 return valx2->v.val_int == valy2->v.val_int;
29076 case dw_val_class_vec:
29077 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
29078 && valx2->v.val_vec.length == valy2->v.val_vec.length
29079 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
29080 valx2->v.val_vec.elt_size
29081 * valx2->v.val_vec.length) == 0;
29082 case dw_val_class_const_double:
29083 return valx2->v.val_double.low == valy2->v.val_double.low
29084 && valx2->v.val_double.high == valy2->v.val_double.high;
29085 case dw_val_class_wide_int:
29086 return *valx2->v.val_wide == *valy2->v.val_wide;
29087 case dw_val_class_addr:
29088 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
29089 default:
29090 gcc_unreachable ();
29092 case DW_OP_bregx:
29093 case DW_OP_bit_piece:
29094 return valx1->v.val_int == valy1->v.val_int
29095 && valx2->v.val_int == valy2->v.val_int;
29096 case DW_OP_addr:
29097 hash_addr:
29098 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
29099 case DW_OP_GNU_addr_index:
29100 case DW_OP_GNU_const_index:
29102 rtx ax1 = valx1->val_entry->addr.rtl;
29103 rtx ay1 = valy1->val_entry->addr.rtl;
29104 return rtx_equal_p (ax1, ay1);
29106 case DW_OP_implicit_pointer:
29107 case DW_OP_GNU_implicit_pointer:
29108 return valx1->val_class == dw_val_class_die_ref
29109 && valx1->val_class == valy1->val_class
29110 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
29111 && valx2->v.val_int == valy2->v.val_int;
29112 case DW_OP_entry_value:
29113 case DW_OP_GNU_entry_value:
29114 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
29115 case DW_OP_const_type:
29116 case DW_OP_GNU_const_type:
29117 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
29118 || valx2->val_class != valy2->val_class)
29119 return false;
29120 switch (valx2->val_class)
29122 case dw_val_class_const:
29123 return valx2->v.val_int == valy2->v.val_int;
29124 case dw_val_class_vec:
29125 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
29126 && valx2->v.val_vec.length == valy2->v.val_vec.length
29127 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
29128 valx2->v.val_vec.elt_size
29129 * valx2->v.val_vec.length) == 0;
29130 case dw_val_class_const_double:
29131 return valx2->v.val_double.low == valy2->v.val_double.low
29132 && valx2->v.val_double.high == valy2->v.val_double.high;
29133 case dw_val_class_wide_int:
29134 return *valx2->v.val_wide == *valy2->v.val_wide;
29135 default:
29136 gcc_unreachable ();
29138 case DW_OP_regval_type:
29139 case DW_OP_deref_type:
29140 case DW_OP_GNU_regval_type:
29141 case DW_OP_GNU_deref_type:
29142 return valx1->v.val_int == valy1->v.val_int
29143 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
29144 case DW_OP_convert:
29145 case DW_OP_reinterpret:
29146 case DW_OP_GNU_convert:
29147 case DW_OP_GNU_reinterpret:
29148 if (valx1->val_class != valy1->val_class)
29149 return false;
29150 if (valx1->val_class == dw_val_class_unsigned_const)
29151 return valx1->v.val_unsigned == valy1->v.val_unsigned;
29152 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
29153 case DW_OP_GNU_parameter_ref:
29154 return valx1->val_class == dw_val_class_die_ref
29155 && valx1->val_class == valy1->val_class
29156 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
29157 default:
29158 /* Other codes have no operands. */
29159 return true;
29163 /* Return true if DWARF location expressions X and Y are the same. */
29165 static inline bool
29166 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
29168 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
29169 if (x->dw_loc_opc != y->dw_loc_opc
29170 || x->dtprel != y->dtprel
29171 || !compare_loc_operands (x, y))
29172 break;
29173 return x == NULL && y == NULL;
29176 /* Hashtable helpers. */
29178 struct loc_list_hasher : nofree_ptr_hash <dw_loc_list_struct>
29180 static inline hashval_t hash (const dw_loc_list_struct *);
29181 static inline bool equal (const dw_loc_list_struct *,
29182 const dw_loc_list_struct *);
29185 /* Return precomputed hash of location list X. */
29187 inline hashval_t
29188 loc_list_hasher::hash (const dw_loc_list_struct *x)
29190 return x->hash;
29193 /* Return true if location lists A and B are the same. */
29195 inline bool
29196 loc_list_hasher::equal (const dw_loc_list_struct *a,
29197 const dw_loc_list_struct *b)
29199 if (a == b)
29200 return 1;
29201 if (a->hash != b->hash)
29202 return 0;
29203 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
29204 if (strcmp (a->begin, b->begin) != 0
29205 || strcmp (a->end, b->end) != 0
29206 || (a->section == NULL) != (b->section == NULL)
29207 || (a->section && strcmp (a->section, b->section) != 0)
29208 || !compare_locs (a->expr, b->expr))
29209 break;
29210 return a == NULL && b == NULL;
29213 typedef hash_table<loc_list_hasher> loc_list_hash_type;
29216 /* Recursively optimize location lists referenced from DIE
29217 children and share them whenever possible. */
29219 static void
29220 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type *htab)
29222 dw_die_ref c;
29223 dw_attr_node *a;
29224 unsigned ix;
29225 dw_loc_list_struct **slot;
29227 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
29228 if (AT_class (a) == dw_val_class_loc_list)
29230 dw_loc_list_ref list = AT_loc_list (a);
29231 /* TODO: perform some optimizations here, before hashing
29232 it and storing into the hash table. */
29233 hash_loc_list (list);
29234 slot = htab->find_slot_with_hash (list, list->hash, INSERT);
29235 if (*slot == NULL)
29236 *slot = list;
29237 else
29238 a->dw_attr_val.v.val_loc_list = *slot;
29241 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
29245 /* Recursively assign each location list a unique index into the debug_addr
29246 section. */
29248 static void
29249 index_location_lists (dw_die_ref die)
29251 dw_die_ref c;
29252 dw_attr_node *a;
29253 unsigned ix;
29255 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
29256 if (AT_class (a) == dw_val_class_loc_list)
29258 dw_loc_list_ref list = AT_loc_list (a);
29259 dw_loc_list_ref curr;
29260 for (curr = list; curr != NULL; curr = curr->dw_loc_next)
29262 /* Don't index an entry that has already been indexed
29263 or won't be output. */
29264 if (curr->begin_entry != NULL
29265 || (strcmp (curr->begin, curr->end) == 0 && !curr->force))
29266 continue;
29268 curr->begin_entry
29269 = add_addr_table_entry (xstrdup (curr->begin), ate_kind_label);
29273 FOR_EACH_CHILD (die, c, index_location_lists (c));
29276 /* Optimize location lists referenced from DIE
29277 children and share them whenever possible. */
29279 static void
29280 optimize_location_lists (dw_die_ref die)
29282 loc_list_hash_type htab (500);
29283 optimize_location_lists_1 (die, &htab);
29286 /* Traverse the limbo die list, and add parent/child links. The only
29287 dies without parents that should be here are concrete instances of
29288 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
29289 For concrete instances, we can get the parent die from the abstract
29290 instance. */
29292 static void
29293 flush_limbo_die_list (void)
29295 limbo_die_node *node;
29297 /* get_context_die calls force_decl_die, which can put new DIEs on the
29298 limbo list in LTO mode when nested functions are put in a different
29299 partition than that of their parent function. */
29300 while ((node = limbo_die_list))
29302 dw_die_ref die = node->die;
29303 limbo_die_list = node->next;
29305 if (die->die_parent == NULL)
29307 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
29309 if (origin && origin->die_parent)
29310 add_child_die (origin->die_parent, die);
29311 else if (is_cu_die (die))
29313 else if (seen_error ())
29314 /* It's OK to be confused by errors in the input. */
29315 add_child_die (comp_unit_die (), die);
29316 else
29318 /* In certain situations, the lexical block containing a
29319 nested function can be optimized away, which results
29320 in the nested function die being orphaned. Likewise
29321 with the return type of that nested function. Force
29322 this to be a child of the containing function.
29324 It may happen that even the containing function got fully
29325 inlined and optimized out. In that case we are lost and
29326 assign the empty child. This should not be big issue as
29327 the function is likely unreachable too. */
29328 gcc_assert (node->created_for);
29330 if (DECL_P (node->created_for))
29331 origin = get_context_die (DECL_CONTEXT (node->created_for));
29332 else if (TYPE_P (node->created_for))
29333 origin = scope_die_for (node->created_for, comp_unit_die ());
29334 else
29335 origin = comp_unit_die ();
29337 add_child_die (origin, die);
29343 /* Output stuff that dwarf requires at the end of every file,
29344 and generate the DWARF-2 debugging info. */
29346 static void
29347 dwarf2out_finish (const char *)
29349 comdat_type_node *ctnode;
29350 dw_die_ref main_comp_unit_die;
29351 unsigned char checksum[16];
29353 /* Flush out any latecomers to the limbo party. */
29354 flush_limbo_die_list ();
29356 if (flag_checking)
29358 verify_die (comp_unit_die ());
29359 for (limbo_die_node *node = cu_die_list; node; node = node->next)
29360 verify_die (node->die);
29363 /* We shouldn't have any symbols with delayed asm names for
29364 DIEs generated after early finish. */
29365 gcc_assert (deferred_asm_name == NULL);
29367 gen_remaining_tmpl_value_param_die_attribute ();
29369 #if ENABLE_ASSERT_CHECKING
29371 dw_die_ref die = comp_unit_die (), c;
29372 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
29374 #endif
29375 resolve_addr (comp_unit_die ());
29376 move_marked_base_types ();
29378 /* Initialize sections and labels used for actual assembler output. */
29379 init_sections_and_labels ();
29381 /* Traverse the DIE's and add sibling attributes to those DIE's that
29382 have children. */
29383 add_sibling_attributes (comp_unit_die ());
29384 limbo_die_node *node;
29385 for (node = cu_die_list; node; node = node->next)
29386 add_sibling_attributes (node->die);
29387 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
29388 add_sibling_attributes (ctnode->root_die);
29390 /* When splitting DWARF info, we put some attributes in the
29391 skeleton compile_unit DIE that remains in the .o, while
29392 most attributes go in the DWO compile_unit_die. */
29393 if (dwarf_split_debug_info)
29395 limbo_die_node *cu;
29396 main_comp_unit_die = gen_compile_unit_die (NULL);
29397 if (dwarf_version >= 5)
29398 main_comp_unit_die->die_tag = DW_TAG_skeleton_unit;
29399 cu = limbo_die_list;
29400 gcc_assert (cu->die == main_comp_unit_die);
29401 limbo_die_list = limbo_die_list->next;
29402 cu->next = cu_die_list;
29403 cu_die_list = cu;
29405 else
29406 main_comp_unit_die = comp_unit_die ();
29408 /* Output a terminator label for the .text section. */
29409 switch_to_section (text_section);
29410 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
29411 if (cold_text_section)
29413 switch_to_section (cold_text_section);
29414 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
29417 /* We can only use the low/high_pc attributes if all of the code was
29418 in .text. */
29419 if (!have_multiple_function_sections
29420 || (dwarf_version < 3 && dwarf_strict))
29422 /* Don't add if the CU has no associated code. */
29423 if (text_section_used)
29424 add_AT_low_high_pc (main_comp_unit_die, text_section_label,
29425 text_end_label, true);
29427 else
29429 unsigned fde_idx;
29430 dw_fde_ref fde;
29431 bool range_list_added = false;
29433 if (text_section_used)
29434 add_ranges_by_labels (main_comp_unit_die, text_section_label,
29435 text_end_label, &range_list_added, true);
29436 if (cold_text_section_used)
29437 add_ranges_by_labels (main_comp_unit_die, cold_text_section_label,
29438 cold_end_label, &range_list_added, true);
29440 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
29442 if (DECL_IGNORED_P (fde->decl))
29443 continue;
29444 if (!fde->in_std_section)
29445 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
29446 fde->dw_fde_end, &range_list_added,
29447 true);
29448 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
29449 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
29450 fde->dw_fde_second_end, &range_list_added,
29451 true);
29454 if (range_list_added)
29456 /* We need to give .debug_loc and .debug_ranges an appropriate
29457 "base address". Use zero so that these addresses become
29458 absolute. Historically, we've emitted the unexpected
29459 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
29460 Emit both to give time for other tools to adapt. */
29461 add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
29462 if (! dwarf_strict && dwarf_version < 4)
29463 add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
29465 add_ranges (NULL);
29469 if (debug_info_level >= DINFO_LEVEL_TERSE)
29470 add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
29471 debug_line_section_label);
29473 if (have_macinfo)
29474 add_AT_macptr (comp_unit_die (),
29475 dwarf_version >= 5 ? DW_AT_macros
29476 : dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros,
29477 macinfo_section_label);
29479 if (dwarf_split_debug_info)
29481 if (have_location_lists)
29483 if (dwarf_version >= 5)
29484 add_AT_loclistsptr (comp_unit_die (), DW_AT_loclists_base,
29485 loc_section_label);
29486 /* optimize_location_lists calculates the size of the lists,
29487 so index them first, and assign indices to the entries.
29488 Although optimize_location_lists will remove entries from
29489 the table, it only does so for duplicates, and therefore
29490 only reduces ref_counts to 1. */
29491 index_location_lists (comp_unit_die ());
29494 if (addr_index_table != NULL)
29496 unsigned int index = 0;
29497 addr_index_table
29498 ->traverse_noresize<unsigned int *, index_addr_table_entry>
29499 (&index);
29503 loc_list_idx = 0;
29504 if (have_location_lists)
29506 optimize_location_lists (comp_unit_die ());
29507 /* And finally assign indexes to the entries for -gsplit-dwarf. */
29508 if (dwarf_version >= 5 && dwarf_split_debug_info)
29509 assign_location_list_indexes (comp_unit_die ());
29512 save_macinfo_strings ();
29514 if (dwarf_split_debug_info)
29516 unsigned int index = 0;
29518 /* Add attributes common to skeleton compile_units and
29519 type_units. Because these attributes include strings, it
29520 must be done before freezing the string table. Top-level
29521 skeleton die attrs are added when the skeleton type unit is
29522 created, so ensure it is created by this point. */
29523 add_top_level_skeleton_die_attrs (main_comp_unit_die);
29524 debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
29527 /* Output all of the compilation units. We put the main one last so that
29528 the offsets are available to output_pubnames. */
29529 for (node = cu_die_list; node; node = node->next)
29530 output_comp_unit (node->die, 0, NULL);
29532 hash_table<comdat_type_hasher> comdat_type_table (100);
29533 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
29535 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
29537 /* Don't output duplicate types. */
29538 if (*slot != HTAB_EMPTY_ENTRY)
29539 continue;
29541 /* Add a pointer to the line table for the main compilation unit
29542 so that the debugger can make sense of DW_AT_decl_file
29543 attributes. */
29544 if (debug_info_level >= DINFO_LEVEL_TERSE)
29545 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
29546 (!dwarf_split_debug_info
29547 ? debug_line_section_label
29548 : debug_skeleton_line_section_label));
29550 output_comdat_type_unit (ctnode);
29551 *slot = ctnode;
29554 /* The AT_pubnames attribute needs to go in all skeleton dies, including
29555 both the main_cu and all skeleton TUs. Making this call unconditional
29556 would end up either adding a second copy of the AT_pubnames attribute, or
29557 requiring a special case in add_top_level_skeleton_die_attrs. */
29558 if (!dwarf_split_debug_info)
29559 add_AT_pubnames (comp_unit_die ());
29561 if (dwarf_split_debug_info)
29563 int mark;
29564 struct md5_ctx ctx;
29566 if (dwarf_version >= 5 && !vec_safe_is_empty (ranges_table))
29567 index_rnglists ();
29569 /* Compute a checksum of the comp_unit to use as the dwo_id. */
29570 md5_init_ctx (&ctx);
29571 mark = 0;
29572 die_checksum (comp_unit_die (), &ctx, &mark);
29573 unmark_all_dies (comp_unit_die ());
29574 md5_finish_ctx (&ctx, checksum);
29576 if (dwarf_version < 5)
29578 /* Use the first 8 bytes of the checksum as the dwo_id,
29579 and add it to both comp-unit DIEs. */
29580 add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
29581 add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
29584 /* Add the base offset of the ranges table to the skeleton
29585 comp-unit DIE. */
29586 if (!vec_safe_is_empty (ranges_table))
29588 if (dwarf_version >= 5)
29589 add_AT_lineptr (main_comp_unit_die, DW_AT_rnglists_base,
29590 ranges_base_label);
29591 else
29592 add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
29593 ranges_section_label);
29596 switch_to_section (debug_addr_section);
29597 ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
29598 output_addr_table ();
29601 /* Output the main compilation unit if non-empty or if .debug_macinfo
29602 or .debug_macro will be emitted. */
29603 output_comp_unit (comp_unit_die (), have_macinfo,
29604 dwarf_split_debug_info ? checksum : NULL);
29606 if (dwarf_split_debug_info && info_section_emitted)
29607 output_skeleton_debug_sections (main_comp_unit_die, checksum);
29609 /* Output the abbreviation table. */
29610 if (vec_safe_length (abbrev_die_table) != 1)
29612 switch_to_section (debug_abbrev_section);
29613 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
29614 output_abbrev_section ();
29617 /* Output location list section if necessary. */
29618 if (have_location_lists)
29620 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
29621 char l2[MAX_ARTIFICIAL_LABEL_BYTES];
29622 /* Output the location lists info. */
29623 switch_to_section (debug_loc_section);
29624 if (dwarf_version >= 5)
29626 ASM_GENERATE_INTERNAL_LABEL (l1, DEBUG_LOC_SECTION_LABEL, 1);
29627 ASM_GENERATE_INTERNAL_LABEL (l2, DEBUG_LOC_SECTION_LABEL, 2);
29628 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
29629 dw2_asm_output_data (4, 0xffffffff,
29630 "Initial length escape value indicating "
29631 "64-bit DWARF extension");
29632 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
29633 "Length of Location Lists");
29634 ASM_OUTPUT_LABEL (asm_out_file, l1);
29635 dw2_asm_output_data (2, dwarf_version, "DWARF Version");
29636 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
29637 dw2_asm_output_data (1, 0, "Segment Size");
29638 dw2_asm_output_data (4, dwarf_split_debug_info ? loc_list_idx : 0,
29639 "Offset Entry Count");
29641 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
29642 if (dwarf_version >= 5 && dwarf_split_debug_info)
29644 unsigned int save_loc_list_idx = loc_list_idx;
29645 loc_list_idx = 0;
29646 output_loclists_offsets (comp_unit_die ());
29647 gcc_assert (save_loc_list_idx == loc_list_idx);
29649 output_location_lists (comp_unit_die ());
29650 if (dwarf_version >= 5)
29651 ASM_OUTPUT_LABEL (asm_out_file, l2);
29654 output_pubtables ();
29656 /* Output the address range information if a CU (.debug_info section)
29657 was emitted. We output an empty table even if we had no functions
29658 to put in it. This because the consumer has no way to tell the
29659 difference between an empty table that we omitted and failure to
29660 generate a table that would have contained data. */
29661 if (info_section_emitted)
29663 switch_to_section (debug_aranges_section);
29664 output_aranges ();
29667 /* Output ranges section if necessary. */
29668 if (!vec_safe_is_empty (ranges_table))
29670 if (dwarf_version >= 5)
29671 output_rnglists ();
29672 else
29673 output_ranges ();
29676 /* Have to end the macro section. */
29677 if (have_macinfo)
29679 switch_to_section (debug_macinfo_section);
29680 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
29681 output_macinfo ();
29682 dw2_asm_output_data (1, 0, "End compilation unit");
29685 /* Output the source line correspondence table. We must do this
29686 even if there is no line information. Otherwise, on an empty
29687 translation unit, we will generate a present, but empty,
29688 .debug_info section. IRIX 6.5 `nm' will then complain when
29689 examining the file. This is done late so that any filenames
29690 used by the debug_info section are marked as 'used'. */
29691 switch_to_section (debug_line_section);
29692 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
29693 if (! DWARF2_ASM_LINE_DEBUG_INFO)
29694 output_line_info (false);
29696 if (dwarf_split_debug_info && info_section_emitted)
29698 switch_to_section (debug_skeleton_line_section);
29699 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
29700 output_line_info (true);
29703 /* If we emitted any indirect strings, output the string table too. */
29704 if (debug_str_hash || skeleton_debug_str_hash)
29705 output_indirect_strings ();
29706 if (debug_line_str_hash)
29708 switch_to_section (debug_line_str_section);
29709 const enum dwarf_form form = DW_FORM_line_strp;
29710 debug_line_str_hash->traverse<enum dwarf_form,
29711 output_indirect_string> (form);
29715 /* Perform any cleanups needed after the early debug generation pass
29716 has run. */
29718 static void
29719 dwarf2out_early_finish (const char *filename)
29721 set_early_dwarf s;
29723 /* PCH might result in DW_AT_producer string being restored from the
29724 header compilation, so always fill it with empty string initially
29725 and overwrite only here. */
29726 dw_attr_node *producer = get_AT (comp_unit_die (), DW_AT_producer);
29727 producer_string = gen_producer_string ();
29728 producer->dw_attr_val.v.val_str->refcount--;
29729 producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
29731 /* Add the name for the main input file now. We delayed this from
29732 dwarf2out_init to avoid complications with PCH. */
29733 add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
29734 add_comp_dir_attribute (comp_unit_die ());
29736 /* When emitting DWARF5 .debug_line_str, move DW_AT_name and
29737 DW_AT_comp_dir into .debug_line_str section. */
29738 if (!DWARF2_ASM_LINE_DEBUG_INFO
29739 && dwarf_version >= 5
29740 && DWARF5_USE_DEBUG_LINE_STR)
29742 for (int i = 0; i < 2; i++)
29744 dw_attr_node *a = get_AT (comp_unit_die (),
29745 i ? DW_AT_comp_dir : DW_AT_name);
29746 if (a == NULL
29747 || AT_class (a) != dw_val_class_str
29748 || strlen (AT_string (a)) + 1 <= DWARF_OFFSET_SIZE)
29749 continue;
29751 if (! debug_line_str_hash)
29752 debug_line_str_hash
29753 = hash_table<indirect_string_hasher>::create_ggc (10);
29755 struct indirect_string_node *node
29756 = find_AT_string_in_table (AT_string (a), debug_line_str_hash);
29757 set_indirect_string (node);
29758 node->form = DW_FORM_line_strp;
29759 a->dw_attr_val.v.val_str->refcount--;
29760 a->dw_attr_val.v.val_str = node;
29764 /* With LTO early dwarf was really finished at compile-time, so make
29765 sure to adjust the phase after annotating the LTRANS CU DIE. */
29766 if (in_lto_p)
29768 early_dwarf_finished = true;
29769 return;
29772 /* Walk through the list of incomplete types again, trying once more to
29773 emit full debugging info for them. */
29774 retry_incomplete_types ();
29776 /* The point here is to flush out the limbo list so that it is empty
29777 and we don't need to stream it for LTO. */
29778 flush_limbo_die_list ();
29780 gen_scheduled_generic_parms_dies ();
29781 gen_remaining_tmpl_value_param_die_attribute ();
29783 /* Add DW_AT_linkage_name for all deferred DIEs. */
29784 for (limbo_die_node *node = deferred_asm_name; node; node = node->next)
29786 tree decl = node->created_for;
29787 if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
29788 /* A missing DECL_ASSEMBLER_NAME can be a constant DIE that
29789 ended up in deferred_asm_name before we knew it was
29790 constant and never written to disk. */
29791 && DECL_ASSEMBLER_NAME (decl))
29793 add_linkage_attr (node->die, decl);
29794 move_linkage_attr (node->die);
29797 deferred_asm_name = NULL;
29799 if (flag_eliminate_unused_debug_types)
29800 prune_unused_types ();
29802 /* Generate separate COMDAT sections for type DIEs. */
29803 if (use_debug_types)
29805 break_out_comdat_types (comp_unit_die ());
29807 /* Each new type_unit DIE was added to the limbo die list when created.
29808 Since these have all been added to comdat_type_list, clear the
29809 limbo die list. */
29810 limbo_die_list = NULL;
29812 /* For each new comdat type unit, copy declarations for incomplete
29813 types to make the new unit self-contained (i.e., no direct
29814 references to the main compile unit). */
29815 for (comdat_type_node *ctnode = comdat_type_list;
29816 ctnode != NULL; ctnode = ctnode->next)
29817 copy_decls_for_unworthy_types (ctnode->root_die);
29818 copy_decls_for_unworthy_types (comp_unit_die ());
29820 /* In the process of copying declarations from one unit to another,
29821 we may have left some declarations behind that are no longer
29822 referenced. Prune them. */
29823 prune_unused_types ();
29826 /* Generate separate CUs for each of the include files we've seen.
29827 They will go into limbo_die_list and from there to cu_die_list. */
29828 if (flag_eliminate_dwarf2_dups)
29830 gcc_assert (limbo_die_list == NULL);
29831 break_out_includes (comp_unit_die ());
29832 limbo_die_node *cu;
29833 while ((cu = limbo_die_list))
29835 limbo_die_list = cu->next;
29836 cu->next = cu_die_list;
29837 cu_die_list = cu;
29841 /* The early debug phase is now finished. */
29842 early_dwarf_finished = true;
29845 /* Reset all state within dwarf2out.c so that we can rerun the compiler
29846 within the same process. For use by toplev::finalize. */
29848 void
29849 dwarf2out_c_finalize (void)
29851 last_var_location_insn = NULL;
29852 cached_next_real_insn = NULL;
29853 used_rtx_array = NULL;
29854 incomplete_types = NULL;
29855 decl_scope_table = NULL;
29856 debug_info_section = NULL;
29857 debug_skeleton_info_section = NULL;
29858 debug_abbrev_section = NULL;
29859 debug_skeleton_abbrev_section = NULL;
29860 debug_aranges_section = NULL;
29861 debug_addr_section = NULL;
29862 debug_macinfo_section = NULL;
29863 debug_line_section = NULL;
29864 debug_skeleton_line_section = NULL;
29865 debug_loc_section = NULL;
29866 debug_pubnames_section = NULL;
29867 debug_pubtypes_section = NULL;
29868 debug_str_section = NULL;
29869 debug_line_str_section = NULL;
29870 debug_str_dwo_section = NULL;
29871 debug_str_offsets_section = NULL;
29872 debug_ranges_section = NULL;
29873 debug_frame_section = NULL;
29874 fde_vec = NULL;
29875 debug_str_hash = NULL;
29876 debug_line_str_hash = NULL;
29877 skeleton_debug_str_hash = NULL;
29878 dw2_string_counter = 0;
29879 have_multiple_function_sections = false;
29880 text_section_used = false;
29881 cold_text_section_used = false;
29882 cold_text_section = NULL;
29883 current_unit_personality = NULL;
29885 early_dwarf = false;
29886 early_dwarf_finished = false;
29888 next_die_offset = 0;
29889 single_comp_unit_die = NULL;
29890 comdat_type_list = NULL;
29891 limbo_die_list = NULL;
29892 file_table = NULL;
29893 decl_die_table = NULL;
29894 common_block_die_table = NULL;
29895 decl_loc_table = NULL;
29896 call_arg_locations = NULL;
29897 call_arg_loc_last = NULL;
29898 call_site_count = -1;
29899 tail_call_site_count = -1;
29900 cached_dw_loc_list_table = NULL;
29901 abbrev_die_table = NULL;
29902 delete dwarf_proc_stack_usage_map;
29903 dwarf_proc_stack_usage_map = NULL;
29904 line_info_label_num = 0;
29905 cur_line_info_table = NULL;
29906 text_section_line_info = NULL;
29907 cold_text_section_line_info = NULL;
29908 separate_line_info = NULL;
29909 info_section_emitted = false;
29910 pubname_table = NULL;
29911 pubtype_table = NULL;
29912 macinfo_table = NULL;
29913 ranges_table = NULL;
29914 ranges_by_label = NULL;
29915 rnglist_idx = 0;
29916 have_location_lists = false;
29917 loclabel_num = 0;
29918 poc_label_num = 0;
29919 last_emitted_file = NULL;
29920 label_num = 0;
29921 tmpl_value_parm_die_table = NULL;
29922 generic_type_instances = NULL;
29923 frame_pointer_fb_offset = 0;
29924 frame_pointer_fb_offset_valid = false;
29925 base_types.release ();
29926 XDELETEVEC (producer_string);
29927 producer_string = NULL;
29930 #include "gt-dwarf2out.h"